gpupainter

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: BSD-3-Clause Imports: 3 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.

README

gpupainter

A go-widgets painter.Painter for the GPU. Widgets draw into it as they draw into PixelPainter. It records their calls into a go-gfx/gpu2d list, and a GPU back-end draws that list in one submission: gpu2d/webgpu in a browser, or gpu2d/metal on a Mac. Pure Go, no cgo.

l, _ := gpu2d.NewList(w, h)
p := gpupainter.New(l)
root.Draw(p, theme)            // any go-widgets widget tree

r, _ := metal.New(w, h)        // or webgpu.New in a browser
copy(r.Pixels(), background)
_ = r.Draw(l)                  // the frame, on the GPU
l.Reset()                      // next frame

One painter for every frame

New is cheap, but a painter's scratch (the glyph surface text is drawn into, the patch a run of pixels becomes, the pixel index) grows to its working size on the first frame. A painter made per frame pays that again each time: on the gallery frame, 3.2 MB per frame. Keep one and Reset it:

l, _ := gpu2d.NewList(w, h)
p := gpupainter.New(l)
for {
	l.Reset()
	p.Reset(l)                 // no translation, text scale 1, pixels dropped; scratch kept
	root.Draw(p, theme)
	_ = r.Draw(p.List())
}

Reset(l) does not reset l. A steady frame through a reused painter and a reused list allocates nothing (TestSteadyFrameAllocatesNothing, and BenchmarkFrame: 31 allocs, 84 KB and 28.7 µs per frame with New; 0 allocs and 10.0 µs with Reset, medians of 8 interleaved rounds on a small widget frame, M4 Max, load about 7). Only a path drawn under a translation still builds a shifted copy of the path.

Same pixels as PixelPainter

Painter implements every capability PixelPainter has:

  • Clipper and Translator;
  • MaskPainter (glyphs), ImagePainter and PathPainter;
  • RasterPainter (go-widgets/painter v0.17.0). Code that composes pixel by pixel, such as the toolkit's bitmap font, sends it the same PutPixels it sends PixelPainter, rather than a Text call in another font;
  • the built-in 5 × 7 font, with SetTextScale, TextWidth and TextHeight.

TestMatchesPixelPainter draws 2000 random scenes with both painters and compares every byte of the result through gpu2d's CPU reference. The scenes include text at scales 1 to 3, single pixels, masks, scaled images, and filled and stroked paths with lines, quadratics and cubics, under nested clips and translations. On the GPU, gpu2d's Metal back-end equals that reference on every channel. Its WebGPU back-end does too over an opaque surface, which is what a window is.

How each primitive is recorded:

  • Text: the built-in font lights each glyph bit with PutPixel, or an n × n fill when magnified. The string is therefore drawn once in white into a scratch buffer and sent as one coverage mask in the ink, which blends exactly as PutPixel would.
  • Paths: rasterized by the same go-gfx/gfx/vector rasterizer PixelPainter uses, and moved by the translation in force, as PixelPainter does since go-widgets/painter v0.16.2.

Pixels painted one by one

A widget that paints pixel by pixel, such as a colour picker's square, would otherwise cost one instance per pixel. In the go-widgets gallery that was 25,181 of 28,753 instances.

PixelPainter blends each PutPixel onto the ground on its own. So a run of them at distinct positions, under one clip and translation, is recorded as one image whose untouched texels are transparent, and it blends identically. The run is flushed before any other call, when a pixel would land twice, and when its box would pass 2^20 pixels. This takes the gallery to 3,733 instances, still byte-identical.

Call Flush, or take the list with List, before handing the list to a renderer. Otherwise the last run of pixels is still pending and is not drawn.

Speed

From go-gfx/gpu2d on an Apple M4 Max, for a widget-toolkit frame:

frame PixelPainter WebGPU Metal
1080p 13.1 ms 0.99 ms 1.08 ms
4K 52.5 ms 1.62 ms 2.79 ms

Where it does not pay: the go-widgets gallery as it ships is 960 × 2834 pixels at 1× scale. On the same machine it takes 1.98 ms on PixelPainter, and less than half of that is rasterising; the rest is the widgets' own logic, which both paths run. Through this painter and Metal it took about 3.5 ms (load about 8, so indicative): 1.7 ms of recording, widget logic included; 0.26 ms of binning; and 1.7 ms for the GPU draw with its upload and synchronous wait.

The GPU wins when pixels dominate: large surfaces, 4K or HiDPI repaints, images and animation. A small frame at 1× is already cheap on the CPU.

Licence

BSD 3-Clause, see LICENSE.

Documentation

Overview

Package gpupainter is a go-widgets painter.Painter that records what a widget draws into a go-gfx/gpu2d list, which a GPU back-end (gpu2d/webgpu in a browser, gpu2d/metal on a Mac) then draws in one submission. It implements every capability PixelPainter has (clip, translation, masks, images, paths, the built-in font and its scale) and the list it records renders byte-identical to what PixelPainter draws.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Painter

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

