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

Closed
leonida/mandy
quietfold wants to merge 0 commits from perf/rt-resource-detile-fast-runs into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 16:55:09 +00:00

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.
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.
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 16:55:25 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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 21:24:27 +00:00

Independent Verification Report — d4628fc665 + origin/main (aa918e5d9f)

Environment: Windows 11 (26200), AMD Ryzen 9 5950X, MSVC 19.44.35228 (x64, /std:c++20 /EHsc /utf-8, Debug /Od /MDd & Release /O2 /MD).
Verdict: PASS (Ready to merge; 1 minor /W4 nit + 8/16 Bpe write-combining observation noted below) — clean merge with origin/main (469f7f75fbf0) and combined Batch 9 set (a422ed80f598).

1. Build & Correctness Verification

  • Compile-time static_assert(swizzle_lut_64k_{1,2,4,8,16}bpe.runs_valid()) verified across all five element widths (run_bytes = 8, 16, 16, 16, 32 B).
  • resource_detile_test (Debug /Od /MDd & Release /O2 /MD): PASS — all bijective LUT checks, round-trips, and scalar-oracle bit-exactness + untouched-padding checks across unaligned widths and block-compressed surfaces (block_dim = 4) pass.

2. Measured Throughput on Ryzen 9 5950X (1024x1024 SW_64KB_S_X, 60 iterations, MSVC 19.44 /O2 /MD)

Bpe origin/main detile #1965 detile detile Speedup origin/main tile #1965 tile tile Speedup
1 1.51 GB/s 22.59 GB/s 15.0x 1.77 GB/s 25.97 GB/s 14.7x
2 2.91 GB/s 30.31 GB/s 10.4x 2.95 GB/s 30.32 GB/s 10.3x
4 5.73 GB/s 23.36 GB/s 4.1x 5.45 GB/s 24.35 GB/s 4.5x
8 7.70 GB/s 13.84 GB/s 1.8x 9.41 GB/s 5.35 GB/s 0.57x
16 2.79 GB/s 4.25 GB/s 1.5x 6.44 GB/s 4.65 GB/s 0.72x

3. Non-Blocking Notes

  • Minor /W4 nit (resource_detile.h:106): Under explicit /W4, MSVC emits C4127: conditional expression is constant on if (tile_w % run_elems != 0) return false; inside runs_valid(); if constexpr (tile_w % run_elems != 0) silences it (CMakeLists.txt builds resource_detile_test at default warning level with 0 warnings).
  • Perf note on ToTiled = true at 8/16 Bpe: detile improves across every Bpe (1.5x–15.0x) and tile jumps 4.5x–14.7x on 1/2/4 Bpe; on 8/16 Bpe (1024x1024 = 8–16 MiB), walking linear source rows with tile_w-wide strides (1 KiB at 8 Bpe / 16 Bpe) touches y_end (64) 4 KiB pages per macro-tile column on the linear read side, slightly trailing linear-row traversal on tile (9.41 -> 5.35 GB/s at 8 Bpe). Optional future follow-up: 8x8 or 16x16 sub-blocking within the 64 KiB macro-tile for ToTiled when linear_stride >= 4096.

AI-assisted.

### Independent Verification Report — `d4628fc665` + `origin/main` (`aa918e5d9f`) **Environment:** Windows 11 (`26200`), AMD Ryzen 9 5950X, MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /utf-8`, Debug `/Od /MDd` & Release `/O2 /MD`). **Verdict:** **PASS (Ready to merge; 1 minor `/W4` nit + `8/16 Bpe` write-combining observation noted below)** — clean merge with `origin/main` (`469f7f75fbf0`) and combined Batch 9 set (`a422ed80f598`). #### 1. Build & Correctness Verification - Compile-time `static_assert(swizzle_lut_64k_{1,2,4,8,16}bpe.runs_valid())` verified across all five element widths (`run_bytes` = `8, 16, 16, 16, 32` B). - `resource_detile_test` (Debug `/Od /MDd` & Release `/O2 /MD`): **PASS** — all bijective LUT checks, round-trips, and scalar-oracle bit-exactness + untouched-padding checks across unaligned widths and block-compressed surfaces (`block_dim = 4`) pass. #### 2. Measured Throughput on Ryzen 9 5950X (`1024x1024` `SW_64KB_S_X`, 60 iterations, MSVC `19.44` `/O2 /MD`) | `Bpe` | `origin/main` `detile` | `#1965` `detile` | `detile` Speedup | `origin/main` `tile` | `#1965` `tile` | `tile` Speedup | |---:|---:|---:|---:|---:|---:|---:| | `1` | `1.51 GB/s` | `22.59 GB/s` | **15.0x** | `1.77 GB/s` | `25.97 GB/s` | **14.7x** | | `2` | `2.91 GB/s` | `30.31 GB/s` | **10.4x** | `2.95 GB/s` | `30.32 GB/s` | **10.3x** | | `4` | `5.73 GB/s` | `23.36 GB/s` | **4.1x** | `5.45 GB/s` | `24.35 GB/s` | **4.5x** | | `8` | `7.70 GB/s` | `13.84 GB/s` | **1.8x** | `9.41 GB/s` | `5.35 GB/s` | `0.57x` | | `16` | `2.79 GB/s` | `4.25 GB/s` | **1.5x** | `6.44 GB/s` | `4.65 GB/s` | `0.72x` | #### 3. Non-Blocking Notes - **Minor `/W4` nit (`resource_detile.h:106`):** Under explicit `/W4`, MSVC emits `C4127: conditional expression is constant` on `if (tile_w % run_elems != 0) return false;` inside `runs_valid()`; `if constexpr (tile_w % run_elems != 0)` silences it (`CMakeLists.txt` builds `resource_detile_test` at default warning level with `0` warnings). - **Perf note on `ToTiled = true` at `8/16 Bpe`:** `detile` improves across every `Bpe` (`1.5x`–`15.0x`) and `tile` jumps `4.5x`–`14.7x` on `1/2/4 Bpe`; on `8/16 Bpe` (`1024x1024` = `8`–`16 MiB`), walking linear source rows with `tile_w`-wide strides (`1 KiB` at `8 Bpe` / `16 Bpe`) touches `y_end` (`64`) 4 KiB pages per macro-tile column on the linear read side, slightly trailing linear-row traversal on `tile` (`9.41 -> 5.35 GB/s` at `8 Bpe`). Optional future follow-up: 8x8 or 16x16 sub-blocking within the 64 KiB macro-tile for `ToTiled` when `linear_stride >= 4096`. AI-assisted.
OreoFR commented 2026-11-28 21:57:03 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head d4628fc66; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^resource_detile$
  • Red on main: OK
  • Green with the PR: OK
  • Verdict: NO RED (performance PR: the new equivalence tests also pass on main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `d4628fc66`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^resource_detile$` - Red on main: OK - Green with the PR: OK - Verdict: NO RED (performance PR: the new equivalence tests also pass on main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:40:13 +00:00

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.

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.
Sign in to join this conversation.
No description provided.