GPUCanvasContext

class wgpu.GPUCanvasContext

Bases: object

Represents a context to configure a canvas and render to it.

Can be obtained via gui.WgpuCanvasInterface.get_context(“wgpu”).

The canvas-context plays a crucial role in connecting the wgpu API to the GUI layer, in a way that allows the GUI to be agnostic about wgpu. It combines (and checks) the user’s preferences with the capabilities and preferences of the canvas.

property canvas

The associated canvas object.

configure(**parameters)

Configures the presentation context for the associated canvas. Destroys any textures produced with a previous configuration. This clears the drawing buffer to transparent black.

Parameters:
  • device (WgpuDevice) – The GPU device object to create compatible textures for.

  • format (enums.TextureFormat) – The format that textures returned by get_current_texture() will have. Must be one of the supported context formats. Can be None to use the canvas’ preferred format.

  • usage (flags.TextureUsage) – Default TextureUsage.OUTPUT_ATTACHMENT.

  • view_formats (List[enums.TextureFormat]) – The formats that views created from textures returned by get_current_texture() may use.

  • color_space (PredefinedColorSpace) – The color space that values written into textures returned by get_current_texture() should be displayed with. Default “srgb”. Not yet supported.

  • tone_mapping (enums.CanvasToneMappingMode) – Not yet supported.

  • alpha_mode (structs.CanvasAlphaMode) – Determines the effect that alpha values will have on the content of textures returned by get_current_texture() when read, displayed, or used as an image source. Default “opaque”.

get_current_texture()

Get the GPUTexture that will be composited to the canvas next.

get_preferred_format(adapter)

Get the preferred surface texture format.

present()

Hook for the canvas to present the rendered result.

Present what has been drawn to the current texture, by compositing it to the canvas. Don’t call this yourself; this is called automatically by the canvas.

unconfigure()

Removes the presentation context configuration. Destroys any textures produced while configured.