gpu: experimental write-watched heap twin (VICTORY_HEAP_TWIN, off) #120

Closed
github-bot wants to merge 1 commit from github-bot/gh-119 into main AGit
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 13:17 UTC · original
SovietBearz0r:longroad-heap-twin → main · +933 −3 · 13 files · 1 commits · closed without merging

Experimental, off-by-default heap twin: VICTORY_HEAP_TWIN=1.

What

Ported from longroad commit d5207e99 (from Darner ea36ba9): the title's ~185 MB graphics heap becomes private write-watched memory, its last ~174 MB get a write-only twin in a GPU upload heap, buffers/windows placed there are placed in the twin, the placed-shadow refresh is skipped, and only CPU-written 4 KiB parts are copied up before each submission. New victory-gpu/src/heap_twin.rs (~709 L) plus xmem/watch/port/descriptor/list/queue wiring; stats on the frame line; the source's refusals kept (no twin if the kernel does not watch the memory or the driver has no GPU-upload heaps — the heap then stays private and copies normally).

Verification

  • heap_twin 10 unit tests (dirty parts → joined copy runs, sync pieces, coverage/skip logic, placement ranges); victory-gpu subsets green (watch 66, inplace 8, port 27, descriptor host 68, list::thin 6, queue 18, land 7); cargo test -p victory-hle --lib 272 passed; cargo check -p victory, cargo xtask policy, clippy: clean.
  • Requires in-game A/B before it can be enabled (not done here): author's data is NVIDIA only (RTX 5080, RT on, 4K DLSS Q: 19.9/21.0 vs 18.0 fps). Check AMD RX 9070 XT + NVIDIA with RT on, frame-line twin stats, no "no twin" warning, and pictures — the risks are corruption, not crashes.
  • Risks (kept in the file): write-watching the title's heap is a correctness gamble behind the title's submissions; unused title physical pages are not copied back; 174 MB VRAM unmetered; ReBAR needed; one port-specific risk: main's refresh merge window could theoretically take a page the twin needs (placed buffers are never refreshed, so it should not).

Source: Lesloo/longroad d5207e99, adapted to this tree's write-watch/port/queue machinery. Keep it opt-in; do not enable without a picture check.

