Skip to content

Apply the S#'s LOD bias in the shader where samplers take none (SHADER-268) - #1845

Open
Jolymmiles wants to merge 1 commit into
ItsNotRexed:mainfrom
Jolymmiles:mac/shader-268-lod-bias-v2
Open

Jolymmiles wants to merge 1 commit into
ItsNotRexed:mainfrom
Jolymmiles:mac/shader-268-lod-bias-v2

Conversation

@Jolymmiles

@Jolymmiles Jolymmiles commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 ignores mipLodBias. With the portability subset enabled, a non-zero mipLodBias is 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_ssharp failed at "point bias +1". Under Wine the same bias is missing in the D3D12 path the game takes on a Mac: d3d12_descriptor failed at literal 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:

  • A 2D implicit PS image_sample, which goes through the typed path with Grad from the quad derivatives, gets those gradients times 2^bias. Any other implicit PS sample gets a Bias operand, and _b adds the S# bias to its own.
  • _l adds it to its LOD.
  • _lz, _c_lz and 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_cl scale their gradients by 2^bias (Grad takes no Bias).
  • image_get_lod queries at its coordinate times 2^bias, because OpImageQueryLod's λ′ includes the sampler's bias.
  • Gathers are unchanged: a Vulkan gather ignores the sampler's bias, which I measured.
  • A literal S# is folded; one without a bias translates as before. The index marks such shaders lod_bias_fallback.

Runtime: vulkan_sampler(…, 0) gives mipLodBias 0. The D3D12 device and vk_sampler_test read the portability subset's features (new runtime/src/vulkan_portability.h), enable VK_KHR_portability_subset where it is offered (VUID-04451), and zero the bias where the feature is missing.

Under Wine: winevulkan doesn't pass VK_KHR_portability_subset on to Windows programs, so a D3D12 device under Wine can't see the missing feature. It does pass the host driver's ID, so vulkan_portability.h gains is_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_test now 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 what ps_ssharp* test.

Selection: it works like the f16 interpolation fallback. tools/vk_subgroup_info.py reports sampler_lod_bias, and make_game.py and run_gpu_tests.py turn the flag on where it is false. LEONIDA_LOD_BIAS_FALLBACK=1/0 overrides it.

Tests:

  • New GPU tests: ps_ssharp_ops covers _l, _b, _d and _d_cl; ps_ssharp_lod covers image_get_lod.
  • Three more 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.
  • The contract-table row in docs/design.md, plus notes in docs/testing.md and docs/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:

config before after changes
MoltenVK 105/170, 64 skipped 108/172, 64 skipped, 0 failed ps_ssharp FAIL→PASS; ps_ssharp_ops, ps_ssharp_lod PASS
MoltenVK emu 141/170, 28 skipped 144/172, 28 skipped, 0 failed same
KosmicKrisp 119/170, 51 skipped 120/172, 51 skipped ps_ssharp_ops PASS, ps_ssharp_lod FAIL (see below)
KosmicKrisp emu 155/170, 15 skipped 156/172, 15 skipped same
llvmpipe 109/170, 61 skipped 111/172, 61 skipped both new tests PASS
llvmpipe emu 140/170, 30 skipped 142/172, 30 skipped same

No other test changed status in any configuration (per-test diff of the runner's output).

  • ps_ssharp_lod on KosmicKrisp without the flag: its OpImageQueryLod leaves the sampler's mipLodBias out 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. With LEONIDA_LOD_BIAS_FALLBACK=1 the test passes on KosmicKrisp as well. I kept the test rather than skip it there, and the KosmicKrisp behaviour is noted in docs/testing.md.
  • Forced flag: with LEONIDA_LOD_BIAS_FALLBACK=1, ps_ssharp, ps_ssharp_ops, ps_ssharp_lod and ps_get_lod pass on KosmicKrisp and llvmpipe, plain and with emu. So the shader-side bias matches the drivers' own sampler bias, applied before the clamp.
  • Without the flag the translation is unchanged: runtime/test/shaders/build.py output on 003e781 and on this branch is byte-identical. That covers 383 files plain and with LEONIDA_W64_EMULATE=1; index.json only gains the two new tests. It also covers 189 files with LEONIDA_F16_INTERP_FALLBACK=1 LEONIDA_TEXOFF_FALLBACK=1 and 191 with LEONIDA_BARY_FALLBACK=1, where ps_ij_computed, which those fallbacks reject, is left out.
  • Validation layer: Khronos 1.4.357, MoltenVK through the loader. A vk_sampler_test that still passes the S# bias reports 04467; this branch's doesn't.
  • Under Wine (rebuilt with clang-cl + xwin, CrossOver 26.3's loader, MoltenVK 1.4.3): d3d12_descriptor gets 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.
  • ctest: 33/33, before and after.
  • Python: tests/semantic OK, regen_fixtures.py --check OK. test_shader_checks, test_vk_subgroup_info, test_make_game_argparse and test_make_game_translator_flags pass, plus pytest test_mesh_compute.py and test_translate.py (499 passed). compileall is clean.
  • Windows build: the D3D12 part (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:

  • That LOD_BIAS reaches explicit LODs and applies before the clamp. This follows radv's mapping, as ps_ssharp did before.
  • Whether RDNA2 applies the bias to image_gather4_* and image_get_lod.
  • Implicit samples outside the PS.

Unverified: _c_lz, 1D/3D/array/cube images, anisotropic and trilinear filtering under a bias (the tests use point filters), and biases beyond maxSamplerLodBias.

No game files. AI-assisted.

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

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

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.

@umnitus

umnitus commented Nov 22, 2026

Copy link
Copy Markdown

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.

@Jolymmiles

Copy link
Copy Markdown
Contributor Author

Rebased onto 4947579 (head fc793ad) and re-verified on the same Apple M3 Max:

  • GPU (passed/total, base → branch): MoltenVK 113/185 → 116/187, MoltenVK+emu 149/185 → 152/187, KosmicKrisp 128/185 → 129/187, KosmicKrisp+emu 164/185 → 165/187, llvmpipe 117/185 → 119/187, llvmpipe+emu 148/185 → 150/187. The only per-test changes are ps_ssharp FAIL→PASS on MoltenVK, the two new tests passing, and ps_ssharp_lod failing natively on KosmicKrisp (the driver's OpImageQueryLod leaves out the sampler's bias, documented in docs/testing.md).
  • Byte identity without the flag: all 204 modules shared with the base are byte-identical in six translator modes (203 under the f16/bary fallbacks), and index.json only gains the two new tests.
  • Rebase fix: upstream now allows an S# given as a register list such as [s20,s21,vcc_lo,vcc_hi] (bda1775). ssharp_lod_bias reads word 2 through sgpr_word for it, with a semantic test that fails without the fix.
  • CPU and build: semantic suite 2577 OK, pytest 550 passed, and the D3D12 targets cross-compile with clang-cl + xwin.

AI-assisted.

@Jolymmiles
Jolymmiles force-pushed the mac/shader-268-lod-bias-v2 branch from fc793ad to ab651e4 Compare November 22, 2026 17:50
@Jolymmiles

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 responding on behalf of Jolymmiles

@Vonsek, @umnitus: fixed in ab651e4, the way the review suggested. With both flags, texel_offset_fold now runs the implicit image_sample_o / _b_o query OpImageQueryLod at the coordinate times 2^bias, as image_get_lod does, so the device applies the min/max LOD clamp after the S#'s bias. Before, the bias was added to the result after the clamp. The S# is wave-uniform, so scaling the coordinate leaves the quad's derivatives exact. _b's own bias is a VGPR that can vary across the quad, so it is still added to the result, as it is without the flag.

Reproduced on GPU, Apple M3 Max. A probe samples image_sample_o and image_sample_b_o with offset (−1, 0) on ps_ssharp's 128/64 texture. It runs over SAMPLER_CASES plus the two MAX_LOD 0.25 cases, with both flags. In the bias cases the old fold scaled the offset by mip 1's size while the sample read mip 0, so pixel (32,0) read the wrong quadrant:

  • MoltenVK 1.4.2 and KosmicKrisp: "bias +1, MAX_LOD 0" and "bias +1, MAX_LOD 0.25" failed before the fix and pass after, three runs each.
  • llvmpipe: "bias +1, MAX_LOD 0.25" is fixed. The two MIN_LOD = MAX_LOD = 0 cases still fail, and they fail the same way with only --texel-offset-fallback and the bias in the sampler. That is the known llvmpipe OpImageQueryLod quirk with a 0..0 range, the one SAMPLER_LOD_CASES already avoids.

test_lod_bias_fallback.py gains TexelOffsetFallback, which checks the query's coordinate scale, nothing added after the extract, _b_o's own bias and the Grad scale. It fails on fc793ad. The contract row in docs/design.md describes the fix.

Still open, and the same with or without this flag: the fold ignores the sampler's [minLod, maxLod] for _l_o, _lz_o and _c_lz_o, and _b_o's own bias still lands after the clamp. Both are limitations of --texel-offset-fallback itself. I'll post them as a finding instead of growing this PR.

Re-verification of ab651e4 (base 4947579, unchanged):

  • GPU (passed/total, base → branch): MoltenVK 113/185 → 116/187, MoltenVK+emu 149/185 → 152/187, KosmicKrisp 128/185 → 129/187, KosmicKrisp+emu 164/185 → 165/187, llvmpipe 117/185 → 119/187, llvmpipe+emu 148/185 → 150/187. The only per-test changes are the expected ones: ps_ssharp FAIL→PASS on MoltenVK; on KosmicKrisp ps_ssharp_ops PASS and ps_ssharp_lod FAIL (the driver bug documented in docs/testing.md); both new tests pass elsewhere. MoltenVK with LEONIDA_TEXOFF_FALLBACK=1 gives the same diff, and ps_texoff / ps_texoff_implicit pass.
  • Byte identity without the flag: all 204 shared modules are byte-identical in six modes (203 under f16, 206 under bary). index.json differs only by ps_ssharp_ops and ps_ssharp_lod.
  • CPU: semantic suite 2581 OK, regen_fixtures --check OK, pytest 580 passed. The D3D12 targets cross-build with clang-cl + xwin.

…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.
@Jolymmiles
Jolymmiles force-pushed the mac/shader-268-lod-bias-v2 branch from ab651e4 to a71346f Compare November 22, 2026 18:17
@Jolymmiles

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 responding on behalf of Jolymmiles

Rebased onto ec8ec4c (head a71346f) and re-verified on the same Apple M3 Max.

  • Conflicts: two files.
  • GPU (passed/total, base → branch): MoltenVK 116/189 → 119/191, MoltenVK+emu 152/189 → 155/191, KosmicKrisp 132/189 → 133/191, KosmicKrisp+emu 168/189 → 169/191, llvmpipe 120/189 → 122/191, llvmpipe+emu 151/189 → 153/191. Only the expected per-test changes:
    • ps_ssharp FAIL→PASS on MoltenVK;
    • both new tests pass, except ps_ssharp_lod natively on KosmicKrisp (its OpImageQueryLod driver bug, in docs/testing.md).
  • Byte identity without the flag: all 214 modules shared with the base are byte-identical in six translator modes (213 under the f16/bary fallbacks). index.json differs only by ps_ssharp_ops and ps_ssharp_lod.
  • CPU and build: semantic suite 4151 OK, regen_fixtures --check OK, pytest 595 passed. The D3D12 targets cross-build with clang-cl + xwin.

@FaKL-Code

Copy link
Copy Markdown
Contributor

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

  • Clean configure + build of runtime/ (Visual Studio generator, RelWithDebInfo): no new warnings (same C4201/C4456/C4996 counts as main).
  • ctest -C RelWithDebInfo -j 4: 116/116 passed (loader run on its own; 7 skipped for missing inputs or optional components, gameinput disabled, as on main).
  • command_list, d3d12_com, d3d12_vulkan_device, d3d12_fence_timing, d3d12_descriptor, d3d12_compute and d3d12_state_map also pass with LEONIDA_VK_VALIDATION=1.
  • This host is not the MoltenVK target the change is aimed at, so the run only shows that the Windows/NVIDIA path does not regress.

AI-assisted (Claude).

@Gabzinnn12

Copy link
Copy Markdown

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.

  • run_gpu_tests.py: 177/191 passed, 14 skipped. The new fixtures pass: ps_ssharp_lod (11 S#/T# cases, 4096 pixels each within 1/16) and ps_ssharp_ops (11 cases, 4096 pixels each exact); ps_ssharp, now 11 cases (8 on main), also exact. These run through vk_sampler_test. Main ec8ec4c on this machine: 175/189, the same 14 skipped.
  • d3d12_descriptor_test, d3d12_vulkan_device, d3d12_com, d3d12_draw, d3d12_compute, d3d12_m2_textured, d3d12_m3_frame, d3d12_m4_compute, d3d12_translated_draw, d3d12_buffer_uav: exit 0 with and without LEONIDA_VK_VALIDATION=1 (+ VK_LAYER_VALIDATE_SYNC=1), no Vulkan validation errors reported.
  • ctest: 0 failed out of 129 (122 passed, 7 skipped, gameinput disabled), including gfx10_descriptors.

AI-assisted (Claude Code, reviewed with Codex). No game files.

@bobcatchris

Copy link
Copy Markdown
Contributor

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.

python -I scripts/run_gpu_tests.py --filter ps_ssharp --build runtime/build/linux: 4 shaders translated, 4 pass spirv-val; 3/3 passed — ps_ssharp "11 S#/T# cases, 4096 pixels each exact", ps_ssharp_lod "11 S#/T# cases, 4096 pixels each within 1/16", ps_ssharp_ops "11 S#/T# cases, 4096 pixels each exact". Both bias cases that failed on unpatched main now pass, which confirms the MoltenVK samplerMipLodBias = false diagnosis in the PR body.

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.

Jolymmiles added a commit to Jolymmiles/Leonida that referenced this pull request Nov 22, 2026
…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.
wavehs added a commit to wavehs/Leonida that referenced this pull request Nov 23, 2026
…-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.
@wavehs

wavehs commented Nov 23, 2026

Copy link
Copy Markdown

Heads-up for whoever lands this: #4641 is now stacked on it (Depends on: #1845) and adds only the KosmicKrisp selection, because this PR's sampler_lod_bias comes from the portability subset's samplerMipLodBias feature and KosmicKrisp reports that feature as true while its OpImageQueryLod drops the bias (#3124, Mesa 26.3.1). As it stands, the known-problems row here asks for LEONIDA_LOD_BIAS_FALLBACK=1 by hand on that driver.

The delta is two hunks, if you would rather have it here than in a stacked PR:

  • tools/vk_subgroup_info.py: sampler_lod_bias false for driver ID 28 (VK_DRIVER_ID_MESA_KOSMICKRISP), plus a kosmickrisp field.
  • runtime/src/vulkan_portability.h: sampler_mip_lod_bias() false for driver ID 28 (is_kosmickrisp), by the ID like is_moltenvk.

Measured here (M3 Pro, Homebrew Mesa, no game files): tools/vk_subgroup_info.py against the real KosmicKrisp driver answers "driver_id": 28 and, with the delta, "sampler_lod_bias": false (before it: true). tests/test_vk_subgroup_info.py 46 OK, runtime builds 96/96. No objection if you fold it in and #4641 gets closed — the GPU pass of ps_ssharp_lod with the flag is #3124's measurement in either case.

wavehs added a commit to wavehs/Leonida that referenced this pull request Nov 23, 2026
…-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.
@bobcatchris

Copy link
Copy Markdown
Contributor

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 LEONIDA_LOD_BIAS_FALLBACK=0 A/B; no regression.

  • --filter ssharp, auto-selected on this driver: ps_ssharp 4096 pixels exact, ps_ssharp_ops exact, ps_ssharp_lod within 1/16 — 3/3 PASS. Forced LEONIDA_LOD_BIAS_FALLBACK=1 is identical.
  • Same three tests with LEONIDA_LOD_BIAS_FALLBACK=0: all three FAIL at "point bias +1" (ps_ssharp reads 1.0,0.0,0.502,0.502 where 0.502,0.502,0.502,0.502 is wanted; ps_ssharp_lod reads 1.0,1.0,1.0,2.0 where 1.0,2.0,1.0,3.0 is wanted). That confirms the flag is what fixes them here, not some other driver change.
  • Full suite with the flag: 119/191 passed, 72 skipped, 0 failed — completely green on this MoltenVK device. Without the flag: 116/191, 72 skipped, 3 failed, and the only three failures are those S# tests. No other test changed status.

Comparison with Jolymmiles' Apple M3 Max result on this PR: same shape — ps_ssharp FAIL→PASS on MoltenVK, the two new fixtures pass, and the per-test delta is exactly +3. KosmicKrisp is not installed on this M2, so I cannot reproduce the M3-only ps_ssharp_lod native-driver OpImageQueryLod finding; here the test passes both natively and with the flag. llvmpipe was not run.

Build caveat: the PR's base ec8ec4c9 does not fully build with AppleClang 17 — runtime/test/vk_pipeline_check.cpp:567 instantiates std::from_chars for double, which this SDK deletes. That file is untouched by this PR and current upstream/main 98fa503f builds cleanly, so it is a base-version issue. I built the six GPU-suite targets explicitly (vk_compute_test, vk_harness, vk_store_test, vk_texture_test, vk_sampler_test, vk_bindless_test); all built and ran. No code changes of my own.

No game data; synthetic fixtures and counts only.

AI-assisted.

@EvgHaze

EvgHaze commented Nov 23, 2026

Copy link
Copy Markdown

Tested a71346ff on Apple M3 (10-core), macOS 27.0 arm64 — the case this PR is about (Metal samplers take no LOD bias).

MoltenVK 1.4.2 — run_gpu_tests.py --filter ps_ssharp: 3/3.

Test main (98fa503) this PR
ps_ssharp FAIL (point bias +1) PASS, 11 S#/T# cases, 4096 pixels each exact
ps_ssharp_lod (not on main) PASS, 11 S#/T# cases, within 1/16
ps_ssharp_ops PASS PASS

KosmicKrisp 26.3.1 (Mesa 26.3.1, through the Vulkan loader) — this is the driver in #3124, where OpImageQueryLod ignores the sampler's mipLodBias:

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 main (220 .asm fixtures against 246 on 98fa503), and here 17 wave32/DPP fixtures that pass on main skip with SKIP (the device can't run this shader at 32 lanes on request) — MoltenVK lists no requiredSubgroupSizeStages. Measured 119/191 passed and 72 skipped, which is not like-for-like with main's 152/217, so take the per-test table above as the result, not the totals. A rebase should clear it.

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.

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.

8 participants