Shaders, GPU: clamp descriptor heap indices to bound host heap guard slot #955

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

@⁠MartinIndra02 on GitHub · 2026-11-24 14:27 UTC · original
MartinIndra02:fix/shader-heap-bounds-clamp → main · +371 −50 · 11 files · 1 commits · closed without merging

Follow-up / replacement for #584 (fixing the AMD >1M heap black screen diagnosed in #584 and confirmed on RDNA3 by @d1ll3x3).

Summary

Clamps ResourceDescriptorHeap and SamplerDescriptorHeap indices at runtime to the bound host heap's dedicated permanently-null guard slot instead of hardcoding 1_000_000 -> 0:

  1. Runtime bound per bound heap (b0 in HEAP_LIMITS_SPACE = 4):
    • Shaders that index ResourceDescriptorHeap or SamplerDescriptorHeap load [view_guard, sampler_guard] once in the entry block from a 2-dword root constant (HEAP_LIMITS_SPACE = 4, leaving spaces 2/3 free for #710/#704).
    • Shaders that do not touch 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, State::bind supplies [view_heap.guard(), sampler_heap.guard()], and shader_cache.rs encodes/decodes heap_limits.
  2. Dedicated permanently-null guard slot covering both title and dynamic slots:
    • HostHeap reserves the final slot total - 1 on view heaps (dynamic = DYNAMIC - 1 or MIN_DYNAMIC - 1, staying within PORTABLE_VIEWS when capped and covering count + dynamic on >1M Tier 3 AMD heaps) and appends 1 guard slot on sampler heaps below D3D12's 2048-slot cap (make(count + 1) with count.saturating_sub(1) fallback at 2048).
    • fill_null initializes all total slots (including the guard slot), and neither mirror nor dynamic can ever overwrite it.
    • select(index <u guard, index, guard) leaves every valid title & dynamic slot (0..guard) untouched and maps every OOB/wrapped index to the dedicated null guard slot (never aliasing live slot 0).

Verification (Windows 11, RTX 3080 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): verifies valid slot 0 (1.0) and OOB slot 16384 (0.0 from 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).
  • 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
  • In-title run on RTX 3080 (diag + DRED): menus, illustrations, and world entry ran with 0 DRED page faults.

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

**@⁠MartinIndra02** on GitHub · 2026-11-24 14:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/955) `MartinIndra02:fix/shader-heap-bounds-clamp` → `main` · +371 −50 · 11 files · 1 commits · closed without merging Follow-up / replacement for #584 (fixing the AMD >1M heap black screen diagnosed in #584 and confirmed on RDNA3 by `@d1ll3x3`). ### Summary Clamps `ResourceDescriptorHeap` and `SamplerDescriptorHeap` indices at runtime to the bound host heap's dedicated permanently-null guard slot instead of hardcoding `1_000_000 -> 0`: 1. **Runtime bound per bound heap (`b0` in `HEAP_LIMITS_SPACE = 4`):** - Shaders that index `ResourceDescriptorHeap` or `SamplerDescriptorHeap` load `[view_guard, sampler_guard]` once in the entry block from a 2-dword root constant (`HEAP_LIMITS_SPACE = 4`, leaving spaces `2`/`3` free for `#710`/`#704`). - Shaders that do not touch 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`, `State::bind` supplies `[view_heap.guard(), sampler_heap.guard()]`, and `shader_cache.rs` encodes/decodes `heap_limits`. 2. **Dedicated permanently-null guard slot covering both title and dynamic slots:** - `HostHeap` reserves the final slot `total - 1` on view heaps (`dynamic = DYNAMIC - 1` or `MIN_DYNAMIC - 1`, staying within `PORTABLE_VIEWS` when capped and covering `count + dynamic` on >1M Tier 3 AMD heaps) and appends 1 guard slot on sampler heaps below D3D12's 2048-slot cap (`make(count + 1)` with `count.saturating_sub(1)` fallback at 2048). - `fill_null` initializes all `total` slots (including the guard slot), and neither `mirror` nor `dynamic` can ever overwrite it. - `select(index <u guard, index, guard)` leaves every valid title & dynamic slot (`0..guard`) untouched and maps every OOB/wrapped index to the dedicated null guard slot (never aliasing live slot `0`). ### Verification (Windows 11, RTX 3080 `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): verifies valid slot `0` (`1.0`) and OOB slot `16384` (`0.0` from 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`). - `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** - In-title run on RTX 3080 (diag + DRED): menus, illustrations, and world entry ran with **0 DRED page faults**. *No game data used. AI-assisted; all builds and GPU tests executed on my machine.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:52 +00:00
github-bot closed this pull request 2026-11-26 04:14:52 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 14:45 UTC · original

Five existing waterfall readbacks with valid bindings passed on both 05c681c and our #483-based comparison build on RTX 4090 under Wine/vkd3d-proton 3.2. The #955 heap-layout test also passed: the dynamic allocation ended before the guard, and a 16-slot sampler heap had a separate guard at slot 16.

