fix(shader): follow carry adds and a sole hull invocation in tessellation addresses #22327
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22327"
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 B-Howell on 2026-11-17T12:05:55Z.
What
A PS4 hull shader built with GCN
v_add_i32was refused by the tessellation lowering:the tessellation ring or LDS address is not affine in the control point and relative patch. The translator lowersv_add_i32/v_add_co_u32toIAddCarry32and takes the sum from component 0 of the pair (ControlFlowInstructions.cppaddU32), and the address evaluator did not follow that. This PR teaches the evaluator inTessellationLowering.cppthe shapes GCN address code uses:IAddCarry32(a, b)is evaluated asa + b, sharing the add/subtract code withIAdd32, so the existing 32-bit bound still refuses a sum that can wrap. Component 1, the carry, is 0 when both operands are non-dynamic and the highest sum fits in 32 bits.IAddCarry32is added to the operations that carry relative-patch bits for the data-use check.SelectU32whose condition can never hold takes its false arm. This coversv_addc_u32carry-in andv_subb_u32borrow-in fed by a carry or borrow that is proven 0: anINotEqual32of two equal scalars, aULessThan32whose left side is never below the right side, or aLogicalOrof such conditions.ShiftRightArithmetic32sign-extends a scalar exactly. For a varying value it is accepted only below the sign bit, where it equals a logical shift. Bloodborne's patch-constant base goes throughs_bfe_i32.BitFieldSExtract(the operands ofv_mul_i32_i24/v_mad_i32_i24,IntegerInstructions.cppinteger24) sign-extends a scalar exactly. For a varying value it is accepted only when the field stays below its sign bit, where it equals the unsigned field.InvocationID * 4andInvocationID * 16, which the quad-factor check would otherwise refuse. shadPS4 makes the same substitution for pass-through hulls (hull_shader_transform.cppat143b58aa48: "Deal with annoying pattern in BB where InvocationID use makes no sense"). The merged-hull LDS record check now also accepts a zero invocation coefficient when there is one output control point.Nothing is keyed to a title, a shader or a value.
Tested
Windows 11 Pro 10.0.26300, WinLibs GCC 15.2, RTX 4080 SUPER, driver 621.14.
tests/TessellationLowering.cppbuilds the IR the translator emits, with 12 new cases:IAddCarry32; a carry that only feeds a high-word add of zero; an add with carry-in from an add that cannot wrap; a subtract whose borrow-in cannot be set; quad factors atrel * 24 + (InvocationID * 16 + 16k) >> 2in a one-control-point hull (Bloodborne's shape); a signed 24-bit multiply-add of non-negative operands; an arithmetic shift right (s_bfe_i32) of a value below the sign bit; and a merged one-control-point hull writing its LDS record atrel * 16 + InvocationID * 16.main, the six lowering cases that existed before the arithmetic-shift case was added fail (not affine, orwrites tessellation factors outside its patch's quad factors). The merged-hull case and the 3 refusals pass on both.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_ADD_LAYER_PATHset to the 1.4.363.1 SDK,VK_LOADER_DEBUG=layer, ctest-Vonagc_tessellation_lowering,agc_tessellation_factor_ring,agc_tessellation_pipeline_cache,agc_driver_tessellation,agc_driver_tessellation_geometryandagc_driver_tessellation_depth_range_remap:main: 5/6 pass; onlyagc_tessellation_loweringfails, on the new cases.VUID-.Insert instance layer "VK_LAYER_KHRONOS_validation"lines, nowas not found but was requested by env var VK_INSTANCE_LAYERS, and noVUID-.0x2d56e0100pc 0xdc (not affine). With this PR its six factor stores and six patch-constant LDS writes lower, and the draw renders. The game then stops at a different draw:VGT_SHADER_STAGES_EN=0xb0(ES, GS and the VS copy shader), which is unsupported. Gameplay is not reached yet.0b5762c8e1merged onto mainf0c01bc69d(comment): Arch Linux, GCC 16.2.1 Release, NVIDIA RTX 4090 (Ada), driver 620.70.06, validation layer 1.4.363.1, same six tests and env: 6/6 pass on both main and this PR, 5 layer-insert lines each, nowas not foundline, noVUID-.check_conventionspasses.pr_overlapfailed closed: #10249 and #20111 also changeTessellationLowering.cppbut conflict withmain, so it could not compare them. I read both diffs by hand: neither changes the address evaluator.s_bfe_i32.0b5762c8e1merged onto main7c836568c8: Linux 7.3.4, GCC 16.2.1, Mesa 26.2.6 RADV, RX 7900 XT (gfx1100) and 7800X3D iGPU (gfx1036), Khronos layer 1.4.363.1. Full suite and-R '^agc'with the layer: no new failures against main on either GPU, the layer loads in every run, and there are 0VUID-and 0SYNC-HAZARDlines.video_out_hdr_presentfailed once in a parallel run and passes 3 of 3 alone on main and on the PR.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T19:05:36Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
Head
0b5762c8e1f0d164a152c837c57c6ba62d9b143a, basemain. No build and no test suite ran in this review. I read the change from the diff, and the code at head throughrepo_read.py:Approval is held back because nothing was built or run here, and the translator side of the claims was not read.
Results
Findings
Blocking
None. I checked the new carry, select, arithmetic-shift and signed-field rules against their conditions and found no case where they accept an address they should refuse. The InvocationID change is gated on
vertexCount == 1, which is only set for a one-control-point hull; the capture path setslaneVertexand does not reach it.Non-blocking
core/libs/prx/libSceAgcDriver/tests/TessellationLowering.cpp:295:WritesOwnFieldsprints every written address to stderr when the check fails. The same test file already printsrefused:on failure, so this matches that style; remove it if the helper should stay quiet.Sources
Repository paths at
0b5762c8e1f0, no external links:core/shader/recompiler/Optimization/src/TessellationLowering.cpp:554to:696: the affine evaluator, including the newCompositeExtractU32x2,ShiftRightArithmetic32andBitFieldSExtractcases.core/shader/recompiler/Optimization/src/TessellationLowering.cpp:863:vertexCountis the output control point count for the control stage, and 1 only for capture.Not verified
addU32inControlFlowInstructions.cppand thev_addc_u32andv_subb_u32lowering that producesIAddCarry32. I did not read it, so the claim that those shapes reach the evaluator is not verified.143b58aa48.pr_overlapnote about #10249 and #20111.core/libs/prx/libSceAgcDriver/tests/TessellationLowering.cppwas read through its diff hunks, which hold every change to it. The rest of the file was not read.To get this to a good state
Nothing is required. The optional point is the one non-blocking note above.