Skip to content

Translator: gather wrap/mirror use OpSRem + fix-up, not OpSMod (AMD Windows driver reads zeros) - #2105

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
whoalin1:gather-addr-srem
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
whoalin1:gather-addr-srem

Conversation

@whoalin1

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new item, #1902 (comment) (from my finding just above it).

What and why

image_gather4_l(_o) maps each texel coordinate through the S#'s address mode in Gen.gather_addr (SHADER-36, #341). Wrap took OpSMod c n, and mirror OpSMod c 2n.

On a Ryzen 5 7600's iGPU (RDNA2) with AMD's Windows driver 24.10.24, ps_gather_wrap failed: the interior case and cases 3, 4 and 5 were exact, while cases 1, 2 and 6 came back all zero. Those are exactly the three cases where a coordinate going into OpSMod is negative:

case modes (x, y) the negative OpSMod input
1 wrap, mirror wrap of x = −1
2 mirror, wrap wrap of y = −12
6 wrap, mirror mirror of y = −12 (x = 13 is positive)

Every passing case has only non-negative OpSMod inputs: case 5 mirrors x = 20, and cases 3 and 4 don't use it. The same module passes on an RTX 4070 SUPER. So the driver gave OpSMod the dividend's sign, a negative coordinate reached OpImageFetch, and the fetch read zeros.

The fix: gather_addr computes c mod m as OpSRem c m, plus m when that remainder is negative. That's the same value as OpSMod for the positive periods used here (n, 2n), and no OpSMod is left in the translator. The project already works around this driver family the same way (FClamp on the run-time LOD path, #1032).

  • tools/rdna2spirv.py: Gen.gather_addr's modulo, with the reason in its docstring.
  • tests/test_translate.py: the gather tests count OpSRem and the fix-up OpSLessThan instead of OpSMod (literal modes per axis; all four modes at run time).
  • docs/design.md: the gather row says why the modulo isn't OpSMod.

Tested

AI-assisted (Claude Code). Windows 11 (build 26200), Python 3.14.4, llvm-mc 21.1.8 (WSL), SPIRV-Tools v2026.4, test programs built with MSVC 14.44. No game files.

This test can only run on that AMD iGPU through #2048, because it has 128 bytes of push constants. So the GPU runs used #2048's branch with this change applied, and --filter gather:

OpSMod (before) OpSRem + fix-up (this PR)
AMD Radeon Graphics (Ryzen 5 7600 iGPU, RDNA2), driver 24.10.24, LEONIDA_PC_TAIL_UBO=1 ps_gather_wrap FAIL: out.g and half of out.a are 0 (cases 1, 2, 6) ps_gather_wrap 4096/4096 pixels exact; ps_gather_sampler_lod pass
NVIDIA RTX 4070 SUPER, driver 610.88, with and without LEONIDA_PC_TAIL_UBO=1 pass pass, both
  • python -m pytest tests/test_translate.py tests/test_mesh_compute.py tests/test_shader_index.py: 530 passed, 2 skipped (the 48 gather tests among them).
  • python -m unittest discover -s tests/semantic: 1671 OK.
  • python -m compileall -q tools tests: clean.

Not checked: whether newer AMD Windows drivers still do this. The design notes record ps_gather_wrap passing on a Radeon 880M, which is RDNA3.5 on a newer driver.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

@AvantBras

Copy link
Copy Markdown
Contributor

Tested da78e803040cc1bbdbc530ebbda4be981b2b5b33 on RTX 3060 12 GB, NVIDIA 580.178.04, Pop!_OS 22.04, using the Vulkan harnesses built at #2048’s head with the UBO tail off.

python -I scripts/run_gpu_tests.py --build <build> --filter gather: both fixtures passed. ps_gather_wrap matched 4,096/4,096 pixels; ps_gather_sampler_lod matched 4,096 RGBA32F pixels in each of two passes and rejected the wrong-mip and wrong-UV controls. All three modules passed spirv-val. This is a Linux NVIDIA regression check, not a reproduction of the AMD Windows bug.

