[PR #146] resource_detile: strip-mined cache-line micro-tile copies, 1.2x-14.6x faster, bit-identical (PERF-RESOURCE-DETILE) #198

Open
opened 2026-11-26 20:17:06 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/146
Author: @Andretuta
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: old-pr/9879


📝 Commits (1)

  • 8df2051 resource_detile: strip-mined cache-line micro-tile copies (1.2x-14.6x faster, bit-identical)

📊 Changes

2 files changed (+330 additions, -43 deletions)

View changed files

📝 runtime/src/resource_detile.h (+235 -43)
📝 runtime/test/resource_detile_test.cpp (+95 -0)

📄 Description

Carried over from old PR 9879 by ChalanaGimhanaX in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9879, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
ChalanaGimhanaX: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Claimed in the coordination issue as: https://github.com/ProjectLeonida/Leonida/issues/9677#issuecomment-7116739617

(Resubmission of #9399, which was closed during the repo transfer cleanup after 6 independent verification passes; cleanly rebased onto ProjectLeonida/Leonida main 7e39a81ea and re-verified.)

Summary

detile_surface_64k() and tile_surface_64k() in runtime/src/resource_detile.h previously copied one element at a time in row-major order across all 64 KiB macro-tiles, recomputing x / tile_w, x % tile_w, lut_x[...] and tx * 65536 per element and touching each tiled cache line repeatedly across separate scanlines.

Every GFX10 SW_64KB_D bit pattern (Mesa addrlib GFX10_SW_64K_D_RBPLUS_PATINFO) maps an aligned micro-tile directly into contiguous 64-byte cache lines in tiled memory:

BPE Micro-tile Tiled footprint Row transfer width
1 B 8x8 64 B (1 cache line; rows 0,2,1,3,4,6,5,7) 8 B
2 B 8x4 64 B (1 cache line; 4 contiguous rows) 16 B
4 B 4x4 64 B (1 cache line; 4 contiguous rows) 16 B
8 B 4x2 64 B (1 cache line; two 2×2 halves of 16 B) 16 B
16 B 4x4 256 B (4 consecutive cache lines; 4 × 64 B linear rows) 32 B

Sweeping y in strips of micro_h rows and streaming x sequentially across macro-tiles touches each tiled cache line completely in one pass, keeps linear memory streaming sequentially (avoiding power-of-two linear-stride L1 set conflicts), and cuts LUT lookups by 8x–64x — with 100% bit-identical output and pure portable ISO C++20 (no intrinsics or compiler extensions).

Changes

  • runtime/src/resource_detile.h:
    • Replace the separate scalar detile_typed() / tile_typed() loops with a shared copy_surface_64k_typed<Bpe, ToTiled>() implementation using strip-mined 64 B / 256 B micro-tile transfers plus overflow-safe right-edge and bottom-edge tails.
    • Add constexpr bool verify_microtile_layout_64k<Bpe>() and static_assert proofs for all five BPEs (1, 2, 4, 8, 16), proving at compile time against SwizzleLut64K<Bpe> that X and Y bit masks are disjoint across the full 64 KiB tile and every micro-tile / contiguous-run offset matches the scalar LUT exactly.
  • runtime/test/resource_detile_test.cpp:
    • Add test_exact_against_scalar<Bpe>() and test_all_edge_remainders<Bpe>() comparing both tile_surface_64k() and detile_surface_64k() byte-for-byte against an independent scalar SwizzleLut64K<Bpe> reference across all 5 BPEs, all edge remainders modulo micro-tile and 64 KiB tile dimensions, and BCn (block_dim = 4) surfaces, while verifying all inactive 0xCC padding bytes stay untouched.

Benchmark (Windows 11 x64, MSVC 19.50.35721 /O2, median of 5 trials, bit-identical verified)

Surface Size detile_surface_64k (before -> after) tile_surface_64k (before -> after)
512x512 R8 (1B) 0.25 MiB 0.272 -> 0.022 ms (12.47x) 0.249 -> 0.017 ms (14.64x)
512x512 R16 (2B) 0.50 MiB 0.235 -> 0.033 ms (7.06x) 0.251 -> 0.031 ms (8.12x)
512x512 RGBA8 (4B) 1.00 MiB 0.312 -> 0.074 ms (4.22x) 0.356 -> 0.075 ms (4.73x)
1024x1024 BC1 (8B) 0.50 MiB 0.071 -> 0.033 ms (2.16x) 0.071 -> 0.024 ms (3.01x)
1024x1024 BC7 (16B) 1.00 MiB 0.088 -> 0.075 ms (1.16x) 0.096 -> 0.051 ms (1.87x)
1920x1080 RGBA8 7.91 MiB 4.114 -> 2.176 ms (1.89x) 3.290 -> 2.081 ms (1.58x)
4096x4096 R8 (1B) 16.00 MiB 27.133 -> 6.574 ms (4.13x) 17.464 -> 5.048 ms (3.46x)
4096x4096 R16 (2B) 32.00 MiB 29.900 -> 13.476 ms (2.22x) 20.299 -> 10.261 ms (1.98x)
4096x4096 RGBA8 64.00 MiB 34.829 -> 17.721 ms (1.97x) 25.407 -> 16.901 ms (1.50x)
4096x4096 BC1 (8B) 8.00 MiB 3.508 -> 2.733 ms (1.28x) 3.622 -> 2.645 ms (1.37x)
4096x4096 BC7 (16B) 16.00 MiB 8.106 -> 6.164 ms (1.32x) 7.126 -> 4.969 ms (1.43x)

Verification (rebased onto 7e39a81ea)

  • MSVC 19.50.35721 (VS 18 Community, Release x64):
    • cmake --build runtime/build --config Release --target resource_detile_test -> clean build (all 5 static_assert layout proofs pass).
    • ctest --test-dir runtime/build -C Release -R ^resource_detile$ --output-on-failure -V -> 1/1 passed (0.19 sec).
    • /std:c++20 /W4 /WX /permissive- /utf-8 -> 0 warnings, 0 errors.
  • GCC 15.2.0: -std=c++20 -O2 -Wall -Wextra -Werror -Wpedantic -> 0 warnings, 0 errors, test passed.
  • Hygiene & clean-room:
    • python -I scripts/check_no_game_data.py runtime/src/resource_detile.h runtime/test/resource_detile_test.cpp -> exit 0.
    • git diff origin/main..HEAD --check -> exit 0; 0 CR (\r) bytes.

Clean-room & disclosure

  • Synthetic buffers only; no game data, keys, decryption, or online endpoints.
  • AI-assisted: yes

🔄 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/146 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `old-pr/9879` --- ### 📝 Commits (1) - [`8df2051`](https://github.com/ubiquitarianism/leonida/commit/8df205109c2c770b3c187ad99890d547aa4c4ea2) resource_detile: strip-mined cache-line micro-tile copies (1.2x-14.6x faster, bit-identical) ### 📊 Changes **2 files changed** (+330 additions, -43 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/resource_detile.h` (+235 -43) 📝 `runtime/test/resource_detile_test.cpp` (+95 -0) </details> ### 📄 Description > Carried over from **old PR 9879** by `ChalanaGimhanaX` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9879`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `ChalanaGimhanaX`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Claimed in the coordination issue as: https://github.com/ProjectLeonida/Leonida/issues/9677#issuecomment-7116739617 *(Resubmission of `#9399`, which was closed during the repo transfer cleanup after 6 independent verification passes; cleanly rebased onto `ProjectLeonida/Leonida` `main` `7e39a81ea` and re-verified.)* ## Summary `detile_surface_64k()` and `tile_surface_64k()` in `runtime/src/resource_detile.h` previously copied one element at a time in row-major order across all 64 KiB macro-tiles, recomputing `x / tile_w`, `x % tile_w`, `lut_x[...]` and `tx * 65536` per element and touching each tiled cache line repeatedly across separate scanlines. Every GFX10 `SW_64KB_D` bit pattern (`Mesa addrlib GFX10_SW_64K_D_RBPLUS_PATINFO`) maps an aligned micro-tile directly into contiguous 64-byte cache lines in tiled memory: | BPE | Micro-tile | Tiled footprint | Row transfer width | | --- | --- | --- | --- | | 1 B | `8x8` | 64 B (1 cache line; rows `0,2,1,3,4,6,5,7`) | 8 B | | 2 B | `8x4` | 64 B (1 cache line; 4 contiguous rows) | 16 B | | 4 B | `4x4` | 64 B (1 cache line; 4 contiguous rows) | 16 B | | 8 B | `4x2` | 64 B (1 cache line; two 2×2 halves of 16 B) | 16 B | | 16 B | `4x4` | 256 B (4 consecutive cache lines; 4 × 64 B linear rows) | 32 B | Sweeping `y` in strips of `micro_h` rows and streaming `x` sequentially across macro-tiles touches each tiled cache line completely in one pass, keeps linear memory streaming sequentially (avoiding power-of-two linear-stride L1 set conflicts), and cuts LUT lookups by 8x–64x — with **100% bit-identical output** and pure portable ISO C++20 (no intrinsics or compiler extensions). ## Changes - **`runtime/src/resource_detile.h`**: - Replace the separate scalar `detile_typed()` / `tile_typed()` loops with a shared `copy_surface_64k_typed<Bpe, ToTiled>()` implementation using strip-mined 64 B / 256 B micro-tile transfers plus overflow-safe right-edge and bottom-edge tails. - Add `constexpr bool verify_microtile_layout_64k<Bpe>()` and `static_assert` proofs for all five BPEs (`1, 2, 4, 8, 16`), proving at compile time against `SwizzleLut64K<Bpe>` that X and Y bit masks are disjoint across the full 64 KiB tile and every micro-tile / contiguous-run offset matches the scalar LUT exactly. - **`runtime/test/resource_detile_test.cpp`**: - Add `test_exact_against_scalar<Bpe>()` and `test_all_edge_remainders<Bpe>()` comparing both `tile_surface_64k()` and `detile_surface_64k()` byte-for-byte against an independent scalar `SwizzleLut64K<Bpe>` reference across all 5 BPEs, all edge remainders modulo micro-tile and 64 KiB tile dimensions, and BCn (`block_dim = 4`) surfaces, while verifying all inactive `0xCC` padding bytes stay untouched. ## Benchmark (Windows 11 x64, MSVC 19.50.35721 `/O2`, median of 5 trials, bit-identical verified) | Surface | Size | `detile_surface_64k` (before -> after) | `tile_surface_64k` (before -> after) | | --- | --- | --- | --- | | `512x512 R8 (1B)` | `0.25 MiB` | `0.272 -> 0.022 ms` (**12.47x**) | `0.249 -> 0.017 ms` (**14.64x**) | | `512x512 R16 (2B)` | `0.50 MiB` | `0.235 -> 0.033 ms` (**7.06x**) | `0.251 -> 0.031 ms` (**8.12x**) | | `512x512 RGBA8 (4B)` | `1.00 MiB` | `0.312 -> 0.074 ms` (**4.22x**) | `0.356 -> 0.075 ms` (**4.73x**) | | `1024x1024 BC1 (8B)` | `0.50 MiB` | `0.071 -> 0.033 ms` (**2.16x**) | `0.071 -> 0.024 ms` (**3.01x**) | | `1024x1024 BC7 (16B)` | `1.00 MiB` | `0.088 -> 0.075 ms` (**1.16x**) | `0.096 -> 0.051 ms` (**1.87x**) | | `1920x1080 RGBA8` | `7.91 MiB` | `4.114 -> 2.176 ms` (**1.89x**) | `3.290 -> 2.081 ms` (**1.58x**) | | `4096x4096 R8 (1B)` | `16.00 MiB` | `27.133 -> 6.574 ms` (**4.13x**) | `17.464 -> 5.048 ms` (**3.46x**) | | `4096x4096 R16 (2B)` | `32.00 MiB` | `29.900 -> 13.476 ms` (**2.22x**) | `20.299 -> 10.261 ms` (**1.98x**) | | `4096x4096 RGBA8` | `64.00 MiB` | `34.829 -> 17.721 ms` (**1.97x**) | `25.407 -> 16.901 ms` (**1.50x**) | | `4096x4096 BC1 (8B)` | `8.00 MiB` | `3.508 -> 2.733 ms` (**1.28x**) | `3.622 -> 2.645 ms` (**1.37x**) | | `4096x4096 BC7 (16B)` | `16.00 MiB` | `8.106 -> 6.164 ms` (**1.32x**) | `7.126 -> 4.969 ms` (**1.43x**) | ## Verification (rebased onto `7e39a81ea`) - **MSVC 19.50.35721 (VS 18 Community, Release x64)**: - `cmake --build runtime/build --config Release --target resource_detile_test` -> clean build (all 5 `static_assert` layout proofs pass). - `ctest --test-dir runtime/build -C Release -R ^resource_detile$ --output-on-failure -V` -> **1/1 passed** (`0.19 sec`). - `/std:c++20 /W4 /WX /permissive- /utf-8` -> **0 warnings, 0 errors**. - **GCC 15.2.0**: `-std=c++20 -O2 -Wall -Wextra -Werror -Wpedantic` -> **0 warnings, 0 errors**, test passed. - **Hygiene & clean-room**: - `python -I scripts/check_no_game_data.py runtime/src/resource_detile.h runtime/test/resource_detile_test.cpp` -> exit `0`. - `git diff origin/main..HEAD --check` -> exit `0`; `0` CR (`\r`) bytes. ## Clean-room & disclosure - Synthetic buffers only; no game data, keys, decryption, or online endpoints. - AI-assisted: yes --- <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#198
No description provided.