Skip to content

GPU+shader: back structured buffer views with raw descriptors for NVIDIA rawBufferLoad - #2178

Open
n-romanovich wants to merge 1 commit into
Marvisak:mainfrom
n-romanovich:pr/raw-structured-buffers
Open

n-romanovich wants to merge 1 commit into
Marvisak:mainfrom
n-romanovich:pr/raw-structured-buffers

Conversation

@n-romanovich

Copy link
Copy Markdown

Summary

On NVIDIA hardware, executing DXIL rawBufferLoad (ByteAddressBuffer) against a D3D12 StructuredBuffer descriptor (StructureByteStride > 0) multiplies the byte offset by StructureByteStride, corrupting reads and returning zeros past width / stride (as exposed by stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride).

This change:

  • Converts structured buffer SRVs and UAVs (StructureByteStride > 0, 4-byte-aligned element size) to DXGI_FORMAT_R32_TYPELESS with D3D12_BUFFER_{SRV,UAV}_FLAG_RAW in buffer_srv and buffer_uav (HostHeap still records the original element stride in VIEW_STRIDES).
  • Adds set_host_raw_structured / host_raw_structured in victory_shader::emit (enabled by victory-gpu and included in shader_cache::key) so indexed/scalar-loaded structured buffers in descriptor tables also lower to raw byte-address reads, writes, atomics, and getDimensions / stride.
  • Updates stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride to pass a raw heap descriptor (Stride::Raw) alongside the VIEW_STRIDES table, matching the emit module contract.

Verification

Tested on NVIDIA GeForce RTX 5060 Ti:

  • cargo test --release -p victory-shader --lib -- stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride diff_tests: 6 passed; 0 failed
  • cargo test --release -p victory-gpu --lib -- shader_cache::tests: 3 passed; 0 failed

@n-romanovich
n-romanovich force-pushed the pr/raw-structured-buffers branch from 5dbc85b to 9124d69 Compare November 25, 2026 07:18

@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 --lib shader_cache:: — 3 passed, 0 failed
  • cargo test -p victory-shader --lib emit::stride_tests:: — 5 passed, 1 failed on RTX 5090: directly fixes a_heap_buffer_read_by_element_takes_the_views_stride on NVIDIA! (The 1 remaining failure, a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, is the separate NVIDIA 1M view-heap cap issue covered by #2177/#2121.)
  • cargo clippy -p victory-gpu -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean raw-descriptor backing for structured buffer views on NVIDIA with cache-key inclusion. Looks good.

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. Structured buffer views backed with raw descriptors — descriptor backing change; mixed gpu+shader, relates to #2128's typed null descriptors.

RecompLive added a commit to RecompLive/victory-fork that referenced this pull request Nov 25, 2026
…y #2178)

Port of Marvisak/VIctory#2178 by n-romanovich (GPL-3.0-or-later).
Structured SRV/UAVs become R32_TYPELESS raw views (aligned, 4-byte
elements), so NVIDIA's rawBufferLoad does not multiply offsets by the
stride; the translator lowers table/scalar structured reads as raw byte
addresses with the length divided by the stride, gated by the new
emit setting that is also part of the shader cache key.

Adapted to ours: the key keeps the view count and page-table flag
(raw structured appended), and the computed-view address words. lower.rs
stays under the 512 KiB asset policy.
@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 9124d69 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 9124d69) merged onto 940d32f 245 2: stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware

So on Ada this PR fixes stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, one of the three failures #1835 tracks. #2177 fixes stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros. 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).

@Capi360

Capi360 commented Nov 25, 2026 •

Copy link
Copy Markdown

Offline A/B on an RTX 3060 Ti (8 GiB, NVIDIA 616.56), Ryzen 7 5700X3D, 32 GiB RAM, Windows 11 Pro build 26200.

