Skip to content

fix(agc): complete a flip only once the frame's GPU work has finished - #417

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:fix/agc-flip-after-gpu
Oct 3, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:fix/agc-flip-after-gpu

Conversation

@oneandonlydean

Copy link
Copy Markdown
Collaborator

What

A flip could complete while the GPU was still executing the frame it shows.

With APS5_FLIP_INFLIGHT at its default of 1, Driver::Present reported the flip (gpuReady, so flipPendingNum drops and the flip event fires) as soon as the blit was queued behind the frame's batches. A title reads that completion as "the GPU is done with this frame" and starts reusing the frame's memory: per-draw constants, vertex data, command buffers. The recorded batches read guest memory in place when they finally execute, so draws that had not run yet read the next frame's data.

In PPSA21564 on my integration branch this showed up as follows (that branch also delivers end-of-pipe interrupts without draining, as #409 proposes, so the window there is wider than on main, see below):

  • runaway shader loops: a vertex program reads an integer sample count from its material constants and steps a loop by its reciprocal. When the constant ring had already been rewritten, it read float 1.0 (0x3f800000) from another draw as the count and looped about 1e9 times, stalling the GPU;
  • exploded geometry and flat coloured frames, from other draws' matrices and constants.

With APS5_FLIP_INFLIGHT=0 (the render fence wait before the flip completes) none of this happened. In my test runs of the affected level (300 s each), the hits of that loop went from 435 to 0 with this change.

The presenter now queues the present as before and then waits for the frame's own fence (FinishPresent), outside the GPU mutex, before reporting the flip. The swapchain present still goes out at once, so the display does not wait for the CPU; only the completion the title observes moves to the point where the GPU no longer reads the frame. APS5_SYNC_FLIP already waited and is unchanged. The comment in FlipRequest::GpuReady that described the old completion point is updated.

On main a RELEASE_MEM that raises an interrupt drains the device, so the window is the frame's work recorded after its last such packet, plus anything ordered only by plain labels or waits. That is still enough for a title to see the flip before its last draws have run.

When no blit is submitted (the window is not presentable, e.g. minimised), the flip still completes without waiting, as before.

The flip now completes later, so a present is more likely to end past the next vblank. That moves the next flip's target out by one vblank on main; #416 fixes that and should go in with or before this one.

Tested

  • Linux (CachyOS, RTX 4090): full build with BUILD_TESTING=ON and ctest pass.
  • New by-hand mode video_out_flip_tests aftergpu (needs a GPU and a display, like the file's other presentation modes): submits a busy compute dispatch followed by an end-of-pipe label and a flip, and checks that the flip does not complete before the label lands. The label has no interrupt, so main does not drain at it. Three runs each: on main the flip completed at 2.1, 9.3 and 10.8 ms while the label landed at about 243 ms, and the test failed; with this change the flip completed 0.1 to 0.2 ms after the label (about 243 to 257 ms) and the test passed.
  • video_out_flip_tests present and backtoback pass.
  • PPSA21564: the change has been on my integration branch through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS), with FPS at the reference.

Checklist

@github-actions github-actions Bot added prx agc libSceAgc* needs rebase Conflicts with main, rebase needed labels Oct 2, 2026
With APS5_FLIP_INFLIGHT (default 1) a presentation trailed on the GPU and
the flip completed as soon as its blit was queued behind the frame's
batches. The title reads that completion as "the GPU is done with the
frame" and rewrites the frame's memory, but the recorded GPU work still reads
guest memory in place when it runs. In PPSA21564 the per-draw constant
ring of the next frames landed under vertex programs that had not run
yet: a program read an integer sample count as float 1.0 from another
draw and stepped a loop by its reciprocal, about 1e9 iterations, and
other draws got wrong matrices.

The presenter now queues the present and then waits for the frame's own
fence, outside the GPU mutex, before reporting the flip, so the title
cannot reuse the frame's memory while the GPU still reads it.

Test: video_out_flip_tests aftergpu (needs a GPU and a display) submits a
busy compute dispatch, an end-of-pipe label without an interrupt (which
does not drain the device) and a flip, and checks that the flip
completes no earlier than the label lands.
@oneandonlydean
oneandonlydean force-pushed the fix/agc-flip-after-gpu branch from a41e357 to d9632d5 Compare October 3, 2026 06:48
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 3, 2026
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 3, 2026
…oykopovar#304 boykopovar#305 boykopovar#306 boykopovar#322 boykopovar#409 boykopovar#416, boykopovar#276 boykopovar#277 boykopovar#275 boykopovar#307, shader ops boykopovar#226 boykopovar#255 boykopovar#260 boykopovar#280 boykopovar#308 boykopovar#309 boykopovar#311 boykopovar#318 boykopovar#320 boykopovar#324 boykopovar#326 boykopovar#379 boykopovar#384 boykopovar#399, boykopovar#261, kernel boykopovar#335 boykopovar#341 boykopovar#349 boykopovar#355, libs boykopovar#227 boykopovar#244 boykopovar#245 boykopovar#273 boykopovar#274 boykopovar#279 boykopovar#286 boykopovar#287 boykopovar#289 boykopovar#296 boykopovar#300 boykopovar#328 boykopovar#358 boykopovar#402) into astrobot

Where our PR merged in a revised form, main's version is taken; where
astrobot carries the same work plus later local commits, astrobot's code
stays and main's additions appear once.

Conflicts:
- AvPlayer (Export.cpp both libs, include/AvPlayer.hpp, src/Player.cpp,
  src/Source.cpp, AvPlayerLibrary.cmake, CMakeLists.txt, SceTypes.hpp,
  tests/GuestAvPlayer.cpp): main's boykopovar#307 (our reviewed squash, which carries
  9c8e7fd e9ea5c8 adcede3 fb4eeb9 953057c ac456ee). It links the
  shared ffmpeg-core target, so astrobot's second FFmpeg build goes:
  3rdparty/FFmpeg submodule, core/libs/FFmpeg.cmake and src/Exports.cpp.
