[PR #1239] [MERGED] Animated block textures on the GPU: frames uploaded once, no per-tick CPU blend or copies (1% lows 1.7-2.4x on Intel) #3447

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

📋 Pull Request Information

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

Base: main ← Head: perf/animated-texture-uploads


📝 Commits (1)

  • abb7a97 Animated sprites on the GPU: frames uploaded once, one dispatch per mip level

📊 Changes

4 files changed (+663 additions, -353 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+15 -350)
➕ crates/horizon-client/src/gfx/animate.rs (+590 -0)
➕ crates/horizon-client/src/gfx/animate.wgsl (+48 -0)
📝 crates/horizon-client/src/sprites.rs (+10 -3)

📄 Description

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

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

What

Animated block textures (water, lava, fire, portals, sea lanterns, prismarine, magma, command blocks, sculk...) are now drawn on the GPU, the way vanilla 26.3 does it. Vanilla keeps every frame of an animated sprite in video memory, and each tick animate_sprite_blit or animate_sprite_interpolate draws the frame on show into the atlas.

  • Every frame's mip chain is uploaded once, when the renderer is created, at the sprite's own size: a 16 px water frame is 1.3 KB, not the 87 KB of its chain in the 64 px array layers. Vanilla's 668 frames take about 1.1 MB.
  • When a tick changes sprites, the CPU writes one 20-byte job for each sprite that changed, then runs one compute dispatch per mip level (gfx/animate.wgsl). The shader repeats the chain into the array's larger levels by whole pixels, as mipmap::generate does, and mixes interpolated frames.
  • The CPU blending at the array's size and the frame mip cache are removed, and so are the copy commands: each changed sprite used to cost one copy per mip level, and each of those copies is a blit in Mesa.

Why

On the ThinkPad (HD 620, Mesa), animated textures were about a fifth of the render thread at render distance 32. Every tick, about 44 sprites changed. Each one was mixed on the CPU at 64×64 with all its mip levels, staged at that size (3.8 MB a tick) and copied level by level (about 300 copy commands a tick). That cost landed on the frames that ran a tick, so it showed up as spikes and poor 1% lows.

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, world ticks frozen (client ticks still run, and texture animation runs on client ticks), HUD off, uncapped;
  • 10 s still at each view, after the terrain settled;
  • main (c16b41ab) and this branch interleaved, 2 runs each, averaged;
  • both builds carry the same local measurement patch, which prints every span every frame plus an animate span.
  • The ThinkPad was shared with perf-server-14's server benchmarks, so each run started only when the load was under 2.
RD view frame avg ms p99 ms 1% low FPS max ms frames > 16.7 ms CPU ms GPU ms encode ms animate ms
16 meadow 3.73 → 3.55 9.29 → 4.05 94 → 226 16.9 → 5.3 0.5 → 0 1.95 → 1.57 3.55 → 3.48 0.91 → 0.70 0.165 → 0.020
16 overview 5.89 → 5.52 11.20 → 5.94 74 → 163 18.6 → 6.8 4 → 0 2.74 → 2.15 5.68 → 5.42 1.36 → 1.03 0.254 → 0.021
16 peak 6.53 → 6.26 12.46 → 8.46 69 → 116 20.8 → 8.9 4.5 → 0 3.24 → 2.24 6.34 → 6.18 1.76 → 1.19 0.326 → 0.021
32 meadow 4.61 → 4.55 10.10 → 5.10 95 → 179 12.2 → 7.8 0 → 0 2.70 → 2.19 4.44 → 4.44 1.03 → 0.75 0.192 → 0.019
32 overview 14.13 → 13.95 15.65 → 14.79 60 → 67 19.7 → 15.2 2 → 0 5.55 → 4.92 13.84 → 13.79 2.40 → 2.09 0.627 → 0.026
32 peak 20.54 → 20.29 22.19 → 21.47 41 → 46 29.3 → 22.6 485 → 493 8.38 → 7.61 20.17 → 20.13 4.83 → 4.69 0.835 → 0.026
  • CPU time per frame drops by 0.4 to 1.0 ms. The 1% lows go up 1.7 to 2.4× wherever the GPU isn't the limit: the tick frames no longer spike.
  • GPU time does not go up. The sprite arrays now carry STORAGE_BINDING, and the GPU time shows no cost from that on Intel.
  • At RD 32 peak, the HD 620 is GPU-bound at 20 ms on both builds.

perf record at RD 32, overview view, 8 s, render thread (user cycles):

main this branch
render thread samples 8298 6196 (−25%)
animating (CPU mix, staging, recording) 12.3% 0.7%
copy_buffer_to_texture inside CommandEncoder::finish 11.2% 0.4%
of which Mesa blorp 7.9% 2.6% (other copies)

Mac mini (M4 Pro, Metal): built, run and captured there (screenshots below). Frame times were not measured: the machine was shared, with a load average of 20 to 60. DirectX 12 was not run (no Windows machine). The work is the same on every backend: one compute pass on the ticks that change sprites.

Parity

  • The picture: frames, frame timing, interpolation and mip levels are the same as before. source_chains_repeat_into_the_array_chain (every mipmap strategy, item and block, native sizes 1 to 32 in 16 to 64 px layers, and a non-power-of-two size) and real_animation_chains_repeat_into_the_array_chain (vanilla's water, lava, fire, portal, sea lantern, prismarine, magma and kelp) check that repeating each frame's own chain gives exactly the array chain the old code uploaded.
  • gpu_layers_match_the_cpu_chains_every_tick runs the shader on a real device. It reads back every level of every layer at 10 ticks (8 px sprites in 32 px layers with and without interpolation, a sprite at the array's size, three arrays) and compares them with a CPU reference. It also checks that only changed sprites are written.
  • Interpolation rounding: the mix is now computed in integers, (a·(1000−m) + b·m + 500) / 1000, which is the same on every GPU. The old CPU path used f32. The two differ only where the exact mix is a half (0.015% of the (a, b, millis) cases): there f32 rounded either way. rounding_matches_f32_mix_but_for_exact_halves checks that. Vanilla mixes in the fragment shader, so at those halves its result is the GPU's own rounding anyway.

Screenshot comparison

Mac mini, Metal. Both builds carry a proof-only patch that makes the animation tick come from a file. A new world has a row of animated blocks: water and lava, still and flowing; fire and soul fire; a nether portal; sea lantern, prismarine, magma, command and repeating command blocks, sculk and sculk vein, stonecutter, a lit blast furnace and smoker, crimson and warped stems. World ticks are frozen, particles minimal, HUD hidden. The order was main / branch / main, at animation ticks 0, 1, 2, 4, 5, 13, 40, 157 and 1234.

Selected ticks (all 9 in mac-anim/):

tick main vs branch: px off by > 3 main vs main: px off by > 3 branch-only
0 1459 1587 207
4 1218 1116 306
13 1310 1261 151
157 1200 1398 277
1234 1740 1783 161

Each run's picture is off by 1–2 levels everywhere from any other run, because the lightmap's block-light flicker is random. Every pixel off by more than that, in both columns, is a nether portal particle: they spawn at random. The masks show no difference on any animated block at any tick.

How tested (Mac mini M4 Pro)

  • cargo fmt --all --check: ok
  • cargo clippy --workspace --all-targets -- -D warnings: ok
  • cargo test -p horizon-client --lib: 987 passed, 0 failed. New tests are in gfx/animate.rs. 8 tests of the removed CPU path and its cache were dropped.
  • tools/parity.py --changed origin/main: 102 passed, 0 failed (on c16b41ab; the branch is since rebased on main without conflicts)

Data and scripts: ~/code/horizon-parity/shots/perf-frame-12/ (ab.jsonl, q-{base,gpu}-32.folded, mac-anim/, scripts/).

What is left

At RD 32 overview, the largest remaining render-thread cost on the HD 620 is the section walk (Resident::visible, the visibility span): 23.5% of the thread. That is 12.6% for the walk itself, 5.9% for frustum tests and 5.0% for sorting the visible sections. After it comes CommandEncoder::finish with the multi-draws. Device::poll (3.6%) is the offscreen benchmark's two-frames-in-flight wait, which a windowed game doesn't have.

Label: feature (frame time and 1% lows on integrated GPUs).

🤖 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/1239 **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/animated-texture-uploads` --- ### 📝 Commits (1) - [`abb7a97`](https://github.com/horizon-rust/horizon/commit/abb7a971363bf2244b63585a8daf9a9d7c620744) Animated sprites on the GPU: frames uploaded once, one dispatch per mip level ### 📊 Changes **4 files changed** (+663 additions, -353 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+15 -350) ➕ `crates/horizon-client/src/gfx/animate.rs` (+590 -0) ➕ `crates/horizon-client/src/gfx/animate.wgsl` (+48 -0) 📝 `crates/horizon-client/src/sprites.rs` (+10 -3) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Part of the frame-cost batch (Kira card #23). ## What Animated block textures (water, lava, fire, portals, sea lanterns, prismarine, magma, command blocks, sculk...) are now drawn on the GPU, the way vanilla 26.3 does it. Vanilla keeps every frame of an animated sprite in video memory, and each tick `animate_sprite_blit` or `animate_sprite_interpolate` draws the frame on show into the atlas. - Every frame's mip chain is uploaded once, when the renderer is created, at the sprite's own size: a 16 px water frame is 1.3 KB, not the 87 KB of its chain in the 64 px array layers. Vanilla's 668 frames take about 1.1 MB. - When a tick changes sprites, the CPU writes one 20-byte job for each sprite that changed, then runs one compute dispatch per mip level (`gfx/animate.wgsl`). The shader repeats the chain into the array's larger levels by whole pixels, as `mipmap::generate` does, and mixes interpolated frames. - The CPU blending at the array's size and the frame mip cache are removed, and so are the copy commands: each changed sprite used to cost one copy per mip level, and each of those copies is a blit in Mesa. ## Why On the ThinkPad (HD 620, Mesa), animated textures were about a fifth of the render thread at render distance 32. Every tick, about 44 sprites changed. Each one was mixed on the CPU at 64×64 with all its mip levels, staged at that size (3.8 MB a tick) and copied level by level (about 300 copy commands a tick). That cost landed on the frames that ran a tick, so it showed up as spikes and poor 1% lows. ## 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, world ticks frozen (client ticks still run, and texture animation runs on client ticks), HUD off, uncapped; - 10 s still at each view, after the terrain settled; - `main` (c16b41ab) and this branch interleaved, 2 runs each, averaged; - both builds carry the same local measurement patch, which prints every span every frame plus an `animate` span. - The ThinkPad was shared with perf-server-14's server benchmarks, so each run started only when the load was under 2. | RD | view | frame avg ms | p99 ms | 1% low FPS | max ms | frames > 16.7 ms | CPU ms | GPU ms | encode ms | animate ms | |---|---|---|---|---|---|---|---|---|---|---| | 16 | meadow | 3.73 → 3.55 | 9.29 → **4.05** | 94 → **226** | 16.9 → 5.3 | 0.5 → 0 | 1.95 → **1.57** | 3.55 → 3.48 | 0.91 → 0.70 | 0.165 → 0.020 | | 16 | overview | 5.89 → 5.52 | 11.20 → **5.94** | 74 → **163** | 18.6 → 6.8 | 4 → 0 | 2.74 → **2.15** | 5.68 → 5.42 | 1.36 → 1.03 | 0.254 → 0.021 | | 16 | peak | 6.53 → 6.26 | 12.46 → **8.46** | 69 → **116** | 20.8 → 8.9 | 4.5 → 0 | 3.24 → **2.24** | 6.34 → 6.18 | 1.76 → 1.19 | 0.326 → 0.021 | | 32 | meadow | 4.61 → 4.55 | 10.10 → **5.10** | 95 → **179** | 12.2 → 7.8 | 0 → 0 | 2.70 → **2.19** | 4.44 → 4.44 | 1.03 → 0.75 | 0.192 → 0.019 | | 32 | overview | 14.13 → 13.95 | 15.65 → 14.79 | 60 → 67 | 19.7 → 15.2 | 2 → 0 | 5.55 → **4.92** | 13.84 → 13.79 | 2.40 → 2.09 | 0.627 → 0.026 | | 32 | peak | 20.54 → 20.29 | 22.19 → 21.47 | 41 → 46 | 29.3 → 22.6 | 485 → 493 | 8.38 → **7.61** | 20.17 → 20.13 | 4.83 → 4.69 | 0.835 → 0.026 | - CPU time per frame drops by 0.4 to 1.0 ms. The 1% lows go up 1.7 to 2.4× wherever the GPU isn't the limit: the tick frames no longer spike. - GPU time does not go up. The sprite arrays now carry `STORAGE_BINDING`, and the GPU time shows no cost from that on Intel. - At RD 32 peak, the HD 620 is GPU-bound at 20 ms on both builds. `perf record` at RD 32, overview view, 8 s, render thread (user cycles): | | main | this branch | |---|---|---| | render thread samples | 8298 | 6196 (−25%) | | animating (CPU mix, staging, recording) | 12.3% | 0.7% | | `copy_buffer_to_texture` inside `CommandEncoder::finish` | 11.2% | 0.4% | | of which Mesa blorp | 7.9% | 2.6% (other copies) | **Mac mini** (M4 Pro, Metal): built, run and captured there (screenshots below). Frame times were not measured: the machine was shared, with a load average of 20 to 60. DirectX 12 was not run (no Windows machine). The work is the same on every backend: one compute pass on the ticks that change sprites. ## Parity - The picture: frames, frame timing, interpolation and mip levels are the same as before. `source_chains_repeat_into_the_array_chain` (every mipmap strategy, item and block, native sizes 1 to 32 in 16 to 64 px layers, and a non-power-of-two size) and `real_animation_chains_repeat_into_the_array_chain` (vanilla's water, lava, fire, portal, sea lantern, prismarine, magma and kelp) check that repeating each frame's own chain gives exactly the array chain the old code uploaded. - `gpu_layers_match_the_cpu_chains_every_tick` runs the shader on a real device. It reads back every level of every layer at 10 ticks (8 px sprites in 32 px layers with and without interpolation, a sprite at the array's size, three arrays) and compares them with a CPU reference. It also checks that only changed sprites are written. - Interpolation rounding: the mix is now computed in integers, `(a·(1000−m) + b·m + 500) / 1000`, which is the same on every GPU. The old CPU path used f32. The two differ only where the exact mix is a half (0.015% of the (a, b, millis) cases): there f32 rounded either way. `rounding_matches_f32_mix_but_for_exact_halves` checks that. Vanilla mixes in the fragment shader, so at those halves its result is the GPU's own rounding anyway. ## Screenshot comparison Mac mini, Metal. Both builds carry a proof-only patch that makes the animation tick come from a file. A new world has a row of animated blocks: water and lava, still and flowing; fire and soul fire; a nether portal; sea lantern, prismarine, magma, command and repeating command blocks, sculk and sculk vein, stonecutter, a lit blast furnace and smoker, crimson and warped stems. World ticks are frozen, particles minimal, HUD hidden. The order was `main` / branch / `main`, at animation ticks 0, 1, 2, 4, 5, 13, 40, 157 and 1234. Selected ticks (all 9 in `mac-anim/`): | tick | main vs branch: px off by > 3 | main vs main: px off by > 3 | branch-only | |---|---|---|---| | 0 | 1459 | 1587 | 207 | | 4 | 1218 | 1116 | 306 | | 13 | 1310 | 1261 | 151 | | 157 | 1200 | 1398 | 277 | | 1234 | 1740 | 1783 | 161 | Each run's picture is off by 1–2 levels everywhere from any other run, because the lightmap's block-light flicker is random. Every pixel off by more than that, in both columns, is a nether portal particle: they spawn at random. The masks show no difference on any animated block at any tick. ## How tested (Mac mini M4 Pro) - `cargo fmt --all --check`: ok - `cargo clippy --workspace --all-targets -- -D warnings`: ok - `cargo test -p horizon-client --lib`: 987 passed, 0 failed. New tests are in `gfx/animate.rs`. 8 tests of the removed CPU path and its cache were dropped. - `tools/parity.py --changed origin/main`: **102 passed, 0 failed** (on c16b41ab; the branch is since rebased on main without conflicts) Data and scripts: `~/code/horizon-parity/shots/perf-frame-12/` (`ab.jsonl`, `q-{base,gpu}-32.folded`, `mac-anim/`, `scripts/`). ## What is left At RD 32 overview, the largest remaining render-thread cost on the HD 620 is the section walk (`Resident::visible`, the `visibility` span): 23.5% of the thread. That is 12.6% for the walk itself, 5.9% for frustum tests and 5.0% for sorting the visible sections. After it comes `CommandEncoder::finish` with the multi-draws. `Device::poll` (3.6%) is the offscreen benchmark's two-frames-in-flight wait, which a windowed game doesn't have. Label: `feature` (frame time and 1% lows on integrated GPUs). 🤖 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:35 +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#3447
No description provided.