[PR #1094] [MERGED] Cave culling: vanilla's graph rules (every entry face, distant ray test) #3043

Closed
opened 2026-11-02 16:01:02 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/1094
Author: @ludovic111
Created: 10/21/2026
Status: ✅ Merged
Merged: 10/21/2026
Merged by: @ludovic111

Base: main ← Head: fix/cave-culling-vanilla-rules


📝 Commits (1)

  • 6f6354c Cave culling: vanilla's graph rules (every entry face, distant ray test)

📊 Changes

2 files changed (+551 additions, -91 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+2 -3)
📝 crates/horizon-client/src/gfx/occlusion.rs (+549 -88)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

Fixes #782. Fixes #629 (reported by @Online5277; see the #629 section below).

What

The cave culling search (crates/horizon-client/src/gfx/occlusion.rs) now follows vanilla 26.3's SectionOcclusionGraph.runUpdates. I checked the control flow in the bytecode with javap, because JADX garbles this method. Horizon had left out two rules:

  1. Every entry face. A section leads on through whatever any face it was entered through connects to (Node.sourceDirections). Horizon only kept the first face, so it could hide a section vanilla shows. entered is now a 6-bit mask of entry faces.
  2. Distant ray test. Beyond 3 sections from the camera (MINIMUM_ADVANCED_CULLING_SECTION_DISTANCE), a step is taken only if the line from the section's corner towards the camera stays in sections the search has reached. The line is sampled in 28-block steps (CEILED_SECTION_DIAGONAL) while it is more than 60 blocks away. A sample outside the level's height counts as visible. The corner choice and the JOML arithmetic order are vanilla's: no FMA, x*x + (y*y + z*z), 1/sqrt.

The search now takes the camera position, not just its section. It still runs on the worker thread from perf-frame-3, with the same partial updates.

Cost

Run naively, rule 2 multiplies the search time several times over. Three exact shortcuts bring that down:

  • One ray per axis per node. Both steps along an axis share the corner. While the camera stays in the section the search started from, every ray sample stays within the current section's span, or nearer the camera on each axis. So no sample can land in a neighbour that this loop adds. The memo is used only while the eye is still in that section (reuse).

  • One answer per section corner during a full search (Graph::rays). A ray depends only on its corner, the camera and the reached set. Neighbouring nodes share corners, and a search only ever adds reached sections. So:

    • a corner that sees stays seeing;
    • a corner that failed is checked again from the sample it failed at, stepping over the earlier samples with the same float additions, so the points are identical.

    Partial updates don't use it.

  • Integer floor instead of libm's on x86-64.

search_cost microbench (synthetic RD 32 world, 86,264 sections reached, identical with and without each shortcut):

M4 Pro HD 620
no ray test (old rules) 10.4 ms
rays, naive 126 ms
+ per-axis memo + integer floor 12.5 ms 45–50 ms
+ per-corner answers (this PR) 5.9–6.2 ms 21.4 ms

Exactness tests

  • same_sections_as_vanilla compares every cell against a literal port of runUpdates (a map of nodes, FIFO, JOML arithmetic). It uses random worlds, radius 6 and 12, off-centre cameras, and asserts that more than 100 steps went through the ray test.
  • a_section_leads_on_through_every_face_it_is_entered_through covers rule 1.
  • distant_sections_behind_a_wall_are_culled covers rule 2.
  • a_line_checked_again_resumes_where_it_failed: a resumed ray ends where one walked from its corner does, as the reached set grows. A mutation of the replay loop, an off-by-one, fails it.
  • constants_are_vanillas and floor_is_std_floor.

#629

I couldn't reproduce it at the reported spot (seed 5862052499468016705, -2143.68 -30 -2156.44, yaw 137.72 pitch 37.08, night vision, RD 8, stepping forward 0 to 3 blocks). The base build draws 6–9 sections there, and its frames match a build with culling switched off pixel for pixel at every step. This PR's build matches them too.

The stable surface shots did turn up a real hole of the kind #629 describes:

  • RD 32 overview, base build. 416 pixels differ from the no-culling reference: 104 differ by more than 8 levels, up to 142, in a bbox at x 703–718, y 675–692. A section that should be drawn is missing, so the sand and water edge shows what is behind it.
  • This PR's build. 118 pixels differ from the reference, each by 1 level. The reference against itself differs by up to 13 pixels at 1 level.

This is rule 1's effect, and it is the bug #629 describes: a section you can see, culled. This PR closes #629. The reporter can reopen it if they still see it at their spot after a release.

Visibility: screenshots vs. no culling

These are the stable shots on the ThinkPad: animated textures, vignette and clouds off. The reference is the same build with culling switched off.

view base vs. ref this PR vs. ref ref vs. ref (noise)
RD 16 overview 100 px at Δ1 0 9 px at Δ1
RD 16 meadow 0 0 0
RD 16 peak 0 0 0
RD 32 overview 416 px, max Δ142 118 px at Δ1 13 px at Δ1
RD 32 meadow 0 0 0
RD 32 peak 3 px at Δ1 4 px at ≤Δ4 (scattered sky or fog pixels) 0

The shots, masks and the hole crop (reference | base | this PR) are in ~/code/horizon-parity/shots/p3-culling/ on the Mac mini.

Frame time, before → after

Each row is the median of 3 runs. Setup: seed 1234, offscreen, spectator, ticks frozen, horizon-ctl bench. The base build is origin/main. Both builds have only a stats patch for counting, which is not committed.

Mac mini (M4 Pro), drawn sections and frame ms:

RD view sections frame ms
16 overview 1296 → 962 2.52 → 2.06 (−18%)
16 meadow 873 → 465 2.09 → 1.49 (−29%)
16 peak 772 → 758 1.68 → 1.47 (−13%)
32 overview 4902 → 2883 4.86 → 3.28 (−33%)
32 meadow 2726 → 917 2.90 → 1.74 (−40%)
32 peak 3366 → 2456 4.75 → 4.01 (−16%)

The cave rows on the Mac are noise: the same build ranges from 350 to 2233 FPS.

ThinkPad (Intel HD 620), frame ms:

RD view frame ms
16 overview 6.18 → 5.61
16 meadow 6.95 → 3.64
16 peak 12.47 → 7.37
16 cave 4.57 → 4.47
32 overview 17.76 → 13.99
32 meadow 11.42 → 4.65
32 peak 34.13 → 18.66
32 cave 2.44 → 3.19 *

* The RD 32 cave is bimodal for both builds: about 220 or about 400 FPS, depending on the GPU power state. The raw runs are base 229/409/420 FPS and this PR 393/313/220 FPS, so this is not a regression.

Share of reached sections drawn at RD 32: overview 0.99 → 0.73, meadow 0.50 → 0.02, peak 0.95 → 0.55.

Flight: 192 blocks in 12 s, so the search keeps restarting.

Mac mini:

RD avg ms p99 ms frames > 16.7 ms frames > 33 ms searches (time each)
16 2.53 → 2.25 12.6 → 13.6 48 → 36 (1.8 → 9.4 ms)
32 5.22 → 3.15 30.2 → 17.5 78 → 43 17 → 6 (4.0 → 33.5 ms)

ThinkPad (HD 620, 4 threads), final code, medians of 3 per build. The render-thread Occlusion::update cost was also measured: about 0.01 ms per frame on average in both builds, at most about 3 ms in both, apart from one 5 ms and one 7.5 ms.

RD avg frame ms p99 ms frames > 16.7 ms frames > 33 ms full searches search ms
16 6.12 → 5.26 9.6 → 10.2 2 → 5 0 → 0 307 → 173 4.2 → 16.1
32 12.51 → 10.83 22.8 → 22.0 42 → 31 3 → 7 127 → 66 19.3 → 85.6

Per-run p99 at RD 16 overlaps between the builds: base 9.1–9.8 ms, this PR 8.2–10.6 ms. Pooled with an earlier round of 3, the medians are 9.35 and 9.6 ms. Frames over 33 ms at RD 32: base 3/5/2, this PR 1/9/7, out of about 1,100 frames per run. Both builds also have lone 100–200 ms frames during flight: base 126 and 154 ms, this PR 109 and 216 ms. These are chunk streaming on the 4-thread laptop, not the search.

An earlier version without the per-corner answers took about 150–280 ms per search in this flight. It had a worse RD 32 tail (p99 22.4 → 26.3 ms over 6 runs) because the worker took a core from the mesh workers. That's what the per-corner answers fix.

The search cost and why it's acceptable

The search gets longer: rays are the price of culling more. It runs on the culling worker, never on the frame:

  • frames keep drawing with the previous result (frames_never_wait_and_partial_updates_open_at_once);
  • block changes still go through partial updates;
  • in caves the search gets faster, because the rays stop it early (ThinkPad RD 32 cave: 3.7 → 1.1 ms).

Final code, ThinkPad flight at RD 32: 19 → 86 ms per search. The frame tables above were measured with the version before the per-corner answers, whose searches were about 2× longer. The per-corner answers are exact, so the drawn sections are the same; the static views draw the same.

Frame time is better at every view and distance measured. The only metrics not clearly better are RD 16 flight p99, which is within run-to-run spread, and the frames over 33 ms at RD 32 flight, which are single-digit counts and noisy.

Proof

On head 6f6354c2, rebased on origin/main e66c35b2, Mac mini:

python3 tools/gen_version_consts.py --check                     exit 0
cargo fmt --all -- --check                                       exit 0
set -o pipefail; cargo clippy --workspace --all-targets -- -D warnings   exit 0
cargo test -p horizon-client                                     exit 0 (952 + 84 + 15 + 2 + 2 + 2 + 1 passed)
cargo test --profile fast -p horizon-client --lib occlusion::tests::search_cost -- --ignored
                                                                 M4 Pro 6.16 ms, HD 620 21.4 ms (86,264 reached)
python3 tools/parity.py --changed origin/main                    262 passed, 2 failed (158 min, 264 selected)

The two failures are main's, not this PR's:

  • entity_model (normals of creaking eyes and warden tendrils) fails the same way on plain origin/main e66c35b2. I ran it there.
  • block_particles (hit-particle counts): every input its tracer recorded is byte-identical to e66c35b2. Those are particles.rs, game.rs, blocks.rs, mesher.rs, its fixture, horizon-core, tools, mods and Cargo.lock; this PR touches only gfx.rs and gfx/occlusion.rs. particles.rs last changed today in #1083.

parity.py --changed selects every harness (325), because shared-local currently has recorded dependencies for only one of them. No harness covers SectionOcclusionGraph; the exactness tests above are its oracle.

For Ludovic

  • Vanilla-exact visibility is now the default. There is no option to go back to the old rule.
  • #1089's "Saves time" tag for cave culling comes from measured frame times, so it will understate the saving once this lands.
  • Surface views draw 40–66% fewer sections at RD 32. That matters for LOD (#718, #1029), which this PR leaves alone.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/1094 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/21/2026 **Status:** ✅ Merged **Merged:** 10/21/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/cave-culling-vanilla-rules` --- ### 📝 Commits (1) - [`6f6354c`](https://github.com/horizon-rust/horizon/commit/6f6354c2c0421fef8bab6f7b0a74c1ecc86bec89) Cave culling: vanilla's graph rules (every entry face, distant ray test) ### 📊 Changes **2 files changed** (+551 additions, -91 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+2 -3) 📝 `crates/horizon-client/src/gfx/occlusion.rs` (+549 -88) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Fixes #782. Fixes #629 (reported by @Online5277; see the #629 section below). ## What The cave culling search (`crates/horizon-client/src/gfx/occlusion.rs`) now follows vanilla 26.3's `SectionOcclusionGraph.runUpdates`. I checked the control flow in the bytecode with `javap`, because JADX garbles this method. Horizon had left out two rules: 1. **Every entry face.** A section leads on through whatever *any* face it was entered through connects to (`Node.sourceDirections`). Horizon only kept the first face, so it could hide a section vanilla shows. `entered` is now a 6-bit mask of entry faces. 2. **Distant ray test.** Beyond 3 sections from the camera (`MINIMUM_ADVANCED_CULLING_SECTION_DISTANCE`), a step is taken only if the line from the section's corner towards the camera stays in sections the search has reached. The line is sampled in 28-block steps (`CEILED_SECTION_DIAGONAL`) while it is more than 60 blocks away. A sample outside the level's height counts as visible. The corner choice and the JOML arithmetic order are vanilla's: no FMA, `x*x + (y*y + z*z)`, `1/sqrt`. The search now takes the camera position, not just its section. It still runs on the worker thread from perf-frame-3, with the same partial updates. ### Cost Run naively, rule 2 multiplies the search time several times over. Three exact shortcuts bring that down: - **One ray per axis per node.** Both steps along an axis share the corner. While the camera stays in the section the search started from, every ray sample stays within the current section's span, or nearer the camera on each axis. So no sample can land in a neighbour that this loop adds. The memo is used only while the eye is still in that section (`reuse`). - **One answer per section corner** during a full search (`Graph::rays`). A ray depends only on its corner, the camera and the reached set. Neighbouring nodes share corners, and a search only ever adds reached sections. So: - a corner that sees stays seeing; - a corner that failed is checked again from the sample it failed at, stepping over the earlier samples with the same float additions, so the points are identical. Partial updates don't use it. - **Integer floor** instead of libm's on x86-64. `search_cost` microbench (synthetic RD 32 world, 86,264 sections reached, identical with and without each shortcut): | | M4 Pro | HD 620 | | --- | --- | --- | | no ray test (old rules) | | 10.4 ms | | rays, naive | | 126 ms | | + per-axis memo + integer floor | 12.5 ms | 45–50 ms | | + per-corner answers (this PR) | 5.9–6.2 ms | 21.4 ms | ### Exactness tests - `same_sections_as_vanilla` compares every cell against a literal port of `runUpdates` (a map of nodes, FIFO, JOML arithmetic). It uses random worlds, radius 6 and 12, off-centre cameras, and asserts that more than 100 steps went through the ray test. - `a_section_leads_on_through_every_face_it_is_entered_through` covers rule 1. - `distant_sections_behind_a_wall_are_culled` covers rule 2. - `a_line_checked_again_resumes_where_it_failed`: a resumed ray ends where one walked from its corner does, as the reached set grows. A mutation of the replay loop, an off-by-one, fails it. - `constants_are_vanillas` and `floor_is_std_floor`. ## #629 I couldn't reproduce it at the reported spot (seed 5862052499468016705, `-2143.68 -30 -2156.44`, yaw 137.72 pitch 37.08, night vision, RD 8, stepping forward 0 to 3 blocks). The base build draws 6–9 sections there, and its frames match a build with culling switched off pixel for pixel at every step. This PR's build matches them too. The stable surface shots did turn up a real hole of the kind #629 describes: - **RD 32 overview, base build.** 416 pixels differ from the no-culling reference: 104 differ by more than 8 levels, up to 142, in a bbox at x 703–718, y 675–692. A section that should be drawn is missing, so the sand and water edge shows what is behind it. - **This PR's build.** 118 pixels differ from the reference, each by 1 level. The reference against itself differs by up to 13 pixels at 1 level. This is rule 1's effect, and it is the bug #629 describes: a section you can see, culled. This PR closes #629. The reporter can reopen it if they still see it at their spot after a release. ## Visibility: screenshots vs. no culling These are the stable shots on the ThinkPad: animated textures, vignette and clouds off. The reference is the same build with culling switched off. | view | base vs. ref | this PR vs. ref | ref vs. ref (noise) | | --- | --- | --- | --- | | RD 16 overview | 100 px at Δ1 | 0 | 9 px at Δ1 | | RD 16 meadow | 0 | 0 | 0 | | RD 16 peak | 0 | 0 | 0 | | RD 32 overview | **416 px, max Δ142** | 118 px at Δ1 | 13 px at Δ1 | | RD 32 meadow | 0 | 0 | 0 | | RD 32 peak | 3 px at Δ1 | 4 px at ≤Δ4 (scattered sky or fog pixels) | 0 | The shots, masks and the hole crop (reference | base | this PR) are in `~/code/horizon-parity/shots/p3-culling/` on the Mac mini. ## Frame time, before → after Each row is the median of 3 runs. Setup: seed 1234, offscreen, spectator, ticks frozen, `horizon-ctl bench`. The base build is origin/main. Both builds have only a stats patch for counting, which is not committed. **Mac mini (M4 Pro)**, drawn sections and frame ms: | RD | view | sections | frame ms | | --- | --- | --- | --- | | 16 | overview | 1296 → 962 | 2.52 → 2.06 (−18%) | | 16 | meadow | 873 → 465 | 2.09 → 1.49 (−29%) | | 16 | peak | 772 → 758 | 1.68 → 1.47 (−13%) | | 32 | overview | 4902 → 2883 | 4.86 → 3.28 (−33%) | | 32 | meadow | 2726 → 917 | 2.90 → 1.74 (−40%) | | 32 | peak | 3366 → 2456 | 4.75 → 4.01 (−16%) | The cave rows on the Mac are noise: the same build ranges from 350 to 2233 FPS. **ThinkPad (Intel HD 620)**, frame ms: | RD | view | frame ms | | --- | --- | --- | | 16 | overview | 6.18 → 5.61 | | 16 | meadow | 6.95 → 3.64 | | 16 | peak | 12.47 → 7.37 | | 16 | cave | 4.57 → 4.47 | | 32 | overview | 17.76 → 13.99 | | 32 | meadow | 11.42 → 4.65 | | 32 | peak | 34.13 → 18.66 | | 32 | cave | 2.44 → 3.19 * | \* The RD 32 cave is bimodal for both builds: about 220 or about 400 FPS, depending on the GPU power state. The raw runs are base 229/409/420 FPS and this PR 393/313/220 FPS, so this is not a regression. Share of reached sections drawn at RD 32: overview 0.99 → 0.73, meadow 0.50 → 0.02, peak 0.95 → 0.55. **Flight**: 192 blocks in 12 s, so the search keeps restarting. Mac mini: | RD | avg ms | p99 ms | frames > 16.7 ms | frames > 33 ms | searches (time each) | | --- | --- | --- | --- | --- | --- | | 16 | 2.53 → 2.25 | 12.6 → 13.6 | | | 48 → 36 (1.8 → 9.4 ms) | | 32 | 5.22 → 3.15 | 30.2 → 17.5 | 78 → 43 | 17 → 6 | (4.0 → 33.5 ms) | ThinkPad (HD 620, 4 threads), final code, medians of 3 per build. The render-thread `Occlusion::update` cost was also measured: about 0.01 ms per frame on average in both builds, at most about 3 ms in both, apart from one 5 ms and one 7.5 ms. | RD | avg frame ms | p99 ms | frames > 16.7 ms | frames > 33 ms | full searches | search ms | | --- | --- | --- | --- | --- | --- | --- | | 16 | 6.12 → 5.26 | 9.6 → 10.2 | 2 → 5 | 0 → 0 | 307 → 173 | 4.2 → 16.1 | | 32 | 12.51 → 10.83 | 22.8 → 22.0 | 42 → 31 | 3 → 7 | 127 → 66 | 19.3 → 85.6 | Per-run p99 at RD 16 overlaps between the builds: base 9.1–9.8 ms, this PR 8.2–10.6 ms. Pooled with an earlier round of 3, the medians are 9.35 and 9.6 ms. Frames over 33 ms at RD 32: base 3/5/2, this PR 1/9/7, out of about 1,100 frames per run. Both builds also have lone 100–200 ms frames during flight: base 126 and 154 ms, this PR 109 and 216 ms. These are chunk streaming on the 4-thread laptop, not the search. An earlier version without the per-corner answers took about 150–280 ms per search in this flight. It had a worse RD 32 tail (p99 22.4 → 26.3 ms over 6 runs) because the worker took a core from the mesh workers. That's what the per-corner answers fix. ### The search cost and why it's acceptable The search gets longer: rays are the price of culling more. It runs on the culling worker, never on the frame: - frames keep drawing with the previous result (`frames_never_wait_and_partial_updates_open_at_once`); - block changes still go through partial updates; - in caves the search gets faster, because the rays stop it early (ThinkPad RD 32 cave: 3.7 → 1.1 ms). Final code, ThinkPad flight at RD 32: 19 → 86 ms per search. The frame tables above were measured with the version before the per-corner answers, whose searches were about 2× longer. The per-corner answers are exact, so the drawn sections are the same; the static views draw the same. Frame time is better at every view and distance measured. The only metrics not clearly better are RD 16 flight p99, which is within run-to-run spread, and the frames over 33 ms at RD 32 flight, which are single-digit counts and noisy. ## Proof On head 6f6354c2, rebased on origin/main e66c35b2, Mac mini: ``` python3 tools/gen_version_consts.py --check exit 0 cargo fmt --all -- --check exit 0 set -o pipefail; cargo clippy --workspace --all-targets -- -D warnings exit 0 cargo test -p horizon-client exit 0 (952 + 84 + 15 + 2 + 2 + 2 + 1 passed) cargo test --profile fast -p horizon-client --lib occlusion::tests::search_cost -- --ignored M4 Pro 6.16 ms, HD 620 21.4 ms (86,264 reached) python3 tools/parity.py --changed origin/main 262 passed, 2 failed (158 min, 264 selected) ``` The two failures are main's, not this PR's: - `entity_model` (normals of creaking eyes and warden tendrils) fails the same way on plain origin/main e66c35b2. I ran it there. - `block_particles` (hit-particle counts): every input its tracer recorded is byte-identical to e66c35b2. Those are particles.rs, game.rs, blocks.rs, mesher.rs, its fixture, horizon-core, tools, mods and Cargo.lock; this PR touches only gfx.rs and gfx/occlusion.rs. particles.rs last changed today in #1083. `parity.py --changed` selects every harness (325), because shared-local currently has recorded dependencies for only one of them. No harness covers `SectionOcclusionGraph`; the exactness tests above are its oracle. ## For Ludovic - Vanilla-exact visibility is now the default. There is no option to go back to the old rule. - #1089's "Saves time" tag for cave culling comes from measured frame times, so it will understate the saving once this lands. - Surface views draw 40–66% fewer sections at RD 32. That matters for LOD (#718, #1029), which this PR leaves alone. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 16:01:02 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#3043
No description provided.