Voxel engine: svet goes outside
The one-month post said the next one would come in a month, or earlier if something interesting arrived. That post ended with what I wanted to see most: the outdoors. Twelve days later the engine has gone outside, it has a name, and the light wobbled again. I think that counts as interesting.
The engine is at version 0.84 today. As before, agents write the code, and I decide what gets built and check the result on the screen. Every frame and timing is from my laptop with an RTX 3080 Laptop GPU and a Ryzen 9, unless it says otherwise. Some readings are also from a slow AMD laptop chip with graphics built into the processor; I use it to see how the engine feels on weak hardware. The engine is built alongside a first game, and this post does not describe the game.
Twelve days by date
When each piece arrived.
- outdoors
- light
- speed
- creatures and tools
- 17 Nov A sky with an atmosphere, a sun and a moon; hills and a forest; the outdoor scene in the tester; one menu for all settings
- 18–19 Nov A picker for creatures, creatures that follow floors and walls, anything from a .vox file; still light under the trees
- 19–26 Nov The light wobble: found, chased through five ideas, mostly gone
- 23 Nov The eye adapts to the sky around the camera, not to the clock
- 24 Nov The engine is renamed svet
- 25 Nov The engine’s own upscaler removed; FSR 2 is the only one
- 26 Nov Light computed on its own thread; buried chunks skip the GPU
- 27 Nov A dig that no longer flashes; a new README with pictures the engine renders itself
- 28 Nov Streamed scenes no longer stagger in flight
Outside
For its first month the engine lived underground: the streamed cave, the showcase hall with its crystals, and a castle standing in rock. On 17 November it got a sky.
The sky is a model of the real atmosphere. It is Sébastien Hillaire’s model from 2020, the one many modern games use, with Earth’s numbers: a planet 6,360 km in radius and air 100 km thick. The GPU builds three small tables from it. One says how much sunlight survives a path through the air. One holds the light that bounced many times between molecules, which is why the sky is bright even away from the sun. The last is the sky as seen from the camera, and every pixel that sees sky reads it. Nobody painted the orange evening; it comes out of the model.
The engine itself has no clock. The application that uses it says where the sun and the moon are. The test application plays a day at latitude 45° on an equinox: the sun rises at 6:00, sets at 18:00 and stands 45° high in the south at noon. The moon is full, opposite the sun, and 400,000 times dimmer. A whole day takes 20 minutes by default. The sun is drawn 3° wide, more than ten times wider than the real one, so that shadows have soft edges.
Under the sky there are hills. As everywhere in the engine, the world is made of 10 cm voxels, grouped into chunks 6.4 metres on a side. The hills come from noise at three scales, computed in whole numbers so every machine gets the same hills: big hills, smaller bumps on them, and small ripples. Heights vary by up to 20 metres. Then there is a forest. Sixteen kinds of tree, trunks five to nine metres tall, round crowns two to three metres across, with a quarter of the leaf voxels kept so light passes between them. In the forest almost every square of 4.8 × 4.8 metres holds a tree. A cave mouth leads from the hills into a tunnel 2.5 metres wide, and the tunnel goes down to a chamber 15 metres under the surface.
The outdoors
Rendered by the engine for its README, at 1600 × 900.
The pictures come from the engine's list of README shots: each is one command line, a seed and a frame number, so anyone can render the same frame.
Full detail reaches 102 metres from the camera, and coarse copies of the chunks reach 154. The tester got a fourth scene: a 73-second flight that starts in front of the cave mouth, sweeps over the hills, threads between the trees, turns back into the low sun, enters the cave and flies down the tunnel. The path is computed from the terrain, and a test checks that it keeps at least a metre of air around the camera the whole way.
I have to say: I was delighted to see the outdoors again. The engine had not shown anything this open since day one, when the world was a box of rock with a hole in it.
What this world can do shows best when you dig. The skylight below is a tunnel whose roof was dug away from inside: the camera looks straight up through the hole, the sun is in the corner, and the walls of the shaft take the light from above.
A skylight
The tunnel's roof dug open to the sky.
Shadows from a map of columns
Shadows from the sun are expensive in a voxel world. For every pixel the engine would have to walk a ray toward the sun, through hundreds of voxels, and do it again every frame because the camera moves. For a while the engine did that near the camera. Now it uses a cheaper trick at every distance, and since that is where the wobble story ended, it is worth seeing how it works.
The engine keeps a column map. Picture the world from above, cut into columns two voxels wide. For each column the map stores one number: the height of its highest solid voxel. For the area the engine keeps around the camera about 160 metres in each direction, that is a texture of 1,632 × 1,632 numbers, about 10 MB.
To ask whether the sun reaches a face, the engine does not walk through voxels. It steps along the map toward the sun, twelve reads, each about 1.55 times further than the one before: 40 cm, 60 cm, a metre and so on up to 51 metres. At each read it compares the column’s top with the sun’s angle. If any top rises above the line toward the sun, the face is in shadow. Then it looks closer at the stretch that looked worst, up to eight more columns. And if the face is below the top of its own column, under a roof or a crown, it is in shadow at once. The answer is not a hard 0 or 1: near the edge it fades over the width of the sun disc, so shadow edges are soft. Twenty reads instead of hundreds of steps.
The price is that a column knows only its top. Light that passes under a tree crown, between the crown and the ground, is lost: for the map the whole column under the crown is covered. That is why the engine’s shadows of trees are solid blobs.
The slice below plays a day over a strip of hills. The slider moves the sun. Point at a slope to see which column tops the check reads. Switch to “where they differ” to see the faces where the map and the exact rays disagree; there are not many, and they are mostly low sun slipping under crowns. The slice is smaller than the engine’s world, so its reads stop at 25 metres. Dig into a hill and the map changes with it.
A day over the hills: shadows from the column map
The white stepped line is the column map: the top of each column two voxels wide. The map counts the pale air under it as covered, so you can see it knows nothing of the space under crowns and in the tunnel. The slider moves the sun. Point at a slope: the yellow line is the direction to the sun (it is infinitely far, so the lines from every slope are parallel), the white dots are the column tops the check read, and the white line is the steepest of them, the horizon. If the yellow line is above the white one, the face is in sun (yellow frame); below it, in shadow (blue frame). Click or drag to dig.
A 2D model of the engine’s key_horizon.wgsl: a map cell is 2 voxels, 12 reads at distances from 4 to 256 voxels, each about 1.55 times further than the one before, then a refine over 8 more cells. In the engine the reads go out to 512 voxels. The shadow edge is blurred four times wider than the sun disc here so you can see it. The sky colours are a simple blend, not the engine’s atmosphere.
The wobble
The light was my main subject for two weeks in the November post, and it ended well: light computed ahead and stored per chunk, which did not change by a pixel while nothing moved. I thought the light was done. Outdoors it turned out to be done only while nothing moved.
On 19 November, looking at the leaves, I wrote: “any view change makes the light wobble for 1-2 s”. The next day, at the window again: “a wobble of everything after turn or when moving, it is clearly seen on far trees for example when I’m slowly fly, it just can’t settle”. While the camera stood still, the light was calm. As soon as it moved, slopes and trees shimmered, the edge of the light crawled up and down the hills, and after a turn the picture needed a second or two to calm down.
This came as a complete surprise. The cave never showed it, because in the cave nearly all light came from stored values and from the headlamp, and the headlamp is computed exactly every frame. Outdoors most light comes from the sun and the sky, and those were estimated with random rays. Each pixel averaged its answers with its own answers from earlier frames. That running average is called the pixel’s history, and it lived on the screen, per pixel.
The direction I gave that day was: “almost good picture without wobble is better than perfect picture with wobble… the further the object - more we can approximate”. And one rule: “if we have to wait for a minute for light to settle - something wrong”.
Then a week of back and forth to find the cause.
First guess: the sun’s shadow on the screen. Each pixel cast one random ray toward the sun disc and added it to its history. When the camera moved, the engine threw a pixel’s history away and started again from one sample, which is 0 or 1. Measured after a 60° turn, 59 percent of far pixels were off by more than 4 brightness levels out of 255, and two seconds later still 45 percent. So far shadows moved to the column map, and near ones got an exact soft edge without randomness. I looked and said it was “roughly the same”. Better, but not gone.
Then: switches. The agents could not see the wobble. The tests could not see it either, because every test compared still frames. So on 21 November we did a diagnosis day: every suspect got a switch in the menu, and I flew around turning them on and off. The light passes’ screen history: “with it wobble worsens”. The upscaler, which renders fewer pixels and rebuilds the full picture from earlier frames, keeps a history of its own. On far trees: “fsr makes them wobble… but without it picture is not right”. Freezing the stored light, to check whether new light arriving was the cause, made it worse, so that was not it. The near soft shadow edge “moves with me which is not right”.
Second guess: calm light. The switches pointed at anything that keeps history on the screen. A pixel shows a place in the world for one frame only. When the camera moves, a pixel lands on another voxel and its history means nothing. So the default became “calm”: no screen history anywhere in the light. The sun’s shadow on world faces comes from the column map at every distance, and each face reads one value that does not depend on which pixel looks at it. On the slow AMD chip the light pass even got faster, from 6 to 11 ms down to about 2. I looked: “wobble on slopes is gone”. What was left was “only distant trees”.
Third guess: the sun fixed in the world. Maybe the shadow edge still moved because it was computed at the exact point a pixel hits, and that point moves a little every frame. So the engine got options to read the sun at the centre of a face, and to walk the map column by column. I looked: “a little wobble”, “the wobble is located only on sunned parts”. None of the options beat the default, so the default stayed.
Then: clips I ranked blind. I recorded five lossless clips on the same path with different settings, and ranked them without knowing which was which. The point was a number that would order the clips the way my eye did, so the agents could chase it alone. One measure, how much the lit edge changes between frames, ordered one pair of clips my way. But the two clips of that pair also differed in resolution, so it proves little. The clips did show where the last second of settling after a turn came from: the upscaler’s history. In that second a pixel changed by 22 brightness levels on average with FSR 2, and by 0.007 without it.
So the cause was history kept on the screen. The light no longer keeps any. One history is left: the upscaler’s. FSR 2 shifts the camera by part of a pixel every frame, which is called jitter, and builds the full picture from the shifted frames. Sparse sunlit leaves at 15 to 60 metres are two to four pixels per voxel, and even with the camera standing still, 17 percent of far pixels see a different voxel from one shift to the next. The leaves of sunlit crowns still sparkle a little while I move, and the picture still settles for about a second after a fast turn.
The slice below shows the difference in two small screens over the same far slope. On the left, light as it was: random rays from every pixel, averaged on the screen, the history restarting whenever a pixel lands on another voxel. On the right, light as it is: one value per voxel face, in the world. Untick “camera moves” and the left side still shimmers: the voxels here are tiny, so the jitter alone moves pixels from voxel to voxel. Untick the jitter too and both sides settle, the left one after a moment.
Where the wobble came from: screen history against light in the world
The same far slope in the afternoon, one voxel per 1.6 pixels. On the left each pixel casts one random ray at the sun and one into the sky, and averages them with its own history, and the history starts again whenever the pixel lands on another voxel. On the right each voxel face has one value, and every pixel reads it.
A simplified model: in the engine the history lived in several light passes, and there were more rays. The edge that still crawls on the right with jitter on is what is left in the engine as well: a pixel lands on one voxel, then on its neighbour.
And again it would not have been possible without me staring at the screen. Every guess above was built properly, with tests, and every one passed its tests. Only my eyes in the window said which guesses helped. The blind ranking was an attempt to hand my eye to the agents, and it is not there yet.
The upscaler I removed
FSR 2 is what is left of the wobble, and since 25 November it is the only upscaler. On 22 October the engine got its own upscaler: render fewer pixels, then rebuild the full picture from earlier frames. Five days later came a port of AMD’s FSR 2, which does the same thing better, and on 3 November FSR 2 became the default. Our own stayed as a second option.
On 25 November I removed it. It was cheaper: 1.8 ms on the slow AMD chip against 4.1 ms for FSR 2. But nobody used it, and every change to the picture had to keep both paths correct. Tests compared one against the other, and running its tests cost almost a minute every time. Its last version is kept under a tag in the repository, in case I ever want it back.
The eye in the tunnel and under the trees
The engine adapts its exposure, how bright it makes the picture, the way an eye does: in a dark place it opens up, in bright light it closes. With the sun this went wrong twice.
At noon the tunnel was black. How far the eye could open was set by the time of day, so at noon it stayed close to the bright day outside. My note: “light is too dark, it should be changing according to surrounding exposure, not time of day”. Now the limit follows how much of the view is sky. Deep in the tunnel the eye opens, and it moves at most 3.75 stops a second, a stop being a doubling of light.
Then the forest got too bright. When the camera moved under the trees and the crowns closed over the last pixel of sky, the eye opened by about five stops, and the shade turned into a bright day. Now the eye looks at two things: how much sky is in view, and how much sky the ground under the camera sees, which the engine has stored anyway. It takes the larger and smooths it over a second. Under trees the ground still sees a lot of sky, so the eye stays calm. The brightest one percent of pixels is also kept from burning out. My note on the version I picked: “looks great”.
A dig that does not flash
Digging made a flash. My words: “it flashes with light when digging… when I dig deeper, more than 2-3 meters it is already bothers”. One blink, every dig.
When you dig, the new faces need light at once, but the correct light for them is computed a few frames later. Until then the engine guessed. The guess started from how much sky the surface above saw, and in a deep pit that is far too much. So a fresh face started bright and fell to its real light, up to a hundred brightness levels out of 255.
Now the guess counts only sky the hole itself can see from its depth, and has no minimum. A fresh face starts slightly dark and rises to its light over four frames. A dark start is something the eye does not notice; a bright one it does. Mine said: “The dark start is not visible at all.”
A dug cave
Under the headlamp, its light settled.
Speed
Most of the speed work these days was about one thing: the engine is now big enough that a frame can be late for reasons that have nothing to do with the GPU.
The light for chunks is computed ahead, in a queue. Until this week the queue’s work on the processor, choosing chunks and preparing their jobs, ran inside the frame. Now it runs on its own thread. While the forest’s light is being computed right after the start, a frame on the RTX takes 8.3 ms instead of 21.9. My reaction: “with thread it got so much better, this is the way!”
The queue also got a rule. A chunk that is buried, with no light source in reach, must be dark. Such a chunk now gets an all-zero light record on the processor, without a job on the GPU. In the forest 3,589 chunks now skip the GPU this way; before the rule was applied everywhere, 261 did. The GPU jobs drop from 12,183 to about 8,000, so the far light lands sooner. To check the rule, an audit computed every such chunk the old way and compared it with zero: 0 differences.
The tester build had shown a stagger: while flying over streamed scenes, now and then a frame came late. The cause was new chunks arriving in bunches. A whole layer of chunks was copied to the GPU in one frame, or the engine dropped thousands of chunks behind the camera at once: one long jump of the camera dropped 13,902 in a single 38 to 52 ms step of the streaming. Now the workers that generate chunks also prepare their GPU copy, a frame copies at most 6 ms worth of chunks, so a layer lands over three to six frames, and at most 256 chunks are dropped per step. On the cave flight at 2560 × 1440, the processor’s part of the slowest one percent of frames went from 18.4 ms to 12.0. A frame at 60 frames per second has 16.6 ms in all. On my four benchmarks the frames longer than 33 ms went from 10 to 34 per run to 0 to 2. “Gone mostly.”
The start got shorter too. The engine computes some light before the first frame, so the world does not appear black. In the forest the first frame used to wait for all of it, 6.3 seconds. Now it waits only for the chunks near the camera, 1.2 seconds, and the rest arrives behind the first frame. A start on a fresh machine, where the graphics driver has never seen the engine’s shaders, now builds them on four threads: 37 seconds instead of 59. That is still long, and it happens once.
Creatures that climb walls
The month post ended with creatures that walked in place at a fixed height. Now they follow the floor and the walls.
A creature moves as a sphere stuck to a surface. It walks along the surface. When it walks into a wall, it takes the wall as its new floor and climbs. When it walks over an edge, it pulls itself back to the surface along its own “down” and wraps around the corner. Dig the ground from under it and it falls and lands. Two short rays ahead warn it about a wall or a drop. To stop a spider from flipping on every voxel step of a slope, its “up” comes from counting solid voxels around its body, so it leans with the slope as a whole. In wander mode a creature picks a new heading every 5 to 15 seconds. All of it is integer math, so the same creature walks the same way on Linux and on Windows. Sixty-four spiders take 1.4 ms per simulation tick.
One bug is still there: the library spider in wander mode turns on the spot and does not go anywhere. That is why the loop below shows it standing and turning.
The spider in the showcase hall
An animated loop from the README.
Placing creatures got a picker. A key opens a page of the library, each model with a small icon that the engine ray-traces on the processor: 1.9 ms for the walker, 14.7 ms for the spider. Then a belt of nine slots appears at the bottom of the screen, like in many games. Where you aim stands a ghost of the chosen model, tinted and outlined. It stays at least three metres away, drops to the floor if you aim at a wall, and turns by 22.5° on a key. The engine can also free a model it no longer needs, and code that still points at a freed model gets an error instead of drawing whatever took its place.
Anything from a .vox
MagicaVoxel is the editor most voxel artists use, and its files are .vox. Now the engine reads more of them. Each colour of a file’s palette becomes a material, and a text file says what it is: glass that lets light through, a colour that glows and lights the room, metal. Save that file while the engine runs and the world reloads it within a second.
Large scenes from a .vox are stamped into the world, and the stamp is kept even for parts of the world that are not loaded: the far part of a castle is written when its chunks are generated. Stored by bricks of 8 × 8 × 8 voxels, skipping empty ones, the castle’s 21 pieces take 45 MB instead of 344 as plain voxel arrays. Structures sit on the same belt as creatures: walker, spider, chapel, castle.
A chapel from a .vox file
Stamped into the cave; its glass and lamps are materials from the palette.
The showcase hall also changed colour. I asked why John Lin’s Crystal Islands video looks so good. The agents’ answer was pale rock, saturated glowing crystals, a little colour noise per voxel and darker caves. So the hall got pale tinted rock and crystals in five hues.
A name
On 24 November the engine became svet. In Russian свет means light, and in older Russian it also means the world: белый свет, the wide world. I am from Russia, and the name is an homage to my Slavic roots. After the last two months, light is the obvious word for what I work with most in this engine.
Everything moved under the name: the code modules are svet-* now, settings start with SVET_, and the tester is svet-tester.
A README, and people who are not me
The README was rewritten. Its pictures are rendered by the engine itself: each is one line in a list, with its exact flags, seed and frame, and a script renders the whole list. Another mode renders the pictures again and compares them with the committed ones, with a tolerance measured from that picture’s own noise. So the README can never show a picture the engine can no longer make. The code example in it is also a test.
The tester build comes for Linux and Windows. It has four scenes now: the cave, the showcase hall, the castle and the outdoors. One button runs all of them in about four minutes and packs a report. Running the archive myself found three problems: the banner hid the frame rate (“fps is unknown and hided”), the archive looked for its materials on the build machine’s disk, and the stagger from the speed section.
The testers were impressed with the progress. And I have slowly started to show the engine to my friends, because now there is something to show.
The engine at four points
In twelve days the engine grew by about a hundred thousand lines of Rust and 750 tests.
What is next
The target is a look I saw in a video of another developer’s voxel renderer: overgrown ruins in canyons, terraced cliffs, a lake, a long hazy view, a lot of green. To get there the engine will get colour in layers on its voxels instead of one colour per material, moss that grows where the sky reaches, vines and flowers as real voxels, trees grown by a branching algorithm with roots that hold the ground, and a demo world of canyons with a lake and ruins. Every look feature gets a cheap form for the default settings and a rich one for a new, heavier profile. After that, a view to a kilometre, water, haze and clouds, weather, and the night sky. Squash and stretch for the clips is still waiting its turn, and the grass is now part of this bigger look. Of the light problems, as far as I know, one is still open: sunlit leaves in far crowns still flicker a little while the camera moves. The cause is FSR 2, the upscaler from above.