NVIDIA: reserve the null clamp slot in every capped view heap and key shader cache by cap #20

Open
rivaldodev wants to merge 1 commit from upstream-pr/20 into main
rivaldodev commented 2026-11-25 04:11:27 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by rivaldodev on 2026-11-25T03:24:24Z.


Reserves the shader's null clamp slot in every view heap on capped hosts (e.g. NVIDIA's 1,000,000-slot view heap limit) and includes the host view heap cap in the shader cache fingerprint.

What changed

  • crates/victory-gpu/src/descriptor/host.rs:
    • Extracted pure view_heap_layout(count, extra, null_slot) sizing helper so capped and uncapped heap arithmetic can be unit-tested without D3D12 initialization.
    • On capped hosts, every view heap (including small and exact-fit title heaps) allocates null + 1 slots and limits title slots to null - DYNAMIC, keeping the clamp slot strictly past both title and dynamic allocations.
    • Immutable heap policy (review item #1): removed the late fallback in HostHeap::new() that previously mutated view_heap_limit after a failed large allocation. probe_view_limit() establishes the cap once before shader translation or heap creation; subsequent allocation failures now fail explicitly rather than invalidating previously translated shaders or cache keys.
    • Fail on null-init error (review item #2): HostHeap::new() now returns None if fill_null() fails instead of returning an uninitialized heap, and only records the range in GPU_RANGES after fill_null() succeeds.
    • Added GPU-free unit test heap_sizing_keeps_the_clamp_slot_past_title_and_dynamic_slots alongside the hardware test view_heaps_keep_the_clamp_slot_null.
  • crates/victory-gpu/src/shader_cache.rs:
    • Calls probe_view_limit() and hashes view_heap_limit() into the build fingerprint so cached translations match the host's heap cap (and documents the host-probe side effect).
  • crates/victory-shader/src/emit/{shader.rs,mod.rs}:
    • Exposed view_heap_limit().

Tests

  • cargo clippy -p victory-gpu -p victory-shader --no-deps --all-targets --locked — clean on changed files.
  • cargo test -p victory-gpu --lib --locked — 112 passed, 0 failed, 1 ignored (including both the pure sizing test and the hardware clamp-slot test).

Split per review feedback: the automatic divergent barrier policy has been moved to a separate draft PR.

<!-- greyfinch-sync:Zettrox/ztrx:issue:6596471558 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/20). Originally posted by [rivaldodev](https://github.com/rivaldodev) on 2026-11-25T03:24:24Z. --- Reserves the shader's null clamp slot in every view heap on capped hosts (e.g. NVIDIA's 1,000,000-slot view heap limit) and includes the host view heap cap in the shader cache fingerprint. ### What changed - **`crates/victory-gpu/src/descriptor/host.rs`**: - Extracted pure `view_heap_layout(count, extra, null_slot)` sizing helper so capped and uncapped heap arithmetic can be unit-tested without D3D12 initialization. - On capped hosts, every view heap (including small and exact-fit title heaps) allocates `null + 1` slots and limits title slots to `null - DYNAMIC`, keeping the clamp slot strictly past both title and dynamic allocations. - **Immutable heap policy (review item #1):** removed the late fallback in `HostHeap::new()` that previously mutated `view_heap_limit` after a failed large allocation. `probe_view_limit()` establishes the cap once before shader translation or heap creation; subsequent allocation failures now fail explicitly rather than invalidating previously translated shaders or cache keys. - **Fail on null-init error (review item #2):** `HostHeap::new()` now returns `None` if `fill_null()` fails instead of returning an uninitialized heap, and only records the range in `GPU_RANGES` after `fill_null()` succeeds. - Added GPU-free unit test `heap_sizing_keeps_the_clamp_slot_past_title_and_dynamic_slots` alongside the hardware test `view_heaps_keep_the_clamp_slot_null`. - **`crates/victory-gpu/src/shader_cache.rs`**: - Calls `probe_view_limit()` and hashes `view_heap_limit()` into the build fingerprint so cached translations match the host's heap cap (and documents the host-probe side effect). - **`crates/victory-shader/src/emit/{shader.rs,mod.rs}`**: - Exposed `view_heap_limit()`. ### Tests - `cargo clippy -p victory-gpu -p victory-shader --no-deps --all-targets --locked` — clean on changed files. - `cargo test -p victory-gpu --lib --locked` — **112 passed, 0 failed, 1 ignored** (including both the pure sizing test and the hardware clamp-slot test). --- *Split per review feedback: the automatic divergent barrier policy has been moved to a separate draft PR.*
copilot-pull-request-reviewer[bot commented 2026-11-25 04:11:27 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T03:24:34Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:Zettrox/ztrx:review:6051398708 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/20#pullrequestreview-6051398708). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/copilot-pull-request-reviewer%5Bbot%5D) on 2026-11-25T03:24:34Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
kaayzouee commented 2026-11-25 04:11:31 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by kaayzouee on 2026-11-25T03:27:41Z.


Code review — Request changes

Reviewed commit 23e2eab. No equivalent fix was found in the target repository’s available pull request history.

The capped-heap sizing arithmetic is sound. Allocating null + 1 slots and limiting the title count to null - DYNAMIC correctly keeps dynamic allocations below the clamp slot. Using the actual allocated size for GPU-range diagnostics is also correct.

However, there are several correctness concerns.

  1. High — Shader cache fingerprint can become inconsistent with the heap cap

In shader_cache.rs, the build fingerprint is computed once through OnceLock, incorporating the cap determined by the initial probe.

However, HostHeap::new() still changes the global cap when a subsequent larger allocation fails.

A host can successfully allocate the 1,000,001-slot probe heap but fail a larger allocation. If the fallback succeeds, the global cap changes from zero to 999,999.

Consequently:

  • Previously translated shaders retain their unclamped indices.
  • Subsequent shaders may be translated with a clamp under the same cached fingerprint as unclamped shaders.
  • Previously allocated small heaps may not contain the new clamp slot.

The late fallback predates this PR, but directly conflicts with the newly introduced cache and heap consistency requirements.

Please establish one immutable heap policy before shader translation and heap allocation. If a subsequent allocation cannot satisfy that policy, fail explicitly rather than silently changing the translation assumptions.

  1. High — Null descriptor initialization failure is ignored

HostHeap::new() logs errors returned by fill_null() but still returns Some(h).

If creation of the CPU staging descriptor heap fails, the reserved clamp slot and padded region may remain uninitialized.

Microsoft’s documentation explicitly warns that accessing uninitialized descriptor memory can cause undefined behavior, including device resets:

https://learn.microsoft.com/en-us/windows/win32/direct3d12/setting-descriptor-heaps

Heap creation should fail if null initialization cannot be completed, or use a verified fallback initialization method.

Additionally, null_view() creates a null Texture2D SRV. This is not a universally compatible null descriptor for all CBV/SRV/UAV accesses.

Both limitations predate this PR, but they undermine its stated clamp-slot safety guarantee. Descriptor type compatibility should be documented or addressed separately.

  1. Automatic barrier synchronization has a narrower safety scope than implied

The extracted barrier_mode() is readable, preserves explicit overrides, and does not alter translation-added meeting barriers.

However, D3D12 group synchronization is a thread-group requirement, not a wave-width requirement.

The new predicate still permits potentially divergent group barriers in a wave32 shader with a 64-thread group running on wave32 hardware.

Microsoft documents divergent group synchronization as undefined behavior:

https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/groupmemorybarrierwithgroupsync

This is an existing limitation retained by the new automatic mode, rather than a newly introduced regression.

Conversely, removing group synchronization preserves memory fences but removes the inter-thread rendezvous. These are not semantically equivalent.

The GPU regression test demonstrates successful dispatch completion but does not exercise LDS producer-consumer communication across the barrier. It therefore does not establish memory synchronization correctness.

Please document automatic mode as a targeted compatibility workaround and consider adding producer-consumer regression tests.

  1. Simplification and maintainability
  • Separate descriptor heap/cache changes from the automatic barrier policy. These are independent fixes with different regression risks.
  • Extract heap-sizing arithmetic into a pure helper that can be tested without D3D12 initialization.
  • Retain barrier_mode() as a standalone function. Its current implementation is sufficiently simple.
  • Measure the overhead of allocating and initializing one million descriptors for every small view heap.
  1. Recommended additional tests
  • Successful initial cap probe followed by a failed larger allocation.
  • Shader cache consistency across heap allocation failures.
  • Null descriptor initialization failure.
  • Actual shader readback through a compatible clamp descriptor.
  • Capped and uncapped heap-sizing arithmetic without GPU dependencies.
  • Multi-wave producer-consumer synchronization around title barriers.

Verdict

Request changes, primarily because the late heap-cap fallback can invalidate shader cache assumptions and because failed null initialization does not prevent heap creation.

The underlying arithmetic fix is good. The automatic barrier workaround is promising, but its semantic limitations should be explicit.

Static review only. I inspected the changed code, surrounding implementations, repository-wide PR search results, and Microsoft API documentation. The reported Windows/GPU tests and allocation failure scenarios were not independently reproduced.

⸻

This is an automated message, if you believe this is incorrect, please reply with the relevant technical context or a reproducible counterexample so the finding can be reassessed.

<!-- greyfinch-sync:Zettrox/ztrx:comment:7151934946 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/20#issuecomment-7151934946). Originally posted by [kaayzouee](https://github.com/kaayzouee) on 2026-11-25T03:27:41Z. --- Code review — Request changes Reviewed commit 23e2eab. No equivalent fix was found in the target repository’s available pull request history. The capped-heap sizing arithmetic is sound. Allocating null + 1 slots and limiting the title count to null - DYNAMIC correctly keeps dynamic allocations below the clamp slot. Using the actual allocated size for GPU-range diagnostics is also correct. However, there are several correctness concerns. 1. High — Shader cache fingerprint can become inconsistent with the heap cap In shader_cache.rs, the build fingerprint is computed once through OnceLock, incorporating the cap determined by the initial probe. However, HostHeap::new() still changes the global cap when a subsequent larger allocation fails. A host can successfully allocate the 1,000,001-slot probe heap but fail a larger allocation. If the fallback succeeds, the global cap changes from zero to 999,999. Consequently: * Previously translated shaders retain their unclamped indices. * Subsequent shaders may be translated with a clamp under the same cached fingerprint as unclamped shaders. * Previously allocated small heaps may not contain the new clamp slot. The late fallback predates this PR, but directly conflicts with the newly introduced cache and heap consistency requirements. Please establish one immutable heap policy before shader translation and heap allocation. If a subsequent allocation cannot satisfy that policy, fail explicitly rather than silently changing the translation assumptions. 2. High — Null descriptor initialization failure is ignored HostHeap::new() logs errors returned by fill_null() but still returns Some(h). If creation of the CPU staging descriptor heap fails, the reserved clamp slot and padded region may remain uninitialized. Microsoft’s documentation explicitly warns that accessing uninitialized descriptor memory can cause undefined behavior, including device resets: https://learn.microsoft.com/en-us/windows/win32/direct3d12/setting-descriptor-heaps Heap creation should fail if null initialization cannot be completed, or use a verified fallback initialization method. Additionally, null_view() creates a null Texture2D SRV. This is not a universally compatible null descriptor for all CBV/SRV/UAV accesses. Both limitations predate this PR, but they undermine its stated clamp-slot safety guarantee. Descriptor type compatibility should be documented or addressed separately. 3. Automatic barrier synchronization has a narrower safety scope than implied The extracted barrier_mode() is readable, preserves explicit overrides, and does not alter translation-added meeting barriers. However, D3D12 group synchronization is a thread-group requirement, not a wave-width requirement. The new predicate still permits potentially divergent group barriers in a wave32 shader with a 64-thread group running on wave32 hardware. Microsoft documents divergent group synchronization as undefined behavior: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/groupmemorybarrierwithgroupsync This is an existing limitation retained by the new automatic mode, rather than a newly introduced regression. Conversely, removing group synchronization preserves memory fences but removes the inter-thread rendezvous. These are not semantically equivalent. The GPU regression test demonstrates successful dispatch completion but does not exercise LDS producer-consumer communication across the barrier. It therefore does not establish memory synchronization correctness. Please document automatic mode as a targeted compatibility workaround and consider adding producer-consumer regression tests. 4. Simplification and maintainability * Separate descriptor heap/cache changes from the automatic barrier policy. These are independent fixes with different regression risks. * Extract heap-sizing arithmetic into a pure helper that can be tested without D3D12 initialization. * Retain barrier_mode() as a standalone function. Its current implementation is sufficiently simple. * Measure the overhead of allocating and initializing one million descriptors for every small view heap. 5. Recommended additional tests * Successful initial cap probe followed by a failed larger allocation. * Shader cache consistency across heap allocation failures. * Null descriptor initialization failure. * Actual shader readback through a compatible clamp descriptor. * Capped and uncapped heap-sizing arithmetic without GPU dependencies. * Multi-wave producer-consumer synchronization around title barriers. Verdict Request changes, primarily because the late heap-cap fallback can invalidate shader cache assumptions and because failed null initialization does not prevent heap creation. The underlying arithmetic fix is good. The automatic barrier workaround is promising, but its semantic limitations should be explicit. Static review only. I inspected the changed code, surrounding implementations, repository-wide PR search results, and Microsoft API documentation. The reported Windows/GPU tests and allocation failure scenarios were not independently reproduced. ⸻ This is an automated message, if you believe this is incorrect, please reply with the relevant technical context or a reproducible counterexample so the finding can be reassessed.
rivaldodev commented 2026-11-25 04:11:31 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by rivaldodev on 2026-11-25T03:59:39Z.


Addressed the review feedback in a850e49:

  1. Split PRs: This PR now covers only the view heap clamp slot and shader cache key; the automatic narrow-wave barrier policy has been moved to a separate PR (#30).
  2. Immutable heap cap policy: Removed the late fallback in HostHeap::new() that previously mutated view_heap_limit mid-run on a failed large allocation. probe_view_limit() now establishes the cap once; subsequent allocation failures fail explicitly.
  3. Fail on fill_null() error: HostHeap::new() now returns None if fill_null() fails and only records the range in GPU_RANGES after null initialization succeeds.
  4. Pure sizing helper + GPU-free test: Extracted view_heap_layout(count, extra, null_slot) and added heap_sizing_keeps_the_clamp_slot_past_title_and_dynamic_slots.

cargo test -p victory-gpu --lib --locked passes (112 passed, 0 failed, 1 ignored). Ready for re-review.

<!-- greyfinch-sync:Zettrox/ztrx:comment:7152429203 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/20#issuecomment-7152429203). Originally posted by [rivaldodev](https://github.com/rivaldodev) on 2026-11-25T03:59:39Z. --- Addressed the review feedback in `a850e49`: 1. **Split PRs:** This PR now covers only the view heap clamp slot and shader cache key; the automatic narrow-wave barrier policy has been moved to a separate PR (#30). 2. **Immutable heap cap policy:** Removed the late fallback in `HostHeap::new()` that previously mutated `view_heap_limit` mid-run on a failed large allocation. `probe_view_limit()` now establishes the cap once; subsequent allocation failures fail explicitly. 3. **Fail on `fill_null()` error:** `HostHeap::new()` now returns `None` if `fill_null()` fails and only records the range in `GPU_RANGES` after null initialization succeeds. 4. **Pure sizing helper + GPU-free test:** Extracted `view_heap_layout(count, extra, null_slot)` and added `heap_sizing_keeps_the_clamp_slot_past_title_and_dynamic_slots`. `cargo test -p victory-gpu --lib --locked` passes (112 passed, 0 failed, 1 ignored). Ready for re-review.
futurepro62 commented 2026-11-25 07:06:30 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by futurepro62 on 2026-11-25T06:38:39Z.


Full-suite run on Windows, since this PR has no test report yet. Tested exact head a850e49 (on its base 90b529f).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (rust-toolchain.toml), DXC + dxil.dll 1.9.2602.17, debug.
  • cargo test --workspace --locked: 498 passed, 0 failed, 7 ignored (main 90b529f on this machine: 496 passed, 0 failed, 7 ignored).
  • cargo clippy --workspace --all-targets --locked -- -D warnings (the CI lint step): passed.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (the CI lint step): passed.
  • The new tests ran and passed: heap_sizing_keeps_the_clamp_slot_past_title_and_dynamic_slots, view_heaps_keep_the_clamp_slot_null.

Not tested

  • AMD (ztrx targets NVIDIA), a release build, the game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Zettrox/ztrx:comment:7155006958 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/20#issuecomment-7155006958). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-25T06:38:39Z. --- Full-suite run on Windows, since this PR has no test report yet. Tested exact head `a850e49` (on its base `90b529f`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (`rust-toolchain.toml`), DXC + dxil.dll 1.9.2602.17, debug. - `cargo test --workspace --locked`: **498 passed, 0 failed, 7 ignored** (main `90b529f` on this machine: 496 passed, 0 failed, 7 ignored). - `cargo clippy --workspace --all-targets --locked -- -D warnings` (the CI lint step): passed. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (the CI lint step): passed. - The new tests ran and passed: `heap_sizing_keeps_the_clamp_slot_past_title_and_dynamic_slots`, `view_heaps_keep_the_clamp_slot_null`. **Not tested** - AMD (ztrx targets NVIDIA), a release build, the game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/20:upstream-pr/20'
git switch 'upstream-pr/20'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/20'
git switch 'upstream-pr/20'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/20'
git switch 'upstream-pr/20'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/20'
git switch 'main'
git merge --squash 'upstream-pr/20'
git switch 'main'
git merge --ff-only 'upstream-pr/20'
git switch 'main'
git merge 'upstream-pr/20'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Zettrox/ztrx!20
No description provided.