Skip to content

vk_pipeline_check: synthesize --bary-fallback GS stage for PS pipelines (VK-PIPELINE-CHECK-BARY-FALLBACK) - #1890

Open
robot00f wants to merge 2 commits into
ProjectLeonida:mainfrom
robot00f:feat/vk-pipeline-check-bary-fallback
Open

robot00f wants to merge 2 commits into
ProjectLeonida:mainfrom
robot00f:feat/vk-pipeline-check-bary-fallback

Conversation

@robot00f

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: VK-PIPELINE-CHECK-BARY-FALLBACK (#14 (comment); continued in #1608)

Depends on: #1497

What and why

When tools/rdna2spirv.py --bary-fallback translates a pixel shader (for GPUs without VK_KHR_fragment_shader_barycentric, such as NVIDIA Pascal, or drivers with broken BaryCoordKHR), the Fragment SPIR-V reads perspective barycentrics from a smooth vec3<f32> Input recorded under "bary_fallback_input": {"location": N} in index.json, while "inputs" lists only the vec4<f32> user param locations exported by <spirv dir>/stubs/vs_<inputs>.spv (tools/spirv_stubs.py).

Previously runtime/test/vk_pipeline_check.cpp ignored "bary_fallback_input" and built a 2-stage VS_stub + PS graphics pipeline (runtime/test/vk_pipeline_check.cpp:1748-1764), which:

  1. Left Location N unwritten by the preceding stage (VUID-RuntimeSpirv-OpEntryPoint-08743);
  2. Skipped checking geometryShader (shaderClipDistance / shaderCullDistance); and
  3. Seeded a 2-stage pipeline entry into pipeline_cache.bin that misses the runtime's 3-stage VS + synthesized GS + PS pipeline (runtime/d3d12: synthesize --bary-fallback geometry shader in graphics PSOs #1497).

Changes:

  • runtime/test/vk_pipeline_check.cpp:
    • Parses "bary_fallback_input" in JsonParser into ShaderMeta::bary_fallback_location (accepting null or {"location": 0..31} and rejecting non-objects, missing "location", or out-of-range locations before loading Vulkan).
    • Caches each partner stub's SPIR-V words alongside its VkShaderModule in stub_cache without re-reading disk per shader.
    • Skips --bary-fallback ps entries with no geometryShader when f2.features.geometryShader is unavailable; otherwise synthesizes the triangle pass-through VK_SHADER_STAGE_GEOMETRY_BIT module in memory via leonida::build_bary_fallback_gs (runtime/src/spirv_bary_fallback.h), gates forwarded VS ClipDistance / CullDistance on shaderClipDistance / shaderCullDistance before vkCreateShaderModule, compiles the 3-stage VS + GS + PS graphics pipeline, and fails the check (PIPELINE FAIL, exit 1) if build_bary_fallback_gs rejects an incompatible VS/PS interface.
  • Tests & docs: Adds CTest vk_pipeline_check_bary_fallback (runtime/test/check_vk_pipeline_bary_fallback.py, pure Python stdlib SPIR-V fixtures so Windows/Linux/macOS CI need no llvm-mc), index parser tests in tests/test_vk_pipeline_check_index.py, and docs in docs/pipeline_check.md.

Tested

  • Before vs after on ps_bary.asm (--bary-fallback, VK_LOADER_LAYERS_DISABLE=~implicit~, __GL_SHADER_DISK_CACHE=0):
Check Before (2-stage VS_stub + PS) After (3-stage VS_stub + bary_GS + PS)
Stage interface link (tools/shader_link_check.py) Incompatible (Location 1 unwritten by vs_0.spv, VUID-RuntimeSpirv-OpEntryPoint-08743) 1 compatible, 0 incompatible, 0 invalid (2 links checked)
3-stage pipeline seeded from old 2-stage cache (--cache-feedback) n/a 0 hits, 1 no hit reported (confirms 2-stage cache missed 3-stage pipeline)
3-stage pipeline seeded from new 3-stage cache (--cache-feedback) n/a 1 hits, 0 no hit reported
  • CTest (3/3 consecutive passes on GTX 1050 Ti):
    • vk_pipeline_check_bary_fallback, vk_pipeline_check_cli, pipeline_cache_seed, vk_pipeline_check_stack, spirv_bary_fallback, d3d12_bary_fallback — all PASS 3/3
  • Python & repository checks:
    • python -I runtime/test/check_vk_pipeline_bary_fallback.py -v — PASS (valid 3-stage VS + GS + PS compile + cache write, incompatible vec4 bary_fallback_input rejected with PIPELINE FAIL exit 1, malformed index.json bary_fallback_input rejected with exit 2)
    • python -I tests/test_vk_pipeline_check_index.py -v — 13/13 PASS
    • python -I tests/test_ctest_registration.py, tests/test_docs_links.py, tests/test_docs_table_rows.py, tests/test_text_write_line_endings.py — PASS
    • python -I scripts/check_no_game_data.py on all touched files — PASS
  • Not tested: Full retail shader corpus (no game files used) and a device without geometryShader (MoltenVK; covered by CI's macos job).

GPUs tested: NVIDIA GeForce GTX 1050 Ti (Pascal, 4 GB), driver 582.28, Vulkan 1.4.312, Windows 10 Pro 10.0.19045

AI-assisted: yes (Antigravity)

Checklist

  • Based on current main and merges cleanly (rebased onto runtime/d3d12: synthesize --bary-fallback geometry shader in graphics PSOs #1497, which is rebased onto current main).
  • One topic; follow-up work goes in a new pull request.
  • 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 (n/a: vk_pipeline_check checker change, no translator change).

@madvillain91 madvillain91 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed diff and independently verified commit 6a707a00d (including stacked #1497 commit 483f0128e) on Windows 11 with an NVIDIA Ampere GPU (against main 71a769d7d):

Environment

  • OS: Windows 11 26H2 (build 26300.9711)
  • Python: CPython 3.12.3 (python -I)
  • Compiler: VS 2022 Build Tools (MSVC 19.44.35230.0, Ninja, RelWithDebInfo)
  • Vulkan SDK: 1.4.363.0
  • GPU: NVIDIA GeForce RTX 3070 Ti (Ampere, 8192 MiB), driver 617.14, Vulkan 1.4.351

Checks run

  • git diff --check across both commits (483f0128e, 6a707a00d): clean
  • Built spirv_bary_fallback_test, shader_library_test, d3d12_bary_fallback_test, vk_pipeline_check, and pipeline_cache_seed_test cleanly in an isolated build: 0 errors
  • spirv_bary_fallback_test: PASS
  • shader_library_test: PASS (SHA-256, inflate, JSON, FSSM extraction, FSPX programs and index lookup OK)
  • d3d12_bary_fallback_test (plain & LEONIDA_VK_VALIDATION=1): PASS, 0 VUIDs (ps_bary (perspective w=1,2,4): center=(184,49,22,191) OK, ps_bary + ClipDistance[1]: left half clipped, right half interpolated OK, ps_bary_f16 (combined f16_halves relocation + bary GS): all 256 pixels OK)
  • pipeline_cache_seed_test <vk_pipeline_check.exe> (LEONIDA_VK_VALIDATION=1): PASS, 0 VUIDs (ok: vk_pipeline_check cache hits on the runtime's device (NVIDIA GeForce RTX 3070 Ti))
  • python -I runtime/test/check_vk_pipeline_bary_fallback.py --ctest --exe <vk_pipeline_check.exe> (plain & LEONIDA_VK_VALIDATION=1): 6/6 PASS in both modes (~5.7s)
  • python -I tests/test_vk_pipeline_check_index.py -v (with LEONIDA_VK_PIPELINE_CHECK set): 13/13 PASS (1.62s)

Diff notes

  • parse_bary_fallback_input() cleanly validates "bary_fallback_input" (null or object with unsigned "location" < 32) before loading Vulkan.
  • stub_cache (std::map<std::string, StubEntry>) preserves reference stability across concurrent worker insertions, and gs_mod is always destroyed after pipeline creation.
  • geometryShader, shaderClipDistance, and shaderCullDistance feature gates match the runtime path in d3d12_vulkan_device.cpp.

Not checked

  • Corpus run with translated --bary-fallback shaders, non-NVIDIA hardware, or a GPU lacking geometryShader / VK_KHR_fragment_shader_barycentric.

LGTM from this Windows 11 + NVIDIA RTX 3070 Ti verification (AI-assisted verification; synthetic test fixtures only, no game data).

@Mby159

Mby159 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

The new bary-fallback test script passes on an AMD Radeon 780M

The checker's new bary-fallback path builds a real pipeline on real hardware here, with synthetic fixtures: AMD Radeon 780M Graphics (RDNA3 iGPU), Windows 11, driver 32.0.32015.12012, Vulkan apiVersion 1.4.359.

Built from this PR's tree (6a707a00d8, base 71a769d7d8) with llvm-mingw clang++ 23.1.6 - this machine has no MSVC and no Windows SDK, and the build was configured with LEONIDA_BUILD_WINDOWS_RUNTIME=OFF. That is not the toolchain upstream CI uses.

  • spirv_bary_fallback_test.exe (the new pure-CPU test for runtime/src/spirv_bary_fallback.h): exits 0.
  • python -I runtime/test/check_vk_pipeline_bary_fallback.py --ctest --exe <build>/vk_pipeline_check.exe (run directly, not through CTest): 6 tests, all ok, none skipped. One verbose run of that script lists the six as test_invalid_bary_fallback_input_rejected_at_index_parse, test_null_and_unknown_keys_in_bary_fallback_input_parse_cleanly, test_synthetic_modules_validate_with_spirv_val, test_valid_bary_fallback_ps_compiles_with_synthesized_gs_and_writes_cache, test_bary_fallback_clip_distance_forwarding_and_unexported_ps_input_zerofill, test_broken_bary_fallback_interface_fails_pipeline_check.
  • python -I tests/test_vk_pipeline_check_index.py: 13 tests OK, exit 0 (that file covers the new bary_fallback_input validation).

What that means on this card: the checker built the synthesized VS + GS + PS pipeline and this driver accepted it. Running the checker on the same synthetic corpus gives ps: 1 modules ok, 0 failed, ps pipelines: 1 ok, 0 failed, 0 skipped, no SKIPPED and no PIPELINE FAIL, and a 7404-byte pipeline_cache.bin. Test 4 asserts the same 1 ok for the run seeded with that cache and under --isolate, and test 6 asserts that a deliberately broken VS/PS interface fails the check with exit 1, directly and under --isolate.

Scope of what ran: tests 5 and 6 skip on a device without geometryShader; test 4 instead asserts the skip line, and none of them skipped here - this device compiled the script's own GS probe. Only the new CPU test and this new script were run; the full GPU suite was not re-run with this PR's checker.

This exercises the checker half only. The runtime/D3D12 half - the #1497 dependency, d3d12_bary_fallback_test.cpp and the runtime/shims/d3d12_vulkan_device.cpp change - was not built or run here, so the runtime's behaviour with a --bary-fallback PSO is not exercised by this run. This card does report barycentric_fallback: false from tools/vk_subgroup_info.py, so the runtime would not ask for --bary-fallback here; what runs on it is the checker's synthesis path.

AI-assisted (MiniMax Code). No game files.

… PSOs

When a translated pixel shader carries `"bary_fallback_input": {"location": N}`
in `spirv/index.json` (`rdna2spirv.py --bary-fallback` for GPUs without
`VK_KHR_fragment_shader_barycentric`), `PipelineState::create_graphics` now
synthesizes the triangle pass-through Geometry SPIR-V 1.5 module in memory
(`leonida::build_bary_fallback_gs` in `runtime/src/spirv_bary_fallback.h`)
after `--f16-interp-fallback` VS/PS location linking:

- Parses `bary_fallback_input.location` in `ShaderLibrary::load_index` into
  `ShaderEntry::bary_fallback_location`.
- Copies `BuiltIn Position` and every `vec4<f32>` `Location` input read by the
  PS (other than `bary_location`) that the VS exports, while zero-filling any
  unexported PS `Location` input without declaring a dangling GS `Input` and
  leaving unread VS outputs unconsumed (`VUID-RuntimeSpirv-OpEntryPoint-08743`).
- Forwards a VS `BuiltIn ClipDistance` or `BuiltIn CullDistance` `float[1..8]`
  array (checking `shaderClipDistance` / `shaderCullDistance`), and emits
  `(1, 0, 0)`, `(0, 1, 0)`, `(0, 0, 1)` at `bary_location` across the 3 strip
  vertices so perspective interpolation matches `BaryCoordKHR`.
- Supports both eager and lazy graphics pipeline builds (`StageSource::bary_fallback`
  in `stage_words`), clamps `--f16-interp-fallback` `top_location` against
  `maxGeometryInputComponents` / `maxGeometryOutputComponents`, and refuses
  mesh PSOs, explicit GS or rectangle-list stages, missing `geometryShader`, or
  non-triangle topologies with clear HRESULTs (`E_NOTIMPL` / `E_INVALIDARG`).
- Adds `spirv_bary_fallback` (portable CPU test) and `d3d12_bary_fallback`
  (end-to-end Vulkan GPU D3D12 PSO test).

Claim: RT-BARY-FALLBACK-PSO
AI-assisted: yes (Antigravity)
When `tools/rdna2spirv.py --bary-fallback` translates a pixel shader (for GPUs
without `VK_KHR_fragment_shader_barycentric` or drivers with broken
`BaryCoordKHR`), the emitted Fragment SPIR-V reads perspective barycentrics from
an ordinary smooth `vec3<f32>` `Input` recorded under
`"bary_fallback_input": {"location": N}` in `index.json`, while `"inputs"` lists
only the `vec4<f32>` user `param` locations exported by `stubs/vs_<inputs>.spv`.
Previously `vk_pipeline_check` ignored `"bary_fallback_input"` and built a
2-stage `VS_stub + PS` pipeline, leaving `Location N` unwritten by the preceding
stage (`VUID-RuntimeSpirv-OpEntryPoint-08743`), omitting the `geometryShader` /
`shaderClipDistance` / `shaderCullDistance` feature gates, and writing a 2-stage
entry into `pipeline_cache.bin` that misses the runtime's 3-stage `VS + GS + PS`
pipeline key.

- Parse `"bary_fallback_input"` in `vk_pipeline_check`'s `JsonParser` into
  `ShaderMeta::bary_fallback_location` (accepting `null` or an object with an
  unsigned 32-bit `"location" < 32`, rejecting non-objects, missing `"location"`,
  and out-of-range locations with exit code 2 before loading Vulkan).
- Cache each loaded partner stub's SPIR-V words alongside its `VkShaderModule`
  in `stub_cache` without re-reading disk per shader.
- Skip `--bary-fallback` `ps` entries with `no geometryShader` when the selected
  device lacks `geometryShader`; otherwise synthesize the triangle pass-through
  `VK_SHADER_STAGE_GEOMETRY_BIT` module in memory via
  `leonida::build_bary_fallback_gs` (`runtime/src/spirv_bary_fallback.h`), gate
  forwarded VS `ClipDistance` / `CullDistance` on `shaderClipDistance` /
  `shaderCullDistance` before creating the GS module, compile the 3-stage
  `VS + GS + PS` graphics pipeline, and fail the check (`PIPELINE FAIL`, exit 1)
  if `build_bary_fallback_gs` rejects an incompatible `VS`/`PS` interface.
- Add CTest `vk_pipeline_check_bary_fallback`
  (`runtime/test/check_vk_pipeline_bary_fallback.py`), index parser cases in
  `tests/test_vk_pipeline_check_index.py`, and documentation in
  `docs/pipeline_check.md`.

Depends on: ProjectLeonida#1497
Claim: ProjectLeonida#14 (comment)
AI-assisted: yes (Antigravity)
@robot00f
robot00f force-pushed the feat/vk-pipeline-check-bary-fallback branch from 13b1ff9 to 79a2437 Compare November 26, 2026 04:41
@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 28, 2026
@MoonEyes

Copy link
Copy Markdown
Collaborator

Hi @robot00f, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/test/vk_pipeline_check.cpp), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants