Skip to content

GPU: release descriptor tables after a rejected bind - #1126

Open
EatCodeSleep2 wants to merge 1 commit into
Marvisak:mainfrom
EatCodeSleep2:fix/release-rejected-descriptor-reservations-911
Open

EatCodeSleep2 wants to merge 1 commit into
Marvisak:mainfrom
EatCodeSleep2:fix/release-rejected-descriptor-reservations-911

Conversation

@EatCodeSleep2

Copy link
Copy Markdown
Contributor

Summary

  • When a draw or dispatch fails to allocate a fresh descriptor table, return the ranges reserved during that bind and remove their cache entries.
  • Keep ranges and cache entries from previously accepted binds available until the command list is reset or released.
  • Add a synthetic D3D12 regression that first accepts a two-slot table, then rejects a four-plus-three-slot bind with six slots free. It checks that the rejected bind returns the four slots, preserves the accepted table, and can be retried after pressure clears.

Refs #911. This addresses the partial reservation described there. The frequency and performance impact during gameplay remain unmeasured.

Verification

  • cargo test --locked -p victory-gpu --lib --features diagnostics -- --quiet (54 passed, 1 ignored)
  • Focused regression passed with native D3D12 and with VICTORY_D3D12_WARP=1. Disabling rollback reproduced the retained table on native D3D12.
  • cargo clippy --locked -p victory-gpu --all-targets --features diagnostics -- -D warnings

AI-assisted. Synthetic inputs only.

@adamdotpy

Copy link
Copy Markdown

Grepping the reviews for this one turns up nothing yet, so here is one with the failure reproduced both ways. Reviewed on Windows 11 / Rust 1.98.1, bb65bc3 on ffe310e (current main, merges as it stands).

The leak is real and the test proves it. With self.release_rejected_tables(reserved_before_bind); replaced by nothing and the test unchanged:

assertion `left == right` failed
  left: 2
 right: 1
test result: FAILED. 0 passed; 1 failed

That is assert_eq!(state.dynamic.len(), 1) — the table the accepted stage reserved plus the one the rejected stage left behind. So the test pins the fix rather than passing next to it. It also really reaches the device here: I checked, because the early return in the no-host case would make it a vacuous pass, and the probe printed "host device present". Green on the branch as pushed: cargo test -p victory-gpu 51 passed / 1 ignored, clippy --all-targets --all-features -D warnings clean, cargo build --release -p victory builds.

The shape is better than the flag it replaces. FreshTable's Drop means the rollback path and the list-teardown path are literally the same code, so they cannot drift — the old impl Drop for State loop was a second place to keep the same list in sync. And drain(reserved_before_bind..) gives back exactly what this bind reserved: a table reused from an earlier successful bind is not in that tail, so it survives, which is right, because it was already accounted for.

Nothing else needs rolling back, and it is worth saying so in the function. I went looking for the second half of the leak and there is not one: the constant data for an abandoned fresh table goes through list.scratch (host.rs:1081), which bump-allocates from the list's own upload chunks and is reclaimed with the list, and the table_entry / readable calls already made for non-fresh tables only record diagnostics. Descriptor slots are the only thing that has to be given back. One sentence in release_rejected_tables saves the next reader the same search.

Two things I would change in the test, both about surviving machines that are not this one.

  • capacity - 8 underflows (and panics in a debug test) if a host's dynamic region is smaller than nine slots; GetDesc().NumDescriptors - count() is vendor-dependent. A let Some(room) = capacity.checked_sub(8) else { return }; keeps the test honest rather than failing on a host with a small view heap.
  • The free ranges are observed by allocating them (view_heap.dynamic(2), dynamic(4)) and releasing by hand, then asserting returned + 4 == tail. That tests the allocator's cursor as much as the fix, and your own comment says the scan splits runs at the cursor (GPU: Dynamic descriptor allocator can falsely report exhaustion when a free range crosses the search cursor #871) — so an allocator change would break this test while the fix stayed correct. A #[cfg(test)] fn free_dynamic(&self) -> u32 that counts the untaken slots would let the test say what it means: the count before a rejected bind comes back after it, and state.fresh_tables keeps the accepted key. The layout arithmetic could go.

Minor. test_view_heap's heap is released through the vtable (object::slot(..., 0x10)) at the end of the test, so a failing assert leaks it in the test process. Harmless, but a small guard struct whose Drop does that would make the test robust to its own failure, which is when you most want to re-run it.

@MichaelSmithAI

Copy link
Copy Markdown

The authored reservation test also passed under Wine/GE 3.2 on an RTX 4090. The checkpoint sits before the stage loop, so if a later stage runs out of room, the bind returns the ranges acquired since that checkpoint and preserves the cached table from the earlier accepted bind.

