Skip to content

Translator: gather LOD ties select the upper mip (SHADER-152) - #1074

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
465d6a0eac:shader152-gather-lod-tie
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
465d6a0eac:shader152-gather-lod-tie

Conversation

@465d6a0eac

@465d6a0eac 465d6a0eac commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-152 (claim). Depends on: #973 (merged).

What and why

image_gather4_l(_o) emulates the explicit-LOD gather with four OpImageFetch, and chose the mip with RoundEven(LOD). #973's contract row records the difference found while checking the sampler LOD bounds: on Radeon Graphics 26.10.1, with an RGBA8 two-level view, nearest mip filtering and bounds [0,1] at UV (0.25,0.25), native OpImageGather Lod (VK_AMD_texture_gather_bias_lod) picked mip 1 at exactly LOD 0.5, while RoundEven picked mip 0 (0.5 ± 1/256 agreed: mip 0 and 1).

This changes the mip to Floor(clamp(LOD, S# MIN_LOD, MAX_LOD) + 0.5): exact ties go up like the native draw; every non-tie value selects the same mip as before, and the existing [0, levels−1] clamp is unchanged. Only image_gather4_l/_l_o are touched.

New tests in tests/test_translate.py pin the emitted sequence (clamped LOD → FAdd of %fhalf_g → Floor, no RoundEven on the LOD), and a table covers negative and near-tie LODs plus the level-range clamp, with the old behaviour as the negative control (0.5 → 0, 2.5 → 2). docs/design.md now states the rule in both gather rows; the "Other additions" row keeps #1017's d16 packing sentence.

Tested

CPU-only: WSL, Python 3.14, LLVM 21 llvm-mc, SPIRV-Tools v2026.1. No GPU run — the machine here has no Vulkan runtime build (RTX 3070 Ti only); the 0.5 rule is @Vonsek's Radeon Graphics 26.10.1 measurement above, and the guest's own gfx1013 tie behaviour is still not hardware-verified.

  • Shard 0/8 of the corpus (3,313 shaders), before and after this change (same base, 43d4703): 3,313/3,313 translated and 3,313/3,313 pass spirv-val in both.
  • Corpus slice of the 16 shaders that use image_gather4_l/_l_o (with their FSSM sidecars), before and after: 16/16 translated and 16/16 valid in both; OpImageFetch count unchanged (946). Normalising the temp ids, the only text differences are the 232 gather sites: one RoundEven out, FAdd+Floor in, plus the NoContraction decoration on the new FAdd.
  • First 200 corpus shaders, before and after: 200/200 translated and 200/200 valid in both.
  • python -m pytest tests at the rebased tip: 3,645 passed, 63 skipped, 22,574 subtests passed (the usual tool-dependent skips).

Checklist

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

AI-assisted: the change, tests and this description were written with an AI assistant. #1017 merged after this was opened and edits the same function, so the branch is rebased onto 48ad23f (and its docs conflict resolved by keeping both rows' sentences).

native OpImageGather Lod (VK_AMD_texture_gather_bias_lod) selected mip 1 on Radeon Graphics 26.10.1 while
the emulation's RoundEven selected mip 0. image_gather4_l(_o) now picks the mip as Floor(clamp(LOD) + 0.5):
ties go up like the native draw, and every non-tie LOD selects the same mip as before. The [0, levels-1]
clamp is unchanged.

Tests: 232 gather sites in the 16 corpus shaders that use image_gather4_l/_o now emit FAdd+Floor instead of
RoundEven and still assemble and pass spirv-val (16/16 translated, 16/16 valid, 946 fetches unchanged); a
200-shader corpus slice is 200/200 translated and valid before and after; the full suite passes (3498 passed,
61 skipped). The 0.5 rule is the Radeon measurement; the guest's own gfx1013 tie behaviour is still not
hardware-verified.
@465d6a0eac
465d6a0eac force-pushed the shader152-gather-lod-tie branch from 4fa8336 to c22798e Compare November 22, 2026 06:45

@whattheflop whattheflop 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.

AI-assisted Windows Vulkan verification of c22798eed5a4f0f6e2a8242aec363a1271712613 against parent 48ad23f0, using the synthetic two-mip RGBA8 fixture with isolated ICDs.

Radeon Graphics iGPU 25.10.30.02 and RTX 3080 616.64 both passed the stock minLOD/maxLOD checks (4/4 draws total). For LODs 0.5 - 1/256, 0x3effffff, 0.5, 0x3f000001, and 0.5 + 1/256, parent mips were 0,0,0,1,1 and this head chose 0,1,1,1,1 on both GPUs (4,096 pixels per draw). The exact tie moves up as intended. The adjacent lower float32 also moves up because FAdd rounds to 1.0 before Floor, so the description's claim that every non-tie is unchanged needs narrowing. All five Radeon draws matched the saved native AMD OpImageGather Lod output pixel for pixel. The stock sampler-bound checks passed here, and #1032 concerns a separate RX 7900 XTX result.

7/7 synthetic modules passed spirv-val. Khronos validation 1.4.363 loaded for all 14 draws with zero errors. Windows 11 build 26200, uv-managed Python 3.14.5, LLVM 21.1.8, SPIRV-Tools 2026.4 RC2.

@465d6a0eac

Copy link
Copy Markdown
Contributor Author

Note on CI: the check job failure is not from this diff. It is main's direct entry point in tests/test_cfg_structure.py: after 40f01a9 (test_random_graphs ends on its assertions and returns nothing), the __main__ block still does (lambda s, r, l, ok, cap: ...)(*test_random_graphs()) at line 179, so python tests/test_cfg_structure.py raises TypeError: argument after * must be an iterable, not NoneType. Already claimed by @whattheflop in #1006 as the TEST-138 follow-up. The translator, semantic, viewer and runtime jobs pass on this branch.

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
# Conflicts:
#	docs/design.md
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>
@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested c22798e (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: 112 shaders translated, 112 pass spirv-val.

AMD iGPU RTX 5070
native 110/110 passed 83/110 passed, 27 skipped (wave64)
LEONIDA_W64_EMULATE=1 110/110 passed 110/110 passed
  • tests/test_translate.py (pytest 9.2.0): 312 passed. Negative control: with main's tools/rdna2spirv.py put back and this PR's tests kept, 2 failed, 310 passed.
  • ctest with LEONIDA_VK_VALIDATION=1: 50/50 on both GPUs.

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

@ItsNotRexed
ItsNotRexed merged commit dfad4c3 into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
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.

4 participants