WGPU

This document describes the wgpu API, which essentially is a Pythonic version of the WebGPU API. It exposes an API for performing operations, such as rendering and computation, on a Graphics Processing Unit.

Note

The WebGPU API is still being developed and occasionally there are backwards incompatible changes. Since we mostly follow the WebGPU API, there may be backwards incompatible changes to wgpu-py too. This will be so until the WebGPU API settles as a standard.

How to read this API

The classes in this API all have a name staring with “GPU”, this helps discern them from flags and enums. These classes are never instantiated directly; new objects are returned by certain methods.

Most methods in this API have no positional arguments; each argument must be referenced by name. Some argument values must be a dict, these can be thought of as “nested” arguments. Many arguments (and dict fields) must be a flag or enum. Flags are integer bitmasks that can be orred together. Enum values are strings in this API. Some arguments have a default value. Most do not.

Differences from WebGPU

This API is derived from the WebGPU spec, but differs in a few ways. For example, methods that in WebGPU accept a descriptor/struct/dict, here accept the fields in that struct as keyword arguments.

wgpu.base.apidiff Differences of base API:
  • Adds GPU.print_report() - Usefull
  • Adds GPUBuffer.map_read() - Alternative to mapping API
  • Adds GPUBuffer.map_write() - Alternative to mapping API
  • Adds GPUCanvasContext.get_preferred_format() - Better place to define the preferred format
  • Adds GPUCanvasContext.present() - Present method is exposed
  • Adds GPUDevice.adapter() - Too useful to not-have
  • Adds GPUDevice.create_buffer_with_data() - replaces WebGPU’s mapping API
  • Adds GPUQueue.read_buffer() - replaces WebGPU’s mapping API
  • Adds GPUQueue.read_texture() - For symmetry, and to help work around the bytes_per_row constraint
  • Adds GPUTexture.size() - Too useful to not-have
  • Adds GPUTextureView.size() - Need to know size e.g. for texture view provided by canvas.
  • Adds GPUTextureView.texture() - Too useful to not-have
  • Changes GPU.get_preferred_canvas_format() - Disabled because we put it on the canvas context.
  • Changes GPU.request_adapter() - arguments include a canvas object
  • Changes GPU.request_adapter_async() - arguments include a canvas object
  • Hides GPUBuffer.get_mapped_range()
  • Hides GPUBuffer.map_async()
  • Hides GPUBuffer.unmap()
  • Hides GPUDevice.import_external_texture() - Specific to browsers.
  • Hides GPUDevice.pop_error_scope()
  • Hides GPUDevice.push_error_scope()
  • Hides GPUQueue.copy_external_image_to_texture() - Specific to browsers.

Each backend may also implement minor differences (usually additions) from the base API. For the rs backend check print(wgpu.backends.rs.apidiff.__doc__).

Overview

This overview attempts to describe how all classes fit together. Scroll down for a list of all flags, enums, structs, and GPU classes.

Adapter, device and canvas

The GPU represents the root namespace that contains the entrypoint to request an adapter.

The GPUAdapter represents a hardware or software device, with specific features, limits and properties. To actually start using that harware for computations or rendering, a GPUDevice object must be requisted from the adapter. This is a logical unit to control your hardware (or software). The device is the central object; most other GPU objects are created from it. Also see the convenience function wgpu.utils.get_default_device(). Information on the adapter can be obtained using TODO in the form of a GPUAdapterInfo.

A device is controlled with a specific backend API. By default one is selected automatically. This can be overridden by setting the WGPU_BACKEND_TYPE environment variable to “Vulkan”, “Metal”, “D3D12”, “D3D11”, or “OpenGL”.

The device and all objects created from it inherit from GPUObjectBase - they represent something on the GPU.

In most render use-cases you want the result to be presented to a canvas on the screen. The GPUCanvasContext is the bridge between wgpu and the underlying GUI backend.

Buffers and textures

A GPUBuffer can be created from a device. It is used to hold data, that can be uploaded using it’s API. From the shader’s point of view, the buffer can be accessed as a typed array.

A GPUTexture is similar to a buffer, but has some image-specific features. A texture can be 1D, 2D or 3D, can have multiple levels of detail (i.e. lod or mipmaps). The texture itself represents the raw data, you can create one or more GPUTextureView objects for it, that can be attached to a shader.

To let a shader sample from a texture, you also need a GPUSampler that defines the filtering and sampling behavior beyond the edges.

WebGPU also defines the GPUExternalTexture, but this is not (yet?) used in wgpu-py.

Bind groups

