Skip to content

Separate 2D depth comparisons from ordinary color sampling on MoltenVK - #4616

Open
nschlaepfer wants to merge 5 commits into
ItsNotRexed:mainfrom
nschlaepfer:codex/separate-depth-comparison-2d
Open

nschlaepfer wants to merge 5 commits into
ItsNotRexed:mainfrom
nschlaepfer:codex/separate-depth-comparison-2d

Conversation

@nschlaepfer

@nschlaepfer nschlaepfer commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Scope

Claimed in coordination issue #4571. This AI-assisted opt-in change uses set 0 binding 29 for supported 2D depth-comparison samples. Bindings 26–28 remain available for the pending typed-resource proposals.

A synthetic shader samples RGBA from one array element and compares a separate D32 image at another. On Apple M3 Max the original module returned red in every color channel, even with constant, distinct indices. SPIRV-Cross at the revision pinned by MoltenVK 1.4.2 declared the shared array as Metal depth textures. Its ordinary sample returned a scalar and the generated Metal broadcast it to the output vector. Giving the depth view its own binding preserved color and both outcomes of the hardware depth comparison.

Build shaders with --separate-depth-2d and run with LEONIDA_SEPARATE_DEPTH_2D=1. Unflagged shaders retain their existing format. The runtime budgets the new array, clears its D32 fallback to zero, updates depth and color views by the same lookup index, and rejects a flagged PSO when the binding is disabled.

Verification

The public fixture uses an orange RGBA texture, a depth of 0.75 and a LESS sampler. The GPU test checks both comparison outcomes, erases and restores the depth key, and replaces a depth view with a color view. It compares 4,096 pixels on every draw.

  • Apple M3 Max: 12 draws, 49,152 exact pixels, zero Khronos validation errors. Includes update-after-bind/partially-bound and core fully populated descriptor sets.
  • Windows RTX 5090, NVIDIA 621.14: 12 draws, zero bad pixels, largest error zero. The Khronos validation layer was unavailable on Windows.
  • The existing bindless gradient and image-atomic controls passed on M3. Translator suite: 581 passed, four skipped. Metal offline layout and alias suites: 37 passed plus 41 subtests. The rewritten synthetic module passed spirv-val.
  • Native M3 CTest: 45 passed, two skipped, one failed. The same overlapping-occlusion test failed at the same assertion on the unmodified base.

Only the redistributable synthetic fixture and public sources are included.

  • The exact submitted head also passed the final 12-draw synthetic regression on the RTX 5090. A mapped-guest check of the ordinary opt-in runtime on both hosts matched the corrected reference across all five attachments in each of two controlled fixtures per host. With the runtime flag disabled, the flagged shader was refused at PSO creation.

The legacy Metal offline binding rewriter retains the earlier argument-buffer indices and rejects the three reserved slots. The new regression checks both color sampling and two genuinely different depth-comparison outcomes.

Dynamic-indexing capability. @whattheflop identified that the baseline translator can dynamically index a sampled texture or sampler array without declaring the corresponding SPIR-V capability. This also applies to the opt-in depth array when indexed dynamically. PR #3045 owns that general correction. Its author independently verified the capability and required device feature for binding 29 in the combined tree. I confirmed the two published heads merge cleanly. The pixel and validation-layer results above are observations for the tested drivers.

Upstream refresh. Merged main at 7b7d73f8 in 0512a0b3. The M3 opt-in path again matched all 49,152 pixels with validation enabled. Default shaders in four controls were byte-identical to the new base. Windows MSVC compiled the affected runtime object and linked the checker and bindless test tools. The general dynamic-indexing work in #3045 is still separate.

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

Built and ran the opt-in path at 17be8954 on Windows 11, RTX 5070 Ti (Vulkan 1.4.363), with LEONIDA_SEPARATE_DEPTH_2D=1 for the translator:

  • runtime/test/shaders/build.py out vs_ids ps_mixed_depth_2d -> 2 shaders translated, 2 pass spirv-val.
  • bindless_sizing_test -> exit 0, bindless sizing: synthetic limits gave the expected counts (the binding-29 array is budgeted in the 93,793 descriptors).
  • vk_bindless_test vs_ids.spv ps_mixed_depth_2d.spv --separate-depth-2d -> 12 draws read back, 0 bad pixels, largest error 0 — including both depth-comparison outcomes, the erased/restored depth key and the depth-view-replaced-by-colour draw.
  • pytest tests/test_spirv_metal_set0*.py -q -> 37 passed, 41 subtests passed.

The change keeps binding 29 opt-in (separate_depth_2d only when the flag is passed and the shader index names it), refuses a flagged PSO when the binding is disabled, and leaves unflagged shaders on their existing format. I could not re-run the Apple M3 MoltenVK side; the Windows/NVIDIA path is exact. git diff --check clean. No blocking issue.

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

AI-assisted Windows CPU review at 17be8954, uv CPython 3.12.13. Thirty hand-written shader builds validated for Vulkan 1.2 and 1.3, with maintenance8 enabled for the native offset case. Only supported 2D comparison samples moved to set 0 binding 29. Color remained at 0 and array comparison at 3. All five default controls were byte-identical to both d0835dd9 and the merge base 03b09a08.

A CPU harness loaded the actual generated index through ShaderLibrary. With opt-in enabled, a 1,212-descriptor synthetic budget allocated 256 entries each to bindings 0 and 29 and used 993 in total. The off layout had no binding 29. The offline Metal rewriter used argument-buffer ID 12545 for 29 and kept earlier IDs. Focused checks passed 137 Python tests, 110 subtests and five CPU CTests.

One existing translator contract carries over to the opt-in path: the fixture dynamically indexes binding 29 and its sampler array without SampledImageArrayDynamicIndexing. It passes offline validation, which does not settle VUID 10128. #3045 already tracks this capability and its current source includes both descriptor accesses. That composition was inspected at source level. No GPU dispatch was submitted in this review.

@rgvxsthi

Copy link
Copy Markdown

Tested 17be8954 merged onto main d0835dd9 on Apple M5 (MacBook Air, 10-core GPU), macOS 26.6 (25G72), with both MoltenVK 1.4.2 and KosmicKrisp (Mesa 26.3.1) from Homebrew, Apple clang 21, CMake 4.5.1, llvm-mc 23.1.6, Python 3.13.18: pass. A second Apple chip besides the M3 Max.

  • LEONIDA_SEPARATE_DEPTH_2D=1 python3 -I runtime/test/shaders/build.py <out> vs_ids ps_mixed_depth_2d: 2 translated, 2 pass spirv-val.
  • vk_bindless_test vs_ids.spv ps_mixed_depth_2d.spv --separate-depth-2d:
    • MoltenVK: 12 draws read back, 0 bad pixels, largest error 0
    • KosmicKrisp (LEONIDA_VULKAN_LOADER + VK_DRIVER_FILES): 12 draws read back, 0 bad pixels, largest error 0
  • Unflagged control, vk_bindless_test vs_ids.spv ps_tex.spv built without the flag: MoltenVK 8 draws, KosmicKrisp 24 draws, 0 bad pixels each (on MoltenVK the "no flags" variant reports skipped, the set does not fit this device's descriptor limits).
  • bindless_sizing_test: synthetic limits gave the expected counts.
  • pytest tests/test_spirv_metal_set0.py tests/test_spirv_metal_set0_aliases.py tests/test_spirv_metal_set0_extra.py: 37 passed, 41 subtests passed (same count as on main).

Not run: the full GPU suite on the PR head, and a negative control of the mixed fixture on main (the fixture needs the flag). Synthetic fixtures only, no game files. AI-assisted (Claude Code).

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

AI-assisted follow-up to my earlier review. I merged this exact head (17be8954) and #3045 (e34b0c7b) into a local scratch copy of upstream d0835dd9. Both merged without conflicts. Tested tree 778c321ba36e92037fba2327936463a692ef11a2.

With separate depth enabled, the mixed and compare-only fixtures used set 0 binding 29 and declared SampledImageArrayDynamicIndexing. Both carried shaderSampledImageArrayDynamicIndexing in required-feature metadata. Binding 29 was absent with the flag off and from the ordinary color and array-load controls. The texture-free control declared neither the capability nor the feature. All ten synthetic modules passed spirv-val for Vulkan 1.2 and 1.3.

The combined source routes both the image and sampler accesses through #3045’s tracker. The pipeline checker consults the required feature before module creation, and the D3D12 bindless device setup checks and enables that feature. On Windows with MSVC 19.38.33145 and Vulkan SDK 1.4.363.0, the checker and D3D12 COM shim built and the checker CLI, bindless sizing and shader library CTests passed (3/3). CPU and SPIR-V verification only. This shows that #3045 can supply the capability for the new path in this combination. Vulkan capability rule.

@Atomic258

Copy link
Copy Markdown

Tested head 17be8954 — Windows 11, VS 2022 BuildTools + Ninja, NVIDIA RTX 5090 (driver 617.42) + CPython 3.15.2, synthetic fixtures, no game files: pass (with a caveat on the GPU half).

  • cmake --build runtime/build: clean.
  • ctest -R "bindless|pipeline_check" --output-on-failure: bindless_sizing (new CPU test, test(semantic): add interpreter tests for SDWA source sub-register selection and broadcast #69) and vk_pipeline_check_cli both Passed.
  • The GPU bindless run inside vk_bindless_test is not exercised: its compute shader is built only where glslangValidator/llvm-mc exists — neither is on this box. The M5 run covered the MoltenVK path; the NVIDIA device path for the bindless set itself is still unverified anywhere.
  • pytest tests/test_spirv_metal_set0.py tests/test_spirv_metal_set0_aliases.py tests/test_spirv_metal_set0_extra.py: 37 passed. Control on main's tools/rdna2spirv.py + tools/spirv_metal_set0.py: 10 failed / 30 passed — the reworked _extra cases discriminate as intended.
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

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