Skip to content

GPU: render targets are demoted last, and upload-heap copies take a share of the VRAM budget - #2222

Draft
0xkn0wh3r3 wants to merge 7 commits into
Marvisak:mainfrom
0xkn0wh3r3:gpu/residency-and-memory-budget
Draft

0xkn0wh3r3 wants to merge 7 commits into
Marvisak:mainfrom
0xkn0wh3r3:gpu/residency-and-memory-budget

Conversation

@0xkn0wh3r3

Copy link
Copy Markdown

Stacked on #2130 (which is stacked on #2120): the copies in GPU upload heaps and docs/gpu-memory.md that this change sizes and documents come from there. The first five commits here are #2120's and #2130's. The last one is this change.

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 (with host::Residency) is called where each allocation is made:

allocation priority why
host textures with the render target, depth stencil or unordered access flag (make_texture) HIGH the GPU writes them every frame, and they hold the only copy of what it wrote
view heaps' stride, mark and address tables HIGH every draw reads them
the views' written-pages bitmap HIGH shaders that mark their writes set it every frame
list scratch chunks HIGH the list's commands read them
host copies in GPU upload heaps HIGH shaders read them every frame, and they are budgeted (below)
textures uploaded from title memory (texture::title_written) NORMAL (no call) streamed, each read by some frames only; sampling one from system memory is slower but does not stall the frame

MAXIMUM is not used: the system keeps such allocations even past the budget, at the cost of other processes. LOW is 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.

Video memory budget. crate::budget calls IDXGIAdapter3::QueryVideoMemoryInfo on 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_POOL takes 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:

local budget used by the rest of the process copies may hold
7.5 GB (8 GB card) 2 GB 2 GiB (ceiling)
7.5 GB 5 GB 1.25 GB
7.5 GB 7.5 GB or more nothing
15 GB (16 GB card) 4 GB 3.75 GB (a quarter of the budget)

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:

  • host textures: all of them, and those lists used;
  • host copies of title memory: all of them, and those in GPU upload heaps;
  • the non-local segment's use and budget.

The check runs with each frame-rate line, whether or not a pool asks.

What stays the same

  • VICTORY_GPU_UPLOAD and VICTORY_GPU_UPLOAD_WRITES work as before, and both are still off by default.
  • A copy that does not fit in an upload heap goes to system memory, as before.
  • Charges are still given back when D3D12 destroys a copy's buffer.
  • On a host that does not report a budget, the pool keeps its fixed 2 GiB.

Tests

  • budget::tests replace the DXGI source with a fake and check that:
    • the limit shrinks as other use rises, down to nothing at the budget, and the pool's own bytes do not shrink it;
    • the ceiling grows only on large budgets;
    • readings are cached until they are old or a change is signalled, and charges count in between;
    • passing nine tenths is logged once;
    • the host adapter answers.
  • 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.
  • Creation paths. ID3D12Device1 has 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 upstream main on this machine: descriptor::host::tests::dynamic_slots_are_held_until_released and memory::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 touch victory-shader.

Docs

docs/gpu-memory.md gets a section on the budget and residency, and the further step "Size the budget from DXGI's video memory budget" is removed from the list because it is done.

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 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.

Synthetic world frame, marked path, median ms: copies in system memory
80.7, mirror always written 114.6, now 69.0.

docs/gpu-memory.md explains how copies bridge the console's unified memory
and the PC's two pools, with the measurements.
…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.

docs/gpu-memory.md: a section on the budget and residency; the further
step "size the budget from DXGI's video memory budget" is done.
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
…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).
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 notes that its textures are committed.
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
…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 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.

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.

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.

2 participants