shader: support image_sample_o and image_sample_lz_o texel offset opcodes #360
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/360"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_oandimage_sample_lz_owere unhandled, and gather operations with explicit level-of-detail (image_gather4_l,image_gather4_l_o) returned errors:Texel Offset Handling:
offset_wordextraction (w = addr_val(self, 0)?) previously only extracted signed 6-bit offsets for gather operations. Extended offset extraction across sampling operations whenoffset_wordis set.image_sample_oandimage_sample_lz_oalongside standardimage_sample/image_sample_lzopcodes.Gather LOD Variants:
image_gather4_landimage_gather4_l_oopcodes 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.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 DXILtextureGatherwithout 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
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
textureGatherdoes not take an explicit LOD argument, so mappingimage_gather4_lto it without preserving the requested mip level would risk sampling incorrect texels when guest shaders specify a nonzero level.Updated the branch (
67a2ee8):image_gather4_l/image_gather4_l_omapping so they preserve their diagnostic until full explicit-LOD lowering and readback tests are in place.image_sample_oandimage_sample_lz_otexel offset extraction.cargo xtask policyclean, and all 69victory-shadertests pass.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:
image_gather4_l/_l_ogo totextureGather, 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.)The offset is passed to
sample/sampleLevelas computed values (Shl/AShrof 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.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_lis 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.
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: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.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:57:50Z.
Review: COMMENTED
Tested
38ac9efon Windows 11, RTX 5070, MSVC:cargo test --workspace --no-fail-fastpasses (264 passed, 0 failed).victory translate <title exe> --validate --pipelinesgives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. None of the 12 embedded programs uses_osamples.The offset decoding is right. Bits 0–5, 8–13 and 16–21 of the offset VGPR are sign-extended with
(w << at) >> 26for at = 26, 18 and 10, and the sample paths already passostosample/sampleLevel/sampleBias/sampleGrad. Two things before I can approve:Sample*. The validator rejects texture-access offsets that aren't constant in -8..7 ("Offsets to texture access operations must be immediate values").Gatherallows programmable offsets, which is why the old code only did this for gathers. Hereos[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 realimage_sample_owould emit DXIL that fails validation. Could you add a test that lowers animage_sample_oand runs it throughemit::validator(asemit/mod.rsdoes), 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).image_gather4_landimage_gather4_l_oare now recognized, but the diff doesn't touch the gather match arm. Line 5436 still lists onlyimage_gather4,_lzand_lz_o. Either that part is missing from the commit, or the description should drop it.AI-assisted (Claude Code).
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 maind2a4bae(emitted / validated). Windows 11, dxil.dll from SDK 10.0.26100.Both clear the 4
image_sample_orefusals:cargo test -p victory-shaderpasses on both.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.
Pull request closed