[PR #939] [MERGED] Terrain: occlusion culling of solid sections that pays for itself (Vulkan) #2928

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

📋 Pull Request Information

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

Base: main ← Head: perf/occlusion-cull-cheap


📝 Commits (1)

  • 13cd895 Terrain: occlusion culling of solid sections (Vulkan)

📊 Changes

5 files changed (+800 additions, -10 deletions)

View changed files

📝 crates/horizon-client/src/game/control.rs (+11 -0)
📝 crates/horizon-client/src/game/render_control.rs (+1 -0)
📝 crates/horizon-client/src/gfx.rs (+176 -10)
➕ crates/horizon-client/src/gfx/cull.rs (+511 -0)
➕ crates/horizon-client/src/gfx/cull.wgsl (+101 -0)

📄 Description

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

Replaces draft #906 (same goal, new method): occlusion culling of solid sections whose overhead no longer outweighs what it saves.

What

Sections that terrain in front hides entirely (after frustum and cave culling) are drawn with an instance count of 0, on Vulkan:

  1. Phase 1 draws the sections that the last test read back (2 to 3 frames late) saw, straight from the frame's draw list.
  2. In the same pass, the test draws the box of every other section, plus an eighth of phase 1's in turn (so a section that goes out of sight leaves phase 1). Each box is grown by one block, drawn front faces only, depth test on with no writes, and its fragment shader is forced past an early depth test (@early_depth_test(force)). A fragment that runs is the box's nearest point on a pixel in front of phase 1's depth. It marks the section seen and sets its phase 2 draws' instance counts to 1.
  3. Phase 2, in a second pass that loads both targets, draws those draws. After it come entities and translucent terrain, unchanged.

The result is exact. A section is left out only if, on every pixel, its box is behind phase 1's depth, and the final depth is only nearer. What the CPU knows only sorts sections between the two phases: a stale result draws a hidden section in phase 1, or tests a visible one.

Why #906 cost so much, and what changed (timed piece by piece on the HD 620; data in horizon-parity/shots/perf-frame-7):

  • #906 kept both phases as full-length draw lists. On Intel Gen9, Mesa unrolls each indirect multi-draw on the CPU, one draw per entry whatever its instance count. Doubling the entries cost 0.3–1.7 ms of CPU and about 0.1–0.4 ms of GPU front end. Here every solid draw is one entry of one list, as on main.
  • Building the Hi-Z level 0 meant resolving and reading the depth buffer: about 0.35 ms on its own, plus about 0.2 ms for the pyramid and the test. The raster test reads no depth: the depth test (with HiZ) does the work.
  • Boxes over a quarter of the screen count as seen without being drawn.
  • Culling pauses while the last result hid under 10% of the frame's solid indices. The peak view, for example, hides about 7%. While paused, one frame in 64 tests every section.

HORIZON_GPU_CULL=0 turns it off. all culls every frame whatever the share, for A/B checks and screenshots. section-cull on|off switches it in a running game. status reports renderer.culled_sections.

Metal: not enabled. The test needs forced early fragment tests, which naga's MSL backend doesn't emit. wgpu's Metal backend also replays a multi-draw as one call per entry. And the M4 Pro is CPU-bound here: its GPU is busy under 2 ms per frame at RD16. #906's Hi-Z version, forced on Metal, only added CPU.

Numbers

ThinkPad HD 620, Mesa 26.1.8 ANV Vulkan, 1280×720, ticks frozen, perf-frame-6's six views. Each row is one binary in one game: culling off/on/on/off, 6 s each, with section-cull (2 rounds at RD16, 1 at RD32).

RD16 view hidden solid indices FPS off → on 1% low off → on CPU ms GPU ms
meadow 68% 184.6 → 242.8 (+32%) 84.5 → 99.9 2.11 → 1.90 5.26 → 3.94
forest 60% 134.3 → 191.1 (+42%) 71.0 → 84.1 2.56 → 2.21 7.27 → 5.07
slope 73% 192.2 → 241.8 (+26%) 77.0 → 83.0 1.86 → 1.82 5.04 → 3.95
mountain 35% 150.9 → 183.4 (+22%) 68.9 → 80.5 2.33 → 2.14 6.46 → 5.29
peak 7% (paused) 157.8 → 157.3 (−0.3%) 87.6 → 99.1 2.68 → 2.64 6.19 → 6.22
overview 41% 138.4 → 169.2 (+22%) 88.9 → 100.5 2.93 → 2.92 7.07 → 5.75
RD32 view hidden solid indices FPS off → on 1% low off → on CPU ms GPU ms
meadow 86% 78.8 → 160.2 (+103%) 49.3 → 86.3 4.75 → 3.10 12.42 → 6.00
forest 79% 53.9 → 112.4 (+109%) 50.6 → 94.8 7.55 → 3.21 18.24 → 8.67
slope 88% 58.7 → 110.1 (+88%) 51.4 → 73.8 6.51 → 3.41 16.70 → 8.81
mountain 50% 51.9 → 75.4 (+45%) 45.1 → 56.9 7.41 → 4.98 18.92 → 12.96
peak 28% 42.8 → 48.9 (+14%) 38.8 → 37.4 9.61 → 8.40 22.93 → 20.06
overview 51% 42.4 → 62.1 (+46%) 38.7 → 42.3 9.42 → 6.53 23.15 → 15.71

For comparison, #906 on the same machine at RD16 went −15% to +17%.

Mac mini (M4 Pro, Metal, RD16). Culling is off on Metal, so this is main vs the PR binary, run in the order main, PR, PR, main. The machine was under load 96 from other agents' CI and harnesses, so the same binary varied by up to ±40% between runs (main's meadow: 194 then 330 FPS) and no difference can be read from it. On Metal the PR draws exactly what main draws: the culling object is never created, and the draw list is built and drawn as before.

