GPU: deduplicate identical per-list defer_gpu_write declarations #3114

Open
github-bot wants to merge 2 commits from github-bot/gh-3114 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 08:29 UTC · original
fuzeofficlal:perf/gpu-dedup-per-list-gpu-writes → main · +389 −9 · 1 files · 2 commits · open

Summary

HostList::defer_gpu_write previously pushed a new Step::GpuWrite into Deferred::copies on every draw or dispatch, even when the same command list repeatedly declared the exact same (shadow, address, len, marked, memory) tuple with no intervening step that writes title memory. In world command lists this appends thousands of redundant Step::GpuWrite entries per frame, repeating Arc::clone, batch.flush() / shadow.writing(...) lock acquisitions in run_deferred, and HashMap deduplication work in Host::execute.

This change mirrors the existing per-list Deferred::refreshed deduplication:

  • Track recorded declarations in Deferred::gpu_writes: HashSet<(usize, usize, usize, bool, bool)> keyed by (Arc::as_ptr(shadow) as usize, address, len, marked, memory).
    • Including both marked and memory in the key ensures that an earlier marked == true declaration never suppresses a subsequent marked == false declaration (which must flush and take a pre-submit snapshot), and an earlier memory == false declaration never suppresses a subsequent memory == true declaration (which governs withholding() on title-memory misses).
    • Because the first accepted declaration pushes Step::GpuWrite(shadow.clone(), ...) into Deferred::copies, the Arc<Shadow> stays alive until HostList::reset() replaces Deferred, so Arc::as_ptr(shadow) cannot alias another copy while in gpu_writes.
  • Add Deferred::title_written(&mut self) to clear both refreshed and gpu_writes, called by HostList::defer_copy and HostList::defer (merging defer's two self.deferred() lock acquisitions into one). HostList::reset continues to reset the entire Deferred struct.

Verification

  • Added four tests in crates/victory-gpu/src/host.rs (host::gpu_write_dedup::*):
    • identical_gpu_writes_deduplicate_until_title_written_or_reset
    • distinct_marked_and_memory_flags_are_never_collapsed
    • deduped_marked_then_unmarked_and_post_copy_writes_copy_back
    • deduped_ordinary_then_memory_write_still_withholds_on_miss
  • Hardware D3D12 (RTX 5090): cargo test -p victory-gpu --lib --locked -- host::gpu_write_dedup:: host::batch:: host::withholding:: — 6 passed, 0 failed.
  • WARP (VICTORY_D3D12_WARP=1): cargo test -p victory-gpu --locked --features diagnostics — 250 passed, 0 failed (1 existing benchmark ignored), verified across 4 consecutive parallel runs.
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features: 0 warnings.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 08:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3114) `fuzeofficlal:perf/gpu-dedup-per-list-gpu-writes` → `main` · +389 −9 · 1 files · 2 commits · open ### Summary `HostList::defer_gpu_write` previously pushed a new `Step::GpuWrite` into `Deferred::copies` on every draw or dispatch, even when the same command list repeatedly declared the exact same `(shadow, address, len, marked, memory)` tuple with no intervening step that writes title memory. In world command lists this appends thousands of redundant `Step::GpuWrite` entries per frame, repeating `Arc::clone`, `batch.flush()` / `shadow.writing(...)` lock acquisitions in `run_deferred`, and `HashMap` deduplication work in `Host::execute`. This change mirrors the existing per-list `Deferred::refreshed` deduplication: - Track recorded declarations in `Deferred::gpu_writes: HashSet<(usize, usize, usize, bool, bool)>` keyed by `(Arc::as_ptr(shadow) as usize, address, len, marked, memory)`. - Including both `marked` and `memory` in the key ensures that an earlier `marked == true` declaration never suppresses a subsequent `marked == false` declaration (which must flush and take a pre-submit snapshot), and an earlier `memory == false` declaration never suppresses a subsequent `memory == true` declaration (which governs `withholding()` on title-memory misses). - Because the first accepted declaration pushes `Step::GpuWrite(shadow.clone(), ...)` into `Deferred::copies`, the `Arc<Shadow>` stays alive until `HostList::reset()` replaces `Deferred`, so `Arc::as_ptr(shadow)` cannot alias another copy while in `gpu_writes`. - Add `Deferred::title_written(&mut self)` to clear both `refreshed` and `gpu_writes`, called by `HostList::defer_copy` and `HostList::defer` (merging `defer`'s two `self.deferred()` lock acquisitions into one). `HostList::reset` continues to reset the entire `Deferred` struct. ### Verification - Added four tests in `crates/victory-gpu/src/host.rs` (`host::gpu_write_dedup::*`): - `identical_gpu_writes_deduplicate_until_title_written_or_reset` - `distinct_marked_and_memory_flags_are_never_collapsed` - `deduped_marked_then_unmarked_and_post_copy_writes_copy_back` - `deduped_ordinary_then_memory_write_still_withholds_on_miss` - Hardware D3D12 (RTX 5090): `cargo test -p victory-gpu --lib --locked -- host::gpu_write_dedup:: host::batch:: host::withholding::` — 6 passed, 0 failed. - WARP (`VICTORY_D3D12_WARP=1`): `cargo test -p victory-gpu --locked --features diagnostics` — 250 passed, 0 failed (1 existing benchmark ignored), verified across 4 consecutive parallel runs. - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features`: 0 warnings. - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed.
HostList::defer_gpu_write previously appended a Step::GpuWrite entry on
every dispatch/draw unconditionally, even when the same command list
repeatedly declared the exact same (shadow, address, len, marked, memory)
tuple with no intervening title-memory write.

Track declared writes in Deferred::gpu_writes keyed by
(Arc::as_ptr(shadow), address, len, marked, memory)—preserving every
distinct (marked, memory) combination so unmarked snapshots and
title-memory withholding semantics are never lost—and clear both
refreshed and gpu_writes via Deferred::title_written() on defer_copy and
defer (as well as HostList::reset).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:34:13 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 08:53 UTC · original · approved these changes

Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib host::gpu_write_dedup:: -- --test-threads=1: 4/4 passed on RTX 5090 and WARP.
  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1: 10/10 passed on RTX 5090 and WARP.
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed on RTX 5090.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly deduplicates identical per-list defer_gpu_write declarations without collapsing distinct marked/memory flags or breaking post-copy/withholding semantics.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 08:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/3114#pullrequestreview-6071021448) · **approved these changes** Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib host::gpu_write_dedup:: -- --test-threads=1`: 4/4 passed on RTX 5090 and WARP. - `cargo test -p victory-gpu --lib shadow:: -- --test-threads=1`: 10/10 passed on RTX 5090 and WARP. - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 20/20 passed on RTX 5090. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly deduplicates identical per-list `defer_gpu_write` declarations without collapsing distinct marked/memory flags or breaking post-copy/withholding semantics. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 09:19 UTC · original

Automated triage for the maintainers.

Needs work: CI is failing. By my reading the change is right. HostList::defer_gpu_write (crates/victory-gpu/src/host.rs) skips a Step::GpuWrite that repeats an identical (shadow, address, len, marked, memory) declaration until defer_copy, defer or reset clears the set. On main, a repeated step only runs batch.flush() and Shadow::writing, and writing does nothing for a range it already holds. Dropping the repeat therefore only defers a flush that a later step does anyway. The key includes marked and memory, which keeps the snapshot and withholding behaviour. The Arc held by the first step stops the pointer key from being reused. Please fix the failing check job. Also coordinate with #3113, which adds its own Deferred::title_written: the merged helper should clear refreshed, filled and gpu_writes. With CI green this would be a merge candidate.

CI: check failing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 09:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3114#issuecomment-7187801216) <!-- victory-triage v1 head=ddcc62673e602b302521b06761baa73bf331d500 verdict=needs-work at=2026-11-26T09:21:00Z --> Automated triage for the maintainers. **Needs work: CI is failing.** By my reading the change is right. `HostList::defer_gpu_write` (`crates/victory-gpu/src/host.rs`) skips a `Step::GpuWrite` that repeats an identical `(shadow, address, len, marked, memory)` declaration until `defer_copy`, `defer` or `reset` clears the set. On `main`, a repeated step only runs `batch.flush()` and `Shadow::writing`, and `writing` does nothing for a range it already holds. Dropping the repeat therefore only defers a flush that a later step does anyway. The key includes `marked` and `memory`, which keeps the snapshot and withholding behaviour. The `Arc` held by the first step stops the pointer key from being reused. Please fix the failing `check` job. Also coordinate with #3113, which adds its own `Deferred::title_written`: the merged helper should clear `refreshed`, `filled` and `gpu_writes`. With CI green this would be a merge candidate. CI: `check` failing. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot force-pushed github-bot/gh-3114 from ddcc62673e to 09cda65e0b 2026-11-26 11:51:32 +00:00 Compare
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:48 UTC · original

Re-pushed identical tree (ddcc6267 -> 09cda65e) to re-trigger CI after an unrelated flaky failure in descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3114#issuecomment-7191200337) Re-pushed identical tree (`ddcc6267` -> `09cda65e`) to re-trigger CI after an unrelated flaky failure in `descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait`.
Author
Owner

@⁠yumaste on GitHub · 2026-11-26 14:09 UTC · original

