Skip to content

perf: viewport-cull machines and items - #17

Merged
vetr0s merged 2 commits into
mainfrom
perf/viewport-cull
Jul 12, 2026
Merged

perf: viewport-cull machines and items#17
vetr0s merged 2 commits into
mainfrom
perf/viewport-cull

Conversation

@vetr0s

@vetr0s vetr0s commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Stacked on #16 (both touch render_game.c). Rebases onto main once #16 merges.

The ground was already culled; machines and items were not. Every entity in the world was pushed into the sort buffer, qsorted, and drawn every frame, on screen or not. An off-screen conveyor still ran ~9 polygons through the projection; an off-screen ore still ran two mach_r2d_text calls, the most expensive per-object path in the renderer.

Screen space, not the grid bbox

The tempting fix is to reuse the ground's gx0/gx1/gy0/gy1 bbox, which is already in scope. It is the wrong tool: that bbox is unprojected on the ground plane, but what hangs outside a cell is measured in pixels, not cells.

object reaches above its ground cell by
dropper (tallest, DROPPER_H 0.80) 0.80 * MACH_ISO_ELEV * zoom ~= 22px * zoom
ore value label glyph_h * label_scale + 4 px, and label_scale is clamp(zoom*0.5, 0.6, 3.0), which does not track zoom linearly

One grid step is only TILE_H/2 * zoom = 16px * zoom, so a margin counted in cells is right at one zoom and wrong at every other. So each object's ground point is projected and tested against the screen rect grown by a per-kind margin in pixels.

Ore is culled on its interpolated position (the same lerp draw_item uses), or an ore sliding in from an off-screen cell pops in halfway through its slide.

Painter's order is untouched: the list is sorted on gx+gy, and a subset of a sorted list is still sorted. Culling before the qsort shrinks the sort as well.

Verification

The backtick debug overlay now reads drawn/total for entities and items, so the cull is observable at runtime and an over-tight margin shows up as a count that drops while the object is still on screen.

A headless probe asserts the one property that matters: never cull something visible. It computes each object's true drawn extent independently, by projecting the geometry the renderer actually emits (a block's 8 corners; an ore's diamond plus its label box), then sweeps every cell around the camera across the whole zoom range, calling the renderer's own margin helpers so the test cannot drift from the shipping numbers.

It caught a real bug. The vertical margins were reversed. A machine hanging off the bottom edge is still visible when its top face pokes back up over it, so the bottom test needs the up extent, not the down one. As first written, the cull dropped exactly the tall pieces at exactly the near edge, which is where the eye is. Fixed, and the test now reports:

machines: 139167 tested, 131256 culled (94.3%), 0 wrongly culled
ore:      417501 tested, 394456 culled (94.5%), 0 wrongly culled

./nob release and ./nob game clean under -Wall -Wextra; release binary runs without a crash.

vetr0s added 2 commits July 11, 2026 23:35
MAX_UPGRADERS was 64 because Item.upgraded_mask (the "which upgraders have
already lifted this ore" set) was a single u64. That made the 65th upgrader in
the WHOLE world a silent rejection, with MAX_ENTITIES at 10000 and the largest
region at 128x128. The cap sat in the middle of the intended late game.

The mask is now u64[UPGRADER_WORDS] and the cap is 256. An ore's mask goes 8 ->
32 bytes, so items[1024] grows 48KB -> 72KB, against the 512KB of grids already
in World. The word/bit split lives in mask_test/mask_set/mask_clear and is not
open-coded anywhere, because an id >= 64 in the old `(u64)1 << id` is undefined
rather than visibly wrong.

Save format v3. v1 and v2 files still load: the two bitmaps were one u64 there,
and the only ids such a file can name are 0..63, so they widen into word 0.
This is the first change to the byte layout, so it is the first version the
reader branches on.

Two latent load bugs on the same path, fixed here:

An upgrader_id was never validated on load. A corrupt file with an id past the
cap shifted out of range the first time an ore touched that upgrader. It now
fails the load.

An upgrader dropped by the placement check (off-grid or occupied cell) leaked
its id forever, because the id bitmap was restored wholesale from the file. The
bitmap is now rebuilt from the upgraders that actually land, so it describes
exactly the world that exists. A duplicate id fails the load rather than gating
two upgraders on one bit.

Covered by a headless probe: 256 place and the 257th is rejected, an upgrader
with an id past word 0 lifts the ceiling exactly once and climbs on re-pass, a
v3 file round-trips bits in all four words, a real v2 file written by the
pre-change binary still loads with its masks in word 0, and a corrupt id fails.
The ground was already culled; machines and items were not. Every entity in the
world was pushed into the sort buffer, qsorted, and drawn every frame, on screen
or not: an off-screen conveyor still ran ~9 polygons through the projection, and
an off-screen ore still ran two text draws, which is the most expensive per-object
path in the renderer.

Cull in screen space, not on the grid bbox. The bbox the ground uses is unprojected
on the ground plane, but what hangs outside a cell is measured in pixels, not cells:
a block's height (elevation * MACH_ISO_ELEV * zoom) and an ore's value label (a text
height that scales on label_scale, not linearly with zoom). One grid step is only
TILE_H/2 * zoom pixels, so a margin counted in cells is right at one zoom and wrong
at every other, and it goes wrong by popping machines out at the screen edge. So:
project each object's ground point and test it against the screen rect grown by a
per-kind margin in pixels.

Ore is culled on its interpolated position, the same one draw_item uses, or an ore
sliding in from an off-screen cell appears halfway through its slide.

Painter's order is untouched: the list is sorted on (gx+gy) and a subset of a sorted
list is still sorted. Culling before the qsort shrinks the sort too.

The debug overlay (backtick) now reads drawn/total for entities and items, so the
cull is observable and, more to the point, an over-tight margin shows up as a count
that drops while the thing is still on screen.

Covered by a headless probe that asserts the property that matters: never cull
something visible. It computes each object's true drawn extent by projecting the
geometry the renderer actually emits, then sweeps every cell around the camera
across the whole zoom range against the renderer's own margin helpers. It caught a
real bug: the vertical margins were reversed. A machine hanging off the BOTTOM edge
is still visible when its top face pokes back up over it, so the bottom test needs
the "up" extent, not the "down" one. As first written it culled exactly the tall
pieces at exactly the near edge. Now: 94% of off-screen objects dropped, zero
visible objects culled at any zoom from 0.5 to 5.0.
@vetr0s
vetr0s changed the base branch from fix/upgrader-cap to main July 12, 2026 20:17
@vetr0s
vetr0s merged commit e48169a into main Jul 12, 2026
3 checks passed
@vetr0s
vetr0s deleted the perf/viewport-cull branch July 12, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant