D3D12-FORMATS-BC7-POST-LUTS: 2.4x faster BCPack BC7 block loop (index tables, one put per field) #2714

Merged
bobcatchris merged 1 commit from upstream-pr/2714 into main 2026-11-30 12:21:39 +00:00
bobcatchris commented 2026-11-30 11:45:43 +00:00 (Migrated from github.com)

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-hle DirectStorage -> 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):

  • nibbles rebuilt every index residual with two 4-step bit folds per byte;
  • each of the 16-32 indices of a block went through add_index, whose closure branches on the widths for every element;
  • endpoint residuals were shifted and masked a column at a time;
  • bc7_pack called Bits::put (a masked u128 shift) about 45 times per block.

Now:

  • nibbles reads a 256-entry const table (the BC6H path shares it);
  • bc7_indices adds the 16 indices of a set to the reference's in one loop with the width shifts hoisted out;
  • endpoint residuals are placed with one multiply by the channel's 1 << (16 - precision) per column (a channel with no bits has 0 there, which also drops the old continue);
  • bc7_pack puts each channel's endpoints, the p-bits and each index set with one put, 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-tree of this branch with the head of each is clean).

Output is unchanged

mod reference in the tests is a verbatim copy of the previous bc7 / 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 and ref_bits > 16 is hit. Output and Result equal 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:

blocks reference ns/block now ns/block speed-up
mode 0 126.1 51.2 2.46x
mode 1 108.7 45.4 2.39x
mode 2 115.5 47.3 2.44x
mode 3 111.8 45.6 2.45x
mode 4 151.3 51.5 2.94x
mode 5 155.3 54.0 2.88x
mode 6 100.4 42.4 2.37x
mode 7 119.5 49.6 2.41x
every control code 123.1 52.2 2.36x

End to end, the existing bcpack_throughput (built from main and from this branch in separate target directories, alternated three times): BC7 (mode 6 only, empty dictionary) 324.7 / 334.2 / 324.2 ns/block on main, 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 on main, 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 in bcpack.rs. The one warning that remains is texture.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.
  • Host: Windows 11 Pro 10.0.26200, Ryzen 7 7800X3D, Rust 1.98.1 (GPU not used).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5564613728 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2714). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T11:45:43Z. --- Claim: D3D12-FORMATS-BC7-POST-LUTS ([comment](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7277472287)). 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-hle` DirectStorage -> `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): - `nibbles` rebuilt every index residual with two 4-step bit folds per byte; - each of the 16-32 indices of a block went through `add_index`, whose closure branches on the widths for every element; - endpoint residuals were shifted and masked a column at a time; - `bc7_pack` called `Bits::put` (a masked `u128` shift) about 45 times per block. Now: - `nibbles` reads a 256-entry `const` table (the BC6H path shares it); - `bc7_indices` adds the 16 indices of a set to the reference's in one loop with the width shifts hoisted out; - endpoint residuals are placed with one multiply by the channel's `1 << (16 - precision)` per column (a channel with no bits has 0 there, which also drops the old `continue`); - `bc7_pack` puts each channel's endpoints, the p-bits and each index set with one `put`, 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-tree` of this branch with the head of each is clean). ## Output is unchanged `mod reference` in the tests is a verbatim copy of the previous `bc7` / `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 and `ref_bits` > 16 is hit. Output and `Result` equal 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: | blocks | reference ns/block | now ns/block | speed-up | |---|---:|---:|---:| | mode 0 | 126.1 | 51.2 | 2.46x | | mode 1 | 108.7 | 45.4 | 2.39x | | mode 2 | 115.5 | 47.3 | 2.44x | | mode 3 | 111.8 | 45.6 | 2.45x | | mode 4 | 151.3 | 51.5 | 2.94x | | mode 5 | 155.3 | 54.0 | 2.88x | | mode 6 | 100.4 | 42.4 | 2.37x | | mode 7 | 119.5 | 49.6 | 2.41x | | every control code | 123.1 | 52.2 | 2.36x | End to end, the existing `bcpack_throughput` (built from `main` and from this branch in separate target directories, alternated three times): BC7 (mode 6 only, empty dictionary) 324.7 / 334.2 / 324.2 ns/block on `main`, 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 on `main`, 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 in `bcpack.rs`. The one warning that remains is `texture.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. - Host: Windows 11 Pro 10.0.26200, Ryzen 7 7800X3D, Rust 1.98.1 (GPU not used).
OreoFR commented 2026-11-30 11:50:55 +00:00 (Migrated from github.com)

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 head e5f9f3b47. MEASURED:

  • cargo test -p leonida-formats (main + this PR): 44 passed, 6 ignored (main: 41 passed, 5 ignored); the new bc7_blocks_match_the_reference and bc7_unused_control_code_stops_at_the_same_block pass, as do the existing bc7_* 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:
mode run 1 run 2
0 63.5 → 22.6 (2.81x) 64.0 → 22.6 (2.84x)
1 54.8 → 19.9 (2.75x) 54.8 → 19.5 (2.81x)
2 59.1 → 20.7 (2.86x) 58.7 → 20.7 (2.83x)
3 55.4 → 19.6 (2.83x) 55.0 → 19.5 (2.82x)
4 79.9 → 21.9 (3.64x) 79.2 → 21.9 (3.62x)
5 82.1 → 23.4 (3.51x) 81.6 → 23.5 (3.48x)

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.

  • Merges cleanly with main.

Not covered: textures in the title (whether the decoded blocks feed anything differently there), and x86-64 numbers.

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7277608175 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2714#issuecomment-7277608175). Originally posted by [OreoFR](https://github.com/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 head e5f9f3b47. MEASURED: - `cargo test -p leonida-formats` (main + this PR): **44 passed**, 6 ignored (main: 41 passed, 5 ignored); the new `bc7_blocks_match_the_reference` and `bc7_unused_control_code_stops_at_the_same_block` pass, as do the existing `bc7_*` 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: | mode | run 1 | run 2 | |---|---|---| | 0 | 63.5 → 22.6 (2.81x) | 64.0 → 22.6 (2.84x) | | 1 | 54.8 → 19.9 (2.75x) | 54.8 → 19.5 (2.81x) | | 2 | 59.1 → 20.7 (2.86x) | 58.7 → 20.7 (2.83x) | | 3 | 55.4 → 19.6 (2.83x) | 55.0 → 19.5 (2.82x) | | 4 | 79.9 → 21.9 (3.64x) | 79.2 → 21.9 (3.62x) | | 5 | 82.1 → 23.4 (3.51x) | 81.6 → 23.5 (3.48x) | 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. - Merges cleanly with main. Not covered: textures in the title (whether the decoded blocks feed anything differently there), and x86-64 numbers. AI-assisted: Claude Code
merzleckeier commented 2026-11-30 12:15:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T12:15:26Z.


Independent full-decoder check on x86-64. I compared main ec1d85846 and that base with e5f9f3b47 merged (tree f529cc0e) on CachyOS, Ryzen 7 7800X3D, Rust 1.98.1. All inputs were synthetic, CPU only.

  • MEASURED, output parity. The public decode returned 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.
  • MEASURED, stock tests. cargo test -p leonida-formats --lib passed in debug and release, 42 passed / 5 ignored on main and 44 passed / 6 ignored with the PR.
  • MEASURED, full decode throughput. Optimized builds pinned to one logical CPU, six samples per revision in ABBA order repeated three times. Each sample decodes the same 16,384-block mixed-mode stream 64 times per format. Values are median ns/block (min–max).
Format Main PR Median throughput change
BC7 335.62 (335.11–336.97) 267.91 (267.14–270.99) +25.28%
BC6H 318.74 (317.80–321.51) 305.57 (305.53–318.01) +4.31%

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7278177400 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2714#issuecomment-7278177400). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T12:15:26Z. --- **Independent full-decoder check on x86-64.** I compared `main` `ec1d85846` and that base with `e5f9f3b47` merged (tree `f529cc0e`) on CachyOS, Ryzen 7 7800X3D, Rust 1.98.1. All inputs were synthetic, CPU only. - **MEASURED, output parity.** The public `decode` returned 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. - **MEASURED, stock tests.** `cargo test -p leonida-formats --lib` passed in debug and release, 42 passed / 5 ignored on main and 44 passed / 6 ignored with the PR. - **MEASURED, full decode throughput.** Optimized builds pinned to one logical CPU, six samples per revision in ABBA order repeated three times. Each sample decodes the same 16,384-block mixed-mode stream 64 times per format. Values are median ns/block (min–max). | Format | Main | PR | Median throughput change | |---|---:|---:|---:| | BC7 | 335.62 (335.11–336.97) | 267.91 (267.14–270.99) | +25.28% | | BC6H | 318.74 (317.80–321.51) | 305.57 (305.53–318.01) | +4.31% | 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.
enlight3d commented 2026-11-30 19:10:48 +00:00 (Migrated from github.com)

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 as df151972c. "Before" is the merge's first parent 8421f9b95, "after" is df151972c. 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_reference among 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 in bcpack.rs. The one warning, texture.rs:1346, is there on both.
  • Mutation (after, debug): bc7_indices with the residual order flipped one column early (k < 3 -> k < 2). bc7_blocks_match_the_reference and bc7_unused_control_code_stops_at_the_same_block both 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.

blocks fastest reference ns/block fastest now ns/block speed-up, median of 5 (min-max)
mode 0 110.7 46.5 2.64x (2.33-2.92)
mode 1 93.6 40.0 2.33x (1.95-2.39)
mode 2 100.3 42.0 2.42x (2.36-2.58)
mode 3 100.0 41.1 2.65x (2.36-3.60)
mode 4 131.0 43.5 3.26x (3.01-3.42)
mode 5 160.8 53.1 3.05x (2.77-3.58)
mode 6 94.9 36.8 2.45x (2.10-2.84)
mode 7 125.0 45.7 2.73x (2.63-3.08)
all control codes 114.9 50.2 2.64x (2.29-3.24)

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7288967971 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2714#issuecomment-7288967971). Originally posted by [enlight3d](https://github.com/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 as df151972c. "Before" is the merge's first parent 8421f9b95, "after" is df151972c. 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_reference` among 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 in `bcpack.rs`. The one warning, `texture.rs:1346`, is there on both. - Mutation (after, debug): `bc7_indices` with the residual order flipped one column early (`k < 3` -> `k < 2`). `bc7_blocks_match_the_reference` and `bc7_unused_control_code_stops_at_the_same_block` both 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. | blocks | fastest reference ns/block | fastest now ns/block | speed-up, median of 5 (min-max) | |---|---:|---:|---:| | mode 0 | 110.7 | 46.5 | 2.64x (2.33-2.92) | | mode 1 | 93.6 | 40.0 | 2.33x (1.95-2.39) | | mode 2 | 100.3 | 42.0 | 2.42x (2.36-2.58) | | mode 3 | 100.0 | 41.1 | 2.65x (2.36-3.60) | | mode 4 | 131.0 | 43.5 | 3.26x (3.01-3.42) | | mode 5 | 160.8 | 53.1 | 3.05x (2.77-3.58) | | mode 6 | 94.9 | 36.8 | 2.45x (2.10-2.84) | | mode 7 | 125.0 | 45.7 | 2.73x (2.63-3.08) | | all control codes | 114.9 | 50.2 | 2.64x (2.29-3.24) | 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2714
No description provided.