GPU: host copies of title buffers in video memory, under a DXGI budget (opt-in) #2993
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2993
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2993"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@r4tedx on GitHub · 2026-11-26 02:38 UTC · original
r4tedx:mem/vram-copies-main→main· +1977 −49 · 7 files · 4 commits · openWhat 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 whenD3D12_OPTIONS16.GPUUploadHeapSupportedis 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:
The CPU never reads video memory. The BAR mapping is a
vram::WriteOnly, a type with only awritemethod and private fields, so no code outsidevram.rscan 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:
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:VICTORY_VRAM_COPIES_MBsets 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;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 underdiagnostics);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 beforecopy_back_marks);memory.rs(the bring-back before computed-address copy-backs);resource.rs(read-only buffers registered as such).Switches
VICTORY_VRAM_COPIES=1VICTORY_VRAM_COPIES=2VICTORY_VRAM_COPIES_MB=NUnset 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
copy_now's signal and wait; #2130 measured 1 page in 0.048 ms and 64 pages in 0.232 ms) plus the bytes.Shadow::copy, or write throughput, 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_COPIESset, 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 withVICTORY_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 isVramReadand a written one's isVramWritten. 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, onmain63bcca43;VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics --lib -- --test-threads=1):VICTORY_VRAM_COPIESunset: 255 passed / 0 failed / 1 ignored (main63bcca43in the same worktree: 246 / 0 / 1). The new tests set the switch in their child processes.--all-targets -D warningsis clean with and without--all-features.VICTORY_VRAM_COPIES=2set 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, fiveshadow::testson marks and snapshots, and #2377's newpartial_refresh_under_marked_range_updates_saved_bytes). They write the GPU side throughwrite_hostand 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 callpull_written, when the process-wide switch is set.Shadow::update_freebecameupdate_tracked(#2172), so the video memory write after zeroing unmapped pages moved into its unmapped loop; andShadow::copynow callsTracked::refreshed(#2377), kept, followed by the video memory write.Earlier, on the pre-rebase branch (mem/vram-copies 72338fe6,
main13f91cae): 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:13f91cae, in the same worktree and conditions (descriptor::host::tests::a_slot_rewritten_while_its_view_is_prepared_views_the_new_recordfailed in another).It passed 3 of 3 run alone. Another test's submission can refresh the shared copy between its
read_hostand itsclean()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:=1(vertex, index and constant data) and more with=2(heaps the shaders write).host copies in video memoryline gives MB held and the cap. Thevideo memory:line stays under the budget.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
mainas is. All arms below: 0 device removals.Against the same build with the switch off (DP1 → VC2; play/main-vram 82c8382b,
VICTORY_VRAM_COPIESoff vs=2, batched clamps off in both):=2Copies 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):=2(this PR)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
copy_now(ported, co-author on the commit), and the child-process test pattern.Overlap
This PR overlaps #2120, #2130 and #2222 (0xkn0wh3r3) directly:
#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:
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
@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_backpassed 1/1 and explicitly printedGPU 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=1first 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.@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 ind2c20e28on this branch. It also has a fix for the tests that failed withVICTORY_VRAM_COPIESset for the whole suite, and, in477c6c6f, a correctness fix we found ourselves (both below).P1: a failed bring-back copy
You're right on every count.
copy_nowreturned nothing, logged the errors, ignoredWaitForSingleObject, and reset the allocator on the next call even whenSignalhad failed afterExecuteCommandLists.Host::copy_now(host.rs:3736) now returns whether the copy's end was seen.Reset,Close) executes nothing, so the copier stays usable.ExecuteCommandLists, a failedSignalmeans 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. IfSetEventOnCompletionor the wait fails, it reads the fence instead, for up to 10 s. A fence ofu64::MAX(removed device) doesn't count as the end.fail(host.rs:3796), as after a submission, so it isn't hidden.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::makeadmits none at all (vram.rs:361).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.shadow::tests::a_failed_gpu_copy_of_shaders_writes_loses_none_of_themruns 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:copy_nowrefuses from then on, and the next copy made stays in system memory.P2a: both switches
Confirmed: with diagnostics'
VICTORY_COPIES_IN_VIDEO_MEMORYset,shared_buffermadehomein a GPU upload heap, so a copy the budget refused stayed in video memory too.Host::system_buffer(host.rs:3662) takes the system memory heap main's copies use (CUSTOM, write-back, L0).shared_bufferuses it unless main's switch is on.VICTORY_VRAM_COPIESon, 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 throughvram::makeand its budget.shadow::tests::with_both_switches_the_cpu_sides_stay_in_system_memoryruns 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.
vram.rs:22).vram.rs:373). A copy is made once per title buffer, so this costs oneQueryVideoMemoryInfoper copy made near the line. Far from the line, the kept reading is still used.vram::tests::a_kept_reading_near_the_line_is_taken_again, with a faked DXGI answer, kept as DXGI's is:Tests run with the switch set for the whole suite
With
VICTORY_VRAM_COPIES=2set for the whole suite, 9 of main's in-process tests failed on1cf4dac8. 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 thepull_writtena submission runs:shadow::tests::marked_pages_go_back_by_the_bytes_the_gpu_changedmarks_of_shaders_using_title_memory_are_held_backpartial_refresh_under_marked_range_updates_saved_bytessnapshots_cover_the_declared_ranges_and_writes_go_back_oncetitle_writes_under_a_busy_range_are_copiedwhat_a_frame_withholds_leaves_the_copies_at_its_endThree read the wrong side. These went through real submissions:
bindless::tests::heap_views_are_currentheap_views_follow_writes_through_another_mappingdraw::tests::root_views_reach_their_copy_and_read_zeros_past_itTheir 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_hostread 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_systemreads the system memory side.write_host(shadow.rs:210) writes as a submission that has ended: the GPU side, then its bytes back, aspull_writtendoes.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.rspull_writtenbeforecopy_back_marksandcopy_back/keep_writes;memory.rspullbeforecopy_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_0as 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 at0. With the new bring-back switched off, the=2test 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_COPIES1cf4dac8d2c20e28477c6c6f=1=2The 5 added are the tests above. Clippy
-p victory-gpu --all-targets -D warningsis 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
memory::bind'spull: 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.=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
@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
mainalone (off vs=2). The verdict stays needs work; needs the maintainer's decision until those are settled.CI: passing.
Generated by Claude Code
@r4tedx on GitHub · 2026-11-26 05:36 UTC · original
Thanks for the triage.
Design. We'd welcome the maintainer's call between this and #2120/#2130/#2222. The main differences:
If this design is preferred, we'll split it as described:
vrammodule, opt-in;If #2130's design is preferred, we're happy to offer the pieces that still apply there instead.
A hardware run of the current head on
mainalone, off vs=2. Agreed, that's the right test. On our RTX 5070 it isn't possible yet: upstreammainon its own (781e846d, with219878a0) currently ends this title's load in a device removal. We haven't attributed that yet and will post what we find. Thed2c20e28and477c6c6fchanges 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
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.