D3D12-FORMATS-BCN-POST-SWAR: eight-byte-wide BC1/BC3/BC4/BC5 block loop after the BCPack lanes (1.1-1.3x) #2715
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2715"
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-30T12:03:38Z.
Claim: D3D12-FORMATS-BCN-POST-SWAR (comment).
AI-assisted: Claude Sonnet 5.5 (Cursor agent). No game files, no GPU involved; everything below is CPU-only and synthetic.
What
The BCPack decoder (
leonida-hleDirectStorage ->leonida_formats::bcpack::decode) runs the rANS lanes first and then turns each block's record into a BC1, BC3, BC4 or BC5 block (plain). That second part did its arithmetic a byte at a time:wrapping_addand width mask;quadsandpairswrote the residuals toPlain::ixone byte at a time (16-32 byte stores, which the vector loads after them then had to wait for);bc1_colorandbc4_alphalooked up a code and shifted it into place for each of the 16 indices;interleave(BC3 and BC5 references) did two 4-step bit folds.Now:
Widthsholds each format's element widths as masks (a word for the endpoints, four for the indices), andplainsums the endpoints and the indices with their references eight bytes at a time. The 8-bit endpoints add their top bits apart so no carry leaves a byte; the indices are at most 7 bits wide, so their masked sums stay in their bytes.quadsreads a 256-entryconsttable and fills the four 4-pixel rows as words;pairsbuilds eight pixels at a time.bc1_codesandbc4_codesturn eight indices into BC1 / BC4 codes at once (BC1 code =(b0 ^ b1) << 1 | b1; BC4 code = index + 1 modulo 8, xor 1 where the index's three bits are alike), andpack2/pack3put them in their 2- or 3-bit fields with three shift-and-mask steps.interleavereads a 16-entryconsttable.The lane loop,
Tables::parse,referencesandpickare untouched, so this is independent of #2690 (lanes), #2714 (BC7 block loop) and #2567 (lanes, BC2, table parsing). #2567's BC2 callsbc1_color,referencesandpick: their signatures are unchanged.git merge-treeof this branch with each of the three heads is clean, and this branch merged with #2567 and #2714 gives 48 passed / 7 ignored incargo test -p leonida-formats --lib, debug and--release. (#2690 and #2567 conflict with each other, not with this.)Output is unchanged
mod reference_plainin the tests is a verbatim copy of the previousplain/quads/pairs/*_residual/bc1_color/bc4_alpha/*_pack/interleave/references/zz, with its own copy ofWidths. New tests:plain_blocks_match_the_reference: 16 rounds of 700 blocks (so the 256-entry history wraps) for each of BC1, BC3, BC4 and BC5; round 0 on the shipped tables, the others on tables whose rank tables and dictionary entries hold any byte. Output equal to the reference's, in a debug build (overflow checks on) and in--release.index_codes_are_the_tables:bc1_codes/bc4_codesgive the code tables' entries for every byte value at every position of the word, with random bytes beside it.index_codes_pack_by_position:pack2/pack3put each byte's field where its position says (20,000 random words, plus all-ones).I checked that the tests have teeth with 17 one-token mutations of the new code (the code words, the shifts of both packers, the quad's right half and bottom row, the endpoint mask and its top-bit add, the index masks, the carry, the BC4 end rule, the spread table, the pair's high pixel, the alpha and colour field positions): 16 were caught. The survivor changes the mask of the 2-bit indices from 3 to 7; it cannot show in the output, since the low two bits of a sum do not depend on the third and the packers read only those, so it is an equivalent mutant.
Numbers (
--release,plain_post_throughput, ignored benchmark)The post stage alone, the production
plainagainst the reference copy in the same binary, alternated in 40 batches of 3 passes over 16384 blocks, fastest batch of each. Random records on the shipped tables; "no references" zeroes the reference bytes, so the branches ofpickare always right. Three runs agreed to 0.1 ns:Before the change, the same harness measured the production
plainat 20.2 (BC1), 26.4 (BC3), 17.0 (BC4) and 24.3 (BC5) ns/block with random references while the verbatim reference copy took 15.8 / 24.5 / 14.6 / 22.4: the same source compiled into two places is up to 1.3x apart (inlining and layout), so the speed-up against the reference above is the conservative figure and the one against the old production code in the same harness (1.42x BC1, 1.33x BC3, 1.50x BC4, 1.31x BC5) is the optimistic one. Take the first.This is a small part of a decode: in the existing
bcpack_throughput(lanes and block loop together) BC1 takes about 99 ns/block and BC4 about 96, so the block loop was about 15% of it and the expected change is 3-5%, below the 92-107 ns this machine's noise moves the same binary between runs (other builds were running); I did not find a gain there beyond the noise, and do not claim one. The point of the change is the share the block loop has once the lanes are made faster (#2690), where it grows.Tested
cargo test -p leonida-formats(debug, overflow checks on): 45 passed, 6 ignored;--release: 45 passed, 6 ignored (main: 42 passed, 5 ignored; the new ones are the three tests above and the ignored benchmark).cargo clippy -p leonida-formats --all-targets --no-deps: no warning inbcpack.rs. The one warning that remains istexture.rs:1346(operator precedence), not touched here (#2681 covers it).rustfmt --edition 2024over the lines this PR adds (the file has other lines that are not rustfmt-clean onmain; those are left as they are).python -I scripts/check_no_game_data.py d3d12/crates/leonida-formats/src/bcpack.rs: clean.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T12:06:57Z.
Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64), Rust 1.98.1, main
ec1d85846, PR headf536b3d50. MEASURED:cargo test -p leonida-formats(main + this PR): 45 passed, 6 ignored; the newplain_blocks_match_the_reference,index_codes_are_the_tables,index_codes_pack_by_positionpass. Merges cleanly with main and with #2714.plain_post_throughput(--release -- --ignored --nocapture), two runs, same result both times, ns/block, reference → now:On arm64, BC4 is ~3.3x slower and BC5 ~1.8x slower than the reference, while the description's table shows 1.29x / 1.21x faster for them (on another machine). BC1/BC3 gain as described. Note the reference itself is much faster here for BC4 (7.5 ns vs your 14.6), so the old byte-at-a-time BC4 path suits this CPU and the new eight-wide one does not; the BC4 code path (
bc4_codes/pack3) would be the place to look. Not a blocker for x86-64 players, but worth either keeping the old BC4/BC5 path where it is faster, or checking whether the x86-64 gain is real on more than one CPU.AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T12:10:33Z.
Independent full-decoder check on x86-64. CachyOS, Ryzen 7 7800X3D, Rust 1.98.1, CPU only. I compared
mainec1d85846with GitHub merge53e345eadfor headf536b3d50. All inputs were synthetic.decodeon both revisions. All 72 outputs matched byte for byte (259,200 bytes). The matrix covers BC1/3/4/5, 1/7/255/256/257/1024 blocks, and mixed, zero, or fixed distance-255 references with a seeded dictionary. A local one-bit shift error in the BC1 color packer changed all BC1 and BC3 outputs, as expected.cargo test -p leonida-formats --libpassed on both revisions in debug and release, 42 passed / 5 ignored on main and 45 passed / 6 ignored on the PR.The sample ranges separate for BC1, BC3 and BC5. They overlap for BC4 and the BC7 control. OreoFR’s Apple M5 result measures the post stage on that host. This table measures the complete decode on x86-64.
AI-assisted with OpenAI Codex. The raw results were independently recomputed before posting.
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T12:26:46Z.
Follow-up by OreoFR after the merge, to set beside merzleckeier's x86-64 table: the whole decode on arm64 (Apple M5, macOS 27.2.1, Rust 1.98.1), the crate's own
bcpack_throughput(--release -- --ignored), two runs on each revision, ns/block:ec1d85846(before)497bdd039(main now, #2714 + #2715 in)bcpack_throughputhas no BC3/BC5 case, so those are not measured here for the whole decode.Not a revert request: DirectStorage decode on an ARM host is not a shipping path today. But if one ever is, BC4 (and likely BC5) would want the old path back on aarch64.
AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T13:53:05Z.
RUN - x86-64 numbers (Verified by OreoFR) - Intel Core Ultra 7 155H (hybrid: P, E and LP E-cores), Windows 11, Rust 1.98.1 (
d3d12/rust-toolchain.toml),--release, CPU only. MEASURED.df151972c(97a7ae7f3^1, #2714 in, this PR not); after =97a7ae7f3(this PR's merge). Separate worktrees and target dirs.plain_post_throughput(post stage, old code vs new in one binary; after only) andbcpack_throughput(whole decode; both revisions).Post stage (
plain_post_throughputat97a7ae7f3), ns/block, reference -> now, random refs / no refs:Whole decode (
bcpack_throughput), ns/block, median of 6 runs a side (pre, post, post, pre x3):df151972c97a7ae7f3bcpack_throughputhas no BC3/BC5 case, so BC3 and BC5 are not measured for the whole decode here.This run was AI-assisted (Claude Code, oreofr-agent).
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-30T19:10:56Z.
Measurement on Windows x86-64 for #2715, which is already merged (no merge verdict, just numbers). Head
f536b3d50, merged as97a7ae7f3. "Before" is the merge's first parentdf151972c(with #2714 in it), "after" is97a7ae7f3. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.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: before 44 passed / 0 failed / 6 ignored; after 47 / 0 / 7 in debug (overflow checks on) and 47 / 0 / 7 with--release. The extra ones are the three new tests and the ignored benchmark.plain_blocks_match_the_referencepasses in both builds.cargo clippy -p leonida-formats --all-targets, before and after: no warning inbcpack.rs.bc4_codeswithout the xor of the end rule.index_codes_are_the_tables,bc4_blocks_with_and_without_referencesandplain_blocks_match_the_referenceall fail.Output before and after: a local harness (not pushed) calls the production
plainthe waydecodedoes, on the same 16,384 random records per format, built once from "before" and once from "after". The output digests are equal for BC1, BC3, BC4 and BC5 in every run.Speed before/after per format, the same harness:
cargo test --release -p leonida-formats --lib --locked local_plain_before_after -- --ignored --nocapture, with before and after alternated, n = 3 each. Each value is the fastest of 40 batches of 3 passes over 16,384 blocks:So on x86-64, BC4 and BC5 are faster, not slower: BC4 gains the most (1.60-1.78x), and BC5 gains 1.36-1.55x.
The PR's own harness (
plain_post_throughput, production code against the verbatim reference copy in the same binary, n = 3): BC1 1.04/0.99/0.99x with random references and 1.08/0.99/1.00x without; BC3 1.35/1.27/1.30x and 1.38/1.27/1.29x; BC4 1.20/1.36/1.36x and 1.44/1.41/1.40x; BC5 0.89/1.27/1.24x and 1.19/1.29/1.30x. The first run was disturbed by other jobs, which is where the one 0.89x comes from. As the PR says, the reference copy runs faster than the old production code did, so this in-binary figure is the conservative one; the before/after builds above are the real change.End to end (
bcpack_throughput, lanes included, BC1 and BC4 only): before vs after stays within this machine's noise (BC1 82.6-108.9 ns/block before, 82.8-129.4 after), as the PR also found.Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB (not used), NVIDIA 621.40.