D3D12-FORMATS-BCPACK-FAST-LANES: branchless AVX2 model lookup for BCPack rANS lanes #2690
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2690"
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-30T02:54:35Z.
Claimed in #2570 as
D3D12-FORMATS-BCPACK-FAST-LANES(#issuecomment-7267372755).What and why
DirectStorage texture streaming decompresses BC1–BC7 textures through
d3d12/crates/leonida-formats/src/bcpack.rs(d3d12/docs/findings.mdnotes DirectStorage threads on the frame's idle chain). Previously, every rANS symbol decoded inLane::step()performed four separate 2-D table lookups (freq,start, 32 KiBbucket[256]window table,recip), a branchywhile start[b + 1] <= slotloop, anif b == 0branch, and an infallibleResult<(), Error>unwrap.This change:
Modelstruct (start_1_8: [i32; 8],adj_start,adj_freq,base_sym,adj_recip) with pre-adjustedb == 0entries (adj_start[0] = 1,adj_freq[0] = freq[0] - 1,adj_recip[0] = 0,base_sym[0] = 0), eliminating theif b == 0branch.lanes()entry to an AVX2 path (#[target_feature(enable = "avx2")]) that resolvesbucket_of_avx2()in 3 instructions (_mm256_cmpgt_epi32+_mm256_movemask_ps+count_ones) with zero branches, falling back cleanly tobucket_ofon non-AVX2 hosts.Result<(), Error>wrapper fromLane::step().Verification & Measured
--releaseA/B BenchmarkHardware / OS: native Windows 11 Pro (
10.0.26200), AMD Ryzen 7 7800X3D, pinned toolchainrustc 1.98.1.bucket_lookup_matches_a_scan_of_the_startsverifiedbucket_of_avx2against the reference scan across all3 × 128 × 65,536 = 25,165,824(table, model, slot)pairs — 0 mismatches.--releasebenchmark (cargo test --release -p leonida-formats bcpack_throughput -- --ignored --nocapture, 16,384 blocks × 40 runs):86.3 → 94.7 MB out/s(92.7 → 84.4 ns/block, +9.7%)83.0 → 93.8 MB out/s(96.4 → 85.3 ns/block, +13.0%)49.7 → 53.3 MB out/s(322.0 → 300.3 ns/block, +7.2%)cargo test -p leonida-formats --lib—41 passed; 0 failed(every_layout_round_trips_through_the_lanes,payload_step_matches_the_reference_decode,bc7_dictionary_indices_line_up).cargo clippy -p leonida-formats --no-deps --lib -- -D warnings— 0 warnings.python -I scripts/check_no_game_data.py d3d12/crates/leonida-formats/src/bcpack.rs— PASS.AI-assisted: Antigravity (Gemini 4 Neon)
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T02:56:45Z.
Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64, no AVX2), Rust 1.98.1,
cargo test -p leonida-formats, main95bc33f6e, PR head121f6a0e2. MEASURED, partial:AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T10:57:10Z.
Independent Linux AVX2 check. I merged
121f6a0e2onto0568cd62e(471e296e1tree). CachyOS x86-64, Ryzen 7 7800X3D, Rust 1.98.1. The merge ontod367675c7is also clean and the formats crate and Cargo inputs are the same.cargo test --locked --offline -p leonida-formats --libgave 42 passed and 5 ignored on each revision. The AVX2 bucket comparison covered the 25,165,824 stock (table, model, slot) combinations.A separate test called the scalar and AVX2 lane loops directly for 72 synthetic streams spanning all six lane layouts, with random symbols and alternating alphabet extremes. Both paths decoded 489,600 symbols exactly and requested bytes in the same lane order.
bcpack_throughputin separate release builds pinned to CPU 6, two ABBA blocks after one warm-up per build. Median ns/block frommainto PR was BC1 93.10 → 81.85 (+13.7% throughput), BC4 93.25 → 83.65 (+11.5%) and BC7 318.70 → 294.00 (+8.4%). One BC1 PR sample was 98.4 ns/block and is included. The other three were 81.6–82.0.CPU-only synthetic tests. AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:11:29Z.
Review: APPROVED
Verified
bucket_of_avx2ind3d12/crates/leonida-formats/src/bcpack.rs:slot + 1is in1..=65536andstart_1_8[k]is in0..=65536, so signed 32-bit_mm256_cmpgt_epi32is exact across the entire range; theb == 0path (adj_start[0] = 1,adj_recip[0] = 0,base_sym[0] = 0) producessym = 0andrem = slot.wrapping_sub(1), bit-identical to the scalar path. Exhaustive 25.1M-entry AVX2/scalar parity tests and non-x86 fallback builds are verified, and all CI checks are green. Merging.(AI-assisted review.)
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T11:18:06Z.
Posted after the merge: the head tested below is the one that was merged.
RUN (independent run on Windows, #2690 head
782c93512, merged locally into maine56aff1cewithgit merge --no-ff, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)This CPU (i9-13900K) has AVX2, so
lanestakes the new AVX2 path here.Commands, in
d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with--locked, at low priority:cargo test -p leonida-formats --no-fail-fast: main 47 passed / 0 failed / 7 ignored; merge 47 / 0 / 7. The pull request adds an AVX2 check insidebucket_lookup_matches_a_scan_of_the_starts, not a new test. The crate has nodiagnosticsfeature.bucket_of_avx2), so the red is by mutation, with the tests kept:bucket_of_avx2off by one (slotinstead ofslot + 1in the compare):bucket_lookup_matches_a_scan_of_the_startsfails on "avx2 model 0 slot 32768" (left 0, right 1). Four decode tests that go throughlanesfail too (every_layout_round_trips_through_the_lanes,bc4_blocks_with_and_without_references,bc7_mode6_block,malformed_input_is_an_error), so the decode tests do run the AVX2 path on this CPU.adj_freq[0] = freq[m][0]instead offreq[m][0] - 1): 6 tests fail, among thempayload_step_matches_the_reference_decode("model 0 x 0x1000001") and the round trips. So the existing tests pin the new tables.git statusclean): 47 / 0 / 7.cargo clippy -p leonida-formats --all-targets, main and merge: 1 warning on each side (texture.rs, unrelated), none new.bcpack_throughput --ignored --nocapture --test-threads=1, run alternately 7 times each, at low priority with other jobs on the PC. Median ns/block, main -> merge: BC1 83.5 -> 68.5 (-18 %), BC4 80.9 -> 70.2 (-13 %), BC7 242.0 -> 200.8 (-17 %). Best of 7: BC1 76.9 -> 66.5, BC4 76.2 -> 63.8, BC7 221.5 -> 197.5. The body gives -7 to -12 % on a Ryzen 7 7800X3D; the gain is there, a bit larger on this CPU.lanes(a CPU without AVX2). On an AVX2 machinelanesnever callslanes_implwithbucket_of; onlypayload_step_matches_the_reference_decodedrivesLane::stepwith the scalarbucket_of. A test that callslanes_implwithbucket_ofwould cover that path on every machine. The game (texture streaming) is not covered either.// shortcut:comment abovelanes, but the decoder still steps one lane at a time, which is what that note was about.Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.