Skip to content

rdna2spirv: texel offset fallback picks the level by the S# mip filter (SHADER-699) - #3882

Open
tupacfan04 wants to merge 2 commits into
ItsNotRexed:mainfrom
tupacfan04:shader-699-texoff-mip-filter
Open

tupacfan04 wants to merge 2 commits into
ItsNotRexed:mainfrom
tupacfan04:shader-699-texoff-mip-filter

Conversation

@tupacfan04

@tupacfan04 tupacfan04 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-699 (claim, from @whattheflop's finding)

What and why

--texel-offset-fallback folds an image_sample*_o offset into the coordinate as offset / size of the sampled level. Gen.texel_offset_fold always floored the LOD to pick that level. A nearest-mip sampler rounds instead, so at LOD 0.75 the sample reads mip 1 while the offset was scaled by mip 0's size. @whattheflop's example: 8×8 base, offset (−1, +1), UV (0.6875, 0.5625). The fold lands on mip-1 texel (2, 2), but the native offset selects (1, 3).

The fold now rounds the LOD the way the S#'s MIP_FILTER (word 2 [27:26]) picks a level, matching how the runtime builds the VkSampler (runtime/src/gfx10_descriptors.h, vulkan_sampler):

MIP_FILTER Runtime sampler Folded level
2 linear MIPMAP_MODE_LINEAR floor(lod), unchanged (the documented two-level approximation)
1 point, 3 MIPMAP_MODE_NEAREST nearest, a tie rounds up: floor(lod) + (lod − floor(lod) ≥ 0.5)
0 none nearest, LOD held in [0, 0.25] 0

A literal S# word 2 (from s_mov_b32 in the same block, via sgpr_imm, as gather_addr_modes already does) emits only the rounding it needs. A run-time S# extracts the field and selects. Either way the result is still clamped to the image's levels.

The nearest level isn't computed as floor(lod + 0.5), because the binary32 sum rounds: 0x3f000001 + 0.5 and 0x3effffff + 0.5 both round to exactly 1.0. floor(lod) and lod − floor(lod) are exact, so the fraction is compared with 0.5 directly. A tie rounds up, which is what RTX 5070 GPUs measured on this PR (@kacper-serewis on 616.56, @jeremygold02 on 621.01): native LOD 0.5 samples mip 1. tools/image_query_probe.py records the same for RDNA2. For OpImageQueryLod (implicit samples), an x that a driver already rounded to an integer stays the same.

Not changed:

docs/design.md (the Texel offsets row) is updated with the rule.

Tested

  • tests/test_texel_offsets.py: new test_fallback_level_follows_mip_filter. It evaluates the emitted level chain (the level operand of OpImageQuerySizeLod) for image_sample_l_o with a literal S# and with a run-time S#. Float results are rounded to binary32, as the module computes them. Cases: point at LOD 0.75 → 1 (the finding), 0.25 → 0, 0x3effffff → 0, 0.5 → 1, 0x3f000001 → 1, 0x3f000002 → 1, 2.6 → 3, 9.0 → 3 (clamped to 4 levels), −1 → 0; MIP_FILTER 3 at 0.75 → 1; linear at 0.75 → 0 and 1.99 → 1; none at 2.75 → 0. New test_fallback_literal_mip_filter_emits_one_rounding. Result: 44 passed, including the spirv-val cases (Vulkan SDK 1.4.363). Negative controls: unmodified main fails the new tests. The first head, d1dea0ad, fails exactly the 0.5 and 0x3f000001 cases, the two rows @jeremygold02 measured as wrong on the GPU. The evaluator now agrees with all five native rows of that table.
  • GPU, NVIDIA GeForce RTX 5070, driver 621.24, Windows 11 26300, LEONIDA_VK_VALIDATION=1: scripts/run_gpu_tests.py --filter texoff is 3/3 pass natively and 3/3 pass with LEONIDA_TEXOFF_FALLBACK=1 (ps_texoff, ps_texoff_forms, ps_texoff_implicit, 4096/4096 pixels each; +16/−32 judged under the fallback). These shaders sample mip 0 with a point sampler, so they show no regression. They do not exercise a fractional LOD on a mipped image; that case is covered by the CPU test above.
  • tests/test_translate.py and tests/semantic: 7362 passed, 566 skipped, 1 xfailed, 51751 subtests passed (python -I -m pytest, Python 3.13).

Depends on: none. #2783 changes the same texel_offset_fold signature and call site, so whichever lands second needs a small rebase; its MIN/MAX_LOD clamp runs before this rounding, so the two compose.

AI-assisted (Claude Code).

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

…r (SHADER-699)

--texel-offset-fallback floored the LOD to choose the level whose size scales
the folded offset. A nearest-mip sampler reads ceil(lod + 0.5) - 1, so at
LOD 0.75 it sampled mip 1 with the offset in mip-0 texels. Read MIP_FILTER
(S# word 2 [27:26], literal or at run time): nearest rounding for point and 3,
level 0 for none, floor kept for linear, as the runtime's vulkan_sampler maps
them.

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

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested d1dea0a on Windows, CPython 3.14.7, CPU only (no GPU, no game files; no spirv-as/spirv-val here).

python -m pytest -q tests/test_texel_offsets.py (venv with the pinned tests/requirements.txt): 32 passed, 9 skipped. With the merge-base translator (f847748), 5 of this head's tests fail, so they pin the change.

One boundary where the test's evaluator and the emitted arithmetic disagree, reproduced: _folded_level evaluates OpFAdd in Python doubles, but the module adds in binary32. For a point sampler with four levels and LOD bits 0x3f000001 (the first binary32 value above 0.5), the evaluator says mip 1; the same instructions evaluated with binary32 round-to-nearest-even give mip 0, because lod + 0.5 is exactly halfway between two binary32 values and rounds to 1.0, so Ceil - 1 is 0.

import inspect, pathlib, sys, tempfile, struct
sys.path[:0] = ["tests", "tools"]
import test_texel_offsets as t
t.rdna2spirv.TEXEL_OFFSET_FALLBACK = True
src = inspect.getsource(t._folded_level).replace('if op[0] in ("OpFAdd", "OpIAdd"):', '''if op[0] == "OpFAdd":
            return struct.unpack("<f", struct.pack("<f", ev(a[0]) + ev(a[1])))[0]
        if op[0] == "OpIAdd":''')
env = dict(vars(t))
exec(src, env)
with tempfile.TemporaryDirectory() as d:
    for sampler in (1 << 26, None):
        spv = t._sample_l_o(pathlib.Path(d), sampler)
        lod = struct.unpack("<f", struct.pack("<I", 0x3f000001))[0]
        print(t._folded_level(spv, lod, 1 << 26), env["_folded_level"](spv, lod, 1 << 26))

Run from the repository root. Output: 1 0 twice (literal and run-time sampler). At exactly 0.5 both give 0, and at 0x3f000002 both give 1.

Two possible follow-ups: round the float operations to binary32 in _folded_level, so the test checks what the module computes; and, if mip 1 is the intended answer just above 0.5, compare the clamped fractional LOD with 0.5 directly instead of adding 0.5. I have no GPU measurement of what the hardware selects at that value.

AI-assisted.

@kacper-serewis kacper-serewis 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.

On RTX 5070 driver 616.56 at d1dea0a, the documented nearest-mip tie at explicit LOD 0.5 affected all 4,096 pixels. Native Vulkan sampled mip-1 texel (41,38) and the fallback sampled (42,37). The five other nearest-mip controls matched. Across six linear-mip LODs the fallback matched its documented approximation within 0.002 in 8-bit channel units. The 24 synthetic draws used a scratch harness honoring the runtime mip-filter setting.

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

The first binary32 LOD above 0.5 also gives a wrong texel on the RTX 5070. At 0x3f000001 (0.5000000596046448), native Vulkan Lod|Offset read (1,3) from mip 1. The translated fallback read (2,2) from mip 1 in all 256 pixels for that input, with either a literal or a memory-loaded sampler descriptor. An independent offset-free sample confirmed the selected mip. This answers @shdann’s hardware question and adds a non-tie case to the exact tie already reported.

Tested head d1dea0ad08072a18e338bd00cd79f5c9dba666c6 and merge base f847748cc76bd3777f9c070c4034eb2fc1c04e64. GPU execution used NVIDIA GeForce RTX 5070, driver 621.01 (32.0.16.2101), on Intel Core Ultra 7 265K / Windows 11 Home 25H2 build 26200.9974.

Explicit LOD bits Sampled mip Native offset (x,y) Head fallback (x,y), both S# forms Base fallback (x,y)
0x3effffff (just below 0.5) 0 (4,5) (4,5) (4,5)
0x3f000000 (0.5) 1 (1,3) (2,2) (2,2)
0x3f000001 1 (1,3) (2,2) (2,2)
0x3f000002 1 (1,3) (1,3) (2,2)
0x3f400000 (0.75) 1 (1,3) (1,3) (2,2)

To reproduce, use the two-mip path in vk_texture_test with an 8×8 base and a 4×4 mip, storing RGBA8 bytes (x, y, level+1, 255) at every texel. Use point/nearest S# words 00000092 00100000 04000000 00000000 (LOD range 0..1). A hand-written image_sample_l_o at UV (0.6875, 0.5625) with packed offset 0x013f (−1,+1) supplies RGB. Export the blue channel of an offset-free image_sample_l at the same UV and LOD alongside it. Run with the native offset (maintenance8 enabled) and with --texel-offset-fallback. Four 64×64 draws covered 16 explicit LOD values, 256 pixels each. The local harness changes were confined to the two synthetic mip sizes and texel contents. All tested modules passed spirv-val.

Please add 0x3f000001 to the regression coverage and make the CPU evaluator round the add to binary32. At that input, lod + 0.5 rounds to exactly 1.0 and Ceil - 1 picks the base-level size for the offset. Comparing the fractional LOD with 0.5 directly would preserve this first-above-tie distinction while keeping the exact-tie policy explicit. These results cover explicit LOD and a nearest-mip sampler on this driver.

AI-assisted.

ceil(lod + 0.5) - 1 rounds the sum in binary32: 0x3f000001 + 0.5 is 1.0, so
it picked mip 0 where the RTX 5070 samples mip 1, and the exact tie 0.5 went
down where the GPU rounds it up (review on ItsNotRexed#3882). floor(lod) and
lod - floor(lod) are exact, so compare the fraction with 0.5. The test's
evaluator now rounds float results to binary32 and covers 0x3effffff, 0.5,
0x3f000001 and 0x3f000002.

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

Copy link
Copy Markdown
Contributor Author

Thanks @shdann, @kacper-serewis, @jeremygold02. All three findings hold, and d08064b9 addresses them:

  • The nearest level is now floor(lod) + (lod − floor(lod) ≥ 0.5) instead of ceil(lod + 0.5) − 1. Both operations are exact in binary32, so 0x3f000001 selects mip 1, and so does the tie at 0.5, matching native on your RTX 5070s. 0x3effffff stays at mip 0. (floor(lod + 0.5) would get that one wrong: the sum rounds up to 1.0.)
  • _folded_level rounds every float result to binary32. The cases now include 0x3effffff, 0.5, 0x3f000001 and 0x3f000002.
  • Control: the first head d1dea0ad fails exactly the 0.5 and 0x3f000001 cases. The evaluator now agrees with all five native rows of @jeremygold02's table.
  • The design doc and docstring state the round-up tie rule and cite your measurements.

Local results: tests/test_texel_offsets.py 44 passed (with spirv-val); tests/test_translate.py 562 passed. RTX 5070 (621.24), validation on: texoff GPU tests 3/3 natively and 3/3 with LEONIDA_TEXOFF_FALLBACK=1. I didn't rebuild your two-mip harness, so the mip-1 cases rest on your GPU table plus the binary32 evaluation. A rerun of that harness on d08064b9 would close it out. AI-assisted (Claude Code).

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

Verified the fix on d08064b97902fc2514659e5965c3be6c318669ce. Both the literal and memory-loaded S# fallback now match the native Vulkan texel-offset frame byte-for-byte, 4,096/4,096 RGBA32F pixels each, across 16 explicit LOD values. This resolves my previous change request.

LOD bits Native and both revised fallbacks (x, y, mip)
0x3effffff (just below 0.5) (4, 5, 0)
0x3f000000 (exactly 0.5) (1, 3, 1)
0x3f000001 (first above) (1, 3, 1)
0x3f000002 (second above) (1, 3, 1)
0x3f400000 (0.75) (1, 3, 1)

I rebuilt the shaders and scratch texture harness from this head and reran the same synthetic two-mip fixture. Four 64×64 draws compared native maintenance8 offset, the two head fallback forms and merge-base fallback. Relative to d1dea0ad, exactly the 256 pixels at the tie and the 256 at the first-above value changed in each fallback. The native and merge-base frames reproduced their previous bytes, and the independent offset-free sample confirmed each mip. All tested modules passed Vulkan 1.2 SPIR-V validation.

GPU execution used NVIDIA GeForce RTX 5070, driver 621.01 (32.0.16.2101), with Intel Core Ultra 7 265K on Windows 11 Home 25H2 build 26200.9974. This retest covers explicit LOD with point/nearest filtering on that driver.

AI-assisted by GPT-6.2 Astra.

@itsonlysimbo

Copy link
Copy Markdown
Contributor

Tested d08064b9 merged onto main aa2988cc (merges cleanly) on RTX 4070, NVIDIA 621.14, Windows 11 (10.0.26300), Vulkan SDK 1.4.363, llvm-mc 23.1.6, test programs from main aa2988cc: pass.

  • tests/test_texel_offsets.py with spirv-as/spirv-val on PATH: 44 passed, none skipped.
  • run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed, both without and with LEONIDA_TEXOFF_FALLBACK=1, same as main. ps_texoff, ps_texoff_forms and ps_texoff_implicit are 4096/4096 exact in both modes (with the fallback, the +16/−32 offsets are judged). These fixtures sample mip 0 only, so they check that nothing regressed, not the new level choice; that is covered by the RTX 5070 readbacks above.
  • No suite shader's SPIR-V changes against main.
  • tests/semantic: 7456 tests, with the same 7 failures and 2 errors main aa2988cc has on this machine; none new. pytest outside tests/semantic: the same 4 failures as main, none new.

AI-assisted (Claude Code).

@tupacfan04

Copy link
Copy Markdown
Contributor Author

A limit of this PR's level rule, found through #4321. @whattheflop measured native image_sample_l on an RDNA2 Radeon iGPU (readback): at LOD 0x3effffff it reads mip 1, but RTX 5070 and RTX 4070 Ti SUPER read mip 0. The AMD sampler quantizes the LOD before rounding, so values within about one binary32 ulp below 0.5 land on the tie.

Here the host sampler picks the mip and the fallback only has to scale the offset by that mip's size, so the target is the host's rule. The exact floor + (fraction >= 0.5) matches NVIDIA. On an AMD host it would scale by mip 0 where the sample reads mip 1, but only for LODs just below a .5 boundary (0x3efe0000 already reads mip 0 there). Main's ceil(lod + 0.5) - 1 was wrong at the tie itself and at 0x3f000001 on both vendors, so this PR is still a strict improvement. I've asked for the precise RDNA2 quantization in #4321 and will follow up here if a host-specific rule turns out to be worth it.

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.

5 participants