[GH-ISSUE #987] [performance] Octree for section frustum culling #273

Closed
opened 2026-11-02 15:37:48 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @ludovic111 on GitHub (Oct 19, 2026).
Original GitHub issue: https://github.com/horizon-rust/horizon/issues/987

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

Requested on Discord by famescare in #general (2026-10-19).

Today each terrain section is tested against the view frustum one by one, before cave culling and (Vulkan) GPU occlusion culling (#939). Vanilla's client keeps an octree of sections (SectionOcclusionGraph / Octree in recent versions) so whole blocks of sections can be accepted or rejected at once. Measure the frustum test's share of the render thread at render distance 32 first; if it's significant, build the octree with identical visible sets (screenshot comparison and the terrain harnesses) and before/after frame times.

Originally created by @ludovic111 on GitHub (Oct 19, 2026). Original GitHub issue: https://github.com/horizon-rust/horizon/issues/987 > 🤖 Filed by Ludovic's AI agent (Claude), not by Ludovic himself. Requested on Discord by **famescare** in #general (2026-10-19). Today each terrain section is tested against the view frustum one by one, before cave culling and (Vulkan) GPU occlusion culling (#939). Vanilla's client keeps an octree of sections (`SectionOcclusionGraph` / `Octree` in recent versions) so whole blocks of sections can be accepted or rejected at once. Measure the frustum test's share of the render thread at render distance 32 first; if it's significant, build the octree with identical visible sets (screenshot comparison and the terrain harnesses) and before/after frame times.
bsakaguchi 2026-11-02 15:37:48 +07:00
Author
Owner

@ludovic111 commented on GitHub (Oct 19, 2026):

🤖 Agent: measurement for this issue (perf-frame-10), before building anything.

The frustum walk (Resident::visible, span visibility) already tests columns first, then their sections. Per-frame cost with LODs off, seed 1234, HUD off, 8 s benches:

machine RD view visibility ms/frame draw_list ms/frame encode ms/frame CPU ms/frame GPU ms/frame
Mac mini M4 Pro 16 overview / meadow / peak 0.002 / 0.001 / 0.001 0.003 / 0.001 / 0.001 0.27 / 0.35 / 0.30 1.08 / 0.97 / 0.98 0.23 / 0.14 / 0.23
Mac mini M4 Pro 32 overview / meadow / peak 0.20 / 0.05 / 0.06 0.46 / 0.12 / 0.27 1.03 / 0.34 / 1.54 3.05 / 1.94 / 3.47 0.65 / 0.33 / 0.85
ThinkPad i5 Kaby Lake-U, HD 620 16 overview / meadow 0.008 / 0.004 0.014 / 0.003 1.78 / 1.11 3.42 / 2.31 5.87 / 3.89
ThinkPad i5 Kaby Lake-U, HD 620 32 overview / meadow 1.42 / 1.00 1.06 / 0.64 5.11 / 2.70 10.59 / 6.37 16.91 / 6.30

(draw_list is a temporary span around the instance list and indirect-draw build, see #997.)

  • At RD 16 the walk is negligible everywhere. At RD 32 it is 0.05–0.2 ms on the M4 Pro and 1.0–1.4 ms on the 4-thread Kaby Lake. There an octree could matter only where the frame is CPU-bound (meadow: CPU 6.4 ms vs GPU 6.3 ms). At the overview the HD 620 is GPU-bound (16.9 ms).
  • The jump from RD 16 to RD 32 is far more than the ~4× more sections (0.008 → 1.42 ms on the ThinkPad). That is worth understanding before choosing a structure: the walk itself has nothing superlinear in it (column test, section test, one sort).
  • encode (CommandEncoder::finish) is 2–5× the walk's cost on both machines and is the bigger CPU item.

Data and scripts: ~/code/horizon-parity/shots/perf-frame-10/ (mac/t3.jsonl, data/thinkpad/tp-t3.jsonl, scripts/t3bench.sh, scripts/t3.py). Nothing implemented: this needs a decision on whether the RD 32 weak-CPU case is worth the octree.

<!-- gh-comment-id:6268072568 --> @ludovic111 commented on GitHub (Oct 19, 2026): 🤖 Agent: measurement for this issue (perf-frame-10), before building anything. The frustum walk (`Resident::visible`, span `visibility`) already tests columns first, then their sections. Per-frame cost with LODs off, seed 1234, HUD off, 8 s benches: | machine | RD | view | `visibility` ms/frame | `draw_list` ms/frame | `encode` ms/frame | CPU ms/frame | GPU ms/frame | | --- | --- | --- | --- | --- | --- | --- | --- | | Mac mini M4 Pro | 16 | overview / meadow / peak | 0.002 / 0.001 / 0.001 | 0.003 / 0.001 / 0.001 | 0.27 / 0.35 / 0.30 | 1.08 / 0.97 / 0.98 | 0.23 / 0.14 / 0.23 | | Mac mini M4 Pro | 32 | overview / meadow / peak | 0.20 / 0.05 / 0.06 | 0.46 / 0.12 / 0.27 | 1.03 / 0.34 / 1.54 | 3.05 / 1.94 / 3.47 | 0.65 / 0.33 / 0.85 | | ThinkPad i5 Kaby Lake-U, HD 620 | 16 | overview / meadow | 0.008 / 0.004 | 0.014 / 0.003 | 1.78 / 1.11 | 3.42 / 2.31 | 5.87 / 3.89 | | ThinkPad i5 Kaby Lake-U, HD 620 | 32 | overview / meadow | 1.42 / 1.00 | 1.06 / 0.64 | 5.11 / 2.70 | 10.59 / 6.37 | 16.91 / 6.30 | (`draw_list` is a temporary span around the instance list and indirect-draw build, see #997.) - At RD 16 the walk is negligible everywhere. At RD 32 it is 0.05–0.2 ms on the M4 Pro and 1.0–1.4 ms on the 4-thread Kaby Lake. There an octree could matter only where the frame is CPU-bound (meadow: CPU 6.4 ms vs GPU 6.3 ms). At the overview the HD 620 is GPU-bound (16.9 ms). - The jump from RD 16 to RD 32 is far more than the ~4× more sections (0.008 → 1.42 ms on the ThinkPad). That is worth understanding before choosing a structure: the walk itself has nothing superlinear in it (column test, section test, one sort). - `encode` (`CommandEncoder::finish`) is 2–5× the walk's cost on both machines and is the bigger CPU item. Data and scripts: `~/code/horizon-parity/shots/perf-frame-10/` (`mac/t3.jsonl`, `data/thinkpad/tp-t3.jsonl`, `scripts/t3bench.sh`, `scripts/t3.py`). Nothing implemented: this needs a decision on whether the RD 32 weak-CPU case is worth the octree.
Author
Owner

@ludovic111 commented on GitHub (Oct 21, 2026):

🤖 Agent: closing this with measurements: an octree for the section frustum test doesn't pay today. A measured "not worth it" close, per the triage rule (batch P3, 2026-10-21).

The most an octree could save is the whole visibility span (the per-frame frustum walk in Resident::visible, which already tests columns before their sections). Seed 1234, LODs off, HUD off, ticks frozen, 8 s benches:

machine RD visibility ms/frame CPU ms/frame GPU ms/frame
Mac mini M4 Pro (perf-frame-10, 10-19) 16 ≤ 0.002 0.97–1.08 0.14–0.23
Mac mini M4 Pro (perf-frame-10, 10-19) 32 0.05–0.20 1.94–3.47 0.33–0.85
ThinkPad HD 620 (perf-frame-10, 10-19) 32 1.00–1.42 6.37–10.59 6.30–16.91
ThinkPad HD 620 (today, main 02bd3cbc) 32 0.60–0.87 3.97–10.03 6.05–20.36
  • On the M4 Pro the whole walk is at most 0.2 ms of a frame that already runs above the 260 FPS cap at RD 32.
  • On the HD 620, where the walk costs up to about 1 ms, the GPU takes longer than the CPU in every view (6–20 ms against 4–10 ms). A faster CPU walk doesn't change the frame time there.
  • The walk shrinks further with vanilla's cave-culling rules (#782, #1094), which leave fewer sections to test. At RD 32 on the HD 620 the meadow view goes from 0.60 to 0.25 ms. At the overview it's 0.87 ms on main and 0.77 ms with the PR.
  • encode is the larger CPU item on both machines, 2–5 ms on the HD 620 at RD 32. That's being worked on in the frame-cost batch.

If a profile ever shows the walk on a CPU-bound frame, the cheap step is a coarse grid of 4×4 columns in front of the column walk, not an octree. That's noted in the LATER list.

<!-- gh-comment-id:6321021509 --> @ludovic111 commented on GitHub (Oct 21, 2026): 🤖 Agent: closing this with measurements: an octree for the section frustum test doesn't pay today. A measured "not worth it" close, per the triage rule (batch P3, 2026-10-21). **The most an octree could save is the whole `visibility` span** (the per-frame frustum walk in `Resident::visible`, which already tests columns before their sections). Seed 1234, LODs off, HUD off, ticks frozen, 8 s benches: | machine | RD | `visibility` ms/frame | CPU ms/frame | GPU ms/frame | | --- | --- | --- | --- | --- | | Mac mini M4 Pro (perf-frame-10, 10-19) | 16 | ≤ 0.002 | 0.97–1.08 | 0.14–0.23 | | Mac mini M4 Pro (perf-frame-10, 10-19) | 32 | 0.05–0.20 | 1.94–3.47 | 0.33–0.85 | | ThinkPad HD 620 (perf-frame-10, 10-19) | 32 | 1.00–1.42 | 6.37–10.59 | 6.30–16.91 | | ThinkPad HD 620 (today, main 02bd3cbc) | 32 | 0.60–0.87 | 3.97–10.03 | 6.05–20.36 | - On the M4 Pro the whole walk is at most 0.2 ms of a frame that already runs above the 260 FPS cap at RD 32. - On the HD 620, where the walk costs up to about 1 ms, the GPU takes longer than the CPU in every view (6–20 ms against 4–10 ms). A faster CPU walk doesn't change the frame time there. - The walk shrinks further with vanilla's cave-culling rules (#782, #1094), which leave fewer sections to test. At RD 32 on the HD 620 the meadow view goes from 0.60 to 0.25 ms. At the overview it's 0.87 ms on main and 0.77 ms with the PR. - `encode` is the larger CPU item on both machines, 2–5 ms on the HD 620 at RD 32. That's being worked on in the frame-cost batch. If a profile ever shows the walk on a CPU-bound frame, the cheap step is a coarse grid of 4×4 columns in front of the column walk, not an octree. That's noted in the LATER list.
Author
Owner

@ludovic111 commented on GitHub (Oct 22, 2026):

🤖 Agent: the RD 16 → RD 32 "jump" in the frustum walk (0.008 → 1.4 ms on the ThinkPad, from perf-frame-10) is a measurement artefact, not a scaling problem.

HORIZON_SPIKES prints only spans of at least 0.5 ms (game.rs, .filter(|(_, ms)| *ms >= 0.5)). At RD 16 the walk is below that, so it was almost never printed and its average came out near zero. At RD 32 it is above it.

Measured on every frame (a local patch prints all spans, the same in both builds). ThinkPad HD 620, main e730dc14, seed 1234, LODs off, 2 runs × 10 s:

visibility ms/frame meadow overview peak
RD 16 0.16 0.23 0.23
RD 32 0.48 0.86 0.65

That is 2.8–3.7× for about 3.4× the resident sections (7.1 k → 24 k): linear. A perf profile (10-19 build) agrees: the walk is 10% of the render thread at RD 16 (≈0.28 ms) and 14.5% at RD 32 (≈0.93 ms). About a third of it is the final sort_unstable_by_key((distance, key)); the rest is the per-section plane tests.

There is no cache, allocation or algorithm problem to fix. I also tried a packed u128 sort key, which was no faster (5 k sections, M4: 110 µs either way). The octree stays not worth it. The cheap step if it is ever needed is still the one noted above: a coarse column grid, or skipping the section test for columns fully inside the frustum.

Data: ~/code/horizon-parity/shots/perf-frame-11/thinkpad/ab-pg.jsonl (main rows), p-base-*.folded.

<!-- gh-comment-id:6339545556 --> @ludovic111 commented on GitHub (Oct 22, 2026): 🤖 Agent: the RD 16 → RD 32 "jump" in the frustum walk (0.008 → 1.4 ms on the ThinkPad, from perf-frame-10) is a measurement artefact, not a scaling problem. `HORIZON_SPIKES` prints only spans of at least 0.5 ms (`game.rs`, `.filter(|(_, ms)| *ms >= 0.5)`). At RD 16 the walk is below that, so it was almost never printed and its average came out near zero. At RD 32 it is above it. Measured on every frame (a local patch prints all spans, the same in both builds). ThinkPad HD 620, main e730dc14, seed 1234, LODs off, 2 runs × 10 s: | `visibility` ms/frame | meadow | overview | peak | |---|---|---|---| | RD 16 | 0.16 | 0.23 | 0.23 | | RD 32 | 0.48 | 0.86 | 0.65 | That is 2.8–3.7× for about 3.4× the resident sections (7.1 k → 24 k): linear. A `perf` profile (10-19 build) agrees: the walk is 10% of the render thread at RD 16 (≈0.28 ms) and 14.5% at RD 32 (≈0.93 ms). About a third of it is the final `sort_unstable_by_key((distance, key))`; the rest is the per-section plane tests. There is no cache, allocation or algorithm problem to fix. I also tried a packed `u128` sort key, which was no faster (5 k sections, M4: 110 µs either way). The octree stays not worth it. The cheap step if it is ever needed is still the one noted above: a coarse column grid, or skipping the section test for columns fully inside the frustum. Data: `~/code/horizon-parity/shots/perf-frame-11/thinkpad/ab-pg.jsonl` (main rows), `p-base-*.folded`.
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#273
No description provided.