Skip to content

GPU: the heap clamp bound must fit what D3D12 gives - #2315

Open
xiaotu030202 wants to merge 1 commit into
Marvisak:mainfrom
xiaotu030202:gpu/heap-clamp-bound-fits-host
Open

xiaotu030202 wants to merge 1 commit into
Marvisak:mainfrom
xiaotu030202:gpu/heap-clamp-bound-fits-host

Conversation

@xiaotu030202

Copy link
Copy Markdown

What this fixes

victory-shader clamps every heap index a translated shader computes to HEAP_VIEWS - 1, and victory-gpu has to give it a heap holding those slots. The two disagreed, and the gap removed the host device.

HEAP_VIEWS was 1 << 21 — 2,097,152. D3D12 takes 1,000,000 descriptors in a shader-visible CBV/SRV/UAV heap on every resource binding tier and refuses one more. Measured on an RTX 5070 Ti, creating a shader-visible view heap at each size:

view   1000 -> OK
view   10000 -> OK
view   65536 -> OK
view   100000 -> OK
view   262144 -> OK
view   500000 -> OK
view   524288 -> OK
view  1000000 -> OK
view  1048576 -> FAIL 0x8007000E  (E_OUTOFMEMORY)
view  1310720 -> FAIL 0x8007000E
view  2097152 -> FAIL 0x8007000E
sampler   2048 -> OK
sampler   4096 -> FAIL 0x8007025D