I tested an ordering case on Windows with WARP. A repeated unmarked Step::GpuWrite can still matter as a flush boundary for a refresh queued since the preceding declaration. A later Step::Host can observe that boundary, for example when reading a shadow in diagnostics.

The fixture declares an unmarked write to A, queues a refresh of dirty B, repeats the identical write to A, and reads B in defer_host. The capture is the byte observed at that step.

Build B observed in host step B after submission
main 63bcca4 0x22 0x22
#3114 09cda65 0x11 0x22
#3114 with a pending-refresh guard 0x22 0x22

I verified a local guard that sets a flag when Refresh, Heap or Memory is recorded, and keeps the next unmarked write even when its key is already present. That recorded write clears the flag because it flushes the batch. Copy and Run also clear it through title_written. The guarded tree passed all 41 tests selected by host:: with diagnostics under WARP, including the four tests from this PR and the reproducer below. The fixture establishes the deferred-ordering difference without any title assets.

Regression test for host.rs, inside the existing test module withholding
#[test]
fn repeated_write_flushes_a_later_refresh_before_host_work() {
    use crate::watch::PAGE;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU32, Ordering};

    let host = super::host().expect("WARP");
    let a = crate::watch::tests::pool(1);
    let b = crate::watch::tests::pool(1);
    // SAFETY: these bytes are in our allocated pools.
    unsafe { (b as *mut u8).write_volatile(0x11) };
    for address in [a, b] {
        crate::shadow::register(address, address as u64, PAGE as u64);
    }
    let write = crate::shadow::find(a as u64, PAGE as u64).unwrap();
    let read = crate::shadow::find(b as u64, PAGE as u64).unwrap();
    read.update(b, PAGE);
    assert_eq!(read.read_host(b as u64, 1)[0] & 0xff, 0x11);

    {
        let _guard = crate::watch::writing(b, 1);
        // SAFETY: the guard opens the page for this write.
        unsafe { (b as *mut u8).write_volatile(0x22) };
    }
    let list = host.new_list().unwrap();
    write.gpu_writes(&list, a as u64, PAGE as u64, false);
    read.refresh(&list, b as u64, PAGE as u64);
    write.gpu_writes(&list, a as u64, PAGE as u64, false);
    let observed = Arc::new(AtomicU32::new(0));
    let at_host_step = observed.clone();
    let read_at_host_step = read.clone();
    list.defer_host(move || {
        at_host_step.store(read_at_host_step.read_host(b as u64, 1)[0] & 0xff, Ordering::Relaxed);
    });
    list.close();
    host.execute(&[&list]);
    let intermediate = observed.load(Ordering::Relaxed);
    let end = read.read_host(b as u64, 1)[0] & 0xff;

    drop(list);
    for address in [a, b] {
        crate::shadow::unregister(address, address as u64);
    }
    drop((write, read));
    for address in [a, b] {
        crate::watch::tests::free(address);
    }
    assert_eq!(end, 0x22, "the final flush happened");
    assert_eq!(intermediate, 0x22, "host step must see the refreshed shadow");
}
$env:VICTORY_D3D12_WARP = '1'
cargo test --locked -p victory-gpu --lib --features diagnostics repeated_write_flushes_a_later_refresh_before_host_work -- --test-threads=1

AI-assisted (Codex).

