Skip to content

GPU: ranges shaders may write are armed as runs of pages, not page by page - #2147

Open
r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:gpu/arm-page-runs
Open

r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:gpu/arm-page-runs

Conversation

@r4tedx

@r4tedx r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Since cd94d35 a range declared as written through views whose writes shaders mark keeps no snapshot: Shadow::writing arms it instead (Tracked::arm), so that the title's first write to one of its pages keeps the page's bytes from before. Arming puts every 64 KiB page of the range into a BTreeSet, once per declaration.

A root view declares its copy from its address to the copy's end (draw.rs, root_view: shadow.left(address) when the copy is marked). On the world's 174 MB heap that is up to 2,784 set entries for each such draw or dispatch, however many declarations before it armed the same pages, and HostList::defer_gpu_write does not merge them.

The change

watch::Saved keeps the armed pages as the runs declared, none inside another, and the single pages a copy-back takes out (Tracked::unarm) in a set of their own until the tracker is disarmed. A declaration inside a run already armed costs a scan of the runs; a root view's runs all end at the copy's end, so the heap's collapse into one.

Which pages are armed at any moment is the same as before: arm re-arms a page that was taken out, as the set insert did, and disarm clears both.

Numbers (synthetic)

The unit cost printed by the benchmark of #2134, WARP, diag profile with diagnostics, 200 declarations each:

declaring as written (marked) main this branch
the 174 MiB heap from its start to its end 31–36 µs each under 0.05 µs
the heap's last 1 MiB 0.1 µs under 0.05 µs

That benchmark's frame declares the heap 66 times, so this is about 2 ms of its 168–225 ms preparing: below the run-to-run spread, and I am not claiming a frame time from it. The job that measured this branch ran with the machine busy (main alone gave 306 and 722 ms in it), so its frame times compare nothing.

How much this is in the title depends on how many ranges a world frame declares through marked root views, which no published log says: the GPU writes step of the copies per frame line (563 ms in the #400 run) holds this together with the refresh batch that step flushes and the snapshots. At thousands of draws a frame it could be tens of milliseconds; a tester's line before and after would say.

Not a duplicate of

#435 merges identical declarations (same copy, address and length) at submission, and #2102 changes the hasher of the per-list and per-submission sets. Neither changes what one declaration costs to arm. This merges cleanly with #2136.

Tested

  • New watch::tests::armed_ranges_keep_their_pages_bytes_from_before_the_first_write: pages of overlapping armed ranges keep their bytes from before the first write, a page next to the range and a page taken out keep none, arming again arms it again, and nothing is kept after disarm.
  • shadow::tests::marked_pages_go_back_by_the_bytes_the_gpu_changed, which arms a copy and relies on the saved page, passes unchanged.
  • The victory-gpu suite on WARP (diagnostics build, VICTORY_D3D12_WARP=1, one thread): 147 passed, 0 failed, 2 ignored.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings, with and without --features diagnostics: clean.

Not run: the title; any hardware GPU (the suite and the benchmark ran on the software adapter only); cargo test --workspace. The title was not used in these tests.

AI-assisted

🤖 Generated with Claude Code

… page

A range declared as written through views whose writes shaders mark is
armed, so that the title's first write to one of its pages keeps the
page's bytes from before. Arming put every page of the range into a
set: a root view declares its copy from its address to the end, so each
declaration on the world's 174 MB heap cost up to 2,784 set entries,
31-36 microseconds in a synthetic measurement, however many
declarations before it had armed the same pages.

The armed pages are now kept as the runs declared, none inside another,
with the single pages a copy-back takes out of them kept apart until
the tracker is disarmed. Which pages are armed is the same as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@DenRakEiw DenRakEiw 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.

Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib watch:: — 7 passed, 0 failed (including armed_ranges_keep_their_pages_bytes_from_before_the_first_write)
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean run-based arming of writable shader page ranges in watch.rs. Looks good.

AI-assisted (Gemini 4 Neon).

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

Shader-writable ranges armed as runs of pages — page-granularity WPM arming; relates to #1865's dirty-bitmaps/WPM work.

r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 25, 2026
…rite are armed as runs of pages, head f869790)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 25, 2026
…sak#2115 in; Marvisak#2150 left out for now

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@r4tedx

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@catminou4 on the relation to #1865: the two do not overlap. #1865 changes only memory.rs (the per-list bitmaps in which shaders mark the title-memory pages they write, and the page table's writes). This PR changes only watch.rs: which pages a tracker keeps the old bytes of when the title first writes them, stored as runs instead of one set entry per page. Merged together locally they apply without conflict (a merge check only; nothing was built or run for this comment).

AI-assisted

@nathanbzrr

Copy link
Copy Markdown
Contributor

