GPU: a partial refresh of an armed page updates the bytes saved of it #2375

Merged
BastienRodz merged 2 commits from upstream-pr/2377 into main 2026-11-25 21:56:36 +00:00
BastienRodz commented 2026-11-25 12:15:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-25T12:15:55Z.


What's wrong

Since cd94d35, marked writes arm pages instead of taking a snapshot. The title's first write to an armed page saves the page's old bytes, and copy_back_marked compares the copy against them. A refresh of only part of such a page in the same submission (update_busy under a written range, update_free beside a sub-page one) copies the title's newer bytes into the copy but leaves the saved bytes as they were: Tracked::take keeps a partly covered page changed, and update_busy updates only Shadow::before, the snapshots of unmarked ranges. Copy-back then takes the refreshed bytes for the GPU's, which puts them back over later title writes, and misses shader writes back to the saved value.

What the fix does

  • Tracked::refreshed(from, bytes) (watch.rs) copies freshly refreshed bytes into the saved bytes of the pages they cover. It returns at once when nothing is saved, the usual case.
  • Shadow::copy calls it after copying. That keeps copy_back_marked's rule ("what differs from the copy's bytes before the submission") for armed pages, as update_busy already does for the snapshots of unmarked ranges.

Tests

New shadow::tests::partial_refresh_under_marked_range_updates_saved_bytes: two pages armed by a range ending 16 bytes into the second, the title writes both pages, part of each is refreshed (under and beside the range), the title then writes again and a shader-side write goes into the copy. Copy-back must keep the later title writes and take the shader's.

  • On main (940d32f) it fails: left: [170, 85, 68, 17] (stale 0x55 over the title's 0x66, the shader's 0x00 lost), right: [170, 102, 0, 17].
  • With the fix it and the other shadow::tests (5) and watch::tests (6) pass. Run under Wine 10 with vkd3d-proton 3.1 on llvmpipe: the copy needs a host device, and the test returns early without one.
  • windows-latest run on the fork: https://github.com/BastienRodz/VIctory/actions/runs/45612010672

Not tested

No title run, and no AMD/NVIDIA GPU. How often the title refreshes part of a page it is also writing through marked views is not measured.

Relation to #2172: that PR widens refreshes of marked view ranges to whole pages, which takes such pages whole and drops their saved bytes. It does not cover a refresh of another range (a constant buffer, say) in the same page, which is what this fixes. The two don't touch the same lines.

Fixes #2375

AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed and verified it). No game data used.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6604259957 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2377). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-25T12:15:55Z. --- ### What's wrong Since `cd94d35`, marked writes arm pages instead of taking a snapshot. The title's first write to an armed page saves the page's old bytes, and `copy_back_marked` compares the copy against them. A refresh of only part of such a page in the same submission (`update_busy` under a written range, `update_free` beside a sub-page one) copies the title's newer bytes into the copy but leaves the saved bytes as they were: `Tracked::take` keeps a partly covered page changed, and `update_busy` updates only `Shadow::before`, the snapshots of unmarked ranges. Copy-back then takes the refreshed bytes for the GPU's, which puts them back over later title writes, and misses shader writes back to the saved value. ### What the fix does - `Tracked::refreshed(from, bytes)` (`watch.rs`) copies freshly refreshed bytes into the saved bytes of the pages they cover. It returns at once when nothing is saved, the usual case. - `Shadow::copy` calls it after copying. That keeps `copy_back_marked`'s rule ("what differs from the copy's bytes before the submission") for armed pages, as `update_busy` already does for the snapshots of unmarked ranges. ### Tests New `shadow::tests::partial_refresh_under_marked_range_updates_saved_bytes`: two pages armed by a range ending 16 bytes into the second, the title writes both pages, part of each is refreshed (under and beside the range), the title then writes again and a shader-side write goes into the copy. Copy-back must keep the later title writes and take the shader's. - On `main` (940d32f) it fails: `left: [170, 85, 68, 17]` (stale `0x55` over the title's `0x66`, the shader's `0x00` lost), `right: [170, 102, 0, 17]`. - With the fix it and the other `shadow::tests` (5) and `watch::tests` (6) pass. Run under Wine 10 with vkd3d-proton 3.1 on llvmpipe: the copy needs a host device, and the test returns early without one. - windows-latest run on the fork: https://github.com/BastienRodz/VIctory/actions/runs/45612010672 ### Not tested No title run, and no AMD/NVIDIA GPU. How often the title refreshes part of a page it is also writing through marked views is not measured. Relation to #2172: that PR widens refreshes of marked view ranges to whole pages, which takes such pages whole and drops their saved bytes. It does not cover a refresh of another range (a constant buffer, say) in the same page, which is what this fixes. The two don't touch the same lines. Fixes #2375 AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed and verified it). No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
AndyLatimer commented 2026-11-25 13:02:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-25T12:30:41Z.

Review: COMMENTED


Tested on NVIDIA hardware, which the description lists as not tested: RTX 3060 Ti (driver 610.88), Windows 11 26200, 81dfeee (base 940d32f).

  • The new test without the fix (only the t.refreshed call in Shadow::copy taken out) fails with left: [170, 85, 68, 17], right: [170, 102, 0, 17], as in the description. A real D3D12 device runs it here; it doesn't return early.
  • With the fix it passes. shadow:: and watch:: (11 tests): 10 of 10 runs.
  • cargo test -p victory-gpu --locked: 142 passed, 2 failed, 2 ignored. The 2 failures are the heap-cap tests this card fails on main too (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views, #1744). main here: 141 passed, the same 2 failed.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings, default and --all-features: clean.

What I checked, all fine:

  • Order. copy_back_marks runs before the copy-backs of declared ranges (host.rs ~988), and copy_back_marked removes the page's saved bytes. So the other place where the copy takes title bytes, the *h = *t sync in copy_back_or_not, can't leave saved bytes stale and doesn't need the same call.
  • Locks. refreshed takes the Saved lock while update_busy holds before. copy_back_or_not already goes before → guards → Saved (through taken), and the write handler goes guards → Saved. The PR adds no new order.
  • A window it also closes, for whole pages. take guards a page, drops its saved bytes and releases the guard lock before the copy is made. If the page is armed (an earlier step of the same submission declared it), a title write in that window faults and saves the bytes from before the write, and the copy then has the write. Without this PR, copy-back takes that write for the GPU's and puts it back over a later title write to the same byte. With it, the saved bytes are the copy's. No test for this one, since it's a race.

Nit: the docs of Saved (watch.rs), Tracked::saved and copy_back_marked still say the saved bytes are what the page held at the title's first write. With this PR they are that with later refreshes laid over, i.e. the copy's bytes before the submission. One line in Saved's doc would cover it.

Overlap with other open PRs on these files:

  • Merged with #2147, #2239 or #2172 (one at a time): builds, and shadow:: and watch:: pass.
  • #2169 merges without a conflict but does not build. It folds Changed's two mutexes into one ChangedState, so self.changed.1 in refreshed is gone (E0609); there it would be self.changed.0.lock() and state.bytes. It also gives copy_back_marked a scratch: &mut Vec<u8> argument, so the new test's two calls need one (E0061). Whichever of the two lands second needs those three lines.
  • With #2242, #1998 and #1986 the only conflict is that both sides add tests at the end of shadow.rs's mod tests.
<!-- greyfinch-sync:Marvisak/VIctory:review:6057625461 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2377#pullrequestreview-6057625461). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-25T12:30:41Z. **Review:** COMMENTED --- Tested on NVIDIA hardware, which the description lists as not tested: RTX 3060 Ti (driver 610.88), Windows 11 26200, `81dfeee` (base `940d32f`). - The new test without the fix (only the `t.refreshed` call in `Shadow::copy` taken out) fails with `left: [170, 85, 68, 17]`, `right: [170, 102, 0, 17]`, as in the description. A real D3D12 device runs it here; it doesn't return early. - With the fix it passes. `shadow::` and `watch::` (11 tests): 10 of 10 runs. - `cargo test -p victory-gpu --locked`: 142 passed, 2 failed, 2 ignored. The 2 failures are the heap-cap tests this card fails on `main` too (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`, #1744). `main` here: 141 passed, the same 2 failed. - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`, default and `--all-features`: clean. What I checked, all fine: - **Order.** `copy_back_marks` runs before the copy-backs of declared ranges (`host.rs` ~988), and `copy_back_marked` removes the page's saved bytes. So the other place where the copy takes title bytes, the `*h = *t` sync in `copy_back_or_not`, can't leave saved bytes stale and doesn't need the same call. - **Locks.** `refreshed` takes the `Saved` lock while `update_busy` holds `before`. `copy_back_or_not` already goes `before` → guards → `Saved` (through `taken`), and the write handler goes guards → `Saved`. The PR adds no new order. - **A window it also closes, for whole pages.** `take` guards a page, drops its saved bytes and releases the guard lock before the copy is made. If the page is armed (an earlier step of the same submission declared it), a title write in that window faults and saves the bytes from before the write, and the copy then has the write. Without this PR, copy-back takes that write for the GPU's and puts it back over a later title write to the same byte. With it, the saved bytes are the copy's. No test for this one, since it's a race. Nit: the docs of `Saved` (`watch.rs`), `Tracked::saved` and `copy_back_marked` still say the saved bytes are what the page held at the title's first write. With this PR they are that with later refreshes laid over, i.e. the copy's bytes before the submission. One line in `Saved`'s doc would cover it. Overlap with other open PRs on these files: - Merged with #2147, #2239 or #2172 (one at a time): builds, and `shadow::` and `watch::` pass. - **#2169 merges without a conflict but does not build.** It folds `Changed`'s two mutexes into one `ChangedState`, so `self.changed.1` in `refreshed` is gone (E0609); there it would be `self.changed.0.lock()` and `state.bytes`. It also gives `copy_back_marked` a `scratch: &mut Vec<u8>` argument, so the new test's two calls need one (E0061). Whichever of the two lands second needs those three lines. - With #2242, #1998 and #1986 the only conflict is that both sides add tests at the end of `shadow.rs`'s `mod tests`.
DenRakEiw commented 2026-11-25 13:02:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T12:31:13Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Updating saved bytes on partial refresh of an armed page cleanly prevents stale copy-back diffs against overwritten title bytes.
  • cargo test -p victory-gpu shadow::: 5/5 passed on RTX 5090, 5/5 passed on WARP (including partial_refresh_under_marked_range_updates_saved_bytes).
  • cargo test -p victory-gpu watch::tests: 6/6 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6057632100 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2377#pullrequestreview-6057632100). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T12:31:13Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Updating saved bytes on partial refresh of an armed page cleanly prevents stale copy-back diffs against overwritten title bytes. - `cargo test -p victory-gpu shadow::`: 5/5 passed on RTX 5090, 5/5 passed on WARP (including `partial_refresh_under_marked_range_updates_saved_bytes`). - `cargo test -p victory-gpu watch::tests`: 6/6 passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 13:02:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T12:36:04Z.

Review: COMMENTED


A partial refresh of an armed page updates the saved bytes too (refreshed() under marked ranges). 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. Closes a copy-back correctness hole in the shadow.rs cluster (#2271/#2272); test included.

<!-- greyfinch-sync:Marvisak/VIctory:review:6057697322 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2377#pullrequestreview-6057697322). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T12:36:04Z. **Review:** COMMENTED --- A partial refresh of an armed page updates the saved bytes too (refreshed() under marked ranges). 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. Closes a copy-back correctness hole in the shadow.rs cluster (#2271/#2272); test included.
BastienRodz commented 2026-11-25 13:02:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-25T12:33:09Z.


Thanks @AndyLatimer, that's a thorough check, and good to have it confirmed on a real device. The ordering and lock analysis match my reading. I hadn't thought of the whole-page window between take dropping the saved bytes and the copy being made; that's a nice extra.

Doc nit addressed in 81e7648: Saved and Tracked::saved now say the saved bytes have later refreshes laid over them, i.e. the copy's bytes before the submission.

On #2169: agreed, whichever lands second needs self.changed.0.lock()/state.bytes in refreshed and the scratch argument in the test's two calls. I'll rebase if this one ends up second.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7162529351 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2377#issuecomment-7162529351). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-25T12:33:09Z. --- Thanks @AndyLatimer, that's a thorough check, and good to have it confirmed on a real device. The ordering and lock analysis match my reading. I hadn't thought of the whole-page window between `take` dropping the saved bytes and the copy being made; that's a nice extra. Doc nit addressed in 81e7648: `Saved` and `Tracked::saved` now say the saved bytes have later refreshes laid over them, i.e. the copy's bytes before the submission. On #2169: agreed, whichever lands second needs `self.changed.0.lock()`/`state.bytes` in `refreshed` and the `scratch` argument in the test's two calls. I'll rebase if this one ends up second.
Marvisak commented 2026-11-25 15:04:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T14:55:00Z.


Automated triage for the maintainers.

Merge candidate.

Shadow::copy now calls Tracked::refreshed (watch.rs). When a refresh covers only part of an armed page, the page's saved bytes take the refreshed bytes too. Before, take left a partly covered page changed with its old saved bytes, so copy_back_marked mistook the refresh for a GPU write. Whole-page takes already drop the saved bytes through taken, and refreshed returns at once when nothing is saved, so the usual path costs nothing extra. The test covers a refresh both under and beside the marked range.

Relation to the other shadow.rs PRs:

  • #2437 (merge candidate) advances the declared-range snapshot (Shadow::before) in copy_back_marked. This PR updates a different buffer (the watch's saved bytes) in a different function, so the two are complementary. Merged together, they conflict only where both add tests before mark_runs_are_reused_and_joined. Keep both tests.
  • #2434 removes Shadow::copy_back_marked. The production change here still applies with it, but this test calls copy_back_marked, so whichever lands second needs to adapt the test.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166015910 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2377#issuecomment-7166015910). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T14:55:00Z. --- <!-- victory-triage v1 head=ae9cbd02afc07a5e361544f5073867b203d4c565 verdict=merge-candidate at=2026-11-25T16:43:20Z --> Automated triage for the maintainers. **Merge candidate.** `Shadow::copy` now calls `Tracked::refreshed` (`watch.rs`). When a refresh covers only part of an armed page, the page's saved bytes take the refreshed bytes too. Before, `take` left a partly covered page changed with its old saved bytes, so `copy_back_marked` mistook the refresh for a GPU write. Whole-page takes already drop the saved bytes through `taken`, and `refreshed` returns at once when nothing is saved, so the usual path costs nothing extra. The test covers a refresh both under and beside the marked range. Relation to the other `shadow.rs` PRs: - #2437 (merge candidate) advances the declared-range snapshot (`Shadow::before`) in `copy_back_marked`. This PR updates a different buffer (the watch's saved bytes) in a different function, so the two are complementary. Merged together, they conflict only where both add tests before `mark_runs_are_reused_and_joined`. Keep both tests. - #2434 removes `Shadow::copy_back_marked`. The production change here still applies with it, but this test calls `copy_back_marked`, so whichever lands second needs to adapt the test. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
BastienRodz commented 2026-11-25 16:09:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-25T15:49:15Z.


Rebased onto main da82e44 (the tests block conflicted with main's two new shadow tests; kept both). Checked again on the new main: with Tracked::refreshed removed the test still fails (left: [170, 85, 68, 17], right: [170, 102, 0, 17]), and it passes with it. shadow:: 9/9, watch:: 12/12 (Wine + vkd3d-proton).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167342097 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2377#issuecomment-7167342097). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-25T15:49:15Z. --- Rebased onto main da82e44 (the tests block conflicted with main's two new shadow tests; kept both). Checked again on the new main: with `Tracked::refreshed` removed the test still fails (`left: [170, 85, 68, 17]`, `right: [170, 102, 0, 17]`), and it passes with it. `shadow::` 9/9, `watch::` 12/12 (Wine + vkd3d-proton).
upstream-sync closed this pull request 2026-11-25 22:03:38 +00:00
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!2375
No description provided.