Documentation
¶
Overview ¶
Package webgpu is the host side of wasi:webgpu (wasi-gfx, wasi:webgpu@0.3.0-rc.2) for core modules: every function of the WIT interface is a typed import of one host module, lowered and lifted with the Canonical ABI (package wit) by hand, without a component-model runtime. Resources are handles in a per-process table.
The functions run against a Backend: in the browser, WebGPU in the engine's worker (backend_js.go); elsewhere none, so get-gpu works and request-adapter answers "none", and a guest can test for a GPU portably. A process without the container's "gpu" capability (run --gpu) gets no adapter either.
Index ¶
- Constants
- Variables
- func AckFrame(surface uint32, seq uint64)
- func HostModules(b Backend) []wasi.HostModule
- func KeyFromCode(code string) (uint32, bool)
- func SurfaceInput(surface uint32, e InputEvent) bool
- func Trapf(format string, a ...any) error
- type Backend
- type Browser
- type Destroyer
- type Handles
- type InputEvent
- type Limits
- type Module
- type Process
- type Session
- type Stream
- type SurfaceEvent
- type TrapError
- type WaitSet
Constants ¶
const AckWait = time.Second
AckWait bounds one wait for acknowledgements: a page that stops acknowledging slows a guest down to one frame per AckWait, never stops it.
const MaxCoordinate = 1 << 16
MaxCoordinate bounds a pointer coordinate: far outside any surface (1920x1080 at most) but finite.
const MaxFramesInFlight = 2
MaxFramesInFlight is how many unacknowledged frames a surface may have before present waits (at most AckWait) for AckFrame.
const MaxInputStreams = 64
MaxInputStreams bounds the streams one surface serves (all kinds).
const MaxKeyText = 64
MaxKeyText bounds a key event's text, in bytes.
const MaxQueued = 256
MaxQueued bounds the events a stream keeps for a guest that does not read them: past it the oldest is dropped (and counted).
const RootModule = wit.RootModule
RootModule is the module of the waitable-set built-ins.
Variables ¶
var DefaultLimits = Limits{Handles: 4096, MemoryBytes: 256 << 20, ShaderBytes: 1 << 20, Pipelines: 256}
DefaultLimits are the per-process quotas.
var ErrHandles = errors.New("too many live resources")
ErrHandles is the quota error of New.
var InputKinds = []string{"pointer-up", "pointer-down", "pointer-move", "key-up", "key-down"}
InputKinds are the kinds a surface serves.
var Interface = func() *wit.Interface { ifs, err := wit.Parse(witdata.WebGPU) if err != nil { panic(err) } return ifs["webgpu"] }()
Interface is the parsed wasi:webgpu interface.
var SurfaceInterfaces = func() map[string]*wit.Interface { ifs, err := wit.ParseFiles([]*wit.Interface{Interface}, witdata.Surface) if err != nil { panic(err) } return ifs }()
SurfaceInterfaces are wasi-gfx:surface's surface and surface-webgpu, as this module declares and serves them (webgpu/wit/surface.wit).
Functions ¶
func HostModules ¶ added in v0.5.0
func HostModules(b Backend) []wasi.HostModule
HostModules is NewModules as the engine's host-module list.
func KeyFromCode ¶ added in v0.6.0
KeyFromCode maps a UI Events KeyboardEvent.code ("KeyA", "Digit0", "ArrowUp", "NumpadClearEntry") to the index of wasi-gfx:surface's key enum ("key-a", "digit0", "arrow-up", "numpad-clear-entry"); ok is false for a code it does not have (the event's key is then none).
func SurfaceInput ¶ added in v0.6.0
func SurfaceInput(surface uint32, e InputEvent) bool
SurfaceInput delivers a pointer or key event from the page to a surface's input streams (docs/DESIGN.md, "Surfaces"); it reports whether the surface exists and the event was valid.
Types ¶
type Backend ¶
type Backend interface {
// Open starts a process's session.
Open(p Process, lim Limits, h *Handles) Session
}
Backend serves the functions for one process.
type Browser ¶
type Browser struct {
// AllowFallback offers a software adapter; off by default, since its
// work is not GPU work.
AllowFallback bool
// GPUTime bounds one submission's GPU work; past it the device is
// destroyed (lost) rather than letting a container hold the GPU
// (default 2 s, the order of a driver watchdog).
GPUTime time.Duration
// OnSurface receives the surfaces' events: "open", "resize", "close",
// and "frame" (an ImageBitmap the receiver must transfer or close,
// with its sequence number). Nil drops frames (closing them). The
// page acknowledges drawn frames with AckFrame (docs/DESIGN.md,
// "Surfaces").
OnSurface func(SurfaceEvent)
// FrameInterval paces present (default 1/60 s): a surface never shows
// more than one frame per interval.
FrameInterval time.Duration
// MaxWidth and MaxHeight bound a surface (default 1920x1080).
MaxWidth, MaxHeight int
}
Browser is the backend in the browser: WebGPU through the worker's navigator.gpu (the playground's engine worker runs the WASI host).
type Destroyer ¶
type Destroyer interface{ Destroy() }
Destroyer is implemented by objects that release something when their handle is dropped or the process ends.
type Handles ¶
type Handles struct {
// contains filtered or unexported fields
}
Handles is a process's resource table: handles start at 1 (0 is never valid), each tagged with its resource type.
type InputEvent ¶ added in v0.6.0
type InputEvent struct {
// Kind is "pointer-down", "pointer-up", "pointer-move", "key-down" or
// "key-up" (the surface methods on-<kind>).
Kind string
// X, Y: pointers, in surface pixels from the top-left corner (the page
// scales from the pane).
X, Y float64
// Code is KeyboardEvent.code ("KeyA"); an unknown code is a key of
// none. Text is the text the key produces (KeyboardEvent.key when it is
// one character), "" for none.
Code, Text string
Alt, Ctrl, Meta, Shift bool
}
InputEvent is a pointer or key event the page (the dashboard pane that shows a surface) sends for that surface; the host turns it into a wasi-gfx:surface pointer-event or key-event on the guest's streams of that kind.
type Limits ¶
type Limits struct {
Handles int // live resources (default 4096)
MemoryBytes int64 // buffer and texture bytes alive (default 256 MiB)
ShaderBytes int // one WGSL source (default 1 MiB)
Pipelines int // pipelines alive (default 256)
}
Limits bound one process's use of the GPU.
type Module ¶
Module is a host module serving one WIT interface: wasi:webgpu, or one of wasi-gfx:surface's. The modules of one Backend share, per process, one handle table and one session, so a texture a surface context returns is a wasi:webgpu resource like any other.
func NewModules ¶ added in v0.5.0
NewModules serves wasi:webgpu, wasi-gfx:surface (surface, surface-webgpu) and the waitable-set built-ins ($root) with one backend: register all four.
type Process ¶ added in v0.5.0
type Process struct {
GPU bool // the process has the gpu capability
Container string // its container's label (wasi.Config.Label), for surfaces
}
Process is what a Backend knows of the process it serves.
type Session ¶
type Session interface {
// Call runs import name with lifted arguments and returns the result
// value (nil for none). A *TrapError traps the guest.
Call(name string, args []any) (any, error)
// Async reports whether name waits on the platform (WebGPU promises):
// such calls run off the host call.
Async(name string) bool
// Close releases everything (the process ended).
Close()
}
Session is one process's GPU state.
type Stream ¶ added in v0.6.0
Stream is the readable end of a stream the host writes (a surface's input events).
func (*Stream) CloseWriter ¶ added in v0.6.0
func (s *Stream) CloseWriter()
CloseWriter ends the stream: once the queue is read, reads report DROPPED.
func (*Stream) SetOnClose ¶ added in v0.6.0
func (s *Stream) SetOnClose(f func())
SetOnClose sets what runs when the guest drops the end (or its process ends).
type SurfaceEvent ¶ added in v0.5.0
type SurfaceEvent struct {
Op string // "open", "resize", "close", "frame"
Surface uint32
Container string // open: the container's label (its ID), "" if unknown
Width, Height int
Seq uint64 // frames: from 1
Bitmap js.Value // frames: an ImageBitmap
}
SurfaceEvent is what OnSurface receives.