Skip to content

runtime: forward mixed clip and cull distances in the fallback geometry shader - #2304

Draft
ecox982745 wants to merge 8 commits into
ProjectLeonida:mainfrom
ecox982745:reviewed-bary-mixed-distances
Draft

ecox982745 wants to merge 8 commits into
ProjectLeonida:mainfrom
ecox982745:reviewed-bary-mixed-distances

Conversation

@ecox982745

@ecox982745 ecox982745 commented Nov 26, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as RT-BARY-FALLBACK-MIXED-DISTANCES.

Depends on: #1930 (which builds on #1426).

What changed

The fallback geometry shader now forwards both ClipDistance and CullDistance when a vertex shader writes both arrays. Each array keeps its own length. This allows the rasterizer to clip part of a primitive and to cull a whole primitive.

ZroIsHere wrote the implementation and CPU test in mrgenty/leonida#1. This draft brings that work upstream at the request of the #1930 author. The original commit is 096a26220. Commit 3d34b0c0f5 adds the combined eight-component D3D12 limit and a boundary test. Review the two-file change.

Verification

I reapplied the original commit locally on top of the updated #1930 head 682de2894. The resulting commit fcbc57c0d has a byte-identical two-file diff and merges cleanly with main at 73516a290.

  • The stock CPU generator test passed on the rebased commit. All ten generated SPIR-V modules passed spirv-val --target-env vulkan1.2.
  • I repeated 44 synthetic Vulkan draws on an NVIDIA GeForce RTX 4090 (Windows 11, driver 616.64). All 22 pairs matched the direct vertex-to-fragment path byte for byte across 90,112 RGBA32F pixels. The cases used clip/cull lengths of (1,1), (2,1), (1,2), and (4,4) and covered both partial clipping and whole-primitive culling. The validation layer reported zero errors. Earlier hardware run on the original commit.
  • The project’s game-data check on the two changed files and git diff --check both passed. All fixtures were synthetic.

Current head and integration

At 3d34b0c0f5 the generator accepts five clip distances plus three cull distances, and rejects five plus four before emitting a geometry shader. The rejection test failed on the preceding head. Microsoft's HLSL specification counts the two arrays together. Clip and cull semantics · D3D12 constants

I applied the cumulative two-file change locally to #1930 at c6aa38942e. Only the opening header comment needed manual reconciliation. The resulting Git tree was 04f4b81461e30df41db123d3617f875bd4156130. The generator's CPU test passed on both trees. All 11 modules on the published head and all 15 on the integration passed SPIR-V validation for Vulkan 1.2 and 1.3.

On that integration, a synthetic 64 × 64 draw used the fifth clip distance and the third cull distance alongside the computed interpolation fallback. On Windows 11 with an RTX 4090 and NVIDIA 616.64, all 12,288 pixels matched the expected values across three draws. The full triangle covered 4,096 pixels, clipping left 2,048, and culling left zero. Khronos validation produced no messages during those draws.

The draft still depends on #1930. Only the two-file feature update is on the public branch. The earlier push that brought in upstream CI workflow changes required GitHub's workflow permission. Previous integration and hardware results

AI-assisted: Yes. The original author disclosed AI assistance. OpenAI Codex (GPT-6) added and tested the combined-limit check, performed hardware verification, and submitted the upstream draft.

Dashhhhhhhh and others added 7 commits November 24, 2026 18:58
Those pixel shaders were refused under both fallbacks, so the shaders stage stopped on a geometry-capable GPU without fragmentShaderBarycentric. The barycentric fallback now supplies the three vertex values, and make_game selects it there.

Co-authored-by: Cursor <cursoragent@cursor.com>
… 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)
…pairs (RT-BARY-FALLBACK-PER-VERTEX)

A pixel shader with a computed I/J pair, translated with --bary-fallback, reads the pair's attribute as a Flat
vec4[3] at the first of three locations its index entry gives under bary_fallback_input.per_vertex. The runtime's
geometry-stage builder read only bary_fallback_input.location and refused that input ("PS pass-through Input at
Location 0 is not vec4<f32>"), so such a PSO was never created.

- shader_library.h: parse per_vertex (attribute -> first location) into ShaderEntry::bary_fallback_per_vertex; one
  malformed entry drops the whole map.
- spirv_bary_fallback.h: build_bary_fallback_gs takes the map and writes the triangle's three values of each mapped
  VS output to the PS's Flat vec4[3] (element k = vertex k, the same at every emitted vertex), as
  tools/bary_fallback_gs.py does. An attribute the VS does not export is zero-filled. A vec4[3] the map does not
  name, a block that runs into another input or the barycentric location, and two attributes at one first location
  are refused. The old signature stays as a wrapper with an empty map.
- d3d12_vulkan_device.cpp: both call sites (eager and lazy) pass the entry's map.
- Tests: spirv_bary_fallback (five per-vertex cases), shader_library (twelve per_vertex parsing cases),
  d3d12_bary_fallback (two draws of a synthetic computed-I/J PS and one refusal).
- docs/design.md: the runtime builder in the "Barycentric fallback" row, with what was and was not checked.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

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

macOS arm64 check of #2304 (RT-BARY-FALLBACK-PER-VERTEX). A translator rule, a Python geometry-shader builder and the runtime builder; the branch is far behind and does not apply to main, so what I could verify is the translator half and the generated modules.

Tested head 096a26220 (7 commits, 3 of them merges of main from when the branch was current), 533 commits behind main bdbc7e923.

It does not apply to main any more. git merge-tree --write-tree ubi/main 096a26220 exits 1 with conflicts in docs/design.md, runtime/shims/d3d12_vulkan_device.cpp, runtime/test/vk_test_device.h and tools/vk_subgroup_info.py. The branch's own diff against its merge-base (b0f3d3998, 533 commits back) fails git apply --check on main in those four plus docs/testing.md and tests/test_vk_subgroup_info.py. So a rebase is needed before CI can run it; I tested two things that can be separated:

  1. The translator half transplanted onto main (tools/rdna2spirv.py, tools/bary_fallback_gs.py, tests/semantic/support.py, tests/semantic/test_interp_computed_ij.py, tests/test_shader_build_fallbacks.py, tests/test_run_gpu_tests.py, scripts/run_gpu_tests.py applied by patch -p1, clean).
    • tests/semantic/test_interp_computed_ij.py: 28 tests, OK - including the ComputedIJBaryFallback cases that compare every computed form against the PerVertexKHR path and the geometry shader's own spirv-val.
    • Whole semantic suite: 500 files / 6,373 tests / 0 files failed (main is 500 / 6,367, so the change adds 6 cases and breaks none).
    • tests/test_shader_build_fallbacks.py: 3 failures, tests/test_run_gpu_tests.py: 2 failures - not defects of the rule: they assert the build-side wording and selection that live in the runtime/make_game half, which I did not transplant (e.g. the test expects per-vertex inputs where main prints computed I/J pairs need PerVertexKHR inputs). They are a reminder that the whole PR has to be rebased before those suites mean anything.
  2. The branch tree as-is on this machine, to exercise the generator end to end. Built the macOS harness from 096a26220 (vk_compute_test, vk_store_test, vk_harness) and ran the project's own runner with LEONIDA_BARY_FALLBACK=1 and KosmicKrisp:
    • 274 shaders: 274 translated, 274 pass spirv-val.
    • Five fallback geometry shaders built: ps_bary, ps_ij_computed, ps_persp_bary, ps_scc_mask32, ps_uniform_branch (<name>.bary_gs.spv). Each passes spirv-val --target-env vulkan1.2 independently (the builder already validates them; I re-ran the check on the artefacts of this run).
    • Suite: 158/264 passed, 72 skipped; the five GS fixtures report SKIP (unsupported: no geometryShader). That is the honest outcome on this device.

What this GPU cannot verify, and why it matters. vk_subgroup_info.py reports geometry_shader: false, per_vertex_inputs: false, barycentric_fallback: false for KosmicKrisp on an Apple M3 Pro: Apple GPUs have no geometry stage, so the pass-through GS this PR adds cannot be created here at all. The design row already lists "a GPU that needs the fallback" as unverified; this machine needs the fallback but cannot run it, so it does not close that gap. What I can say positively: under --bary-fallback every fixture translates, the PS values of the computed-I/J forms match the PerVertexKHR path in the mini interpreter, and all five generated GS modules are valid Vulkan 1.2 SPIR-V.

Review of the rule as far as it can be read

  • Location allocation cannot collide: assemble() rents three consecutive free locations per per-vertex attribute, excluding the attribute's own location, and rejects a block that overlaps any other input (tools/rdna2spirv.py:15798-15808); the loop's search bound len(taken) + 3 is always enough because at most len(taken) of its candidates can be taken. The index keeps the attribute number in inputs for those attributes (:19199) and records the block under bary_fallback_input.per_vertex (:19206), which is what both the Python GS builder and the runtime builder consume.
  • The GS writes vertex k's value into element k at every emitted vertex, so the flat array is order-independent, as the docstring says; the PS then weights P0 + I·(P1−P0) + J·(P2−P0) with its own I/J.
  • --f16-interp-fallback plus --bary-fallback (the Pascal combination) is covered by the new semantic cases; the 16-bit reads decode the raw words of the array rather than the producer's halves, which is the only consistent choice since there are no halves on that path.

Findings

  1. Rebase needed (same commit as the last one I saw for it): the file list above; runtime/src/arena_blocks.h and runtime/shims/d3d12_vulkan_device.cpp have moved under it, and tools/vk_subgroup_info.py now conflicts, so the automatic fallback selection changes need re-checking against main's version after the rebase.
  2. No macOS-executable verification of the new path. Stated above so nobody reads this as a green run: the fixture that would draw it skips on this device. A Linux run on a driver with geometry shaders but no VK_KHR_fragment_shader_barycentric (or Pascal) remains the way to execute it; the author already reports such a check on a GTX 1060.
  3. Small operational note: with the fallback forced on a device without a geometry stage, run_gpu_tests.py --require-pass exits 1 with no GPU tests passed because the only fixture skipped. That is the runner's contract, but a first-time user on Apple Silicon may read it as a failure; a line in the message when the skip is no geometryShader would explain it.

python -I scripts/check_no_game_data.py on the seven transplanted files: clean. I did not run the shader corpus or GPU fixtures against main (the translator change cannot be applied there beyond the files above), so no baseline deltas are claimed beyond the semantic counts.

No game files. AI-assisted.

@ecox982745

Copy link
Copy Markdown
Author

I followed up on the macOS review. The full dependency stack merges cleanly with main 2d43417d49 after reapplying the two-file mixed clip/cull commit. All five fixtures that skipped on Apple M3 ran on an RTX 4090.

To reproduce the tested tree, start at #1930 head 682de28946, cherry-pick 096a26220f, then merge 2d43417d49. The resulting Git tree is 6ff8680e10ce90599940f3ece50488949c6fa829. The public draft still points to the older stack. The individual change in this PR touches only spirv_bary_fallback.h and its C++ test. The translator and device-selection changes are in its dependencies.

Check on the combined tree Result
CPU translator suite 6,379 tests across 500 files, zero files failed
Build selection, GPU runner and Vulkan capability unit tests 7/7, 71/71 and 56/56 passed, respectively. This includes the two suites that had failures on the partial transplant.
Stock C++ geometry-shader generator All ten emitted modules passed spirv-val --target-env vulkan1.2 and matched the prior rebased build byte for byte, including mixed clip/cull.
Windows 11, RTX 4090, driver 616.64 ps_bary, ps_ij_computed, ps_persp_bary, ps_scc_mask32 and ps_uniform_branch each passed the pixel readback with automatic selection and with LEONIDA_BARY_FALLBACK=1. The forced runs emitted five geometry shaders, all independently validated.
D3D12 runtime path on the same GPU d3d12_bary_fallback passed with Vulkan validation enabled, including partial clipping, f16 relocation and both computed-I/J layouts.

The RTX reports geometry_shader=true, per_vertex_inputs=true and barycentric_fallback=false. Automatic selection used its native path. Forcing the fallback exercised the geometry stage on hardware. The public branch update is still pending.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026
@ecox982745

ecox982745 commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

I tested this patch against the updated dependency in #1930, including a draw that needs both changes in the same geometry shader. Cherry-picking 096a26220f onto 0b7532951f applied cleanly (tree 8b52538a2e). Merging main d1dff7949b into that result also applied cleanly. All results below are from the resulting tree ff32b57406f99271788e9e039d496b8b854bae37.

RTX 4090, Windows 11, NVIDIA 616.64. The synthetic vertex shader writes two clip distances and one cull distance. The fragment shader reads the per-vertex attribute at 29 alongside decoded FP16 halves of 0.25 and 0.75. I compared all four channels at every pixel of each 64 × 64 readback with the expected values.

Vertex attribute 29 Full triangle Half clipped Fully culled
Missing 4,096 pixels returned 0 for the attribute 2,048 returned 0 0 pixels drawn
Authored as 0.625 4,096 returned 0.625 2,048 returned 0.625 0 pixels drawn

The two FP16 channels kept their expected values in every drawn pixel, and every unshaded pixel retained the clear color. As a control I omitted the raw-export set from the generator for one full draw. The linked high half at location 29 then appeared as 0.75 instead of 0 in all 4,096 pixels. All seven draws ran with Khronos validation and produced no validation errors.

The stock shader_library, spirv_f16_halves, and spirv_bary_fallback CPU tests passed. All 13 geometry modules dumped by the generator passed spirv-val for Vulkan 1.2 and 1.3. Both D3D12 GPU tests (d3d12_f16_halves, d3d12_bary_fallback) also passed on the 4090 with validation enabled. The public draft head is still 096a26220f. AI-assisted verification.

Retest on the updated #1930 dependency. I replayed the two commits of this draft (096a26220 and 3d34b0c0f) onto #1930 head 0ce4df8a229626f21c4511f40e1dcab30d515f35, which contains main cc0f662a70b6e6e561692f56342b5a7df3831bd7. The one conflict was in the opening header comment. I kept the dependency's description of pruning unread inputs and the draft's description of forwarding both arrays. The tested integration tree was edb95365ec0899b0cf93d1b608c161a80fd91f92.

  • CPU. shader_library, spirv_f16_halves and spirv_bary_fallback passed. All 15 generated geometry modules passed SPIR-V validation for both Vulkan 1.2 and 1.3. The combined-distance boundary checks ran in the generator suite.
  • GPU. d3d12_f16_halves and d3d12_bary_fallback passed on an RTX 4090, NVIDIA 616.64, Windows 11 build 26300, with Khronos validation enabled. I also reran the five-clip and three-cull distance fixture with computed interpolation. Across three 64 by 64 draws, all 12,288 pixels matched. The draws covered 4,096 pixels at full coverage, 2,048 after clipping and zero after culling. The mixed-distance draws produced no Vulkan validation errors.

The integration is local and the draft still depends on #1930. AI-assisted with OpenAI Codex (GPT-6.2 Sol).

Check the D3D12 eight-component budget before generating a fallback geometry shader. Cover an unequal split at the limit and the first disallowed total.

AI-assisted-by: OpenAI Codex (GPT-6)
@ecox982745

Copy link
Copy Markdown
Author

Verified the mixed clip/cull change against the updated geometry-limit check in #1930. I applied this PR’s two-file diff at 3d34b0c0f5 to dependency head 32b2d6e663. Only the opening comment in the generator header needed reconciliation. The local integration tree is 7a7036148755648f686073d6467497da92144e4d; the public draft still depends on #1930.

I exercised the D3D12 pipeline creation and readback path on Windows with a physical RTX 4090 (616.64). The hardware reports 128 geometry input and output components. I set both runtime limit overrides to 64. The shader used Position, five clip distances and three cull distances, which contribute 12 built-in components.

Vertex attribute / first output location of the three-location block Result
12 / 10 The input and output each needed exactly 64 components. The pipeline drew all 256 pixels, then 128 with the fifth clip distance active, then zero with the third cull distance active. All 768 RGBA8 pixel values, including clears, matched.
13 / 10 Pipeline creation returned E_NOTIMPL before submission. The diagnostic identified 56 user + 12 built-in input components against 64.
12 / 11 Pipeline creation returned E_NOTIMPL before submission. This time the diagnostic identified 56 user + 12 built-in output components against 64.

The stock CPU geometry-generator test passed. Vulkan validation reported zero errors in the five cases. The two refusal checks exercised the runtime guard. The three accepted cases exercised physical GPU rendering under the advertised limit override. AI-assisted with OpenAI Codex (GPT-6).

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.

7 participants