[PR #487] [CLOSED] Clouds: keep the mesh and rebuild it only when vanilla does #2600

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/487
Author: @ludovic111
Created: 10/16/2026
Status: ❌ Closed

Base: main ← Head: fix/clouds-cached-mesh


📝 Commits (1)

  • dc9dcba Keep the cloud mesh and rebuild it only when vanilla does

📊 Changes

7 files changed (+515 additions, -154 deletions)

View changed files

📝 crates/horizon-client/src/clouds.rs (+390 -0)
📝 crates/horizon-client/src/entity.wgsl (+11 -2)
📝 crates/horizon-client/src/game.rs (+26 -118)
📝 crates/horizon-client/src/game/packs.rs (+1 -1)
📝 crates/horizon-client/src/gfx.rs (+78 -25)
📝 crates/horizon-client/src/gfx/entity_batch.rs (+6 -8)
📝 crates/horizon-client/src/terrain.wgsl (+3 -0)

📄 Description

Fixes #480. Thanks to @Enzerot for the very well-measured report.

What

Vanilla 26.3's CloudRenderer keeps the cloud cells it built and rebuilds them only when one of these changes:

  • the camera's cloud cell,
  • its position against the cloud layer (above, inside or below),
  • the cloud type,
  • the Cloud Distance.

Each frame it only moves them by the offset into the cell (CloudInfo). Horizon rebuilt every cloud quad on the main thread every frame and uploaded them with the entities. At Cloud Distance 128 that cost about 1.8 ms of CPU per frame, standing still.

Now:

  • clouds::Clouds holds clouds.png and the cached mesh. prepare is vanilla's prepare: it computes the cell and the offset, and rebuilds only on vanilla's triggers. A resource reload replaces it, which is vanilla's markForRebuild.
  • The renderer keeps the mesh in its own vertex buffer and uploads it only when its generation changes. It draws it with the same cloud pipeline as before. The entity batch no longer has a cloud bucket.
  • Each vertex holds its cell's low corner, the face shade index, and whether it sits on the far side of the cell in x, y and z. entity.wgsl adds the offset, then the 12x4x12 cell size, so positions round exactly as when they were built in place (a unit test checks this bit for bit against the old builder). The shaded cloud colours are packed RGBA8 in Globals as before, so day/night colour changes need no rebuild.

Vanilla 26.3's CloudRenderer has no frustum pass of its own (it draws the whole face buffer), so none was added.

Benchmark (horizon-ctl bench 6)

Setup as in the issue: fresh creative world, seed 1234, standing still at spawn, render distance 12, 1280x720, VSync off, unlimited FPS, inactivity FPS limit off. Measured on the Mac mini (M4 Pro, Metal), release builds of origin/main and this branch, run base/new/base/new; each row is the average of the two runs. The screen session was locked, so the game ran --offscreen (no present mode applies; the GPU timer reports nothing offscreen on Metal). Load average during the runs was 3–7 on 14 threads; another agent session was using about 2–8 cores.

Before (origin/main)

Clouds Cloud Distance avg FPS CPU ms/frame prepare ms render ms
Fancy 128 466 2.13 1.10 0.91
Fast 128 493 2.01 0.78 1.13
Fancy 64 1,375 0.72 0.32 0.35
Fast 64 1,456 0.68 0.23 0.41
Fancy 32 2,361 0.40 0.14 0.23
Fast 32 2,286 0.40 0.11 0.26
Off - 2,456 0.29 0.07 0.18

After (this PR)

Clouds Cloud Distance avg FPS CPU ms/frame prepare ms render ms
Fancy 128 1,588 0.34 0.09 0.20
Fast 128 1,577 0.31 0.08 0.19
Fancy 64 1,812 0.29 0.07 0.18
Fast 64 2,328 0.26 0.07 0.17
Fancy 32 2,441 0.26 0.07 0.17
Fast 32 2,440 0.27 0.07 0.17
Off - 2,476 0.27 0.07 0.16

With clouds on, CPU time per frame is now within about 0.07 ms of clouds off at every Cloud Distance. What remains between Fancy 128 (1,588 FPS) and Off is GPU time: drawing the clouds themselves.

Visual proof

The comparison builds carried an identical, uncommitted test hook: an env var, read once, that pins the cloud scroll time. Without it the cloud position depends on the game tick, which keeps advancing during world load. Capture setup: offscreen, /tick freeze, spectator, GUI hidden, vignette off. Render distance 2 keeps terrain out of the frame (terrain detail varies between runs); the clouds still use Cloud Distance 128.

  • Static shots, 40 frames. Fancy and Fast, at times 1000, 6000, 12600 and 18000, from below (y 150), inside (y 194.2) and above (y 240) the layer and looking at the far horizon. Also just under the bottom (y 192.2), just over the top (y 196.5), at a cell corner, and one shot twice to check determinism.

    • Baseline run twice: identical except a "New Recipes" toast in the first frame.
    • Base against new: 22 frames identical. 17 frames differ in 1–3 pixels, by 1/255 in one channel. The first frame differs only in the toast.
  • Moving and flying through the layer, 82 frames. No wait-ready between steps, so each frame uses the mesh cached at the previous position:

    • stepping y through 186 → 192.30 / 192.36 (either side of the bottom) → 196.30 / 196.36 (either side of the top) → 200 and back;
    • stepping x/z across cell boundaries and back.

    63 frames identical; 19 differ in at most 2 pixels by 1/255. A stale mesh would differ in thousands of pixels: no popping.

  • The CPU-side mesh is bit-identical to the old per-frame builder (cached_mesh_moved_into_place_matches_building_in_place). The leftover 1/255 pixels come from the GPU doing the translation in the shader (as vanilla does), not from geometry or colour differences.

Self-check

  • cargo fmt --all: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • cargo test -p horizon-client: all pass (751 lib + the rest). New tests:
    • cached_mesh_moved_into_place_matches_building_in_place (the old builder kept as the oracle),
    • mesh_is_rebuilt_only_when_vanilla_rebuilds_it,
    • shaded_colors_pack_like_vertex_colors.
  • tools/parity.py --changed origin/main (3 jobs): 84 passed, 0 failed, including cloud_render and weather_render.

Needs Ludovic

Nothing blocking. The benchmark ran offscreen on the Mac mini because the screen session was locked; a windowed run on Windows/Vulkan (Enzerot's setup) would confirm the Immediate-present numbers.

🤖 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/487 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/16/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/clouds-cached-mesh` --- ### 📝 Commits (1) - [`dc9dcba`](https://github.com/horizon-rust/horizon/commit/dc9dcba5e30b65da3f6c02b7a140a7b6dbb89ca1) Keep the cloud mesh and rebuild it only when vanilla does ### 📊 Changes **7 files changed** (+515 additions, -154 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/clouds.rs` (+390 -0) 📝 `crates/horizon-client/src/entity.wgsl` (+11 -2) 📝 `crates/horizon-client/src/game.rs` (+26 -118) 📝 `crates/horizon-client/src/game/packs.rs` (+1 -1) 📝 `crates/horizon-client/src/gfx.rs` (+78 -25) 📝 `crates/horizon-client/src/gfx/entity_batch.rs` (+6 -8) 📝 `crates/horizon-client/src/terrain.wgsl` (+3 -0) </details> ### 📄 Description Fixes #480. Thanks to @Enzerot for the very well-measured report. ## What Vanilla 26.3's `CloudRenderer` keeps the cloud cells it built and rebuilds them only when one of these changes: - the camera's cloud cell, - its position against the cloud layer (above, inside or below), - the cloud type, - the Cloud Distance. Each frame it only moves them by the offset into the cell (`CloudInfo`). Horizon rebuilt every cloud quad on the main thread every frame and uploaded them with the entities. At Cloud Distance 128 that cost about 1.8 ms of CPU per frame, standing still. Now: - `clouds::Clouds` holds clouds.png and the cached mesh. `prepare` is vanilla's `prepare`: it computes the cell and the offset, and rebuilds only on vanilla's triggers. A resource reload replaces it, which is vanilla's `markForRebuild`. - The renderer keeps the mesh in its own vertex buffer and uploads it only when its generation changes. It draws it with the same cloud pipeline as before. The entity batch no longer has a cloud bucket. - Each vertex holds its cell's low corner, the face shade index, and whether it sits on the far side of the cell in x, y and z. `entity.wgsl` adds the offset, then the 12x4x12 cell size, so positions round exactly as when they were built in place (a unit test checks this bit for bit against the old builder). The shaded cloud colours are packed RGBA8 in `Globals` as before, so day/night colour changes need no rebuild. Vanilla 26.3's `CloudRenderer` has no frustum pass of its own (it draws the whole face buffer), so none was added. ## Benchmark (`horizon-ctl bench 6`) Setup as in the issue: fresh creative world, seed 1234, standing still at spawn, render distance 12, 1280x720, VSync off, unlimited FPS, inactivity FPS limit off. Measured on the Mac mini (M4 Pro, Metal), release builds of origin/main and this branch, run base/new/base/new; each row is the average of the two runs. The screen session was locked, so the game ran `--offscreen` (no present mode applies; the GPU timer reports nothing offscreen on Metal). Load average during the runs was 3–7 on 14 threads; another agent session was using about 2–8 cores. **Before (origin/main)** | Clouds | Cloud Distance | avg FPS | CPU ms/frame | prepare ms | render ms | | --- | ---: | ---: | ---: | ---: | ---: | | Fancy | 128 | 466 | 2.13 | 1.10 | 0.91 | | Fast | 128 | 493 | 2.01 | 0.78 | 1.13 | | Fancy | 64 | 1,375 | 0.72 | 0.32 | 0.35 | | Fast | 64 | 1,456 | 0.68 | 0.23 | 0.41 | | Fancy | 32 | 2,361 | 0.40 | 0.14 | 0.23 | | Fast | 32 | 2,286 | 0.40 | 0.11 | 0.26 | | Off | - | 2,456 | 0.29 | 0.07 | 0.18 | **After (this PR)** | Clouds | Cloud Distance | avg FPS | CPU ms/frame | prepare ms | render ms | | --- | ---: | ---: | ---: | ---: | ---: | | Fancy | 128 | 1,588 | 0.34 | 0.09 | 0.20 | | Fast | 128 | 1,577 | 0.31 | 0.08 | 0.19 | | Fancy | 64 | 1,812 | 0.29 | 0.07 | 0.18 | | Fast | 64 | 2,328 | 0.26 | 0.07 | 0.17 | | Fancy | 32 | 2,441 | 0.26 | 0.07 | 0.17 | | Fast | 32 | 2,440 | 0.27 | 0.07 | 0.17 | | Off | - | 2,476 | 0.27 | 0.07 | 0.16 | With clouds on, CPU time per frame is now within about 0.07 ms of clouds off at every Cloud Distance. What remains between Fancy 128 (1,588 FPS) and Off is GPU time: drawing the clouds themselves. ## Visual proof The comparison builds carried an identical, uncommitted test hook: an env var, read once, that pins the cloud scroll time. Without it the cloud position depends on the game tick, which keeps advancing during world load. Capture setup: offscreen, `/tick freeze`, spectator, GUI hidden, vignette off. Render distance 2 keeps terrain out of the frame (terrain detail varies between runs); the clouds still use Cloud Distance 128. - **Static shots, 40 frames.** Fancy and Fast, at times 1000, 6000, 12600 and 18000, from below (y 150), inside (y 194.2) and above (y 240) the layer and looking at the far horizon. Also just under the bottom (y 192.2), just over the top (y 196.5), at a cell corner, and one shot twice to check determinism. - Baseline run twice: identical except a "New Recipes" toast in the first frame. - Base against new: 22 frames identical. 17 frames differ in 1–3 pixels, by 1/255 in one channel. The first frame differs only in the toast. - **Moving and flying through the layer, 82 frames.** No `wait-ready` between steps, so each frame uses the mesh cached at the previous position: - stepping y through 186 → 192.30 / 192.36 (either side of the bottom) → 196.30 / 196.36 (either side of the top) → 200 and back; - stepping x/z across cell boundaries and back. 63 frames identical; 19 differ in at most 2 pixels by 1/255. A stale mesh would differ in thousands of pixels: no popping. - The CPU-side mesh is bit-identical to the old per-frame builder (`cached_mesh_moved_into_place_matches_building_in_place`). The leftover 1/255 pixels come from the GPU doing the translation in the shader (as vanilla does), not from geometry or colour differences. ## Self-check - `cargo fmt --all`: clean - `cargo clippy --workspace --all-targets -- -D warnings`: clean - `cargo test -p horizon-client`: all pass (751 lib + the rest). New tests: - `cached_mesh_moved_into_place_matches_building_in_place` (the old builder kept as the oracle), - `mesh_is_rebuilt_only_when_vanilla_rebuilds_it`, - `shaded_colors_pack_like_vertex_colors`. - `tools/parity.py --changed origin/main` (3 jobs): **84 passed, 0 failed**, including `cloud_render` and `weather_render`. ## Needs Ludovic Nothing blocking. The benchmark ran offscreen on the Mac mini because the screen session was locked; a windowed run on Windows/Vulkan (Enzerot's setup) would confirm the Immediate-present numbers. 🤖 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:30 +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#2600
No description provided.