Skip to content

[Metal] Present a copy of each flip's frame: Poppy Playtime title flicker - #51

Merged
xnetcat merged 1 commit into
mainfrom
fix/present-snapshot
Nov 19, 2026
Merged

xnetcat merged 1 commit into
mainfrom
fix/present-snapshot

Conversation

@xnetcat

@xnetcat xnetcat commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Before submitting

Please read our contribution guidelines before opening a pull request:

➡️ CONTRIBUTING.md

Summary

Stacked on #49 (it changes the same presenter and VideoOut files); retarget to main once that merges.

Poppy Playtime Chapter 1's title menu flickered: the whole game image went black for a frame, with the perf overlay still drawn. It happens on main as well (17 of 160 rapid window captures).

Cause: the game has two display buffers and starts drawing its next frame into the one still on screen, because its sceAgcDcbWaitUntilSafeForRendering is answered as soon as that buffer's own flip was shown. The window thread samples the display buffer on its own command queue, unordered against the guest's rendering, so a present that lands after the redraw began shows the cleared frame.

Fix: every flip now hands the presenter a copy of its frame, blitted in the command stream at the flip (the pool that SHARPEMU_FLIP_AHEAD already used), and the presenter promotes it only once that command buffer completed. The window thread never samples a display buffer the guest can still write. Cost: one blit per flip and the pool's textures (4 in use in Poppy and Demon's Souls, 8.1 MB each at 1080p; at most 7). No wait is added on the guest or the GPU worker. SHARPEMU_METAL_V2_PRESENT_SNAPSHOT=0 presents the display image directly as before.

Tried first and rejected: making the safe-for-rendering wait hold while the buffer is on screen. It removes the flicker too, but stops the worker from recording the next frame while the previous one executes; Demon's Souls dropped from 248 to about 183 flips in the same 30 s segment.

Testing

  • Poppy Playtime Chapter 1 (PPSA18454), title menu, same build, switch only:
    • 15 s screen recording of the window: 0 of 784 frames black with the fix, 78 of 804 with SHARPEMU_METAL_V2_PRESENT_SNAPSHOT=0.
    • 160 rapid window captures: 0 black with the fix, 31 with the switch off.
    • 60 fps in both arms; a normal frame looks the same in both (scaling, colours, overlay).
  • Demon's Souls (PPSA01342) – reaches gameplay with the fix, the scene is correct.
  • New device test TheSnapshotHandsThePresenterAFrameARedrawOfTheDisplayBufferDoesNotChange; MetalFlipCopyTests 2 of 2 with SHARPEMU_TEST_REQUIRE_DEVICE=1.
  • dotnet test SharpEmu.slnx -c Release: every assembly passes (Libs 10639, Gpu.Metal 2074, ShaderCompiler.Metal 4833, ShaderCompiler 2550).
  • uvx reuse lint clean.

Not covered

  • Frame-time cost in Demon's Souls. Not measured: the machine was swapping for every run, so the three runs made say nothing either way. It is one full-frame blit per flip.
  • Other titles were not run with this change.
  • A title that flips from the CPU faster than the presenter drains could exhaust the pool; the copy is then refused, logged once and counted, and that frame is presented directly.

Checklist

  • I have read and followed CONTRIBUTING.md.
  • I tested my changes or marked the testing section as N/A.
  • I wrote this pull request description myself and did not paste AI-generated explanations.
  • I listed the game(s) I tested (or marked the testing section as N/A).

…e flip

The window thread sampled the guest's display image on its own queue, unordered against
the guest's GPU work. A double-buffered title that starts its next frame in the buffer
still on screen (its safe-for-rendering wait is answered once that buffer's own flip was
shown) then had its half-drawn, cleared frame presented: Poppy Playtime's title menu went
black in ~10% of window captures.

Every flip, GPU or CPU, now blits its frame into a presenter-owned texture in the same
command buffer as the flip, fenced after the passes that drew it and before anything the
guest records after the flip (the copy SHARPEMU_FLIP_AHEAD already used). The frame is
promoted only once that command buffer completed, and a pool texture is reused only when
it is not presented, queued or being encoded and its last presentation finished, so the
window thread never samples a copy being written. The guest is not held back; the cost is
one blit per flip. SHARPEMU_METAL_V2_PRESENT_SNAPSHOT=0 presents the display image live.

Poppy title, 160 window shots each: 0/160 black with the snapshot, 31/160 with it off.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 17:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@xnetcat
xnetcat changed the base branch from perf/ds-30fps to main November 19, 2026 19:14
@xnetcat
xnetcat merged commit 5cb184e into main Nov 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants