Skip to content

feat(agc): copy HLE for the 16-byte stream copy kernel - #5281

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
parl0v:feat/copy-hle-stream-kernel
Oct 14, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
parl0v:feat/copy-hle-stream-kernel

Conversation

@parl0v

@parl0v parl0v commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

A second copy kernel for the existing copy HLE. It is a 16-word compute kernel that copies a buffer in 16-byte elements. Each group has 64 threads, and thread t of group g loads elements g128+t and g128+64+t from the index-addressed buffer in s[0:3], then stores them at the same indices of s[4:7].

PPSA25344 runs it every frame on its title screen to copy 3 MiB. Run as a shader against host imports, it costs ~11 ms of GPU per present on an RTX 4060 (the top program in APS5_PROFILE_GPU).

  • MatchesStreamCopyKernel (BufferCopy.hpp) matches it by its 16 code words, as MatchZeroDwordFill does, with 64×1×1 threads, group id x and 8 user-data words. Its loads and stores are format instructions, so both descriptors must have a 16-byte stride, no swizzle and the same word 3, with what the pattern fill takes there: 32_32_32_32_UINT, XYZW selects, no add-tid, out-of-bounds mode 0, type 0.
  • copyBuffer then copies the elements the kernel stores as one copy through the existing GPU/CPU copy paths: min(groups × 128, the destination's record count), from StreamCopyRange.
  • It refuses when the source's record count does not cover those elements, since the kernel would store zeros there. It also refuses when the bytes are 0 or above 16 MiB, or either base isn't 16-byte aligned. Overlapping ranges go through the existing CopyOverlapping refusal.
  • APS5_NO_COPY_HLE turns it off together with the existing kernel.
  • countCopy counts each copy path in copyPathCounts whether or not APS5_PROFILE_DRAW is set, so a test can tell the HLE from the shader.

Tested

  • New agc_driver_stream_copy (Windows 10 19045, RTX 4060, driver 617.42): the match against every code word with a bit flipped and each descriptor and dispatch refusal; the range clamp and refusals; nine dispatches through the driver, four taking the HLE path and five refused for their shape, checking every destination word. It passes, also with APS5_NO_COPY_HLE=1, and fails with copyBuffer refusing the kernel. 516 of 517 agc_ tests pass; agc_shader_disk_cache fails on this machine because Windows denies removing its cache directory. Rebased on main (145cb82f3); tools/check_conventions.py --base origin/main passes.

  • Windows 10 19045 (MinGW-w64 GCC 15.2): libs builds; tools/check_conventions.py --base origin/main passes. Fork CI (the build.yml steps on GitHub runners): Windows passes. Linux passes 1,066 of 1,067; the one failure is guest_math, which also fails on plain main on that runner (fix(libc): return the x86 default NaN from acosf for |x| > 1 #5154).

  • PPSA25344 title screen, main (60fb61870) against this branch, same relinked executable, APS5_PROFILE_DRAW=1. Mean of the last twelve 10 s windows of a 300 s run, after a warm-up run:

    GPU main this PR GPU busy per present
    RTX 3060 12 GB (i5-12400F, 32 GB) 4.90 FPS 5.09 / 5.08 FPS (two runs) 94.2 → 86.2 ms
    RTX 4060 8 GB (i5-14400F, 16 GB) 3.80 FPS 3.97 / 3.97 FPS (two runs) 121.6 → 110.8 ms

    [copy] with the change: 1,431 copy kernels matched, 954 done as GPU copies (2,862 MiB). On main the line never appears. Frame dumps: mean difference 0.07 (of 255) between main and this PR, against 0.06 between two runs of this PR, so the picture is unchanged within run noise.

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 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
  • Unblocks: none
  • AI-assisted: yes

@github-actions github-actions Bot added the agc libSceAgc* label Oct 13, 2026
@adriadam10 adriadam10 self-assigned this Oct 14, 2026

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the measurements. The match rests on a 16-word kernel that nothing in the PR lets us check on Linux, so I'd like a test before merging:

  • Add a test in core/libs/prx/libSceAgcDriver/tests (registered in ctest) that dispatches the 16 code words of this kernel with the user data from matchesStreamCopyKernel and checks the destination bytes after the copy. It should show that thread t of group g really copies elements g*128+t and g*128+64+t, and that matchesStreamCopyKernel accepts it. The code words also pin kernelHash, so a reviewer can verify it.
  • Include rows for the refusals: source record count below groups × 128 (userData[2] < elements), destination smaller than groups × 128 (clamped by userData[6]), 0 bytes, above 16 MiB, and a base that isn't 16-byte aligned. Each should fall back to the shader path with the destination unchanged by the HLE.
  • The test must fail without the change (the kernel runs as a shader today, so assert the HLE path, e.g. via the [copy] counters).

Locally, the build and libSceAgcDriver ctest show no regression from this diff: agc_recorder_kept_bytes and agc_driver_tessellation crash here with or without it.

A 16-word compute kernel copies a buffer in 16-byte elements: 64 threads a
group, thread t of group g loads elements g*128+t and g*128+64+t from the
index-addressed buffer in s[0:3] and stores them at the same indices of
s[4:7]. Run as a shader against host imports it costs 11.8 ms of GPU per
present in PPSA25344's main menu on an RTX 4060.

Matched like the existing copy kernel, by its code hash and its descriptors
(16-byte stride and no swizzle on both, the same word 3), it becomes one copy
through the existing copy HLE path: the elements the kernel stores, min of
groups * 128 and the destination's record count, when the source's record
count covers them all. APS5_NO_COPY_HLE turns it off with the other kernel.
The stream copy kernel is matched by its 16 code words instead of a hash, and the
match and its range checks become free functions (MatchesStreamCopyKernel,
StreamCopyRange) so a test can reach them. Its loads and stores are format
instructions, so the match also requires the descriptor the pattern fill takes:
32_32_32_32_UINT, XYZW selects, no add-tid, out-of-bounds mode 0, type 0. Copy
paths are counted in copyPathCounts whether or not APS5_PROFILE_DRAW is set.

agc_driver_stream_copy checks the match against every word with a bit flipped,
the descriptor and dispatch refusals, the range clamp and refusals, and runs nine
dispatches through the driver: four must take the HLE path and copy elements
g*128+t and g*128+64+t, five must be refused for their shape and leave the
shader's result.
@parl0v
parl0v force-pushed the feat/copy-hle-stream-kernel branch from a6bfc99 to fa4fc23 Compare October 14, 2026 14:33
@parl0v

parl0v commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

@adriadam10 added in fa4fc23, rebased onto current main.

Match by code words. The kernel is now compared word for word, as MatchZeroDwordFill does, instead of through a hash, so the words are in the source:

words instruction
d7460008 04010e08 v_lshl_add_u32 v8, s8, 7, v0 (g*128 + t)
8f6a8708 s_lshl_b32 vcc_lo, s8, 7
d76d0009 0401806a v_add3_u32 v9, vcc_lo, 64, v0 (g*128 + 64 + t)
e00c2000 80000008 buffer_load_format_xyzw v[0:3], v8, s[0:3] idxen
e00c2000 80000409 buffer_load_format_xyzw v[4:7], v9, s[0:3] idxen
bf8c3f71 s_waitcnt vmcnt(1)
e01c2000 80010008 buffer_store_format_xyzw v[0:3], v8, s[4:7] idxen
bf8c3f70 s_waitcnt vmcnt(0)
e01c2000 80010409 buffer_store_format_xyzw v[4:7], v9, s[4:7] idxen
bf810000 s_endpgm

The loads and stores are format instructions, so the copy is bit-exact only for a 16-byte format with identity selects. The match now also requires what the pattern fill does: format 0x4b (32_32_32_32_UINT), dst_sel XYZW (0xfac), no add-tid, out-of-bounds mode 0, type 0. Both descriptors carry the same word 3, as before.

The kernel match and its range checks are now free functions (MatchesStreamCopyKernel, StreamCopyRange in BufferCopy.hpp), so the test can call them. countCopy also counts each path in copyPathCounts without APS5_PROFILE_DRAW, so the test can tell the HLE from the shader.

agc_driver_stream_copy (tests/execution/StreamCopy.cpp):

  • Match: the kernel matches. Each of the 16 words with a bit flipped, a truncated kernel, a stride other than 16 on either descriptor, a swizzled descriptor, descriptors of different formats, a 32-bit format, a swapped destination select, add-tid, raw out-of-bounds mode, another descriptor type, a ninth user SGPR, 32 threads and no group id are all refused.
  • Range: 4 groups copy 512 elements (8 KiB). The copy is clamped to the destination's records (userData[6] = 300 copies 300). Records above the launch copy only the launch. 16 MiB exactly is taken. Refused: source records below the copy (userData[2] < elements), 0 destination records, 0 groups, above 16 MiB (8,193 groups), and a source or destination base not aligned to 16 bytes.
  • Execution: nine dispatches through AgcDriver::Submit, checking every destination word.
    • 4 groups, 16 groups, the clamp, and records above the launch must raise the taken count (CPU, GPU or aliased) by one. They must write elements g*128+t and g*128+64+t from the source and leave the rest untouched.
    • The five refusals must raise CopyShape by one and leave the shader's own result: zeros past the source's records, nothing at 0 records, the full 16 MiB + 2 KiB copy, the 4-byte-offset copies.
    • With APS5_NO_COPY_HLE=1, every row must take neither path and still match.

It fails without the change: without this PR's functions it doesn't build. With copyBuffer refusing the stream kernel (matchers intact) it stops at the copy did not take the HLE path (4 groups).

Tested on Windows 10 19045, RTX 4060 (driver 617.42), MinGW-w64 GCC 15.2: agc_driver_stream_copy passes (5.3 s), and also with APS5_NO_COPY_HLE=1. The rest of the agc_ tests: 516 of 517 pass; agc_shader_disk_cache fails on this machine because Windows denies removing its cache directory ("cannot remove the shader cache test directory: Access is denied"), unrelated to this diff.

@github-actions

github-actions Bot commented Oct 14, 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
#5497 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/Dispatch.cpp L4

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the new agc_driver_stream_copy: it matches the kernel by its 16 code words (every single-bit flip is refused), covers the descriptor/dispatch refusals and runs nine dispatches through the driver asserting the HLE path via copyPathCounts and every destination word. It passes here, and without the change it doesn't even build, so it can't pass on main. The element indices (g*128+t, g*128+64+t) match what the HLE copies. Merging once the full Build is green.

@adriadam10
adriadam10 merged commit 93cd77a into boykopovar:main Oct 14, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants