Repository navigation
D3D12-FORMATS-BC7-POST-LUTS: 2.4x faster BCPack BC7 block loop (index tables, one put per field) - #2714
Conversation
…FORMATS-BC7-POST-LUTS) The BC7 block loop of the BCPack decoder took 100-155 ns a block on top of the rANS lanes: every index residual was rebuilt bit by bit (two 4-iteration folds per byte), each index went through an add_index with per-element branches, endpoint residuals were done a column at a time and bc7_pack made about 45 Bits::put calls on a u128. - nibbles() reads a 256-entry table (also used by BC6H). - The sixteen indices of a set are added to the reference's in one pass with the shifts hoisted out of the loop (bc7_indices). - Endpoint residuals of a row go through one multiply-by-precision per column instead of a masked shift per column. - bc7_pack puts each channel's endpoints, the p-bits and each index set with one put, taking an anchor's top bit out of the fixed-width fields afterwards. Output is unchanged: a copy of the previous block loop is kept in the tests as the reference, and bc7_blocks_match_the_reference compares the two byte for byte on the shipped tables and on tables with arbitrary control words, dictionaries and reference widths (history wrapping), and on the error of an unused control code. 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 d367675, PR head e5f9f3b. MEASURED:
So the speed-up holds on arm64 too (2.75-3.64x, stable across runs), and above the 2.4x in the title. The comparison is against the in-test
Not covered: textures in the title (whether the decoded blocks feed anything differently there), and x86-64 numbers. AI-assisted: Claude Code |
|
Independent full-decoder check on x86-64. I compared
The BC7 sample ranges separate. The BC6H ranges overlap, so the smaller BC6H effect is less clear on this machine. AI-assisted with OpenAI Codex. The raw results were independently recomputed before posting. |
|
Measurement on Windows x86-64 for #2714, which is already merged (no merge verdict, just numbers). Head e5f9f3b, merged as df15197. "Before" is the merge's first parent 8421f9b, "after" is df15197. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass. Commands, in
Speed on x86-64 (i9-13900K):
End to end ( So on x86-64 the BC7 block loop is about 2.3x to 3.3x faster depending on the mode (2.64x over every control code). That is in the range the PR measured on its Ryzen 7 7800X3D (2.36-2.94x); the arm64 runs reported 2.75-3.64x. |
Claim: D3D12-FORMATS-BC7-POST-LUTS (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 BC7 block (bc7). That second part cost 100-155 ns a block (about a third of a BC7 decode in a local lane-plus-block benchmark on a stream with realistic symbol statistics, which is not part of this PR):nibblesrebuilt every index residual with two 4-step bit folds per byte;add_index, whose closure branches on the widths for every element;bc7_packcalledBits::put(a maskedu128shift) about 45 times per block.Now:
nibblesreads a 256-entryconsttable (the BC6H path shares it);bc7_indicesadds the 16 indices of a set to the reference's in one loop with the width shifts hoisted out;1 << (16 - precision)per column (a channel with no bits has 0 there, which also drops the oldcontinue);bc7_packputs each channel's endpoints, the p-bits and each index set with oneput, and takes an anchor's top bit out of the fixed-width index fields afterwards (highest anchor first).The lane loop is untouched, so this is independent of #2690 and of #2567 (it rewrites the lanes, adds BC2 and changes table parsing;
git merge-treeof this branch with the head of each is clean).Output is unchanged
mod referencein the tests is a verbatim copy of the previousbc7/bc7_pack/nibbles/add_index. New tests:bc7_blocks_match_the_reference: 24 rounds of 700 blocks (so the 256-entry history wraps) of random records; round 0 on the shipped tables, the others on tables whose 512 control words, 255 dictionary entries and reference index widths (0-255) are all random, so every mode, subset count, partition, rotation andref_bits> 16 is hit. Output andResultequal the reference's, in a debug build (overflow checks on) and in--release.bc7_unused_control_code_stops_at_the_same_block: the error comes at the same block and the blocks before it are written alike.I checked that the differential test has teeth: five one-token mutations of the new code (an anchor bit, the endpoint mask, the reference mask, the residual order, the anchor top-bit position) were each caught. A sixth, a header width, was not, because that line is the same in the reference's copy and both changed; the header code is not changed by this PR.
Numbers (
--release,bc7_post_throughput, ignored benchmark)Same binary, reference and new alternated in 40 batches of 3 passes over 16384 blocks, fastest batch of each. Random records on the shipped tables; "mode N" is the first control code of that mode on every block:
End to end, the existing
bcpack_throughput(built frommainand from this branch in separate target directories, alternated three times): BC7 (mode 6 only, empty dictionary) 324.7 / 334.2 / 324.2 ns/block onmain, 273.3 / 287.6 / 269.5 on this branch; BC1 and BC4 (they do not touch this code) are the same within noise (BC1 92-107 onmain, 95-114 here; BC4 94-96 and 94-97).The machine was not otherwise idle (other builds were running), hence the alternating fastest-of-N method; absolute numbers will differ elsewhere, the ratios did not move between runs.
Tested
cargo test -p leonida-formats --lib: 44 passed, 6 ignored (debug); same in--release.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).python -I scripts/check_no_game_data.py d3d12/crates/leonida-formats/src/bcpack.rs: clean.