GPU: preserve title dirty marks after equal marked-page reads #3850

Open
lhnizdo wants to merge 1 commit from upstream-pr/3850 into main
lhnizdo commented 2026-11-29 16:33:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lhnizdo on 2026-11-29T16:33:07Z.


A marked page can compare equal with its title snapshot while a title thread dirties the page immediately afterward. Calling Tracked::taken in that case clears the new dirty mark, so the next Shadow::update can leave the title byte stale in the host copy.

Return before acknowledging the page when the comparison calls for no write. A deterministic fixture writes a byte just after the marked page is read and checks that the next refresh picks it up. Against main the refresh copied zero bytes. With the fix it copied the page and the byte matched. The focused shadow suite passed all 10 tests.
Focused validation also passed the asset policy hook and victory-gpu Clippy with all targets and features.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5552752230 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3850). Originally posted by [lhnizdo](https://github.com/lhnizdo) on 2026-11-29T16:33:07Z. --- A marked page can compare equal with its title snapshot while a title thread dirties the page immediately afterward. Calling `Tracked::taken` in that case clears the new dirty mark, so the next `Shadow::update` can leave the title byte stale in the host copy. Return before acknowledging the page when the comparison calls for no write. A deterministic fixture writes a byte just after the marked page is read and checks that the next refresh picks it up. Against main the refresh copied zero bytes. With the fix it copied the page and the byte matched. The focused shadow suite passed all 10 tests. Focused validation also passed the asset policy hook and victory-gpu Clippy with all targets and features.
AurumCarlos commented 2026-11-29 20:28:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-29T20:28:16Z.

Review: COMMENTED


The equal case is fixed, but the same title write is still lost when the copy-back does write: that path has the same window and one more way to lose it.

  • shadow.rs:538-546: after the read, write_mapped writes all n bytes. That is host when nothing was saved, or the merged title buffer. Both hold the title's bytes as read, so a title write that lands after read_into is overwritten with the stale byte. t.taken then clears the page's mark, so the next update copies nothing and the copy keeps the stale byte too.

Repro, your fixture plus one shader change elsewhere in the page so written is Some. On bffa7b8 it fails with left: 0, right: 187; the 11 other shadow:: tests pass (Wine, MSVC target):

/// As above, with a shader change elsewhere in the page, so the
/// copy-back writes: the title's write after the read must survive.
#[test]
fn a_title_write_after_the_read_survives_a_written_copy_back() {
    let page = crate::watch::tests::pool(1);
    register(page, page as u64, PAGE as u64);
    let copy = find(page as u64, PAGE as u64).expect("host GPU shadow");
    assert_eq!(copy.update(page, PAGE), PAGE);
    copy.writing(page, PAGE, true);
    // The shaders changed byte 2 of the copy.
    // SAFETY: byte two is inside the live host copy.
    unsafe { copy.data.add(2).write(0x77) };
    AFTER_MARKED_PAGE_READ.with(|write| write.set(Some(page + 1)));
    assert_eq!(copy.copy_back_marked(page), PAGE);
    // SAFETY: the pool's page, mapped.
    let (one, two) = unsafe { ((page as *const u8).add(1).read(), (page as *const u8).add(2).read()) };
    assert_eq!(two, 0x77, "the shaders' byte reaches title memory");
    assert_eq!(one, 0xbb, "the title's byte written after the read is kept");
    copy.update(page, PAGE);
    // SAFETY: byte one is inside the live host copy.
    assert_eq!(unsafe { copy.data.add(1).read() }, 0xbb, "and reaches the copy");
    unregister(page, page as u64);
    drop(copy);
    crate::watch::tests::free(page);
}

Fix: write only the bytes the shaders changed, and leave the page marked as you do for the equal case. With this, all 12 shadow:: tests pass, yours and marked_pages_go_back_by_the_bytes_the_gpu_changed included:

+        // The bytes the shaders changed: only those go to title memory, so a
+        // title write after the read above keeps the page's other bytes.
+        let shaders: Vec<bool> = match &saved {
+            None => host.iter().zip(&title).map(|(h, t)| h != t).collect(),
+            Some(before) => {
+                let before = &before[from - page..to - page];
+                host.iter().zip(before).map(|(h, b)| h != b).collect()
+            }
+        };
         let written = match &saved {
@@
         let Some(bytes) = written else { return 0 };
-        {
-            let _writing = crate::watch::writing(from, n);
-            crate::host::write_mapped(from as *mut u8, bytes, n);
-        }
-        // Equal to title memory now: taken, unless it holds the page in part.
-        if from == page && to == page + PAGE {
-            t.taken(&[page / PAGE]);
+        let _writing = crate::watch::writing(from, n);
+        let mut i = 0;
+        while i < n {
+            if !shaders[i] {
+                i += 1;
+                continue;
+            }
+            let run = i;
+            while i < n && shaders[i] {
+                i += 1;
+            }
+            crate::host::write_mapped((from + run) as *mut u8, bytes.wrapping_add(run), i - run);
         }
+        // Not taken: the title may have written the page after the read, as
+        // for an equal read; the next refresh takes its bytes.
         n

The cost is one page refresh after each marked copy-back. A byte that both the title and a shader wrote is still the shaders', as before.

<!-- greyfinch-sync:Marvisak/VIctory:review:6104650144 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3850#pullrequestreview-6104650144). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-29T20:28:16Z. **Review:** COMMENTED --- The equal case is fixed, but the same title write is still lost when the copy-back *does* write: that path has the same window and one more way to lose it. - `shadow.rs:538-546`: after the read, `write_mapped` writes all `n` bytes. That is `host` when nothing was saved, or the merged `title` buffer. Both hold the title's bytes **as read**, so a title write that lands after `read_into` is overwritten with the stale byte. `t.taken` then clears the page's mark, so the next `update` copies nothing and the copy keeps the stale byte too. Repro, your fixture plus one shader change elsewhere in the page so `written` is `Some`. On `bffa7b8` it fails with `left: 0, right: 187`; the 11 other `shadow::` tests pass (Wine, MSVC target): ```rust /// As above, with a shader change elsewhere in the page, so the /// copy-back writes: the title's write after the read must survive. #[test] fn a_title_write_after_the_read_survives_a_written_copy_back() { let page = crate::watch::tests::pool(1); register(page, page as u64, PAGE as u64); let copy = find(page as u64, PAGE as u64).expect("host GPU shadow"); assert_eq!(copy.update(page, PAGE), PAGE); copy.writing(page, PAGE, true); // The shaders changed byte 2 of the copy. // SAFETY: byte two is inside the live host copy. unsafe { copy.data.add(2).write(0x77) }; AFTER_MARKED_PAGE_READ.with(|write| write.set(Some(page + 1))); assert_eq!(copy.copy_back_marked(page), PAGE); // SAFETY: the pool's page, mapped. let (one, two) = unsafe { ((page as *const u8).add(1).read(), (page as *const u8).add(2).read()) }; assert_eq!(two, 0x77, "the shaders' byte reaches title memory"); assert_eq!(one, 0xbb, "the title's byte written after the read is kept"); copy.update(page, PAGE); // SAFETY: byte one is inside the live host copy. assert_eq!(unsafe { copy.data.add(1).read() }, 0xbb, "and reaches the copy"); unregister(page, page as u64); drop(copy); crate::watch::tests::free(page); } ``` Fix: write only the bytes the shaders changed, and leave the page marked as you do for the equal case. With this, all 12 `shadow::` tests pass, yours and `marked_pages_go_back_by_the_bytes_the_gpu_changed` included: ```diff + // The bytes the shaders changed: only those go to title memory, so a + // title write after the read above keeps the page's other bytes. + let shaders: Vec<bool> = match &saved { + None => host.iter().zip(&title).map(|(h, t)| h != t).collect(), + Some(before) => { + let before = &before[from - page..to - page]; + host.iter().zip(before).map(|(h, b)| h != b).collect() + } + }; let written = match &saved { @@ let Some(bytes) = written else { return 0 }; - { - let _writing = crate::watch::writing(from, n); - crate::host::write_mapped(from as *mut u8, bytes, n); - } - // Equal to title memory now: taken, unless it holds the page in part. - if from == page && to == page + PAGE { - t.taken(&[page / PAGE]); + let _writing = crate::watch::writing(from, n); + let mut i = 0; + while i < n { + if !shaders[i] { + i += 1; + continue; + } + let run = i; + while i < n && shaders[i] { + i += 1; + } + crate::host::write_mapped((from + run) as *mut u8, bytes.wrapping_add(run), i - run); } + // Not taken: the title may have written the page after the read, as + // for an equal read; the next refresh takes its bytes. n ``` The cost is one page refresh after each marked copy-back. A byte that both the title and a shader wrote is still the shaders', as before.
cloph-dsp commented 2026-11-29 23:00:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-29T23:00:51Z.


Independent local verification (Windows, Rust 1.100.0/MSVC, no game data):

  • PR #3850 at head bffa7b89e1ef62eb27cb950e1e66d265e7211b08, base/parent 63bcca43c6a00a758510398fa43096ce7e4f7df0.
  • cargo test --locked -p victory-gpu --lib -- shadow::tests::equal_marked_page_keeps_a_title_write_after_the_read --exact --test-threads=1 --quiet → 1 passed, 0 failed, 232 filtered, exit 0.
  • git grep -F for the test name on the base returned no match (test is new on this PR); git diff --check 63bcca4 HEAD clean (1 file, +34/-2).
  • Used a per-worktree CARGO_TARGET_DIR (never shared) with CARGO_BUILD_JOBS=2.
  • Only the named synthetic test was replayed; full workspace suite and clippy were not run locally. The one review on this PR is COMMENTED (not an approval) and flags the written copy-back path as still losing a concurrent title write — that case is outside the test replayed here.
  • GPU/driver: N/A (CPU-only shadow test). No game data, assets, extracted shaders, DRM or decrypt involved.

AI-assisted (Hy4 flash).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7264125169 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3850#issuecomment-7264125169). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-29T23:00:51Z. --- Independent local verification (Windows, Rust 1.100.0/MSVC, no game data): - PR #3850 at head `bffa7b89e1ef62eb27cb950e1e66d265e7211b08`, base/parent `63bcca43c6a00a758510398fa43096ce7e4f7df0`. - `cargo test --locked -p victory-gpu --lib -- shadow::tests::equal_marked_page_keeps_a_title_write_after_the_read --exact --test-threads=1 --quiet` → 1 passed, 0 failed, 232 filtered, exit 0. - `git grep -F` for the test name on the base returned no match (test is new on this PR); `git diff --check 63bcca4 HEAD` clean (1 file, +34/-2). - Used a per-worktree `CARGO_TARGET_DIR` (never shared) with `CARGO_BUILD_JOBS=2`. - Only the named synthetic test was replayed; full workspace suite and clippy were not run locally. The one review on this PR is COMMENTED (not an approval) and flags the written copy-back path as still losing a concurrent title write — that case is outside the test replayed here. - GPU/driver: N/A (CPU-only shadow test). No game data, assets, extracted shaders, DRM or decrypt involved. AI-assisted (Hy4 flash).
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/3850:upstream-pr/3850'
git switch 'upstream-pr/3850'

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/3850'
git switch 'upstream-pr/3850'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3850'
git switch 'upstream-pr/3850'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3850'
git switch 'main'
git merge --squash 'upstream-pr/3850'
git switch 'main'
git merge --ff-only 'upstream-pr/3850'
git switch 'main'
git merge 'upstream-pr/3850'
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!3850
No description provided.