[PR #501] [MERGED] Upload animated sprites through a reused staging belt #2602

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

📋 Pull Request Information

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

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


📝 Commits (1)

  • 9e73a84 Upload animated sprites through a reused staging belt

📊 Changes

2 files changed (+212 additions, -18 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+16 -18)
➕ crates/horizon-client/src/gfx/upload.rs (+196 -0)

📄 Description

What

Animated sprites (water, lava, fire, portals, prismarine, sea lanterns...) now upload their changed frames through one reused wgpu::util::StagingBelt, recorded into the frame's encoder, instead of one Queue::write_texture per sprite and mip level.

Why

Queue::write_texture (and write_buffer) allocate a fresh staging buffer on every call; on Metal each is a kernel call (IOGPUResourceCreate). The animations write every changed sprite's 5 mip levels once per tick, so the frame that follows each tick spent about 1.3 ms creating buffers. In a Time Profiler capture at render distance 16, queue_write_texture was 9.7% of the render thread, all of it on tick frames: one of the "spike at every game tick" sources in #495 that sits in the renderer rather than in ticking.

How

  • gfx/upload.rs: Uploads wraps a 1 MiB-chunk staging belt. texture() pads rows to COPY_BYTES_PER_ROW_ALIGNMENT and records a copy_buffer_to_texture; finish() closes the chunks and has them re-mapped when the frame's submission completes (finish_and_recall_on_submit), so they are reused the next tick.
  • Renderer::render creates its encoder before animate and passes it in. The copies precede the frame's passes in the same command buffer, as the queue's pending writes did.
  • The fire overlay's two HUD sprites and the per-frame buffer writes are unchanged here (next PR).

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, render distance 16, 10 s horizon-ctl bench per run, interleaved base/new/base/new. Other agents' games and builds were running on the same machine, so absolute FPS is noisy; the tick-frame column is the direct effect.

Run Avg FPS 1% low FPS CPU ms/frame avg CPU ms p99 render phase in the slowest 1% of frames (ms)
base 895 179 0.62 5.21 2.69
new 1445 249 0.41 3.53 1.40
base 816 165 0.75 5.64 2.86
new 995 225 0.54 4.04 1.46

GPU ms: not available on Apple GPUs yet (the frame timer needs timestamps inside encoders; Apple only samples at pass boundaries).

Correctness

  • New GPU test belt_uploads_match_queue_writes_on_every_mip_level: uploads 5 mip levels of one array layer through the belt and through write_texture, two frames (the second reusing recalled chunks), and compares readbacks byte for byte. It skips when no adapter exists. Breaking the row padding makes it fail.
  • Screenshots (mountain scene, ticks frozen): base vs new differs less (21% of pixels, all by 1 level) than two launches of base (42%): the vignette smoothing and the reused world change between launches. Water and lava render normally.
cargo fmt --all --check                                   ok
cargo clippy --workspace --all-targets -- -D warnings      ok
cargo test -p horizon-client --lib                        750 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/501 **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/animated-sprite-uploads` --- ### 📝 Commits (1) - [`9e73a84`](https://github.com/horizon-rust/horizon/commit/9e73a8417f7d30e914c4330c03cfed70700ec67d) Upload animated sprites through a reused staging belt ### 📊 Changes **2 files changed** (+212 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+16 -18) ➕ `crates/horizon-client/src/gfx/upload.rs` (+196 -0) </details> ### 📄 Description ## What Animated sprites (water, lava, fire, portals, prismarine, sea lanterns...) now upload their changed frames through one reused `wgpu::util::StagingBelt`, recorded into the frame's encoder, instead of one `Queue::write_texture` per sprite and mip level. ## Why `Queue::write_texture` (and `write_buffer`) allocate a fresh staging buffer on every call; on Metal each is a kernel call (`IOGPUResourceCreate`). The animations write every changed sprite's 5 mip levels once per tick, so the frame that follows each tick spent about 1.3 ms creating buffers. In a Time Profiler capture at render distance 16, `queue_write_texture` was 9.7% of the render thread, all of it on tick frames: one of the "spike at every game tick" sources in #495 that sits in the renderer rather than in ticking. ## How - `gfx/upload.rs`: `Uploads` wraps a 1 MiB-chunk staging belt. `texture()` pads rows to `COPY_BYTES_PER_ROW_ALIGNMENT` and records a `copy_buffer_to_texture`; `finish()` closes the chunks and has them re-mapped when the frame's submission completes (`finish_and_recall_on_submit`), so they are reused the next tick. - `Renderer::render` creates its encoder before `animate` and passes it in. The copies precede the frame's passes in the same command buffer, as the queue's pending writes did. - The fire overlay's two HUD sprites and the per-frame buffer writes are unchanged here (next PR). ## 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, render distance 16, 10 s `horizon-ctl bench` per run, interleaved base/new/base/new. Other agents' games and builds were running on the same machine, so absolute FPS is noisy; the tick-frame column is the direct effect. | Run | Avg FPS | 1% low FPS | CPU ms/frame avg | CPU ms p99 | render phase in the slowest 1% of frames (ms) | | --- | --- | --- | --- | --- | --- | | base | 895 | 179 | 0.62 | 5.21 | 2.69 | | new | 1445 | 249 | 0.41 | 3.53 | 1.40 | | base | 816 | 165 | 0.75 | 5.64 | 2.86 | | new | 995 | 225 | 0.54 | 4.04 | 1.46 | GPU ms: not available on Apple GPUs yet (the frame timer needs timestamps inside encoders; Apple only samples at pass boundaries). ## Correctness - New GPU test `belt_uploads_match_queue_writes_on_every_mip_level`: uploads 5 mip levels of one array layer through the belt and through `write_texture`, two frames (the second reusing recalled chunks), and compares readbacks byte for byte. It skips when no adapter exists. Breaking the row padding makes it fail. - Screenshots (mountain scene, ticks frozen): base vs new differs less (21% of pixels, all by 1 level) than two launches of base (42%): the vignette smoothing and the reused world change between launches. Water and lava render normally. ``` cargo fmt --all --check ok cargo clippy --workspace --all-targets -- -D warnings ok cargo test -p horizon-client --lib 750 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:32 +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#2602
No description provided.