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

Open
github-bot wants to merge 1 commit from github-bot/gh-205 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-24 22:33 UTC · original
AscilCH:fix/memory-per-list-copy-sync → main · +73 −17 · 2 files · 1 commits · open

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

**@⁠AscilCH** on GitHub · 2026-11-24 22:33 UTC · [original](https://github.com/fxceless/victory-fork/pull/205) `AscilCH:fix/memory-per-list-copy-sync` → `main` · +73 −17 · 2 files · 1 commits · open ## 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.)*
In `memory::ready`, `list.first_use(READ)` and `list.first_use(WRITE)` only
tracked whether a list had ever used raw title memory, plus `n >= numbered`
for copies newly added during the current call. When two command lists were
recorded concurrently (such as the ray-tracing BVH builder across worker
lists), copies numbered by list B between list A's dispatches were never
refreshed or marked `gpu_writes` on list A — leaving list A reading stale
inputs and dropping list A's writes to those copies on copy-back.

Replace the binary flags with per-list `memory_read_seen` and
`memory_write_seen` watermarks on `Deferred` (reset on list reset, with
`memory_read_seen` also reset after `defer_copy` and `defer` alongside
`refreshed.clear()`). Add a unit test covering interleaved lists, read-only
to read-write transitions, and mid-list title copies.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:38:15 +00:00
Author
Owner

@⁠Etr0p on GitHub · 2026-11-25 00:31 UTC · original

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.

**@⁠Etr0p** on GitHub · 2026-11-25 00:31 UTC · [original](https://github.com/fxceless/victory-fork/pull/205#issuecomment-7148598069) 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 has changes conflicting with the target branch.
  • crates/victory-gpu/src/host.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/205/head:github-bot/gh-205'
git switch 'github-bot/gh-205'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-205'
git switch 'github-bot/gh-205'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-205'
git switch 'github-bot/gh-205'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-205'
git switch 'main'
git merge --squash 'github-bot/gh-205'
git switch 'main'
git merge --ff-only 'github-bot/gh-205'
git switch 'main'
git merge 'github-bot/gh-205'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.