[PR #503] [MERGED] Skip section face groups that face away from the eye #2605

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

📋 Pull Request Information

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

Base: main ← Head: perf/section-face-culling


📝 Commits (1)

  • f7d042c Skip section face groups that face away from the eye

📊 Changes

4 files changed (+305 additions, -17 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+21 -16)
📝 crates/horizon-client/src/mesher.rs (+11 -0)
➕ crates/horizon-client/src/mesher/facing.rs (+272 -0)
📝 crates/horizon-client/src/mesher/leaf_culling_tests.rs (+1 -1)

📄 Description

What

Each section's solid quads are now grouped by the axis side they face (-X, -Z, -Y, not axis-aligned, +Y, +Z, +X), as Sodium's ModelQuadFacing does, and the renderer leaves out of the section's draws every group whose quads all face away from the eye.

Why

At render distance 32 the world pass is vertex-bound on the M4 Pro: in a Metal System Trace, the "Horizon world" encoder spent about 2 ms per frame in the vertex stage against 0.5-0.65 ms of fragment work. Terrain culls back faces, so every back-facing quad was shaded and then discarded. A quad in the plane x = p facing +X is front-facing only while the eye is at x > p; when the eye is behind the nearest plane of a whole group, the GPU would discard all of it.

How

  • mesher/facing.rs: group() classifies each solid quad by its winding normal (an axis group only when all four corners lie exactly in one axis plane; rotated elements and sloped fluid tops stay unaligned and always draw), stable-sorts the quads by group and stores each group's quad count and its plane nearest the facing side. Mesher::mesh = tesselate (model order, which the leaf-culling test checks) + group.
  • Renderer::render: for each visible section, FaceGroups::ranges(eye) yields the visible groups as runs (adjacent visible groups merged), one indirect draw each. The eye is the inverse of bob * view applied to the origin, so view bobbing is taken into account. A group is skipped only when the eye is more than 1/16 block behind its nearest plane.
  • The order is stable within a group, so coplanar quads (a grass block's side overlay over its base) still draw in the same order. Translucent geometry is unchanged.

Measurements

Mac mini M4 Pro (Metal), 1280x720 offscreen, seed 1234 creative world, standing at the village path's end (-960 125 272, pitch 35), noon.

Solid quads submitted per frame, counted in the renderer, culling off vs on (deterministic):

RD Sections drawn Solid quads before after Draws before after
8 125 56,212 39,862 (-29%) 125 232
16 568 300,062 204,119 (-32%) 568 1,076
32 2,681 1,448,654 931,393 (-36%) 2,681 5,448

Frame time: the same game process switching between the old and new draw lists every 5 s (a local-only toggle, not in this PR), 5 interleaved rounds. The machine was shared with other agents' builds and harnesses (load 25-65), so averages swing; the medians are the steadier figure:

RD Median frame ms, before (5 rounds) Median frame ms, after (5 rounds)
16 0.97, 1.01, 1.39, 1.21, 1.11 (mean 1.14) 0.72, 1.14, 1.38, 0.82, 1.04 (mean 1.02)
32 3.57, 2.42, 2.97, 2.40, 3.31 (mean 2.93) 2.26, 2.26, 2.39, 2.37, 2.39 (mean 2.33)

On Metal wgpu emulates multi-draw-indirect as one draw per entry, so the extra draws cost some CPU; on Vulkan and DX12 they are one native multi-draw call. GPU ms per frame are not reported on Apple GPUs yet (the frame timer needs timestamps inside encoders).

Correctness

  • Screenshots in one frozen game, GUI hidden, culling on/off/on/off, five views (mountain, village, forest pond, river, village street): the mountain view and a view looking up from the valley are pixel-identical; in the others the only differing pixels are animated water and a moving mob, which also differ between two shots of the same mode.
  • Unit tests: winding-to-group classification for all six sides and diagonal quads; stable grouping (a coplanar overlay keeps its order); ranges skipping only back-facing groups, with the margin.
cargo fmt --all --check                                   ok
cargo clippy --workspace --all-targets -- -D warnings      ok
cargo test -p horizon-client --lib                        751 passed, 5 ignored
tools/parity.py --changed origin/main (84 harnesses)       84 passed, 0 failed

Needs from 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/503 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/section-face-culling` --- ### 📝 Commits (1) - [`f7d042c`](https://github.com/horizon-rust/horizon/commit/f7d042c6d0540a7b367694081ba3a3623e1d1304) Skip section face groups that face away from the eye ### 📊 Changes **4 files changed** (+305 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+21 -16) 📝 `crates/horizon-client/src/mesher.rs` (+11 -0) ➕ `crates/horizon-client/src/mesher/facing.rs` (+272 -0) 📝 `crates/horizon-client/src/mesher/leaf_culling_tests.rs` (+1 -1) </details> ### 📄 Description ## What Each section's solid quads are now grouped by the axis side they face (-X, -Z, -Y, not axis-aligned, +Y, +Z, +X), as Sodium's `ModelQuadFacing` does, and the renderer leaves out of the section's draws every group whose quads all face away from the eye. ## Why At render distance 32 the world pass is vertex-bound on the M4 Pro: in a Metal System Trace, the "Horizon world" encoder spent about 2 ms per frame in the vertex stage against 0.5-0.65 ms of fragment work. Terrain culls back faces, so every back-facing quad was shaded and then discarded. A quad in the plane `x = p` facing +X is front-facing only while the eye is at `x > p`; when the eye is behind the nearest plane of a whole group, the GPU would discard all of it. ## How - `mesher/facing.rs`: `group()` classifies each solid quad by its winding normal (an axis group only when all four corners lie exactly in one axis plane; rotated elements and sloped fluid tops stay unaligned and always draw), stable-sorts the quads by group and stores each group's quad count and its plane nearest the facing side. `Mesher::mesh` = `tesselate` (model order, which the leaf-culling test checks) + `group`. - `Renderer::render`: for each visible section, `FaceGroups::ranges(eye)` yields the visible groups as runs (adjacent visible groups merged), one indirect draw each. The eye is the inverse of `bob * view` applied to the origin, so view bobbing is taken into account. A group is skipped only when the eye is more than 1/16 block behind its nearest plane. - The order is stable within a group, so coplanar quads (a grass block's side overlay over its base) still draw in the same order. Translucent geometry is unchanged. ## Measurements Mac mini M4 Pro (Metal), 1280x720 offscreen, seed 1234 creative world, standing at the village path's end (-960 125 272, pitch 35), noon. Solid quads submitted per frame, counted in the renderer, culling off vs on (deterministic): | RD | Sections drawn | Solid quads before | after | Draws before | after | | --- | --- | --- | --- | --- | --- | | 8 | 125 | 56,212 | 39,862 (-29%) | 125 | 232 | | 16 | 568 | 300,062 | 204,119 (-32%) | 568 | 1,076 | | 32 | 2,681 | 1,448,654 | 931,393 (-36%) | 2,681 | 5,448 | Frame time: the same game process switching between the old and new draw lists every 5 s (a local-only toggle, not in this PR), 5 interleaved rounds. The machine was shared with other agents' builds and harnesses (load 25-65), so averages swing; the medians are the steadier figure: | RD | Median frame ms, before (5 rounds) | Median frame ms, after (5 rounds) | | --- | --- | --- | | 16 | 0.97, 1.01, 1.39, 1.21, 1.11 (mean 1.14) | 0.72, 1.14, 1.38, 0.82, 1.04 (mean 1.02) | | 32 | 3.57, 2.42, 2.97, 2.40, 3.31 (mean 2.93) | 2.26, 2.26, 2.39, 2.37, 2.39 (mean 2.33) | On Metal wgpu emulates multi-draw-indirect as one draw per entry, so the extra draws cost some CPU; on Vulkan and DX12 they are one native multi-draw call. GPU ms per frame are not reported on Apple GPUs yet (the frame timer needs timestamps inside encoders). ## Correctness - Screenshots in one frozen game, GUI hidden, culling on/off/on/off, five views (mountain, village, forest pond, river, village street): the mountain view and a view looking up from the valley are pixel-identical; in the others the only differing pixels are animated water and a moving mob, which also differ between two shots of the same mode. - Unit tests: winding-to-group classification for all six sides and diagonal quads; stable grouping (a coplanar overlay keeps its order); `ranges` skipping only back-facing groups, with the margin. ``` cargo fmt --all --check ok cargo clippy --workspace --all-targets -- -D warnings ok cargo test -p horizon-client --lib 751 passed, 5 ignored tools/parity.py --changed origin/main (84 harnesses) 84 passed, 0 failed ``` Needs from 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-02 15:57:33 +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#2605
No description provided.