Documentation
¶
Overview ¶
Package gpu draws for render on WebGPU (gogpu/wgpu): textures images live in, programs drawing triangles into them with a fragment written in WGSL, and the frame's commands, gathered and submitted at once. The engine hands it the window's device (Use); a test or a tool draws without a window (Headless).
Images ¶
A Texture holds 8-bit RGBA with colours premultiplied by alpha and no sRGB conversion. A draw targets a rectangle of a texture, clipping to it; its vertices are in the texture's pixels.
Programs ¶
A Program is a WGSL fragment on a vertex stage of the package's own: the source defines
fn Fragment(dst: vec4<f32>, src: vec2<f32>, color: vec4<f32>, custom: vec4<f32>) -> vec4<f32>
and reads up to four images with imageSrcNAt(p), p in the texture's pixels, and the frame's images (group 3, Draw.Frame, up to FrameImages), which whoever names them declares. Its uniforms are a struct Uniforms, read as U, laid out by a Layout and handed to each draw packed, at most 4096 bytes in all. A mesh program (NewMesh) brings its own vertex stage and draws against a Depth buffer (Mesh); a fragment of its that writes a depth no nearer than its triangle's says so (@early_depth_test(less_equal)), and what lies behind what is drawn is then not worked out where the device can tell (Vulkan). One whose fragment writes a depth (@builtin(frag_depth)) drawn without a buffer is drawn against one of the device's own, the target's size, tested always and never written: WebGPU and Metal make no pipeline of it without a depth.
Frames ¶
Draws are gathered into one command buffer and written to the GPU as it is submitted: at Submit, before pixels are read out of a texture or a big write goes into one, and when the gathered data outgrows its buffers, which then grow to it. A write of pixels lands after every draw made before it, drawn in place among them.
Index ¶
- Constants
- Variables
- func ClearDepth(target Image, dp *Depth)
- func Colored(dst Image, verts []Vertex, indices []uint16, blend Blend)
- func CompileAll(progs []*Program) error
- func CopyDepth(target Image, from, to *Depth)
- func DrawImage(dst, src Image, m Affine, color [4]float32, linear bool, blend Blend)
- func DrawImageIn(dst, src Image, m Affine, linear bool, pts [][2]float32, blend Blend)
- func DrawPieces(dst, src Image, pieces []Piece, color [4]float32, blend Blend)
- func Fill(dst Image, r, g, b, a float32)
- func FillBehind(dst Image, dp *Depth, r, g, b, a float32)
- func FillDepth(target Image, dp *Depth, at Window, depth float32)
- func Headless(software bool) error
- func Mesh(dw *MeshDraw)
- func PlaceDepth(target Image, from, to *Depth, at Window)
- func Present(enc *wgpu.CommandEncoder, view *wgpu.TextureView, ...) error
- func RealizeAll()
- func Sprites(dst, src Image, verts []Vertex, indices []uint16, linear bool, blend Blend)
- func Submit()
- func Triangles(dw *Draw, verts []Vertex, indices []uint16)
- func Use(dev *wgpu.Device, backend gputypes.Backend) error
- func Windowed()
- type Affine
- type Blend
- type Depth
- type DepthTest
- type Draw
- type Image
- type Indices
- type Instances
- type Layout
- type MeshDraw
- type Piece
- type Program
- type Range
- type Rect
- type Sight
- type Texture
- type Uniform
- type Vertex
- type Window
Constants ¶
const DepthFormat = gputypes.TextureFormatDepth32Float
DepthFormat is what a depth buffer holds: 32-bit floats, reversed — 1 nearest, 0 furthest.
const Format = gputypes.TextureFormatRGBA8Unorm
Format is the colour format every texture holds: 8-bit RGBA, premultiplied, no sRGB.
const FrameImages = 9
FrameImages is how many images a frame hands every program (group 3, Draw.Frame): with a draw's own four and its depth, 14 of the 16 a stage samples on a device's default limits.
Variables ¶
var Identity = Affine{A: 1, D: 1}
Identity is the affine that leaves a point where it is.
Functions ¶
func ClearDepth ¶
ClearDepth clears the depth buffer dp, fitted to target's texture.
func CompileAll ¶ added in v0.6.0
CompileAll compiles the programs now, as many at once as there are CPUs, and reports every error.
func CopyDepth ¶
CopyDepth puts what from holds in place of what to holds, both fitted to target's texture: a draw, since a texture copy of a depth is wrong on some backends. From must be Readable.
func DrawImage ¶
DrawImage draws src into dst, its top-left corner at the origin moved by m, its colours times color (premultiplied), sampled between texels where linear.
func DrawImageIn ¶
DrawImageIn draws src into dst as DrawImage does, its top-left corner at the origin moved by m, sampled between texels where linear, only inside the convex polygon pts, in dst's pixels.
func DrawPieces ¶
DrawPieces draws pieces of src into dst, their colours times color (premultiplied).
func FillBehind ¶
FillBehind puts the colour (r, g, b, a), premultiplied, 0 to 1, over all of dst where nothing was drawn into dp: at the furthest depth, tested.
func FillDepth ¶
FillDepth puts depth in place of what dp holds at the pixels of target's texture wholly within the window at, dp fitted to it, the rest kept.
func Headless ¶
Headless gives the package a device of its own, with no window: the best GPU there is, or the software rasteriser when software, which draws this package's programs wrong still.
func PlaceDepth ¶
PlaceDepth puts what from holds, kept at the size it was drawn at, in place of what to holds within the window at of target's texture, to fitted to it: each pixel there takes the depth of the one it maps to, the rest is kept. From must be Readable; one never drawn places nothing.
func Present ¶
func Present( enc *wgpu.CommandEncoder, view *wgpu.TextureView, format gputypes.TextureFormat, w, h int, screen *Texture, ) error
Present draws screen over the window's surface view, w by h pixels of format, into enc, the window's frame encoder. Everything drawn before is submitted first.
func RealizeAll ¶
func RealizeAll()
RealizeAll puts on the GPU every texture whose pixels wait here.
func Sprites ¶
Sprites draws the triangles of verts, their Src in src's texture pixels, sampled between texels where linear.
Types ¶
type Affine ¶
type Affine struct{ A, B, C, D, TX, TY float32 }
Affine maps a point of a source image to the target: x' = A·x + C·y + TX, y' = B·x + D·y + TY.
type Depth ¶
type Depth struct {
// contains filtered or unexported fields
}
Depth is a depth buffer for meshes, the size of the texture they are drawn into.
func NewDepth ¶
func NewDepth() *Depth
NewDepth is a depth buffer, sized at each draw to the texture drawn into.
type DepthTest ¶
type DepthTest uint8
DepthTest is how a draw meets its depth buffer: drawn only where it lies as near as what is there or nearer, and the buffer kept or written; or drawn everywhere, the buffer kept.
type Draw ¶
type Draw struct {
Target Image
Program *Program
Images [4]Image
// Frame is the images of the frame drawn, which every program reads by name; nil a blank.
Frame [FrameImages]*Texture
Uniforms []byte // packed by WGSL's layout (Layout)
Blend Blend
// Place lays the vertices' Dst on the target scaled by its first two and moved by its last
// two; zero, as they are.
Place [4]float32
// Tint multiplies the vertices' colours; zero, white.
Tint [4]float32
// Depth is the depth buffer the draw meets as Test says; nil, none.
Depth *Depth
Test DepthTest
// Sight takes the vertices' DstZ for heights; nil, for depths as they are.
Sight *Sight
}
Draw is a draw of triangles into a rectangle of a texture, clipped to it.
type Image ¶
Image is a rectangle of a texture a draw reads or draws into; a zero Rect is all of it.
type Indices ¶
type Indices struct {
// contains filtered or unexported fields
}
Indices are a mesh's triangles, three vertex indices each, kept on the GPU.
func NewIndices ¶
NewIndices keeps the triangles of indices, going to the GPU at their first draw.
type Instances ¶ added in v0.22.0
type Instances struct {
// contains filtered or unexported fields
}
Instances are an Instanced program's instances, its vec4s each, kept on the GPU.
func NewInstances ¶ added in v0.22.0
NewInstances keeps the instances of data, going to the GPU at their first draw.
type Layout ¶
Layout lays uniforms out as WGSL lays a struct of them out in a uniform buffer: Fields is their WGSL, Size how many bytes they take in all.
type MeshDraw ¶
type MeshDraw struct {
Target Image
Depth *Depth
ClearDepth bool
Write bool
Primed bool
Program *Program
Images [4]Image
// Frame is the images of the frame drawn, which every program reads by name; nil a blank.
Frame [FrameImages]*Texture
Uniforms []byte
Indices *Indices
// Ranges are the ranges of the Indices drawn, at one go; none, every one
Ranges []Range
// without Indices: how many vertices, in order, three a triangle
Vertices uint32
// an Instanced program's: its vec4s an instance, the mesh drawn once each
Instances []float32
// Kept are the instances kept on the GPU, in place of Instances; Spans the ranges of them
// drawn, at one go; none, every one
Kept *Instances
Spans []Range
Blend Blend
// ReadDepth is a depth buffer the program reads (depthAt), another than Depth, at the size it
// was drawn at.
ReadDepth *Depth
// Untested draws everywhere, the Depth kept as it is.
Untested bool
}
MeshDraw is a draw of a mesh (NewMesh) into a rectangle of a texture, tested against the depth buffer Depth and written to it where Write says; without a Depth it is drawn whole, untested. Primed, its depth is laid first alone by the program's fs_prime, and its colour then only where the depth is met exactly.
type Piece ¶
type Piece struct{ DstX, DstY, SrcX, SrcY, W, H float32 }
Piece is a W by H rectangle of a source image, at SrcX, SrcY in it, drawn at DstX, DstY.
type Program ¶
type Program struct {
// contains filtered or unexported fields
}
Program is a WGSL fragment on the package's vertex stage, compiled at first draw.
func NewMesh ¶
NewMesh is a program with a vertex stage of its own, source defining vs_main and fs_main over the package's bindings, its uniforms the WGSL struct fields given.
func NewProgram ¶
NewProgram is the fragment source over the package's vertex stage, its uniforms the WGSL struct fields given, none an empty string.
type Range ¶
type Range struct{ First, Count uint32 }
Range is Count of a mesh's Indices, or of its instances kept, from First.
type Sight ¶
type Sight struct {
Origin, DX, DY [3]float32
Dir, DDX, DDY [3]float32
Z, W [4]float32
Eye [3]float32
Bend float32
}
Sight is how the heights of a draw's vertices become depths: the line of sight through the target's pixel (x, y) starts at Origin + x·DX + y·DY and runs along Dir + x·DDX + y·DDY; where it meets a vertex's height, sunk Bend·d² round Eye, the rows Z and W of the camera's matrix take it.
type Texture ¶
type Texture struct {
// contains filtered or unexported fields
}
Texture is an image's pixels: on the GPU once there is a device, until then kept here.
func (*Texture) ReadPixels ¶
ReadPixels copies the texture, RGBA row by row, into dst: after every draw made so far.
func (*Texture) Release ¶
func (t *Texture) Release()
Release frees the texture; it may not be used after.
func (*Texture) WritePixels ¶
WritePixels puts pix, RGBA w by h, at (x, y) of the texture: after every draw made so far.
type Uniform ¶
Uniform is one of a program's uniforms: Size floats, 1 to 4 (f32, vec2, vec3, vec4), 16 (a mat4x4, column by column) or more, a multiple of 4 (an array of vec4s, one after another).
type Vertex ¶
type Vertex struct {
DstX, DstY, DstZ float32
SrcX, SrcY float32
ColorR, ColorG, ColorB, ColorA float32
Custom0, Custom1 float32
Custom2, Custom3 float32
}
Vertex is one corner of a triangle drawn: where in the target (Dst) and in the first image (Src), both in pixels, its colour and four numbers the fragment reads (Custom). DstZ is its depth, or with the draw's Sight its height in the world.