perf(console_texture): stack-buffered runs, hoisted shifts, and fixed-width detile/tile dispatch (RT-CONSOLE-TEX-FAST-RUNS) #1901

Open
github-bot wants to merge 1 commit from github-bot/gh-1901 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 15:17 UTC · original
IlyassRS3:perf/rt-console-tex-fast-runs → main · +443 −33 · 3 files · 1 commits · open

Item ID: RT-CONSOLE-TEX-FAST-RUNS (claimed in #1608).

Summary

Optimizes leonida::console_texture::Layout::detile() and tile() (runtime/src/console_texture_layout.h) while preserving bit-exact output and every existing validation/error path:

  1. Zero-allocation stack run buffer (kStackRuns = 128):
    • Stores column runs on the stack for up to 128 runs (covering mip tails and small/medium textures without heap allocations), falling back to a pre-reserved std::vector<Run> only for wider surfaces.
  2. Hoisted power-of-two shifts and masks:
    • Precomputes bw_log2, bw_mask, bh_log2, bh_mask, align_log2, pitch_blocks, tail offsets, and pattern_[2][inner] once per subresource/slice instead of recomputing std::countr_zero(alignment) and integer division inside the per-row loop.
  3. Compile-time fixed-width run dispatch (with tail-run peeling):
    • Detects uniform micro-tile run lengths (1, 2, 4, 8, 16, 32, 64, 128, 256 bytes) across runs[0 .. body_count - 1] — peeling off an optional shorter final tail run for non-power-of-two widths like 1920x1080 — and dispatches once per subresource so std::memcpy lowers to fixed-width loads/stores instead of runtime-length calls.
  4. Contiguous linear slice fast path:
    • Copies tightly packed linear slices (row_pitch == bytes && m.pitch == m.width) in a single std::memcpy.

A/B Benchmark (-O2, macOS Apple Silicon, 20-iteration average)

Synthetic surface (detile) main This PR Speedup
2048x2048 RGBA8 64KB_S 1814.9 us (9.24 GB/s) 970.7 us (17.28 GB/s) 1.87x
2048x2048 RGBA8 64KB_D 1695.8 us (9.89 GB/s) 728.1 us (23.04 GB/s) 2.33x
2048x2048 BC7 64KB_S 424.2 us (9.89 GB/s) 164.3 us (25.53 GB/s) 2.58x
1920x1080 RGBA8 64KB_S 831.6 us (9.97 GB/s) 467.7 us (17.74 GB/s) 1.78x

Verification

  • AI-assisted: yes.
  • GPUs tested: none (macOS host, pure CPU C++20 portable test; no game files used).
  • Added portable CTest target console_texture_layout (runtime/test/console_texture_layout_test.cpp):
    • Exhaustive element-by-element oracle against Layout::offset() and tile -> detile round trips across all 7 addressable swizzle modes (kLinear, 256B_S/D, 4KB_S/D, 64KB_S/D), all element sizes (1..16), block=1 and block=4 (BC), 1D/2D/3D, mip tails, odd/prime extents (tail runs), wide textures (>128 runs heap fallback), tight & padded row/depth pitches, and all error paths (invalid, too_small, unsupported, SIZE_MAX overflow).
    • Clang 20 (-std=c++20 -Wall -Wextra -Werror): ASan+UBSan PASS, TSan PASS, -O2 × 3 PASS.
  • Hygiene: python3 -I scripts/check_no_game_data.py on all 3 touched files and python3 -I tests/test_text_write_line_endings.py passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 15:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1901) `IlyassRS3:perf/rt-console-tex-fast-runs` → `main` · +443 −33 · 3 files · 1 commits · open Item ID: `RT-CONSOLE-TEX-FAST-RUNS` (claimed in #1608). ### Summary Optimizes `leonida::console_texture::Layout::detile()` and `tile()` (`runtime/src/console_texture_layout.h`) while preserving bit-exact output and every existing validation/error path: 1. **Zero-allocation stack run buffer (`kStackRuns = 128`)**: - Stores column runs on the stack for up to 128 runs (covering mip tails and small/medium textures without heap allocations), falling back to a pre-reserved `std::vector<Run>` only for wider surfaces. 2. **Hoisted power-of-two shifts and masks**: - Precomputes `bw_log2`, `bw_mask`, `bh_log2`, `bh_mask`, `align_log2`, `pitch_blocks`, tail offsets, and `pattern_[2][inner]` once per subresource/slice instead of recomputing `std::countr_zero(alignment)` and integer division inside the per-row loop. 3. **Compile-time fixed-width run dispatch (with tail-run peeling)**: - Detects uniform micro-tile run lengths (`1, 2, 4, 8, 16, 32, 64, 128, 256` bytes) across `runs[0 .. body_count - 1]` — peeling off an optional shorter final tail run for non-power-of-two widths like `1920x1080` — and dispatches once per subresource so `std::memcpy` lowers to fixed-width loads/stores instead of runtime-length calls. 4. **Contiguous linear slice fast path**: - Copies tightly packed linear slices (`row_pitch == bytes && m.pitch == m.width`) in a single `std::memcpy`. ### A/B Benchmark (`-O2`, macOS Apple Silicon, 20-iteration average) | Synthetic surface (`detile`) | `main` | This PR | Speedup | |---|---:|---:|---:| | `2048x2048 RGBA8 64KB_S` | `1814.9 us` (`9.24 GB/s`) | `970.7 us` (`17.28 GB/s`) | **1.87x** | | `2048x2048 RGBA8 64KB_D` | `1695.8 us` (`9.89 GB/s`) | `728.1 us` (`23.04 GB/s`) | **2.33x** | | `2048x2048 BC7 64KB_S` | `424.2 us` (`9.89 GB/s`) | `164.3 us` (`25.53 GB/s`) | **2.58x** | | `1920x1080 RGBA8 64KB_S` | `831.6 us` (`9.97 GB/s`) | `467.7 us` (`17.74 GB/s`) | **1.78x** | ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (macOS host, pure CPU C++20 portable test; no game files used). - Added portable CTest target `console_texture_layout` (`runtime/test/console_texture_layout_test.cpp`): - Exhaustive element-by-element oracle against `Layout::offset()` and `tile -> detile` round trips across all 7 addressable swizzle modes (`kLinear`, `256B_S/D`, `4KB_S/D`, `64KB_S/D`), all element sizes (`1..16`), `block=1` and `block=4` (BC), 1D/2D/3D, mip tails, odd/prime extents (tail runs), wide textures (`>128` runs heap fallback), tight & padded row/depth pitches, and all error paths (`invalid`, `too_small`, `unsupported`, `SIZE_MAX` overflow). - Clang 20 (`-std=c++20 -Wall -Wextra -Werror`): ASan+UBSan PASS, TSan PASS, `-O2` × 3 PASS. - Hygiene: `python3 -I scripts/check_no_game_data.py` on all 3 touched files and `python3 -I tests/test_text_write_line_endings.py` passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:10:38 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 15:17 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 15:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1901#pullrequestreview-6060328864) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1901/head:github-bot/gh-1901'
git switch 'github-bot/gh-1901'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1901'
git switch 'github-bot/gh-1901'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1901'
git switch 'github-bot/gh-1901'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1901'
git switch 'main'
git merge --squash 'github-bot/gh-1901'
git switch 'main'
git merge --ff-only 'github-bot/gh-1901'
git switch 'main'
git merge 'github-bot/gh-1901'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.