**@⁠yumaste** on GitHub · 2026-11-26 14:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/3114#issuecomment-7194348017) I tested an ordering case on Windows with WARP. A repeated unmarked `Step::GpuWrite` can still matter as a flush boundary for a refresh queued since the preceding declaration. A later `Step::Host` can observe that boundary, for example when reading a shadow in diagnostics. The fixture declares an unmarked write to A, queues a refresh of dirty B, repeats the identical write to A, and reads B in `defer_host`. The capture is the byte observed at that step. | Build | B observed in host step | B after submission | | --- | ---: | ---: | | `main` `63bcca4` | `0x22` | `0x22` | | #3114 `09cda65` | `0x11` | `0x22` | | #3114 with a pending-refresh guard | `0x22` | `0x22` | I verified a local guard that sets a flag when `Refresh`, `Heap` or `Memory` is recorded, and keeps the next unmarked write even when its key is already present. That recorded write clears the flag because it flushes the batch. `Copy` and `Run` also clear it through `title_written`. The guarded tree passed all 41 tests selected by `host::` with diagnostics under WARP, including the four tests from this PR and the reproducer below. The fixture establishes the deferred-ordering difference without any title assets. <details> <summary>Regression test for host.rs, inside the existing test module withholding</summary> ```rust #[test] fn repeated_write_flushes_a_later_refresh_before_host_work() { use crate::watch::PAGE; use std::sync::Arc; use std::sync::atomic::{AtomicU32, Ordering}; let host = super::host().expect("WARP"); let a = crate::watch::tests::pool(1); let b = crate::watch::tests::pool(1); // SAFETY: these bytes are in our allocated pools. unsafe { (b as *mut u8).write_volatile(0x11) }; for address in [a, b] { crate::shadow::register(address, address as u64, PAGE as u64); } let write = crate::shadow::find(a as u64, PAGE as u64).unwrap(); let read = crate::shadow::find(b as u64, PAGE as u64).unwrap(); read.update(b, PAGE); assert_eq!(read.read_host(b as u64, 1)[0] & 0xff, 0x11); { let _guard = crate::watch::writing(b, 1); // SAFETY: the guard opens the page for this write. unsafe { (b as *mut u8).write_volatile(0x22) }; } let list = host.new_list().unwrap(); write.gpu_writes(&list, a as u64, PAGE as u64, false); read.refresh(&list, b as u64, PAGE as u64); write.gpu_writes(&list, a as u64, PAGE as u64, false); let observed = Arc::new(AtomicU32::new(0)); let at_host_step = observed.clone(); let read_at_host_step = read.clone(); list.defer_host(move || { at_host_step.store(read_at_host_step.read_host(b as u64, 1)[0] & 0xff, Ordering::Relaxed); }); list.close(); host.execute(&[&list]); let intermediate = observed.load(Ordering::Relaxed); let end = read.read_host(b as u64, 1)[0] & 0xff; drop(list); for address in [a, b] { crate::shadow::unregister(address, address as u64); } drop((write, read)); for address in [a, b] { crate::watch::tests::free(address); } assert_eq!(end, 0x22, "the final flush happened"); assert_eq!(intermediate, 0x22, "host step must see the refreshed shadow"); } ``` ```powershell $env:VICTORY_D3D12_WARP = '1' cargo test --locked -p victory-gpu --lib --features diagnostics repeated_write_flushes_a_later_refresh_before_host_work -- --test-threads=1 ``` </details> AI-assisted (Codex).
Track whether a Refresh, Heap, or Memory step has been queued on Deferred since the last Batch::flush boundary (Copy, Run, or unmarked GpuWrite).

When pending_refresh is set, HostList::defer_gpu_write still records an unmarked Step::GpuWrite even if its key is already in Deferred::gpu_writes and then clears pending_refresh, so subsequent Step::Host callbacks observe the same flush boundary as main. Marked writes do not flush the batch and leave pending_refresh set; title_written (Copy/Run) and HostList::reset clear it.

Add a regression test covering Refresh, Heap, Memory, marked-write preservation, and Copy/Run clearing.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 16:41 UTC · original

Thanks @⁠yumaste for catching this edge case and putting together a minimal reproducer!

Pushed ccbba392 adding a Deferred::pending_refresh guard set when Step::Refresh, Step::Heap, or Step::Memory is recorded, cleared on title_written (Copy/Run), HostList::reset, and any recorded unmarked Step::GpuWrite (while leaving it set for marked writes, which do not flush Batch). While pending_refresh is set, a repeated unmarked defer_gpu_write is still recorded once as a flush boundary before deduplicating subsequent repeats again.

Added regression test host::gpu_write_dedup::repeated_unmarked_write_flushes_pending_refresh_before_host_work covering the Refresh -> defer_host ordering (0x11 before fix -> 0x22 after), marked preservation, Heap/Memory queueing, and Copy/Run clearing, verified on both RTX 5090 and WARP with VICTORY_D3D12_DEBUG=1.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/3114#issuecomment-7197966024) Thanks @⁠yumaste for catching this edge case and putting together a minimal reproducer! Pushed `ccbba392` adding a `Deferred::pending_refresh` guard set when `Step::Refresh`, `Step::Heap`, or `Step::Memory` is recorded, cleared on `title_written` (`Copy`/`Run`), `HostList::reset`, and any recorded unmarked `Step::GpuWrite` (while leaving it set for `marked` writes, which do not flush `Batch`). While `pending_refresh` is set, a repeated unmarked `defer_gpu_write` is still recorded once as a flush boundary before deduplicating subsequent repeats again. Added regression test `host::gpu_write_dedup::repeated_unmarked_write_flushes_pending_refresh_before_host_work` covering the `Refresh` -> `defer_host` ordering (`0x11` before fix -> `0x22` after), `marked` preservation, `Heap`/`Memory` queueing, and `Copy`/`Run` clearing, verified on both RTX 5090 and WARP with `VICTORY_D3D12_DEBUG=1`.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3114'
git switch 'github-bot/gh-3114'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3114'
git switch 'github-bot/gh-3114'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3114'
git switch 'main'
git merge --squash 'github-bot/gh-3114'
git switch 'main'
git merge --ff-only 'github-bot/gh-3114'
git switch 'main'
git merge 'github-bot/gh-3114'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!3114
No description provided.