Skip to content

GPU: clears of one view in a list share one dynamic slot - #2871

Open
r4tedx wants to merge 3 commits into
Marvisak:mainfrom
r4tedx:gpu/clear-view-one-slot
Open

r4tedx wants to merge 3 commits into
Marvisak:mainfrom
r4tedx:gpu/clear-view-one-slot

Conversation

@r4tedx

@r4tedx r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Split out of #2619 at the maintainer's triage: this changes clear_unordered for every unordered access clear, the title's own included, so it gets its own review. #2619 and #2620 will be rebased on top of it.

What changes

clear_unordered took a fresh dynamic slot of the bound view heap on every call and held it until the list was reset. A list that clears the same view many times (a per-pass clear in a loop of passes, or a stand-in after each dispatch of its program) took one slot per clear from the capped heap. When the pool runs out, the draws and dispatches that need dynamic slots after it are dropped.

  • The list's draw state now keeps the slot it made per (view heap, view, host resource), and later clears of that view in the same list reuse it.
  • The cache lives with the slots. It goes when the list is reset (or released) and at a cut of the list, where root views are made again too. The host resource in the key is one the list keeps, so its address names no other resource while the slot is held.
  • The host view is now made before a slot is taken, so a view the host cannot make takes no slot.
  • The clear itself moves into State::clear_view (the resolved view in, whether it was recorded out). clear_unordered calls it after it looks up the title's CPU handle, and the test drives it directly. GPU: an opt-in constant stands in for one view a named program writes (VICTORY_AMBIENT) #2619 builds on the same function.

Nothing else changes: the clear's barriers, the heap binding and the Clear call are as before.

Tests

The title was not used in these tests.

  • New, draw::tests::clears_of_one_view_in_a_list_share_a_dynamic_slot (WARP): eight clears of one view take one slot, a clear of a second view one more, and a further clear of the first none. A compute pass then reads both views, and each holds its last clear (0x55 and 0x77 at four texels). The slots come back at reset, and a new recording takes one afresh. HostHeap::dynamic_free (tests only) counts the free slots.
  • Results on main f3578e3:
    • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings and the same with --all-features: both clean.
    • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --lib --locked: 244 passed, 0 failed, 1 ignored (an existing benchmark).

Note for later: if a change lands that lends each list a run of dynamic slots at once (rather than one slot at a time), this test's count of the heap's free slots has to count out of the list's run instead. We hit that in our play build and fixed the test that way; the slot sharing itself was unaffected.

🤖 Generated with Claude Code

clear_unordered took a dynamic slot of the bound view heap on every call
and held it until the list was reset, so a list that clears the same
view many times (a per-frame clear inside a loop of passes, say) took one
slot per clear from the capped heap; when the pool runs out, later draws
and dispatches that need dynamic slots are dropped. The list's draw state
now keeps the slot it made per view (view heap, view, host resource) and
clears of that view reuse it. The cache lives with the slots: it goes
when the list is reset (or released) and at a cut of the list, where root
views are made again too. The host resource in the key is one the list
keeps, so its address names no other while the slot is held. The view is
now made before a slot is taken, so a view the host cannot make takes
none.

The clear itself moves into State::clear_view (taking the resolved view,
returning whether it was recorded), which clear_unordered calls after it
looks the title's CPU handle up; the test drives it directly.

Test (WARP): eight clears of a view take one slot, another view one
more, a further clear of the first none; the views hold their last
clears; the slots come back at reset. HostHeap::dynamic_free (tests
only) counts the free slots.

Split from Marvisak#2619 at the maintainer's triage.

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

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

Sound: keying clear slots by (heap, view, host resource) and releasing them with the list's dynamic pool gives each cleared view exactly one slot per list — the resource in the key is what makes reuse safe since the list holds it. Clearing on reset matches root_slots' lifetime model. LGTM.

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

The focused clear-slot test at e7169ee passed on Windows with WARP and the D3D12 debug layer requested. The test covers repeated clears, final texel values, and slot release on reset.

The patch also clears the lookup cache at a list cut while retaining the slots for the earlier segment. That is an important lifetime boundary for the world-rendering path. A useful follow-up would be to clear the same UAV on both sides of a cut, submit both segments, and verify the clear result and that the old segment's slot stays held until reset.

AI-assisted review and test.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and verified e7169ee6 on Windows 11 (RTX 5090 + VICTORY_D3D12_DEBUG=1 + WARP) against main (842b0cb):

  • Keying State::clear_slots on (heap_ptr, view.clone(), resource.as_raw() as usize) while list.keep(&resource) keeps the host ID3D12Resource alive for the lifetime of the recorded list guarantees cache misses whenever the bound heap changes, the title rewrites the descriptor slot, or the underlying host texture/shadow is replaced.
  • State::cut and State::reset both clear clear_slots in lockstep with fresh_tables/root_slots and dynamic slot release.
  • Local checks: RTX 5090 draw::tests 21/21 (including clears_of_one_view_in_a_list_share_a_dynamic_slot), descriptor::host::tests 14/14, list::tests 4/4 passed; WARP draw::tests 21/21 and list::tests 4/4 passed; VICTORY_D3D12_DEBUG=1 passed on both RTX 5090 and WARP; cargo clippy (default & diagnostics) 0 warnings; cargo xtask policy (190 files ok).

