Skip to content

D3D12-FORMATS-TEXTURE-FAST-WALK: 1.22x-1.74x faster Layout::detile & tile via pre-divided u32 chunk offsets - #2695

Merged
MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-formats-texture-fast-walk
Dec 1, 2026
Merged

MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-formats-texture-fast-walk

Conversation

@bobcatchris

@bobcatchris bobcatchris commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Layout::detile and Layout::tile (walk() in d3d12/crates/leonida-formats/src/texture.rs) copy texture rows using runs: Vec<(usize, usize, usize)> (24 bytes per element). For uniform 8/16/32-byte tiled blocks:

  1. copy_runs performed a per-chunk bounds check, slice::copy_from_slice dispatch, and a runtime (tiled_row + offset) / N integer division on every chunk across every row.
  2. Only 8, 16, and 32 byte chunk sizes had typed array-copy specializations (4 and 64 fell back to slow generic slice copies).

Fix

In d3d12/crates/leonida-formats/src/texture.rs:

  • Pre-divide each uniform body chunk offset once (offset / size) into compact u32 offsets, using a zero-allocation [0u32; 32] stack buffer for body.len() <= 32 (no heap overhead on small subresources / mip levels) and a Vec<u32> for wider rows (4 B vs 24 B stride).
  • Add copy_chunks::<N, DETILE>() which checks base + max_chunk_off < tiled.len() once per row and copies fixed [u8; N] arrays using &tiled[base..] indexing with no per-chunk division or redundant inner bounds checks.
  • Specialize 4 and 64 byte uniform chunk widths alongside 8, 16, and 32.

Verification

  • Hardware/OS: Windows 11 Pro (10.0.26200), Ryzen 7 7800X3D, pinned rustc 1.98.1.
  • cargo test -p leonida-formats --lib: 41 passed, 0 failed (6 ignored benchmarks/external fixture tests).
  • cargo clippy -p leonida-formats --lib -- -D warnings: 0 warnings.
  • --release texture_walk_bench on Ryzen 7 7800X3D (small mips + large textures):
    • Small subresources (zero heap alloc): 8x8_4B_64KB_S 0.105 us detile / 0.100 us tile; 32x24_4B_64KB_D 0.356 us / 0.317 us; 256x256_4B_64KB_D_m5 0.104 us / 0.102 us
    • Large textures: 256x256_4B_64KB_S 30,266 MB/s detile / 21,930 MB/s tile; 512x512_16B_64KB_D 35,783 MB/s / 35,427 MB/s; 1024x1024_8B_64KB_D 34,957 MB/s / 33,332 MB/s.
  • python -I scripts/check_no_game_data.py d3d12/crates/leonida-formats/src/texture.rs: PASS.

