Skip to content

Translator: gather wrap/mirror modulo without OpSMod, independent of the driver's sign (SHADER-369) - #2144

Closed
Vonsek wants to merge 1 commit into
ItsNotRexed:mainfrom
Vonsek:vonsek4/gather-wrap-nonneg
Closed

Vonsek wants to merge 1 commit into
ItsNotRexed:mainfrom
Vonsek:vonsek4/gather-wrap-nonneg

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

SHADER-369 (Vonsek4)

Gen.gather_addr (the image_gather4_l emulation) wrapped texel coordinates with OpSMod c n and mirrored them with OpSMod c 2n. By the SPIR-V spec, OpSMod's result has the divisor's sign, which is positive here. @whoalin1 found that AMD's Windows driver 24.10.24 on a Ryzen 5 7600 iGPU returns all-zero gathers in exactly the three ps_gather_wrap cases whose wrap or mirror coordinate is negative (cases 1, 2, 6), while NVIDIA passes (finding). That fits a driver that computes OpSMod with the dividend's sign, which sends the fetch out of range.

The fix is a new Gen.nonneg_mod(c, m) that computes OpSRem c m and adds m when the remainder is negative. OpSRem is defined with the dividend's sign, so it doesn't depend on which sign convention a driver uses for OpSMod. Both wrap and mirror use it, and the gather emulation no longer emits OpSMod anywhere. The docs/design.md row now records the rule.

Tests

  • New: test_gather_addr_does_not_depend_on_how_a_driver_signs_smod. It drives Gen.gather_addr with a stub and evaluates the emitted integer code on the CPU, with OpSMod read both ways: the divisor's sign (spec) and the dividend's sign. The result must be Vulkan's wrap, mirror, clamp or mirror-once texel for every coordinate c in [-3n-2, 3n+2] and n ∈ {1, 2, 3, 8, 16}. It covers all four literal modes plus a run-time mode, including modes 4 and 7, which act as clamp.
    • Without the change: 3 of 5 fail (wrap, mirror and run time, e.g. -1 == 1). Clamp and mirror-once use no modulo, so they pass.
    • With the change: 5 of 5 pass.
  • Updated counts: test_gather4_l_literal_sampler_emits_only_the_code_its_modes_need and test_gather4_l_run_time_sampler_selects_the_mode_from_the_sgpr now count OpSRem with its OpSLessThan fix-up, and require zero OpSMod. Without the change they fail on the OpSMod count.
  • Suites: tests/test_translate.py gives 503 passed, 2 skipped. python -m unittest discover -s tests/semantic gives 1671 OK. compileall is clean.
  • GPU: scripts/run_gpu_tests.py --filter ps_gather gives ps_gather_wrap at 4096/4096 pixels exact and ps_gather_sampler_lod passing, on both machines below. The module now has 0 OpSMod and 76 OpSRem (before: 76 and 0).
    • AMD Radeon Graphics, the RDNA2 iGPU of a Ryzen 9 9950X3D, driver 32.0.21045.16010, run with LEONIDA_VK_DEVICE=radeon.
    • NVIDIA RTX 5080, driver 32.0.16.1692.
    • Windows 11 on both.
  • Not reproduced here: this machine's newer AMD driver passed before the change as well, so it can't show the zeros. A check on driver 24.10.24 (or any driver that showed the zeros) would confirm the fix there.

No game files used or included.

AI-assisted.

@itsonlysimbo

Copy link
Copy Markdown
Contributor

Tested 0b37b890 on RTX 4070 (subgroup 32), NVIDIA 621.14, Windows 11 (10.0.26300), Vulkan SDK 1.4.363, llvm-mc 23.1.6: pass. Test programs built from main 4eb6155d, since the PR has no runtime/ change.

  • ps_gather_wrap 4096/4096 pixels exact; ps_gather_sampler_lod passes. These are the only two shaders in the suite whose SPIR-V changes with this PR.
  • run_gpu_tests.py: 136/180 passed, 44 skipped, 0 failed, same as main.
  • tests/semantic: 1671 OK, same as main.

AI-assisted (Claude Code).

@ddm-j

ddm-j commented Nov 22, 2026

Copy link
Copy Markdown

Tested 0b37b89 on AMD Radeon 610M (RDNA2 iGPU, RADV Mesa 26.2.6) and NVIDIA RTX 4070 Ti (615.78.08), Fedora 45: pass

These add the two Linux drivers to the Windows results above. Neither driver showed the zeros on main either, so this is not a reproduction.

  • ps_gather_wrap and ps_gather_sampler_lod pass on RADV, on NVIDIA, and on NVIDIA with LEONIDA_W64_EMULATE=1.
  • run_gpu_tests.py gives the same as main 4eb6155: RADV 167/180, 13 skipped; NVIDIA 136/180, 44 skipped; emulated 167/180, 13 skipped.
  • tests/test_translate.py: 503 passed, 2 skipped (main: 498 passed, 2 skipped).
  • With main's tools/rdna2spirv.py put back, 16 fail: 3 of the new test (wrap, mirror, run time), plus 12 + 1 cases of the two updated count tests.
  • tests/semantic: 1671 OK.

AI-assisted (Claude Code); numbers copied from the run logs.

The image_gather4_l emulation wrapped and mirrored texel coordinates with
OpSMod. By the spec its result has the divisor's (positive) sign, but AMD's
Windows driver 24.10.24 on a Ryzen 5 7600 iGPU returned all-zero gathers in
exactly the ps_gather_wrap cases whose coordinate is negative, as if it took
the dividend's sign. Use OpSRem plus the divisor when the remainder is
negative (Gen.nonneg_mod): the same value under either reading.
@Vonsek
Vonsek force-pushed the vonsek4/gather-wrap-nonneg branch from 0b37b89 to 987bf44 Compare November 22, 2026 19:40
@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main after #2025 (gather border modes) landed (Vonsek4). Two conflicts:

The stub test now evaluates the border modes too (6 as clamp, 7 as mirror once) and reads the coordinate from the (coordinate, outside) pair.

Re-run after the rebase:

  • tests/test_translate.py: 546 passed, 4 skipped.
  • tests/semantic: OK.
  • run_gpu_tests.py --filter gather: ps_gather_border, ps_gather_wrap and ps_gather_sampler_lod pass on RTX 5080 and on Radeon Graphics (gfx1036, LEONIDA_VK_DEVICE=radeon).

AI-assisted.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded (Vonsek4): #2105 by @whoalin1, the finder, landed the same fix on main (c7e2fa47, gather_addr takes the modulo with OpSRem and a fix-up). Nothing here is left that main lacks. Thanks to everyone who tested it.

AI-assisted.

@Vonsek Vonsek closed this Nov 22, 2026
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.

3 participants