One integration detail from source review: on a capped view heap, the mirrored title descriptors and the host's fresh table allocations have different extents. #483 checks direct title indices against the mirrored descriptor count, while allowing translated table indices to use the dynamic region up to the physical guard. Keeping those bounds distinct preserves both kinds of binding. The readbacks above exercise valid views. The effect on title rendering was not measured in this comparison.

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 14:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/955#issuecomment-7134894843) Five existing waterfall readbacks with valid bindings passed on both `05c681c` and our #483-based comparison build on RTX 4090 under Wine/vkd3d-proton 3.2. The #955 heap-layout test also passed: the dynamic allocation ended before the guard, and a 16-slot sampler heap had a separate guard at slot 16. One integration detail from source review: on a capped view heap, the mirrored title descriptors and the host's fresh table allocations have different extents. #483 checks direct title indices against the mirrored descriptor count, while allowing translated table indices to use the dynamic region up to the physical guard. Keeping those bounds distinct preserves both kinds of binding. The readbacks above exercise valid views. The effect on title rendering was not measured in this comparison.
Author
Owner

@⁠shortselow-byte on GitHub · 2026-11-24 15:01 UTC · original

Tested at head 05c681c (on main ffe310e) on AMD, since the run above is NVIDIA only: Windows 11, Rust 1.98.1 MSVC, AMD Radeon RX 6900 XT (RDNA2, driver 32.0.21045.16010).

  • out_of_bounds_heap_index_clamps_to_null_guard_not_slot_zero: passed (AMD device and WARP, --test-threads=1).
  • host_heap_guard_covers_title_and_dynamic_slots: passed, also with VICTORY_D3D12_DEBUG=1.
  • cargo test --locked -p victory-shader --lib -- --test-threads=1: 121 passed, 1 ignored. -p victory-gpu --lib: 51 passed, 1 ignored. Whole workspace: 344 passed, 0 failed.
  • cargo clippy --workspace --all-targets --locked -- -D warnings, and with --all-features: clean.

The >1M case on AMD. The new heap test uses 64 title slots, so it never reaches the size this PR is about. With a local, unpublished test making a view heap of 1,100,000 title slots, the AMD driver accepted the full count + DYNAMIC heap (1,362,144 slots, not the MIN_DYNAMIC or PORTABLE_VIEWS fallbacks): dynamic = 262143, guard = 1362143 = count + dynamic, and taking every dynamic slot ends right at the guard. So the layout holds where AMD actually goes past 1M.

Not done: an unclamped control on the AMD device (an out-of-bounds index past a 3-slot heap could hang the driver), and no in-title run.

A question on samplers: when make(count + 1) fails at D3D12's 2048 cap, guard = count - 1 is a live title sampler, so out-of-bounds sampler indices would read that sampler rather than a null one. Is that intended as the least bad option there?

**@⁠shortselow-byte** on GitHub · 2026-11-24 15:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/955#issuecomment-7135331056) Tested at head `05c681c` (on `main` `ffe310e`) on AMD, since the run above is NVIDIA only: Windows 11, Rust 1.98.1 MSVC, **AMD Radeon RX 6900 XT** (RDNA2, driver 32.0.21045.16010). - `out_of_bounds_heap_index_clamps_to_null_guard_not_slot_zero`: passed (AMD device and WARP, `--test-threads=1`). - `host_heap_guard_covers_title_and_dynamic_slots`: passed, also with `VICTORY_D3D12_DEBUG=1`. - `cargo test --locked -p victory-shader --lib -- --test-threads=1`: 121 passed, 1 ignored. `-p victory-gpu --lib`: 51 passed, 1 ignored. Whole workspace: 344 passed, 0 failed. - `cargo clippy --workspace --all-targets --locked -- -D warnings`, and with `--all-features`: clean. **The >1M case on AMD.** The new heap test uses 64 title slots, so it never reaches the size this PR is about. With a local, unpublished test making a view heap of 1,100,000 title slots, the AMD driver accepted the full `count + DYNAMIC` heap (1,362,144 slots, not the `MIN_DYNAMIC` or `PORTABLE_VIEWS` fallbacks): `dynamic = 262143`, `guard = 1362143 = count + dynamic`, and taking every dynamic slot ends right at the guard. So the layout holds where AMD actually goes past 1M. Not done: an unclamped control on the AMD device (an out-of-bounds index past a 3-slot heap could hang the driver), and no in-title run. A question on samplers: when `make(count + 1)` fails at D3D12's 2048 cap, `guard = count - 1` is a live title sampler, so out-of-bounds sampler indices would read that sampler rather than a null one. Is that intended as the least bad option there?
Author
Owner

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

@⁠shortselow-byte Thanks a lot for testing on AMD RDNA2 (RX 6900 XT) and especially for verifying the >1M title-slot allocation (1,100,000 -> 1,362,144, guard = 1,362,143)!

Yes, guard = count.saturating_sub(1) at D3D12's hard 2048 shader-visible sampler heap cap is intentional as the least bad option:

  • D3D12 forbids allocating a 2049-slot shader-visible SAMPLER heap, so we can't append a dedicated null slot without stealing slot 2047 from a title that actually uses all 2048 samplers.
  • Unlike view heaps (where aliasing a live slot can cause SRV/UAV/CBV type mismatches or stale resource reads), every slot in a sampler heap is a plain sampler state, so clamping OOB indices to 2047 preserves all 2048 title slots while still preventing any out-of-bounds heap read/fault.
**@⁠MartinIndra02** on GitHub · 2026-11-24 15:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/955#issuecomment-7135549280) @⁠shortselow-byte Thanks a lot for testing on AMD RDNA2 (RX 6900 XT) and especially for verifying the >1M title-slot allocation (`1,100,000` -> `1,362,144`, `guard = 1,362,143`)! Yes, `guard = count.saturating_sub(1)` at D3D12's hard 2048 shader-visible sampler heap cap is intentional as the least bad option: - D3D12 forbids allocating a 2049-slot shader-visible `SAMPLER` heap, so we can't append a dedicated null slot without stealing slot `2047` from a title that actually uses all 2048 samplers. - Unlike view heaps (where aliasing a live slot can cause SRV/UAV/CBV type mismatches or stale resource reads), every slot in a sampler heap is a plain sampler state, so clamping OOB indices to `2047` preserves all 2048 title slots while still preventing any out-of-bounds heap read/fault.
Author
Owner

@⁠shortselow-byte on GitHub · 2026-11-24 15:22 UTC · original

Correction to my comment above: "also with VICTORY_D3D12_DEBUG=1" means nothing there. That variable only takes effect with --features diagnostics, which I did not pass, and this machine does not have the D3D12 debug layer installed anyway. So the heap test ran on the AMD device without validation; the other results stand.

**@⁠shortselow-byte** on GitHub · 2026-11-24 15:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/955#issuecomment-7135915330) Correction to my comment above: "also with `VICTORY_D3D12_DEBUG=1`" means nothing there. That variable only takes effect with `--features diagnostics`, which I did not pass, and this machine does not have the D3D12 debug layer installed anyway. So the heap test ran on the AMD device without validation; the other results stand.
Author
Owner

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

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

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/955#issuecomment-7136424522) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 05c681c: `cargo test -p victory-gpu -p victory-shader` passes, 172 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠YaBulochka on GitHub · 2026-11-24 17:41 UTC · original

Numbers from a Linux/RADV run that fit this diagnosis (Mesa 26.3.1, RX 7600 XT, Wine 11.22 on a Proton prefix, vkd3d-proton from Proton):

  • the title's view heap asks for 1,245,184 descriptors;
  • CreateDescriptorHeap fails above 1,000,000 (0x80070057), so HostHeap::new mirrors 934,464 and leaves the rest out;
  • the title then writes the whole range anyway: 310,720 slots past the mirror, one warning each, index=934464…1245183.

With this PR on top of #994 the artifacts on the loading screen are unchanged: reads are clamped to the guard slot now, but the descriptors in those slots were never mirrored, so a shader that reaches them reads a null view instead of the title's. Happy to test a follow-up that pages the tail of the heap in.

**@⁠YaBulochka** on GitHub · 2026-11-24 17:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/955#issuecomment-7139568858) Numbers from a Linux/RADV run that fit this diagnosis (Mesa 26.3.1, RX 7600 XT, Wine 11.22 on a Proton prefix, vkd3d-proton from Proton): - the title's view heap asks for **1,245,184** descriptors; - `CreateDescriptorHeap` fails above 1,000,000 (`0x80070057`), so `HostHeap::new` mirrors **934,464** and leaves the rest out; - the title then writes the whole range anyway: **310,720** slots past the mirror, one warning each, `index=934464…1245183`. With this PR on top of #994 the artifacts on the loading screen are unchanged: reads are clamped to the guard slot now, but the descriptors in those slots were never mirrored, so a shader that reaches them reads a null view instead of the title's. Happy to test a follow-up that pages the tail of the heap in.
Author
Owner

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

Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1133. Replacement for #584. Main has the static clamp to HEAP_VIEWS-1; the remaining capped-host case is done more simply by #1133 (the cap as a translation constant, in the cache key) than by a new root-constant space.

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:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/955#issuecomment-7148550978) Automated triage for the maintainers. Closing because another open pull request covers the same change. See #1133. Replacement for #584. Main has the static clamp to HEAP_VIEWS-1; the remaining capped-host case is done more simply by #1133 (the cap as a translation constant, in the cache key) than by a new root-constant space. 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!955
No description provided.