gpu

package
v0.24.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

DepthFormat is what a depth buffer holds: 32-bit floats, reversed — 1 nearest, 0 furthest.

Format is the colour format every texture holds: 8-bit RGBA, premultiplied, no sRGB.

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

View Source
var Identity = Affine{A: 1, D: 1}

Identity is the affine that leaves a point where it is.

Functions

func ClearDepth

func ClearDepth(target Image, dp *Depth)

ClearDepth clears the depth buffer dp, fitted to target's texture.

func Colored

func Colored(dst Image, verts []Vertex, indices []uint16, blend Blend)

Colored draws the triangles of verts in their colours, premultiplied.

func CompileAll added in v0.6.0

func CompileAll(progs []*Program) error

CompileAll compiles the programs now, as many at once as there are CPUs, and reports every error.

func CopyDepth

func CopyDepth(target Image, from, to *Depth)

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

func DrawImage(dst, src Image, m Affine, color [4]float32, linear bool, blend Blend)

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

func DrawImageIn(dst, src Image, m Affine, linear bool, pts [][2]float32, blend Blend)

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

func DrawPieces(dst, src Image, pieces []Piece, color [4]float32, blend Blend)

DrawPieces draws pieces of src into dst, their colours times color (premultiplied).

func Fill

func Fill(dst Image, r, g, b, a float32)

Fill puts the colour (r, g, b, a), premultiplied, 0 to 1, in place of all of dst.

func FillBehind

func FillBehind(dst Image, dp *Depth, r, g, b, a float32)

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

func FillDepth(target Image, dp *Depth, at Window, depth float32)

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

func Headless(software bool) error

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 Mesh

func Mesh(dw *MeshDraw)

Mesh draws the triangles of dw's Indices with its program.

func PlaceDepth

func PlaceDepth(target Image, from, to *Depth, at Window)

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

func Sprites(dst, src Image, verts []Vertex, indices []uint16, linear bool, blend Blend)

Sprites draws the triangles of verts, their Src in src's texture pixels, sampled between texels where linear.

func Submit

func Submit()

Submit sends every draw gathered so far to the GPU.

func Triangles

func Triangles(dw *Draw, verts []Vertex, indices []uint16)

Triangles draws the triangles indices picks out of verts as dw says.

func Use

func Use(dev *wgpu.Device, backend gputypes.Backend) error

Use has the package draw on dev, the device of the engine's window.

func Windowed

func Windowed()

Windowed says the device will be the engine's window's: none is made without it.

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.

func (Affine) Apply

func (m Affine) Apply(x, y float32) (float32, float32)

Apply is where the affine takes (x, y).

type Blend

type Blend uint8

Blend is how a draw lays its colour over what is there.

const (
	// SourceOver lays a premultiplied colour over what is there, as its alpha covers it.
	SourceOver Blend = iota
	// Copy puts the colour in place of what is there.
	Copy
	// Darkest keeps the lesser of the colour and what is there, channel by channel.
	Darkest
)

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.

func (*Depth) Readable

func (dp *Depth) Readable() *Depth

Readable has dp read as well as drawn against (MeshDraw.ReadDepth, CopyDepth); asked before its first draw, it keeps what it holds. A buffer only drawn against is kept packed, faster.

func (*Depth) Release

func (dp *Depth) Release()

Release frees the buffer.

func (*Depth) Size

func (dp *Depth) Size() (w, h int)

Size is the buffer's width and height: those of the texture it was last drawn with.

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.

const (
	Tested DepthTest = iota
	Written
	Untested
)

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

type Image struct {
	Texture *Texture
	Rect    Rect
}

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

func NewIndices(indices []uint32) *Indices

NewIndices keeps the triangles of indices, going to the GPU at their first draw.

func (*Indices) Release

func (x *Indices) Release()

Release frees the indices on the GPU.

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

func NewInstances(data []float32) *Instances

NewInstances keeps the instances of data, going to the GPU at their first draw.

func (*Instances) Release added in v0.22.0

func (x *Instances) Release()

Release frees the instances on the GPU.

type Layout

type Layout struct {
	Fields string

	Size int
	// contains filtered or unexported fields
}

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.

func NewLayout

func NewLayout(us []Uniform) *Layout

NewLayout lays out us in order.

func (*Layout) Put

func (l *Layout) Put(dst []byte, name string, v []float32)

Put writes v, as many floats as the uniform name holds at most, into dst, the layout's bytes; a name the layout lacks is ignored.

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

func NewMesh(label, uniformFields, source string) *Program

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

func NewProgram(label, uniformFields, source string) *Program

NewProgram is the fragment source over the package's vertex stage, its uniforms the WGSL struct fields given, none an empty string.

func (*Program) Compile

func (p *Program) Compile() error

Compile compiles the program now rather than at its first draw.

func (*Program) Instanced

func (p *Program) Instanced(vec4s int) *Program

Instanced has the mesh program p take vec4s vectors an instance, at the vertex stage's locations 0 up.

func (*Program) Source

func (p *Program) Source() string

Source is the program's whole WGSL.

type Range

type Range struct{ First, Count uint32 }

Range is Count of a mesh's Indices, or of its instances kept, from First.

type Rect

type Rect struct{ X, Y, W, H int }

Rect is a rectangle of a texture, in pixels.

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 NewTexture

func NewTexture(w, h int) *Texture

NewTexture is a w by h texture, transparent.

func (*Texture) ReadPixels

func (t *Texture) ReadPixels(dst []byte)

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) Size

func (t *Texture) Size() (w, h int)

Size is the texture's width and height in pixels.

func (*Texture) WritePixels

func (t *Texture) WritePixels(x, y, w, h int, pix []byte)

WritePixels puts pix, RGBA w by h, at (x, y) of the texture: after every draw made so far.

type Uniform

type Uniform struct {
	Name string
	Size int
}

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.

type Window

type Window struct{ X, Y, W, H float32 }

Window is a part of a texture in its pixels: its top-left corner and its size.

Jump to

Keyboard shortcuts

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