Skip to content

GPU: preserve disjoint shadow refresh ranges - #1818

Open
aycow wants to merge 6 commits into
Marvisak:mainfrom
aycow:fix/disjoint-shadow-refresh-current
Open

aycow wants to merge 6 commits into
Marvisak:mainfrom
aycow:fix/disjoint-shadow-refresh-current

Conversation

@aycow

@aycow aycow commented Nov 24, 2026 •

Copy link
Copy Markdown

What changed

Keep the union of requested refresh ranges through the pending and completed parts of a submission batch. The previous bounding range copied 192 KiB for two 64 KiB requests and consumed the dirty page in the gap before its reader. The fixture now copies 128 KiB for the two requests and 64 KiB when the gap is requested later. The common one-range case retains inline storage.

Thanks to @pablogonz12 for the original reproduction in #185. This carries the narrow range correction onto the current unified journal. It preserves the ranges supplied to the batch. Callers that pass a bounding range still request all of it. #1472 changes when the batch flushes around a GPU write. This PR retains the requested ranges without altering those flush points. Original report · Related flush work

Verification

All four disjoint refresh regressions passed on AMD Radeon Graphics and WARP at 4a191d8 with native device creation required. The cases cover the gap's later reader, repeated reads after a physical alias write, decommit and remap at the same address, and partial refreshes of declared UAV writes. The last case exercises #1322's preparation contract and confirms copyback preserves a later CPU write to a neighboring byte. Strict diagnostics Clippy passed. The updated fixture also verifies that zeroing an unmapped page leaves the copied-byte count at zero.

$env:VICTORY_TEST_SHADOW_DISJOINT_REQUIRE_GPU = '1'
cargo test -p victory-gpu --features diagnostics --locked --lib disjoint -- --nocapture --test-threads=1
$env:VICTORY_D3D12_WARP = '1'
cargo test -p victory-gpu --features diagnostics --locked --lib disjoint -- --nocapture --test-threads=1

The copied-byte results are from the synthetic fixture. Title preparation and frame-time effects remain unverified.

AI-assisted using Codex. Generated test data only.

Keep the requested union through pending and completed preparation. Leave dirty gaps for later readers and retain one inline range for the common case.

Reproduces the copy amplification reported by pablogonz12 on Marvisak#185 (Marvisak#185 (comment)) and carries forward the range behavior from 20ff80e on the current unified journal.
Exercise independent shadow observers, concurrent alias writes over three submissions, a same-address decommit and recommit, and a pending GPU-written dword next to partial CPU refreshes.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Disjoint shadow refresh ranges preserved: 18K gpu diff on shadow/refresh range tracking. Windows-only.

@Melow-code Melow-code left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Union logic is correct — half-open ranges, dedup_by in the right order, covers only called after merge. Two disjoint ranges in one page are safe because Tracked::take keeps a partly-copied page as changed.

Two points before merge:

  1. Each disjoint range is now a separate update() call with its own locks. A frame-time measurement from a world scene would show if this matters.
  2. PR #1555 also changes how refreshes combine (joining nearby ones). The two change the same code — only one approach should land.

@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown
Author

@ruipedro-pinheiro Agreed on comparing the per-range cost in a matched world run. Each disjoint span does call Shadow::update separately. The synthetic case confirms the copy-volume change from 192 KiB to 128 KiB and preserves the gap's dirty page for its later reader. It leaves world frame time unverified.

I checked @Texbirds’ #1555 at 9fb81e1. It joins gaps of up to and including 64 KiB, so it would join the exact dirty 64 KiB gap in this fixture. #1818 merges touching ranges and leaves that gap for its reader. These are alternative policies in the same batch code. I agree that we should pick one after comparing both.

@pablogonz12 If an authorized world run is available, could you compare 70564ae, #1818 at 24a7d70, and #1555 at 9fb81e1 as separate warm-cache builds? Please hold the rest of the stack, adapter, scene, inputs and frame window fixed. Aggregate frame time, preparation and batch-flush time, bytes copied and watch queries from the diagnostics frame lines would show whether the saved copies outweigh the extra calls. Draw and submission counts help check that the windows match. Review · #1555

AI-assisted using Codex.

@pablogonz12

Copy link
Copy Markdown
Contributor

Noted. A matched warm-cache world A/B of 70564ae, #1818 at 24a7d70 and #1555 at 9fb81e1, as three separate builds, is on our list for the next free GPU slot; I will post the frame numbers here once it has run. Not tested yet, so no numbers today.

@Melow-code

Copy link
Copy Markdown

