Blog » gg, week 4: clouds, scale, and taking the look back out

A rim light that turned out to be most of a moonlit frame, proper clouds, deleting the art direction, and a frame that cost 12.9 ms drawing nothing.

Last week ended with a physical sky and four complaints about the frame: a moonlit forest that looked like shiny ghosts, ground in shade looking blue and wet, a forest floor with no shade in it at all, and clouds like an N64 skybox.

Most of that had one root cause, and it's the sort of thing that only shows up when you change the scale of something underneath a value you tuned by eye.

The rim light

The renderer had a rim light term, normalised to unit luminance on purpose so it wouldn't fade at dusk. Against the old gradient sky, with its static, bright, artist-owned ambient, that was a fairly defensible thing to do.

Against a derived sky whose ambient spans four orders of magnitude, it made the rim 85% of a moonlit frame. 85%! Normalising by luminance had also left the tint very blue, so the rim was where all the blue in the shade came from, too. Making the additive terms proportional put the shade's colour balance back where the old gradient sky had it, and roughly tripled the contrast between lit ground and shade.

The tempting fix was to just put the gradient sky back, and reconstructing it faithfully is what killed that idea: in gradient mode the rim and the ambient specular were already most of the light in shade. The decoration hadn't changed at all; the thing it was being measured against had.

The occlusion had a reach problem, too: the screen-space search covers three quarters of a metre, and a twenty-metre canopy is invisible to it at any radius. Turning the radius up was the obvious move and did nothing, because a knob whose reach is clamped by something else just looks ineffective, and nothing anywhere said so.

Then there was an estimator that failed with the wrong sign, which is what gave it away. Averaging independent disk taps for a horizon made the ground under a canopy come out lighter the further the taps looked, because far taps see open sky. A term that gets weaker when it should get stronger is wrong, and no amount of fiddling with the constants will save it.

And the usual one: instruments first. There was no debug view of the ambient or any of its terms, so the one quantity behind three of the four complaints had to be reverse-engineered by regressing against captured PNGs, which is a stupid way to find anything. The ambient, its terms and the sky's visibility all have debug views now.

Clouds

The flat cloud slab became a raymarched spherical shell shaped by a weather map and traced at reduced resolution, with a cirrus sheet above it. It casts shadows on the ground and shafts of light through the aerial perspective.

the village under heavy overcastgod rays through a cloud bank over a forest at low suna red and gold sunset over a dark treeline

Overcast, shafts and sunset. A sunset is pretty much just transmittance taken at each sample's altitude, and it's all shader work.

Two bugs on the way, neither visible from the outside.

The cloud light march never advanced its origin, so it read far more optical depth than it should have, and the only symptom was clouds slightly too dark between their lit rims.

And the actual cause of the "N64 skybox" complaint was duller still: the scene was authoring a sixteen-kilometre base feature. That's one cloud across the entire visible sky. Whoops.

Taking the art direction back out

The renderer had an art direction baked into it: an ink outline pass, and banded shading that the clouds shared.

All of it's gone now. The argument against it had been sitting in my own plan document since day one, I just hadn't applied it here: the engine may serve more than one game, so the look belongs to the game. Phase 6 built one into the renderer anyway, and it had reached further than I'd realised, all the way from the import settings through the cooked model format to the layout of the scene's attachments. Changing the art direction later would've been surgery.

What settled it was measuring first: with the ink and the bands both gone, a village frame still looked like a coherent stylised scene, because the packs carry the style in their art.

Fair caveat: the demo village is somebody else's stylised content, which flatters a renderer with no look of its own, and a flat greybox would probably come out differently.

the village, without the outline pass and banded shading

The same village with the ink and the bands deleted. What's left is a wrap term and a rim.

Everything came out properly. Defaulting it to off would have left the art direction deciding the frame's shape, and still costing an attachment, a uniform slot and a field of a cooked format. The attachments with other consumers stayed, and the line I ended up drawing is that the renderer publishing data is fine and the renderer holding an opinion about it isn't.

One bug from the deletion. Removing a slot from a uint4 renumbered the ones after it, and one of the two places filling that block was still writing by the old index. Every cloud trace then read its debug view as its sky mode and took the branch with no atmosphere in it, and a sunset's clouds came out neutral and more than twice as bright. Both layout guards passed, because both sides agreed about where the four words sat and neither had an opinion about which word meant what.

A change that re-records its own golden baselines can't also be guarded by them. Thirty baselines got re-recorded because the change moved every lit surface, and eighteen of them enshrined the bug above as correct on the way past. The suite was green and the defect was in the baselines. What found it was looking at a frame the change had no business touching: the clear sky either side of the bad one was byte-identical, which said the cloud pass alone had moved. Use your eyes, per the week one pro-tip.

