Skip to content

rdna2spirv: --texel-offset-fallback folds the offset at the _cl clamp's level (SHADER-842) - #4722

Open
wilu222 wants to merge 1 commit into
ItsNotRexed:mainfrom
wilu222:fix/texoff-fallback-minlod
Open

wilu222 wants to merge 1 commit into
ItsNotRexed:mainfrom
wilu222:fix/texoff-fallback-minlod

Conversation

@wilu222

@wilu222 wilu222 commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: SHADER-842 (claim). This PR also includes the GPU test claimed as a follow-up here.

Depends on: #2783. It doesn't build on it, but both edit the level computation in texel_offset_fold, so whichever lands second needs a small rebase. #2783 (SHADER-491) clamps to the S#'s MIN_LOD/MAX_LOD, which is about the sampler. This PR is about the instruction's own _cl clamp.

Rebased onto main at 8575d36ed, which includes #3882 (SHADER-699, the level rounded by the S# MIP_FILTER). The clamp is applied before that rounding, so a clamped level is rounded the same way as an unclamped one. Outside the PS, main now passes a _b_cl / _c_b_cl sample's level in as max(bias, clamp), so the fold skips the clamp there and only adds it where it's missing.

What and why

With --texel-offset-fallback (GPUs without VK_KHR_maintenance8, for example NVIDIA Pascal), an image_sample*_o has its offset folded into the coordinate as offset ÷ the size of the sampled level. texel_offset_fold never received a _cl form's min-LOD clamp:

  • In the pixel stage, a clamp above the computed LOD makes the sample read a coarser level, but the fold divided by the size of the unclamped level. image_sample_cl_o, _b_cl_o, _c_cl_o and _c_b_cl_o shifted by texels of a finer level, which is twice the offset per level of difference.
  • Outside the pixel stage, the fold used level 0 while the sample reads Lod max(0, clamp).

The fold now raises its level to max(LOD, clamp) (outside the PS, max(0, clamp)) before the MIP_FILTER rounding and the clamp to the image's levels. It uses the same SPIR-V value the sample takes as MinLod or in its Lod. OpImageQueryLod leaves the MinLod operand out, so the fold has to add it.

The existing GPU fixture couldn't show this: vk_texture_test --two-mip has a solid-cyan mip 1, so a sample clamped to mip 1 reads the same colour wherever the offset lands. This PR therefore also adds a GPU test that can:

  • vk_texture_test --two-mip-index: the same two-mip texture, but mip 1 texel (x, y) = (4x, 4y, 0x80, 0xFF), so every mip 1 texel is different.
  • runtime/test/shaders/ps_texoff_cl.asm: image_sample_cl_o with offset (+3, −2) and coordinates ÷ 4 (unclamped LOD −1). The clamp is 0 on the left half (mip 0) and 1 on the right half (mip 1). The header derives every expected pixel by hand and lists what a dropped offset, an ignored clamp and the old fold would give.
  • check.py (texoff_cl_pixel, texoff_cl_problem, texoff_cl_test, in PIXEL_TESTS) and scripts/run_gpu_tests.py judge it. It skips (exit 77) without maintenance8 unless the fallback is on, and without shaderResourceMinLod.
  • tests/test_shader_checks.py (TexoffClJudge) tests the judge: the rule passes, and the pre-fix fold, a dropped offset, an ignored clamp and a short dump all fail.

Changes in tools/rdna2spirv.py: one new parameter and one FMax. docs/design.md is updated in the "Min-LOD clamp forms" and "Texel offsets" rows, and docs/testing.md lists ps_texoff_cl with the other offset tests.

Found by reading the code. No open PR or coordination comment mentioned it.

Tested

All results below are on the rebased commit unless noted. On an NVIDIA GeForce RTX 5070, driver 617.42 (32.0.16.1742), Vulkan, Windows 10 22H2:

