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

Open
opened 2026-11-26 21:19:41 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1989
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-dstorage-swizzle-linear-write


📝 Commits (1)

  • 5564ecc runtime: unshuffle DirectStorage stripes in a single sequential output pass

📊 Changes

3 files changed (+135 additions, -7 deletions)

View changed files

📝 runtime/CMakeLists.txt (+3 -0)
📝 runtime/src/dstorage_swizzle.h (+26 -7)
➕ runtime/test/dstorage_swizzle_test.cpp (+106 -0)

📄 Description

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)

🔄 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/1989 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-dstorage-swizzle-linear-write` --- ### 📝 Commits (1) - [`5564ecc`](https://github.com/ubiquitarianism/leonida/commit/5564eccb034eac01a67383e9bbd42ca663488721) runtime: unshuffle DirectStorage stripes in a single sequential output pass ### 📊 Changes **3 files changed** (+135 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+3 -0) 📝 `runtime/src/dstorage_swizzle.h` (+26 -7) ➕ `runtime/test/dstorage_swizzle_test.cpp` (+106 -0) </details> ### 📄 Description 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) --- <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#1996
No description provided.