No project description provided
Project description
xgpu
xgpu
is an aggressively typed, red-squiggle-free Python binding
of wgpu-native, autogenerated from
the upstream C headers.
Not 'production ready'.
Install
Wheels are built for Mac (x86 only), Windows, and Linux for Python 3.7+:
pip install xgpu
Motivation
Why another webgpu/wgpu_native binding when wgpu-py already exists and is semi-mature?
- Typing:
xgpu
takes full advantage of Python type annotations, enabling quality of life features like IDE autocomplete for enum values - Up to date:
xgpu
is 99% autogenerated from the headers, and aims to always be in sync with the latestwgpu-native
release - Performance:
xgpu
is substantially faster thanwgpu
Conventions/Philosophy
xgpu
is a mostly 1-to-1 binding of webgpu.h
(+wgpu.h
from wgpu-native
).
General name conventions
xgpu
largely tries to maintain the names from webgpu.h
rather than localizing
them into Python's conventions.
- Names keep their formatting from
webgpu.h
but loseWGPU
prefixes:WGPUTextureSampleType
->TextureSampleType
- Fields:
WGPUAdapterProperties.vendorName
->AdapterProperties.vendorName
- Member functions:
wgpuDeviceHasFeature
->Device.hasFeature
- Enum values:
WGPUTextureUsage_CopySrc
->TextureUsage.CopySrc
- Names invalid in Python are prefixed with "_":
WGPUBufferUsage_None
->BufferUsage._None
,WGPUTextureDimension_2D
->TextureDimension._2D
- Names invalid in Python are prefixed with "_":
Struct constructors
webgpu.h
requires constructing various structs, for example WGPUExtent3D
. These can be created in two ways:
# Recommended: create explicit initialized struct (note lowercase name)
extents = xgpu.extent3D(width = 100, height = 100, depthOrArrayLayers = 1)
# Alternative: create 0-initialized struct and then mutate values
extents = xgpu.Extent3D()
extents.width = 100
extents.height = 100
extents.depthOrArrayLayers = 1
Member functions
As a C API, webgpu.h
follows typical C convention for member functions, which is to define
them like:
uint32_t wgpuTextureGetHeight(WGPUTexture texture)
In xgpu
these become genuine member functions, e.g.,
class Texture:
def getHeight(self) -> int
Array arguments / fields
Some webgpu.h
functions and structs take arrays using the convention of passing first
the array item count, and then the array pointer, e.g.,
void wgpuQueueSubmit(WGPUQueue queue, size_t commandCount, WGPUCommandBuffer const * commands)
typedef struct WGPUPipelineLayoutDescriptor {
// ...
size_t bindGroupLayoutCount;
WGPUBindGroupLayout const * bindGroupLayouts;
} WGPUPipelineLayoutDescriptor;
These are translated to take lists:
class Queue:
def submit(self, commands: List[CommandBuffer]])
def pipelineLayoutDescriptor(*, bindGroupLayouts: List["BindGroupLayout"])
Enums and Flags
Enums are translated into IntEnum
s:
mode = xgpu.AddressMode.MirrorRepeat
print(int(mode)) # 2
print(mode.name) # "MirrorRepeat"
mode = xgpu.AddressMode(2)
print(mode.name) # "ClampToEdge"
Some enums are meant to be ORed together into bitflags. These can be combined in the natural way:
usage = xgpu.BufferUsage.MapRead | xgpu.BufferUsage.CopyDst
print(usage) # prints: 9
This works because IntEnums
inherit all the int methods include bitwise
operations; however, this discards the type information.
A slightly more annoying but type-safer way is:
usage = xgpu.BufferUsage.MapRead.asflag() | xgpu.BufferUsage.CopyDst
print(usage) # prints: BufferUsage.MapRead | BufferUsage.CopyDst
You can also create typed flags from bare ints:
usage = xgpu.BufferUsageFlags(0b1001)
print(usage) # prints: BufferUsage.MapRead | BufferUsage.CopyDst
You can test for a particular flag with the python in
operator:
has_map_read = xgpu.BufferUsage.MapRead in mybuffer.getUsage()
Callbacks
Callbacks must be explicitly wrapped in the appropriate callback type:
def my_adapter_cb(status: xgpu.RequestAdapterStatus, gotten: xgpu.Adapter, msg: str):
print(f"Got adapter with msg:'{msg}', status: {status.name}")
cb = xgpu.RequestAdapterCallback(my_adapter_cb)
Chained structs
The webgpu.h
structure chaining convention is represented by ChainedStruct
, whose
constructor takes a list of Chainable
and automatically creates the linked chain.
shader_source = """..."""
shader = device.createShaderModule(
nextInChain=xgpu.ChainedStruct(
[xgpu.shaderModuleWGSLDescriptor(code=shader_source)]
),
hints=[],
)
Byte buffers, void pointers
xgpu
has two translations for void *
: VoidPtr
represents a pointer to
opaque data (e.g., a window handle) while DataPtr
represents a pointer
to a sized data structure (e.g., texture data you want to upload).
For example,
# Note use of VoidPtr.NULL and VoidPtr.raw_cast
surf_desc = xgpu.surfaceDescriptorFromWindowsHWND(
hinstance=xgpu.VoidPtr.NULL,
hwnd=xgpu.VoidPtr.raw_cast(self.window_handle),
)
# DataPtr.wrap can wrap anything supporting the 'buffer' interface
bytedata = bytearray(100)
wrapped = xgpu.DataPtr.wrap(bytedata)
queue.writeBuffer(
buffer=some_buffer,
bufferOffset=0,
data=wrapped
)
# This includes numpy arrays
my_array = np.ones(100, dtype=np.float32)
wrapped = xgpu.DataPtr.wrap(my_array)
Codegen/Local Build
You will need bun to run the codegen. Deno might
work but just go ahead and install bun. You will also need to have
ruff and cffi installed in python (pip install ruff cffi
).
Then:
python codegen/fetch_wgpu_bins.py
bun codegen/generate.ts
cd xgpu
python _build_ext.py
cd ..
pip install .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for xgpu-0.6.5-pp310-pypy310_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cd414f7a4af50b6c386dd0cea4c7a0bea5229feb7468eb7b177053d5a090f24 |
|
MD5 | 1949b01fa6814786bc70038c6447d081 |
|
BLAKE2b-256 | ec069179233acbe7f1b5b4cbb6f82225709c9073b85d7a2773bd2a6bd3e7f1f2 |
Hashes for xgpu-0.6.5-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d1473567d639806c79cae18aab9463e3a006e51f3873baea24054203dcd15d7 |
|
MD5 | 7e0adf2ba148d3dd910439a0dfa81b58 |
|
BLAKE2b-256 | fb4b5f5256c575a952e75907f4527c1bea4c11fbf57463d748b7d5862bc5f532 |
Hashes for xgpu-0.6.5-pp39-pypy39_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da7dd6b0b73db8936d5a4c5f8521c9b7ac20977f8ee36a645647aaaf0d6b9225 |
|
MD5 | eec5a77d2ae3d40e30adc8dbc0165a5d |
|
BLAKE2b-256 | 2ef78099ec082637c75ce6f27d5197008a6e6fff8f1827f0167167fdd468bfab |
Hashes for xgpu-0.6.5-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 557e7e7082262b5657a483cb6f4bda4114f485cbec278674643fcc95772c092f |
|
MD5 | b7f2617dcf50635ba68dea03eeca2505 |
|
BLAKE2b-256 | 09e0fd80fa19bd99e8db652be331e879208f7070132a7b868a213d42fdedd70a |
Hashes for xgpu-0.6.5-pp38-pypy38_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8923a065cd7b7001853da0ea9bc55fe2aca481c5a38885e75e6f96bd925c67d |
|
MD5 | e6afb25f4bea4ac0db99908a89f49c35 |
|
BLAKE2b-256 | bc73f76f03c08e27439023ea2287f8e400ceadeebbe1f24d9a4ac15a5544fd9d |
Hashes for xgpu-0.6.5-pp38-pypy38_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | faa047370bdea9f0c953e52c9698a61dadde6806f365acbfa3433a5b8e71151f |
|
MD5 | 5a26cea43042976b45a3964370cb3fb1 |
|
BLAKE2b-256 | f5b4b798e6518313e4a9a5fafd3de2a6e181b38b77b0b20eb5bae58f7256cb00 |
Hashes for xgpu-0.6.5-pp37-pypy37_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99c1b31efc5f9448a34b8b888b8a0012d264aaa78f05e147abf0e18e9cd0ad79 |
|
MD5 | 0b7e12cfeb2876b497967bad9901a265 |
|
BLAKE2b-256 | 560eeebedd16ce5fc5dd06adba8751e11df4dfa7e9204c6d237903a48b2bbc4c |
Hashes for xgpu-0.6.5-pp37-pypy37_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5361f240af9eaaf164432421a6b5f66488ea7bf2e4c57da92f8588ebecd67e8 |
|
MD5 | d3d94212a34da8332e3b437c558f43f8 |
|
BLAKE2b-256 | c02f0ffa97b5275ce5579287ab2fa917b309a4623170de82e9be2847f5ec14a5 |
Hashes for xgpu-0.6.5-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 338ac5358dfa50d3ed41d064a1e170de4065c8d4c588eb42156f8eb40c13128f |
|
MD5 | 37273fb07f0ae4c7ed270283b5f3784b |
|
BLAKE2b-256 | 0e3a50e1d443445016437264ce2f95c3a5f741745715d0924ae4be887e6865fa |
Hashes for xgpu-0.6.5-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57a8b33a02bd3b939c8877ad7440c744707e4c7a8d7554f7f167b3d8e3aae2ef |
|
MD5 | 6ee74c1cfda33fa18512e800a45f44a4 |
|
BLAKE2b-256 | 48dbd86ac8b94c0c3e0896e385da3c895b300b3ebd9580fd919342ef28f00ac9 |
Hashes for xgpu-0.6.5-cp312-cp312-macosx_10_13_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b91cb366cee1596b564738249e94425431efc4a4ff147b80406b33e46d6a87ba |
|
MD5 | a8c8f331a09435e920f68fe4c997bee6 |
|
BLAKE2b-256 | aa2dbf3be353492186c3b5fcf3f00ef66f9477e3689e697f8809afe21933ef73 |
Hashes for xgpu-0.6.5-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cd60f50b7e8678c849c7a9e9a4aac91a799b6cdca8aadf25e3f204a0cc0e220 |
|
MD5 | 55926bb0be220f24bde8df578e4d89b0 |
|
BLAKE2b-256 | e44cf0f966d0c3157fe22aa5b9216add2741255eb3cf18e775e2aa9e05444721 |
Hashes for xgpu-0.6.5-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b0116de3760ce11185fa298c05477f830863a13b41781bcecdf46ede2af502d |
|
MD5 | 4b0fe755303ad70e7ea909e29ef2cc39 |
|
BLAKE2b-256 | f05a64ef5d03a6d1bd5a718c49c34cc2c8783bb7573d89368a55f7855efd686a |
Hashes for xgpu-0.6.5-cp311-cp311-macosx_10_13_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | deab6efa1496cbfce54a0d7a18f20ca8421be6a43fdfbd5405246b1ee585173e |
|
MD5 | 977717dc212efa0fafcfaac5d9628f9f |
|
BLAKE2b-256 | 5c64179550a2727389005d855d72c1f22b968c54d7dc3cd04ae2908bd9738277 |
Hashes for xgpu-0.6.5-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 775d1cbe53b9ed6839962c31440152083ec7285fdba5f45a1519cdce9ffc5ac0 |
|
MD5 | cca95294e0e347af5ebdb2563c13b52b |
|
BLAKE2b-256 | 62cdb365456328b1e811b3a7f84999162cdeeb62ad0f24b1b9151b0fedab26f3 |
Hashes for xgpu-0.6.5-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffb73e6894c207b5a252bf6698f2e64ebdf6604122a2ce82050121de885b9df7 |
|
MD5 | 37c406d19fc63519b94ab6d1a7788182 |
|
BLAKE2b-256 | d4ae8a944c31a1879d3d9310e69191e6dd4310a25166ee6ffbbd03d048813ed2 |
Hashes for xgpu-0.6.5-cp310-cp310-macosx_10_13_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8739302e05ab1fd662e41daf6638fb3d32a894cff968195c53ba678edbb6a696 |
|
MD5 | bccc667edd0e85e78274a0f7fa713def |
|
BLAKE2b-256 | 11ed8f0318bd345d6ca10dd661bf220153db223f977c111fce8e526b7d034f5c |
Hashes for xgpu-0.6.5-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdde9d19c7650564dd402dbc1261a0ae12932625dae49953f4462f9e4e619482 |
|
MD5 | 907871193d91a36778289ef06f0a62b9 |
|
BLAKE2b-256 | fce72cdc394430b214924592b0b94792ed1cc6cdbc85443906270ae02790a155 |
Hashes for xgpu-0.6.5-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92c9ad06732485471a4765923e18b7f30bfd9b1be035533ebc52391d29921c1d |
|
MD5 | 6fc25bcb12edbccf60f468cf7a686b3e |
|
BLAKE2b-256 | 52f12208206954c6a20f4d77dc3526de04d7543950c65ba23e5c5cfb123b2ca7 |
Hashes for xgpu-0.6.5-cp39-cp39-macosx_10_13_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b5609579dbda31ca0827e8cbba16860bf6a9803a2fee63f831777b8331bd736 |
|
MD5 | b8991d86bb8c64adcf153dd46d2e3dbc |
|
BLAKE2b-256 | 97871692bbb8513ba029a112f85f1c98dd28c036fac596e71c030a0d608cbab6 |
Hashes for xgpu-0.6.5-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cbf56c41c175ed27fd24ab06059fae4912c2ae205605d5ff4c4231863d5d398 |
|
MD5 | 423e3a907d0826c9d2f9aa79ae62023e |
|
BLAKE2b-256 | fa093d20a1ad4bf98ed8fe7da9751e89dc613bb259bf65230dc62ff97f1d3d0a |
Hashes for xgpu-0.6.5-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a0ae557bd5d382065b5f22fe776dc71dd47db7e77807ac9ecd1ab9939c4ca07 |
|
MD5 | ee20c86ed2c1f0878b3b251b4a749eb6 |
|
BLAKE2b-256 | e1682cd0ef0c36cf7dead70415371e6c4fe3260af1880d046217d787fa8b130c |
Hashes for xgpu-0.6.5-cp38-cp38-macosx_10_13_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a93e74646c1f23d554a15efa35460d2b30fd8fc36b48de2293deda619f313a7b |
|
MD5 | eeb79bb586c8619ab09dd02e4a06e693 |
|
BLAKE2b-256 | b27f0d190c7795a8676f4a3558cc5d5aac3bae33c86eb94d37549a2eb7578da5 |
Hashes for xgpu-0.6.5-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e9988a8eefcaa6787d5c2c037353db4285506d36e5ed8e02ecc8a4a86fec4ba |
|
MD5 | 6a76e3ce39c6e743303adb1cbcd3d709 |
|
BLAKE2b-256 | d5bfee4b2ea10ce61a1d787e2ff6c7cd7178ee04fa7c86b909a9421b236593ac |
Hashes for xgpu-0.6.5-cp37-cp37m-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f94ca4a8cba3db492e0c02e7f1341a853bc287e63515a08576569ac2ddf372d |
|
MD5 | 2fe67762a3ea275f5e206afa2b717c57 |
|
BLAKE2b-256 | cc36f6eca0a8c6226f833df12f90cbc4ed8c553c915ca383e5fb164378efee21 |