Journal
Voxels7 min read

How I Test a Real-Time Renderer

The reflections in my pond trailed 14 pixels behind where they should have been - here's how I measure bugs like that instead of squinting at them.

By James

The pond in the voxel glade in afternoon light, the sun in a clear sky above the treeline and shadow reaching across the water.

The reflections in my pond used to smear whenever the camera moved - a lamp's glow in the water stretched out into a line. Measured under a sideways strafe, the reflected treeline trailed 14 pixels behind where it should have been. That's the kind of bug renderers are full of: not broken, just slightly wrong.

A shadow shifts by a pixel in one corner of one view. An optimisation changes the texture of the noise. An edit leaves one wrong voxel eighty metres away, where nobody will look until a player does. You won't catch any of that by squinting at the screen.

So my voxel engine has one rule: if I claim something about it, a machine produced a file that says so. Every change gets checked against reference frames that have to match to the byte before I even start judging whether it looks nice.

None of this is new. It's standard practice - I'm just strict about it.

The engine runs without a screen

One of the first things I built, before the engine drew anything worth looking at, was a headless mode. It runs the full renderer with no window, draws a scene from a given camera for a given number of frames, saves the frames as PNGs and drops a metrics file next to them.

The metrics file records the GPU time of every frame, broken down by stage, so a slowdown has a name and an address. It records how much of the image had to throw away its history each frame, which is how I put a number on ghosting. And it records how many warnings the graphics driver's validation layer raised, so "no validation errors" is a number in a committed file rather than a sentence in a document.

Everything is a command-line flag: the scene, the camera, the time of day, the wind, the haze, and a list of edits to make at particular frames. Camera paths run on a fixed 60 Hz clock, so a fly-through produces the same frames on a slow GPU as on a fast one. Windowed runs can be automated too, with injected keyboard and mouse input, so interactive features get the same repeatable checks.

Same input, same bytes

The finished image is a poor thing to compare byte for byte, because it blends many frames of history and the denoiser smooths everything it touches. So I compare two other views the engine can output directly. The raw view is one path-traced sample per pixel, before any cleaning up, noise and all. The normals view colours every surface by the direction it faces. Both are completely deterministic: the random numbers behind the raw view are seeded per frame, so the same inputs always give the same bytes.

The same frame in the normals view, every surface coloured by the direction it faces.
The glade rendered as a single noisy path-traced sample per pixel, before denoising.
RawNormals
One frame in two views: a single raw sample per pixel, and surface normals.

I use them to check three kinds of sameness.

New features start switched off. A feature lands in the code disabled, and with it disabled every reference frame has to match the frames from before the change, hash for hash. Only once it's proven to disturb nothing do I turn it on and measure what it does. It sounds pedantic. It's meant to.

Two routes, one answer. Make a set of edits to the world, and the result has to match, byte for byte, a world generated with those edits already baked in. Undo them all and you have to get back the frames of a world nobody ever touched. Redo them and the edited bytes have to come back exactly. If the step-by-step route and the from-scratch route disagree by one pixel, one of them is wrong, and I go and find out which.

Still means still. With the wind switched off, two frames rendered seconds apart must be identical. If anything moves in a world where nothing should, that's a bug with a very short list of suspects.

The check itself is tiny: hash the frame and compare it with the hash recorded when the reference was last approved. A hash can't tell you what changed, only that something did, which is all you want from a tripwire. When it fires, a pixel diff finds out where.

When exact matching is impossible, as with the finished image, I still measure instead of eyeballing: the share of pixels that changed, the brightness of a named patch of the frame, how much of it crushes to black. Every number points at the capture it came from. That's where the pond's 14 pixels came from, and after the fix the reflection trails by zero to one pixel.

The same moment after the fix, the reflected trees sharp in the water.
The pond rendered during camera motion, its reflection washed into a blur.
BeforeAfter
The same frame of the same camera move, before and after the fix.

Timing runs that don't lie

GPU timings are noisy in ways that look exactly like progress. The clocks take a while to ramp up after a cold start. Other programs borrow the GPU without asking: a background video process once took a share of the card during a timing run, and I've seen editing work run three to five times slower for stretches because of other software on the machine.

So I never compare today's number with yesterday's. When a change needs costing, the builds before and after it run paired and interleaved in the same session - A, B, A, B, several rounds - and then I compare the medians. Both builds suffer the same background noise, so whatever difference is left belongs to the code. Medians rather than averages, because one run hit by a hiccup shouldn't drag the result.

If the runs of a single build disagree with each other by more than the difference I'm trying to measure, the answer is "I don't know yet", and I run more rounds. When a number still surprises me, I split the change into pieces and time each piece the same way until the cost has an owner.

A fuzzer whose job is to break the world

Editing is where my bugs like to live, because players do things nobody plans: thousands of carves, placements and paints at random spots, overlapping each other, with undo mashed in between.

So I do that first, on purpose. The test harness generates seeded random sequences of edits in every shape and size, and after each sequence it checks things that must always be true. The edited world must match one rebuilt from scratch with the same edits, byte for byte, at every level of detail. Undo must restore the previous bytes exactly, and a separate test undoes the last ten edits of a run and then redoes them, checking that the edited bytes come back. And no blade of grass or flower may be left floating in mid-air, which is harder than it sounds once a fuzzer is loose in a meadow.

// Seeded fuzzing of world edits.
var rng   = new Random(seed);                  // same seed, same sequence, every time
var world = Scene.Generate(seed);
var log   = new List<EditCommand>();

for (int step = 0; step < 25; step++)
{
    var edit = EditCommand.Random(rng);        // carve, place or paint; any shape, any size
    world.Apply(edit);
    log.Add(edit);

    if (rng.Next(8) == 0)                      // undo is mixed in at random, about 1 in 8
    {
        world.Undo();
        log.RemoveAt(log.Count - 1);
    }
}

// The invariant: once the sequence is done, the edited world must match one built from scratch.
var rebuilt = Scene.Generate(seed, edits: log);
Assert.BytesEqual(rebuilt, world);             // incremental equals from scratch

When an assertion fails, the seed reproduces the failure exactly, so I can rerun it as many times as it takes.

A terraced pit carved into the flower meadow, a glowing lamp placed on its floor lighting the walls.
One of the scripted edits I replay headless: a pit carved into the meadow, with a lamp dropped in the bottom.

Random edits find the common bugs. The weird ones need someone actively trying to break things, so nasty cases get added by hand too: walls one voxel thick, a placement and a carve sharing a single voxel, bricks added and removed by the same stroke. Each one becomes a permanent test.

Looking at captures also catches things no invariant thinks to ask about. That's how I found out that my first walls built from several blocks showed see-through slits from far enough away. The fuzzer doesn't look at the view from eighty metres.

There are a few hundred unit tests in all, and they run as a gate on every change.

A brick wall with a round hollow carved into its face, the brick pattern and joints continuing inside the hollow.
A carve into a brick wall. The pattern and its joints have to carry on inside the hole, with no gaps and no stray voxels.

The parts a machine can't judge

Does the wind look like wind, or like a screensaver? Is that the right green? Does holding the mouse button feel like a steady stream of digging or a series of hiccups? For those there's a review pack: the frames to look at, the numbers that go with them, and a verdict written down up front, so the review checks a conclusion instead of forming one from scratch. When a verdict is "not good enough yet", it stays open until it is.

That's the method: write the number down and say where it came from.