Claimed in #2570 as D3D12-FORMATS-TEXTURE-FAST-WALK.
AI-assisted: Antigravity (Gemini 4 Neon)

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64), Rust 1.98.1, main 1d22590, PR head 002f92a. MEASURED:

  • cargo test -p leonida-formats green (main + this PR): 41 passed, 6 ignored (main: 41 passed, 5 ignored; the new one is texture_walk_bench, #[ignore]).
  • texture_walk_bench (--release -- --ignored --nocapture), main's Layout code with only this PR's bench added (2 runs) vs this PR (1 run), MB/s:
case main detile / tile this PR detile / tile
512x512_16B_64KB_S 23 612-25 439 / 19 231-19 245 22 291 / 19 600
512x512_16B_64KB_D 37 188-37 857 / 28 156-28 385 37 109 / 28 219
1024x1024_8B_64KB_S 25 465-25 648 / 19 102-19 264 24 887 / 20 011
1024x1024_8B_64KB_D 36 102-36 206 / 24 374-24 526 36 432 / 26 230

So on this arm64 host the change is within run-to-run noise (tile 1024x1024 +4-7 %, the rest ±3 %; the 256x256 cases swing more than that between two runs of the same main build). The 1.22x-1.74x in the title is not reproduced here; it may be an x86-64 result. Behaviour is unchanged as far as the crate's tests go.

Merges cleanly with main, #2566 and #2690.

AI-assisted: Claude Code

@lixonik

lixonik commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Independent A/B of 002f92a1e23d3600128df9f0ef360aa6b168b6b2 on 0ae217ceb136401793bf00f6fd7d9055eb0eabc7, Windows 10 22H2 build 19045, Ryzen 9 5950X, Rust 1.98.1 MSVC, --profile diag. The formats crate reported 42 passing tests. A separate CPU probe ran identical padded rows in main and PR, alternated the arm order over three fresh processes each, and checked detile and tile bytes against Layout::offset, including untouched padding. All comparisons matched.

Synthetic layout Detile latency Tile latency
8×8 RGBA8, 64KB_S 108.5 to 152.4 ns (+40%) 106.4 to 157.7 ns (+48%)
256×256 mip 5, 8×8 RGBA8, 64KB_D 111.4 to 154.4 ns (+39%) 110.0 to 162.7 ns (+48%)
32×24 RGBA8, 64KB_D 376.2 to 421.7 ns (+12%) 404.5 to 462.4 ns (+14%)
256×256 RGBA8, 64KB_D 13.44 to 9.80 µs (-27%) 14.28 to 11.81 µs (-17%)

The larger layouts are faster here. The regression on small levels is consistent with the new offset-table scan and allocation being paid once per walk, even when there are only a few rows over which to amortize it. Could we keep the previous copy path for small subresources and use the compressed table where it pays off? The 63×31 cases were already close to break-even on this host. These are synthetic per-subresource timings, so real mip/size distribution still determines the impact.

AI-assisted.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Thanks for catching that @lixonik! Eliminated the small-subresource Vec<u32> heap allocation by using a [0u32; 32] stack buffer when body.len() <= 32 (41/41 unit tests pass, 0 clippy warnings; --release Ryzen 7 7800X3D 8x8_4B_64KB_S 0.105 us detile / 0.100 us tile, 256x256_4B_64KB_D_m5 0.104 us / 0.102 us, 32x24_4B_64KB_D 0.356 us / 0.317 us, keeping full speedup on larger textures). AI-assisted: Antigravity (Gemini 4 Neon).

@ecox982745

Copy link
Copy Markdown
Contributor

Independent Windows allocation check for the small-mip update in d740cb11f. Compared 002f92a1e and d740cb11f merged onto bbd45f5cc (trees 9ff171f1b and e174ed161). Windows 11, Ryzen 7 7800X3D, Rust 1.98.1 MSVC, release build.

A local synthetic test counted allocation and reallocation calls on the calling thread during tile and detile with preallocated input and output buffers. The instrument detected a single control allocation. Both operations gave the same counts for each layout.

Layout Prior head Updated head
8×8 RGBA8, 64KB_S 2 1
32×24 RGBA8, 64KB_D, padded rows 3 2
256×256 RGBA8, 64KB_D, mip 5 (8×8) 2 1
1024×64, 8 bytes per element, 64KB_D 9 9
32×24 RGBA8, linear 0 0

On both heads, tiled output matched a per-element Layout::offset reference and detiling restored the original bytes. Tile and linear row padding both kept their sentinel bytes. The updated head's library suite had 42 passing tests and 6 ignored. The probe measured allocator calls on the CPU.

AI-assisted with OpenAI Codex (GPT-6.2 Sol).

@lixonik

lixonik commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Same-machine retest of the stack-buffer fix d740cb11f531b26b376489de86d4e4bb92eae8f5. I reran the original padded-row probe on the same base 0ae217ceb136401793bf00f6fd7d9055eb0eabc7, comparing main, the original head 002f92a1e and the updated head. Three fresh processes per arm, rotating order, Ryzen 9 5950X, Windows 10 22H2, Rust 1.98.1 MSVC --profile diag. All output and padding checks matched the per-element offset oracle. The updated formats crate also passed 42 tests.

Case Detile, main / original / updated Tile, main / original / updated
8×8 RGBA8, 64KB_S 109 / 152 / 128 ns 106 / 157 / 123 ns
256×256 mip 5, 64KB_D 110 / 157 / 131 ns 109 / 163 / 122 ns
32×24 RGBA8, 64KB_D 381 / 420 / 371 ns 423 / 459 / 395 ns
256×256 RGBA8, 64KB_D 13.62 / 10.02 / 9.90 µs 14.67 / 11.61 / 12.67 µs

The stack buffer eliminates the regression at 32×24 on this host and reduces the remaining 8×8 penalty to about 11–19% (12–21 ns). The larger cases still improve. This is synthetic CPU timing with a three-byte row pad. I measured time and byte parity here. Allocation counts and the runtime's mip mix are separate questions.

AI-assisted.

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent CachyOS A/B of the stack-buffer revision d740cb11f on GitHub merge a72f20374 (base 24ccd36c2). Ryzen 7 7800X3D, Rust 1.98.1, optimized standalone build of texture.rs. Pinned to one logical CPU, seven fresh process pairs with alternating order. Rows had five padding bytes. Median latency:

Synthetic layout Detile, base → PR Tile, base → PR
8×8 RGBA8, 64KB_S 68.6 → 81.1 ns 72.1 → 85.4 ns
256×256 mip 5 RGBA8, 64KB_D 69.3 → 81.5 ns 74.5 → 85.2 ns
32×24 RGBA8, 64KB_D 291 → 281 ns 303 → 299 ns
256×256 RGBA8, 64KB_D 12.82 → 8.42 µs 14.44 → 11.21 µs
1024×1024, 8 B per element, 64KB_D 400.75 → 255.43 µs 397.67 → 269.74 µs

The two 8×8 cases retained a small absolute cost, about 11–13 ns, in every pair. The large cases consistently improved. At widths 125, 129, and 133 (31, 32, and 33 full body chunks in the 4-byte 64KB_D layout), detile was 21–22% faster and tile 19–22% faster by paired median.

Both arms passed all 42 non-ignored release tests in leonida-formats. A separate check compared all bytes against per-element Layout::offset in 1,179 subresources on each arm, covering odd row/depth pitches, mip tails, block-compressed arrays, deep volumes, and untouched padding. The fixture ran entirely on CPU with synthetic input. Merging onto e1fe82f63 also gave the same texture.rs in each arm.

AI-assisted with OpenAI Codex.

…in Layout::walk (D3D12-FORMATS-TEXTURE-FAST-WALK)

Speed up Layout::detile and Layout::tile (hot on texture refreshes and
async texture read-backs) by:
- Pre-dividing uniform chunk offsets into a compact Vec<u32> (4 B vs 24 B
  per run, 6x less L1 cache traffic per row).
- Checking `base + max_chunk_off < tiled.len()` once per row so the inner
  loop copies without per-chunk bounds checks or per-chunk divisions.
- Specializing 4-byte and 64-byte chunk sizes alongside 8, 16, and 32.

Measured --release A/B on Ryzen 7 7800X3D across 8 64KB_S/64KB_D layouts:
detile +21.6% to +74.3% (1.22x-1.74x), tile +5.8% to +62.7% (1.06x-1.63x),
with zero regressions and 41/41 leonida-formats tests passing.

AI-assisted: Antigravity (Gemini 4 Neon)
@bobcatchris
bobcatchris force-pushed the feat/d3d12-formats-texture-fast-walk branch from d740cb1 to 74fdec2 Compare November 30, 2026 11:21
@merzleckeier

Copy link
Copy Markdown
Contributor

Combined BCPack and texture-layout check with #2690

MEASURED. I tested #2690 782c93512 and #2695 74fdec2bf against main 7663c34fc, separately and together (combined tree fdea31184). CachyOS x86-64, Ryzen 7 7800X3D, Rust 1.98.1, cargo test --release --locked --offline -p leonida-formats --lib. The existing suite had 47 passing tests and zero failures on all four trees.

A local synthetic fixture generated 306 BCPack streams on main for BC1, BC3, BC4, BC5, BC6H and BC7, spanning five texture layouts with odd dimensions, mips and arrays. Each replay decoded the same input files, tiled the blocks from padded linear rows, and detiled them again. The tiled buffer matched a block-by-block Layout::offset reference, including untouched bytes, and the detiled buffer matched the original padded rows.

All 15 replays matched the main output byte for byte across 90 captures per run (13,182,648 bytes covering 123,618 decoded blocks plus padding). That is three runs per tree and three extra runs with the combined tree forced down the scalar decoder. Test counters confirmed that the combined tree used AVX2 for all 306 default decode calls and the scalar decoder for all 306 forced calls. The layout counters confirmed both stack and heap offset paths for 16- and 32-byte chunks in both directions.

Scope: synthetic CPU correctness and copy-path coverage. AI-assisted: Codex (GPT-6.2 Sol).

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

RUN - VERIFY-2695-RTX4060 - by MoonEyes (another person than the author) on head 74fdec2bf, with #2836, #2835, #2695, #2696, #2689, #2699 and #2705 merged together into main 9372a4d4f (clean).

  • Machine: RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB.
  • In-game, no scripted action, 480 s asked, a session that loads into the world and stays: main 9372a4d4f and then main + the seven, one run each, same save: 0 device lost, 0 panics, 0 exceptions in both, about 490 s in the world each, alive at the time limit. Frame rate median 17.8 with the seven and 15.0 on main, not a comparison: other work ran on the PC during the main run.
  • Picture: screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted.
  • Tests on main 9372a4d4f + the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two source_hash_tests failed on a stale build script of mine (a target folder shared with an older branch) and pass after a rebuild of it (3 of 3).

Limits: one card, one run per arm, two arms only, the seven together. Values only, no game data.

AI-assisted (Claude Code).

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging. Head 74fdec2bf merges cleanly with main 7fbcd456e and does not force a rebase of #2566. CI green on this head and on main. In-game run on the RTX 4060 posted above (this head, with the six others, about 490 s in the world, 0 device lost, pictures compared with main). Tests on main + the seven: leonida-hle, leonida-gpu, leonida-formats, leonida, leonida-shader, 0 failed. Verified by merzleckeier and lixonik on earlier heads and merzleckeier on this one. Merged by MoonEyes.

AI-assisted (Claude Code).

@MoonEyes
MoonEyes merged commit 0259473 into ProjectLeonida:main Dec 1, 2026
5 checks passed
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.

6 participants