Skip to content

Shaders: clamp ResourceDescriptorHeap and SamplerDescriptorHeap indices to host limits - #584

Closed
FibbS6 wants to merge 1 commit into
Marvisak:mainfrom
FibbS6:fix/heap-handle-bounds-clamp
Closed

FibbS6 wants to merge 1 commit into
Marvisak:mainfrom
FibbS6:fix/heap-handle-bounds-clamp

Conversation

@FibbS6

@FibbS6 FibbS6 commented Nov 24, 2026

Copy link
Copy Markdown

Problem

Dynamic bindless indices passed to createHandleFromHeap can exceed the host descriptor heap bounds (e.g., from uninitialized or out-of-range shader values), causing immediate GPU page faults and device removal on NVIDIA D3D12.

Fix

  • Clamp ResourceDescriptorHeap and SamplerDescriptorHeap indices emitted by lower.rs to the configured host heap limits before calling createHandleFromHeap.

Verification

  • cargo xtask policy — passed (151 files ok)
  • cargo clippy -p victory-shader --lib -- -D warnings — 0 warnings
  • cargo test -p victory-shader -- --test-threads=1 — 69 passed, 0 failed
  • Verified on RTX 4070: resolves out-of-bounds bindless GPU page faults.

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech 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 Windows 11 D3D12 (WARP):

  • All 69 \�ictory-shader\ tests pass.
  • Using unsigned integer comparison (\IULT) against host heap bounds (\1_000_000\ for ResourceDescriptorHeap, \2048\ for SamplerDescriptorHeap) correctly clamps out-of-range/negative indices to slot 0, avoiding GPU page faults on NVIDIA hardware.
  • \cargo clippy -p victory-shader\ is clean with 0 warnings.
  • \cargo xtask policy\ passes (151 files checked, 0 violations).

@schmiticle

Copy link
Copy Markdown

Black screen before the menu with this PR (RX 9070 XT)

Summary: With #584 applied to main, Grand Theft Auto VI never reaches the menu. The title
keeps presenting (~590 fps, 13 draws/frame) but the window stays black and input does nothing.
main without #584 reaches the loading screens on the same machine.

Setup

Results

Build Result
main (d2a4bae) loading screens
main + #584 only black from start-up, no menu, no input response
main + 182 open PRs up to #560 intro cutscene: audio and title cards (scene black, as in the README)
main + those + #565…#580 not black
main + those + #584 black

Found by bisecting a local stack of open PRs, then confirmed with main + #584 alone.

Console in the black state

  • victory_gpu::host every 5 s: ~580–597 fps, 1.7 ms/frame, 13 draws/frame, 0 dropped,
    0 indirect
  • no errors or warnings, no device removal
  • shaders from the cache and translated so far hits=0 misses=426 at frame 65536
  • Enter / Esc (keyboard pad) have no effect

Guess: the title uses ResourceDescriptorHeap / SamplerDescriptorHeap indices above
the clamp, so after clamping, shaders read the wrong descriptors from the first frames.
Happy to test a revised version or capture a diag build log.

@MartinIndra02

Copy link
Copy Markdown

Root cause of the black screen reported above on RX 9070 XT:

In crates/victory-shader/src/emit/shader.rs, heap_handle hardcodes 1_000_000 for ResourceDescriptorHeap:

let limit = self.m.i32(if sampler { 2048 } else { 1_000_000 });
let in_bounds = self.m.cmp(block, super::bitcode::pred::IULT, index, limit);
let index = self.m.select(block, in_bounds, index, zero);

The title requests a view heap larger than 1,000,000 slots (1,245,184 slots). On NVIDIA D3D12 the host caps the view heap at 1,000,000 (host view heap capped at 1000000), but on AMD hosts that allow >1M Tier 3 descriptors, valid indices >= 1_000_000 are silently rewritten to slot 0.

Two related issues with hardcoding the bound and mapping OOB to 0:

  1. Any host heap smaller than 1_000_000 (e.g. smaller heaps or unit tests) is still not bounded to its actual size.
  2. Redirecting OOB indices to slot 0 can alias a live descriptor of a different resource type (e.g. texture SRV vs buffer UAV), whereas a dedicated guard/null slot (as in Shaders, GPU: a heap index computed from data stays inside the heap #483) avoids type mismatches.

Code review note; no game data. AI-assisted.

@d1ll3x3

d1ll3x3 commented Nov 24, 2026

Copy link
Copy Markdown

tested: #584 with the bound set to the host heap's real size | base: main@ffe310e + #431 + #568 + #67 + #493 + #585, plus #609's Staging/Shadow Drop impls ported onto #431's retire | on: Windows 11, RX 7800 XT (RDNA3, 16 GB), 32 GB RAM, diag build, VICTORY_D3D12_DRED=1, Xbox Series 1.0.2.1

This confirms MartinIndra02's diagnosis from the AMD side, and the clamp itself fixes a real crash on RDNA3 once its bound is right.

The bound has to cover the dynamic slots too. On this host the view heap is the title's 1,245,184 slots plus DYNAMIC (1 << 18), so 1,507,328 slots (DRED reports the heap as +0x2e00000 bytes). With the bound at 1,245,184, the dynamic tables would be clamped as well. Locally I changed only the constant:

let limit = self.m.i32(if sampler { 2048 } else { 1_507_328 });

That is a local value for this GPU. The real fix needs the host heap's size, count + DYNAMIC (or MIN_DYNAMIC, or the capped count), passed into the translation rather than hard-coded.

What it fixes here. Without the clamp, the opening cutscene always ended in a page fault at an address with no allocation, neither live nor recently freed. It hit the same VA in three runs on two builds:

page fault at 0x1ae1dc000; view heap 0x104f30000+0x2e00000: ... past its end (slot 88692224)
stopped at command 193 of 223 (op 6): dispatch 48x48x1 [compute 4ceef06fab7b49dc]

With that pipeline skipped, a second one followed: slot 16728192, compute 26b8bbb2e7ae4dc6, 6x54x1. Both slot numbers are far past the 1.5M-slot heap, so these are garbage heap indices, not freed resources.

With the clamp (bound 1,507,328), no VICTORY_SKIP_PIPELINES for these two:

  • First run: no page fault, 6 min 07 s, then a TDR without a page fault in compute eeed82b877b44b5f (12x1x1). That shader loops for (i = lane; i < n; i += 1024), with n read from a buffer at a data-dependent offset, so a garbage n makes it run until the TDR.
  • With VICTORY_SHADER_LOOP_LIMIT=4096 as well: no device removal at all. It is still running in the cutscene after 16 min (frame 2,049) at about 0.4 fps, and the scene is still dark, as the README says.

The only skip left was the known mesh hang 04b9be3756e8f41c/1af4a50c7fb40c97 (#597, #71).

On slot 0: I didn't see a type-mismatch failure from redirecting to slot 0, but I can't rule one out. A null or guard slot as in #483 sounds safer.

AI-assisted (Claude Code).

@MartinIndra02

Copy link
Copy Markdown

Following up on the root-cause analysis and @d1ll3x3's RDNA3 verification above, I implemented the dynamic host-heap bound + dedicated null guard slot version on a main-based branch (ffe310e):

Design (addresses all three issues with the hardcoded 1_000_000 -> 0 clamp)

  1. Runtime bound per bound heap (covers title + dynamic slots on AMD, NVIDIA, and small heaps):
    • Instead of baking a compile-time constant into DXIL (which breaks AMD >1M heaps, misses DYNAMIC slots, and doesn't know which heap is bound at draw time), shaders that index ResourceDescriptorHeap or SamplerDescriptorHeap load [view_guard, sampler_guard] once in the entry block from a 2-dword root constant b0 in HEAP_LIMITS_SPACE = 4 (leaving spaces 2/3 free for #710/#704).
    • Shaders that don't index descriptor heaps emit nothing (HostShader::heap_limits == false, zero root-signature overhead).
    • HostPipeline adds one D3D12_SHADER_VISIBILITY_ALL 2-dword root constant parameter when any stage sets heap_limits, and State::bind passes [view_heap.guard(), sampler_heap.guard()] for the currently bound host heaps.
    • shader_cache.rs encodes/decodes heap_limits cleanly.
  2. Dedicated permanently-null guard slot (never aliases live slot 0):
    • HostHeap reserves the final slot total - 1 on view heaps (dynamic = DYNAMIC - 1 or MIN_DYNAMIC - 1, so total stays within PORTABLE_VIEWS when capped and covers count + dynamic on >1M Tier 3 heaps) and appends 1 guard slot on sampler heaps below D3D12's 2048-slot cap (make(count + 1), falling back to count.saturating_sub(1) at 2048).
    • fill_null initializes all total slots (including the guard slot), and neither mirror nor dynamic can ever overwrite it.
    • Clamping with select(index <u guard, index, guard) leaves every valid title & dynamic slot (0..guard) untouched and sends every OOB/wrapped index (>= guard) to the dedicated null guard slot.

Verification (Windows 11, RTX 3080 driver 621.24, WARP, Rust 1.98.1)

  • New GPU test emit::waterfall_tests::out_of_bounds_heap_index_clamps_to_null_guard_not_slot_zero (RTX 3080 + WARP): sends lanes 0..16 to slot 0 (red = 1.0) and lanes 16..32 to OOB slot 16384 — verifies lanes 0..16 read 1.0 and lanes 16..32 read 0.0 from the null guard slot (no page fault, no aliasing to slot 0).
  • New GPU test descriptor::host::tests::host_heap_guard_covers_title_and_dynamic_slots (RTX 3080 + VICTORY_D3D12_DEBUG=1): verifies guard() sits past both title and dynamic slots on view heaps and past title slots on sampler heaps.
  • cargo test -p victory-shader --lib -- --test-threads=1: 121 passed, 0 failed, 1 ignored
  • cargo test -p victory-gpu --lib -- --test-threads=1: 51 passed, 0 failed, 1 ignored
  • cargo clippy -p victory-shader -p victory-gpu --all-targets -- -D warnings (default + --features diagnostics): 0 warnings
  • cargo xtask policy: 154 files ok

Ready for @FibbS6 or a collaborator to pick up / open as a PR whenever PRs reopen.

No game data used. AI-assisted; all builds and GPU tests executed on my machine.

@mercuryVM mercuryVM 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 0bcf2df on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 69 passed, and cargo clippy -p victory-shader --all-targets is clean. victory translate --validate --pipelines on the local title executable still reports 12 of 12 programs.

The bound baked into the shader doesn't match the heap the host actually creates, so it misses the out-of-range case and can break in-range ones:

  • emit/shader.rs (heap handle): limit is a compile-time 1_000_000 for views. The host view heap is title count + DYNAMIC (descriptor/host.rs, HostHeap::new, with DYNAMIC = 1 << 16) and is usually far smaller than 1M. An index between the real heap size and 1M still passes the check and reads past the heap, which is the fault this PR targets.
  • On resource-binding tier 3, make(count + extra) can succeed above PORTABLE_VIEWS. Then a valid index ≥ 1,000,000 is silently redirected to slot 0, a wrong but live descriptor, so a title with a large heap renders with the wrong resources instead of faulting.
  • Fix: the shader can't know the heap size at translate time, so pass it in. For example, add the bound as a root constant that draw.rs fills from HostHeap::count + DYNAMIC (2048 for samplers, which is the D3D12 shader-visible sampler limit). Or do the clamp where the host already knows the heap: drop or redirect the draw on the CPU side when a table index the title wrote is out of range. Selecting slot 0 should also be a null descriptor of the right type, not whatever the title put in slot 0.

AI-assisted (Claude Code).

@MartinIndra02

Copy link
Copy Markdown

Quick in-title confirmation for fix/shader-heap-bounds-clamp (05c681c) on Windows 11 / RTX 3080 (621.24), diag build with VICTORY_D3D12_DRED=1, VICTORY_SHADER_LOOP_LIMIT=1000 (stacked on main ffe310e + #439 + #431 + #724/#725 + #783 + #776 + #757):

  • Menus, loading illustrations, and world entry all rendered with 0 DRED page faults and 0 host pipeline refusals across 1,903 translated shaders (1.0 – 1.5 fps in the world before an unrelated mesh TDR at command 80 of 140 (op 42)).

No game data posted. AI-assisted; build and title run executed on my machine.

PastiiJay added a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 24, 2026
Yaekai pushed a commit to Yaekai/VIctory that referenced this pull request Nov 24, 2026
PastiiJay added a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 25, 2026
@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Main clamps heap indices already (emit/shader.rs heap_handle: an index above HEAP_VIEWS-1 reads the last slot, which the host keeps null).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
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.

7 participants