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.

fixed scene, fixed time render the frame, on the GPU read back actual, on the CPU golden, from disk same? pass fail: write actual.png and diff.png
A failed test leaves three pictures for a person: the golden, the new image and the difference. Most failures are understood in one look at the difference.

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:

GPU AGPU B: 2 pixels

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.

(a + b) + c = 1.0000001 a + (b + c) = 0.9999999 float addition is not associative

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.

a tie on a shared edge

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.

Start with "same code, another run" and lower T to 0: noise fails the test. Then pick each bug and raise T or F until it passes: that is how loose is too loose. The defaults pass the noise and fail both bugs.

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.

Never raise T or F to make a red test green

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:

12 40 77 200 13 12 40 78 200 13 one step: fail

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.

one number for the whole image

RMSE, PSNR

The mean of the squared differences. One wrong object in a corner hides in the mean of a large, correct image.

a score per window

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.

how visible, per pixel

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.

software rasterizer same pixels on any CPU matches its golden: max diff 0 GPU A its own rounding vs. software golden: edges differ GPU B another compiler, filter vs. software golden: edges differ
A golden is a contract with one driver at one version. Change the driver version, and the goldens are rendered again on purpose, in a change of their own.

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:

256-bit and 128-bit vectors

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.

tiles over threads

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.

driver.7z v 26.3.0 sha256 c505… a fixed build, checked

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:

  1. Test failsafter a change that should move pixels
  2. Look at diff.pngonly the expected places are red?
  3. Render new goldensonly the tests that changed
  4. Say why in the changewith before and after
The review rule

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/.

tools/image_tests.py --lavapipe: fetch, check, set VK_DRIVER_FILES Mesa 26.3.0sha256 Sponzasha256 rasm_image_tests one engine, the tests in order helmet, sponza, alpha modes, msaa4, taa, bloom, many lights, local shadows, ktx2, debug normals, determinism, skinning, morph, node animation, materials, views, meshlets tests with the same build share its scene each test waitForLoads N frames, fixed time readFrame compare: T, F fail: actual, diff a table: test, result, share over T, largest difference, seconds
One engine runs all the tests, so lavapipe compiles each pipeline once.

The tests

TestWhat it guards
helmet_iblDamagedHelmet: image-based lighting (venice sunset), four shadow cascades, emissive
sponzaSponza (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_msaa4AlphaBlendModeTest: opaque, MASK with discard, BLEND sorted; the same at 4 samples with alpha-to-coverage; Sponza at 4 samples
taathe helmet after 16 jittered frames
bloomEmissiveStrengthTest with the bloom passes
skinning, morph, node_animCesiumMan and two foxes at 1.25 s; MorphStressTest at fixed weights; AnimatedCube at 0.3 s
many_lights, local_shadows1024 point lights in clusters; a spot light's shadow map and a point light's cube
ktx2, materialsFlightHelmet's Basis textures; texture transforms, vertex colors, a quantized mesh
views, views_window2Renderer::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_normalsthe normals debug view
determinismthe helmet again in the same process: it must equal helmet_ibl exactly (T = 0, F = 0)
meshlets, meshlet_sponzathe 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_normalsthe 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_taathe 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.

0largest difference, the same job twice15 tests, every pixel equal
0largest difference, two CI runsand a third run with each test alone in its process
≈ 1 minthe 15 tests on lavapipe in CI50 to 80 s; lavapipe from the cache takes 8 s more
This pageRASM
A fixed scene and timeEach 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 comparecompareImages: the largest channel difference per pixel; red over T, orange at or under T, gray where equal. readPng reads the golden.
A software rasterizer, pinnedlavapipe 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 storedSponza (CRYENGINE Limited License) is fetched from a pinned commit of KhronosGroup/glTF-Sample-Assets into build/test-assets/.
Run the driver only on requesttools/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 GPUsNot against the goldens: verify-change compares every example with a baseline taken on the same machine.
Continuous integrationThe 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

WhatWhere
The tests, scenes and comparetests/image/imageTests.cpp: the TESTS table, buildGraph, renderTest, compareImages, runTests
The goldenstests/golden/lavapipe/
Fetch, run, reporttools/image_tests.py (--lavapipe, --update, --scenes, --fetch-only)
Registration and presetstests/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 PNGreadPng in src/renderer/readback.cpp
Rules to keep

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