gpu

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 12 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 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 (
	ImageYUV     = 2
	ImageYUVGray = 8
)

ImageYUV is the code an image instance's Params[1] holds for a frame of NV12, plus its matrix (scene.YUVMatrix), plus ImageYUVGray for one in grayscale: the shaders sample its chroma from texture 3 and convert. Params[1] is otherwise 1 for grayscale, or 0.

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 EnableInstances added in v0.9.1

func EnableInstances()

EnableInstances makes builders build the effects that paint quads (scene.EffectOp.Instances), which they leave out before. Package ui calls it as it paints one (Painter.ShaderInstances), on any goroutine: apps that never do carry none of the code.

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 Ages added in v0.6.0

type Ages struct {
	// Forget, unless nil, is called with each buffer Ages stops tracking.
	Forget func(id uintptr)
	// contains filtered or unexported fields
}

Ages keeps what changed in the frames of a window since each buffer of its swap chain was drawn, by an identity of the buffer (the texture of a CAMetalLayer's drawable).

func (*Ages) Add added in v0.6.0

func (a *Ages) Add(changed []image.Rectangle, whole bool)

Add notes a new frame, which changed the rectangles changed of the last, or everything when whole.

func (*Ages) Has added in v0.6.0

func (a *Ages) Has(id uintptr) bool

Has reports whether Ages tracks buffer id.

func (*Ages) Reset added in v0.6.0

func (a *Ages) Reset()

Reset forgets every buffer, as a swap chain makes new ones.

func (*Ages) Take added in v0.6.0

func (a *Ages) Take(id uintptr) (changed []image.Rectangle, known bool)

Take returns what changed in the frames since buffer id was drawn, the buffer the frame is drawn into, in rectangles apart from each other valid until the next Take, and false when that is unknown (the buffer is new, or everything may have changed): the frame draws it whole. It then holds the frame.

type Batch

type Batch struct {
	Start, Count int
	Scissor      Scissor
	// Image is the renderer's texture of the batch's image, or 0, and
	// Chroma, for a video frame in NV12 (scene.SurfaceNV12), the texture of
	// its chroma, whose luma Image is: the shaders sample it as their
	// image's second plane (texture 3), and convert what they sample with
	// the matrix the instance says (ImageYUV).
	Image, Chroma 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
	// Joined, for a batch of ClassGeneral that joined small batches of
	// other classes (see joinLast), has a bit (1 << Class) for each
	// class among them: a renderer may draw it with a shader holding
	// only their paths, which draws the same pixels. 0 is every path.
	Joined uint32
}

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

func (*Batch) Classes added in v0.6.0

func (b *Batch) Classes() uint32

Classes returns the classes of the instances of b, a bit (1 << Class) for each (see Joined).

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
	// NoCornerSplit draws every shape with continuous corners as one
	// instance (see addCorners), for tests that compare the two.
	NoCornerSplit 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
	// (Metal's and Direct3D 11's).
	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
	// NoJoin keeps the batches of each class apart, however small (see
	// joinLast), for tests and measurements that compare the two.
	NoJoin bool
	// NoFold draws the ops that only translate under their transforms (see
	// scene.TranslationFolder), for tests that compare the two.
	NoFold bool
	// OpStart holds, for each op of the scene built, the index of its
	// first instance in Instances, and then their number: the instances of
	// op i are those from OpStart[i] to OpStart[i+1].
	OpStart []int32
	// Culled is how many ops the last Build left out as covered.
	Culled, OpaqueCores int

	// Chroma, unless nil, returns the texture of the chroma of an image
	// showing a frame in NV12 (scene.SurfaceNV12), after the image's
	// texture, its luma, was asked for: renderers that sample such frames
	// set it. Without it, or where it returns 0, the image draws from its
	// pixels in RGBA, as other images do.
	Chroma func(*scene.Image) uintptr
	// 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, rounded (or masked) with content inside that a transform scales, turns, skews or shows in perspective, or reaches into the rounded corners of a clip around it: the others, and those holding content only moved, 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) SplitCorners added in v0.6.0

func (b *Builder) SplitCorners()

SplitCorners makes the builder draw large shapes with continuous corners as nine-patches (see addCorners), for a renderer whose shader draws them (Metal's), and cut clips with continuous corners in that shader where it cuts circular ones (analyticClip). Only through it do apps link that code, which the other renderers' apps never run.

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, or g*x + h*y - 1 when Transform2.z is
	// 1 (scene.Affine.Behind): 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 Part added in v0.7.0

type Part struct {
	Rects []Scissor
	// contains filtered or unexported fields
}

Part is the part of a frame to redraw: rectangles apart from each other, none when nothing changed.

func (*Part) Bounds added in v0.7.0

func (p *Part) Bounds() Scissor

Bounds returns the rectangle around the part's.

func (*Part) Set added in v0.7.0

func (p *Part) Set(changed []image.Rectangle, w, h int, grow func([]image.Rectangle) []image.Rectangle) bool

Set makes p the part of a w×h frame to redraw, given what changed in the buffer drawn into (Ages.Take, or scene.Differ.Diff), grown by grow (the backdrops of effects, scene.Differ.AddBackdrops), and returns false when the frame is better drawn whole: past half the frame together, drawing it whole costs about the same.

func (*Part) String added in v0.7.0

func (p *Part) String() string

String describes the part, for FrameInfo.

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 Pipelines added in v0.6.0

type Pipelines struct {
	// contains filtered or unexported fields
}

Pipelines keeps, for each op of the frames a renderer builds, a hash of the pipelines (shaders, blending) its instances draw with. An op drawn with others than in the last frame (a batch specialized for other classes as batching changed around it, a pipeline made meanwhile, a clip cut by a mask) draws a unit in the last place apart here and there, which a frame drawn whole would change too: its bounds count as changed.

func (*Pipelines) Damage added in v0.6.0

func (p *Pipelines) Damage(b *Builder, pipe func(i int) uint64, d *scene.Differ, damage []image.Rectangle, known bool) []image.Rectangle

Damage hashes the pipelines of the ops b built, pipe(i) being batch i's, and adds to damage, what changed since the last frame drawn as d found (unless not known), the bounds of the ops d matches with an op of the last frame drawn with other pipelines, and returns it.

func (*Pipelines) Remember added in v0.6.0

func (p *Pipelines) Remember()

Remember keeps the hashes of the frame Damage was given last, drawn, to compare the next with.

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.

func (Scissor) Intersect added in v0.6.0

func (s Scissor) Intersect(o Scissor) Scissor

Intersect returns the pixels both s and o take.

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