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

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1989 into main
IlyassRS3 commented 2026-11-25 17:32:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T17:32:53Z.


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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496091650 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1989). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T17:32:53Z. --- 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)
copilot-pull-request-reviewer[bot] commented 2026-11-25 17:33:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T17:33:07Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062133163 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1989#pullrequestreview-6062133163). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T17:33:07Z. **Review:** COMMENTED --- 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 12:50:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:50:49Z.


Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 5564eccb03 (claim #2570#issuecomment-7239425776):

  • Line-by-line diff audit: Clean (runtime/CMakeLists.txt, runtime/src/dstorage_swizzle.h, runtime/test/dstorage_swizzle_test.cpp); writes unshuffled DirectStorage output groups sequentially in a single pass instead of striding across destination planes, handles empty spans cleanly, and rejects overlapping input/output spans before std::memcpy. check_no_game_data.py clean.
  • Red (d48a957bb + PR test): dstorage_swizzle_test.exe fails (rc=1, line 91: !unshuffle(2, std::span(buf).subspan(0, 32768), std::span(buf).subspan(0, 32768))).
  • Green (d48a957bb + 5564eccb03 merge): Auto-merges cleanly (rc=0), compiles with 0 warnings under /W4 /permissive-, and passes both dstorage_swizzle_test.exe (dstorage_swizzle_test: all checks passed, rc=0) and full dstorage_test.exe (rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239507739 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1989#issuecomment-7239507739). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:50:49Z. --- Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `d48a957bb` + `5564eccb03` (claim `#2570#issuecomment-7239425776`): - **Line-by-line diff audit**: Clean (`runtime/CMakeLists.txt`, `runtime/src/dstorage_swizzle.h`, `runtime/test/dstorage_swizzle_test.cpp`); writes unshuffled DirectStorage output groups sequentially in a single pass instead of striding across destination planes, handles empty spans cleanly, and rejects overlapping input/output spans before `std::memcpy`. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: `dstorage_swizzle_test.exe` fails (`rc=1`, `line 91: !unshuffle(2, std::span(buf).subspan(0, 32768), std::span(buf).subspan(0, 32768))`). - **Green (`d48a957bb` + `5564eccb03` merge)**: Auto-merges cleanly (`rc=0`), compiles with `0` warnings under `/W4 /permissive-`, and passes both `dstorage_swizzle_test.exe` (`dstorage_swizzle_test: all checks passed`, `rc=0`) and full `dstorage_test.exe` (`rc=0`). AI-assisted.
OreoFR commented 2026-11-29 02:40:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T02:40:10Z.


Verified by OreoFR

  • Base: main aa918e5d9 + PR head 5564eccb0
  • Test: ctest -R ^dstorage_swizzle$
  • Red on main: fails at line 91: unshuffle accepts the same span as input and output
  • Green with the PR: passes; package builds, full ctest shows no new failure
  • Verdict: READY
  • 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249572584 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1989#issuecomment-7249572584). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T02:40:10Z. --- **Verified by OreoFR** - Base: main `aa918e5d9` + PR head `5564eccb0` - Test: `ctest -R ^dstorage_swizzle$` - Red on main: fails at line 91: `unshuffle` accepts the same span as input and output - Green with the PR: passes; package builds, full ctest shows no new failure - Verdict: READY - 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).
aslan2142 commented 2026-12-01 16:40:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:40:50Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318022096 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1989#issuecomment-7318022096). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:40:50Z. --- 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1989
No description provided.