Documentation
¶
Overview ¶
Package gpuimage provides offscreen WebGPU rendering, texture upload and sampling, and optional pixel readback into Go images. It uses gogpu/wgpu's native and browser backends and exposes upstream types for custom drawing.
Pixels ¶
Targets and textures are RGBA8Unorm without color-space conversion. Readback returns the target's bytes as image.NRGBA, so a shader that writes premultiplied color must be un-premultiplied by the caller. Uploads store straight (non-premultiplied) alpha.
Threading ¶
A Renderer owns one goroutine pinned to an OS thread. Every GPU call — device creation, target allocation, the caller's draw callback, the copy, the map, and all releases — runs on that goroutine. Use Renderer.Do to run pipeline and buffer setup on it, and Frame.Device inside a draw callback.
gogpu/wgpu documents devices as safe for concurrent use and command encoders as not. The single thread is this package's choice: it keeps one frame in flight, keeps the wait for a mapped buffer on the thread that submitted the work, and gives thread-bound backends such as GLES a stable thread. Code that receives the device must not retain it for use on other goroutines.
Browser ¶
On GOOS=js the map step awaits a JavaScript promise, which is fine from any Go goroutine except one that is itself running inside a JS callback (that would deadlock the event loop). Start rendering from a Go goroutine.
When attaching Frame.Depth (a depth-only Depth24Plus view) to a render pass, set StencilReadOnly: true on the RenderPassDepthStencilAttachment. gogpu's browser backend otherwise emits stencilLoadOp/stencilStoreOp, which WebGPU rejects for a format with no stencil aspect; native backends ignore the flag for a depth-only attachment.
Index ¶
- Variables
- type Error
- type Frame
- type Options
- type Renderer
- func (r *Renderer) AdapterInfo() gputypes.AdapterInfo
- func (r *Renderer) Close() error
- func (r *Renderer) Do(f func(dev *wgpu.Device) error) error
- func (r *Renderer) ReadPixels(ctx context.Context) (*image.NRGBA, Stats, error)
- func (r *Renderer) Render(ctx context.Context, draw func(*Frame) error) (*image.NRGBA, Stats, error)
- func (r *Renderer) RenderToTexture(ctx context.Context, draw func(*Frame) error) (Stats, error)
- func (r *Renderer) Resize(w, h int) error
- func (r *Renderer) Size() (w, h int)
- func (r *Renderer) UploadTexture(src image.Image, opts TextureOptions) (*Texture, error)
- type Stats
- type Texture
- type TextureOptions
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("gpuimage: renderer closed")
ErrClosed is returned by every Renderer method after Close.
var ErrNoAdapter = errors.New("gpuimage: no GPU adapter available")
ErrNoAdapter is returned by New when the instance reports no GPU adapter.
Functions ¶
This section is empty.
Types ¶
type Frame ¶
type Frame struct {
Device *wgpu.Device // the renderer's device, for queue writes and resource updates
Encoder *wgpu.CommandEncoder
Color *wgpu.TextureView // RGBA8Unorm, RenderAttachment|CopySrc
Depth *wgpu.TextureView // nil unless Options.Depth
Width int
Height int
Seq uint64 // frame sequence number, starting at 1
}
Frame is handed to the draw callback of Render. Record render passes into Encoder against Color (and Depth when allocated); the Renderer appends the readback copy and submits Encoder once the callback returns. Every field is valid only until the callback returns.
type Options ¶
type Options struct {
Width, Height int // initial target size in pixels; both must be > 0
Depth bool // allocate a Depth24Plus attachment alongside the color target
Label string // optional prefix for GPU object labels
// Backends restricts native backend selection. Zero means all.
Backends gputypes.Backends
// PowerPreference is forwarded to RequestAdapter.
PowerPreference gputypes.PowerPreference
// ForceFallback requests the software (CPU) adapter. Useful for
// deterministic tests; ignored where no such adapter exists.
ForceFallback bool
}
Options configures a Renderer at construction.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer owns a GPU device and an offscreen render target, and reads frames back as *image.NRGBA. All GPU work runs on an internal executor goroutine pinned to one OS thread. Renderer is safe for concurrent use; calls are serialized. Its methods must not be called from inside Do or a draw callback, which already occupy the executor.
func New ¶
New creates the instance, adapter, device, and an initial target of opts.Width×opts.Height. Everything created so far is released on failure.
func (*Renderer) AdapterInfo ¶
func (r *Renderer) AdapterInfo() gputypes.AdapterInfo
AdapterInfo reports immutable adapter metadata: its name, backend, and device type. A DeviceTypeCPU adapter is wgpu's software rasterizer.
func (*Renderer) Do ¶
Do runs f on the executor thread with the device. Use it for pipeline, shader, and buffer setup so those calls obey the single-thread rule.
func (*Renderer) ReadPixels ¶
ReadPixels explicitly copies and maps the current color attachment into a caller-owned Go image. Call after drawing; Render combines both steps in one submission. Concurrent callers read the most recently submitted frame. It fails if nothing was drawn since New or the last Resize.
func (*Renderer) Render ¶
func (r *Renderer) Render(ctx context.Context, draw func(*Frame) error) (*image.NRGBA, Stats, error)
Render runs draw against a fresh command encoder, appends the readback copy, submits, maps the staging buffer, and returns a new NRGBA image that the caller owns. One frame is in flight at a time.
Render returns ctx.Err() if ctx ends while the call waits for the GPU thread, during draw, or while the GPU finishes the frame. It does not interrupt a running draw callback. After a cancellation the GPU may still complete the abandoned frame; later calls are unaffected. Watching a context that can end adds about a tenth of a millisecond to a native readback.
func (*Renderer) RenderToTexture ¶
RenderToTexture draws and submits without copying pixels to Go, for callers that read back only some frames. The color attachment keeps the frame until the next draw or Resize, and ReadPixels copies it on request. The attachment cannot be sampled: it has no TextureBinding usage.
Native submission waits for the GPU when ctx cannot end, such as context.Background. Otherwise, and always in the browser, it returns once the frame is submitted.
func (*Renderer) Resize ¶
Resize replaces the color/depth textures and the staging buffer. It is a no-op when the size is unchanged, and on failure the old target is kept.
func (*Renderer) UploadTexture ¶
UploadTexture copies an image into a sampled GPU texture. The source image can have any bounds or image.Image implementation; pixels are copied before this method returns. Close the Texture when it is no longer needed, or let Renderer.Close release it with the device.
type Stats ¶
type Stats struct {
Seq uint64
Draw time.Duration // the caller's draw callback
Submit time.Duration // copy encode + queue submit
Map time.Duration // staging-buffer map wait (GPU sync)
Copy time.Duration // de-pad into the NRGBA image
Total time.Duration
BytesPerRow int // padded staging row stride
}
Stats reports where one Render spent its time.
type Texture ¶
type Texture struct {
// contains filtered or unexported fields
}
Texture is an RGBA8 image owned by a Renderer. It can be sampled in a draw callback, updated without reallocating, or copied back to Go pixels. Binding 0 is texture_2d<f32>; binding 1 is a filtering sampler. UVs clamp at the edges.
func (*Texture) BindGroupLayout ¶
func (t *Texture) BindGroupLayout() *wgpu.BindGroupLayout
BindGroupLayout is the layout to include in a caller's pipeline layout. Use BindGroup at the matching group index inside Renderer.Render/Do. These raw GPU handles are valid only while the Texture and Renderer are open.
func (*Texture) Close ¶
Close releases this texture's GPU resources. It is safe to call more than once; Renderer.Close also releases any textures left open.
func (*Texture) ReadPixels ¶
ReadPixels returns a fresh, tightly packed NRGBA copy of this texture. It synchronizes with pending GPU work; use it only when a CPU consumer needs the pixels. Sampling this texture in a render pass does not require readback.
type TextureOptions ¶
type TextureOptions struct {
Filter gputypes.FilterMode
}
TextureOptions controls the sampling filter. Zero selects linear filtering. Nearest filtering is useful for pixel art or an atlas without padded tiles.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
clear
command
Clear renders a solid color offscreen and writes the pixels as a PNG.
|
Clear renders a solid color offscreen and writes the pixels as a PNG. |
|
texture
command
Texture uploads a Go image, samples it in a custom shader, updates it, and writes both rendered frames as PNG files.
|
Texture uploads a Go image, samples it in a custom shader, updates it, and writes both rendered frames as PNG files. |
|
internal
|
|
|
browsertest
command
Command browsertest runs a GOOS=js GOARCH=wasm test binary in headless Chrome so tests can reach a real browser WebGPU device.
|
Command browsertest runs a GOOS=js GOARCH=wasm test binary in headless Chrome so tests can reach a real browser WebGPU device. |