[PR #1198] [MERGED] Section draws grouped by arena page: a third of the multi-draws, encode 25% faster on Intel (RD 32) #3411

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

📋 Pull Request Information

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

Base: main ← Head: perf/frame-encode


📝 Commits (1)

  • 14bd239 Solid section draws grouped by arena page within 64-block bands

📊 Changes

2 files changed (+224 additions, -4 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+43 -4)
➕ crates/horizon-client/src/gfx/page_runs.rs (+181 -0)

📄 Description

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

Part of the frame-cost batch (Kira card #23).

What

Solid section draws are now grouped by vertex arena page within 64-block bands of distance, in phase 1 and phase 2 of occlusion culling and for far terrain tiles. Each band still goes front to back within a page, and bands go front to back.

Why

A profile of the render thread on the ThinkPad (HD 620, Mesa 26.1.8 Vulkan, render distance 32) showed CommandEncoder::finish at 34% of it. wgpu 30 records passes lazily, so finish is where the whole frame is validated and encoded, and the largest part was Mesa's gfx9_CmdDrawIndexedIndirect.

Sections sit in arena pages of 256 MiB. A list sorted only by distance switches page (vertex buffer) every few sections, so every switch costs one indirect multi-draw. At the peak view that was 2220 multi-draws a frame, most of them a few entries long. Mesa's Intel driver unrolls a multi-draw of fewer than 4 entries on the CPU, and above that it runs a draw-generation shader for each call. wgpu-core also validates every call and every vertex buffer change.

With grouping there is one call per page and band, so 2.1 to 3.1× fewer calls.

Before / after

ThinkPad: i5 Kaby Lake-U (2 cores, 4 threads), HD 620, Mesa 26.1.8 Vulkan, 1280×720 offscreen.

Setup:

  • seed 1234 template world, LODs off, ticks frozen, HUD off, uncapped;
  • 10 s still at each view, after the terrain settled;
  • main (e730dc14) and this branch interleaved, 2 runs each, averaged;
  • both builds carry the same local measurement patch, which prints every span every frame.
RD view frame avg ms p99 ms frames > 16.7 ms CPU ms GPU ms encode ms multi-draws
16 meadow 3.63 → 3.67 8.8 → 9.9 0.5 → 0.5 1.83 → 2.02 3.47 → 3.49 0.85 → 0.92 14 → 10
16 overview 5.59 → 5.60 9.2 → 10.1 0.5 → 0 2.58 → 2.57 5.43 → 5.43 1.26 → 1.14 141 → 47
16 peak 6.36 → 6.17 9.8 → 10.3 1 → 1.5 2.98 → 2.60 6.20 → 6.01 1.59 → 1.20 356 → 129
32 meadow 4.64 → 4.67 10.2 → 9.9 1 → 0 2.77 → 2.74 4.45 → 4.49 1.06 → 1.00 35 → 22
32 overview 14.02 → 13.85 17.6 → 16.1 11.5 → 4.5 5.74 → 5.20 13.73 → 13.55 2.43 → 1.75 455 → 146
32 peak 18.65 → 17.45 23.0 → 18.5 530 → 572 7.73 → 6.90 18.30 → 17.13 4.42 → 3.29 2220 → 1062
  • encode (CommandEncoder::finish) drops 25–28% where the list switched pages often (overview and peak).
  • At RD 32 the HD 620 is GPU-bound, and the GPU time also drops a little: fewer draw-generation dispatches.
  • At the peak view every frame is over 16.7 ms on both builds (GPU 17–18 ms), so that count says nothing there.
  • Views with few calls (meadow) don't change beyond noise.

perf record at RD 32, overview view, share of the render thread's samples:

main this branch
CommandEncoder::finish 34.3% 26.4%
of which Mesa gfx9_CmdDrawIndexedIndirect 10.6% 5.3%
the regrouping itself (PageRuns::regroup) 3.7%

Mac mini: M4 Pro, Metal. The machine was shared, with a load average of 20 to 58, so the frame times are noise. RD 32, 3 interleaved runs:

  • encode: overview 0.60 → 0.42 ms, peak 1.32 → 0.78 ms, meadow 0.57 → 0.49 ms.
  • Flattened draw calls: overview 537 → 133, peak 2218 → 1048.

DirectX 12 was not run (no Windows machine). The change is the same draw list for every backend: fewer vertex buffer switches and calls.

Screenshot comparison

Animated textures off, ticks frozen, the 6 template views at RD 16 and RD 32, main / branch / main:

  • Vulkan (HD 620), RD 16: pixel-identical in all views.
  • Metal (M4 Pro), RD 16: pixel-identical except what main also changes between its own runs.
  • Both backends, RD 32: the same except 64 pixels in the meadow view, on leaves.

Those 64 pixels have two states, and main produces both. On the Mac, two main runs differ by exactly those pixels: main against main is 64 px, and the branch matches one of the two main runs except 7 px of the same set. With cave culling off, the branch and main are pixel-identical. The pixels follow which sections cave culling leaves in, not the draw order.

How tested (Mac mini M4 Pro)

On one integration commit with #1199 (Far Detail):

  • cargo fmt --all --check: ok
  • set -o pipefail; cargo clippy --workspace --all-targets -- -D warnings: ok
  • cargo test -p horizon-client: 989 + 91 + 15 + 2 + 2 + 2 + 1 passed, 0 failed. New unit tests in gfx/page_runs.rs.
  • tools/parity.py --changed origin/main: 102 passed, 0 failed.

Data and scripts: ~/code/horizon-parity/shots/perf-frame-11/ (thinkpad/ab-pg.jsonl, thinkpad/q-*-32.folded, mac/mac-pg2.jsonl, scripts/fbench.sh).

What is left

What's left of finish on the HD 620 is mostly animated textures. Every animation frame is one copy_buffer_to_texture per sprite and mip level, and each of those is a full blorp operation in Mesa: 6.5% of the render thread. With the CPU-side mixing in animate_sprites, animated textures are about 20% of the render thread at RD 32 on the ThinkPad.

Label: feature (frame time on Intel iGPUs).

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/1198 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/22/2026 **Status:** ✅ Merged **Merged:** 10/23/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/frame-encode` --- ### 📝 Commits (1) - [`14bd239`](https://github.com/horizon-rust/horizon/commit/14bd239141ffedab749097a6a5f97d6b897a3b69) Solid section draws grouped by arena page within 64-block bands ### 📊 Changes **2 files changed** (+224 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+43 -4) ➕ `crates/horizon-client/src/gfx/page_runs.rs` (+181 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Part of the frame-cost batch (Kira card #23). ## What Solid section draws are now grouped by vertex arena page within 64-block bands of distance, in phase 1 and phase 2 of occlusion culling and for far terrain tiles. Each band still goes front to back within a page, and bands go front to back. ## Why A profile of the render thread on the ThinkPad (HD 620, Mesa 26.1.8 Vulkan, render distance 32) showed `CommandEncoder::finish` at 34% of it. wgpu 30 records passes lazily, so `finish` is where the whole frame is validated and encoded, and the largest part was Mesa's `gfx9_CmdDrawIndexedIndirect`. Sections sit in arena pages of 256 MiB. A list sorted only by distance switches page (vertex buffer) every few sections, so every switch costs one indirect multi-draw. At the peak view that was 2220 multi-draws a frame, most of them a few entries long. Mesa's Intel driver unrolls a multi-draw of fewer than 4 entries on the CPU, and above that it runs a draw-generation shader for each call. wgpu-core also validates every call and every vertex buffer change. With grouping there is one call per page and band, so 2.1 to 3.1× fewer calls. ## Before / after **ThinkPad**: i5 Kaby Lake-U (2 cores, 4 threads), HD 620, Mesa 26.1.8 Vulkan, 1280×720 offscreen. Setup: - seed 1234 template world, LODs off, ticks frozen, HUD off, uncapped; - 10 s still at each view, after the terrain settled; - `main` (e730dc14) and this branch interleaved, 2 runs each, averaged; - both builds carry the same local measurement patch, which prints every span every frame. | RD | view | frame avg ms | p99 ms | frames > 16.7 ms | CPU ms | GPU ms | encode ms | multi-draws | |---|---|---|---|---|---|---|---|---| | 16 | meadow | 3.63 → 3.67 | 8.8 → 9.9 | 0.5 → 0.5 | 1.83 → 2.02 | 3.47 → 3.49 | 0.85 → 0.92 | 14 → 10 | | 16 | overview | 5.59 → 5.60 | 9.2 → 10.1 | 0.5 → 0 | 2.58 → 2.57 | 5.43 → 5.43 | 1.26 → 1.14 | 141 → 47 | | 16 | peak | 6.36 → 6.17 | 9.8 → 10.3 | 1 → 1.5 | 2.98 → 2.60 | 6.20 → 6.01 | 1.59 → **1.20** | 356 → 129 | | 32 | meadow | 4.64 → 4.67 | 10.2 → 9.9 | 1 → 0 | 2.77 → 2.74 | 4.45 → 4.49 | 1.06 → 1.00 | 35 → 22 | | 32 | overview | 14.02 → 13.85 | 17.6 → **16.1** | 11.5 → **4.5** | 5.74 → **5.20** | 13.73 → 13.55 | 2.43 → **1.75** | 455 → 146 | | 32 | peak | 18.65 → **17.45** | 23.0 → **18.5** | 530 → 572 | 7.73 → **6.90** | 18.30 → **17.13** | 4.42 → **3.29** | 2220 → 1062 | - `encode` (`CommandEncoder::finish`) drops 25–28% where the list switched pages often (overview and peak). - At RD 32 the HD 620 is GPU-bound, and the GPU time also drops a little: fewer draw-generation dispatches. - At the peak view every frame is over 16.7 ms on both builds (GPU 17–18 ms), so that count says nothing there. - Views with few calls (meadow) don't change beyond noise. `perf record` at RD 32, overview view, share of the render thread's samples: | | main | this branch | |---|---|---| | `CommandEncoder::finish` | 34.3% | 26.4% | | of which Mesa `gfx9_CmdDrawIndexedIndirect` | 10.6% | 5.3% | | the regrouping itself (`PageRuns::regroup`) | | 3.7% | **Mac mini**: M4 Pro, Metal. The machine was shared, with a load average of 20 to 58, so the frame times are noise. RD 32, 3 interleaved runs: - `encode`: overview 0.60 → 0.42 ms, peak 1.32 → 0.78 ms, meadow 0.57 → 0.49 ms. - Flattened draw calls: overview 537 → 133, peak 2218 → 1048. DirectX 12 was not run (no Windows machine). The change is the same draw list for every backend: fewer vertex buffer switches and calls. ## Screenshot comparison Animated textures off, ticks frozen, the 6 template views at RD 16 and RD 32, `main` / branch / `main`: - **Vulkan (HD 620), RD 16**: pixel-identical in all views. - **Metal (M4 Pro), RD 16**: pixel-identical except what `main` also changes between its own runs. - **Both backends, RD 32**: the same except 64 pixels in the meadow view, on leaves. Those 64 pixels have two states, and `main` produces both. On the Mac, two `main` runs differ by exactly those pixels: `main` against `main` is 64 px, and the branch matches one of the two `main` runs except 7 px of the same set. With cave culling off, the branch and `main` are pixel-identical. The pixels follow which sections cave culling leaves in, not the draw order. ## How tested (Mac mini M4 Pro) On one integration commit with #1199 (Far Detail): - `cargo fmt --all --check`: ok - `set -o pipefail; cargo clippy --workspace --all-targets -- -D warnings`: ok - `cargo test -p horizon-client`: 989 + 91 + 15 + 2 + 2 + 2 + 1 passed, 0 failed. New unit tests in `gfx/page_runs.rs`. - `tools/parity.py --changed origin/main`: **102 passed, 0 failed**. Data and scripts: `~/code/horizon-parity/shots/perf-frame-11/` (`thinkpad/ab-pg.jsonl`, `thinkpad/q-*-32.folded`, `mac/mac-pg2.jsonl`, `scripts/fbench.sh`). ## What is left What's left of `finish` on the HD 620 is mostly animated textures. Every animation frame is one `copy_buffer_to_texture` per sprite and mip level, and each of those is a full blorp operation in Mesa: 6.5% of the render thread. With the CPU-side mixing in `animate_sprites`, animated textures are about 20% of the render thread at RD 32 on the ThinkPad. Label: `feature` (frame time on Intel iGPUs). 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:09:20 +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#3411
No description provided.