GPU: retry a fallback descriptor heap that could not be created #4806

Open
Jineggegg wants to merge 16 commits from upstream-pr/4806 into main
Jineggegg commented 2026-12-01 09:33:29 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Jineggegg on 2026-12-01T09:33:29Z.


Based on main at e1251c31; one commit.

Problem

Sometimes the title binds no view heap or no sampler heap, but the draw uses a root signature with CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED or SAMPLER_HEAP_DIRECTLY_INDEXED. The host then binds a 1-descriptor fallback heap (Host::fallback_view_heap, Host::fallback_sampler_heap).

Both heaps are cached as OnceLock<Option<ID3D12DescriptorHeap>>. If the first CreateDescriptorHeap fails (memory pressure, or a transient driver refusal), None stays cached for the rest of the run. From then on, every list that needs the fallback sets the root signature without both heaps bound. That is debug-layer error 1419, and on NVIDIA it removes the device.

The draw path also marks its heaps as set when one of the two is missing (draw.rs, host_heaps_set). So it does not try again until the title's heaps change.

Reproduction

This is a resource-exhaustion path, so a normal run does not hit it on demand. To force it:

  1. Make the first fallback heap creation fail. For example, return an error from the closure in fallback_view_heap on its first call, or run with VRAM nearly exhausted by another process.
  2. Run a scene where a pass binds no view heap but its root signature indexes the heap directly. Turn the D3D12 debug layer on.
  3. Before this change: error 1419 on every such draw for the rest of the run, and the device is removed on NVIDIA. After: only the draws before the next successful creation are affected; binding then returns to normal.

Change

  • New helper made_once(cell, make). It keeps a value in a OnceLock<T> only if make produced one.
    • A failure leaves the cell empty, so the next caller tries again.
    • If several callers race, all of them get the value published first. The extra heaps are dropped.
    • A caller that fails while another thread publishes gets the published heap.
  • fallback_view and fallback_sampler become OnceLock<ID3D12DescriptorHeap> and use made_once. The heap creation code is unchanged.
  • In draw.rs (heap binding before the root signature), host_heaps_set is set only when both heaps were bound. While a fallback is missing, the next draw or dispatch tries again.

