upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/memory-separation.md
blob: 0120e60001bd57395269c3c281ec55cd9cebee2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# 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: `DARNER_CARD_ALL` gives every title
buffer's copy a second system-memory side, and `DARNER_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` reports active copies in system memory, direct placements and an
active graphics heap twin. `DARNER_MEMORY_REPORT=1` forces the line and `=0` silences it.
It includes:

- 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);
- graphics heap twin: the size of its single `GPU_UPLOAD` allocation. The CPU copies changed
  pages into it before submissions, as reported by the twin's own per-frame line;
- 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 (`DARNER_INPLACE_VIEW=1` windows,
`=2` title buffers) and then private memory (`DARNER_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 `DARNER_INPLACE=1`/`=2` keep their old
"place anywhere openable" meaning and renderer port 1 keeps `DARNER_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, `DARNER_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`).