Repository navigation
Apply the S#'s LOD bias in the shader where samplers take none (SHADER-268) - #1845
Jolymmiles wants to merge 1 commit into
Conversation
Vonsek
left a comment
There was a problem hiding this comment.
I found a case where the LOD-bias and texel-offset fallbacks select different mip levels for the same sample. I built a synthetic image_sample_o with both flags on 61e06e9f using the normal fixture builder. It passes spirv-val. The emitted texel_offset_fold extracts component 0 of OpImageQueryLod, adds the sampler bias, then uses that result to pick the mip dimensions. But component 0 already reflects level selection. The actual Grad sample applies the sampler clamp after the bias.
A CPU model of the emitted expression predicts a wrong texel with a point sampler, two 8×8 / 4×4 mips, base LOD 0, sampler bias +1, min/max LOD [0, 0.25], UV (2.5/8, 2.5/8), and decoded offset (+1, 0). The sample selects mip 0, while the emitted offset size uses mip 1. At mip 0 the shader therefore advances two texels, reading x=4 instead of x=3. The literal S# words in the synthetic fixture are 0x92, 0x40000, 0x100, 0; no game data or GPU run.
Could the implicit offset path query at the bias-scaled coordinate, as image_get_lod already does? That would let Vulkan apply the clamp before providing the mip used for the offset size. The existing single-mip offset fixture cannot distinguish these outcomes. The four relevant Python suites passed 123 tests on Windows.
AI-assisted.
466dd26 to
b284dfe
Compare
|
I reproduced the one-texel error from Vonsek's review on 466dd26. Tested on RTX 3060 12 GB, NVIDIA 621.14, Vulkan 1.4.357, Windows 11 26H2. Using synthetic 8×8 and 4×4 mip levels and an image_sample_o probe, the native path and either fallback alone returned texel (3, 2). With both LEONIDA_LOD_BIAS_FALLBACK=1 and LEONIDA_TEXOFF_FALLBACK=1, the GPU returned (4, 2). The sample used UV (0.3125, 0.3125), offset (+1, 0), and sampler words 0x92, 0x40000, 0x100, 0. I ran build.py for vs_ids and ps_combined_probe and read back the rendered R/G channels. Both shaders passed spirv-val in all four modes. The same result reproduced twice on the earlier 61e06e9 head. Synthetic inputs only. AI-assisted setup and verification; tests ran locally. |
b284dfe to
fc793ad
Compare
|
Rebased onto 4947579 (head fc793ad) and re-verified on the same Apple M3 Max:
AI-assisted. |
fc793ad to
ab651e4
Compare
|
Note 🤖 Claude Opus 5.5 responding on behalf of Jolymmiles @Vonsek, @umnitus: fixed in ab651e4, the way the review suggested. With both flags, Reproduced on GPU, Apple M3 Max. A probe samples
Still open, and the same with or without this flag: the fold ignores the sampler's [minLod, maxLod] for Re-verification of ab651e4 (base 4947579, unchanged):
|
…R-268) MoltenVK reports VkPhysicalDevicePortabilitySubsetFeaturesKHR::samplerMipLodBias false (Metal samplers have no LOD bias) and ignores mipLodBias, which with the portability subset is also invalid usage (VUID-...-samplerMipLodBias-04467), so ps_ssharp failed at "point bias +1" and every biased sampler read the wrong mip. - rdna2spirv.py --lod-bias-fallback: samples apply S# word 2 [13:0] where Vulkan adds the sampler's bias, before the clamp: gradients x 2^bias for a 2D implicit PS sample (sampled with Grad from the quad derivatives), the Bias operand for other implicit PS samples (added to _b's), added to _l's LOD, LOD = bias for _lz, _c_lz and implicit samples outside the PS, gradients x 2^bias for _d/_d_cl. image_get_lod queries at the coordinate x 2^bias, since OpImageQueryLod's lambda' includes the sampler's bias. With --texel-offset-fallback, an implicit image_sample_o/_b_o finds the level its offset is scaled by the same way, so the device clamps after the bias as the sample does. A scalar-list S# (VCCTuple) reads word 2 from its third entry. Gathers are unchanged. The index marks such shaders lod_bias_fallback. Without the flag all 214 GPU-test modules and index.json are byte-identical to ec8ec4c's. - vulkan_sampler(..., 0) gives mipLodBias 0; the D3D12 device and vk_sampler_test read the portability subset (runtime/src/vulkan_portability.h), enable it where offered, and zero the bias where the feature is missing; vk_sampler_test also takes --shader-lod-bias and enumerates portability drivers behind the loader. - vk_subgroup_info.py reports sampler_lod_bias; make_game.py and run_gpu_tests.py turn the flag on where it is false (LEONIDA_LOD_BIAS_FALLBACK=1/0 overrides). - ps_ssharp_ops (image_sample_l, _b, _d, _d_cl), ps_ssharp_lod (image_get_lod at LOD 1 and 2, over the cases without MIN_LOD = MAX_LOD, which llvmpipe leaves out of the query, plus MAX_LOD 0.25) and three more SAMPLER_CASES (+0.375, bias -1 with MIN_LOD 1, +1 with MAX_LOD 0); test_lod_bias_fallback.py. Apple M3 Max: ps_ssharp, ps_ssharp_ops and ps_ssharp_lod pass on MoltenVK 1.4.2 with the flag (chosen by itself), and on KosmicKrisp and llvmpipe (Mesa 26.3.1) with it forced on, plain and with LEONIDA_W64_EMULATE=1 LEONIDA_MESH_COMPUTE=1. Natively llvmpipe passes all three and KosmicKrisp ps_ssharp and ps_ssharp_ops; its OpImageQueryLod leaves the sampler's bias out of lambda', so ps_ssharp_lod fails there (docs/testing.md). The D3D12 part compiles and links with clang-cl 23.1.6 and the xwin 10.0.26100 SDK; not run on Windows. AI-assisted.
ab651e4 to
a71346f
Compare
|
Note 🤖 Claude Opus 5.5 responding on behalf of Jolymmiles Rebased onto ec8ec4c (head a71346f) and re-verified on the same Apple M3 Max.
|
|
Tested a71346f (merged onto main 4947579) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass
AI-assisted (Claude). |
|
Tested a71346f on AMD Radeon RX 7600 (RDNA3), AMD 26.11.2 (LLPC), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8: pass.
AI-assisted (Claude Code, reviewed with Codex). No game files. |
|
Apple M2 validation of the S# LOD-bias fix that resolves the failure I reported. Tested head a71346f on Apple M2 (Mac14,3), macOS 26.4 (25E246), MoltenVK 1.4.2 (Homebrew, direct), subgroup 4..32, llvm-mc 23.1.6, Python 3.13.18.
Full suite on this head: 195 shaders translated, 195 pass spirv-val; 119/191 passed, 72 skipped, 0 failed (exit 0). With the same runner on current main 1628210 the M2 had 116/189 passed, 72 skipped, 1 failed (ps_ssharp). No failures remain on this Mac with this PR. Build note: the full build fails on AppleClang 17 at vk_pipeline_check.cpp:567 (BUILD-822, fixed by open PR #2927); all GPU binaries built, so the runs above are valid. Head fetched unchanged into a separate worktree; no rebase/edit/push. Synthetic shaders and counts only; no game files. AI-assisted. |
…ython suites runtime.yml gets a macos job on macos-26: Homebrew's MoltenVK, Vulkan headers and loader, llvm-mc, SPIRV-Tools and glslang; the runtime built through sccache; ctest and run_gpu_tests.py on MoltenVK. ci.yml gets macos-python: the standard-library loop, the semantic suite alongside it, and the pytest files with Homebrew's llvm-mc. The hosted runner's Metal device is Apple Paravirtual device (family Mac2), which MoltenVK runs on; KosmicKrisp finds no device there. macos-15 can't build the runtime (Xcode 16's libc++ has no floating-point std::from_chars). The two tests that fail on MoltenVK today, also on an M3 Max, are listed with the pull requests that fix them (xdxr_build_check: ItsNotRexed#2423, ps_ssharp: ItsNotRexed#1845); scripts/ci_expected_failures.py fails the step on any other failure and notes an expected one that passes.
…-918) KosmicKrisp (Mesa's Metal driver, driver ID 28) reports VkPhysicalDevicePortabilitySubsetFeaturesKHR::samplerMipLodBias as true, yet its OpImageQueryLod leaves the sampler's mipLodBias out of the calculated LOD and adds it, unrounded and past the last level, to the clamped one (ItsNotRexed#3124, Mesa 26.3.1 on an M3 Max). ps_ssharp_lod therefore fails there while the probe says the samplers take a bias, and make_game.py translates without --lod-bias-fallback: the driver drops the S#'s bias from the LOD query and nothing applies it. This is the one piece ItsNotRexed#1845 (SHADER-268) leaves open: it selects the fallback from the portability subset's samplerMipLodBias feature, which KosmicKrisp reports as present, and documented the KosmicKrisp failure as needing LEONIDA_LOD_BIAS_FALLBACK=1 by hand. - tools/vk_subgroup_info.py: sampler_lod_bias is false for driver ID 28, whatever the feature says, and the answer carries kosmickrisp. - runtime/src/vulkan_portability.h: sampler_mip_lod_bias() is false for driver ID 28 (is_kosmickrisp, by the driver ID as is_moltenvk is, since the extension may not be in sight), so vulkan_sampler() creates samplers with mipLodBias 0 and the shaders translated with --lod-bias-fallback apply the S#'s bias there, as on MoltenVK. Older Vulkan headers get the driver ID constant. - docs/design.md: how sampler_lod_bias is chosen now; docs/testing.md: the ps_ssharp_lod row says the tooling selects the fallback for KosmicKrisp. - tests/test_vk_subgroup_info.py: a KosmicKrisp device that reports the feature (with and without VK_KHR_portability_subset), MoltenVK, and an AMD device that keeps the bias. Depends on ItsNotRexed#1845. The PR diff shows ItsNotRexed#1845's commit as well: this one is the last commit, 205810f3. Tested on macOS 27.2.1 (Apple M3 Pro, CPython 3.15.2), no game files: - python -I tests/test_vk_subgroup_info.py -> 46 tests OK. - tools/vk_subgroup_info.py against the real drivers on this machine: KosmicKrisp (Homebrew Mesa, driver 28) -> "kosmickrisp": true, "sampler_lod_bias": false; MoltenVK (driver 14) -> "sampler_lod_bias": false. Before this change the same KosmicKrisp device answered true, which is the bug. - tests/test_docs_design_rows.py, test_docs_links.py, test_docs_repo_paths.py, test_make_game_translator_flags.py, test_shader_checks.py -> OK; python -m compileall -q tools tests -> OK. - cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo and cmake --build runtime/build -> 96/96 targets, clean. The GPU result ps_ssharp_lod passes with the fallback on KosmicKrisp is ItsNotRexed#3124's and ItsNotRexed#1845's measurement (M3 Max, Mesa 26.3.1); this change only makes that selection automatic, and was not re-run on a GPU here. AI-assisted.
|
Heads-up for whoever lands this: #4641 is now stacked on it ( The delta is two hunks, if you would rather have it here than in a stacked PR:
Measured here (M3 Pro, Homebrew Mesa, no game files): |
…-918) KosmicKrisp (Mesa's Metal driver, driver ID 28) reports VkPhysicalDevicePortabilitySubsetFeaturesKHR::samplerMipLodBias as true, yet its OpImageQueryLod leaves the sampler's mipLodBias out of the calculated LOD and adds it, unrounded and past the last level, to the clamped one (ItsNotRexed#3124, Mesa 26.3.1 on an M3 Max). ps_ssharp_lod therefore fails there while the probe says the samplers take a bias, and make_game.py translates without --lod-bias-fallback: the driver drops the S#'s bias from the LOD query and nothing applies it. This is the one piece ItsNotRexed#1845 (SHADER-268) leaves open: it selects the fallback from the portability subset's samplerMipLodBias feature, which KosmicKrisp reports as present, and documented the KosmicKrisp failure as needing LEONIDA_LOD_BIAS_FALLBACK=1 by hand. - tools/vk_subgroup_info.py: sampler_lod_bias is false for driver ID 28, whatever the feature says, and the answer carries kosmickrisp. - runtime/src/vulkan_portability.h: sampler_mip_lod_bias() is false for driver ID 28 (is_kosmickrisp, by the driver ID as is_moltenvk is, since the extension may not be in sight), so vulkan_sampler() creates samplers with mipLodBias 0 and the shaders translated with --lod-bias-fallback apply the S#'s bias there, as on MoltenVK. Older Vulkan headers get the driver ID constant. - docs/design.md: how sampler_lod_bias is chosen now; docs/testing.md: the ps_ssharp_lod row says the tooling selects the fallback for KosmicKrisp. - tests/test_vk_subgroup_info.py: a KosmicKrisp device that reports the feature (with and without VK_KHR_portability_subset), MoltenVK, and an AMD device that keeps the bias. Depends on ItsNotRexed#1845. The PR diff shows ItsNotRexed#1845's commit as well: this one is the last commit, 205810f3. Tested on macOS 27.2.1 (Apple M3 Pro, CPython 3.15.2), no game files: - python -I tests/test_vk_subgroup_info.py -> 46 tests OK. - tools/vk_subgroup_info.py against the real drivers on this machine: KosmicKrisp (Homebrew Mesa, driver 28) -> "kosmickrisp": true, "sampler_lod_bias": false; MoltenVK (driver 14) -> "sampler_lod_bias": false. Before this change the same KosmicKrisp device answered true, which is the bug. - tests/test_docs_design_rows.py, test_docs_links.py, test_docs_repo_paths.py, test_make_game_translator_flags.py, test_shader_checks.py -> OK; python -m compileall -q tools tests -> OK. - cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo and cmake --build runtime/build -> 96/96 targets, clean. The GPU result ps_ssharp_lod passes with the fallback on KosmicKrisp is ItsNotRexed#3124's and ItsNotRexed#1845's measurement (M3 Max, Mesa 26.3.1); this change only makes that selection automatic, and was not re-run on a GPU here. AI-assisted.
|
Tested a71346f on Apple M2 with MoltenVK 1.4.2 (macOS 26.4 (25E246), subgroup 4..32, device Vulkan 1.4.357): S# tests 3/3 pass with the flag and 0/3 without; full suite 119/191 passed, 72 skipped, 0 failed with the flag vs 116/191 passed, 72 skipped, 3 failed with
Comparison with Jolymmiles' Apple M3 Max result on this PR: same shape — Build caveat: the PR's base No game data; synthetic fixtures and counts only. AI-assisted. |
|
Tested MoltenVK 1.4.2 —
KosmicKrisp 26.3.1 (Mesa 26.3.1, through the Vulkan loader) — this is the driver in #3124, where
So on the Mac side this PR does what it says on MoltenVK and has the escape hatch KosmicKrisp needs. One thing before the full-suite numbers are worth reading: this head is based on an older Built with AppleClang 21.0.0, CMake 4.4.4 + Ninja, Homebrew llvm-mc 23.1.4 (AMDGPU), SPIRV-Tools 1.4.363.0. AI-assisted. |
Claimed in the coordination issue as: SHADER-268, #1548 (comment)
What and why
MoltenVK reports
VkPhysicalDevicePortabilitySubsetFeaturesKHR::samplerMipLodBias = VK_FALSE(Metal samplers have no LOD bias) and ignoresmipLodBias. With the portability subset enabled, a non-zeromipLodBiasis also invalid usage (VUID-VkSamplerCreateInfo-samplerMipLodBias-04467).vulkan_sampler()put the S#'s LOD_BIAS there, so on a Mac every biased sampler read the wrong mip:ps_ssharpfailed at "point bias +1". Under Wine the same bias is missing in the D3D12 path the game takes on a Mac:d3d12_descriptorfailed atliteral sampler bias 31: 0/64(CrossOver 26.3 with MoltenVK 1.4.3).Translator,
--lod-bias-fallback: each sample applies S# word 2 [13:0] at the point where Vulkan's level-of-detail operation adds the sampler's bias, which is before the [minLod, maxLod] clamp:image_sample, which goes through the typed path withGradfrom the quad derivatives, gets those gradients times 2^bias. Any other implicit PS sample gets aBiasoperand, and_badds the S# bias to its own._ladds it to its LOD._lz,_c_lzand implicit samples outside the PS use LOD = bias, which is what the native path gives, since Vulkan adds the sampler's bias to an explicit LOD too._d/_d_clscale their gradients by 2^bias (Gradtakes noBias).image_get_lodqueries at its coordinate times 2^bias, becauseOpImageQueryLod's λ′ includes the sampler's bias.lod_bias_fallback.Runtime:
vulkan_sampler(…, 0)givesmipLodBias0. The D3D12 device andvk_sampler_testread the portability subset's features (newruntime/src/vulkan_portability.h), enableVK_KHR_portability_subsetwhere it is offered (VUID-04451), and zero the bias where the feature is missing.Under Wine: winevulkan doesn't pass
VK_KHR_portability_subseton to Windows programs, so a D3D12 device under Wine can't see the missing feature. It does pass the host driver's ID, sovulkan_portability.hgainsis_moltenvk().sampler_mip_lod_bias()and the D3D12 device treat MoltenVK without the extension in sight as a device whose samplers take no bias.d3d12_descriptor_testnow uses the same check. That test samples literal S#s through a GLSL shader, which applies no bias, so on such a device its "literal sampler bias 31" case is reported as not judged, with the reason in the output; the bias −32 case still runs. The translated-shader path is whatps_ssharp*test.Selection: it works like the f16 interpolation fallback.
tools/vk_subgroup_info.pyreportssampler_lod_bias, andmake_game.pyandrun_gpu_tests.pyturn the flag on where it is false.LEONIDA_LOD_BIAS_FALLBACK=1/0overrides it.Tests:
ps_ssharp_opscovers_l,_b,_dand_d_cl;ps_ssharp_lodcoversimage_get_lod.SAMPLER_CASES: +0.375, "bias −1, MIN_LOD 1" and "bias +1, MAX_LOD 0". The last two tell bias-then-clamp from clamp-then-bias.tests/semantic/test_lod_bias_fallback.py.docs/design.md, plus notes indocs/testing.mdanddocs/d3d12_vulkan.md.Tested
Apple M3 Max, macOS 27.3. MoltenVK 1.4.2 (Homebrew), KosmicKrisp and llvmpipe (Mesa 26.3.1), on the base of this branch (003e781) and on the branch. "emu" means
LEONIDA_W64_EMULATE=1 LEONIDA_MESH_COMPUTE=1. Results are passed/total:ps_ssharpFAIL→PASS;ps_ssharp_ops,ps_ssharp_lodPASSps_ssharp_opsPASS,ps_ssharp_lodFAIL (see below)No other test changed status in any configuration (per-test diff of the runner's output).
ps_ssharp_lodon KosmicKrisp without the flag: itsOpImageQueryLodleaves the sampler'smipLodBiasout of λ′ and returns the clamped level past the last mip: at "point bias +1" it gives (2, 1) for (1, 2). llvmpipe and MoltenVK with the flag agree with the expected values. WithLEONIDA_LOD_BIAS_FALLBACK=1the test passes on KosmicKrisp as well. I kept the test rather than skip it there, and the KosmicKrisp behaviour is noted indocs/testing.md.LEONIDA_LOD_BIAS_FALLBACK=1,ps_ssharp,ps_ssharp_ops,ps_ssharp_lodandps_get_lodpass on KosmicKrisp and llvmpipe, plain and with emu. So the shader-side bias matches the drivers' own sampler bias, applied before the clamp.runtime/test/shaders/build.pyoutput on 003e781 and on this branch is byte-identical. That covers 383 files plain and withLEONIDA_W64_EMULATE=1;index.jsononly gains the two new tests. It also covers 189 files withLEONIDA_F16_INTERP_FALLBACK=1 LEONIDA_TEXOFF_FALLBACK=1and 191 withLEONIDA_BARY_FALLBACK=1, whereps_ij_computed, which those fallbacks reject, is left out.vk_sampler_testthat still passes the S# bias reports 04467; this branch's doesn't.d3d12_descriptorgets past the bias case ("not judged … MoltenVK") and then stops at its bindless-capacity check (view 5001 through sampler 2100 … got 0,0,0,0, line 1876). That check was never reached on a Mac before, it is unrelated to this change, and I'll post it as a separate finding.tests/semanticOK,regen_fixtures.py --checkOK.test_shader_checks,test_vk_subgroup_info,test_make_game_argparseandtest_make_game_translator_flagspass, plus pytesttest_mesh_compute.pyandtest_translate.py(499 passed).compileallis clean.gta6rt_d3d12_com,gta6rt,d3d12_descriptor_test,d3d12_vulkan_device_test,vk_sampler_test) compiles and links with clang-cl 23.1.6 and the xwin 10.0.26100 SDK (runtime/cmake/clang-cl-xwin.cmake) on an earlier base of this commit. The rebases since touched only Python and docs. It was not run on Windows.Not measured on RDNA2:
ps_ssharpdid before.image_gather4_*andimage_get_lod.Unverified:
_c_lz, 1D/3D/array/cube images, anisotropic and trilinear filtering under a bias (the tests use point filters), and biases beyondmaxSamplerLodBias.No game files. AI-assisted.
Checklist
docs/design.md, with how it was confirmed.🤖 Generated with Claude Code