shader: pass vertex clip distances through the rect list tessellation stages (GTA V) #1145
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1145
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1145"
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?
@AN0DIN on GitHub · 2026-10-07 14:56 UTC · original
AN0DIN:pr/kyty/rectlist-clip-distance→main· +274 −56 · 10 files · 1 commits · openProblem
Since
86414760the SPIR-V backend appends a clip plane to vertex shaders that export a position (it culls zerohomogeneous positions; skipped when all eight clip and cull distances are already in use), and
feed075aadded user clipplanes, so nearly every vertex shader writes clip distances. The backend wrote them in a separate
gl_ClipDistancevariable. Rect lists are emulated with generated TCS/TES stages, and those stages only declared the position. On macOS
(MoltenVK, where tessellation runs through Metal compute) this goes wrong without any message:
nothing: a standalone draw covered 0 of 1024 pixels with MoltenVK 1.4.2, and in game (my macOS working branch:
mainplus unmerged macOS fixes, with an experimental MoltenVK build; see the earlier testing below) GTA V'sdeferred lighting stayed black;
mainfrom November 1 and MoltenVK 1.4.2, GTA V drew the characters (the player included) as translucentsilhouettes. A local build that leaves out the zero-position plane drew them whole, and so does this change. I have
not traced which draws are involved.
Change
member 1 of
gl_PerVertex(Position,ClipDistance[N]) instead of a separategl_ClipDistanceoutput, so thevertex output block matches the control shader's input block (Vulkan interface matching of built-in blocks).
DistancePointergives every writer that member. Shaders without clip distances keep the one-member block. Thisapplies to every vertex shader that writes clip distances, on all platforms.
them for the fourth one, like the position; the TES passes them on. Both stages declare the
ClipDistancecapabilitywhen they carry clip distances.
CountOutputDistancesgives the SPIR-V backend and the rect list generator one count. Rect lists with user clipplanes are still refused by
PipelineCache, so the generated stages carry the vertex shader's own distances and thezero-position plane; the comment there now says so.
CheckRectListShaderscovers a vertex program with a shader clip distance, and compiles a vertex shader to checkthat its clip distances are member 1 of its built-in block, the block the control shader declares, without and with
two user clip planes. It fails if the vertex shader writes a separate
gl_ClipDistancevariable.shader_recompiler_compute_tests --rect-list-onlyruns it alone.shader_cfg_testsexpected the separate variable:TestRepeatedExportsHaveOneInterfacenow expects one outputvariable whose
ClipDistancemember holds the plane,TestNewShaderRecompilerAuxPositionExportslooks for themember decoration, and the
SpirvStoredBuiltInElementshelper reads the element index of an arrayed block member,so the expected clip distance elements are unchanged.
Known limitation: other built-ins a rect list's vertex shader may write (
PointSize,Layer, cull distances) arestill not passed on, so such a draw may still render nothing on MoltenVK.
Testing
main(02b17af3): emulator and testtargets build; ctest 14/14 (the macOS CI selection);
shader_recompiler_compute_tests --rect-list-onlypasses, andfails when the backend is forced to write a separate
gl_ClipDistancevariable (checked by hand).shader_cfg_tests(not in the macOS CI selection), on the same machine:mainstops atTestGpuProducedWritableDescriptor("GPU-produced descriptor lost current scalar data or repeated its reads"), whichthis change does not touch. This branch without the
shader_cfgupdates stopped earlier, atTestRepeatedExportsHaveOneInterface. With them it stops at the same check asmain, with the same output apartfrom source line numbers, and with that one check skipped locally every other check passes.
shader_recompiler_compute,shader_recompiler_tessellation,graphics_pipeline_rasterization,graphics_draw_offsets); the fullshader_recompiler_compute_testsneeds fragment barycentrics and 64-bit shared/image atomics, which MoltenVK doesnot report here.
mainfrom November 1 plus the macOS fixes GTA V needs on this machine (not inmain): without thischange, the characters were translucent silhouettes on all 5 launches (one of them on the commit this change was
applied to); with it (the same code before the test changes and formatting), they were drawn whole and opaque on
both launches (loading Story Mode, then a benchmark tour). A local build without the zero-position plane also drew
the player whole (1 launch).
part of this PR): one rect list drawn through the generated TCS/TES, with a vertex shader that writes a position and
two clip distances, 64x64 target, covered pixels counted on the host (the rectangle covers 1024 pixels):
The generated TCS/TES and the vertex shader pass
spirv-val --target-env vulkan1.2.them in gl_PerVertex on every platform.
gl_PerVertexblock change), kept for reference:--rect-list-onlypasses. The fullshader_recompiler_compute_testscannot run there: its GPU harness requires a device with fragment barycentricsand 64-bit shared/image atomics, which MoltenVK does not report here.
earlier version of this change (before the capability line was added), with the vertex shader GTA V uses for that
lighting pass: with MoltenVK 1.4.2, 0 of 1024 pixels covered when the stages leave out the clip distances, 1024
of 1024 with them; the same draw as a plain triangle list covers 496 (about half the rectangle). Same result
with an experimental MoltenVK build (carbonimax's mesh shader branch).
plus unmerged macOS fixes, that experimental MoltenVK build): GTA V's lighting came back.
--rect-list-onlypasses.main(
bb98f264) alone fails the same three tests on this machine (kernel_file_system, exit code 0xc0000409;shader_recompiler_compute;compute_meta_clear_classification). On this branchshader_recompiler_computestops at a fatal error in the
ResourceMaterializationpass, which this change does not touch, afterRectListShaders(and the checks before it) passed.route (standing at the spawn point, then two replayed clips), on TheCruZ's fork (v7) plus four local patches
unrelated to rect lists, with and without that version of the fix, two runs each, alternated (A B B A). All four
runs completed the route without a crash. The four screenshots taken during the clips differ between builds by the
same order as between two runs of the same build (mean absolute difference per channel, sampled every 8th pixel:
up to 7.8/255 across builds, up to 7.5/255 within a build). Frame rate was not compared conclusively (two runs per
build); no performance change is claimed.
AI assistance
An AI coding assistant (Claude Code) found the cause with me, wrote the change, the tests and this description, and
ran the builds, tests and game runs above. I reviewed the change, this text and the results, and checked the game's
rendering myself.
@coderabbitai[bot] on GitHub · 2026-10-07 15:01 UTC · original · edited 2026-11-02 10:27 UTC
Findings
ac3f5a6d-3808-4c53-b6d2-de73f35496e1Reviewing files that changed from the base of the PR and between d607e5d4a660edb47def9f5383916eb5f56f59bb and
1cebcc6358.src/graphics/host_gpu/renderer/pipeline/pipelineCache.cppsrc/graphics/host_gpu/renderer/pipeline/shaders.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterFlow.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterModule.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/rectListShader.cppsrc/graphics/shader/rectListShader.htests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Walkthrough
A shared helper now counts clip and cull distances, user clip planes, and the invalid-position plane. The SPIR-V emitter can store clip distances in
gl_PerVertexwhen the shader exports a position. Generated rectangle-list tessellation stages now receive and propagate clip distances. Tests cover distance declarations, output-block matching, and stored built-in array elements.Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Moderate · up to
1cebcA rectangle with an invalid source position can still render part of its geometry. Correct the fourth-corner clipping behavior before merging.
Pre-merge checks:
4 ·
1
Full details: Title check
Explanation
The title names the main change: vertex clip distances are passed through the generated rect-list tessellation stages. The raw summary confirms the TCS/TES changes, the gl_PerVertex block change, and the rect-list shader check. The parenthetical GTA V reference is extra context and does not make the title off-topic.
Full details: Description check
Explanation
The description is closely related to the changeset. It explains the problem with clip distances written to a separate gl_ClipDistance variable, the gl_PerVertex block change, the TCS/TES updates, CountOutputDistances, the PipelineCache comment, the CheckRectListShaders test, and the shader_cfg_tests updates. It also covers testing and known limitations.
Full details: Docstring Coverage
Explanation
Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 18 functions across 8 files. (2 skipped: 2 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@ -39,0 +45,4 @@{{spv::OpMemberDecorate, {0u, spv::DecorationBuiltIn, spv::BuiltInPosition}},{spv::OpMemberDecorate, {1u, spv::DecorationBuiltIn, spv::BuiltInClipDistance}},{spv::OpDecorate, {spv::DecorationBlock}}},vec4_float_type, Array(float_type, clip_distance_count));@coderabbitai[bot] on GitHub · 2026-10-07 15:01 UTC · original
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Match the vertex and control-stage built-in interfaces.
When the vertex shader exports a clip distance,
src/graphics/shader/recompiler/backend/spirv/spirvEmitterModule.cppputsPositionin a one-member block andClipDistancein a separate output variable. This change makes the control-stage input a two-member block. Vulkan requires the control-stage input block to match the preceding output interface. The mismatch can make rect-list pipeline creation invalid, so the new distance reads do not establish a valid pass-through. Use matching built-in block layouts across the two stages. (docs.vulkan.org)@coderabbitai[bot] on GitHub · 2026-10-07 15:01 UTC · original · reviewed
ℹ️ Review info
⚙️ Run configuration
aa10cafa-3aad-4d0b-a7eb-e1f411fb3101📥 Commits
Reviewing files that changed from the base of the PR and between
d2413fc2eband 9004d98196376a58c1b9800b1b9f250d959439b2.📒 Files selected for processing (4)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterModule.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/rectListShader.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@AN0DIN on GitHub · 2026-10-12 13:51 UTC · original
Good catch, thanks. Built-ins only take part in interface matching inside the built-in block, and the
control shader's input block has to match the vertex shader's output block member by member. The
vertex shader now writes its clip distances as member 1 of gl_PerVertex (Position, ClipDistance[N])
instead of a separate gl_ClipDistance variable, so it declares the same block as the generated
TCS/TES. Shaders without clip distances are unchanged, and the test now compiles a real vertex
shader and checks that its block matches the control shader's.
Tested on macOS (Apple M5, Rosetta 2, MoltenVK 1.4.2): ctest 14/14,
--rect-list-onlypasses, thegenerated shaders pass
spirv-val, and a standalone rect list draw covers 1024/1024 pixels with thenew layout. Not retested in game yet; Windows/Linux not tested locally.
@AN0DIN on GitHub · 2026-11-02 10:11 UTC · original
Rebased on current
main(02b17af3): the previous head no longer merged afterbfc37762andfeed075a. I updated thedescription (problem, change, testing) and added "(GTA V)" to the title.
What changed:
feed075aadded user clip planes. When a shader exports a position, all its clip distances (its own, the user planes,then the zero-position plane) are now member 1 of
gl_PerVertex;DistancePointergives every writer that member.Rect lists with user clip planes are still refused by
PipelineCache, so the generated stages carry the vertexshader's own distances and the zero-position plane, counted by the same
CountOutputDistances; the comment therenow says so.
adapted to the new code, and also covers two user clip planes. It fails when the backend is forced to write a
separate
gl_ClipDistancevariable.shader_cfgchecks expected the separategl_ClipDistancevariable; they now look for theClipDistancemember of
gl_PerVertex, with the same expected elements.Tested on macOS (Apple M5, x86_64 build under Rosetta 2, MoltenVK 1.4.2): build OK, ctest 14/14 (the macOS CI
selection),
--rect-list-onlypasses.shader_cfg_testsstops at the same check asmainon this machine(
TestGpuProducedWritableDescriptor, not touched here), and every other check passes with that one skipped locally.In game this time, with
mainfrom November 1 plus the macOS fixes GTA V needs on this machine: without the change thecharacters were translucent silhouettes (5 launches); with it (the same code before the test changes and formatting)
they were drawn whole (2 launches). Windows and Linux not tested.
@ -110,0 +125,4 @@values[i] =Load(float_type, Access(ptr_input_float, gl_in, Int(i), Int(1), Int(c)));}const auto fourth = InterpolateScalar(values, barycentric);@coderabbitai[bot] on GitHub · 2026-11-02 10:27 UTC · original
Keep the invalid-position plane negative at the fourth corner.
If the first input vertex has a zero position, the vertex shader writes
-1to its reserved clip plane and writes0for valid vertices.InterpolateScalarapplies weights{-1, 1, 1}to a rectangle’s fourth corner, so this plane becomes+1. The triangle formed from that corner and the two valid input vertices can then render instead of collapsing at the clip plane. Handle the reserved plane separately from shader-authored clip distances; propagate an invalid input to the fourth corner. Vulkan treats positive clip distances as inside the clip half-space. (docs.vulkan.org)@coderabbitai[bot] on GitHub · 2026-11-02 10:27 UTC · original · reviewed
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.