No game files. AI-assisted verification.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested da78e80 on Windows 11, RTX 4060 Ti (Ada, subgroup 32), NVIDIA 621.24, llvm-mc 21.1.8: pass. tests/test_translate.py 498 passed, 2 skipped. run_gpu_tests.py --filter gather: ps_gather_wrap 4096/4096 exact, ps_gather_sampler_lod exact. I'm closing my duplicate #2115 in favour of this one.

AI-assisted (Claude Code).

@whattheflop

Copy link
Copy Markdown
Contributor

Independent synthetic verification (AI-assisted with Codex) of head da78e803040cc1bbdbc530ebbda4be981b2b5b33 against base 4eb6155d3ac4f9d3fadb0d095c4ce13e76783615.

Windows 11, Ryzen 7 9800X3D integrated AMD Radeon Graphics (PCI device 0x13c0). Vulkan reports the AMD proprietary shader compiler as 25.10.30.02 and maxPushConstantsSize as 256 bytes. I built the stock vk_texture_test target at each revision with MSVC 19.38.33145 and Windows SDK 10.0.26100.0, then ran its explicit-file mode with one AMD ICD, an empty implicit layer path and Khronos validation 1.4.363.

Synthetic draw Base Head
ps_gather_wrap 4,096/4,096 exact 4,096/4,096 exact
ps_gather_sampler_lod, MAX_LOD 0.25 (mip 0 quadrants) 4,096/4,096 exact 4,096/4,096 exact
ps_gather_sampler_lod, MIN_LOD 0.75 (mip 1 cyan) 4,096/4,096 exact 4,096/4,096 exact

All three base/head readback pairs were byte-identical, with zero validation diagnostics across the six draws. I assembled vs_ids and both fragments once with LLVM 21.1.8 in WSL and fed the same inputs to each translator. Both revisions translated and passed spirv-val --target-env vulkan1.2 for 3/3 modules (SPIRV-Tools 2026.4). In the tested binaries, spirv-dis showed 76 and 32 OpSMod instructions on base for wrap and sampler LOD respectively. Head replaced all of them with OpSRem and the negative-remainder fix-up. Focused positive gather tests passed 43/43 per revision on uv CPython 3.12.13.

The base result confirms the earlier stock wrap pass on this adapter in #2025 and adds an exact base/head comparison with both LOD bounds. Driver 25.10.30.02 handled the stock OpSMod path in these fixtures. This pair used the stock 144-byte push-constant range. The author's failure was on the Ryzen 5 7600 iGPU with driver 24.10.24 and the #2048 UBO-tail stack for its 128-byte limit.

@Yourrespawn

Copy link
Copy Markdown
Contributor

Tested da78e803 on NVIDIA GeForce RTX 4080 (Ada, subgroup 32..32), driver 617.14, Vulkan API 1.4.351, Windows 11 Pro 10.0.26200: pass — cross-vendor confirmation of the rewrite.

  • Semantic suite: 1671 OK (Python 3.12.10)
  • run_gpu_tests.py native: ps_gather_wrap 4096/4096 pixels exact and ps_gather_sampler_lod two passes, 4096 RGBA32F pixels each exact — the gather fixtures stay exact on NVIDIA with OpSRem + fix-up where main's OpSMod also passed, so the rewrite costs nothing on this vendor while fixing the AMD driver's negative-dividend behaviour; suite 136/180 passed, 44 skipped (native wave64, expected on this 32-lane GPU)

AI-assisted (ZCode agent): builds and runs executed by a coding agent on this machine.

@gkissel

gkissel commented Nov 22, 2026

Copy link
Copy Markdown

Tested da78e803 (merged onto main 4eb6155, a fast-forward) on an NVIDIA GeForce GTX 1650 Mobile (Turing TU117, 4 GiB), NVIDIA 620.70.06, Vulkan 1.4.357, CachyOS Linux: pass. This is a non-regression check on NVIDIA, which already passed with OpSMod; it can't reproduce the AMD Windows driver bug.

Full suite, LEONIDA_W64_EMULATE=1 LEONIDA_VK_DEVICE=1650 python -I scripts/run_gpu_tests.py --build runtime/build/linux (test programs built from 4eb6155; the PR changes no C++):