Thanks — a matched warm-cache world A/B of main, #1818 and #1555 is exactly what the per-range question needs, and the synthetic case already shows the copy-volume change going the right way. I will read the frame numbers when they land. (Reply written with DeepSeek V4 Pro, OpenCode.)

zhaibingye added a commit to zhaibingye/VIctory that referenced this pull request Nov 25, 2026
Retain original author history and tests. Copy only the requested union,
leaving interior dirty pages for their own readers.

Verified the original patch ID, a portable exhaustive small-range model,
and Windows MSVC workspace test compilation. No local Windows GPU run.
@aycow

aycow commented Nov 25, 2026

Copy link
Copy Markdown
Author

Updated this branch to ccfb029 at 254a67f. The disjoint-refresh implementation is unchanged by the follow-up. The regression now matches #1322: a declared UAV range refreshes CPU-written bytes before execution and updates its copyback snapshot. In the fixture, both requested spans refresh and the gap stays untouched. After the GPU changes one byte, copyback preserves a later CPU write to its neighbor.

All four disjoint tests passed with a required device on the default AMD adapter and WARP. The matched warm-cache comparison of main, #1818, and #1555 remains the evidence needed for the per-range performance question. The original comparison heads are still valid to reproduce the originally queued test.

qub3d2 pushed a commit to qub3d2/victory-fork that referenced this pull request Nov 25, 2026
Adapted from Aydan (aycow) in Marvisak/VIctory#1818.
Thanks to pablogonz12 for the original reproduction in Marvisak/VIctory#185.
ZaneIceMaster11 pushed a commit to ZaneIceMaster11/victory-fork that referenced this pull request Nov 25, 2026
Keep the union of overlapping and adjacent refresh ranges per shadow. Cover range normalization with CPU unit tests and preserve the WARP readback regression.

Adapted from Aydan (aycow) in Marvisak/VIctory#1818.
Thanks to pablogonz12 for the original reproduction in Marvisak/VIctory#185.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work (kept). The new head only merges current main (f3578e3) into the branch. The PR's own diff to crates/victory-gpu/src/host.rs (Batch, RefreshRanges) and watch.rs is identical to the one reviewed last time, and it merges cleanly.

The open points from the last review are still open:

CI: passing (check).


Generated by Claude Code

RicerBoy pushed a commit to RicerBoy/DropItLikeItsHot that referenced this pull request Nov 25, 2026
Part of #32 (VIwork port).

Adapted from Aydan (aycow) in Marvisak/VIctory#1818.
Thanks to pablogonz12 for the original reproduction in Marvisak/VIctory#185.

(cherry picked from commit 0d22b3808d0e5413dd845c771de5d6977cfbdc20)

(cherry picked from commit 3cd984de3a3d6fc31e983e8120f941f5d6ad3565)

@Melow-code Melow-code left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the union logic against Shadow::update and Tracked::take on main (70564ae). I found no correctness bug.

  • RefreshRanges::add and merge treat the ranges as half-open. They join ranges that touch and keep ranges with a gap apart.
  • The dedup_by closure gets the later element first, so previous.1 = max(...) extends the kept range.
  • covers needs sorted, disjoint ranges. Every path that writes current calls merge first, so this holds.
  • A debug_assert! on that order in covers would protect later callers.
  • Two disjoint ranges in one page are safe. Tracked::take keeps a page that it copies only in part as changed. So the second range still copies its bytes.

Two points to check before merge:

  1. Cost. Each disjoint range is now one update call, and each call takes the pending and changed locks and the guards. One copy with many small disjoint requests now makes many calls in place of one. A frame-time number from a world scene would show if this matters.
  2. Overlap with #1555. That PR also changes how the batch combines refreshes (it joins nearby ones). The two change the same code, so only one approach should land.

The CloseHandle in the twin_views test helper is correct. The views keep the section alive.

@Melow-code Melow-code left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the union logic against Shadow::update and Tracked::take on main (70564ae). I found no correctness bug.

  • RefreshRanges::add and merge treat the ranges as half-open. They join ranges that touch and keep ranges with a gap apart.
  • The dedup_by closure gets the later element first, so previous.1 = max(...) extends the kept range.
  • covers needs sorted, disjoint ranges. Every path that writes current calls merge first, so this holds.
  • A debug_assert! on that order in covers would protect later callers.
  • Two disjoint ranges in one page are safe. Tracked::take keeps a page that it copies only in part as changed. So the second range still copies its bytes.

