Skip to content

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

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-dstorage-swizzle-linear-write
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-dstorage-swizzle-linear-write

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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 AI balanced review requested due to automatic review settings November 25, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

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

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants