shader: support image_sample_o and image_sample_lz_o texel offset opcodes #360

Closed
bitzin-dev wants to merge 2 commits from upstream-pr/360 into main
bitzin-dev commented 2026-11-24 02:34:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bitzin-dev on 2026-11-24T02:34:47Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6573328432 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360). Originally posted by [bitzin-dev](https://github.com/bitzin-dev) on 2026-11-24T02:34:47Z. --- ### 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 commented 2026-11-24 06:55:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ifBars on 2026-11-24T02:37:47Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7120331063 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7120331063). Originally posted by [ifBars](https://github.com/ifBars) on 2026-11-24T02:37:47Z. --- 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](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#texturegather)
bitzin-dev commented 2026-11-24 06:55:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bitzin-dev on 2026-11-24T02:40:11Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7120368438 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7120368438). Originally posted by [bitzin-dev](https://github.com/bitzin-dev) on 2026-11-24T02:40:11Z. --- 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 commented 2026-11-24 06:55:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-24T03:14:45Z.


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. (#160 lowers these only when the level is a known 0 and refuses otherwise; #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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7120908205 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7120908205). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-24T03:14:45Z. --- 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. (#160 lowers these only when the level is a known 0 and refuses otherwise; #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 commented 2026-11-24 06:55:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bitzin-dev on 2026-11-24T04:13:51Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121827677 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7121827677). Originally posted by [bitzin-dev](https://github.com/bitzin-dev) on 2026-11-24T04:13:51Z. --- 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.
mercuryVM commented 2026-11-24 08:02:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:57:50Z.

Review: COMMENTED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6035565896 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360#pullrequestreview-6035565896). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T07:57:50Z. **Review:** COMMENTED --- 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).
slint6614-dev commented 2026-11-24 08:02:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T07:47:25Z.


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:

  • #634: the 4 programs then validate (VS/PS 12276 / 12176).
  • #360: they emit but all 4 fail the validator with "offset texture instructions must take offset which can resolve to integer literal" (VS/PS 12276 / 12172), so no validated gain.

cargo test -p victory-shader passes on both.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7125373701 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7125373701). Originally posted by [slint6614-dev](https://github.com/slint6614-dev) on 2026-11-24T07:47:25Z. --- 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: - #634: the 4 programs then validate (VS/PS 12276 / 12176). - #360: they emit but all 4 fail the validator with "offset texture instructions must take offset which can resolve to integer literal" (VS/PS 12276 / 12172), so no validated gain. `cargo test -p victory-shader` passes on both.
upstream-sync closed this pull request 2026-11-24 16:03:50 +00:00
bitzin-dev commented 2026-11-24 16:05:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by bitzin-dev on 2026-11-24T15:28:53Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136098302 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7136098302). Originally posted by [bitzin-dev](https://github.com/bitzin-dev) on 2026-11-24T15:28:53Z. --- 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!360
No description provided.