004 — Frustum Culling, Early-Z Sorting and a First Steam Deck Run
ARAL stops drawing what the camera can't see, sorts the rest for early-Z, and a debug view shows the culling result itself
2026-11-02 → 2026-11-05
Until now, ARAL drew everything, every frame, whatever the camera was looking at. SDrawCall already had
a bounds field, but nobody filled it. This step is the first real optimization for the Steam Deck and the
Switch 2, and a prerequisite for the Clustered Forward+ coming later.

Left: ARAL, the culled camera. Right: SOLID, with a free camera placed above, drawing what ARAL decided:
green visible, red rejected by the sphere, orange sphere OK but rejected by the box.
The white lines are the frustum of the ARAL camera. Bottom line of the overlay: 263 / 613 visible, 142 draws.
What was implemented
- Local bounds per mesh, computed in
createMeshfrom the positions: AABB min / max, then a sphere (max distance to the center). The glTF accessormin/maxare ignored: nothing guarantees an exporter writes them correctly, only the positions are trusted. - World bounds per object (
computeWorldBound): transformed center, half-extents|M|·e, radius × max scale. One pass only:CScene::update()builds the draw list once, every view reads it. Mirrored objects (negative determinant) get a flag and a dynamicvkCmdSetFrontFace. - Frustum planes per view, extracted from the same
viewProjas the frame UBO (Gribb-Hartmann), Vulkan convention (depth in [0, 1] → the near plane is row 2 alone), normalized. - A conservative test: sphere first (cheap early rejection), then the AABB
(
dot(n, c) + d < -dot(|n|, e)). An object crossing a border stays visible. Brute force, scalar for now — SIMD only if a measurement asks for it. - One entry point:
frustumCull(frustum, list, view, &visibles), a free public function inInGa/gfx/culling.h, open to third-party renderers. ARAL culls its two lists (dynamic draws, then the scene) through the same path. - Visible list per view → contiguous
firstInstanceranges: an SSBO of visible indices per view, read through an indirection inobject.vert.flushonly draws what each viewport sees.
Sorting for early-Z
ARAL will be limited by fragments, not by vertices: drawing opaque objects front to back lets the depth test reject hidden fragments before the shader runs.
- sort key
[queue | pipeline group | depth 16 bits]+ index, radix sort - fixed queue order planned: cheap opaque → expensive opaque (POM) → alpha test → transparent (back to front, separate pass) → overlay. The expensive ones go after the cheap ones: early-Z rejects the hidden POM fragments.
- the pipeline group is a compact index given by the renderer (0, 1, 2… per variant), never a pipeline id. Today everything is in group 0.
- consecutive objects with the same mesh and the same quantized depth are merged into one instanced draw: 263 visible → 142 draws on the capture above. A key on the mesh would give ~30–40 draws, but would lose early-Z precision: refused for now, to reopen only if a Steam Deck measurement shows draw recording dominating.
While at it, the draw recording got lighter:
- the mega-buffer is bound once per pass:
vkCmdDrawIndexed(indexCount, 1, firstIndex, vertexOffset, firstInstance)instead ofvkCmdBindVertexBuffers+vkCmdBindIndexBufferfor every draw (4 calls → 2); - vertex offsets aligned on
sizeof(SVertex)= 48 sovertexOffsetcan be counted in vertices; - the index buffer is rebound only when the index type changes (U16 / U32);
- the
viewIdpush constant is sent once per view.
A LOD interface is reserved at the same time: a mesh can carry up to 4 index ranges with a screen-size
threshold each (SMeshLod). The selection happens at culling time — LOD0 forced for now. Importing Blender
_LODn meshes comes later.
A debug view that shows the real result
The rule: the debug view displays ARAL's result (one bit per object and per view), never a recomputation — otherwise you test the debug code, not the culling. It draws the tested volumes (world AABBs), not the meshes.
SDrawCall::cullFlag: the state of each object for each view (visible / rejected by the sphere / rejected by the AABB), Debug builds onlyinga:debugMode <mode> <view> [sourceWindow], modesnone,aabb,culling,aabb_cube,culling_cube,aabb_mesh,culling_mesh: SOLID draws the draws submitted by the source window- the 12 edges of the frustum: a unit wire box sent into the frustum by a projective matrix (the GPU does the division by w, one draw per view); the observed view in white, the others in grey
- overlay: one line
cull win <id> v<view> <visible> / <total> visibleper ARAL view, and acullprofiler zone
The same debug on meshes (culling_mesh), early version: SOLID on top colors the real meshes, ARAL below
renders the scene. Everything is green: the camera sees the whole scene.

And in motion: ARAL in one window, SOLID in the other, switching between the debug modes while the ARAL camera moves. The counter changes when the camera turns, objects go from green to red as they leave the frustum.
The profiler grew too
The CPU profiler of the previous post now also has GPU timestamps per pass (gpu.aral.scene,
gpu.aral.tonemap, gpu.aral.overlay, same for SOLID) and a CPU / GPU line under the FPS. A Profile build
(Release + profiler, no overlay) writes the same report into the log every 2 seconds, and a --spam N scene
(N copies of the ruins in a grid) gives something heavier to measure.
The numbers in the captures are Debug builds (-O0 + ASan). In FIFO, vk.acquire is mostly waiting for vsync.
How it was tested
- top view with the SOLID free camera: green = exactly what is inside the frustum (up to height), boxes crossing a border stay green;
- the visible counter changes while turning the camera;
- no visible object disappears at the edges of the screen;
--spam 20 --spam-step 2: 263 visible out of 613 → 142 draws;- validation and synchronization clean.
First test on the Steam Deck
Done in desktop mode, binary copied over SSH, no Steam build. The Deck runs glibc 2.41 while my machine is
already at 2.44, so the Deck build is made in a podman container pinned on the Arch Linux Archive (glibc 2.41).
No GLIBCXX dependency at all: without std::, the Deck's own libstdc++ is enough.
It launched without error or warning (XCB, RADV, 1280×800 swapchain, FIFO). Release build with the profiler,
--spam scene (613 objects), internal screen at 60 Hz, GPU clock forced to high, window 1280×696
(the Plasma taskbar takes the rest):
| Zone (average) | Steam Deck |
|---|---|
CPU frame.work | 0.62–0.67 ms |
| GPU total | 0.99 ms |
gpu.aral.scene | ~765 µs |
gpu.aral.tonemap | 226 µs |
Stable 60 fps, with a lot of room left. The same scene at the same resolution on my RX 6800 gives the factors used by the overlay to estimate Deck times from the desktop: CPU ×3, GPU ×12. To measure again after PBR and shadows, when the scene costs more than a few ms on the Deck.
Little problems
- The frustum edges were flickering.
inverse(viewProj)in float, with near 0.1 / far 400, gives w ≈ 7·10⁻⁴ at the far plane: the sign became unstable. The inverse is now written analytically, with the far plane clamped for display (inga:debugFrustumFar, 150 by default). - Order between renderers. SOLID reads the
cullFlagof the current frame: ARAL must flush first. And when a renderer is removed, SOLID's debug source is reset tonullptr— never a dangling pointer. - A recycled mesh slot kept the
lodCountof the previous mesh:createMeshnow rewrites all the LODs.
Tested on
AMD RX 6800 (RADV), Linux / Wayland.
Next step
Step 12: a static sky gradient (full-screen triangle) and an ambient light tinted by the sky.