Files
VIctory-NVIDIA/docs/memory-separation.md
cwWwWwW 56dd8d81ff GPU memory: record the audit of the memory split and the copy bridges
The previous two commits change where copies live and where they are made; this one writes
down what they were measured against, what they removed, and what a real-hardware run still
has to establish.

- docs/memory-separation.md: the platform model (console unified memory vs the host's two
  domains), the heap classes as the API reports them, ownership rules, how a copy is admitted
  (card_admission / window_admission and the budget lines), the placement ledger and how to
  read it, the direct-access path and its switches, the transfer and synchronisation
  contract, what main already fixes from other forks and what this work adds, and what only
  an NVIDIA run can establish.
- docs/memory-separation-window-budget.md: the window device-local budget (window_card_most,
  window_line, card_copy_fits, WINDOW_CARD_BYTES), what is charged to it - a second
  system-memory side is not (fixed here) - the by-address arena budget separation, the
  exception matrix, what the ledger shows for windows, and the hardware-only questions.
- docs/copy-bridges.md: the inventory and classification of every copy bridge: title buffers,
  windows, view-byte copies, by-address arenas, textures, targets/depth/read-backs, D3D12
  infrastructure, the title's own CPU steps and the experiment switches; the direct-access
  mechanism implemented in the previous commit (openability, SectionView, shared regions,
  place_fallback, declared placed writes, lifetime hooks, the switches); the copies that
  remain and why; the follow-ups; what a hardware run must establish; the verification
  record.

No source change: documentation only.
2026-11-29 21:59:04 +08:00

10 KiB

System RAM and GPU VRAM: the separation the layer works with

This is the audit the placement ledger and the direct-access work were written from, updated to the tree they are in. It says which memory belongs to whom, which heap class the API reports for every host buffer the layer makes, why copies are admitted the way they are, and where the two memory domains meet.

1. The platform model

The title is an Xbox Series title. On the console its GPU memory is unified memory: a GPU virtual address is a CPU address, and the title's own CPU steps read and write exactly what its shaders read and write.

On the PC the layer runs the title natively with a host GPU behind it, and the host has two memory domains: system RAM (what the CPU owns) and the card's local VRAM (what the GPU owns). The D3D12 copy machinery moves bytes between them:

  • D3D12_HEAP_TYPE_CUSTOM + write-back + L0: host buffers in system memory that the CPU maps and shaders read and write over the bus (Host::shared_buffer, CardCpu::allocate);
  • D3D12_HEAP_TYPE_DEFAULT: device-local memory (Host::uav_buffer);
  • UPLOAD / READBACK: the CPU-visible staging kinds (Host::upload_buffer, Host::read_back_buffer);
  • GPU_UPLOAD (resizable BAR): VRAM the CPU maps (Host::mapped_card_buffer).

"The console's memory" is not one of these. The layer reproduces it: xmem makes pagefile-backed sections for the title's physical pages, maps views of them at the title's addresses through placeholders, and the title runs against those views. So a title address is a view of a section page, and the section keeps the page alive while anything of ours holds a view of it (crate::inplace::SectionView).

2. Heap classes, as the API reports them

The placement ledger (crate::residency) asks every host buffer it sees which heap class the API reports for it: HeapClass and class_of call ID3D12Resource::GetHeapProperties. residency::placed counts every buffer made by the copy and transfer machinery in host.rs (shared, upload, read-back, unordered-access and CPU-mapped card buffers) by class and bytes. The five-second line prints the counts.

This is deliberately the API's answer, not a claim about physical placement: a DEFAULT allocation is counted as device-local because that is the heap class the API reports, not because the bytes are proven resident. D3D12 has no per-resource residency query, and WDDM may page a DEFAULT resource out. Nothing in this tree claims physical residency.

3. Ownership

  • CPU-owned, stays in system memory: the title's own allocations and their section views; host mirrors of title memory (CardCpu and the single-sided shared buffers); staging and read-back buffers; the descriptor-stride table and per-draw CPU-written scratch; the layer's UI and CPU bookkeeping.
  • GPU-owned, device-local: the card sides of title buffers and windows (Card with a DEFAULT buffer), the by-address arenas (ARENA_CARD_BYTES), textures and render targets the layer creates, depth twins, and the indirect-clamp buffers. Their contents are the card's; the layer keeps track of what changed and sends/fetches it.
  • Cross-domain transfers, explicit: every two-sided copy (shadow's refresh, sends, compare and copy-back; texture detiling staging; read-backs) and every directly placed buffer (below).
  • Directly placed title memory: a buffer placed over the title's own section page or private allocation (crate::inplace), behind switches (section 6). The GPU reads and writes the title's bytes in place; there is no host copy for it at all.
  • Not established from source: the driver's actual placement and pinning decisions (GetHeapProperties reports what was asked for, not where the bytes are).

4. How a copy is admitted

Title buffers (shadow::lookup) and windows (shadow::loose) ask shadow::card_admission / shadow::window_admission before anything is allocated: the cards switch, card_limits / window_card_most sizes, Host::card_ready, the DXGI budget line (card_room_kept_on 97% or 80% for buffers; window_line with the kept room and resource::room_line for windows; resource::card_copy_fits), and the path's own byte cap. A refusal is a reason (residency::Fallback), not silence, and the copy then stays in system memory.

The windows' device-local side is deliberately late for many windows: card_late gives a card side only to windows asked for in two or more frames, most asked first. A window that would get no card side this frame is not a refusal by the policy and is not placed directly before that decision; it is a copy in system memory until the late policy gives it a device-local side (or not).

The second memory sides are experiments, off by default: VICTORY_CARD_ALL gives every title buffer's copy a second system-memory side, and VICTORY_WINDOWS_OWN does it for windows. A second side is system memory. It is counted by the placement ledger, not against the device-local budget of its path (section 5).

5. The placement ledger

residency::placement_line prints, every 5 seconds while a copy in system memory is alive (or when VICTORY_MEMORY_REPORT is set; =0 silences it):

  • host buffers made since the start as the API reports them: count and MB by HeapClass;
  • copies whose GPU-visible side is system memory: alive count and MB by Fallback reason (switch off, not eligible, host unavailable, budget, cap, allocation failed, view copy), and the total made since the start;
  • directly backed copies: windows and title buffers alive, total made, and the refused direct asks by NoDirect reason (not openable, cap, host refused, host unavailable);
  • the system's DXGI local and shared usage/budget for comparison.

The line is the measurement the direct-access switches are evaluated with: run with the switches off, then on the same save, and compare what moved from the fallback classes to the directly backed counts and what still had to copy.

6. Direct access

crate::inplace opens the title's own memory as a host heap and places a buffer in it:

  • inplace::openability classifies a range: private memory committed whole, one contiguous run of one section (Openability::View), or a refusal (Unmapped, Scattered, Holes);
  • for a section run, a refcounted SectionView maps our own view of the run, OpenExistingHeapFromAddress1 opens a heap on that view, and a placed buffer covers the range; one view and one heap are shared per run (REGIONS, REGION_MOST 512 MiB), and the view keeps the section's pages alive after the title unmaps its own view and closes its handle;
  • for private memory, a committed part of the allocation is opened the same way. A heap over the title's private pages pins them, so the title's own VirtualFree/VirtualProtect there can fail while it stands; that is the documented private-placement exception, and why the page-pool view is the recommended route;
  • inplace::part_opening probes ID3D12Device13 once per process; without it every placement is refused as HostUnavailable and copies are made as before.

inplace::place_fallback is the policy: exactly where card_admission/window_admission refused a device-local side, it tries page-pool view memory (VICTORY_INPLACE_VIEW=1 windows, =2 title buffers) and then private memory (VICTORY_INPLACE=3). A placed copy makes no staging buffer, no mirror, no card side, no hash twin and no send/fetch/compare/copy-back state; the mirror remains the fallback for every refusal. Legacy VICTORY_INPLACE=1/=2 keep their old "place anywhere openable" meaning and renderer port 1 keeps VICTORY_PORT_CONSTANTS; those are not the migration policy and are unchanged.

7. Transfers and the synchronisation contract

For two-sided copies the contract is the copy machinery's: the CPU's mirror is refreshed before a submission, shader writes are declared (Shadow::gpu_writes), what changed goes up before the next submission, and what shaders wrote is found and copied back after the waits. For a directly placed copy:

  • shader writes are declared via HostList::defer_gpu_write, so the copy machinery's late waits order a CPU reader of those title bytes behind the flight that may write them;
  • the title's own VirtualFree/decommit/release path (watch::title_memory_freed), its VirtualProtect (intercepted unconditionally in xmem) and XMemFreeDefault's raw VirtualFree for 32-bit allocations let placed copies go before the kernel call;
  • a mapping change lets go of every placed copy over the range through the exact placed-copy registry (shadow::in_place_let_go), and the section view stays mapped while a placed buffer holds it;
  • the shortcut that predates this work stays: a host copy of the same bytes that a shader writes through a different copy is not told, because the card's writes are not seen by write watching. Placement does not widen it.

8. What this tree can and cannot establish

Local checks (cargo check --workspace --all-targets --locked, all features) and the software adapter (WARP) tests in CI establish the mechanics: the API's heap class for the buffers made, the placement data path both ways, the refcounted view's lifetime, the exact invalidation, the declaration of placed writes, and the fallback policy. They do not establish:

  • whether the target NVIDIA driver accepts OpenExistingHeapFromAddress1 on private allocations and on section views, what it pins and at what working-set cost;
  • physical residency, WDDM paging, and device-removal/TDR behaviour of the direct paths;
  • how much of the real title's memory is openable (the in-place census, VICTORY_INPLACE_PROBE=1, has not been recorded on a run);
  • the PCIe cost of GPU access to title memory in place against the copies it replaces.

Those need a run on the target hardware and driver; docs/copy-bridges.md section 9 states what to measure and what would make a switch a default.

9. Checks

  • cargo check --workspace --all-targets --locked: PASS (see docs/copy-bridges.md section 10 for the verification record, including the CI full-test run and its baseline comparison).
  • cargo check --workspace --all-targets --all-features --locked: the CI build check.
  • cargo test --workspace --locked: the CI test run.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: the tree's own gate; its pre-existing failures are unchanged by this work (the same lints at 829188e).