rasterized occlusion culling #38

Merged
embeddedt merged 20 commits from rdh/celeritas:raster into stonecutter 2026-09-04 22:15:05 -04:00
20 Commits
Author SHA1 Message Date
embeddedt bb8c95dbd2 Re-search when a rebuild changes occluder boxes 2026-09-04 22:12:10 -04:00
embeddedt 1ffa1c2079 Count remaining voxels after clearing section bits 2026-09-04 22:12:10 -04:00
embeddedt 6bf9fb66e5 Implement missing raster culling override on ornithe 2026-09-04 22:12:10 -04:00
embeddedt b69525d7d9 Clean up lattice and only store raster occlusion arrays when enabled 2026-09-04 21:47:17 -04:00
embeddedt 344e3c549c Remove single-use RasterMatrices class 2026-09-04 21:40:07 -04:00
embeddedt bd657ebc03 Hoist some loads for consistency 2026-09-04 21:37:19 -04:00
embeddedt 26784eb0e8 Refactor visibility builder logic 2026-09-04 20:19:42 -04:00
embeddedt 2545927343 Define occluder data flag properly 2026-09-04 20:07:07 -04:00
embeddedt 254659b5ec cut raster occlusion overhead by a third at 32 chunks and over half at 64
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.
2026-09-04 19:55:56 -04:00
embeddedt f947f8f3ab speed up raster occlusion culling ~4-8x and harden its accuracy
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.
2026-09-04 19:55:56 -04:00
rdh 33671a2f30 oops
this is why you dont mess with git history kids
2026-09-04 19:55:56 -04:00
rdh 74c6cf0ffd test sections against the bounds of what they draw
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
2026-09-04 19:55:56 -04:00
rdh 5ea0f09aef cull sections against the rasterized occlusion buffer 2026-09-04 19:55:56 -04:00
rdh 3487525d91 carry the view matrix on Viewport 2026-09-04 19:55:56 -04:00
rdh c56653597f wire up occluder box generation 2026-09-04 19:55:56 -04:00
rdh 9a662d7577 generate and store occluder boxes 2026-09-04 19:55:56 -04:00
rdh aa377a21e3 1024px raster buffer and cull report baseline 2026-09-04 19:55:56 -04:00
rdh f20917afb5 add BoxFinder benchmark
benches the cost on the build threads
2026-09-04 19:55:56 -04:00
rdh 852bfb8d8d port BoxFinder and add raster occlusion toggle 2026-09-04 19:55:56 -04:00
rdh 706e69884b vendor bitraster 2026-09-04 19:55:56 -04:00