Both Series X runs used the diagnostic build, one GPU list at a time, fresh caches and the default shader loop limit. The control (6934776, main 940d32f plus the NVIDIA heap bounds, packed target changes and #242) reset with 0x887A0006 at the same direct 16×16×16 compute dispatch as the packed baseline. No world-rate samples were recorded.

Adding this PR (9124d69, tested in integration commit 7f74e22) passed that point and logged seven intervals with more than 200 draws/frame, averaging 0.64 FPS. It later reset at a separate single-record indirect compute dispatch. Visible geometry and playability are still unverified. All six stride_tests passed on the integration build.

AI-assisted testing. Code-only integration branch: https://github.com/Capi360/VIctory/tree/codex/rtx-3060-ti-current-raw-views

I also tested a follow-up for structured views whose offset in the host buffer is a valid element offset but is not 16-byte aligned. This PR can leave those views structured even when the shader expects raw access. The follow-up uses the same aligned copy when creating and refreshing the view. The targeted D3D12 regression test passed on the RTX 3060 Ti. The later indirect compute reset still reproduced. Code: Capi360@9aad41f

@carlos-seiffert

Copy link
Copy Markdown

RTX 2060 6 GB (Turing), driver 621.40, Windows 11, on main ccfb029, cargo test -p victory-shader -- --test-threads=1 (hardware, then WARP) and cargo test -p victory-gpu:

build victory-shader still failing victory-gpu
this PR (pr-2178 head) 245 passed, 2 failed gathers_at_a_level_match_the_hardware, ..._no_buffer_view_is_reads_zeros (heap cap, #1779) 141 passed, 2 failed (heap cap)
this PR + #1887 246 passed, 2 failed #1887's StructuredDesc test, the heap cap 141 passed, 2 failed (heap cap)

The gather test still fails here because synthetic_tests binds its input with Stride::Structured(16), which the test host makes structured; #1887 makes the test host's view raw, and with both the gather passes. The two only conflict on one line in stride_tests.rs (this PR's Stride::Raw against #1887's read_by_element(stride) helper). #1887's StructuredDesc test reproduces the runtime's view as it is before this PR, so it should go if this lands; I'll drop it there on rebase.

🤖 Generated with Claude Code

@steven-seagull27

Copy link
Copy Markdown

On my RTX 5080, #2178 fixed the structured-buffer read at byte offset 0. I found a remaining case through the production descriptor path. With a 12-byte record stride, views starting at bytes 12, 24 and 36 returned the wrong data. A shader querying one of those 32-record views also got 10 records. Offsets 0 and 48 worked, and moving the same data from offset 12 to offset 0 restored all 32 values.

I tested on Windows 11 build 26300.10075, NVIDIA driver 621.40 and built-in WARP 10.0.26100.10068. The baseline combines this head 9124d69, #2177 at 1a3261f, and Venom2054’s heap-allocation correction. Both columns below use that same baseline. Exact integration commit

Runtime readback RTX mismatches before → after WARP mismatches before → after
Offsets, relocated input and null slot 96 → 0 of 224 0 → 0 of 224
GPU writes observed through overlapping views 1,425 → 0 of 2,400 0 → 0 of 2,400
Replacing a heap slot with different starts, strides and null views 160 → 0 of 480 0 → 0 of 480
Fixed table, record count and payload checked separately 192 → 0 of 320 0 → 0 of 320

The candidate aligns the raw SRV start down to 16 bytes, retains the original end, and records the leading dwords in the slot’s stride entry. The translated read adds that offset, and the dimensions query subtracts it. The original GPU backing stays shared. After the change, both readers saw the value 31 written by lane 31, including the offset-12 reader that previously returned zero. All tested views reported the expected 32 records. The change covers SRV reads. Displaced views whose expanded byte length exceeds the 32-bit offset range retain the previous handling. These are synthetic GPU readbacks. Game frame times remain unmeasured. Read-side fix · Four opt-in tests

The fix and tests are separate commits that can be cherry-picked onto the combined baseline. To rerun the exact published tree from a VIctory checkout in PowerShell:

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/structured-srv-readbacks
git worktree add --detach ../victory-srv-readbacks FETCH_HEAD
Set-Location ../victory-srv-readbacks
$env:VICTORY_D3D12_WARP = '0'
cargo test -p victory-gpu --all-features --lib --locked structured_srv_readbacks -- --ignored --nocapture --test-threads=1
$env:VICTORY_D3D12_WARP = '1'
cargo test -p victory-gpu --all-features --lib --locked structured_srv_readbacks -- --ignored --nocapture --test-threads=1

@Qedycik

Qedycik commented Nov 25, 2026

Copy link
Copy Markdown

Vendor matrix and an in-game measurement (RTX 5080, AMD RDNA2 iGPU)

We ran into the same NVIDIA behaviour independently, in our own tree (upstream f821cb9 plus local changes, not this PR's code), and measured both directions of the mismatch with a synthetic compute test (our own GFX10 words, no title data). Lane t reads the 16-byte element t of a heap SRV whose elements are (t, 1000+t, 2000+t, 3000+t) and stores what it read. The right answer is t.

Access, view RTX 5080 AMD Radeon iGPU (RDNA2)
raw read (offen, byte 16·t) of a structured view read in elements: 0, 4, 8, ...; zeros past the view right
indexed read (idxen) of a raw view read in 4-byte elements: 0, 1000, ... zeros
raw read of a raw view right right
indexed read lowered to raw (index·stride + offset) of a raw view right right

Driver at the time of writing: 616.56; the version during the 24 Nov measurements was not recorded.

What this means for the change here:

  • Once a view is made raw, every indexed access through it has to be lowered raw as well, on every vendor. AMD returns zeros for a structured declaration over a raw view, so a raw view that some shader still reads structured breaks AMD instead of fixing NVIDIA.
  • We made a structured view raw only when its start allows a raw view (byte offset a multiple of 16, D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT) and kept the title's kind otherwise. That matches the remaining 12-byte-stride case reported above: views at bytes 12, 24 and 36 are wrong, 0 and 48 are right.

In the world, one compute dispatch (42683eeb) reads two sizes from small structured views (strides 20 and 16). Reading zeros, its loop bounds never ended: every group ran to the translator's loop limit and the dispatch took 780-845 ms of each world frame. With structured heap views made raw on NVIDIA, the dispatch left the slow-command list (nothing above 50 ms) and host GPU time per world frame went from about 920 ms to 80-230 ms. With indexed accesses lowered raw as well (raw views on every vendor), a 90 s world run with device-removed data on had no removal and 60-180 ms of host GPU time per frame.

Not measured: this PR's own head on our machine.

(Measurements and write-up AI-assisted, Claude Code.)

@steven-seagull27

Copy link
Copy Markdown

@Qedycik, thanks for checking both vendors and measuring the slow world dispatch. Could you try the aligned-start SRV patch on your RDNA2 iGPU? The published test tree is e61603d. The four opt-in tests print the selected device. I’m particularly interested in whether views starting at bytes 12, 24 and 36 return the expected data and report all 32 records. The adapter name, driver, and any mismatch counts would help. Test commands and RTX/WARP results

If you still have that world scene handy, could you also compare the same scene on the same adapter and settings before and after the patch? The exact A/B is 7c2e21c → bb066b8. Host GPU time per frame and the time for dispatch 42683eeb (or whether it drops off the slow-command list) would let us compare with your earlier run. Either the AMD readbacks or the world timings would help on their own. Baseline · Patch

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.

8 participants