rasterized occlusion culling #38

Merged
embeddedt merged 20 commits from rdh/celeritas:raster into stonecutter 2026-09-04 22:15:05 -04:00
Collaborator

Adds an optional software occlusion buffer that culls chunk sections hidden behind solid terrain, which the connectivity graph cannot catch, since a section can have an air path to the camera and still be hidden.

Chunk builds decompose a section's opaque blocks into a small set of occluder boxes (BoxFinder, ported from Canvas) and store them on BuiltRenderSectionData. During the graph search each section's boxes are tested against a 1024×1024 coverage buffer and, if visible, drawn into it as an occluder. Testing inside the search rather than after it means an occluded section is not expanded through, so culling shrinks the traversal instead of adding to it.

Sections are tested against the bounds of what they actually draw. A section whose geometry is hidden but whose volume is not fully covered stays traversable, so geometry behind it is not lost.

Results

gradle :common:rasterCullReport: visible sections at 32 render distance while turning sideways:

world camera baseline occluded delta backtrack
SURFACE surface 13872 10767 -22.4% 379
SURFACE treetop 15441 12249 -20.7% 384
SURFACE high 5060 5034 -0.5% 0
CAVES surface 1953 11 -99.4% 1
CAVES treetop 3882 20 -99.5% 4
CAVES high 1123 385 -65.7% 27

Aerial views over surface terrain gain little, as expected. The buffer holds coverage with no depth, so it is only correct if sections are drawn front to back; the BFS visits in hop-count order, which approximates that. backtrack counts sections culled while the buffer already held something further away. It's just an upper bound on how often that approximation could mislead though, not an actual error count.

Adds an optional software occlusion buffer that culls chunk sections hidden behind solid terrain, which the connectivity graph cannot catch, since a section can have an air path to the camera and still be hidden. Chunk builds decompose a section's opaque blocks into a small set of occluder boxes (`BoxFinder`, ported from Canvas) and store them on `BuiltRenderSectionData`. During the graph search each section's boxes are tested against a 1024×1024 coverage buffer and, if visible, drawn into it as an occluder. Testing inside the search rather than after it means an occluded section is not expanded through, so culling shrinks the traversal instead of adding to it. Sections are tested against the bounds of what they actually draw. A section whose geometry is hidden but whose volume is not fully covered stays traversable, so geometry behind it is not lost. ## Results `gradle :common:rasterCullReport`: visible sections at 32 render distance while turning sideways: | world | camera | baseline | occluded | delta | backtrack | |---------|---------|---------:|---------:|-------:|----------:| | SURFACE | surface | 13872 | 10767 | -22.4% | 379 | | SURFACE | treetop | 15441 | 12249 | -20.7% | 384 | | SURFACE | high | 5060 | 5034 | -0.5% | 0 | | CAVES | surface | 1953 | 11 | -99.4% | 1 | | CAVES | treetop | 3882 | 20 | -99.5% | 4 | | CAVES | high | 1123 | 385 | -65.7% | 27 | Aerial views over surface terrain gain little, as expected. The buffer holds coverage with no depth, so it is only correct if sections are drawn front to back; the BFS visits in hop-count order, which approximates that. `backtrack` counts sections culled while the buffer already held something further away. It's just an upper bound on how often that approximation could mislead though, not an actual error count.
embeddedt added 12 commits 2026-09-04 19:56:12 -04:00
benches the cost on the build threads
a section holding one block was tested as though it filled its whole volume, so sparse sections were barely culled even though they contributed basically no coverage
this is why you dont mess with git history kids
Per-frame cost of the rasterized occlusion culler drops from 3.1ms to 1.4ms
at render distance 32 and from 15.7ms to under 4ms at 64 (JMH rotatingCamera,
SURFACE), against 0.5ms / 1.9ms for the culler with the raster off. Culled
section counts are unchanged to within a few sections per frame.

Rasterizer (grondag.bitraster):
- Test polygons take a center-pixel fast path: the centre of a convex box
  projects inside its silhouette, so one clear pixel there proves visibility
  with a single transform. Resolves ~80% of section tests.
- Boxes are drawn and tested as one convex silhouette polygon (4 or 6
  vertices, derived from the existing per-face quad tables) instead of two or
  three overlapping quads. The quad path remains as the near-clip fallback.
- Edge events are populated lazily per 8-row tile band instead of for the
  whole polygon up front, with the flat-edge filler clamped to the band.
- Tests exit early on an untouched tile, and each band computes the range of
  tile columns fully inside the polygon so tests return and draws fill
  without computing coverage.
- A one-bit-per-tile "fully covered" bitset lets scans skip covered runs 64
  tiles at a time.
- Vertex transforms use a float view-projection matrix plus a per-region
  offset instead of copying and translating a fixed-point matrix per section;
  box corners are built from one transformed corner and three axis deltas.
- Edge slopes are computed once per polygon (double divide) rather than per
  band with a 64-bit integer divide, and only when a band actually needs them.
- Test polygons are dilated by one pixel on every side so a visible strip
  thinner than the pixel pitch still reaches a sampled pixel centre. This
  replaces the distance-gated one-block world-space fuzz, whose margin was
  several pixels mid-range and under a pixel at the far end.
- Buffer geometry is per instance and per axis. Each frame the width and
  height are chosen from the view-projection matrix's row lengths (the
  horizontal and vertical projection scales) so that a block at the search
  distance spans at least one pixel on each axis, with hysteresis so
  field-of-view animation cannot thrash it. Sizes range 512..2048 per axis.
- Fix an off-by-one in the centre test where a screen coordinate just below
  1.0 rounded to a pixel row equal to the buffer height.
- Work counters for the stats report, gated behind a JIT-constant flag.

Culler integration:
- The lattice mirrors each cell's occluder box array so the raster test does
  not chase section and build-data pointers per visited cell.
- The search distance is passed to the raster occluder for buffer sizing.

Benchmarks:
- RasterStatsReport (gradle rasterStats): per-frame counts of tests, draws
  and tiles by outcome, a test/occlude time split, and a camera-height scan.
- RasterCullReport prints the chosen buffer size and accepts render distance
  and field of view via system properties, plus an FOV sweep mode.
Per-frame cost of the rasterized culler (JMH rotatingCamera, SURFACE, two
forks) drops from 1.42ms to 1.09ms at render distance 32 and from 4.62ms to
3.09ms at 64, against 0.48ms / 1.86ms with the raster off. Culled section
counts match the previous commit within a few sections per placement at 32
and within 0.4% at 64; CAVES is unchanged.

Culler integration:
- Whole regions are settled at once where possible. The rasterizer keeps a
  coarse map of 64px cells that have been drawn into, with a version that
  changes when a cell is first touched. Each region's screen rectangle is
  projected once per scene; while its cells are untouched, an air section in
  it is visible with one compare, since its centre pixel must be clear. A
  region straddling a screen edge keeps the clip-space position of its first
  section centre, from which any section centre's on-screen check is a few
  multiplies with no divide. Resolves 4.6k of 11k tests at 32 and 30k of 51k
  at 64. Exactly equivalent to the centre test it replaces.
- The lattice's metadata word carries a "has occluder data" bit and the
  bounds word is mirrored densely, so air sections skip the pointer load and
  terrain sections skip a second dependent cache miss.

Rasterizer (grondag.bitraster):
- Buffer axes are any multiple of eight tiles, not powers of two: rows are
  addressed by multiplication, so the buffer at 32 chunks is 1280x832 instead
  of 2048x1024 and at 64 it is 2048x1472 instead of 2048x2048. Hysteresis is
  one 64px step, so a wavering field of view neither grows nor shrinks it.
- Projected silhouette vertices are sampled before any polygon setup; a clear
  pixel under one proves visibility. Cuts polygon tests from 2.4k to 750 per
  frame at 32.
- Edges are prepared lazily and as left/right chains walked per band, with
  only the edge active on each row evaluated. This handles any chain length,
  where the previous max-of-three silently dropped a fourth edge produced by
  the rounding of a nearly flat one, and drops edges that contain no row
  centre instead of extrapolating them.
- Band population is a single pass that derives row spans from the polygon's
  row range, replacing the flat-event, dilation and range passes.
- Draws bulk-fill runs of interior tiles and clip each band's scan to the
  polygon's actual extent; tile coverage is computed without branches.
- A small polygon whose bounding rectangle is fully covered at the pixel
  level is hidden before any edge setup; catches 29% of hidden tests at 64.
- Vertex setup no longer stores clip-space coordinates the unclipped path
  never reads, and rounds through the floor intrinsic.
- Region offsets no longer overflow int beyond 524k blocks.

Measured and left alone: halving the pixels-per-block requirement gains only
8% at 32 and nothing at 64 now that per-polygon setup rather than tile work
dominates, so the buffer size policy stands; skipping the near-camera box
draws gains 3-5% but visibly weakens cave culling.

The report tasks accept -Dreport.renderDistance and the stats report counts
region skips, vertex hits and rectangle-hidden tests.
embeddedt force-pushed raster from e6cb8e9f74 to 254659b5ec 2026-09-04 19:56:12 -04:00 Compare
embeddedt added 1 commit 2026-09-04 20:07:32 -04:00
embeddedt added 1 commit 2026-09-04 20:19:55 -04:00
embeddedt added 1 commit 2026-09-04 21:37:34 -04:00
embeddedt added 1 commit 2026-09-04 21:40:23 -04:00
embeddedt added 1 commit 2026-09-04 21:47:32 -04:00
embeddedt added 3 commits 2026-09-04 22:12:30 -04:00
embeddedt merged commit 7190f87d81 into stonecutter 2026-09-04 22:15:05 -04:00
embeddedt deleted branch raster 2026-09-04 22:15:05 -04:00
Sign in to join this conversation.
No Reviewers
No labels
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: embeddedt/celeritas#38