ps_texoff_cl (run_gpu_tests.py --filter ps_texoff_cl) Result
Native Offset (maintenance8), this PR PASS, 4096/4096 pixels exact
LEONIDA_TEXOFF_FALLBACK=1, this PR PASS, 4096/4096 pixels exact
LEONIDA_TEXOFF_FALLBACK=1, translator from main FAIL: 2048 of 4096 pixels differ, the whole right half. At (32, 0) it got R = 36/255 (mip 1 texel 9) where 44/255 (texel 11) is expected, which is the old fold's shift the .asm header predicts
  • tests/test_texel_offsets.py: three new tests trace the fold's level back from OpImageQuerySizeLod (SClamp ← the MIP_FILTER rounding ← Floor ← FMax) and check that the FMax takes the sample's own clamp value, for image_sample_cl_o, image_sample_c_b_cl_o and a vertex-stage image_sample_cl_o. All three fail with main's translator and pass with the fix. rdna2spirv: texel offset fallback picks the level by the S# mip filter (SHADER-699) #3882's MIP_FILTER tests pass unchanged, and the whole file plus tests/test_texoff_probe.py passes, including spirv-val (SPIRV-Tools v2026.4) of every fallback form.
  • tests/test_shader_checks.py (all TexoffClJudge tests pass; the 15 Wave64Checks failures happen on main too on this machine), tests/test_run_gpu_tests.py, tests/semantic test_sample_cl and test_sample_c (18 passed), test_design_contract_rows and the CSV tests pass. Three docs and line-ending tests fail, identically on main, in files this PR doesn't touch: test_docs_links (an anchor in docs/dev-tools.md), test_docs_cli_flags (docs/dev-tools.md) and test_text_write_line_endings (tools/leonida_tui.py, tools/wave_diff.py). python -m compileall -q tools tests scripts runtime/test/shaders passes.
  • vk_texture_test builds with MSVC + Ninja. Before the rebase, the full scripts/run_gpu_tests.py gave 170/217 passed, 0 failed, 47 skipped (wave64 and ctest-run tests).
  • The full-corpus translated/spirv-val counts weren't run because I have no game copy. The translator change only affects _cl_o samples under --texel-offset-fallback, which is off by default.

