D3D12-FORMATS-BC7-POST-LUTS: 2.4x faster BCPack BC7 block loop (index tables, one put per field) #2714
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2714"
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-30T11:45:43Z.
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 (#2681 covers it).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-30T11:50:55Z.
Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64), Rust 1.98.1, main
d367675c7, PR heade5f9f3b47. MEASURED:cargo test -p leonida-formats(main + this PR): 44 passed, 6 ignored (main: 41 passed, 5 ignored); the newbc7_blocks_match_the_referenceandbc7_unused_control_code_stops_at_the_same_blockpass, as do the existingbc7_*cases.bc7_post_throughput(--release -- --ignored --nocapture, two runs; it also asserts the output equals the in-test reference before timing): reference → new, ns/block: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
reference::bc7, not main's build of the crate.Not covered: textures in the title (whether the decoded blocks feed anything differently there), and x86-64 numbers.
AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T12:15:26Z.
Independent full-decoder check on x86-64. I compared
mainec1d85846and that base withe5f9f3b47merged (treef529cc0e) on CachyOS, Ryzen 7 7800X3D, Rust 1.98.1. All inputs were synthetic, CPU only.decodereturned byte-identical outputs for 36 streams (207,312 decoded bytes). The BC7 cases cover all eight modes, 285 mode/partition/rotation/index-selection combinations, and history across block 256. The BC6H cases cover all 14 modes through the shared nibble decoder. For an unused BC7 control at zero-based block 256, both returned the same error and left the remainder of the output untouched. A local off-by-one in the anchor bit removal changed 17 of the 18 valid BC7 outputs and preserved all BC6H outputs.cargo test -p leonida-formats --libpassed in debug and release, 42 passed / 5 ignored on main and 44 passed / 6 ignored with the PR.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.
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-30T19:10:48Z.
Measurement on Windows x86-64 for #2714, which is already merged (no merge verdict, just numbers). Head
e5f9f3b47, merged asdf151972c. "Before" is the merge's first parent8421f9b95, "after" isdf151972c. 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 42 passed / 0 failed / 5 ignored; after 44 / 0 / 6 (the two new tests, plus the ignored benchmark).cargo test --release -p leonida-formats --lib bc7(after): 5 passed / 1 ignored (the benchmark),bc7_blocks_match_the_referenceamong them. So the output matches the reference copy in an optimized build too.cargo clippy -p leonida-formats --all-targets, before and after: no warning inbcpack.rs. The one warning,texture.rs:1346, is there on both.bc7_indiceswith the residual order flipped one column early (k < 3->k < 2).bc7_blocks_match_the_referenceandbc7_unused_control_code_stops_at_the_same_blockboth fail.Speed on x86-64 (i9-13900K):
cargo test --release -p leonida-formats --lib --locked bc7_post_throughput -- --ignored --nocapture, n = 5 runs. Each run is the PR's own harness: the reference copy and the new code alternated in 40 batches of 3 passes over 16,384 blocks, fastest batch of each. Other jobs were running on the machine, so absolute times moved by up to 3x between runs. The ratio is taken within one run, and it held.End to end (
bcpack_throughput, lanes and block loop together, separate builds of before and after, alternated): BC7 went from 301.5 to 254.7 and from 369.2 to 299.5 ns/block, about 1.2x. That is in line with the PR's 324 -> 273. A third "before" build was lost to a rustc crash (0xC0000005, nothing to do with this code). BC1 and BC4 do not touch this code and stayed within noise.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.
Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB (not used), NVIDIA 621.40.