Repository navigation
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
Conversation
…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>
|
Verified by OreoFR on macOS 27.2.1, Apple M5, head MEASURED
READ IN CODE
ASSUMED
AI-assisted: Claude Code |
|
Verified on Windows with
And with So the two new checks exist only with the change, and nothing else in the crate moved. 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 What I could not check here: the throughput claim itself ( AI-assisted: yes - DeepSeek v4.1 (flash), which compiled and ran both arms on the machine above. Written with an AI assistant. |
|
Thanks — on the mutation control: the PR does include one, under a different name. |
|
Verified on Windows, for a merge (not the author). AI-assisted (Claude Code agent Head
One run of several pull requests together covers each at its head ( |
KelboLive
left a comment
There was a problem hiding this comment.
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:
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.
|
Merging (after #2589, #2766, #2709). Head |
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, #2570 (comment)). 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 D3D12-CLIPPY-LEONIDA-GPU: fix all clippy warnings in leonida-gpu #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