shader: support image_sample_o and image_sample_lz_o texel offset opcodes #360

Closed
github-bot wants to merge 2 commits from gh-pr/360/head into gh-pr/360/base
Owner

@⁠bitzin-dev on GitHub · 2026-11-24 02:34 UTC · original
bitzin-dev:shader/image-sample-offset-and-gather-l → main · +12 −12 · 1 files · 2 commits · closed without merging

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.
**@⁠bitzin-dev** on GitHub · 2026-11-24 02:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/360) `bitzin-dev:shader/image-sample-offset-and-gather-l` → `main` · +12 −12 · 1 files · 2 commits · closed without merging ### 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.
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:00:48 +00:00
github-bot closed this pull request 2026-11-26 04:00:48 +00:00
Author
Owner

@⁠ifBars on GitHub · 2026-11-24 02:37 UTC · original

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

**@⁠ifBars** on GitHub · 2026-11-24 02:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7120331063) 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)
Author
Owner

@⁠bitzin-dev on GitHub · 2026-11-24 02:40 UTC · original

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.

**@⁠bitzin-dev** on GitHub · 2026-11-24 02:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7120368438) 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.
Author
Owner

@⁠r4tedx on GitHub · 2026-11-24 03:14 UTC · original

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.

**@⁠r4tedx** on GitHub · 2026-11-24 03:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7120908205) 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.
Author
Owner

@⁠bitzin-dev on GitHub · 2026-11-24 04:13 UTC · original

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.

**@⁠bitzin-dev** on GitHub · 2026-11-24 04:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7121827677) 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.
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 07:47 UTC · original

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.

**@⁠slint6614-dev** on GitHub · 2026-11-24 07:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7125373701) 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.
Author
Owner

@⁠bitzin-dev on GitHub · 2026-11-24 15:28 UTC · original

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.

**@⁠bitzin-dev** on GitHub · 2026-11-24 15:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/360#issuecomment-7136098302) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!360
No description provided.