gpu

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package gpu turns scenes into what the GPU renderers draw: one instanced quad per operation, for a shader that computes rounded rectangles, borders, gradients, stripes, shadows and atlas coverage itself, in batches that share a scissor rectangle and an image. Every renderer (d3d11, metal, gl) draws the same instances, the way internal/raster draws scenes on the CPU.

An effect (scene.Effect) draws with a pipeline of its own, made from its shader, in a batch of its own. One reading its backdrop has its batch start after a Backdrop step, which renderers take between batches: they read the backdrop's area of what they drew so far, average it over squares (the pass "down") into a texture, blur that along rows into another and along columns back (the pass "blur"), keeping 8 bits a channel, and bind it for the batch (the shaders' backdrop texture), as internal/raster computes it.

The shaders write a second color for blending, the source's alpha for each channel: the alpha of the color for everything but subpixel glyphs, whose subpixels cover each channel by its own. Renderers blend with dual-source blending: the destination times one minus the second color, plus the first.

A hole (scene.OpHole) is an opaque fill in a batch of its own, which renderers blend with none of the source: the destination times one minus the second color, its coverage, which leaves it transparent.

Index

Constants

View Source
const (
	// PassClear reads what lies beyond the area (down) or the texels
	// (blur) as transparent, where it would repeat the last ones.
	PassClear = 1 << iota
	// PassUnflipped tells OpenGL's pass down that its source's rows go
	// down, as an image uploaded: its row y is y less Shift.y.
	PassUnflipped
)

Modes of passes (Pass.Mode).

View Source
const BackdropImage = 2

BackdropImage is the image of a shader of package ui/shader that holds its backdrop, when it reads it (internal/shader.BackdropImage): renderers bind their backdrop's texture in its place.

View Source
const InstanceSize = int(unsafe.Sizeof(Instance{}))

InstanceSize is the size of an Instance in bytes.

View Source
const StopsWidth = 1024

StopsWidth is how many texels a row of the texture of a frame's stops holds (Builder.Stops).

Variables

This section is empty.

Functions

func GoBytes added in v0.2.10

func GoBytes(name string, b []byte) string

GoBytes returns the Go declaration of a variable name holding b, as code compiled ahead of time is generated.

func SourceSum

func SourceSum(src string) string

SourceSum returns the SHA-256, in hex, of a shader's source as the repository holds it, with LF line endings, which shaders compiled ahead of time record: the source matches them however it was checked out, as with CRLF endings on Windows.

Types

type Batch

type Batch struct {
	Start, Count int
	Scissor      Scissor
	// Image is the renderer's texture of the batch's image, or 0.
	Image uintptr
	// ClipMask is a cached nested clip texture, sampled in frame coordinates.
	ClipMask uintptr
	// Effect is the effect of the batch's one instance, drawn with a
	// pipeline of its own, and Backdrop, unless 0, 1 + the index in
	// Builder.Backdrops of its backdrop, which the renderer reads before
	// drawing it.
	Effect   *scene.Effect
	Backdrop int
	// Shader is the effect's op, for a shader's (Effect.Shader), and
	// Textures the renderer's textures of its images, 0 for none.
	Shader   *scene.EffectOp
	Textures [4]uintptr
	// Uniforms are the words of the shader's uniforms the batch draws
	// with: the op's, or its wide ones for a wide gamut (Wide).
	Uniforms []uint32
	// Hole tells that the batch's instances make their shapes transparent
	// (scene.OpHole): renderers blend them with a source factor of zero.
	Hole bool
	// Opaque tells that every fragment the batch's instances draw within
	// its scissor rectangle is opaque, with coverage exactly 1 (the cores of
	// opaque fills, see addOpaque): renderers may draw it without blending,
	// which writes the same bytes, and which lets a tile-based GPU
	// discard whatever it drew under them before shading it.
	Opaque bool
	// Class is the paths of the shader that the batch's instances need
	// (see Class), and Continuous whether any of them has continuous
	// corners, in its shape or in the clip the shader cuts; renderers that do
	// not specialize their shader ignore them.
	Class      Class
	Continuous bool
}

Batch is a run of instances that draw with the same scissor rectangle and image.

type Builder

type Builder struct {
	Instances []Instance
	Batches   []Batch
	Backdrops []scene.Backdrop
	// Stops holds the stops of the gradients the instances draw with
	// (scene.Stop), two RGBA texels of 32-bit floats each, StopsWidth to
	// a row: the color, straight sRGB, then its place and its color in
	// Oklab. An instance painting with stops has the first's index and
	// how many in Color2.
	Stops []float32
	// BlurredImage, unless nil, returns the texture of an image's area
	// blurred as bk says (scene.ImageBlur), bk.Size() texels, rows going
	// down, which the renderer computes before the frame's pass: the
	// image's texture averaged over squares, blurred along rows and along
	// columns (ImageDownPass, ImageBlurPass). Without it, images draw
	// unblurred.
	BlurredImage func(img *scene.Image, bk scene.Backdrop) uintptr
	// Wide draws the colors of scenes outside the sRGB gamut
	// (Scene.Wide), for a target that keeps them, as Metal's float16
	// drawables do; without it, their nearest sRGB colors.
	Wide bool
	// NoShadowSplit draws every shadow as one instance, for comparison tests.
	NoShadowSplit bool
	// NoRingSplit draws every border-only fill as one instance (see
	// addFill), for tests that compare the two.
	NoRingSplit bool
	// NoClasses puts every batch in ClassGeneral, for tests and
	// measurements that compare the specialized shaders with the general one.
	NoClasses bool
	// Classes separates batches for renderers with specialized fragment shaders.
	Classes bool
	// GeneralClasses has a bit (1 << Class) for each class that draws with the
	// general shader, for tests that find which specialized one differs.
	GeneralClasses uint32
	// Opaque lets large opaque fills be drawn as an opaque core with blended
	// edges around it (see addOpaque), which a renderer sets that gains from
	// drawing the cores without blending (Metal's tile-based GPUs: hidden
	// surfaces are discarded): the others would only draw more calls for the
	// same pixels. Tests and measurements switch it to compare the two.
	Opaque bool
	// NoCull draws every op, even those that later opaque fills cover whole
	// (see planCull), for tests that compare the two.
	NoCull bool
	// GeneralFill makes the shaders take their general path for every fill,
	// and NoClipSkip makes them compute the clip's coverage where it is 1
	// (see markFill), for tests and measurements that compare them.
	GeneralFill, NoClipSkip bool
	// ClipSkip enables the clip-coverage flag understood by the Metal shader.
	ClipSkip bool
	// Culled is how many ops the last Build left out as covered.
	Culled, OpaqueCores int
	// contains filtered or unexported fields
}

Builder builds the instances and batches of scenes, reusing its memory from frame to frame.

func (*Builder) BackdropSize added in v0.2.10

func (b *Builder) BackdropSize() (w, h int)

BackdropSize returns how large the textures of the backdrops of the last scene built must be, in texels: as the largest.

func (*Builder) Build

func (b *Builder) Build(s *scene.Scene, image func(*scene.Image) uintptr) *scene.Scene

Build turns a scene directly into GPU instances, retaining local geometry and composing transforms in the vertex shader. Nested affine clips use cached masks; stationary clipping never rasterizes again as content moves. A clip is masked only when it is transformed, nested in a transformed clip, or rounded (or masked) with transformed content inside: the others cut in the shader as in a scene without transforms.

func (*Builder) Release added in v0.4.3

func (b *Builder) Release()

Release frees the retained clipping resources and instance buffers.

func (*Builder) StopsSize added in v0.5.5

func (b *Builder) StopsSize() (w, h int)

StopsSize returns how large the texture of the frame's stops must be, in texels, for Stops: StopsWidth across, or 0 by 0 for none.

func (*Builder) Summary added in v0.5.0

func (b *Builder) Summary() string

Summary describes the last scene built, for logs (MYGO_FRAME_STATS): how many instances in how many batches, how many ops were left out as covered, how many fills drew as opaque cores, and the batches of each class.

type Class added in v0.5.0

type Class uint8

Class says which paths of the fragment shader a batch's instances take. Renderers specialize their shader for it (Metal's function constants, a #define of GLSL, a macro of HLSL): the general shader holds the code of every path, and registers for the heaviest, so the plain fills most of a window is made of ran at a fraction of the speed it has without the rest. A batch holds instances of one class.

const (
	// ClassGeneral is the whole shader; transformed instances, effects and
	// anything else use it.
	ClassGeneral Class = iota
	// ClassPlain is fills that are solid, at opacity 1, without a border, not
	// transformed.
	ClassPlain
	// ClassFill is the other fills (gradients, stripes, borders, dashes), not
	// transformed.
	ClassFill
	// ClassShadow is shadows, ClassGlyph mask glyphs, ClassSubpixel
	// subpixel glyphs and ClassImage images and color glyphs, not transformed.
	ClassShadow
	ClassGlyph
	ClassSubpixel
	ClassImage
	// NumClasses is how many classes there are.
	NumClasses
)

type Instance

