Skip to main content

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 latest wgpu-native release
  • Performance: xgpu is substantially faster than wgpu

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 lose WGPU 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

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 .

Metadata

Release files for xgpu 0.10.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for xgpu 0.10.3
File
xgpu-0.10.3-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
xgpu-0.10.3-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details
xgpu-0.10.3-cp313-cp313-macosx_10_13_universal2.whl CPython 3.13 CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64) Details
xgpu-0.10.3-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
xgpu-0.10.3-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details
xgpu-0.10.3-cp312-cp312-macosx_10_13_universal2.whl CPython 3.12 CPython 3.12 macOS 10.13+ universal2 (ARM64, x86-64) Details
xgpu-0.10.3-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
xgpu-0.10.3-cp311-cp311-manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64 Details
xgpu-0.10.3-cp311-cp311-macosx_10_13_universal2.whl CPython 3.11 CPython 3.11 macOS 10.13+ universal2 (ARM64, x86-64) Details
xgpu-0.10.3-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
xgpu-0.10.3-cp310-cp310-manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64 Details
xgpu-0.10.3-cp310-cp310-macosx_10_13_universal2.whl CPython 3.10 CPython 3.10 macOS 10.13+ universal2 (ARM64, x86-64) Details
xgpu-0.10.3-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
xgpu-0.10.3-cp39-cp39-manylinux_2_28_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.28+ x86-64 Details
xgpu-0.10.3-cp39-cp39-macosx_10_13_universal2.whl CPython 3.9 CPython 3.9 macOS 10.13+ universal2 (ARM64, x86-64) Details
xgpu-0.10.3-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
xgpu-0.10.3-cp38-cp38-manylinux_2_28_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.28+ x86-64 Details
xgpu-0.10.3-cp38-cp38-macosx_10_13_universal2.whl CPython 3.8 CPython 3.8 macOS 10.13+ universal2 (ARM64, x86-64) Details
xgpu-0.10.3-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
xgpu-0.10.3-cp37-cp37m-manylinux_2_28_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.28+ x86-64 Details

Total release size: 71.9 MB

Release files / xgpu-0.10.3-cp313-cp313-win_amd64.whl

Download URL xgpu-0.10.3-cp313-cp313-win_amd64.whl
Size 3.1 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
44d57a1fa9300b45c57761dc91fb96262615e7742813186c4773a014f1dd155d
BLAKE2b-256 checksum
How to use checksums
08b2a96d5f27f11865df666707f3054310797e8b542a70f2fd62264bd60ac382
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL xgpu-0.10.3-cp313-cp313-manylinux_2_28_x86_64.whl
Size 3.3 MB
Tags CPython 3.13 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
e828b3cf1ddcd005b91af303b21fc1ef3aae23aa891edcdced7cf7c37976cb56
BLAKE2b-256 checksum
How to use checksums
3053522a19cb37385152170c4617b6653cc1fb02264210f1be9c8dd38f619e47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp313-cp313-macosx_10_13_universal2.whl

Download URL xgpu-0.10.3-cp313-cp313-macosx_10_13_universal2.whl
Size 4.4 MB
Tags CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
37cb1c8fbf3a64fa43f81e2290a57eeb39fb3518a505a8c1f95139d8ba0516c9
BLAKE2b-256 checksum
How to use checksums
fefe581a4a13673cc3dcf20157311322d2a9c02d5d4023cfc9ad917636de7c77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp312-cp312-win_amd64.whl

Download URL xgpu-0.10.3-cp312-cp312-win_amd64.whl
Size 3.1 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
cfadb68f82360b1824f5706e2ba7b171817a67f05e2fdcda89304d5751421ca5
BLAKE2b-256 checksum
How to use checksums
8ccd7b6317f5d23b021627334b29459a2b6ce1efa1b0fc890e6212c5ac299135
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL xgpu-0.10.3-cp312-cp312-manylinux_2_28_x86_64.whl
Size 3.3 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
2f5191b7cff8502f65618c83e5c2ddd399fc745c35247d5c21cdd0b4b6fc20b9
BLAKE2b-256 checksum
How to use checksums
636acbbad76845b00537c6993decc5b5930e2ba652ac7cc073c8a780f4a0af18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp312-cp312-macosx_10_13_universal2.whl

