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

Open
Slay326 wants to merge 1 commit from upstream-pr/3199 into main
Slay326 commented 2026-11-26 12:06:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Slay326 on 2026-11-26T12:06:18Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5509717069 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3199). Originally posted by [Slay326](https://github.com/Slay326) on 2026-11-26T12:06:18Z. --- ## 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.
Dukestar69 commented 2026-11-26 12:50:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T12:50:54Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6074422501 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3199#pullrequestreview-6074422501). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T12:50:54Z. **Review:** APPROVED --- 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).
Jackster commented 2026-11-26 23:10:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T23:10:50Z.


Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • cargo test -p victory-gpu --lib: 234 passed / 0 failed / 1 ignored (unpatched 63bcca4 here: 231; +3).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

The three added tests are marked_pages_of_view_memory_go_back_beside_the_guard, writes_beside_reach_pages_committed_through_the_titles_view and copy_backs_beside_the_guard_keep_it_and_the_titles_writes; all pass with the opt-in path on. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205322147 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3199#issuecomment-7205322147). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T23:10:50Z. --- Windows 11, `63bcca4` + this PR, Radeon RX 7900 XTX: - `cargo test -p victory-gpu --lib`: 234 passed / 0 failed / 1 ignored (unpatched `63bcca4` here: 231; +3). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. The three added tests are `marked_pages_of_view_memory_go_back_beside_the_guard`, `writes_beside_reach_pages_committed_through_the_titles_view` and `copy_backs_beside_the_guard_keep_it_and_the_titles_writes`; all pass with the opt-in path on. Synthetic only. AI-assisted.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3199:upstream-pr/3199'
git switch 'upstream-pr/3199'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3199'
git switch 'upstream-pr/3199'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3199'
git switch 'upstream-pr/3199'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3199'
git switch 'main'
git merge --squash 'upstream-pr/3199'
git switch 'main'
git merge --ff-only 'upstream-pr/3199'
git switch 'main'
git merge 'upstream-pr/3199'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3197
No description provided.