Verified head f869790fe14675920d9aea7ad18117af0fca75a5 merged with #2134 (and 3-way merged with #2136) onto main (940d32f69b) on Windows 11 x64 / AMD Radeon RX 7600:

  • victory-gpu lib tests: 144/144 passed (+#2134) and 146/146 passed (+#2134+#2136 via 3-way git merge; note plain git apply conflicts on watch.rs near Saved/impl Changed, while git merge / git cherry-pick resolves cleanly).
  • world_frame_preparing unit_costs (RX 7600): declaring the heap from its start to its end as written (marked) dropped from 73.0 us (end 9.1 us) on 940d32f + #2134 to 0.0 us (end 0.2-0.3 us). (Median full-frame preparing: 263.3 / 250.8 ms with #2134+#2147, and 125.5 / 112.1 ms RX 7600 / 104.2 ms WARP when combined with #2136.)

Finding (Saved::arm Linear Blowup on Non-Nested Ranges)

Saved::arm only prunes strict subsets (a <= first && last <= b or first <= a && b <= last) and never merges adjacent or partially overlapping runs.

  • Live repro (adjacent_and_overlapping_armed_runs_coalesce): pushing 200 adjacent [2k, 2k+2) runs leaves s.armed.len() == 200 instead of [(0, 400)], turning arm() into $O(N^2)$ and Saved::armed(page) into an $O(P)$ linear scan inside the VEH on_write handler under the global GUARDS lock (relevant for descriptor-table UAV sub-ranges in draw.rs:1733, which #2134's single-range unit_costs section did not exercise).

(Proposed / uncompiled fix sketch using sorted disjoint runs via partition_point:)

fn armed(&self, page: usize) -> bool {
    let i = self.armed.partition_point(|&(_, last)| last <= page);
    self.armed.get(i).is_some_and(|&(first, _)| first <= page) && !self.unarmed.contains(&page)
}

fn arm(&mut self, first: usize, last: usize) {
    if !self.unarmed.is_empty() {
        let drop_pages: Vec<usize> = self.unarmed.range(first..last).copied().collect();
        for page in drop_pages { self.unarmed.remove(&page); }
    }
    let lo = self.armed.partition_point(|&(_, b)| b < first);
    let hi = lo + self.armed[lo..].iter().take_while(|&&(a, _)| a <= last).count();
    if lo + 1 == hi && self.armed[lo].0 <= first && last <= self.armed[lo].1 { return; }
    let merged_first = self.armed.get(lo).filter(|_| lo < hi).map_or(first, |r| r.0.min(first));
    let merged_last = if lo < hi { self.armed[hi - 1].1.max(last) } else { last };
    self.armed.splice(lo..hi, [(merged_first, merged_last)]);
}

AI-assisted; synthetic tests only, no game files, dumps, or captures used.

Runs armed end to end or overlapping (descriptor tables' UAV sub-ranges)
each stayed a run of their own, so arming was quadratic in them and the
write handler's check of a page, under the guards' lock, scanned them
all. Arming now merges every run it overlaps or touches, and a page's
check is a binary search. Reported by nathanbzrr on Marvisak#2147.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@r4tedx

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@nathanbzrr thank you, the finding holds. Pushed as f2ca0868 on top of f869790 (fast-forward, no rebase).

  • Saved::armed is now a sorted list of separate runs; runs that overlap or touch merge on arm. armed(page) is a binary search, and both ends of the merge are found by binary search (your sketch's take_while replaced by a second partition_point on run starts).
  • Two edge cases beyond the sketch: an empty range (first >= last) now returns early, so arm(x, x) no longer inserts an empty run and unarmed.range(first..last) can't panic on first > last. Neither is reachable from Tracked::arm today, which ensures from < to.
  • Your test adjacent_and_overlapping_armed_runs_coalesce is in, extended with nested, disjoint (stays sorted), touching on both sides, one range spanning several runs, the empty range, armed(page) edges, and unarmed pages surviving a merge until armed again.

Verified on WARP (VICTORY_D3D12_WARP=1, --features diagnostics, one test thread): the new test fails on f869790 (200 runs left) and passes on f2ca0868. All victory-gpu lib tests: 148 passed, 0 failed, 2 ignored. cargo clippy -p victory-gpu --features diagnostics --all-targets: no warnings. I did not re-measure world_frame_preparing, since that bench comes from #2134 and isn't on this branch.

AI-assisted; synthetic tests only, no title data used.

@r4tedx

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

In-game counters from one PC: RTX 5070 (driver 616.92), Ryzen 7 7800X3D (max processor state 99 %), Windows 11, diagnostics builds, VICTORY_LOG=info VICTORY_D3D12_DRED=1. The title was run locally from my own copy. Only counts and timings are given here; no logs or title data are shared.

This is not a same-frame A/B:

The scenes are different moments of the opening. Here are the preparing steps of the copies per frame: line, for a late world frame of each run:

step (each with the batch it flushes) before after, run A after, run B
GPU writes 86.8 ms 0.0 ms 0.0 ms
title memory 30.0 ms 0.4 ms 0.4 ms
in refresh batches, total 294.7 ms 31.9 ms n/a
snapshots 187.9 MB in 8.8 ms 0.0 MB n/a
  • What this PR targets: the arming. The "GPU writes" step falls to 0.0 ms.
  • Part of the drop is the batch flush, which GPU: a host copy nothing wrote is not asked for at submission #2136 also shrinks, so the step time doesn't isolate this PR's arming cost. The synthetic unit cost in the PR description is still the measurement that does.
  • No device removal, wrong draw or hang that I could trace to this change, in 2 x 10 min.

AI-assisted

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. watch::Saved in crates/victory-gpu/src/watch.rs now keeps armed pages as a sorted list of separate runs, plus an unarmed set for pages a copy-back took out, instead of one BTreeSet entry per page. I checked that the armed pages behave as before. Saved::arm clears unarmed pages in the new range, as the old set insert re-armed them. Its two partition_point bounds pick up exactly the runs that overlap or touch, and it merges them. armed(page) is a binary search. unarm and disarm match the old remove/clear. Both new tests cover the cases @nathanbzrr found (adjacent, overlapping, nested and spanning runs, plus pages unarmed inside a merged run). The measured gain is the declaration cost on a large heap, and the in-game run reports no regression. CI: passing (check).


Generated by Claude Code

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.

5 participants