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

Open
opened 2026-11-26 21:19:23 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1901
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-console-tex-fast-runs


📝 Commits (1)

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

📊 Changes

3 files changed (+443 additions, -33 deletions)

View changed files

📝 runtime/CMakeLists.txt (+3 -0)
📝 runtime/src/console_texture_layout.h (+179 -33)
➕ runtime/test/console_texture_layout_test.cpp (+261 -0)

📄 Description

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.

🔄 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/ubiquitarianism/leonida/pull/1901 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-console-tex-fast-runs` --- ### 📝 Commits (1) - [`7c06770`](https://github.com/ubiquitarianism/leonida/commit/7c06770ce3bd83861caa7cec238ed5413c730ecd) perf(console_texture): stack-buffered runs, hoisted shifts, and fixed-width detile/tile dispatch (RT-CONSOLE-TEX-FAST-RUNS) ### 📊 Changes **3 files changed** (+443 additions, -33 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+3 -0) 📝 `runtime/src/console_texture_layout.h` (+179 -33) ➕ `runtime/test/console_texture_layout_test.cpp` (+261 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
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
starred/leonida#1912
No description provided.