Skip to content

GPU, Shaders: view structured buffers raw on host for NVIDIA heap reads (FIND-11) - #2885

Open
jujumoneylol wants to merge 3 commits into
Marvisak:mainfrom
jujumoneylol:fix/find-11-raw-structured-buffers
Open

jujumoneylol wants to merge 3 commits into
Marvisak:mainfrom
jujumoneylol:fix/find-11-raw-structured-buffers

Conversation

@jujumoneylol

Copy link
Copy Markdown

Summary

Fixes FIND-11 (tracked in #1633 / #1744): NVIDIA GPUs misread heap-indexed structured buffers when a DXIL raw handle (ByteAddressBuffer, resource kind 11) indexes into a host D3D12 structured buffer descriptor (DXGI_FORMAT_UNKNOWN, StructureByteStride > 0).

Root Cause

In DXIL, annotateHandle(createHandleFromHeap(idx), ResourceProperties) requires a compile-time constant resource kind and StructStrideInBytes. Because heap-indexed buffers have runtime-variable strides (loaded from VIEW_STRIDES), victory-shader lowers heap-indexed buffer accesses through raw handles ((11 | u, 0)) and computes byte = index * view_stride[idx] + offset.

Previously, victory-gpu created host D3D12 structured views (StructureByteStride = S) for structured buffers. On NVIDIA hardware (verified on Turing GTX 1650, matching the RTX 5070 report in #1744), rawBufferLoad(raw_handle, byte_offset, undef) against a structured descriptor divides byte_offset / 4 and uses that as the element index at inner offset 0 — reading [101, 104, 107, 110, ...] instead of [200, 201, 202, ...] for a 12-byte stride buffer with offset:4.

Changes

  • crates/victory-gpu/src/descriptor/host.rs:
    • Represent structured buffer SRVs and UAVs (StructureByteStride > 0, 4-byte-multiple byte length fitting in u32 dwords) as host raw views (DXGI_FORMAT_R32_TYPELESS, FirstElement = offset / 4, NumElements = total_bytes / 4, StructureByteStride = 0, D3D12_BUFFER_{SRV,UAV}_FLAG_RAW), respecting D3D12's 16-byte raw-view alignment (D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT via shadow::for_view(..., 16) in both buffer_srv/buffer_uav and view_copy).
    • Keep the title's original element stride in VIEW_STRIDES via view_stride().
    • Add unit test structured_buffer_views_stay_raw_on_host_and_keep_stride_table verifying host view creation, VIEW_STRIDES preservation, and zero D3D12 debug-layer errors.
  • crates/victory-gpu/src/draw.rs:
    • Update structured_views_off_their_buffers_elements_get_their_own_copy for 16-byte raw-view alignment (2 * 456 = 912 uses the buffer's copy; 456 (8 mod 16) and 100 (4 mod 16) get exact copies).
  • crates/victory-shader/src/emit/lower.rs:
    • Lower table-bound structured buffer loads, stores, and atomics through raw handles ((11 | u, 0)) with explicit byte = index * stride + offset so descriptor kinds match the raw host views across both table-bound and heap-indexed paths.
    • Lower structured buffer getDimensions (Fields::Buffer and Fields::Smem) through raw handles ((11 | u, 0)) and divide the returned byte count by stride (UDiv) to recover the element record count.
  • crates/victory-shader/src/emit/host.rs & diff_tests.rs:
    • Update the test harness's Stride::Structured to create host raw SRVs matching victory-gpu, while adding Stride::PcStructured for DXC-compiled PC reference shaders in diff_tests.
  • crates/victory-shader/src/emit/stride_tests.rs:
    • Add heap_indexed_structured_buffers_with_distinct_strides (multiple distinct strides in one heap on hardware + WARP).
    • Add table_bound_structured_buffer_reads_by_element_and_dimensions (table-bound idxen + offset:4 + num_records via getDimensions on hardware + WARP).

Verification (NVIDIA GeForce GTX 1650 + WARP, Windows 11)

  • Before (on main f3578e3):
    • emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride FAILED on hardware: got [101, 104, 107, 110, 113, 116, 119, 122, 125, 128, 131, 0, ...] instead of [200..231].
  • After (rebased on main 842b0cb):
    • emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride: PASS on both hardware (GTX 1650) and warp.
    • emit::stride_tests::heap_indexed_structured_buffers_with_distinct_strides: PASS on both hardware and warp.
    • emit::stride_tests::table_bound_structured_buffer_reads_by_element_and_dimensions: PASS on both hardware and warp.
    • Full cargo test -p victory-shader --lib: 303 passed (only failure is the pre-existing FIND-9 NVIDIA 1M view-heap cap test a_heap_buffer_read_where_no_buffer_view_is_reads_zeros).
    • Full cargo test -p victory-gpu --lib: 227 passed including 20/20 draw::tests and structured_buffer_views_stay_raw_on_host_and_keep_stride_table with VICTORY_D3D12_DEBUG=1 (only failures are the 2 pre-existing FIND-9 1M view-heap cap tests).
    • cargo clippy -p victory-shader -p victory-gpu --all-targets -- -D warnings: 0 warnings/errors.
    • cargo check --workspace --all-targets: clean.
    • cargo xtask policy: pass (asset policy: 191 files ok).

Notes

  • Clean-room implementation and synthetic tests only; no game binaries or assets used.
  • AI-assisted (Claude/Gemini-assisted coding, reviewed and hardware-verified locally on GTX 1650 + WARP).
  • Claims coordination: FIND-11 in Coordinating who works on what #1633.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Verified 41773eb8 on Windows 11 (NVIDIA RTX 5090 Blackwell + WARP + dxil.dll) against main (842b0cb):

Before / After on RTX 5090 (FIND-11 confirmed fixed!)

  • Baseline main (842b0cb): emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride fails deterministically on RTX 5090 (returns [101, 104, 107, ...] instead of [200, 201, ...]).
  • With #2885 (41773eb8): a_heap_buffer_read_by_element_takes_the_views_stride, heap_indexed_structured_buffers_with_distinct_strides, and table_bound_structured_buffer_reads_by_element_and_dimensions all pass on both RTX 5090 and WARP!
  • stride_tests: 9/10 passed on RTX 5090 (only the pre-existing baseline failure a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, which also fails with E_INVALIDARG on 842b0cb on RTX 5090).

Suite & Audit Notes

  • victory-shader: emit::lower::tests 33/33, diff_tests 5/5 passed; victory-gpu: RTX 5090 descriptor::host::tests 15/15 (including structured_buffer_views_stay_raw_on_host_and_keep_stride_table) & draw::tests 20/20, WARP draw::tests 20/20 passed; cargo clippy (default & diagnostics) 0 warnings; cargo xtask policy (191 files ok; emit/lower.rs = 521,447 bytes, 2,841 bytes under 512 KiB).
  • Non-blocking notes: (1) if a table-bound structured view falls back to a native structured descriptor (total_bytes % 4 != 0), the shader side still emits a raw handle (rare in practice since structured strides/totals are almost always 4-byte multiples); (2) touches buffer_srv/buffer_uav in descriptor/host.rs alongside #2873, so landing order will need a small merge resolution.

Great fix for NVIDIA — looks ready to merge.

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

Correct raw-view rules: R32_TYPELESS + FLAG_RAW is the only legal representation, gated on byte-length divisibility and u32 element count, with the 16-byte FirstElement alignment pushed down into view_align so for_view reserves a conforming copy. Complements #2873's format legalization. LGTM.

@alonso49noob

Copy link
Copy Markdown

RTX 5060 8 GB (Blackwell), driver 621.01, Windows 11, Rust 1.98.1. I compared main 63bcca4 with main + this PR (41773eb), which merges cleanly. Each tree was built in its own target dir. The full shader runs skip the #2571 crash test.

main 63bcca4 + this PR
stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride (x3) fails, [101, 104, 107, …] passes 3 of 3
synthetic_tests::gathers_at_a_level_match_the_hardware (x3) fails, lane 0 [0, 1, 0, 0] passes 3 of 3
the PR's two new stride_tests n/a pass
cargo test -p victory-shader --lib 298 passed, 3 failed 302 passed, 1 failed
cargo test -p victory-gpu --lib 229 passed, 2 failed 230 passed, 2 failed

The gather test is the other fixture named when #2357 was closed, and the reports above don't cover it. It passes here, and this PR doesn't change synthetic_tests.rs. The failures left are the HEAP_VIEWS cap ones, the same as on main: a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views. I saw no new failures.

Unrelated note: descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait failed in 1 of 3 full victory-gpu runs on main. It passed 6 of 6 alone and in every other full run on both trees, so it looks intermittent and not tied to this PR.

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

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

Ampere data point: RTX 3080 (617.42), Windows 10 19045, main 63bcca4 + #486 (Agility runtime for the shader tests) + #2596 (#2571), --test-threads=1.

victory-shader --lib victory-gpu --lib
base 298 passed, 4 failed 226 passed, 5 failed
+ #2885 302 passed, 2 failed 227 passed, 5 failed
+ #2885 + #2461 303 passed, 1 failed 230 passed, 3 failed
  • With this PR: a_heap_buffer_read_by_element_takes_the_views_stride and gathers_at_a_level_match_the_hardware pass, along with the new stride tests and structured_buffer_views_stay_raw_on_host_and_keep_stride_table. Nothing new fails.
  • With #2461 as well: the two PRs merge cleanly onto main and with each other, and #2461 clears the heap-cap failures.
    • Shader suite: what's left is pops_wait_loop_exits_immediately_for_inactive_lanes, which needs an SM 6.6 WARP that Windows 10 doesn't have.
    • GPU suite: the three left are the Windows 10 in-box-runtime failures that an Agility opt-in for the GPU test binary removes (see #2878).

So on this card the two together leave no NVIDIA-specific failure.

One scope note: unlike the raw views in #2878, which only apply on NVIDIA, this views structured buffers raw on every host, and getDimensions divides by the stride everywhere. The runs so far are NVIDIA and WARP, so a run on AMD hardware would close that.

@alonso49noob: the intermittent views_bound_while_another_slot_gets_its_texture_do_not_wait on main is a race between tests over the shared WHILE_PREPARING hook from 42ea09f. It's unrelated to this PR, and #2585 fixes it.

@steven-seagull27

Copy link
Copy Markdown

@jujumoneylol I found a WARP regression when a shader writes through one structured view and another shader reads the same records through a view with a different starting offset. The NVIDIA fix for reading the untouched fields also showed up in this comparison.

Wrong words out of 2,400 GeForce RTX 5080, 621.40 Windows WARP, 10.0.26100.10068
Direct parent 842b0cb 2,375 0
This PR, 41773eb 544 544

On this PR, both adapters returned all 1,600 untouched fields and all 160 newly written values read through the writer's own origin. All 544 stale words were reads of a GPU-written field through a different origin. For example, a writer at byte 12 and a reader at byte 24 share 31 records. Reader lane 0 should see the 1 stored by writer lane 1. It saw the initial 0xa1700008. All 31 overlapping records were stale. The last, non-overlapping record kept its correct initial value.

The fixture places 32-element views at byte offsets 0, 12, 24, 36 and 48 in a 432-byte write-watched title GPU buffer. Every access is inside its declared view. Each writer and reader runs in a separate fence-waited submission and schedules the same heap refresh that draw::State::bind uses. There are no intervening CPU writes. Your existing descriptor/stride test still passed on both adapters. The same normal-submit comparison measured 544/2,400 wrong on each adapter on the current #2178 head 319749e. Related alignment and alias results on #2178

For the alignment fallback, the acceptance case I would keep is that the later SRV sees all 31 overlapping writes across differently aligned origins. The test-only commit directly on this PR can be run in a separate checkout with:

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/pr2885-fenced-structured-readback
git switch --detach FETCH_HEAD
foreach ($adapter in '0', '1') {
    $env:VICTORY_D3D12_WARP = $adapter
    cargo test -p victory-gpu --all-features --lib --locked gpu_writes_visible_through_overlapping_views -- --ignored --nocapture --test-threads=1
}

Synthetic D3D12 readbacks on Windows. The game and AMD hardware remain untested here. AI-assisted with Codex.

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

Changing my earlier approval. @steven-seagull27's fixture (8c474d5) reproduces exactly on the RTX 3080 (617.42) / Windows 10. Hardware adapter, with an Agility opt-in for the GPU test binary (Windows 10's WARP has no SM 6.6, so no WARP row here):

wrong words / 2,400 RTX 3080
main 63bcca4 + fixture 2,375
#2885 41773eb + fixture 544

All 18 failing reads on this PR are the GPU-written field read through a different origin, as they describe. The pattern also shows the cause. The five origins are bytes 0, 12, 24, 36 and 48. The two that are 16-byte aligned, 0 and 48, see each other's writes: writer 0 → reader 4 and writer 4 → reader 0 are both clean. Every pair involving an origin that isn't 16-byte aligned is stale (29-31 of 32).

That matches the fallback in this PR: a raw view whose start isn't 16-byte aligned gets "a copy of its own bytes", which draw.rs's updated test now asserts for 456 = 8 mod 16. A write through one such copy never reaches another view of the same records. On hosts where structured views read correctly today (WARP: 0 wrong on main, presumably AMD too), that's a regression.

Two shapes that would keep one copy:

  • View raw from the start rounded down to 16 bytes and add the remainder in the shader (the stride table already carries per-view data).
  • Keep the native structured view where the start isn't 16-byte aligned. That still leaves NVIDIA's structured-read problem for those views, but doesn't break the others.

The fixture is a good acceptance test to carry into the PR. All 31 overlapping writes should be visible across differently aligned origins.

Marvisak commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Thanks for e7250c5; both points are addressed. In descriptor/host.rs, buffer_srv/buffer_uav now view structured buffers raw from the preceding 16-byte boundary of the shared copy, with alignment RAW_WORD_ALIGN. The 0-12-byte remainder goes into VIEW_STRIDES bits 28-29, and the shader adds it back (view_raw_bias in emit/lower.rs), so overlapping views share one copy. The overlap fixture shows 0 of 2,400 words wrong on both adapters. Structured views that cannot be made raw are now refused instead of falling back to a structured descriptor. VIEW_STRIDE_BITS shrinks to 28 bits, which still leaves room for any real stride.

Notes for the maintainer: every buffer handle now reads the stride table (a small per-access cost), and lower.rs is about 1.5 KiB under the policy size cap. This replaces the approach in #2178 (open, global toggle, private copies), so only one should land.

CI: passing.


Generated by Claude Code

lhnizdo added a commit to lhnizdo/VIctory that referenced this pull request Nov 26, 2026
…t of their own

Review of Marvisak#2878: the NVIDIA raw-structured change in the shader crate's
test host is a separate matter (and is to be checked against Marvisak#2885).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
@jujumoneylol

Copy link
Copy Markdown
Author

Thanks @steven-seagull27, @fyernald, and the triage review — pushed e7250c5 addressing both findings:

1. Overlapping structured views now share one host copy (no private copies for 4-byte-aligned starts)

  • victory-gpu (descriptor/host.rs): Structured buffer SRVs/UAVs now use 4-byte alignment (RAW_WORD_ALIGN = 4) in shadow::for_view and view_align instead of forcing 16-byte private copies. When shadow.offset(start) is 4-byte aligned (not 16-byte aligned), buffer_srv/buffer_uav round FirstElement down to the preceding 16-byte boundary (aligned = offset - bias, NumElements = (total_bytes + bias) / 4), adjust MARK to shadow.mark_entry(start - bias), and store the 0..12-byte start remainder in VIEW_STRIDES bits 28..29 (VIEW_RAW_OFFSET_SHIFT = 28, 2 bits for 0..=3 dwords; VIEW_STRIDE_BITS = (1 << 28) - 1).
  • victory-shader (emit/shader.rs, emit/lower.rs): Record all buffer handles (kinds 10..=12, SRV and UAV) in view_index, add view_raw_bias(h) ((word >> 26) & 0xC) to raw_view offsets for heap-direct and table-bound structured buffers, and subtract it before UDiv in getDimensions so element counts remain exact.
  • Carried @steven-seagull27's overlapping-views fixture (crates/victory-gpu/src/descriptor/overlap_test.rs) directly into the default test suite (skipping cleanly on hosts without SM 6.6).

2. No native structured descriptor fallback behind raw shader handles

  • buffer_srv and buffer_uav now refuse structured views whose byte length is 0, not a multiple of 4, not 4-byte aligned on the host copy, or whose raw dword count overflows u32, rather than falling back to creating a native D3D12 structured descriptor. Covered in structured_buffer_views_stay_raw_on_host_and_keep_stride_table.

Verification (e7250c5, Windows 11, VICTORY_D3D12_DEBUG=1)

Test / Check GeForce GTX 1650 Windows WARP
overlap_test::gpu_writes_visible_through_overlapping_views 0 / 2,400 wrong (PASS, 0 D3D12 errors) 0 / 2,400 wrong (PASS, 0 D3D12 errors)
stride_tests::table_bound_structured_buffer_reads_by_element_and_dimensions (bias=0 & bias=12) PASS PASS
stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride + heap_indexed_structured_buffers_with_distinct_strides PASS PASS
victory-gpu descriptor::host::* + draw::tests (--test-threads=1) 36/37 PASS (only pre-existing FIND-9 dynamic_slots_are_held_until_released) 36/37 PASS (only FIND-9)
victory-shader (stride_tests, diff_tests, lower::tests, waterfall_tests, synthetic_tests) 63/64 PASS (only pre-existing FIND-9 a_heap_buffer_read_where_no_buffer_view_is_reads_zeros) —
cargo clippy -p victory-shader -p victory-gpu --all-targets -- -D warnings (default & --all-features) 0 warnings —
cargo xtask policy pass (192 files ok, lower.rs = 522,717 bytes) —

@jujumoneylol

Copy link
Copy Markdown
Author

Pushed one small cleanup/fix ($sha):

  • Lower::view_raw_bias now reuses the VIEW_STRIDES dword already loaded by Lower::buffer_guard (guarded_stride) instead of reloading VIEW_STRIDES[index] via self.s.view_index.
  • This avoids a redundant root-SRV load on every heap-direct buffer access and prevents an out-of-bounds VIEW_STRIDES[null_buffer_view()] read when �uffer_guard redirects a non-buffer slot to the null buffer view on a small VIEW_STRIDES table (caught when testing GPU, Shaders: view structured buffers raw on host for NVIDIA heap reads (FIND-11) #2885 stacked with GPU and shaders: a host that caps its view heaps gets heaps at its ca… #2461 on GTX 1650 in �_heap_buffer_read_where_no_buffer_view_is_reads_zeros, which now passes cleanly).
  • Verified cargo xtask policy (lower.rs 523,185 bytes < 512 KiB), cargo clippy -p victory-shader -p victory-gpu --all-targets -- -D warnings, and overlap_test + structured-buffer descriptor tests on GTX 1650 (VICTORY_D3D12_DEBUG=1).

@frenziedcoder

Copy link
Copy Markdown

RTX 5080 (Blackwell), driver 621.40, title runs: with this PR in place of #2464 in the NVIDIA stack, the interior shots draw a character that #2464's tree draws as broken spikes, with no other change in what translates and no device removal in 600 s. For the choice between this PR and #2464 the triage mentions, measured in the title rather than synthetically.

Trees: main 842b0cb + #1906, #2098, #2626, #2628, #2176 (fb99078), #2118 + the local wave32_approx change and BVH-builder hold-back from #2176's thread, and then either #2464 (b9dc4b0) or this PR (e7250c5; #2464 reverted cleanly, this PR merged cleanly). Diag build, VICTORY_D3D12_DRED=1, scripted Enter, 600 s, one run each.

with #2464 with this PR
programs refused (translate --cache) 426 426
pipelines skipped, compute / pixel shader does not translate 219 / 1 219 / 1
programs whose draws were dropped in the run 85 85
host pipelines refused 0 0
device removed in 600 s no no
world's first frame frame 1,268 (52 s) frame 1,346 (56 s)
frame 2,048 reached at 528 s 548 s
interior shot (frames ~1,500–2,048) window and room; where the character stands, spiky dark shards (frames 1,508, 1,748, 2,048) the character drawn whole in front of the window (head, hair, shoulders), room less broken (frames 1,586, 2,048)

The translation counts are identical, so the difference is in how the views are made. My guess is the unaligned structured origins this PR copies to aligned ones and #2464 leaves structured (steven-seagull27's 12/24/36-byte case), but I haven't traced which buffer the character reads.

Synthetic, same tree: emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride passes (it fails on main on this card); a_heap_buffer_read_where_no_buffer_view_is_reads_zeros still fails with dispatch failed: The parameter is incorrect, as on main (the view-heap cap of #1835). victory-gpu overlap tests: 2 passed.

Not tested: 767f228 (pushed after these runs; the view_raw_bias stride-reuse cleanup); more than one run per tree; this PR without the rest of the stack; AMD.

AI use: Claude Code (Claude Opus 5.5) built the trees, ran the title on my machine (sandboxed at Low integrity, game folder read-only), read the logs and frame captures, and wrote this report.

@Dukestar69 Dukestar69 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 on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-shader --lib emit::stride_tests:: -- --test-threads=1: 9/10 passed on RTX 5090 — notably a_heap_buffer_read_by_element_takes_the_views_stride now passes on RTX 5090 (fails on clean origin/main), along with both new tests (heap_indexed_structured_buffers_with_distinct_strides and table_bound_structured_buffer_reads_by_element_and_dimensions). The single remaining failure (a_heap_buffer_read_where_no_buffer_view_is_reads_zeros) reproduces identically on clean origin/main.
  • cargo test -p victory-gpu --lib descriptor::host:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released: 16/16 passed (including overlap_test::gpu_writes_visible_through_overlapping_views and structured_buffer_views_stay_raw_on_host_and_keep_stride_table).
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed.
  • cargo clippy -p victory-shader -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

Great NVIDIA fix (FIND-11): viewing structured buffers raw on the host makes heap-indexed structured buffer reads work cleanly on RTX 5090.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

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

The raw-view + VIEW_STRIDES 0..12-byte bias approach works cleanly on NVIDIA hardware — verified locally on Windows 11 with an RTX 5090 and WARP (git merge-tree against 63bcca43 clean, cargo clippy --workspace --all-targets [--all-features] -- -D warnings clean, cargo xtask policy 192 files ok):

  • descriptor::host::overlap_test::gpu_writes_visible_through_overlapping_views: passed (0/2400 mismatches) on RTX 5090, RTX 5090 + VICTORY_D3D12_DEBUG=1, and WARP + VICTORY_D3D12_DEBUG=1
  • structured_buffer_views_stay_raw_on_host_and_keep_stride_table and structured_views_off_their_buffers_elements_get_their_own_copy: passed on RTX 5090, RTX 5090 Debug, and WARP Debug
  • victory-shader stride_tests:: + diff_tests:: + waterfall_tests::: 19/20 passed on RTX 5090 + WARP (including both new structured-buffer tests; the lone a_heap_buffer_read_where_no_buffer_view_is_reads_zeros failure reproduces identically on origin/main 63bcca43 due to NVIDIA's 1M heap cap in emit::host).

Two correctness bugs should be fixed before merge:

1. Out-of-bounds VIEW_STRIDES root-SRV read and silently dropped raw_bias for dynamic-slot tables (crates/victory-gpu/src/descriptor/host.rs:129-143, 511-519)

Before this PR, table-bound structured buffers used native structured handles (12 | u, stride) and did not read VIEW_STRIDES. Now every table-bound structured buffer (u.stride >= 4) loads VIEW_STRIDES[index] via view_raw_bias(h) -> view_word(index) (lower.rs:1808-1815, 4646-4652) using the unclamped heap slot index from entry_heap_index/heap_index.

However, HostHeap::new allocates self.strides for only count.max(HEAP_VIEWS) dwords (host.rs:130), and write_stride drops any write with index >= self.count.max(HEAP_VIEWS) (host.rs:513). On uncapped heaps where count + extra > count.max(HEAP_VIEWS) (e.g. a title heap with count = 1_245_184 + DYNAMIC = 262_144, or count = 900_000 + 262_144 > HEAP_VIEWS), any fresh descriptor table placed in dynamic slots at index >= count.max(HEAP_VIEWS) has its raw_bias write silently dropped and causes the shader to read past the end of the VIEW_STRIDES root SRV (no hardware bounds check — garbage bias or GPU page fault / device removal).

Allocating strides (and marks symmetrically at lines 135 & 531) after (heap, count, extra) is resolved with (count + extra).max(views) entries, and checking index >= (self.count + self.dynamic).max(HEAP_VIEWS) in write_stride / set_mark, fixes this cleanly.

2. ClearUnorderedAccessViewUint/Float on a biased (bias = 4/8/12) structured UAV clobbers 4..12 preceding bytes in the shared host copy (descriptor/host.rs:1393-1415, draw.rs:1056-1085)

buffer_uav rounds FirstElement down by bias = offset & 15 (aligned = offset - bias) and sets NumElements = (total_bytes + bias) / 4, relying on translated shaders to skip the leading bias bytes via VIEW_STRIDES.

draw.rs::clear_unordered invokes ClearUnorderedAccessViewUint/Float directly on that biased raw host UAV without shader execution (and with unshifted rects). When bias > 0, D3D12 clears [aligned, aligned + raw_count * 4), overwriting the 4..12 bytes immediately preceding start inside the shared Shadow buffer — bytes outside buffer_range(&view) that readable did not even snapshot.

For bias > 0 structured UAVs, clear_unordered needs to restrict the clear to [bias / 4 .. raw_count) (via shifted/clamped D3D12_RECTs on the raw buffer UAV, or an exact 16-byte-aligned clear path) so preceding neighbor bytes in the shared copy are never overwritten.

3. Minor: getDimensions (num_records) with idxen == false on a table-bound structured buffer (crates/victory-shader/src/emit/lower.rs:4442-4455)

When a table-bound buffer with used.stride >= 4 is accessed via buffer_load with idxen == false (div_stride == None, !is_heap_direct), line 4445 skips subtracting view_raw_bias(h), returning total_bytes + bias instead of total_bytes. Including || used.is_some_and(|u| u.stride >= 4) in that condition handles it consistently.

@qtLkz

qtLkz commented Nov 26, 2026

Copy link
Copy Markdown

Tested on NVIDIA Ada (the description has Turing): RTX 4070 Ti, driver 621.01, Windows 11 Pro 26300, Rust 1.98.1, this PR (767f228) on main 63bcca4, together with #2596 / #2683 / #2633 (clean merges), fixtures untouched.

cargo test -p victory-shader --lib --locked -- --test-threads=1: 313 passed, 0 failed, including the two that fail on NVIDIA on main because of raw reads through structured views:

  • stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride ... ok (on main: left: [101, 104, 107, ...], right: [200, 201, ...], the stride applied twice)
  • synthetic_tests::gathers_at_a_level_match_the_hardware ... ok

victory-gpu: 233 passed, 0 failed. Full workspace green. Details in #2317.

AI-assisted (Claude).

@steven-seagull27

Copy link
Copy Markdown

@jujumoneylol @fuzeofficlal I reproduced the structured UAV clear in review item 2 on native Windows. Both the RTX 5080 and inbox WARP gave the following results in two fresh runs per adapter and revision.

Revision Cases matching both readbacks Wrong words in shared GPU buffer Wrong words in title copy
PR head 767f228 4/16 36/512 22/512
With 22f19d9 16/16 0/512 0/512

Each case starts with 32 distinct guard words in one allocated buffer. A six-record view with a 12-byte stride starts at a 0, 4, 8, or 12-byte bias. The test clears either the whole view or the relative word rectangle [2,6), with both Uint and Float. On the PR head the whole clear changed exactly one, two, or three preceding words at the respective nonzero biases. The title copy hid those changes because it only copied the logical view back. At bias 12, the rectangle was shifted three words toward the start of the buffer, giving six wrong GPU words.

The fix uses an exact R32_UINT view with zero structure stride and no RAW flag for the biased native clear, and copies that CPU descriptor into the shader-visible slot. The clear itself produced no D3D12 InfoQueue warnings or errors on either adapter, measured after heap setup through execution. Four existing overlap, structured-view and clear-slot tests also passed on each. This covers the clear item. The stride-table and getDimensions review items remain separate.

The commit applies directly to 767f228. Fork commit · Downloadable patch

git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2885-structured-clear-range
git cherry-pick 22f19d9
$env:VICTORY_D3D12_DEBUG = '1'
$env:VICTORY_D3D12_WARP = '0'
cargo test -p victory-gpu --features diagnostics --lib structured_clears_preserve_neighbor_words -- --nocapture
$env:VICTORY_D3D12_WARP = '1'
cargo test -p victory-gpu --features diagnostics --lib structured_clears_preserve_neighbor_words -- --nocapture

AI-assisted patch and tests, run locally.

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.

10 participants