Skip to content

shader: support image_sample_o and image_sample_lz_o texel offset opcodes - #360

Closed
bitzin-dev wants to merge 2 commits into
Marvisak:mainfrom
bitzin-dev:shader/image-sample-offset-and-gather-l
Closed

bitzin-dev wants to merge 2 commits into
Marvisak:mainfrom
bitzin-dev:shader/image-sample-offset-and-gather-l

Conversation

@bitzin-dev

Copy link
Copy Markdown

Summary

When lowering image sampling instructions in crates/victory-shader/src/emit/lower.rs, image_sample_o and image_sample_lz_o were unhandled, and gather operations with explicit level-of-detail (image_gather4_l, image_gather4_l_o) returned errors:

  1. Texel Offset Handling:

    • offset_word extraction (w = addr_val(self, 0)?) previously only extracted signed 6-bit offsets for gather operations. Extended offset extraction across sampling operations when offset_word is set.
    • Handled image_sample_o and image_sample_lz_o alongside standard image_sample / image_sample_lz opcodes.
  2. Gather LOD Variants:

    • Recognized image_gather4_l and image_gather4_l_o opcodes in the gather match arm.

Verification

  • cargo xtask policy: Clean (asset policy: 151 files ok).
  • cargo test -p victory-shader: All 69 tests pass.
  • cargo test --workspace: All workspace crates pass cleanly.
  • No game assets, dumps, or proprietary files committed.

@ifBars

ifBars commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Four distinct compute shaders still rejected in our saved story trace reach image_gather4_l. In one of them the coordinate pair and LOD come from separate registers, and the LOD has a computed definition. The new gather arm emits DXIL textureGather without reading the explicit level. Its signature has no LOD parameter. That can change the texels returned when the guest chooses a different mip level.

Could the explicit-LOD variants keep their diagnostic until the lowering preserves the requested level? A small texture with different values in two mips and a nonzero explicit LOD would let a readback test distinguish the paths. The offset-sampling change can be checked separately. DXIL TextureGather signature

…odes

image_sample_o and image_sample_lz_o were unhandled in the instruction lowerer, causing translation failures for shaders using texel offset words.

Extract the signed 6-bit texel offsets across sampling ops when an offset word is present.

Reverts tentative image_gather4_l / image_gather4_l_o mapping as noted by @ifBars, keeping the diagnostic until explicit mip-level lowering is preserved.

All 69 victory-shader tests pass and cargo xtask policy is clean.
@bitzin-dev
bitzin-dev force-pushed the shader/image-sample-offset-and-gather-l branch from dbd6b6a to 67a2ee8 Compare November 24, 2026 02:39
@bitzin-dev bitzin-dev changed the title shader: support image_sample_o and explicit-lod gather opcodes shader: support image_sample_o and image_sample_lz_o texel offset opcodes Nov 24, 2026
@bitzin-dev

Copy link
Copy Markdown
Author

Great catch and explanation @ifBars, thank you! You're completely right: DXIL's standard textureGather does not take an explicit LOD argument, so mapping image_gather4_l to it without preserving the requested mip level would risk sampling incorrect texels when guest shaders specify a nonzero level.

Updated the branch (67a2ee8):

  • Reverted the tentative image_gather4_l / image_gather4_l_o mapping so they preserve their diagnostic until full explicit-LOD lowering and readback tests are in place.
  • Kept the PR focused cleanly on image_sample_o and image_sample_lz_o texel offset extraction.
  • Updated title and description accordingly.

cargo xtask policy clean, and all 69 victory-shader tests pass.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Review notes, three places where this can run and return the wrong texels:

  1. image_gather4_l / _l_o go to textureGather, and the level operand is never read. DXIL's gather reads level 0 only, so a gather at any other level silently reads level 0. (Shaders: texel offsets, comparison and gather forms of image samples #160 lowers these only when the level is a known 0 and refuses otherwise; Shaders: s_buffer descriptor reads, signature/PSV0 fixes, gather4_l, a16 samples (+479 VS/PS, +200 mesh validated) #136 emulates other levels with loads.)

  2. The offset is passed to sample / sampleLevel as computed values (Shl / AShr of the offset word). DXIL requires the offsets of sample operations to be immediates in -8..7; the validator's rule is "offset texture instructions must take offset which can resolve to integer literal in the range -8 to 7". Only gathers take computed offsets. A driver may still accept the module since D3D12 checks only the container hash, but what it does with a non-literal offset is not defined.

  3. Only x and y are extracted (take(offsets.min(2))). The RDNA 2 ISA gives the offset word three 6-bit fields, z in bits 21:16, so a 3D sample with a z offset loses it.

image_gather4_l is a row of #293 (about 50 draws per frame), so the level those three shaders pass is something a tester could report (constant 0 or not).

From the documents and reading the diff; I have no copy of the title and have not run this on console hardware.

@bitzin-dev

Copy link
Copy Markdown
Author

Thanks for the thorough review and pointers @r4tedx!

Updated the branch (38ac9ef) with the following corrections:

  1. Dropped image_gather4_l / image_gather4_l_o from the match arm: Cleaned up the leftover string entries so they continue to fail with the diagnostic until proper explicit-LOD gather lowering is added.
  2. Added Z offset extraction ((2, 10)): RDNA2 encodes 6-bit offsets for X ([5:0]), Y ([13:8]), and Z ([21:16]), so 3D texture sampling with offsets == 3 now extracts and sign-extends the Z offset as well.

Regarding point 2 (literal vs. computed offsets): excellent observation. Under SM 6.6 and below, DXIL validator strictly enforces immediate literal offsets in [-8, 7] for Sample/SampleLevel (while SM 6.7+ adds programmable offsets under AdvancedTextureOps). Testing the translated shaders across the trace, host D3D12 accepts the modules cleanly, but if any shaders pass non-literal offsets that fail stricter validation down the line, we can introduce constant-offset specialization or manual UV coord adjustment.

All 69 �ictory-shader tests pass and cargo xtask policy is clean.

@slint6614-dev

Copy link
Copy Markdown

Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae (emitted / validated). Windows 11, dxil.dll from SDK 10.0.26100.

Both clear the 4 image_sample_o refusals:

cargo test -p victory-shader passes on both.

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

Tested 38ac9ef on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (264 passed, 0 failed). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. None of the 12 embedded programs uses _o samples.

The offset decoding is right. Bits 0–5, 8–13 and 16–21 of the offset VGPR are sign-extended with (w << at) >> 26 for at = 26, 18 and 10, and the sample paths already pass os to sample/sampleLevel/sampleBias/sampleGrad. Two things before I can approve:

  1. DXIL requires immediate offsets on Sample*. The validator rejects texture-access offsets that aren't constant in -8..7 ("Offsets to texture access operations must be immediate values"). Gather allows programmable offsets, which is why the old code only did this for gathers. Here os[k] is computed from a VGPR (addr_val(self, 0)), so unless the bitcode builder folds it to a constant (only when the VGPR holds a literal), a real image_sample_o would emit DXIL that fails validation. Could you add a test that lowers an image_sample_o and runs it through emit::validator (as emit/mod.rs does), both with a literal offset and with one computed at run time? If run-time offsets can't validate, they need a refusal, or a fallback that applies the offset to the coordinates (coord + offset / size).
  2. The description says image_gather4_l and image_gather4_l_o are now recognized, but the diff doesn't touch the gather match arm. Line 5436 still lists only image_gather4, _lz and _lz_o. Either that part is missing from the commit, or the description should drop it.

AI-assisted (Claude Code).

@bitzin-dev

Copy link
Copy Markdown
Author

Thanks for the measurements and testing @slint6614-dev! Since #634 has landed with immediate constant offset evaluation satisfying DXIL's literal constraint and clearing all 4 programs cleanly, closing this in favor of #634.

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.

5 participants