fix(shader): follow carry adds and a sole hull invocation in tessellation addresses #22327

Merged
B-Howell merged 1 commit from upstream-pr/22327 into main 2026-11-19 16:43:27 +00:00
B-Howell commented 2026-11-17 12:05:55 +00:00 (Migrated from github.com)

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_i32 was refused by the tessellation lowering: the tessellation ring or LDS address is not affine in the control point and relative patch. The translator lowers v_add_i32 / v_add_co_u32 to IAddCarry32 and takes the sum from component 0 of the pair (ControlFlowInstructions.cpp addU32), and the address evaluator did not follow that. This PR teaches the evaluator in TessellationLowering.cpp the shapes GCN address code uses:

  • Add with carry-out. Component 0 of IAddCarry32(a, b) is evaluated as a + b, sharing the add/subtract code with IAdd32, 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. IAddCarry32 is added to the operations that carry relative-patch bits for the data-use check.
  • Carry-in and borrow-in. A SelectU32 whose condition can never hold takes its false arm. This covers v_addc_u32 carry-in and v_subb_u32 borrow-in fed by a carry or borrow that is proven 0: an INotEqual32 of two equal scalars, a ULessThan32 whose left side is never below the right side, or a LogicalOr of such conditions.
  • Arithmetic shifts. ShiftRightArithmetic32 sign-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 through s_bfe_i32.
  • Signed 24-bit fields. BitFieldSExtract (the operands of v_mul_i32_i24 / v_mad_i32_i24, IntegerInstructions.cpp integer24) 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.
  • A single hull invocation. In a hull program with one output control point, InvocationID is always 0, so the evaluator takes it as 0. Bloodborne's factor and patch-constant addresses add InvocationID * 4 and InvocationID * 16, which the quad-factor check would otherwise refuse. shadPS4 makes the same substitution for pass-through hulls (hull_shader_transform.cpp at 143b58aa48: "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.cpp builds the IR the translator emits, with 12 new cases:
    • 8 that must lower: a ring address summed by 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 at rel * 24 + (InvocationID * 16 + 16k) >> 2 in 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 at rel * 16 + InvocationID * 16.
    • 4 that must be refused: an add whose carry-in can be set, an add with carry-out that can wrap, an arithmetic shift of a value that can reach the sign bit, and the same invocation-scaled factor address in a hull with several control points.
    • On main, the six lowering cases that existed before the arithmetic-shift case was added fail (not affine, or writes tessellation factors outside its patch's quad factors). The merged-hull case and the 3 refusals pass on both.
  • Validation: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_ADD_LAYER_PATH set to the 1.4.363.1 SDK, VK_LOADER_DEBUG=layer, ctest -V on agc_tessellation_lowering, agc_tessellation_factor_ring, agc_tessellation_pipeline_cache, agc_driver_tessellation, agc_driver_tessellation_geometry and agc_driver_tessellation_depth_range_remap:
    • This PR: 6/6 pass.
    • main: 5/6 pass; only agc_tessellation_lowering fails, on the new cases.
    • After the arithmetic-shift case was added, this PR was run again: 6/6, 0 VUID-.
    • Both logs have 5 Insert instance layer "VK_LAYER_KHRONOS_validation" lines, no was not found but was requested by env var VK_INSTANCE_LAYERS, and no VUID-.
  • Bloodborne CUSA00900 1.09 (a local tree with #22251, this PR and #22328): Continue used to stop at about 49 s at hull 0x2d56e0100 pc 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.
  • Tested by domx4q on head 0b5762c8e1 merged onto main f0c01bc69d (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, no was not found line, no VUID-.
  • check_conventions passes. pr_overlap failed closed: #10249 and #20111 also change TessellationLowering.cpp but conflict with main, so it could not compare them. I read both diffs by hand: neither changes the address evaluator.
  • Independent review (Codex): the first pass was NO-GO, because the single-invocation rule refused merged one-control-point hulls. That is fixed, with a test. The second pass was GO. The arithmetic-shift case was added after both passes, when the Bloodborne run reached s_bfe_i32.
  • Colewiz7 on head 0b5762c8e1 merged onto main 7c836568c8: 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 0 VUID- and 0 SYNC-HAZARD lines. video_out_hdr_present failed once in a parallel run and passes 3 of 3 alone on main and on the PR.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: CUSA00900 Bloodborne #3094
  • GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 621.14, Windows 11 Pro 10.0.26300; NVIDIA GeForce RTX 4090, driver 620.70.06, Arch Linux (domx4q); AMD Radeon RX 7900 XT and 7800X3D iGPU, Mesa 26.2.6 RADV, Linux 7.3.4 (Colewiz7)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5356486618 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22327). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-11-17T12:05:55Z. --- ### What A PS4 hull shader built with GCN `v_add_i32` was refused by the tessellation lowering: `the tessellation ring or LDS address is not affine in the control point and relative patch`. The translator lowers `v_add_i32` / `v_add_co_u32` to `IAddCarry32` and takes the sum from component 0 of the pair (`ControlFlowInstructions.cpp` `addU32`), and the address evaluator did not follow that. This PR teaches the evaluator in `TessellationLowering.cpp` the shapes GCN address code uses: - **Add with carry-out.** Component 0 of `IAddCarry32(a, b)` is evaluated as `a + b`, sharing the add/subtract code with `IAdd32`, 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. `IAddCarry32` is added to the operations that carry relative-patch bits for the data-use check. - **Carry-in and borrow-in.** A `SelectU32` whose condition can never hold takes its false arm. This covers `v_addc_u32` carry-in and `v_subb_u32` borrow-in fed by a carry or borrow that is proven 0: an `INotEqual32` of two equal scalars, a `ULessThan32` whose left side is never below the right side, or a `LogicalOr` of such conditions. - **Arithmetic shifts.** `ShiftRightArithmetic32` sign-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 through `s_bfe_i32`. - **Signed 24-bit fields.** `BitFieldSExtract` (the operands of `v_mul_i32_i24` / `v_mad_i32_i24`, `IntegerInstructions.cpp` `integer24`) 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. - **A single hull invocation.** In a hull program with one output control point, InvocationID is always 0, so the evaluator takes it as 0. Bloodborne's factor and patch-constant addresses add `InvocationID * 4` and `InvocationID * 16`, which the quad-factor check would otherwise refuse. shadPS4 makes the same substitution for pass-through hulls (`hull_shader_transform.cpp` at `143b58aa48`: "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.cpp` builds the IR the translator emits, with 12 new cases: - 8 that must lower: a ring address summed by `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 at `rel * 24 + (InvocationID * 16 + 16k) >> 2` in 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 at `rel * 16 + InvocationID * 16`. - 4 that must be refused: an add whose carry-in can be set, an add with carry-out that can wrap, an arithmetic shift of a value that can reach the sign bit, and the same invocation-scaled factor address in a hull with several control points. - On `main`, the six lowering cases that existed before the arithmetic-shift case was added fail (`not affine`, or `writes tessellation factors outside its patch's quad factors`). The merged-hull case and the 3 refusals pass on both. - Validation: `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_ADD_LAYER_PATH` set to the 1.4.363.1 SDK, `VK_LOADER_DEBUG=layer`, ctest `-V` on `agc_tessellation_lowering`, `agc_tessellation_factor_ring`, `agc_tessellation_pipeline_cache`, `agc_driver_tessellation`, `agc_driver_tessellation_geometry` and `agc_driver_tessellation_depth_range_remap`: - This PR: 6/6 pass. - `main`: 5/6 pass; only `agc_tessellation_lowering` fails, on the new cases. - After the arithmetic-shift case was added, this PR was run again: 6/6, 0 `VUID-`. - Both logs have 5 `Insert instance layer "VK_LAYER_KHRONOS_validation"` lines, no `was not found but was requested by env var VK_INSTANCE_LAYERS`, and no `VUID-`. - Bloodborne CUSA00900 1.09 (a local tree with #22251, this PR and #22328): Continue used to stop at about 49 s at hull `0x2d56e0100` pc 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. - Tested by domx4q on head `0b5762c8e1` merged onto main `f0c01bc69d` ([comment](https://github.com/boykopovar/AnyPS5/pull/22327#issuecomment-6952891176)): 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, no `was not found` line, no `VUID-`. - `check_conventions` passes. `pr_overlap` failed closed: #10249 and #20111 also change `TessellationLowering.cpp` but conflict with `main`, so it could not compare them. I read both diffs by hand: neither changes the address evaluator. - Independent review (Codex): the first pass was NO-GO, because the single-invocation rule refused merged one-control-point hulls. That is fixed, with a test. The second pass was GO. The arithmetic-shift case was added after both passes, when the Bloodborne run reached `s_bfe_i32`. - [Colewiz7](https://github.com/boykopovar/AnyPS5/pull/22327#issuecomment-7011450338) on head `0b5762c8e1` merged onto main `7c836568c8`: 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 0 `VUID-` and 0 `SYNC-HAZARD` lines. `video_out_hdr_present` failed once in a parallel run and passes 3 of 3 alone on main and on the PR. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: CUSA00900 Bloodborne #3094 - [x] GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 621.14, Windows 11 Pro 10.0.26300; NVIDIA GeForce RTX 4090, driver 620.70.06, Arch Linux (domx4q); AMD Radeon RX 7900 XT and 7800X3D iGPU, Mesa 26.2.6 RADV, Linux 7.3.4 (Colewiz7) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-17 19:05:36 +00:00 (Migrated from github.com)

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, base main. No build and no test suite ran in this review. I read the change from the diff, and the code at head through repo_read.py:

repo_read.py search "evaluate" core/shader/recompiler/Optimization/src/TessellationLowering.cpp
repo_read.py cat core/shader/recompiler/Optimization/src/TessellationLowering.cpp 380 700
repo_read.py cat core/shader/recompiler/Optimization/src/TessellationLowering.cpp 1086 1104
repo_read.py cat core/shader/recompiler/Optimization/src/TessellationLowering.cpp 1672 1727

Approval is held back because nothing was built or run here, and the translator side of the claims was not read.

Results

Check Result Note
Diff read passed both changed files, all hunks
Evaluator at head passed carry, select, shifts, fields
Test suite not run author reports 6/6 on two GPUs
Build not run nothing built here

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 sets laneVertex and does not reach it.

Non-blocking

  • core/libs/prx/libSceAgcDriver/tests/TessellationLowering.cpp:295: WritesOwnFields prints every written address to stderr when the check fails. The same test file already prints refused: 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:554 to :696: the affine evaluator, including the new CompositeExtractU32x2, ShiftRightArithmetic32 and BitFieldSExtract cases.
  • core/shader/recompiler/Optimization/src/TessellationLowering.cpp:863: vertexCount is the output control point count for the control stage, and 1 only for capture.

Not verified

  • The build and the six ctest targets. The results in the description are the author's, on RTX 4080 SUPER (Windows) and RTX 4090 (Arch). I did not re-run them, and the new refusal cases were not run.
  • The translator side: addU32 in ControlFlowInstructions.cpp and the v_addc_u32 and v_subb_u32 lowering that produces IAddCarry32. I did not read it, so the claim that those shapes reach the evaluator is not verified.
  • The Bloodborne CUSA00900 run, and the shadPS4 reference 143b58aa48.
  • The pr_overlap note about #10249 and #20111.
  • core/libs/prx/libSceAgcDriver/tests/TessellationLowering.cpp was 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5944803593 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22327#pullrequestreview-5944803593). Originally posted by [MarcZX10](https://github.com/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`, base `main`. No build and no test suite ran in this review. I read the change from the diff, and the code at head through `repo_read.py`: ``` repo_read.py search "evaluate" core/shader/recompiler/Optimization/src/TessellationLowering.cpp repo_read.py cat core/shader/recompiler/Optimization/src/TessellationLowering.cpp 380 700 repo_read.py cat core/shader/recompiler/Optimization/src/TessellationLowering.cpp 1086 1104 repo_read.py cat core/shader/recompiler/Optimization/src/TessellationLowering.cpp 1672 1727 ``` Approval is held back because nothing was built or run here, and the translator side of the claims was not read. ## Results | Check | Result | Note | |---|---|---| | Diff read | passed | both changed files, all hunks | | Evaluator at head | passed | carry, select, shifts, fields | | Test suite | not run | author reports 6/6 on two GPUs | | Build | not run | nothing built here | ## 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 sets `laneVertex` and does not reach it. ### Non-blocking - `core/libs/prx/libSceAgcDriver/tests/TessellationLowering.cpp:295`: `WritesOwnFields` prints every written address to stderr when the check fails. The same test file already prints `refused:` 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:554` to `:696`: the affine evaluator, including the new `CompositeExtractU32x2`, `ShiftRightArithmetic32` and `BitFieldSExtract` cases. - `core/shader/recompiler/Optimization/src/TessellationLowering.cpp:863`: `vertexCount` is the output control point count for the control stage, and 1 only for capture. ## Not verified - The build and the six ctest targets. The results in the description are the author's, on RTX 4080 SUPER (Windows) and RTX 4090 (Arch). I did not re-run them, and the new refusal cases were not run. - The translator side: `addU32` in `ControlFlowInstructions.cpp` and the `v_addc_u32` and `v_subb_u32` lowering that produces `IAddCarry32`. I did not read it, so the claim that those shapes reach the evaluator is not verified. - The Bloodborne CUSA00900 run, and the shadPS4 reference `143b58aa48`. - The `pr_overlap` note about #10249 and #20111. - `core/libs/prx/libSceAgcDriver/tests/TessellationLowering.cpp` was 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!22327
No description provided.