AI-assisted: yes (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. This is a correction to existing rows, confirmed by the GPU test and the structural tests above.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 4b7b0f5 on Windows 11 with CPython 3.14.7. The CPU translation checks pass.

I ran python -I -m pytest -q tests/test_texel_offsets.py tests/test_texoff_probe.py. The result is 44 passed and 14 skipped. The 14 cases require SPIRV-Tools. I also translated separate hand-written listings for image_sample_b_cl_o and image_sample_c_cl_o. In each listing, the offset calculation and the sample use the same minimum LOD operand.

I copied the new test file to the parent revision and ran python -I -m pytest -q tests/test_texel_offsets.py -k fallback_level_takes_the_clamp. All three new cases fail there. This check covers the emitted instructions. GPU pixel output remains untested here.

AI-assisted.

@wilu222
wilu222 force-pushed the fix/texoff-fallback-minlod branch from 4b7b0f5 to 87535fe Compare November 23, 2026 04:42
@wilu222

wilu222 commented Nov 23, 2026

Copy link
Copy Markdown
Author

@shdann thanks for testing! Since then I've folded the GPU test into this PR (now 87535fe0, force-pushed): vk_texture_test --two-mip-index and ps_texoff_cl. On an RTX 5070 it's 4096/4096 with the native Offset and with LEONIDA_TEXOFF_FALLBACK=1, and the fallback without the fix gives 2048 wrong pixels (mip 1 texel 9 instead of 11). The translator change is the same as in 4b7b0f50, so your CPU results still hold.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified commit 87535fe0 locally on Windows 11 x64 (AMD Radeon RX 6700 XT, LLVM 21.1.8, SPIRV-Tools, LEONIDA_VK_VALIDATION=1):

  • Live GPU pixel verification (scripts/run_gpu_tests.py, LEONIDA_VK_VALIDATION=1):
    • ps_texoff (default Offset): 5 shaders translated and spirv-val valid, 4/4 passed — including ps_texoff_cl: 4096/4096 pixels exact (mip 1 texel the offset reached), plus ps_texoff, ps_texoff_forms, ps_texoff_implicit.
    • ps_texoff with LEONIDA_TEXOFF_FALLBACK=1: 5 shaders translated and spirv-val valid, 4/4 passed — including ps_texoff_cl: 4096/4096 pixels exact (mip 1 texel the offset reached).
    • ps_texture with LEONIDA_TEXOFF_FALLBACK=1: 9 shaders translated and spirv-val valid, 8/8 passed (0 validation errors).
  • Unit & translation checks:
    • tests/test_texel_offsets.py (run via a minimal Python @pytest/fixture shim because pytest is not installed here, with spirv-as/spirv-val on PATH): 48/48 passed, 0 skipped (all 14 test_fallback_needs_no_maintenance8 cases and the new _cl_o clamp checks passed).
    • python -I tests/test_shader_checks.py: 79/79 OK (2.291s, including the 7 new TexoffClJudge cases and PixelTestRegistry).
    • python -I tests/test_run_gpu_tests.py: 41/41 OK (0.946s).
    • tests/test_docs_*.py: 30/30 OK (35.159s); py_compile, scripts/check_no_game_data.py, and git diff --check: all exit 0.

Reviewed the diff (tools/rdna2spirv.py, runtime/test/vk_texture_test.cpp, runtime/test/shaders/ps_texoff_cl.asm, runtime/test/shaders/check.py, scripts/run_gpu_tests.py, tests/test_texel_offsets.py, tests/test_shader_checks.py, docs/design.md, docs/testing.md); looks solid.

AI-assisted (Gemini, Antigravity).

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

Ran this at 87535fe07 on Windows 11: pytest tests/test_texel_offsets.py tests/test_shader_checks.py -q -> 138 passed, 267 subtests passed; git diff --check clean.

--texel-offset-fallback now folds the offset at the _cl clamp's level, matching the measured form, with a new GPU fixture (ps_texoff_cl) and runner/check support; the tests cover the folded form, the non-_cl path and the fixture's expected words. No blocking issue.

@memorypressure

memorypressure commented Nov 23, 2026 •

Copy link
Copy Markdown

Verified on Linux: tests/test_texel_offsets.py passes all 48 tests (the two new fallback-level-clamp tests plus the pre-existing suite, so the fold change regresses nothing), the semantic test_sample_cl (10 passed) and the translator-use guard pass, and both design.md contract rows were updated as translator changes require. The SPIR-V checks confirm the fold now uses max(LOD, clamp) in-PS and max(0, clamp) outside it. No CI checks were scheduled yet; your commit also sits on the round-43 stacked base. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

Correction (2026-11-23): the rebase/integration-stack note above was wrong — our verifier tracked a stale main ref. Those base commits are current main; this head contains main and merge-trees cleanly against de67135. No rebase needed; the tests-pass verdict stands unchanged. See the correction note on #4421.

@thehollowedone

Copy link
Copy Markdown

Independent native Windows check of 87535fe0. RTX 3080 Ti, NVIDIA 621.35, MSVC 19.44, LLVM MC 23.1.6, Vulkan SDK 1.4.363.0.

The stock ps_texoff_cl fixture passed with native offsets and with LEONIDA_TEXOFF_FALLBACK=1. All 4,096 pixels matched the expected values in each mode, and the two readbacks were byte-identical. Both modules passed SPIR-V validation in each mode. The fallback shader used gradients and minimum LOD with no Offset operand. Khronos core and synchronization validation reported zero errors.

The test harness enabled maintenance8 on this adapter in both runs. The environment override exercised the fallback translation path.

AI-assisted.

…'s level (SHADER-842)

The texel offset fallback divides the offset by the size of the level the
sample reads, but texel_offset_fold never received a _cl form's min-LOD
clamp. A clamp above the computed LOD makes the sample read a coarser level,
so image_sample*_cl_o shifted by texels of a finer level: twice the offset
per level. Outside the pixel stage the fold used level 0 while the sample
reads max(0, clamp).

The fold now raises its level to max(LOD, clamp) before flooring, with the
same clamp value the sample takes as MinLod. Vulkan applies MinLod after the
bias and the sampler's clamps, and OpImageQueryLod leaves it out.

Tests:
- tests/test_texel_offsets.py traces the fold's level back from
  OpImageQuerySizeLod for image_sample_cl_o, image_sample_c_b_cl_o and a
  vertex-stage image_sample_cl_o; the three tests fail without the fix.
- GPU test ps_texoff_cl on a new vk_texture_test --two-mip-index texture,
  whose mip 1 texels all differ (the --two-mip mip 1 is solid cyan, so no
  GPU test could see where an offset lands on a clamped level):
  image_sample_cl_o with offset (+3, -2), clamp 0 on the left half and 1 on
  the right. check.py and run_gpu_tests.py judge it against the texels
  derived in the .asm header; tests/test_shader_checks.py tests the judge.
  On an RTX 5070 it passes with the native Offset and with
  LEONIDA_TEXOFF_FALLBACK=1, and the fallback without this change gives
  2048 wrong pixels (mip 1 texel 9 instead of 11 at x = 32).
@wilu222
wilu222 force-pushed the fix/texoff-fallback-minlod branch from 87535fe to 7462eec Compare November 23, 2026 16:37
@wilu222

wilu222 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Rebased onto main (8575d36ed) to resolve the conflict with #3882 (SHADER-699). The new head is 7462eec89, which replaces 87535fe0.

Thanks to everyone who tested 87535fe0. The fix and the GPU test are unchanged apart from fitting them onto #3882.

@aslan2142

Copy link
Copy Markdown
Contributor

Tested 7462eec8 on RADV: ps_texoff_cl passes on all three devices, both with the native Offset and with LEONIDA_TEXOFF_FALLBACK=1.

Run as scripts/run_gpu_tests.py --filter ps_texoff_cl, offscreen. One GPU per run, picked with LEONIDA_VK_DEVICE (plus MESA_VK_DEVICE_SELECT); the device: line confirmed which one ran. All three have maintenance8 and shaderResourceMinLod (vulkaninfo), so nothing skipped.

Device Native Offset (maintenance8) LEONIDA_TEXOFF_FALLBACK=1 Fallback, main's translator
llvmpipe (LLVM 23.1.5) PASS, 4096/4096 PASS, 4096/4096 FAIL
Radeon gfx1036 iGPU (RDNA2, RADV RAPHAEL_MENDOCINO) PASS, 4096/4096 PASS, 4096/4096 FAIL
RX 7900 XTX (RDNA3, RADV NAVI31) PASS, 4096/4096 PASS, 4096/4096 FAIL

Negative control (this PR's tree with tools/rdna2spirv.py from main at e36d4c45):

  • Every device fails the same way: 2048 of 4096 pixels differ (the whole right half).
  • At (32, 0) it got R = 36/255 (mip 1 texel 9) where 44/255 (texel 11) is expected. That's the old fold's shift the .asm header predicts, and the same thing you saw on the RTX 5070.
  • The native SPIR-V has Offset|MinLod on the sample. The fallback SPIR-V has no Offset and uses OpImageQueryLod / OpImageQuerySizeLod, so the two modes really do take different paths.

Unit tests on the head:

  • pytest tests/test_shader_checks.py -k TexoffClJudge: 7 passed.
  • The whole file: 15 Wave64Checks failures, the same as on main.
  • tests/test_texel_offsets.py: 62 passed.
  • Small catch: TexoffClJudge (like TexelOffsetFormsChecks on main) sits below the file's if __name__ == "__main__": unittest.main(). So python -I tests/test_shader_checks.py never runs it; only pytest does.

Merge status:

Environment: Linux, Mesa 26.3.1 (RADV and llvmpipe), LLVM 23.1.5 llvm-mc.

AI-assisted (Claude Code, Claude Opus 5.5).

@thehollowedone

Copy link
Copy Markdown

Independent Windows recheck of 7462eec8 on an RTX 3080 Ti (driver 621.35, Vulkan SDK 1.4.363.0, MSVC 19.44, LLVM MC 23.1.6).

run_gpu_tests.py --filter ps_texoff --require-pass passed all four fixtures in both native offset mode and with LEONIDA_TEXOFF_FALLBACK=1. For ps_texoff_cl, all 4,096 pixels matched the expected values in each mode, and the readbacks were byte-identical. The native runs for ps_texoff and ps_texoff_implicit omit the +16/−32 offset because this adapter supports native offsets from −8 to +7. The fallback runs checked that offset too.

All five modules per mode passed spirv-val. Vulkan core and synchronization validation reported zero diagnostics. Synthetic inputs only. AI-assisted.

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.

7 participants