Download URL xgpu-0.10.3-cp312-cp312-macosx_10_13_universal2.whl
Size 4.4 MB
Tags CPython 3.12 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
c6a7876f974d6c71bc042e2dfab92a0adbe23471fd9befc84555793a51755af0
BLAKE2b-256 checksum
How to use checksums
6b07128ec5d5d61ab6f24ab09de2c81baca1a930f539dc84fd58579ea791c75b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp311-cp311-win_amd64.whl

Download URL xgpu-0.10.3-cp311-cp311-win_amd64.whl
Size 3.1 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
1e64d2601d6cc49864203fbabf4034c9861d8022e9b080d1800bb2873c69ffe7
BLAKE2b-256 checksum
How to use checksums
e975401e0d3551435d7e27dc386dd6247855ca4eb92416f111c9799fabe93f8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp311-cp311-manylinux_2_28_x86_64.whl

Download URL xgpu-0.10.3-cp311-cp311-manylinux_2_28_x86_64.whl
Size 3.3 MB
Tags CPython 3.11 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
8535b0831ef141c48600a2d0125415a4b5869972a1c8f5c4b7eea0971dc9f421
BLAKE2b-256 checksum
How to use checksums
e19974ebb2f3a457476e6f45c0b70288aa91a51cab0f50fb461a85bdc6bc61de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp311-cp311-macosx_10_13_universal2.whl

Download URL xgpu-0.10.3-cp311-cp311-macosx_10_13_universal2.whl
Size 4.4 MB
Tags CPython 3.11 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
945f859e308a14c5ea599bfa6056894b32a56957b4fd556f9a14ff928d497e7b
BLAKE2b-256 checksum
How to use checksums
016215248d4614fa4d8f2f429508d912e511790eccb6b06873ea2c912504d7ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp310-cp310-win_amd64.whl

Download URL xgpu-0.10.3-cp310-cp310-win_amd64.whl
Size 3.1 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
dbc4195cc081a68f806acc9269c2b1b43c1612eb605a934c53774b4eda6164f5
BLAKE2b-256 checksum
How to use checksums
eaa0bcf8135eb2fd2ff80594d52a0a6e5b3e6ce114419c66b6153214f83da348
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp310-cp310-manylinux_2_28_x86_64.whl

Download URL xgpu-0.10.3-cp310-cp310-manylinux_2_28_x86_64.whl
Size 3.3 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
d6c1c5ac23e7ba25ad59a12688f7a14552bf9a7973a9500bfd641073fdcab20b
BLAKE2b-256 checksum
How to use checksums
ea70b16d6e4b15c3637e2c2f080be67e0145ecac327a5fea21c6fcc63af81d47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp310-cp310-macosx_10_13_universal2.whl

Download URL xgpu-0.10.3-cp310-cp310-macosx_10_13_universal2.whl
Size 4.4 MB
Tags CPython 3.10 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
3db3520b25b606fb9a015fb9146cd7d0d65c7187c34dffed831e096765af2cd6
BLAKE2b-256 checksum
How to use checksums
0802eb4791d6f3f8900db3408ec0e50ac34bbf4b9aec66990a18695133cf7cde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp39-cp39-win_amd64.whl

Download URL xgpu-0.10.3-cp39-cp39-win_amd64.whl
Size 3.1 MB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
a4b1a8034f178c55ed6d7c232503b967f66a43bc199ef2f5226f8c6d693fead4
BLAKE2b-256 checksum
How to use checksums
c4210d680a788c01d20a4c31a39e8dfd839933ec7d42bc18a6e3df954b564671
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp39-cp39-manylinux_2_28_x86_64.whl

