[PR #743] [MERGED] Metal: draw terrain sections as one index list per page run #2774

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

📋 Pull Request Information

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

Base: main ← Head: perf/terrain-draws


📝 Commits (2)

  • fc6db37 GPU frame time on Metal, render spans and section draw counts
  • 51b58f3 Metal: draw terrain sections as one index list per page run

📊 Changes

8 files changed (+982 additions, -52 deletions)

View changed files

📝 crates/horizon-client/src/game/control.rs (+12 -0)
📝 crates/horizon-client/src/game/render_control.rs (+1 -0)
📝 crates/horizon-client/src/gfx.rs (+187 -29)
📝 crates/horizon-client/src/gfx/arena.rs (+24 -8)
➕ crates/horizon-client/src/gfx/flat.rs (+658 -0)
➕ crates/horizon-client/src/gfx/flatten.wgsl (+35 -0)
📝 crates/horizon-client/src/gfx/timing.rs (+47 -14)
📝 crates/horizon-client/src/terrain.wgsl (+18 -1)

📄 Description

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

Stacked on #742 (GPU timer on Macs and the draw counters); review that one first.

The cost

At render distance 32 the render thread encoded about 9,300 terrain draws per frame on the Mac (one per visible face range of each solid section and one per translucent section). draw_indexed_indirect with a count is a multi-draw only where the backend has one:

Backend (wgpu 30) Multi-draw indirect Used by Horizon
Vulkan native (vkCmdDrawIndexedIndirect with a count) Linux, Windows
Metal emulated: wgpu-hal loops drawIndexedPrimitives:indirectBuffer: once per entry macOS
DX12 native (ExecuteIndirect) not used (Windows runs Vulkan)

A 10 s profile at RD 32 on the Mac mini: CommandEncoder::finish took about half of Renderer::render, most of it in Metal's per-entry indirect draw encoding.

The change

Where multi-draw is emulated (Metal) and a frame has at least 4,096 section draws, a compute pass (flatten.wgsl) copies the frame's section draws into one index list, in draw order (solid front to back, then translucent back to front with their sorted indices), and each run of sections in the same arena page becomes one draw call. At RD 32: 9,309 draws become 708 calls in the reference view (330–3,256 over seven views).

The picture is unchanged by construction: the same triangles from the same vertices, in the same order, through the same shader code. Only where a vertex finds its section's origin changes, from the per-draw instance attribute to a table: a section's vertices fill whole 256-vertex blocks of its arena page, each block names the section by a stable id, and the section's camera-relative origin is computed on the CPU as before (vs_terrain_flat shares terrain_vertex with vs_terrain).

  • Vulkan keeps the native multi-draw; nothing changes there.
  • Fallback: adapters without vertex-stage storage buffers or compute, or a frame whose list would exceed the storage binding limit, keep the per-section indirect draws.
  • Below 4,096 draws the per-section path stays: the list costs GPU time (about +0.4 ms at RD 16 on the M4 Pro) that the CPU saving there does not repay.
  • HORIZON_FLAT_DRAWS=1 draws every frame flat on any backend that supports it, 0 never; horizon-ctl section-draws flat|indirect switches in a running game, for A/B checks.

Picture

Same game, same frame state (tick frozen, spectator, animated textures off), RD 32, one screenshot with per-section draws and one with the index list, at seven views: 6 of 7 have 0 differing pixels; the seventh differs only inside the fading chat box (the chat faded between the two screenshots).

View Section draws → draw calls Pixels differing
0 9,309 → 708 0
1 3,550 → 330 chat box only
2 4,792 → 342 0
3 6,155 → 431 0
4 18,960 → 3,256 0
5 1,634 → 218 0
6 5,654 → 492 0

Performance

Mac mini M4 Pro (Metal), offscreen 1280×720, the bench save, spectator. Paired A/B inside one game (the section-draws switch alternating per-section and flat runs), because the machine was heavily loaded by other sessions (load average 46–120 on 14 threads): every pair shares the same load, the sign count says how often the flat list won. Turning 360° per run.

RD 32, 12 pairs of 5 s:

per-section draws index list list better in
avg FPS 128 162
1% low FPS 9.8 11.0
0.1% low FPS 6.86 6.85
render phase median (ms) 4.83 3.45 9/12
CPU ms/frame 6.76 5.73 9/12
CPU p99 (ms) 70.2 63.3 7/12
frame p99 (ms) 80.8 69.4 8/12
GPU ms/frame 1.24 1.24 6/12

RD 32, 5 pairs of 8 s:

per-section draws index list
avg FPS 72.4 102.3
1% low FPS 6.87 8.43
0.1% low FPS 5.04 6.38
CPU ms/frame 13.3 8.45
render phase (ms) 8.58 5.50
GPU ms/frame 1.26 1.73

Why the threshold, RD 16, 12 pairs of 5 s with the list forced on: render 2.44 → 1.93 ms (10/12), CPU 3.41 → 2.81 ms (9/12), but GPU 0.58 → 0.96 ms (the list was slower on GPU in 12/12). RD 12: 1,465 draws → 2 calls, render 2.28 → 2.08 ms, GPU 0.42 → 0.63 ms. RD 16 and below have fewer than 4,096 draws per frame and keep the per-section draws, so they are unchanged by this PR.

The 0.1% lows are set by world ticks and load spikes on this machine, not by the draws; noise hides any effect there.

Checks

  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test -p horizon-client --lib: pass, including two new tests: the draw-run planner, and a GPU test that the compute pass writes exactly the per-section draws' indices (runs on Metal; skipped without an adapter).
  • Local parity (tools/parity.py --changed origin/main): 88 passed, 0 failed.
  • Not run on Vulkan hardware: the default there is unchanged (native multi-draw); HORIZON_FLAT_DRAWS=1 on Vulkan is untested.

🤖 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/743 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/17/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/terrain-draws` --- ### 📝 Commits (2) - [`fc6db37`](https://github.com/horizon-rust/horizon/commit/fc6db37b6a70da4d3a5ad341d8a9ae9dda790422) GPU frame time on Metal, render spans and section draw counts - [`51b58f3`](https://github.com/horizon-rust/horizon/commit/51b58f38979fe8626b508498ab9f729068590c22) Metal: draw terrain sections as one index list per page run ### 📊 Changes **8 files changed** (+982 additions, -52 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/control.rs` (+12 -0) 📝 `crates/horizon-client/src/game/render_control.rs` (+1 -0) 📝 `crates/horizon-client/src/gfx.rs` (+187 -29) 📝 `crates/horizon-client/src/gfx/arena.rs` (+24 -8) ➕ `crates/horizon-client/src/gfx/flat.rs` (+658 -0) ➕ `crates/horizon-client/src/gfx/flatten.wgsl` (+35 -0) 📝 `crates/horizon-client/src/gfx/timing.rs` (+47 -14) 📝 `crates/horizon-client/src/terrain.wgsl` (+18 -1) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Stacked on #742 (GPU timer on Macs and the draw counters); review that one first. ## The cost At render distance 32 the render thread encoded about 9,300 terrain draws per frame on the Mac (one per visible face range of each solid section and one per translucent section). `draw_indexed_indirect` with a count is a multi-draw only where the backend has one: | Backend (wgpu 30) | Multi-draw indirect | Used by Horizon | | --- | --- | --- | | Vulkan | native (`vkCmdDrawIndexedIndirect` with a count) | Linux, Windows | | Metal | emulated: wgpu-hal loops `drawIndexedPrimitives:indirectBuffer:` once per entry | macOS | | DX12 | native (`ExecuteIndirect`) | not used (Windows runs Vulkan) | A 10 s profile at RD 32 on the Mac mini: `CommandEncoder::finish` took about half of `Renderer::render`, most of it in Metal's per-entry indirect draw encoding. ## The change Where multi-draw is emulated (Metal) and a frame has at least 4,096 section draws, a compute pass (`flatten.wgsl`) copies the frame's section draws into one index list, in draw order (solid front to back, then translucent back to front with their sorted indices), and each run of sections in the same arena page becomes one draw call. At RD 32: 9,309 draws become 708 calls in the reference view (330–3,256 over seven views). The picture is unchanged by construction: the same triangles from the same vertices, in the same order, through the same shader code. Only where a vertex finds its section's origin changes, from the per-draw instance attribute to a table: a section's vertices fill whole 256-vertex blocks of its arena page, each block names the section by a stable id, and the section's camera-relative origin is computed on the CPU as before (`vs_terrain_flat` shares `terrain_vertex` with `vs_terrain`). - Vulkan keeps the native multi-draw; nothing changes there. - Fallback: adapters without vertex-stage storage buffers or compute, or a frame whose list would exceed the storage binding limit, keep the per-section indirect draws. - Below 4,096 draws the per-section path stays: the list costs GPU time (about +0.4 ms at RD 16 on the M4 Pro) that the CPU saving there does not repay. - `HORIZON_FLAT_DRAWS=1` draws every frame flat on any backend that supports it, `0` never; `horizon-ctl section-draws flat|indirect` switches in a running game, for A/B checks. ## Picture Same game, same frame state (tick frozen, spectator, animated textures off), RD 32, one screenshot with per-section draws and one with the index list, at seven views: 6 of 7 have 0 differing pixels; the seventh differs only inside the fading chat box (the chat faded between the two screenshots). | View | Section draws → draw calls | Pixels differing | | --- | --- | --- | | 0 | 9,309 → 708 | 0 | | 1 | 3,550 → 330 | chat box only | | 2 | 4,792 → 342 | 0 | | 3 | 6,155 → 431 | 0 | | 4 | 18,960 → 3,256 | 0 | | 5 | 1,634 → 218 | 0 | | 6 | 5,654 → 492 | 0 | ## Performance Mac mini M4 Pro (Metal), offscreen 1280×720, the bench save, spectator. Paired A/B inside one game (the `section-draws` switch alternating per-section and flat runs), because the machine was heavily loaded by other sessions (load average 46–120 on 14 threads): every pair shares the same load, the sign count says how often the flat list won. Turning 360° per run. RD 32, 12 pairs of 5 s: | | per-section draws | index list | list better in | | --- | --- | --- | --- | | avg FPS | 128 | 162 | | | 1% low FPS | 9.8 | 11.0 | | | 0.1% low FPS | 6.86 | 6.85 | | | render phase median (ms) | 4.83 | 3.45 | 9/12 | | CPU ms/frame | 6.76 | 5.73 | 9/12 | | CPU p99 (ms) | 70.2 | 63.3 | 7/12 | | frame p99 (ms) | 80.8 | 69.4 | 8/12 | | GPU ms/frame | 1.24 | 1.24 | 6/12 | RD 32, 5 pairs of 8 s: | | per-section draws | index list | | --- | --- | --- | | avg FPS | 72.4 | 102.3 | | 1% low FPS | 6.87 | 8.43 | | 0.1% low FPS | 5.04 | 6.38 | | CPU ms/frame | 13.3 | 8.45 | | render phase (ms) | 8.58 | 5.50 | | GPU ms/frame | 1.26 | 1.73 | Why the threshold, RD 16, 12 pairs of 5 s with the list forced on: render 2.44 → 1.93 ms (10/12), CPU 3.41 → 2.81 ms (9/12), but GPU 0.58 → 0.96 ms (the list was slower on GPU in 12/12). RD 12: 1,465 draws → 2 calls, render 2.28 → 2.08 ms, GPU 0.42 → 0.63 ms. RD 16 and below have fewer than 4,096 draws per frame and keep the per-section draws, so they are unchanged by this PR. The 0.1% lows are set by world ticks and load spikes on this machine, not by the draws; noise hides any effect there. ## Checks - `cargo fmt --all --check`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test -p horizon-client --lib`: pass, including two new tests: the draw-run planner, and a GPU test that the compute pass writes exactly the per-section draws' indices (runs on Metal; skipped without an adapter). - Local parity (`tools/parity.py --changed origin/main`): 88 passed, 0 failed. - Not run on Vulkan hardware: the default there is unchanged (native multi-draw); `HORIZON_FLAT_DRAWS=1` on Vulkan is untested. 🤖 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:58:56 +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#2774
No description provided.