Skip to content

perf(agc): copy back staged writes by groups of eight words, reading the import only where the shader wrote - #21338

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
AlexFigas:perf/agc-copy-back-whole-lines
Nov 17, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
AlexFigas:perf/agc-copy-back-whole-lines

Conversation

@AlexFigas

@AlexFigas AlexFigas commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What

The changed-byte copy-back kernels (StagingCopyBack.comp, StagingCopyBackBound.comp) merge the shader's result into the host import word by word: each lane loads the target word from the import (system memory) and stores it back if a byte changed. Where the shader changed few words, the import is hit with scattered 4-byte loads and stores.

Each group of eight words (32 bytes) is now handled by eight lanes. The group is left alone unless the shader changed a byte in it. Otherwise the lanes store all eight words, the untouched ones with the value just loaded, so the stores to the import are adjacent words of one group instead of scattered single words. What the kernels store for a changed byte and what they count is unchanged, and APS5_CHANGED_COPY_ALWAYS still stores every word. The bound kernel's dispatches are sized to a multiple of 64 words so a split never cuts a group.

One behaviour is new. In a written group, every word is stored, also a word the shader left alone and a word whose changed bytes the CPU had changed too (the old kernel stored nothing for these), with the value loaded from the import a moment earlier. A CPU store to such a word between that load and the store is lost. The old whole-range copy (APS5_NO_CHANGED_COPY_BACK) overwrote every word of the range. I measured the window on one level only (see Tested); what that leaves open is recorded in a debt fragment (docs/dev/debt/changes/perf-agc-copy-back-whole-lines.md). The guarded store (if (stored != 0u)) with the same group skipping is one line per kernel; an earlier build of that variant had staging-out at 54 to 64 ms per present against 66 to 72 ms on main (same method, two runs each, on the main before #20292), against 7 to 8 ms with the unguarded store below.

This replaces the per-word early return of #20292 (merged): on this level that early return alone did not change the frame time in the one run I measured (p50 343 to 370 ms, as on main).

The two tests of the kernels now also use shader changes that leave whole groups untouched. The tests check the stored values and the counter, not which words are loaded or stored.

Tested

Windows 10.0.26300, AMD Ryzen 5 9600X, NVIDIA GeForce RTX 5070 Ti, driver 621.14, display 2560x1440 at 164 Hz, AC power, 2026-11-16. The timings compare main ac29a0c26c with 416ae4616f (that main plus this change as first written). The head, b105ab6836, is b7a3334fc7 plus one commit that changes only the debt fragment; b7a3334fc7 is the change as first written, rebased on dcf3d15978 with two edits that leave the compiled kernels the same (a trailing newline and a blank line in the shaders) and one that only matters when a copy-back is split into several dispatches (their size is a multiple of 64 words). No commit between ac29a0c26c and dcf3d15978 touches the two kernels or the two tests. The tests below were run again on ad5d92582c (the head before the last rebase, over four commits that touch libSceLibcInternal, libc locale support and tools/performance_bisect.py) and on its base e04bd77e7f.

Why this level. One run each of 26 normal levels of PPSA21564 01.007.000 (-lvl <file>, 90 s, no input, APS5_PROFILE_DRAW=1, on main ef4b9389b6, which predates #20292; one level wrote no summary) ranked speedrun_inflator first by the mean frame p50 of the 10 s intervals from 40 s on (406 ms). The next were hub_crashsite at 338 ms, only because of one 856 ms interval while the level started (its last two intervals were 87 and 71 ms), then sponge_dirty_bath at 227 and path_maker_mana_tree at 214. On speedrun_inflator the GPU is busy 88 to 96% of the frame from 20 s on, and one class dominates: staging-out (the copy-backs) takes 61 to 68 ms per present in the profile below.

PPSA21564 -lvl speedrun_inflator: the frame p50 went from 363 to 301 ms, GPU busy from 328 to 264 ms per present, in four alternating window captures; every run of the change was faster than every run of main.

  • Scene: -lvl speedrun_inflator, default settings, 3840x2160 output, caches warm, no input (the level flies on by itself). The window is 32 to 40 s after the first present.
  • Capture: APS5_TRACE_SECONDS=32:8 and APS5_TRACE_DIR, no other APS5_TRACE_* or APS5_PROFILE_* switch, ANYPS5_GPU=NVIDIA, ANYPS5_DISPLAY_INDEX=0, ANYPS5_VOLUME=0. Order main, change, four times. Each run was stopped 64 s after start (62 s asked for); no trace dropped events.
  • Local change on both sides, not part of this PR: libSceAudioPropagation.prx built from fix(libSceAudioPropagation): find no path without rays and set audio paths #19326, as in perf(agc): read the import only for words a staged copy-back changes #20292.
  • Libraries: 5 of the 418 staged files differ between the two sets: libSceAgcDriver (the kernels), and libc, libkernel, libSceVideoOut, libSceJson2 (the commit or link time they embed).
run commit presents frame p50 p90 p99 GPU busy per present
main 1 ac29a0c26c20 22 358.9 382.9 517.9 332.6
change 1 416ae4616fa5 27 306.1 323.5 355.7 262.8
main 2 ac29a0c26c20 23 360.6 389.6 533.3 313.0
change 2 416ae4616fa5 28 291.3 319.4 372.5 253.1
main 3 ac29a0c26c20 22 363.6 390.6 512.5 323.8
change 3 416ae4616fa5 27 300.5 336.9 369.4 264.7
main 4 ac29a0c26c20 22 367.7 389.3 544.7 342.3
change 4 416ae4616fa5 26 305.8 331.6 354.8 277.6

Summary lines of round 2 (all eight lines, the traces of round 2 and their trace_digest.py output are in this gist):

{"commit":"ac29a0c26c20","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"32:8"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 621.14","os":"Windows 10.0.26300","cpu":"AMD Ryzen 5 9600X 6-Core Processor","cpu_threads":12,"memory_mib":31861,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"seconds 32:8","first_present":316,"ms":8360.503},"presents":23,"frame_ms":{"p50":360.580,"p90":389.573,"p99":533.325,"max":533.325},"gpu_busy_ms_per_present":313.014,"worker_busy_pct":{"queue worker 0x0":99.174,"queue worker 0x40":95.824,"queue worker 0x48":95.286,"queue worker 0x46":0.156},"phase_totals_ms":{"Driver.Completion":3.189,"Driver.Dispatch":3362.480,"Driver.Dispatch.capture_resources":1198.204,"Driver.Dispatch.decode_key_device_setup":15.776,"Driver.Dispatch.lookup_cache":11.983,"Driver.Dispatch.materialize":180.126,"Driver.Dispatch.prepared_invocation":11.436,"Driver.Draw":6863.254,"Driver.Draw.device_setup":32.555,"Driver.Draw.draw_enqueued":6080.055,"Driver.Draw.post_compile_prepare":7.153,"Driver.Draw.prepare":80.019,"Driver.Draw.shader_compile_and_link":593.303,"Driver.EndSubmission":19.706,"Driver.Packet":24259.403,"Driver.Packet.device_lock":11.913,"Driver.Packet.dispatch":3529.384,"Driver.Packet.draw":6871.731,"Driver.Packet.execute":44.654,"Driver.Packet.flip_flush":0.832,"Driver.Packet.flush_between_packets":45.001,"Driver.Packet.memory_wait":13291.432,"Driver.Packet.pipeline_drain":206.864,"Driver.Packet.pipeline_prepare":15.946,"Driver.Packet.prepare_memory":158.938,"Driver.Packet.rendering_wait":0.083,"Driver.Present":19.644,"Driver.Present.acquire_image":0.349,"Driver.Present.device_setup":0.009,"Driver.Present.gpu_mutex_wait":0.048,"Driver.Present.inflight_wait":0.058,"Driver.Present.present_display_buffer":3.355,"Driver.Present.queue_present":8.262,"Driver.Present.release_and_callback":0.020,"Driver.Present.render_fence_wait":7.480,"Driver.Present.resize":0.044,"Driver.Present.validate":0.007,"Driver.Suspend":0.006,"GpuMutex.wait.dispatch":1308.235,"GpuMutex.wait.draw":100.262,"GpuMutex.wait.end":4.170,"GpuMutex.wait.fill":0.151,"GpuMutex.wait.flush":0.559,"GpuMutex.wait.hook":7.886,"GpuMutex.wait.indirect":0.469,"GpuMutex.wait.label":12.059,"GpuMutex.wait.present":0.003,"GpuMutex.wait.wait":14.879,"Graphics.Draw":6903.740,"Graphics.Draw.draw_and_resource_release":842.700,"Graphics.Draw.index_upload":50.845,"Graphics.Draw.validate":48.524,"Graphics.Draw.vertex_upload":5954.062,"Graphics.DrawWithRecipe":11.683,"Graphics.DrawWithRecipe.draw_and_resource_release":10.900,"Graphics.DrawWithRecipe.index_upload":0.018,"Graphics.DrawWithRecipe.validate":0.121,"Graphics.DrawWithRecipe.vertex_upload":0.600,"Recorder.Reap":24.262,"Recorder.Submit":74.296,"Recorder.WaitSerial":80.997,"Resources.Complete":146.738,"Resources.Prepare":75.925,"Shader.DrawStage":474.505,"Shader.DrawStage.capture_resources":254.877,"Shader.DrawStage.materialize":172.081,"Shader.DrawStage.prepared_invocation":42.669,"Shader.ResolveAbi":3.334,"Shader.ResolveGraphicsAbi":0.954,"Shader.ResolveGraphicsStagesAbi":24.271,"VideoOut.Flip":22.934,"VideoOut.Flip.completion_mutex_wait":0.030,"VideoOut.Flip.config_mutex_wait":0.002,"VideoOut.Flip.frame_limit":0.012,"VideoOut.Flip.notify_game":2.143,"VideoOut.Flip.present":19.343,"VideoOut.Flip.validate":0.025,"VideoOut.Flip.vblank_wait":0.016,"VideoOut.Flip.window_prepare":0.039,"VideoOut.Flip.window_title":1.309,"VideoOut.Readiness":0.069,"VideoOut.Readiness.locks_validate":0.023,"Vulkan.Acquire":0.323,"Vulkan.Acquire.acquire_fence_wait":0.043,"Vulkan.Acquire.acquire_image":0.223,"Vulkan.Acquire.fence_reset":0.039,"Vulkan.Acquire.retired_swapchains":0.010,"Vulkan.Dispatch":405.297,"Vulkan.Dispatch.command_record":98.649,"Vulkan.Dispatch.shader_resources":303.857,"Vulkan.DispatchRecipe":21.246,"Vulkan.DispatchRecipe.command_record":21.099,"Vulkan.DrawFromRecipe":11.763,"Vulkan.Present":2.345,"Vulkan.Present.command_record_scale":1.075,"Vulkan.Present.pixel_upload":0.078,"Vulkan.Present.queue_submit":1.177,"Vulkan.QueuePresent":8.231,"Vulkan.QueuePresent.queue_present":8.204,"Vulkan.Retire":7.479,"Vulkan.Retire.wait":7.073},"top_phases_ms_per_frame":{"Driver.Packet":1054.757,"Driver.Packet.memory_wait":577.888,"Graphics.Draw":300.163,"Driver.Packet.draw":298.771,"Driver.Draw":298.402,"Driver.Draw.draw_enqueued":264.350,"Graphics.Draw.vertex_upload":258.872,"Driver.Packet.dispatch":153.451},"owned_draw_counts":{},"graphics_track":{"Graphics.Barrier.copy":494,"Graphics.Barrier.dcc-clear":276,"Graphics.Barrier.dcc-keys":276,"Graphics.Barrier.dispatch-lead":4381,"Graphics.Barrier.dispatch-trail":21283,"Graphics.Barrier.draw":15006,"Graphics.Barrier.fill":728,"Graphics.Barrier.fill-clear":396,"Graphics.Barrier.indirect-args":161,"Graphics.Barrier.input-mirror":1868,"Graphics.Barrier.label-run":6978,"Graphics.Barrier.present-blit":46,"Graphics.Barrier.shadow-publish":230,"Graphics.Barrier.staging-in":26639,"Graphics.Barrier.staging-out":12505,"Graphics.Barrier.storage-upload":1298,"Graphics.Barrier.storage-writeback":4156,"Graphics.BarrierMerged.dispatch-lead":17224,"Graphics.BarrierMerged.fill":194,"Graphics.BarrierMerged.label-run":2,"Graphics.BarrierMerged.shadow-publish":184,"Graphics.BarrierMerged.staging-in":12570,"Graphics.BarrierMerged.staging-out":12505,"Graphics.BarrierMerged.storage-writeback":645,"Graphics.Copy":28617,"Graphics.Copy.bytes":19145412656,"Graphics.RenderPass":6874,"Graphics.RenderPass.Close.command":5467,"Graphics.RenderPass.Close.image":135,"Graphics.RenderPass.Close.key":95,"Graphics.RenderPass.Close.label":2,"Graphics.RenderPass.Close.new-batch":1175,"Graphics.RenderPass.Continued":51130},"trace_dropped":false,"trace":"aps5-trace-ac29a0c26c20-17960.json.gz"}
{"commit":"416ae4616fa5","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"32:8"},"gpu":"NVIDIA GeForce RTX 5070 Ti","driver":"NVIDIA 621.14","os":"Windows 10.0.26300","cpu":"AMD Ryzen 5 9600X 6-Core Processor","cpu_threads":12,"memory_mib":31861,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"seconds 32:8","first_present":333,"ms":8096.110},"presents":28,"frame_ms":{"p50":291.312,"p90":319.426,"p99":372.494,"max":372.494},"gpu_busy_ms_per_present":253.101,"worker_busy_pct":{"queue worker 0x0":98.905,"queue worker 0x40":97.249,"queue worker 0x48":97.273,"queue worker 0x46":0.216},"phase_totals_ms":{"Driver.Completion":3.720,"Driver.Dispatch":3328.549,"Driver.Dispatch.capture_resources":1332.076,"Driver.Dispatch.decode_key_device_setup":19.593,"Driver.Dispatch.lookup_cache":15.642,"Driver.Dispatch.materialize":15.836,"Driver.Dispatch.prepared_invocation":13.495,"Driver.Draw":6663.495,"Driver.Draw.device_setup":43.792,"Driver.Draw.draw_enqueued":5405.154,"Driver.Draw.post_compile_prepare":10.014,"Driver.Draw.prepare":111.058,"Driver.Draw.shader_compile_and_link":1010.581,"Driver.EndSubmission":415.028,"Driver.Packet":23354.950,"Driver.Packet.device_lock":15.790,"Driver.Packet.dispatch":3536.636,"Driver.Packet.draw":6674.875,"Driver.Packet.execute":57.971,"Driver.Packet.flip_flush":1.018,"Driver.Packet.flush_between_packets":51.785,"Driver.Packet.memory_wait":12622.665,"Driver.Packet.pipeline_drain":82.278,"Driver.Packet.pipeline_prepare":20.798,"Driver.Packet.prepare_memory":198.387,"Driver.Packet.rendering_wait":0.036,"Driver.Present":42.762,"Driver.Present.acquire_image":0.400,"Driver.Present.device_setup":0.011,"Driver.Present.gpu_mutex_wait":0.052,"Driver.Present.inflight_wait":0.076,"Driver.Present.present_display_buffer":4.420,"Driver.Present.queue_present":11.701,"Driver.Present.release_and_callback":0.029,"Driver.Present.render_fence_wait":25.982,"Driver.Present.resize":0.069,"Driver.Present.validate":0.009,"Driver.Suspend":0.008,"GpuMutex.wait.dispatch":1184.317,"GpuMutex.wait.draw":117.488,"GpuMutex.wait.end":393.911,"GpuMutex.wait.fill":0.290,"GpuMutex.wait.flush":1.214,"GpuMutex.wait.hook":2.536,"GpuMutex.wait.indirect":0.628,"GpuMutex.wait.label":13.565,"GpuMutex.wait.wait":0.427,"Graphics.Draw":6391.543,"Graphics.Draw.draw_and_resource_release":1043.335,"Graphics.Draw.index_upload":66.231,"Graphics.Draw.validate":73.030,"Graphics.Draw.vertex_upload":5198.065,"Graphics.DrawWithRecipe":21.902,"Graphics.DrawWithRecipe.draw_and_resource_release":20.572,"Graphics.DrawWithRecipe.index_upload":0.084,"Graphics.DrawWithRecipe.validate":0.203,"Graphics.DrawWithRecipe.vertex_upload":0.965,"Recorder.Reap":28.728,"Recorder.Submit":90.628,"Recorder.WaitSerial":123.011,"Resources.Complete":217.636,"Resources.Prepare":92.679,"Shader.DrawStage":845.183,"Shader.DrawStage.capture_resources":389.181,"Shader.DrawStage.materialize":388.347,"Shader.DrawStage.prepared_invocation":60.611,"Shader.ResolveAbi":3.969,"Shader.ResolveGraphicsAbi":1.231,"Shader.ResolveGraphicsStagesAbi":32.786,"VideoOut.Flip":44.142,"VideoOut.Flip.completion_mutex_wait":0.034,"VideoOut.Flip.config_mutex_wait":0.003,"VideoOut.Flip.frame_limit":0.015,"VideoOut.Flip.notify_game":0.674,"VideoOut.Flip.present":41.590,"VideoOut.Flip.validate":0.031,"VideoOut.Flip.vblank_wait":0.022,"VideoOut.Flip.window_prepare":0.053,"VideoOut.Flip.window_title":1.704,"VideoOut.Readiness":0.071,"VideoOut.Readiness.locks_validate":0.032,"Vulkan.Acquire":0.368,"Vulkan.Acquire.acquire_fence_wait":0.047,"Vulkan.Acquire.acquire_image":0.251,"Vulkan.Acquire.fence_reset":0.048,"Vulkan.Acquire.retired_swapchains":0.013,"Vulkan.Dispatch":466.021,"Vulkan.Dispatch.command_record":120.619,"Vulkan.Dispatch.shader_resources":342.098,"Vulkan.DispatchRecipe":26.987,"Vulkan.DispatchRecipe.command_record":26.791,"Vulkan.DrawFromRecipe":22.024,"Vulkan.Present":3.162,"Vulkan.Present.command_record_scale":1.379,"Vulkan.Present.pixel_upload":0.051,"Vulkan.Present.queue_submit":1.714,"Vulkan.QueuePresent":11.664,"Vulkan.QueuePresent.queue_present":11.631,"Vulkan.Retire":25.979,"Vulkan.Retire.wait":25.400},"top_phases_ms_per_frame":{"Driver.Packet":834.105,"Driver.Packet.memory_wait":450.809,"Driver.Packet.draw":238.388,"Driver.Draw":237.982,"Graphics.Draw":228.269,"Driver.Draw.draw_enqueued":193.041,"Graphics.Draw.vertex_upload":185.645,"Driver.Packet.dispatch":126.308},"owned_draw_counts":{},"graphics_track":{"Graphics.Barrier.copy":619,"Graphics.Barrier.dcc-clear":336,"Graphics.Barrier.dcc-keys":336,"Graphics.Barrier.dispatch-lead":5361,"Graphics.Barrier.dispatch-trail":25935,"Graphics.Barrier.draw":18944,"Graphics.Barrier.fill":883,"Graphics.Barrier.fill-clear":504,"Graphics.Barrier.indirect-args":196,"Graphics.Barrier.input-mirror":2327,"Graphics.Barrier.label-run":8416,"Graphics.Barrier.present-blit":56,"Graphics.Barrier.shadow-publish":309,"Graphics.Barrier.staging-in":31569,"Graphics.Barrier.staging-out":14928,"Graphics.Barrier.storage-upload":1642,"Graphics.Barrier.storage-writeback":5223,"Graphics.BarrierMerged.dispatch-lead":20966,"Graphics.BarrierMerged.fill":237,"Graphics.BarrierMerged.label-run":3,"Graphics.BarrierMerged.shadow-publish":253,"Graphics.BarrierMerged.staging-in":14956,"Graphics.BarrierMerged.staging-out":14928,"Graphics.BarrierMerged.storage-writeback":763,"Graphics.Copy":33723,"Graphics.Copy.bytes":21593947394,"Graphics.RenderPass":8734,"Graphics.RenderPass.Close.command":7064,"Graphics.RenderPass.Close.image":224,"Graphics.RenderPass.Close.key":128,"Graphics.RenderPass.Close.label":3,"Graphics.RenderPass.Close.new-batch":1315,"Graphics.RenderPass.Continued":69805},"trace_dropped":false,"trace":"aps5-trace-416ae4616fa5-6252.json.gz"}

A second series with APS5_TRACE_SUMMARY_EVERY=10 (no trace; mean over the 10 s intervals at 20 to 50 s after start), same scene, six alternating rounds: main frame p50 364.5 ms and GPU busy 334.0 ms per present over the 3 runs that lasted that long, change 292.6 and 260.2 over 6 runs. Three of the six main runs ended early in an access violation on the title's Draw Geometry thread (24, 28 and 64 s after start); no run of the change did. The same access violation also ended runs of main and of the change in earlier series on this level (the same thread; I did not find its cause and do not claim the change affects it). The interval lines are in the gist.

GPU time by class, one run per side with APS5_PROFILE_GPU=1 (it adds timestamp queries; not part of the comparison), the three 10 s windows inside the level, per present: staging-out 67.7, 61.2 and 61.9 ms on main, 7.6, 7.4 and 7.8 ms with the change. The other classes stayed within a few ms (dispatch-trail 142 to 172 against 159 to 165, staging-in about 9 against 8.5, draw 10 to 13). staging-out copied back 283 to 322 MiB per present on main. The [gputime] lines are in the gist.

Other levels, same method (interval summaries, three alternating rounds, mean p50 over the 10 s intervals at 20 to 50 s; round 1 of main started its level late and is left out): timestop_casino 64.0 and 65.0 ms on main, 64.2, 63.5 and 64.0 ms with the change (that second run of the change ended in a device-lost abort, vkWaitSemaphoresKHR result -4, after about 50 s; no main run did); hub_crashsite 65.8 and 66.4 ms on main, 65.3, 65.2 and 65.5 ms with the change. Those two levels spend little time in these kernels and did not move beyond the run-to-run spread.

Tests:

  • tools/select_tests.py for the five changed files, on e04bd77e7f and ad5d92582c: 3068 tests each; the same tests fail or do not run on both: pr_overlap_tools, conventions_doc_links and title_run fail, and agc_driver_upload_cache_read_only_flush does not build on Windows (setenv; fix(tests): set the upload cache test's environment with _putenv_s on Windows #19899 is open for it).
  • ctest -R 'changed_copy|staging|copy_back|copied_write' -V on both commits (17 tests each), with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, implicit layers disabled, validation layer 1.4.368: all pass. The loader inserts the layer in 15 of the 17 tests on each side (agc_adaptive_staging and agc_buffer_pool_small_device_no_staging make no Vulkan instance; the others show Insert instance layer "VK_LAYER_KHRONOS_validation"), and no log shows was not found or any VUID- message. The same 17 tests and patched_libraries pass on b7a3334fc7 (the head before the debt-only commit) with the same settings (the loader inserts the layer in the same 15 tests; no log shows was not found or VUID-); main was not run again for this rebase.
  • agc_changed_copy, agc_bound_changed_copy and their _always variants check every word and the counter against a CPU model, with words both sides changed, bytes that mix, and now long stretches where the shader changed nothing. Breaking the change each in turn (the commit passes all four): reading the group flag of group 0 for every lane fails agc_changed_copy and agc_bound_changed_copy; a flag that ignores the shader's changes fails all four; leaving APS5_CHANGED_COPY_ALWAYS out of the flag fails the two _always tests; skipping a lane whose own word is unchanged (the previous per-word behaviour) fails the two _always tests and passes the others, since its stored bytes are the same.
  • The load-to-store window (asked in MarcZX10's review). A local diagnostic on b7a3334fc7, not committed (window-diagnostic.patch in the gist): in StagingCopyBackBound.comp the store of a word in a written group became an atomicCompSwap from the loaded value. Counters: stores to words the shader changed a byte of, stores to words it left alone, failed swaps (the word differed from the loaded value when the swap ran), and words left alone that differed from the snapshot when loaded. The stores counted can only come from the push-descriptor kernel, which therefore ran on this GPU; StagingCopyBack.comp was not measured. Three runs of about 75 s on -lvl speedrun_inflator (no input, ANYPS5_GPU=NVIDIA, ANYPS5_VOLUME=0, APS5_TRACE_COPY_BACK=1, the libraries of main dcf3d15978 with this change and the diagnostic): 626,836,635 stores, 364,275,227 to words the shader changed and 262,561,408 to words it left alone. No swap failed in either group, and every word left alone equalled the snapshot when loaded. After each successful swap on a word the shader changed, a read of the word found the stored value every time. Control (window-control.txt in the gist): with one extra line that overwrites each word just before the swap, the failures equal the stores in both groups (one 45 s run: 64,815,109 and 41,469,714). The counter lines and the summary are in the gist (window-*).
    Limits: the swap and the counters change the timing of the kernel, so this is not the rate of the kernel as committed; the control shows that the counters and the swap work on this memory, not that a CPU store racing with the GPU is caught (the control overwrites from the GPU; I added no CPU writer of my own, the game's own threads ran as usual); equal values do not exclude a store of the same value or two stores that cancel. Not compared with the old kernel's overlap.
  • The tests cannot show which words are loaded or stored; the speed claim rests on the traces above. The mutations ran on an earlier commit with the same kernels.

Not covered: other GPUs and drivers (AMD, Intel), Linux and macOS, other titles, and the load-to-store window in any scene but the one measured above or in StagingCopyBack.comp.

tools/pr_overlap.py --branch found no PR that shares lines with this one. It could not compare 36 PRs that conflict with main and share a file; I read their diffs for the changed functions and kernels. The only ones that mention them: #13863 and #13920 have a hunk right after the end of RecordBoundChangedCopy (not the lines changed here), and #15548, #17611 and #18515 call RecordChangedCopy (#18515 adds a call on a written-back resident buffer, which would get the behaviour described above). #6186 lists StagingCopyBack.comp only to wire it into CMake.

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: none
  • GPUs tested: NVIDIA GeForce RTX 5070 Ti, driver 621.14, Windows 10.0.26300
  • AI-assisted: yes

@github-actions github-actions Bot added docs agc libSceAgc* labels Nov 16, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — no blocking findings

The shader changes appear internally consistent, but I could not run the reported checks or independently verify the performance captures, so a formal approval is not justified.

What I ran

No commands or tests were run. I read the prepared description, commits, manifest, diff, both changed shaders, the changed Recorder.cpp ranges, the changed test ranges, and the debt entry at head dd038373bc58547dd2bcadc5d2ba0a4703b22089. core/libs/prx/libSceAgcDriver/tests/Recorder.cpp was excluded from the review bundle; I read the ranges containing its changed lines through repo_read.py.

Results

Check Result Note
Diff review passed Changed code and relevant callers inspected
Tests not run Test results in the description are author-reported
Performance evidence not run Captures and traces were not independently inspected

Findings

Blocking

None identified in the code inspected.

Non-blocking

  • The new kernels store every valid word in any eight-word group with a shader change. That matches the stated optimization, while preserving the existing byte merge and counter calculation. The potential lost CPU store in the load-to-store window is documented in the description and debt entry. Consider measuring this behavior in representative titles before closing the debt.

Sources

Not verified

  • I did not run tests or reproduce performance measurements. The test results, mutation checks, timing summaries, and trace availability described in the PR remain author-reported.
  • I did not read the full 17,396-line test file; I read the ranges containing the changed lines and relevant changed-copy test logic. No other changed files were excluded; all five changed files and their changed hunks were read.
  • Behavior on other GPUs, drivers, operating systems, titles, and CPU writes during the load-to-store window was not verified.

To get this to a good state

Nothing is required. Optional: independently inspect the performance captures and measure whether titles write into the documented load-to-store window.

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Nov 16, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@github-actions

github-actions Bot commented Nov 16, 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
PR 9508 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp L3301-3302

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1368 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 1395 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 4448 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 5609 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 5925 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6186 core/libs/prx/libSceAgcDriver/Graphics/shaders/StagingCopyBack.comp
core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6188 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6192 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6209 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 9289 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 9356 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 9387 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 9712 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 9852 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 10249 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 10442 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 10443 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 11444 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 13251 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 13809 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 13863 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 13920 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 13928 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 13929 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 13963 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 14262 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 15136 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 15529 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 15548 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 15570 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 17611 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 18173 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 18515 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 19100 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 20315 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp

…the import only where the shader wrote

The changed-byte copy-back kernels load the target word, which lies in the host import in system memory, and store it back word by word, so a group of neighbouring words is written as scattered 4-byte stores. Each group of eight words is now handled by eight lanes: it is left alone unless the shader changed a byte in it, and otherwise the lanes store all eight words, the untouched ones with the value just loaded, so the stores to the import are adjacent words of one group. What the kernels store for a changed byte, and what they count, is unchanged, and APS5_CHANGED_COPY_ALWAYS still stores every word. The bound kernel's dispatches are a multiple of 64 words, so a split never cuts a group.

A word of a written group that the shader left alone, or whose changed bytes the CPU had changed too, is now stored back with the value loaded a moment earlier, so a CPU store to it between the load and the store is lost. The debt file records it.

The two kernels are now also tested with long stretches where the shader changes nothing.
@github-actions github-actions Bot removed the perf numbers missing perf pull request without a before and after summary line label Nov 16, 2026
@AlexFigas
AlexFigas force-pushed the perf/agc-copy-back-whole-lines branch from dd03837 to b7a3334 Compare November 16, 2026 10:05
@AlexFigas
AlexFigas requested a review from MarcZX10 November 16, 2026 13:20

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — no blocking findings

The reviewed change is internally consistent with its stated group writeback behavior; I found no verified blocking defect in the inspected code.

What I ran

  • Reviewed head b7a3334fc731e8b5156cbf51248daef4382a5048 using repo_read.py to inspect the prepared description, manifest, diff, both changed shaders, the affected Recorder.cpp functions, and the debt entry.
  • No tests or builds were run. The description reports author-run checks; those are not checks performed for this review.

Results

Check Result Note
Diff and changed code passed Kernels, dispatch sizing, tests, and debt entry inspected
Build and tests not run Author-reported runs only
Files outside inspected ranges not run Large test file not read

Findings

Blocking

None verified in the inspected code.

Non-blocking

  • The kernels store every word in an active eight-word group, so a concurrent CPU write to an otherwise untouched word can be overwritten by the value loaded immediately before the store. This behavior is documented in the PR description and debt entry; its occurrence rate was not measured.

Not verified

  • The changed test hunks in core/libs/prx/libSceAgcDriver/tests/Recorder.cpp were not read because the full file was excluded from the review bundle. The author reports the test runs in the description, but they were not independently verified.
  • No build or test commands were run. Performance measurements, generated SPIR-V correspondence, and behavior on other GPUs, drivers, operating systems, titles, and physical consoles were not independently verified.
  • Files read: review/description.md, review/commits.md, review/manifest.json, review/diff.patch, StagingCopyBack.comp, StagingCopyBackBound.comp, selected ranges of Graphics/src/Recorder.cpp, and perf-agc-copy-back-whole-lines.md.
  • Excluded from inspection: changed test file core/libs/prx/libSceAgcDriver/tests/Recorder.cpp (full text excluded for size). No files.json was available through the reader.

To get this to a good state

Nothing is required. Optional: measure how often CPU writes overlap the load-to-store window described above.

AlexFigas added a commit to AlexFigas/AnyPS5 that referenced this pull request Nov 16, 2026
@AlexFigas
AlexFigas force-pushed the perf/agc-copy-back-whole-lines branch from 62b13aa to b105ab6 Compare November 16, 2026 14:00
@AlexFigas

Copy link
Copy Markdown
Contributor Author

@MarcZX10 About your review of b7a3334fc7:

The kernels store every word in an active eight-word group, so a concurrent CPU write to an otherwise untouched word can be overwritten by the value loaded immediately before the store. This behavior is documented in the PR description and debt entry; its occurrence rate was not measured.

I measured it on one level. The new bullet "The load-to-store window" in the description's Tested section has the method and the numbers. The patch and the counter lines are in the gist (window-*).

  • Method. A local diagnostic, not committed. The store of a word in a written group became an atomicCompSwap from the loaded value. A failed swap means the word changed between the load and the swap.
  • Result. Three runs of about 75 s on PPSA21564 -lvl speedrun_inflator (NVIDIA GeForce RTX 5070 Ti, driver 621.14, Windows 10.0.26300): 626,836,635 stores, 262,561,408 of them to words the shader left alone. No swap failed, in either group. Every word the shader left alone equalled the snapshot when loaded.
  • Control. With one extra line that overwrites each word just before the swap, the failures equal the stores in both groups (one 45 s run). This shows the counters and the swap work on this memory. The control overwrites from the GPU and I added no CPU writer of my own, so it does not show that a racing CPU store is caught.
  • Limits. The swap and the counters change the timing, so this is not the rate of the kernel as committed. It is one scene with no input, one GPU, and only the push-descriptor kernel (StagingCopyBackBound.comp); the stores counted can only come from that kernel, so StagingCopyBack.comp did not run on this GPU.

b105ab6 changes only the debt fragment: it replaces "were not measured" with "measured once, with a diagnostic" and what that leaves open, and it drops the closing condition "or a title's writes into the window are shown not to occur", since one scene does not close the gap. The kernels, Recorder.cpp and the tests are the same as in b7a3334. Could you review the head again?

@domx4q

domx4q commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Linux NVIDIA run with the Khronos validation layer of the current head, since the description's runs are on Windows.

Setup: Arch Linux, kernel 7.3.4, GCC 16.2.1 Release, NVIDIA GeForce RTX 4090 (10de:2684), driver 620.70.06, Vulkan 1.4.357, Khronos validation layer 1.4.363.1. Head b105ab6836 merged with --no-ff onto main e4caa07950 (d170d73a9e, clean merge).

ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R 'changed_copy|staging|copy_back|copied_write' -V
main e4caa07950 this PR
Result 17/17 passed 17/17 passed
Insert instance layer "VK_LAYER_KHRONOS_validation" 16 16
was not found but was requested by env var VK_INSTANCE_LAYERS 0 0
VUID- 0 0

The set holds the same 17 tests on both sides, among them agc_changed_copy, agc_bound_changed_copy, their _always variants, agc_copied_write_back, agc_bda_staging(_bound) and agc_read_written_staging, so both copy-back kernels run. Both logs show Selected GPU name=NVIDIA GeForce RTX 4090. This is a correctness and validation check only, no timing.

No new VUID- errors; all focused tests pass on Ada.

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — looks good

Nothing blocks this; the points below are optional.

What I ran

Reviewed head b105ab6836cd98744da87dc71131484b1ec99870 from the prepared diff and repository reader. No tests or builds were run in this review.

Results

Check Result Note
Diff and changed code passed Reviewed all five changed files
Build and tests not run Author reports runs in the PR description

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libSceAgcDriver/Graphics/shaders/StagingCopyBack.comp:43 and core/libs/prx/libSceAgcDriver/Graphics/shaders/StagingCopyBackBound.comp:44 write every word in a changed group, including words with no shader-written bytes. This matches the documented tradeoff and is covered in the debt fragment, but it expands the CPU/GPU load-to-store race window to those words. The submitted diagnostic measures one level on NVIDIA with the bound kernel; it does not quantify CPU-write races, other GPUs, or the address-based kernel.

Not verified

  • I read the prepared description, commits, manifest, diff, README, both shader files, the changed Recorder.cpp ranges, the changed test ranges, and the full debt fragment. The full test file was accessed through repo_read.py because it was excluded from the review folder for size; no other changed files were excluded.
  • I did not run builds or tests, verify author-reported results, or inspect performance traces. Those results are reported in the PR description only.
  • Behavior on other GPUs, drivers, operating systems, titles, and the address-based kernel's load-to-store window was not verified.

To get this to a good state

Nothing is required. The optional point above records the documented race-window limitation.

@mrfelek

mrfelek commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Linux NVIDIA timing, since the runs in the description are on Windows. The copy-back time drops 5 to 7 times here too, and every run of this change had less GPU time per present than every run of main.

Setup: ASTRO BOT (PPSA21564) 01.007.000, -lvl speedrun_inflator, no input, Linux 7.3, RTX 3080 Ti (NVIDIA 615.71.09), Ryzen 9 5950X, 3840x2160, warm shader cache. main is this PR's base dcf3d15978 for every library. The other side differs only in libSceAgcDriver.prx, built from head b105ab6836 merged onto that base. Both sides use libSceAudioPropagation.prx from #19326, as in the description. Each figure is the mean of the last five 10 s windows of aps5-summary.jsonl, ending 90 s after Level has started. Run order A B B A A B.

run side frame p50 (ms) GPU busy per present (ms)
1 main 286 261.2
2 this PR 269 240.6
3 this PR 284 249.9
4 main 277 253.0
5 main 285 258.2
6 this PR 253 222.7
  • GPU busy per present averages 257.5 ms on main and 237.7 ms with this change. Frame p50 averages 283 and 269 ms, and the two arms overlap.
  • One more pair with APS5_PROFILE_GPU=1 (last four [gputime] windows, 60 s after level start): staging-out takes 35.0 to 51.8 ms per present on main and 7.3 to 7.9 ms with this change.
  • The bytes staging-out moves per present go up, from 218–224 MiB to 274–286 MiB. That fits the kernels now storing every word of a written group.
  • For scale: six earlier runs on this level where both sides were main gave 260 to 288 ms GPU per present, so the total-frame difference is close to run-to-run noise. The staging-out drop is the clear part.

Not checked: the validation layer, and the CPU-store window the description records as debt.

AI-assisted.

@oneandonlydean
oneandonlydean merged commit 7c7d9ed into boykopovar:main Nov 17, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants