Files
chandra/docs/rendering.md
T
nandiandClaude Opus 5.5 cfb25fe699 Draw on the GPU: the desktop with OpenGL ES, apps through linux-dmabuf
The host's rasteriser has a GPU half (raster_gpu.c): the same primitives, drawn the same way, on
machines with a real GPU and OpenGL ES 3, straight into the display's buffers in a login session.
EGL and GLES are loaded at run time; without them everything is drawn on the CPU as before
(CHANDRA_GPU=0 does that too). zwp_linux_dmabuf_v1 lets apps draw on the GPU as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 16:42:22 -08:00

105 lines
6.1 KiB
Markdown

# Rendering, animations and machines without a GPU
On a machine with a GPU the host (build/wasmhost) draws the desktop on it, and apps hand over the
buffers they drew on it. Anywhere else the host rasterises each frame on the CPU and apps use shared
memory, as before. The component never knows the difference: it sends the same draw operations.
## Drawing on the GPU
At start the host looks for a GPU and logs it (or why there is none):
gpu: drawing on Mesa Intel(R) HD Graphics 4600 (HSW GT2)
- **The desktop** (`wasmhost/bin/raster_gpu.c`): OpenGL ES 3 on a DRM render node, with libEGL and
libGLESv2 loaded at run time, so neither is needed to build or to run without a GPU. It takes
raster_stub.c's calls and draws the same shapes (within rounding), into a texture that keeps its
contents, so only damage is redrawn.
- **The display** (`--drm`): the finished frame is drawn into the display's own buffers (the DRM dumb
buffers, shared as dmabufs), scaled and turned as the output is. When the GPU can't draw there,
the frame is read back and copied, which works on any display.
- **A nested window, X11, screenshots, recordings, the remote mirror** need the pixels on the CPU: the
part that changed is read back when one of them asks.
- **Apps** (`compositor/wayland/dmabuf.mbt`): `zwp_linux_dmabuf_v1` version 4. Without it Mesa's EGL and
Vulkan draw on the CPU whatever GPU there is. A committed buffer is copied on the GPU and
released at once. Formats: 8-bit RGB with or without alpha, in every layout the driver samples.
Which GPU: the first render node whose driver is a real one (the list below), or the one
`CHANDRA_RENDER_NODE` names. `CHANDRA_GPU=0`, `CHANDRA_RENDER=software` and `LIBGL_ALWAYS_SOFTWARE=1`
keep to the CPU. `CHANDRA_GPU=1` takes whatever OpenGL there is (a virtual machine's, llvmpipe,
NVIDIA's). NVIDIA's proprietary driver is not used by itself: the code relies on implicit sync
(the kernel ordering buffer use), which that driver lacks.
If a login session dies within seconds of first drawing on the display with the GPU, the next one
keeps to the CPU once (`~/.local/state/chandra/gpu-starting`).
Not done: handing an app's buffer to the display without any copy, dmabufs to the host compositor
when nested, recovering from a GPU reset, OpenGL ES 2 only GPUs (they use the CPU).
`scripts/test-gpu.sh` (`just test-gpu`) compares both rasterisers on this machine's GPU and draws
through real display buffers, without needing a free display.
On an HD 4600 (Haswell), 1920x1080, median time to rasterise a frame, CPU then GPU: the overview
gliding with four windows 8 ms, 1 ms; a full-screen terminal scrolling 8 ms, 3 ms. alacritty (OpenGL)
scrolling: 7 frames a second drawing in software, 42 on the GPU. vkcube (Vulkan) showed
no frames at all without dmabufs, and 57 a second with them.
## Software or hardware mode
Apart from where the pixels are drawn, the shell asks less of a machine without a GPU. At start it
looks for one and logs what it decided:
rendering: software (no GPU found)
rendering: hardware (GPU: amdgpu)
- A DRM render node (`/sys/class/drm/renderD*`) whose driver drives a real GPU (i915, xe,
amdgpu, radeon, nouveau, nvidia, SoC GPUs), or NVIDIA's proprietary driver
(`/proc/driver/nvidia/version`), means **hardware**.
- No render node, or only display-only / virtual ones (simpledrm, vkms, virtio_gpu, qxl, bochs,
vmwgfx, ast, ...), or `LIBGL_ALWAYS_SOFTWARE=1`, means **software**.
Overrides, strongest first:
- `CHANDRA_RENDER=software` or `CHANDRA_RENDER=hardware` in the environment.
- Settings > Session > **Lighter animations (no GPU found / GPU found)**: on is software mode.
Saved in state.edn as `:render :software` or `:render :hardware`, only when it differs from
what was detected (so the same state.edn follows you between machines).
What software mode changes: window previews that keep changing (a video in the overview or
Alt+Tab) are remade every 200 ms instead of 100 ms, and a desktop module (Live Starfield) gets at
most ~30 frames a second however often it asks.
## What makes animations smooth everywhere
- At most one frame is drawn ahead of the output: while a drawn frame waits for the page flip in
flight (KMS) or for the previous frame to be shown (nested, X11), no other one is drawn. Before,
frames drawn meanwhile were thrown away (the KMS loop runs at 125 Hz), each costing a whole
frame of CPU. Drawing the next frame while the X server copies the last one keeps both busy:
waiting for the copy first halved the frame rate on Xorg. An output that never says it is ready
(a hidden nested window) still gets a frame every 250 ms.
- A backdrop (wallpaper or desktop module) whose ops change every frame but begin the same, like
Live Starfield's sky and nebulae under moving stars, keeps that common start cached as pixels and
draws only the rest over a copy of it (MoonBit host).
- Animations are timed by the clock, so a slow frame makes the next one jump ahead instead of
slowing the animation down.
- The rasteriser blends two colour channels at a time (16-bit lanes, exact division by 255),
draws filled circles as row spans (big translucent discs cost a square root per pixel before),
and scales images with 32-bit sums.
- Window previews gliding in the overview are sampled straight from the preview at each frame's
size (nearest pixel), not filtered and cached at a size used once; once the overview settles
they are drawn at the size they were made at. On the MoonBit host other large images drawn
at a new size every frame are treated the same way from their second frame on.
## Measuring
`CHANDRA_FRAME_TRACE=1` logs one line per drawn frame:
frame 1795657160219 compose 1 raster 3
(the clock in ms, then how long composing the scene and rasterising it took). The benchmark in
the project files (`smooth-anim/anim-bench.sh`) runs a headless 1920x1080 session with four
windows, opens and closes the overview six times and runs Live Starfield, and prints frame
rates, frame gaps and raster times from that trace.
Window frames, their shadow included, are draw ops from `@scene.build_window_decoration`
(docs/window-frames.md): plain spans, redrawn only when the window or what is above it changes.