Skip to content

Shaders: read heap-loaded s_buffer_load raw; SampleLevel outside pixel shaders - #1409

Closed
icdmkg2 wants to merge 2 commits into
Marvisak:mainfrom
icdmkg2:fix/shader-heap-buffer-loads-and-compute-sample
Closed

icdmkg2 wants to merge 2 commits into
Marvisak:mainfrom
icdmkg2:fix/shader-heap-buffer-loads-and-compute-sample

Conversation

@icdmkg2

@icdmkg2 icdmkg2 commented Nov 24, 2026

Copy link
Copy Markdown

Two shader translation fixes found while chasing #1348, and a clearer device-removal message. These don't fix the world-load removal in #1348.

1. s_buffer_load on a descriptor read from ResourceDescriptorHeap

Before, smem() lowered every s_buffer_load whose descriptor has no SRV used-resource entry as a constant-buffer read. For a descriptor the shader loads from the resource heap at a computed index, that put a CBV handle on a slot the host fills with a raw buffer view. GBV flagged the mismatch, WARP read nulls, and it can hang a GPU. The hair-strand compute shaders (smh_modify:CS_*Strand*) hit this.

Selection rule (smem() in crates/victory-shader/src/emit/lower.rs). The load is read raw, the same way as an SRV used-resource, when both of these hold:

  1. No used-resource entry covers the descriptor (self.used_kind(src) is None).
  2. The descriptor came from the resource heap. It must be a Source::Loaded whose scalar load traces, through self.scalar[load].param, to a root parameter with root == RootType::ResourceHeap. In practice that is s_load_dwordx4 s[n:n+3], s[heap_base], soffset, with the heap index (base + offset + soffset) >> 5 from heap_index().

The rule looks only at where the descriptor was loaded from. It doesn't look at how the shader uses it: read-only and written heap descriptors are treated the same. It relies on the host making the heap's buffer views raw (descriptor/host.rs). A descriptor that a used-resource entry names as a CBV still takes the CBV path.

The read-only-descriptor case in #1354 at cd9699f, which returned 73 through a CBV and 41 through an explicit raw table SRV, should be a direct control for this rule.

2. image_sample outside pixel shaders

image_sample in a compute, vertex or mesh shader emitted Sample, which DXIL rejects outside pixel shaders ("features incompatible with the shader stage"). CS_DensityShadowBlur_H failed validation this way. It now emits SampleLevel at LOD 0. image_sample_o and image_sample_b outside pixel shaders now fail as untranslatable instead of emitting invalid DXIL.

3. Diagnostics

  • The device-removed failure now includes the code from GetDeviceRemovedReason.
  • shader-info --verbose prints each container's runtime hash: BLAKE3 of the container bytes, sized by the DXBC header at 0x18. Hashes in the logs can then be matched to containers.

Testing

  • cargo clippy is clean, and cargo test -p victory-shader passes (120 passed, 1 ignored), rebased on ffe310e.
  • On WARP with GBV, the hair-strand shaders' CBV/SRV mismatch errors are gone, and CS_DensityShadowBlur_H now passes validation.
  • The world-load removal still reproduces with this branch. See World load: device hung, DRED hides it #1348: the latest DRED runs on an RX 6750 XT show page faults from two different shaders, one of them inside a freed 1024x1024 texture.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GHAMAwekwWQWP6FsLe2hsw

- smem: an s_buffer_load from a descriptor read from the resource heap
  at a computed index (no used-resource entry) is a buffer view the host
  makes raw, so read it raw. It was lowered as a constant-buffer read,
  i.e. a CBV handle on an SRV slot (GBV mismatch, WARP null reads, can
  hang a GPU). Hit by the hair-strand compute shaders.
- image_sample outside pixel shaders: emit SampleLevel at LOD 0 instead
  of Sample, which DXIL rejects there (CS_DensityShadowBlur_H failed
  validation). Biased or offset samples outside pixel shaders are now
  reported as untranslatable.
- The device-removed failure names GetDeviceRemovedReason's code.
- shader-info --verbose prints each container's runtime hash.

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

Copy link
Copy Markdown

We tested the scalar-buffer selection rule from this PR together with #1354 at cd9699f on an RTX 4090 under Wine/GE. A source-built compute shader used a runtime index to read 41 from raw SRV slot 2 while slot 1 held 99. A named table CBV returned 73. Both wrote the result through a slot inferred to be a raw UAV. All 15 lowering tests passed.

The combined lowering keeps #1354’s inferred UAV class on the raw path and adds this PR’s resource-heap origin check for an unnamed read-only descriptor. A separate fixture exercised creation of all three descriptor classes through HostHeap::make_at. The numerical fixture used matching host bindings. A directly indexed heap CBV without used-resource metadata remains ambiguous. These checks cover shader translation and descriptor creation; we have no title run measuring the effect on #1348.

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Independent verification of \cfc0edf\ on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200) / Rust 1.98.1 MSVC:

  • Merge check vs \main\ (\ fe310e): clean
  • \cargo test -p victory-shader --lib\ (real RX 9060 XT GPU execution): 120 passed, 0 failed, 1 ignored
  • \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and --all-features): clean
  • \cargo xtask policy: 154 files ok

Code audit: routing \RootType::ResourceHeap\ \s_buffer_load\ descriptors through raw SRV (11, 0)\ and lowering non-pixel \image_sample\ to \sampleLevel\ (level 0) looks clean and directly addresses the #1348 hang pattern; adding targeted unit tests for both paths would make regressions easy to catch.

AI-assisted; synthetic tests only, no game files.

- scalar_loads_from_a_resource_heap_descriptor_are_raw: an s_buffer_load
  on a descriptor s_load'ed from the ResourceHeap root parameter, with no
  used-resource entry, is read raw; the same load from a table stays a
  constant-buffer read. Counted by a test-only `heap_raw_loads`.
- compute_image_sample_reads_level_zero: the indexed waterfall program
  with image_sample in place of image_sample_lz validates in compute and
  samples each lane's texture (hardware and WARP).

Both fail with the fixes reverted (the second with the validator's
"features incompatible with the shader stage (cs)").

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

icdmkg2 commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks both. @geiyiliyabumo, I've added the targeted tests in de109f1:

  • scalar_loads_from_a_resource_heap_descriptor_are_raw (in lower.rs tests): an s_buffer_load on a descriptor s_loaded from a ResourceHeap root parameter, with no used-resource entry, is read raw. As a control, the same load from a Table stays a constant-buffer read. It is counted by a test-only heap_raw_loads.
  • compute_image_sample_reads_level_zero (in waterfall_tests.rs): the indexed waterfall program, with image_sample in place of image_sample_lz. It validates in compute and samples each lane's texture on hardware and WARP.

Both fail with the fixes reverted. The second fails with the validator's Function uses features incompatible with the shader stage (cs). With the fixes: 122 passed, clippy -D warnings clean, xtask policy ok.

@MichaelSmithAI, agreed on the open case. A heap CBV that is directly indexed, with no used-resource metadata, is read raw under this rule. If the title wrote a CBV into that slot, the host has built a CBV descriptor there, so the two don't match. The rule only looks at where the descriptor came from. One way to settle it would be for the host to record the view kind it built for each heap slot, so the translator, or a debug check, could compare against it. For now I'd treat that as a known limit rather than widen this PR.

Separately, merged with #568, this gets an RX 6750 XT to about 6 minutes in the world (frame 6350). Details are in #508.

@sinan-cmyk

Copy link
Copy Markdown

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

Result: +24 programs over main, 0 lost.

Offline. I merged head de109f1 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,748 with this PR.

Failure on main Gained
validator: Function: main: error: Entry function performs some operation that is incompatible with the shader stage or other entry properties. See other errors for details. 24

All 24 are among the programs that #1402 gains. They failed in the validator on main ("Entry function performs some operation that is incompatible with the shader stage").

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 122, 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).

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

In-world evidence for part 1, from a fork that runs main into the world: fxceless/victory-gta6, the comment at its s_buffer_load heap path in lower.rs.

  • It is what blocks meshlet culling. The title's culling programs (BatchCullMeshlets, BatchCullPreSkinnedMeshlets, BatchCullBatchCSMeshlets) read their batch records, mesh headers and instance transforms through heap-picked buffers. Read as constant buffers, the loads gave nothing (validation: "Descriptor Type in Heap: SRV, Register Type: CBV"), so culling passed no meshlet and every indirect mesh draw ran empty. This PR's hair-strand case is the same pattern.
  • One design difference. The fork decides per heap slot at run time. The host keeps a table of buffer-view strides by slot, and only a slot that holds a buffer view (stride ≠ 0) is read raw; anything else is still read as a constant buffer, with a guard around each load. The rule here is static: a heap-loaded descriptor with no used-resource entry is always read raw. They differ only if a heap index the program computes can land on a CBV slot. If that can happen in the title, the fork's check covers it and this rule would read a CBV slot as a raw buffer.
  • Part 2 (image_sample outside pixel shaders, as SampleLevel at 0) is the same shortcut the fork took. It overlaps with Shaders: implicit-level samples in compute take their gradients from the quad #1237 (gradients from quad lanes) and Shaders: texel offsets, comparison and gather forms of image samples #160 (keeps sample and relies on driver compute derivatives), so those three need one choice.

Reading only, nothing run. AI-assisted (Claude Code).

@sinan-cmyk

Copy link
Copy Markdown

Stacked on #1402: no change, and one behaviour contradicts #1015

Result: 0 gained, 0 lost, and no failure reason changes on top of #1402. On main this PR gained 24 programs, and all of them are among #1402's gains.

Merging it needs two decisions. There are 5 conflict hunks in lower.rs, shader.rs and waterfall_tests.rs:

With those choices, cargo test --locked -p victory-shader --lib: 139 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), against #1402 at 2310409. AI-assisted (Claude Code). No shader names or data are included, per the asset policy.

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

Its main fix (s_buffer_load through a heap-loaded descriptor read raw) is in main (lower.rs smem heap_constants); the rest turns non-pixel image_sample into SampleLevel 0, against main's deliberate implicit-LOD samples in compute, with no evidence.

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.

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.

6 participants