type Instance struct {
	// Rect is x, y, width, height; Radii the corners' radii (top-left,
	// top-right, bottom-right, bottom-left), negative for continuous
	// corners (scene.Corners), which only Metal's shader draws, or a
	// glyph's gamma ratios
	// (scene.TextParams); Inner those of a border's inner edge, or of the
	// box casting a shadow, or a glyph's contrast and thin boost in Inner[0]
	// and Inner[1].
	Rect, Radii, Inner [4]float32
	// Color and Color2 (a gradient's end) and Border are straight RGBA;
	// Color tints an image (white for none).
	Color, Color2, Border [4]float32
	// Grad holds a gradient's start and end points, or the stripes' unit
	// vector across them, width and period; UV the texture rectangle,
	// normalized, a fill's border widths (top, right, bottom, left), or
	// the box casting a shadow, which shows only outside it (none when
	// empty).
	//
	// An effect has its parameters (scene.EffectOp.Params) in Inner,
	// Color, Color2, Border and Grad, and in UV where its backdrop's area
	// starts in the frame and its size in texels.
	Grad, UV [4]float32
	// Clip and ClipRadii are the innermost clip, which the shader cuts.
	Clip, ClipRadii [4]float32
	// Params is the kind (0 fill, 1 shadow, 2 mask glyph, 3 color glyph, 4
	// image, 5 subpixel glyph, 6 effect); 1 for a dashed border, a
	// grayscale image or an inner shadow, whose box is in Rect and Radii
	// and the hole it leaves in UV and Inner (none when empty); the
	// shadow's sigma (0 for none), the paint (scene.Paint), 1 for an image
	// sampled at its nearest texel, or the size of the squares an effect's
	// backdrop averages; and the opacity.
	Params [4]float32
	// Transform rows map local pixels to the frame. Transform0.w enables
	// transformed antialiasing, Transform1.w selects how the clip applies: 0
	// the clip rectangle cut by the shader, 1 a cached clip mask, 2 a hard
	// clip over a masked one, and 3 none, as the builder found the clip's
	// rectangle to hold the whole quad with room for the antialiasing (see
	// markFill): the shaders then skip computing it.
	// Transform2 holds the projective row's g and h, the frame point of
	// local (x, y) being Transform0's and Transform1's dot products with
	// (x, y, 1) over g*x + h*y + 1: shaders interpolate in perspective.
	Transform0, Transform1, Transform2 [4]float32
}

Instance is the data of one quad, in device pixels, as the shaders read it: thirteen float4s.

type Pass added in v0.2.10

type Pass struct {
	Origin, Limit, Shift, Dir [2]int32
	Down, Radius              int32
	Sigma                     float32
	// Mode is PassClear for passes reading an image to blur (scene.Op's
	// Blur), and PassUnflipped for those of OpenGL reading an image whose
	// rows go down.
	Mode int32
}

Pass is what a pass computing a backdrop reads, the layout of the shaders' Pass: down reads the area from Origin to Limit, frame pixels, at their place less Shift in its source, and averages squares of Down pixels a side; blur reads texels up to Limit, Radius of them each way along Dir, weighted by a Gaussian of standard deviation Sigma.

func BlurPass added in v0.2.10

func BlurPass(bk scene.Backdrop, dir [2]int32) Pass

BlurPass returns the pass blurring bk's texels along rows (dir 1, 0) or columns (0, 1).

func DownPass added in v0.2.10

func DownPass(bk scene.Backdrop, shift [2]int32) Pass

DownPass returns the pass averaging the squares of bk's area, whose frame pixel (x, y) is at (x, y) less shift in the texture it reads.

func ImageBlurPass added in v0.5.5

func ImageBlurPass(bk scene.Backdrop, dir [2]int32) Pass

ImageBlurPass returns the pass blurring the texels of an image's blur along rows (dir 1, 0) or columns (0, 1), transparent beyond them.

func ImageDownPass added in v0.5.5

func ImageDownPass(bk scene.Backdrop) Pass

ImageDownPass returns the pass averaging the squares of an image's area that bk blurs (scene.ImageBlur), reading its texture's texels where they are, transparent beyond the area.

type Scissor

type Scissor struct{ Left, Top, Right, Bottom int32 }

Scissor is a scissor rectangle in device pixels, the layout of a Direct3D RECT.

func (Scissor) Empty

func (s Scissor) Empty() bool

Empty reports whether the rectangle has no pixels.

Directories

Path Synopsis
Package d3d11 draws scenes with Direct3D 11 into a window through a DXGI flip model swap chain, called through syscall like the rest of the Windows backend (no cgo).
Package d3d11 draws scenes with Direct3D 11 into a window through a DXGI flip model swap chain, called through syscall like the rest of the Windows backend (no cgo).
device
Package device creates Direct3D 11 devices: the device of package d3d11's renderer, and that of the Windows backend, which shows the controls of windows with a material through DirectComposition without the renderer.
Package device creates Direct3D 11 devices: the device of package d3d11's renderer, and that of the Windows backend, which shows the controls of windows with a material through DirectComposition without the renderer.
Package gl draws scenes with OpenGL 3.3, OpenGL ES 3.0 or WebGL 2 into the framebuffer bound when Render is called: the one GTK's GtkGLArea binds while it emits its render signal, or a canvas's (package web).
Package gl draws scenes with OpenGL 3.3, OpenGL ES 3.0 or WebGL 2 into the framebuffer bound when Render is called: the one GTK's GtkGLArea binds while it emits its render signal, or a canvas's (package web).
Package gputest has what the tests of the GPU renderers share: a scene that draws every kind of operation, and a comparison of what a renderer drew with what the CPU renderer draws, which the renderers must match.
Package gputest has what the tests of the GPU renderers share: a scene that draws every kind of operation, and a comparison of what a renderer drew with what the CPU renderer draws, which the renderers must match.
Package metal draws scenes with Metal into a CAMetalLayer, called through the Objective-C runtime with purego like the rest of the macOS backend (no cgo).
Package metal draws scenes with Metal into a CAMetalLayer, called through the Objective-C runtime with purego like the rest of the macOS backend (no cgo).

Jump to

Keyboard shortcuts

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