- libScePngEnc: main's boykopovar#227 (supersedes OverkillLabs' encoder).
- libScePad Export.cpp: main's boykopovar#287 tilt correction (supersedes 49d45f5).
- libSceAudioPropagation Export.cpp: astrobot's object model, which
  defines every function boykopovar#289 declares as a stub.
- libSceSaveData.native Export.cpp: both (SaveDataHoldWrites and boykopovar#289's
  unknown exports).
- VideoOutDriver.cpp: astrobot's (2546bd1 counters around boykopovar#416's
  lastFlipVblank store, same value). tests/Flip.cpp: boykopovar#416's pacing test and
  boykopovar#417's after-GPU test both kept.
- boykopovar#278 files (DisplayFormat, VulkanDevice, GpuColorTransfer,
  ResidentPresent, agc CMakeLists): main's comment-free boykopovar#278 plus
  astrobot's P5 present-direct (b29728d), which removed DetileImage,
  ResidentPresent.hpp and the recorder-test hook; the resident present test
  stays its own executable.
- VulkanDevice.cpp/.hpp: astrobot's GDS, local memory probe, sample dumps
  and P5 code; boykopovar#303/boykopovar#409 comments as main has them; main's shader clock
  (boykopovar#324) and DepthSurface-free includes.
- RequestSerializer.cpp: version stays 8 (6 mesh passthrough, 7
  nonConstantImageOffsets, 8 astrobot's later field). boykopovar#303's auto-merged
  second write and read of nonConstantImageOffsets removed.
- SpirvEmitter.hpp, SpirvEmitterState.hpp, Recompiler.hpp/.cpp, StageIO.hpp,
  SpirvOptimizer.cpp, InstructionTranslator.cpp, ShaderReplay.cpp: local
  fields kept, boykopovar#303/boykopovar#305 comments as main has them.
- SpirvAluEmitterMath.cpp: v_readlane keeps the split-wave path and takes
  boykopovar#322's wrap at the wave size for the others.
- SpirvEmitter.cpp: astrobot's single-lane layout check.
- SpirvFlowEmitter.cpp: boykopovar#304 as merged (the second lane emits a shared
  value itself when the first lane defined nothing).
- SpirvImageEmitter.cpp: main's Offset operand placed before MinLod.
- boykopovar#304: main's WaveUniformValues (SpirvAnalysis.cpp) replaces astrobot's
  copy in SpirvWaveExchange.cpp.
- Recorder.hpp/.cpp, Draw.hpp, Graphics Draw.cpp, ShaderResources.cpp:
  astrobot's boykopovar#276/boykopovar#277 with its later snapshot work (per-use budgets,
  stale-snapshot refresh, profile counters, boykopovar#410, boykopovar#424, fan GS); boykopovar#409/boykopovar#306
  comments as main has them.
- PacketMemory.cpp: boykopovar#409 as merged plus astrobot's two opt-outs.
- tests: ShaderMemory.cpp takes boykopovar#303's and boykopovar#304's new tests; Recorder.cpp
  takes boykopovar#409's afterRecordedWorkTests and drops main's copies of the
  boykopovar#276/boykopovar#277 tests astrobot already has; Structurizer.cpp keeps boykopovar#413's case.

boykopovar#143 (NGG passthrough) stays as on astrobot: upstream did not touch its
files since the boykopovar#258 revert.
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 3, 2026
…heir PR heads

The code of the three open PRs is unchanged on their heads apart from the
opt-outs and comments astrobot keeps. Their heads add tests: boykopovar#415's
EvaluatedValues table test (and drops an unused include), boykopovar#417's flip test
releases its GPU work without an interrupt select, and boykopovar#418's
linux_entry_argv relinks a fixture whose entry checks argc and argv.
@oneandonlydean
oneandonlydean merged commit 7f5c3ec into boykopovar:main Oct 3, 2026
4 of 5 checks passed
@oneandonlydean
oneandonlydean deleted the fix/agc-flip-after-gpu branch October 3, 2026 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant