GPU: copy-back writes whole 64 KiB pieces back and can undo the title's own writes #721

Open
opened 2026-11-26 04:13:53 +00:00 by github-bot · 8 comments
Owner

@⁠Dufftwt on GitHub · 2026-11-24 08:26 UTC · original

What's wrong

Shadow::copy_back (crates/victory-gpu/src/shadow.rs:190) brings the GPU's writes back into title memory one 64 KiB piece at a time:

  1. It reads the piece from title memory (read_into).
  2. It puts the GPU's bytes into that buffer.
  3. It writes the whole piece back with write_mapped (:225-228).

The title's threads keep running during all three steps. If one writes any byte of the piece that the GPU didn't touch, between steps 1 and 3, step 3 overwrites it with the older value.

The function's own comment says why this matters: it copies back only the shader-written bytes because putting older bytes back "broke the title's own records (a fence list read as -1)". The merge loop does that per byte, but the write-back still covers the whole piece, so the same corruption can happen through this race.

Example

  • A shader writes byte 0 of a page.
  • While copy_back runs, a title thread bumps a counter at byte 64 of the same page.
  • write_mapped puts the old counter back. The title's increment is lost.

Evidence

A test reproduces it with real write-watched memory and a host copy. A title thread keeps bumping a word in each of 64 pages while copy_back writes a shader byte back into each page. On main (dd54de2), on GitHub's windows-latest:

assertion `left == right` failed: title writes undone by the copy-back
  left: 63
 right: 0

The test watches one word in each of the 64 pages: 63 of the title's writes were put back to an older value. The test is below in case it helps whoever picks this up. The same module also holds the test for #722.

Tests (append to crates/victory-gpu/src/shadow.rs)
#[cfg(test)]
mod race_tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
    use windows_sys::Win32::System::Memory::{
        MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc, VirtualFree,
    };
    const MEM_WRITE_WATCH: u32 = 0x20_0000;
    const PAGES: usize = 64;

    /// Write-watched title memory of `PAGES` pages, freed on drop.
    struct Title(usize);
    impl Title {
        fn new() -> Title {
            // SAFETY: a fresh allocation, freed on drop.
            let p = unsafe {
                VirtualAlloc(
                    std::ptr::null(),
                    PAGES * PAGE,
                    MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH,
                    PAGE_READWRITE,
                )
            } as usize;
            assert_ne!(p, 0);
            Title(p)
        }
    }
    impl Drop for Title {
        fn drop(&mut self) {
            // SAFETY: our allocation.
            unsafe { VirtualFree(self.0 as *mut _, 0, MEM_RELEASE) };
        }
    }

    /// A host copy of the title memory, brought up to date, with shaders of
    /// the submission declared to write all of it and byte 0 of every page
    /// written by "the GPU".
    fn submitted(base: usize) -> Option<Shadow> {
        let (buffer, data) = host()?.shared_buffer((PAGES * PAGE) as u64)?;
        // SAFETY: a live buffer.
        let gpu = unsafe { buffer.GetGPUVirtualAddress() };
        let s = Shadow::new(buffer, data, gpu, base as u64, (PAGES * PAGE) as u64);
        assert_eq!(s.update(base, PAGES * PAGE), PAGES * PAGE);
        s.writing(base, PAGES * PAGE);
        for p in 0..PAGES {
            // SAFETY: inside the copy.
            unsafe { s.data.add(p * PAGE).write(1) };
        }
        Some(s)
    }

    fn peek(at: usize) -> u8 {
        // SAFETY: the tests pass addresses inside their memory.
        unsafe { (at as *const u8).read_volatile() }
    }

    /// The title's threads keep writing words no shader writes, in every
    /// page shaders wrote, while the copy-back runs: none goes back to an
    /// older value.
    #[test]
    fn copy_back_does_not_undo_title_writes_made_while_it_runs() {
        let title = Title::new();
        let base = title.0;
        let Some(s) = submitted(base) else {
            return; // no host GPU here
        };
        let stop = AtomicBool::new(false);
        let undone = std::thread::scope(|t| {
            let writer = t.spawn(|| {
                let mut last = [0u32; PAGES];
                let mut undone = 0;
                while !stop.load(Relaxed) {
                    for (p, last) in last.iter_mut().enumerate() {
                        let at = (base + p * PAGE + 64) as *mut u32;
                        // SAFETY: inside the title memory.
                        unsafe {
                            if at.read_volatile() != *last {
                                undone += 1;
                            }
                            *last += 1;
                            at.write_volatile(*last);
                        }
                    }
                }
                undone
            });
            s.copy_back(base, PAGES * PAGE);
            stop.store(true, Relaxed);
            writer.join().unwrap()
        });
        assert_eq!(undone, 0, "title writes undone by the copy-back");
        assert!((0..PAGES).all(|p| peek(base + p * PAGE) == 1), "the shader's bytes");
    }

    /// The title writes a page once the copy-back has put the shader's
    /// byte there (its read of the page is done), while later pages are
    /// still being copied back: the next refresh brings the write in.
    #[test]
    fn a_title_write_during_copy_back_reaches_the_copy() {
        let title = Title::new();
        let base = title.0;
        let Some(s) = submitted(base) else {
            return; // no host GPU here
        };
        std::thread::scope(|t| {
            t.spawn(|| {
                while peek(base + PAGE) != 1 {
                    std::hint::spin_loop();
                }
                // SAFETY: inside the title memory.
                unsafe { ((base + 8) as *mut u8).write_volatile(7) };
            });
            s.copy_back(base, PAGES * PAGE);
        });
        assert_eq!(peek(base + 8), 7, "in title memory");
        s.update(base, PAGE);
        assert_eq!(peek(s.data as usize + 8), 7, "in the copy");
    }
}

Suggested fix

In the merge loop, call watch::writing and write_mapped only on each run of bytes the GPU actually changed (*h != *o && *t != *h), not on all n bytes. The copy can keep taking *h = *t as now.

  • #124, #401 and #693 restructure copy_back but all keep the whole-piece write.
  • #261 fixes other cases where older bytes were copied back (overlapping and gap ranges); this is a separate path.
  • #722 is about the same function: pages marked current after a title write.
**@⁠Dufftwt** on GitHub · 2026-11-24 08:26 UTC · [original](https://github.com/Marvisak/VIctory/issues/721) ## What's wrong `Shadow::copy_back` (`crates/victory-gpu/src/shadow.rs:190`) brings the GPU's writes back into title memory one 64 KiB piece at a time: 1. It reads the piece from title memory (`read_into`). 2. It puts the GPU's bytes into that buffer. 3. It writes **the whole piece** back with `write_mapped` (`:225-228`). The title's threads keep running during all three steps. If one writes any byte of the piece that the GPU didn't touch, between steps 1 and 3, step 3 overwrites it with the older value. The function's own comment says why this matters: it copies back only the shader-written bytes because putting older bytes back "broke the title's own records (a fence list read as -1)". The merge loop does that per byte, but the write-back still covers the whole piece, so the same corruption can happen through this race. ## Example - A shader writes byte 0 of a page. - While `copy_back` runs, a title thread bumps a counter at byte 64 of the same page. - `write_mapped` puts the old counter back. The title's increment is lost. ## Evidence A test reproduces it with real write-watched memory and a host copy. A title thread keeps bumping a word in each of 64 pages while `copy_back` writes a shader byte back into each page. On `main` (dd54de2), on GitHub's `windows-latest`: ``` assertion `left == right` failed: title writes undone by the copy-back left: 63 right: 0 ``` The test watches one word in each of the 64 pages: 63 of the title's writes were put back to an older value. The test is below in case it helps whoever picks this up. The same module also holds the test for #722. <details><summary>Tests (append to <code>crates/victory-gpu/src/shadow.rs</code>)</summary> ```rust #[cfg(test)] mod race_tests { use super::*; use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc, VirtualFree, }; const MEM_WRITE_WATCH: u32 = 0x20_0000; const PAGES: usize = 64; /// Write-watched title memory of `PAGES` pages, freed on drop. struct Title(usize); impl Title { fn new() -> Title { // SAFETY: a fresh allocation, freed on drop. let p = unsafe { VirtualAlloc( std::ptr::null(), PAGES * PAGE, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE, ) } as usize; assert_ne!(p, 0); Title(p) } } impl Drop for Title { fn drop(&mut self) { // SAFETY: our allocation. unsafe { VirtualFree(self.0 as *mut _, 0, MEM_RELEASE) }; } } /// A host copy of the title memory, brought up to date, with shaders of /// the submission declared to write all of it and byte 0 of every page /// written by "the GPU". fn submitted(base: usize) -> Option<Shadow> { let (buffer, data) = host()?.shared_buffer((PAGES * PAGE) as u64)?; // SAFETY: a live buffer. let gpu = unsafe { buffer.GetGPUVirtualAddress() }; let s = Shadow::new(buffer, data, gpu, base as u64, (PAGES * PAGE) as u64); assert_eq!(s.update(base, PAGES * PAGE), PAGES * PAGE); s.writing(base, PAGES * PAGE); for p in 0..PAGES { // SAFETY: inside the copy. unsafe { s.data.add(p * PAGE).write(1) }; } Some(s) } fn peek(at: usize) -> u8 { // SAFETY: the tests pass addresses inside their memory. unsafe { (at as *const u8).read_volatile() } } /// The title's threads keep writing words no shader writes, in every /// page shaders wrote, while the copy-back runs: none goes back to an /// older value. #[test] fn copy_back_does_not_undo_title_writes_made_while_it_runs() { let title = Title::new(); let base = title.0; let Some(s) = submitted(base) else { return; // no host GPU here }; let stop = AtomicBool::new(false); let undone = std::thread::scope(|t| { let writer = t.spawn(|| { let mut last = [0u32; PAGES]; let mut undone = 0; while !stop.load(Relaxed) { for (p, last) in last.iter_mut().enumerate() { let at = (base + p * PAGE + 64) as *mut u32; // SAFETY: inside the title memory. unsafe { if at.read_volatile() != *last { undone += 1; } *last += 1; at.write_volatile(*last); } } } undone }); s.copy_back(base, PAGES * PAGE); stop.store(true, Relaxed); writer.join().unwrap() }); assert_eq!(undone, 0, "title writes undone by the copy-back"); assert!((0..PAGES).all(|p| peek(base + p * PAGE) == 1), "the shader's bytes"); } /// The title writes a page once the copy-back has put the shader's /// byte there (its read of the page is done), while later pages are /// still being copied back: the next refresh brings the write in. #[test] fn a_title_write_during_copy_back_reaches_the_copy() { let title = Title::new(); let base = title.0; let Some(s) = submitted(base) else { return; // no host GPU here }; std::thread::scope(|t| { t.spawn(|| { while peek(base + PAGE) != 1 { std::hint::spin_loop(); } // SAFETY: inside the title memory. unsafe { ((base + 8) as *mut u8).write_volatile(7) }; }); s.copy_back(base, PAGES * PAGE); }); assert_eq!(peek(base + 8), 7, "in title memory"); s.update(base, PAGE); assert_eq!(peek(s.data as usize + 8), 7, "in the copy"); } } ``` </details> ## Suggested fix In the merge loop, call `watch::writing` and `write_mapped` only on each run of bytes the GPU actually changed (`*h != *o && *t != *h`), not on all `n` bytes. The copy can keep taking `*h = *t` as now. ## Related - #124, #401 and #693 restructure `copy_back` but all keep the whole-piece write. - #261 fixes other cases where older bytes were copied back (overlapping and gap ranges); this is a separate path. - #722 is about the same function: pages marked current after a title write.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:13:53 +00:00
Author
Owner

@⁠xstraok on GitHub · 2026-11-24 09:18 UTC · original

A fix along the lines suggested here, with the first test from this issue: https://github.com/xstraok/VIctory/tree/copy-back-runs (a42f374 on 8e79c80). Opening a PR is refused for my account right now (does not have the correct permissions to execute CreatePullRequest), so it is here instead.

copy_back now writes back only the runs of bytes the GPU changed. They are written with plain stores after watch::writing opens the piece's guards. A WriteProcessMemory per run made copy-back take 2.4–26 s per world frame under Wine (0.05–0.5 s before). The piece was just read through the kernel, so it is mapped; a title unmapping it before the stores would fault (marked shortcut:).

Tested (Arch Linux, RX 9060 XT, Mesa 26.2.6 RADV, Proton Experimental + vkd3d-proton, x86_64-pc-windows-gnu, tests run under Wine):

  • the test without the fix: 3 of 3 runs failed (63, 64, 64 of 64 writes undone); with it: 6 of 6 passed
  • cargo test -p victory-gpu --lib: 49 passed, 2 failed, 1 ignored; the same two fail on main under Wine (argument_clamp_builds, a_rejected_blob_gives_an_empty_library)
  • GTA VI 1.0.2.1, diag build of 8e79c80 + #190 + #454 + #431 + #493: without the fix the title froze after ~4 min in the world with two threads spinning in a hash-table probe (+0x50eb81a..+0x50eb83d); with it, ~10 min in the world to my time limit, no freeze, copy-back median 84 ms/frame. One run each, and the link between the freeze and this race is a guess.

Not tested: Windows, NVIDIA, release builds.

Written with Claude Code (Claude Opus 5.5) from runs on my machine.

**@⁠xstraok** on GitHub · 2026-11-24 09:18 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7127241004) A fix along the lines suggested here, with the first test from this issue: https://github.com/xstraok/VIctory/tree/copy-back-runs (`a42f374` on `8e79c80`). Opening a PR is refused for my account right now (`does not have the correct permissions to execute CreatePullRequest`), so it is here instead. `copy_back` now writes back only the runs of bytes the GPU changed. They are written with plain stores after `watch::writing` opens the piece's guards. A `WriteProcessMemory` per run made copy-back take 2.4–26 s per world frame under Wine (0.05–0.5 s before). The piece was just read through the kernel, so it is mapped; a title unmapping it before the stores would fault (marked `shortcut:`). **Tested** (Arch Linux, RX 9060 XT, Mesa 26.2.6 RADV, Proton Experimental + vkd3d-proton, `x86_64-pc-windows-gnu`, tests run under Wine): - the test without the fix: 3 of 3 runs failed (63, 64, 64 of 64 writes undone); with it: 6 of 6 passed - `cargo test -p victory-gpu --lib`: 49 passed, 2 failed, 1 ignored; the same two fail on `main` under Wine (`argument_clamp_builds`, `a_rejected_blob_gives_an_empty_library`) - GTA VI 1.0.2.1, diag build of `8e79c80` + #190 + #454 + #431 + #493: without the fix the title froze after ~4 min in the world with two threads spinning in a hash-table probe (`+0x50eb81a..+0x50eb83d`); with it, ~10 min in the world to my time limit, no freeze, copy-back median 84 ms/frame. One run each, and the link between the freeze and this race is a guess. **Not tested:** Windows, NVIDIA, release builds. Written with Claude Code (Claude Opus 5.5) from runs on my machine.
Author
Owner

@⁠Hiiva on GitHub · 2026-11-24 09:30 UTC · original

I reproduced both this race and #722 with deterministic synthetic tests on native Windows/MSVC, then added a combined fix on top of #261: github.com/Hiiva/VIctory@d44fa0053b

The test injects a title write immediately after copy_back reads a piece. Before the changes, that write is overwritten. Restricting write-back to GPU-changed byte runs fixes this test while the separate write-watch refresh test still fails. Keeping the earlier copied epochs then fixes #722: the next refresh sees the concurrent title write instead of accepting the older host snapshot as current.

The four shadow tests pass, diagnostics Clippy passes with warnings denied, and the integrated workspace suite passes. These tests use tiny WARP allocations without shader dispatch. The branch retains scoped kernel-write leases (#723).

This implementation retains write_mapped for each changed run, preserving the existing unmapped-memory handling. xstraok's Wine timing above makes the possible cost clear. The integrated native Windows build has reached menus, intro and setup, but I have not yet measured world copy-back performance. The epoch fix also deliberately refreshes pages written by copy-back again.

**@⁠Hiiva** on GitHub · 2026-11-24 09:30 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7127517091) I reproduced both this race and #722 with deterministic synthetic tests on native Windows/MSVC, then added a combined fix on top of #261: https://github.com/Hiiva/VIctory/commit/d44fa0053b1e4c75602d0deee75e07795362afea The test injects a title write immediately after copy_back reads a piece. Before the changes, that write is overwritten. Restricting write-back to GPU-changed byte runs fixes this test while the separate write-watch refresh test still fails. Keeping the earlier copied epochs then fixes #722: the next refresh sees the concurrent title write instead of accepting the older host snapshot as current. The four shadow tests pass, diagnostics Clippy passes with warnings denied, and the integrated workspace suite passes. These tests use tiny WARP allocations without shader dispatch. The branch retains scoped kernel-write leases (#723). This implementation retains write_mapped for each changed run, preserving the existing unmapped-memory handling. xstraok's Wine timing above makes the possible cost clear. The integrated native Windows build has reached menus, intro and setup, but I have not yet measured world copy-back performance. The epoch fix also deliberately refreshes pages written by copy-back again.
Author
Owner

@⁠Vaximous on GitHub · 2026-11-24 13:59 UTC · original

Runtime evidence for this race, plus one more ready branch.

Branch: https://github.com/Vaximous/VIctory/compare/main...fix/copy-back-changed-runs (commit ba8548a, on ffe310e). It is the fix suggested above: watch::writing and write_mapped now run only on each run of bytes the GPU changed. It also adds a deterministic test (a_title_write_during_copy_back_stays), where a hook between copy-back's read and its write performs a title write. With the whole-piece write the test fails; with the fix it passes. One file, crates/victory-gpu/src/shadow.rs, +88/-8. cargo test -p victory-gpu: 52 passed (1 ignored). Clippy -D warnings is clean.

Runtime (RTX 4070 Ti, Windows 11): measured on our local integration build (main plus a set of open PRs, #261 among them), not on plain main. The title crashed in the world at title+0x247a098: a pointer that a title thread had just stored read back as null. With this change the crash did not come back in 5 runs.

Follow-up, not included: when a shader's writes are scattered, each run becomes its own kernel call. In the world that cost 1.3-1.9 s per frame of copy-back. Locally we store runs directly once a piece has more than 8 of them. That relies on guard handling from our integration build, so it is left out of this branch.

Related: #261 (earlier comment: #261), #722.

Written with Claude Code (Claude Opus 5.5); tested locally.

**@⁠Vaximous** on GitHub · 2026-11-24 13:59 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7133645788) Runtime evidence for this race, plus one more ready branch. **Branch:** https://github.com/Vaximous/VIctory/compare/main...fix/copy-back-changed-runs (commit ba8548a, on ffe310e). It is the fix suggested above: `watch::writing` and `write_mapped` now run only on each run of bytes the GPU changed. It also adds a deterministic test (`a_title_write_during_copy_back_stays`), where a hook between copy-back's read and its write performs a title write. With the whole-piece write the test fails; with the fix it passes. One file, `crates/victory-gpu/src/shadow.rs`, +88/-8. `cargo test -p victory-gpu`: 52 passed (1 ignored). Clippy `-D warnings` is clean. **Runtime (RTX 4070 Ti, Windows 11):** measured on our local integration build (main plus a set of open PRs, #261 among them), not on plain main. The title crashed in the world at title+0x247a098: a pointer that a title thread had just stored read back as null. With this change the crash did not come back in 5 runs. **Follow-up, not included:** when a shader's writes are scattered, each run becomes its own kernel call. In the world that cost 1.3-1.9 s per frame of copy-back. Locally we store runs directly once a piece has more than 8 of them. That relies on guard handling from our integration build, so it is left out of this branch. Related: #261 (earlier comment: https://git.gtahub.st/github/Marvisak--VIctory/pulls/261), #722. Written with Claude Code (Claude Opus 5.5); tested locally.
Author
Owner

@⁠Dufftwt on GitHub · 2026-11-24 14:01 UTC · original

@⁠Vaximous thanks. A crash in the world on Windows that stops with the change is the first in-game sign on Windows that this race matters. Until now we only had xstraok's freeze under Wine.

The per-run cost applies to my branch too (Dufftwt:fix/copy-back-only-gpu-bytes): it also makes one write_mapped call, so one WriteProcessMemory, per run. That makes four branches for this issue, and none of them is fast and tested on Windows yet:

Branch Write-back Also fixes #722 Measured in the world
xstraok copy-back-runs (a42f374) plain stores no Wine + RADV: freeze gone, copy-back median 84 ms/frame
Hiiva d44fa00 (on #261) write_mapped per run yes, keeps the earlier copied epochs not yet
Vaximous fix/copy-back-changed-runs (ba8548a) write_mapped per run no Windows, 4070 Ti: crash gone in 5 runs, copy-back 1.3–1.9 s/frame
Dufftwt fix/copy-back-only-gpu-bytes (3b2181a) write_mapped per run yes, re-reads a page before marking it current no

One question about your path for more than 8 runs, since it decides whether plain stores need a fallback: what happens if the title unmaps a page, or makes it read-only, after copy-back has read it? Our vectored handler only takes stores into blocks it guarded itself and passes any other fault on. So the process would crash where write_mapped skips the page.

**@⁠Dufftwt** on GitHub · 2026-11-24 14:01 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7133719369) @⁠Vaximous thanks. A crash in the world on Windows that stops with the change is the first in-game sign on Windows that this race matters. Until now we only had xstraok's freeze under Wine. The per-run cost applies to my branch too (`Dufftwt:fix/copy-back-only-gpu-bytes`): it also makes one `write_mapped` call, so one `WriteProcessMemory`, per run. That makes four branches for this issue, and none of them is fast and tested on Windows yet: | Branch | Write-back | Also fixes #722 | Measured in the world | |---|---|---|---| | xstraok `copy-back-runs` (`a42f374`) | plain stores | no | Wine + RADV: freeze gone, copy-back median 84 ms/frame | | Hiiva `d44fa00` (on #261) | `write_mapped` per run | yes, keeps the earlier copied epochs | not yet | | Vaximous `fix/copy-back-changed-runs` (`ba8548a`) | `write_mapped` per run | no | Windows, 4070 Ti: crash gone in 5 runs, copy-back 1.3–1.9 s/frame | | Dufftwt `fix/copy-back-only-gpu-bytes` (`3b2181a`) | `write_mapped` per run | yes, re-reads a page before marking it current | no | One question about your path for more than 8 runs, since it decides whether plain stores need a fallback: what happens if the title unmaps a page, or makes it read-only, after copy-back has read it? Our vectored handler only takes stores into blocks it guarded itself and passes any other fault on. So the process would crash where `write_mapped` skips the page.
Author
Owner

@⁠Machad3x on GitHub · 2026-11-24 14:12 UTC · original

Fix for this, with your test (it failed 3/3 before, passes 3/3 after): github.com/Machad3x/VIctory@3800387a5c on the fork branch claude/gta6-shader-translation-crashes-705550. Shadow::copy_back now collects, per 64 KiB piece, the runs of bytes the GPU changed (host != before and title != host) and writes only those with write_mapped; a piece with more than 64 runs is still written whole to bound the kernel calls (marked shortcut). We hit this on an RTX 3080 Ti: after an in-place compute kernel using two root UAVs of the same address, the title faulted reading a pointer slot with -1 in its high dword. PR creation is limited to collaborators, so this is the diff for whoever lands it.

**@⁠Machad3x** on GitHub · 2026-11-24 14:12 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7134004246) Fix for this, with your test (it failed 3/3 before, passes 3/3 after): https://github.com/Machad3x/VIctory/commit/3800387a5cce9c030c2dbaeefcde104253e7e3e6 on the fork branch claude/gta6-shader-translation-crashes-705550. Shadow::copy_back now collects, per 64 KiB piece, the runs of bytes the GPU changed (host != before and title != host) and writes only those with write_mapped; a piece with more than 64 runs is still written whole to bound the kernel calls (marked shortcut). We hit this on an RTX 3080 Ti: after an in-place compute kernel using two root UAVs of the same address, the title faulted reading a pointer slot with -1 in its high dword. PR creation is limited to collaborators, so this is the diff for whoever lands it.
Author
Owner

@⁠Dufftwt on GitHub · 2026-11-24 14:20 UTC · original

I've fixed the per-run cost in Dufftwt:fix/copy-back-only-gpu-bytes (67125b3). For each 64 KiB piece, one VirtualQuery checks that the GPU's runs lie in committed read-write memory, and if so, they're stored directly. Elsewhere each run still goes through write_mapped. A read-only page is left alone, as before, instead of faulting. A new test covers that.

This was one copy_back of 64 pages on GitHub's windows-latest, in a release build, 3 rounds each:

Shader writes Runs per page write_mapped per run Stored directly
every 2nd byte 32,768 15.6 s 14–17 ms
every 16th byte 4,096 2.0 s 5.4–5.7 ms
whole pages 1 10.7 ms 10.4–11.6 ms

So scattered writes now cost about what whole pieces did.

@⁠Machad3x thanks for the fix and the in-game report. One catch with writing pieces of more than 64 runs whole: it keeps the race exactly in the scattered pieces. Vaximous's numbers suggest those are common in the world.

**@⁠Dufftwt** on GitHub · 2026-11-24 14:20 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7134203935) I've fixed the per-run cost in `Dufftwt:fix/copy-back-only-gpu-bytes` (`67125b3`). For each 64 KiB piece, one `VirtualQuery` checks that the GPU's runs lie in committed read-write memory, and if so, they're stored directly. Elsewhere each run still goes through `write_mapped`. A read-only page is left alone, as before, instead of faulting. A new test covers that. This was one `copy_back` of 64 pages on GitHub's `windows-latest`, in a release build, 3 rounds each: | Shader writes | Runs per page | `write_mapped` per run | Stored directly | |---|---|---|---| | every 2nd byte | 32,768 | 15.6 s | 14–17 ms | | every 16th byte | 4,096 | 2.0 s | 5.4–5.7 ms | | whole pages | 1 | 10.7 ms | 10.4–11.6 ms | So scattered writes now cost about what whole pieces did. @⁠Machad3x thanks for the fix and the in-game report. One catch with writing pieces of more than 64 runs whole: it keeps the race exactly in the scattered pieces. Vaximous's numbers suggest those are common in the world.
Author
Owner

@⁠Dufftwt on GitHub · 2026-11-24 15:10 UTC · original

PRs are open again, so the fix is now #1024 (Dufftwt:fix/copy-back-only-gpu-bytes, 67125b3). It writes back only the GPU's runs, stored directly after one memory query per piece, with the timings above, and it also fixes #722.

**@⁠Dufftwt** on GitHub · 2026-11-24 15:10 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7135569831) PRs are open again, so the fix is now #1024 (`Dufftwt:fix/copy-back-only-gpu-bytes`, `67125b3`). It writes back only the GPU's runs, stored directly after one memory query per piece, with the timings above, and it also fixes #722.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:47 UTC · original

Automated triage for the maintainers.

Confirmed on current main (940d32f), including the two newest commits. cd94d35 adds a second copy-back path for pages that shaders mark as written, and 940d32f stops that path from saving the page for its own copy. Both paths in crates/victory-gpu/src/shadow.rs still read the piece, merge the GPU's bytes, and write the whole piece back with host::write_mapped:

  • Shadow::copy_back_or_not (the snapshot path, used by copy_back), per 64 KiB piece;
  • Shadow::copy_back_marked (the new marked-page path), per 64 KiB page;
  • Shadow::copy_back_page likewise writes the whole page.

A title write to bytes the GPU didn't change, landing between the read_into and the write_mapped, is still overwritten with the older value. watch::writing opens the guards only after the read. The new arm/saved mechanism keeps what the page held before the title's first write, which protects the shaders' bytes, not a title write made during the copy-back itself.

The fix is in an open pull request: #1024 (by the reporter, also fixes #722). It was written against ffe310e and is labelled as needing a rebase. The rebase should also cover the new copy_back_marked and copy_back_page, which have the same whole-piece write. The fork branches in the comments (xstraok, Hiiva, Vaximous, Machad3x) are alternatives to it.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:47 UTC · [original](https://github.com/Marvisak/VIctory/issues/721#issuecomment-7149966926) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T15:10:06Z at=2026-11-25T01:50:30Z --> Automated triage for the maintainers. Confirmed on current `main` (`940d32f`), including the two newest commits. `cd94d35` adds a second copy-back path for pages that shaders mark as written, and `940d32f` stops that path from saving the page for its own copy. Both paths in `crates/victory-gpu/src/shadow.rs` still read the piece, merge the GPU's bytes, and write the **whole** piece back with `host::write_mapped`: - `Shadow::copy_back_or_not` (the snapshot path, used by `copy_back`), per 64 KiB piece; - `Shadow::copy_back_marked` (the new marked-page path), per 64 KiB page; - `Shadow::copy_back_page` likewise writes the whole page. A title write to bytes the GPU didn't change, landing between the `read_into` and the `write_mapped`, is still overwritten with the older value. `watch::writing` opens the guards only after the read. The new arm/saved mechanism keeps what the page held *before* the title's first write, which protects the shaders' bytes, not a title write made during the copy-back itself. The fix is in an open pull request: #1024 (by the reporter, also fixes #722). It was written against `ffe310e` and is labelled as needing a rebase. The rebase should also cover the new `copy_back_marked` and `copy_back_page`, which have the same whole-piece write. The fork branches in the comments (xstraok, Hiiva, Vaximous, Machad3x) are alternatives to it. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#721
No description provided.