Skip to content

GPU, Shaders: a capped view heap clamps shader heap indices from the first shader - #2628

Open
moe-miau wants to merge 3 commits into
Marvisak:mainfrom
moe-miau:port/heapclamp
Open

moe-miau wants to merge 3 commits into
Marvisak:mainfrom
moe-miau:port/heapclamp

Conversation

@moe-miau

@moe-miau moe-miau commented Nov 25, 2026 •

Copy link
Copy Markdown

Problem

On NVIDIA the shader-visible CBV/SRV/UAV heap holds at most 1,000,000 descriptors, while translated shaders clamp their heap indices below HEAP_VIEWS (2^21). main already rebuilds tables past the cap and falls back to fewer dynamic slots (3827767, 3c81420, e8d3b64). The translator still lets a shader index anywhere below HEAP_VIEWS, including programs translated before the title made its heap. On a capped heap those indices name descriptors that do not exist.

On an RTX 4070 SUPER this is visible in main's own tests. cargo test -p victory-gpu fails dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views (NumDescriptors >= HEAP_VIEWS).

Change

  • Before the first translation, the host probes whether it can make the full HEAP_VIEWS view heap (portable_view_heap). If it cannot, every heap uses the portable layout (PORTABLE_VIEWS, MIN_DYNAMIC dynamic slots).
  • The translator gets that limit (emit::Options::view_heap_limit). Indices at or past it go to null views.
  • The shader's last three slots stay reserved for the null UAV, SRV and general view on every heap, large or capped. Mirrored and dynamic slots end before them (view_layout), so a redirected index never reaches a live resource.
  • An index the title takes from the heap itself past its own slots (PORTABLE_TITLE_VIEWS) reads the null view, so it never reaches the dynamic slots that follow them. Table indices keep the heap-wide clamp.
  • The limit is part of the shader cache key.
  • Raw-memory view pairs keep their own range.
  • A capped heap keeps all 2^18 dynamic slots (main drops to 2^16 there, which ran out in game). The title keeps about 737,000 slots, above the highest it was seen to use (about 720,000).
  • On a host whose heap holds HEAP_VIEWS, the layout matches main.

Ported from Zettrox/ztrx (commit 578d459: probe_view_limit, set_view_heap_limit), which is based on fxceless/victory-fork. Both are GPL-3.0-or-later. Unlike the fork, the probe asks for the size actually used (HEAP_VIEWS, not 1,000,001), and the null slots are reserved on large heaps too.

Tested

RTX 4070 SUPER, driver 32.0.16.1742, Windows 11 26300.

AI-assisted (Claude Code, with a review pass by Codex). Runs done on my machine.

🤖 Generated with Claude Code

@moe-miau

Copy link
Copy Markdown
Author

Overlaps #2461 (lhnizdo), the same port from victory-fork/ztrx, opened earlier today. #2461 conflicts with main now; this one merges. I took #2461's point about the dynamic range: 36ccd6e keeps all 2^18 dynamic slots on a capped heap, after a game run here dropped draws with 2^16. If #2461 is rebased and preferred, close this one.

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

I ran a standalone D3D12 probe that explicitly selected the RTX 4070 Ti (driver 621.01, Windows 11 build 26300).

Shader-visible CBV/SRV/UAV slots requested CreateDescriptorHeap
2,097,152 E_INVALIDARG
1,000,001 E_INVALIDARG
1,000,000 S_OK

For a large title heap on this layout, the dynamic range starts at 737_853. The direct texture heap clamp checks the physical limit, so a title index like 800_000 passes the clamp and can select a live dynamic descriptor. That index was still in the mirrored title range on the base.

Could direct title-heap accesses check the mirrored range separately from host-generated indices? A focused test could populate a dynamic slot and verify that a title-derived index with the same number resolves to a null view.

The probe confirms the heap size. I traced the descriptor overlap in the code and didn’t run the PR or the title.

AI-assisted with Codex.

@moe-miau

Copy link
Copy Markdown
Author

@DrDarkDK Thanks, you're right. Moving the dynamic slots down to the title's end (36ccd6e) let a title index from about 737,853 to 999,996 reach a live dynamic descriptor. On main that index was still a title slot. Fixed in 46d530a:

  • emit::PORTABLE_TITLE_VIEWS (= 1,000,000 − 3 − 2^18 = 737,853) is the title's slot count on the portable heap. Shader::clamp_title_heap_index sends a title index past it to the general null view, view_limit - 1.
  • It is applied in buffer_guard, which every heap-handle path goes through, but only when every path of the descriptor comes from the resource heap itself (is_heap_only). Table indices may name dynamic slots (tables rebuilt per draw), so they keep the heap-wide clamp. The stride lookup for heap-direct buffer reads (view_stride) uses the same clamp.
  • The GPU layout asserts at compile time that PORTABLE_TITLE_VIEWS == PORTABLE_VIEWS - 3 - DYNAMIC, and the layout test checks that a large title heap gets exactly that many slots. If either side changes, the build fails.
  • Hosts with a full HEAP_VIEWS heap are unchanged: the clamp returns the index as it is.

Tests: the new bitcode::tests::title_indices_past_the_portable_title_slots_read_the_null_view checks the redirect on the portable heap and that there is none on the full heap. victory-gpu: 197 passed, 0 failed. victory-shader --lib: 268 passed, and the same 3 NVIDIA failures as main. Clippy is clean in both configurations, and xtask policy is ok.

I didn't write the end-to-end test you suggested (fill a dynamic slot, read a title index with the same number). The shader test harness doesn't build the host's heap layout, so it would mean new harness work. The unit test checks the generated select and compare instead.

In game (RTX 4070 SUPER, with #2464 on top so that the world draws): a 6-minute run with no device removal, no draws dropped for lack of dynamic slots, and the world draws at 0.6–1.7 fps.

@kotekompotek

Copy link
Copy Markdown

Reviewed: pulling PORTABLE_VIEWS/HEAP_VIEWS/HEAP_SAMPLERS from victory_shader::emit makes the shader-side index clamps and the host heap cap come from one constant — the right dependency direction (the compiler must know what the runtime caps). The visible_heap probe for tier-3 extra capacity is a clean capability check. CI pending at review time.

@alonso49noob

Copy link
Copy Markdown

First independent run, on Blackwell: RTX 5060 8 GB, driver 621.01, Windows 11, Rust 1.98.1. Base is main 13f91ca + #2357 + #2587, so the shader suite is not stopped by the #2571 crash or the two fixture failures. These are local merges, and each tree is built in its own target dir.

tree cargo test -p victory-gpu --lib cargo test -p victory-shader --lib
base 193 passed, 2 failed (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) 270 passed, 1 failed (a_heap_buffer_read_where_no_buffer_view_is_reads_zeros)
base + this PR (36ccd6e) 197 passed, 0 failed 271 passed, 1 failed (same test)
base + #1777 (bd76752), for comparison 197 passed, 0 failed 271 passed, 0 failed

This PR merges cleanly onto 13f91ca together with #2357 and #2587. The shader test that still fails stops at stride_tests.rs:352 with dispatch failed (E_INVALIDARG). The shader crate's own test host still makes the View::NullBuffers heap with HEAP_VIEWS slots (emit/host.rs:1466), and this PR does not change that host. #1777 sizes that heap with heap_views(), which is why the test passes there. This matches the result reported above on the 4070 SUPER, so it is outside this PR's scope rather than a regression.

Written with AI help (Claude Code); the numbers are from runs on my machine.

@Vedoxdev123

Copy link
Copy Markdown

RTX 4060 test (AI-assisted)
Windows 64-bit, Ryzen 5 5600X, GeForce RTX 4060 (8 GB), on main 13f91ca + this PR (46d530a).

@moe-miau

Copy link
Copy Markdown
Author

Thanks @alonso49noob and @Vedoxdev123 for the RTX 5060 and 4060 runs: the same 197/0 here. Agreed on a_heap_buffer_read_where_no_buffer_view_is_reads_zeros: it fails because the shader crate's own test host (emit/host.rs) still makes its null-buffer heap with HEAP_VIEWS slots, which this PR leaves alone on purpose. #1777 sizes that heap, so the two together clear it.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Thanks. Noted: if #1777 lands first, you will rebase this as a follow-up that keeps only the full DYNAMIC range and the title-index null redirect. The verdict stays the same (needs the maintainer's decision between this PR and #1777). CI: passing.


Generated by Claude Code

moe-miau and others added 3 commits November 26, 2026 02:43
…first shader

On NVIDIA the shader-visible CBV/SRV/UAV heap holds at most 1,000,000
descriptors, while translated shaders clamp their heap indices below
HEAP_VIEWS (2^21). Programs translated before the title made its heap,
and any index past the host heap, read descriptors that do not exist.
The host now probes the largest view heap it can make before the first
translation and gives the translator that limit: indices at or past it go
to a null slot the capped heap keeps, and the limit is part of the shader
cache key. Raw-memory view pairs and dynamic slots keep their ranges.

On this RTX 4070 SUPER, `cargo test -p victory-gpu` goes from 2 failures
(`dynamic_slots_are_held_until_released`,
`lists_share_a_table_and_copies_keep_their_views`) to none.

Ported from Zettrox/ztrx (commit 578d459), based on fxceless/victory-fork.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With 2^16 dynamic slots on NVIDIA's 1,000,000-view heap, draws were
dropped for lack of dynamic slots in game (40 to 103 times a run). The
title's slots now give way to the full 2^18; about 737,000 remain, above
the highest title slot seen (about 720,000).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the null view

The capped layout puts the host's dynamic slots right after the title's
737,853 (PORTABLE_TITLE_VIEWS). An index the title takes from the
resource heap itself past them passed the clamp at the heap's end and
could name a live dynamic descriptor. Such indices (every path from the
heap) now read the general null view; table indices, which may name
dynamic slots, keep the heap-wide clamp. The GPU layout asserts the
title slot count shaders assume.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@moe-miau

Copy link
Copy Markdown
Author

On the choice with #1777: whichever the maintainer prefers. If #1777 lands, I'll rebase this onto it as a follow-up carrying only what #1777 doesn't have: the full DYNAMIC range on a capped heap (with MIN_DYNAMIC the RTX 4070 SUPER dropped draws for want of dynamic slots in the cutscene) and the title-index redirect past PORTABLE_TITLE_VIEWS to the null view (DrDarkDK's review case). If this one is preferred, I'm happy to credit #1777's author for the probe.

JohnTWillemsen added a commit to JohnTWillemsen/VIctory-W that referenced this pull request Nov 27, 2026
JohnTWillemsen added a commit to JohnTWillemsen/VIctory-W that referenced this pull request Nov 27, 2026
JohnTWillemsen added a commit to JohnTWillemsen/VIctory-W that referenced this pull request Nov 27, 2026
JohnTWillemsen added a commit to JohnTWillemsen/VIctory-W that referenced this pull request Nov 27, 2026
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.

6 participants