[PR #1434] [MERGED] Metal: GPU occlusion culling (M4 Pro at RD 32: +8% to +48% FPS) #3520

Closed
opened 2026-11-03 04:10:08 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: perf/metal-cull-flat


📝 Commits (3)

  • 9fcf91e Metal: occlusion culling of solid sections, the depth test in the shader
  • 14be6ca Metal: occlusion culling with flat draws too
  • 660407f Metal: cull only frames with 4,096 section draws or more

📊 Changes

5 files changed (+821 additions, -230 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+115 -57)
📝 crates/horizon-client/src/gfx/cull.rs (+187 -30)
📝 crates/horizon-client/src/gfx/cull.wgsl (+20 -8)
📝 crates/horizon-client/src/gfx/flat.rs (+455 -132)
📝 crates/horizon-client/src/gfx/flatten.wgsl (+44 -3)

📄 Description

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

What

GPU occlusion culling of solid sections now runs on Metal. It was Vulkan only (FEATURES 4zd, brief perf-translucent-1). On an M4 Pro at render distance 32 and 1080p, average FPS goes from 233 to 345 on the mountain (+48%), 546 to 625 in the village (+14%), 332 to 366 on the ocean (+10%) and 296 to 319 in the forest (+8%). Nothing changes at render distance 12, or on Vulkan.

Three commits:

  1. Depth test in the shader (gfx/cull.rs, cull.wgsl). The Vulkan test relies on early fragment tests. naga's Metal backend doesn't emit [[early_fragment_tests]], so on Metal a box fragment would record "seen" even when depth rejects it. On Metal the boxes are now drawn in a pass of their own, between the two phases. Phase 1's depth is bound read-only, both as the attachment and as a texture_depth_2d, and the fragment shader makes the comparison itself: in.position.z >= textureLoad(depth, ...), reversed Z, the same GreaterEqual as the fixed-function test. Cull::supported returns DepthTest::Early (Vulkan) or DepthTest::InShader (Metal). HORIZON_GPU_CULL=shader takes the in-shader path on Vulkan as well, for A/B checks.
  2. Flat draws with culling (gfx/flat.rs, flatten.wgsl). Apple GPUs draw high render distances "flat" (one index list per frame, from 4,096 draws), and flat frames had no culling. Now phase 1's sections and the translucent ones are flattened before the world pass, as before. Phase 2's sections are flattened after the test by a new flatten_seen entry, which copies only the sections the test saw: each page run reserves room for all its draws and packs the seen ones at its start. Each run is then drawn with draw_indexed_indirect, using the count the copy reached. plan now splits page runs at the phase 1/phase 2 boundary.
  3. Metal culls only frames with 4,096 section draws or more (cull::TILED_MIN_DRAWS). A tile-based GPU stores and reloads the whole frame at each pass the test adds, a fixed cost. At render distance 12 that cost was larger than the saving: +0.05 ms of GPU per frame while hiding 15–30% of the solid indices (village 1672 → 1554 FPS, ocean 1231 → 1176 in an in-game A/B). The gate goes by the last frame's draw count. Vulkan keeps culling every frame, and HORIZON_GPU_CULL=all culls every frame on Metal too.

Why

Perf is priority 1. #1397's timer showed the M4 at render distance 32 GPU-bound on the mountain: 4.46 ms of GPU in a 4.51 ms frame. With culling the GPU p50 there drops from 4.21 to 2.68 ms.

How tested

Performance: tools/perf_bench.py, M4 Pro, Metal, --offscreen --sizes 1920x1080 --repeats 1, main (e325fa65) and this branch back to back, interleaved

The Mac mini was shared with other sessions the whole time: load 31–62, CPU 38–94% busy. Games where another session's GPU work showed (GPU p50 doubled in both builds) are left out. Avg / 1% low FPS, with the GPU p50 in ms:

RD 32 main (2 games) this PR (clean game) earlier build of the same code, 2 games
village 546 / 329, 557 / 376 (GPU 1.75, 1.71) 625 / 361 (GPU 1.48) 626 / 353, 621 / 337
forest 296 / 222, 295 / 222 (GPU 3.33, 3.36) 319 / 236 (GPU 2.91) 325 / 253, 325 / 258
ocean 332 / 255, 332 / 271 (GPU 2.93, 2.95) 366 / 291 (GPU 2.47) 366 / 289, 367 / 280
mountain 233 / 185, 229 / 157 (GPU 4.21, 4.22) 345 / 234 (GPU 2.68) 345 / 233, 345 / 240

The last column is commit 2 without the gate. At RD 32 every frame has more than 4,096 draws, so it runs the same code. The same run's per-section culling (HORIZON_FLAT_DRAWS=0) reached 589 / 304 / 350 / 317: slower than flat draws with culling, so flat stays the default.

RD 12 (1,000–1,600 draws, below the gate, so no culling): the undisturbed games give GPU p50 0.566 / 0.576 ms on main vs 0.579 ms on this branch in the village, and 0.785 / 0.796 vs 0.758 / 0.804 on the ocean. Equal within noise. On this shared machine FPS at RD 12 varied more than that (best village game: 1726 on main, 1639 on this branch, with CPU p50 0.31 vs 0.35 ms from the other sessions).

Same results as Vulkan

  • Same sections hidden, Vulkan. RTX 4080, RD 12, 1920x1080, 8 views, day and night (culled_sections: hidden boxes / share of solid indices hidden). The in-shader test (HORIZON_GPU_CULL=shader) hides exactly what the early test hides in all 16 cases: 189 / 0.1964, 214 / 0.2395, 132 / 0.1483, 87 / 0.0535, 145 / 0.1702, 136 / 0.1899, 225 / 0.2248, 202 / 0.2284.
  • Metal vs Vulkan. On the M4 at the same size and views: 189, 214, 132, 86, 144, 136, 224, 202 boxes. That is 5 views identical and 3 one box apart (forest1, ocean0, mountain0; different GPUs rasterize box edges differently). Flat draws with culling hide exactly the per-section counts on both backends.
  • Pixels. Per view: screenshots with culling off, on, off, on in one game. Mobs killed, ticks frozen, HUD hidden, no texture animation; a "consistent difference" is a pixel where both offs agree, both ons agree and off ≠ on. Metal per-section: 0 in all 8 views. On both backends a 1-LSB difference sometimes shows on the village houses of one view (village0, about 1,290 px). main's own Vulkan culling does the same (2 of 3 runs of e325fa65 on the RTX 4080), so this PR doesn't introduce it (FEATURES LATER).
  • flat::tests::plan_gives_phase_2_calls_of_its_own checks the new layout. The GPU test flatten_writes_the_indices_of_the_per_section_draws now also runs phase 2 with only the last draw seen. cull::tests::shaders_validate validates both shader variants, the in-shader one without EARLY_DEPTH_TEST.

Self-check

  • cargo fmt --all -- --check: ok (660407fa)
  • cargo clippy --workspace --all-targets -- -D warnings: ok (desktop, 660407fa)
  • cargo test --release -p horizon-client --lib gfx::: 80 passed, 5 ignored (desktop, Vulkan, 660407fa); the same on the M4 (Metal), 80 passed, 5 ignored
  • tools/parity.py --changed origin/main: 104 passed, 0 failed in 34.7 min (desktop, 14be6ca3). The last commit only adds the Metal gate, and on Vulkan its threshold is 0.

Notes

  • #1290 (Java packs' culling, stacked on #1277/#1267) edits cull.rs and cull.wgsl too, so whichever merges second needs a rebase. Its test_gl pass relies on a fixed-function depth test with a fragment side effect. Once Metal has a Cull (this PR), that pass would mark every box seen on Metal: no wrong picture, but no culling either, until it uses the in-shader test there.
  • Branch perf/metal-occlusion-cull holds commit 1 alone (9fcf91e4) from before the three commits were joined into one PR. It's contained in this branch and can be deleted once this merges.

Needs Ludovic

Nothing.

🤖 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/1434 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/23/2026 **Status:** ✅ Merged **Merged:** 10/23/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/metal-cull-flat` --- ### 📝 Commits (3) - [`9fcf91e`](https://github.com/horizon-rust/horizon/commit/9fcf91e4bce67dce335874e97fa8eb3fbc05dfec) Metal: occlusion culling of solid sections, the depth test in the shader - [`14be6ca`](https://github.com/horizon-rust/horizon/commit/14be6ca3943944c6a80ff9f747375e6c363fe7b5) Metal: occlusion culling with flat draws too - [`660407f`](https://github.com/horizon-rust/horizon/commit/660407fa58d03fa6e41fe2edc7d4ae3658841233) Metal: cull only frames with 4,096 section draws or more ### 📊 Changes **5 files changed** (+821 additions, -230 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+115 -57) 📝 `crates/horizon-client/src/gfx/cull.rs` (+187 -30) 📝 `crates/horizon-client/src/gfx/cull.wgsl` (+20 -8) 📝 `crates/horizon-client/src/gfx/flat.rs` (+455 -132) 📝 `crates/horizon-client/src/gfx/flatten.wgsl` (+44 -3) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What GPU occlusion culling of solid sections now runs on Metal. It was Vulkan only (FEATURES 4zd, brief perf-translucent-1). On an M4 Pro at render distance 32 and 1080p, average FPS goes from 233 to 345 on the mountain (+48%), 546 to 625 in the village (+14%), 332 to 366 on the ocean (+10%) and 296 to 319 in the forest (+8%). Nothing changes at render distance 12, or on Vulkan. Three commits: 1. **Depth test in the shader** (`gfx/cull.rs`, `cull.wgsl`). The Vulkan test relies on early fragment tests. naga's Metal backend doesn't emit `[[early_fragment_tests]]`, so on Metal a box fragment would record "seen" even when depth rejects it. On Metal the boxes are now drawn in a pass of their own, between the two phases. Phase 1's depth is bound read-only, both as the attachment and as a `texture_depth_2d`, and the fragment shader makes the comparison itself: `in.position.z >= textureLoad(depth, ...)`, reversed Z, the same `GreaterEqual` as the fixed-function test. `Cull::supported` returns `DepthTest::Early` (Vulkan) or `DepthTest::InShader` (Metal). `HORIZON_GPU_CULL=shader` takes the in-shader path on Vulkan as well, for A/B checks. 2. **Flat draws with culling** (`gfx/flat.rs`, `flatten.wgsl`). Apple GPUs draw high render distances "flat" (one index list per frame, from 4,096 draws), and flat frames had no culling. Now phase 1's sections and the translucent ones are flattened before the world pass, as before. Phase 2's sections are flattened after the test by a new `flatten_seen` entry, which copies only the sections the test saw: each page run reserves room for all its draws and packs the seen ones at its start. Each run is then drawn with `draw_indexed_indirect`, using the count the copy reached. `plan` now splits page runs at the phase 1/phase 2 boundary. 3. **Metal culls only frames with 4,096 section draws or more** (`cull::TILED_MIN_DRAWS`). A tile-based GPU stores and reloads the whole frame at each pass the test adds, a fixed cost. At render distance 12 that cost was larger than the saving: +0.05 ms of GPU per frame while hiding 15–30% of the solid indices (village 1672 → 1554 FPS, ocean 1231 → 1176 in an in-game A/B). The gate goes by the last frame's draw count. Vulkan keeps culling every frame, and `HORIZON_GPU_CULL=all` culls every frame on Metal too. ## Why Perf is priority 1. #1397's timer showed the M4 at render distance 32 GPU-bound on the mountain: 4.46 ms of GPU in a 4.51 ms frame. With culling the GPU p50 there drops from 4.21 to 2.68 ms. ## How tested ### Performance: `tools/perf_bench.py`, M4 Pro, Metal, `--offscreen --sizes 1920x1080 --repeats 1`, main (e325fa65) and this branch back to back, interleaved The Mac mini was shared with other sessions the whole time: load 31–62, CPU 38–94% busy. Games where another session's GPU work showed (GPU p50 doubled in both builds) are left out. Avg / 1% low FPS, with the GPU p50 in ms: | RD 32 | main (2 games) | this PR (clean game) | earlier build of the same code, 2 games | | --- | --- | --- | --- | | village | 546 / 329, 557 / 376 (GPU 1.75, 1.71) | **625** / 361 (GPU 1.48) | 626 / 353, 621 / 337 | | forest | 296 / 222, 295 / 222 (GPU 3.33, 3.36) | **319** / 236 (GPU 2.91) | 325 / 253, 325 / 258 | | ocean | 332 / 255, 332 / 271 (GPU 2.93, 2.95) | **366** / 291 (GPU 2.47) | 366 / 289, 367 / 280 | | mountain | 233 / 185, 229 / 157 (GPU 4.21, 4.22) | **345** / 234 (GPU 2.68) | 345 / 233, 345 / 240 | The last column is commit 2 without the gate. At RD 32 every frame has more than 4,096 draws, so it runs the same code. The same run's per-section culling (`HORIZON_FLAT_DRAWS=0`) reached 589 / 304 / 350 / 317: slower than flat draws with culling, so flat stays the default. RD 12 (1,000–1,600 draws, below the gate, so no culling): the undisturbed games give GPU p50 0.566 / 0.576 ms on main vs 0.579 ms on this branch in the village, and 0.785 / 0.796 vs 0.758 / 0.804 on the ocean. Equal within noise. On this shared machine FPS at RD 12 varied more than that (best village game: 1726 on main, 1639 on this branch, with CPU p50 0.31 vs 0.35 ms from the other sessions). ### Same results as Vulkan - **Same sections hidden, Vulkan.** RTX 4080, RD 12, 1920x1080, 8 views, day and night (`culled_sections`: hidden boxes / share of solid indices hidden). The in-shader test (`HORIZON_GPU_CULL=shader`) hides exactly what the early test hides in all 16 cases: 189 / 0.1964, 214 / 0.2395, 132 / 0.1483, 87 / 0.0535, 145 / 0.1702, 136 / 0.1899, 225 / 0.2248, 202 / 0.2284. - **Metal vs Vulkan.** On the M4 at the same size and views: 189, 214, 132, 86, 144, 136, 224, 202 boxes. That is 5 views identical and 3 one box apart (forest1, ocean0, mountain0; different GPUs rasterize box edges differently). Flat draws with culling hide exactly the per-section counts on both backends. - **Pixels.** Per view: screenshots with culling off, on, off, on in one game. Mobs killed, ticks frozen, HUD hidden, no texture animation; a "consistent difference" is a pixel where both offs agree, both ons agree and off ≠ on. Metal per-section: 0 in all 8 views. On both backends a 1-LSB difference sometimes shows on the village houses of one view (village0, about 1,290 px). **main's own Vulkan culling does the same** (2 of 3 runs of e325fa65 on the RTX 4080), so this PR doesn't introduce it (FEATURES LATER). - `flat::tests::plan_gives_phase_2_calls_of_its_own` checks the new layout. The GPU test `flatten_writes_the_indices_of_the_per_section_draws` now also runs phase 2 with only the last draw seen. `cull::tests::shaders_validate` validates both shader variants, the in-shader one without `EARLY_DEPTH_TEST`. ### Self-check - `cargo fmt --all -- --check`: ok (660407fa) - `cargo clippy --workspace --all-targets -- -D warnings`: ok (desktop, 660407fa) - `cargo test --release -p horizon-client --lib gfx::`: 80 passed, 5 ignored (desktop, Vulkan, 660407fa); the same on the M4 (Metal), 80 passed, 5 ignored - `tools/parity.py --changed origin/main`: 104 passed, 0 failed in 34.7 min (desktop, 14be6ca3). The last commit only adds the Metal gate, and on Vulkan its threshold is 0. ## Notes - #1290 (Java packs' culling, stacked on #1277/#1267) edits `cull.rs` and `cull.wgsl` too, so whichever merges second needs a rebase. Its `test_gl` pass relies on a fixed-function depth test with a fragment side effect. Once Metal has a `Cull` (this PR), that pass would mark every box seen on Metal: no wrong picture, but no culling either, until it uses the in-shader test there. - Branch `perf/metal-occlusion-cull` holds commit 1 alone (9fcf91e4) from before the three commits were joined into one PR. It's contained in this branch and can be deleted once this merges. ## Needs Ludovic Nothing. 🤖 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-03 04:10:08 +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#3520
No description provided.