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

Closed
AndyLatimer wants to merge 3 commits from upstream-pr/143 into main
AndyLatimer commented 2026-11-24 00:57:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T00:57:11Z.


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 #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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572134494 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/143). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T00:57:11Z. --- `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 #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](https://claude.com/claude-code)
jeremygold02 commented 2026-11-24 06:55:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:36:07Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122168764 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/143#issuecomment-7122168764). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:36:07Z. --- 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](https://github.com/Marvisak/VIctory/blob/652f06057d13bb183ff64d48806205d25f9debd2/crates/victory-gpu/src/host.rs)). - `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`](https://github.com/Marvisak/VIctory/blob/d2a4bae1f49533ef5cb512fd1b1ba1513f2e1989/crates/victory-gpu/src/host.rs), which has one test module). Texture unit tests could not run until the branch is updated to a base without that duplicate.
mercuryVM commented 2026-11-24 07:52:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:52:32Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6035511176 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/143#pullrequestreview-6035511176). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T07:52:32Z. **Review:** APPROVED --- 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).
AndyLatimer commented 2026-11-24 08:02:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T07:47:01Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7125366096 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/143#issuecomment-7125366096). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T07:47:01Z. --- 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.
AndyLatimer commented 2026-11-24 12:04:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T11:20:19Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:comment:7129967744 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/143#issuecomment-7129967744). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T11:20:19Z. --- 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 commented 2026-11-24 14:18:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T14:18:33Z.

Review: COMMENTED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6040861461 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/143#pullrequestreview-6040861461). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T14:18:33Z. **Review:** COMMENTED --- 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).
upstream-sync closed this pull request 2026-11-24 23:03:55 +00:00

Pull request closed

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!143
No description provided.