**@⁠SovietBearz0r** on GitHub · 2026-12-05 13:17 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/119) `SovietBearz0r:longroad-heap-twin` → `main` · +933 −3 · 13 files · 1 commits · closed without merging Experimental, off-by-default heap twin: `VICTORY_HEAP_TWIN=1`. ## What Ported from longroad commit `d5207e99` (from Darner `ea36ba9`): the title's ~185 MB graphics heap becomes private write-watched memory, its last ~174 MB get a write-only twin in a GPU upload heap, buffers/windows placed there are placed in the twin, the placed-shadow refresh is skipped, and only CPU-written 4 KiB parts are copied up before each submission. New `victory-gpu/src/heap_twin.rs` (~709 L) plus xmem/watch/port/descriptor/list/queue wiring; stats on the frame line; the source's refusals kept (no twin if the kernel does not watch the memory or the driver has no GPU-upload heaps — the heap then stays private and copies normally). ## Verification - `heap_twin` 10 unit tests (dirty parts → joined copy runs, sync pieces, coverage/skip logic, placement ranges); `victory-gpu` subsets green (watch 66, inplace 8, port 27, descriptor host 68, list::thin 6, queue 18, land 7); `cargo test -p victory-hle --lib` 272 passed; `cargo check -p victory`, `cargo xtask policy`, clippy: clean. - **Requires in-game A/B before it can be enabled** (not done here): author's data is NVIDIA only (RTX 5080, RT on, 4K DLSS Q: 19.9/21.0 vs 18.0 fps). Check AMD RX 9070 XT + NVIDIA with RT on, frame-line twin stats, no "no twin" warning, and **pictures** — the risks are corruption, not crashes. - Risks (kept in the file): write-watching the title's heap is a correctness gamble behind the title's submissions; unused title physical pages are not copied back; 174 MB VRAM unmetered; ReBAR needed; one port-specific risk: main's refresh merge window could theoretically take a page the twin needs (placed buffers are never refreshed, so it should not). Source: Lesloo/longroad `d5207e99`, adapted to this tree's write-watch/port/queue machinery. Keep it opt-in; do not enable without a picture check.
Port of Lesloo/longroad d5207e9 (+869, 11 files), from Darner ea36ba9 by
trustno1-coder with ef01f05's joined askings, without its constant-buffer
alignment. The title's ~185 MB graphics heap (kind 0x42000, below 4 GiB)
becomes plain private memory the kernel watches for writes; the title's
splits, joins and page mappings in it do nothing. Its last ~174 MB get a twin
in a GPU upload heap: title buffers and windows there are placed in the twin,
their refreshes are left out, and before each submission (both sending paths,
the direct one and the send stage's) the 4 KiB parts the CPU wrote are copied
up. A thread of ours syncs ahead as the title submits, and CPU copies of ours
into the heap write through. Off, nothing changes.

Adapted to main rather than copied:

- `xmem` takes the stretch `reserve_low_window` keeps below 4 GiB and commits
  it with MEM_WRITE_WATCH; the parts are answered from the private range and
  the physical pages the title gives stay unused (the source's structure, on
  main's low-window machinery).
- The write watch is main's (`victory-gpu`'s `watch`: `writes`,
  `parts_written`, the reservation bounds registered with
  `title_memory_watched`), not the fork's copy of it.
- Main's shadow skips a refresh for anything placed, so the source's extra
  `refresh` refusal is not needed; placement itself goes through main's
  `port::place_in` (renderer port #1's path), which the twin takes first.
- The source's `Step::Run` "a step of ours wrote somewhere" invalidation is
  hooked once, in `watch::writing`, so every CPU write of ours into title
  memory (the direct copy steps, the placed copies, landings of the merge)
  forces the next sync; the data of the direct copy steps is written into the
  twin in `copy_mapped` as it lands in title memory.
- The shader-visible descriptor heap tracking is cut at the twin's start, as
  the source does.

Off by default as in the source (it is an experiment: see the risks listed in
`heap_twin.rs`, chiefly that our submissions run behind the title's and that
nothing on the CPU needs what shaders wrote in the heap; it needs a resizable
BAR and no other consumer may take the write-watch bits of a placed buffer's
pages). Ports' author data is NVIDIA (RTX 5080, RT on, 4K DLSS Q: 19.9/21.0
fps against 18.0 with the twin alone, V1CE's measurements): no AMD card has run
it yet, and an in-game A/B on an RX 9070 XT (fps + corruption) is still due.
Conflicts with renderer port #1 (`VICTORY_PORT_CONSTANTS`, the same heap) are
documented: the twin places first.

Tests: the twin's bookkeeping is unit-tested without a GPU or the title --
which 4 KiB parts of a 64 KiB page were written become the copy-up runs
(joined, cut to the asked range), the used chunks become the sync pieces (cut
at PIECE), the askings advance only where the kernel answered, only a
submission whose stamp is covered skips a sync, ranges and the twin
placement's offset and length, and the heap's sizes/pages. `cargo test -p
victory-gpu --lib`'s watch, inplace, port, descriptor::host, list::thin,
queue and land suites, and `cargo test -p victory-hle --lib`, pass; the
touched crates add no clippy warning, `cargo check -p victory` passes and
`cargo xtask policy` is clean.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 13:20:04 +00:00
Author
Owner

@⁠ReTerryDavis on GitHub · 2026-12-05 17:37 UTC · original

Closing this one — we're trying to avoid landing large, intrusive, unverified experimental switches (+933 lines across 13 core files), and VICTORY_HEAP_TWIN is hardcoded to a single magic heap allocation, untested on ReVIctory/AMD, allocates 174 MiB of unmetered GPU_UPLOAD VRAM, and has concrete correctness bugs:

  • Sync-bypass race (heap_twin.rs:360): OURS.swap(false, SeqCst) runs before acquiring SYNC_LOCK, allowing a second thread to observe ours == false && covered == true and submit GPU work before the first thread's CPU writes reach the twin.
  • Invalid low-window commit (xmem.rs:1073): VirtualAlloc2(..., MEM_COMMIT | MEM_WRITE_WATCH, ...) on a reserved placeholder is invalid (MEM_WRITE_WATCH requires MEM_RESERVE), causing it to fail and VirtualFree the reserved low window.
  • Openly drops GPU->CPU readbacks and risks overwriting shader-written bytes sharing a 4 KiB page.
**@⁠ReTerryDavis** on GitHub · 2026-12-05 17:37 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/119#issuecomment-7442340875) Closing this one — we're trying to avoid landing large, intrusive, unverified experimental switches (`+933` lines across 13 core files), and `VICTORY_HEAP_TWIN` is hardcoded to a single magic heap allocation, untested on ReVIctory/AMD, allocates 174 MiB of unmetered `GPU_UPLOAD` VRAM, and has concrete correctness bugs: - **Sync-bypass race (`heap_twin.rs:360`):** `OURS.swap(false, SeqCst)` runs *before* acquiring `SYNC_LOCK`, allowing a second thread to observe `ours == false && covered == true` and submit GPU work before the first thread's CPU writes reach the twin. - **Invalid low-window commit (`xmem.rs:1073`):** `VirtualAlloc2(..., MEM_COMMIT | MEM_WRITE_WATCH, ...)` on a reserved placeholder is invalid (`MEM_WRITE_WATCH` requires `MEM_RESERVE`), causing it to fail and `VirtualFree` the reserved low window. - Openly drops GPU->CPU readbacks and risks overwriting shader-written bytes sharing a 4 KiB page.
github-bot closed this pull request 2026-12-05 17:43:27 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.