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

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1901 into main
IlyassRS3 commented 2026-11-25 15:17:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T15:17:32Z.


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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5494106798 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1901). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T15:17:32Z. --- 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.
copilot-pull-request-reviewer[bot] commented 2026-11-25 15:17:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T15:17:47Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6060328864 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1901#pullrequestreview-6060328864). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T15:17:47Z. **Review:** COMMENTED --- 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.
zmasi commented 2026-11-28 12:41:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:41:27Z.


#1901 | head 7c06770ce3 (merged cleanly with main d48a957bb9) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(console_texture_layout|resource_detile|d3d12_mapped_plane_async)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) + console_texture_layout_test --bench A/B on MSVC RelWithDebInfo | baseline on main: 2048x2048 RGBA8 64KB_S 4487.3 us (3.74 GB/s), 64KB_D 4562.6 us (3.68 GB/s), BC7 64KB_S 642.9 us (6.52 GB/s), 1920x1080 RGBA8 64KB_S 1831.7 us (4.53 GB/s) | green on PR+main: 3/3 CTests pass (console_texture_layout exhaustive oracle + error paths, resource_detile, and d3d12_mapped_plane_async) on RTX 3090 with 0 Khronos validation errors, and --bench speeds up to 2656.7 us (6.32 GB/s, 1.69x), 2959.3 us (5.67 GB/s, 1.54x), 309.0 us (13.57 GB/s, 2.08x), and 539.4 us (15.38 GB/s, 3.40x via tail-run peeling); check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239400492 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1901#issuecomment-7239400492). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:41:27Z. --- #1901 | head 7c06770ce3bd83861caa7cec238ed5413c730ecd (merged cleanly with main d48a957bb9988afce424552a3c0c8d01ef12320f) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(console_texture_layout|resource_detile|d3d12_mapped_plane_async)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) + `console_texture_layout_test --bench` A/B on MSVC `RelWithDebInfo` | baseline on main: `2048x2048 RGBA8 64KB_S` `4487.3 us` (`3.74 GB/s`), `64KB_D` `4562.6 us` (`3.68 GB/s`), `BC7 64KB_S` `642.9 us` (`6.52 GB/s`), `1920x1080 RGBA8 64KB_S` `1831.7 us` (`4.53 GB/s`) | green on PR+main: 3/3 CTests pass (`console_texture_layout` exhaustive oracle + error paths, `resource_detile`, and `d3d12_mapped_plane_async`) on RTX 3090 with `0` Khronos validation errors, and `--bench` speeds up to `2656.7 us` (`6.32 GB/s`, **1.69x**), `2959.3 us` (`5.67 GB/s`, **1.54x**), `309.0 us` (`13.57 GB/s`, **2.08x**), and `539.4 us` (`15.38 GB/s`, **3.40x** via tail-run peeling); `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
OreoFR commented 2026-11-29 02:41:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T02:41:07Z.


Verified by OreoFR

  • Base: main aa918e5d9 + PR head 7c06770ce
  • Test: ctest -R ^console_texture_layout$
  • Red on main: passes (the new test covers behaviour already on main)
  • Green with the PR: passes; package builds, full ctest shows no new failure
  • Verdict: NO RED (optimization; the new test also passes on main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

Note: shares runtime/CMakeLists.txt with the open #2566; a rebase may be needed if #2566 lands first.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249582690 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1901#issuecomment-7249582690). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T02:41:07Z. --- **Verified by OreoFR** - Base: main `aa918e5d9` + PR head `7c06770ce` - Test: `ctest -R ^console_texture_layout$` - Red on main: passes (the new test covers behaviour already on main) - Green with the PR: passes; package builds, full ctest shows no new failure - Verdict: NO RED (optimization; the new test also passes on main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 Note: shares `runtime/CMakeLists.txt` with the open #2566; a rebase may be needed if #2566 lands first. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:38:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:38:46Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317955122 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1901#issuecomment-7317955122). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:38:46Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1901
No description provided.