webgpu

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

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

This section is empty.

Variables

View Source
var DefaultLimits = Limits{Handles: 4096, MemoryBytes: 256 << 20, ShaderBytes: 1 << 20, Pipelines: 256}

DefaultLimits are the per-process quotas.

View Source
var ErrHandles = errors.New("too many live resources")

ErrHandles is the quota error of New.

View Source
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.

View Source
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 Trapf

func Trapf(format string, a ...any) error

Trapf makes a TrapError.

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

func (*Handles) Drop

func (h *Handles) Drop(n uint32, res string) error

Drop removes handle n (a res), destroying its object.

func (*Handles) Get

func (h *Handles) Get(n uint32, res string) (any, error)

Get returns handle n's object, which must be a res.

func (*Handles) Len

func (h *Handles) Len() int

Len is the number of live handles.

func (*Handles) New

func (h *Handles) New(res string, obj any) (uint32, error)

New registers obj as a resource of type res and returns its handle.

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

type Module struct {
	Backend Backend
	Limits  Limits
	// contains filtered or unexported fields
}

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 NewModule

func NewModule(b Backend) *Module

NewModule serves wasi:webgpu with a backend (nil: no GPU anywhere).

func NewModules added in v0.5.0

func NewModules(b Backend) []*Module

NewModules serves wasi:webgpu and wasi-gfx:surface (surface, surface-webgpu) with one backend: register all three.

func (*Module) Funcs

func (m *Module) Funcs() []wasi.HostFunc

Funcs implements wasi.HostModule: every function of the interface, with its Canonical ABI core signature, and a drop per resource.

func (*Module) Module

func (m *Module) Module() string

Module implements wasi.HostModule.

func (*Module) Open

func (m *Module) Open(p *wasi.Proc) wasi.Instance

Open implements wasi.HostModule.

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 TrapError

type TrapError struct{ Msg string }

TrapError traps the guest (the Canonical ABI's answer to an invalid handle, or a quota exceeded where the API has no error to report).

func (*TrapError) Error

func (e *TrapError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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