Skip to content

GPU+shader: clamp and place null buffer views within capped host descriptor heaps - #2177

Open
n-romanovich wants to merge 6 commits into
Marvisak:mainfrom
n-romanovich:pr/capped-descriptor-heaps
Open

n-romanovich wants to merge 6 commits into
Marvisak:mainfrom
n-romanovich:pr/capped-descriptor-heaps

Conversation

@n-romanovich

Copy link
Copy Markdown

Summary

When a host GPU caps shader-visible CBV_SRV_UAV descriptor heaps below HEAP_VIEWS (e.g. 1_000_000 on NVIDIA), two issues occurred:

  1. Translated shaders still clamped heap indices to HEAP_VIEWS - 1 (2_097_151) and routed invalid buffer views in buffer_guard to NULL_BUFFER_VIEW (2_097_150), reading out of bounds past the 1_000_000-slot heap.
  2. When a title heap exceeded PORTABLE_VIEWS (e.g. 1_245_184 slots), HostHeap::new allocated PORTABLE_VIEWS - MIN_DYNAMIC title slots + MIN_DYNAMIC dynamic slots, leaving 0 slots for crate::memory copy view pairs (pairs_end == count + extra).

This change:

  • Probes the device's actual shader-visible view heap limit (host_heap_views(), up to HEAP_VIEWS) and propagates it to victory_shader::emit::set_host_heap_views and shader_cache::key.
  • Uses host_heap_views() and null_buffer_view() (host_heap_views() - 2) in heap_handle and buffer_guard so clamped indices and null SRV/UAV slots stay inside the allocated heap.
  • Reserves CAPPED_PAIRS (24_576) and CAPPED_DYNAMIC (1 << 17) plus the 3 top null/clamp slots when a title heap must be capped, and throttles past-heap diagnostics in HostHeap::mirror to populated VIEW_TAG entries.
  • Updates stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros and descriptor::host::tests::dynamic_slots_are_held_until_released to query max_views() / host_heap_views().

Verification

Tested on NVIDIA GeForce RTX 5060 Ti:

  • cargo test --release -p victory-shader --lib -- stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros: 1 passed; 0 failed
  • cargo test --release -p victory-gpu --lib -- descriptor::host::tests memory::tests shader_cache::tests: 11 passed; 0 failed

@DenRakEiw DenRakEiw 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 locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu — 143 passed, 0 failed (2 ignored) on NVIDIA RTX 5090 (resolves both 1M view-heap cap failures on main 940d32f)
  • cargo test -p victory-shader --lib emit::stride_tests:: — 5 passed, 1 failed on RTX 5090: directly fixes a_heap_buffer_read_where_no_buffer_view_is_reads_zeros! (The 1 remaining failure, a_heap_buffer_read_by_element_takes_the_views_stride, is the separate NVIDIA structured-view issue covered by #2178.)
  • cargo clippy -p victory-gpu -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean clamping and null-buffer view placement within capped host descriptor heaps. (Note for triage: overlaps #2185 and #2121.)

AI-assisted (Gemini 4 Neon).

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

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked (shader half; the GPU half is Windows-only).

144 passed / 0 failed / 2 ignored. Null buffer views clamped and placed within capped host heaps — heap-cap safety for null views; mixed gpu+shader.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 1a3261f onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@sinan-cmyk

Copy link
Copy Markdown

Shader tests on NVIDIA Ada

I ran cargo test --locked -p victory-shader --lib on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, VICTORY_WARP unset.

Build Passed Failed
main 940d32f 244 3: stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware
this PR (head 1a3261f9) merged onto 940d32f 245 2: stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, synthetic_tests::gathers_at_a_level_match_the_hardware

So on Ada this PR fixes stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, one of the three failures #1835 tracks. #2178 fixes stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride. The two PRs conflict with each other in victory-gpu/src/shader_cache.rs (each one adds its own parameter to key) and in victory-shader/src/emit/mod.rs, so I could not test them together. synthetic_tests::gathers_at_a_level_match_the_hardware still fails with both (lane 0 gives [0, 1, 0, 0], expected [0, 0, 0, 0]).

AI-assisted (Claude Code).

@Venom2054

Copy link
Copy Markdown

I found a remaining allocation-bound issue in this PR on an RTX 5070 Ti (16 GB, NVIDIA 621.01, Windows 11 Pro 26200).

Tested base: main 940d32f69b22265690a0b5a15d6ded572ba33baf + this PR at 1a3261fe2e2c054c7f93060958f7888316370703.

For HostHeap::new(false, 800_000) on a device capped at 1,000,000 views, the first allocation of 1,062,144 is rejected. The smaller fallback succeeds with 865,536 descriptors, but the shader clamp still reaches 999,999 and the null SRV index is 999,998. Both can lie beyond the actual allocation.

The fallback needs to allocate at least views:

-            match make(count + MIN_DYNAMIC) {
+            match make((count + MIN_DYNAMIC).max(views)) {

I also tested this regression in descriptor/host.rs:

    #[test]
    fn a_capped_heap_keeps_the_shader_clamp_and_null_views_in_bounds() {
        let Some(heap) = HostHeap::new(false, 800_000) else {
            return; // no host GPU
        };
        // SAFETY: a plain query of a live descriptor heap.
        let total = unsafe { heap.heap.GetDesc() }.NumDescriptors;
        assert!(
            total >= victory_shader::emit::host_heap_views(),
            "the fallback heap has {total} slots, fewer than the shader clamp",
        );
        assert!(victory_shader::emit::null_buffer_view() < total);
    }

Before the fix, the new test failed with the fallback heap has 865536 slots, fewer than the shader clamp. After it, the GPU suite passed: 147 passed, 0 failed, 2 ignored on this isolated integration.

Command:

cargo test -p victory-gpu --lib --profile diag --all-features --locked -- --test-threads=1

Build, Clippy and asset policy also passed. The full workspace at this stage had 704 passed, 2 failed, 10 ignored; both failures also reproduced on original main (a_heap_buffer_read_by_element_takes_the_views_stride and gathers_at_a_level_match_the_hardware). These numbers describe this PR plus the small fix, not my later integration with additional PRs.

The test uses synthetic allocations and no game data. This fixes the allocation bound; it does not establish a fix for a separate title GPU crash.

Prepared and tested with assistance from OpenAI Codex. Feel free to incorporate the correction and regression test into this PR.

Venom

@steven-seagull27

Copy link
Copy Markdown

I reproduced Venom2054’s allocation finding on a second NVIDIA model, an RTX 5080 with driver 621.40. With 800,000 title entries, the real HostHeap fallback allocated 865,536 descriptors while the shader expected 1,000,000 and selected null SRV slot 999,998. A host-side assertion caught the discrepancy before dispatch.

With Venom2054’s one-line correction, the same allocation returned 1,000,000 descriptors and a missing buffer slot read back 32/32 zeros. Built-in WARP allocated 2,097,152 descriptors in both builds, with null SRV slot 2,097,150. The A/B held #2178 and the rest of the combined tree fixed. Original finding · One-line commit directly on this PR head

To pick it onto 1a3261f:

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/structured-srv-readbacks
git cherry-pick 11504f7edafbc1382d7e9153c79c7fb9c7560a96

The corrected allocation also let me check 12-byte structured subviews through the production heap. Their offset-dependent read failures and the separate SRV fix are documented on #2178. Full paired results and runnable tests

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (worth a look). On a host that caps shader-visible view heaps (NVIDIA, 1,000,000), translated shaders clamped indices and placed null buffer views at HEAP_VIEWS, past the heap. The head 0497f58 passes the probed limit (host::host_heap_views) through a new emit::Options::host_heap_views field (0 = HEAP_VIEWS) into Shader::heap_handle / buffer_guard, and puts it in shader_cache::key only when it differs, so uncapped hosts keep their cache. In HostHeap::new (descriptor/host.rs) the capped branch now reserves CAPPED_DYNAMIC dynamic slots plus CAPPED_PAIRS copy pairs below the null slots. CI check passes and it merges cleanly.

On your reply: I checked the three points. The process-wide atomic is gone. Title slots kept are now monotonic in count: they grow with count up to cap - CAPPED_DYNAMIC - CAPPED_PAIRS - 3 (844,349 on a 1M heap) and stay there. That is below main's 934,464 for counts in between, a trade the RTX 5070 Ti prologue measurement in the thread supports (peak 101,530 dynamic slots held; 3,763 failed allocations and a hang with 1 << 16). Remaining notes for the maintainer, not blockers: victory translate still passes the default (uncapped) options, so offline output on a capped host differs from what the runtime emits; CAPPED_PAIRS has not been exercised by a measured workload yet (per the tester). This overlaps #2176 in Options and shader_cache::key; whichever lands second needs a rebase.


Generated by Claude Code

@n-romanovich
n-romanovich force-pushed the pr/capped-descriptor-heaps branch from 1a3261f to d15c68b Compare November 25, 2026 14:03
@n-romanovich

Copy link
Copy Markdown
Author

@Marvisak, updated pr/capped-descriptor-heaps (d15c68b) to address the triage points:

  1. .max(views) in the MIN_DYNAMIC fallback: HostHeap::new in crates/victory-gpu/src/descriptor/host.rs now calls make((count + MIN_DYNAMIC).max(views)) instead of make(count + MIN_DYNAMIC), so heaps like 800,000 (865,536 with MIN_DYNAMIC) are still sized to at least host_heap_views() and keep the clamp / null-view slots in bounds.
  2. Regression test: added descriptor::host::tests::min_dynamic_fallback_keeps_at_least_host_heap_views (HostHeap::new(false, 800_000)).
  3. Coordination with GPU+shader: back structured buffer views with raw descriptors for NVIDIA rawBufferLoad #2178: both PRs touch shader_cache::key and emit/mod.rs; whichever lands second will rebase cleanly onto the first.

@Venom2054

Copy link
Copy Markdown

I retested the updated PR at d15c68bc on an RTX 5070 Ti, NVIDIA 621.01, Windows 11 build 26200. For 800,000 requested entries, the live D3D12 heap reported 1,000,000 descriptors. The shader limit was 1,000,000 and the null SRV index was 999,998.

As a control, I restored the previous allocation expression in a scratch copy of the same commit. The heap then reported 865,536 descriptors and the new min_dynamic_fallback_keeps_at_least_host_heap_views test failed its size assertion. Both measurements used the same test-only print of the heap descriptor count, shader limit, and null SRV index.

On the unmodified PR, the full victory-gpu library suite passed (147 passed, 0 failed, 2 ignored benchmarks). The separate missing-buffer shader test also passed on hardware and WARP, reading back 32 zeros on each. This verifies the allocation fix I reported earlier on this card.

@Venom2054

Copy link
Copy Markdown

I measured the dynamic reserve on an RTX 5070 Ti (621.01, Windows 11 build 26200) during the prologue. The title requested 1,245,184 descriptors from a host heap capped at 1,000,000. The first request that failed with MIN_DYNAMIC needed just 9 consecutive slots, 58.9 seconds after launch.

Capped view heap PR reserve MIN_DYNAMIC
Dynamic slots reserved 131,072 65,536
Title slots mirrored 844,349 909,885
Peak dynamic slots held simultaneously 101,530 65,500
Calls that found no contiguous range 0 of 960,545 3,763 of 124,885
Peak dedicated SRV/UAV pairs used 0 0

With the PR reserve the game reached the yard and kept presenting until the 258 s cutoff. With MIN_DYNAMIC, D3D12 reported DXGI_ERROR_DEVICE_HUNG at 107.0 s and the process exited at 111.7 s. The refusals came first. One run per layout leaves the cause of the hang open.

This supports keeping more than 65,536 dynamic slots for this workload. The largest single request was 256 slots in both runs. Neither run exercised the separate 24,576-slot reserve (12,288 pairs) or spilled a pair into the dynamic pool on this heap, so sizing CAPPED_PAIRS still needs a different workload.

I used a local integration that reaches the yard and checked the allocator and capped layout against d15c68bc. Both runs used the same executable, initial save, pipeline cache and scripted inputs. Only the reserve setting changed. Counters tracked individual allocations and releases and were logged every eight presents. Final samples were at 253.3 s and 99.6 s respectively. Failed calls can include retries.

…heaps

# Conflicts:
#	crates/victory-gpu/src/descriptor/host.rs
@n-romanovich

Copy link
Copy Markdown
Author

@Marvisak Updated pr/capped-descriptor-heaps (0497f58):

  • Moved host_heap_views into emit::Options (and lower::Parts / Shader), removing the process-wide HOST_HEAP_VIEWS atomic so offline victory translate and runtime translation are pure functions of Options.
  • Fixed the discontinuity in HostHeap::new so title_slots is monotonic in count on capped heaps (CAPPED_DYNAMIC is used consistently in the capped branch).
  • Added doc comments for CAPPED_DYNAMIC and noted the RTX 4060 / 1M-slot prologue measurement for CAPPED_PAIRS.
  • Merged main; victory-gpu / victory-shader tests and CI check pass.

Note: I am also the author of #2178 and will reconcile whichever lands second immediately after the first merges.

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.

7 participants