perf: viewport-cull machines and items - #17
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #16 (both touch render_game.c). Rebases onto
mainonce #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 twomach_r2d_textcalls, 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/gy1bbox, 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.DROPPER_H 0.80)0.80 * MACH_ISO_ELEV * zoom~= 22px * zoomglyph_h * label_scale + 4px, andlabel_scaleisclamp(zoom*0.5, 0.6, 3.0), which does not track zoom linearlyOne 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_itemuses), 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 theqsortshrinks the sort as well.Verification
The backtick debug overlay now reads
drawn/totalfor 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:
./nob releaseand./nob gameclean under-Wall -Wextra; release binary runs without a crash.