Skip to content

GPU: copies of read-only title buffers in GPU upload heaps (opt-in) - #2120

Draft
0xkn0wh3r3 wants to merge 2 commits into
Marvisak:mainfrom
0xkn0wh3r3:gpu/upload-heap-copies
Draft

0xkn0wh3r3 wants to merge 2 commits into
Marvisak:mainfrom
0xkn0wh3r3:gpu/upload-heap-copies

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown

The problem

Shaders read title buffers about 30 times slower than the console's GPU does.

Host copies of title buffers are in system memory (Host::shared_buffer: a custom heap, write-back, memory pool L0), so that the CPU can refresh them and read back what shaders write. Every shader read of a copy then crosses PCIe. On an RTX 5070 Ti a shader reads 256 MiB from such a copy at 22.9 GB/s, and from video memory at 713 GB/s. The console's memory is one pool, and the GPU reads it at video memory speed.

The fix

A buffer the title makes without unordered access is one shaders only read: nothing reads its copy back, and the CPU only writes it (refreshes). Its copy can be in video memory the CPU writes through the PCI BAR: a GPU upload heap (D3D12_HEAP_TYPE_GPU_UPLOAD, D3D12_OPTIONS16, on hosts with resizable BAR).

With VICTORY_GPU_UPLOAD=1:

  • shadow::register_read_only registers such buffers (resource.rs, from the title's ALLOW_UNORDERED_ACCESS flag), and shadow::find makes their copies with Host::gpu_upload_buffer.
  • Copies past 2 GiB of video memory, and every copy on hosts without GPU upload heaps, stay in system memory, as before.
  • Shaders that still write such a copy (through a view of the same memory under another resource) get it right, slowly: the copy-back reads video memory at ~50 MB/s. A warning names the first one.

Measured on an RTX 5070 Ti (driver 621.24, Ryzen 9 7900X), 256 MiB, through shadow::find (reading_copies_in_video_and_system_memory):

copy shader reads refresh writes (first, every page)
writable buffer's (system memory) 11.74 ms, 22.9 GB/s 42.1 ms
read-only buffer's (GPU upload heap) 0.38 ms, 713 GB/s 32.8 ms

Off by default: how much of a world frame's GPU time this is depends on which buffers the title makes without unordered access, which needs a run of the title to measure.

How to try it

Set VICTORY_GPU_UPLOAD=1 before victory run. The log says whether it is on (host copies that shaders only read in GPU upload heaps). It needs resizable BAR enabled (Windows 11 or a recent Agility SDK runtime). A world-frame comparison with and without it (GPU time per frame, VICTORY_GPU_TIMING=1) is what would tell whether to turn it on by default.

Testing

  • New test read_only_buffers_get_their_copies_in_video_memory (in a child process with the switch on): checks that a read-only buffer's copy is in a GPU upload heap, that the GPU reads what a refresh wrote there, and that a writable buffer's copy stays in system memory.
  • reading_copies_in_video_and_system_memory is the benchmark behind the table above (ignored by default; run in release with --ignored --nocapture).
  • cargo test -p victory-gpu --lib, with and without VICTORY_GPU_UPLOAD=1: 142 passed, the same 2 failing as on main on this GPU (NVIDIA D3D12 (70564ae): stride_tests & synthetic_tests fail due to HEAP_VIEWS (2M > 1M NVIDIA limit) and Stride::Structured with rawBufferLoad #1835). The victory-gpu tests give the same results with the switch on and off.
  • cargo clippy -p victory-gpu --all-targets with and without --features diagnostics: clean.
  • Not run with a title (no game data here).

Related

Host copies of title buffers are in system memory (`Host::shared_buffer`:
a custom heap, write-back, memory pool L0) so that the CPU can refresh
them and read back what shaders write. Every shader read of a copy then
crosses PCIe. On an RTX 5070 Ti a shader reads 256 MiB from such a copy
at 22.9 GB/s, and from video memory at 713 GB/s: the console's memory is
one pool, and the GPU reads it at video memory speed.

A buffer the title makes without unordered access is one shaders only
read: nothing reads its copy back, and the CPU only writes it (refreshes).
Its copy can be in video memory the CPU writes through the PCI BAR: a GPU
upload heap (`D3D12_HEAP_TYPE_GPU_UPLOAD`, `D3D12_OPTIONS16`, on hosts
with resizable BAR). With `VICTORY_GPU_UPLOAD=1`:

- `shadow::register_read_only` registers such buffers (`resource.rs`, from
  the title's `ALLOW_UNORDERED_ACCESS` flag), and `shadow::find` makes
  their copies with `Host::gpu_upload_buffer`;
- copies past 2 GiB of video memory, and every copy on hosts without GPU
  upload heaps, stay in system memory, as before;
- shaders that still write such a copy (through a view of the same memory
  under another resource) get it right, slowly: the copy-back reads video
  memory at ~50 MB/s; a warning names the first one.

Measured on an RTX 5070 Ti (driver 621.24, Ryzen 9 7900X), 256 MiB,
through `shadow::find` (`reading_copies_in_video_and_system_memory`):

| copy | shader reads | refresh writes (first, every page) |
|---|---|---|
| writable buffer's (system memory) | 11.74 ms, 22.9 GB/s | 42.1 ms |
| read-only buffer's (GPU upload heap) | 0.38 ms, 713 GB/s | 32.8 ms |

Off by default: how much of a world frame's GPU time this is depends on
which buffers the title makes without unordered access, which needs a
run of the title to measure.

`read_only_buffers_get_their_copies_in_video_memory` (in a child process
with the switch on) checks that a read-only buffer's copy is in a GPU
upload heap, that the GPU reads what a refresh wrote there, and that a
writable buffer's copy stays in system memory. The victory-gpu tests
give the same results with the switch on and off.
@desplmfao

Copy link
Copy Markdown

Thanks for the bandwidth numbers. The 22.9 vs 713 GB/s comparison is very useful. Some context from the #2112 side, in case it helps decide how the two fit together:

  • GPU: title memory in place instead of copied, opt-in (VICTORY_IN_PLACE=1; stacked on #2088, towards #880) #2112 reads over the same path as the copies on main today: those already live in shared_buffer (custom heap, L0, system memory). So in place doesn't make shader reads slower than now. It only removes the copy and its refreshes. This PR is the one that makes reads faster.
  • They pull in different directions on memory. On a 16 GB RTX 5080 under Wine, video memory is currently what runs out first: every run of mine fills it while the world loads, before the first world frame. Title buffers here are about 1.2 GB, so moving the read-only ones into GPU upload heaps would hit that limit sooner on cards like this one. On cards with more headroom, your path is the better one.
  • So they look complementary rather than competing. Per buffer: a GPU upload copy while video memory has room (QueryVideoMemoryInfo budget), and in place otherwise. Both touch shadow::find, so whichever lands second will need a small rebase. I'm happy to do that on my side.

Not measured on my side: the read-only share of those 1.2 GB. Your register_read_only would give that directly if a run logs it.

@Dathinel

Copy link
Copy Markdown
Contributor

The 2 GiB allowance currently counts successful allocations across the lifetime of the process. On e8df416 I repeatedly registered one synthetic 32 MiB read-only buffer, called shadow::find, inspected its heap type, then unregistered it and dropped the last shadow reference before the next iteration. I verified that its Weak reference expired each time.

With the production budget and counter unchanged on an RTX 3050 Laptop, allocations 1–64 used GPU upload heaps; 65 and 66 used system memory. This can permanently disable the fast path after enough resource turnover even when very few shadows are live at once.

If the intent is an outstanding-allocation cap, successful reservations need a matching release. A separate one-page-headroom control still fell back while the first shadow was held; experimentally returning its charge on final shadow drop allowed the next allocation to use GPU upload again. When choosing the release point, consider that the descriptor path also clones the underlying D3D resource. The test establishes budget exhaustion, not leaked VRAM.

AI-assisted. Synthetic buffers only.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Read-only title buffers copied into GPU upload heaps — upload-heap placement for RO buffers; pairs with #2130's written-buffer path (complementary, not dupes).

The 2 GiB allowance for host copies in GPU upload heaps counted every
copy made over the whole run and never took any back. After enough read-
only buffers came and went (64 of 32 MiB on an RTX 3050 Laptop, with
only one alive at a time), every later copy went to system memory for
the rest of the run.

A copy's bytes now leave the count when D3D12 destroys its buffer, through
the buffer's destruction notifier. That is when the last reference goes
(the copy's, and the clones lists and descriptor tables hold), not when
the copy drops. A host without the notifier keeps copies in system memory
instead of charging them for good.

Test: upload_budget_is_given_back_when_copies_go. It makes 20 copies of
128 MiB one after another (2.5 GiB, past the budget). Each one is in a GPU
upload heap, stays charged while a clone of its buffer is held after the
copy dropped, and is given back once the clone goes. With the callback
doing nothing, it fails in the first round.
@0xkn0wh3r3

Copy link
Copy Markdown
Author

@Dathinel thanks, good catch. The allowance was meant to cap outstanding copies, and it never took any back. Fixed in 772c567.

A copy's bytes now leave the count when D3D12 destroys its buffer: the buffer's ID3DDestructionNotifier callback gives the charge back. That covers your point about the descriptor path. The copy, lists and descriptor tables all hold clones of the same resource, so the charge stays until the last one goes, not just until the Shadow drops. A host without the notifier keeps these copies in system memory rather than charging them for good.

Regression test: upload_budget_is_given_back_when_copies_go. It makes 20 copies of 128 MiB, one after another (2.5 GiB in all, past the 2 GiB budget). Each copy lands in a GPU upload heap, stays charged while a clone of its buffer is held after the Shadow dropped, and is given back once the clone goes. With the callback doing nothing, it fails in the first round. cargo test -p victory-gpu --lib is unchanged with and without VICTORY_GPU_UPLOAD=1, apart from the two NVIDIA #1835 failures.

@desplmfao thanks, that fits. Measured against the budget rather than a fixed 2 GiB, the two paths make a natural split: a GPU upload copy while video memory has room, in place otherwise. The fixed 2 GiB is already marked as a shortcut in the code for that reason. I'm happy for whichever lands second to take the small rebase in shadow::find. Logging the read-only share of the 1.2 GB through register_read_only is a good idea; I'll add that when this comes out of draft.

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
…mmit

Marvisak#2130 was rewritten on top of Marvisak#2120's update, so layering it on the old
merges kept two versions of it. This tree is upstream main (940d32f) with
all 32 open PRs of 0xkn0wh3r3 at their current commits, the upstream
fixes Marvisak#1887, Marvisak#1717, Marvisak#1832, Marvisak#1630, Marvisak#1763 and Marvisak#1613, and the demo-only
conformance list; the old history stays as this merge's first parent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants