Culling and the frame
Image tests: golden images
A unit test checks a number. A renderer's result is a picture, and most of its bugs show only there: a shadow in the wrong place, a black material, a missing mesh. An image test renders a fixed scene, reads the frame back, and compares it with a stored image that a person once checked: the golden image. This page is about the compare: when two pictures count as "the same", and how to keep that answer honest.
In short
- Render a fixed scene headless, read the frame back, compare it with the golden. On a failure, save the new image and a difference image that shows where it changed.
- Exact equality is too strict: the same code gives slightly different pixels on another GPU, driver or CPU. A test needs a tolerance.
- A simple, strong rule: a pixel differs when a channel is off by more than T; the test fails when more than F of the pixels differ. Set T and F from the noise you measure, not by guess.
- Perceptual metrics such as FLIP say how visible a difference is. They answer "would a person notice?", which is a different question from "did the code change?".
- Goldens belong to one driver. A software rasterizer gives the same pixels on every machine and needs no GPU, so continuous integration can run it.
- A golden changes only on purpose: the change that moves it says why, with the image before and after.
1One image test
The test is the same four steps for every scene. Only the last step needs a decision: same or not.
Everything before the compare must give the same frame on every run: all loads done before the first frame, a fixed frame count, animation at fixed times, no on-screen statistics. Headless rendering and readback explains why each of these matters.
2Why exact equality fails
Rendering is floating-point math, done in parallel, by code you do not own: the driver compiles your shaders, and the hardware rounds. The same scene, with the same code, can come out a little different:
Another GPU or driver
Each compiles the shaders its own way and rounds differently: a·b + c fused or not, faster sin and exp, another texture filter.
Another order
A parallel sum, blending or a reduction that runs in another order rounds differently. A wider SIMD unit or more threads changes the order.
Ties
A sample exactly on an edge or a depth exactly equal to another: tiny changes in math flip which side wins, and one pixel jumps by a lot.
So a good test has two parts: the noise it accepts, measured, and the bugs it must catch. A test that fails on noise teaches people to ignore it. A test so loose that it passes a real bug is worse than none.
3A threshold and a share of pixels
The rule this page uses has two numbers. The first, T, asks "is this pixel different?". The second, F, asks "are enough pixels different to fail?".
diff(p) = max over r, g, b, a of | actual(p) − golden(p) |
p differs ⇔ diff(p) > T
fail ⇔ (pixels that differ) / (all pixels) > F
- T
- the largest change of one 8-bit channel that is still noise, in steps of 1/255
- F
- the largest share of pixels allowed over T: the rare ties and edge pixels
T alone fails on a single tie pixel. F alone ignores how big a change is. Together they let through many tiny changes, or a few big ones, but not many big ones. Try it on a small image: three versions of a new render, one with noise only and two with a bug.
Set both numbers from a measurement: run the same code twice (and on two machines, if the test runs on more than one), compare, and read the largest difference and the share over each T. Choose T a little above the noise, and F a little above the share of outliers. Write the numbers down next to the test, so the next person knows why they are what they are.
If a test fails on noise, first find where the noise comes from: a timer, a thread count, a random seed. A tolerance hides a problem only until a real bug hides behind it.
4Other ways to compare
There are many metrics. They differ in the question they answer:
Exact match (hash)
Cheapest and strictest. Works when the whole chain is the same and nothing is parallel in an unknown order. Breaks on the first rounding change.
RMSE, PSNR
The mean of the squared differences. One wrong object in a corner hides in the mean of a large, correct image.
SSIM
Compares local brightness, contrast and structure in small windows. Good for compression and filtering; hard to turn into a pass or fail you can explain.
FLIP
NVIDIA's metric models how a person sees two images flipped back and forth: color, edges, viewing distance. It gives a map of how visible each change is.
FLIP answers "how much would a person notice?". That is the right question when two different renderers must agree, or when you tune a denoiser. A regression test asks "did my change move these pixels?". A change that nobody would notice, like a shadow one pixel off, is still a change the author should know about. So a regression test wants the strict, simple rule, with a tolerance only for noise. FLIP stays useful to judge a failure: is the difference big, or only real?
5Goldens belong to one driver
Section 2's noise is small on one machine and large across machines: two GPU vendors can differ on thousands of pixels and still both be right. A golden is therefore stored per driver, and the tests run on that driver.
A software rasterizer is a Vulkan driver that runs on the CPU: Mesa's lavapipe is one, and SwiftShader and WARP (for Direct3D) are others. It is slow, but it gives the same result on any CPU with the same build and settings, and continuous integration machines have no GPU anyway. Three things decide its pixels, so a test pins them, or measures that they do not matter:
Vector width
The shader compiler inside the driver picks its SIMD width from the CPU. Pin it, so a CPU with wider vectors compiles the same code.
Thread count
Tiles are spread over threads. When each pixel is computed by one thread, the count changes only the speed; measure that it does, or pin it.
The build itself
A new driver version changes its code, so the test fetches one exact build and checks its hash before it runs it.
Real GPUs are still worth testing: they are what users run. Compare those against a baseline taken on the same machine just before the change, which answers "did my change move pixels here?" without any stored file.
6Changing a golden
Goldens must change sometimes: a better shadow filter is a different picture. The danger is the change nobody looked at. So the rule is about people, not code:
- Test failsafter a change that should move pixels
- Look at diff.pngonly the expected places are red?
- Render new goldensonly the tests that changed
- Say why in the changewith before and after
A change that changes a golden says why in its description, and shows the old and new image side by side. A reviewer who sees a golden change without that asks for it. Never update every golden at once to make the tests pass.
7Pitfalls
- The test fails on one run and passes on the next
- Something depends on time: a load that is not awaited, the wall clock, a thread race. Fix the cause; a larger tolerance only hides it.
- Every test fails on a developer's machine
- It ran on the local GPU, not on the driver of the goldens. Check which device the log names.
- A golden shows a bug
- It was captured without being looked at. Look at every golden before you store it: it must be a correct picture, or the test guards the bug.
- The repository grows with each update
- Keep goldens small (a few hundred pixels wide) and few. A small image still shows a wrong shadow or a black material.
- An asset can't be stored with the tests
- Its license may not allow it. Fetch it at test time from a fixed version and check its hash, like the driver.
- Two tests in one process disagree with two processes
- State leaks from one test to the next. Render one scene twice in one process and require a difference of 0.
8In RASM
RASM's image tests are one executable, rasm_image_tests (tests/image/imageTests.cpp), and one script that runs it on lavapipe, tools/image_tests.py. Each test renders a scene headless at 512 × 288 and compares the frame with its golden in tests/golden/lavapipe/.
The tests
| Test | What it guards |
|---|---|
| helmet_ibl | DamagedHelmet: image-based lighting (venice sunset), four shadow cascades, emissive |
| sponza | Sponza (fetched): many materials, MASK foliage, cascades in a large scene. The sky's light is not shadowed (D23), so the atrium is lit everywhere. |
| alpha_modes, msaa4, sponza_msaa4 | AlphaBlendModeTest: opaque, MASK with discard, BLEND sorted; the same at 4 samples with alpha-to-coverage; Sponza at 4 samples |
| taa | the helmet after 16 jittered frames |
| bloom | EmissiveStrengthTest with the bloom passes |
| skinning, morph, node_anim | CesiumMan and two foxes at 1.25 s; MorphStressTest at fixed weights; AnimatedCube at 0.3 s |
| many_lights, local_shadows | 1024 point lights in clusters; a spot light's shadow map and a point light's cube |
| ktx2, materials | FlightHelmet's Basis textures; texture transforms, vertex colors, a quantized mesh |
| views, views_window2 | Renderer::render with four views: split screen, a map in the corner without bloom, and a security camera drawn into a view texture first, which a screen in the scene shows (Many views of one scene), and the security camera in a second, offscreen window (Renderer::addWindow), which views_window2 reads (readFrame(window)); their steady frames, like every test's, make no heap allocation |
| debug_normals | the normals debug view |
| determinism | the helmet again in the same process: it must equal helmet_ibl exactly (T = 0, F = 0) |
| meshlets, meshlet_sponza | the helmet and Sponza drawn by meshlets on the vertex-pulling path: they must equal helmet_ibl and sponza exactly (T = 0, F = 0) |
| mesh_helmet, mesh_sponza, mesh_msaa4, mesh_taa, mesh_normals | the same scenes on the mesh-shader path, against helmet_ibl, sponza, sponza_msaa4, taa and debug_normals of the same run (T = 3/255, F = 0.01%): lavapipe's mesh shaders rasterize a few edge pixels differently (3 of Sponza's). On an RTX 3060 they are equal. |
| far_shadows, far_skinning, far_taa | the scenes of local_shadows, skinning and taa moved 6370 km from the origin (Earth-centred coordinates), against those tests of the same run (T = 8/255, F = 0.1%): each view draws in its camera's render space, so only shadow-edge pixels may differ, where the shadow maps' world texel grid falls differently (large worlds). |
The numbers
Every test uses T = 3/255 and F = 0.1% (147 of the 147,456 pixels), except the tests that compare with another test of the same run (determinism, meshlets, mesh_helmet, ...). They come from the noise measured on lavapipe, which was none: all runs were on one CPU model (AMD EPYC 7763), so T and F are a margin for a runner with another CPU, not for noise seen so far.
| This page | RASM |
|---|---|
| A fixed scene and time | Each test waits for its loads (Renderer::waitForLoads), renders ImageTest::frames frames (4, or 16 for TAA) with setAnimationTime at its fixed time, then readFrame. No UI, no stats. |
| The compare | compareImages: the largest channel difference per pixel; red over T, orange at or under T, gray where equal. readPng reads the golden. |
| A software rasterizer, pinned | lavapipe from mesa-dist-win 26.3.0, SHA-256 checked, with LP_NATIVE_VECTOR_WIDTH=256. The thread count did not change a pixel (LP_NUM_THREADS=1 against the default gave the same PNG), so it is not pinned. |
| An asset that can't be stored | Sponza (CRYENGINE Limited License) is fetched from a pinned commit of KhronosGroup/glTF-Sample-Assets into build/test-assets/. |
| Run the driver only on request | tools/image_tests.py downloads and runs Mesa only with --lavapipe; verify-change calls it only with --images. The presets' ctest leaves out the label image. |
| Real GPUs | Not against the goldens: verify-change compares every example with a baseline taken on the same machine. |
| Continuous integration | The Windows job runs the tests after the Release tests (about 1 minute) and uploads actual.png, expected.png and diff.png as image-test-failures when one fails. |
The tests found a real bug on their first run. The MASK shader sampled textures after a discard that killed the pixel, so those samples had undefined derivatives. Real GPUs drew a plausible image; lavapipe crashed. Now discard demotes the pixel to a helper (B33).
Two more crashes showed lavapipe running code a real GPU skips. A demoted pixel keeps running and loading: the point resolve's empty pixels read the point of instance row 0, which after another scene was a mesh, and followed it out of the point buffer (B39; the RTX 3060 lost the device on the same reads). And lavapipe runs a loop's body once with no lane on: a scene with no light made the light loop load through a null pointer (B38). The tests export_points and no_light keep both fixed.
Code map
| What | Where |
|---|---|
| The tests, scenes and compare | tests/image/imageTests.cpp: the TESTS table, buildGraph, renderTest, compareImages, runTests |
| The goldens | tests/golden/lavapipe/ |
| Fetch, run, report | tools/image_tests.py (--lavapipe, --update, --scenes, --fetch-only) |
| Registration and presets | tests/CMakeLists.txt (label image), CMakePresets.json (the default test presets leave it out) |
| CI | .github/workflows/ci.yml: the steps Install lavapipe and Image tests |
| Reading a PNG | readPng in src/renderer/readback.cpp |
A golden changes only in a change that says why, with the old and new image. Look at every new golden before you store it. Set T or F per test only from measured noise, and write the reason next to it. All the tests share one engine, so their materials add up: the 15 tests make 73 of the 255 an engine can hold (D34). Past that, a glTF primitive falls back to the default material with only a warning, and the image test fails as if the rendering were wrong.
Further reading
- NVIDIA, "FLIP: A Difference Evaluator for Alternating Images" (Andersson et al., 2020).
- Mesa documentation, "LLVMpipe" (its environment variables).
- Headless rendering and readback, Mipmaps and sampling, Transparency.