webgpu

package
v1.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package webgpu is the WebGPU backend of the explicit renderer (SPEC §9.5): an explicit.Device over navigator.gpu, through syscall/js, so that internal/gpu/explicit draws on the web the way it draws on Vulkan.

The browser's GPU API is asynchronous, and the UI goroutine never waits for a promise once the event listeners are installed (SPEC §23.3). So the adapter and the device are awaited by Acquire, which the engine calls while it starts, before the platform installs its listeners; what comes after is a callback that posts to the UI goroutine: a read-back that finishes later (ReadTexture), the loss of the device, and the new device that follows a loss.

The decisions that need no browser are plain Go, tested in the normal run and under Node: the names the API uses for formats, usages, blend factors and vertex formats (mapping.go), the bind group layouts of a pipeline (layout.go), the supply of devices (supply.go), the loss of a device (loss.go) and the rows of a read-back (readback.go). The files that touch the browser carry the js tag; elsewhere Acquire fails with WebGPUUnavailable (unavailable.go).

Flutter: Impeller's Metal and Vulkan backends are its counterparts; Flutter web uses CanvasKit or Skwasm (lib/web_ui/lib/src/engine/).

Index

Constants

View Source
const (
	// TagUnavailable is the tag of a browser that has no navigator.gpu, or gives no adapter.
	TagUnavailable = "WebGPUUnavailable"
	// TagDeviceFailed is the tag of an adapter that does not give a device.
	TagDeviceFailed = "WebGPUDeviceFailed"
	// TagShaderInvalid is the tag of a WGSL module the browser rejects: an internal bug.
	TagShaderInvalid = "ShaderModuleInvalid"
	// TagDeviceLost is the tag of a device that was lost, which explicit.Renderer recovers from.
	TagDeviceLost = explicit.TagGPUDeviceLost
)

The tags of the errors of this package. Each failure has a tag of its own (CLAUDE.md), and the route of an error lists the calls it came through.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Logger gets the log of the backend: a lost device, the messages the browser's validation
	// reports; slog.Default() when nil.
	Logger *slog.Logger
	// Post runs fn on the UI goroutine. The browser's callbacks (a finished read-back, a lost
	// device, a device that came after a loss) call it so that their work happens where the
	// framework runs (SPEC §23.3). When nil, fn runs inside the callback.
	Post func(fn func())
	// OnDeviceReady is called on the UI goroutine, through Post, when the device that was
	// asked for after a loss has arrived; the handle is the window the last device was opened
	// for. The engine asks for a frame of that window, which opens the device.
	OnDeviceReady func(h gpu.SurfaceHandle)
	// Timeout is how long Acquire waits for the adapter and the device; 20 seconds when zero.
	Timeout time.Duration
}

Options are the optional parameters of Acquire.

Flutter: no counterpart

type Provider

type Provider struct{}

Provider hands out the device of the browser's WebGPU. Off the web there is none: Acquire fails and Open on a nil provider says so.

Flutter: no counterpart

func Acquire

func Acquire(context.Context, Options) (*Provider, error)

Acquire fails with WebGPUUnavailable: only a browser has WebGPU.

Flutter: no counterpart

func (*Provider) Close

func (p *Provider) Close()

Close does nothing: there is no device.

Flutter: no counterpart

func (*Provider) Open

Open fails with WebGPUUnavailable.

Flutter: no counterpart

type Renderer

type Renderer struct {
	*explicit.Renderer
}

Renderer is the explicit renderer over a WebGPU device (SPEC §9.5). It draws as the Vulkan renderer does, with one difference: a snapshot (Picture.ToImage) is drawn by the software renderer. A WebGPU read-back finishes in a later browser task and the UI goroutine does not wait for one (SPEC §23.3), while ToImage returns at once (SPEC §12.10).

Flutter: no counterpart (flutter::Rasterizer)

func NewRenderer

func NewRenderer(open func(gpu.SurfaceHandle) (explicit.Device, error), opts ...explicit.RendererOpts) *Renderer

NewRenderer returns a renderer that opens its device with open when it draws its first frame: the Open method of a Provider, or a function that gives another device. More than one options value panics with MultipleOptions.

Flutter: no counterpart

func (*Renderer) RasterizeScene

func (r *Renderer) RasterizeScene(scene *ui.Scene, width, height int) (*ui.Image, error)

RasterizeScene draws the scene into a new image of width x height pixels, as Snapshot does: the ui.Rasterizer for Picture.ToImage and Scene.ToImage.

Flutter: no counterpart

func (*Renderer) Snapshot

func (r *Renderer) Snapshot(scene *ui.Scene, size ui.Size) (*image.RGBA, error)

Snapshot draws the scene into a new image of the size with the software renderer, over transparent pixels, premultiplied, top row first: the pixels of Picture.ToImage. It fails with RendererSizeInvalid for a size that is not positive.

Flutter: no counterpart

Directories

Path Synopsis
Command gpuwebtest is the program of the browser tests of the WebGPU renderer (step 21.5, SPEC §9.5, §17).
Command gpuwebtest is the program of the browser tests of the WebGPU renderer (step 21.5, SPEC §9.5, §17).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL