Skip to content

GPU: hash and detile unwatched textures from a kernel-read copy - #143

Closed
AndyLatimer wants to merge 3 commits into
Marvisak:mainfrom
AndyLatimer:fix/gpu-texture-hash-unmapped
Closed

AndyLatimer wants to merge 3 commits into
Marvisak:mainfrom
AndyLatimer:fix/gpu-texture-hash-unmapped

Conversation

@AndyLatimer

@AndyLatimer AndyLatimer commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Staging::refresh_now (crates/victory-gpu/src/texture.rs) has two paths for a sampled texture's memory:

  • Watched path: reads it through copy_mapped (ReadProcessMemory), which reports unmapped pages instead of faulting.
  • Unwatched path: the physical-page views of the texture pool. It checked committed() and then read the memory directly, twice: once for blake3::hash and once for detile.

If the title unmaps those pages between the check and the reads, the read faults. That happens, for example, when it evicts a texture that a big descriptor table still lists, because the GPU would never sample it. The vectored handler in watch.rs takes only write faults, so the process dies.

The unwatched path now reads through copy_mapped as well. Unmapped pages come back as zeros. The copy goes into a per-thread scratch buffer that is reused between textures, so no CPU copy is kept per texture (keeping one in Last::read, as the watched path does, would hold a copy of every sampled texture in the pool). The committed() early-out stays. The hash and detile code moved unchanged into Staging::changed.

Cost: one extra copy of each hashed texture per refresh, on top of the hash's own read. I have not measured it in game. The diagnostics copy statistics (MoveUs, TexHashed) show it.

Testing

  • cargo test -p victory-gpu: 43 passed, 1 ignored, with this commit rebased onto d2a4bae. That commit includes GPU: rename the second test module in host.rs so the tests compile #129, which fixed the duplicate mod tests in host.rs that kept victory-gpu's tests from building on the branch's base 9aef3d9.
  • cargo test --workspace --exclude victory-gpu on the branch: 221 passed, 3 ignored (need external files).
  • cargo clippy -p victory-gpu --all-targets and cargo clippy -p victory --features diagnostics: no new warnings. The ones in host.rs:267 (test code) and xmem.rs are on main.
  • cargo xtask policy: pass. rustfmt --check on texture.rs: clean.
  • No new test: the race needs pages unmapped by another thread mid-read. Not run against a title.

🤖 Generated with Claude Code

Textures in memory with no write watch (the physical-page views of the
texture pool) were checked with committed() and then read directly,
twice (hash, detile). A title unmapping those pages in between, for
example evicting a texture a big descriptor table still lists, faulted
on the read, and the vectored handler takes only writes. The memory is
now read through copy_mapped (ReadProcessMemory, zeros for unmapped
pages), as the watched path already does, into a per-thread scratch
buffer reused between textures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AntoninMichelq pushed a commit to AntoninMichelq/VIctory that referenced this pull request Nov 24, 2026
… kernel-read copy) into the integration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…read copy

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

Tested head 652f06057d13bb183ff64d48806205d25f9debd2:

  • cargo test -p victory-gpu --lib texture -- --test-threads=1 was blocked before running tests by E0428: crates/victory-gpu/src/host.rs defines mod tests twice (lines 252 and 1974; PR head).
  • cargo check -p victory-gpu passed.

#143 changes only crates/victory-gpu/src/texture.rs; host.rs is unchanged from this branch's base. The duplicate is not present on current upstream d2a4bae1 (main host.rs, which has one test module). Texture unit tests could not run until the branch is updated to a base without that duplicate.

@AndyLatimer

Copy link
Copy Markdown
Contributor Author

Thanks @jeremygold02. The duplicate mod tests came from this branch's old base (9aef3d9); upstream fixed it in #129. I merged main (d2a4bae) into the branch, new head 8d9e40f, without changing the fix. cargo test -p victory-gpu --lib now builds and passes here: 43 passed, 1 ignored.

@mercuryVM mercuryVM 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.

Tested 8d9e40f on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (264 passed, 0 failed).

  • The race is real. committed() followed by a direct read leaves a window where the title can unmap the view, and watch.rs's vectored handler only takes write faults. Reading through copy_mapped, the same ReadProcessMemory path the watched branch uses, turns that into zeros instead of a crash. Splitting the hash and detile into changed is a pure move.
  • A thread-local scratch buffer is the right cost model, since it holds no per-texture copy.

Heads-up for whoever merges: this collides with #472. Both add an item named SCRATCH to texture.rs: #472 a static Mutex<Vec<u8>> for the watched path, this PR a thread_local!. The two merge without a textual conflict, but the result doesn't compile (E0428, the name is defined twice). If both land, the unwatched path could just reuse #472's buffer (or the thread-local could replace it), since both are "one scratch buffer for reading texture memory". I have not merged this into our fork for that reason.

AI-assisted (Claude Code).

…llision

Rename the thread-local SCRATCH buffer in crates/victory-gpu/src/texture.rs to UNWATCHED_SCRATCH so PR Marvisak#143 composes cleanly with PR Marvisak#472's static SCRATCH mutex without an E0428 duplicate-definition error (flagged by @mercuryVM).
@AndyLatimer

Copy link
Copy Markdown
Contributor Author

Thanks @mercuryVM for the review and the heads-up about the SCRATCH name collision with #472!

Renamed the thread-local buffer in crates/victory-gpu/src/texture.rs to UNWATCHED_SCRATCH in 525a713 so both branches compose cleanly without an E0428 duplicate definition.

Verified on Windows 11 (rustc 1.98.1):

  • cargo test -p victory-gpu: 43 passed, 0 failed, 1 ignored
  • cargo test -p victory-gpu --features diagnostics: 45 passed, 0 failed, 1 ignored
  • cargo clippy -p victory-gpu --lib -- -D warnings (default and --features diagnostics), rustfmt --edition 2024 --check crates/victory-gpu/src/texture.rs, cargo xtask policy: clean

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…es from a kernel-read copy

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@mercuryVM mercuryVM 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.

Re-tested 525a713 on Windows 11, RTX 5070, MSVC, merged on top of current upstream main plus #472: cargo check --workspace --tests is clean, and cargo test --workspace passes, with victory-shader run single-threaded.

  • Renaming the unwatched-texture scratch buffer resolves the duplicate-definition clash with #472's SCRATCH that I hit earlier (E0428), so the two now land together without conflict.

AI-assisted (Claude Code).

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…itives, capture), verified together

Merges #124 #119 #515 #934 #484 #143 #1191 #278 #553 #153 #270 #1158 #1204
#1220 #488 #1146 #631 onto main (ffe310e), each head unchanged, no conflicts,
no changes of mine. cargo clippy --workspace --all-targets --locked -- -D
warnings, with and without --all-features, is clean and cargo test
--workspace --locked passes (372 passed, 8 ignored; main has 342 passed,
6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…ures, device, DXGI, objects, primitives, capture)

Kept from our side:
- watch.rs: the per-submission journal (Journal.round, new_round, covered,
  cover, writes_once) and the WatchKernelUs/WatchGuardUs/WatchPages
  counters; the guard timing now wraps the PR's by-allocation fallback,
  and the per-allocation kernel askings (#515) count into WatchKernelUs
  and WatchPages too. Both sets of watch tests kept.
- texture.rs: our version (CommittedRegions, RUNTIME_WRITES and the
  last.checked skip, changed-subresource detiling, mip drop, widened
  packed16) with the PR's footprint fix on top: upload footprints end at
  the last row's data bytes (GetCopyableFootprints row sizes), detiling
  goes through detile_plane, title_written_flags, overflow-checked
  committed() and refresh range, and the PR's tests. #143 was already
  merged; the SCRATCH -> UNWATCHED_SCRATCH rename is left out (no #472
  here).
- copystat.rs: our counters and lines, plus the PR's process memory line.
- primitive.rs: our position+color fast path, plus the PR's zero and
  overflowing stride guards in the generic path.
- #124, #119, device.rs and object.rs changes were already in our tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak Marvisak closed this Nov 24, 2026
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