[PR #865] [MERGED] Terrain vertices in 24 bytes instead of 28 (UV as 16-bit fixed point) #2869

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

📋 Pull Request Information

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

Base: main ← Head: perf/compact-terrain-vertex


📝 Commits (1)

  • df58a47 Terrain vertices in 24 bytes: the UV as 16-bit fixed point

📊 Changes

6 files changed (+71 additions, -41 deletions)

View changed files

📝 crates/horizon-client/examples/section_mesh_fixture.rs (+2 -9)
📝 crates/horizon-client/src/gfx.rs (+2 -2)
📝 crates/horizon-client/src/mesher.rs (+56 -12)
📝 crates/horizon-client/src/mesher/facing.rs (+1 -7)
📝 crates/horizon-client/src/mesher/fluid.rs (+1 -7)
📝 crates/horizon-client/src/terrain.wgsl (+9 -4)

📄 Description

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

What

TerrainVertex (every section vertex the GPU stores) goes from 28 to 24 bytes. The UV becomes two u16 in 32768 steps per unit instead of two f32. Model UVs are multiples of 1/16 (or their halves), so they are stored exactly and terrain.wgsl decodes them exactly (an integer below 2^24 times a power of two). Flowing fluid UVs (rotated by the flow angle) round to 1/32768 of a sprite. Position, layer, light and colour don't change.

Why, and what it buys

The ThinkPad (Intel HD 620) spends about 3.9 of its 5.2 ms of GPU per frame on solid terrain (perf-frame-4). The brief asked for a packed terrain vertex: the smallest layout that keeps the pixels exactly the same. I measured three layouts in the same world, ABBA-paired as main, 24, 20, 20, 24, main:

  • 24 bytes (this PR): float position, fixed-point UV.
  • 20 bytes (not in this PR): the position as three 21-bit fixed-point values (1/65536 block).
  • main: 28 bytes.

ThinkPad: HD 620, Mesa ANV Vulkan, RD 16, 1280×720 offscreen, uncapped, ticks frozen, 12 s per view, 2 runs per build. Cells are main / 24 B / 20 B.

view FPS GPU ms/frame
meadow 190 / 189 / 197 5.12 / 5.15 / 4.93
forest 171 / 171 / 176 5.67 / 5.68 / 5.51
slope 192 / 193 / 200 5.03 / 5.02 / 4.85
mountain 191 / 192 / 198 5.09 / 5.06 / 4.92
peak 186 / 186 / 196 5.23 / 5.22 / 4.95
overview 141 / 141 / 151 6.90 / 6.87 / 6.46
mean 178.7 / 178.5 / 186.3 5.51 / 5.50 / 5.27

Section vertex memory at RD 16: 380 → 325 MB (−14%) with 24 bytes and 271 MB (−29%) with 20 bytes. The vertex arena allocation drops 503 → 369 MB (24 B) or 289 MB (20 B). On an iGPU that is system RAM.

What this shows about the iGPU: vertex size is a small part of the terrain cost. 24 bytes gives the same GPU time as 28 (the 24-byte layout is faster than main in 7 of 12 pairs). 20 bytes is 4% faster in all 12 pairs, which puts vertex fetch at roughly 1 ms of the 3.9. The rest is per-vertex work: vertex shader invocations, the 9 interpolated outputs, setup. Fewer vertices (GPU culling) is the bigger lever; it is not started here, as the brief asked. CPU per frame was 2.31 / 2.28 / 2.04 ms.

Mac mini (M4 Pro, Metal), same method: no regression. GPU ms/frame mean was 0.62 / 0.54 / 0.51, but other sessions were loading the machine, so FPS there is noise (115–320).

This PR is the 24-byte layout: memory −14%, iGPU frame time unchanged, pixels unchanged.

Needs Ludovic

The 20-byte layout gives another +4% FPS on the HD 620 and saves another 15% of memory. It is not pixel-exact: in each view, 3 to 27 pixels on the edges of offset plants (grass, flowers) differ, because fixed point moves those vertices by at most 1/131072 block. The rules here keep the picture identical by default, so it is not in this PR. It is ready on request.

Picture

Each view is a fresh game on a copy of the same saved world (seed 1234, no mobs, RD 16). Ticks are frozen and HUD, vignette and clouds are off. Captures go main, PR, main; the second main capture is the noise floor. Metal uses this PR's final commit. The Vulkan captures (ThinkPad) were taken with the same 24-byte encoding but an earlier field order. Changed pixels:

view Metal main vs PR Metal main vs main Vulkan main vs PR Vulkan main vs main
meadow (river) 12,151 15,892 9,794 11,610
forest 15 21 21 21
slope 936 941 736 835
mountain 14,358 39,337 28,374 14,622
peak (sea) 178,859 251,830 255,342 241,600
overview (sea) 130,970 144,734 164,126 181,986

Every pixel that differs between main and the PR is on water, which animates between captures even with ticks frozen. That includes water seen through leaves, where pixels differ by 1. The masks (red = differs) show this. In the forest view, the PR's differing pixels (15 on Metal, 21 on Vulkan) are all among the pixels that also differ main vs main. Drawn section counts are identical in every view. Captures, masks and scripts are in ~/code/horizon-parity/shots/perf-frame-5/ on the Mac mini (metal-rd16-final, vulkan-rd16, README.txt).

How tested (self-check)

On one integration commit of the three PRs (perf/compact-terrain-vertex, perf/light-dirty-sections, perf/mesh-queue-columns) on origin/main e508a5ce, Mac mini:

cargo fmt --all --check                                        ok
cargo clippy --workspace --all-targets -- -D warnings          ok, no warnings
cargo test -p horizon-client --lib                             870 passed, 0 failed, 11 ignored
cargo build -p horizon-client --bin horizon-client             ok
tools/parity.py --changed origin/main                          95 of 95 passed, 0 failed (155.9 min)

The harnesses covering the changed code passed: section_mesh, translucency and light. tooltip_display and mob_ai, which fail on main, also passed here. origin/main has moved since then (#859, #860); neither touches these files.

🤖 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/865 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/18/2026 **Status:** ✅ Merged **Merged:** 10/18/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/compact-terrain-vertex` --- ### 📝 Commits (1) - [`df58a47`](https://github.com/horizon-rust/horizon/commit/df58a474fbd654e7d74b7c4298e1278bbbe611dd) Terrain vertices in 24 bytes: the UV as 16-bit fixed point ### 📊 Changes **6 files changed** (+71 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/examples/section_mesh_fixture.rs` (+2 -9) 📝 `crates/horizon-client/src/gfx.rs` (+2 -2) 📝 `crates/horizon-client/src/mesher.rs` (+56 -12) 📝 `crates/horizon-client/src/mesher/facing.rs` (+1 -7) 📝 `crates/horizon-client/src/mesher/fluid.rs` (+1 -7) 📝 `crates/horizon-client/src/terrain.wgsl` (+9 -4) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What `TerrainVertex` (every section vertex the GPU stores) goes from 28 to 24 bytes. The UV becomes two `u16` in 32768 steps per unit instead of two `f32`. Model UVs are multiples of 1/16 (or their halves), so they are stored exactly and `terrain.wgsl` decodes them exactly (an integer below 2^24 times a power of two). Flowing fluid UVs (rotated by the flow angle) round to 1/32768 of a sprite. Position, layer, light and colour don't change. ## Why, and what it buys The ThinkPad (Intel HD 620) spends about 3.9 of its 5.2 ms of GPU per frame on solid terrain (perf-frame-4). The brief asked for a packed terrain vertex: the smallest layout that keeps the pixels exactly the same. I measured three layouts in the same world, ABBA-paired as main, 24, 20, 20, 24, main: - **24 bytes (this PR):** float position, fixed-point UV. - **20 bytes (not in this PR):** the position as three 21-bit fixed-point values (1/65536 block). - **main:** 28 bytes. ThinkPad: HD 620, Mesa ANV Vulkan, RD 16, 1280×720 offscreen, uncapped, ticks frozen, 12 s per view, 2 runs per build. Cells are main / 24 B / 20 B. | view | FPS | GPU ms/frame | | --- | --- | --- | | meadow | 190 / 189 / 197 | 5.12 / 5.15 / 4.93 | | forest | 171 / 171 / 176 | 5.67 / 5.68 / 5.51 | | slope | 192 / 193 / 200 | 5.03 / 5.02 / 4.85 | | mountain | 191 / 192 / 198 | 5.09 / 5.06 / 4.92 | | peak | 186 / 186 / 196 | 5.23 / 5.22 / 4.95 | | overview | 141 / 141 / 151 | 6.90 / 6.87 / 6.46 | | **mean** | **178.7 / 178.5 / 186.3** | **5.51 / 5.50 / 5.27** | Section vertex memory at RD 16: **380 → 325 MB (−14%)** with 24 bytes and 271 MB (−29%) with 20 bytes. The vertex arena allocation drops 503 → 369 MB (24 B) or 289 MB (20 B). On an iGPU that is system RAM. What this shows about the iGPU: vertex size is a small part of the terrain cost. 24 bytes gives the same GPU time as 28 (the 24-byte layout is faster than main in 7 of 12 pairs). 20 bytes is 4% faster in all 12 pairs, which puts vertex fetch at roughly 1 ms of the 3.9. The rest is per-vertex work: vertex shader invocations, the 9 interpolated outputs, setup. Fewer vertices (GPU culling) is the bigger lever; it is not started here, as the brief asked. CPU per frame was 2.31 / 2.28 / 2.04 ms. Mac mini (M4 Pro, Metal), same method: no regression. GPU ms/frame mean was 0.62 / 0.54 / 0.51, but other sessions were loading the machine, so FPS there is noise (115–320). **This PR is the 24-byte layout:** memory −14%, iGPU frame time unchanged, pixels unchanged. ## Needs Ludovic The 20-byte layout gives another +4% FPS on the HD 620 and saves another 15% of memory. It is not pixel-exact: in each view, 3 to 27 pixels on the edges of offset plants (grass, flowers) differ, because fixed point moves those vertices by at most 1/131072 block. The rules here keep the picture identical by default, so it is not in this PR. It is ready on request. ## Picture Each view is a fresh game on a copy of the same saved world (seed 1234, no mobs, RD 16). Ticks are frozen and HUD, vignette and clouds are off. Captures go main, PR, main; the second main capture is the noise floor. Metal uses this PR's final commit. The Vulkan captures (ThinkPad) were taken with the same 24-byte encoding but an earlier field order. Changed pixels: | view | Metal main vs PR | Metal main vs main | Vulkan main vs PR | Vulkan main vs main | | --- | --- | --- | --- | --- | | meadow (river) | 12,151 | 15,892 | 9,794 | 11,610 | | forest | 15 | 21 | 21 | 21 | | slope | 936 | 941 | 736 | 835 | | mountain | 14,358 | 39,337 | 28,374 | 14,622 | | peak (sea) | 178,859 | 251,830 | 255,342 | 241,600 | | overview (sea) | 130,970 | 144,734 | 164,126 | 181,986 | Every pixel that differs between main and the PR is on water, which animates between captures even with ticks frozen. That includes water seen through leaves, where pixels differ by 1. The masks (red = differs) show this. In the forest view, the PR's differing pixels (15 on Metal, 21 on Vulkan) are all among the pixels that also differ main vs main. Drawn section counts are identical in every view. Captures, masks and scripts are in `~/code/horizon-parity/shots/perf-frame-5/` on the Mac mini (`metal-rd16-final`, `vulkan-rd16`, README.txt). ## How tested (self-check) On one integration commit of the three PRs (perf/compact-terrain-vertex, perf/light-dirty-sections, perf/mesh-queue-columns) on origin/main `e508a5ce`, Mac mini: ``` cargo fmt --all --check ok cargo clippy --workspace --all-targets -- -D warnings ok, no warnings cargo test -p horizon-client --lib 870 passed, 0 failed, 11 ignored cargo build -p horizon-client --bin horizon-client ok tools/parity.py --changed origin/main 95 of 95 passed, 0 failed (155.9 min) ``` The harnesses covering the changed code passed: section_mesh, translucency and light. tooltip_display and mob_ai, which fail on main, also passed here. origin/main has moved since then (#859, #860); neither touches these files. 🤖 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:59:43 +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#2869
No description provided.