RD16 view FPS main → PR 1% low CPU ms GPU ms
meadow 262.1 → 199.6 21.4 → 18.3 2.75 → 3.59 0.43 → 0.56
forest 379.8 → 316.0 27.4 → 20.1 1.75 → 2.18 0.36 → 0.36
slope 254.0 → 239.8 17.0 → 18.6 3.14 → 3.20 0.54 → 0.38
mountain 302.3 → 355.9 23.4 → 31.2 2.71 → 2.29 0.45 → 0.38
peak 267.6 → 173.7 23.5 → 15.1 2.89 → 3.51 0.57 → 0.64
overview 220.8 → 194.2 15.9 → 16.1 3.61 → 3.52 0.50 → 0.64

Per-run FPS, to show the spread: meadow main 194/330 vs PR 182/217; mountain main 428/177 vs PR 488/223; peak main 353/183 vs PR 188/159.

Pixels

  • Static views (RD16). Animated textures off, three fresh games per view: off, HORIZON_GPU_CULL=all, off. Meadow, forest, slope and mountain differ by 0 px. Peak differs by 1 px, the same pixel as main vs main. Overview differs by 441 px against 426 px main vs main, all on water. Outside the main-vs-main noise, every view differs by 0 px.
  • Camera jumps. 21 frames, each the first frame or so after a jump: 12 yaw jumps of 30° and 9 of 40° at another pitch, with phase 1 coming from a view far away. Culling off vs all: 0 px in every frame, so nothing pops.

How tested

cargo fmt --all --check                                          ok
cargo clippy --workspace --all-targets -- -D warnings            ok
gfx::cull::tests::shader_validates (naga)                        ok
cargo test -p horizon-client                                     ok (887 + 75 + 15 + ... passed)
tools/parity.py section_mesh translucency light hud              4 passed, 0 failed (Mac mini)

CI parity covers the other harnesses. They run on the Mac, where this code path is off (Metal).

Needs Ludovic

Nothing. #906 can be closed once this merges; it's left open for the orchestrator.

🤖 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/939 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/18/2026 **Status:** ✅ Merged **Merged:** 10/18/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/occlusion-cull-cheap` --- ### 📝 Commits (1) - [`13cd895`](https://github.com/horizon-rust/horizon/commit/13cd89578a3ef14d9e74ac8be81a3032aad699c6) Terrain: occlusion culling of solid sections (Vulkan) ### 📊 Changes **5 files changed** (+800 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/control.rs` (+11 -0) 📝 `crates/horizon-client/src/game/render_control.rs` (+1 -0) 📝 `crates/horizon-client/src/gfx.rs` (+176 -10) ➕ `crates/horizon-client/src/gfx/cull.rs` (+511 -0) ➕ `crates/horizon-client/src/gfx/cull.wgsl` (+101 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Replaces draft #906 (same goal, new method): occlusion culling of solid sections whose overhead no longer outweighs what it saves. ## What Sections that terrain in front hides entirely (after frustum and cave culling) are drawn with an instance count of 0, on Vulkan: 1. **Phase 1** draws the sections that the last test read back (2 to 3 frames late) saw, straight from the frame's draw list. 2. In the same pass, the **test** draws the box of every other section, plus an eighth of phase 1's in turn (so a section that goes out of sight leaves phase 1). Each box is grown by one block, drawn front faces only, depth test on with no writes, and its fragment shader is forced past an early depth test (`@early_depth_test(force)`). A fragment that runs is the box's nearest point on a pixel in front of phase 1's depth. It marks the section seen and sets its phase 2 draws' instance counts to 1. 3. **Phase 2**, in a second pass that loads both targets, draws those draws. After it come entities and translucent terrain, unchanged. The result is exact. A section is left out only if, on every pixel, its box is behind phase 1's depth, and the final depth is only nearer. What the CPU knows only sorts sections between the two phases: a stale result draws a hidden section in phase 1, or tests a visible one. **Why #906 cost so much, and what changed** (timed piece by piece on the HD 620; data in `horizon-parity/shots/perf-frame-7`): - #906 kept both phases as full-length draw lists. On Intel Gen9, Mesa unrolls each indirect multi-draw on the CPU, one draw per entry whatever its instance count. Doubling the entries cost 0.3–1.7 ms of CPU and about 0.1–0.4 ms of GPU front end. Here every solid draw is one entry of one list, as on main. - Building the Hi-Z level 0 meant resolving and reading the depth buffer: about 0.35 ms on its own, plus about 0.2 ms for the pyramid and the test. The raster test reads no depth: the depth test (with HiZ) does the work. - Boxes over a quarter of the screen count as seen without being drawn. - Culling pauses while the last result hid under 10% of the frame's solid indices. The peak view, for example, hides about 7%. While paused, one frame in 64 tests every section. `HORIZON_GPU_CULL=0` turns it off. `all` culls every frame whatever the share, for A/B checks and screenshots. `section-cull on|off` switches it in a running game. `status` reports `renderer.culled_sections`. **Metal: not enabled.** The test needs forced early fragment tests, which naga's MSL backend doesn't emit. wgpu's Metal backend also replays a multi-draw as one call per entry. And the M4 Pro is CPU-bound here: its GPU is busy under 2 ms per frame at RD16. #906's Hi-Z version, forced on Metal, only added CPU. ## Numbers ThinkPad HD 620, Mesa 26.1.8 ANV Vulkan, 1280×720, ticks frozen, perf-frame-6's six views. Each row is one binary in one game: culling off/on/on/off, 6 s each, with `section-cull` (2 rounds at RD16, 1 at RD32). | RD16 view | hidden solid indices | FPS off → on | 1% low off → on | CPU ms | GPU ms | | --- | ---: | --- | --- | --- | --- | | meadow | 68% | 184.6 → 242.8 (+32%) | 84.5 → 99.9 | 2.11 → 1.90 | 5.26 → 3.94 | | forest | 60% | 134.3 → 191.1 (+42%) | 71.0 → 84.1 | 2.56 → 2.21 | 7.27 → 5.07 | | slope | 73% | 192.2 → 241.8 (+26%) | 77.0 → 83.0 | 1.86 → 1.82 | 5.04 → 3.95 | | mountain | 35% | 150.9 → 183.4 (+22%) | 68.9 → 80.5 | 2.33 → 2.14 | 6.46 → 5.29 | | peak | 7% (paused) | 157.8 → 157.3 (−0.3%) | 87.6 → 99.1 | 2.68 → 2.64 | 6.19 → 6.22 | | overview | 41% | 138.4 → 169.2 (+22%) | 88.9 → 100.5 | 2.93 → 2.92 | 7.07 → 5.75 | | RD32 view | hidden solid indices | FPS off → on | 1% low off → on | CPU ms | GPU ms | | --- | ---: | --- | --- | --- | --- | | meadow | 86% | 78.8 → 160.2 (+103%) | 49.3 → 86.3 | 4.75 → 3.10 | 12.42 → 6.00 | | forest | 79% | 53.9 → 112.4 (+109%) | 50.6 → 94.8 | 7.55 → 3.21 | 18.24 → 8.67 | | slope | 88% | 58.7 → 110.1 (+88%) | 51.4 → 73.8 | 6.51 → 3.41 | 16.70 → 8.81 | | mountain | 50% | 51.9 → 75.4 (+45%) | 45.1 → 56.9 | 7.41 → 4.98 | 18.92 → 12.96 | | peak | 28% | 42.8 → 48.9 (+14%) | 38.8 → 37.4 | 9.61 → 8.40 | 22.93 → 20.06 | | overview | 51% | 42.4 → 62.1 (+46%) | 38.7 → 42.3 | 9.42 → 6.53 | 23.15 → 15.71 | For comparison, #906 on the same machine at RD16 went −15% to +17%. Mac mini (M4 Pro, Metal, RD16). Culling is off on Metal, so this is main vs the PR binary, run in the order main, PR, PR, main. The machine was under load 96 from other agents' CI and harnesses, so the same binary varied by up to ±40% between runs (main's meadow: 194 then 330 FPS) and no difference can be read from it. On Metal the PR draws exactly what main draws: the culling object is never created, and the draw list is built and drawn as before. | RD16 view | FPS main → PR | 1% low | CPU ms | GPU ms | | --- | --- | --- | --- | --- | | meadow | 262.1 → 199.6 | 21.4 → 18.3 | 2.75 → 3.59 | 0.43 → 0.56 | | forest | 379.8 → 316.0 | 27.4 → 20.1 | 1.75 → 2.18 | 0.36 → 0.36 | | slope | 254.0 → 239.8 | 17.0 → 18.6 | 3.14 → 3.20 | 0.54 → 0.38 | | mountain | 302.3 → 355.9 | 23.4 → 31.2 | 2.71 → 2.29 | 0.45 → 0.38 | | peak | 267.6 → 173.7 | 23.5 → 15.1 | 2.89 → 3.51 | 0.57 → 0.64 | | overview | 220.8 → 194.2 | 15.9 → 16.1 | 3.61 → 3.52 | 0.50 → 0.64 | Per-run FPS, to show the spread: meadow main 194/330 vs PR 182/217; mountain main 428/177 vs PR 488/223; peak main 353/183 vs PR 188/159. ## Pixels - **Static views (RD16).** Animated textures off, three fresh games per view: off, `HORIZON_GPU_CULL=all`, off. Meadow, forest, slope and mountain differ by 0 px. Peak differs by 1 px, the same pixel as main vs main. Overview differs by 441 px against 426 px main vs main, all on water. Outside the main-vs-main noise, every view differs by 0 px. - **Camera jumps.** 21 frames, each the first frame or so after a jump: 12 yaw jumps of 30° and 9 of 40° at another pitch, with phase 1 coming from a view far away. Culling off vs `all`: 0 px in every frame, so nothing pops. ## How tested ``` cargo fmt --all --check ok cargo clippy --workspace --all-targets -- -D warnings ok gfx::cull::tests::shader_validates (naga) ok cargo test -p horizon-client ok (887 + 75 + 15 + ... passed) tools/parity.py section_mesh translucency light hud 4 passed, 0 failed (Mac mini) ``` CI parity covers the other harnesses. They run on the Mac, where this code path is off (Metal). ## Needs Ludovic Nothing. #906 can be closed once this merges; it's left open for the orchestrator. 🤖 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:00:11 +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#2928
No description provided.