Skip to content

Shaders: use declared structured SRVs for scalar loads and record counts - #995

Closed
jeremygold02 wants to merge 2 commits into
Marvisak:mainfrom
jeremygold02:codex/declared-structured-srv-scalar-loads
Closed

jeremygold02 wants to merge 2 commits into
Marvisak:mainfrom
jeremygold02:codex/declared-structured-srv-scalar-loads

Conversation

@jeremygold02

Copy link
Copy Markdown
Contributor

Scalar s_buffer_load_dword* instructions use byte addresses. On ffe310e, a descriptor-table SRV takes the raw-buffer path even when the original shader declares a structured SRV. Reading descriptor word 2 (NUM_RECORDS) as ordinary data can also stop translation. Native stride alone cannot identify the view because a raw SRV can carry the same stride metadata.

This change checks the original PSV0 declaration and the exact, statically addressed native table slot. A unique, single-register structured SRV with a matching native binding uses structured addressing. Each dword of a wide load gets its own record index and byte offset, including when the load crosses a record boundary. A use of the record count as ordinary data or through a phi obtains it from GetDimensions. Loads that only need the descriptor handle omit that query. Raw, ambiguous, dynamically selected and mixed-consumer bindings keep their existing behavior.

Relationship to other PRs

#984, opened while this branch was being verified, also splits each scalar dword address and covers flat vector loads, stores and atomics. It selects the structured path from table binding and aligned stride. The scalar address arithmetic overlaps. This patch adds the declaration and slot check and recovers the consumed record count for that same structured SRV.

#136 demonstrated the structured scalar case using native stride. #329 recovers counts for descriptors with no tracked resource consumer. #437 preserves raw scalar loads, and #953 uses a raw view to query size for a scalar consumer. The case here reads an element count from a declared structured view and addresses each scalar dword within that view.

Verification

  • On the submitted revision against ffe310e, all five new regression tests and the upstream stencil-export test passed with Windows SDK 10.0.26100.0 dxil.dll. The broader victory-shader library run passed 103 tests, with one benchmark ignored and 22 GPU or differential cases filtered. cargo clippy --locked -p victory-shader --all-targets --all-features -- -D warnings passed.
  • On parent c2e7254, two vertex shaders failed on the scalar descriptor-data read. Both translated and passed SDK validation on 89b6c67. Each emitted one live count query and 21 dword loads split into record index and byte offset.
  • Four small dispatches on a physical RTX 5070 checked the words on either side of an 80-byte record boundary, a nonzero view start, a matching raw subview, and both outcomes of a count guard. All four fences completed and the output canaries were preserved. Revision 89b6c67 reproduced the three hardware-tested DXIL containers byte for byte.
  • An earlier comparison of the same change on d2a4bae covered 25,694 inputs. Fifteen additional shaders passed SDK validation and all 21,971 previously valid shaders still passed. The emitted code changed for 18,136 of those previously valid shaders.

Gameplay impact remains unverified.

Use the original PSV binding and the exact native table slot to recover the record count and split scalar byte offsets within structured records. Preserve existing handling of ambiguous, mixed-consumer, and raw views.

Refs: Marvisak#136, Marvisak#329, Marvisak#437
Merge upstream main, retain both shader regression tests, and pass absent PSV metadata in the three synthetic fixtures that construct lowering inputs directly.

Refs: Marvisak#136, Marvisak#329, Marvisak#437
@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 78c8078: cargo test -p victory-shader passes, 125 passed / 0 failed. Workspace baseline on main (ffe310e) was green on the same machine (342 passed).

Tested with AI assistance (Claude).

MfDev0 pushed a commit to MfDev0/victory that referenced this pull request Nov 24, 2026
…ueries

smem() splits scalar loads of a structured table SRV into record and
offset (Marvisak#984 port), but descriptor_data() made the size query's handle
for the same slot a raw view: two kinds for one descriptor, and word 2
reported bytes where the record count was read. Use the structured
stride there too.

Idea from upstream PR Marvisak#995 (89b6c67, Jeremy Goldberg); its PSV
declaration check is not ported (our structured choice follows the
table binding as in Marvisak#984).

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>

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

Using the declared structured SRV for scalar loads is implemented as a proof, not a heuristic: scalar_structured_stride requires the PSV bindings, an exact single table slot with no other consumers, exactly one matching used-resource entry (kind SRV, 4 dwords, stride 4..=2048 multiple of 4), and singleton_structured_srv on the lower bound — any doubt falls back to the old raw path. The raw-buffer side correctly splits the byte address into record index + within-record offset so wide loads crossing record boundaries land right. record_count_word conservatively bails on mixed consumers and phi-selected descriptors. The +251 lines of native tests plus the emit-side proof test back the guards.

Verified locally: test-merge with main (ffe310e) clean; cargo test -p victory-shader --locked (macOS arm64, rustc 1.98.1) → 101 passed / 0 failed / 1 ignored. Scope note: host-execution coverage of the new structured path is Windows-gated; macOS covers compilation and all unit tests. Approving.

@jeremygold02

Copy link
Copy Markdown
Contributor Author

I compared the same two mission vertex shaders on Windows 11 with Windows SDK 10.0.26100.0. The draw-entry observations for the affected slot are in the earlier verification on #136.

Revision SDK-valid outputs Result for the affected 80-byte structured SRV
ffe310e (main) 0/2 Both stop at the scalar descriptor-data read.
c42cbb0 (#953) 2/2 Neither output queries dimensions on an 80-byte structured handle.
5011645 (#984) 0/2 Both stop at the same descriptor-data read as main.
78c8078 (this PR) 2/2 Each output queries the count once and splits all 21 dword loads from the same binding into record index and byte offset.

I also ran #984’s separate one-thread buffer-read regression on a physical RTX 5070. I restricted the test adapter loop to hardware and checked the device LUID’s adapter description before dispatch. Both scalar and flat vector reads returned the expected word at all nine offsets across three 56-byte records. That fixture covers a distinct case worth retaining alongside the count-consuming shaders here. The mission shaders were translated offline.

@jtrudeau1530

Copy link
Copy Markdown

Validated this exact change (78c8078) locally on RX 7900 XT / RADV under Wine 11.18 and vkd3d-proton 3.2.0. The test base also contains the mapped-copy fix and #1056; no title-derived fixtures were used.

A synthetic scalar-buffer fixture passed 12 hardware dispatches with exact eight-word readbacks: stride 80, x2/x4 loads across record boundaries at byte offsets 76 and 156, a count-derived last-record address, both count-guard outcomes, and raw-buffer controls with the same native stride. Canary words remain intact. The raw count remains refused rather than being inferred from stride. Microsoft DXIL validator release 1.7.2308 was required and passed.

The identical declaration/count fixture on the test-only parent reproduces the original scalar-memory-read refusal; the patched candidate passes. This establishes those scalar cases, not full game correctness or performance.

@Ctrl-Alt-Haze

Copy link
Copy Markdown

Independent verification at 78c8078 on Windows 11, RTX 5070 Ti (driver 32.0.16.2135) + WARP (VICTORY_D3D12_DEBUG=1), dxil.dll 1.8.2502.11, Rust 1.98.1 against upstream/main (ffe310eb):

  • Cleanly on ffe310eb; cargo xtask policy: 154 files ok; both clippy checks pass.
  • cargo test --workspace --locked: 347 passed, 0 failed, 6 ignored.
  • victory-shader --features host (VICTORY_D3D12_DEBUG=1, RTX 5070 Ti & VICTORY_WARP=1): 125 passed, 0 failed, 1 ignored (0 D3D12 debug errors/warnings).
  • victory-gpu --features diagnostics (VICTORY_D3D12_DEBUG=1, RTX 5070 Ti & VICTORY_D3D12_WARP=1): 53 passed, 0 failed, 1 ignored (0 D3D12 debug errors/warnings).
  • Zero unsafe added; PSV0 + unique static slot check + wide-load dword splitting avoids the raw bytes/4 NUM_RECORDS mismatch in #953 (note textual conflict with #953 in lower.rs — prefer #995).

AI-assisted verification on local RTX 5070 Ti + WARP; no game data.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: +15 programs over main, 0 lost.

Offline. I merged head 78c8078 onto main ffe310e (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: 21,724 on main, 21,739 with this PR.

Failure on main Gained
emit: a scalar memory read as data 15

All 15 are among the programs that #1319 gains. Another 114 programs move from "a scalar memory read as data" on to "a descriptor's address or format read as data". That is the reason #1597 targets, and I'm measuring this PR stacked on #1597.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 125, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@anlnimus

Copy link
Copy Markdown

Hardware note from testing on AMD Radeon RX 6900 XT (RDNA2), Adrenalin driver 32.0.21045.16010, Windows 11 Pro (10.0.26200):

While verifying structured SRVs on hardware in a local integration build (#986 + #995-style lowering), reading a structured heap SRV (StructureByteStride > 0) through a raw DXIL ByteAddressBuffer handle returned all zeros on RX 6900 XT (even where WARP / NVIDIA tolerate the view/resource kind mismatch). Lowering scalar buffer loads on structured table SRVs with a matching StructuredBuffer handle ((12, stride) + UDiv/URem element indexing) returned the exact expected bytes on RX 6900 XT.

This directly supports #995's motivation on AMD RDNA2 hardware. (No game assets or dumps attached; tested with AI assistance.)

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Structured addressing for scalar loads chosen from PSV0, and record counts: main settled this the other way (scalar loads read raw views by byte address, emit/mod.rs notes) and reads record counts through getDimensions (75677cb).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
sinan-cmyk added a commit to sinan-cmyk/VIctory that referenced this pull request Nov 25, 2026
… too

A structured view's dword 1 holds the stride in its high half, which
GetDimensions reads, and the address in its low half. descriptor_data
gives the word no value, so a direct read is refused either way; at a
join with data it is now the undefined path, as for a raw view. Without
this, a PR that turns raw views into structured ones (Marvisak#995) loses
programs that the raw-view rule covered.

On a retail dump: 1 more program validates, none lost.
@sinan-cmyk

Copy link
Copy Markdown

Stacked results

The merge with #1402 conflicts in 4 hunks (emit/lower.rs, emit/mod.rs). In two places the intents contradict each other:

  1. On the scalar-load path, an SRV raw view takes this PR's structured stride when it is known. Otherwise [BUNDLE] High-yield shader PRs (+295/+520/+153/+117 programs in #1247): #1067 #305 #306 #176, stacked on #1319 #1402's raw or UAV choice applies.
  2. In Shaders: handle s_buffer_load SMEM uses in descriptor_data (#798) #953's s_buffer_load arm, the view becomes structured if this PR's record-count and declaration conditions hold. Otherwise it stays Shaders: handle s_buffer_load SMEM uses in descriptor_data (#798) #953's raw view.

This PR's test scalar_table_record_count_requires_an_exact_unambiguous_declaration expects a refusal, but with #953 the merge falls back to a raw view. I had to relax that test to merge. On the merge, cargo test --locked -p victory-shader --lib: 142 passed, 0 failed.

Method: victory translate --validate --list on shaders.rpf (1.0.2.1), dxil.dll 1.8.2502.11, compared program by program (25,889 programs). AI-assisted (Claude Code). No shader names or data are included, per the asset policy.

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