runtime: unshuffle DirectStorage stripes in a single sequential output pass (RT-DSTORAGE-SWIZZLE-LINEAR-WRITE-AND-TESTS) #1989

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

@⁠IlyassRS3 on GitHub · 2026-11-25 17:32 UTC · original
IlyassRS3:perf/rt-dstorage-swizzle-linear-write → main · +135 −7 · 3 files · 1 commits · open

Item ID: RT-DSTORAGE-SWIZZLE-LINEAR-WRITE-AND-TESTS (claimed in #1608)

Summary

Optimizes and hardens leonida::dstorage::unshuffle() (runtime/src/dstorage_swizzle.h), used by the DirectStorage shim (runtime/shims/dstorage.cpp:1150) to macro-unshuffle swizzled BC texture streams:

  1. Single-pass sequential destination writes: previously looped outer-by-stripe and inner-by-group, writing to out with 16 KiB / 32 KiB strides across the full multi-megabyte destination buffer 3–5 separate times (thrashing cache/TLB and breaking write-combining). Inverting the loop to outer-by-group / inner-by-stripe emits each destination group contiguously from start to end in one linear pass while reading 3–5 sequential plane streams.
  2. Overlap & empty-span guards: rejects overlapping input/out spans cleanly (overflow-free pointer distance check) instead of invoking std::memcpy UB, and handles empty spans in O(1).
  3. New portable pure-CPU unit test runtime/test/dstorage_swizzle_test.cpp wired into runtime/CMakeLists.txt (dstorage_swizzle), verifying byte-exact parity against the stripe-major reference across all 4 modes and multiple group counts, empty inputs, invalid modes/sizes, overlapping span rejection, and adjacent non-overlapping slices.

Benchmarks (-O2, 32 MiB texture, 16-iteration average, bit-exact A/B vs upstream/main)

  • Mode 1 (4K/4K/8K, 16 KiB group): 3.512 ms → 0.694 ms (5.06× faster)
  • Mode 2 (4K/4K/24K, 32 KiB group): 0.838 ms → 0.839 ms (1.00×, parity)
  • Mode 3 (4K/12K/4K/4K/8K, 32 KiB group): 1.642 ms → 0.591 ms (2.78× faster)
  • Mode 4 (4K/12K/4K/12K, 32 KiB group): 1.394 ms → 0.554 ms (2.52× faster)

Testing & Disclosure

  • AI-assisted: yes
  • GPUs tested: none (macOS host, CPU-only portable unit test and A/B benchmark)
  • clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/dstorage_swizzle_test.cpp: PASS
  • clang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/dstorage_swizzle_test.cpp: PASS
  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror runtime/test/dstorage_swizzle_test.cpp: PASS
  • python3 -I scripts/check_no_game_data.py runtime/src/dstorage_swizzle.h runtime/test/dstorage_swizzle_test.cpp runtime/CMakeLists.txt: PASS
  • python3 -I tests/test_text_write_line_endings.py: PASS (3/3)
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1989) `IlyassRS3:perf/rt-dstorage-swizzle-linear-write` → `main` · +135 −7 · 3 files · 1 commits · open Item ID: `RT-DSTORAGE-SWIZZLE-LINEAR-WRITE-AND-TESTS` (claimed in #1608) ## Summary Optimizes and hardens `leonida::dstorage::unshuffle()` (`runtime/src/dstorage_swizzle.h`), used by the DirectStorage shim (`runtime/shims/dstorage.cpp:1150`) to macro-unshuffle swizzled BC texture streams: 1. **Single-pass sequential destination writes**: previously looped outer-by-stripe and inner-by-group, writing to `out` with 16 KiB / 32 KiB strides across the full multi-megabyte destination buffer 3–5 separate times (thrashing cache/TLB and breaking write-combining). Inverting the loop to outer-by-group / inner-by-stripe emits each destination group contiguously from start to end in one linear pass while reading 3–5 sequential plane streams. 2. **Overlap & empty-span guards**: rejects overlapping `input`/`out` spans cleanly (overflow-free pointer distance check) instead of invoking `std::memcpy` UB, and handles empty spans in O(1). 3. **New portable pure-CPU unit test** `runtime/test/dstorage_swizzle_test.cpp` wired into `runtime/CMakeLists.txt` (`dstorage_swizzle`), verifying byte-exact parity against the stripe-major reference across all 4 modes and multiple group counts, empty inputs, invalid modes/sizes, overlapping span rejection, and adjacent non-overlapping slices. ## Benchmarks (`-O2`, 32 MiB texture, 16-iteration average, bit-exact A/B vs `upstream/main`) - Mode 1 (4K/4K/8K, 16 KiB group): 3.512 ms → **0.694 ms** (**5.06×** faster) - Mode 2 (4K/4K/24K, 32 KiB group): 0.838 ms → **0.839 ms** (1.00×, parity) - Mode 3 (4K/12K/4K/4K/8K, 32 KiB group): 1.642 ms → **0.591 ms** (**2.78×** faster) - Mode 4 (4K/12K/4K/12K, 32 KiB group): 1.394 ms → **0.554 ms** (**2.52×** faster) ## Testing & Disclosure - AI-assisted: yes - GPUs tested: none (macOS host, CPU-only portable unit test and A/B benchmark) - `clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/dstorage_swizzle_test.cpp`: PASS - `clang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/dstorage_swizzle_test.cpp`: PASS - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror runtime/test/dstorage_swizzle_test.cpp`: PASS - `python3 -I scripts/check_no_game_data.py runtime/src/dstorage_swizzle.h runtime/test/dstorage_swizzle_test.cpp runtime/CMakeLists.txt`: PASS - `python3 -I tests/test_text_write_line_endings.py`: PASS (3/3)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:03 +00:00
Author
Owner

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

Merge

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