No project description provided
Project description
xgpu
xgpu
is an agressively typed, red-squiggle-free Python binding
of wgpu-native, autogenerated from
the C headers to always track the latest upstream release.
Not 'production ready'.
Install
Wheels are built for Mac (x86 only), Windows, and Linux for Python 3.7+:
pip install xgpu
Motivation
The motivation for this project compared to the other, currently-existing,
semi-mature wgpu_native binding wgpu-py
is very simple: I would like to enable typechecking and not get red squiggles
everywhere. Also, I would like the Python bindings to update as soon as possible
after upstream wgpu_native has a new release. xgpu
addresses the first by
being fully type-hinted and red-squiggle-free, and the second by being
automatically generated from the raw upstream C headers.
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
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.1.1-pp310-pypy310_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d242148b0645604d7e9ba7332bc3101cc76d02159be2da2f1dd5814654794750 |
|
MD5 | b9368a2efcce3d6165c62d31e30f0490 |
|
BLAKE2b-256 | 14109dae6d3cfc91d878f6a20af0fdfff0ed7d47ba0e089669c692cfdb78875f |
Hashes for xgpu-0.1.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee0463bcdd2d8c4f92b092da11c09e6c8d49b08b7ed9d8df9fdc6c0cb4021c69 |
|
MD5 | f84d67cb58768a2a8bee345de01a7a93 |
|
BLAKE2b-256 | bce4ecb4eaa61458d5333b6ba654a2510afdd3a11a61fdc4bf1d174713c3bc83 |
Hashes for xgpu-0.1.1-pp310-pypy310_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a02f359ee610bcf8a53606b7d6796b3310b29c5742c4ea79ecca43b4c595d36 |
|
MD5 | 67974a4b63d9a6ad2708bb81d69e936a |
|
BLAKE2b-256 | e0e758dfada528c0b06193a67d4b73b9d2e52be1ec298cb59439a503e204771f |
Hashes for xgpu-0.1.1-pp39-pypy39_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 027dcb96eda21f9c3fae55d86194fa79bbf8ae22580db2b0c08fd07bcd2050f0 |
|
MD5 | af0cf2e3bf146b9bce652f080106227c |
|
BLAKE2b-256 | bd7051f177a288c2193fadeba5f21c893edf03648c62b1b3a720d3d10985e5ff |
Hashes for xgpu-0.1.1-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1147a06d4363609baa7a4712fa71ec093286afc73dac07bce98d36796a2bad58 |
|
MD5 | 42d4862c607ddadd38357d8f457b8068 |
|
BLAKE2b-256 | b4d2eb0dc733c1186d24e7f9b629bf363f076d0bebccc3e48a7015530f458a81 |
Hashes for xgpu-0.1.1-pp39-pypy39_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc677793d7114355bdaa2e39eb6907db6257013b6e656babaeed00b99ab42825 |
|
MD5 | e9eec0c2a4107fef153af1ce7a2e2ba8 |
|
BLAKE2b-256 | 2761604399d9acd3b32203f07ed15e7756b1e8aa2dddbf7a9baf5500ecf937b0 |
Hashes for xgpu-0.1.1-pp38-pypy38_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97323017486f2f27039f534a5ed1ebd8eebbbe7eaa40bc953772ba858d619719 |
|
MD5 | be0431d65d2fc7db75c5046df648843e |
|
BLAKE2b-256 | 35486bd81249fc5125df0f3ced8e09c02c176c44f9ca7863b09740120ec6d289 |
Hashes for xgpu-0.1.1-pp38-pypy38_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ce7ca4d2b7c32b73272d00d9dc7e0889b31f96e0a9b7976187678967932874e |
|
MD5 | 371b7496aea2a6009c16834efd61ca2c |
|
BLAKE2b-256 | 95a89cab8891474e2145163418aff6b20e38cbbde32ae9b19abd0b07042eb3a7 |
Hashes for xgpu-0.1.1-pp38-pypy38_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87c374bb9cdb3b59f356061fd2481bd3bb7134f4e1a778ce551c6a09d9db12e5 |
|
MD5 | a03b5b3ccbcf812f3d068c88c6a0c2b6 |
|
BLAKE2b-256 | a0f67c264fed4cd9bd0c2499517d9b35fc6c84ce22050ae1469e10bc2b64bc6f |
Hashes for xgpu-0.1.1-pp37-pypy37_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13cb29f2a25dafa6a7f90e60704f99a740ea8febe9aa7dd33197f74bb2ed8405 |
|
MD5 | e989456eb7301b41cd071cce8ac75c86 |
|
BLAKE2b-256 | 216c67c6187ac8d3013b6296457f6eb4f375553c7c5ce7b2b2747273c14d5fbe |
Hashes for xgpu-0.1.1-pp37-pypy37_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6353666f9448fdf282e8e03a4ded7acd5bf263614777baa32b9ac00ed9548b02 |
|
MD5 | 3b7ea5dc62b440d9a0a6fe3a384d1c4a |
|
BLAKE2b-256 | d8d96ec301005bd340e1865efd7e8ef3b958d09d99fbdd4529d1975d80ae06ff |
Hashes for xgpu-0.1.1-pp37-pypy37_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4003b4b60d00a97a610ffcba7921c49930b1dec7881bf1905cd249be0e894f33 |
|
MD5 | 5ccce23f03efddfaab14b789e47ea481 |
|
BLAKE2b-256 | 5187cd7d84d08a4fb802c1d5c22daeafa85718c72469b50ea6bc001e805494b1 |
Hashes for xgpu-0.1.1-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15087bb518e2aa0b63981e737f33b11b757f833d074e48685b1e191495431c14 |
|
MD5 | 5bbffd1b508214dc8c99fe7e0e263b39 |
|
BLAKE2b-256 | 36e38a4271e448920d019e10f1338f944a0c60ed94645b35a2f1e70a79a9a790 |
Hashes for xgpu-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c19546625c21c484b15219196b285d05e5f215940910e3b981ba1dc66085d3c8 |
|
MD5 | 7ce585e106e17cec90735d2f4a607d94 |
|
BLAKE2b-256 | 2aef77851de912d6d46995501f43419e4c3c87a0a377d73ba7d55ab2a48cd62a |
Hashes for xgpu-0.1.1-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1da5e3839af5f29083d7c69912b8bcf5b12845e4228b413f431ef472efe2b564 |
|
MD5 | 0eaa02ce20bed6fa2fdfb33ddc7ebb15 |
|
BLAKE2b-256 | 5337207c9e28e2418479ed43f523ec9e81e579749efbe0caa5197532d3bb736c |
Hashes for xgpu-0.1.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a94216f73b4997d0165727ba2c140ea73e771cedd469cf6c7ad90e07d006c8d |
|
MD5 | 171679dc01c2b7ebbd8a7e0ebc5487cd |
|
BLAKE2b-256 | 6418cb98aae7249a540fdaa5fe9068bd368bd8ac31411be9f624eaf25d3e4c79 |
Hashes for xgpu-0.1.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37c6a5b8b8e0f605852735b7266c5a48ebc2d93ed3af57187a139c66c5465acb |
|
MD5 | eb3aa3847e67470a3efd6a76d7e098f6 |
|
BLAKE2b-256 | e01cfc8db347ce9f51b1314a284fbf4e423927ba6b3e812c97c13e9fccaa0b83 |
Hashes for xgpu-0.1.1-cp311-cp311-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f430a462376716b9e384f37cdad39cea1548ac69c50daf232f3fdcc89b0c146d |
|
MD5 | be2d313b78f3308a6c758403c2a09558 |
|
BLAKE2b-256 | c2e56d0841a44036ef74839a8284d40edb03c23be3421ba325e37975ac052524 |
Hashes for xgpu-0.1.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5be19ee96dbb2fc0ab62a5bbf12dceff6bf91b17a46251e4a1dad8b97629ecb |
|
MD5 | 900f41e8fd3dda44eac199ad9d5d5a23 |
|
BLAKE2b-256 | afa49f05fe6316df56ee66fd70aed64e8466cc6f88592276d4b7373a9205ef22 |
Hashes for xgpu-0.1.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1459526dfc3ae5407f5f16b2b42153192f53b2418d13daeacc276935a16ac08 |
|
MD5 | 5f87f347bbc368a6135a1c8efda55c3c |
|
BLAKE2b-256 | 850675bfc24d3f6ea5e72cb52ab38b124cebb610cba2c00fc954ba78d71b74ab |
Hashes for xgpu-0.1.1-cp310-cp310-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc7ed644481ea587d6239e655e19fe2fbbd11e10f1835f7b9457e29d0823e3a9 |
|
MD5 | bf096fb82cc084a71cf2d0c08a86338b |
|
BLAKE2b-256 | 06a69e4d268abb112211ebfa877d1e4fb1307c92a4ee4f3d0073784df0995708 |
Hashes for xgpu-0.1.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b51e20466d9f85346695e6f2badbe0094e7ff53faaff33b8d564b693bfeb746 |
|
MD5 | 2cf2c2a4ba26d6027de65e66d71c288e |
|
BLAKE2b-256 | ef796105321dfecc78e8923e0c6b1737ab095bea1edb8b44ddab7a339100fac2 |
Hashes for xgpu-0.1.1-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27fa542e8286eb17e0b0a22910045133df108377d3cd9bb2a1f8de29cbdc8bfe |
|
MD5 | 852ae9c5539850de091348f1cefc367c |
|
BLAKE2b-256 | dc1b5c8b0c219101dd743877a03f7b98fefac2045772827450afab52387cfab3 |
Hashes for xgpu-0.1.1-cp39-cp39-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99ffec25a7904caffc40201eae5554fffdc25aa0ad1966e91147c5a8edae03c7 |
|
MD5 | c3930d7afcfb501068d8bb36d132afe1 |
|
BLAKE2b-256 | db37cfc1ed37e572cbd194c56e9e5be64c83372c33afd3697ffdb1d67a1ca81a |
Hashes for xgpu-0.1.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e42f4996f7d66de50725fd657b4d1a9aae3550fa006be138bfb3078f9fafff6 |
|
MD5 | b46a6f55418a709df3d3cb1c7e7b0d78 |
|
BLAKE2b-256 | ca1b724833560805f9c6ff4faa117540dfcae79dfa3a33da7692062765062f10 |
Hashes for xgpu-0.1.1-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e9a35ce2dfb99c6c3d6dc4e1f8b35edb1a8c2be29d47fe0781afef4d59b54c9 |
|
MD5 | ab55de02bf73032fb7d47ada7ee1b0bc |
|
BLAKE2b-256 | 2ba4ca0598daa572b4206e34de369ddc1d523623004b7aef06e268c3ad179e6d |
Hashes for xgpu-0.1.1-cp38-cp38-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08c155ba47645a30f2cee2a48fcf2a1f0e8dc17004ccccc1100addaf8e6715c8 |
|
MD5 | a1e11dfeb8d245962c2035635771f9a9 |
|
BLAKE2b-256 | 4ed89062533a7c2192df33ff9291b8f68d72a5f4ee22d3b8cc25e53c439ead6d |
Hashes for xgpu-0.1.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4a6912bf8881bd5c33a50ffdab6ecf778d4f65f6825e27c6cb47a5fedf3dbaf |
|
MD5 | 1307f49a13255cb368f11ceaee76a5de |
|
BLAKE2b-256 | a05e17fa3ce294806da4322d0ceea33fb1326b30e64beee59770ec1715fdd9bb |
Hashes for xgpu-0.1.1-cp37-cp37m-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f575ac3077b3aae8805e28f0c644efd68565c3790600460d4b8f120c5d83ea0f |
|
MD5 | 7dbba59301b8aecd9127e58e317f4c0e |
|
BLAKE2b-256 | c056b4349a6099a94222d1e9baa591a51317d8e4e59d662d72b58eb8361080a2 |
Hashes for xgpu-0.1.1-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1715386b4dc76e997429e7dc626af941ee88ac7f6f5c015b9f6e45dc34c6634c |
|
MD5 | 78d410bee1f61c98b0cdafc1fd2a4fe6 |
|
BLAKE2b-256 | 10a440cb0c456323472d419a412d080b8745dc73ba58a1e9ab56fca18b5e5414 |