Skip to content

rdna2spirv: --min-lod-fallback, image_sample_d_cl without shaderResourceMinLod (Intel) - #4642

Open
kapitche wants to merge 5 commits into
ItsNotRexed:mainfrom
kapitche:kap-minlod-fb
Open

kapitche wants to merge 5 commits into
ItsNotRexed:mainfrom
kapitche:kap-minlod-fb

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

Item: new SHADER/TEST item, claimed at #4571 (comment) (from my finding #4571 (comment)). Independent of #3058, which adds the other _cl forms.

What broke. image_sample_d_cl is translated to Grad|MinLod, which needs the core shaderResourceMinLod feature. The Intel UHD 770 (driver 32.0.101.6129) does not have it, so the 29 of the 12,101 pixel shaders (one build's index) that declare MinLod cannot be created there (4 of the 480 in the #4628 sweep; vk_ps_sweep reports "unsupported: shaderResourceMinLod"). Repro without game data: LEONIDA_VK_DEVICE / VK_LOADER_DRIVERS_DISABLE=*nv* on that machine and python -I scripts/run_gpu_tests.py --filter ps_texture_grad_cl gives SKIP.

Fix. A new translator flag --min-lod-fallback. The clamp is applied to the gradients instead: the footprint rho^2 = max(|dxsize|^2, |dysize|^2) is measured in level-0 texels (OpImageQuerySizeLod), and where rho^2 < 4^minlod the gradients become an isotropic footprint of 2^minlod texels on their own axes (OpSelect between the new and the shader's own gradients). Otherwise they are unchanged, so that case is exact. The module then declares no MinLod. Known difference: a clamped sample takes no extra anisotropic taps along the stretched axis (written up in docs/design.md).

make_game.py turns the flag on by itself when the runtime's GPU lacks the feature: tools/vk_subgroup_info.py now reports shader_resource_min_lod (core VkPhysicalDeviceFeatures, 43rd member), and --min-lod-fallback is in FALLBACK_FLAGS, so it is recorded with the translation and a change retranslates. LEONIDA_MINLOD_FALLBACK=1 does the same for runtime/test/shaders/build.py and scripts/run_gpu_tests.py.

Tests.

  • Linux, Python 3.12.16: python -I tests/test_min_lod_fallback.py (7 tests, synthetic listings: with and without the flag, 1D / 2D / 2D array / cube / 3D / a16, the selected gradients, the unclamped arm kept, the clamped footprint on its own axis, other samples unchanged), tests/test_vk_subgroup_info.py (49), tests/test_make_game_*.py (the new device-follows test, and the four files that mock the probes now mock this one too), tests/test_docs_*.py, tests/test_ctest_registration.py.
  • Windows 11, Python 3.13.1, MSVC 14.51: scripts/run_gpu_tests.py --filter ps_texture_grad_cl (the existing two-mip GPU test, a clamp of 0 left of x = 32 and 1 right of it), 4096 pixels each:
GPU without the flag with LEONIDA_MINLOD_FALLBACK=1
Intel UHD 770 SKIP (no shaderResourceMinLod) PASS 4096/4096
RTX 4080 SUPER PASS PASS

Negative control. tests/test_min_lod_fallback.py with tools/rdna2spirv.py from upstream/main: 13 failures and 2 errors (no flag, still MinLod). GPU: with the clamp test inverted (OpFOrdGreaterThan for OpFOrdLessThan), the Intel run fails with 2048 of 4096 pixels differing (the right half reads mip 0 instead of cyan).

Not tested: 1D, 3D and array targets on a GPU, anisotropic samplers, and real corpus shaders on Intel (I will post that on this PR from the #4628 sweep).

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted. Tested c9a4a82 on Windows 11 with an NVIDIA GeForce RTX 4090 (driver 32.0.16.2101) and AMD Radeon Graphics (driver 32.0.21043.5001). The MinLod fallback passes on both devices.

I used the hand-written vs_ids and ps_texture_grad_cl_a16 listings. I enabled MIN_LOD_FALLBACK in the translator and assembled its output with tools/spirv_asm.py. I ran vk_texture_test with --two-mip. I checked all 4,096 pixels with texture_gradient_a16_test on each device. All pixels match. The device line confirms each adapter. I selected the AMD driver with VK_LOADER_DRIVERS_SELECT=amd.

I ran the six changed Python test files with CPython 3.14.7 in isolated mode. They ran 227 tests. One test skipped. The other 226 passed. On merge base d0835dd, the seven fallback tests report 13 subtest failures and two errors.

@justr4mzi

Copy link
Copy Markdown
Contributor

Tested at c9a4a823 on Windows 11 with discrete AMD Radeon RX 6700 XT (RDNA2, gfx1031), LLVM_MC, SPIRV-Tools, and LEONIDA_VK_VALIDATION=1.

GPU & Subgroup Probe Verification

  • python -I tools/vk_subgroup_info.py: reports "shader_resource_min_lod": true on AMD Radeon RX 6700 XT (since this discrete card supports shaderResourceMinLod, the --min-lod-fallback path was exercised explicitly via LEONIDA_MINLOD_FALLBACK=1).
  • python -I scripts/run_gpu_tests.py --build build --filter ps_texture_grad --require-pass (LEONIDA_VK_VALIDATION=1, assembling .asm with llvm-mc and validating with spirv-val):
    • Default (LEONIDA_MINLOD_FALLBACK unset): 3 shaders: 3 translated, 3 pass spirv-val, 2/2 passed (ps_texture_grad_a16, ps_texture_grad_cl_a16 — 4096 RGBA pixels matched each, 0 validation errors).
    • Fallback (LEONIDA_MINLOD_FALLBACK=1): 3 shaders: 3 translated, 3 pass spirv-val, 2/2 passed (ps_texture_grad_a16, ps_texture_grad_cl_a16 — 4096 RGBA pixels matched each, 0 validation errors).

Python & Docs Suites (python -I / unittest)

  • tests/test_min_lod_fallback.py: 7/7 OK (0.280s)
  • tests/test_vk_subgroup_info.py: 49/49 OK (0.048s)
  • tests/test_make_game_argparse.py: 36/36 OK (11.589s)
  • tests/test_make_game_preflight.py: 79/79 OK (1.348s)
  • tests/test_make_game_translator_flags.py: 18/18 OK (0.453s)
  • tests/test_make_game_wave64.py: 38/38 OK (0.712s)
  • tests/test_shader_checks.py: 75/75 OK (1.462s)
  • tests/test_run_gpu_tests.py: 40/40 OK (0.560s)
  • tests/test_docs_*.py: 20/20 OK (17.676s)
  • py_compile, scripts/check_no_game_data.py, and git diff --check: exited 0.

AI-assisted (Gemini, Antigravity).

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested c4d567e on an RTX 3070 (610.88, Windows 11 26300, MSVC VS 2022, Vulkan SDK 1.4.363 layers, llvm-mc 21), with the validation layer: pass, with one finding about anisotropic samplers below. At c9a4a82, before the rebase, full ctest was 154/154 with 0 build warnings and run_gpu_tests.py 164/210 with 46 skips. ps_texture_grad_cl_a16 passes both ways. The 9 tests/semantic failures in that run (test_loop_ballot_barrier, test_mutations, test_cmp_class_f64) are the same with d0835dd9's rdna2spirv.py, so they predate this PR. The rebase leaves min_lod_grads byte-identical, and I reran the checks below on c4d567e.

Native MinLod against --min-lod-fallback on the same GPU. The 3070 has shaderResourceMinLod, so I built one local pixel shader both ways and diffed the two renders on vk_texture_test --two-mip. The shader sweeps the gradient LOD from -2 to +2 across x and the clamp from 0 to 1.5 down y, with isotropic gradients in the top half and 4:1 (dv/dy = 4 du/dx) in the bottom half. The native build declares MinLod and the fallback build doesn't. Mip mode and anisotropy were set through a local, test-only change to vk_texture_test's sampler. 0 validation messages in every run.

Sampler Pixels that differ (of 4096)
nearest mip 0
linear mip 0 (max 1/255)
nearest mip, 16x anisotropy 669
linear mip, 16x anisotropy 1772

So with anisotropy off the fallback matches MinLod exactly, including the 4:1 footprints and fractional clamps under trilinear filtering. ps_texture_grad_cl_a16 matches on all four samplers.

Finding: under an anisotropic sampler the clamp can be missed, not only the extra taps. min_lod_grads compares the major axis (max(|dx|², |dy|²)) with 4^minlod. With anisotropic filtering the LOD comes from the minor axis. So when the major axis is past the clamp but the minor axis isn't, the gradients are kept and the sample reads a finer level than the clamp. With nearest mip and 16x anisotropy, the 4:1 half has 417 pixels where the fallback reads mip 0 and MinLod reads mip 1. One example is clamp 0.68 with a minor-axis LOD of -1.22 and a major-axis LOD of +0.78. This follows from how anisotropic LOD is defined, not from NVIDIA, so I'd expect it on Intel too. I haven't run it there.

The shader can't see whether the sampler is anisotropic, so there may be no exact fix. Two options:

  • Name the case in docs/design.md: clamped samples through anisotropic samplers with stretched footprints read finer than the clamp.
  • Scale both gradients until the minor axis reaches 2^minlod. That keeps anisotropic samplers clamped, but where the major axis is longer it makes non-anisotropic samples coarser than MinLod.

An NVIDIA-only effect that explains the isotropic half. The 216 differing pixels in the top half all go the other way: the fallback reads coarser than the clamp. On this driver, explicit isotropic gradients through an anisotropic sampler give a larger LOD than they should. A plain image_sample_d (no clamp) with the fallback's own gradients, (2^c/128, 0) and (0, 2^c/128), reads as follows:

c anisotropy off or 1 2 16
0.07 0.070 0.105 0.141
0.30 0.305 0.457 0.609
0.54 0.535 0.801 1.000

The min/mag filter doesn't change this. Native MinLod clamps the final LOD, so it isn't affected. The fallback goes through the gradients, so it is. The fallback only runs where shaderResourceMinLod is missing, so this doesn't reach NVIDIA users, but it's worth knowing before comparing the two paths on NVIDIA with anisotropic samplers.

I didn't run 1D, 3D or array targets: vk_texture_test only has a two-mip 2D texture. The 1D and 3D gradient paths are only in your Python listing tests.

AI-assisted (Claude Code).

@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 c4d567e91 on Windows 11: pytest tests/test_min_lod_fallback.py tests/test_make_game_translator_flags.py tests/test_make_game_argparse.py tests/test_make_game_preflight.py tests/test_make_game_wave64.py -q -> 189 passed, 1 skipped, 85 subtests passed; git diff --check clean.

--min-lod-fallback is threaded through make_game.py, the shader build and the GPU runner, with the translator lowering image_sample_d_cl for devices without shaderResourceMinLod (the Intel path); the added tests cover the flag wiring, the translator form and the unchanged default. No blocking issue.

@kapitche

Copy link
Copy Markdown
Contributor Author

Thank you for the measurements. The finding is right and follows from the code: min_lod_grads compares the major axis with 4^minlod, while an anisotropic sampler picks its level from the minor axis. A stretched footprint whose major axis is past the clamp and whose minor axis is not therefore keeps the shader's gradients and reads a finer level than MinLod. The shader cannot see whether the sampler is anisotropic, so I did not change the lowering: scaling the gradients until the minor axis reaches the clamp would make non-anisotropic samples coarser than MinLod, where today they match exactly (your 0/4096 with anisotropy off).

Pushed d7a83cf, docs only: the --min-lod-fallback row in docs/design.md now states the anisotropic-sampler difference (no extra taps, and the major-axis test), with your RTX 3070 numbers (669 of 4096 pixels with nearest mip and 16x anisotropy, 0 with anisotropy off). Anisotropic samplers on the Intel UHD 770 stay listed as not GPU-tested; I did not measure them. Checked on Python 3.12.16, Linux: test_min_lod_fallback.py OK and all tests/test_docs_*.py plus test_i18n_index.py pass.

The isotropic-half effect you saw (explicit gradients read a larger LOD through an anisotropic sampler on NVIDIA) only touches the fallback path, which runs where shaderResourceMinLod is missing, so I left it out of the row.

@kapitche

Copy link
Copy Markdown
Contributor Author

Measured anisotropic samplers on the Intel UHD 770 (driver 32.0.101.6129) with the flag, as the "not tested" line promised. Windows 11, MSVC 14.51, Python 3.13.1, built from d7a83cfb8 plus a throwaway local change that is not pushed: vk_texture_test sets anisotropyEnable/maxAnisotropy on the shader's sampler (env var) and the fixture's du/dx is 8/128, so the footprint is 8:1 (clamp 0 on the left half of the 64x64 target, 1 on the right).

Pixels read, per half (2048 each), from the raw output:

build, GPU, sampler left half (clamp 0) right half (clamp 1)
MinLod, RTX 4080 SUPER, 8x or 16x, point or linear mip 0 mip 1 (cyan)
--min-lod-fallback, RTX 4080 SUPER, 8x or 16x mip 0 mip 0
--min-lod-fallback, UHD 770, 8x or 16x, linear filters mip 0 mip 0
--min-lod-fallback, UHD 770, 8x or 16x, point filters mip 1 mip 1 (the driver ignores the anisotropy)
either build, either GPU, 1x or off mip 1 mip 1

(MinLod cannot be created on the Intel card, so the native reference is the RTX.) So the Intel card behaves like the RTX with the flag: where the major axis (8 texels) is past the clamp but the anisotropic level (about 1 texel) is not, the flagged build reads mip 0 where MinLod gives mip 1, 2048 of 4096 pixels here. That is the difference the design note already described for the RTX 3070; the exact fix needs the sampler's max anisotropy, which the shader does not see, so I left the behaviour and updated docs/design.md with these numbers and removed "anisotropic samplers on Intel" from the not-tested list (b1c7a1c; tests/test_docs_*.py, test_i18n_index.py and test_min_lod_fallback.py OK on Linux, Python 3.12.16).

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