Once the heap exists, nothing changes: the fast path is a single OnceLock::get, as before. In a normal run both heaps are always bound (the title's or the fallback), so host_heaps_set behaves as before.

Tests

New unit tests, no GPU needed (host::made_once_tests):

  • a_failure_is_tried_again_and_a_success_is_kept: failures are not cached, the first success is kept, and nothing is made after it.
  • racing_callers_all_get_the_value_published_first: a caller that fails while another publishes gets the published value; 8 racing threads (half of them failing) all see one value.

Results on Windows (toolchain pinned in rust-toolchain.toml, Rust 1.98.1; RTX 5090, driver 621.14):

  • Clippy. main (e1251c31) does not pass -D warnings at the moment: 4 warnings without --all-features (field loop_caps is never read in crates/victory-shader/src/emit/lower.rs; manual implementation of .is_multiple_of() in crates/victory-gpu/src/host.rs; match can be simplified with .unwrap_or_default() in crates/victory-hle/src/modules/xmem.rs; method ranged is never used in crates/victory-gpu/src/shadow.rs) and 5 with it (also function mb is never used in crates/victory-gpu/src/copystat.rs). Both CI configurations were therefore run without -D and compared with main's diagnostics. This branch adds none, in either configuration.
  • cargo test --workspace --locked --no-fail-fast on the GPU: 2522 passed, 6 failed, 96 ignored (main: 2520 passed, 5 failed, 96 ignored). The failures main has itself (list::thin::tests::bindings_record_no_work, shader_cache::tests::every_switch_of_the_translator_is_in_the_key, emit::bitcode::opt::tests::hw_tests::split_lds_wave32_read_after_read_barrier_coalescing_and_parity, policy::tests::repository_passes_policy), and host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end. That one is intermittent on main too: running the victory-gpu lib tests of main, this branch and two other branches in turn in one GPU window, it failed in 1 of 22 runs on main and 0 of 22 on this branch. The test expects each round's first submission (a few milliseconds of GPU copies) to be still in flight when the second is submitted; in the failing round the second submission took 110 µs because the first had already finished. This change does not touch that path. So no new failure. Failing on main but passing here: victory_hle::modules::xmem::tests::a_read_by_range_waits_only_for_a_remapping_that_meets_it. Unit tests that need no device, cargo test -p victory-gpu --lib: host::made_once_tests: 2 passed, 0 failed.

Performance

Nothing to measure on the normal path: once a fallback heap exists, the only cost is the same OnceLock::get as before, and the draw path binds the same heaps. The change only matters after a failed creation. It is a correctness fix for an edge case, not a speed change.

Risk

  • Low. A device that fails the allocation every time now tries again on each draw that needs the heap. That costs one failed CreateDescriptorHeap and one redundant SetDescriptorHeaps per such draw, only while creation keeps failing.
  • Callers that race can briefly create two 1-descriptor heaps; one of them is dropped.

Attribution

Ported from the author's own fork, where the same fix was made for its empty view heap. No third-party code.

AI assistance: the change, its tests and this description were prepared with an AI coding assistant (Claude Code) and checked by the results above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5582927664 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4806). Originally posted by [Jineggegg](https://github.com/Jineggegg) on 2026-12-01T09:33:29Z. --- Based on `main` at `e1251c31`; one commit. ## Problem Sometimes the title binds no view heap or no sampler heap, but the draw uses a root signature with `CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED` or `SAMPLER_HEAP_DIRECTLY_INDEXED`. The host then binds a 1-descriptor fallback heap (`Host::fallback_view_heap`, `Host::fallback_sampler_heap`). Both heaps are cached as `OnceLock<Option<ID3D12DescriptorHeap>>`. If the first `CreateDescriptorHeap` fails (memory pressure, or a transient driver refusal), `None` stays cached for the rest of the run. From then on, every list that needs the fallback sets the root signature without both heaps bound. That is debug-layer error 1419, and on NVIDIA it removes the device. The draw path also marks its heaps as set when one of the two is missing (`draw.rs`, `host_heaps_set`). So it does not try again until the title's heaps change. ## Reproduction This is a resource-exhaustion path, so a normal run does not hit it on demand. To force it: 1. Make the first fallback heap creation fail. For example, return an error from the closure in `fallback_view_heap` on its first call, or run with VRAM nearly exhausted by another process. 2. Run a scene where a pass binds no view heap but its root signature indexes the heap directly. Turn the D3D12 debug layer on. 3. Before this change: error 1419 on every such draw for the rest of the run, and the device is removed on NVIDIA. After: only the draws before the next successful creation are affected; binding then returns to normal. ## Change - New helper `made_once(cell, make)`. It keeps a value in a `OnceLock<T>` only if `make` produced one. - A failure leaves the cell empty, so the next caller tries again. - If several callers race, all of them get the value published first. The extra heaps are dropped. - A caller that fails while another thread publishes gets the published heap. - `fallback_view` and `fallback_sampler` become `OnceLock<ID3D12DescriptorHeap>` and use `made_once`. The heap creation code is unchanged. - In `draw.rs` (heap binding before the root signature), `host_heaps_set` is set only when both heaps were bound. While a fallback is missing, the next draw or dispatch tries again. Once the heap exists, nothing changes: the fast path is a single `OnceLock::get`, as before. In a normal run both heaps are always bound (the title's or the fallback), so `host_heaps_set` behaves as before. ## Tests New unit tests, no GPU needed (`host::made_once_tests`): - `a_failure_is_tried_again_and_a_success_is_kept`: failures are not cached, the first success is kept, and nothing is made after it. - `racing_callers_all_get_the_value_published_first`: a caller that fails while another publishes gets the published value; 8 racing threads (half of them failing) all see one value. Results on Windows (toolchain pinned in `rust-toolchain.toml`, Rust 1.98.1; RTX 5090, driver 621.14): - Clippy. `main` (`e1251c31`) does not pass `-D warnings` at the moment: 4 warnings without `--all-features` (field `loop_caps` is never read in `crates/victory-shader/src/emit/lower.rs`; manual implementation of `.is_multiple_of()` in `crates/victory-gpu/src/host.rs`; match can be simplified with `.unwrap_or_default()` in `crates/victory-hle/src/modules/xmem.rs`; method `ranged` is never used in `crates/victory-gpu/src/shadow.rs`) and 5 with it (also function `mb` is never used in `crates/victory-gpu/src/copystat.rs`). Both CI configurations were therefore run without `-D` and compared with `main`'s diagnostics. This branch adds none, in either configuration. - `cargo test --workspace --locked --no-fail-fast` on the GPU: 2522 passed, 6 failed, 96 ignored (`main`: 2520 passed, 5 failed, 96 ignored). The failures `main` has itself (`list::thin::tests::bindings_record_no_work`, `shader_cache::tests::every_switch_of_the_translator_is_in_the_key`, `emit::bitcode::opt::tests::hw_tests::split_lds_wave32_read_after_read_barrier_coalescing_and_parity`, `policy::tests::repository_passes_policy`), and `host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end`. That one is intermittent on `main` too: running the `victory-gpu` lib tests of `main`, this branch and two other branches in turn in one GPU window, it failed in 1 of 22 runs on `main` and 0 of 22 on this branch. The test expects each round's first submission (a few milliseconds of GPU copies) to be still in flight when the second is submitted; in the failing round the second submission took 110 µs because the first had already finished. This change does not touch that path. So no new failure. Failing on `main` but passing here: `victory_hle::modules::xmem::tests::a_read_by_range_waits_only_for_a_remapping_that_meets_it`. Unit tests that need no device, `cargo test -p victory-gpu --lib`: `host::made_once_tests`: 2 passed, 0 failed. ## Performance Nothing to measure on the normal path: once a fallback heap exists, the only cost is the same `OnceLock::get` as before, and the draw path binds the same heaps. The change only matters after a failed creation. It is a correctness fix for an edge case, not a speed change. ## Risk - Low. A device that fails the allocation every time now tries again on each draw that needs the heap. That costs one failed `CreateDescriptorHeap` and one redundant `SetDescriptorHeaps` per such draw, only while creation keeps failing. - Callers that race can briefly create two 1-descriptor heaps; one of them is dropped. ## Attribution Ported from the author's own fork, where the same fix was made for its empty view heap. No third-party code. AI assistance: the change, its tests and this description were prepared with an AI coding assistant (Claude Code) and checked by the results above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
andrewpozdnakov7-jpg commented 2026-12-01 09:54:07 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by andrewpozdnakov7-jpg on 2026-12-01T09:54:07Z.


I cherry-picked 5156288 onto current main 5fabb46 as 8aa89eb. The Git author and patch ID match, and the unrelated release history drops out of the comparison.

One gap remains after the retry change. The draw path still reaches the root signature on the first missing-heap call. Victory's root signatures set both directly indexed heap flags, and D3D12 requires those heaps to be bound before the root signature. DirectX Shader Model 6.6 specification

Proposed follow-up commit returns from State::bind before recording a root signature while either heap is missing. The calling draw and dispatch paths already honor that result. The next call tries the fallback again.

A native Rust fixture using the exact binding blocks checked a missing view heap, a missing sampler heap and both missing. In all three cases the author head proceeded to the root signature. The patch skipped the recording step and, on retry, bound both heaps before the root signature. The fixture mocks D3D12 calls. cargo check --locked -p victory-gpu --jobs 2 and the staged policy check for the follow-up passed.

Please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding the follow-up into another commit.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7307373012 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4806#issuecomment-7307373012). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-12-01T09:54:07Z. --- I cherry-picked `5156288` onto current `main` `5fabb46` as `8aa89eb`. The Git author and patch ID match, and the unrelated release history drops out of the comparison. One gap remains after the retry change. The draw path still reaches the root signature on the first missing-heap call. Victory's root signatures set both directly indexed heap flags, and D3D12 requires those heaps to be bound before the root signature. [DirectX Shader Model 6.6 specification](https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_DynamicResources.html#setdescriptorheaps-and-setrootsignature) [Proposed follow-up commit](https://github.com/andrewpozdnakov7-jpg/VIctory-NVIDIA/commit/a1e50eb673ff3b7388859bbccfc021c0c934b260) returns from `State::bind` before recording a root signature while either heap is missing. The calling draw and dispatch paths already honor that result. The next call tries the fallback again. A native Rust fixture using the exact binding blocks checked a missing view heap, a missing sampler heap and both missing. In all three cases the author head proceeded to the root signature. The patch skipped the recording step and, on retry, bound both heaps before the root signature. The fixture mocks D3D12 calls. `cargo check --locked -p victory-gpu --jobs 2` and the staged policy check for the follow-up passed. Please retain the Git author on cherry-pick or rebase, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding the follow-up into another commit.
Jineggegg commented 2026-12-01 11:19:48 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Jineggegg on 2026-12-01T11:19:48Z.


Thanks for checking this against current main and for the follow-up. It's right: Victory's root signatures set both directly indexed heap flags, so binding the root signature with a heap missing was wrong.

I've taken your commit as it was, with you as the Git author (4572d52a, on top of a merge of main 5fabb46e into this branch). It applied cleanly. State::bind's three callers already skip the draw or dispatch when it returns false, and the two tests that assert it succeeds set both heaps first.

Checked on Windows (Rust 1.98.1, RTX 5090):

  • Both Clippy configurations (with and without --all-features) report the same 5 warnings as main 5fabb46e, and nothing else. CI's -D warnings stops on those 5 on main too.
  • cargo test --workspace --locked: 2489 passed, 1 failed, 96 ignored. The failure is modules::xmem::tests::a_read_by_range_waits_only_for_a_remapping_that_meets_it, which also fails on main. Nothing new.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7309489899 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4806#issuecomment-7309489899). Originally posted by [Jineggegg](https://github.com/Jineggegg) on 2026-12-01T11:19:48Z. --- Thanks for checking this against current `main` and for the follow-up. It's right: Victory's root signatures set both directly indexed heap flags, so binding the root signature with a heap missing was wrong. I've taken your commit as it was, with you as the Git author (`4572d52a`, on top of a merge of `main` `5fabb46e` into this branch). It applied cleanly. `State::bind`'s three callers already skip the draw or dispatch when it returns false, and the two tests that assert it succeeds set both heaps first. Checked on Windows (Rust 1.98.1, RTX 5090): - Both Clippy configurations (with and without `--all-features`) report the same 5 warnings as `main` `5fabb46e`, and nothing else. CI's `-D warnings` stops on those 5 on `main` too. - `cargo test --workspace --locked`: 2489 passed, 1 failed, 96 ignored. The failure is `modules::xmem::tests::a_read_by_range_waits_only_for_a_remapping_that_meets_it`, which also fails on `main`. Nothing new.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/4806:upstream-pr/4806'
git switch 'upstream-pr/4806'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/4806'
git switch 'upstream-pr/4806'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/4806'
git switch 'upstream-pr/4806'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/4806'
git switch 'main'
git merge --squash 'upstream-pr/4806'
git switch 'main'
git merge --ff-only 'upstream-pr/4806'
git switch 'main'
git merge 'upstream-pr/4806'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
trustno1-coder/VIctory-NVIDIA!4806
No description provided.