Skip to content

GPU: fresh tables past a capped heap's title slots copy views made once per slot - #3068

Open
r4tedx wants to merge 3 commits into
Marvisak:mainfrom
r4tedx:gpu/overflow-views
Open

r4tedx wants to merge 3 commits into
Marvisak:mainfrom
r4tedx:gpu/overflow-views

Conversation

@r4tedx

@r4tedx r4tedx commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What changes

On a host that caps its shader-visible view heap (NVIDIA: 1,000,000 descriptors), main keeps 934,464 title slots and 65,536 dynamic ones. A title table that reaches past the title slots is made afresh at every use (State::tables): make_at creates each of its views into dynamic slots, every time. In our title run that came to about 30,000 views created a frame on the recording threads.

This PR keeps a view for each title slot past the cap, and fresh tables copy those views instead of creating them again.

  • An overflow heap. A capped view heap gets a second, CPU-only CBV/SRV/UAV heap (HostHeap::overflow), with one slot per title slot past the host's. That's 310,720 slots for our title, 32 B each on NVIDIA, about 10 MB of CPU memory. It is not shader-visible, so the host's cap doesn't apply to it. Its size is logged once, when it's made.
  • One view per slot, made at its first use. When a fresh table is used (HostHeap::copy_overflow), each of its slots is compared with the record (view entry, generation) its overflow slot was made from. A view is created only where the record changed. This is the same rule the title slots follow in HostHeap::mirror. The cache is per slot, not per record: the same record in two slots is made twice.
  • Then one CopyDescriptorsSimple of the whole table into its dynamic slots. Each dynamic slot keeps the host resource, stride, write mark and view address that make_at would have given it. Its own readable calls (transitions, copy refreshes) are unchanged.
  • Placed textures and failed views.
    • An overflow slot viewing a texture the title placed itself is made again when the host texture behind it has changed. This is the same check mirror makes for title slots (needs_remake).
    • A view the host couldn't make is tried again at the next use.
  • Locks. Each table takes one look-up in the views' registry for all its records. It locks the overflow shards it spans (1,024 slots each, in order, so lists recording at once rarely meet) and takes the kept-resources lock once. What an overflow slot kept before it's replaced is retired as for a title slot (crate::host::retire).
  • Switch. VICTORY_OVERFLOW_VIEWS=0 restores main's path, with every view made per use. A heap that isn't capped has no overflow heap, so nothing changes there.

Files: descriptor/host.rs (Overflow, copy_overflow, keep_run, the heap's creation); descriptor.rs (views_of: the views of several records under one lock); draw.rs (State::tables copies where the heap has an overflow heap, else makes views as before; tests).

This doesn't depend on any other open PR.

Tests

The title was not used in these tests.

New:

  • draw::tests::a_table_past_the_title_slots_reads_the_same_views_copied_or_made (WARP). It makes a view heap that keeps 4 of its 16 title slots (a test-only, thread-local override of the title slots kept, as a capped heap would) and runs three checks on a table at title slot 8:
    • copied at its first use (1 view made), a dispatch writes through it into the buffer the title's record names;
    • after the title writes another record there, it is made again (1 view) and writes into the new buffer;
    • copied again unchanged (0 views made), it still writes the new buffer, as a table made one by one with make_at does.
  • descriptor::host::tests::overflow_views_are_on_unless_switched_off.
  • draw::tests::fresh_tables_made_or_copied_per_1000: a microbench, ignored by default. It times 1,000 fresh tables of 8 three ways: made one by one, copied at first use, and copied with the records unchanged.

Results on main 63bcca4 (this machine, WARP):

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings, and the same with --features diagnostics: both clean.
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --lib --locked: 248 passed, 0 failed, 2 ignored (the existing benchmark and the new one). On main itself: 246 passed, 0 failed, 1 ignored.
  • The two new tests by name: 2 passed.
  • The microbench (--ignored --nocapture, WARP, one run), 1,000 tables of 8:
    • views made one by one: 7.6 ms;
    • copied, first use (views made in the overflow heap): 9.1 ms;
    • copied, records unchanged: 3.5 ms.

In game

Local title data was used for these runs; only counts and timings from the logs are given here.

This was on an RTX 5070, in the title's first open-world area, 10 minutes a run with 0 device removals in either run. OV0 has the overflow heap switched off (VICTORY_OVERFLOW_VIEWS=0) and OV1 has it on, with everything else the same exe and switches.

The play build carries other unmerged changes: our dynamic-slot reclaim (see #688), host copies in video memory, and others. So the absolute numbers aren't main's. The difference between the two runs is this change.

OV0 (off) OV1 (on)
frame ms, mean / p50 / p95 816 / 778 / 1,021 699 / 682 / 791
preparing ms (mean) 270 221
GPU ms (mean) 125 119
fresh tables' views made / frame 28,908 24,602
copied from the overflow heap / frame 0 2,778
  • Median frame time: −73 to −118 ms (95 % bootstrap interval over the 5-second meter windows).
  • Most of the gain is in preparing (−49 ms).
  • Only about 10 % of the fresh tables' views came from the overflow heap. Most slots had a different record at their next use, so their views were made again. Whether the title cycles the same records through different slots (which a cache keyed per record would serve) or writes new ones is a follow-up; we have a diagnostics build for it but no run yet.
  • That run was of the play build's version of this change, which kept placed textures by record only. This PR also checks their host texture, as mirror does, so it may make a few more views than OV1 did.

Related

AI-assisted

🤖 Generated with Claude Code

…ce per slot

On a capped view heap (NVIDIA, 1,000,000 views) main keeps 934,464
title slots, and every use of a title table past them made its views
afresh into dynamic slots: about 30,000 views a frame in our title run.

A CPU-only overflow heap now keeps a view for each title slot past the
host's (310,720 for that title, 32 B each on NVIDIA, logged at
creation). Each is made from the title's record at the slot's first
use, and again only when the slot's record changes, as title slots are
made when the title writes them. A fresh table makes the views whose
records changed there, then copies all of them into its dynamic slots
in one CopyDescriptorsSimple. Each dynamic slot keeps, strides and
marks as make_at would. A placed texture's overflow slot is made again
when its host texture changed, the check mirror makes for title slots
(now placed_texture_moved, shared by both); a view the host could not
make is tried again at the next use.

Locks are taken once per table: the views' registry for the table's
records, the overflow shards (1,024 slots each) the table spans, the
kept resources.

VICTORY_OVERFLOW_VIEWS=0 makes every view per use, as before. Heaps
that are not capped have no overflow heap.

Tests (WARP): a table past the title slots, copied, writes through its
view the buffer the title's record names, and the new one after the
title rewrites the slot; unchanged, it is copied without a view made;
made one by one, the same buffer. A microbench (ignored by default)
times 1,000 fresh tables of 8 both ways.

Ported from play/main-overflow-views 203fe02 onto main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • cargo test -p victory-gpu descriptor::host::tests -- --test-threads=1 --skip dynamic_slots_are_held_until_released (RTX 5090 & WARP): 15 passed, 0 failed on both (including overflow_views_are_on_unless_switched_off). (Pre-existing dynamic_slots_are_held_until_released fails on NVIDIA's 1M view-heap cap #1744.)
  • cargo test -p victory-gpu draw::tests -- --test-threads=1 (RTX 5090): 21 passed, 0 failed (including a_table_past_the_title_slots_reads_the_same_views_copied_or_made).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified the whole path: Overflow CPU heap keyed by generation<<32|index (NULL=no view, 0=unmakeable->retried), placed_texture_moved factored correctly out of mirror, shards locked in index order, CopyDescriptorsSimple under the shard locks, retired kept go through host::retire post-unlock, keep_run/set_stride/set_mark/note_address mirror make_at's bookkeeping, graceful None fallback to one-by-one. views_of batches the VIEWS lock with the same tag+generation check. End-to-end test discriminates made=1/copied=0 and verifies actual dispatch writes.

@r4tedx

r4tedx commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Head update 67a8692 → b40e60b: adds @passerbyyhx's regression test an_overflow_srv_follows_a_replaced_texture_without_a_new_record (draw.rs only, plus Slots::new split into Slots::from_heap; no change to the fix). The title's record and generation stay the same while the placed texture's backing resource is replaced; the overflow copy must remake once and read the new contents.

Verified here on WARP: clippy clean; draw:: tests 22 passed, 0 failed, 1 ignored. With the resource-change check disabled, the new test fails (0 remakes, stale texel), so it guards that check. The author also reports RTX 4090 Laptop and full WARP diagnostics results in r4tedx#20; those were not re-run here. Synthetic tests only, no title data.

AI-assisted

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

Tested locally on Windows 11 x64 (rustc 1.98.1, NVIDIA GeForce RTX 5090 + WARP):

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default and --all-features): clean.
  • RTX 5090: descriptor::host::tests (15 passed, skipping pre-existing NVIDIA cap test dynamic_slots_are_held_until_released), draw::tests (22 passed, 1 ignored). Microbench (fresh_tables_made_or_copied_per_1000): 8.5 ms one-by-one vs 7.0 ms first copy vs 2.8 ms unchanged copy.
  • WARP (--features diagnostics, VICTORY_D3D12_WARP=1): descriptor::host::tests (16 passed), draw::tests (22 passed, 1 ignored). Microbench: 10.0 ms one-by-one vs 10.6 ms first copy vs 3.6 ms unchanged copy.
  • cargo xtask policy: 191 files ok.

One real bug in texture eviction handling should be fixed before merge:

HostHeap::evict does not evict self.overflow (crates/victory-gpu/src/descriptor/host.rs)
When crate::resource::give_up evicts a host texture (resource.rs:1687-1709), it calls HostHeap::evict(&raws) and clears resource(r).host = None. Because HostHeap::evict only walks self.kept and never touches self.overflow:

  1. VRAM leak: Overflow's Over.kept retains a strong ID3D12Resource COM reference to every evicted texture viewed through an overflow slot until a different title record overwrites that slot, keeping its VRAM pinned.
  2. Stale descriptor for evicted regular (non-placed) textures: For title slots (< self.count), HostHeap::evict clears self.kept[index] = None, which causes HostHeap::mirror's needs_remake() (let Some(held) = held else { return true; }) to remake the view on the next draw. In copy_overflow, however, slot.kept is still Some(old_evicted_tex) and moved() only checks is_placed_texture(v). On the next draw of an evicted regular texture (View::Shader(r, ..) / View::Unordered(r, ..)), slot.key == key matches and moved() returns false, so copy_overflow copies the stale descriptor pointing to the old evicted ID3D12Resource (made == 0), while draw.rs's readable() recreates a fresh HostTexture and uploads the title bytes into the new resource. The shader then keeps sampling the stale evicted texture.

Fix: in HostHeap::evict, also walk self.overflow shards—when slot.kept matches textures (with k.1.is_none()), clear slot.kept = None, reset slot.key = 0 (so the next copy_overflow remakes it), and crate::host::retire the replaced ID3D12Resource after dropping the shard lock. Lock ordering stays clean (HostHeap::evict runs under SHADER_HEAPS, and copy_overflow never acquires SHADER_HEAPS). Adding a regression test mirroring a_texture_given_up_is_made_again_with_its_bytes_and_recorded_lists_keep_it through an overflow slot (evict with resource::give_up_now, mutate title bytes, re-copy and assert made == 1 + updated texel readback) will cover this directly.

Minor non-blocking notes:

  • keep_run omits the #[cfg(feature = "diagnostics")] crate::resource::life::note(...) tracking present in HostHeap::keep.
  • In copy_overflow, first.checked_add(n).is_none_or(|end| end > self.count + self.dynamic) avoids relying on caller-guaranteed non-overflow for first + n.

Review of Marvisak#3068 (fuzeofficlal): `HostHeap::evict` walked only the slots'
kept resources, never the overflow heap's. When `resource::give_up` gave
up a host texture, an overflow slot viewing it kept the old resource
alive (its video memory too) until another record took the slot, and
since its record had not changed, `copy_overflow` copied its stale view
into the next fresh table while `readable` made the texture again from
title memory: the shader read the texture given up.

`evict` now also walks the overflow shards: a slot holding a texture
given up lets go of it (retired once the shard's lock is dropped) and
clears its key, so its next use makes the view again, of the texture
made again. Lock order: under SHADER_HEAPS, then a shard;
`copy_overflow` takes shards without SHADER_HEAPS, and eviction runs on
the submitting thread before a submission (`resource::evict_unused`).

Also from the review: `keep_run` notes a texture a slot stops viewing,
as `keep` does (diagnostics), and `copy_overflow`'s bound on its
dynamic slots uses `checked_add`.

Test: `an_overflow_table_reads_a_texture_given_up_as_made_again` reads a
texture through an overflow table, gives it up, changes its title bytes
and copies again: one view made, the new bytes read. It fails without
the fix (no view made, left 0, right 1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@r4tedx

r4tedx commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal for the careful review and the RTX 5090 numbers.

You were right. I checked it in the code before changing anything.

  • HostHeap::evict walked only self.kept (title and dynamic slots), never self.overflow.
  • After resource::give_up gave up a regular (non-placed) texture, the overflow slot still held the old ID3D12Resource, so its video memory stayed pinned.
  • On the next use the slot's key was unchanged, and moved() only looks at placed textures. So copy_overflow copied the stale view (made == 0), while readable made the texture again from title memory, and the shader read the one given up.

The regression test you suggested shows it: without the fix it fails at "made again: its texture was given up" (left 0, right 1).

The fix (6b42656, on top of b40e60b):

  • HostHeap::evict now also walks the overflow shards. A slot holding a given-up texture (k.1.is_none() and in the set) drops it and clears its key, so the next copy_overflow makes the view again, of the texture made again.
    • What the slot held is retired after the shard's lock is dropped.
    • The overflow descriptor itself is left as it is: it's in a CPU-only heap the GPU never reads, and it is made again before it's next copied.
  • Lock order: descriptor::evict takes SHADER_HEAPS, then each shard. copy_overflow takes shards and never SHADER_HEAPS, and nothing it calls while holding them (make_marked) reaches SHADER_HEAPS. Eviction runs on the submitting thread before a submission (resource::evict_unused), not from view creation.
  • Your two minor notes, both taken:
    • keep_run now has the same #[cfg(feature = "diagnostics")] crate::resource::life::note(...) as keep.
    • copy_overflow bounds its dynamic slots with first.checked_add(n).is_none_or(|end| end > self.count + self.dynamic).

The test. draw::tests::an_overflow_table_reads_a_texture_given_up_as_made_again, modelled on a_texture_given_up_is_made_again_with_its_bytes_and_recorded_lists_keep_it:

  1. Reads a texture fed from title memory through an overflow table (title slot 8 of a heap keeping 4).
  2. Gives it up with resource::give_up_now.
  3. Changes the title bytes from 0x40 to 0x80.
  4. Copies again and asserts one view made and the new texel (128/255) read back.

It fails without the fix and passes with it.

Testing (WARP, Windows 11):

  • cargo clippy -p victory-gpu --all-targets -- -D warnings, with and without --features diagnostics: clean.
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --lib -- --test-threads=1: 250 passed, 0 failed, 2 ignored. That includes the new test, an_overflow_srv_follows_a_replaced_texture_without_a_new_record and a_table_past_the_title_slots_reads_the_same_views_copied_or_made.

The title was not used in these tests.

AI-assisted

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Verdict: needs work. The overflow heap is a reasonable approach and the record-key and placed-texture checks match HostHeap::mirror. One interaction with texture eviction is missing, though.

  • Eviction does not reach overflow slots. crate::resource::give_up → descriptor::evict → HostHeap::evict (descriptor/host.rs) nulls every slot whose kept holds a given-up host texture, so the texture can be freed and is made again on the next read. The new Overflow slots keep their own Over.kept, which HostHeap::evict never walks. So any overflow slot that last viewed a given-up texture keeps it alive until that slot is used again, which may be never. Under the card's budget (BUDGET_HIGH), that memory doesn't come back. The overflow slot's key also stays the same, so on its next use it is copied without being made again. placed_texture_moved catches that case for placed textures only; a non-placed evictable texture would be viewed stale.
  • What would change the verdict: have HostHeap::evict also clear overflow slots that hold one of the textures (null view, key = 0, kept retired). Add a test that gives up a texture viewed through an overflow slot and checks that it is released and made again at the next copy_overflow.

CI: check passing on b40e60b. The default-on switch (VICTORY_OVERFLOW_VIEWS) and the measured gain are noted; this is not a judgement on the approach.


Generated by Claude Code

@r4tedx

r4tedx commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

For triage's point (written at b40e60b): it's addressed in 6b42656, as described in the reply above.

  • HostHeap::evict now walks the overflow shards too. A slot holding a given-up texture has its key cleared and what it kept retired, after the shard's lock is dropped.
  • The test is draw::tests::an_overflow_table_reads_a_texture_given_up_as_made_again. It gives up a texture viewed through an overflow slot (resource::give_up_now), changes its title bytes, copies the table again, and checks that the view is made again (one view made) and that the new texel is read back. Without the fix it fails ("made again: its texture was given up", left 0, right 1).
  • One difference from the suggestion: the overflow descriptor itself isn't nulled. It sits in a CPU-only heap the GPU never reads, and with its key cleared it's always made again before it's next copied into a shader-visible slot. So nulling it would add a write without changing what any shader can see. Happy to add it if you'd rather have the slot null in between.

The title was not used in these tests.

AI-assisted

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

All three points from the earlier review are cleanly addressed in 6b42656a:

  • HostHeap::evict now calls evict_overflow after dropping made, clearing (slot.key, slot.mark) and retiring evicted ID3D12Resource handles outside the shard locks. Lock ordering (SHADER_HEAPS -> individual shards in evict_overflow, ascending shards without SHADER_HEAPS held across CopyDescriptorsSimple in copy_overflow, and retire/keep_run after dropping shards) is deadlock- and race-free, and since Over::NULL == u64::MAX and valid generations start at 1, key = 0 guarantees the CPU descriptor is rebuilt before the next copy.
  • keep_run logs resource::life::note under diagnostics, matching HostHeap::keep, and copy_overflow uses checked_add for the dynamic-slot bound check.
  • draw::tests::an_overflow_table_reads_a_texture_given_up_as_made_again directly exercises the eviction -> rewrite -> remake -> readback path.

Verified locally on Windows 11 (RTX 5090 + WARP + D3D12 Debug Layer):

  • cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --all-features): clean
  • RTX 5090: descriptor:: 22/22 passed (skipping the pre-existing NVIDIA dynamic_slots_are_held_until_released cap failure), draw:: 23/23 passed (1 ignored bench)
  • RTX 5090 + VICTORY_D3D12_DEBUG=1 (--features diagnostics): overflow 9/9 passed (including an_overflow_table_reads_a_texture_given_up_as_made_again)
  • WARP + VICTORY_D3D12_DEBUG=1 (--features diagnostics, VICTORY_D3D12_WARP=1): descriptor:: 23/23 passed, draw:: 23/23 passed (1 ignored bench)
  • cargo xtask policy: 191 files ok

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants