fix(agc): fuse geometry shader halves into one self-contained program #124

Merged
oneandonlydean merged 1 commit from upstream-pr/124 into main 2026-10-01 07:12:31 +00:00
oneandonlydean commented 2026-09-29 00:32:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T00:32:53Z.


What

Rebased onto current main. This PR now replaces only the geometry path of sceAgcUnknownFuseShaderHalves (fd5Bp5tGTgo) and sceAgcUnknownGetFusedShaderSize (dolOmWH+huQ). Hull halves keep the register-level fusion from fd4700b9 unchanged, and the half-type and VGT_SHADER_STAGES_EN checks stay shared.

Why the geometry path changes. fd4700b9 fuses geometry halves at the register level: the back half's registers with SPI_SHADER_PGM_LO_ES pointing at the front half's code. The driver then compiles that front half as the draw's whole program (type 4, Stage::Mesh) and skips the back (Role::GeometryBack). Nothing links the two halves (LinkedProgram is only serialized, and DecodeRdnaFrontProgram has no caller). The front half ends in s_setpc_b64 s[6:7] with no s_endpgm, so decoding runs on into the s_code_end padding and the program trailer. In PPSA21564 the geometry-shader draws are skipped:

[gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x514080000]
[gpu] skipped draw: unsupported scalar source operand code 115 at program counter 64 [DRAW_INDEX_INDIRECT, color target 0x514080000]

Hull halves work with the register-level fusion, since the tessellation path compiles the local and hull programs separately, so they are left as they are.

Geometry halves are fused into one self-contained Gs program:

  • Code: the front half's code up to its final s_setpc_b64 s[6:7] (its length comes from the program trailer), then s_nop padding to 256 bytes, then the whole back half. The front falls through into the back.
  • SH registers: the back half's set, with these changes:
    • PGM_LO/HI_GS points at the copied back code, and the ES program address at the fused code.
    • RSRC1 takes the larger VGPR and SGPR counts of the two halves.
    • RSRC2 takes the front's user-SGPR count. The SPI loads the front layout, and the back reloads its own layout from memory through s[0:1].
    • The front's other SH registers are appended, except its checksum.
  • Context registers: merged the same way.
  • Other tables: inputs come from the front half. Outputs, specials and user data come from the back half. The scratch size is the larger of the two.
  • Memory layout: everything the fused Shader points to, including a copy of the header, is written into the caller's memory after the code, and the header is registered with the driver as a self-contained shader. Titles pass memory without honouring the reported alignment (found by @Pacuka with The Smurfs Dreams), so the program is placed at the first 256-byte boundary inside it, and GetFusedShaderSize returns the total size plus 255 bytes of padding, with log2 alignment 8. SizeAlign moves into ShaderFusion.hpp unchanged, so the test can call both functions.
  • Throws: halves with embedded constant buffers, a front half without a trailer or without a final s_setpc_b64 s[6:7], a front half needing RSRC2 state the back half doesn't enable, geometry halves without fused memory, missing tables, and a program address above 48 bits. Mismatched half types and stage bits return 0x8a6c0008 as on main.
  • Driver change: s[0:1] of a Gs program is now loaded from its user-data address registers whenever they are set. Before, this only happened for GsFront programs. The back half of a fused program reads its user data through them. A program without those registers keeps zero there (SamG-Coder's fix, 8345fbc1).

Tested

  • Linux (CachyOS, GCC, RTX 4090). agc_shader_fusion_tests, registered in ctest:
    • geometry fusion into scratch memory that is aligned, misaligned by 0xa0, and misaligned by 4 bytes: code placement and the s_nop fall-through, both patched program addresses, the RSRC1/RSRC2 merge, the SH and context register merge, semantics, specials, scratch size and user data copied into the fused memory, the self-contained header, and nothing written past the reported size;
    • hull halves: register-level fusion as on main (checksums from the front, RSRC merge, PGM_LO/HI_LS at the front code, back registers untouched) and main's size query;
    • rejections: null arguments, swapped halves, mismatched GS stage bits, and each geometry case listed under Throws.
      With main's ShaderFusion.cpp the geometry cases fail ("fused shaders are not 256-byte aligned").
  • PPSA21564 with this geometry path on our integration branch: the errors above are gone, and the title scene and the world map render as before, across three consecutive runs of our visual regression check. This branch was not run in the title after the rebase; its geometry path is the same code.
  • ctest: 80/80 pass. agc_driver_graphics_tests, agc_driver_mesh_tests, agc_driver_tests and agc_driver_recorder_tests pass.

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
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4670960940 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/124). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-29T00:32:53Z. --- ### What Rebased onto current `main`. This PR now replaces only the geometry path of `sceAgcUnknownFuseShaderHalves` (`fd5Bp5tGTgo`) and `sceAgcUnknownGetFusedShaderSize` (`dolOmWH+huQ`). Hull halves keep the register-level fusion from fd4700b9 unchanged, and the half-type and `VGT_SHADER_STAGES_EN` checks stay shared. **Why the geometry path changes.** fd4700b9 fuses geometry halves at the register level: the back half's registers with `SPI_SHADER_PGM_LO_ES` pointing at the front half's code. The driver then compiles that front half as the draw's whole program (type 4, `Stage::Mesh`) and skips the back (`Role::GeometryBack`). Nothing links the two halves (`LinkedProgram` is only serialized, and `DecodeRdnaFrontProgram` has no caller). The front half ends in `s_setpc_b64 s[6:7]` with no `s_endpgm`, so decoding runs on into the `s_code_end` padding and the program trailer. In PPSA21564 the geometry-shader draws are skipped: ``` [gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x514080000] [gpu] skipped draw: unsupported scalar source operand code 115 at program counter 64 [DRAW_INDEX_INDIRECT, color target 0x514080000] ``` Hull halves work with the register-level fusion, since the tessellation path compiles the local and hull programs separately, so they are left as they are. **Geometry halves** are fused into one self-contained `Gs` program: - **Code:** the front half's code up to its final `s_setpc_b64 s[6:7]` (its length comes from the program trailer), then `s_nop` padding to 256 bytes, then the whole back half. The front falls through into the back. - **SH registers:** the back half's set, with these changes: - `PGM_LO/HI_GS` points at the copied back code, and the ES program address at the fused code. - RSRC1 takes the larger VGPR and SGPR counts of the two halves. - RSRC2 takes the front's user-SGPR count. The SPI loads the front layout, and the back reloads its own layout from memory through `s[0:1]`. - The front's other SH registers are appended, except its checksum. - **Context registers:** merged the same way. - **Other tables:** inputs come from the front half. Outputs, specials and user data come from the back half. The scratch size is the larger of the two. - **Memory layout:** everything the fused `Shader` points to, including a copy of the header, is written into the caller's memory after the code, and the header is registered with the driver as a self-contained shader. Titles pass memory without honouring the reported alignment (found by @Pacuka with The Smurfs Dreams), so the program is placed at the first 256-byte boundary inside it, and `GetFusedShaderSize` returns the total size plus 255 bytes of padding, with log2 alignment 8. `SizeAlign` moves into `ShaderFusion.hpp` unchanged, so the test can call both functions. - **Throws:** halves with embedded constant buffers, a front half without a trailer or without a final `s_setpc_b64 s[6:7]`, a front half needing RSRC2 state the back half doesn't enable, geometry halves without fused memory, missing tables, and a program address above 48 bits. Mismatched half types and stage bits return `0x8a6c0008` as on `main`. - **Driver change:** `s[0:1]` of a `Gs` program is now loaded from its user-data address registers whenever they are set. Before, this only happened for `GsFront` programs. The back half of a fused program reads its user data through them. A program without those registers keeps zero there (SamG-Coder's fix, 8345fbc1). ### Tested - Linux (CachyOS, GCC, RTX 4090). `agc_shader_fusion_tests`, registered in ctest: - geometry fusion into scratch memory that is aligned, misaligned by 0xa0, and misaligned by 4 bytes: code placement and the `s_nop` fall-through, both patched program addresses, the RSRC1/RSRC2 merge, the SH and context register merge, semantics, specials, scratch size and user data copied into the fused memory, the self-contained header, and nothing written past the reported size; - hull halves: register-level fusion as on `main` (checksums from the front, RSRC merge, `PGM_LO/HI_LS` at the front code, back registers untouched) and `main`'s size query; - rejections: null arguments, swapped halves, mismatched GS stage bits, and each geometry case listed under Throws. With `main`'s `ShaderFusion.cpp` the geometry cases fail ("fused shaders are not 256-byte aligned"). - PPSA21564 with this geometry path on our integration branch: the errors above are gone, and the title scene and the world map render as before, across three consecutive runs of our visual regression check. This branch was not run in the title after the rebase; its geometry path is the same code. - `ctest`: 80/80 pass. `agc_driver_graphics_tests`, `agc_driver_mesh_tests`, `agc_driver_tests` and `agc_driver_recorder_tests` pass. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Pacuka commented 2026-09-29 19:48:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-09-29T19:48:29Z.


Tested this locally with The Smurfs Dreams (UE, Linux). The game calls sceAgcUnknownFuseShaderHalves with scratch memory that is not 256-byte aligned (e.g. 0x7f7fdf3ee5a0), whatever sceAgcUnknownGetFusedShaderSize reports as alignment (the address stayed the same with m_align = 8 and = 256), so it throws fused shader memory is not a valid program address.

What worked here: report totalBytes + FusedCodeAlignment - 1 as the size and place the fused program at AlignUp(scratch, FusedCodeAlignment) inside the scratch memory instead of requiring the scratch itself to be aligned. With that the fused GS draws run.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5897475915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/124#issuecomment-5897475915). Originally posted by [Pacuka](https://github.com/Pacuka) on 2026-09-29T19:48:29Z. --- Tested this locally with The Smurfs Dreams (UE, Linux). The game calls `sceAgcUnknownFuseShaderHalves` with scratch memory that is not 256-byte aligned (e.g. `0x7f7fdf3ee5a0`), whatever `sceAgcUnknownGetFusedShaderSize` reports as alignment (the address stayed the same with `m_align` = 8 and = 256), so it throws `fused shader memory is not a valid program address`. What worked here: report `totalBytes + FusedCodeAlignment - 1` as the size and place the fused program at `AlignUp(scratch, FusedCodeAlignment)` inside the scratch memory instead of requiring the scratch itself to be aligned. With that the fused GS draws run.
oneandonlydean commented 2026-09-29 19:52:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T19:52:52Z.


Thanks for testing with The Smurfs Dreams. You're right: the title doesn't honour the reported alignment, so the library has to align inside the scratch memory. I'll report totalBytes + FusedCodeAlignment - 1 as the size and place the fused program (and its header) at AlignUp(scratch, FusedCodeAlignment), keeping the 48-bit address check, plus a test that fuses into deliberately misaligned scratch.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5897541062 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/124#issuecomment-5897541062). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-29T19:52:52Z. --- Thanks for testing with The Smurfs Dreams. You're right: the title doesn't honour the reported alignment, so the library has to align inside the scratch memory. I'll report `totalBytes + FusedCodeAlignment - 1` as the size and place the fused program (and its header) at `AlignUp(scratch, FusedCodeAlignment)`, keeping the 48-bit address check, plus a test that fuses into deliberately misaligned scratch.
oneandonlydean commented 2026-09-29 19:55:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T19:55:12Z.


Pushed: the fused program is now placed at the first 256-byte boundary inside the scratch memory, and the size query includes 255 bytes of padding. The test fuses into aligned and misaligned scratch (0xa0 and 4 bytes off) and fails without the change. Rebased on current main as well. Could you check it with The Smurfs Dreams when you get a chance?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5897579551 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/124#issuecomment-5897579551). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-29T19:55:12Z. --- Pushed: the fused program is now placed at the first 256-byte boundary inside the scratch memory, and the size query includes 255 bytes of padding. The test fuses into aligned and misaligned scratch (0xa0 and 4 bytes off) and fails without the change. Rebased on current main as well. Could you check it with The Smurfs Dreams when you get a chance?
github-actions[bot] commented 2026-09-30 16:12:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-09-30T16:12:45Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#147 core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp L4168-4178
#167 core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp L4165-4179
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5915142003 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/124#issuecomment-5915142003). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-09-30T16:12:45Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #147 | `core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp` [L4168-4178](https://github.com/boykopovar/AnyPS5/blob/a67b644ff62ee4a1fe4167f673b49df816e194b4/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp#L4168-L4178) | | | | #167 | `core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp` [L4165-4179](https://github.com/boykopovar/AnyPS5/blob/a67b644ff62ee4a1fe4167f673b49df816e194b4/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp#L4165-L4179) | | |
oneandonlydean commented 2026-09-30 19:27:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-30T19:27:38Z.


Some data on how this compares with the fusion that landed on main (fd4700b9), from PPSA21564.

With main's register-level fusion, the geometry-shader draws in PPSA21564 are skipped:

[gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x514080000]
[gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x5367f0000]
[gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x53aa00000]
[gpu] skipped draw: unsupported scalar source operand code 115 at program counter 64 [DRAW_INDEX_INDIRECT, color target 0x514080000]
[gpu] skipped draw: unsupported scalar source operand code 115 at program counter 64 [DRAW_INDEX_INDIRECT, color target 0x5168c0000]

What happens: the fused registers keep the back half's state with SPI_SHADER_PGM_LO_ES pointing at the front half's code. The driver compiles that front half as the draw's whole program (type 4, Stage::Mesh) and skips the back (Role::GeometryBack). Nothing links the two halves (LinkedProgram is only serialized, and DecodeRdnaFrontProgram has no caller). The front half ends in s_setpc_b64 s[6:7] with no s_endpgm, so decoding runs on into the s_code_end padding and the program trailer, which is where opcode 31 and operand 115 come from.

With this PR's approach for geometry halves (the front up to its s_setpc_b64, s_nop padding to 256 bytes, then the back, as one self-contained program), those errors are gone and the title scene and the world map render as before. That is across three consecutive runs of our visual regression check on current main plus this change. Hull halves are fine with main's register-level fusion, so I'd keep that for them.

I'll rebase this PR onto current main so it only replaces the geometry path and leaves the hull path as it is on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5918177529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/124#issuecomment-5918177529). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-30T19:27:38Z. --- Some data on how this compares with the fusion that landed on main (fd4700b9), from PPSA21564. With main's register-level fusion, the geometry-shader draws in PPSA21564 are skipped: ``` [gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x514080000] [gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x5367f0000] [gpu] skipped draw: unsupported SOPP opcode 31 [DRAW_INDEX_AUTO, color target 0x53aa00000] [gpu] skipped draw: unsupported scalar source operand code 115 at program counter 64 [DRAW_INDEX_INDIRECT, color target 0x514080000] [gpu] skipped draw: unsupported scalar source operand code 115 at program counter 64 [DRAW_INDEX_INDIRECT, color target 0x5168c0000] ``` What happens: the fused registers keep the back half's state with `SPI_SHADER_PGM_LO_ES` pointing at the front half's code. The driver compiles that front half as the draw's whole program (type 4, Stage::Mesh) and skips the back (Role::GeometryBack). Nothing links the two halves (`LinkedProgram` is only serialized, and `DecodeRdnaFrontProgram` has no caller). The front half ends in `s_setpc_b64 s[6:7]` with no `s_endpgm`, so decoding runs on into the `s_code_end` padding and the program trailer, which is where opcode 31 and operand 115 come from. With this PR's approach for geometry halves (the front up to its `s_setpc_b64`, `s_nop` padding to 256 bytes, then the back, as one self-contained program), those errors are gone and the title scene and the world map render as before. That is across three consecutive runs of our visual regression check on current main plus this change. Hull halves are fine with main's register-level fusion, so I'd keep that for them. I'll rebase this PR onto current main so it only replaces the geometry path and leaves the hull path as it is on main.
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!124
No description provided.