GPU, HLE: marked copy-backs into view memory go through a view of our own (VICTORY_BACK_OWN_VIEW, opt-in) #3199

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

@⁠Slay326 on GitHub · 2026-11-26 12:06 UTC · original
Slay326:pr/own-view → main · +475 −10 · 6 files · 1 commits · open

Relation to #3069

This overlaps with #3069 by moe-miau (VICTORY_COPY_BACK_BESIDE), which uses the same mechanism: a marked page goes back through a second view of its section, so the title's guarded view is never opened. Either this PR or #3069 should be merged, not both. If #3069 is merged, this PR becomes obsolete except for one detail I would offer as a small follow-up: after a write beside the guard, the page is not taken again, so a title write that lands during the copy-back is kept for the next refresh instead of being lost (main and #3069 take the page afterwards).

We wrote this version independently and measured it in game, so I am opening it mainly for the numbers. The differences I can see:

  • Here the GPU side keeps the section/view table (watch.rs, with its own lock, taken before the guard lock). xmem only reports sections and views through three hooks. #3069 writes under xmem's mapping lock through a registered page writer.
  • Only pages the title has not written since the copy took them go beside. Those pages are still guarded, so they need no take afterwards. A page the title writes during the copy-back faults as usual. That fault marks the writer's tracker, so the page is left for the next refresh instead of being taken. The same happens to a page whose write did not land. Pages the title wrote before the copy-back are open already and go the old way.
  • The numbers come from an RTX 3070 (and a 4070) on a different stack from #3069's 4070 SUPER.

Problem

After a submission, each marked page goes back into title memory in Shadow::copy_back_marked. watch::writing opens the guard (VirtualProtect under the guard lock), host::write_mapped writes the page, and Tracked::taken guards it again. That takes the guard lock three times per page, strictly one page at a time, and the title threads' faults wait for the same lock. In the world, the "back" part of the frame was about 70 ms.

Change

With VICTORY_BACK_OWN_VIEW=1:

  • victory-hle xmem.rs: reports each section it makes for physical pages (title_memory_section), each view it maps of one (title_memory_view), and the section's drop (title_memory_section_gone).
  • victory-gpu watch.rs: maps one read-write view of each whole section and keeps the title's views of it by address. Tracked::write_beside writes through that view when the range lies in one known title view:
    • While the write runs, the page is busy, as under writing, so no tracker takes it.
    • The other trackers of the page, and those of its other mappings, are marked changed twice: before the write (armed ones save their bytes, as writing does) and after it (as its drop does).
    • No guard is opened and no system call runs under the guard lock.
    • A remapped title view is forgotten, and dropping a section unmaps our view of it.
  • shadow.rs copy_back_marked: uses write_beside for pages with no saved bytes and takes the old path otherwise. Private memory (XMemVirtualAlloc pools) has no second view and goes the old way.
  • host.rs: write_mapped reports whether every byte landed.
  • copystat.rs (diagnostics only): an "own view" line with the pages written beside, the ones the title wrote meanwhile, and the pages in memory with no view of ours.

The switch is off by default. With the variable unset, no section is mapped and nothing is recorded.

Tested

Windows 11 Pro 26200, RTX 3070 8 GB (driver 610.88), title 1.0.2.1, release + diagnostics, BVH on, VICTORY_BACK_THREADS=4. 480 s runs in A-B-A order, same shader cache.

main alone cannot be measured on this card: it runs at 0.1 fps because the view heap overflows without #1779. The numbers therefore come from our test stack, an older main plus open NVIDIA PRs (among them #1762, #1779 and #2242/#2280/#2409). On that stack, copy-back compares 32-byte lines instead of whole pages, so the code around it differs from main's copy_back_marked; the write-beside part is the same. The variable was the only change between A and B:

run ms/frame (median) = prepare + GPU + back p95 fps
A, mean of 4 runs (spread 10.7 ms) 405 1,280-1,814 2.4-2.5
B, VICTORY_BACK_OWN_VIEW=1 382.3 = 133.2 + 199.7 + 47.3 1,190 2.6
  • B is 23 ms a frame faster than A, and all of the gain is in copy-back (70 -> 47 ms).
  • Each frame, 975 marked pages went beside the guards. 666 were in memory with no view of ours (the private pools), which take the old path.
  • Combined with the clamp windows of #3197 (my other PR, on a separate topic), the gains add up: 354.2 ms, 51 ms below A.
  • No crash or device removal. The world frame looks the same.
  • RTX 4070 (second machine, same stack): copy-back drops by about 32 ms there, but the frame as a whole did not get faster beyond the run-to-run spread (A 393-410 ms, B 414 ms). I can't explain that yet.

