GPUCanvasContext

class wgpu.GPUCanvasContext

Bases: object

Represents a context to configure a canvas.

Is also used to obtain the texture to render to.

Can be obtained via gui.WgpuCanvasInterface.get_context().

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. An often used format is “bgra8unorm-srgb”.

  • 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”.

  • alpha_mode (enums.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. This method should be called exactly once during each draw event.

get_preferred_format(adapter)

Get the preferred surface texture format.

present()

Present what has been drawn to the current texture, by compositing it to the canvas. Note that a canvas based on gui.WgpuCanvasBase will call this method automatically at the end of each draw event.

unconfigure()

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