GPU: host copies of title buffers in video memory, under a DXGI budget (opt-in) #2993

Open
github-bot wants to merge 4 commits from github-bot/gh-2993 into main AGit
Owner

@⁠r4tedx on GitHub · 2026-11-26 02:38 UTC · original
r4tedx:mem/vram-copies-main → main · +1977 −49 · 7 files · 4 commits · open

What it changes

Off by default. Without the switch, no code path changes.

  • Where shaders read. With VICTORY_VRAM_COPIES=1, the host copy of a title buffer made without unordered access gets a second buffer in video memory: a GPU upload heap, which the CPU maps through the PCI BAR. It is used only when D3D12_OPTIONS16.GPUUploadHeapSupported is set (Resizable BAR). Shaders, views, root descriptors and page tables use that buffer. With =2, copies of buffers that shaders may write get one too. Loose windows and the copies of single views stay in system memory.

  • The CPU's side stays in system memory, always. The system memory buffer that main has today is kept for every copy. The CPU reads only that buffer (snapshots, page compares, copy-backs). Every CPU write into the copy goes there first and then into video memory:

    • refreshes, including those under a range a submitted list declared;
    • the zeros of unmapped pages;
    • copy-back merges.
  • The CPU never reads video memory. The BAR mapping is a vram::WriteOnly, a type with only a write method and private fields, so no code outside vram.rs can read through it. A CPU read of a BAR runs at about 50 MB/s (#2120's table).

  • Shader writes come back through main's page marks. After a submission's GPU work, and before any copy-back reads a copy, one GPU copy brings into the system memory side:

    • the ranges its lists declared unmarked (snapshot ranges), withheld ones too. Main compares these whole, so they come back whole;
    • the pages marked in either written-pages bitmap, held-back ones too.

    Ranges declared marked (views that mark their writes, root UAVs of marked copies) do not come back whole. The CPU reads nothing in them but their marked pages. Pages written at computed addresses (memory.rs) are brought back the same way before their copy-back. The copy-backs then run main's code unchanged: compare, write the changed bytes into title memory, take the page again. The system memory side then matches the GPU side wherever shaders wrote, withheld writes included.

  • The order of a copy-back. The order is: bring back, merge and write, take the page again, read the page from title memory once more, then write video memory. A title write that lands after the page went back and before its guard is back is in that last read. A write after the guard faults and marks the page changed. If video memory were written before the read, it would miss such a write for good, because the page counts as current. For copies in system memory, nothing changes.

  • Admission through the DXGI budget. QueryVideoMemoryInfo (local segment) is asked at most every 250 ms. Bytes taken since the last reading count as used. A copy goes to video memory only when:

    • copies there stay within the cap: a quarter of the budget (VICTORY_VRAM_COPIES_MB sets it by hand), and never more than what the rest of the process leaves under 85 % of the budget. The cap shrinks as textures, render targets and depth buffers fill the card;
    • use stays under 85 % of the budget;
    • use has not passed 85 % since it was last under 75 %. Near a full budget, room a texture gives back is soon taken again;
    • there is a reading at all.

    Admission and charge happen under one lock. A copy's bytes go back when D3D12 destroys its buffer (ID3DDestructionNotifier), because lists and heap slots can hold the buffer after the copy is gone.

  • Copies yield to textures past the budget. Each buffer in video memory gets D3D12_RESIDENCY_PRIORITY_LOW. Copies are made early, before most textures; in the title run they took 1.2 GB at 54 % of the budget, and textures then took the card to 95 %. Past the budget, the system moves our copies to system memory first. Shaders then read them across the bus, as they read a system copy. The title's textures (normal), render targets and depth buffers (high on play/main) stay. The buffer and its GPU address do not change.

  • Past the cap, a copy stays in system memory. It is a plain copy on main's path, with main's tests. It loses no writes, because nothing about it differs from main.

Files: vram.rs (new: switch, WriteOnly, budget, buffer creation, the line under diagnostics); shadow.rs (the video memory side, put/settle, pull_written/pull; device-removed reports say when the nearest copy's GPU side is in video memory); host.rs (mapped_buffer, copy_now, memory_info, the bring-back before copy_back_marks); memory.rs (the bring-back before computed-address copy-backs); resource.rs (read-only buffers registered as such).

Switches

switch effect
VICTORY_VRAM_COPIES=1 copies of buffers made without unordered access go to video memory
VICTORY_VRAM_COPIES=2 copies of every title buffer do
VICTORY_VRAM_COPIES_MB=N cap of N MB in place of a quarter of the budget

Unset or 0: off. With --features diagnostics, a line per stats interval reports:

host copies in video memory: N of read-only buffers (MB), N of written ones (MB), MB in all (cap now MB; card MB of MB[, past 85 %: admitting none]); refused for the budget N (MB); shaders' writes brought back MB/frame in N GPU copies/frame (declared ranges, marked pages, computed-address pages MB/frame)

"cap now" is the cap after the margin. The three parts are counted before runs of one copy that meet are joined, so they can add up to more than the total.

Limits

  • Twice the memory for each copy in video memory. The system memory side is kept in full. The extra RAM was accepted for this design: it keeps the CPU off the BAR and makes falling back free.
  • Refreshes write twice. One write goes to system memory and one to the BAR, about 22.7 GB/s on a 5070 Ti per #2120. A lazy CPU side, as in #2130's mirror, would avoid the second write but adds stale-page tracking on every CPU read. That is left for a later change, if a title run shows refresh time matters.
  • One GPU copy per submission that wrote copies in video memory. It costs a fixed ~0.05 ms (copy_now's signal and wait; #2130 measured 1 page in 0.048 ms and 64 pages in 0.232 ms) plus the bytes.
  • Admission only. Nothing is moved out of video memory when the budget shrinks later. Placement is decided once, when the copy is made. Past the budget, the low residency priority lets the system demote copies before textures (not measured on hardware yet).
  • Undeclared, unmarked shader writes stay on the GPU side, as they stay in the copy on main, until a refresh of their page.
  • Not built on #2051. #2051 (refreshes on several threads) is not on main. Its queued refresh copies must call Shadow::copy, or write through put, to reach video memory. A refresh that wrote only the system memory side was the bug the rebase onto #2130 found.

Tests (WARP, --features diagnostics)

Each test below re-runs itself in a child process with VICTORY_VRAM_COPIES set, because the switch is read once per process. It asserts the class it expects, so it cannot pass by skipping. The parent checks that the child ran 1 test.

The software adapter here does report GPUUploadHeapSupported, so the tests use real GPU upload heaps. For hosts without them, and with VICTORY_TEST_VRAM_STAND_IN=1, the tests use a write-combined CUSTOM heap that the CPU maps in their place, so the same code runs either way. W0 prints which heap it used.

  • copies_in_video_memory_are_made_charged_and_given_back (W0): a read-only buffer's copy is VramRead and a written one's is VramWritten. Their GPU sides are in the video memory heap and the CPU side is in system memory. Both are charged. A clone of a buffer, as heap slots hold, keeps its bytes charged. The bytes go back when the clone goes.

  • copies_past_the_cap_stay_in_system_memory_and_lose_no_writes (#693, Belobug #105): with a faked 64 MiB budget, 4 copies of 4 MiB go to video memory and the fifth stays in system memory. With faked use near the budget, a sixth stays too, and 2 refusals are counted. A submission's shaders then write into two copies in video memory and two in system memory, one marked and one declared (snapshot) of each. All four writes reach title memory.

  • refreshes_of_copies_in_video_memory_reach_video_memory: what the GPU reads matches the title's bytes after the first refresh, after a title write, and under a range a list declared as written by shaders.

  • shader_writes_into_copies_in_video_memory_come_back: a read-only buffer's copy is written by shaders anyway, through a view that writes. A marked page and a declared range go back by the bytes the GPU changed, brought back by a GPU copy (counted). A title write in the marked page during the submission stays, and is in both sides afterwards.

  • a_title_write_during_a_copy_back_reaches_video_memory (the order): a test hook writes title memory between a marked page's copy-back write and the page being taken again. The page is then current, so no refresh brings the write. Video memory has it.

  • copies_go_to_video_memory_within_the_cap_and_the_budget (no GPU): the admission rule, including #693's numbers (19,042 MB of a 10,570 MB budget: not even one page).

  • a_copy_in_video_memory_takes_no_more_dynamic_slots_than_one_in_system_memory (draw.rs): one copy is admitted (VramRead), the next is refused (System). Over 8 submissions each list takes the same dynamic slots for root views of either: read and written, at two addresses, one view per copy and kind. The copy in video memory keeps the buffer and GPU address it was made with.

  • marked_ranges_of_copies_in_video_memory_come_back_by_their_marked_pages: a range declared marked comes back as its one marked page (64 KiB of 256), and an unmarked range comes back whole, once. Both are counted by why.

  • the_cap_shrinks_as_the_card_fills_and_copies_wait_near_a_full_budget (no GPU): the title run's numbers. The cap is a quarter of the budget at first. With 9,800 MB used by others, under 118 MB is left. At the run's end (11,063 of 11,668 MB) nothing is admitted. At 80 % still nothing, after passing 85 %. Under 75 % copies are admitted again.

Results on this head (1cf4dac8, on main 63bcca43; VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics --lib -- --test-threads=1):

  • With VICTORY_VRAM_COPIES unset: 255 passed / 0 failed / 1 ignored (main 63bcca43 in the same worktree: 246 / 0 / 1). The new tests set the switch in their child processes.
  • Clippy --all-targets -D warnings is clean with and without --all-features.
  • With VICTORY_VRAM_COPIES=2 set for the whole suite, 9 of main's in-process tests fail (bindless::tests::heap_views_are_current, heap_views_follow_writes_through_another_mapping, draw::tests::root_views_reach_their_copy_and_read_zeros_past_it, five shadow::tests on marks and snapshots, and #2377's new partial_refresh_under_marked_range_updates_saved_bytes). They write the GPU side through write_host and then call copy-back directly, without the post-submission bring-back (pull_written) the real path runs, so they read zeros. Eight fail the same way before the rebase. If wanted, these tests can skip, or call pull_written, when the process-wide switch is set.
  • The rebase met two of main's changes: Shadow::update_free became update_tracked (#2172), so the video memory write after zeroing unmapped pages moved into its unmapped loop; and Shadow::copy now calls Tracked::refreshed (#2377), kept, followed by the video memory write.

Earlier, on the pre-rebase branch (mem/vram-copies 72338fe6, main 13f91cae): 207 / 0 / 1, and the play/main port (82c8382b): 289 / 0 / 10.

  • An existing test fails now and then in the full suite, on main as well. memory::tests::a_submission_that_missed_leaves_the_copies_at_once ("the written page counts as changed at once") failed:

    • in 3 of 5 suite runs on this branch, one of them with the five new child-process tests skipped;
    • in 1 of 3 suite runs on main 13f91cae, in the same worktree and conditions (descriptor::host::tests::a_slot_rewritten_while_its_view_is_prepared_views_the_new_record failed in another).

    It passed 3 of 3 run alone. Another test's submission can refresh the shared copy between its read_host and its clean() check, so it is a race in the test, not something this PR changes.

What a title run should show

Compare the same save and camera, VICTORY_GPU_TIMING=1, with --features diagnostics, in three runs: off, =1, and =2. Expected:

  • GPU ms/frame drops with =1 (vertex, index and constant data) and more with =2 (heaps the shaders write).
    • #2120's synthetic read: 31x faster on a 5070 Ti.
    • The #2130 title run on a 5090 (wbkepler): GPU 316-332 → 100-103 ms.
  • VRAM MB: the host copies in video memory line gives MB held and the cap. The video memory: line stays under the budget.
  • No device removal.
  • Copy-back ms rises a little (one GPU copy per writing submission). Preparing ms rises by the second write of refreshed bytes.

Game results

Local runs on an RTX 5070 (12 GB, Windows 11), the same open-world save, about 10 minutes of world each, diagnostics builds. The title's own data was used in these runs; only counts and timings from the logs are given here. The play builds also carry other open PRs and our own fixes not yet upstream (a dynamic descriptor pool fix, bounded list memory and others), so these numbers show this change against the same build without it, not main as is. All arms below: 0 device removals.

Against the same build with the switch off (DP1 → VC2; play/main-vram 82c8382b, VICTORY_VRAM_COPIES off vs =2, batched clamps off in both):

off =2
median frame time, difference (95 % CI) −64 .. −127 ms
mean frame time 910 ms 817 ms
GPU / preparing / copy-back ms per frame 316 / 295 / 121 216 / 268 / 130
drops per frame: no host pipeline / indirect 565 / 472 417 / 205

Copies in video memory: 1,231 MB (203 read-only + 1,028 written). The 85 % stop held the card at 90 % of the budget ("past 85 %: admitting none"). Shaders' writes brought back: 81 MB/frame in 20 GPU copies, marked pages only.

Against #2130's GPU upload heaps, on one build (N2U → N2P; play/main-next-2 deeb5939, which carries both; N2U: VICTORY_GPU_UPLOAD=1 + _WRITES=1, this switch off; N2P: VICTORY_VRAM_COPIES=2, the upload heaps off):

upload heaps (#2130) =2 (this PR)
median frame time, difference (95 % CI) −50 .. −96 ms
mean / p50 / p95 frame time 838 / 762 / 1,399 ms 746 / 692 / 930 ms
GPU / preparing / copy-back ms per frame 126 / 283 / 133 123 / 268 / 133
drops per frame: no host pipeline / indirect 389 / 108 382 / 99

The GPU time is about the same either way. The difference is in fewer long frames: the 95th percentile falls from 1,399 to 930 ms.

Against the same build without any GPU placement (BM1 → N2P): median −7 .. −36 ms (95 % CI); GPU 227 → 123 ms per frame; preparing 227 → 268 ms. Preparing eats most of the GPU gain. Part of it is the second write of refreshed bytes and the bring-backs listed under Limits.

Earlier runs of this change (VB and VC, before the fix) were cut short by the memory guard, because of a host upload-buffer growth unrelated to this change (fixed separately since), and their pool drops confounded the frame times. They are left out here.

Credit

  • 0xkn0wh3r3, #2120 and #2130: copies in GPU upload heaps, read-only buffers registered by their flags, the destruction-callback charge, copy_now (ported, co-author on the commit), and the child-process test pattern.
  • #2222 (0xkn0wh3r3): the idea of sizing the copies by the DXGI budget and charging bytes between readings. The budget code here is our own.
  • wbkepler, #693: the over-budget evidence (an RTX 5070: 19 GB of a 10.5 GB budget, then a TDR).
  • Belobug #105: the copies past a cap that lost their writes.

Overlap

This PR overlaps #2120, #2130 and #2222 (0xkn0wh3r3) directly:

  • Its read-only half does what #2120 does.
  • Its written half does what #2130 does, with a different system memory side: always current and always kept, against #2130's lazy mirror.
  • Its admission covers what #2222's upload pool does for copies.

#2222's residency priorities and render-target demotion are not here. If the maintainer prefers 0xkn0wh3r3's PRs, the parts of this one that stand on their own are:

  • the write-only mapping type;
  • every CPU write going through one path to video memory;
  • the ordering rule and its test;
  • the WARP stand-in heap with the child-process tests.

They can go there as suggested commits. #2389 (desplmfao, texture levels on demand) is separate and makes room for these copies on 12 GB cards.

The tests above are synthetic and do not use the title. The game results use local title data, as stated in their section.

AI-assisted

🤖 Generated with Claude Code

**@⁠r4tedx** on GitHub · 2026-11-26 02:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/2993) `r4tedx:mem/vram-copies-main` → `main` · +1977 −49 · 7 files · 4 commits · open ### What it changes Off by default. Without the switch, no code path changes. - **Where shaders read.** With `VICTORY_VRAM_COPIES=1`, the host copy of a title buffer made without unordered access gets a second buffer in video memory: a GPU upload heap, which the CPU maps through the PCI BAR. It is used only when `D3D12_OPTIONS16.GPUUploadHeapSupported` is set (Resizable BAR). Shaders, views, root descriptors and page tables use that buffer. With `=2`, copies of buffers that shaders may write get one too. Loose windows and the copies of single views stay in system memory. - **The CPU's side stays in system memory, always.** The system memory buffer that main has today is kept for every copy. The CPU reads only that buffer (snapshots, page compares, copy-backs). Every CPU write into the copy goes there first and then into video memory: - refreshes, including those under a range a submitted list declared; - the zeros of unmapped pages; - copy-back merges. - **The CPU never reads video memory.** The BAR mapping is a `vram::WriteOnly`, a type with only a `write` method and private fields, so no code outside `vram.rs` can read through it. A CPU read of a BAR runs at about 50 MB/s (#2120's table). - **Shader writes come back through main's page marks.** After a submission's GPU work, and before any copy-back reads a copy, one GPU copy brings into the system memory side: - the ranges its lists declared unmarked (snapshot ranges), withheld ones too. Main compares these whole, so they come back whole; - the pages marked in either written-pages bitmap, held-back ones too. Ranges declared marked (views that mark their writes, root UAVs of marked copies) do not come back whole. The CPU reads nothing in them but their marked pages. Pages written at computed addresses (`memory.rs`) are brought back the same way before their copy-back. The copy-backs then run main's code unchanged: compare, write the changed bytes into title memory, take the page again. The system memory side then matches the GPU side wherever shaders wrote, withheld writes included. - **The order of a copy-back.** The order is: bring back, merge and write, take the page again, read the page from title memory once more, then write video memory. A title write that lands after the page went back and before its guard is back is in that last read. A write after the guard faults and marks the page changed. If video memory were written before the read, it would miss such a write for good, because the page counts as current. For copies in system memory, nothing changes. - **Admission through the DXGI budget.** `QueryVideoMemoryInfo` (local segment) is asked at most every 250 ms. Bytes taken since the last reading count as used. A copy goes to video memory only when: - copies there stay within the cap: a quarter of the budget (`VICTORY_VRAM_COPIES_MB` sets it by hand), and never more than what the rest of the process leaves under 85 % of the budget. The cap shrinks as textures, render targets and depth buffers fill the card; - use stays under 85 % of the budget; - use has not passed 85 % since it was last under 75 %. Near a full budget, room a texture gives back is soon taken again; - there is a reading at all. Admission and charge happen under one lock. A copy's bytes go back when D3D12 destroys its buffer (`ID3DDestructionNotifier`), because lists and heap slots can hold the buffer after the copy is gone. - **Copies yield to textures past the budget.** Each buffer in video memory gets `D3D12_RESIDENCY_PRIORITY_LOW`. Copies are made early, before most textures; in the title run they took 1.2 GB at 54 % of the budget, and textures then took the card to 95 %. Past the budget, the system moves our copies to system memory first. Shaders then read them across the bus, as they read a system copy. The title's textures (normal), render targets and depth buffers (high on play/main) stay. The buffer and its GPU address do not change. - **Past the cap, a copy stays in system memory.** It is a plain copy on main's path, with main's tests. It loses no writes, because nothing about it differs from main. Files: `vram.rs` (new: switch, `WriteOnly`, budget, buffer creation, the line under `diagnostics`); `shadow.rs` (the video memory side, `put`/`settle`, `pull_written`/`pull`; device-removed reports say when the nearest copy's GPU side is in video memory); `host.rs` (`mapped_buffer`, `copy_now`, `memory_info`, the bring-back before `copy_back_marks`); `memory.rs` (the bring-back before computed-address copy-backs); `resource.rs` (read-only buffers registered as such). ### Switches | switch | effect | |---|---| | `VICTORY_VRAM_COPIES=1` | copies of buffers made without unordered access go to video memory | | `VICTORY_VRAM_COPIES=2` | copies of every title buffer do | | `VICTORY_VRAM_COPIES_MB=N` | cap of N MB in place of a quarter of the budget | Unset or `0`: off. With `--features diagnostics`, a line per stats interval reports: `host copies in video memory: N of read-only buffers (MB), N of written ones (MB), MB in all (cap now MB; card MB of MB[, past 85 %: admitting none]); refused for the budget N (MB); shaders' writes brought back MB/frame in N GPU copies/frame (declared ranges, marked pages, computed-address pages MB/frame)` "cap now" is the cap after the margin. The three parts are counted before runs of one copy that meet are joined, so they can add up to more than the total. ### Limits - **Twice the memory for each copy in video memory.** The system memory side is kept in full. The extra RAM was accepted for this design: it keeps the CPU off the BAR and makes falling back free. - **Refreshes write twice.** One write goes to system memory and one to the BAR, about 22.7 GB/s on a 5070 Ti per #2120. A lazy CPU side, as in #2130's mirror, would avoid the second write but adds stale-page tracking on every CPU read. That is left for a later change, if a title run shows refresh time matters. - **One GPU copy per submission that wrote copies in video memory.** It costs a fixed ~0.05 ms (`copy_now`'s signal and wait; #2130 measured 1 page in 0.048 ms and 64 pages in 0.232 ms) plus the bytes. - **Admission only.** Nothing is moved out of video memory when the budget shrinks later. Placement is decided once, when the copy is made. Past the budget, the low residency priority lets the system demote copies before textures (not measured on hardware yet). - **Undeclared, unmarked shader writes stay on the GPU side**, as they stay in the copy on main, until a refresh of their page. - **Not built on #2051.** #2051 (refreshes on several threads) is not on main. Its queued refresh copies must call `Shadow::copy`, or write through `put`, to reach video memory. A refresh that wrote only the system memory side was the bug the rebase onto #2130 found. ### Tests (WARP, `--features diagnostics`) Each test below re-runs itself in a child process with `VICTORY_VRAM_COPIES` set, because the switch is read once per process. It asserts the class it expects, so it cannot pass by skipping. The parent checks that the child ran 1 test. The software adapter here does report `GPUUploadHeapSupported`, so the tests use real GPU upload heaps. For hosts without them, and with `VICTORY_TEST_VRAM_STAND_IN=1`, the tests use a write-combined CUSTOM heap that the CPU maps in their place, so the same code runs either way. W0 prints which heap it used. - `copies_in_video_memory_are_made_charged_and_given_back` (W0): a read-only buffer's copy is `VramRead` and a written one's is `VramWritten`. Their GPU sides are in the video memory heap and the CPU side is in system memory. Both are charged. A clone of a buffer, as heap slots hold, keeps its bytes charged. The bytes go back when the clone goes. - `copies_past_the_cap_stay_in_system_memory_and_lose_no_writes` (#693, Belobug #105): with a faked 64 MiB budget, 4 copies of 4 MiB go to video memory and the fifth stays in system memory. With faked use near the budget, a sixth stays too, and 2 refusals are counted. A submission's shaders then write into two copies in video memory and two in system memory, one marked and one declared (snapshot) of each. All four writes reach title memory. - `refreshes_of_copies_in_video_memory_reach_video_memory`: what the GPU reads matches the title's bytes after the first refresh, after a title write, and under a range a list declared as written by shaders. - `shader_writes_into_copies_in_video_memory_come_back`: a read-only buffer's copy is written by shaders anyway, through a view that writes. A marked page and a declared range go back by the bytes the GPU changed, brought back by a GPU copy (counted). A title write in the marked page during the submission stays, and is in both sides afterwards. - `a_title_write_during_a_copy_back_reaches_video_memory` (the order): a test hook writes title memory between a marked page's copy-back write and the page being taken again. The page is then current, so no refresh brings the write. Video memory has it. - `copies_go_to_video_memory_within_the_cap_and_the_budget` (no GPU): the admission rule, including #693's numbers (19,042 MB of a 10,570 MB budget: not even one page). - `a_copy_in_video_memory_takes_no_more_dynamic_slots_than_one_in_system_memory` (`draw.rs`): one copy is admitted (`VramRead`), the next is refused (System). Over 8 submissions each list takes the same dynamic slots for root views of either: read and written, at two addresses, one view per copy and kind. The copy in video memory keeps the buffer and GPU address it was made with. - `marked_ranges_of_copies_in_video_memory_come_back_by_their_marked_pages`: a range declared marked comes back as its one marked page (64 KiB of 256), and an unmarked range comes back whole, once. Both are counted by why. - `the_cap_shrinks_as_the_card_fills_and_copies_wait_near_a_full_budget` (no GPU): the title run's numbers. The cap is a quarter of the budget at first. With 9,800 MB used by others, under 118 MB is left. At the run's end (11,063 of 11,668 MB) nothing is admitted. At 80 % still nothing, after passing 85 %. Under 75 % copies are admitted again. Results on this head (1cf4dac8, on `main` 63bcca43; `VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics --lib -- --test-threads=1`): - With `VICTORY_VRAM_COPIES` unset: 255 passed / 0 failed / 1 ignored (`main` 63bcca43 in the same worktree: 246 / 0 / 1). The new tests set the switch in their child processes. - Clippy `--all-targets -D warnings` is clean with and without `--all-features`. - With `VICTORY_VRAM_COPIES=2` set for the whole suite, 9 of main's in-process tests fail (`bindless::tests::heap_views_are_current`, `heap_views_follow_writes_through_another_mapping`, `draw::tests::root_views_reach_their_copy_and_read_zeros_past_it`, five `shadow::tests` on marks and snapshots, and #2377's new `partial_refresh_under_marked_range_updates_saved_bytes`). They write the GPU side through `write_host` and then call copy-back directly, without the post-submission bring-back (`pull_written`) the real path runs, so they read zeros. Eight fail the same way before the rebase. If wanted, these tests can skip, or call `pull_written`, when the process-wide switch is set. - The rebase met two of main's changes: `Shadow::update_free` became `update_tracked` (#2172), so the video memory write after zeroing unmapped pages moved into its unmapped loop; and `Shadow::copy` now calls `Tracked::refreshed` (#2377), kept, followed by the video memory write. Earlier, on the pre-rebase branch (mem/vram-copies 72338fe6, `main` 13f91cae): 207 / 0 / 1, and the play/main port (82c8382b): 289 / 0 / 10. - An existing test fails now and then in the full suite, on main as well. `memory::tests::a_submission_that_missed_leaves_the_copies_at_once` ("the written page counts as changed at once") failed: - in 3 of 5 suite runs on this branch, one of them with the five new child-process tests skipped; - in 1 of 3 suite runs on main 13f91cae, in the same worktree and conditions (`descriptor::host::tests::a_slot_rewritten_while_its_view_is_prepared_views_the_new_record` failed in another). It passed 3 of 3 run alone. Another test's submission can refresh the shared copy between its `read_host` and its `clean()` check, so it is a race in the test, not something this PR changes. ### What a title run should show Compare the same save and camera, `VICTORY_GPU_TIMING=1`, with `--features diagnostics`, in three runs: off, `=1`, and `=2`. Expected: - **GPU ms/frame** drops with `=1` (vertex, index and constant data) and more with `=2` (heaps the shaders write). - #2120's synthetic read: 31x faster on a 5070 Ti. - The #2130 title run on a 5090 (wbkepler): GPU 316-332 → 100-103 ms. - **VRAM MB:** the `host copies in video memory` line gives MB held and the cap. The `video memory:` line stays under the budget. - **No device removal.** - **Copy-back ms** rises a little (one GPU copy per writing submission). **Preparing ms** rises by the second write of refreshed bytes. ### Game results Local runs on an RTX 5070 (12 GB, Windows 11), the same open-world save, about 10 minutes of world each, diagnostics builds. **The title's own data was used in these runs; only counts and timings from the logs are given here.** The play builds also carry other open PRs and our own fixes not yet upstream (a dynamic descriptor pool fix, bounded list memory and others), so these numbers show this change against the same build without it, not `main` as is. All arms below: 0 device removals. **Against the same build with the switch off** (DP1 → VC2; play/main-vram 82c8382b, `VICTORY_VRAM_COPIES` off vs `=2`, batched clamps off in both): | | off | `=2` | |---|---:|---:| | median frame time, difference (95 % CI) | | −64 .. −127 ms | | mean frame time | 910 ms | 817 ms | | GPU / preparing / copy-back ms per frame | 316 / 295 / 121 | 216 / 268 / 130 | | drops per frame: no host pipeline / indirect | 565 / 472 | 417 / 205 | Copies in video memory: 1,231 MB (203 read-only + 1,028 written). The 85 % stop held the card at 90 % of the budget ("past 85 %: admitting none"). Shaders' writes brought back: 81 MB/frame in 20 GPU copies, marked pages only. **Against #2130's GPU upload heaps, on one build** (N2U → N2P; play/main-next-2 deeb5939, which carries both; N2U: `VICTORY_GPU_UPLOAD=1` + `_WRITES=1`, this switch off; N2P: `VICTORY_VRAM_COPIES=2`, the upload heaps off): | | upload heaps (#2130) | `=2` (this PR) | |---|---:|---:| | median frame time, difference (95 % CI) | | −50 .. −96 ms | | mean / p50 / p95 frame time | 838 / 762 / 1,399 ms | 746 / 692 / 930 ms | | GPU / preparing / copy-back ms per frame | 126 / 283 / 133 | 123 / 268 / 133 | | drops per frame: no host pipeline / indirect | 389 / 108 | 382 / 99 | The GPU time is about the same either way. The difference is in fewer long frames: the 95th percentile falls from 1,399 to 930 ms. **Against the same build without any GPU placement** (BM1 → N2P): median −7 .. −36 ms (95 % CI); GPU 227 → 123 ms per frame; preparing 227 → 268 ms. Preparing eats most of the GPU gain. Part of it is the second write of refreshed bytes and the bring-backs listed under Limits. Earlier runs of this change (VB and VC, before the fix) were cut short by the memory guard, because of a host upload-buffer growth unrelated to this change (fixed separately since), and their pool drops confounded the frame times. They are left out here. ### Credit - 0xkn0wh3r3, #2120 and #2130: copies in GPU upload heaps, read-only buffers registered by their flags, the destruction-callback charge, `copy_now` (ported, co-author on the commit), and the child-process test pattern. - #2222 (0xkn0wh3r3): the idea of sizing the copies by the DXGI budget and charging bytes between readings. The budget code here is our own. - wbkepler, #693: the over-budget evidence (an RTX 5070: 19 GB of a 10.5 GB budget, then a TDR). - Belobug #105: the copies past a cap that lost their writes. ### Overlap This PR overlaps #2120, #2130 and #2222 (0xkn0wh3r3) directly: - Its read-only half does what #2120 does. - Its written half does what #2130 does, with a different system memory side: always current and always kept, against #2130's lazy mirror. - Its admission covers what #2222's upload pool does for copies. #2222's residency priorities and render-target demotion are not here. If the maintainer prefers 0xkn0wh3r3's PRs, the parts of this one that stand on their own are: - the write-only mapping type; - every CPU write going through one path to video memory; - the ordering rule and its test; - the WARP stand-in heap with the child-process tests. They can go there as suggested commits. #2389 (desplmfao, texture levels on demand) is separate and makes room for these copies on 12 GB cards. The tests above are synthetic and do not use the title. The game results use local title data, as stated in their section. AI-assisted 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With VICTORY_VRAM_COPIES=1, the copy of a title buffer made without
unordered access gets a GPU side in a GPU upload heap (resizable BAR,
D3D12_OPTIONS16.GPUUploadHeapSupported); with =2, so do copies of buffers
shaders may write. Shaders use the video memory buffer. The system memory
buffer stays the copy's CPU side: every CPU write into the copy (refreshes,
the zeros of unmapped pages, copy-back merges) goes there and then into
video memory through a write-only mapping, which has no way to be read
(the CPU reads a BAR at ~50 MB/s).

What shaders wrote comes back before the CPU reads it: after a
submission ran, the ranges its lists declared and the pages marked in the
written-pages bitmaps (withheld ones too) are copied by the GPU into the
system memory side, in one list; pages written at computed addresses
likewise, before their copy-back. The copy-backs then compare as for any
copy. When a copy-back takes a page again, the system memory side reads
it from title memory once more before its bytes go to video memory, so a
title write that lands while the page goes back reaches video memory.

Admission: copies in video memory hold at most a quarter of the budget
DXGI gives the process (VICTORY_VRAM_COPIES_MB sets the cap by hand), and
none is made where use would pass nine tenths of the budget, or without
a reading. Their bytes go back when D3D12 destroys the buffer. A copy
refused stays in system memory, on main's path.

Tests run on the software adapter with a write-combined stand-in heap when
it has no GPU upload heaps, each in a child process with the switch set.

Ideas, the GPU copy and the child-process test pattern from #2120 and

Co-Authored-By: 0xkn0wh3r3 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first title runs of the switch (RTX 5070, 12 GB) showed three things.

The budget. Copies were admitted at 54 % of the budget, before the
world's textures loaded, and the textures then took the card to 95 %.
Admission now leaves a margin: use stays under 85 % of the budget, and
the cap (a quarter of the budget, or VICTORY_VRAM_COPIES_MB) is never
more than what the rest of the process leaves under 85 %, so it shrinks
as the card fills. Once use passes 85 %, nothing is admitted until it is
back under 75 %. Each buffer in video memory gets
D3D12_RESIDENCY_PRIORITY_LOW: past the budget the system moves these
copies to system memory before the title's textures and targets, with
the same buffer and GPU address.

The bring-back. Ranges declared written and marked came back whole after
each submission, though the CPU reads nothing of them but their marked
pages, which come back anyway. Now only unmarked ranges (main compares
them whole) and marked pages come back. The diagnostics line splits the
bytes by why: declared ranges, marked pages, computed-address pages.

Descriptor slots. A copy in video memory costs a list no more dynamic
slots than one in system memory. Its buffer and GPU address are fixed at
creation. Root-view slots are keyed by copy and kind, fresh tables by
the title's descriptor words, and page-table view pairs by copy. A test
now checks this over 8 submissions. The run's slot shortage followed
more draws and twice the host textures under =2, not the copies' views.

Tests: a copy in video memory takes the same slots as a system copy over
8 submissions, and keeps its address; marked ranges come back by their
marked pages and unmarked ones whole, once; the margin, the 75 % latch
and the shrinking cap with the run's numbers; #693 and Belobug #105 as
before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:37 +00:00
From the review of #2993 (fuzeofficlal):

- A GPU copy that brings shaders' writes back reports whether its end
  was seen (`Host::copy_now`). Its allocator is reset only after its last
  list was seen to end. If the signal fails after the list was executed,
  the copier is not used again. If the event fails, the fence is read
  instead. A removed device ends the run, as after a submission. When the
  bring-back's copy fails, the bytes are read through the mapping before
  the copy-backs, so none of the shaders' writes is lost and no stale
  side is read. From then on, no copy is admitted to video memory.
  Without a copier, none is admitted at all.
- With `VICTORY_VRAM_COPIES` on, host copies (their system memory sides,
  copies the budget refused, loose windows, copies of single views) are
  made in system memory always (`Host::system_buffer`). Diagnostics'
  `VICTORY_COPIES_IN_VIDEO_MEMORY` no longer puts them in GPU upload
  heaps outside the budget.
- A reading kept up to 250 ms is taken again when a copy would take use
  past 75 % on it. The 85 % margin is documented as approximate.

Tests run in-process with `VICTORY_VRAM_COPIES` set for the whole suite
(9 failed at `=2`) now see what a submission's end does. `write_host`
brings back what it writes, as `pull_written` does, and `write_gpu`
writes the GPU side alone, inside a submission. `read_host` reads what
shaders see: the GPU side, where undeclared, unmarked writes through
heap views stay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A range declared unmarked is snapshotted at submission, and its
copy-back takes what differs from the snapshot for this submission's
shader writes. On main the copy is one buffer, so the snapshot holds
whatever earlier shaders wrote there through views no list declared or
marked (a shader without write marks). With a video memory side, the
snapshot came from the system memory side, which lacks such a write.
The copy-back then put that write into title memory, late, over a title
write made meanwhile. Main never does that.

`Shadow::writing` now brings the range back from video memory first,
where no snapshot covers it yet: one GPU copy per such range of a copy
in video memory, counted as "before snapshots" on the diagnostics line.
Ranges declared marked, and copies in system memory, are not affected.

Test: an_old_unmarked_write_does_not_reach_title_memory_late_at_2 and
_at_0. The title's byte survives in both. With the bring-back off, the
`=2` test fails (title memory gets the old shader byte).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@⁠pickles976 on GitHub · 2026-11-26 02:46 UTC · original

Independent Wine/vkd3d-proton 3.2.0 run on the RTX 4060 Laptop GPU (8 GiB), driver 590.48.1, exact head 1cf4dac8, vendor masking disabled: copies_in_video_memory_are_made_charged_and_given_back passed 1/1 and explicitly printed GPU upload heaps used: true (not the CUSTOM stand-in), so the native GPU-upload path is available on this stack. Three other child-process fixtures each passed 1/1: cap refusal preserves system-memory fallback and writes; marked/declared shader writes come back; a title write in the copy-back/re-guard window reaches video memory. No title run or frame-time/budget-residency result. On this 16 GiB RAM/8 GiB VRAM machine this is not a loading RAM fix: the full system-memory CPU copy remains and enabling the option adds VRAM use. Recommend comparing off vs =1 first with a guarded, same-save title trial only after a separate loading-RAM fix clears our 2,048 MiB available safety floor; record admitted MB, video-memory budget, preparing/copy-back/GPU ms and device removals before considering =2. Logs .compat-runtime/logs/verify-pr2993-wine-rtx4060-{vram-copy-availability,copies_past_the_cap_stay_in_system_memory_and_lose_no_writes,shader_writes_into_copies_in_video_memory_come_back,a_title_write_during_a_copy_back_reaches_video_memory}.log.

**@⁠pickles976** on GitHub · 2026-11-26 02:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/2993#issuecomment-7181112281) Independent Wine/vkd3d-proton 3.2.0 run on the **RTX 4060 Laptop GPU (8 GiB)**, driver 590.48.1, exact head 1cf4dac8, vendor masking disabled: `copies_in_video_memory_are_made_charged_and_given_back` passed 1/1 and explicitly printed `GPU upload heaps used: true` (not the CUSTOM stand-in), so the native GPU-upload path *is* available on this stack. Three other child-process fixtures each passed 1/1: cap refusal preserves system-memory fallback and writes; marked/declared shader writes come back; a title write in the copy-back/re-guard window reaches video memory. No title run or frame-time/budget-residency result. On this 16 GiB RAM/8 GiB VRAM machine this is not a loading RAM fix: the full system-memory CPU copy remains and enabling the option adds VRAM use. Recommend comparing off vs `=1` first with a guarded, same-save title trial **only after** a separate loading-RAM fix clears our 2,048 MiB available safety floor; record admitted MB, video-memory budget, preparing/copy-back/GPU ms and device removals before considering `=2`. Logs `.compat-runtime/logs/verify-pr2993-wine-rtx4060-{vram-copy-availability,copies_past_the_cap_stay_in_system_memory_and_lose_no_writes,shader_writes_into_copies_in_video_memory_come_back,a_title_write_during_a_copy_back_reaches_video_memory}.log`.
Author
Owner

@⁠r4tedx on GitHub · 2026-11-26 04:42 UTC · original

Thanks @⁠fuzeofficlal for the careful read. All three points hold against 1cf4dac8, and they're fixed in d2c20e28 on this branch. It also has a fix for the tests that failed with VICTORY_VRAM_COPIES set for the whole suite, and, in 477c6c6f, a correctness fix we found ourselves (both below).

P1: a failed bring-back copy

You're right on every count. copy_now returned nothing, logged the errors, ignored WaitForSingleObject, and reset the allocator on the next call even when Signal had failed after ExecuteCommandLists.

  • Host::copy_now (host.rs:3736) now returns whether the copy's end was seen.
    • A failure while recording (allocator or list Reset, Close) executes nothing, so the copier stays usable.
    • After ExecuteCommandLists, a failed Signal means the end can't be seen. The copier is then marked broken (host.rs:3800) and never used again, so its allocator is never reset under a list that may still be running.
    • Copier::wait (host.rs:3855) checks the wait's result. If SetEventOnCompletion or the wait fails, it reads the fence instead, for up to 10 s. A fence of u64::MAX (removed device) doesn't count as the end.
    • A removed device ends the run through fail (host.rs:3796), as after a submission, so it isn't hidden.
  • On a failed bring-back, bring_back (shadow.rs:1280) reads the runs through the mapping into the system memory side (WriteOnly::read_back, vram.rs:103). That happens before the copy-backs, so nothing they read is stale and no write is lost. vram::stop (vram.rs:123) then stops admitting copies. Without a copier, vram::make admits none at all (vram.rs:361).
  • Where I differ from the suggestion: a copy already in video memory can't drop its video memory side, because its GPU address is in views, root descriptors and page tables. And keeping the dirty state would only defer the loss while no copier works. So for those copies the fallback is the slow read through the mapping (~50 MB/s), counted in vram::READ_BACK, and only after a failure. The ordering of a copy-back is unchanged: bring back, merge and write, take the page, read it again, write video memory.
  • Test: shadow::tests::a_failed_gpu_copy_of_shaders_writes_loses_none_of_them runs a submission whose shaders write a marked page and a declared range, with a test hook (host::COPY_FAILS) failing the bring-back's copy:
    • Its event first: the fence is read, nothing is read back, and the copier still works.
    • Then its signal: the bytes are read back through the mapping, title memory and the system memory side have both writes, copy_now refuses from then on, and the next copy made stays in system memory.

P2a: both switches

Confirmed: with diagnostics' VICTORY_COPIES_IN_VIDEO_MEMORY set, shared_buffer made home in a GPU upload heap, so a copy the budget refused stayed in video memory too.

  • New Host::system_buffer (host.rs:3662) takes the system memory heap main's copies use (CUSTOM, write-back, L0). shared_buffer uses it unless main's switch is on.
  • With VICTORY_VRAM_COPIES on, every host copy buffer is made there (copy_buffer, shadow.rs:1305): system memory sides, copies the budget refused, loose windows and copies of single views. Video memory is then reached only through vram::make and its budget.
  • Test: shadow::tests::with_both_switches_the_cpu_sides_stay_in_system_memory runs a child with both switches. It asserts main's switch is on there, and that both a copy in video memory's system memory side and a refused copy are in a write-back CUSTOM heap. The child-process tests now clear main's switch unless they name it (in_child_with, vram.rs:517).

P2b: the cached reading

Agreed. Within the 250 ms, the reading sees only our own charges.

  • The module docs now call the 85 % margin approximate within that interval (vram.rs:22).
  • When a copy would take use past 75 % of the budget on a kept reading, DXGI is asked again before admission (vram.rs:373). A copy is made once per title buffer, so this costs one QueryVideoMemoryInfo per copy made near the line. Far from the line, the kept reading is still used.
  • Test: vram::tests::a_kept_reading_near_the_line_is_taken_again, with a faked DXGI answer, kept as DXGI's is:
    • at 70 % a copy is admitted;
    • after others take 200 MB, the next copy is still admitted on the kept reading (the approximation, as documented);
    • after a new reading at 76 %, one more is admitted;
    • with 90 % behind the kept reading, the next copy asks again and is refused.

Tests run with the switch set for the whole suite

With VICTORY_VRAM_COPIES=2 set for the whole suite, 9 of main's in-process tests failed on 1cf4dac8. All 9 were the harness; no shader read a wrong byte.

  • Six skipped the submission's bring-back. They wrote the GPU side with write_host, then called the copy-backs directly, without the pull_written a submission runs:

    • shadow::tests::marked_pages_go_back_by_the_bytes_the_gpu_changed
    • marks_of_shaders_using_title_memory_are_held_back
    • partial_refresh_under_marked_range_updates_saved_bytes
    • snapshots_cover_the_declared_ranges_and_writes_go_back_once
    • title_writes_under_a_busy_range_are_copied
    • what_a_frame_withholds_leaves_the_copies_at_its_end
  • Three read the wrong side. These went through real submissions:

    • bindless::tests::heap_views_are_current
    • heap_views_follow_writes_through_another_mapping
    • draw::tests::root_views_reach_their_copy_and_read_zeros_past_it

    Their raw-HLSL shaders write the output through a heap or table view that no list declares and nothing marks. As bindless::refresh's shortcut says, those writes stay in the copy, which is now its GPU side. read_host read the system memory side. Their output buffers are made without unordered access, so they go to video memory even at =1.

The fix is in the helpers, not the tests:

  • read_host (shadow.rs:177) reads what shaders see: the GPU side, by a GPU copy, for a copy in video memory. read_system reads the system memory side.
  • write_host (shadow.rs:210) writes as a submission that has ended: the GPU side, then its bytes back, as pull_written does.
  • write_gpu (shadow.rs:229) writes the GPU side alone, for writes inside a submission. The new video memory tests use it, so they still check the real bring-back.

Every production copy-back already ran after its bring-back (host.rs pull_written before copy_back_marks and copy_back/keep_writes; memory.rs pull before copy_back_page).

A snapshot holds the GPU side (found while checking the above)

While checking which CPU reads of a copy could differ from main, we found one that did. A range declared unmarked is snapshotted at submission, and its copy-back takes whatever differs from the snapshot for this submission's shader writes. On main the copy is one buffer, so the snapshot already holds anything earlier shaders wrote there through a view that no list declared or marked (a shader translated without write marks).

With a video memory side, the snapshot came from the system memory side, which lacks such a write. The copy-back then put that old write into title memory, late, over a title write made in between. Main never does that.

Shadow::writing (shadow.rs:468) now brings the range back from video memory before snapshotting it, where no snapshot covers it yet. This is one GPU copy per such range, counted as "before snapshots" on the diagnostics line. Ranges declared marked, and copies in system memory, are unchanged.

We considered keeping such copies in system memory at admission instead. But a write can only go unmarked through a shader without write marks, and that is known per draw, not when the copy is made. Copies without mark bits would need 2^22 pages of copies first.

The test is shadow::tests::an_old_unmarked_write_does_not_reach_title_memory_late_at_2, with _at_0 as the baseline. A shader write lands on the GPU side, undeclared. Then the title writes the same byte while a later submission declares the range unmarked and writes another byte of it. The title's byte survives at =2, as at 0. With the new bring-back switched off, the =2 test fails: title memory gets the old shader byte (0xaa) in place of the title's (0x77).

Results

WARP, VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics --lib -- --test-threads 1:

VICTORY_VRAM_COPIES 1cf4dac8 d2c20e28 477c6c6f
unset 255 / 0 / 1 258 / 0 / 1 260 / 0 / 1
=1 not run 258 / 0 / 1 260 / 0 / 1
=2 246 / 9 / 1 258 / 0 / 1 260 / 0 / 1

The 5 added are the tests above. Clippy -p victory-gpu --all-targets -D warnings is clean with and without --all-features.

Not done: no hardware run. The failure paths are exercised only through the test hook, not by a real failed Signal.

Other notes on this PR

  • @⁠kotekompotek, thanks for the review. On the synchronous wait in memory::bind's pull: in our two title runs with =2 (about 10 minutes each, RTX 5070), the computed-address bring-back was 0.00 MB/frame in every diagnostics line (103 and 106 lines). All bring-backs were marked pages: 79 and 87 MB/frame, in about 20 GPU copies a frame. So that wait didn't cost anything there. The marked-page bring-backs are the likelier cost in preparing.
  • @⁠pickles976, thanks for the vkd3d-proton run. You're right that this is no fix for loading RAM: keeping the system memory side is by design (see Limits). On =1: in an earlier title run it made no measurable difference to frame time, with 203 MB of read-only copies admitted. The gain we measured is at =2. The game results in the description are local title runs, counts and timings only.

The tests above don't use the title. The two notes just above refer to the local title runs reported in the description.

AI-assisted

**@⁠r4tedx** on GitHub · 2026-11-26 04:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/2993#issuecomment-7182834858) Thanks @⁠fuzeofficlal for the careful read. All three points hold against `1cf4dac8`, and they're fixed in `d2c20e28` on this branch. It also has a fix for the tests that failed with `VICTORY_VRAM_COPIES` set for the whole suite, and, in `477c6c6f`, a correctness fix we found ourselves (both below). ### P1: a failed bring-back copy You're right on every count. `copy_now` returned nothing, logged the errors, ignored `WaitForSingleObject`, and reset the allocator on the next call even when `Signal` had failed after `ExecuteCommandLists`. - `Host::copy_now` (`host.rs:3736`) now returns whether the copy's end was seen. - A failure while recording (allocator or list `Reset`, `Close`) executes nothing, so the copier stays usable. - After `ExecuteCommandLists`, a failed `Signal` means the end can't be seen. The copier is then marked broken (`host.rs:3800`) and never used again, so its allocator is never reset under a list that may still be running. - `Copier::wait` (`host.rs:3855`) checks the wait's result. If `SetEventOnCompletion` or the wait fails, it reads the fence instead, for up to 10 s. A fence of `u64::MAX` (removed device) doesn't count as the end. - A removed device ends the run through `fail` (`host.rs:3796`), as after a submission, so it isn't hidden. - On a failed bring-back, `bring_back` (`shadow.rs:1280`) reads the runs through the mapping into the system memory side (`WriteOnly::read_back`, `vram.rs:103`). That happens before the copy-backs, so nothing they read is stale and no write is lost. `vram::stop` (`vram.rs:123`) then stops admitting copies. Without a copier, `vram::make` admits none at all (`vram.rs:361`). - Where I differ from the suggestion: a copy already in video memory can't drop its video memory side, because its GPU address is in views, root descriptors and page tables. And keeping the dirty state would only defer the loss while no copier works. So for those copies the fallback is the slow read through the mapping (~50 MB/s), counted in `vram::READ_BACK`, and only after a failure. The ordering of a copy-back is unchanged: bring back, merge and write, take the page, read it again, write video memory. - Test: `shadow::tests::a_failed_gpu_copy_of_shaders_writes_loses_none_of_them` runs a submission whose shaders write a marked page and a declared range, with a test hook (`host::COPY_FAILS`) failing the bring-back's copy: - Its event first: the fence is read, nothing is read back, and the copier still works. - Then its signal: the bytes are read back through the mapping, title memory and the system memory side have both writes, `copy_now` refuses from then on, and the next copy made stays in system memory. ### P2a: both switches Confirmed: with diagnostics' `VICTORY_COPIES_IN_VIDEO_MEMORY` set, `shared_buffer` made `home` in a GPU upload heap, so a copy the budget refused stayed in video memory too. - New `Host::system_buffer` (`host.rs:3662`) takes the system memory heap main's copies use (CUSTOM, write-back, L0). `shared_buffer` uses it unless main's switch is on. - With `VICTORY_VRAM_COPIES` on, every host copy buffer is made there (`copy_buffer`, `shadow.rs:1305`): system memory sides, copies the budget refused, loose windows and copies of single views. Video memory is then reached only through `vram::make` and its budget. - Test: `shadow::tests::with_both_switches_the_cpu_sides_stay_in_system_memory` runs a child with both switches. It asserts main's switch is on there, and that both a copy in video memory's system memory side and a refused copy are in a write-back CUSTOM heap. The child-process tests now clear main's switch unless they name it (`in_child_with`, `vram.rs:517`). ### P2b: the cached reading Agreed. Within the 250 ms, the reading sees only our own charges. - The module docs now call the 85 % margin approximate within that interval (`vram.rs:22`). - When a copy would take use past 75 % of the budget on a kept reading, DXGI is asked again before admission (`vram.rs:373`). A copy is made once per title buffer, so this costs one `QueryVideoMemoryInfo` per copy made near the line. Far from the line, the kept reading is still used. - Test: `vram::tests::a_kept_reading_near_the_line_is_taken_again`, with a faked DXGI answer, kept as DXGI's is: - at 70 % a copy is admitted; - after others take 200 MB, the next copy is still admitted on the kept reading (the approximation, as documented); - after a new reading at 76 %, one more is admitted; - with 90 % behind the kept reading, the next copy asks again and is refused. ### Tests run with the switch set for the whole suite With `VICTORY_VRAM_COPIES=2` set for the whole suite, 9 of main's in-process tests failed on `1cf4dac8`. All 9 were the harness; no shader read a wrong byte. - **Six skipped the submission's bring-back.** They wrote the GPU side with `write_host`, then called the copy-backs directly, without the `pull_written` a submission runs: - `shadow::tests::marked_pages_go_back_by_the_bytes_the_gpu_changed` - `marks_of_shaders_using_title_memory_are_held_back` - `partial_refresh_under_marked_range_updates_saved_bytes` - `snapshots_cover_the_declared_ranges_and_writes_go_back_once` - `title_writes_under_a_busy_range_are_copied` - `what_a_frame_withholds_leaves_the_copies_at_its_end` - **Three read the wrong side.** These went through real submissions: - `bindless::tests::heap_views_are_current` - `heap_views_follow_writes_through_another_mapping` - `draw::tests::root_views_reach_their_copy_and_read_zeros_past_it` Their raw-HLSL shaders write the output through a heap or table view that no list declares and nothing marks. As `bindless::refresh`'s shortcut says, those writes stay in the copy, which is now its GPU side. `read_host` read the system memory side. Their output buffers are made without unordered access, so they go to video memory even at `=1`. The fix is in the helpers, not the tests: - `read_host` (`shadow.rs:177`) reads what shaders see: the GPU side, by a GPU copy, for a copy in video memory. `read_system` reads the system memory side. - `write_host` (`shadow.rs:210`) writes as a submission that has ended: the GPU side, then its bytes back, as `pull_written` does. - `write_gpu` (`shadow.rs:229`) writes the GPU side alone, for writes inside a submission. The new video memory tests use it, so they still check the real bring-back. Every production copy-back already ran after its bring-back (`host.rs` `pull_written` before `copy_back_marks` and `copy_back`/`keep_writes`; `memory.rs` `pull` before `copy_back_page`). ### A snapshot holds the GPU side (found while checking the above) While checking which CPU reads of a copy could differ from main, we found one that did. A range declared unmarked is snapshotted at submission, and its copy-back takes whatever differs from the snapshot for this submission's shader writes. On main the copy is one buffer, so the snapshot already holds anything earlier shaders wrote there through a view that no list declared or marked (a shader translated without write marks). With a video memory side, the snapshot came from the system memory side, which lacks such a write. The copy-back then put that old write into title memory, late, over a title write made in between. Main never does that. `Shadow::writing` (`shadow.rs:468`) now brings the range back from video memory before snapshotting it, where no snapshot covers it yet. This is one GPU copy per such range, counted as "before snapshots" on the diagnostics line. Ranges declared marked, and copies in system memory, are unchanged. We considered keeping such copies in system memory at admission instead. But a write can only go unmarked through a shader without write marks, and that is known per draw, not when the copy is made. Copies without mark bits would need 2^22 pages of copies first. The test is `shadow::tests::an_old_unmarked_write_does_not_reach_title_memory_late_at_2`, with `_at_0` as the baseline. A shader write lands on the GPU side, undeclared. Then the title writes the same byte while a later submission declares the range unmarked and writes another byte of it. The title's byte survives at `=2`, as at `0`. With the new bring-back switched off, the `=2` test fails: title memory gets the old shader byte (0xaa) in place of the title's (0x77). ### Results WARP, `VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics --lib -- --test-threads 1`: | `VICTORY_VRAM_COPIES` | `1cf4dac8` | `d2c20e28` | `477c6c6f` | |---|---|---|---| | unset | 255 / 0 / 1 | 258 / 0 / 1 | 260 / 0 / 1 | | `=1` | not run | 258 / 0 / 1 | 260 / 0 / 1 | | `=2` | 246 / 9 / 1 | 258 / 0 / 1 | 260 / 0 / 1 | The 5 added are the tests above. Clippy `-p victory-gpu --all-targets -D warnings` is clean with and without `--all-features`. Not done: no hardware run. The failure paths are exercised only through the test hook, not by a real failed `Signal`. ### Other notes on this PR - @⁠kotekompotek, thanks for the review. On the synchronous wait in `memory::bind`'s `pull`: in our two title runs with `=2` (about 10 minutes each, RTX 5070), the computed-address bring-back was 0.00 MB/frame in every diagnostics line (103 and 106 lines). All bring-backs were marked pages: 79 and 87 MB/frame, in about 20 GPU copies a frame. So that wait didn't cost anything there. The marked-page bring-backs are the likelier cost in preparing. - @⁠pickles976, thanks for the vkd3d-proton run. You're right that this is no fix for loading RAM: keeping the system memory side is by design (see Limits). On `=1`: in an earlier title run it made no measurable difference to frame time, with 203 MB of read-only copies admitted. The gain we measured is at `=2`. The game results in the description are local title runs, counts and timings only. The tests above don't use the title. The two notes just above refer to the local title runs reported in the description. AI-assisted
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 05:34 UTC · original · edited 2026-11-26 07:19 UTC

Automated triage for the maintainers.

Thanks for the reply. The head is unchanged since our last comment and the reply agrees with the open points: the maintainer's choice between this design and #2120/#2130/#2222, and a hardware run of the current head on main alone (off vs =2). The verdict stays needs work; needs the maintainer's decision until those are settled.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 05:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/2993#issuecomment-7183603861) · edited 2026-11-26 07:19 UTC <!-- victory-triage v1 head=477c6c6fab2c799b8f4555361783c53706080364 verdict=needs-work at=2026-11-26T07:19:35Z --> Automated triage for the maintainers. Thanks for the reply. The head is unchanged since our last comment and the reply agrees with the open points: the maintainer's choice between this design and #2120/#2130/#2222, and a hardware run of the current head on `main` alone (off vs `=2`). The verdict stays **needs work; needs the maintainer's decision** until those are settled. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠r4tedx on GitHub · 2026-11-26 05:36 UTC · original

Thanks for the triage.

  1. Design. We'd welcome the maintainer's call between this and #2120/#2130/#2222. The main differences:

    • this keeps the system memory side current, through one CPU write path, so the CPU never reads video memory;
    • it admits copies by a DXGI margin, with residency demotion;
    • it brings shader writes back by marked pages.

    If this design is preferred, we'll split it as described:

    • (a) the write-only mapping and the vram module, opt-in;
    • (b) the single CPU write path;
    • (c) copy-back ordering and the bring-back, with their tests.

    If #2130's design is preferred, we're happy to offer the pieces that still apply there instead.

  2. A hardware run of the current head on main alone, off vs =2. Agreed, that's the right test. On our RTX 5070 it isn't possible yet: upstream main on its own (781e846d, with 219878a0) currently ends this title's load in a device removal. We haven't attributed that yet and will post what we find. The d2c20e28 and 477c6c6f changes have been tested on WARP only so far. The frame times in the description come from builds carrying other unmerged PRs, as it says.

Local title data was used for the runs mentioned here; only their outcome is given.

AI-assisted

**@⁠r4tedx** on GitHub · 2026-11-26 05:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/2993#issuecomment-7183640629) Thanks for the triage. 1. **Design.** We'd welcome the maintainer's call between this and #2120/#2130/#2222. The main differences: - this keeps the system memory side current, through one CPU write path, so the CPU never reads video memory; - it admits copies by a DXGI margin, with residency demotion; - it brings shader writes back by marked pages. If this design is preferred, we'll split it as described: - (a) the write-only mapping and the `vram` module, opt-in; - (b) the single CPU write path; - (c) copy-back ordering and the bring-back, with their tests. If #2130's design is preferred, we're happy to offer the pieces that still apply there instead. 2. **A hardware run of the current head on `main` alone, off vs `=2`.** Agreed, that's the right test. On our RTX 5070 it isn't possible yet: upstream `main` on its own (781e846d, with 219878a0) currently ends this title's load in a device removal. We haven't attributed that yet and will post what we find. The `d2c20e28` and `477c6c6f` changes have been tested on WARP only so far. The frame times in the description come from builds carrying other unmerged PRs, as it says. Local title data was used for the runs mentioned here; only their outcome is given. AI-assisted
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/2993/head:github-bot/gh-2993'
git switch 'github-bot/gh-2993'

Merge

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