What a second game asks for

Taking the look out raised a bigger question. The engine holds no art direction now, but it still holds a pile of assumptions about the kind of content it's going to be handed.

The next survey was against two kinds of frame the demo village can't show: tens of thousands of things, and a photoreal look. What reordered everything was that every performance number on file had been taken where the renderer is fragment-bound and the CPU is free, and both of those stop being true well before that scale. With fifty thousand extra instances the frame cost about the same at 160x90 as at 1080p, so it didn't care about pixels at all. Aimed at empty sky, drawing nothing whatsoever, it still cost 12.9 ms.

The per-frame cost was O(resident), and nothing in the frame was threaded. Oops.

Both halves got fixed. The renderer now takes a change set and patches the boxes it already holds, so a settled frame pays only for what moved, and the frustum cull and the item gather fan out on a job system over enkiTS. The worker count is stated in config, because the replay tool depends on determinism and "however many cores this box has" isn't deterministic.

That more than halved the worst of the stress frames, though the threads did less of it than the arithmetic. The tree query barely scaled at all, because a frustum walk is a chain of dependent random loads and threads don't multiply a memory latency.

Also: two threads' push_back calls writing into one cache line cost 0.19 ms of the gather on their own. False sharing, the classic.

The other half of the survey is the part I keep coming back to. An assumption about content is the same kind of defect as an opinion about look, and it's much harder to see, because nobody can point at it in a screenshot. This engine still carries four of them: MSAA as the only antialiasing assumes geometric edges dominate; an 8-bit texture cook assumes there's no high dynamic range data anywhere; a material model of one UV set assumes the asset tier the packs happen to be; and a single-valued heightfield assumes no caves.

Each of those is probably fine for the first game and a wall for some other one. I don't know yet which wall the second game hits first, and I'm not going to guess.

Distant trees, and a resolution that drives itself

a wood seen from above with impostor quads standing in for distant trees

The impostors debug view. A model with a cutout material and enough triangles gets a grid of views baked at cook time, and a distant instance draws as one quad.

The impostor bake stores colour, coverage and a normal with no lighting at all, so a distant tree is lit at runtime and still turns with the sun. Each tile is shaped to the model's proportions, so a birch doesn't spend two thirds of every tile on air.

The other scaling item came from me misreading a symptom. A quality tier states MSAA and a render scale as absolute numbers, and the same tier is then a different amount of work on every display. On a nearly-4K window the high tier cost 16.0 ms against a 16.67 ms budget, and a frame that misses vsync waits for the next refresh, so a frame rate on that edge alternates between 60 and 30 and averages 45. I read that as "the engine is slow". It wasn't. The tier was mis-scaled for the display.

There's a resolution scaler now, and every decision in it is about it not becoming a second opinion about the look. It only ever scales up to the ceiling the tier already stated, and it never touches an offscreen frame with a golden image waiting on it.

A fork of SDL, and packs of art

The engine now builds against my own fork of SDL, and the fork is mandatory. It carries GPU timestamp queries, in the API shape the maintainers themselves blessed in an earlier issue, and read-only device properties naming the native objects underneath. With those the engine can report the device's heap and budget as well as what it asked for.

Native handle exposure has been refused upstream on the record, by three maintainers across three issues, so that one isn't going anywhere. Fine. What actually limits how many patches I'm willing to carry is the rebase, since every SDL bump conflicts on the files a GPU patch touches whether the branch carries one patch or four.

And bulk art moved out of git entirely. A pack is a zip on a GitHub release, pinned by SHA256 and fetched at configure time into a gitignored directory. Size wasn't really what decided it. A gitignored directory needs no incantation at clone time and can't have a mistake committed into it that only a history rewrite can cure, and git couldn't give me either of those at any price.

Each pack also ships a written guide, and the guide is the interesting bit, because it records the stuff no query can tell you: the grid the modules sit on, whether an origin is at the foot of a thing or buried under it, which names lie about their colour or their facing, and what the set doesn't contain. The village kit shares one origin across every ground-level piece and its walls have a different material on each face, and both of those will waste an hour of anybody's time exactly once.

Where it stands

Four weeks in and the renderer is finished enough to leave alone for a while. What it doesn't have is a game attached to it, and the two subsystems a player notices before any of the rendering are the two it's never had: it makes no sound at all, and it can't draw a menu. So that's next.

Credits

Everything in these shots that isn't engine is bought or borrowed:

  • The buildings, props, trees, foliage and rocks are Quaternius kits: the Medieval Village and Stylized Nature MegaKits, bought as the paid source versions, plus the free Ultimate Stylized Nature set. All of it is released CC0, and he takes Patreon support.
  • The ground materials are Poly Haven textures, CC0.