Skip to content

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

Merged
boykopovar merged 1 commit into
boykopovar:mainfrom
oneandonlydean:feat/agc-fuse-shader-halves
Oct 1, 2026
Merged

boykopovar merged 1 commit into
boykopovar:mainfrom
oneandonlydean:feat/agc-fuse-shader-halves

Conversation

@oneandonlydean

@oneandonlydean oneandonlydean commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

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 fd4700b unchanged, and the half-type and VGT_SHADER_STAGES_EN checks stay shared.

Why the geometry path changes. fd4700b 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, 8345fbc).

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

@Pacuka

Pacuka commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator Author

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
oneandonlydean force-pushed the feat/agc-fuse-shader-halves branch from c7a106e to 67c3256 Compare September 29, 2026 19:54
@oneandonlydean

Copy link
Copy Markdown
Collaborator Author

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?

oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Sep 29, 2026
…its scratch memory

Titles pass scratch memory without honouring the reported alignment; the size query now includes
255 bytes of padding and the fused program is aligned inside the scratch. Same change as upstream
PR boykopovar#124.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ButHENCbXXt8h4EZuWUabJ
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Sep 30, 2026
@oneandonlydean
oneandonlydean force-pushed the feat/agc-fuse-shader-halves branch from 67c3256 to 0cdb5df Compare September 30, 2026 11:12
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Sep 30, 2026
…, boykopovar#129) into astrobot

Conflicts resolved:
- Shader disk cache (boykopovar#129 as merged upstream): upstream's version, plus
  astrobot's imageDepthCompare and waveLayout fields (format 3).
- Shader fusion: upstream's fd4700b replaces astrobot's boykopovar#124 version.
- -KRzWekV120: upstream's export replaces astrobot's; its test now calls
  sceAgcUnknown__MKRzWekV120.
- Misaligned buffer views: upstream's push-constant adjustment (3dec6ce)
  replaces astrobot's view aliases; GuestBufferMemory is the rebased boykopovar#121
  version (write watch on shared direct memory).
- DirectMemory: rebased boykopovar#121 (re-register after the shared-backing remap,
  aliased pages leave the watch) plus boykopovar#164's per-piece unmap.
- Depth: astrobot's DepthTarget model is kept for attachments; upstream's
  DepthSurface module is dropped (it would duplicate it). Upstream's
  mip-chain colour targets, CB_COLOR_VIEW MIP_LEVEL at bits 26-29 and
  export formats are taken into DecodeColorBuffer.
- GPU-selected V#s: upstream's gpuDescriptor replaces astrobot's opt-in
  runtimeDescriptor; image_bvh_intersect_ray is upstream's.
- Occlusion counter dumps: upstream's Vulkan occlusion queries replace
  astrobot's always-visible PIXEL_PIPE_STAT dump.
- AudioOut2 DualSense ports: rebased boykopovar#180 on upstream's 16-bit ports.
- Pixel stage decode: rebased boykopovar#136; mesh push constants: rebased boykopovar#133.
- Label waits (boykopovar#178): noteHeldAtSubmit moved into reserveOutputs.
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Sep 30, 2026
… again

Upstream's sceAgcUnknownFuseShaderHalves (fd4700b) fuses 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 (LinkedProgram is only serialized and
DecodeRdnaFrontProgram has no caller). The front ends in
s_setpc_b64 s[6:7] and has no s_endpgm, so the decode runs on into the
s_code_end padding and the program trailer: Astro Bot's geometry-shader
draws were skipped with "unsupported SOPP opcode 31" (s_code_end) and
"unsupported scalar source operand code 115 at program counter 64".

