Skip to content

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

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

n-romanovich wants to merge 1 commit 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

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.

6 participants