Journal
Voxels8 min read

Why I Built My Own Engine

There's no off-the-shelf engine under my voxel world - here's why, and what a single frame looks like from the inside.

By James

The same meadow frame split into three strips: the colour-coded normals view, the image after the temporal blend alone, and the finished frame.

I wrote my voxel engine myself, from scratch, in C# on Vulkan. There's no off-the-shelf engine under it, and no plugin for someone else's editor. I've wanted to build this for years, and I've had a few goes at it, learning more each time. I tried existing engines, but I always ended up working around them: their renderers are built for meshes, not millions of tiny voxels, a lot of the work happens in an editor instead of in code, and when something broke I was digging through someone else's engine to find out why. I'd rather know every part of mine, from the ground up.

I'm not the first person to want this. Over the years a few people have shown voxel worlds at this kind of scale, and some of the most impressive have since stopped being developed. John Lin's work is the biggest reason I wanted to build one. When I made my test world, the Glade, I used his look as the benchmark: if my engine could get there, the foundations were right. That look is one style the engine can do. The point is for people to build their own worlds on it, in that style or any other. Everything under it, from how the voxels are laid out to how rays find them, is my own design, and that's what the rest of this entry is about.

For a game made of triangle meshes, a general engine is the obvious pick. That's just not what I'm making. I'm making a world out of voxels five centimetres across, every one of which can be dug out, placed or painted, and all of it lit by path tracing. That combination leans hard on exactly the parts of an engine that a general-purpose one keeps to itself.

Voxels this small are a data problem

A general engine is built around meshes: triangles prepared ahead of time, streamed in, drawn by a rasteriser and lit with a mix of baked and real-time tricks. It's very good at that.

This world has no meshes. My test world alone is more than a million bricks of 512 voxels each, stored as one byte per voxel, and any of them can change at any moment. For that to work I have to decide:

  • how the voxels are laid out in memory, because at this scale a single wasted byte per voxel is hundreds of megabytes;
  • how rays find them, because the GPU's ray tracing hardware is built for triangles and has to be taught about voxels;
  • what happens in the frame an edit lands, because it has to show up immediately, with no rebuild and no hitch;
  • how to test all of it, because a renderer can be quietly wrong in ways nobody spots.

In a general engine those decisions have already been made, with other kinds of games in mind, and changing them means learning and working inside a very large codebase built around different priorities. They're the decisions I most wanted to make myself.

Why ray tracing, and not only for the pictures

The obvious reason to ray trace is that it looks good. The less obvious one is that in a world you can change, it's the most direct way to get lighting that never goes stale.

Many games bake their lighting. Someone arranges the level, a tool spends minutes or hours working out how light bounces around it, and the result is saved and shipped. It looks great until someone moves a wall. When any voxel can be dug out at any moment there's nothing to bake against, because the level is never finished. Tracing rays every frame means the light is always worked out from the world as it is right now. Dig a tunnel into a hillside and it's dark inside because there's a hill above it, not because someone remembered to mark it dark.

The second reason goes well beyond pictures. Underneath all the shading, a ray tracer only ever answers one question: starting here and going this way, what do I hit first, and how far away is it? A renderer asks that hundreds of millions of times a second, so the hardware and the data structures are built to answer it fast. And a lot of a game turns out to be that same question:

  • What am I pointing at? An editing tool has to find the voxel under the cursor before it can carve or place anything. That's a single ray, walked through the same voxels the renderer draws.
  • Can I see that? Will this hit something? Line of sight for a creature, the path of a thrown object, whether a spot is sheltered from the rain. All rays.
  • What does this sound like? An early prototype already had reverb in a test room that came purely from the shape of the room. Sound bounces around a space much like light does. Trace rays from a sound to the listener, off walls and floors, and you get echoes, muffling behind walls and reverb that fits the room, from the geometry itself rather than from hand-placed "this is a cave" zones. It's called geometric acoustics: each path a ray finds from the sound to your ear arrives a little later and a little quieter after every bounce, and together those paths are the reverb.
  • What is it made of? Every hit also reports the material. Footsteps on stone and on grass, an impact on wood or on metal: the sound can follow the material the ray found, with nobody tagging surfaces by hand.

All of these answers come from the same voxels. In a typical engine the thing you see, the thing you collide with and the thing sound bounces off are often separate simplified versions of the level, kept in sync by hand. Here the renderer and the tools walk the same voxel data, and the sound's geometry is built straight from it. Carve a hole and the light sees it in the same frame, and so does everything else that asks a ray question, because there's only one world for them to look at.

