[PR #686] [MERGED] Blend animated sprite frames without a roundf call per byte on x86 #2730

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

📋 Pull Request Information

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

Base: main ← Head: perf/sprite-blend-round


📝 Commits (1)

  • 15b2679 Blend animated sprite frames without a roundf call per byte on x86

📊 Changes

1 file changed (+27 additions, -1 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+27 -1)

📄 Description

What

Interpolated animated sprites (prismarine, magma, sea lantern and the other "interpolate": true textures) are blended on the CPU into every mip level with ((a * (1 - t) + b * t) * 255).round() as u8. Baseline x86-64 has no rounding instruction (that needs SSE4.1), so every byte of every blended mip chain was a call into libm's roundf. On Linux and Windows x86 this was 7.8–8.5% of the render thread's CPU samples. Apple Silicon has a rounding instruction, so the Mac never showed it.

round_unorm8 rounds with a truncation and an exact fraction test instead. For 0 ≤ x < 2^23, x - trunc(x) is exact, so adding one when it is ≥ 0.5 is exactly round-half-away-from-zero, like f32::round. Saturation matches as u8.

Why

Found while profiling #494 (Linux, CPU-bound) on the ThinkPad: perf record on the render thread showed roundf as the second hottest symbol after Renderer::render. Its hot caller was the mix_frames loop inlined into Renderer::render (cvtdq2ps, mulss, then a call through the GOT per byte). It runs at every tick that changes a blend.

Before / after

ThinkPad (Intel i5 Kaby Lake-U, 2C/4T, HD 620, Mesa 26.1.8 ANV, NixOS), headless sway (Wayland) with --offscreen, RD 8, seed 1234, the same saved world copied fresh for each run. 15 s standing, after loading settled. Run under nice -n 19. base = origin/main 1af78463, new = this branch.

run avg FPS 1% low 0.1% low CPU ms/frame (avg) CPU p95 ms render phase ms GPU ms/frame roundf share of render thread
base 99.4 35.0 32.1 5.08 22.7 2.77 3.90 8.46%
new 106.1 41.5 39.8 4.32 16.6 2.26 4.03 – (not sampled)
base 98.2 35.0 31.2 5.27 22.3 2.87 3.90 7.84%
new 102.6 40.9 37.5 4.85 19.5 2.49 3.94 –

That is about +5% average FPS, +17% 1% lows, +22% 0.1% lows and −0.6 ms of CPU per frame. GPU time is unchanged.

Image

Bit-identical: blended_frames_round_like_f32_round compares the new rounding with f32::round for every pair of frame bytes (256×256) at 144 blend times, plus the edge values (0.49999997, x.5, 255.00002, 300). The uploaded texels are byte-for-byte the same, so no screenshot can differ.

Self-check

  • cargo fmt --all --check: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • cargo test -p horizon-client --lib: 810 passed
  • Local parity (tools/parity.py --changed origin/main): 86 passed, 0 failed (44.5 min, Mac mini) on an integration commit of this branch with #688 (#600's fix, which includes #676).

🤖 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/686 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/17/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/sprite-blend-round` --- ### 📝 Commits (1) - [`15b2679`](https://github.com/horizon-rust/horizon/commit/15b26795e626903bc61fcac9165e7efdf779dc95) Blend animated sprite frames without a roundf call per byte on x86 ### 📊 Changes **1 file changed** (+27 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+27 -1) </details> ### 📄 Description ## What Interpolated animated sprites (prismarine, magma, sea lantern and the other `"interpolate": true` textures) are blended on the CPU into every mip level with `((a * (1 - t) + b * t) * 255).round() as u8`. Baseline x86-64 has no rounding instruction (that needs SSE4.1), so every byte of every blended mip chain was a call into libm's `roundf`. On Linux and Windows x86 this was **7.8–8.5% of the render thread's CPU samples**. Apple Silicon has a rounding instruction, so the Mac never showed it. `round_unorm8` rounds with a truncation and an exact fraction test instead. For 0 ≤ x < 2^23, `x - trunc(x)` is exact, so adding one when it is ≥ 0.5 is exactly round-half-away-from-zero, like `f32::round`. Saturation matches `as u8`. ## Why Found while profiling #494 (Linux, CPU-bound) on the ThinkPad: `perf record` on the render thread showed `roundf` as the second hottest symbol after `Renderer::render`. Its hot caller was the `mix_frames` loop inlined into `Renderer::render` (`cvtdq2ps`, `mulss`, then a call through the GOT per byte). It runs at every tick that changes a blend. ## Before / after ThinkPad (Intel i5 Kaby Lake-U, 2C/4T, HD 620, Mesa 26.1.8 ANV, NixOS), headless sway (Wayland) with `--offscreen`, RD 8, seed 1234, the same saved world copied fresh for each run. 15 s standing, after loading settled. Run under `nice -n 19`. base = origin/main 1af78463, new = this branch. | run | avg FPS | 1% low | 0.1% low | CPU ms/frame (avg) | CPU p95 ms | render phase ms | GPU ms/frame | `roundf` share of render thread | |---|---|---|---|---|---|---|---|---| | base | 99.4 | 35.0 | 32.1 | 5.08 | 22.7 | 2.77 | 3.90 | 8.46% | | new | 106.1 | 41.5 | 39.8 | 4.32 | 16.6 | 2.26 | 4.03 | – (not sampled) | | base | 98.2 | 35.0 | 31.2 | 5.27 | 22.3 | 2.87 | 3.90 | 7.84% | | new | 102.6 | 40.9 | 37.5 | 4.85 | 19.5 | 2.49 | 3.94 | – | That is about +5% average FPS, +17% 1% lows, +22% 0.1% lows and −0.6 ms of CPU per frame. GPU time is unchanged. ## Image Bit-identical: `blended_frames_round_like_f32_round` compares the new rounding with `f32::round` for every pair of frame bytes (256×256) at 144 blend times, plus the edge values (0.49999997, x.5, 255.00002, 300). The uploaded texels are byte-for-byte the same, so no screenshot can differ. ## Self-check - `cargo fmt --all --check`: clean - `cargo clippy --workspace --all-targets -- -D warnings`: clean - `cargo test -p horizon-client --lib`: 810 passed - Local parity (`tools/parity.py --changed origin/main`): 86 passed, 0 failed (44.5 min, Mac mini) on an integration commit of this branch with #688 (#600's fix, which includes #676). 🤖 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:58:36 +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#2730
No description provided.