Unit tests (synthetic, no game data):

  • watch::copy_backs_beside_the_guard_keep_it_and_the_titles_writes: a pagefile section mapped twice as title views. It checks that:
    • the bytes land and the guard stays PAGE_READONLY
    • the writer stays clean, while another tracker of the page and one of the other view are marked
    • a title write while the copy-back is under way marks the writer too
    • private memory, a remapped view and a dropped section are not written beside
  • watch::writes_beside_reach_pages_committed_through_the_titles_view: a SEC_RESERVE section like xmem's. A page committed only through the title's view is written. A page committed nowhere is not written and is left changed.
  • shadow::marked_pages_of_view_memory_go_back_beside_the_guard: goes through copy_back_marked. A clean page goes beside and stays guarded. A page the title wrote first goes the old way with both bytes kept.

Checks on this branch (origin/main 63bcca4 + this commit), RTX 3070:

  • cargo test -p victory-gpu --release -- --test-threads=1: 247 passed / 2 failed with diagnostics, 232 passed / 2 failed without. The two failures are the known heap-size pair on NVIDIA (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views), which fail on main too.
  • cargo test -p victory-hle --release: 152 passed.
  • cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean with and without diagnostics.
  • cargo xtask policy: ok.

Not addressed

  • Private memory (the pools) still goes the old way. Making the pools sections too would be a larger change.
  • Pages that hold no changed byte are still compared and read.
  • The game numbers come from the same mechanism on our stack, not from this exact branch.

AI-assisted.

**@⁠Slay326** on GitHub · 2026-11-26 12:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3199) `Slay326:pr/own-view` → `main` · +475 −10 · 6 files · 1 commits · open ## Relation to #3069 This overlaps with #3069 by moe-miau (`VICTORY_COPY_BACK_BESIDE`), which uses the same mechanism: a marked page goes back through a second view of its section, so the title's guarded view is never opened. Either this PR or #3069 should be merged, not both. If #3069 is merged, this PR becomes obsolete except for one detail I would offer as a small follow-up: after a write beside the guard, the page is not taken again, so a title write that lands during the copy-back is kept for the next refresh instead of being lost (main and #3069 take the page afterwards). We wrote this version independently and measured it in game, so I am opening it mainly for the numbers. The differences I can see: - Here the GPU side keeps the section/view table (`watch.rs`, with its own lock, taken before the guard lock). xmem only reports sections and views through three hooks. #3069 writes under xmem's mapping lock through a registered page writer. - Only pages the title has not written since the copy took them go beside. Those pages are still guarded, so they need no take afterwards. A page the title writes during the copy-back faults as usual. That fault marks the writer's tracker, so the page is left for the next refresh instead of being taken. The same happens to a page whose write did not land. Pages the title wrote before the copy-back are open already and go the old way. - The numbers come from an RTX 3070 (and a 4070) on a different stack from #3069's 4070 SUPER. ## Problem After a submission, each marked page goes back into title memory in `Shadow::copy_back_marked`. `watch::writing` opens the guard (`VirtualProtect` under the guard lock), `host::write_mapped` writes the page, and `Tracked::taken` guards it again. That takes the guard lock three times per page, strictly one page at a time, and the title threads' faults wait for the same lock. In the world, the "back" part of the frame was about 70 ms. ## Change With `VICTORY_BACK_OWN_VIEW=1`: - `victory-hle` `xmem.rs`: reports each section it makes for physical pages (`title_memory_section`), each view it maps of one (`title_memory_view`), and the section's drop (`title_memory_section_gone`). - `victory-gpu` `watch.rs`: maps one read-write view of each whole section and keeps the title's views of it by address. `Tracked::write_beside` writes through that view when the range lies in one known title view: - While the write runs, the page is busy, as under `writing`, so no tracker takes it. - The other trackers of the page, and those of its other mappings, are marked changed twice: before the write (armed ones save their bytes, as `writing` does) and after it (as its drop does). - No guard is opened and no system call runs under the guard lock. - A remapped title view is forgotten, and dropping a section unmaps our view of it. - `shadow.rs` `copy_back_marked`: uses `write_beside` for pages with no saved bytes and takes the old path otherwise. Private memory (`XMemVirtualAlloc` pools) has no second view and goes the old way. - `host.rs`: `write_mapped` reports whether every byte landed. - `copystat.rs` (diagnostics only): an "own view" line with the pages written beside, the ones the title wrote meanwhile, and the pages in memory with no view of ours. The switch is off by default. With the variable unset, no section is mapped and nothing is recorded. ## Tested Windows 11 Pro 26200, RTX 3070 8 GB (driver 610.88), title 1.0.2.1, release + `diagnostics`, BVH on, `VICTORY_BACK_THREADS=4`. 480 s runs in A-B-A order, same shader cache. `main` alone cannot be measured on this card: it runs at 0.1 fps because the view heap overflows without #1779. The numbers therefore come from our test stack, an older main plus open NVIDIA PRs (among them #1762, #1779 and #2242/#2280/#2409). On that stack, copy-back compares 32-byte lines instead of whole pages, so the code around it differs from `main`'s `copy_back_marked`; the write-beside part is the same. The variable was the only change between A and B: | run | ms/frame (median) = prepare + GPU + back | p95 | fps | |---|---|---|---| | A, mean of 4 runs (spread 10.7 ms) | 405 | 1,280-1,814 | 2.4-2.5 | | B, `VICTORY_BACK_OWN_VIEW=1` | 382.3 = 133.2 + 199.7 + 47.3 | 1,190 | 2.6 | - B is 23 ms a frame faster than A, and all of the gain is in copy-back (70 -> 47 ms). - Each frame, 975 marked pages went beside the guards. 666 were in memory with no view of ours (the private pools), which take the old path. - Combined with the clamp windows of #3197 (my other PR, on a separate topic), the gains add up: 354.2 ms, 51 ms below A. - No crash or device removal. The world frame looks the same. - RTX 4070 (second machine, same stack): copy-back drops by about 32 ms there, but the frame as a whole did not get faster beyond the run-to-run spread (A 393-410 ms, B 414 ms). I can't explain that yet. Unit tests (synthetic, no game data): - `watch::copy_backs_beside_the_guard_keep_it_and_the_titles_writes`: a pagefile section mapped twice as title views. It checks that: - the bytes land and the guard stays `PAGE_READONLY` - the writer stays clean, while another tracker of the page and one of the other view are marked - a title write while the copy-back is under way marks the writer too - private memory, a remapped view and a dropped section are not written beside - `watch::writes_beside_reach_pages_committed_through_the_titles_view`: a `SEC_RESERVE` section like xmem's. A page committed only through the title's view is written. A page committed nowhere is not written and is left changed. - `shadow::marked_pages_of_view_memory_go_back_beside_the_guard`: goes through `copy_back_marked`. A clean page goes beside and stays guarded. A page the title wrote first goes the old way with both bytes kept. Checks on this branch (`origin/main` 63bcca4 + this commit), RTX 3070: - `cargo test -p victory-gpu --release -- --test-threads=1`: 247 passed / 2 failed with `diagnostics`, 232 passed / 2 failed without. The two failures are the known heap-size pair on NVIDIA (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`), which fail on `main` too. - `cargo test -p victory-hle --release`: 152 passed. - `cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings`: clean with and without `diagnostics`. - `cargo xtask policy`: ok. ## Not addressed - Private memory (the pools) still goes the old way. Making the pools sections too would be a larger change. - Pages that hold no changed byte are still compared and read. - The game numbers come from the same mechanism on our stack, not from this exact branch. AI-assisted.
A marked page's copy-back opened the title's guard (VirtualProtect under
the guard lock) and guarded it again after, taking the guard lock three
times a page, strictly one page at a time, while the title threads'
faults wait for the same lock.

View memory is a section (xmem's physical pages): the runtime now tells
the GPU side about its sections and views, and with VICTORY_BACK_OWN_VIEW=1
the GPU side maps a view of its own of each whole section. A marked page
the title has not written since the copy took it goes back through that
view: the title's view keeps its guard, nothing is opened or guarded
again, and the guard lock is taken twice a page with no system call under
it, to mark the page busy and its other trackers (and those of its other
mappings) changed. A title write meanwhile still faults and marks the
writer's tracker too, as does a write that did not land: such a page is
left for the next refresh. Pages the title wrote before, and private
memory, go as before. Off unless asked for.

Diagnostics: an "own view" line with the pages written beside the guards,
those the title wrote meanwhile, and those in memory with no view of ours.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 12:07:56 +00:00
Author
Owner

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

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

  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1: 11/11 passed (including marked_pages_of_view_memory_go_back_beside_the_guard).
  • cargo test -p victory-gpu --lib watch:: -- --test-threads=1: passed.
  • cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1: 14/14 passed.
  • cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly routes marked copy-backs into view memory through an unguarded companion view.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 12:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3199#pullrequestreview-6074422501) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib shadow:: -- --test-threads=1`: 11/11 passed (including `marked_pages_of_view_memory_go_back_beside_the_guard`). - `cargo test -p victory-gpu --lib watch:: -- --test-threads=1`: passed. - `cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1`: 14/14 passed. - `cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly routes marked copy-backs into view memory through an unguarded companion view. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/3199/head:github-bot/gh-3199'
git switch 'github-bot/gh-3199'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3199'
git switch 'github-bot/gh-3199'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3199'
git switch 'github-bot/gh-3199'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3199'
git switch 'main'
git merge --squash 'github-bot/gh-3199'
git switch 'main'
git merge --ff-only 'github-bot/gh-3199'
git switch 'main'
git merge 'github-bot/gh-3199'
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!3199
No description provided.