[PR #906] [CLOSED] Terrain: GPU occlusion culling of solid sections (draft, mixed on HD 620) #2902

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/906
Author: @ludovic111
Created: 10/18/2026
Status: ❌ Closed

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


📝 Commits (1)

  • b20a3c2 Terrain: occlusion culling of solid sections on the GPU (Vulkan)

📊 Changes

6 files changed (+718 additions, -11 deletions)

View changed files

📝 crates/horizon-client/src/game/control.rs (+10 -0)
📝 crates/horizon-client/src/game/render_control.rs (+1 -0)
📝 crates/horizon-client/src/gfx.rs (+124 -11)
➕ crates/horizon-client/src/gfx/cull.rs (+418 -0)
➕ crates/horizon-client/src/gfx/cull.wgsl (+117 -0)
➕ crates/horizon-client/src/gfx/hiz.wgsl (+48 -0)

📄 Description

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

Draft: not ready to merge. The pixels are exact and the GPU work it removes is real, but on the HD 620 it adds a fixed cost of about 0.7 ms GPU and about 1 ms CPU per frame, so net FPS is mixed. It is stacked on #905. It is up so the measurement, the design and the A/B switch aren't lost.

What

Two-phase occlusion culling of solid section draws on the GPU (Vulkan, where multi-draw is native):

  1. A compute pass copies the frame's solid indirect draws into two lists. Phase 1 keeps the sections that were in sight last frame (per resident slot); phase 2 starts empty.
  2. The world pass draws phase 1 and stores depth.
  3. A compute pass builds a Hi-Z pyramid (farthest depth per texel, odd edges folded into the last texel) and tests each solid draw's section box, grown by one block, against it. Phase 2 gets the boxes in sight that phase 1 didn't draw; the result becomes the next frame's phase 1.
  4. A second world pass (loading colour and depth) draws phase 2, then everything the world pass drew after solid terrain, unchanged.

A section is skipped only when its box is behind phase 1's depth everywhere it covers, so every fragment would fail the depth test against the final depth. Translucent draws, Metal and the flat path are unchanged. HORIZON_GPU_CULL=0/1 overrides the default; section-cull on|off switches it in a running game.

Why: what share is hidden

A local measurement build (not in this PR) rendered each frame's solid sections into a scratch depth buffer, read it back and tested each drawn section's grown box. Seed 1234, 1280×720, ticks frozen (the Mac and the ThinkPad agree to the vertex on the view both measured):

view RD16 sections hidden RD16 solid vertices hidden RD32 vertices hidden
meadow 68% 57% 84%
forest 69% 53% 77%
slope 69% 69% 86%
mountain 45% 31% 49%
peak 9% 3.5% 23%
overview 45% 36% 47%

Frustum culling already leaves nothing off-screen at section level (0 sections in every view).

Numbers (ThinkPad HD 620, Mesa ANV Vulkan, RD16, 1280×720, Xvfb)

One game per run. Each view settles once, then 2 rounds of culling off/on/on/off, 8 s each, toggled with section-cull:

view FPS off → on 1% low off → on GPU ms off → on CPU ms off → on
meadow 167.4 → 173.4 41.9 → 45.7 5.00 → 4.33 4.39 → 5.10
forest 130.0 → 152.3 59.2 → 59.3 7.48 → 6.12 3.25 → 4.22
slope 183.2 → 181.3 64.8 → 52.9 5.10 → 4.89 3.16 → 3.99
mountain 123.3 → 125.2 29.2 → 29.9 7.13 → 6.47 5.24 → 6.31
peak 122.3 → 104.1 29.9 → 27.4 6.65 → 7.41 6.60 → 8.59
overview 113.6 → 112.1 31.0 → 31.4 7.53 → 6.91 6.81 → 8.20

The peak view (3.5% hidden) shows the overhead: +0.76 ms GPU. Where half the vertices are hidden the GPU saves 0.6–1.4 ms, less than the vertex share suggests. Suspects for the overhead, not yet measured separately: sampling a depth buffer on Gen9 (ANV resolves HiZ for it), the render→compute→render switches, and storing and reloading the targets. The CPU side adds two bind groups and three passes per frame.

Screenshots

Static captures (animated textures off), RD16, flat draws off, three fresh games: culling off, on, off.

view Metal off vs on px Metal off vs off px Vulkan off vs on px Vulkan off vs off px outside noise (Metal / Vulkan)
meadow 0 0 0 0 0 / 0
forest 78 100 93 76 0 / 0
slope 0 0 0 0 0 / 0
mountain 0 0 0 0 0 / 0
peak 0 0 0 0 0 / 0
overview 62 42 93 93 1 (delta 1) / 0

Next steps to make it a win

  • Time the pieces on the HD 620 (Hi-Z build, the depth resolve, the pass split) with timestamps per pass.
  • Build the Hi-Z from a smaller depth copy, or test against last frame's pyramid for phase 1 and only re-test the remainder.
  • Turn it on per view only when the last frame's culled share pays for it, or only at larger render distances (RD32 hides 47–86% of vertices).

How tested

cargo fmt --all --check                                         ok
cargo clippy --workspace --all-targets -- -D warnings           ok
gfx::cull::tests::shaders_validate (naga)                       ok
parity harnesses                                                not run (draft)

Needs Ludovic

Nothing yet. It stays a draft until it wins everywhere.

🤖 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/906 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/18/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/section-occlusion-cull` --- ### 📝 Commits (1) - [`b20a3c2`](https://github.com/horizon-rust/horizon/commit/b20a3c2111ae370c7047b8022707bb30dcb9c43c) Terrain: occlusion culling of solid sections on the GPU (Vulkan) ### 📊 Changes **6 files changed** (+718 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/control.rs` (+10 -0) 📝 `crates/horizon-client/src/game/render_control.rs` (+1 -0) 📝 `crates/horizon-client/src/gfx.rs` (+124 -11) ➕ `crates/horizon-client/src/gfx/cull.rs` (+418 -0) ➕ `crates/horizon-client/src/gfx/cull.wgsl` (+117 -0) ➕ `crates/horizon-client/src/gfx/hiz.wgsl` (+48 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. **Draft: not ready to merge.** The pixels are exact and the GPU work it removes is real, but on the HD 620 it adds a fixed cost of about 0.7 ms GPU and about 1 ms CPU per frame, so net FPS is mixed. It is stacked on #905. It is up so the measurement, the design and the A/B switch aren't lost. ## What Two-phase occlusion culling of solid section draws on the GPU (Vulkan, where multi-draw is native): 1. A compute pass copies the frame's solid indirect draws into two lists. Phase 1 keeps the sections that were in sight last frame (per resident slot); phase 2 starts empty. 2. The world pass draws phase 1 and stores depth. 3. A compute pass builds a Hi-Z pyramid (farthest depth per texel, odd edges folded into the last texel) and tests each solid draw's section box, grown by one block, against it. Phase 2 gets the boxes in sight that phase 1 didn't draw; the result becomes the next frame's phase 1. 4. A second world pass (loading colour and depth) draws phase 2, then everything the world pass drew after solid terrain, unchanged. A section is skipped only when its box is behind phase 1's depth everywhere it covers, so every fragment would fail the depth test against the final depth. Translucent draws, Metal and the flat path are unchanged. `HORIZON_GPU_CULL=0/1` overrides the default; `section-cull on|off` switches it in a running game. ## Why: what share is hidden A local measurement build (not in this PR) rendered each frame's solid sections into a scratch depth buffer, read it back and tested each drawn section's grown box. Seed 1234, 1280×720, ticks frozen (the Mac and the ThinkPad agree to the vertex on the view both measured): | view | RD16 sections hidden | RD16 solid vertices hidden | RD32 vertices hidden | | --- | ---: | ---: | ---: | | meadow | 68% | 57% | 84% | | forest | 69% | 53% | 77% | | slope | 69% | 69% | 86% | | mountain | 45% | 31% | 49% | | peak | 9% | 3.5% | 23% | | overview | 45% | 36% | 47% | Frustum culling already leaves nothing off-screen at section level (0 sections in every view). ## Numbers (ThinkPad HD 620, Mesa ANV Vulkan, RD16, 1280×720, Xvfb) One game per run. Each view settles once, then 2 rounds of culling off/on/on/off, 8 s each, toggled with `section-cull`: | view | FPS off → on | 1% low off → on | GPU ms off → on | CPU ms off → on | | --- | --- | --- | --- | --- | | meadow | 167.4 → 173.4 | 41.9 → 45.7 | 5.00 → 4.33 | 4.39 → 5.10 | | forest | 130.0 → 152.3 | 59.2 → 59.3 | 7.48 → 6.12 | 3.25 → 4.22 | | slope | 183.2 → 181.3 | 64.8 → 52.9 | 5.10 → 4.89 | 3.16 → 3.99 | | mountain | 123.3 → 125.2 | 29.2 → 29.9 | 7.13 → 6.47 | 5.24 → 6.31 | | peak | 122.3 → 104.1 | 29.9 → 27.4 | 6.65 → 7.41 | 6.60 → 8.59 | | overview | 113.6 → 112.1 | 31.0 → 31.4 | 7.53 → 6.91 | 6.81 → 8.20 | The peak view (3.5% hidden) shows the overhead: +0.76 ms GPU. Where half the vertices are hidden the GPU saves 0.6–1.4 ms, less than the vertex share suggests. Suspects for the overhead, not yet measured separately: sampling a depth buffer on Gen9 (ANV resolves HiZ for it), the render→compute→render switches, and storing and reloading the targets. The CPU side adds two bind groups and three passes per frame. ## Screenshots Static captures (animated textures off), RD16, flat draws off, three fresh games: culling off, on, off. | view | Metal off vs on px | Metal off vs off px | Vulkan off vs on px | Vulkan off vs off px | outside noise (Metal / Vulkan) | | --- | ---: | ---: | ---: | ---: | ---: | | meadow | 0 | 0 | 0 | 0 | 0 / 0 | | forest | 78 | 100 | 93 | 76 | 0 / 0 | | slope | 0 | 0 | 0 | 0 | 0 / 0 | | mountain | 0 | 0 | 0 | 0 | 0 / 0 | | peak | 0 | 0 | 0 | 0 | 0 / 0 | | overview | 62 | 42 | 93 | 93 | 1 (delta 1) / 0 | ## Next steps to make it a win - Time the pieces on the HD 620 (Hi-Z build, the depth resolve, the pass split) with timestamps per pass. - Build the Hi-Z from a smaller depth copy, or test against last frame's pyramid for phase 1 and only re-test the remainder. - Turn it on per view only when the last frame's culled share pays for it, or only at larger render distances (RD32 hides 47–86% of vertices). ## How tested ``` cargo fmt --all --check ok cargo clippy --workspace --all-targets -- -D warnings ok gfx::cull::tests::shaders_validate (naga) ok parity harnesses not run (draft) ``` ## Needs Ludovic Nothing yet. It stays a draft until it wins everywhere. 🤖 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 15:59:58 +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#2902
No description provided.