Repository navigation
Conversation
…t (opt-in) 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 Marvisak#2120 and Co-Authored-By: 0xkn0wh3r3 <360897260+0xkn0wh3r3@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…at was marked 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; Marvisak#693 and Belobug Marvisak#105 as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Independent Wine/vkd3d-proton 3.2.0 run on the RTX 4060 Laptop GPU (8 GiB), driver 590.48.1, exact head 1cf4dac, vendor masking disabled: |
kotekompotek
left a comment
There was a problem hiding this comment.
LGTM — verified the data-flow invariants end to end.
What I checked:
- Direction discipline: every CPU write path (refresh, unmapped zero-fill, copy-back merges via
settle) goes sysmem side thenput→BAR;write_hostgoes BAR-only (it's the "what shaders see" side); shader writes return viapull/pull_written→copy_now(src=buffer, dst=home). No path reads the BAR —WriteOnlymakes it unrepresentable, not just undocumented. - Ordering:
pull_writtenbeforecopy_back_marks(bitmaps preserved for it);settlere-reads title memory after re-taking the guard — theBEFORE_TAKENtest hook writes between the copy-back write and the re-take and asserts the byte survives in VRAM, which is the exact race the ordering exists for. - Admission:
limit= min(cap, 85%·budget − others),admitsadds 75% hysteresis,chargedcovers the window between DXGI readings, and thesize|classdestruction-callback packing is safe (low 16 bits free on 64KiB multiples). - Merge logic in
bring_back: overlapping/adjacent runs per copy join correctly; a marked page inside a declared range dedupes to the union. - Off-by-default: level 0 →
wanted→System →vram=Noneeverywhere;IN_VRAMearly-out means the pull paths cost one relaxed load when disabled. - Tests are discriminating: slot-parity across 8 submissions, class assertions per level, the budget table mirrors #693's actual numbers, and
gpu_byteschecks real GPU-side contents after each write path.
One note, non-blocking: pull inside memory::bind runs a synchronous fence wait per call while enabled — fine as designed (it's gated opt-in and the copies are what make it worthwhile), just worth watching in the diagnostics line (PULLED_BY[Computed]) on real runs.
fuzeofficlal
left a comment
There was a problem hiding this comment.
Code review on 1cf4dac8 (static/source inspection; no runtime GPU execution):
- P1 — Failed VRAM readback swallows errors and risks resetting an in-flight copy allocator (
crates/victory-gpu/src/host.rs:3750-3775,crates/victory-gpu/src/shadow.rs:1200-1208):- If copier initialization fails (
host.rs:3750),copy_buffersreturns early without copying. - At
host.rs:3765-3774,ExecuteCommandListsruns beforeSignalandSetEventOnCompletion, errors are only logged and discarded, andWaitForSingleObject's return value is ignored. - Because
pull_vram(shadow.rs:1202-1203) returns normally, callers proceed to read the stale system-memory mirror and clear shader-write dirty tracking (host.rs:1414-1434,memory.rs:684-697). Worse, ifSignalorSetEventOnCompletionfails afterExecuteCommandLists, the submitted copy list may still be in flight when the next call invokesc.allocator.Reset()athost.rs:3759. - Suggest propagating/checking confirmed fence completion before clearing dirty state or reusing the copy allocator, and falling back to system-memory copies if the copier cannot be initialized.
- If copier initialization fails (
- P2 — Dual VRAM switches can bypass the new budget in
diagnosticsbuilds (crates/victory-gpu/src/shadow.rs:1284-1299,crates/victory-gpu/src/host.rs:3645-3658):homeis allocated viahost.shared_buffer(size), which switches toD3D12_HEAP_TYPE_GPU_UPLOADwheneverVICTORY_COPIES_IN_VIDEO_MEMORYis set. With both flags enabled, the "system memory side" mirror already occupies VRAM outsidevram::make, and a budget refusal (None => (home, None)) still leaves the buffer in VRAM. Givinghomean explicit system-memory L0 heap path avoids this bypass.
- P2 — 250 ms cached DXGI usage can overshoot the 85% ceiling (
crates/victory-gpu/src/vram.rs:204-226,310-325):reading()adds onlylast.charged(newly admitted copies) duringMAX_AGE = 250msand does not observe concurrent texture/render-target VRAM allocations in the same window. Either refresh when other allocations consume headroom or document the 85% margin as approximate within the cache interval.
…eir budget From the review of Marvisak#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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
|
Thanks @fuzeofficlal for the careful read. All three points hold against P1: a failed bring-back copyYou're right on every count.
P2a: both switchesConfirmed: with diagnostics'
P2b: the cached readingAgreed. Within the 250 ms, the reading sees only our own charges.
Tests run with the switch set for the whole suiteWith
The fix is in the helpers, not the tests:
Every production copy-back already ran after its bring-back ( 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.
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 ResultsWARP,
The 5 added are the tests above. Clippy Not done: no hardware run. The failure paths are exercised only through the test hook, not by a real failed Other notes on this PR
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 |
|
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 CI: passing. Generated by Claude Code |
|
Thanks for the triage.
Local title data was used for the runs mentioned here; only their outcome is given. AI-assisted |
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 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 (GPU: copies of read-only title buffers in GPU upload heaps (opt-in) #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; GPU: copies of written title buffers in video memory with a mirror (opt-in) #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 GPU: copies of written title buffers in video memory with a mirror (opt-in) #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(gpu: move shadow buffers to DEFAULT VRAM with pooled staging transfers #693, Belobug Shaders: a loop that branches back to the entry instruction translates #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 gpu: move shadow buffers to DEFAULT VRAM with pooled staging transfers #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 (1cf4dac, on
main63bcca4;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 (main63bcca4 in 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 GPU: a partial refresh of an armed page updates the bytes saved of it #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(GPU: marked UAV refreshes cover whole 64 KiB pages #2172), so the video memory write after zeroing unmapped pages moved into its unmapped loop; andShadow::copynow callsTracked::refreshed(GPU: a partial refresh of an armed page updates the bytes saved of it #2377), kept, followed by the video memory write.Earlier, on the pre-rebase branch (mem/vram-copies 72338fe,
main13f91ca): 207 / 0 / 1, and the play/main port (82c8382): 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: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 82c8382,
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 deeb593, 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