victory-gpu: a copy of title memory in video memory, prepared and tested #3295

Open
github-bot wants to merge 2 commits from github-bot/gh-3295 into main AGit
Owner

@⁠pes7 on GitHub · 2026-11-26 16:16 UTC · original
pes7:perf/vram-copies → main · +564 −0 · 2 files · 2 commits · open

What this is

shadow's copies of title memory live in system memory — Host::shared_buffer is a CUSTOM heap buffer with CPU_PAGE_PROPERTY_WRITE_BACK and MEMORY_POOL_L0 — because the title's memory is host memory and the copy has to be something the CPU can refresh and compare. The cost is that every shader read of game data crosses PCIe, where on the console the same read is GDDR6.

This PR is the mechanism for the other arrangement: the copy a shader reads lives in video memory (D3D12_HEAP_TYPE_DEFAULT), kept up to date by a copy per changed range and read back through a readback buffer.

VramCopy::new(len)               // a buffer in video memory, ALLOW_UNORDERED_ACCESS, common
.upload(list, offset, bytes)     // staged in upload memory now, copied when the list runs
.read(list, offset, len) -> Read // a readback recorded on the list, read after it ran
.gpu()                           // the address a shader reads it at

Why it is not wired in

Deliberately: the integration is a rewrite of Shadow's write path (Batch::flush stages instead of memcpying, Shadow::copy_back_page reads through read), and a rewrite nobody can A/B is worth nothing. Landing the unit first means the next PR is a measured before/after with this underneath it. The module doc lists the four places the integration touches, and #![allow(dead_code)] says so at the top of the file.

What the tests are, and what they caught

The six tests are the semantics the integration needs. They run on any host GPU (and skip where there is none or no shader compiler), so CI keeps them honest:

test what it holds
uploaded_bytes_reach_a_shader_and_its_result_reads_back the staged bytes reach a shader that reads the copy by GPU address, and its result reads back
what_a_shader_writes_reads_back what the write-back path would compare is what the shader wrote
two_ranges_in_one_list_do_not_overwrite_each_other two staged ranges of one list land where they were asked for
an_upload_reads_back_without_a_shader the copy path alone: bytes in, bytes out
a_range_past_the_end_is_refused the edges (last element fits, one past does not, empty is refused)
a_copy_is_whole_pages the length is whole 64 KiB and the address is aligned

The first version was wrong, and the tests said why. upload/read copied from COMMON; the readback came back zeroed — the shader read the buffer before the copy landed. A copy from COMMON is not ordered against a shader access on either side of it. Both operations now enter and leave an explicit state:

COMMON -> COPY_DEST   -> copy  -> COPY_DEST   -> COMMON
COMMON -> COPY_SOURCE -> copy  -> COPY_SOURCE -> COMMON

which is the shape capture.rs already uses for its readback, and the reason its comment says a title buffer's copy, never transitioned, is only ever read.

Verification

cargo test --release -p victory-gpu       237 passed, 1 ignored   (six new)
cargo clippy -p victory-gpu --all-targets -- -D warnings        clean

No frame-time claim: nothing in the frame path changes.

Where this can go

If it pays, the order that makes it measurable is: (1) this unit, (2) Shadow with Option<VramCopy> for copies above a size and within a VRAM budget, behind a switch, (3) the same scene twice. If it does not pay, the switch stays off and the module stays as the documented reason why — which is also worth having, because VICTORY_COPIES_IN_VIDEO_MEMORY's result (worse, 3 196 -> 4 289 ms) is easy to misread as "video memory does not help".

**@⁠pes7** on GitHub · 2026-11-26 16:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/3295) `pes7:perf/vram-copies` → `main` · +564 −0 · 2 files · 2 commits · open ## What this is `shadow`'s copies of title memory live in **system memory** — `Host::shared_buffer` is a `CUSTOM` heap buffer with `CPU_PAGE_PROPERTY_WRITE_BACK` and `MEMORY_POOL_L0` — because the title's memory is host memory and the copy has to be something the CPU can refresh and compare. The cost is that every shader read of game data crosses PCIe, where on the console the same read is GDDR6. This PR is the mechanism for the other arrangement: the copy a shader reads lives in video memory (`D3D12_HEAP_TYPE_DEFAULT`), kept up to date by a copy per changed range and read back through a readback buffer. ```rust VramCopy::new(len) // a buffer in video memory, ALLOW_UNORDERED_ACCESS, common .upload(list, offset, bytes) // staged in upload memory now, copied when the list runs .read(list, offset, len) -> Read // a readback recorded on the list, read after it ran .gpu() // the address a shader reads it at ``` ## Why it is not wired in Deliberately: the integration is a rewrite of `Shadow`'s write path (`Batch::flush` stages instead of memcpying, `Shadow::copy_back_page` reads through `read`), and a rewrite nobody can A/B is worth nothing. Landing the unit first means the next PR is a measured before/after with this underneath it. The module doc lists the four places the integration touches, and `#![allow(dead_code)]` says so at the top of the file. ## What the tests are, and what they caught The six tests are the semantics the integration needs. They run on any host GPU (and skip where there is none or no shader compiler), so CI keeps them honest: | test | what it holds | |---|---| | `uploaded_bytes_reach_a_shader_and_its_result_reads_back` | the staged bytes reach a shader that reads the copy by GPU address, and its result reads back | | `what_a_shader_writes_reads_back` | what the write-back path would compare is what the shader wrote | | `two_ranges_in_one_list_do_not_overwrite_each_other` | two staged ranges of one list land where they were asked for | | `an_upload_reads_back_without_a_shader` | the copy path alone: bytes in, bytes out | | `a_range_past_the_end_is_refused` | the edges (last element fits, one past does not, empty is refused) | | `a_copy_is_whole_pages` | the length is whole 64 KiB and the address is aligned | **The first version was wrong, and the tests said why.** `upload`/`read` copied from `COMMON`; the readback came back zeroed — the shader read the buffer before the copy landed. A copy from `COMMON` is not ordered against a shader access on either side of it. Both operations now enter and leave an explicit state: ``` COMMON -> COPY_DEST -> copy -> COPY_DEST -> COMMON COMMON -> COPY_SOURCE -> copy -> COPY_SOURCE -> COMMON ``` which is the shape `capture.rs` already uses for its readback, and the reason its comment says a title buffer's copy, never transitioned, is only ever read. ## Verification ``` cargo test --release -p victory-gpu 237 passed, 1 ignored (six new) cargo clippy -p victory-gpu --all-targets -- -D warnings clean ``` No frame-time claim: nothing in the frame path changes. ## Where this can go If it pays, the order that makes it measurable is: (1) this unit, (2) `Shadow` with `Option<VramCopy>` for copies above a size and within a VRAM budget, behind a switch, (3) the same scene twice. If it does not pay, the switch stays off and the module stays as the documented reason why — which is also worth having, because `VICTORY_COPIES_IN_VIDEO_MEMORY`'s result (worse, 3 196 -> 4 289 ms) is easy to misread as "video memory does not help".
`shadow`'s copies of title memory live in system memory (`Host::shared_buffer`
is a `CUSTOM`/`L0` buffer): the title's memory is host memory, so the copy is
something the CPU refreshes and compares, and every shader read of it crosses
PCIe. On the console the same read is GDDR6. The one experiment so far
(`VICTORY_COPIES_IN_VIDEO_MEMORY`) put them in a `GPU_UPLOAD` heap and measured
*worse* (3 196 -> 4 289 ms a frame in the world): that is a BAR window, not
video memory.

This is the mechanism for the other arrangement, small enough to review on its
own and not wired into the frame path:

    VramCopy::new(len)                    a buffer in D3D12_HEAP_TYPE_DEFAULT
    upload(list, offset, bytes)           staged now, copied when the list runs
    read(list, offset, len) -> Read        a readback after the list ran
    gpu()                                 the address a shader reads

Nothing is committed to using it yet - `#![allow(dead_code)]` and the module
doc say where it plugs in (`Shadow` gaining an `Option<VramCopy>`, `Batch::flush`
staging instead of memcpying, `Shadow::copy_back_page` reading through `read`).
Landing it separately means the integration PR can be a measured before/after
rather than a rewrite nobody can A/B.

The tests are the semantics the integration depends on (and they are what found
the one non-obvious rule):

* `uploaded_bytes_reach_a_shader_and_its_result_reads_back` - bytes the host
  stages arrive at a shader that reads the copy by GPU address, and the
  shader's result reads back;
* `what_a_shader_writes_reads_back` - what a shader writes is what the
  write-back path would see;
* `two_ranges_in_one_list_do_not_overwrite_each_other` - staged ranges are not
  shared behind the caller's back;
* `a_range_past_the_end_is_refused`, `a_copy_is_whole_pages`,
  `an_upload_reads_back_without_a_shader` - the edges, and the copy path with
  no shader in it at all.

**What the tests found:** a copy from `COMMON` is not ordered against a shader
access on either side of it. The first version of `upload`/`read` copied from
`COMMON` and the readback came back zeroed - the shader read the buffer before
the copy landed. Both now enter and leave an explicit state
(`COMMON -> COPY_DEST -> COMMON`, `COMMON -> COPY_SOURCE -> COMMON`), which is
the same shape `capture.rs` uses for its readback and the reason its comment
says a title buffer's copy, never transitioned, is only ever read.

Verification: `cargo test --release -p victory-gpu` - 237 passed, 1 ignored
(the new six among them); clippy `--all-targets -D warnings` clean on `main`.

No frame-time claim: nothing in the frame path changes here.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:21:57 +00:00
A synthetic measurement, no title involved: the same copy of 128 MiB read by a
compute shader, once from video memory (`VramCopy`) and once from the system
memory `shadow`'s copies live in today (`Host::shared_buffer`), with two
access shapes - a stride-1 scan and a random 16-byte line a thread - one 16-byte
read a thread and no stores, so the timing is the reads.

Measured here (RX 9070 XT, shared machine, best of three, ten dispatches a
list):

    128 MiB read        video     system    ratio
    stride-1 scan      3.829ms    4.021ms   1.05x
    random lines      56.301ms   57.100ms   1.01x

Earlier shapes agreed before the parallism was raised (1.21x / 1.04x), and the
one in-game experiment that exists - `VICTORY_COPIES_IN_VIDEO_MEMORY`, a
`GPU_UPLOAD` heap - measured *worse* (3 196 -> 4 289 ms a frame).

So: on this machine, putting these copies in video memory does not make
shader reads faster, in either access shape. Two readings of that, and both
matter before anyone writes the integration:

* either the reads are not what the frame spends its time on (then variant A
  would have bought nothing, and the frame's cost is elsewhere), or
* the `CUSTOM`/`L0` copies already come back at PCIe speed on this machine
  (~32 GB/s measured) and video memory has no headroom left to give.

Either way the number that decides the integration now exists, and it says no.
The unit stays (its own tests hold the mechanism), and this test is the record
of why it is not wired in - and the thing to re-run on a machine whose result
differs.

Verification: `cargo test --release -p victory-gpu` - 238 passed, 1 ignored;
`clippy --all-targets -D warnings` clean.
Author
Owner

@⁠pes7 on GitHub · 2026-11-26 16:29 UTC · original

Update: the measure that decides the integration is in, and it says no

The two commits before this one prepared the mechanism; this one answers whether it is worth integrating, on the machine, in seconds, without the title: the same 128 MiB copy read by a compute shader, once from video memory (VramCopy) and once from the system memory shadow's copies live in today (Host::shared_buffer), with two access shapes — a stride-1 scan, and a random 16-byte line a thread — one read a thread and no stores, so the timing is the reads.

128 MiB read        video     system    ratio
stride-1 scan      3.829ms    4.021ms   1.05x
random lines      56.301ms   57.100ms   1.01x

(Earlier, lower-parallelism shapes agreed: 1.21x / 1.04x. Best of three, ten dispatches a list, RX 9070 XT on a shared machine.)

So video memory buys nothing here. Two readings, and both matter:

  1. the reads are not what the frame spends its time on — then variant A would have bought nothing and the cost is elsewhere; or
  2. the CUSTOM/L0 copies already come back at PCIe speed on this machine (~32 GB/s measured) and there is no headroom for video memory to give.

Either way, the number that decided whether to rewrite Shadow's write path now exists, and it does not justify a week of it. The one in-game datapoint agrees: VICTORY_COPIES_IN_VIDEO_MEMORY (a GPU_UPLOAD heap) measured worse, 3 196 → 4 289 ms.

What stays useful:

  • the unit and its six semantics tests (a copy of title memory that lives in video memory has to be ordered by explicit transitions around its copies — that is what the tests found, and it will bite whoever wires it later);
  • the benchmark itself, which is four lines to re-run on any other machine (cargo test -p victory-gpu --release the_read_cost -- --nocapture) and is the honest way to decide this on hardware where the answer may differ;
  • the note in the module doc of where the integration plugs in, if it ever does.

cargo test --release -p victory-gpu — 238 passed, 1 ignored; clippy --all-targets -D warnings clean.

**@⁠pes7** on GitHub · 2026-11-26 16:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3295#issuecomment-7197723197) ## Update: the measure that decides the integration is in, and it says no The two commits before this one prepared the mechanism; this one answers whether it is worth integrating, **on the machine, in seconds, without the title**: the same 128 MiB copy read by a compute shader, once from video memory (`VramCopy`) and once from the system memory `shadow`'s copies live in today (`Host::shared_buffer`), with two access shapes — a stride-1 scan, and a random 16-byte line a thread — one read a thread and no stores, so the timing is the reads. ``` 128 MiB read video system ratio stride-1 scan 3.829ms 4.021ms 1.05x random lines 56.301ms 57.100ms 1.01x ``` (Earlier, lower-parallelism shapes agreed: 1.21x / 1.04x. Best of three, ten dispatches a list, RX 9070 XT on a shared machine.) **So video memory buys nothing here.** Two readings, and both matter: 1. the reads are not what the frame spends its time on — then variant A would have bought nothing and the cost is elsewhere; or 2. the `CUSTOM`/`L0` copies already come back at PCIe speed on this machine (~32 GB/s measured) and there is no headroom for video memory to give. Either way, the number that decided whether to rewrite `Shadow`'s write path now exists, and it does not justify a week of it. The one in-game datapoint agrees: `VICTORY_COPIES_IN_VIDEO_MEMORY` (a `GPU_UPLOAD` heap) measured **worse**, 3 196 → 4 289 ms. What stays useful: * the unit and its six semantics tests (a copy of title memory that lives in video memory has to be ordered by explicit transitions around its copies — that is what the tests found, and it will bite whoever wires it later); * the benchmark itself, which is four lines to re-run on any other machine (`cargo test -p victory-gpu --release the_read_cost -- --nocapture`) and is the honest way to decide this on hardware where the answer may differ; * the note in the module doc of where the integration plugs in, if it ever does. `cargo test --release -p victory-gpu` — 238 passed, 1 ignored; clippy `--all-targets -D warnings` clean.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 17:10 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP):

  • Standalone vram.rs module cleanly implements and tests VRAM-backed title memory copies (whole-page rounding, out-of-range guards, upload/shader/readback paths, and disjoint ranges in one list).
  • cargo test -p victory-gpu --lib vram:: -- --test-threads=1 (RTX 5090): 7 passed, 0 failed.
  • VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib vram:: -- --test-threads=1 (WARP): 7 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 17:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3295#pullrequestreview-6078118620) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP): - Standalone `vram.rs` module cleanly implements and tests VRAM-backed title memory copies (whole-page rounding, out-of-range guards, upload/shader/readback paths, and disjoint ranges in one list). - `cargo test -p victory-gpu --lib vram:: -- --test-threads=1` (RTX 5090): 7 passed, 0 failed. - `VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib vram:: -- --test-threads=1` (WARP): 7 passed, 0 failed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 192 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3295/head:github-bot/gh-3295'
git switch 'github-bot/gh-3295'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3295'
git switch 'github-bot/gh-3295'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3295'
git switch 'github-bot/gh-3295'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3295'
git switch 'main'
git merge --squash 'github-bot/gh-3295'
git switch 'main'
git merge --ff-only 'github-bot/gh-3295'
git switch 'main'
git merge 'github-bot/gh-3295'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3295
No description provided.