So the first request in HostHeap::new:

} else if let Ok(heap) = make((count + DYNAMIC).max(views)) {

could never succeed — on any device, from any vendor. Every host fell into the fallback, which asked for count + MIN_DYNAMIC (65,536 + the title's) and got it: a heap of 65,540 slots in the host() test heap. Translated shaders still clamped to 2^21 - 1, so a shader reading a computed index past what the heap held handed the GPU a descriptor that did not exist. That is 0x887A0005 (DXGI_ERROR_DEVICE_REMOVED), and once the device was gone every later test in the process failed — one of them then took the whole test binary down with STATUS_STACK_BUFFER_OVERRUN.

The fallback's comment blamed NVIDIA:

// shortcut: a host that caps its heaps below `HEAP_VIEWS` (NVIDIA)
// faults on an index past them, as before the clamp.

It was every vendor; the comment was written from an AMD card, where the fallback path also gets taken but the tests that exercise it are the ones gated on hardware this project was developed against. It is also the same shape as #59 (device removal while loading the world on an RX 9070 XT).

What changed

Three things, all where the heap geometry is decided:

crates/victory-shader/src/emit/shader.rs — HEAP_VIEWS is 1 << 19. The clamp bound cannot take the whole 1,000,000 ceiling: the slots kept past it need room too.

crates/victory-gpu/src/descriptor/host.rs

  • MIN_DYNAMIC becomes PAIR_RESERVE (1 << 16): the slots HostHeap::pair hands a host copy's two views. This was a real second bug the first fix exposed — with a heap that exactly reaches the clamp bound, every pair went to a dynamic slot, and memory::tests::lists_share_a_table_and_copies_keep_their_views caught it (pairs = (2, 4), wanting >= 1 << 18).
  • extra counts the dynamic slots alone, so pairs starts past them and has room to the end of the heap.
  • The branch chain asks for one size — max(clamp bound, title count) + PAIR_RESERVE + DYNAMIC, capped at PORTABLE_VIEWS — and falls back to the same size minus the dynamic range, never below the clamp bound.

A const assertion holds the invariant that was violated:

const {
    assert!(victory_shader::emit::HEAP_VIEWS <= PORTABLE_VIEWS);
    assert!(
        victory_shader::emit::HEAP_VIEWS as u64 + DYNAMIC as u64 + PAIR_RESERVE as u64
            <= PORTABLE_VIEWS as u64
    );
}

Tests

New: the_clamp_bound_fits_in_a_host_heap — asks the device for a real heap and asserts NumDescriptors >= HEAP_VIEWS. That is exactly the property that was false, and it fails on the old code.

Measured here (Windows 11, RTX 5070 Ti, Rust 1.98.1 pinned, --test-threads=1), before → after:

group before after
descriptor:: 9 passed, 1 FAILED 11 passed
memory:: 2 passed, 1 FAILED 3 passed
draw:: 1 passed, 4 FAILED 4 passed, 1 FAILED
victory-shader 245 passed, 2 failed 245 passed, 2 failed (unchanged)

indirect::, list::, host::, pipeline::, device::, queue::, query::, fence::, frame::, dxgi::, bindless::, object::, primitive::, resource:: are all green.

What is left, and why it is not here

Two failures remain and neither comes from this patch — I stashed it and re-ran to confirm:

  1. draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view fails at CreateRootSignature with 0x887A0005 on its own, isolated, with the patch stashed as well. It does not touch the heap geometry (it builds its own Slots::new(4) and a fixed root signature).
  2. victory-shader's stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride and synthetic_tests::gathers_at_a_level_match_the_hardware compare shader output against RDNA2 hardware results. On a GeForce the first returns 11 of 32 lanes' worth (left: [101..131, 0, 0, ...], right: [200..231]). Both fail identically with the patch stashed. Judging them needs an AMD card, and they deserve their own issue rather than a patch from here.

I could not run the title (no game files), so this is not evidence that the world now renders. What it does establish is that a host heap can now hold every index the translator is allowed to name, and that the tests which check it pass — including on NVIDIA, where they could not before.

Environment

Windows 11, Ryzen 7 9800X3D, RTX 5070 Ti (driver 32.0.16.2140), MSVC 14.44.35207, Windows SDK 10.0.26100.0, VICTORY_DXIL pointed at that SDK's dxil.dll.

`victory-shader` clamps every heap index a translated shader computes to
`HEAP_VIEWS - 1`, and `victory-gpu` must give it a heap that has those
slots. The two disagreed.

`HEAP_VIEWS` was `1 << 21` (2,097,152). D3D12 takes 1,000,000 descriptors
in a shader-visible CBV/SRV/UAV heap on every resource binding tier and
refuses one more with `E_OUTOFMEMORY` — measured on an RTX 5070 Ti, where
every size up to 1,000,000 succeeds and 1,048,576 fails:

    view   1000000 -> OK
    view   1048576 -> FAIL 0x8007000E (E_OUTOFMEMORY)
    view   2097152 -> FAIL 0x8007000E

So the first request, `make((count + DYNAMIC).max(views))`, could never
succeed on any device. Every host fell into the fallback, which asked for
`count + MIN_DYNAMIC` (65,536 + the title's) and got it — a heap smaller
than the index shaders were still allowed to name. A shader that read one
past it removed the device (`0x887A0005`, and one test then took the whole
test process down with `STATUS_STACK_BUFFER_OVERRUN`).

The comment on that fallback blamed NVIDIA; it was every vendor. The world
runs in Marvisak#59 (a device removal on an RX 9070 XT) are the same shape.

Three changes, all in the one place the geometry is decided:

- `HEAP_VIEWS` is `1 << 19`. The bound has to share D3D12's 1,000,000 with
  the slots kept past it, not take the whole ceiling.
- `PAIR_RESERVE` (`1 << 16`) replaces `MIN_DYNAMIC`: the slots
  `HostHeap::pair` hands a host copy's two views. Without a reserve, a heap
  that exactly reaches the clamp bound sends every pair to a dynamic slot,
  which the clamp can then reach. `extra` counts the dynamic slots alone,
  so `pairs` starts past them and has room to the end.
- A `const` assertion holds the invariant: the clamp bound, the dynamic
  slots and the pair reserve together must fit in `PORTABLE_VIEWS`.

A test, `the_clamp_bound_fits_in_a_host_heap`, asserts the heap the device
actually gives holds every index the clamp reaches — the thing that was
false.

Measured on Windows 11, RTX 5070 Ti, Rust 1.98.1 (pinned), before → after,
single-threaded:

    descriptor::     9 passed, 1 FAILED  ->  11 passed
    memory::         2 passed, 1 FAILED  ->   3 passed
    draw::           1 passed, 4 FAILED  ->   4 passed, 1 FAILED
    victory-shader   (unchanged: 245 passed, 2 failed)

The remaining two are other faults, unchanged by this: `draw`'s
`a_texture_written_in_one_format_reads_in_another_through_a_placed_view`
fails the same way with this patch stashed, and `victory-shader`'s
`stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride` and
`synthetic_tests::gathers_at_a_level_match_the_hardware` compare against
RDNA2 results a GeForce does not reproduce. Both stashed-and-rerun the
same, so they are not from here; the last two would need an AMD card to
judge and are worth their own report.
@andrewpozdnakov7-jpg

Copy link
Copy Markdown

The NVIDIA ceiling is reproducible on the reported setup. Tier 3 also permits larger shader-visible view heaps when the driver accepts them, per the D3D12 Resource Binding specification.

I ran the same synthetic fixture on app-local WARP Tier 3. A direct CreateDescriptorHeap succeeded with 1,000,001 and 2,097,152 shader-visible CBV/SRV/UAV descriptors on all three heads. The VIctory entry point behaved differently:

Tree HostHeap::new(false, 4) HostHeap::new(false, 1_245_184)
upstream 940d32f 2,097,152 host slots 2,097,152 host slots, all title slots retained
this PR 43aa1b2 851,968 host slots None
#2177 1a3261f 2,097,152 host slots 2,097,152 host slots, all title slots retained

The same assertion passes on main and #2177 and fails on this PR. The early least > PORTABLE_VIEWS return replaces a successful larger allocation on this device. A capability probe with a capped fallback, as in #2177, addresses the NVIDIA ceiling while preserving the successful Tier 3 path here.

AI-assisted. Synthetic tests only.

@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 against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Functionally fixes both pre-existing NVIDIA view-heap baseline failures (#1744): descriptor::host::tests 6/6 passed on RTX 5090 and 6/6 on WARP, and memory::tests 3/3 passed on RTX 5090.
  • cargo xtask policy: 183 files ok.
  • Clippy blocker (cargo clippy -p victory-gpu --all-targets -- -D warnings):
    error: this assertion has a constant value
        --> crates\victory-gpu\src\descriptor\host.rs:1532:9
         |
    1532 | /         assert!(
    1533 | |             victory_shader::emit::HEAP_VIEWS <= PORTABLE_VIEWS,
    1534 | |             "HEAP_VIEWS is past what D3D12 gives a shader-visible heap"
    1535 | |         );
         | |_________^
         |
         = help: consider moving this into a const block: `const { assert!(..) }`
         = note: `-D clippy::assertions-on-constants` implied by `-D warnings`
    
    Wrapping that assertion in const { assert!(...); } will make clippy --all-targets -- -D warnings pass cleanly.

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: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving the shader side — heap clamp bound fits what D3D12 gives. GPU side Windows-only, static.

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.

4 participants