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.5.0-pp310-pypy310_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7aaeac6a63d04647af8b8c6194b2ee646dac411ecaaa2c736e01b4edeefb85c |
|
MD5 | b232e30acc6e8980dcffa9a6bc5e52cf |
|
BLAKE2b-256 | 3a3d6fccd493be67cec4291bb474da9ce3a13e4341032dbdb87d08cfdde31990 |
Hashes for xgpu-0.5.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7380f2ef4f6a4a7b67c88ea1810973bd346705ec296c90e42f13d5b6ebe02a3a |
|
MD5 | c216bde9088eb2d240e3fab47a84f680 |
|
BLAKE2b-256 | 23194f6b6a30e8050ab411a15c5f68805e4512792194f25677b436a2f0140980 |
Hashes for xgpu-0.5.0-pp310-pypy310_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fecc83eb77955a6c5a1f0d800e4c5467b8549b638bbf28acfb1d42adc434797 |
|
MD5 | 3eefbf75e51ca03f99442f0dc860249d |
|
BLAKE2b-256 | 33af76dcf932bca6dcab63f7ea2cefabf781c595fcc9045b6d308b18213f38f9 |
Hashes for xgpu-0.5.0-pp39-pypy39_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75e5c2a890bda0d227e042a5874ebcf61ac230bbf579e9ea669f7b253eb57f05 |
|
MD5 | db78eec9f5996c537622e0bc9d370b54 |
|
BLAKE2b-256 | 0ae696f4fc0734533135dc27ac82eb5db4506c75a46d9b26b5165c03d5269862 |
Hashes for xgpu-0.5.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af60f6dbcf9dec286552058fe91cef5ef66f497ccdb6deaace3afcececf161fc |
|
MD5 | f29b708a8face43bbea886eb5e1fadd6 |
|
BLAKE2b-256 | 246ed6e2a7b14adda653a2bc3caa184bdb039b545c289eb9531754e36128b043 |
Hashes for xgpu-0.5.0-pp39-pypy39_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72f9d974cd859dcbf7e25de7a425319254790827395998e4e5d4ef6e5ca5832a |
|
MD5 | 3fc92d761648c518850379c879e64f0e |
|
BLAKE2b-256 | f952a132e2ceb992f2ae0369d345d0ad68fe3e966018106043e2e1ea906bc42b |
Hashes for xgpu-0.5.0-pp38-pypy38_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 849c90aebe59a2b2b54849d3ffe9deec4f0526a24c6d1d083a0df4533304da85 |
|
MD5 | 19b6787751bffb810df3fae49df87ca8 |
|
BLAKE2b-256 | 0eb95bb9ff7e52cd9211ec7b2dd51942ad43f528f6620c66018ea0c9e7dfc499 |
Hashes for xgpu-0.5.0-pp38-pypy38_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d561c632a350edb3d857844fc3b3efe65931e8222dfafba6449d60e321589965 |
|
MD5 | 937cff8087fd093c0eebb78c7d14cc5f |
|
BLAKE2b-256 | 325362bf75e2a1cb72a6c028590570e3c583aae9a034ad5e4f617160007c6189 |
Hashes for xgpu-0.5.0-pp38-pypy38_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35fb7c86c8f799a76142a1131f58c3d1c66344930302a61524e10a8f2f80a4aa |
|
MD5 | 5852aaf1cf4171fc63ae8acdaa333dc1 |
|
BLAKE2b-256 | fa08763114bd2bad47034b5d063d4707d2747f6fb525897a00c02f1ff787ed48 |
Hashes for xgpu-0.5.0-pp37-pypy37_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5df51a40da97279a20838e66534a1a192c68a9fe7917eecf6d32c568ad372c92 |
|
MD5 | 1ff6622dd3dc6a0aea53272d62f22ee9 |
|
BLAKE2b-256 | 40adf3bce804300bded3cf260fc8811cd4ee8feef5fe14680e6349cf5a7bbbc7 |
Hashes for xgpu-0.5.0-pp37-pypy37_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fad6b57cf4731436cfa33f42f89e2492bfd2062b2d86402bef24dc11cc55a783 |
|
MD5 | 2adebacbdab06f07bb518cce041608bf |
|
BLAKE2b-256 | 02ac9b0139d84bb80bc6c0ccc2821da6238d1a142760487bea42e0422740f38c |
Hashes for xgpu-0.5.0-pp37-pypy37_pp73-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6299cf40532f5ca108119afb6991cc9eac43fccf60839744f3a6d7aae5c8f6e1 |
|
MD5 | 357640e67a3b7353c323707841411537 |
|
BLAKE2b-256 | 0bef919b2946488871195de19c8cf92711176affd3c863aa3dab5ec72d518859 |
Hashes for xgpu-0.5.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51cc674084b0d4a93aff44692bcd0114e26029b3bd17db3e0fd37e3a437f670b |
|
MD5 | d7f0dbd1ceb4907b15e5a898b0e6c221 |
|
BLAKE2b-256 | 875d61114addf80e268fbd96209957e604471c4a1cc9071d8b68f06e8c53c2a3 |
Hashes for xgpu-0.5.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 165898f2489c65ddde47fefe21d723c27b88c57ea9f1c930f84e1d2ac478d223 |
|
MD5 | c7a6d003b3c3a0ce4c44698a97dca961 |
|
BLAKE2b-256 | 10a9aebbccabc083bd6af6a1fef47dbf05c7a114a09740048ee47c81346d5e40 |
Hashes for xgpu-0.5.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75cd3ce31b275b2a183f37c010e85ab1c9fd0de93600901bb1f333a7ea258d2b |
|
MD5 | 7b15e756b5cf10993651a6de29a891f1 |
|
BLAKE2b-256 | 2734d65d39ef5901b1aeeea0880f8d2ae20cf8cc7d8791a1abe49e28d5ad4914 |
Hashes for xgpu-0.5.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c811f135ea34174f99141031c6d538d5c372757ea82915a2f6be5fa87f4560dd |
|
MD5 | 71609bf219412fff0a0740e83d3e5fd9 |
|
BLAKE2b-256 | a13f65fdd1572119fdae7718306e6d7c846095d5dfc2c0a6ec258dfc48975299 |
Hashes for xgpu-0.5.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50adb7edb9f102c59c7cb280b30f970b72f860046ac4e220c3d468ff41a0fa86 |
|
MD5 | 3f50ddfee69c004ef4fc9e9dbc93a5af |
|
BLAKE2b-256 | e36b154a620be31610d5d41ea4685ce3b319d04705c95a887bb8dfdf0c5aeb90 |
Hashes for xgpu-0.5.0-cp311-cp311-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ead630c26f51ff5eac956767df01a68f96af1f4caa13a3e092c7caf4d7cb961a |
|
MD5 | a50e0ce9312045d2026986ea204ad2a7 |
|
BLAKE2b-256 | 5eda3f03cd132c0dc6a32597a0becdd82223a04a488309300b0a569196bfd9ae |
Hashes for xgpu-0.5.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79c174f235b45fc7e9139426ff7e80012938cbd4feba0123e2c2ad83b81ceda8 |
|
MD5 | a0d768e750efe078b2053233a22cbeb0 |
|
BLAKE2b-256 | be634ce172fb894e52f2cb7aa356e45f5b331673629b60ecb604e6513c90bd69 |
Hashes for xgpu-0.5.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34b5a39bd87feea307af185fe8419ac2dcfcb870587f694ee18183cac06f5a0a |
|
MD5 | fe1140aeb12feac03d134d620229d443 |
|
BLAKE2b-256 | 56bfe819da032cf262022df0efa9519e8b0e517be67ec1bf884e35b813c737ba |
Hashes for xgpu-0.5.0-cp310-cp310-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32b2d71352d7aa6dccb500d17c53199e6ae50d7730880afd1014039b06f90f07 |
|
MD5 | 56fa3a7eded197861a0183fffa8b42ed |
|
BLAKE2b-256 | ea6b2f17f7a2f56294dd4ece87873b4ab1230a241319ab827506093379985535 |
Hashes for xgpu-0.5.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2cb2b6d6516ba2d474ba7fe407e1d9c68889bd8110093b8d5ce98046417319a0 |
|
MD5 | bba37cb73f580a64e6560dcf293bb517 |
|
BLAKE2b-256 | d1ba8caaeb2f3930e6171e9aa98f730063ba5f7b0a82278147c925162f0208da |
Hashes for xgpu-0.5.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edb24182fbd983f462505d54cec78594405bab2fc7f35c8559a97e9a9f27d4f7 |
|
MD5 | 7550d3d4c74bf9343cbe228bd6e09aef |
|
BLAKE2b-256 | 5ffafe7470fb3a2474740ea2935fd641af35febc97aa9a44c93391966526a828 |
Hashes for xgpu-0.5.0-cp39-cp39-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ae4c6c4e81cb0e032efe7433f97261a05f2aa178f312d00c42cf0d453429020 |
|
MD5 | 36dd30dd4e4be99b48c5580075a744ed |
|
BLAKE2b-256 | b99be32c7a79d0d6e6c93b6f89d3e67a22fca7a8f890ce10673ab682c7700882 |
Hashes for xgpu-0.5.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6da96091354ad95fad41891ebea630a7dc3d36b818fa4cc90de973901813d03 |
|
MD5 | e8c7f440ed0ab00e348028fe96b55c63 |
|
BLAKE2b-256 | e0a0ef90d65f4035551c17a73321a63912a29a7439e080d8adb3b593b04d6887 |
Hashes for xgpu-0.5.0-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a6e0ad6f57c14ace99b3e40ac72d025385a09f83675895ecf0629f8b75e6e3e |
|
MD5 | 93119b1602c93f5e945e08224ec87d6e |
|
BLAKE2b-256 | 1a4fd93dbd24a3e12d76aeb859b45a4422c2f4dd2d2ceb663e7c353142dc2657 |
Hashes for xgpu-0.5.0-cp38-cp38-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1db23edf25ba8fdaa5a8974ea98e3cc96cc4fb38bbfadd5cc79749dd39b6857 |
|
MD5 | 6ab12f9c7352e130cae757a83b60d462 |
|
BLAKE2b-256 | 29b80d42fd647d97d0b14d1d2f37ef9914174706ca570a2bfb642b5743bdc381 |
Hashes for xgpu-0.5.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a965a8db0159f417d9c00f2bb7ca8777394063b578c413d6d24adf3a7e70a6b0 |
|
MD5 | 64233d8ff2d9962d97a0e1093033368a |
|
BLAKE2b-256 | 30de9d35651da564c04582293549737867ebd044ab188e51f2cb35edb36c6ff2 |
Hashes for xgpu-0.5.0-cp37-cp37m-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74ec4ff76fe039a2f6e70afc4e815a35fbb4e7d538766568e8202d7c483bd16f |
|
MD5 | 5d44dfdd40015442ecc97add74394123 |
|
BLAKE2b-256 | 5eb8d6b1a34aecdadc07dd4ff47a16e0fcc874169c4c8e8efa7d7b5721249277 |
Hashes for xgpu-0.5.0-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b83e829ac8c0adabc5c4d7762bdced77cec62dc083b45b8af4948bcc1bda0bf |
|
MD5 | 373aa7a1d616fd0784ad4ca505fcdcd4 |
|
BLAKE2b-256 | 96c3e722f52e0d9c68957c80780a42be273d2b6a32a82fb8ca2b2fd6e7660bc9 |