Skip to content

Translator: clamp run-time gather LOD with FMax/FMin - #1045

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
bobcatchris:fix/gather-lod-clamp-1032
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
bobcatchris:fix/gather-lod-clamp-1032

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

What

Fixes #1032.

On an AMD RX 7900 XTX (LLPC 26.10.1, Windows 32.0.32015.7029), ps_gather_sampler_lod's minLOD pass returned the mip 0 red quadrant at (0,0) instead of mip 1 cyan. #973 clamps the explicit-gather LOD with GLSL FClamp(lod, MIN_LOD, MAX_LOD), decoding the bounds from S# word 1 at run time. LLPC miscompiles that run-time FClamp on this path: replacing it with FMax then FMin makes the same test pass unchanged.

This keeps FClamp for a literal S# (the case #973's existing test pins, and where the bounds are constants), and uses the two-instruction clamp only for a run-time S#:

lower   = OpExtInst %float %glsl FMax lod minLod
clamped = OpExtInst %float %glsl FMin lower maxLod

For min <= max this is exactly the specified FClamp result.

Testing

  • Before: run_gpu_tests.py --filter ps_gather_sampler_lod on the RX 7900 XTX → FAIL, (1,0,0,1) != (0,1,1,1), every run.
  • After: --filter gather → 2/2 passed (ps_gather_sampler_lod, ps_gather_wrap), same GPU/driver.
  • pytest tests/test_translate.py → 281 passed, including the new test_gather4_l_run_time_lod_bounds_clamp_without_fclamp, which requires FMax/FMin and no FClamp on a run-time S#; Translator: honor sampler LOD bounds in explicit gathers #973's literal-bounds test is unchanged and still passes.
  • No game files; synthetic fixture only.

Reported in #1032 (claimed in #1006: #1006 (comment)).

AI-assisted.

LLPC 26.10.1 miscompiles the run-time FClamp on the explicit-gather path and
leaves the fetch at mip 0 when MIN_LOD should raise it (ItsNotRexed#1032). Keep FClamp
when the S# bounds are literals, and use FMax then FMin for a run-time S#,
which is the same clamp for min <= max. Add a translator test that pins the
run-time path away from FClamp.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@evilclanker9000

Copy link
Copy Markdown
Contributor

Verified a4728898adcae7d48adb23c60f22497c5d9e056e with a single pixel shader containing both a descriptor-fed gather and a literal-S# gather, on RTX 5080 and Radeon Graphics (RDNA2). I used the source probe in #1032 twice, changing only the requested LOD from 0 to 1. All four combinations of requested LOD and sampler bounds matched all 4,096 pixels on each GPU.

Two independent controls in the validated SPIR-V separate the bounds. With requested LOD 0 and min=0.75, bypassing only FMax changed the descriptor-fed red tap from 0 to 1 at all 4,096 pixels. With requested LOD 1 and max=0.25, bypassing only FMin changed that tap from 1 to 0 at all 4,096 pixels. The two raw descriptor fields and literal gather tap stayed bit-identical in both controls. This checks both sides of the workaround on the other adapters; the affected RDNA3 result is the author's. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested a472889 on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51, llvm-mc 23.1.6): pass

@evilclanker9000

Copy link
Copy Markdown
Contributor

The runtime-bound workaround also preserved both D16 gathers when combined with #1017 at 773891bd (this PR at a4728898). A separate two-mip draw checked the packed result of both image_gather4_l and image_gather4_l_o with the same runtime S#. On RTX 5080 and Radeon RDNA2, removing either active bound selected the wrong packed words at all 4,096 pixels. The exact input conditions, expected words and integration tree are in the combined result on #1017. AI-assisted.

@quackottu quackottu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested a472889 on NVIDIA GeForce RTX 5070 (subgroup 32), driver 621.24, Windows 11 Pro (26300), Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 (built with scripts/build_llvm_mc.ps1), scripts/run_gpu_tests.py:

  • native: 78/105 passed, 27 skipped (main 8df277f here: 64/91 passed, 27 skipped, all wave64 tests needing a 64-wide subgroup)
  • LEONIDA_W64_EMULATE=1: 105/105 passed (main here: 91/91)
  • python -m unittest discover -s tests/semantic: Ran 1062 tests in 168.837s OK

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested a472889 (on main) on AMD RDNA2 iGPU and RTX 5070: pass, native and emulated.

Windows 11 Home 10.0.26300, llvm-mc 21.1.8 (AMDGPU, native), Vulkan SDK 1.4.363.0, MSVC 14.44.35207, Python 3.14.7, VK_LOADER_LAYERS_DISABLE=~implicit~. One PC, two GPUs, picked with VK_LOADER_DRIVERS_SELECT: AMD Radeon iGPU of a Ryzen 7 9800X3D (RDNA2, AMD 2026.04.17, subgroup 32..64) and NVIDIA GeForce RTX 5070 (Blackwell, 617.42, subgroup 32).

scripts/run_gpu_tests.py: 107 shaders translated, 107 pass spirv-val.

AMD iGPU RTX 5070
native 105/105 passed 78/105 passed, 27 skipped (wave64)
LEONIDA_W64_EMULATE=1 105/105 passed 105/105 passed
  • tests/test_translate.py (pytest 9.2.0): 281 passed.
  • ctest with LEONIDA_VK_VALIDATION=1: 47/47 on both GPUs.

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

@ItsNotRexed
ItsNotRexed merged commit be43d27 into ItsNotRexed:main Nov 22, 2026
9 of 10 checks passed
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
…ounding

#1045's test found the clamp as RoundEven's operand; #1074 now rounds the gather LOD as Floor(lod + 0.5), so the
test follows Floor -> FAdd(+0.5) -> FMin. It still checks a run-time S# clamps with FMax/FMin and no FClamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

ps_gather_sampler_lod: minLOD pass returns mip 0 on RX 7900 XTX (AMD 26.10.1)

6 participants