Repository navigation
D3D12-FORMATS-BCN-POST-SWAR: eight-byte-wide BC1/BC3/BC4/BC5 block loop after the BCPack lanes (1.1-1.3x) - #2715
Conversation
…g (D3D12-FORMATS-BCN-POST-SWAR) After the rANS lanes the plain formats' block loop did its arithmetic a byte at a time: a per-element wrapping add and width mask for each of 8 endpoints and 32 indices, 16 scattered byte stores per quad/pair residual, a table lookup and shift per index to build the BC1/BC4 code words, and two 4-step folds per interleave(). - Widths holds the masks of the elements' bits (a word for the endpoints, four for the indices); endpoints and indices are summed with their references eight bytes at a time (top bits added apart for the 8-bit endpoints, masked sums for the at most 7-bit indices). - quads() takes a 256-entry const table and fills the four 4-pixel rows as words; pairs() builds eight pixels at a time. - BC1 and BC4 index codes are computed for eight indices at once (bc1_codes, bc4_codes) and packed by shifts (pack2, pack3). - interleave() reads a 16-entry const table. Output is unchanged: a copy of the previous loop is kept in the tests as the reference, and plain_blocks_match_the_reference compares the two byte for byte for BC1, 3, 4 and 5 on the shipped tables and on tables whose ranks and dictionaries hold any byte (history wrapping). index_codes_are_the_tables and index_codes_pack_by_position check the new code words and packers against the code tables for every byte. AI-assisted: Claude Sonnet 5.5 Co-authored-by: Cursor <cursoragent@cursor.com>
|
Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64), Rust 1.98.1, main ec1d858, PR head f536b3d. MEASURED:
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 ( AI-assisted: Claude Code |
|
Independent full-decoder check on x86-64. CachyOS, Ryzen 7 7800X3D, Rust 1.98.1, CPU only. I compared
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. |
|
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
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 |
|
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 (
Post stage (
Whole decode (
This run was AI-assisted (Claude Code, oreofr-agent). |
|
Measurement on Windows x86-64 for #2715, which is already merged (no merge verdict, just numbers). Head f536b3d, merged as 97a7ae7. "Before" is the merge's first parent df15197 (with #2714 in it), "after" is 97a7ae7. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass. Commands, in
Output before and after: a local harness (not pushed) calls the production Speed before/after per format, the same harness:
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 ( End to end ( |
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 (D3D12-CLIPPY-NON-GPU-CRATES: fix all clippy warnings across non-gpu d3d12 crates #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.