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

Closed
github-bot wants to merge 1 commit from gh-pr/584/head into gh-pr/584/base
Owner

@⁠FibbS6 on GitHub · 2026-11-24 05:13 UTC · original
FibbS6:fix/heap-handle-bounds-clamp → main · +8 −1 · 1 files · 1 commits · closed without merging

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.
**@⁠FibbS6** on GitHub · 2026-11-24 05:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/584) `FibbS6:fix/heap-handle-bounds-clamp` → `main` · +8 −1 · 1 files · 1 commits · closed without merging ### 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:09:14 +00:00
github-bot closed this pull request 2026-11-26 04:09:14 +00:00
Author
Owner

@⁠schmiticle on GitHub · 2026-11-24 08:15 UTC · original

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

  • GPU: AMD Radeon RX 9070 XT (RDNA4)
  • CPU: Ryzen 7 7800X3D, 32 GB RAM
  • OS: Windows [11]
  • Base: main at d2a4bae (#129)

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.

**@⁠schmiticle** on GitHub · 2026-11-24 08:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/584#issuecomment-7125911162) ### 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** - GPU: AMD Radeon RX 9070 XT (RDNA4) - CPU: Ryzen 7 7800X3D, 32 GB RAM - OS: Windows [11] - Base: `main` at `d2a4bae` (#129) **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.
Author
Owner

@⁠MartinIndra02 on GitHub · 2026-11-24 09:19 UTC · original

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 #483) avoids type mismatches.

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

**@⁠MartinIndra02** on GitHub · 2026-11-24 09:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/584#issuecomment-7127261814) 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`: ```rust 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 #483) avoids type mismatches. *Code review note; no game data. AI-assisted.*
Author
Owner

@⁠d1ll3x3 on GitHub · 2026-11-24 11:12 UTC · original

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

**@⁠d1ll3x3** on GitHub · 2026-11-24 11:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/584#issuecomment-7129791901) 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: ```rust 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).
Author
Owner

@⁠MartinIndra02 on GitHub · 2026-11-24 11:40 UTC · original

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.

**@⁠MartinIndra02** on GitHub · 2026-11-24 11:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/584#issuecomment-7130399288) 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`): - **Branch:** https://github.com/MartinIndra02/VIctory/tree/fix/shader-heap-bounds-clamp (commit `05c681c`) ### 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.*
Author
Owner

@⁠MartinIndra02 on GitHub · 2026-11-24 12:08 UTC · original

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.

**@⁠MartinIndra02** on GitHub · 2026-11-24 12:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/584#issuecomment-7131012633) 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.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:25 UTC · original

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** on GitHub · 2026-11-25 00:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/584#issuecomment-7148499195) 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.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!584
No description provided.