[PR #1497] runtime/d3d12: synthesize --bary-fallback geometry shader in graphics PSOs #1527

Open
opened 2026-11-26 20:21:33 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1497
Author: @robot00f
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: feat/rt-bary-fallback-pso


📝 Commits (1)

  • 53df7d1 runtime/d3d12: synthesize --bary-fallback geometry shader in graphics PSOs

📊 Changes

8 files changed (+1747 additions, -10 deletions)

View changed files

📝 runtime/CMakeLists.txt (+10 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+90 -10)
📝 runtime/src/shader_library.h (+10 -0)
➕ runtime/src/spirv_bary_fallback.h (+428 -0)
➕ runtime/test/d3d12_bary_fallback_test.cpp (+678 -0)
📝 runtime/test/shader_library_test.cpp (+34 -0)
➕ runtime/test/spirv_bary_fallback_test.cpp (+495 -0)
📝 runtime/test/vk_test_device.h (+2 -0)

📄 Description

Claimed in the coordination issue as: RT-BARY-FALLBACK-PSO (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7149404640)

What and why

On GPUs without VK_KHR_fragment_shader_barycentric (for example NVIDIA Pascal), rdna2spirv.py --bary-fallback emits a pixel shader's raw barycentrics as an ordinary perspective-interpolated vec3<f32> Input and records "bary_fallback_input": {"location": N} in spirv/index.json. Offline tools/bary_fallback_gs.py already specified the triangle pass-through geometry shader shape, and PipelineState::create_graphics already supported explicit geometry stages (#2109), rectangle-list geometry stages (#984) and per-pipeline --f16-interp-fallback linking (spirv_f16_halves.h), but ShaderLibrary did not read bary_fallback_input and create_graphics did not insert the pass-through geometry stage — leaving any graphics PSO with a --bary-fallback PS without a stage writing bary_location.

This PR wires --bary-fallback pixel shaders end to end into D3D12→Vulkan graphics PSO creation:

  1. runtime/src/shader_library.h: parses "bary_fallback_input": {"location": N} (exact non-negative uint32_t) into ShaderEntry::bary_fallback_location.
  2. runtime/src/spirv_bary_fallback.h: pure C++20 leonida::build_bary_fallback_gs(vs_words, ps_words, bary_location, gs_words, &info, &error) synthesizing a SPIR-V 1.5 Geometry module (Triangles in, Invocations 1, OutputTriangleStrip, OutputVertices 3) for the (VS, PS) pair:
    • copies BuiltIn Position across the three triangle vertices and writes (1, 0, 0), (0, 1, 0), (0, 0, 1) to Output vec3<f32> at bary_location;
    • forwards every vec4<f32> Location input the PS declares (other than bary_location) that the VS exports, leaves unread VS outputs unconsumed on VS -> GS (even if the VS exports an unread output at bary_location), and zero-fills any unexported PS Location input via OpConstantNull without declaring a dangling GS Input (VUID-RuntimeSpirv-OpEntryPoint-08743);
    • inspects the VS for BuiltIn ClipDistance or BuiltIn CullDistance (float[1..8]) and forwards it with the matching capability, rejecting mixed ClipDistance + CullDistance and ShaderLayer / ShaderViewportIndex.
  3. runtime/shims/d3d12_vulkan_device.cpp: in PipelineState::create_graphics and stage_words (StageSource::bary_fallback for lazy graphics pipeline builds), runs build_bary_fallback_gs after link_f16_halves on the post-link (vs, ps) words (clamping top_location against maxGeometryInputComponents / maxGeometryOutputComponents) so relocated/synthesized f16_halves locations are forwarded automatically through the GS. Refuses mesh PSOs, explicit desc.GS, rectangle-list PSOs, disabled geometryShader / shaderClipDistance / shaderCullDistance (E_NOTIMPL via stream_unsupported), and non-triangle PrimitiveTopologyType (E_INVALIDARG).
  4. Tests (runtime/CMakeLists.txt, runtime/test/vk_test_device.h, runtime/test/*):
    • extends shader_library_test with bary_fallback_input index parsing checks;
    • adds spirv_bary_fallback (spirv_bary_fallback_test.cpp), a portable pure-CPU test covering pass-through locations, unread VS locations, unexported PS Flat zero-fill, ClipDistance[2], CullDistance[3], combined link_f16_halves + build_bary_fallback_gs, and clean refusals;
    • enables shaderClipDistance and shaderCullDistance where available in vktest::Device and adds d3d12_bary_fallback (d3d12_bary_fallback_test.cpp), exercising real Vulkan GPU draws through the Xbox-ABI proxy.

Tested

  • 3 consecutive runs + eager & lazy graphics pipeline modes (ctest --output-on-failure -R "^(shader_library|spirv_bary_fallback|d3d12_bary_fallback)$", plus LEONIDA_LAZY_GRAPHICS=0 and LEONIDA_LAZY_GRAPHICS=1): 3/3 passed on all runs:
    • d3d12_bary_fallback verified all 256 pixels of: (1) a non-uniform-W (w = 1, 2, 4) triangle checking perspective-correct (b0, b1, b2) + pass-through Location 0 (center=(184,49,22,191) vs affine (128,64,64,191)), (2) ClipDistance[1] clipping ndc_x < 0 while preserving barycentrics on ndc_x > 0, (3) a 16-param VS + colliding f16_halves + bary_fallback_input PSO combining link_f16_halves relocation/synthesis with build_bary_fallback_gs, plus E_INVALIDARG / E_NOTIMPL refusal checks.
  • SPIR-V validation: dumped all 6 synthesized GS variants from spirv_bary_fallback_test --dump-dir (gs_basic, gs_unread_vs, gs_zero_fill, gs_clip2, gs_cull3, gs_f16_linked) and validated each with spirv-val --target-env vulkan1.2 and spirv-val --target-env vulkan1.3 (0 errors).
  • Neighbor tests: spirv_f16_halves, d3d12_f16_halves, d3d12_geometry, d3d12_translated_draw (4/4 passed).
  • Repo / policy checks: python -I scripts/check_no_game_data.py on all touched files, tests/test_bary_fallback_gs.py (29), tests/test_ctest_registration.py (5), tests/test_docs_links.py (16), tests/test_docs_table_rows.py (2), git diff --check.
  • Not tested: VK_LAYER_KHRONOS_validation is not installed on this Windows 10 host; no game shaders or executable were run.

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 (a conflicting pull request is labeled needs rebase).
  • 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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1497 **Author:** [@robot00f](https://github.com/robot00f) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/rt-bary-fallback-pso` --- ### 📝 Commits (1) - [`53df7d1`](https://github.com/ubiquitarianism/leonida/commit/53df7d1aee7c5fc53ee97a9ff320faa2aa7d13b2) runtime/d3d12: synthesize --bary-fallback geometry shader in graphics PSOs ### 📊 Changes **8 files changed** (+1747 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+10 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+90 -10) 📝 `runtime/src/shader_library.h` (+10 -0) ➕ `runtime/src/spirv_bary_fallback.h` (+428 -0) ➕ `runtime/test/d3d12_bary_fallback_test.cpp` (+678 -0) 📝 `runtime/test/shader_library_test.cpp` (+34 -0) ➕ `runtime/test/spirv_bary_fallback_test.cpp` (+495 -0) 📝 `runtime/test/vk_test_device.h` (+2 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-BARY-FALLBACK-PSO (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7149404640) ### What and why On GPUs without `VK_KHR_fragment_shader_barycentric` (for example NVIDIA Pascal), `rdna2spirv.py --bary-fallback` emits a pixel shader's raw barycentrics as an ordinary perspective-interpolated `vec3<f32>` `Input` and records `"bary_fallback_input": {"location": N}` in `spirv/index.json`. Offline `tools/bary_fallback_gs.py` already specified the triangle pass-through geometry shader shape, and `PipelineState::create_graphics` already supported explicit geometry stages (#2109), rectangle-list geometry stages (#984) and per-pipeline `--f16-interp-fallback` linking (`spirv_f16_halves.h`), but `ShaderLibrary` did not read `bary_fallback_input` and `create_graphics` did not insert the pass-through geometry stage — leaving any graphics PSO with a `--bary-fallback` PS without a stage writing `bary_location`. This PR wires `--bary-fallback` pixel shaders end to end into D3D12→Vulkan graphics PSO creation: 1. **`runtime/src/shader_library.h`**: parses `"bary_fallback_input": {"location": N}` (exact non-negative `uint32_t`) into `ShaderEntry::bary_fallback_location`. 2. **`runtime/src/spirv_bary_fallback.h`**: pure C++20 `leonida::build_bary_fallback_gs(vs_words, ps_words, bary_location, gs_words, &info, &error)` synthesizing a SPIR-V 1.5 `Geometry` module (`Triangles` in, `Invocations 1`, `OutputTriangleStrip`, `OutputVertices 3`) for the `(VS, PS)` pair: - copies `BuiltIn Position` across the three triangle vertices and writes `(1, 0, 0)`, `(0, 1, 0)`, `(0, 0, 1)` to `Output vec3<f32>` at `bary_location`; - forwards every `vec4<f32>` `Location` input the PS declares (other than `bary_location`) that the VS exports, leaves unread VS outputs unconsumed on `VS -> GS` (even if the VS exports an unread output at `bary_location`), and zero-fills any unexported PS `Location` input via `OpConstantNull` without declaring a dangling GS `Input` (`VUID-RuntimeSpirv-OpEntryPoint-08743`); - inspects the VS for `BuiltIn ClipDistance` or `BuiltIn CullDistance` (`float[1..8]`) and forwards it with the matching capability, rejecting mixed `ClipDistance` + `CullDistance` and `ShaderLayer` / `ShaderViewportIndex`. 3. **`runtime/shims/d3d12_vulkan_device.cpp`**: in `PipelineState::create_graphics` and `stage_words` (`StageSource::bary_fallback` for lazy graphics pipeline builds), runs `build_bary_fallback_gs` after `link_f16_halves` on the post-link `(vs, ps)` words (clamping `top_location` against `maxGeometryInputComponents` / `maxGeometryOutputComponents`) so relocated/synthesized `f16_halves` locations are forwarded automatically through the GS. Refuses mesh PSOs, explicit `desc.GS`, rectangle-list PSOs, disabled `geometryShader` / `shaderClipDistance` / `shaderCullDistance` (`E_NOTIMPL` via `stream_unsupported`), and non-triangle `PrimitiveTopologyType` (`E_INVALIDARG`). 4. **Tests (`runtime/CMakeLists.txt`, `runtime/test/vk_test_device.h`, `runtime/test/*`)**: - extends `shader_library_test` with `bary_fallback_input` index parsing checks; - adds `spirv_bary_fallback` (`spirv_bary_fallback_test.cpp`), a portable pure-CPU test covering pass-through locations, unread VS locations, unexported PS `Flat` zero-fill, `ClipDistance[2]`, `CullDistance[3]`, combined `link_f16_halves` + `build_bary_fallback_gs`, and clean refusals; - enables `shaderClipDistance` and `shaderCullDistance` where available in `vktest::Device` and adds `d3d12_bary_fallback` (`d3d12_bary_fallback_test.cpp`), exercising real Vulkan GPU draws through the Xbox-ABI proxy. ### Tested - **3 consecutive runs + eager & lazy graphics pipeline modes** (`ctest --output-on-failure -R "^(shader_library|spirv_bary_fallback|d3d12_bary_fallback)$"`, plus `LEONIDA_LAZY_GRAPHICS=0` and `LEONIDA_LAZY_GRAPHICS=1`): 3/3 passed on all runs: - `d3d12_bary_fallback` verified all 256 pixels of: (1) a non-uniform-W (`w = 1, 2, 4`) triangle checking perspective-correct `(b0, b1, b2)` + pass-through `Location 0` (`center=(184,49,22,191)` vs affine `(128,64,64,191)`), (2) `ClipDistance[1]` clipping `ndc_x < 0` while preserving barycentrics on `ndc_x > 0`, (3) a 16-param VS + colliding `f16_halves` + `bary_fallback_input` PSO combining `link_f16_halves` relocation/synthesis with `build_bary_fallback_gs`, plus `E_INVALIDARG` / `E_NOTIMPL` refusal checks. - **SPIR-V validation**: dumped all 6 synthesized GS variants from `spirv_bary_fallback_test --dump-dir` (`gs_basic`, `gs_unread_vs`, `gs_zero_fill`, `gs_clip2`, `gs_cull3`, `gs_f16_linked`) and validated each with `spirv-val --target-env vulkan1.2` and `spirv-val --target-env vulkan1.3` (0 errors). - **Neighbor tests**: `spirv_f16_halves`, `d3d12_f16_halves`, `d3d12_geometry`, `d3d12_translated_draw` (4/4 passed). - **Repo / policy checks**: `python -I scripts/check_no_game_data.py` on all touched files, `tests/test_bary_fallback_gs.py` (29), `tests/test_ctest_registration.py` (5), `tests/test_docs_links.py` (16), `tests/test_docs_table_rows.py` (2), `git diff --check`. - **Not tested**: `VK_LAYER_KHRONOS_validation` is not installed on this Windows 10 host; no game shaders or executable were run. 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1527
No description provided.