Shaders need access to resources like buffers, texture views, and samplers. The access to these resources occurs via so called bindings. There are integer slots, which must be specifie both via the API, and in the shader.

Bindings are organized into GPUBindGroup s, which are essentially a list of GPUBinding s.

Further, in wgpu you need to specify a GPUBindGroupLayout, providing meta-information about the binding (type, texture dimension etc.).

Multiple bind groups layouts are collected in a GPUPipelineLayout, which represents a complete layout description for a pipeline.

Shaders and pipelines

The wgpu API knows three kinds of shaders: compute, vertex and fragment. Pipelines define how the shader is run, and with what resources.

Shaders are represented by a GPUShaderModule.

Compute shaders are combined with a pipelinelayout into a GPUComputePipeline. Similarly, a vertex and (optional) fragment shader are combined with a pipelinelayout into a GPURenderPipeline. Both of these inherit from GPUPipelineBase.

Command buffers and encoders

The actual rendering occurs by recording a series of commands and then submitting these commands.

The root object to generate commands with is the GPUCommandEncoder. This class inherits from GPUCommandsMixin (because it generates commands), and GPUDebugCommandsMixin (because it supports debugging).

Commands specific to compute and rendering are generated with a GPUComputePassEncoder and GPURenderPassEncoder respectively. You get these from the command encoder by the corresponding begin_x_pass() method. These pass encoders inherit from GPUBindingCommandsMixin (because you associate a pipeline) and the latter also from GPURenderCommandsMixin.

When you’re done generating commands, you call finish() and get the list of commands as an opaque object: the GPUCommandBuffer. You don’t really use this object except for submitting it to the GPUQueue.

The command buffers are one-time use. The GPURenderBundle and GPURenderBundleEncoder can be used to record commands to be used multiple times, but this is not yet implememted in wgpu-py.

Error handling

Errors are caught and logged using the wgpu logger.

Todo: document the role of these classes: GPUUncapturedErrorEvent GPUError GPUValidationError GPUOutOfMemoryError GPUInternalError GPUPipelineError GPUDeviceLostInfo

TODO

These classes are not supported and/or documented yet. GPUCompilationMessage GPUCompilationInfo GPUQuerySet

List of flags, enums, and structs

List of GPU classes

GPU The entrypoint to the wgpu API.
GPUAdapterInfo Represents information about an adapter.
GPUAdapter Represents an abstract wgpu implementation.
GPUBindGroup Represents a group of resource bindings (buffer, sampler, texture-view).
GPUBindGroupLayout Defines the interface between a set of resources bound in a GPUBindGroup.
GPUBindingCommandsMixin Mixin for classes that defines bindings.
GPUBuffer Represents a block of memory that can be used in GPU operations.
GPUCanvasContext Represents a context to configure a canvas.
GPUCommandBuffer Stores a series of commands generated by a GPUCommandEncoder.
GPUCommandEncoder Object to record a series of commands.
GPUCommandsMixin Mixin for classes that encode commands.
GPUCompilationInfo TODO
GPUCompilationMessage An object that contains information about a problem with shader compilation.
GPUComputePassEncoder Object to records commands for a compute pass.
GPUComputePipeline Represents a single pipeline for computations (no rendering).
GPUDebugCommandsMixin Mixin for classes that support debug groups and markers.
GPUDevice The top-level interface through which GPU objects are created.
GPUDeviceLostInfo An object that contains information about the device being lost.
GPUError A generic GPU error.
GPUExternalTexture Ignore this - specific to browsers.
GPUInternalError An error raised for implementation-specific reasons.
GPUObjectBase The base class for all GPU objects.
GPUOutOfMemoryError An error raised when the GPU is out of memory.
GPUPipelineBase A mixin class for render and compute pipelines.
GPUPipelineError An error raised when a pipeline could not be created.
GPUPipelineLayout Describes the layout of a pipeline, as a list of GPUBindGroupLayout objects.
GPUQuerySet TODO
GPUQueue Object to submit command buffers to.
GPURenderBundle TODO: not yet wrapped.
GPURenderBundleEncoder TODO: not yet wrapped
GPURenderCommandsMixin Mixin for classes that provide rendering commands.
GPURenderPassEncoder Object to records commands for a render pass.
GPURenderPipeline Represents a single pipeline to draw something.
GPUSampler Defines how a texture (view) must be sampled by the shader.
GPUShaderModule Represents a programmable shader.
GPUTexture Represents a 1D, 2D or 3D color image object.
GPUTextureView Represents a way to represent a GPUTexture.
GPUUncapturedErrorEvent TODO
GPUValidationError An error raised when the pipeline could not be validated.