Skip to content

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

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-console-tex-fast-runs
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-console-tex-fast-runs

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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.

…-width detile/tile dispatch (RT-CONSOLE-TEX-FAST-RUNS)
Copilot AI balanced review requested due to automatic review settings November 25, 2026 15:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#1901 | head 7c06770 (merged cleanly with main d48a957) | 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

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants