feat(shader): emulate fragment barycentrics with a generated geometry stage #1547
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1547"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by russhil on 2026-10-08T11:49:52Z.
What
Without
VK_KHR_fragment_shader_barycentric(lavapipe in CI, NVIDIA before Turing, older Intel), the recompiler droppedv_interp_p1_f32and hadv_interp_p2_f32read the attribute Vulkan interpolated, but it never wrote the I/J VGPRs. So onmain:v_interp_p1/p2of the same pair (EvaluateAttributeAt*-style offsets, custom weights, exporting them) silently gets 0;v_interp_mov_f32of P10/P20, or of P0 of an interpolated input, needsPerVertexKHRand fails pipeline validation ("device does not support enabled VK_KHR_fragment_shader_barycentric");This removes the silent stub listed in TechnicalDebt and replaces it with exact emulation:
v_interp_p1_f32writes anInterpolateHostP1witness, andv_interp_p2_f32adds a side-effectingInterpolateHostP2(attr, chan, J, partial)witness before its usual host-interpolated write.HostInterpolationChecker, which runs after the SSA/fold/DCE/read-lane/masked-select passes:BaryCoordKHR/BaryCoordNoPerspKHRbecomevec3inputs; the linear one isNoPerspective.PerVertexKHRparameters becomeFlat vec4[3]inputs.LayoutBarycentricEmulation.RecompileResult::barycentricEmulationrecords this; the shader disk cache format goes to 8.BuildBarycentricGeometryShadergenerates a pass-through geometry stage: triangles in, a three-vertex strip out. It copies the position and the plain parameters, writes the one-hot weights (1,0,0)/(0,1,0)/(0,0,1), and copies the three vertices' values into each per-vertex array. The rasterizer then interpolates exactly the weightsBaryCoordKHRgives, with the same vertex order (gl_in[0]stays the provoking vertex). The driver inserts the stage between the vertex and fragment shaders, like the rect-list stages.ValidateShadersaccepts[Vertex, Geometry, Fragment]only for an emulated fragment shader and triangle topologies. The pipeline and validation keys treat the stage as generated, andPipelinerequiresgeometryShader.Limits. Tessellation, mesh and rect-list pipelines with such a shader throw, as do other primitive types, more than 32 fragment input locations, and interfaces past the device's geometry limits. These limits are recorded in TechnicalDebt.
Behaviour that does not change:
v_interp_p1/p2keep host interpolation and their previous SPIR-V.Sources:
v_interp_p1_f32/v_interp_p2_f32/v_interp_mov_f32(P0, P10, P20, I/J): AMD "RDNA 2" Instruction Set Architecture, section "Parameter Interpolation".VK_KHR_fragment_shader_barycentric.Tested
Ubuntu (Linux 7.0, GCC 15.2.0), Release,
-DBUILD_TESTING=ON, lavapipe (Mesa, LLVM 21), which has noVK_KHR_fragment_shader_barycentricbut hasgeometryShader. No title or hardware GPU.agc_driver_pixel_barycentric_emulation(GPU, lavapipe). It draws a triangle whose vertices have w = 1, 2.5 and 0.5 into a 64x48 R32G32B32A32 float target:v0, v1with PERSP_CENTER, then with LINEAR_CENTER, matches the perspective-correct and the screen-space barycentrics computed on the CPU within 2.5e-4 at all 932 interior pixels (hardware snaps window vertices to 1/256 pixel before computing the weights, lavapipe does not).v_interp_mov_f32P10, P20 and P0 of an interpolated input give exactlyx1-x0,x2-x0andx0.v_interp_p1/p2keeps host interpolation and matches the perspective-correct value.mainthe same exports read 0 at every pixel (e.g. pixel (32,24): 0 0 0 0, expected I=0.137, J=0.717), and thev_interp_movdraw throws.agc_driver_graphics(barycentricEmulationTests), CPU only:FragmentBarycentricKHR, noBaryCoordKHR, flat arrays);ValidateShadersaccepts the three-stage pipeline for triangle lists and strips and refuses points or a non-emulated fragment shader;agc_shader_disk_cache: the new result fields round-trip.cteston this branch (rebased onmain6b56175a): 480/480 passed, 24 GPU tests skipped by lavapipe (subgroup size 8), the same set as onmain.python3 tools/check_conventions.py --base upstream/main: passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by russhil on 2026-10-08T11:57:50Z.
On the overlap with #1252: both PRs add one call after
rectListTests();inmain()oftests/Graphics.cpp(#1252floatControlsModeTests();, this onebarycentricEmulationTests();). The changes are otherwise independent, so whichever merges second keeps both calls; I will rebase this one if #1252 lands first.Imported from boykopovar/AnyPS5. Originally posted by yahya308 on 2026-10-08T15:28:49Z.
Tested on top of main
9e2ffe0d, Windows 11, MinGW-w64 GCC 16.2.0, full ctest withANYPS5_REQUIRE_VULKAN=1 --repeat until-fail:3, NVIDIA RTX 4070 Laptop (32.0.16.1074) and AMD Radeon 780M (32.0.31041.1004).vopc_compare_tablefails on the 780M on main too (#825, #1473).Head
54e5719b. All tests outside this PR match main, but the newagc_driver_pixel_barycentric_emulationfails on both GPUs with the same value:0.237278 is what
Barycentrics()returns when the window-space corners are snapped to 1/256 pixel (8 subpixel bits) before the edge functions. The unsnapped reference differs from the snapped one by up to 1.43e-4 in the perspective weights (19 of the 932 interior pixels exceed 1e-4) and by 4.2e-5 in the linear ones. Locally, either change makes the test pass on both GPUs (932 perspective and 932 linear pixels, plus thev_interp_movand p1/p2 checks):window[i]to 1/256 inBarycentrics(), or1e-4ftolerances to2.5e-4f.I did not check lavapipe. If it weights unsnapped positions, the snapped reference could fail there, so the tolerance change may be the safer one.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by russhil on 2026-10-08T15:40:50Z.
Thanks @yahya308, confirmed: with window vertices snapped to 1/256 my reference gives the same 0.237278 at (53, 36), and up to 1.43e-4 off the unsnapped weights. I took the tolerance option (both checks now 2.5e-4, 1e08abbb, rebased on main) because snapping the reference fails on lavapipe, which reads 0.237381 there: llvmpipe snaps to 1/256 only for coverage (
lp_rast.hFIXED_ORDER 8,subpixel_snapinlp_setup_tri.c) and builds the interpolation planes from the float positions (init_argsinlp_state_setup.c). Full suite 464/464 on lavapipe.Imported from boykopovar/AnyPS5. Originally posted by russhil on 2026-10-09T06:58:14Z.
Thanks @yahya308, the hardware traces settled it. In 2c80448d (rebased on main
f1451396) the test carries both references:BarycentricsSnapped()snaps the window corners to 1/256 before the edge functions,BarycentricsUnsnapped()is the previous math, and every interior I/J and p1/p2 pixel must match one of them within the original 1e-4. At (53, 36) channel 1 they give 0.237278 and 0.237381, your reading and lavapipe's. Lavapipe still passes (495/495). If you have time, could you re-runagc_driver_pixel_barycentric_emulationon the 4070 and the 780M to confirm it now passes within 1e-4 on both?Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T00:17:28Z.
Checked on lavapipe (
geometryShader, no barycentrics):agc_driver_graphics,agc_shader_disk_cacheandagc_driver_pixel_barycentric_emulationpass withANYPS5_REQUIRE_VULKAN=1, and the new tests don't build without the change.The branch conflicted with
main(tests/Graphics.cpp,tests/ShaderDiskCache.cpp, thestatic_assertsizes inShaderDiskCache.cpp,TechnicalDebt.md). I rebased it and added one change:ShaderDiskCache::FormatVersionis 22, sincemainalready took 21. Rebuilt and reran the scoped ctest (291/291) after the rebase.Not checked here: behaviour on NVIDIA/Windows hardware. @yahya308, if you can, please rerun
agc_driver_pixel_barycentric_emulationon the 4070 and the 780M with the current head.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:04:28Z.
Checked on lavapipe (
geometryShader, no barycentrics):agc_driver_graphics,agc_shader_disk_cacheandagc_driver_pixel_barycentric_emulationpass withANYPS5_REQUIRE_VULKAN=1, and the new tests don't build without the change.The branch conflicted with
mainagain (Pipeline.cpp,pipelineKey). I rebased it and added one change:ShaderDiskCache::FormatVersionis 23, sincemainalready took 22. Rebuilt and reran the scoped ctest (294/294) after the rebase.Not checked here: behaviour on NVIDIA/Windows hardware. @yahya308, if you can, please rerun
agc_driver_pixel_barycentric_emulationon the 4070 and the 780M with the current head.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T06:07:31Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/shader/recompiler/ShaderDiskCache.hppL15core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ProgramMetadata.hppL21