Skip to content

GPU: refresh a buffer copy's ranges by page; answer clean guarded views from the journal - #393

Closed
kxiserino wants to merge 1 commit into
Marvisak:mainfrom
kxiserino:gpu/batch-interval-coalescing-and-clean-guard-fastpath
Closed

kxiserino wants to merge 1 commit into
Marvisak:mainfrom
kxiserino:gpu/batch-interval-coalescing-and-clean-guard-fastpath

Conversation

@kxiserino

Copy link
Copy Markdown

Summary

Two costs in the per-submission refresh of host buffer copies.

  1. Batch::add merged every range of a copy into one bounding range. A submission that reads a few small ranges at the low and high ends of one big shared buffer (a heap of over 100 MB, say) then had Shadow::update ask watch::writes (and so GetWriteWatch, or the guard walk) about the whole untouched middle, and copy whatever was stale there, though nothing declared it read.

    • Batch now keeps a Vec<(from, to)> per copy. At the flush, coalesce_ranges(&mut ranges, watch::PAGE) sorts them and merges ranges whose 64 KiB pages overlap or touch (next_from / page <= cur_to.div_ceil(page)). Ranges with a whole untouched page between them stay apart, and each one is updated by itself.
    • Empty ranges are dropped, and address + len saturates.
    • The new ranges are a subset of the old bounding range. They still cover every declared byte, so nothing read goes stale. The ranges shaders write (pending) are skipped by update as before.
  2. watch::writes(.., guarded = true) on a clean guarded view. Views can't be write-watched, so every asking first called GetWriteWatch, which fails for them, and then guard's VirtualQuery. If every block of the range is already guarded and still clean, nothing has written there since it was guarded: a title write faults into our handler, which removes the block from clean. writing() and title_memory_remapped() remove it too. So the journal already has the answer.

    • New fast path: when guarded && first < last and guards().clean.range(first..last).count() == last - first, skip both syscalls, bump asked and return written.range(first..last) as usual.
    • Lock order: JOURNAL is taken, then GUARDS, the same order guard() already uses. install(), guard(), open(), writing(), title_memory_remapped() and the vectored handler only ever take GUARDS, and none of them take JOURNAL. Nothing takes the two locks in the opposite order, so this can't deadlock.
    • A const assert pins BLOCK == PAGE, which the fast path relies on (blocks are pages).

Related: #119 makes guard's query cheaper. This change skips the query when every block is clean, so the two complement each other.

Testing

All the tests are synthetic: they use their own allocations and pagefile sections, and no title data.

  • host::batch: empty_ranges_are_dropped, ranges_in_one_page_merge, ranges_in_touching_pages_merge, ranges_pages_apart_stay_apart (low and high ends of a 256 MiB range), ranges_at_the_top_of_the_address_space_do_not_overflow.
  • watch::tests::clean_guarded_views_are_answered_from_the_journal covers repeated writes(.., true) on a clean mapped section. A test-only thread-local counter shows the kernel isn't asked again, and the answer stays the same. A later title write (through the handler), writing() and title_memory_remapped() are each still reported, then the range settles back onto the fast path.
  • watch::tests::partly_clean_ranges_ask_the_kernel checks that a partly guarded range still takes the slow path, and that unguarded askers never take the journal's word.
  • The new watch tests drop their pages' journal entries before unmapping. Otherwise written_pages_are_reported_to_every_asker could see stale entries if its allocation lands at the same address.

Results:

  • cargo test -p victory-gpu: 50 passed. With -- --test-threads=1: 50 passed. With --features diagnostics -- --test-threads=1: 52 passed.
  • cargo clippy -p victory-gpu --features diagnostics -- -D warnings and the same without features: clean.
  • cargo clippy -p victory-gpu --all-targets [--features diagnostics] -- -D warnings: the only error is the existing missing SAFETY comment at host.rs:267 on main, which Chore: clear two workspace Clippy errors #355 fixes. With that line patched locally, both are clean.
  • cargo xtask policy: asset policy: 151 files ok.

In-game frame time is not measured here.

🤖 Generated with Claude Code

… views from the journal

A submission's refreshes of one host copy were merged into the single
range covering them all. Two small ranges at the low and high ends of a
big shared buffer then had the whole untouched middle asked for written
pages (and its stale pages copied). The batch now keeps each copy's ranges
and merges only those whose 64 KiB pages overlap or touch, at the flush.

`watch::writes` for guarded memory asks the kernel first, which fails for
views, and then `guard` queries the view. When every block of the range
is already guarded and clean, nothing was written there: the journal
answers without either system call. `JOURNAL` is still taken before
`GUARDS`; nothing takes them the other way round.

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

Copy link
Copy Markdown
Contributor