One qualification to the earlier review: table_entry calls readable for a mirrored view, and readable does more than log. Depending on the view, it can record a texture upload or state transition, refresh a shadow buffer, or register a UAV range for copyback. Fresh constant views can also schedule a deferred fill. Releasing the new descriptor reservations leaves those list operations in place. The regression uses empty descriptor slots and checks ownership without submitting the list, so it establishes the reservation fix. Whether preparation from rejected binds affects a title is still open.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and tested rejected descriptor reservation rollback on RTX 5090 and WARP (VICTORY_D3D12_DEBUG=1). Three remaining gaps to watch out for on top of rolling back tables():

  1. HostList side effects before a later stage fails: In a multi-stage graphics bind (e.g. VS succeeds, PS runs out of dynamic slots), State::bind has already recorded SetDescriptorHeaps / SetPipelineState / root constants, allocated list.scratch, queued defer_fill, and run readable() (ensure_state transitions + shadow.gpu_writes(), which copies stale host buffer contents back over title memory after submission even though the draw/dispatch never ran). Splitting stage reservation (reserve_stage) before any HostList mutation keeps rejected binds side-effect-free.
  2. Post-bind early-return leaks & null index buffer: prepare_for checks indexed after self.bind() (and without a v.address == 0 || v.size == 0 guard, a null index buffer falls into shadow::find(0, 0) allocating a 4 MiB loose shadow window), draw validates RECTANGLES vertex buffers after self.prepare(), and dispatch_mesh casts ID3D12GraphicsCommandList6 after prepare_for(). Any rejection there still retains the dynamic tables just reserved by bind().
  3. Contiguous span split across HostHeap::dynamic's next cursor: In #911's 8-slot repro (2 used, 6 contiguous free [2..8]), reserving 4 + 3 advances *next to 6 before rolling back [2..6]. Without allowing the wrap-around scan to span across *next (#871), an immediate contiguous dynamic(6) request still fails because [2..8] is split at 6.

Reference commit with synthetic SM 6.6 GPU readback + multi-stage/post-bind regression tests: fuzeofficlal@4fd6e2c

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

On bb65bc3, the rollback makes the descriptor slots reusable. A populated slot can still hold its ID3D12Resource reference after rollback and command-list reset. HostHeap::make_at stores that reference in kept, and HostHeap::release clears only the occupancy bits. The heap can therefore retain the reference until the slot is rewritten or the heap is destroyed. This behavior is also present on ffe310e. The empty-slot fixture checks reservation recovery and leaves resource lifetime untested. This is a source inspection. I did not measure runtime memory impact.

AI-assisted.

@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown

@EatCodeSleep2 The composition with #562 kept your original commit. In a capped 65,536-slot fixture, 16 and 178 slots fit before a 256-slot request rejected the bind. The rollback returned all 194 slots, restored the prior CBV version and accepted a retry after the blocker was removed. The reservation and separate mixed replay checks passed on AMD, NVIDIA and WARP with GBV.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at bb65bc3: cargo test -p victory-gpu passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@FunWithFlagsDev

Copy link
Copy Markdown

Following up on #911 and the descriptor rollback implemented here, I found a potentially related but distinct preparation-side effect.

During State::bind, readable() can enqueue Step::GpuWrite before a later table allocation fails. Although the draw or dispatch is rejected, these deferred write declarations remain and can subsequently participate in shadow snapshots, pending-range exclusions and copy-back.

The current #1126 diff releases descriptor reservations but does not remove these declarations.

I prepared an experimental opt-in census measuring rejected write declarations per submission, including:

  • Total declared bytes.
  • Unique range coverage.
  • Overlap with other write declarations.
  • Exclusive rejected-range coverage.

The diagnostic patch preserves baseline behavior. The interval analysis passed nine Python tests, including 20,000 synthetic submissions and 240,183 range declarations, with no discrepancies against an independent oracle.

Native compilation and end-to-end bind validation remain pending.

#1322 may mitigate the stale-data consequence on the synchronous path, but the unnecessary write declarations and their potential bookkeeping costs are a separate question.

I suggest measuring their actual frequency before extending the rollback.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/draw.rs, crates/victory-gpu/src/pipeline.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

@JurajSimicevic

Copy link
Copy Markdown

@EatCodeSleep2, in case it saves you the rebase the triage asked for: your commit on today's main (940d32f), with you as the author: https://github.com/JurajSimicevic/VIctory/tree/rebase/1126 (30248b9). Use it or ignore it.

It is not a mechanical rebase, so here is what changed:

  • main now makes a table afresh only where it reaches past a host heap smaller than the title's (the capped heap on NVIDIA and WARP). Constant buffer tables are read from the title's slots, so your test's tables would no longer be made afresh. The test now uses a 1,000,000-view title heap, which the host caps, and puts its tables past the host's slots. It skips where the host takes the whole heap.
  • In the same list, root views' slot runs and unordered-access clears now hold dynamic slots too. Each held range records whether it is a fresh table, and only those are given back. Giving back a root view run would let the next root view take a slot that is free again.
  • Ranges in our own view heap (MEMORY_HEAP) keep their own release.

On an RTX 5080 (driver 32.0.16.1742), the test passes, and it fails with the rollback turned off (left: 2, right: 1). cargo test -p victory-gpu: 142 passed (146 with --features diagnostics) and 2 failed, the two capped-heap tests that also fail on main there (#1751). Clippy is clean with and without --all-features, and there are no new rustfmt hunks.

In the world, which the description lists as unmeasured. The base is 940d32f + #1775's typed-UAV check + #1762, with #1968's program A skipped and DRED on, on the same card. The drop counter is logged at each power of two:

counter reached, world time without this with this
1,024 +0 s +36 s
4,096 +41 s +71 s
16,384 +86 s +204 s
32,768 +354 s +261 s

Fewer drops in the first minutes, and about the same after five; one run each. On this card most of the drops come from the 2^16 dynamic slots of the capped heap running out (#747), which this change does not touch.

AI-assisted.

@Machad3x

Copy link
Copy Markdown

Merge note: the offered rebase (30248b9, on 940d32f) conflicts with #688 in crates/victory-gpu/src/draw.rs. Both change what State::dynamic is: #688 makes it a shared descriptor::lease::Lease (so slots can be taken back from lists that ran), while this change makes it a Vec<Held> so that a rejected bind can release only its own fresh tables. The conflicting places are the field, the release in Drop/reset, the three pushes (root-view run, single slot, fresh table) and the two test blocks.

Checked by cherry-picking 30248b9 onto a local stack (940d32f plus #688 and other open fixes); I did not resolve or run it. Whichever lands second needs the lease to be able to give back the entries of one bind.

AI-assisted (Claude Code).

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.