Skip to content

Barycentrics without VK_KHR_fragment_shader_barycentric: geometry-shader fallback - #794

Closed
DaniSBR1 wants to merge 1 commit into
ItsNotRexed:mainfrom
DaniSBR1:pr-bary-gs
Closed

DaniSBR1 wants to merge 1 commit into
ItsNotRexed:mainfrom
DaniSBR1:pr-bary-gs

Conversation

@DaniSBR1

Copy link
Copy Markdown
Contributor

New SHADER item, claimed in #634. Depends on: none. Related: #88, #146, #166 (known failures: ps_bary, ps_uniform_branch, ps_scc_mask32 without the extension).

Pixel shaders that read the raw barycentric pair (v0 = i, v1 = j outside v_interp, ~315 in the game) are
translated to BaryCoordKHR. Devices without VK_KHR_fragment_shader_barycentric can't run them: RDNA1 and older on
RADV, Intel Gen9, llvmpipe. Since f348eb9 the GPU tests skip them there. RADV on GFX10.1 also gets BaryCoordKHR wrong
in wave64 pixel shaders (2x2 quads read .xy instead of .yz, #88/#146), so the extension path isn't enough there
either.

This adds a fallback that needs only geometryShader. For each such shader the translator also writes:

  • <name>.bary.spv: the same shader, reading %bary from a smooth vec3 input at the first free Location;
  • <name>.bary_gs.spv: a pass-through geometry shader that copies Position and the varyings and gives the three
    vertices the corners (1,0,0), (0,1,0), (0,0,1) at that Location. Interpolated, that is BaryCoordKHR. The vertex
    shader can't write it: in an indexed mesh one vertex can be corner 1 of one triangle and corner 2 of the next.
    Vertex order is kept, so the provoking vertex (Flat inputs) and the winding don't change.

index.json records them as "bary_fallback": {"location", "ps", "gs"}. vk_harness draws the fallback
(VKH_BARY_PS + VKH_BARY_GS) when the device lacks the extension, before f348eb9's unsupported check;
VKH_BARY=gs|builtin forces one path. check.py and run_gpu_tests.py pass it from index.json.

Not carried by the geometry shader: the vertex shader's ClipDistance, CullDistance, Layer and ViewportIndex.

Results (main 8df277f vs this branch)

main this branch
ps_bary, ps_uniform_branch, ps_scc_mask32 on an RX 5700 XT (RADV) SKIP (no extension) PASS (ps_bary 4096/4096 pixels exact)
the same three on llvmpipe SKIP PASS
run_gpu_tests.py, RX 5700 XT 88 of 96 pass, 8 skipped* 91/96, 5 skipped (mesh), 0 failed
run_gpu_tests.py, llvmpipe 63/97, 34 skipped, 0 failed 66/97, 31 skipped, 0 failed
check.py, RX 5700 XT 82/88, 6 skipped 85/88, 3 skipped (mesh), 0 failed
pytest 2218 2220 (+2 new tests)

* main prints 89/97 there: cs_wavemask is reported twice (fixed separately, #793). The same three tests skip on
the AMD Windows driver on RDNA1 too, which doesn't expose the extension either (#166, TEST-58 entry). With the
llvmpipe CI job claimed in #634, they would run there as well.

With the pixel shader forced to wave64 and to wave32 (VKH_SUBGROUP_SIZE), both tests pass through the fallback;
through BaryCoordKHR at wave64, ps_bary had 512/4096 pixels wrong. Flat integer varyings keep their bits through
the geometry shader (ps_flat drawn through it).

Cost of the geometry shader on the same GPU, per 1M triangles: +0.00 ms with 1 varying, +0.17 ms with 8, +0.64 ms
with 16 (visible triangles at 1920x1080). Only pipelines whose pixel shader reads raw barycentrics pay it.

AI-assisted (Claude Code): code, tests and this description. The tests ran on my own RX 5700 XT.

Tested 00884f9 (on main 8df277f) on AMD Radeon RX 5700 XT (RDNA1, Navi 10), RADV Mesa 26.0.8-1ubuntu0.3, Vulkan 1.4.335, Ubuntu 26.04.1 (kernel 7.0.0-42), LLVM 21: pass

🤖 Generated with Claude Code

…der fallback

A pixel shader that reads the raw barycentric pair (v0 = i, v1 = j outside v_interp, ~315 in the game) maps it
to BaryCoordKHR. RDNA1 and older on RADV (and Intel Gen9) don't expose the extension, and RADV gets
BaryCoordKHR wrong in wave64 pixel shaders on GFX10.1 (2x2 quads read .xy instead of .yz, ItsNotRexed#88).

For each such shader the translator now also writes:
- <name>.bary.spv: the same shader reading %bary from a smooth input at the first free Location;
- <name>.bary_gs.spv: a pass-through geometry shader that copies Position and the varyings, and gives the
  three vertices the corners (1,0,0), (0,1,0), (0,0,1) at that Location. Interpolated, that is BaryCoordKHR.
  The vertex shader can't write it: indexed meshes share vertices between triangles.
index.json records them as "bary_fallback": {"location", "ps", "gs"}.

vk_harness draws the fallback (VKH_BARY_PS + VKH_BARY_GS) when the device lacks the extension;
VKH_BARY=gs|builtin forces one path. check.py and run_gpu_tests.py pass it from index.json.

RX 5700 XT (RADV, Mesa 26.0.8), pixel shader forced to each wave size:
  ps_bary            GS: 0/4096 wrong at wave64 and wave32; BaryCoordKHR: 512/4096 wrong at wave64
  ps_uniform_branch  GS: passes at wave64 and wave32;       BaryCoordKHR: fails at wave64
Flat integer varyings keep their bits through the GS (ps_flat). run_gpu_tests.py: 38/42 as before (the
three ms_* need VK_EXT_mesh_shader). pytest: 832 passed (830 before, +2 new tests).

Not carried by the GS: the vertex shader's ClipDistance, CullDistance, Layer and ViewportIndex.

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

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Perspective interpolation readback on Windows for 00884f982828120f050966bef7f0c41b1bb5d60c: the forced geometry-shader fallback and the native BaryCoordKHR path produced byte-identical RGBA32F images on each GPU. Both matched an independent CPU reference.

I built the PR's ps_bary.asm and used its unchanged vk_harness at wave32. A synthetic VS projected a triangle to screen positions (5,8), (57,16), (21,55) in a 64×64 viewport with clip w = (1,2,4). Its three attr0.xy values were (0.25,−0.5), (2.25,0.75), (−1,3). For each pixel center the CPU computed screen-space weights from signed areas, divided each by its vertex w and normalized the sum. RG should be the weighted attributes, BA the second and third normalized weights.

GPU Covered pixels within 2×10⁻⁶ in all four channels, each path Outside pixels still exactly clear Maximum absolute channel error
RTX 5080, 616.92 1,158/1,158 2,938/2,938 5.26×10⁻⁷ or less
Radeon(TM) Graphics, 26.10.1 1,158/1,158 2,938/2,938 1.25×10⁻⁶ or less

Negative control: I added NoPerspective only to the fallback PS's raw barycentric input at Location 1. On both GPUs all 1,158 covered pixels then failed the perspective oracle, all matched the affine BA prediction, and RG still matched the perspective-correct attributes. At (25,25) BA changed from approximately (0.241562, 0.131825) to (0.295121, 0.322107).

AI-assisted independent verification using source-built shaders.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

The fallback location allocator hits a device limit on both this head (00884f9) and the independently implemented #804 (5591f49). With a single synthetic attr30 varying, both assign the barycentric vec3 to Location 31 even though Locations 0–29 are free. Both pairs pass offline spirv-val.

On RTX 5080 (616.92) and Radeon Graphics (26.10.1), maxFragmentInputComponents and maxGeometryOutputComponents are both 128. Khronos validation 1.4.350 reports VUID-RuntimeSpirv-Location-06272 when creating either fallback PS: Location 31, component 2, plus four built-in components (the shader declares FragCoord). My validation callback aborted on that error, before submitting a draw.

Two controls on both adapters, using the previously measured 64×64 perspective triangle:

  • Changed only the fallback location in the generated PS/GS pair from 31 to 0; the ordinary varying stayed at 30. Both implementations rendered all 4,096 pixels byte-identically to the CPU-checked reference image for that adapter, with no validation error.
  • Moved the ordinary varying down one slot to attr29. Both unmodified allocators then chose Location 30; both fallback draws and the native builtin draw passed validation and produced the reference image.

To reproduce, substitute attr30 for attr0 in ps_bary.asm, move the VS varying to Location 30, and declare the VS gl_PerVertex with only gl_Position. A first attempt with attr31 exceeded the VS output component limit itself. Also, the native builtin PS with attr30 was rejected here with seven builtin components; the successful attr30 measurement above is specifically the fallback with the free lower slot. This is a synthetic interface boundary case. Selecting the first unused location would address it in either implementation.

AI-assisted independent verification.

@DaniSBR1

Copy link
Copy Markdown
Contributor Author

Thanks, @Vonsek. This covers what my runs couldn't: a GPU that has the extension, both paths side by side, and non-uniform w. The tests in this PR draw with w = 1 everywhere (vs_ids), so perspective correctness wasn't exercised; your (1, 2, 4) triangle and the NoPerspective control pin it down.

Would you mind if I add a small version of it to this PR (a vertex shader with per-vertex w and a check against the same CPU weights), so check.py keeps testing it? If you'd rather send it as your own follow-up test, I'm happy to link it from here instead.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Yes, please add the small regression to this PR. With clip w = (1, 2, 4), compute the screen-space barycentric weights at each pixel center, divide each weight by its vertex w, and normalize by their sum. Our NoPerspective control on the raw weights changed all 1,158 covered pixels while leaving the ordinary interpolated attributes correct. Keeping that control alongside the CPU comparison should catch a regression even on devices where only the fallback runs.

@DaniSBR1

Copy link
Copy Markdown
Contributor Author

Thanks, @Vonsek, and sorry for the noise: I missed that SHADER-98 was claimed by @Jess-Doit in #374 two hours before my claim, and #804 implements it. Under #634's rules that claim comes first, so I'm closing this in favour of #804.

I'll still add the regression you describe, as its own test PR on main: a vertex shader with clip w = (1, 2, 4) and per-vertex attributes, checked against the perspective-correct CPU weights, plus a check that the affine (NoPerspective) prediction differs on every covered pixel, so the test can't pass by accident. On main it runs the native BaryCoordKHR path; with #804 it covers the fallback too.

Two notes from this branch for #804:

@DaniSBR1

Copy link
Copy Markdown
Contributor Author

@Vonsek, the regression is up as #837: ps_persp_bary drawn by vs_persp (clip w = 1, 2, 4), checked against the perspective-correct CPU weights, with the affine control built in. It skips on RDNA1 here (no extension), so a run on one of your GPUs with the native path would be welcome.

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

2 participants