Download URL xgpu-0.10.3-cp39-cp39-manylinux_2_28_x86_64.whl
Size 3.3 MB
Tags CPython 3.9 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
42179c067defa2b38a3ffb0062e51f556002f75f1fe797d5b36bb9b0bc423991
BLAKE2b-256 checksum
How to use checksums
d8f0be19e9e5b2f144a3b9ac4b0cfa7421af4a2e6c47040e2cbf3f847afd9468
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp39-cp39-macosx_10_13_universal2.whl

Download URL xgpu-0.10.3-cp39-cp39-macosx_10_13_universal2.whl
Size 4.4 MB
Tags CPython 3.9 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
30e9f9f53ef695059b4c503bb734248954709e674b84adafdb516e7acafaf937
BLAKE2b-256 checksum
How to use checksums
2318590d618fc4b0a705e74828a970da2712c1d89a4e8ce7f6583219abece70b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp38-cp38-win_amd64.whl

Download URL xgpu-0.10.3-cp38-cp38-win_amd64.whl
Size 3.1 MB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
6923414a1541c2e8e05d703b2e1aa8af8e82b6affbe3889b8aacd6f9118d6318
BLAKE2b-256 checksum
How to use checksums
8f1fab3fd23ee82a41ad485afa22f961ca24f4e6c0c1251e2f0bd5d65c6ba7d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp38-cp38-manylinux_2_28_x86_64.whl

Download URL xgpu-0.10.3-cp38-cp38-manylinux_2_28_x86_64.whl
Size 3.3 MB
Tags CPython 3.8 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
8843bc1ed74d253dfe5f6a0755845145a123710df6a0f1c6cfd81d4bdcf72f70
BLAKE2b-256 checksum
How to use checksums
df564aad2e773ef611de9c2503a0f289d7140f1fc7a8b04d35d4d2497ad7731e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp38-cp38-macosx_10_13_universal2.whl

Download URL xgpu-0.10.3-cp38-cp38-macosx_10_13_universal2.whl
Size 4.4 MB
Tags CPython 3.8 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
33b5cf5cd1ee5b647c4a243c8a814b9ae0a4d4edda32dcdf809571d0851a4522
BLAKE2b-256 checksum
How to use checksums
da834bafcf1ba696a33e0aafe4ac4bd6c27eaf8a20dcb5be304fe9731ec85345
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp37-cp37m-win_amd64.whl

Download URL xgpu-0.10.3-cp37-cp37m-win_amd64.whl
Size 3.1 MB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
cbde00fe5f5e800f5e70f49f217ce179278c07571fc5427e4966d212c7524c27
BLAKE2b-256 checksum
How to use checksums
b386ccb25ca12f75204a5af4f95c39d4f6cf46207c5c45e7da3fc17437c3dd7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release files / xgpu-0.10.3-cp37-cp37m-manylinux_2_28_x86_64.whl

Download URL xgpu-0.10.3-cp37-cp37m-manylinux_2_28_x86_64.whl
Size 3.3 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
6d0f5161719b6cc47948b5cbc6fd956f2eb877be4b6cc434d119b35ee925972a
BLAKE2b-256 checksum
How to use checksums
1f6d0498aabe92cc414f351fb95c29d4ad60ebb254956f8a2083c7611df52014
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.11

Release history Release notifications | RSS feed

This release

0.10.3 This release

20 release files

0.9.0

25 release files

0.6.6

25 release files

0.6.5

25 release files

0.6.4

25 release files

0.6.3

25 release files

0.6.2

25 release files

0.6.1

30 release files

0.6.0

30 release files

0.5.1

30 release files

0.5.0

30 release files

0.4.0

30 release files

0.3.0

30 release files

0.2.0

30 release files

0.1.1

30 release files

0.1.0

30 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page