Painter records into a gpu2d list. The list is the caller's: Reset it between frames, and take it with List (or call Flush) before handing it to a renderer, so that pixels still being batched are recorded.

func New

func New(l *gpu2d.List) *Painter

New returns a Painter recording into l.

func (*Painter) DrawImage

func (p *Painter) DrawImage(dst painter.Rect, src []byte, srcW, srcH int)

DrawImage draws RGBA pixels scaled into dst by nearest neighbour.

func (*Painter) DrawMask

func (p *Painter) DrawMask(dst painter.Rect, mask []byte, stride int, ink painter.RGBA)

DrawMask paints ink through 8-bit coverage.

func (*Painter) FillPath

func (p *Painter) FillPath(pth *painter.Path, c painter.RGBA, rule painter.FillRule)

FillPath fills pth with c under rule.

func (*Painter) FillRect

func (p *Painter) FillRect(r painter.Rect, c painter.RGBA)

FillRect fills r.

func (*Painter) FillRoundRect

func (p *Painter) FillRoundRect(r painter.Rect, radius int, c painter.RGBA)

FillRoundRect fills r with anti-aliased corners of the given radius.

func (*Painter) Flush added in v0.2.0

func (p *Painter) Flush()

Flush records any pixels still pending. List flushes too; a caller that keeps the list from New and hands it to a renderer calls Flush first.

func (*Painter) List

func (p *Painter) List() *gpu2d.List

List flushes pending pixels and returns the list the painter records into.

func (*Painter) PopClip

func (p *Painter) PopClip()

PopClip removes the innermost clip.

func (*Painter) PopTranslate

func (p *Painter) PopTranslate()

PopTranslate removes the innermost translation.

func (*Painter) PushClip

func (p *Painter) PushClip(r painter.Rect)

PushClip confines drawing to r, translated and intersected as PixelPainter does.

func (*Painter) PushTranslate

func (p *Painter) PushTranslate(dx, dy int)

PushTranslate shifts what is drawn after it. The painter keeps its own copy of the offset for paths, which reach the rasterizer in surface pixels.

func (*Painter) PutPixel

func (p *Painter) PutPixel(x, y int, c painter.RGBA)

PutPixel paints one pixel.

func (*Painter) Raster added in v0.4.0

func (p *Painter) Raster()

Raster marks the Painter as a painter.RasterPainter: PutPixel lands exactly PixelPainter's pixel (batched into an image, blended identically). Code that composes pixel by pixel, such as the toolkit's bitmap font, therefore sends it the same pixels it sends PixelPainter rather than a Text call.

func (*Painter) Reset added in v0.3.0

func (p *Painter) Reset(l *gpu2d.List)

Reset points the painter at l and returns it to the state New leaves it in: no pending pixels (they are dropped, not recorded: Flush first to keep them), no translation, text scale 1. It keeps every scratch buffer (the glyph surface, the pixel-run patch and index), so a painter reused for each frame allocates nothing once its buffers have grown. It does not reset l: call l.Reset() for a new frame, then p.Reset(l) (or p.Reset(p.List())).

func (*Painter) SetTextScale

func (p *Painter) SetTextScale(n int)

SetTextScale magnifies the built-in font by a whole number (1 at least).

func (*Painter) Size

func (p *Painter) Size() (int, int)

Size is the list's surface size.

func (*Painter) StrokePath

func (p *Painter) StrokePath(pth *painter.Path, c painter.RGBA, width float64)

StrokePath paints pth's outline width pixels wide.

func (*Painter) StrokeRect

func (p *Painter) StrokeRect(r painter.Rect, c painter.RGBA, lineW int)

StrokeRect paints lineW concentric outlines inward from r's edge.

func (*Painter) StrokeRoundRect

func (p *Painter) StrokeRoundRect(r painter.Rect, radius int, c painter.RGBA, lineW int)

StrokeRoundRect paints lineW concentric rounded outlines.

func (*Painter) Text

func (p *Painter) Text(x, y int, s string, ink painter.RGBA)

Text paints s in the built-in 5 × 7 font. PixelPainter lights each glyph bit with PutPixel (or an n × n fill when magnified) in the ink, and glyphs never overlap; so the string is drawn once in opaque white into a scratch buffer with PixelPainter itself, and its alpha goes through DrawMask with the real ink. Coverage 255 blends exactly as PutPixel(ink) does.

func (*Painter) TextHeight

func (p *Painter) TextHeight() int

TextHeight is how tall a line of the built-in font is drawn.

func (*Painter) TextScale

func (p *Painter) TextScale() int

TextScale is the magnification of the built-in font.

func (*Painter) TextWidth

func (p *Painter) TextWidth(s string) int

TextWidth is how wide s is drawn in the built-in font.

Jump to

Keyboard shortcuts

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