Skip to content

feat(gpu): stilus CPU renderer, CPU surfaces, shader effects and cera surfaces - #172

Merged
timzifer merged 2 commits into
mainfrom
feat/stilus-cpu-renderer
Nov 17, 2026
Merged

timzifer merged 2 commits into
mainfrom
feat/stilus-cpu-renderer

Conversation

@timzifer

@timzifer timzifer commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds a pure-Go CPU renderer built on stilus (RFC-001 §6.1, "Software (CI, Headless)"), CPU surfaces that work on both backends, Go ports of the built-in shader effects, and a cera adapter that shows PDF pages as lux surfaces.

CPU renderer (internal/gpu/stilus_renderer.go)

  • Draws rects, rounded rects, shadows (inset/outer), linear/radial gradients, tessellated paths, bitmap/MSDF/emoji text, images, blur regions, shader rects, CPU surfaces and overlays, in the same order as WGPURenderer.
  • Renders in horizontal bands on all cores, one stilus.Canvas per band. The output is byte-identical to a single band (tested).
  • app.WithSoftwareRenderer(present): frames go to present. With nil, frames go to the window via GDI on Windows; elsewhere they are only rendered, which is what headless/CI needs.
  • uitest.RenderImage(root, w, h): pixel tests of element trees with real text shaping.

CPU surfaces (draw.SurfaceRasterizer)

  • A ui.SurfaceProvider that also implements RasterizeSurface(dst, rect, clip) is drawn by the renderer:
    • CPU renderer: straight into the framebuffer, no copy.
    • wgpu renderer: into a scratch image, then uploaded.
  • One provider therefore works on both backends. CPU and GPU produce matching pixels (tested on Metal).

Shader effects

  • Noise, plasma and voronoi are ported to Go.
  • New ShaderDesc.CPU (draw.ShaderFunc) supplies a CPU form of custom WGSL. Without it the CPU renderer skips the rect.

contrib/cerasurface (separate module, Go 1.27.1)

  • &cerasurface.Page{Page: p} draws a cera page straight into lux's framebuffer: the framebuffer is aliased to page coordinates, no copy, rendered in bands on all cores.
  • Options: fit-to-width or a fixed scale, scroll offset, per-frame time budget.
  • Its go.mod points at this checkout via replace; switch to a released lux version before tagging it.

wgpu renderer

  • Config.Offscreen, ReadPixels, WaitIdle: render without a window and read the frame back. This uses an optional wgpu.TextureReader, implemented in the gogpu backend.
  • Fix: with several surfaces in one frame, all of them drew at the last surface's rect, because the shared 16-byte instance buffer was rewritten during the render pass.
  • Fix: ensureGPUBuffer dereferenced a nil buffer on its first growth.

Benchmark

Apple M4 (10 cores), macOS, Metal via gogpu, median of 6 runs, ms per frame. Full table and method in docs/cpu-renderer-benchmark.md.

Scene Size CPU (10 cores) CPU (1 core) GPU frame GPU + readback
Dashboard 800×600 0.86 3.2 0.37 1.49
Dashboard 1920×1080 4.3 15.8 0.88 6.0
Dashboard + blur 1920×1080 24.1 100 4.2 7.3
2000 rects 1920×1080 2.4 7.8 0.91 7.6
100 shadows 1920×1080 3.3 11.8 0.55 5.9
Full-frame CPU surface 1920×1080 1.55 – 5.4 –
PDF page (cera), 1600×1130 – 1.27 6.1 – –
  • On screen the GPU stays 2–9× faster. The CPU renderer is not a replacement where a GPU exists.
  • When the pixels are needed in memory (tests, CI, screenshots, remote), the CPU renderer is on par with or faster than GPU + readback, except with blur.
  • CPU surfaces (PDF, software video) are about 3.5× cheaper on the CPU renderer, since they are drawn into the frame without an upload.
  • Blur (exact Gaussian) is the CPU's weak spot.

Type of change

  • New feature (non-breaking change that adds functionality)
  • Breaking change

draw.ShaderDesc (and with it draw.Paint) now holds a func field, so Paint values can no longer be compared with ==. Nothing in the repo compares them.

Testing

  • go build -tags nogui ./... passes
  • go vet -tags nogui ./... passes
  • Unit tests pass (-tags nogui, all packages); renderer tests also pass with -race
  • UI golden-file tests pass
  • Manually verified on: macOS arm64. GPU offscreen tests and the benchmark ran on Metal. They need a GPU, so with -tags nogui they are skipped in CI.
  • Cross-builds pass (CGO_ENABLED=0): windows/amd64 (GUI + nogui), linux/amd64 nogui, darwin/arm64 cocoa, js/wasm. The Windows GDI presentation is build-checked only, not run.
  • New tests:
    • Pixel tests per primitive.
    • Bands vs. single worker produce identical bytes.
    • GPU vs. CPU pixels for surfaces.
    • App headless run with the software renderer.
    • cera surface: offset, clip, scroll, allocations, and use inside a widget tree.

Note: GPU tests and benchmarks lock the OS thread, because Metal autorelease pools are per thread (the app's render loop already does this).

Not in this PR

WGPURenderer never draws ShaderRects: ShaderCache is built but unused, and DrawShaderRect.Time is never set. Built-in effects and custom shaders therefore render only on the CPU backend for now. Tracked in #173.

Checklist

  • I have read CONTRIBUTING.md
  • Documentation updated: docs/platform-backends.md, docs/testing.md, docs/cpu-renderer-benchmark.md

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

All Contributors have signed the CLA. Thanks!
Posted by the CLA Assistant Lite bot.

timzifer and others added 2 commits November 16, 2026 21:47
Add StilusRenderer, a pure-Go CPU backend built on github.com/timzifer/stilus
(RFC-001 §6.1 "Software (CI, Headless)"). It draws rects, shadows, gradients,
paths, bitmap/MSDF/emoji text, images, blur, shader effects, CPU surfaces and
overlays in the wgpu draw order, in horizontal bands on all cores
(byte-identical to a single band).

- app.WithSoftwareRenderer(present): use the CPU renderer; frames go to
  present, or to the window via GDI on Windows.
- uitest.RenderImage: pixel tests of element trees with real text.
- draw.SurfaceRasterizer: surface providers that rasterize on the CPU are
  drawn in place by the CPU renderer and via scratch image + upload by the
  wgpu renderer, so one provider works on both backends.
- Built-in shader effects (noise, plasma, voronoi) ported to Go;
  ShaderDesc.CPU supplies a CPU form of custom WGSL.
- WGPURenderer: offscreen target (Config.Offscreen), ReadPixels and
  WaitIdle via an optional TextureReader on the gogpu backend.
- Fix: multiple surfaces in one frame all drew at the last surface's rect
  (shared 16-byte instance buffer overwritten during the pass).
- Fix: ensureGPUBuffer dereferenced a nil buffer on first growth.
- BenchmarkCompare: CPU vs GPU on dashboard, blur, rect and shadow scenes;
  results in docs/cpu-renderer-benchmark.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cerasurface.Page shows a page rendered by github.com/timzifer/cera in a
ui.Surface. It implements draw.SurfaceRasterizer: cera draws straight into
the CPU renderer's framebuffer (aliased to page coordinates, no copy), in
bands on all cores. Separate module because cera needs Go 1.27.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timzifer
timzifer force-pushed the feat/stilus-cpu-renderer branch from 4560989 to 88aa065 Compare November 16, 2026 20:47
@timzifer
timzifer merged commit 6e10ee0 into main Nov 17, 2026
4 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant