GPU: a fast content digest for untracked textures, and their commit check kept per remap epoch #2281

Open
alessiodeia wants to merge 2 commits from upstream-pr/2283 into main
alessiodeia commented 2026-11-25 09:40:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alessiodeia on 2026-11-25T09:40:09Z.


Closes #916. Opened as the issue triage asked, carried onto current main (940d32f) with the unmap-race fix from that thread.

What it changes

Staging::refresh_now, for sampled textures whose memory is not tracked:

  • Digest. BLAKE3 over the whole tiled memory is replaced by a 128-bit non-cryptographic digest: four 64-bit lanes, 32 bytes per step, and a SplitMix-style finish. Its only job is to tell whether the bytes changed since the last detile.
  • Commit check per remap epoch. title_memory_remapped now advances a remap epoch. A range made only of mapped views of physical pages (MEM_MAPPED) can only change through the runtime, so a texture keeps "committed" for it until the epoch moves. Any other range is asked through VirtualQuery every time, as before. committed() keeps main's overflow and PAGE_NOACCESS/PAGE_GUARD checks.
  • Unmap race (found by @DrDarkDK on #916). xmem::unmap notifies before UnmapViewOfFile2, so a refresh between the two could cache the still-mapped view under the new epoch. The new title_memory_unmapped advances it once more after the view is gone.

The bytes are still digested in full on every refresh. Pages remapped under a stable address still show as a change, which was @pickles976's concern on #916.

On main the tracked/untracked split is already decided per texture (Staging::tracked), so the original issue's "known not watched" cache is no longer needed and is not part of this PR.

Numbers (RTX 2070 SUPER, Windows 11)

  • Benchmark, 160 MiB, release: BLAKE3 70.7 ms (2.26 GB/s), new digest 11.5 ms (13.9 GB/s), 6.1× (content_hash_vs_blake3_throughput, ignored by default).
  • Measured on the issue's base (ffe310e): the world frame hashed about 156 MB of untracked textures with nothing changed, about 383 ms per frame. I haven't measured the world on current main on this card, because main doesn't reach it here (#1753 removes the device first).

Tests

  • content_hash_detects_single_bit_tail_and_order_changes: single-bit flips in the 32-byte blocks, the 8-byte words and the tail, plus a swap and a length change.
  • a_mapped_range_cached_before_its_unmap_is_asked_again: walks xmem::unmap's order (notify, still Mapped, unmap, No, notify again). Negative control: with title_memory_unmapped a no-op, it fails with "a mapping cached between the notification and the unmap must not stay current".
  • cargo clippy --workspace --all-targets -D warnings: clean, with and without --all-features.
  • cargo test -p victory-gpu: 143 passed, 2 failed. Both failures, dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views, also fail on clean main on this NVIDIA card (capped view heap), so they're unrelated.
  • cargo test -p victory-hle xmem: 10 passed.
  • cargo xtask policy: ok.
  • rustfmt --check on the touched files shows only the differences main already has.

Choosing a non-cryptographic digest for change detection is the maintainers' call, as the triage noted.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5488816128 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2283). Originally posted by [alessiodeia](https://github.com/alessiodeia) on 2026-11-25T09:40:09Z. --- Closes #916. Opened as the issue triage asked, carried onto current `main` (`940d32f`) with the unmap-race fix from that thread. ### What it changes `Staging::refresh_now`, for sampled textures whose memory is not tracked: - **Digest.** BLAKE3 over the whole tiled memory is replaced by a 128-bit non-cryptographic digest: four 64-bit lanes, 32 bytes per step, and a SplitMix-style finish. Its only job is to tell whether the bytes changed since the last detile. - **Commit check per remap epoch.** `title_memory_remapped` now advances a remap epoch. A range made only of mapped views of physical pages (`MEM_MAPPED`) can only change through the runtime, so a texture keeps "committed" for it until the epoch moves. Any other range is asked through `VirtualQuery` every time, as before. `committed()` keeps main's overflow and `PAGE_NOACCESS`/`PAGE_GUARD` checks. - **Unmap race (found by @DrDarkDK on #916).** `xmem::unmap` notifies before `UnmapViewOfFile2`, so a refresh between the two could cache the still-mapped view under the new epoch. The new `title_memory_unmapped` advances it once more after the view is gone. The bytes are still digested in full on every refresh. Pages remapped under a stable address still show as a change, which was @pickles976's concern on #916. On `main` the tracked/untracked split is already decided per texture (`Staging::tracked`), so the original issue's "known not watched" cache is no longer needed and is not part of this PR. ### Numbers (RTX 2070 SUPER, Windows 11) - Benchmark, 160 MiB, release: BLAKE3 70.7 ms (2.26 GB/s), new digest 11.5 ms (13.9 GB/s), **6.1×** (`content_hash_vs_blake3_throughput`, ignored by default). - Measured on the issue's base (`ffe310e`): the world frame hashed about 156 MB of untracked textures with nothing changed, about 383 ms per frame. I haven't measured the world on current `main` on this card, because `main` doesn't reach it here (#1753 removes the device first). ### Tests - `content_hash_detects_single_bit_tail_and_order_changes`: single-bit flips in the 32-byte blocks, the 8-byte words and the tail, plus a swap and a length change. - `a_mapped_range_cached_before_its_unmap_is_asked_again`: walks `xmem::unmap`'s order (notify, still `Mapped`, unmap, `No`, notify again). Negative control: with `title_memory_unmapped` a no-op, it fails with "a mapping cached between the notification and the unmap must not stay current". - `cargo clippy --workspace --all-targets -D warnings`: clean, with and without `--all-features`. - `cargo test -p victory-gpu`: 143 passed, 2 failed. Both failures, `dynamic_slots_are_held_until_released` and `lists_share_a_table_and_copies_keep_their_views`, also fail on clean `main` on this NVIDIA card (capped view heap), so they're unrelated. - `cargo test -p victory-hle xmem`: 10 passed. - `cargo xtask policy`: ok. - `rustfmt --check` on the touched files shows only the differences `main` already has. Choosing a non-cryptographic digest for change detection is the maintainers' call, as the triage noted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DenRakEiw commented 2026-11-25 09:42:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T09:42:27Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib texture:: — 4 passed, 0 failed (1 ignored), including content_hash_detects_single_bit_tail_and_order_changes
  • cargo clippy -p victory-gpu -p victory-hle --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean fast content digest for untracked textures and per-remap-epoch commit-check caching. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6055166210 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2283#pullrequestreview-6055166210). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T09:42:27Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-gpu --lib texture::` — 4 passed, 0 failed (1 ignored), including `content_hash_detects_single_bit_tail_and_order_changes` - `cargo clippy -p victory-gpu -p victory-hle --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean fast content digest for untracked textures and per-remap-epoch commit-check caching. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 10:19:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T10:19:06Z.

Review: COMMENTED


GPU+HLE: fast content digest (blake3 → [u64;2]) for untracked textures, plus title_memory_unmapped export. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.

<!-- greyfinch-sync:Marvisak/VIctory:review:6055711041 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2283#pullrequestreview-6055711041). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T10:19:06Z. **Review:** COMMENTED --- GPU+HLE: fast content digest (blake3 → [u64;2]) for untracked textures, plus title_memory_unmapped export. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.
Marvisak commented 2026-11-25 14:07:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T13:32:34Z.


Automated triage for the maintainers.

Merge candidate (worth a look; the digest choice is the maintainer's call). Updated by merging main. In Staging::refresh_now (crates/victory-gpu/src/texture.rs), BLAKE3 is replaced by content_hash, a 128-bit non-cryptographic digest, at all three change-detection sites. It only decides whether bytes changed since the last detile, so collision resistance against an adversary is not needed. The committed check is cached per remap epoch for ranges made only of MEM_MAPPED views. watch::title_memory_remapped advances the epoch, and the new title_memory_unmapped, called in xmem::unmap after UnmapViewOfFile2, closes the race between the notification and the unmap. Other ranges are queried every time as before. Tests cover bit flips, swaps and length in the digest, and the unmap order, with a negative control. CI check passes and it merges cleanly. The touched xmem code is memory mapping, not the guarded net/media modules.

On your reply: the rebase re-applies the change on main's rewritten refresh, including the new per-subresource digests, and I found no conflict left. One thing for the maintainer to weigh: the cached answer assumes a mapped view's protection changes only through the runtime's remaps; a title call that makes a mapped view PAGE_NOACCESS without a remap would not be noticed until the epoch moves.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163927803 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2283#issuecomment-7163927803). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T13:32:34Z. --- <!-- victory-triage v1 head=6a6dcacd974a5d27d0f3229a51f0d7cf2c3f27f0 verdict=merge-candidate at=2026-11-26T00:23:11Z --> Automated triage for the maintainers. **Merge candidate (worth a look; the digest choice is the maintainer's call).** Updated by merging `main`. In `Staging::refresh_now` (`crates/victory-gpu/src/texture.rs`), BLAKE3 is replaced by `content_hash`, a 128-bit non-cryptographic digest, at all three change-detection sites. It only decides whether bytes changed since the last detile, so collision resistance against an adversary is not needed. The `committed` check is cached per remap epoch for ranges made only of `MEM_MAPPED` views. `watch::title_memory_remapped` advances the epoch, and the new `title_memory_unmapped`, called in `xmem::unmap` after `UnmapViewOfFile2`, closes the race between the notification and the unmap. Other ranges are queried every time as before. Tests cover bit flips, swaps and length in the digest, and the unmap order, with a negative control. CI `check` passes and it merges cleanly. The touched `xmem` code is memory mapping, not the guarded `net`/`media` modules. On your reply: the rebase re-applies the change on `main`'s rewritten refresh, including the new per-subresource digests, and I found no conflict left. One thing for the maintainer to weigh: the cached answer assumes a mapped view's protection changes only through the runtime's remaps; a title call that makes a mapped view `PAGE_NOACCESS` without a remap would not be noticed until the epoch moves. --- _Generated by [Claude Code](https://claude.ai/code)_
alessiodeia commented 2026-11-25 21:03:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alessiodeia on 2026-11-25T20:36:46Z.


Updated for current main (13f91ca) by merging it into the branch, with no force-push. Head is now 6a6dcac.

main's rewrite of the texture refresh conflicted with this PR in texture.rs. I re-applied the change on top of the new code, not on the old one:

  • The digest now replaces BLAKE3 at all three hashing sites:

    • the untracked whole-texture digest, as before;
    • the digest of the zero fill for uncommitted memory;
    • the new per-subresource digests of tracked textures, a spot main added since this PR was opened.

    All three only decide whether bytes changed since the last detile.

  • The untracked path's commit check is kept per remap epoch for ranges made only of mapped views, as before. Uncommitted memory still takes main's new zero-fill path.

  • watch.rs, lib.rs and xmem.rs merged cleanly. The epoch bump in title_memory_remapped and title_memory_unmapped after UnmapViewOfFile2 are unchanged.

Re-verified on Windows 11 with an RTX 2070 SUPER (driver 621.40):

  • workspace clippy -D warnings is clean, with and without --all-features;
  • cargo test -p victory-gpu: 195 passed, including the new texture tests. The same 2 tests that fail on clean main on this NVIDIA card (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views, capped view heap) fail here too;
  • cargo test -p victory-hle xmem: 11 passed;
  • cargo xtask policy: ok;
  • the race test still fails with title_memory_unmapped made a no-op, and passes with it;
  • rustfmt --check adds no lines beyond those main already has.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7173927595 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2283#issuecomment-7173927595). Originally posted by [alessiodeia](https://github.com/alessiodeia) on 2026-11-25T20:36:46Z. --- Updated for current `main` (`13f91ca`) by merging it into the branch, with no force-push. Head is now `6a6dcac`. `main`'s rewrite of the texture refresh conflicted with this PR in `texture.rs`. I re-applied the change on top of the new code, not on the old one: - **The digest now replaces BLAKE3 at all three hashing sites:** - the untracked whole-texture digest, as before; - the digest of the zero fill for uncommitted memory; - the new per-subresource digests of tracked textures, a spot `main` added since this PR was opened. All three only decide whether bytes changed since the last detile. - **The untracked path's commit check** is kept per remap epoch for ranges made only of mapped views, as before. Uncommitted memory still takes `main`'s new zero-fill path. - `watch.rs`, `lib.rs` and `xmem.rs` merged cleanly. The epoch bump in `title_memory_remapped` and `title_memory_unmapped` after `UnmapViewOfFile2` are unchanged. Re-verified on Windows 11 with an RTX 2070 SUPER (driver 621.40): - workspace clippy `-D warnings` is clean, with and without `--all-features`; - `cargo test -p victory-gpu`: 195 passed, including the new texture tests. The same 2 tests that fail on clean `main` on this NVIDIA card (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`, capped view heap) fail here too; - `cargo test -p victory-hle xmem`: 11 passed; - `cargo xtask policy`: ok; - the race test still fails with `title_memory_unmapped` made a no-op, and passes with it; - `rustfmt --check` adds no lines beyond those `main` already has.
fuzeofficlal commented 2026-11-26 19:44:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:44:48Z.

Review: APPROVED


Reviewed and verified 6a6dcacd974a5d27d0f3229a51f0d7cf2c3f27f0 merged onto origin/main (63bcca43c6a00a758510398fa43096ce7e4f7df0) on Windows 11 x64 (Ryzen 9 9950X, RTX 5090 + Microsoft WARP, --features diagnostics with VICTORY_D3D12_DEBUG=1, Rust 1.98.1 MSVC).

Code & Memory-Safety Audit

  • Audited every UnmapViewOfFile*, VirtualFree, and VirtualProtect call across crates/: xmem::unmap (crates/victory-hle/src/modules/xmem.rs:940-952) is the only path that unmaps title-visible MEM_MAPPED views (the other UnmapViewOfFile* sites in xmem.rs only unmap private temporary or rolled-back views never exposed to title code). Calling title_memory_remapped before UnmapViewOfFile2 and title_memory_unmapped (crates/victory-gpu/src/watch.rs:729-731) after it closes the window where a concurrent refresh caches Committed::Mapped under the pre-unmap epoch.
  • committed_kind (crates/victory-gpu/src/texture.rs:788-819) preserves overflow, MEM_COMMIT, and PAGE_NOACCESS | PAGE_GUARD checks, and only caches ranges whose every queried sub-region is MEM_MAPPED; any Committed::Other or Committed::No range resets last.committed_epoch = 0 (L637-652) and is re-queried every time.
  • content_hash (L889-941) uses four odd multipliers (K0..K3), distinct rotations, handles arbitrary tail lengths (0..31), and mixes bytes.len() at both init and SplitMix64 finalization. (blake3 remains used by pipeline and shader_cache, so no unused dependency is left behind.)

Local Verification

  • cargo xtask policy — pass (191 files ok).
  • cargo clippy --locked -p victory-gpu -p victory-hle --all-targets -- -D warnings (default and --all-features) — 0 warnings.
  • cargo test --locked -p victory-gpu --features diagnostics --lib texture::tests:: (RTX 5090 and WARP) — 5/5 passed (1 ignored benchmark), including content_hash_detects_single_bit_tail_and_order_changes and a_mapped_range_cached_before_its_unmap_is_asked_again.
  • cargo test --locked -p victory-hle --lib xmem — 14/14 passed.
  • Additional probe verifying distinct digests for all zero-filled lengths 0..=65, every single-bit flip across all 568 bit positions of an unaligned 71-byte buffer, last-byte flips across all 1..39 chunk/tail boundaries, and committed_kind(null, 16) == Committed::No — passed on both RTX 5090 and WARP.
  • Release throughput benchmark on Ryzen 9 9950X (content_hash_vs_blake3_throughput, 160 MiB): blake3 13.85 ms (11.55 GB/s) vs content_hash 5.12 ms (31.22 GB/s) (2.7x speedup).

Looks good to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079708372 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2283#pullrequestreview-6079708372). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T19:44:48Z. **Review:** APPROVED --- Reviewed and verified `6a6dcacd974a5d27d0f3229a51f0d7cf2c3f27f0` merged onto `origin/main` (`63bcca43c6a00a758510398fa43096ce7e4f7df0`) on Windows 11 x64 (Ryzen 9 9950X, RTX 5090 + Microsoft WARP, `--features diagnostics` with `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1 MSVC). ### Code & Memory-Safety Audit - Audited every `UnmapViewOfFile*`, `VirtualFree`, and `VirtualProtect` call across `crates/`: `xmem::unmap` (`crates/victory-hle/src/modules/xmem.rs:940-952`) is the only path that unmaps title-visible `MEM_MAPPED` views (the other `UnmapViewOfFile*` sites in `xmem.rs` only unmap private temporary or rolled-back views never exposed to title code). Calling `title_memory_remapped` before `UnmapViewOfFile2` and `title_memory_unmapped` (`crates/victory-gpu/src/watch.rs:729-731`) after it closes the window where a concurrent refresh caches `Committed::Mapped` under the pre-unmap epoch. - `committed_kind` (`crates/victory-gpu/src/texture.rs:788-819`) preserves overflow, `MEM_COMMIT`, and `PAGE_NOACCESS | PAGE_GUARD` checks, and only caches ranges whose every queried sub-region is `MEM_MAPPED`; any `Committed::Other` or `Committed::No` range resets `last.committed_epoch = 0` (`L637-652`) and is re-queried every time. - `content_hash` (`L889-941`) uses four odd multipliers (`K0..K3`), distinct rotations, handles arbitrary tail lengths (`0..31`), and mixes `bytes.len()` at both init and SplitMix64 finalization. (`blake3` remains used by `pipeline` and `shader_cache`, so no unused dependency is left behind.) ### Local Verification - `cargo xtask policy` — pass (`191 files ok`). - `cargo clippy --locked -p victory-gpu -p victory-hle --all-targets -- -D warnings` (default and `--all-features`) — 0 warnings. - `cargo test --locked -p victory-gpu --features diagnostics --lib texture::tests::` (RTX 5090 and WARP) — 5/5 passed (1 ignored benchmark), including `content_hash_detects_single_bit_tail_and_order_changes` and `a_mapped_range_cached_before_its_unmap_is_asked_again`. - `cargo test --locked -p victory-hle --lib xmem` — 14/14 passed. - Additional probe verifying distinct digests for all zero-filled lengths `0..=65`, every single-bit flip across all 568 bit positions of an unaligned 71-byte buffer, last-byte flips across all `1..39` chunk/tail boundaries, and `committed_kind(null, 16) == Committed::No` — passed on both RTX 5090 and WARP. - Release throughput benchmark on Ryzen 9 9950X (`content_hash_vs_blake3_throughput`, 160 MiB): `blake3 13.85 ms (11.55 GB/s)` vs `content_hash 5.12 ms (31.22 GB/s)` (**2.7x** speedup). Looks good to merge.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2283:upstream-pr/2283'
git switch 'upstream-pr/2283'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2283'
git switch 'upstream-pr/2283'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2283'
git switch 'upstream-pr/2283'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2283'
git switch 'main'
git merge --squash 'upstream-pr/2283'
git switch 'main'
git merge --ff-only 'upstream-pr/2283'
git switch 'main'
git merge 'upstream-pr/2283'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!2281
No description provided.