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: #3882, #2783. This PR builds on neither, but all three edit the level computation in texel_offset_fold, so whichever lands later needs a small rebase. #3882 (SHADER-699) rounds the level by the S# mip filter and #2783 (SHADER-491) clamps to the S#'s MIN_LOD/MAX_LOD. Both are about the sampler. This PR is about the instruction's own _cl clamp, which neither handles.

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 flooring and clamping to the image's levels. It uses the same SPIR-V value the sample takes as MinLod / in its Lod. Vulkan applies the MinLod image operand after the bias and the sampler's LOD clamps, and OpImageQueryLod doesn't include it, so the fold must add it itself.

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

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 ← ConvertFToS ← 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 on main and pass with the fix. The whole file plus tests/test_texoff_probe.py gives 58 passed, including spirv-val (SPIRV-Tools v2026.4) of every fallback form.
  • tests/test_shader_checks.py (90 passed), tests/test_run_gpu_tests.py (41 passed), tests/semantic test_sample_cl and test_sample_c (18 passed), and the tests/test_docs_*, test_design_contract_rows, line-ending and CSV tests all pass. python -m compileall -q tools tests scripts runtime/test/shaders passes.
  • The runtime builds with MSVC + Ninja. Earlier on main, 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.

…'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 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

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).

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