Two points to check before merge:

  1. Cost. Each disjoint range is now one update call, and each call takes the pending and changed locks and the guards. One copy with many small disjoint requests now makes many calls in place of one. A frame-time number from a world scene would show if this matters.
  2. Overlap with #1555. That PR also changes how the batch combines refreshes (it joins nearby ones). The two change the same code, so only one approach should land.

The CloseHandle in the twin_views test helper is correct. The views keep the section alive.

@Melow-code Melow-code left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the union logic against Shadow::update and Tracked::take on main (70564ae). I found no correctness bug.

  • RefreshRanges::add and merge treat the ranges as half-open. They join ranges that touch and keep ranges with a gap apart.
  • The dedup_by closure gets the later element first, so previous.1 = max(...) extends the kept range.
  • covers needs sorted, disjoint ranges. Every path that writes current calls merge first, so this holds.
  • A debug_assert! on that order in covers would protect later callers.
  • Two disjoint ranges in one page are safe. Tracked::take keeps a page that it copies only in part as changed. So the second range still copies its bytes.

Two points to check before merge:

  1. Cost. Each disjoint range is now one update call, and each call takes the pending and changed locks and the guards. One copy with many small disjoint requests now makes many calls in place of one. A frame-time number from a world scene would show if this matters.
  2. Overlap with #1555. That PR also changes how the batch combines refreshes (it joins nearby ones). The two change the same code, so only one approach should land.

The CloseHandle in the twin_views test helper is correct. The views keep the section alive.

@shortselow-byte

Copy link
Copy Markdown

Merged onto current main (940d32f), this no longer compiles its tests, although GitHub shows it as mergeable. The branch is based on ccfb029, one commit before cd94d35 ("GPU: shaders mark the pages they write through views"), which gave Shadow::writing a third parameter:

error[E0061]: this method takes 3 arguments but 2 arguments were supplied
    --> crates\victory-gpu\src\host.rs:3223:14
     |
3223 |         copy.writing(busy, 4);
     |              ^^^^^^^--------- argument #3 of type `bool` is missing
note: method defined here
    --> crates\victory-gpu\src\shadow.rs:274:19
     |
 274 |     pub(crate) fn writing(&self, address: usize, len: usize, marked: bool) {

So cargo test -p victory-gpu (and clippy --all-targets) would fail on main after the merge. Checked locally on Windows 11, Rust 1.98.1 MSVC, AMD Radeon RX 6900 XT, in an isolated target directory:

  • with copy.writing(busy, 4, false), disjoint_partial_refreshes_update_declared_write_snapshots passes;
  • with true, it fails at host.rs:3250.

So a rebase with false there looks like all it needs. I only ran that one test with the fix, not the whole workspace.

@Texbirds

Copy link
Copy Markdown
Contributor

Thanks for the implementation and regression coverage. As the author of #1555, I think minimizing copied bytes and minimizing preparation time are two different objectives.

The 64 KiB coalescing threshold in #1555 intentionally reduces Shadow::update() calls, lock acquisitions and change-tracking overhead. In the three-page regression, assuming no intervening writes or invalidations, both approaches ultimately copy 192 KiB, but #1555 requires one update call instead of three.

The findings in #2134 and #2136 further support investigating this overhead independently of copy volume.

Given the recently reported compilation issue against main (940d32f) and the GPU write-tracking changes in cd94d35, both PRs should be validated and benchmarked against the same updated baseline.

I also see potential in combining #1818's exact range tracking with #1555's batching strategy through shared locking.

Ultimately, correctness, preparation time and actual frame time should determine which approach lands. Neither approach has demonstrated superior real-world performance yet.

@aycow

aycow commented Nov 25, 2026

Copy link
Copy Markdown
Author

@shortselow-byte Thanks for isolating the compile error and checking both values of the new parameter. The merge at 4a191d8 uses marked=false for the declared-write fixture and preserves main's clean-shadow shortcut. All four disjoint tests passed on default hardware and WARP, including unmapped-page zeroing and preservation of the neighboring CPU byte.

@Texbirds The current control measures copy volume at the first read. If the gap is eventually read with no intervening changes, both approaches can ultimately move the same number of bytes and the lock and query costs can decide the result. The matched title comparison on the same current-main baseline is still pending. This update makes no title frame-time claim.

lopesdevi pushed a commit to lopesdevi/victory-dedsec-gta that referenced this pull request Nov 25, 2026
Adapted from Aydan (aycow) in Marvisak/VIctory#1818.
Thanks to pablogonz12 for the original reproduction in Marvisak/VIctory#185.

(cherry picked from commit 0d22b3808d0e5413dd845c771de5d6977cfbdc20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants