Skip to content

runtime, tools: select the LOD-bias fallback on KosmicKrisp too (TEST-918) - #4641

Open
wavehs wants to merge 2 commits into
ItsNotRexed:mainfrom
wavehs:test-918-kosmickrisp-lod-bias
Open

wavehs wants to merge 2 commits into
ItsNotRexed:mainfrom
wavehs:test-918-kosmickrisp-lod-bias

Conversation

@wavehs

@wavehs wavehs commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: TEST-918.

Depends on #1845 (SHADER-268, the shader-side LOD bias fallback itself). The PR diff therefore shows #1845's commit too: the change to review here is the last commit, 8e55d5d3 (git show 8e55d5d3).

What and why

KosmicKrisp, Mesa's Metal driver (driver ID 28), reports VkPhysicalDevicePortabilitySubsetFeaturesKHR::samplerMipLodBias as true, but 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 (#3124, Mesa 26.3.1, M3 Max). So ps_ssharp_lod fails natively there while the probe says the samplers take a bias.

#1845 selects --lod-bias-fallback from that portability-subset feature, so on KosmicKrisp it selects nothing, and its own known-problems row says to force LEONIDA_LOD_BIAS_FALLBACK=1 by hand. This PR closes that: the fallback is selected for KosmicKrisp by driver ID.

Changes (commit 8e55d5d3)

  • 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 like is_moltenvk, 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. 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.

Gone from the earlier version of this PR, on review: the runtime branch that claimed shaders handle the bias while nothing did, and the sampler_lod_bias flag inferred from a driver blacklist — the field and its MoltenVK meaning are #1845's, and it is only overridden for the one driver that measurably drops the bias.

Testing

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; cmake --build runtime/build -> 96/96 targets, clean.

ps_ssharp_lod passing with the fallback on KosmicKrisp is #3124's and #1845's measurement (M3 Max, Mesa 26.3.1); this change makes that selection automatic and was not re-run on a GPU here.

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A translator rule is in the contract table of docs/design.md, with how it was confirmed.

…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.
@tupacfan04

Copy link
Copy Markdown
Contributor

Request changes: the documented shader-side LOD-bias fallback does not exist, so this cannot fix ps_ssharp_lod.

Verified: tests/test_vk_subgroup_info.py 49 OK, docs tests 20, compileall clean, and the negative control is red on main's tool (11 failures + 1 error). Driver id 28 is correct (VK_DRIVER_ID_MESA_KOSMICKRISP).

But there is no LOD_BIAS handling in tools/rdna2spirv.py on main, and docs/design.md:357 says raw LOD comes from the driver sampler. With max_sampler_lod_bias = 0 (d3d12_vulkan_device.cpp:4176), mipLodBias becomes clamp(bias, 0, 0) = 0, so a non-zero sampler bias is silently dropped on KosmicKrisp and nothing applies it in shaders. The log at :4177 and docs/testing.md:406 overstate what happens.

Also, tools/vk_subgroup_info.py:333 reports sampler_lod_bias: False for MoltenVK with no evidence, and tests/test_vk_subgroup_info.py:826 asserts it while only mirroring the constant.

The conflict in tests/test_vk_subgroup_info.py (both add a method near :822) is trivial to rebase.

AI-assisted review, no game files, no GPU.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested head 61f2aef9 — Windows 11, CPython 3.15.2 + VS 2022 BuildTools, NVIDIA RTX 5090 (driver 617.42), no game files: pass (first test on this PR).

  • python -I tests/test_vk_subgroup_info.py -> 49/49 OK. Control (main's tools/vk_subgroup_info.py + this test file): 11 failures + 1 error — the new kosmickrisp/sampler_lod_bias fields discriminate as intended.
  • python -I tools/vk_subgroup_info.py live-probes the 5090 and reports the new fields correctly: "kosmickrisp": false, "sampler_lod_bias": true (NVIDIA supports sampler LOD bias, so no fallback applies — the KosmicKrisp-only path stays dark here by design).
  • cmake --build runtime/build: clean. Regression spot-check ctest -R "d3d12_vulkan_device|d3d12_present": 2/2 Passed (d3d12_vulkan_device 10.82s, d3d12_present 1.32s).
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

@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 d90394e03 on Windows 11: pytest tests/test_vk_subgroup_info.py -q -> 50 passed, 75 subtests passed; git diff --check clean.

vk_subgroup_info now recognises KosmicKrisp and reports its sampler-LOD-bias fallback, and the runtime enables the matching path for that driver; the added cases pin the detection and the non-KosmicKrisp path. I cannot run the KosmicKrisp driver here. No blocking issue.

@wavehs
wavehs force-pushed the test-918-kosmickrisp-lod-bias branch from d90394e to 8e55d5d Compare November 23, 2026 04:55
@wavehs wavehs changed the title runtime, tools: handle KosmicKrisp sampler LOD bias fallback (TEST-918) runtime, tools: select the LOD-bias fallback on KosmicKrisp too (TEST-918) Nov 23, 2026
@wavehs

wavehs commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

@tupacfan04 You are right on every point, and the PR is rebuilt around it.

The fallback the old version documented does exist — it is #1845 (SHADER-268), still open, which is why main's tools/rdna2spirv.py has no LOD_BIAS handling and why the runtime branch here could not do anything: with max_sampler_lod_bias = 0 from the device's own limit (d3d12_vulkan_device.cpp, state_.max_sampler_lod_bias), the assignment was a no-op and the log line claimed a shader-side fallback nothing implemented.

So this PR is now stacked on #1845 (Depends on #1845) and keeps only what #1845 leaves open:

Verified on this machine (M3 Pro, Homebrew Mesa), no game files: tools/vk_subgroup_info.py against the real KosmicKrisp driver answers "driver_id": 28, "kosmickrisp": true, "sampler_lod_bias": false (before: true), MoltenVK answers false; tests/test_vk_subgroup_info.py 46 OK; the runtime builds (96/96). The GPU pass of ps_ssharp_lod with the fallback is #3124's measurement, not re-run here.

Review only 8e55d5d3; the rest of the diff is #1845's commit.

…-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 force-pushed the test-918-kosmickrisp-lod-bias branch from 8e55d5d to dc68669 Compare November 23, 2026 05:02
@tupacfan04

Copy link
Copy Markdown
Contributor

PARTIAL: the fallback is real now, but the runtime never selects it.

At the head I checked (dc686697, newer than the d90394e0 I was pointed at), the translator fallback exists (tools/rdna2spirv.py:88,8118-8143,8641), docs/design.md:357 is rewritten, and docs/testing.md:332 matches. Tests pass: test_vk_subgroup_info.py 46 OK, docs 11, semantic LOD-bias 14 OK, compileall clean. MoltenVK evidence improved but is prose-only (the tests still simulate feature=false).

The blocking gap is the wiring: runtime/shims/d3d12_vulkan_device.cpp:7178 still sets info.sampler_mip_lod_bias directly from samplerMipLodBias and never calls sampler_mip_lod_bias(); is_kosmickrisp appears only in runtime/src/vulkan_portability.h:88. So on KosmicKrisp the runtime keeps a non-zero mipLodBias while make_game.py selects the shader fallback, double-applying the bias. runtime/test/vk_sampler_test.cpp:164 also omits properties2, so it cannot detect KosmicKrisp either.

The merge conflicts with current main 98fa503f in 12 files (including docs/design.md, make_game.py, tools/rdna2spirv.py, tools/vk_subgroup_info.py), so a rebase is needed anyway.

AI-assisted review, no game files, no game GPU.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested dc68669 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R "^(d3d12_descriptor|gfx10_descriptors|vk_pipeline_check_cli)$" (3/3 on each GPU) and the full run_gpu_tests.py (includes vk_sampler_test and the ps_ssharp shaders; Radeon forced with VK_LOADER_DRIVERS_SELECT=*amd*): Radeon 176/191 passed, 15 skipped, 0 failed; RTX 5080 145/191 passed, 46 skipped, 0 failed. This does not exercise the KosmicKrisp path itself. No game files.

AI-assisted.

@memorypressure

Copy link
Copy Markdown

Verified at the PR head: tests/test_vk_subgroup_info.py passes 46 under python -I, the new test_lod_bias_fallback passes under pytest (14 + 26 subtests; like its siblings it needs the pytest venv), and main's translator-use guard passes when the test is copied onto a current-main tree. However the branch no longer merges with current main — docs/d3d12_vulkan.md conflicts; a rebase is needed and will interact with #4642's same-tuple change. The vulkan_portability.h part and the KosmicKrisp Mesa measurement are unverified here (no cmake build, no Apple hardware). No CI checks reported. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@rgvxsthi

Copy link
Copy Markdown
Contributor

An Apple M5 (MacBook Air, macOS 26.6) with Homebrew Mesa KosmicKrisp (kosmickrisp_mesa_icd.aarch64.json) and MoltenVK is available to test this on the real driver. As far as I can see, no one has run the shaders on KosmicKrisp yet.

I'm holding off at dc686697 because the wiring gap @tupacfan04 raised is still there: runtime/shims/d3d12_vulkan_device.cpp:7178 sets info.sampler_mip_lod_bias from samplerMipLodBias directly instead of calling sampler_mip_lod_bias(). vk_sampler_test and d3d12_descriptor_test do call the helper, so they would pass on KosmicKrisp while the D3D12 runtime path still double-applies the bias.

Once the fix and the rebase onto main are pushed, I'll run these on the M5:

  • tools/vk_subgroup_info.py: expect kosmickrisp: true, sampler_lod_bias: false
  • vk_sampler_test, ps_ssharp_lod and ps_ssharp_ops through run_gpu_tests.py with the fallback auto-selected
  • a control run on main, where ps_ssharp_lod should fail

AI-assisted (Claude Code).

@DomasUrbonas

Copy link
Copy Markdown
Contributor

Independent check on Apple M4 Pro, macOS 27.2.1, comparing #1845 a71346f with #4641 dc68669. With LEONIDA_LOD_BIAS_FALLBACK unset, KosmicKrisp 26.2.99 passed 2/3 sampler tests on #1845 (ps_ssharp_lod failed at point bias +1) and 3/3 on #4641. The generated metadata confirmed automatic fallback selection on #4641.

Forcing the fallback on in #1845 made ps_ssharp_lod pass. Forcing it off in #4641 reproduced the same failure. MoltenVK 1.4.2 passed all three tests on both revisions. All generated shaders passed spirv-val, and all 46 device-probe unit tests passed. Native macOS tests only. Synthetic fixtures, no game files. 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.

9 participants