Looks clean and ready to merge.

…held

A view cleared on both sides of a cut (a wait that moves the recording
on to a new host list), both segments submitted in order: the first
segment's clears share one slot, the cut gives none back, the second
segment takes one slot of its own (its view heap is bound afresh) and
shares it for its next clear, and both stay held until the list is
reset, when both come back. A read in each segment sees the last clear
recorded before it in that segment.

Suggested in review of Marvisak#2871.

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

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, all three of you. @fuzeofficlal, the RTX 5090 run with the debug layer is very useful, since we can only test on WARP here.

@AwsAli05, I've added the test you suggested in d3f9b11: draw::tests::a_clear_after_a_cut_takes_a_new_slot_and_the_old_one_stays_held (WARP). One view is cleared twice in a list, the list is cut (State::cut onto a second host list, as a wait does), and the view is cleared twice more. Both segments are submitted in order. It checks that:

  • the first segment's two clears share one slot, and the cut gives none back;
  • the second segment takes one slot of its own (its view heap is bound afresh) and shares it for its second clear;
  • both slots stay held after execution until the list is reset, and then both come back;
  • a read pass at the end of each segment sees that segment's last clear (0x33, then 0x44, at four texels).

Results at d3f9b11 on main f3578e3: clippy -D warnings clean with and without --all-features; VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --lib --locked 245 passed, 0 failed, 1 ignored. The title was not used in these tests.

AI-assisted

@pawrenity

Copy link
Copy Markdown

VERIFY-2871: verified d3f9b11e (incl. the new cut test) on NVIDIA GeForce RTX 4070, Windows 11 (default adapter, confirmed via GetAdapterLuid → EnumAdapterByLuid), base f3578e38.

Review: no defects found.

  • clear_view makes the CPU view before taking a slot, so a view the host cannot make no longer leaks a dynamic slot.
  • The slot is written (make_at) only on a cache miss. The key (heap, View, host resource) changes whenever the bound heap, the title's view or the host resource changes, and list.keep(&resource) holds the resource, so the cached descriptor still names the same view when it is reused.
  • State::reset (*self = State::default()) drops clear_slots along with dynamic, whose Drop releases the slots. State::cut clears clear_slots together with fresh_tables/root_slots and releases nothing.

Results (cargo test -p victory-gpu --locked --lib):

  • draw::tests::clears_of_one_view_in_a_list_share_a_dynamic_slot and draw::tests::a_clear_after_a_cut_takes_a_new_slot_and_the_old_one_stays_held: pass.
  • Same two plus all of draw::: 22/22 pass with --features diagnostics and VICTORY_D3D12_DEBUG=1, --test-threads=1.
  • The tests catch a missing cache. At head, with the cache lookup forced to miss (.filter(|_| false)), both fail on the slot count: "eight clears of one view, one slot" (65528 vs 65535) and "one slot before the cut" (65534 vs 65535).
  • Full lib suite at head: 228 passed, 2 failed, 1 ignored. The 2 failures are descriptor::host::tests::dynamic_slots_are_held_until_released (NumDescriptors >= HEAP_VIEWS) and memory::tests::lists_share_a_table_and_copies_keep_their_views ("past the dynamic slots"). Both also fail alone (--test-threads=1) on base f3578e38 and on current main 63bcca4 on this machine, so this PR does not cause them.
  • A trial merge onto current main 63bcca4 merges cleanly (auto-merge in draw.rs) and gives 231 passed with the same 2 failures that main already has.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean.

AI-assisted (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Repeated unordered-access clears of one view in one list now share a single dynamic slot via State::clear_slots, keyed on (view heap, View, host resource), and the host view is made before a slot is taken so a view the host cannot make no longer leaks one. The key is sound (View derives Ord, the resource is kept alive by list.keep), the cache is cleared in State::cut next to root_slots, and the tests check slot counts and the texel values each view holds after its last clear.

CI: passing.


Generated by Claude Code

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

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 22/22 passed (including clears_of_one_view_in_a_list_share_a_dynamic_slot and a_clear_after_a_cut_takes_a_new_slot_and_the_old_one_stays_held).
  • cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released: 21/21 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly reuses a single dynamic slot for repeated clears of the same view in a command list and allocates a fresh slot after a cut.

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

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

The slot-sharing idea and cut/reset handling are clean, and both new tests (clears_of_one_view_in_a_list_share_a_dynamic_slot and a_clear_after_a_cut_takes_a_new_slot_and_the_old_one_stays_held) plus all 22 draw::tests pass on RTX 5090 and WARP under VICTORY_D3D12_DEBUG=1.

However, reordering make_view(view, at) before visible.make_at(slot, view) in State::clear_view (crates/victory-gpu/src/draw.rs) introduces a descriptor-identity bug on cache misses that did not exist on main:

  1. Unaligned buffer UAVs (shadow::for_view → shadow::exact / Arena) can clear the wrong slice silently.

    • On main, visible.make_at(slot, &view) ran first, pinning own: Option<Arc<Shadow>> inside visible.kept[slot], so the subsequent make_view(&view, at) upgraded the exact same live Weak<Shadow> in shadow::EXACT and wrote matching (chunk, FirstElement) to both descriptors.
    • Here, make_view(view, at) runs first, and make_view (crates/victory-gpu/src/descriptor/host.rs:1461) does make(view, at).flatten().map(|k| k.0), dropping k.1: Option<Arc<Shadow>> immediately. If no other heap slot currently holds that exact shadow, Shadow::drop (shadow.rs:694-698) immediately calls arena().give_back(slice) and kills the Weak in EXACT while at still points to the freed Arena offset.
    • If another recording thread or heap sync allocates from ARENA before visible.make_at(slot, view) runs, that unrelated view v2 takes v1's freed offset (e.g. offset 0) and visible.make_at(slot, view) allocates a different offset (e.g. 256, or even another Arena chunk).
    • Reproducing that sequence with ClearUnorderedAccessViewUint(visible.gpu(slot), at, &resource, ...) on both RTX 5090 and WARP under VICTORY_D3D12_DEBUG=1 leaves v1 uncleared ([0, 0, 0, 0]) and silently overwrites v2's Arena slice with v1's clear values — with zero D3D12 Debug Layer messages because both slices share the same Arena chunk buffer. (If visible.make_at spills into a new chunk, the GPU and CPU descriptors also disagree on the ID3D12Resource and clear_slots caches the stale resource.as_raw().)
    • Even single-threaded, every unaligned cache miss allocates and immediately drops an Arena slice and MARKS range in make_view, only to allocate them again in visible.make_at.
  2. Concurrent texture eviction/recreation mismatch.

    • Between make_view(view, at) and visible.make_at(slot, view), a concurrent host texture eviction/recreation can cause visible.make_at to populate slot with a different ID3D12Resource (or a null view) than the resource written into at and stored in clear_slots.

Suggested fix:
On a cache miss, build the view once while retaining Kept (both the ID3D12Resource and Option<Arc<Shadow>>), and copy the already-created non-shader-visible descriptor from at into visible.cpu(slot) via CopyDescriptorsSimple (storing kept and the slot's stride/mark/address metadata on visible). That guarantees bit-for-bit identical GPU and CPU descriptors referencing the same resource and Arena slice, closes both race windows, and avoids creating the UAV twice.

…rena slice

On a miss, the clear makes its descriptor in a CPU-only heap, keeps the
resource, the optional arena slice and the write mark it was made from,
copies it into the reserved dynamic slot, and attaches the same metadata
there. Before, the slot was made afresh after the CPU descriptor's arena
slice had been let go, so another view could take that slice in between,
and a remade texture could leave the two descriptors on different
resources. The cache key now holds the exact shadow as well, so slices of
one arena buffer don't share a slot; an unmakeable view returns before
reserving one.

Test: an_unaligned_clear_keeps_its_arena_slice_until_the_slot_is_written
allocates a second slice at that point and checks both after the clear.

Review fix for Marvisak#2871 (reported on an RTX 5090 and WARP).

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

r4tedx commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@fuzeofficlal Thanks for tracing the reorder and for reproducing the wrong-slice clear on the RTX 5090 and WARP. Your suggested fix is what 9e75580 does:

  • On a miss, State::clear_view builds the view once in the CPU-only heap with make_cpu_view, which keeps what make_view dropped: the ID3D12Resource, the optional Arc<Shadow> holding the arena slice, and the write mark.
  • That descriptor is copied into the shader-visible slot with CopyDescriptorsSimple (HostHeap::copy_view_at). The slot then holds the same resource, slice and mark, plus its stride and address metadata. Both descriptors are bit-for-bit the same and name the same resource, and the view is no longer made twice.
  • The clear_slots key now includes the exact shadow as well as the resource, so two slices of one arena buffer don't share a slot.
  • A view that can't be made returns before a slot is reserved.

New regression test: draw::tests::an_unaligned_clear_keeps_its_arena_slice_until_the_slot_is_written. Between making the CPU descriptor and installing the slot, it allocates a second, equally sized arena slice. It then submits a typed UAV clear and checks that the first slice has the clear value and the second still has its sentinel. With the old order, the second allocation could take the first descriptor's freed slice.

#2844 deals with the same descriptor-slot lifetime hazard from the recording side (keeping CPU descriptor slots until recording consumes them). This change only covers the clear path.

Tested:

  • cargo test -p victory-gpu --features diagnostics --lib -- --test-threads=1 on WARP: 246 passed, 0 failed, 1 ignored. That includes the new test and the two earlier slot tests.
  • cargo clippy -p victory-gpu --features diagnostics --all-targets: no errors, and no warnings in the two changed files.

Not verified: NVIDIA hardware, and a run under VICTORY_D3D12_DEBUG=1. The title was not used in these tests.

AI-assisted

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.

7 participants