Repository navigation
GPU: copies of read-only title buffers in GPU upload heaps (opt-in) - #2120
0xkn0wh3r3 wants to merge 2 commits into
Conversation
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.
|
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:
Not measured on my side: the read-only share of those 1.2 GB. Your |
|
The 2 GiB allowance currently counts successful allocations across the lifetime of the process. On 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
left a comment
There was a problem hiding this comment.
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.
|
@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 Regression test: @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 |
…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.
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_onlyregisters such buffers (resource.rs, from the title'sALLOW_UNORDERED_ACCESSflag), andshadow::findmakes their copies withHost::gpu_upload_buffer.Measured on an RTX 5070 Ti (driver 621.24, Ryzen 9 7900X), 256 MiB, through
shadow::find(reading_copies_in_video_and_system_memory):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=1beforevictory 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
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_memoryis the benchmark behind the table above (ignored by default; run in release with--ignored --nocapture).cargo test -p victory-gpu --lib, with and withoutVICTORY_GPU_UPLOAD=1: 142 passed, the same 2 failing as onmainon 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-targetswith and without--features diagnostics: clean.Related