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:
xgputakes full advantage of Python type annotations, enabling quality of life features like IDE autocomplete for enum values - Up to date:
xgpuis 99% autogenerated from the headers, and aims to always be in sync with the latestwgpu-nativerelease - Performance:
xgpuis 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.hbut loseWGPUprefixes: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 IntEnums:
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xgpu-0.10.3-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44d57a1fa9300b45c57761dc91fb96262615e7742813186c4773a014f1dd155d
|
|
| MD5 |
a546bb440a43260cf8f56fc498e5a94e
|
|
| BLAKE2b-256 |
08b2a96d5f27f11865df666707f3054310797e8b542a70f2fd62264bd60ac382
|
File details
Details for the file xgpu-0.10.3-cp313-cp313-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp313-cp313-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.13, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e828b3cf1ddcd005b91af303b21fc1ef3aae23aa891edcdced7cf7c37976cb56
|
|
| MD5 |
25c2c5b8ce314736366fa2092606feca
|
|
| BLAKE2b-256 |
3053522a19cb37385152170c4617b6653cc1fb02264210f1be9c8dd38f619e47
|
File details
Details for the file xgpu-0.10.3-cp313-cp313-macosx_10_13_universal2.whl.
File metadata
- Download URL: xgpu-0.10.3-cp313-cp313-macosx_10_13_universal2.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.13, macOS 10.13+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37cb1c8fbf3a64fa43f81e2290a57eeb39fb3518a505a8c1f95139d8ba0516c9
|
|
| MD5 |
283e28c697efda2deb1462f45650282e
|
|
| BLAKE2b-256 |
fefe581a4a13673cc3dcf20157311322d2a9c02d5d4023cfc9ad917636de7c77
|
File details
Details for the file xgpu-0.10.3-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfadb68f82360b1824f5706e2ba7b171817a67f05e2fdcda89304d5751421ca5
|
|
| MD5 |
626cf711f9fe24b816df6c16554fac69
|
|
| BLAKE2b-256 |
8ccd7b6317f5d23b021627334b29459a2b6ce1efa1b0fc890e6212c5ac299135
|
File details
Details for the file xgpu-0.10.3-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f5191b7cff8502f65618c83e5c2ddd399fc745c35247d5c21cdd0b4b6fc20b9
|
|
| MD5 |
a588f3ae784ed60b84be078b9d02e867
|
|
| BLAKE2b-256 |
636acbbad76845b00537c6993decc5b5930e2ba652ac7cc073c8a780f4a0af18
|
File details
Details for the file xgpu-0.10.3-cp312-cp312-macosx_10_13_universal2.whl.
File metadata
- Download URL: xgpu-0.10.3-cp312-cp312-macosx_10_13_universal2.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.12, macOS 10.13+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6a7876f974d6c71bc042e2dfab92a0adbe23471fd9befc84555793a51755af0
|
|
| MD5 |
605b342f6c3acbf2fd9944749a6f24fa
|
|
| BLAKE2b-256 |
6b07128ec5d5d61ab6f24ab09de2c81baca1a930f539dc84fd58579ea791c75b
|
File details
Details for the file xgpu-0.10.3-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e64d2601d6cc49864203fbabf4034c9861d8022e9b080d1800bb2873c69ffe7
|
|
| MD5 |
c4231ac388c4c2c9398829b739707a53
|
|
| BLAKE2b-256 |
e975401e0d3551435d7e27dc386dd6247855ca4eb92416f111c9799fabe93f8b
|
File details
Details for the file xgpu-0.10.3-cp311-cp311-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8535b0831ef141c48600a2d0125415a4b5869972a1c8f5c4b7eea0971dc9f421
|
|
| MD5 |
46134000c670e0ea56d30a07825505f0
|
|
| BLAKE2b-256 |
e19974ebb2f3a457476e6f45c0b70288aa91a51cab0f50fb461a85bdc6bc61de
|
File details
Details for the file xgpu-0.10.3-cp311-cp311-macosx_10_13_universal2.whl.
File metadata
- Download URL: xgpu-0.10.3-cp311-cp311-macosx_10_13_universal2.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.11, macOS 10.13+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
945f859e308a14c5ea599bfa6056894b32a56957b4fd556f9a14ff928d497e7b
|
|
| MD5 |
ca4c933cabc046d59574c379ce0ca1be
|
|
| BLAKE2b-256 |
016215248d4614fa4d8f2f429508d912e511790eccb6b06873ea2c912504d7ed
|
File details
Details for the file xgpu-0.10.3-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbc4195cc081a68f806acc9269c2b1b43c1612eb605a934c53774b4eda6164f5
|
|
| MD5 |
e8513e1a904637ecf8c717f235f573f3
|
|
| BLAKE2b-256 |
eaa0bcf8135eb2fd2ff80594d52a0a6e5b3e6ce114419c66b6153214f83da348
|
File details
Details for the file xgpu-0.10.3-cp310-cp310-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp310-cp310-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6c1c5ac23e7ba25ad59a12688f7a14552bf9a7973a9500bfd641073fdcab20b
|
|
| MD5 |
927cab04070c7492167a8a5733ec3bad
|
|
| BLAKE2b-256 |
ea70b16d6e4b15c3637e2c2f080be67e0145ecac327a5fea21c6fcc63af81d47
|
File details
Details for the file xgpu-0.10.3-cp310-cp310-macosx_10_13_universal2.whl.
File metadata
- Download URL: xgpu-0.10.3-cp310-cp310-macosx_10_13_universal2.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.10, macOS 10.13+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3db3520b25b606fb9a015fb9146cd7d0d65c7187c34dffed831e096765af2cd6
|
|
| MD5 |
66e862a8b8dabfde401a77a55fc22c68
|
|
| BLAKE2b-256 |
0802eb4791d6f3f8900db3408ec0e50ac34bbf4b9aec66990a18695133cf7cde
|
File details
Details for the file xgpu-0.10.3-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4b1a8034f178c55ed6d7c232503b967f66a43bc199ef2f5226f8c6d693fead4
|
|
| MD5 |
27495454706635e8d08fe36717399309
|
|
| BLAKE2b-256 |
c4210d680a788c01d20a4c31a39e8dfd839933ec7d42bc18a6e3df954b564671
|
File details
Details for the file xgpu-0.10.3-cp39-cp39-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp39-cp39-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.9, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42179c067defa2b38a3ffb0062e51f556002f75f1fe797d5b36bb9b0bc423991
|
|
| MD5 |
1039e9c325d0efc5ced2bd53fe8590a8
|
|
| BLAKE2b-256 |
d8f0be19e9e5b2f144a3b9ac4b0cfa7421af4a2e6c47040e2cbf3f847afd9468
|
File details
Details for the file xgpu-0.10.3-cp39-cp39-macosx_10_13_universal2.whl.
File metadata
- Download URL: xgpu-0.10.3-cp39-cp39-macosx_10_13_universal2.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.9, macOS 10.13+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30e9f9f53ef695059b4c503bb734248954709e674b84adafdb516e7acafaf937
|
|
| MD5 |
84b85b56c46c4b43f7fd5e04bde8feb2
|
|
| BLAKE2b-256 |
2318590d618fc4b0a705e74828a970da2712c1d89a4e8ce7f6583219abece70b
|
File details
Details for the file xgpu-0.10.3-cp38-cp38-win_amd64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6923414a1541c2e8e05d703b2e1aa8af8e82b6affbe3889b8aacd6f9118d6318
|
|
| MD5 |
7ce42080a3a881b307e4ec38dabbe007
|
|
| BLAKE2b-256 |
8f1fab3fd23ee82a41ad485afa22f961ca24f4e6c0c1251e2f0bd5d65c6ba7d8
|
File details
Details for the file xgpu-0.10.3-cp38-cp38-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp38-cp38-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.8, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8843bc1ed74d253dfe5f6a0755845145a123710df6a0f1c6cfd81d4bdcf72f70
|
|
| MD5 |
2e40cd79a68ec6e3fa4443a3dd8ced43
|
|
| BLAKE2b-256 |
df564aad2e773ef611de9c2503a0f289d7140f1fc7a8b04d35d4d2497ad7731e
|
File details
Details for the file xgpu-0.10.3-cp38-cp38-macosx_10_13_universal2.whl.
File metadata
- Download URL: xgpu-0.10.3-cp38-cp38-macosx_10_13_universal2.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.8, macOS 10.13+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33b5cf5cd1ee5b647c4a243c8a814b9ae0a4d4edda32dcdf809571d0851a4522
|
|
| MD5 |
1bbe7d695d9d1ea50b4c0abe9b0ba34b
|
|
| BLAKE2b-256 |
da834bafcf1ba696a33e0aafe4ac4bd6c27eaf8a20dcb5be304fe9731ec85345
|
File details
Details for the file xgpu-0.10.3-cp37-cp37m-win_amd64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 3.1 MB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbde00fe5f5e800f5e70f49f217ce179278c07571fc5427e4966d212c7524c27
|
|
| MD5 |
362dd54631cd73ea76cf735371440117
|
|
| BLAKE2b-256 |
b386ccb25ca12f75204a5af4f95c39d4f6cf46207c5c45e7da3fc17437c3dd7e
|
File details
Details for the file xgpu-0.10.3-cp37-cp37m-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: xgpu-0.10.3-cp37-cp37m-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.7m, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d0f5161719b6cc47948b5cbc6fd956f2eb877be4b6cc434d119b35ee925972a
|
|
| MD5 |
90d2bb3e9b2d1e7a7a8cb01b1761af50
|
|
| BLAKE2b-256 |
1f6d0498aabe92cc414f351fb95c29d4ad60ebb254956f8a2083c7611df52014
|