perf(resource_detile): compile-time contiguous X micro-runs and macro-tile blocking for 64 KiB swizzle #1965

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

@⁠IlyassRS3 on GitHub · 2026-11-25 16:55 UTC · original
IlyassRS3:perf/rt-resource-detile-fast-runs → main · +170 −41 · 2 files · 1 commits · open

Claim: RT-RESOURCE-DETILE-FAST-RUNS (claim comment #7168674568 on #1608).

Optimization (runtime/src/resource_detile.h)

Previously detile_typed<Bpe> and tile_typed<Bpe> copied a single element (1/2/4/8/16 bytes) per iteration with x / tile_w, x % tile_w, and lut_x lookup on every element while walking row-major across all tiles_x macro-tiles.

  1. Compile-time contiguous X micro-runs (SwizzleLut64K<Bpe>::lut_run_x):
    • In GFX10_SW_64K_D_RBPLUS_PATINFO, the lowest X coordinate bits form contiguous horizontal runs inside each 64 KiB tile (8×1B = 8B, 8×2B = 16B, 4×4B = 16B, 2×8B = 16B, 2×16B = 32B).
    • Added compile-time lut_run_x and static_assert(runs_valid()) proving for every Bpe in {1, 2, 4, 8, 16} that every micro-run is contiguous, aligned to run_bytes, and bit-disjoint from lut_y[y] (so tile_row + lut_run_x[r] replaces | without per-element division/modulo).
  2. Macro-tile blocking + exact-length contiguous tail copy (transfer_typed<Bpe, ToTiled>):
    • Processes one 64 KiB macro-tile at a time so the swizzled side stays within a single 64 KiB cache footprint.
    • Copies full micro-runs with compile-time constant run_bytes (8/16/32 bytes) and handles any partial last macro-tile remainder with a single exact-length contiguous tail memcpy (never writing past active bounds).
    • Consolidates duplicated tile/detile loops into one template.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files; 0 CMakeLists.txt changes).
  • Expanded runtime/test/resource_detile_test.cpp with test_oracle_exact<Bpe>() verifying bit-exact match against the scalar per-element LUT oracle and untouched 0xA5 padding bytes across every tail remainder (0..run_elems-1) and macro-tile boundary (tile_w-1, tile_w, tile_w+1, multi-tile, and BC1/BC7).
  • Ran resource_detile_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Bit-exact A/B benchmark at -O2 vs main (best of 5 runs, Apple Silicon):
R8_1024x1024       tile:   424.9 ->   34.3 us (12.41x) | detile:   421.0 ->   33.8 us (12.46x)
R16_1024x1024      tile:   375.0 ->   48.0 us ( 7.81x) | detile:   384.0 ->   48.2 us ( 7.96x)
RGBA8_1080p        tile:   749.6 ->  191.2 us ( 3.92x) | detile:   762.0 ->  245.5 us ( 3.10x)
RGBA16F_1024x768   tile:   318.2 ->  147.8 us ( 2.15x) | detile:   290.7 ->  137.0 us ( 2.12x)
BC1_2048x2048      tile:    96.6 ->   48.9 us ( 1.97x) | detile:    95.5 ->   47.1 us ( 2.03x)
BC7_2048x1024      tile:    75.1 ->   48.3 us ( 1.56x) | detile:    54.3 ->   43.9 us ( 1.24x)
BC7_1920x1080      tile:    67.7 ->   42.4 us ( 1.60x) | detile:    55.1 ->   41.6 us ( 1.32x)
  • Ran python3 -I scripts/check_no_game_data.py on both files and python3 -I tests/test_text_write_line_endings.py: all passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 16:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1965) `IlyassRS3:perf/rt-resource-detile-fast-runs` → `main` · +170 −41 · 2 files · 1 commits · open Claim: `RT-RESOURCE-DETILE-FAST-RUNS` (claim comment `#7168674568` on #1608). ### Optimization (`runtime/src/resource_detile.h`) Previously `detile_typed<Bpe>` and `tile_typed<Bpe>` copied a single element (`1/2/4/8/16` bytes) per iteration with `x / tile_w`, `x % tile_w`, and `lut_x` lookup on every element while walking row-major across all `tiles_x` macro-tiles. 1. **Compile-time contiguous X micro-runs (`SwizzleLut64K<Bpe>::lut_run_x`)**: - In `GFX10_SW_64K_D_RBPLUS_PATINFO`, the lowest X coordinate bits form contiguous horizontal runs inside each 64 KiB tile (`8×1B = 8B`, `8×2B = 16B`, `4×4B = 16B`, `2×8B = 16B`, `2×16B = 32B`). - Added compile-time `lut_run_x` and `static_assert(runs_valid())` proving for every `Bpe` in `{1, 2, 4, 8, 16}` that every micro-run is contiguous, aligned to `run_bytes`, and bit-disjoint from `lut_y[y]` (so `tile_row + lut_run_x[r]` replaces `|` without per-element division/modulo). 2. **Macro-tile blocking + exact-length contiguous tail copy (`transfer_typed<Bpe, ToTiled>`)**: - Processes one 64 KiB macro-tile at a time so the swizzled side stays within a single 64 KiB cache footprint. - Copies full micro-runs with compile-time constant `run_bytes` (`8/16/32` bytes) and handles any partial last macro-tile remainder with a single exact-length contiguous tail `memcpy` (never writing past active bounds). - Consolidates duplicated tile/detile loops into one template. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (host is macOS arm64; no retail files; 0 `CMakeLists.txt` changes). - Expanded `runtime/test/resource_detile_test.cpp` with `test_oracle_exact<Bpe>()` verifying bit-exact match against the scalar per-element LUT oracle and untouched `0xA5` padding bytes across every tail remainder (`0..run_elems-1`) and macro-tile boundary (`tile_w-1`, `tile_w`, `tile_w+1`, multi-tile, and BC1/BC7). - Ran `resource_detile_test` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Bit-exact A/B benchmark at `-O2` vs `main` (best of 5 runs, Apple Silicon): ```text R8_1024x1024 tile: 424.9 -> 34.3 us (12.41x) | detile: 421.0 -> 33.8 us (12.46x) R16_1024x1024 tile: 375.0 -> 48.0 us ( 7.81x) | detile: 384.0 -> 48.2 us ( 7.96x) RGBA8_1080p tile: 749.6 -> 191.2 us ( 3.92x) | detile: 762.0 -> 245.5 us ( 3.10x) RGBA16F_1024x768 tile: 318.2 -> 147.8 us ( 2.15x) | detile: 290.7 -> 137.0 us ( 2.12x) BC1_2048x2048 tile: 96.6 -> 48.9 us ( 1.97x) | detile: 95.5 -> 47.1 us ( 2.03x) BC7_2048x1024 tile: 75.1 -> 48.3 us ( 1.56x) | detile: 54.3 -> 43.9 us ( 1.24x) BC7_1920x1080 tile: 67.7 -> 42.4 us ( 1.60x) | detile: 55.1 -> 41.6 us ( 1.32x) ``` - Ran `python3 -I scripts/check_no_game_data.py` on both files and `python3 -I tests/test_text_write_line_endings.py`: all passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:13:01 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 16:55 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 16:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1965#pullrequestreview-6061588298) · **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/1965/head:github-bot/gh-1965'
git switch 'github-bot/gh-1965'

Merge

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