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
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2802"
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-12-01T00:04:05Z.
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.mdalready records what hashing costs:LEONIDA_TEXTURE_HASH_BACKOFFtook 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 (
aesis checked withis_x86_feature_detected!)digestis nowdigest_aes: eight 128-bit lanes, laneitaking thei-th 16 bytes of each 128-byte block withaesenc; 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, https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7295616442). No open pull request edits
digest; the only open one ontexture.rsis 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::hashandresource::Holds(aMutex<Option<(u32, Digest)>>), andDigestis not serialised. The version used cannot change during a run (the CPU's features are fixed). The doc comment ofdigestsays 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: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 byR(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:a_changed_bit_turns_half_the_aes_digest_overholds 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 fordigestand for each of its two versions.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 fordigest_aes, none fordigest_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.a_changed_bit_turns_half_the_aes_digest_over.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 newdigest_speedsamong 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;mainhas others infg.rs,descriptor/host.rs,port.rs,queue.rs,heap_sweep_tests.rsand one in the old test line oftexture.rsthat #2682 fixes.python -I scripts/check_no_game_data.pyon 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
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T00:09:44Z.
Verified by OreoFR on macOS 27.2.1, Apple M5, head
4959d6b63, maincd9fe51a9. This is a type-check plus a code read:leonida-gpudoes 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-treeonto 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 / 11diagnostics, 24 / 24--tests(this compilesdigest_aes, the negative controlaes_digest_met_by_one_roundand the new tests under#[target_feature(enable = "aes,sse2")]), and 19 / 19 forleonida-hle --tests.READ IN CODE
digest()takesdigest_aesonly under#[cfg(target_arch = "x86_64")]andis_x86_feature_detected!("aes"). Otherwise it usesdigest_folded, which is main's old body (sameKconstants). A CPU without AES-NI, or a non-x86 target, therefore keeps today's digest.texture.rs(the hashes at ~2384–2498,fingerprint_spreadat ~2666, the comparison at ~2749): all of them are in-process comparisons. The other "fingerprint" uses in the crate (shader_cache.rsand others) are the translation fingerprint, not this digest. Sinceis_x86_feature_detectedcan't change during a run, one run never mixes the two.if !is_x86_feature_detected!("aes")), so CI on a runner without it would pass vacuously rather than fail.ASSUMED
AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-01T01:20:06Z.
Verified on Windows with
cargo: 584 tests pass with this change, 582 without it, and the two that arrive are the ones it adds.maind920d850dplus this pull request,cargo 1.98.1, warm target:And with
crates/leonida-gpu/src/texture.rstaken frommain:So the two new checks exist only with the change, and nothing else in the crate moved.
digest_speedsmarked#[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_overfail - 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, anddigest_speedsis 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.
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-12-01T02:06:31Z.
Thanks — on the mutation control: the PR does include one, under a different name.
no_two_changes_cancel_in_the_aes_digestruns the same two-changes-cancel search againstaes_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.5Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T03:16:30Z.
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
4959d6b63merged into currentmain90c514437(clean), tested in one batch with #2589, #2766, #2709 and #2802 (main + all four, merged in that order, clean)._mm_set_epi64x(0, 0)in place of the loaded block, first occurrence indigest_aes),a_changed_bit_turns_half_the_aes_digest_overandno_two_changes_cancel_in_the_aes_digestfail. 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.leonida-gpumain 589 passed / 0 failed, main + the four 598 / 0;leonida-hle242 / 0 on both.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 breaksmain, the others are split out and re-run.Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:07:37Z.
Review: COMMENTED
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:digest_foldedvsorigin/maindigestdigest()vsdigest_aeson this machinen(tail over-read)digest_foldedis textually identical to main's body, so a machine without AES-NI stays bit-for-bit on the old values;digest_aesdiffers on every input by design.The construction reads right: eight
aesencchains 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:
Digesthas no serde and lives inStaging::hash,Last::hashandresource::Holds(resource.rs:1272-1287). Please say that at those sites, or name itDigestV2, so a future texture cache keyed on it cannot silently mix a saved digest from another machine.unwrap_or(&[0; 128])at:1382is dead —chunks_exact(128)always yields 128 bytes.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T05:34:47Z.
Merging (after #2589, #2766, #2709). Head
4959d6b63merges cleanly with main1fc92830b(merge commit640b0d0df).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-gpu599 passed, 0 failed (main alone 590/0);-p leonida-hle242/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 thenmainonly 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 oncd9fe51a9).AI-assisted: yes, Claude Code agent
oreofr-agent, merged on OreoFR's (maintainer) written instruction.