Skip to content

D3D12-FORMATS-BCN-POST-SWAR: eight-byte-wide BC1/BC3/BC4/BC5 block loop after the BCPack lanes (1.1-1.3x) - #2715

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

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

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

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

  • every block summed 8 endpoints and 32 indices with their references one element at a time, each with its own wrapping_add and width mask;
  • quads and pairs wrote the residuals to Plain::ix one byte at a time (16-32 byte stores, which the vector loads after them then had to wait for);
  • bc1_color and bc4_alpha looked 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:

  • Widths holds each format's element widths as masks (a word for the endpoints, four for the indices), and plain sums 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.
  • quads reads a 256-entry const table and fills the four 4-pixel rows as words; pairs builds eight pixels at a time.
  • bc1_codes and bc4_codes turn 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), and pack2 / pack3 put them in their 2- or 3-bit fields with three shift-and-mask steps.
  • interleave reads a 16-entry const table.

The lane loop, Tables::parse, references and pick are untouched, so this is independent of #2690 (lanes), #2714 (BC7 block loop) and #2567 (lanes, BC2, table parsing). #2567's BC2 calls bc1_color, references and pick: their signatures are unchanged. git merge-tree of this branch with each of the three heads is clean, and this branch merged with #2567 and #2714 gives 48 passed / 7 ignored in cargo test -p leonida-formats --lib, debug and --release. (#2690 and #2567 conflict with each other, not with this.)

Output is unchanged

mod reference_plain in the tests is a verbatim copy of the previous plain / quads / pairs / *_residual / bc1_color / bc4_alpha / *_pack / interleave / references / zz, with its own copy of Widths. 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_codes give 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 / pack3 put 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 plain against 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 of pick are always right. Three runs agreed to 0.1 ns:

format refs reference ns/block now ns/block speed-up
BC1 random 15.8 14.2 1.11x
BC1 none 15.6 13.7 1.14x
BC3 random 24.6 19.9 1.23x
BC3 none 23.8 19.1 1.24x
BC4 random 14.6 11.3 1.29x
BC4 none 14.0 10.7 1.31x
BC5 random 22.3 18.5 1.21x
BC5 none 21.7 17.9 1.22x

Before the change, the same harness measured the production plain at 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 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).
  • rustfmt --edition 2024 over the lines this PR adds (the file has other lines that are not rustfmt-clean on main; those are left as they are).
  • 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).

…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>
@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 ec1d858, PR head f536b3d. MEASURED:

format refs run 1 run 2
BC1 random / none 10.9 → 6.0 (1.81x) / 10.4 → 5.4 (1.93x) 10.0 → 5.4 (1.85x) / 10.4 → 5.4 (1.92x)
BC3 random / none 19.7 → 9.9 (1.99x) / 18.6 → 9.2 (2.03x) 18.2 → 9.2 (1.98x) / 18.9 → 9.6 (1.98x)
BC4 random / none 7.5 → 24.5 (0.30x) / 7.6 → 25.4 (0.30x) 7.5 → 24.5 (0.31x) / 7.4 → 24.4 (0.30x)
BC5 random / none 16.4 → 29.4 (0.56x) / 16.3 → 31.6 (0.52x) 16.4 → 29.4 (0.56x) / 16.3 → 29.4 (0.55x)

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 (bc4_codes / pack3) would be the place to look. Not a blocker for x86-64 players, but worth either keeping the old BC4/BC5 path where it is faster, or checking whether the x86-64 gain is real on more than one CPU.

AI-assisted: Claude Code

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent full-decoder check on x86-64. CachyOS, Ryzen 7 7800X3D, Rust 1.98.1, CPU only. I compared main ec1d85846 with GitHub merge 53e345ead for head f536b3d50. All inputs were synthetic.

  • MEASURED, output parity. I fed identical encoded streams to the public decode on both revisions. All 72 outputs matched byte for byte (259,200 bytes). The matrix covers BC1/3/4/5, 1/7/255/256/257/1024 blocks, and mixed, zero, or fixed distance-255 references with a seeded dictionary. A local one-bit shift error in the BC1 color packer changed all BC1 and BC3 outputs, as expected.
  • MEASURED, stock tests. cargo test -p leonida-formats --lib passed on both revisions in debug and release, 42 passed / 5 ignored on main and 45 passed / 6 ignored on the PR.
  • MEASURED, full decode throughput. One pinned logical CPU, optimized builds, six timed samples per revision in ABBA order repeated three times. Each sample decodes the same 16,384-block stream 48 times per format. Values are median ns/block (min–max).
Format Main PR Median throughput change
BC1 93.67 (93.12–94.01) 91.82 (91.17–91.98) +2.02%
BC3 182.72 (180.64–183.21) 175.25 (174.86–175.69) +4.27%
BC4 95.32 (94.74–95.48) 94.62 (94.03–94.97) +0.73%
BC5 183.80 (183.24–185.17) 176.81 (175.95–177.24) +3.96%
BC7 control 318.96 (318.59–319.37) 319.11 (318.14–322.81) −0.05%

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.

@aslan2142
aslan2142 merged commit 97a7ae7 into ProjectLeonida:main Nov 30, 2026
5 checks passed
@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

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 bcpack_throughput (--release -- --ignored), two runs on each revision, ns/block:

format ec1d85846 (before) 497bdd039 (main now, #2714 + #2715 in)
BC1 82.3 / 57.5 68.4 / 53.5
BC4 57.1 / 55.5 70.0 / 68.7
BC7 191.5 / 187.4 156.4 / 157.0
  • BC4: about 23 % slower on arm64 for the whole decode (both runs, ranges apart), where the x86-64 table shows +0.73 %. This matches the post-stage numbers above (BC4 7.5 → 24.5 ns/block): the eight-wide BC4 code path costs this CPU more than the byte loop did.
  • BC1's first run on the old revision is a warm-up outlier, so no conclusion there; BC7 gains ~17 %, which is D3D12-FORMATS-BC7-POST-LUTS: 2.4x faster BCPack BC7 block loop (index tables, one put per field) #2714.
  • bcpack_throughput has no BC3/BC5 case, so those are not measured here for the whole decode.

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

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

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 (d3d12/rust-toolchain.toml), --release, CPU only. MEASURED.

  • Revisions: before = df151972c (97a7ae7f3^1, D3D12-FORMATS-BC7-POST-LUTS: 2.4x faster BCPack BC7 block loop (index tables, one put per field) #2714 in, this PR not); after = 97a7ae7f3 (this PR's merge). Separate worktrees and target dirs.
  • Benchmarks: the crate's own ignored tests, the same ones as the arm64 comments above: plain_post_throughput (post stage, old code vs new in one binary; after only) and bcpack_throughput (whole decode; both revisions).
  • Pinned: on this hybrid CPU an unpinned run moves 125-159 ns/block between runs, so each run is pinned to one logical CPU at High priority: CPU 12 (a P-core, ~14 ns/block BC1 post) and CPU 2 (an E-core, ~24 ns). Nothing else heavy was running.

Post stage (plain_post_throughput at 97a7ae7f3), ns/block, reference -> now, random refs / no refs:

format P-core run 1 P-core run 2 E-core (3 runs, within 0.1 ns)
BC1 14.1 -> 13.9 / 13.9 -> 13.7 13.9 -> 13.9 / 13.6 -> 13.5 24.3 -> 23.8 / 23.1 -> 22.7
BC3 21.7 -> 16.7 / 20.0 -> 15.8 21.6 -> 16.7 / 20.1 -> 15.6 40.0 -> 32.5 / 38.8 -> 31.4
BC4 13.3 -> 10.2 / 13.3 -> 9.5 13.3 -> 10.5 / 17.6 -> 14.5 23.3 -> 18.1 / 22.0 -> 16.9
BC5 19.8 -> 15.5 / 18.5 -> 14.1 19.7 -> 15.3 / 18.4 -> 14.8 35.9 -> 29.8 / 34.8 -> 28.7

Whole decode (bcpack_throughput), ns/block, median of 6 runs a side (pre, post, post, pre x3):

core format before df151972c after 97a7ae7f3 change
P BC1 103.1 95.8 -7 %
P BC4 101.3 93.8 -7 %
P BC7 (control) 299.0 290.7 -3 %
E BC1 164.5 157.4 -4 %
E BC4 163.8 153.9 -6 %
E BC7 (control) 477.4 477.8 0 %
  • BC4 and BC5 do not regress on x86-64. In the post stage, BC4 is 1.21-1.40x faster and BC5 1.21-1.32x faster, on both core types. In the whole decode, BC4 is 6-7 % faster; the E-core ranges are apart, and the P-core runs are noisier. The arm64 slowdown (BC4 7.5 -> 24.5 ns/block on the M5) does not show here, so it looks specific to aarch64.
  • BC3 gains as described (1.23-1.30x).
  • BC1's post stage is flat on this CPU (1.00-1.02x), against 1.11-1.14x in the description (Ryzen 7 7800X3D). The whole-decode BC1 still reads 4-7 % faster.
  • bcpack_throughput has no BC3/BC5 case, so BC3 and BC5 are not measured for the whole decode here.

This run was AI-assisted (Claude Code, oreofr-agent).

@enlight3d

Copy link
Copy Markdown
Contributor

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 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 44 passed / 0 failed / 6 ignored; after 47 / 0 / 7 in debug (overflow checks on) and 47 / 0 / 7 with --release. The extra ones are the three new tests and the ignored benchmark. plain_blocks_match_the_reference passes in both builds.
  • cargo clippy -p leonida-formats --all-targets, before and after: no warning in bcpack.rs.
  • Mutation (after): bc4_codes without the xor of the end rule. index_codes_are_the_tables, bc4_blocks_with_and_without_references and plain_blocks_match_the_reference all fail.

Output before and after: a local harness (not pushed) calls the production plain the way decode does, on the same 16,384 random records per format, built once from "before" and once from "after". The output digests are equal for BC1, BC3, BC4 and BC5 in every run.

Speed before/after per format, the same harness: cargo test --release -p leonida-formats --lib --locked local_plain_before_after -- --ignored --nocapture, with before and after alternated, n = 3 each. Each value is the fastest of 40 batches of 3 passes over 16,384 blocks:

format before ns/block (3 runs) after ns/block (3 runs) speed-up
BC1 15.8 / 14.5 / 14.5 11.8 / 11.8 / 11.8 1.23-1.34x
BC3 21.5 / 20.1 / 20.4 14.2 / 14.3 / 14.1 1.41-1.51x
BC4 15.5 / 14.1 / 14.1 8.7 / 8.8 / 8.6 1.60-1.78x
BC5 20.6 / 18.7 / 18.9 13.3 / 13.7 / 13.2 1.36-1.55x

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 (plain_post_throughput, production code against the verbatim reference copy in the same binary, n = 3): BC1 1.04/0.99/0.99x with random references and 1.08/0.99/1.00x without; BC3 1.35/1.27/1.30x and 1.38/1.27/1.29x; BC4 1.20/1.36/1.36x and 1.44/1.41/1.40x; BC5 0.89/1.27/1.24x and 1.19/1.29/1.30x. The first run was disturbed by other jobs, which is where the one 0.89x comes from. As the PR says, the reference copy runs faster than the old production code did, so this in-binary figure is the conservative one; the before/after builds above are the real change.

End to end (bcpack_throughput, lanes included, BC1 and BC4 only): before vs after stays within this machine's noise (BC1 82.6-108.9 ns/block before, 82.8-129.4 after), as the PR also found.
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