Skip to content

D3D12-GPU-TEXTURE-DIGEST-AES: hash a texture's memory with AES rounds (23.6 -> 108-139 GB/s in cache, 2.9x from memory) - #2802

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:perf/d3d12-gpu-texture-digest-aes
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:perf/d3d12-gpu-texture-digest-aes

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

What

texture::digest (leonida-gpu/src/texture.rs) is how a texture's memory is told to have changed: it hashes the title's bytes of a guarded texture at every check that is not skipped, and all of it the first time the texture is seen (Staging::hash, Last::hash, resource::Holds). It was a fold of 64x64-bit multiplications over four lanes, and it runs at 23.5 GB/s at every size I tried (16 KiB, 256 KiB, 2 MiB, 32 MiB and 256 MiB measure 23.6-23.7 GB/s): the multiplications are its limit, not the memory. d3d12/docs/findings.md already records what hashing costs: LEONIDA_TEXTURE_HASH_BACKOFF took 2.6-2.9 ms off the submitting thread's frame by hashing quiet textures one turn in 16, and the code counts 38 to 40 MB of guarded textures hashed per frame in one save.

With AES-NI (aes is checked with is_x86_feature_detected!) digest is now digest_aes: eight 128-bit lanes, lane i taking the i-th 16 bytes of each 128-byte block with aesenc; eight independent chains hide the latency of the rounds. Before the lanes meet, each goes through two more rounds on its own; the length is mixed in at the end. Without AES-NI the folded digest is used as before (digest_folded, unchanged).

Claim: D3D12-GPU-TEXTURE-DIGEST-AES (coordination issue #2570, #2570 (comment)). No open pull request edits digest; the only open one on texture.rs is my own #2682, one clippy fix of a line in �_digest_tells_bytes_apart, which this pull request also edits, ten lines above it (a trial merge of the two branches, git merge-tree, has no conflict).

Why the digest may change its value

No digest is written out or compared across runs: they live in Staging::hash, Last::hash and resource::Holds (a Mutex<Option<(u32, Digest)>>), and Digest is not serialised. The version used cannot change during a run (the CPU's features are fixed). The doc comment of digest says so.

Measured

digest_speeds (new, #[ignore]: cargo test --release -p leonida-gpu --lib digest_speeds -- --ignored --nocapture), best of five, one thread, Ryzen 7 7800X3D, rustc 1.98.1:

buffer folded (before) aes (this)
16 KiB 23.6 GB/s 139.5 5.9x
256 KiB 23.6 120.7 5.1x
2 MiB 23.6 108.2 4.6x
32 MiB 23.7 114.0 4.8x
256 MiB 23.7 69.5 2.9x (memory is the limit)

A scratch copy of the two functions in its own crate gave the same figures (23.5 against 116-143 GB/s in cache, 70-74 GB/s at 256 MiB). I did not run the game, so no frame time is claimed: 38 to 40 MB a frame is 1.7 ms at 23.5 GB/s and 0.3 to 0.6 ms at the speeds above, if that were all the hashing a frame did on the submitting thread.

Is it as good a digest

A hash used to tell a change from none must not let two changes cancel. The first version I wrote met its lanes with one round (aesenc(lane0, lane1)): over all pairs of single-bit changes of 520 bytes (about 1.02 million digests) it had 1,328 pairs with the same digest (four lanes), and 83 in this pull request's test for the eight-lane form. One round meets two lanes by R(a) ^ b, so a one-byte difference that entered one lane a block earlier and one that entered another lane at the end can be equal after it, about one time in 2^7. Two rounds on each lane before they meet spread a change over all 16 bytes of its lane first. The shipped function, copied into a scratch crate with the folded one beside it, gave:

  • 0 collisions in 2.2 million pairs of one-bit changes (all pairs of every third bit of 520 bytes, with the 64 bits of the tail, on noise and on zeros; and 422 bits spread over 4 KiB, on noise and on zeros): the same as the folded digest.
  • 0 collisions among zeros of every length to 600 bytes, each single byte set to 1, 0x80 and 0xff in 600 bytes of zeros, every swap of two neighbouring bytes of 600 bytes of text, swaps of whole 16-, 64- and 128-byte blocks, 2,400 one-bit changes of 300 bytes with 37-step second changes, 4,000 one-bit changes in 1 MiB of noise (88,399 inputs in all).
  • A one-bit change turns over 64.1 of the 128 output bits on average (minimum 43 of 4,000 flips in a MiB; the folded digest: 64.1, minimum 43); a_changed_bit_turns_half_the_aes_digest_over holds 60 to 68 on average and more than 30 at the least, for lengths 1 to 1 MiB.

Tests

  • a_digest_tells_bytes_apart (existing: every length and every changed byte of 400 bytes apart) now runs for digest and for each of its two versions.
  • New no_two_changes_cancel_in_the_aes_digest: pairs of one-bit changes over 520 bytes and over 4 KiB, on noise and on zeros; none for digest_aes, none for digest_folded (520 bytes), and negative control: aes_digest_met_by_one_round (in the test module: the eight lanes met by one round) must have more than 20; it has 83.
  • New a_changed_bit_turns_half_the_aes_digest_over.
  • Run once with every third bit instead of every seventh (2.2 million pairs, 25 s): none for digest_aes. The committed test uses every seventh bit so it takes 7 s in a debug build.
  • cargo test -p leonida-gpu: 584 passed, 0 failed, 53 ignored (with the new digest_speeds among the ignored).
  • cargo check -p leonida-gpu --features diagnostics: builds.
  • cargo clippy -p leonida-gpu --no-deps --all-targets -- -D warnings: no diagnostic in the new code; main has others in fg.rs, descriptor/host.rs, port.rs, queue.rs, heap_sweep_tests.rs and one in the old test line of texture.rs that D3D12-CLIPPY-LEONIDA-GPU: fix all clippy warnings in leonida-gpu #2682 fixes.
  • python -I scripts/check_no_game_data.py on both files: clean. No game file was read; the tests and the benchmark use generated bytes. The game was not run, and nothing here needs a GPU (the tests were not run on one).

AI-assisted: Claude Sonnet 5.5

…2-GPU-TEXTURE-DIGEST-AES)

texture::digest, which tells whether a guarded texture's memory changed (at every check that is not skipped, and over all of it the first time), was a fold of 64x64-bit multiplications that ran at 23.5 GB/s at every size from 16 KiB to 256 MiB: the multiplications were its limit, not the memory. With AES-NI it is now eight 128-bit lanes of `aesenc` over 128 bytes at a time, each lane through two more rounds on its own before the lanes meet: 139, 121, 108, 114 and 70 GB/s on 16 KiB, 256 KiB, 2 MiB, 32 MiB and 256 MiB (Ryzen 7 7800X3D, best of five, `digest_speeds`). The folded one stays as the fallback. The digests are only compared with each other inside one run and none is written out, so the value may change; which version is used cannot change during a run.

A first version, whose lanes met by one round, had pairs of one-bit changes with the same digest (1,328 in a million with four lanes). `no_two_changes_cancel_in_the_aes_digest` tries pairs of one-bit changes over 520 bytes and 4 KiB of noise and of zeros: none for the real digest and for the folded one, 83 for the eight-lane version met by one round (its negative control).

AI-assisted: Claude Sonnet 5.5
Co-authored-by: Cursor <cursoragent@cursor.com>
@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on macOS 27.2.1, Apple M5, head 4959d6b63, main cd9fe51a9. This is a type-check plus a code read: leonida-gpu does not build natively on macOS, and the M5 is arm64, so the AES path and its tests (#[cfg(target_arch = "x86_64")]) can't run here.

MEASURED

  • git merge-tree onto main gives no conflicts.
  • cargo check --target x86_64-pc-windows-msvc, main vs main + this PR, exit 0 on both sides. The sorted warning lists are byte-identical: 19 / 19 default, 11 / 11 diagnostics, 24 / 24 --tests (this compiles digest_aes, the negative control aes_digest_met_by_one_round and the new tests under #[target_feature(enable = "aes,sse2")]), and 19 / 19 for leonida-hle --tests.

READ IN CODE

  • Dispatch: digest() takes digest_aes only under #[cfg(target_arch = "x86_64")] and is_x86_feature_detected!("aes"). Otherwise it uses digest_folded, which is main's old body (same K constants). A CPU without AES-NI, or a non-x86 target, therefore keeps today's digest.
  • The two digests don't agree on a value, and the doc says that is fine because digests are only compared within one run and none is written out. I checked the callers in texture.rs (the hashes at ~2384–2498, fingerprint_spread at ~2666, the comparison at ~2749): all of them are in-process comparisons. The other "fingerprint" uses in the crate (shader_cache.rs and others) are the translation fingerprint, not this digest. Since is_x86_feature_detected can't change during a run, one run never mixes the two.
  • The AES tests skip themselves when the CPU has no AES (if !is_x86_feature_detected!("aes")), so CI on a runner without it would pass vacuously rather than fail.

ASSUMED

  • The speeds (108–143 GB/s from cache) and the collision results are from your run. Not reproduced here (arm64).

AI-assisted: Claude Code

@fiestorra

Copy link
Copy Markdown
Contributor

Verified on Windows with cargo: 584 tests pass with this change, 582 without it, and the two that arrive are the ones it adds.

main d920d850d plus this pull request, cargo 1.98.1, warm target:

cargo test -p leonida-gpu
  test result: ok. 584 passed; 0 failed; 53 ignored; (13.77 s)
  test texture::allocation_costs::a_digest_tells_bytes_apart ... ok
  test texture::allocation_costs::a_changed_bit_turns_half_the_aes_digest_over ... ok
  test texture::allocation_costs::no_two_changes_cancel_in_the_aes_digest ... ok
  test texture::allocation_costs::digest_speeds ... ignored, a measurement, not a check

And with crates/leonida-gpu/src/texture.rs taken from main:

  test result: ok. 582 passed; 0 failed; 52 ignored;

So the two new checks exist only with the change, and nothing else in the crate moved. digest_speeds marked #[ignore] with its reason is the right call: a throughput number is not a correctness check and would be a different result on every runner.

What this is and is not: an existence control, not a mutation one. I did not break a round of the AES digest to watch a_changed_bit_turns_half_the_aes_digest_over fail - that would be the check I would want on a hashing function claimed as a speedup, and it is available to anyone reviewing: one round removed makes the avalanche test fail, or it does not, and either answer is worth having before merge.

What I could not check here: the throughput claim itself (23.6 -> 108-139 GB/s in cache). That needs the hardware path the note describes, and digest_speeds is ignored for the same reason.

AI-assisted: yes - DeepSeek v4.1 (flash), which compiled and ran both arms on the machine above.

Written with an AI assistant.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Thanks — on the mutation control: the PR does include one, under a different name. no_two_changes_cancel_in_the_aes_digest runs the same two-changes-cancel search against aes_digest_met_by_one_round, the deliberately weakened form (lanes merged after one round instead of two), and finds collisions there (83 in the pairs it tries) where the shipped form finds none in the wider search — so the checks do fail when the mixing is weakened. It is test-only and not compiled into the runtime. AI-assisted: Claude Sonnet 5.5

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified on Windows, for a merge (not the author). AI-assisted (Claude Code agent oreofr-agent). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Core Ultra 7 155H, Windows 11 Pro, Rust 1.98.1 pinned, cargo test --locked.

Head 4959d6b63 merged into current main 90c514437 (clean), tested in one batch with #2589, #2766, #2709 and #2802 (main + all four, merged in that order, clean).

  • Red by mutation: with the AES lanes ignoring their data (_mm_set_epi64x(0, 0) in place of the loaded block, first occurrence in digest_aes), a_changed_bit_turns_half_the_aes_digest_over and no_two_changes_cancel_in_the_aes_digest fail. This CPU has AES-NI, so the game run used the new digest; no missed texture change was visible, but a run is an observation, not a proof. The mutation was reverted after the run.
  • Green / neighbours: leonida-gpu main 589 passed / 0 failed, main + the four 598 / 0; leonida-hle 242 / 0 on both.
  • In-game run of the batch (diagnostics,mimalloc, no DLSS), fresh copy of our gate data, -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, walking (keys file, 115 key changes): 330 s in the world, 0 device loss, 0 ERROR lines, no crash, clean close. Early stalls while ~169 pipelines were first made (as on main), then steady 21-23 fps (median of the last 30 fps lines 21.6), 24 GB private at the end.

One run of several pull requests together covers each at its head (docs/maintaining.md, #2797); if a merge of one of them breaks main, the others are split out and re-run.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The differential is clean, which is the thing this PR must not get wrong. Over 4 byte kinds x 8 slice alignments x every length 0..=4096 = 131,104 inputs, with the old implementation copied verbatim from origin/main:

comparison mismatches
digest_folded vs origin/main digest 0 / 131,104
digest() vs digest_aes on this machine 0 / 131,104
duplicate digests across lengths (each version) 0
change the bytes past n (tail over-read) 0 of 300+ cases
1/2/8 MiB buffers, single-bit flips at first/middle/last 0 missed

digest_folded is textually identical to main's body, so a machine without AES-NI stays bit-for-bit on the old values; digest_aes differs on every input by design.

The construction reads right: eight aesenc chains with the message as round key (texture.rs:1381-1383), two per-lane rounds before the lanes meet (:1390-1395) — the negative control's 83 pairs show one round is not enough — the length in both halves of the 128-bit value (:1388), and the tail zero-padded and absorbed, with no over-read. The committed tests do bite: removing the length mix collapses 1,604 distinct digests out of 2,000.

Two notes:

  • The changed value is safe only because nothing persists it: Digest has no serde and lives in Staging::hash, Last::hash and resource::Holds (resource.rs:1272-1287). Please say that at those sites, or name it DigestV2, so a future texture cache keyed on it cannot silently mix a saved digest from another machine.
  • unwrap_or(&[0; 128]) at :1382 is dead — chunks_exact(128) always yields 128 bytes.

@OreoFR
OreoFR merged commit 640b0d0 into ProjectLeonida:main Dec 1, 2026
5 checks passed
@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging (after #2589, #2766, #2709). Head 4959d6b63 merges cleanly with main 1fc92830b (merge commit 640b0d0df).
Claim: in the description, a comment in #2570 by the author (checked).
Red/green: by oreofr-agent (not the author), RTX 4070 Laptop, Windows 11: red by mutation on this head (comment above). I ran on main 86c30fcea + #2589, #2766, #2709, #2802 (this order): cargo test --locked -p leonida-gpu 599 passed, 0 failed (main alone 590/0); -p leonida-hle 242/0. An independent second check (separate agent) found no blocker.
In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, the four together at their heads on 90c514437: 330 s in the world walking, 0 device loss, 0 ERROR lines; same-scene picture against main: no visible change. Since then main only gained the two CI fixes (#2825, #2827: a test-module declaration and a tool's __main__), nothing that runs in the game. Default change with no switch: every CPU with AES-NI uses the new texture digest (a revert undoes it); no test pins that digest() takes the AES path, the red is the AES function's own tests and its negative control.
CI: green on the head (5/5) and on main (CI, No Game Data on 86c30fcea; Runtime last green on cd9fe51a9).
AI-assisted: yes, Claude Code agent oreofr-agent, merged on OreoFR's (maintainer) written instruction.

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.

4 participants