main 4eb6155 this PR
translated / pass spirv-val 185 of 185 185 of 185
run_gpu_tests.py 167/180 passed, 13 skipped, 0 failed 167/180 passed, 13 skipped, 0 failed
ps_gather_wrap PASS, 4096/4096 exact PASS, 4096/4096 exact
ps_gather_sampler_lod PASS PASS
modulo ops in the translated ps_gather_wrap (spirv-dis) 76 OpSMod 76 OpSRem, no OpSMod
  • No test changed status between the two runs (per-test diff).
  • pytest tests/test_translate.py (pytest 9.2.0): 498 passed, 2 skipped, the same as on main.

AI-assisted (Claude Code). No game files used.

@smowlyy

smowlyy commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested da78e80 (applied on #2048's head dfc3c78) on Windows 11 Home 26300: pass. AI-assisted (Claude Code).

Hardware: AMD Ryzen 5 7600X; iGPU AMD Radeon(TM) Graphics (RDNA2, subgroup 32..64), AMD driver 24.10.24 (Vulkan driver 2.0.302); NVIDIA GeForce RTX 5060 Ti, driver 610.88. MSVC 14.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 (scripts\build_llvm_mc.ps1), Python 3.12.10. Implicit layers off. No game files.

run_gpu_tests.py --filter gather, LEONIDA_PC_TAIL_UBO=1:

#2048 only (OpSMod) #2048 + #2105 (OpSRem + fix-up)
Radeon iGPU, 24.10.24 ps_gather_wrap FAIL (4096 pixels differ); ps_gather_sampler_lod pass ps_gather_wrap 4096/4096 exact; ps_gather_sampler_lod pass
RTX 5060 Ti, 610.88 2/2 pass 2/2 pass

This reproduces the failure on a second machine with the same driver and confirms the fix.

  • Whole suite on the Radeon with both PRs: 164/180 passed, 15 skipped, 1 failed: ms_clip, which also fails on main e7584c5 on this driver (SHADER-167). Not related to this PR.
  • python -m unittest discover -s tests/semantic: OK. compileall: clean. Not run: tests/test_translate.py (needs pytest).

@AvantBras

Copy link
Copy Markdown
Contributor

Tested the rebased head c7e2fa477cdce75472399cd720f6e723960d8755 (the thread's earlier results are at da78e803) on RTX 3060 12 GB, NVIDIA 580.178.04, Pop!_OS 22.04, with the Vulkan harnesses built at this branch's merge base main 49475796.

python -I scripts/run_gpu_tests.py --build <build> --filter gather: 4 shaders translated, 4 passed spirv-val, 3/3 fixtures passed, no skips.

  • ps_gather_wrap — 4096/4096 pixels exact
  • ps_gather_sampler_lod — two passes, 4096 RGBA32F pixels each exact, wrong mip and wrong UV rejected
  • ps_gather_border — 4096/4096 pixels exact

This is NVIDIA only: the OpSRem-plus-fix-up rewrite targets the AMD Windows driver, and I have no AMD device. It confirms the rebase onto current main did not disturb the wrap/mirror results.

No game files. AI-assisted verification.

…up, not OpSMod (AMD Windows driver)

On a Ryzen 5 7600's iGPU (RDNA2) with AMD's Windows driver 24.10.24, OpSMod of a negative coordinate came
out with the dividend's sign, so ps_gather_wrap's three cases with a negative wrap or mirror coordinate
fetched outside the image and read zeros. Gen.gather_addr now computes c mod n as OpSRem plus n when the
remainder is negative, the same value for a positive period; the same module is then exact there, and still
on an RTX 4070 SUPER. tests/test_translate.py counts the new instructions; docs/design.md records why.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested 35781d4 on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 175/189 passed, 14 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base ec8ec4c on the same machine, every test has the same result.
  • python -m unittest discover -s tests/semantic: 4137 OK
  • tests/test_translate.py: 541 passed, 4 skipped in 5.56s

No game files. AI-assisted.

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.

9 participants