Geometry halves are fused as astrobot did before (boykopovar#124): the front up to
its s_setpc_b64, s_nop padding to 256 bytes and the back, with a
self-contained header in the scratch memory. Hull halves keep upstream's
register-level fusion, which the tessellation path's separate local and
hull programs fit.
@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

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

@oneandonlydean

Copy link
Copy Markdown
Collaborator Author

Some data on how this compares with the fusion that landed on main (fd4700b), 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.

sceAgcUnknownFuseShaderHalves (fd4700b) fuses both kinds of halves at
the register level: the back half's registers with the front half's
program address in PGM_LO_ES (PGM_LO_LS for hull halves). For geometry
halves 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. The front ends in s_setpc_b64 s[6:7] and has no
s_endpgm, so the decode runs on into the s_code_end padding and the
program trailer. In PPSA21564 the geometry shader draws were skipped
with "unsupported SOPP opcode 31" (s_code_end) and "unsupported scalar
source operand code 115 at program counter 64".

Geometry halves are now fused into one Gs program: the front half's code
up to its final s_setpc_b64 s[6:7] (its length comes from the program
trailer), s_nop padding to the next 256 bytes, then the whole back half,
so the front falls through into the back. Its SH registers are the back
half's, with PGM_LO/HI_GS pointing at the copied back code, the ES
program address at the fused code, RSRC1 taking the larger VGPR and SGPR
counts of the two halves, RSRC2 taking the front half's user-SGPR count
(the SPI loads the front layout; the back reloads its own from memory
through s[0:1]), and the front's other SH registers except its checksum
appended. Context registers are merged the same way. Inputs come from
the front, outputs, specials and user data from the back; the scratch
size is the larger of the two.

Everything the fused Shader points to, including a copy of the header
itself, 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, so the program is
placed at the first 256-byte boundary inside it, and the size query
returns the total plus 255 bytes of padding (alignment reported as log2
8). Halves with embedded constant buffers, a front half without a
trailer or final s_setpc_b64, a front half that needs RSRC2 state the
back half does not enable, and geometry halves without fused memory
throw.

Hull halves keep the register-level fusion, which the tessellation
path's separate local and hull programs fit. The half-type and stage
checks stay shared.

The driver now loads s[0:1] of a Gs program from its user-data address
registers whenever they are set, not only for GsFront programs, since
the back half of a fused program reloads its user data from there; a
program without those registers keeps zero there (from SamG-Coder's
8345fbc).
@oneandonlydean
oneandonlydean force-pushed the feat/agc-fuse-shader-halves branch from 0cdb5df to 258e4d5 Compare September 30, 2026 19:47
@oneandonlydean oneandonlydean changed the title feat(agc): fuse geometry shader halves fix(agc): fuse geometry shader halves into one self-contained program Sep 30, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Sep 30, 2026
@boykopovar
boykopovar merged commit 89afcdc into boykopovar:main Oct 1, 2026
3 checks passed
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 1, 2026
…opovar#208 test layout, our merged PRs) into astrobot

Conflicts resolved:
- Driver.cpp: upstream split it into Execution/src/Driver/*; astrobot's
  driver changes are ported into the split files (flip throttle
  APS5_MAX_QUEUED_FLIPS, dispatch tolerance, NoteProgram, metadata-pass
  switch and DB HTILE blits, depth state in the draw register key, HTILE
  fill clears, wait outcome counters, opt-in deferred EOP interrupts).
  boykopovar#192's KnownDrawRejection is taken.
- Our merged PRs take upstream's reviewed versions: boykopovar#124 shader fusion,
  boykopovar#128 videoout open param, boykopovar#132 CB metadata passes, boykopovar#138 EOP events,
  boykopovar#176 GE_INDX_OFFSET, boykopovar#136/boykopovar#137 pixel inputs and NoPerspective by I/J
  pair, boykopovar#133 mesh tests. boykopovar#142's interpolation validation is upstream's.
- Depth: astrobot's DepthTarget model stays; upstream's DepthSurface
  module is dropped again, boykopovar#188's 16-bit depth-bit reads are kept on top.
- Videodec2: upstream's ffmpeg-core version (boykopovar#42 rework); 3rdparty/FFmpeg
  stays for libSceAvPlayer only.
- Tests follow boykopovar#208's layout (add_test_executable, build/tests); the
  AvPlayer, GDS and wave tests are defined the same way.
- Shader disk cache format 5 and request serialization version 7 cover
  both sides' fields.
- Duplicates the auto-merge produced (state decoders, RequestInputInfo,
  structurizer helpers, image offsets, opcode enum, tests) are removed.
@oneandonlydean
oneandonlydean deleted the feat/agc-fuse-shader-halves branch October 1, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants