GPU: track numbered title-memory copies per command list in memory::ready (#16) #205

Open
AscilCH wants to merge 1 commit from upstream-pr/205 into main
AscilCH commented 2026-11-24 22:33:48 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-24T22:33:48Z.


Problem

Related to #16 (ray-tracing BVH builder stale inputs / missing box-bound updates).

In crates/victory-gpu/src/memory.rs, ready() tracked whether a command list had refreshed and declared gpu_writes for numbered title-memory copies using binary flags (list.first_use(READ) / list.first_use(WRITE)) plus n >= numbered for copies newly added during the current call:

let first_read = list.first_use(READ);
let first_write = writes && list.first_use(WRITE);
for (n, shadow) in m.copies.iter().enumerate() {
    if first_read || n >= numbered {
        shadow.refresh(list, start, len);
    }
    if writes && (first_write || n >= numbered) {
        shadow.gpu_writes(list, start, len);
    }
}

When two command lists are recorded concurrently (e.g. BVH builder stages across worker command lists):

  1. List A records its first dispatch when m.copies.len() == 10 (first_read = true, first_write = true, covers 0..10).
  2. List B records a dispatch and numbers copies 10..15 (m.copies.len() == 15).
  3. List A records its next dispatch (first_read = false, first_write = false, numbered = 15): n >= numbered is false for copies 10..15, so List A never refreshes copies 10..15 (stale inputs if List A runs before List B) and never calls shadow.gpu_writes on copies 10..15 (writes from List A into copies 10..15 are never copied back to title memory).
  4. Likewise, if a Step::Copy or Step::Run (defer_copy / defer) runs mid-list between two ready() dispatches, refreshed was cleared, but first_use(READ) stayed set so numbered copies were not refreshed after the copy.

Fix

  • Replace the binary READ/WRITE flags with per-list memory_read_seen and memory_write_seen watermarks on Deferred (crates/victory-gpu/src/host.rs), reset on HostList::reset() (memory_read_seen is also reset in defer_copy and defer alongside refreshed.clear()).
  • In memory::ready() (crates/victory-gpu/src/memory.rs), advance the list's watermarks via list.memory_sync_range(m.copies.len(), writes) and refresh m.copies[read_from..] followed by gpu_writes on m.copies[write_from..] when writes is true, keeping m.directory.0 and m.faults.0 once per list via first_use(KEEP).
  • Add host::relay_tests::memory_sync_range_covers_copies_numbered_by_other_lists covering interleaved lists, read-only → read-write promotion, mid-list title copies, and list reset.

Verification

  • rustfmt --edition 2024 --check crates/victory-gpu/src/memory.rs: clean
  • cargo test -p victory-gpu --locked -- --test-threads=1: 111 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6592756309 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/205). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-24T22:33:48Z. --- ## Problem Related to #16 (ray-tracing BVH builder stale inputs / missing box-bound updates). In `crates/victory-gpu/src/memory.rs`, `ready()` tracked whether a command list had refreshed and declared `gpu_writes` for numbered title-memory copies using binary flags (`list.first_use(READ)` / `list.first_use(WRITE)`) plus `n >= numbered` for copies newly added during the current call: ```rust let first_read = list.first_use(READ); let first_write = writes && list.first_use(WRITE); for (n, shadow) in m.copies.iter().enumerate() { if first_read || n >= numbered { shadow.refresh(list, start, len); } if writes && (first_write || n >= numbered) { shadow.gpu_writes(list, start, len); } } ``` When two command lists are recorded concurrently (e.g. BVH builder stages across worker command lists): 1. List A records its first dispatch when `m.copies.len() == 10` (`first_read = true`, `first_write = true`, covers `0..10`). 2. List B records a dispatch and numbers copies `10..15` (`m.copies.len() == 15`). 3. List A records its next dispatch (`first_read = false`, `first_write = false`, `numbered = 15`): **`n >= numbered` is false for copies `10..15`**, so List A never refreshes copies `10..15` (stale inputs if List A runs before List B) and never calls `shadow.gpu_writes` on copies `10..15` (writes from List A into copies `10..15` are never copied back to title memory). 4. Likewise, if a `Step::Copy` or `Step::Run` (`defer_copy` / `defer`) runs mid-list between two `ready()` dispatches, `refreshed` was cleared, but `first_use(READ)` stayed set so numbered copies were not refreshed after the copy. ## Fix - Replace the binary `READ`/`WRITE` flags with per-list `memory_read_seen` and `memory_write_seen` watermarks on `Deferred` (`crates/victory-gpu/src/host.rs`), reset on `HostList::reset()` (`memory_read_seen` is also reset in `defer_copy` and `defer` alongside `refreshed.clear()`). - In `memory::ready()` (`crates/victory-gpu/src/memory.rs`), advance the list's watermarks via `list.memory_sync_range(m.copies.len(), writes)` and refresh `m.copies[read_from..]` followed by `gpu_writes` on `m.copies[write_from..]` when `writes` is true, keeping `m.directory.0` and `m.faults.0` once per list via `first_use(KEEP)`. - Add `host::relay_tests::memory_sync_range_covers_copies_numbered_by_other_lists` covering interleaved lists, read-only → read-write promotion, mid-list title copies, and list reset. ## Verification - `rustfmt --edition 2024 --check crates/victory-gpu/src/memory.rs`: clean - `cargo test -p victory-gpu --locked -- --test-threads=1`: 111 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
Etr0p commented 2026-11-25 01:12:00 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Etr0p on 2026-11-25T00:31:37Z.


A note for anyone who stacks this PR together with #293 ("Performance fixes", now closed but used in several local integration trees).

This PR adds two per-list watermarks to Deferred (memory_read_seen, memory_write_seen) and relies on a list reset making a fresh Deferred to zero them. #293 changes HostList::reset to call Deferred::clear(), which keeps the container capacities but does not touch those two fields. With both merged, a reused command list keeps its old watermarks: on its next recording, memory_sync_range returns ranges that start past copies the list has never refreshed or declared written in this recording, so copies numbered before the reset (possibly by another list) are read stale and their shader stores are not looked for on copy-back, which is exactly the case #205 is meant to close.

The fix is two lines in Deferred::clear():

     fn clear(&mut self) {
         ALIVE[6].fetch_sub(self.uploads.len() as u64, Ordering::Relaxed);
         ALIVE[7].fetch_sub(self.upload_bytes, Ordering::Relaxed);
         self.upload_bytes = 0;
         self.copies.clear();
         self.uploads.clear();
         self.kept.clear();
         self.refreshed.clear();
         self.textures.clear();
         self.writes.clear();
         self.backs.clear();
         self.budget_zeroed = false;
+        self.memory_read_seen = 0;
+        self.memory_write_seen = 0;
     }

(Without #293's clear() reset nothing is needed. indirect_readable is left alone on purpose, as #293 keeps the scratch buffer readable between executions.)

How it was checked: I merged #293 onto a tree that already had this PR and resolved the conflicts by hand. Reading the merged code, Deferred::default() was no longer called on reset, so the watermarks survived it. After adding the two lines above, cargo test -p victory-gpu passes (152 tests, including #205's memory_sync_range_covers_copies_numbered_by_other_lists), and so does cargo test -p victory-shader. This was found by reading the code; I have no in-game measurement of the stale-copy case itself.

AI-assisted (Claude Code); measured on my own machine.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7148598069 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/205#issuecomment-7148598069). Originally posted by [Etr0p](https://github.com/Etr0p) on 2026-11-25T00:31:37Z. --- A note for anyone who stacks this PR together with #293 ("Performance fixes", now closed but used in several local integration trees). This PR adds two per-list watermarks to `Deferred` (`memory_read_seen`, `memory_write_seen`) and relies on a list reset making a fresh `Deferred` to zero them. #293 changes `HostList::reset` to call `Deferred::clear()`, which keeps the container capacities but does not touch those two fields. With both merged, a reused command list keeps its old watermarks: on its next recording, `memory_sync_range` returns ranges that start past copies the list has never refreshed or declared written in this recording, so copies numbered before the reset (possibly by another list) are read stale and their shader stores are not looked for on copy-back, which is exactly the case #205 is meant to close. The fix is two lines in `Deferred::clear()`: ```diff fn clear(&mut self) { ALIVE[6].fetch_sub(self.uploads.len() as u64, Ordering::Relaxed); ALIVE[7].fetch_sub(self.upload_bytes, Ordering::Relaxed); self.upload_bytes = 0; self.copies.clear(); self.uploads.clear(); self.kept.clear(); self.refreshed.clear(); self.textures.clear(); self.writes.clear(); self.backs.clear(); self.budget_zeroed = false; + self.memory_read_seen = 0; + self.memory_write_seen = 0; } ``` (Without #293's `clear()` reset nothing is needed. `indirect_readable` is left alone on purpose, as #293 keeps the scratch buffer readable between executions.) How it was checked: I merged #293 onto a tree that already had this PR and resolved the conflicts by hand. Reading the merged code, `Deferred::default()` was no longer called on reset, so the watermarks survived it. After adding the two lines above, `cargo test -p victory-gpu` passes (152 tests, including #205's `memory_sync_range_covers_copies_numbered_by_other_lists`), and so does `cargo test -p victory-shader`. This was found by reading the code; I have no in-game measurement of the stale-copy case itself. AI-assisted (Claude Code); measured on my own machine.
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/205:upstream-pr/205'
git switch 'upstream-pr/205'

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