Skip to content

Translator: --bary-fallback for raw barycentrics without VK_KHR_fragment_shader_barycentric (SHADER-98) - #804

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Jess-Doit:translator/bary-fallback
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Jess-Doit:translator/bary-fallback

Conversation

@Jess-Doit

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-98 (#374 (comment), carried over to #634)

What and why

The pixel shaders that read the raw barycentric pair (v0/v1 outside v_interp, docs/design.md "PS system inputs")
are translated to BuiltIn BaryCoordKHR. They can't run at all on a device without
VK_KHR_fragment_shader_barycentric: NVIDIA Pascal is one, and its #166 entry skips ps_bary, ps_uniform_branch
and ps_scc_mask32. This adds an opt-in translation that runs them there, the way --wave64-emulate does for
wave64.

  • tools/rdna2spirv.py --bary-fallback: the pair comes from an ordinary vec3 input, %bary, at one location past
    the shader's attributes, instead of the builtin. The module then needs neither FragmentBarycentricKHR nor the
    extension. The location is in the index as bary_fallback_input. The input keeps default interpolation, which is
    perspective-correct like BaryCoordKHR (not BaryCoordNoPerspKHR), so .y/.z stay AMD's i/j. Shaders that
    don't read the pair translate to the same module with or without the flag.
  • tools/bary_fallback_gs.py: writes the stage that fills %bary. It is a triangle pass-through geometry shader,
    generated from the index, that copies Position and the PS's input locations unchanged and writes (1,0,0),
    (0,1,0), (0,0,1) at the three vertices. Interpolating those weights gives the barycentrics. The device needs
    geometry shaders instead, which Pascal has (gs_tri passes there).
  • LEONIDA_BARY_FALLBACK=1, like LEONIDA_W64_EMULATE:
    • build.py translates with the flag and builds <name>.bary_gs.spv for each such PS.
    • check.py and run_gpu_tests.py draw those shaders through it.
  • vk_harness: VKH_GS_HELPER=1 marks the geometry shader as a helper, so VKH_SUBGROUP_SIZE stays on the
    fragment stage under test (Tests: skip barycentric tests without the extension and run pixel tests at their subgroup size (RDNA1 results) #146), not on the GS.
  • Docs: a contract row "Barycentric fallback" in docs/design.md, the index key, and a docs/testing.md section.

Follow-up, not in this PR: the runtime builds the same GS per pipeline from the VS output signature when the
device lacks the extension. That needs the graphics pipelines of #291 (D3D-4), and I'll post it as its own item.

Tested

AI-assisted change. NVIDIA GeForce GTX 1060 6GB (Pascal: no VK_KHR_fragment_shader_barycentric), driver 560.94,
Vulkan 1.3.280, Windows 10 22H2, Vulkan SDK 1.4.363.0, llvm-mc 23.1.6, on main 8df277f:

  • run_gpu_tests.py:
    • Without the variable: 62/97 passed, 35 skipped, 0 failed.
    • With LEONIDA_BARY_FALLBACK=1: 65/97 passed, 32 skipped, 0 failed. ps_bary is 4096/4096 pixels exact, and
      ps_uniform_branch and ps_scc_mask32 pass. All three went from SKIP to PASS; nothing else changed.
  • build.py + check.py with LEONIDA_BARY_FALLBACK=1: the three shaders report ok, each drawn through its
    generated .bary_gs.spv.
  • Khronos validation (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation; vk_harness doesn't read
    LEONIDA_VK_VALIDATION) on the three fallback draws: zero messages. Positive control: drawing ps_bary through
    ps_uniform_branch's GS gives VUID-RuntimeSpirv-OpEntryPoint-08743 (interface mismatch), so the layer was
    active.
  • tests/semantic/test_bary_fallback.py (8 tests, new ps_bary fixture):
    • Both modules give the same raw i/j in the interpreter.
    • The fallback module has no capability, extension or builtin, and its location sits past the attributes.
    • ps_ops, ps_flat and cs_bounds are unchanged by the flag.
    • The GS text, collision refusal and index handling are checked, as are spirv-as / spirv-val on the GS.
  • Pixel-shader corpus, 12,101 PS from my own copy (outcomes only), flag off vs on:
    • Off: 12,101 translated, 12,101 pass spirv-val.
    • On: 12,101 translated, 12,101 pass spirv-val.
    • 321 PS switch from FragmentBarycentricKHR to bary_fallback_input, at locations 1-5. None collides with an
      input.
    • The other 11,780 have identical index entries in both modes. The 321 differ only in capabilities,
      extensions, features and bary_fallback_input.
    • Only pixel shaders were translated, since the flag changes nothing in other stages. After the last rebase I
      re-translated the 321 in both modes: 321/321 translated and valid each way.
  • ctest (on 67204bf, before the last rebase): 28/29. d3d12_vulkan_device fails its GetCopyableFootprints comparison on the 10×6 BC1 case, which
    Windows 10's in-box runtime rejects. That's the same failure I reported on Runtime: compute path and graphics draws on Vulkan (D3D-3, D3D-4, milestone M1) #291, and it's unrelated to this PR.
  • Python suite (on 67204bf): 2835 passed. Two subtests of test_audio_wav.py fail identically on clean main 67204bf.

Overlap: #436 also edits vk_harness.cpp, check.py and run_gpu_tests.py (zero-based vertex IDs), but not the
barycentric code. I'm happy to rebase if it lands first.

Checklist

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01SSJXX67xeoorUsLJ2f9zms

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

I checked the same sparse input on this head (5591f49e57f48db6d463a61d58218e8eef0cea5c) and on #794. Here --bary-fallback records bary_fallback_input.location = 31 for a PS with only attr30. Both the PS and the GS from bary_fallback_gs.py pass spirv-val, then Khronos validation flags the PS during module creation on RTX 5080 and Radeon Graphics (both expose 128 fragment input components). Moving just the two matching barycentric Location decorations to unused slot 0 let both GPUs render the expected 4,096 pixels. The neighboring attr29 case also rendered with the unmodified allocator. This synthetic case falls outside the locations 1–5 measured in your PR.

Both PRs implement the same high-water location choice, so one change to select a free lower slot could cover whichever fallback is integrated. Full setup, exact VUID and paired controls. AI-assisted verification.

Jess-Doit added a commit to Jess-Doit/Leonida that referenced this pull request Nov 22, 2026
…ast the highest

A PS whose only attribute is attr30 put %bary at Location 31, which Khronos
validation rejects on drivers with 128 fragment input components (found by
@Vonsek on ItsNotRexed#804). The lowest location no attribute uses is always available
and is 0 there. The corpus is unchanged: the 321 fallback shaders keep
locations 1-5, since their attributes are dense. New semantic test for the
sparse case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSJXX67xeoorUsLJ2f9zms
@Jess-Doit
Jess-Doit force-pushed the translator/bary-fallback branch from 5591f49 to ffd0786 Compare November 22, 2026 03:48
@Jess-Doit

Copy link
Copy Markdown
Contributor Author

Thanks, confirmed. ffd0786 picks the lowest location no attribute uses instead of one past the highest, so your
attr30-only case gets %bary at Location 0. The generated GS writes it there and copies attr30 at 30.
test_sparse_attributes_take_the_lowest_free_location covers it. Against the previous translator the test fails
with {'location': 31} != {'location': 0}.

On the real corpus nothing moves: I re-translated the 321 fallback pixel shaders with the change, and all keep their
locations (1–5, mostly 2) and translate and pass spirv-val. Their attributes have no gaps.

I also rebased onto dedd75c (the PR conflicted). On the GTX 1060, run_gpu_tests.py gives 63/98 without the
variable and 66/98 with LEONIDA_BARY_FALLBACK=1, 0 failed. ps_bary, ps_uniform_branch and ps_scc_mask32
go from SKIP to PASS as before.

About the wine check on the previous head: its root-signature, WARP and D3D12 failures also show up on passing
PRs (#814's run). The one extra was run_tiny_game_exit_code, in the loader, which this PR doesn't touch. I'll
look again if it recurs on the new head.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Retested the original attr30-only triangle at ffd0786150b33572d99b4de051666007341dae13 on RTX 5080 and AMD Radeon Graphics. The generated PS reads attr30 at Location 30 and barycentrics at 0, and the generated GS writes both. The stock harness rendered the pair on both GPUs. The 1,158 covered pixels matched the perspective-correct CPU reference in all four channels (maximum absolute error 5.26×10⁻⁷ on RTX and 1.25×10⁻⁶ on Radeon), and the other 2,938 stayed exactly clear. The full image was also byte-identical to the native barycentric reference for each adapter.

Khronos validation produced zero errors with the generated pair. Restoring only the two barycentric decorations to 31 reproduced VUID-RuntimeSpirv-Location-06272 on both GPUs, although both modules still passed standalone spirv-val. This closes the sparse-location case I reported above.

AI-assisted verification with source-built shaders.

DaniSBR1 added a commit to DaniSBR1/Leonida that referenced this pull request Nov 22, 2026
The raw-barycentric tests draw with vs_ids, where clip w = 1 at every vertex, so nothing checked that the raw
i/j (and the attributes) follow perspective-correct weights. vs_persp draws one triangle at screen (5,8),
(57,16), (21,55) with clip w = 1, 2, 4 and per-vertex attr0; ps_persp_bary is ps_bary's code drawn with it.
check.py's bary_persp_problem compares every pixel with CPU weights p_k = (l_k / w_k) / sum(l_m / w_m) from the
screen-space weights l_k, checks that pixels outside stay clear, and requires the affine weights to differ by
more than 100x the tolerance on every judged pixel, so a NoPerspective regression can't pass. run_gpu_tests.py
uses the same reference (PIXEL_VS, DRAWN_WITH). Suggested by Vonsek in ItsNotRexed#794.

On main it runs the native BaryCoordKHR path (it skips without VK_KHR_fragment_shader_barycentric, like
ps_bary); any barycentric fallback that lands (ItsNotRexed#804) is exercised by it too. Checked on an RX 5700 XT (RADV)
through a GS fallback: passes at wave32 and wave64; with NoPerspective on the fallback's input, all 1149
covered pixels fail while the interpolated attributes still match. New tests/test_bary_persp_oracle.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DaniSBR1

Copy link
Copy Markdown
Contributor

Ran ffd0786 (on dedd75c) on an RDNA1 card, which has geometry shaders but no VK_KHR_fragment_shader_barycentric: AMD Radeon RX 5700 XT (Navi 10), RADV Mesa 26.0.8, Vulkan 1.4.335, Ubuntu 26.04.1, LLVM 21.

  • run_gpu_tests.py:
    • Without the variable: 90/98 passed, 8 skipped (5 mesh, ps_bary, ps_scc_mask32, ps_uniform_branch), 0 failed.
    • With LEONIDA_BARY_FALLBACK=1: 93/98 passed, 5 skipped (mesh only), 0 failed. ps_bary is 4096/4096 pixels exact, and ps_scc_mask32 and ps_uniform_branch pass.
  • build.py + check.py: 94/94 translated and valid in both modes. check.py goes from 83/89 to 86/89 with the variable (the remaining skips are the mesh fixtures).
  • tests/semantic/test_bary_fallback.py: 9 passed. Python suite (--ignore=tests/semantic): 2256 passed, 0 failed.

Two cases the suite doesn't cover, drawn through the GS that tools/bary_fallback_gs.py generates (VKH_GS_HELPER=1):

  • Wave64 pixel shader. RADV runs pixel shaders at wave64 here, and that is where BaryCoordKHR misbehaves on GFX10.1. ps_bary at VKH_SUBGROUP_SIZE=64 is exact (max error 0), the same as at 32.
  • Perspective (GPU tests: raw barycentrics under perspective (ps_persp_bary, vs_persp) #837's vs_persp at 3ef3ee4, clip w = 1, 2, 4). ps_persp_bary translated with --bary-fallback matches the perspective-correct CPU weights on all 1158 covered pixels at wave32 and wave64. A control with NoPerspective on %bary fails on all 1158, with the interpolated attribute still right. So the default interpolation on the input is the right choice.

It merges cleanly onto current main (4a36bb1).

AI-assisted (Claude Code); the runs are on my own RX 5700 XT.

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

Tested 5591f49 on NVIDIA GeForce RTX 5070 (subgroup 32), driver 621.24, Windows 11 Pro (26300), Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 (built with scripts/build_llvm_mc.ps1), scripts/run_gpu_tests.py:

  • native: 70/97 passed, 27 skipped (main 8df277f here: 64/91 passed, 27 skipped, all wave64 tests needing a 64-wide subgroup)
  • LEONIDA_W64_EMULATE=1: 97/97 passed (main here: 91/91)
  • python -m unittest discover -s tests/semantic: Ran 980 tests in 328.087s OK

Jess-Doit added a commit to Jess-Doit/Leonida that referenced this pull request Nov 22, 2026
…ast the highest

A PS whose only attribute is attr30 put %bary at Location 31, which Khronos
validation rejects on drivers with 128 fragment input components (found by
@Vonsek on ItsNotRexed#804). The lowest location no attribute uses is always available
and is 0 there. The corpus is unchanged: the 321 fallback shaders keep
locations 1-5, since their attributes are dense. New semantic test for the
sparse case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSJXX67xeoorUsLJ2f9zms
@Jess-Doit
Jess-Doit force-pushed the translator/bary-fallback branch from ffd0786 to 4f5714e Compare November 22, 2026 04:33
@poststructural

Copy link
Copy Markdown

GPU check of 4f5714e on Linux: NVIDIA GeForce RTX 2080 (Turing, subgroup 32), driver 610.57.04, llvm-mc 21.1.8 (Ubuntu), GCC build of runtime/. AI-assisted.

  • scripts/run_gpu_tests.py: 74/101 passed, 27 skipped (the skips are the wave64 tests: no 64-wide subgroup); with LEONIDA_W64_EMULATE=1: 101/101 passed, 0 failures.
  • python -m unittest discover -s tests/semantic: Ran 1027 tests, OK.

Same machine on main (b0befb4): 74/101 native, 101/101 emulated, so no regressions.

@philip-roy-jones

Copy link
Copy Markdown

The geometry-shader fallback fixed the ps_bary.asm readback on a physical Radeon RX 580 (Polaris) at 4f5714ed99cc58270d41cba499324333a80b91dd. The Windows driver (31.0.21925.1001, Vulkan 1.3.260, native wave64) advertises VK_KHR_fragment_shader_barycentric, so both paths ran on the same card.

I ran scripts/run_gpu_tests.py --filter ps_bary.asm in separate work directories. The expected triangle weights at pixel (0, 0) were (0.00390625, 0.00390625).

Mode Pixels matching the reference exactly Weights at (0, 0)
Default 0 / 4,096 (0.9921875, 0.00390625)
LEONIDA_BARY_FALLBACK=1 4,096 / 4,096 (0.00390625, 0.00390625)

The stock harness requests 144 push-constant bytes and this card supports 128. I repeated both draws with the layout and upload limited to 128 bytes and unused push-constant members removed from each fragment shader. Khronos validation reported zero errors on both repeats. Each image matched its original readback byte for byte. RX 580 baseline in #166

Jess-Doit and others added 2 commits November 21, 2026 22:28
…ent_shader_barycentric (SHADER-98)

The 321 pixel shaders that read the raw barycentric pair are translated to
BuiltIn BaryCoordKHR, so they can't run on a device without the extension
(NVIDIA Pascal, for one). With --bary-fallback the pair comes from an
ordinary vec3 input one location past the shader's attributes; the module
needs neither the capability nor the extension, and the index records the
location as bary_fallback_input. tools/bary_fallback_gs.py writes the
pass-through geometry shader that fills it: Position and the PS's inputs
copied unchanged, plus (1,0,0), (0,1,0), (0,0,1) at the three vertices,
whose perspective interpolation is the BaryCoordKHR value.

- LEONIDA_BARY_FALLBACK=1 for build.py, check.py and run_gpu_tests.py
  (like LEONIDA_W64_EMULATE): translate with the flag, build
  <name>.bary_gs.spv, draw those shaders through it.
- vk_harness: VKH_GS_HELPER=1 keeps VKH_SUBGROUP_SIZE on the fragment
  stage when the GS only feeds the pixel shader under test.
- tests/semantic/test_bary_fallback.py and the ps_bary fixture.
- docs/design.md contract row and index key, docs/testing.md section.

GTX 1060 (Pascal): ps_bary 4096/4096 exact, ps_uniform_branch and
ps_scc_mask32 pass with the fallback (SKIP without it). Pixel-shader corpus:
12,101/12,101 translate and pass spirv-val with and without the flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSJXX67xeoorUsLJ2f9zms
…ast the highest

A PS whose only attribute is attr30 put %bary at Location 31, which Khronos
validation rejects on drivers with 128 fragment input components (found by
@Vonsek on ItsNotRexed#804). The lowest location no attribute uses is always available
and is 0 there. The corpus is unchanged: the 321 fallback shaders keep
locations 1-5, since their attributes are dense. New semantic test for the
sparse case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SSJXX67xeoorUsLJ2f9zms
@Jess-Doit
Jess-Doit force-pushed the translator/bary-fallback branch from 4f5714e to f040a94 Compare November 22, 2026 06:31
@tylerstraub

Copy link
Copy Markdown
Contributor

Tested f040a941f785f6da910260fac5fb0ccc9d1504a9 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Independent check at f040a941f785f6da910260fac5fb0ccc9d1504a9 on GeForce RTX 5090, NVIDIA 595.99.02, Ubuntu 26.04.1 LTS. I ran the synthetic ps_bary fixture twice with the PR's GPU runner, using a fresh shader build for each draw.

LEONIDA_BARY_FALLBACK Observed readback
0 (native barycentrics) 4096/4096 pixels exact
1 (generated geometry shader) 4096/4096 pixels exact

Both translations passed spirv-val. The fallback run generated ps_bary.bary_gs.spv. This covers the opt-in path on an extension-capable GPU and one synthetic fixture. AI-assisted.

AI-assisted verification.

@ItsNotRexed
ItsNotRexed merged commit d1d23d8 into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
DaniSBR1 added a commit to DaniSBR1/Leonida that referenced this pull request Nov 22, 2026
The raw-barycentric tests draw with vs_ids, where clip w = 1 at every vertex, so nothing checked that the raw
i/j (and the attributes) follow perspective-correct weights. vs_persp draws one triangle at screen (5,8),
(57,16), (21,55) with clip w = 1, 2, 4 and per-vertex attr0; ps_persp_bary is ps_bary's code drawn with it.
check.py's bary_persp_problem compares every pixel with CPU weights p_k = (l_k / w_k) / sum(l_m / w_m) from the
screen-space weights l_k, checks that pixels outside stay clear, and requires the affine weights to differ by
more than 100x the tolerance on every judged pixel, so a NoPerspective regression can't pass. run_gpu_tests.py
uses the same reference (PIXEL_VS, DRAWN_WITH). Suggested by Vonsek in ItsNotRexed#794.

On main it runs the native BaryCoordKHR path (it skips without VK_KHR_fragment_shader_barycentric, like
ps_bary); any barycentric fallback that lands (ItsNotRexed#804) is exercised by it too. Checked on an RX 5700 XT (RADV)
through a GS fallback: passes at wave32 and wave64; with NoPerspective on the fallback's input, all 1149
covered pixels fail while the interpolated attributes still match. New tests/test_bary_persp_oracle.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DaniSBR1 added a commit to DaniSBR1/Leonida that referenced this pull request Nov 23, 2026
…ometry shader

A PS translated with --bary-fallback (ItsNotRexed#804) reads its raw barycentrics from an input location
(bary_fallback_input in the index), and nothing in the layer wrote it. create_graphics now builds the
pass-through GS that tools/bary_fallback_gs.py writes for the GPU tests (runtime/src/bary_fallback_gs.h):
Position, the linked PS's input locations and the VS's clip/cull distance arrays copied per vertex, and the
one-hot barycentrics. A PSO with its own GS, a non-triangle topology, no geometryShader, a VS writing
Layer/ViewportIndex or a PS with PerVertexKHR inputs is E_NOTIMPL with a log.

Tests: bary_fallback_gs_test (module interface, clip/cull forwarding, refusals; spirv-val on the generated
modules), d3d12_bary_fallback_test (a stand-in PS drawn through a PSO: every covered pixel holds its
barycentrics), shader_library_test (the new index fields). The C++ module matches the Python template
instruction for instruction. RX 5700 XT, AMD 26.11.2, Windows 11: ctest 159/159; the new D3D12 test also
passes under the Khronos validation layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

8 participants