GPU: the Xbox's shared CPU/GPU memory on PC GPUs: simulated in video memory on discrete cards, title memory itself on unified ones #3102
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!3102
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3102"
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?
@0xkn0wh3r3 on GitHub · 2026-11-26 08:03 UTC · original
0xkn0wh3r3:gpu/host-memory-placement→main· +5529 −47 · 20 files · 23 commits · openGoal: give titles the Xbox Series' shared memory model on a PC whose GPU has its own video memory.
On the console, the CPU and GPU share one pool of memory. A title builds vertex data, constants and command data at an address, hands the GPU that address, and reads back what shaders wrote at that same address, with nothing copied. Titles are written around that.
A PC with a discrete GPU has two pools: system memory next to the CPU, and video memory on the card, joined by PCI Express. Each processor is fast in its own pool and slow in the other: on an RTX 5070 Ti, the GPU reads system memory at ~23 GB/s and video memory at ~720 GB/s.
This PR simulates the console's single pool on such a GPU:
Where the host really has one pool (an APU or integrated GPU), no simulation is needed: the GPU uses title memory itself, exactly as on the console.
Everything here is my own work: new code, plus my open PRs, which this replaces (#2120, #2130, #2222, #2870, #2912, #2921, #2929, #2945). It depends on no one else's PR.
How the shared pool is simulated
Why simulate instead of sharing literally on a discrete GPU? The GPU can read title memory itself across the bus (
VICTORY_IN_PLACE=1). Measured, that takes 43.7 ms per submission, against 29.7–36.7 ms for the simulated pool in video memory: shaders reading across PCI Express cost more than keeping the image coherent. So literal sharing is only the default where the pool really is shared.What each host gets by default
No environment variable is needed. The switches only override.
The startup log names the outcome:
host GPU memory: ..., plus a line when in place turns on or is refused.Overrides:
VICTORY_GPU_UPLOAD=0: every copy in system memory.VICTORY_GPU_UPLOAD_WRITES=0|1: written buffers' copies in system memory, or in video memory on any GPU.VICTORY_IN_PLACE=0|1: in place off, or on wherever the GPU can open title memory as a heap.The diagnostics ranking (from #2929) needs a
--features diagnosticsbuild plusVICTORY_GPU_TIMING=1 VICTORY_D3D12_DRED=1.Results
Synthetic:
shadow::bench::compute_heavy_submissions_by_copy_placementruns one submission of 200 compute dispatches in 8 lists over 320 MiB of title buffers, through the real copy paths (shadow::find,Shadow::refresh,Shadow::gpu_writes,HostList,Host::execute). Each placement runs in its own process, and the test checks that title memory ends up byte-identical in all of them. Ryzen 9 7900X, Windows 11. Each number is the median of three runs, each the median of 5 submissions, ms per submission.VICTORY_TEST_BENCH_LOOSE=1, 5070 Ti): 30.6-44.4 ms with its 4 MiB windows' copies in video memory, against 66.3-103.1 ms in system memory (−54 to −57%). The GPU's time falls from about 50 ms to 10 (2 with snapshots).VICTORY_IN_PLACE=1: 43.6-43.8 ms. That beats copies in system memory but loses to copies in video memory, so in place stays off on discrete GPUs.docs/gpu-memory.mdhas the full tables, every placement on both GPUs.What changes
Detecting the host (from #2912)
crate::unified::detectruns at host creation:ARCHITECTURE1.UMA/CacheCoherentUMA,GPUUploadHeapSupportedand the DXGI budgets. It costs about 0.3 ms and logs one line (host GPU memory: ...), which also goes into the GPU failure text.Copies in video memory on discrete GPUs (from #2120, #2130, #2222, #2870)
VICTORY_GPU_UPLOAD=0andVICTORY_GPU_UPLOAD_WRITES=0|1override.Title memory in place on unified hosts (new,
crate::in_place)OpenExistingHeapFromAddress1), with one buffer placed at its start.Shadowgains an in-place kind whosedatais title memory itself. Refresh, snapshot and copy-back do nothing for it.unified::self_test. The self-test runs once, only on unified hosts: it has the GPU copy into and out of memory allocated like the title's pools, and checks that both sides see the other's writes. That guards against layers that have the API but hand the GPU new memory, such as vkd3d-proton withoutVK_EXT_external_memory_host.VICTORY_IN_PLACE=1turns it on wherever heaps open;=0turns it off.Freeing memory held in place (from #2921, now with its caller)
VirtualFree,VirtualProtectandUnmapViewOfFile*with error 87.title_memory_change. That releases the buffers held over the range first, then retries for up to 100 ms while lists still holding them are reset.Withholding still works with in place
memory::withholding). A buffer in place cannot withhold anything, so:Diagnostics (from #2929)
VICTORY_GPU_TIMING=1 VICTORY_D3D12_DRED=1). Buffers in place aren't ranked.Limits
shadow::exact, ~2 KB each, in shared buffers) stay in system memory.Tests
shadow::tests::a_title_buffer_in_place_needs_no_copiesthe_title_frees_memory_a_buffer_is_in_place_overa_buffer_in_place_holds_its_heap_and_pinpage_tables_move_written_buffers_to_copieshost::withholding::a_program_using_title_memory_moves_a_buffer_in_place_to_a_copyin_place::tests::the_switch_decides_and_unset_follows_the_hostin_place::tests::a_host_that_never_opens_heaps_is_not_asked_againshadow::tests::loose_memory_is_in_video_memory_with_a_mirrorVICTORY_IN_PLACE=1; elsewhere tests keep the copies, so the copy machinery stays tested on unified hosts.dynamic_slots_are_held_until_releasedandlists_share_a_table_and_copies_keep_their_viewsfail the same way on clean main on this machine.VICTORY_TEST_ADAPTER=radeon): 271 of 271.victory-gpu/diagnostics.Related
@Ahmet-Dedeler on GitHub · 2026-11-26 08:04 UTC · original
A data point for the Mac side, since Apple Silicon is the obvious unified host: through Apple's D3DMetal (CrossOver 26.3, D3DMetal 4.0b3, M5 Max) this path won't turn on, and it can't as written.
UMA=false, sounified::detectwon't see a unified host even though it is one.OpenExistingHeapFromAddressreturns E_NOTIMPL,OpenExistingHeapFromAddress1E_FAIL, andOpenExistingHeapFromFileMapping(SEC_COMMIT and SEC_RESERVE) E_NOTIMPL. So the self-test should fail cleanly and fall back to copies, which is the right outcome.What does work there is the reverse direction: let D3DMetal allocate a mapped write-back buffer, then use
mach_vm_remap(copy=FALSE, fixed + overwrite) to put that buffer's pages at the title's own address. The title and the GPU then share the pages. It's written up with a standalone probe in pragyaangaur/VIctory-macOS#204, and pragyaangaur/VIctory-macOS#207 (merged there,VICTORY_ALIAS_BUFFERS=1) implements it behind a startup probe. It only covers buffers: Metal textures have their own swizzled storage, so textures keep their copies.So if
in_placehad a seam where the "open the title's pages as a heap + place a buffer" step can be swapped for "make a host buffer + alias it over the title's pages", the shadow side of this PR (the in-place kind with no refresh, snapshot or copy-back, the pinning, the withholding rules) looks like it would carry over to the Mac as is. I haven't tried this PR on the Mac myself.@Dukestar69 on GitHub · 2026-11-26 08:08 UTC · original · approved these changes
Tested locally merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):cargo test -p victory-gpu shadow::tests -- --test-threads=1(RTX 5090 & WARP): 24 passed, 0 failed on both (including all new video-memory mirror and in-place tests).cargo test -p victory-gpu draw::tests -- --test-threads=1(RTX 5090): 20 passed, 0 failed.cargo test -p victory-hle memoryapi&xmem: all tests passed.cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 199 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@Marvisak on GitHub · 2026-11-26 08:24 UTC · original
Automated triage for the maintainers.
Needs work, and needs the maintainer's decision. This merges cleanly, but it is 5,425 added lines in 20 files, folding in eight earlier PRs, and some of it is beyond what triage can pass:
docs/gpu-memory.md(529 lines of rationale, charts and measurement tables) goes against the repo's rule that docs stay short. Please cut it to a few lines on the switches (VICTORY_GPU_UPLOAD,VICTORY_GPU_UPLOAD_WRITES,VICTORY_IN_PLACE) and keep the measurements in the PR description.std::process::Command::new(std::env::current_exe())with environment switches (shadow.rs,in_place.rs,shadow/bench.rs). Spawning processes needs the maintainer's decision.crates/victory-hle/src/modules/memoryapi.rsnow interceptsVirtualFree,VirtualProtect,UnmapViewOfFileand their variants, retrying for up to 100 ms while the GPU side lets go of pinned heaps. That changes runtime behaviour for every title memory change, and it is only exercised synthetically ("No title has been run on either host").unified.rs), video-memory copies (budget.rs,heat.rs,shadow.rs), in place (in_place.rs,pinned.rs) and the HLE wrappers can each be reviewed and reverted on their own. Splitting them would make each reviewable. The PR is the replacement for #2120, #2130, #2222, #2870, #2912, #2921, #2929 and #2945, so those can be closed in its favour once a direction is chosen.CI (
check) was still running when this was written. What would change the verdict: the doc trimmed, the maintainer's call on the child-process tests and thememoryapioverrides, and ideally a split into reviewable pieces.Generated by Claude Code
@r4tedx on GitHub · 2026-11-26 09:02 UTC · original
Thanks for pulling #2120, #2130, #2222 and the rest into one PR. The lazy mirror and the in-place path for unified hosts are nice, and keeping read-only copies in a single buffer saves the RAM that #2993 spends. I read #3102 against #2993, which covers the same ground, and wrote synthetic tests for the places where the two differ. Below are only the findings those tests reproduce on #3102's own code, at
63ab7631.How the tests run. All of them are in one file,
crates/victory-gpu/src/shadow/tests/repro_3102.rs, on the branchr4tedx/VIctory:repro/3102-findings(commit47bd0cf3). That branch is one commit on top of #3102's63ab7631, with tests only and no fixes.They use your helpers (
mirrored_buffer,gpu_store,gpu_bytes,mark_entry).Each runs in a child process with the switches your own mirror tests set:
VICTORY_GPU_UPLOAD=1andVICTORY_GPU_UPLOAD_WRITES=1. That is the default on NVIDIA.They ran on WARP with
--features diagnostics --test-threads 1. WARP reports GPU upload heaps, so no test returns early. Each one asserts and printsadapter "Microsoft Basic Render Driver" vendor 0x1414: gpu_upload true gpu_upload_writes true.I added two hooks, both
#[cfg(test)], so they are compiled out of every other build:Host::copy_now, a switch that makes the copy not run;Shadow::copy_back_marked, a closure that runs just beforetaken.Nothing else in the production code changed.
Command:
VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --lib repro_3102 -- --test-threads 11. A failed bring-back loses a marked shader write on every side, and can write stale mirror bytes into title memory
Where:
host.rs:4127-4189.copy_nowreturns(). With no copier it returns silently (:4164). Errors fromReset,Close,SignalorSetEventOnCompletionare only logged (:4187). The result ofWaitForSingleObjectis ignored (:4182).shadow.rs:1376.bring_backclears the stale bits before it callscopy_now(:1385).shadow.rs:695and:705.copy_back_markedcompares the mirror with title memory, then alwayspushes the mirror into video memory.What goes wrong. When the copy that brings the shaders' writes back doesn't run:
pushwrites the mirror's old bytes over the shader's write in video memory too. The write is gone from title memory, the mirror and video memory.Tests:
e_a_failed_bring_back_keeps_the_shaders_writes(mirror current)e_a_failed_bring_back_keeps_the_shaders_writes_stale_mirrorEach test runs one submission. Its shaders write
0x5ainto a marked page and into a declared range, and the GPU copies after the submission do not run.Output
How #2993 handles it (
d2c20e28, from fuzeofficlal's review there):copy_nowreturns whether it saw the list end.a_failed_gpu_copy_of_shaders_writes_loses_none_of_them.You're welcome to take any of it. For a lazy mirror, the smallest change would be to clear the stale bits only after
copy_nowreports success, and to skip the copy-back's compare andpushfor pages whose pull failed.2. An old unmarked GPU write reaches title memory late, over a title write
Where:
shadow.rs:1275-1277: the shortcut, under which writes outside declared ranges and marked pages stay in video memory.shadow.rs:1304-1329:snapshots_aheadbrings back stale pages only.shadow.rs:483:keep_beforetakes the snapshot from the mirror.host.rs:1649:pull(&snapshots)after the submission brings back whole pages.shadow.rs:620: the merge.What goes wrong. An earlier shader writes a byte through a view no list declares or marks, as a shader without write marks does. The byte lands in video memory only and sets no stale bit. Later, the title writes that byte while a submission declares the page unmarked and its shader writes another byte there. The snapshot comes from the mirror, which lacks the old byte. The pull after the submission brings the old byte in, so the merge takes it for this submission's write and puts it over the title's byte. On main, and on #3102 with
VICTORY_GPU_UPLOAD_WRITES=0, the snapshot already holds the old byte, so the title's byte stays.Tests:
h_an_old_unmarked_write_does_not_reach_title_memory_late_with_mirrorsfails.h_..._in_system_memory(VICTORY_GPU_UPLOAD_WRITES=0), passes.Output
How #2993 handles it (
477c6c6f):Shadow::writingbrings the range back from video memory before taking the snapshot, wherever no snapshot covers it yet. Tests:an_old_unmarked_write_does_not_reach_title_memory_late_at_2and_at_0. With a lazy mirror, the equivalent is forsnapshots_aheadto pull the whole declared range, not only its stale pages, before the snapshot. That costs one GPU copy per unmarked range.3. With
VICTORY_COPIES_IN_VIDEO_MEMORYset as well (diagnostics), the CPU sides go to GPU upload heapsWhere:
host.rs:4208. The diagnostics override inshared_mapped_bufferapplies to everyshared_buffer. That includes:shadow.rs:1541);None => mirror,shadow.rs:1555);shadow.rs:1090).What goes wrong:
UPLOAD_POOL's budget.freshencopies video memory into video memory.It affects diagnostics builds only.
Tests:
f_with_both_switches_the_cpu_sides_stay_in_system_memory_mirrorf_with_both_switches_the_cpu_sides_stay_in_system_memory_kept_copy(_WRITES=0, the same branch a copy the budget refuses takes)Output
(heap type, CPU page property): 5 =
GPU_UPLOAD, 4/3 =CUSTOM/WRITE_BACK.How #2993 handles it (
d2c20e28): while its switch is on,Host::system_bufferalways makes CPU sides in the CUSTOM write-back L0 heap, and the diagnostics override no longer applies to them. Test:with_both_switches_the_cpu_sides_stay_in_system_memory.4. A title write during a marked copy-back misses the copy (main's window, now on both sides)
Where:
shadow.rs:676-708. The copy-back reads title memory, merges, writes title memory,pushes the mirror, and then callstaken(:708). Nothing re-reads title memory after the page is guarded again.copy_back_or_not(:620-638) has the same order.What goes wrong. A title write to the page after the copy-back's read and before
takenreaches neither the mirror nor video memory.takenthen counts the page as current, so the next refresh copies 0 bytes, and the GPU keeps the old byte.Tests:
c_a_title_write_during_a_copy_back_reaches_video_memory_with_mirrorsfails._WRITES=0,c_..._in_system_memoryfails the same way. So this is main's existing window, not something #3102 adds. With mirrors it covers both sides.Output
How #2993 handles it (
80aac1c3): after taking the page,settlereads title memory into the system side once more, and only then writes video memory. Test:a_title_write_during_a_copy_back_reaches_video_memory. It is lower priority than 1–3 because main has the same window.Not confirmed
copy_queuedto write the mirror alone. #3102 doesn't contain #2051, so I couldn't test that directly.d_refreshes_of_mirrored_copies_reach_video_memorypasses: it checks the first refresh, a title write, and a write under a busy range, read back by GPU copy.d_a_write_of_the_mirror_alone_is_seen_by_read_host_not_the_gpuwrites the mirror by hand, ascopy_queuedwritesself.data.read_hostthen reports0xc1, while the GPU side still has0x28.gpu_bytes, or adebug_assert!(self.vram.is_none())incopy_queued, would catch it.#3102 and #2993 overlap: two designs for the same feature, and only one can merge. Which one is the maintainer's call. Whichever lands, we'd be glad to help port the fixes and tests above onto it.
The title was not used in these tests.
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.