Repository navigation
GPU: render targets are demoted last, and upload-heap copies take a share of the VRAM budget - #2222
0xkn0wh3r3 wants to merge 8 commits into
Conversation
…p is one function Render targets and textures shaders write, placed in a title heap's host heap, are used every frame: the host heap gets a high residency priority, so that when video memory runs short Windows moves them out last. The priority goes on the heap, not on the textures placed in it: asked of a placed texture, SetResidencyPriority removed the device at the next submission (found merging this with residency priorities for committed textures, Marvisak#2222). Where a title texture lies in its title heap is worked out by `spot`, from the resource alone, so that the host texture can be made later than the resource (Marvisak#2221 makes host textures on first use).
…s' into demo With host textures made on first use (Marvisak#2221), a texture's place in its title heap's host heap is worked out when it is made (`spot`), not when the title creates the resource. With residency priorities (Marvisak#2222), a texture placed in a host heap has its heap's priority set: asked of the placed texture itself, the call removed the device at the next submission (aliased_targets_cleared_in_turn_read_their_own_clears).
catminou4
left a comment
There was a problem hiding this comment.
Render targets demoted last + video-memory budget module (new budget.rs, +2407). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Big but well-motivated (system demotion picking pools by residency priority); DXGI QueryVideoMemoryInfo polling capped at 250 ms is reasonable.
…p is one function Render targets and textures shaders write, placed in a title heap's host heap, are used every frame: the host heap gets a high residency priority, so that when video memory runs short Windows moves them out last. The priority goes on the heap, not on the textures placed in it: asked of a placed texture, SetResidencyPriority removed the device at the next submission (found merging this with residency priorities for committed textures, Marvisak#2222). Where a title texture lies in its title heap is worked out by `spot`, from the resource alone, so that the host texture can be made later than the resource (Marvisak#2221 makes host textures on first use).
|
Automated triage for the maintainers. Needs work: needs the maintainer's decision. It now merges cleanly into
Generated by Claude Code |
a363e34 to
4495caf
Compare
…ies, written copies, residency and the memory budget) into demo With Marvisak#2051, queued refreshes of a copy in video memory copy at once (`Shadow::copy` writes the video memory, not the mirror). With Marvisak#2226, textures in a title heap's host heap take that heap's residency priority (D3D12 gives placed resources none of their own), and Marvisak#2222's test makes its textures with no spot.
|
A note for whichever of #2222 and #2226 lands second: they merge cleanly but clash in
With both merged, #2226's |
4495caf to
1495ed8
Compare
…p is one function Render targets and textures shaders write, placed in a title heap's host heap, are used every frame: the host heap gets a high residency priority, so that when video memory runs short Windows moves them out last. The priority goes on the heap, not on the textures placed in it: asked of a placed texture, SetResidencyPriority removed the device at the next submission (found merging this with residency priorities for committed textures, Marvisak#2222). Where a title texture lies in its title heap is worked out by `spot`, from the resource alone, so that the host texture can be made later than the resource (Marvisak#2221 makes host textures on first use).
…p is one function Render targets and textures shaders write, placed in a title heap's host heap, are used every frame: the host heap gets a high residency priority, so that when video memory runs short Windows moves them out last. The priority goes on the heap, not on the textures placed in it: asked of a placed texture, SetResidencyPriority removed the device at the next submission (found merging this with residency priorities for committed textures, Marvisak#2222). Where a title texture lies in its title heap is worked out by `spot`, from the resource alone, so that the host texture can be made later than the resource (Marvisak#2221 makes host textures on first use).
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.
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.
…pt-in) Copies shaders write stayed in system memory, because the CPU reads them back (copy-back compares what shaders wrote; snapshots read them before a submission) and reading a GPU upload heap from the CPU runs at ~50 MB/s. Shaders reading and writing system memory are slower still than reading it: on an RTX 5070 Ti a shader reading and writing 256 MiB of a copy takes 212 ms in system memory and 1.25 ms in video memory. With `VICTORY_GPU_UPLOAD_WRITES=1` (and `VICTORY_GPU_UPLOAD=1`) such a copy is in a GPU upload heap, which shaders use, mirrored in system memory (`Shadow::data`), where the CPU reads and writes as before: - every CPU write into the copy (refreshes, copy-back taking the title's bytes) goes on into video memory (`Shadow::push`); - before the CPU reads what shaders may have written, the GPU copies it into the mirror (`shadow::pull`, `Host::copy_now`: one list and one wait per call): the marked pages in `copy_back_marks`, the declared ranges before `copy_back`/`keep_writes` in `Host::execute`, the title memory pages in `memory`'s copy-back; - shaders' writes outside every declared range and marked page reach the copy only, as they did; the mirror has them once a later range brings them (a shortcut, noted at `pull`). Copies of loose title memory and of single views (`loose`, `exact`) stay in system memory. Tests (each in a child process with the switches on): `written_buffers_copies_are_in_video_memory_with_a_mirror` (placement; the GPU sees refreshes), `gpu_writes_to_mirrored_copies_reach_title_memory` (a GPU write into the copy in a declared range, then in a marked page, with the title writing the same page meanwhile, through `Host::execute`: title memory gets both), `withheld_writes_stay_in_the_copy_and_its_mirror`. Without `pull` the second and third fail; without `push` the first two do; without the marked pages' pull the marked case does. The victory-gpu tests give the same results with the switches off, on for read-only copies, and on for both. Benchmark: `written_copies_in_video_and_system_memory`.
A copy in video memory (VICTORY_GPU_UPLOAD_WRITES=1) took every refresh twice: into its system-memory mirror, then on into video memory. Now a refresh writes video memory only and marks the 64 KiB pages it touched as stale in the mirror. Before the CPU reads the mirror, its stale pages come back from video memory in one GPU copy (`freshen`, `bring_back`). A refresh over a page whose bytes from before the title's first write are saved writes the mirror too, so that those bytes take it (`Tracked::saves`, `Tracked::refreshed`). A submission's snapshot ranges are brought up to date up front, in one copy (`snapshots_ahead`), and refreshes there write the mirror too until the copy-backs, so no snapshot waits for the GPU. The declared-range pull now covers only those ranges: nothing reads the mirror for a marked range, whose pages `copy_back_marks` pulls. Copy-back reads the mirror only where it compares.
Render targets and textures shaders write, placed in a title heap's host heap, are used every frame: the host heap gets a high residency priority, so that when video memory runs short Windows moves them out last. The priority goes on the heap, not on the textures placed in it: asked of a placed texture, SetResidencyPriority removed the device at the next submission (seen when this branch was merged with Marvisak#2222, which gives committed textures residency priorities; neither needs the other).
The mirror of a copy in video memory (VICTORY_GPU_UPLOAD_WRITES=1) took the GPU's writes only through pulls: of declared ranges, marked pages and the title memory pages that go back. Other writes stayed in video memory alone: past a root view's declared 1 MiB, through the heap's views of copies without marks, and on pages of programs using title memory whose results were held back. A later snapshot over such bytes read the mirror's older ones, so that submission's copy-back took the earlier GPU bytes for its own and put them into title memory, over a byte the title wrote during the submission. Held-back pages are now pulled with the others (the copies keep the GPU's bytes either way). After a submission, the pages shaders may have written past a root view's range (`Shadow::may_write`) or through the heap's unordered views of copies without marks (`bindless::written_unseen`) are stale in the mirror, so the CPU reads video memory's bytes there first. Tests: writes_past_declared_ranges_leave_the_mirror_stale (without the stale marks, the later copy-back puts the GPU's earlier byte over the title's) and held_back_marked_pages_come_back_to_the_mirror.
…hare of the VRAM budget When the process uses more video memory than DXGI's budget, Windows' video memory manager demotes some of its allocations to system memory. VIctory set no residency priorities, so the manager chose among all of them alike, render targets and depth buffers drawn every frame included. Frames then take seconds. The host copies in GPU upload heaps also had a fixed 2 GiB allowance (`UPLOAD_BUDGET`), whatever the card and whatever the title's own textures already took. Residency priorities (`host::keep_resident`, `host::Residency`), set where the allocations are made: - `HIGH`: host textures with the render target, depth stencil or unordered access flag (`make_texture`: the GPU writes them every frame, and they hold the only copy of what it wrote), view heaps' stride, mark and address tables, the views' written-pages bitmap, list scratch chunks, and host copies in GPU upload heaps. Not `MAXIMUM`, which the system keeps even past the budget at the cost of other processes. - `NORMAL` (no call: every allocation starts there): textures uploaded from title memory (`texture::title_written`). They are streamed, each read by some frames, and sampling one from system memory is slower without stalling the frame. Not `LOW`, which would demote them before allocations with no priority set (depth twins, upload staging). - None for descriptor heaps: on an RTX 5070 Ti (driver 621.24) a priority on a shader-visible descriptor heap removed the device at the next submission (every later GPU test failed with 0x887A0005). The budget (`crate::budget`): `QueryVideoMemoryInfo` on the host device's adapter, local and non-local segment groups, read at most every 250 ms and again as soon as the system signals a change (`RegisterVideoMemoryBudgetChangeNotificationEvent`). Bytes a pool takes between readings (`Budget::charge`) count as used until the next reading. `Budget::limit` answers how many bytes a pool may hold now: its part of what the rest of the process leaves of the local budget, up to a ceiling, the ceiling alone without a reading. Copies in GPU upload heaps (`UPLOAD_POOL`) take half of what the rest of the process leaves, at most 2 GiB (the old fixed size) or a quarter of the budget where that is more. A copy that does not fit stays in system memory, as before. On an 8 GB card with 5 GB used by the rest, that is 1.25 GB instead of 2 GiB; on a 16 GB card with 4 GB used, 3.75 GB. `VICTORY_GPU_UPLOAD` and `VICTORY_GPU_UPLOAD_WRITES` are unchanged. The first time local use passes nine tenths of the budget, a warning names what VIctory knows takes it: host textures (and those lists used), host copies of title memory (and those in GPU upload heaps), and the non-local use and budget. It is checked with each frame-rate line, whether or not a pool asks. Tests: - budget: the limit shrinks as others' use rises (to nothing at the budget) and not with the pool's own bytes; the ceiling grows only on large budgets; readings are cached until old or signalled, with charges counted in between; nine tenths is logged once; the host adapter answers. - upload_copies_take_their_share_of_the_budget: the upload pool's admission under a fake budget. - textures_the_gpu_writes_are_kept_resident_first: the level chosen from texture flags. The device has no getter for priorities, so the helper records what it was asked per object in test builds, and host_textures_are_kept_resident_by_use (make_texture), list_scratch_chunks_are_kept_resident, view_heap_tables_are_kept_resident, the_written_pages_bitmap_is_kept_resident and upload_budget_is_given_back_when_copies_go check the creation paths.
A resource placed in a heap takes its heap's residency: asked of a placed texture, SetResidencyPriority removed the device at the next submission, found merging this with host heaps shared by placed render targets (Marvisak#2226), whose heaps take the priority instead. keep_resident says so, and make_texture gives a priority to the textures it commits only: those it places in a heap of textures (`crate::placement`, fed from title memory) keep their heap's, the normal one.
1495ed8 to
2f74d7b
Compare
|
Thanks. Rebased with #2120 and #2130 onto 63bcca4; head is now Stack order. Understood. The branch is #2120's two commits, then #2130's three, then this PR's own two:
How the device removals were reproduced. Both were seen on one GPU and driver only: an RTX 5070 Ti with driver 621.24, the machine all my numbers come from. Both showed up in the victory-gpu tests, not in a title run:
I re-checked both on this head today, one temporary change at a time (reverted, nothing pushed), with
Neither was tried on another vendor or driver. So "no priority there" is what avoids the removal on this one, not a rule shown in general. Leaving those objects at the normal priority costs nothing on hosts where the call would have worked: the heap's tables and committed render targets still get Checks on
|
# Conflicts: # crates/victory-gpu/src/descriptor/host.rs
With Marvisak#2121's view kinds: the read-lock fast path goes into words_for, view_at and the tests take the read or write lock, and pages taken back or remade clear their slots' ready mark (mirror's lock-free check must not pass them by). Two Marvisak#2222 tests call make_texture with Marvisak#2226's spot argument.
… of their own Marvisak#2222's guard skipped textures in upstream's heaps of textures (crate::placement) but not those Marvisak#2226 places in a title heap's host heap (crate::alias), whose heap carries the priority: asked of such a texture, SetResidencyPriority removed the device at the next submission (aliased_targets_cleared_in_turn_read_their_own_clears and every GPU test after it).
… unchanged; its docs stay)
…ddress kept() read what keep_resident was asked from a map keyed by the object's pointer, whose entries outlived their objects. Run in parallel, a texture made at the address of another test's released one read that one's residency (host_textures_are_kept_resident_by_use: Some(Frame) for a placed texture, which gets none). The residency is now private data on the object itself (SetPrivateData), gone with it.
|
Replaced by #3102, which combines this with the rest of the host-memory placement work in one PR, with measurements on a discrete and an integrated GPU. Closing here. |
Stacked on #2130 (which is stacked on #2120): the copies in GPU upload heaps that this change sizes come from there,
UPLOAD_BUDGETincluded. The first five commits here are #2120's (two) and #2130's (three). The last two are this change:05160205(priorities, the budget pool, the warning) and2f74d7bd(priorities for heaps and committed resources only). Once #2120 and #2130 are settled, this rebases to those two.Problem
When the process uses more video memory than DXGI's budget, Windows' video memory manager demotes some of its allocations to system memory. VIctory sets no residency priorities, so the manager chooses among all of them alike, including render targets and depth buffers drawn every frame. Frames then take seconds. VIctory's own copies in GPU upload heaps also had a fixed 2 GiB allowance (
UPLOAD_BUDGET). It did not depend on how much the card has, or on how much the title's own textures already take.Fix
Residency priorities.
host::keep_resident(withhost::Residency) is called where each heap or committed resource is made:make_texture)HIGHHIGHHIGHHIGHHIGHtexture::title_written)NORMAL(no call)MAXIMUMis not used: the system keeps such allocations even past the budget, at the cost of other processes.LOWis not used either: it would demote streamed textures before allocations with no priority set (depth twins, upload staging).Descriptor heaps get no priority. On an RTX 5070 Ti (driver 621.24), a priority on a shader-visible descriptor heap removed the device at the next submission, and every later GPU test failed with
0x887A0005. Resources placed in a heap get none either: their residency is their heap's. A priority asked of a placed texture also removed the device at the next submission, somake_texturesets one only on textures it commits.Video memory budget.
crate::budgetcallsIDXGIAdapter3::QueryVideoMemoryInfoon the host device's adapter, for the local and non-local segment groups. It reads at most every 250 ms, and again as soon as the system signals a change (RegisterVideoMemoryBudgetChangeNotificationEvent), so pools can ask as often as they like. Bytes a pool takes between readings (Budget::charge) count as used until the next reading includes them.Budget::limit(pool, own)answers how many bytes a pool may hold now: a part of what the rest of the process leaves of the local budget, up to a ceiling. Other use shrinks the answer; the pool's own bytes do not. Without a reading, the answer is the ceiling.Upload-heap copies.
UPLOAD_POOLtakes half of what the rest of the process leaves. Its ceiling is 2 GiB, as before, or a quarter of the budget where that is more:A warning, once per run. The first time local use passes nine tenths of the budget, a warning names what VIctory knows takes the memory:
The check runs with each frame-rate line, whether or not a pool asks. Upstream (69d1e8e) starts giving up unused host textures fed from title memory at the same point, 90 % of the card's budget, so the warning marks where that eviction begins.
What stays the same
VICTORY_GPU_UPLOADandVICTORY_GPU_UPLOAD_WRITESwork as before, and both are still off by default.Tests
budget::testsreplace the DXGI source with a fake and check that:host::tests::upload_copies_take_their_share_of_the_budget: the upload pool's admission against a fake budget.host::tests::textures_the_gpu_writes_are_kept_resident_first: the level chosen from texture flags.ID3D12Device1has no getter for priorities, so in test builds the helper records what it was asked for each object. These tests check that each path calls it:resource::tests::host_textures_are_kept_resident_by_use(make_texture);host::tests::list_scratch_chunks_are_kept_resident;descriptor::host::tests::view_heap_tables_are_kept_resident(the heap itself gets no priority);shadow::tests::the_written_pages_bitmap_is_kept_resident;shadow::tests::upload_budget_is_given_back_when_copies_go(copies in GPU upload heaps).Run on Windows 11 with an RTX 5070 Ti (driver 621.24):
cargo clippy --workspace --all-targets, with and without--features victory-gpu/diagnostics: no warnings.cargo test --workspace --exclude victory-shader: everything passes except two tests that fail the same way on upstreammainon this machine:descriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views.cargo test -p victory-shader -- --test-threads=1: three hardware comparisons fail (a_heap_buffer_read_by_element_takes_the_views_stride,a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,gathers_at_a_level_match_the_hardware). This change does not touchvictory-shader.Docs
None:
docs/gpu-memory.mdis gone from #2130, and this PR no longer adds to it. The budget and the priorities are described where they are code: thebudget.rsmodule docs andhost::keep_resident.Rebased onto 13f91ca
keep_residentis now for heaps and committed resources only:make_texturesets a priority on the textures it commits, and placed ones keep their heap's normal priority.host_textures_are_kept_resident_by_useexpects no call for a placed streamed texture.mapped_buffer(size, upload)is nowshared_mapped_buffer.Rebased onto 63bcca4 (with #2120 and #2130)
docs/gpu-memory.mdis dropped with that file.cargo test -p victory-gpu --lib -- --test-threads=1:HIGHon the shader-visible view heap inHostHeap::new: the first test that submits work after making one (bindless::tests::heap_views_are_current) fails with0x887A0005(device removed), and every later GPU test fails with it: 60 failures, against 2 on this head without the change.HIGHon texturesmake_textureplaces in a heap of textures (and nothing else changed), each test run alone:bindless::tests::heap_views_are_currentfails with0x887A0005, andresource::tests::only_textures_fed_from_title_memory_are_given_upgets no host texture for its placed one. Both pass without the change. First seen merging this with GPU: title textures the GPU writes share their heap's memory on the host #2226's placed render targets (itsaliased_targets_cleared_in_turn_read_their_own_clearsandplaced_targets_at_overlapping_offsets_share_one_host_heapfailed).cargo test -p victory-gpu --lib: 252 passed, with and without both switches; the same 2 failures as onmainhere (NVIDIA D3D12 (70564ae): stride_tests & synthetic_tests fail due to HEAP_VIEWS (2M > 1M NVIDIA limit) and Stride::Structured with rawBufferLoad #1835). With both switches and--features diagnostics(skippingcompositor_failure_presents_primary_in_the_same_frame, which exits the process onmaintoo here): 265 passed, plus the flakyframe::tests::waits_end_on_ticks_and_count_frames.cargo clippy -p victory-gpu --all-targets, with and without--features diagnostics: clean.cargo xtask policy: ok.