Thanks @kxiserino for this journal-backed range handling. I tested head 6e7186c09b15f25822b05707f0b7ded00f57a747 with:

cargo test --locked -p victory-gpu --features diagnostics --lib watch::tests:: -- --test-threads=1

Result: 4 passed, 0 failed. The watch tests cover clean guarded reads answered from the journal, partially clean ranges falling back to the kernel, guarded kernel reads, and reporting written pages to all askers. These are synthetic watch/journal unit tests, not a title workload.

aycow added a commit to aycow/VIctory that referenced this pull request Nov 24, 2026
Plan only requested pages outside pending GPU writes. Use a verified
reservation extent for dense groups and share a fresh observation within
each group. Query journal chunks against the oldest valid shadow bytes.

Adapt the clean guarded-memory path from PR Marvisak#393 and the guarded view
lookup cache from PR Marvisak#490. Remap and allocation notifications invalidate
their proof. Preserve exact byte coverage and journal observations across
mixed allocations, including the wholly reserved case examined in Marvisak#515.

Co-authored-by: KaiserBloo <kaiserbloo@protonmail.com>
Co-authored-by: wrothgaril <211408787+wrothgaril@users.noreply.github.com>
@LooperSalty

Copy link
Copy Markdown

Measured in the world on an RTX 4070 SUPER + i7-13700KF (diag build, same ~20-PR stack before and after, 2 minutes in the world, VICTORY_SHADER_LOOP_LIMIT=32), per frame from copystat:

What remains on that path here is ~81,000 guard page faults per frame and ~136 MB of unchanged textures hashed per frame, so the frame time only moved from about 550 to 500 ms; #185's per-reservation write-watch looks like the next step.

🤖 Generated with Claude Code

giritsu pushed a commit to giritsu/VIctory that referenced this pull request Nov 24, 2026
…s from journal (PR Marvisak#393)

Merge Batch refreshes per 64 KiB page rather than spanning untouched
middles of large buffers, and answer clean guarded ranges from the
watch journal without kernel or page-guard syscalls.
giritsu pushed a commit to giritsu/VIctory that referenced this pull request Nov 24, 2026
…s (PR Marvisak#693)

Allocate Shadow host copies in D3D12_HEAP_TYPE_DEFAULT VRAM and stage
dirty 64 KiB page uploads and pre/post GPU-write readbacks through
pooled 4 MiB StagingArenas, integrated with bindless prepare (Marvisak#413),
page-coalesced refreshes (Marvisak#393), and lock-free retirement (Marvisak#609).
Update raw_view_tests::unknown_stride_reads to stage via DEFAULT buffers.
PastiiJay added a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 24, 2026
@AgneyaK

AgneyaK commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Test report on a 6 GB / 16 GB laptop: this PR takes write-watch time from 475 ms to 13 ms per world frame, and frames get 16% shorter.

Setup: RTX 3050 6 GB Laptop (NVIDIA, driver 32.0.16.2124), i7-13700H, 16 GB RAM, Windows 11 26300, diag build. Title 1.0.2.1, Series X profile, the story load. Base tree: main ffe310e + the PRs listed in #1232 + #431. Each PR was merged onto that base, and each row is one run of 300 s from launch. "World frame" is the average of the fps, ms/frame (…) lines under 10 fps with 20+ draws. Write-watch numbers are the last 8 copystat lines.

tree world frame preparing copies write-watch askings / frame their time / frame
base (+ #431) 3,966 ms 1,988 447 867 475 ms
+ #393 3,347 ms 1,579 100 585 13 ms
+ #440 + #393 3,221 ms 1,553 67 572 14 ms
+ #515 5,256 ms 3,228 1,223 884 1,605 ms
+ #440 + #515 + #393 4,090 ms 2,243 201 550 29 ms
  • The askings' time is almost gone, and copies drop from 447 to 100 ms per frame.
  • Merges cleanly onto the base above.
  • Run-to-run spread on this machine is about ±10%. The write-watch and copy columns moved far more than that.

Not tested: more than one run, a release build, AMD. I didn't check correctness beyond the run not failing (no device removal or title stop in the 300 s window).

AI use: Claude Code (Claude Opus 5.5) built the trees, ran the title on my machine and drafted this report; results are from the runs described.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
Jarrrk pushed a commit to Jarrrk/VIctory that referenced this pull request Nov 24, 2026
Refresh ranges are kept separate and merged only where their 64 KiB pages
touch, so the untouched middle of a big buffer is neither checked nor
copied; clean guarded view ranges are answered from the journal without a
GetWriteWatch kernel call; and copy-back caches pages already compared
against the snapshot so overlapping ranges skip them.

Ported from Marvisak/VIctory Marvisak#393 and Marvisak#124.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants