Skip to content

Translator: honor sampler LOD bounds in explicit gathers - #973

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:codex/gather-sampler-lod
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:codex/gather-sampler-lod

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in #634.

What and why

The explicit-LOD gather emulates four taps with OpImageFetch, bypassing the bound Vulkan sampler. On main it selected mip 1 even when the sampler restricted the draw to mip 0, and vice versa. This applies S# word 1's MIN_LOD [11:0] and MAX_LOD [23:12] (unsigned 4.8) before selecting the mip. A literal word is decoded at translation time; an SGPR loaded from memory is decoded in the shader.

ps_gather_sampler_lod loads the S# from the word range following its T# in the test heap. The 64×64 draw requests LOD U across the viewport. The base texture has four colored quadrants, and mip 1 is solid cyan. The fixture uses image_gather4_l for RGB and its zero-offset _l_o variant for A. Both project runners execute it. This branch merges cleanly in git merge-tree with #928's D16 writeback and #946's A16 offset sampling.

Tested

Windows RTX 5080 (616.92) and Radeon Graphics (26.10.1), synthetic inputs only. Raw RGBA readbacks compared with the CPU quadrant/cyan reference:

Sampler LOD bounds Mismatched pixels on main per GPU Mismatched pixels on this branch per GPU
[0, 0.25] 2048/4096, right half 0/4096
[0.75, 1] 2048/4096, left half 0/4096
[0, 1] 0/4096 0/4096

I also compared literal S# bounds on Radeon against native OpImageGather Lod using VK_AMD_texture_gather_bias_lod on the same RGBA8 view, sampler and coordinates. The driver reports supportsTextureGatherLODBiasAMD=1 for that format. Four controls forced the opposite mip with max 0/0.25 or min 1/0.75. Main differed at every pixel for all four. This branch matched all four 4096-pixel readbacks. With bounds [0,1], both paths agree at LOD 0.5 ± 1/256. At exactly 0.5, the native path picks mip 1 and the existing RoundEven picks mip 0. That rounding difference remains separate from the sampler-bound fix.

scripts/run_gpu_tests.py --filter ps_gather_sampler_lod passed on both GPUs. Using the expanded fixture set, 97/97 synthetic shaders translated and passed spirv-val both before and after. The full RTX runner passed 75 with 27 skips; the separate shader checker passed 65 with 28 skips. The translator/runner tests passed 327 cases, the semantic suite passed 1020, and CTest passed all 33 enabled cases (GameInput disabled in this build).

Checklist

  • No game data, keys or decryption.
  • The contract table in docs/design.md records the fields, GPU evidence and observed tie behavior.

AI-assisted with Codex.

Explicit-LOD gathers use four texel fetches that bypass the Vulkan sampler. Decode its 4.8 LOD bounds before selecting the mip so clamped gathers agree with a sampler-backed draw.
@kapitche

Copy link
Copy Markdown
Contributor

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

  • pytest tests/test_translate.py tests/test_shader_checks.py: 307 passed, 42 subtests. With main's tools/rdna2spirv.py, 1 fails.
  • run_gpu_tests.py --filter gather:
RTX 4080 SUPER (32.0.16.1714) Intel UHD 770 (32.0.101.6129)
ps_gather_sampler_lod (new) PASS: two passes, 4096 RGBA32F pixels each exact; wrong mip and UV rejected PASS, same
ps_gather_wrap PASS 4096/4096 PASS 4096/4096

For other testers: ps_gather_sampler_lod needs this PR's vk_texture_test. Run against an older runtime build, it fails on both GPUs ((0,0): RGBA32F bits (1065353216, 0, 0, 1065353216) != (0, …)). So rebuild runtime/ before running it.

@ItsNotRexed
ItsNotRexed merged commit 07402f3 into ItsNotRexed:main Nov 22, 2026
11 checks passed
ItsNotRexed pushed a commit that referenced this pull request Nov 22, 2026
#973 clamps the explicit-gather LOD to the S# bounds and records that, at exactly 0.5 with bounds [0,1],
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.
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