D3D12-FORMATS-TEXTURE-FAST-WALK: 1.22x-1.74x faster Layout::detile & tile via pre-divided u32 chunk offsets #2695
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2695"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T04:58:06Z.
Problem
Layout::detileandLayout::tile(walk()ind3d12/crates/leonida-formats/src/texture.rs) copy texture rows usingruns: Vec<(usize, usize, usize)>(24 bytes per element). For uniform 8/16/32-byte tiled blocks:copy_runsperformed a per-chunk bounds check,slice::copy_from_slicedispatch, and a runtime(tiled_row + offset) / Ninteger division on every chunk across every row.8,16, and32byte chunk sizes had typed array-copy specializations (4and64fell back to slow generic slice copies).Fix
In
d3d12/crates/leonida-formats/src/texture.rs:offset / size) into compactu32offsets, using a zero-allocation[0u32; 32]stack buffer forbody.len() <= 32(no heap overhead on small subresources / mip levels) and aVec<u32>for wider rows (4 Bvs24 Bstride).copy_chunks::<N, DETILE>()which checksbase + 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.4and64byte uniform chunk widths alongside8,16, and32.Verification
10.0.26200), Ryzen 7 7800X3D, pinnedrustc 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.--releasetexture_walk_benchon Ryzen 7 7800X3D (small mips + large textures):8x8_4B_64KB_S0.105 usdetile /0.100 ustile;32x24_4B_64KB_D0.356 us/0.317 us;256x256_4B_64KB_D_m50.104 us/0.102 us256x256_4B_64KB_S30,266 MB/sdetile /21,930 MB/stile;512x512_16B_64KB_D35,783 MB/s/35,427 MB/s;1024x1024_8B_64KB_D34,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)
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T05:00:18Z.
Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64), Rust 1.98.1, main
1d22590b9, PR head002f92a1e. MEASURED:cargo test -p leonida-formatsgreen (main + this PR): 41 passed, 6 ignored (main: 41 passed, 5 ignored; the new one istexture_walk_bench,#[ignore]).texture_walk_bench(--release -- --ignored --nocapture), main'sLayoutcode with only this PR's bench added (2 runs) vs this PR (1 run), MB/s: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
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-30T06:02:15Z.
Independent A/B of
002f92a1e23d3600128df9f0ef360aa6b168b6b2on0ae217ceb136401793bf00f6fd7d9055eb0eabc7, 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 againstLayout::offset, including untouched padding. All comparisons matched.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.
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T06:14:24Z.
Thanks for catching that @lixonik! Eliminated the small-subresource
Vec<u32>heap allocation by using a[0u32; 32]stack buffer whenbody.len() <= 32(41/41unit tests pass,0clippy warnings;--releaseRyzen 7 7800X3D8x8_4B_64KB_S0.105 usdetile /0.100 ustile,256x256_4B_64KB_D_m50.104 us/0.102 us,32x24_4B_64KB_D0.356 us/0.317 us, keeping full speedup on larger textures). AI-assisted: Antigravity (Gemini 4 Neon).Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-30T07:04:36Z.
Independent Windows allocation check for the small-mip update in
d740cb11f. Compared002f92a1eandd740cb11fmerged ontobbd45f5cc(trees9ff171f1bande174ed161). 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
tileanddetilewith preallocated input and output buffers. The instrument detected a single control allocation. Both operations gave the same counts for each layout.On both heads, tiled output matched a per-element
Layout::offsetreference 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).
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-30T07:09:31Z.
Same-machine retest of the stack-buffer fix
d740cb11f531b26b376489de86d4e4bb92eae8f5. I reran the original padded-row probe on the same base0ae217ceb136401793bf00f6fd7d9055eb0eabc7, comparing main, the original head002f92a1eand 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.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.
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T07:34:58Z.
Independent CachyOS A/B of the stack-buffer revision
d740cb11fon GitHub mergea72f20374(base24ccd36c2). Ryzen 7 7800X3D, Rust 1.98.1, optimized standalone build oftexture.rs. Pinned to one logical CPU, seven fresh process pairs with alternating order. Rows had five padding bytes. Median latency: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-elementLayout::offsetin 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 ontoe1fe82f63also gave the sametexture.rsin each arm.AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T17:00:43Z.
Combined BCPack and texture-layout check with #2690
MEASURED. I tested #2690
782c93512and #269574fdec2bfagainstmain7663c34fc, separately and together (combined treefdea31184). 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
mainfor 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-blockLayout::offsetreference, including untouched bytes, and the detiled buffer matched the original padded rows.All 15 replays matched the
mainoutput 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).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T08:33:33Z.
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 intomain9372a4d4f(clean).main9372a4d4fand thenmain+ 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 onmain, not a comparison: other work ran on the PC during themainrun.main9372a4d4f+ the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: twosource_hash_testsfailed 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).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T09:35:19Z.
Merging. Head
74fdec2bfmerges cleanly withmain7fbcd456eand 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).