Inside one frame

CPU (C# / .NET)GPU (VULKAN)OUTPUTWorldbricks of 8x8x8 voxelsgeneration, edits, undoFrame setupcamera, sun, windchanged bricks onlyUploadvoxel data to GPU memoryrebuild ray structuresRay tracing passhardware finds the bricksa shader walks voxelsshade: sun, sky, lampsone bounce, glass, waterDenoise (compute)reproject last frameblend with historyedge-aware waveletfilterComposite (compute)tone map (AgX)encode to sRGBWindowswapchain presentHeadlessframe_NNNN.pngmetrics.json
One frame, from the world on the CPU to pixels. The CPU owns the voxels, the GPU traces them, compute passes clean up the image, and the result goes to a window or to a PNG file with its measurements.

The authoritative copy of the world lives on the CPU, in C#. It's divided into bricks of 8 x 8 x 8 voxels, and only bricks with something in them exist. Generation, editing and undo all happen here, and each frame only the bricks that changed are sent to the GPU.

An early version of the voxel glade seen from inside the forest: broadleaf and conifer trees over a meadow of grass and small flowers.
An early frame from inside the forest of my test world. Every trunk, leaf and blade of grass is voxels, generated from a seed on the CPU.

Modern graphics cards have dedicated ray tracing hardware, and out of the box it understands triangles. For anything else, like my voxel bricks, you describe your objects to it as boxes and supply a small program, an intersection shader, that the hardware calls whenever a ray enters one of your boxes. Mine walks the voxels inside the brick and reports the first solid one. The boxes live in an acceleration structure, a tree of boxes that lets the hardware skip empty space quickly, and when bricks change, their entries are patched and the top level of the tree is rebuilt, which takes about a millisecond.

The ray tracing pass does the path tracing. For each pixel it finds the surface, then traces shadow rays towards the sun and nearby lamps, a bounce ray for indirect light, and rays through water and glass. That gives one noisy sample per pixel.

Everything after that is compute shaders, small general-purpose GPU programs. A denoiser in the SVGF family blends each pixel with its own history from previous frames, then filters it with an edge-aware wavelet, so the noise goes without blurring the edges of the voxels. A final pass tone maps the result with AgX and encodes it for the screen.

The same meadow frame after the full denoiser, smooth with crisp voxel edges.
A meadow by the pond after the temporal blend alone, with visible grain on the grass and flowers.
History onlyFinished frame
The same frame after the temporal blend alone and after the full denoiser.

Stripped of detail, the loop that ties it together is short:

while (running)
{
    input.Poll();
    camera.Update(input, dt);

    world.ApplyPendingEdits();              // CPU: carve, place, paint, undo
    var changed = world.TakeChangedBricks();
    gpu.Upload(changed);                    // only what changed this frame
    gpu.RebuildAccelerationFor(changed);

    using var frame = gpu.BeginFrame();
    frame.TraceRays(camera, sun, wind);     // one sample per pixel
    frame.Denoise();                        // compute: history + wavelet filter
    frame.Composite(look);                  // compute: tone map, encode

    if (headless) capture.Save(frame);      // PNG + metrics.json
    else          swapchain.Present(frame);
}

Headless capture came first

The same engine that draws to a window can run with no window at all: render a scene from a given camera, save the frames as PNGs, and write a metrics file with the GPU time of every stage. It was one of the first things I built, before the engine could draw anything worth looking at.

That's how the numbers in this journal were measured. It lets me compare frames byte for byte after a change, time two builds against each other fairly, and replay hundreds of seeded random edits looking for bugs. There's a whole entry on it: how I test a real-time renderer.

The same rolling voxel terrain in the finished frame.
Rolling voxel terrain in the normals debug view, every surface coloured by the direction it faces.
Normals viewFinished frame
One of the engine's first test scenes, a kilometre of rolling terrain at 5 cm, in the normals debug view and as the final image. Debug views like this are deterministic, so they can be compared exactly.

What I gave up

Building your own engine means building everything: the window, the input, the camera, the capture tools, the editing tools. There's no asset store, and no forum thread where someone has already solved your exact problem.

The engine also needs ray tracing hardware. There's no fallback renderer, because a second renderer is a second engine to maintain.

What I get back is control over how a lot of very small data is stored, traced, changed and checked. The next entries go into it: what the engine can do, how I test it, and what I've built with it.