GPU: less copying back and forth: hot title buffers kept in video memory (opt-in, stacked on #2112) #2732

Open
sirrus98 wants to merge 24 commits from upstream-pr/2734 into main
sirrus98 commented 2026-11-25 19:22:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sirrus98 on 2026-11-25T19:22:19Z.


Stacked on #2112 (title memory in place, VICTORY_IN_PLACE=1). Everything up to 14d9119 is #2112; the new commits are the last two here. Thanks to #2112 for the groundwork: this builds directly on its buffers in place.

Change

With #2112, a title buffer in place is the title's own pages, and shaders read and write it across the bus. With VICTORY_IN_PLACE_VRAM=1 as well, a buffer in place from 1 MiB up gets a copy in video memory (new crates/victory-gpu/src/resident.rs). Views and the memory page table point at that copy, and the card alone keeps it in step with title memory, inside the submission:

  • Before the submission's lists: the 64 KiB pages the title wrote since they last went up (found by write watching; the runtime's pools are MEM_WRITE_WATCH) are copied by the GPU from the buffer in place into the video-memory copy, and from there into a twin.
  • After the lists: a compute pass of ours compares the copy with its twin over the ranges declared written. It stores each changed dword into the buffer in place and into the twin. A partial dword goes through InterlockedAnd/InterlockedOr, so bytes the CPU wrote meanwhile in the same dword stay. A row is 64 dwords side by side, so the stores cross the bus as runs of whole bytes.
  • Writes through the memory page table: they are only known from its bitmaps after the submission ran. One more list of ours merges them, and it is waited for before the title is told.

Nothing is copied, snapshotted, compared or hashed on the CPU for these buffers. Budget: 60% of the card's (VICTORY_IN_PLACE_VRAM_MB), and none past 80% of the budget in use. Minimum size: VICTORY_IN_PLACE_VRAM_MIN_MB. A log line every 5 s counts the copies and what went up and back. Unset, nothing changes.

The first commit fixes a gap in #2112 that this needs. For a buffer in place, Shadow::gpu_writes returned before the write was noted with the list. So landed() never marked the pages that shaders wrote through root and table views, and other copies over those pages never saw them; only writes through the memory page table were marked. The write is now declared as for any copy. Nothing is snapshotted or copied back for it, since the in-place paths still return early.

This is the "hot memory in video memory" stage of fxceless/victory-gta6#772, ported to this tree's synchronous submissions.

Related drafts, none of which writes back from the GPU: #2130 (written buffers in a GPU upload heap plus a CPU mirror), #693 (shadows in DEFAULT video memory), #2494 (SEC_COMMIT in place).

Not measured / limits

  • No in-game numbers on this tree yet. On my PC (RTX 4090, driver 621.01, Windows) plain main loses the device before the world with every switch off, so #2112 and this PR do too. It isn't caused by either. The debug layer and DRED name three NVIDIA-only causes in main:
    1. B5G5R5A1/B4G4R4A4 render targets and typed UAVs, which NVIDIA refuses.
    2. Shaders clamp view indices below 2^21, but NVIDIA caps view heaps at 1,000,000.
    3. Ray-tracing compute passes whose acceleration-structure views have no host copy, which run until the driver resets the card.
      I have fixes for the first two locally, and with them main reaches the world. I'll send those separately.
  • Numbers measured in the fork are in fxceless/victory-gta6#772. That's a different tree, so they don't carry over here.
  • Write watching is reset by this code (GetWriteWatch with reset); nothing else in the tree resets it.
  • Not tested on AMD or under Wine/vkd3d-proton.

Tested

  • New: a_buffer_in_video_memory_takes_the_titles_writes_and_gives_back_the_cards. It runs on the host GPU (verified on the RTX 4090): the title's writes go up, the card's come back, and a CPU byte beside a byte the card wrote in the same dword stays. Also new: pages_go_up_in_runs, ranges_join_where_they_touch, writes_declared_in_place_reach_the_copy_back.
  • a_list_keeps_the_in_place_buffers_it_uses now counts the declared write's reference.
  • Two tests fail (descriptor::host dynamic slots, memory lists share a table). They fail the same on #2112 without these commits.
  • cargo clippy -D warnings, with and without --all-features: clean. cargo run -p xtask -- policy: ok.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6611967202 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2734). Originally posted by [sirrus98](https://github.com/sirrus98) on 2026-11-25T19:22:19Z. --- Stacked on #2112 (title memory in place, `VICTORY_IN_PLACE=1`). Everything up to 14d9119 is #2112; the new commits are the last two here. Thanks to #2112 for the groundwork: this builds directly on its buffers in place. ## Change With #2112, a title buffer in place is the title's own pages, and shaders read and write it across the bus. With `VICTORY_IN_PLACE_VRAM=1` as well, a buffer in place from 1 MiB up gets a copy in video memory (new `crates/victory-gpu/src/resident.rs`). Views and the memory page table point at that copy, and the card alone keeps it in step with title memory, inside the submission: - **Before the submission's lists:** the 64 KiB pages the title wrote since they last went up (found by write watching; the runtime's pools are `MEM_WRITE_WATCH`) are copied by the GPU from the buffer in place into the video-memory copy, and from there into a twin. - **After the lists:** a compute pass of ours compares the copy with its twin over the ranges declared written. It stores each changed dword into the buffer in place and into the twin. A partial dword goes through `InterlockedAnd`/`InterlockedOr`, so bytes the CPU wrote meanwhile in the same dword stay. A row is 64 dwords side by side, so the stores cross the bus as runs of whole bytes. - **Writes through the memory page table:** they are only known from its bitmaps after the submission ran. One more list of ours merges them, and it is waited for before the title is told. Nothing is copied, snapshotted, compared or hashed on the CPU for these buffers. Budget: 60% of the card's (`VICTORY_IN_PLACE_VRAM_MB`), and none past 80% of the budget in use. Minimum size: `VICTORY_IN_PLACE_VRAM_MIN_MB`. A log line every 5 s counts the copies and what went up and back. Unset, nothing changes. The first commit fixes a gap in #2112 that this needs. For a buffer in place, `Shadow::gpu_writes` returned before the write was noted with the list. So `landed()` never marked the pages that shaders wrote through root and table views, and other copies over those pages never saw them; only writes through the memory page table were marked. The write is now declared as for any copy. Nothing is snapshotted or copied back for it, since the in-place paths still return early. This is the "hot memory in video memory" stage of fxceless/victory-gta6#772, ported to this tree's synchronous submissions. Related drafts, none of which writes back from the GPU: #2130 (written buffers in a GPU upload heap plus a CPU mirror), #693 (shadows in DEFAULT video memory), #2494 (`SEC_COMMIT` in place). ## Not measured / limits - **No in-game numbers on this tree yet.** On my PC (RTX 4090, driver 621.01, Windows) plain `main` loses the device before the world with every switch off, so #2112 and this PR do too. It isn't caused by either. The debug layer and DRED name three NVIDIA-only causes in `main`: 1. `B5G5R5A1`/`B4G4R4A4` render targets and typed UAVs, which NVIDIA refuses. 2. Shaders clamp view indices below 2^21, but NVIDIA caps view heaps at 1,000,000. 3. Ray-tracing compute passes whose acceleration-structure views have no host copy, which run until the driver resets the card. I have fixes for the first two locally, and with them `main` reaches the world. I'll send those separately. - Numbers measured in the fork are in fxceless/victory-gta6#772. That's a different tree, so they don't carry over here. - Write watching is reset by this code (`GetWriteWatch` with reset); nothing else in the tree resets it. - Not tested on AMD or under Wine/vkd3d-proton. ## Tested - New: `a_buffer_in_video_memory_takes_the_titles_writes_and_gives_back_the_cards`. It runs on the host GPU (verified on the RTX 4090): the title's writes go up, the card's come back, and a CPU byte beside a byte the card wrote in the same dword stays. Also new: `pages_go_up_in_runs`, `ranges_join_where_they_touch`, `writes_declared_in_place_reach_the_copy_back`. - `a_list_keeps_the_in_place_buffers_it_uses` now counts the declared write's reference. - Two tests fail (`descriptor::host` dynamic slots, `memory` lists share a table). They fail the same on #2112 without these commits. - `cargo clippy -D warnings`, with and without `--all-features`: clean. `cargo run -p xtask -- policy`: ok. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
kotekompotek commented 2026-11-25 21:03:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T20:36:42Z.


Read the resident.rs design + the watch/shadow integration. The R/B twin-diff approach is coherent: prepare() uploads title-written pages before the submission, MERGE runs after the lists but before the fence signal so host-visible memory is consistent once waited, and the InterlockedAnd/Or partial-dword path correctly preserves CPU bytes in a shared dword (the hard case for any card-side merge). written_beside handles the aliasing leak I was looking for. Opt-in + stacked on #2112 makes the risk acceptable. What reading can't confirm: whether the submit-ordering (our lists before/after in separate ExecuteCommandLists) is airtight against a title list that both reads R and signals early - worth a soak test once #2112 lands.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173925898 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2734#issuecomment-7173925898). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T20:36:42Z. --- Read the resident.rs design + the watch/shadow integration. The R/B twin-diff approach is coherent: prepare() uploads title-written pages before the submission, MERGE runs after the lists but before the fence signal so host-visible memory is consistent once waited, and the InterlockedAnd/Or partial-dword path correctly preserves CPU bytes in a shared dword (the hard case for any card-side merge). written_beside handles the aliasing leak I was looking for. Opt-in + stacked on #2112 makes the risk acceptable. What reading can't confirm: whether the submit-ordering (our lists before/after in separate ExecuteCommandLists) is airtight against a title list that both reads R and signals early - worth a soak test once #2112 lands.
Marvisak commented 2026-11-26 01:03:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T00:15:20Z.


Automated triage for the maintainers.

This pull request conflicts with main (conflicting files: crates/victory-gpu/src/host.rs, crates/victory-gpu/src/shadow.rs). It will be reviewed once it is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178563485 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2734#issuecomment-7178563485). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T00:15:20Z. --- <!-- victory-triage v1 head=4d28d6de7da4ef25f2218c83136bf2d534bf5f80 verdict=needs-rebase at=2026-11-26T00:15:20Z --> Automated triage for the maintainers. This pull request conflicts with `main` (conflicting files: `crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/shadow.rs`). It will be reviewed once it is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
potatoe0100 commented 2026-11-26 15:02:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by potatoe0100 on 2026-11-26T14:48:22Z.


Hardware run on a second NVIDIA card: RTX 4060 8 GB (Ada, PCIe 4.0 x8, Resizable BAR on), driver 621.14, Windows 11 Pro 26300. I tested at the PR head 4d28d6d as it is (its base is 67e8ae1), because it conflicts with current main. cargo test -p victory-gpu --lib --locked -- --test-threads=1. No game files.

  • a_buffer_in_video_memory_takes_the_titles_writes_and_gives_back_the_cards passes on this card, as do pages_go_up_in_runs, ranges_join_where_they_touch and writes_declared_in_place_reach_the_copy_back.
  • 210 passed, 2 failed. The 2 are the known capped-heap tests, which you already noted fail the same on #2112.

For context on why this stage matters on narrow-link cards: on the same 4060, #3102's synthetic benchmark puts title memory in place at 113-122 ms per submission, against 53-106 ms with copies in video memory (numbers). I'm happy to re-run this once it's rebased onto main.

AI-assisted (Claude)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195355361 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2734#issuecomment-7195355361). Originally posted by [potatoe0100](https://github.com/potatoe0100) on 2026-11-26T14:48:22Z. --- Hardware run on a second NVIDIA card: **RTX 4060 8 GB** (Ada, PCIe 4.0 x8, Resizable BAR on), driver 621.14, Windows 11 Pro 26300. I tested at the PR head `4d28d6d` as it is (its base is `67e8ae1`), because it conflicts with current `main`. `cargo test -p victory-gpu --lib --locked -- --test-threads=1`. No game files. - `a_buffer_in_video_memory_takes_the_titles_writes_and_gives_back_the_cards` **passes** on this card, as do `pages_go_up_in_runs`, `ranges_join_where_they_touch` and `writes_declared_in_place_reach_the_copy_back`. - 210 passed, 2 failed. The 2 are the known capped-heap tests, which you already noted fail the same on #2112. For context on why this stage matters on narrow-link cards: on the same 4060, #3102's synthetic benchmark puts title memory in place at 113-122 ms per submission, against 53-106 ms with copies in video memory ([numbers](https://github.com/Marvisak/VIctory/pull/3102#issuecomment-7194296926)). I'm happy to re-run this once it's rebased onto `main`. AI-assisted (Claude)
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/2734:upstream-pr/2734'
git switch 'upstream-pr/2734'

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/2734'
git switch 'upstream-pr/2734'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2734'
git switch 'upstream-pr/2734'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2734'
git switch 'main'
git merge --squash 'upstream-pr/2734'
git switch 'main'
git merge --ff-only 'upstream-pr/2734'
git switch 'main'
git merge 'upstream-pr/2734'
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!2732
No description provided.