Skip to content

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

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:perf/d3d12-formats-bc7-post-luts
Nov 30, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:perf/d3d12-formats-bc7-post-luts

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

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 (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.
  • Host: Windows 11 Pro 10.0.26200, Ryzen 7 7800X3D, Rust 1.98.1 (GPU not used).

…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>
@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Checked by OreoFR on macOS 27.2.1, Apple M5 (arm64), Rust 1.98.1, main d367675, PR head e5f9f3b. 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

Copy link
Copy Markdown
Contributor

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.

@aslan2142
aslan2142 merged commit df15197 into ProjectLeonida:main Nov 30, 2026
5 checks passed
@enlight3d

Copy link
Copy Markdown
Contributor

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 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants