Documentation
¶
Overview ¶
Package gpu2d describes a 2D frame as a list of instances that a GPU draws as one quad each and that Rasterize executes on the CPU. Rasterize is the reference: it ports go-widgets/painter's PixelPainter formulas, and a GPU back-end must match it within 1 per channel (see docs/DESIGN.md).
Index ¶
- Constants
- Variables
- func AppendBytes(dst []byte, ins []Instance) []byte
- func Coverage(k Kind, x, y int, r Rect, rad int) float64
- func WidgetFrame(c Canvas)
- type Canvas
- type Instance
- type Kind
- type List
- func (l *List) Bin(spans, list []uint32) ([]uint32, []uint32, error)
- func (l *List) Data() []byte
- func (l *List) DrawAlpha(cov []float64, ox, oy, w, h int, c RGBA)
- func (l *List) DrawImage(dst Rect, src []byte, srcW, srcH int)
- func (l *List) DrawMask(dst Rect, mask []byte, stride int, ink RGBA)
- func (l *List) Err() error
- func (l *List) FillRect(r Rect, c RGBA)
- func (l *List) FillRoundRect(r Rect, radius int, c RGBA)
- func (l *List) Instances() []Instance
- func (l *List) PopClip()
- func (l *List) PopTranslate()
- func (l *List) PushClip(r Rect)
- func (l *List) PushTranslate(dx, dy int)
- func (l *List) Rasterize(buf []byte) error
- func (l *List) Reset()
- func (l *List) Size() (w, h int)
- func (l *List) StrokeRect(r Rect, c RGBA, lineW int)
- func (l *List) StrokeRoundRect(r Rect, radius int, c RGBA, lineW int)
- func (l *List) Tiles() (across, down int)
- type RGBA
- type Rect
Constants ¶
const InstanceSize = 64
InstanceSize is the byte size of one Instance.
const MaxBinned = 1 << 26
MaxBinned bounds the entries Bin may produce, one per instance per tile it touches: 2^26 entries are 256 MiB of indices, far past any real frame (a 4K frame of 2200 widget instances needs about 0.2 M). A list past it is ErrTooLarge, so no list can size an unbounded allocation.
const MaxCoord = 1 << 24
MaxCoord bounds every coordinate a list records, translation included: 2^24 is the last integer up to which float32, which a shader computes in, represents every integer exactly. A primitive reaching past it is not recorded, and a surface larger than it is refused by NewList.
const MaxData = 1 << 28
MaxData bounds the list's data arena, which holds the masks and images of a frame: 2^28 bytes is 256 MiB, eight full 4K RGBA images. A primitive that would pass it is refused and the list's Err reports ErrTooLarge.
const MaxRadius = 16383
MaxRadius bounds a corner radius, after it is clamped to half the smaller side: 4·D, four times the squared distance from a corner pixel to the circle's centre, is below 8·radius², which stays under 2^31 here, so every kernel decides the corner coverage in 32-bit integers. A rounded rectangle past it (one at least 32,768 pixels on a side) is not recorded and the list's Err reports ErrTooLarge.
const MaxScaled = 1 << 31
MaxScaled bounds an image's scaling products W·SrcW and H·SrcH, which a GPU computes in 32-bit integers for the nearest-neighbour mapping. An image past it is refused and Err reports ErrImageScale.
const Tile = 16
Tile is the side, in pixels, of the squares Bin sorts instances into.
Variables ¶
var ErrBuffer = errors.New("gpu2d: buffer smaller than 4 × width × height")
ErrBuffer reports a buffer too small for the list's surface.
var ErrImageScale = errors.New("gpu2d: image size times its destination size passes MaxScaled")
ErrImageScale reports an image whose scaling products pass MaxScaled.
var ErrSize = errors.New("gpu2d: surface side outside [0, MaxCoord]")
ErrSize reports a surface side outside [0, MaxCoord].
var ErrTooLarge = errors.New("gpu2d: binned list larger than MaxBinned")
ErrTooLarge reports a list whose binning would pass MaxBinned.
var KernelWGSL string
KernelWGSL is the whole raster kernel as one WGSL compute shader, entry point "raster", workgroup 16 × 8, one invocation per pixel. It binds, at @group(0): the instances (@binding(0), InstanceSize bytes each), the bins (@binding(1): List.Bin's spans followed by its list, one buffer), the arena (@binding(2), List.Data padded to a whole word), the pixels (@binding(3), one little-endian RGBA word each, read and written) and a uniform {w, h, tiles across, Tile} of four words (@binding(4)). It blends in integers as Rasterize does, so a frame is byte-identical to Rasterize's over any ground. docs/NATIVE.md, decision 1, says why it is one WGSL file.
Functions ¶
func AppendBytes ¶
AppendBytes appends the instances to dst as a GPU reads them: InstanceSize bytes each, every field a little-endian 32-bit word in declaration order. It is the one encoding every back-end uploads.
func Coverage ¶
Coverage is the share of pixel (x, y) that a primitive of kind k over r with corner radius rad covers, from 0 to 1: PixelPainter's cornerFillCoverage for a fill and the ring of its strokeRoundOutline for an outline. A GPU fragment shader computes the same expression at the same pixel centre.
func WidgetFrame ¶
func WidgetFrame(c Canvas)
WidgetFrame draws a frame shaped like a widget toolkit's: an opaque background, then a grid of cards, each a rounded fill, a translucent rounded border and a smaller rounded control inside, and every seventh card under a translucent hover wash.
Types ¶
type Canvas ¶
type Canvas interface {
Size() (w, h int)
FillRect(r Rect, c RGBA)
FillRoundRect(r Rect, radius int, c RGBA)
StrokeRoundRect(r Rect, radius int, c RGBA, lineW int)
}
Canvas is what WidgetFrame draws on: a List, or an adapter over another renderer, so that every renderer's benchmark draws the same frame.
type Instance ¶
type Instance struct {
X, Y, W, H int32
ClipX0, ClipY0, ClipX1, ClipY1 int32
Color uint32 // R | G<<8 | B<<16 | A<<24
Radius int32
Kind Kind
Data, Stride, SrcW, SrcH uint32
// contains filtered or unexported fields
}
Instance is one primitive as a GPU reads it: 64 bytes, every field 32 bits. X..H are in surface pixels, translation applied. Clip is the half-open box [ClipX0, ClipX1) × [ClipY0, ClipY1) the primitive may touch. Radius is already clamped to half the smaller side. Data is a byte offset into the list's data arena (a multiple of 4), for the kinds that read one.
type Kind ¶
type Kind uint32
Kind says how an instance covers its rectangle.
const ( // KindFill covers the rectangle, its corners rounded to Radius. KindFill Kind = iota // KindOutline covers the one-pixel outline, its corners rounded to Radius. KindOutline // KindMask paints Color through 8-bit coverage in the data arena, one // byte per pixel of the rectangle, Stride bytes per row: PixelPainter's // DrawMask. KindMask // KindAlpha paints Color's RGB at the alpha the data arena gives for each // pixel, laid out as for KindMask: a path's coverage already scaled by // its colour's alpha and rounded, as PixelPainter composites it. KindAlpha // KindImage draws the SrcW × SrcH RGBA image at Data, scaled into the // rectangle by nearest neighbour: PixelPainter's DrawImage. KindImage )
type List ¶
type List struct {
// contains filtered or unexported fields
}
List records a frame. The zero value is not ready: use NewList, which knows the surface the clip starts from.
func NewList ¶
NewList starts a frame for a w × h surface. A side below zero or above MaxCoord is ErrSize.
func (*List) Bin ¶
Bin sorts the list's instances by the Tile × Tile squares their drawable box (rectangle cut by clip) touches. spans gets two words per tile, row after row: the first entry of the tile in list, and how many. list gets instance indices, each tile's in draw order. Both reuse the storage of the slices passed in.
func (*List) Data ¶ added in v0.2.0
Data returns the arena the mask, alpha and image instances read from.
func (*List) DrawAlpha ¶ added in v0.2.0
DrawAlpha paints c's RGB at a per-pixel alpha computed from coverage: cov holds w × h values from 0 to 1 for the box at (ox, oy), in surface pixels with no translation, and each pixel's alpha is c.A·cov rounded, as PixelPainter composites a path's coverage. It is what a path rasterizer's output becomes.
func (*List) DrawImage ¶ added in v0.2.0
DrawImage draws src, srcW × srcH RGBA pixels, scaled into dst by nearest neighbour: PixelPainter's DrawImage. A src shorter than srcW·srcH·4 is ignored, as PixelPainter ignores it.
func (*List) DrawMask ¶ added in v0.2.0
DrawMask paints ink through mask, one coverage byte per pixel of dst, stride bytes per row: PixelPainter's DrawMask, short rows included (a row the mask cannot supply across the visible part is left untouched).
func (*List) Err ¶ added in v0.2.0
Err reports the first primitive the list refused since NewList or Reset (ErrTooLarge, ErrImageScale); the primitives after it are still recorded.
func (*List) FillRoundRect ¶
FillRoundRect fills r with its corners rounded to radius; a radius that clamps to zero or less is a plain fill.
func (*List) PopClip ¶
func (l *List) PopClip()
PopClip removes the innermost clip; on an empty stack it does nothing.
func (*List) PopTranslate ¶
func (l *List) PopTranslate()
PopTranslate removes the innermost translation; on an empty stack it does nothing.
func (*List) PushClip ¶
PushClip confines drawing to r, translated by the translation in force and intersected with the enclosing clip.
func (*List) PushTranslate ¶
PushTranslate shifts everything drawn after it by dx, dy, added to the enclosing translation. The sum saturates at ±2·MaxCoord, past which nothing can land on a surface anyway.
func (*List) Rasterize ¶
Rasterize executes the list into buf, an RGBA surface of the list's size, in draw order. It is the reference every GPU back-end is checked against.
func (*List) Reset ¶
func (l *List) Reset()
Reset empties the list for the next frame, keeping its storage.
func (*List) StrokeRect ¶
StrokeRect paints lineW concentric one-pixel outlines inward from r's edge. lineW is capped at MaxCoord: a wider ring cannot fit any recordable rectangle, and the cap bounds the loop whatever the caller passes.
func (*List) StrokeRoundRect ¶
StrokeRoundRect paints lineW concentric rounded outlines, innermost first, each ring's radius one less than the ring outside it; lineW is capped at MaxCoord as in StrokeRect.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package d3d11 draws a gpu2d list on a Direct3D 11 device with a compute kernel, through go-mswin/d3d11 (pure Go, COM through syscall, no cgo).
|
Package d3d11 draws a gpu2d list on a Direct3D 11 device with a compute kernel, through go-mswin/d3d11 (pure Go, COM through syscall, no cgo). |
|
internal
|
|
|
hlslgen
Package hlslgen makes the Direct3D 11 back-end's raster kernel from kernel.wgsl, through go-gfx/wgsl's HLSL emitter, so the raster arithmetic is written once in WGSL (docs/NATIVE.md, decision 1).
|
Package hlslgen makes the Direct3D 11 back-end's raster kernel from kernel.wgsl, through go-gfx/wgsl's HLSL emitter, so the raster arithmetic is written once in WGSL (docs/NATIVE.md, decision 1). |
|
hlslgen/cmd/gendxbc
command
Command gendxbc compiles d3d11/kernel_gen.hlsl with D3DCompile; it can only run on Windows.
|
Command gendxbc compiles d3d11/kernel_gen.hlsl with D3DCompile; it can only run on Windows. |
|
hlslgen/cmd/genhlsl
command
Command genhlsl writes d3d11/kernel_gen.hlsl from kernel.wgsl.
|
Command genhlsl writes d3d11/kernel_gen.hlsl from kernel.wgsl. |
|
mslgen
Package mslgen makes the Metal back-end's raster kernel from kernel.wgsl, through go-gfx/wgsl's MSL emitter, so the raster arithmetic (coverage, tile walk and integer blend) is written once in WGSL instead of once per shading language.
|
Package mslgen makes the Metal back-end's raster kernel from kernel.wgsl, through go-gfx/wgsl's MSL emitter, so the raster arithmetic (coverage, tile walk and integer blend) is written once in WGSL instead of once per shading language. |
|
mslgen/cmd/genmsl
command
Command genmsl writes metal/kernel_gen.metal from kernel.wgsl.
|
Command genmsl writes metal/kernel_gen.metal from kernel.wgsl. |
|
scenes
Package scenes records the random frames every back-end's test holds to gpu2d.Rasterize, so that they all draw the same mix.
|
Package scenes records the random frames every back-end's test holds to gpu2d.Rasterize, so that they all draw the same mix. |
|
spirvgen
Package spirvgen makes the Vulkan back-end's raster kernel from kernel.wgsl, through go-gfx/wgsl's SPIR-V emitter, so the raster arithmetic is written once in WGSL (docs/NATIVE.md, decision 1).
|
Package spirvgen makes the Vulkan back-end's raster kernel from kernel.wgsl, through go-gfx/wgsl's SPIR-V emitter, so the raster arithmetic is written once in WGSL (docs/NATIVE.md, decision 1). |
|
spirvgen/cmd/genspv
command
Command genspv writes vulkan/kernel_gen.spv and vulkan/kernel_rte_gen.spv from kernel.wgsl.
|
Command genspv writes vulkan/kernel_gen.spv and vulkan/kernel_rte_gen.spv from kernel.wgsl. |
|
presentbench
module
|
|
|
Package metal draws a gpu2d list on a Mac's GPU with a compute kernel, through go-macos/metal (pure Go, no cgo).
|
Package metal draws a gpu2d list on a Mac's GPU with a compute kernel, through go-macos/metal (pure Go, no cgo). |
|
Package vulkan draws a gpu2d list on a Vulkan device with a compute kernel, through go-gfx/vulkan (pure Go, no cgo).
|
Package vulkan draws a gpu2d list on a Vulkan device with a compute kernel, through go-gfx/vulkan (pure Go, no cgo). |
|
Package webgpu draws a gpu2d list with WebGPU in the browser (GOOS=js GOARCH=wasm), through go-gfx/webgpu.
|
Package webgpu draws a gpu2d list with WebGPU in the browser (GOOS=js GOARCH=wasm), through go-gfx/webgpu. |