perf(ui): hand a pop-out its picture without slowing the game's present #3195
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3195
Loading…
Reference in a new issue
No description provided.
Delete branch "neotoxic/Longshot:perf/popout-handoff"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
A follow-up to #3175, pushed to its branch after it was merged.
A mod window popped out of the game's picture gets a new picture every time its UI changes, every frame for a graph. Handing it to the system window was done on the thread the game waits for, and did more there than it needs:
hos_display_popout_showpremultiplied every pixel under the lock and posted a paint message per picture;pop_paintcreated and deleted a DIB section per picture. Now the game's thread does a plain copy (onememcpywhen the rows are packed) and posts one message until the window has painted. The premultiply moved topop_paint, on the window's own thread, which swaps the picture out under the lock and premultiplies it after the lock is let go (as dyst suggested), and keeps its DIB section while the size holds and frees it when the window goes. A picture that could not be painted no longer leaves the window waiting for a message that never comes.popouts_turnswaps the picture with a buffer of its own instead of amallocand a copy per picture, andhos_sdl_window_blitblends over the background through three 256-entry tables, with no index division per pixel when the surface is the picture's size.Also, a second commit:
tests/test_ui_renderer.pyfails onmain. #3175 was merged with its pop-out reads changed to the present's fences, and the check still looked for the earlier slot count. It now checks the merged reads.Read first:
hos_display_popout_showandpop_paintinhost/display.c,popouts_turninhost/posix/display_sdl.c,hos_sdl_window_blitinhost/hos_sdl.c.Testing
Tested:
dev.py rtanddev.py smokeon this branch (PASS, in play from kokiri-start, the menu opened), and the FPS counter's graph popped out in Hyrule Field for 3 runs of 60 s (the measurements below): it showed and updated, no FATAL line.dev.py rtand the graph popped out for 3 runs of 60 s uncapped and 3 capped at 60 fps.2472246d(dyst's review: Linux's uncovered window, the premultiply after the lock): on Windows the hand-off measurement below, the graph popped out for 3 runs of 60 s, shown and updated, no FATAL line; on CachyOSdev.py rtanddev.py smokePASS at220b70eb(the Linux fix).tests/test_ui_renderer.pypasses, its two new checks failing on the code before them.run_all.py --changed origin/mainon Windows, 38 passed;test_dlssg.pyfails here and onmainalike (its check ofvk_draw.cpp's Upscaler row, a file this does not touch).Still to test:
Measurements
Verdict: on Windows, handing a picture to a popped-out window takes the present thread about a quarter less time: a median of 53-56 µs against 72-76 µs on
main, the runs' spreads apart. That is about 20 µs of a 5.3 ms frame, under what the frame time can show, and the frame time indeed shows no change. The lock is never waited on, on either side, so the premultiply after the lock (dyst's suggestion) changes no number here; it keeps a slow paint from ever holding a present. Linux's hand-off is not timed yet (Still to test).The hand-off itself (dyst's review, point 2).
mainatde6fb7b5(A) against this PR at2472246d(B), both with the same local timing patch, not in this PR:hos_display_popout_showtimed on the present thread with QueryPerformanceCounter, the whole call and its wait onpop_lock, each 4,096 hand-offs written as a median, p99, max and mean.dev.py ab --a-runtime(two builds), Hyrule Field, the FPS counter's graph popped out (it changes every frame), 3 interleaved runs of 60 s,HOS_FPS_MAX=1000. Windows 11, RTX 5080; each cell: the three runs, each the median of its 3 or 4 blocks.de6fb7b52472246dOn Windows
mainpremultiplies every pixel there; this PR copies the rows. The copy is most of what is left: the graph's picture is copied whole each frame.Earlier, the frame time (before dyst's review):
Setup:
dev.py ab, Hyrule Field, the FPS counter's graph in a window (it changes every frame), 3 interleaved runs of 60 s a side, uncapped (HOS_FPS_MAX=1000) unless said. Before: #3175's head before this change (e185922e), whose host code is whatmainhas had sincea87eb850; after: this change (6d8c02ea, the same host code as this PR).e185922eOn the PC, popped out holds exactly 164.9 fps in every run, a display's refresh rate there: while a layered window is updated every frame the desktop compositor paces the game's presents. A window in the picture costs nothing measurable on either machine (173.6 fps with or without it on the PC, 80.5 and 81.2 on the laptop).
Risks
A pop-out window that shows no picture, or a stale one, on Windows or Linux; a revert is enough. The Windows window keeps one DIB section of its size while it is open. Nothing changes for a player without a popped-out window.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). (test_dlssg.pyfails onmaintoo: its check ofvk_draw.cpp's Upscaler row)main, or Measurements says why there is nothing to measure. (measured: no gain the frame time shows)third_party/untouched; nothing GPL-incompatible.When it applies
tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Tested in game: Windows 11, RTX 5080: dev.py rt and smoke PASS on this branch, and the FPS counter's graph popped out in Hyrule Field for 3 runs of 60 s: it showed and updated every frame, no FATAL line. CachyOS, RTX 3060 Laptop, Wayland: the same host code (6d8c02ea) popped out for 6 runs of 60 s, uncapped and capped at 60 fps.
WIP: perf(ui): hand a pop-out its picture without slowing the game's presentto perf(ui): hand a pop-out its picture without slowing the game's presentOn why the numbers still hold on current
main(18edd509): whatmaingained since the measured code (#3161's PCIe probe, #2423's fog controls, #3081's float paths off by default) changes none of the pop-out's code or the host's hand-off this PR touches. They may move the frame time on both sides alike; the comparison is in-picture against popped-out on the same build, and that difference is the system compositor's, as Measurements says.@review
Recommendation: Request changes. A failed Windows paint-message post can leave a pop-out permanently blank or stale. The reported runs also show no measurable performance gain, so the performance change has not met the repository’s merge standard.
The change coalesces Windows paint messages, keeps the Windows bitmap, swaps Linux picture buffers and uses lookup tables for Linux blending. It adds retained bitmap and buffer memory; the reported frame rates do not show a player-visible gain.
Blocking (1): must change before merging.
host/display.c:1175: A failed PostMessageW leaves fresh set, so subsequent pictures do not post another paint message. When: If the window thread’s message queue is full when a picture is posted, PostMessageW fails. Later calls replace the pixels but see fresh already set; no paint message arrives to clear it, so the pop-out stays blank or stale until it is reopened. Do: Handle posting failure with synchronized pending-message state and a retry path, so a later picture can schedule a paint without losing a concurrent update.Should fix (1): not blocking, but worth doing before merging.
tests/test_ui_renderer.py:225: The new swap checks only match source text and do not exercise the buffer hand-off or paint retry behavior. (more on the line) Do: Add a game-free behavioral test covering successive buffer swaps, the newest picture, cleanup and recovery after a failed paint-message post.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
host/display.c:1203: The measurements do not show whether the change reduces the hand-off cost or improves frame pacing. (more on the line) Do: Measure the targeted hand-off and lock-wait time, average and 1% low fps, and median and p99 frame times against the merge base over interleaved runs on Windows and Linux with the graph popped out. (@fixdoes its part first: Move the Windows clamp outside the shared lock using a separate picture buffer, so the presenting thread does not wait for that per-pixel work.)@fixcan do L1, L2 and L3.Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven. The PR reports 164.9 fps before and after on Windows, where compositor pacing capped the result, and 75.1 versus 74.6 fps on Linux across three runs per side. It does not report the targeted hand-off time, 1% lows or frame-time distributions.
Other systems: The changed hand-off runs on Windows and Linux for popped-out windows, independent of GPU rendering. Windows was tested on an RTX 5080 and Linux Wayland on an RTX 3060 Laptop; other window-system and Tier 1 coverage is not reported. Gamescope disables pop-outs. Linux retains a second picture buffer per open pop-out; Windows retains a DIB.
Switches: None added, changed or removed.
Reviewed:
c51774f2e0against18edd5097e: Read AGENTS.md, CONTRIBUTING.md, the PR template, the relevant standards, commits and complete diff; read the changed pop-out and blit functions, their callers and fence collection path, and the changed test. The large changed files were not all read in full. docs/review-checklist.md and the adjacent Tooling checkout were absent. No generated files, translations or recorded run files were changed.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run for this review. The author reports in-game Windows and Linux runs; CI was still running.
Overlaps and merge order: No overlapping pull requests could be checked with the network closed. The PR is reported as not mergeable.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -1201,0 +1200,4 @@if (p->dib && p->dib_bits) {/* (premultiplied as Windows takes it: no colour above its alpha, which an sRGB image's encoding can leave atan edge's partly covered pixels) */const uint8_t *s = p->pix;L2 (should fix): The measurements do not show whether the change reduces the hand-off cost or improves frame pacing.
When: On Windows the pixel clamp now runs on the window thread but still holds pop_lock, which the presenting thread also needs. The reported Windows frame rate is compositor-limited and the Linux difference is within noise, so neither result establishes a gain.
Do: Measure the targeted hand-off and lock-wait time, average and 1% low fps, and median and p99 frame times against the merge base over interleaved runs on Windows and Linux with the graph popped out.
@ -221,3 +224,2 @@shown = turn[turn.find("if (p->fresh && p->pix) {"):turn.find("ReleaseSRWLockExclusive(&pop_lock);", turn.find("if (p->fresh && p->pix) {"))]c.true("host/posix/display_sdl.c: a picture is marked shown only once its copy was made (no copy: still fresh)",0 <= shown.find("if (pix) {") < shown.find("p->fresh = 0;") and shown.find("if (pix) {") < shown.find("p->shown = 1;"))c.true("host/posix/display_sdl.c: a picture is taken by swapping buffers, no allocation or copy per picture",L3 (should fix): The new swap checks only match source text and do not exercise the buffer hand-off or paint retry behavior.
When: The test still passes if the swapped-out picture is corrupted before blitting or a failed Windows message post permanently prevents later paints, provided the checked strings remain present.
Do: Add a game-free behavioral test covering successive buffer swaps, the newest picture, cleanup and recovery after a failed paint-message post.
L1 fixed in
a567ced9: whenPostMessageWfails,hos_display_popout_showclearsfreshunderpop_lock, so the next picture posts again. No paint message can be in flight then, becausefreshwas clear before this picture set it.L3:
tests/test_ui_renderer.pynow also checks that a failed post clearsfresh. A behavioural test of the retry would need a window thread whose message queue can be made full, which no game-free check here has, so it stays a source check.Brought up to current
main(de6fb7b5) by a merge,b597b488, nothing rewritten. The one conflict was the pop-out read check intests/test_ui_renderer.py:main's version is kept, plus this branch'snewest->at > p.shown_atline. On Windows 11, RTX 5080,dev.py rtanddev.py smokepass onb597b488(in play from kokiri-start, 900 frames, the menu opened), andtests/test_ui_renderer.pypasses.Recommendation: Request changes. On Linux, a pop-out that has been uncovered or restored now redraws the picture before its newest one, or nothing at all, because the expose handler still reads the buffer the new swap has made the spare. And it is a performance change whose own measurement shows no gain ("No measured gain, no merge", standards).
For a player with a mod window popped out, this aims to make handing over each picture cheaper on the thread the game's present waits for. On Windows, the premultiply moves to the window's thread, the DIB is kept and there is one paint message until the window paints. On Linux, a buffer swap replaces a malloc and a copy, and the blend uses lookup tables. The code is a little more involved than main's, and the gain is not shown yet.
Does it help: Unproven. Measurements' own verdict is "no change the frame time shows". On Windows the compositor paces both sides at 164.9 fps, so these runs can't show a CPU-side gain. On Linux, popped-out goes from 75.1 to 74.6 fps over 3 runs a side with no spread given. By those fps the pop-out's cost rises from +0.99 to +1.07 ms, while the table's ms column says +0.95 to +0.81, so the two columns disagree. The earlier review's L2 (measure the hand-off itself) has no answer yet.
Other systems: the change is host-side, so the GPU vendor doesn't matter. What matters is the window system. X11 sends exposes whenever a window is uncovered or restored, and that is where blocking point 1 shows. No run so far uses X11, or a mod window whose picture stays still (a graph hides it: every frame brings a new picture). Gamescope has no pop-outs.
Switches: none.
Reviewed:
b597b4889againstde6fb7b5(the merge's main parent; the diff is the four files, +72/-40). Read in full where it changed and what it relies on:hos_display_popout_show,pop_paint,pop_of_window,popouts_sync's close path, and the WM_APP+17/+18 dispatch.Pop,hos_display_popout_show,popout_event,popouts_turn.hos_sdl_window_blit, and the change totests/test_ui_renderer.py.Nothing was run.
Fixed in this review: nothing.
Checked and fine:
a567ced9. A failedPostMessageWclearsfreshunder the lock. With one producer, no message can be in flight then. When no further picture comes, it behaves as main does.popouts_sync(WM_APP+17), andpop_paintruns on WM_APP+18. It is deselected beforeDeleteDC, and deleted on a size change and on close. A failedCreateDIBSectionis tried again at the next picture and still clearsfresh. A first DIB failure no longer marks the window shown without showing it (main setsshownbefore it checks the bitmap).needstays withinuint32_t(w and h ≤ 8192). Packed rows take onememcpy, other rows one each, as on Linux.pix(reallocated pastpcap), and the blit reads onlyspare, on the display thread. Both are freed on close. After the first swappixis NULL withpcap0, so the producer allocates.hos_sdl_window_blit: the tables hold exactly the old per-pixel(bg*k+127)/255, andsamepicks the same rows and columns. That is 768 divisions a blit.tests/test_ui_renderer.py, and theif (newest->at > p.shown_at) {it checks is atgpu/vk/vk_paint.cpp:1379.Blocking
host/posix/display_sdl.c:827:case HOS_SDL_EV_EXPOSED: if (p->pix) p->fresh = 1;still treatspixas the picture on screen. Afterpopouts_turn's swap (913-918),pixis the spare:This shows for any mod window whose picture stays still: on X11, when another window is moved off it or it is restored from minimised, it shows the wrong picture until its UI next changes. Main redraws the newest. One fix: when nothing newer is waiting, swap back. Nothing else is needed:
popout_eventruns on the same thread aspopouts_turn, so no blit is readingsparethen, andpw/phstill describe it whilefreshis clear.Also add a check in
tests/test_ui_renderer.pythat the EXPOSED case shows the spare again; the new swap checks pass with this bug in place.Measurements: this is a
perfPR, and the standards say "No measured gain, no merge". Measure what the title claims, main against this PR, interleaved, with medians and spreads. That means either the present thread's time inhos_display_popout_showand its wait onpop_lock, or the frame time (with 1% lows and p99) somewhere the compositor doesn't pace the game. If no gain shows, the change doesn't merge as a performance change.Suggestions
host/display.c:1261: the premultiply now runs insidepop_lockon the window's thread. If the present thread's nexthos_display_popout_showorhos_display_popoutscomes during it, that thread waits for the whole per-pixel loop. Swappingpixwith a window-thread buffer under the lock (as Linux now does) and premultiplying into the DIB after the lock is released would keep that loop off the lock. That is what the title promises.Testing: By the author:
dev.py rtandsmokeonb597b488, and the graph popped out for 3×60 s.a567ced9and the merge).Missing: X11, and a still (non-graph) pop-out that is covered and uncovered, minimised and restored, on both systems. The measurement in point 2. The Tier 1 box is unticked. CI on
b597b488has not started (11 checks waiting).Overlaps and merge order:
host/display.c,host/posix/display_sdl.c,host/hos_sdl.c,vk_paint.cpp). Whichever lands second rebases.@ -1255,0 +1258,4 @@p->dib = CreateDIBSection(mem, &bi, DIB_RGB_COLORS, &p->dib_bits, NULL, 0);p->dw = p->dib && p->dib_bits ? pw : 0, p->dh = p->dib && p->dib_bits ? ph : 0;}if (p->dib && p->dib_bits) {Suggestion: this per-pixel loop runs under pop_lock, so a present-thread call that comes during it waits for all of it. Swap pix with a window-thread buffer under the lock and premultiply into the DIB after releasing it.
@ -918,0 +913,4 @@if (p->fresh && p->pix) { /* (swapped out, no copy: the blit runs without the lock on this thread's buffer) */uint8_t *const t = p->spare;const uint32_t tcap = p->scap;p->spare = p->pix, p->scap = p->pcap, p->pix = t, p->pcap = tcap;After this swap, pix is the spare, but popout_event's HOS_SDL_EV_EXPOSED (line 827) still does if (p->pix) p->fresh = 1: NULL after the first picture (the expose is dropped), else the picture before the newest (an uncovered window redraws one back; exposes flip between the two). There: swap back when !p->fresh && p->spare, then set fresh (see the review body).
@dyst: thanks. Blocking 1 is fixed in
220b70eb, as you proposed. The EXPOSED case swaps the spare back intopixwhen nothing newer is waiting, then setsfresh.popout_eventholdspop_lockon the display thread, aspopouts_turndoes, so no blit reads the spare then.tests/test_ui_renderer.pynow checks the case: it fails on the old line and passes with the fix. Checked on CachyOS (RTX 3060 Laptop, Wayland):dev.py rtanddev.py smokepass on220b70eb. A still pop-out covered and uncovered on X11 has not been tried yet.Blocking 2: agreed, the frame time can't show this. Next I'll time the hand-off itself on the present thread,
mainagainst this PR, interleaved, with medians and spreads, on both systems: the time spent inhos_display_popout_showand its wait onpop_lock. I'll take your suggestion (premultiply after the lock is released) into that run, since it is what the title promises. If no gain shows, I'll say so and this stops being aperfchange.@dyst: point 2, measured, and your suggestion is in
2472246d. Measurements has the table.2472246d):pop_paintswaps the picture out ofpixunderpop_lock, as Linux'spopouts_turndoes, and premultiplies into the DIB after the lock is released.freshis still cleared under the lock. The spare, the DIB andshownare touched only on the window's thread, which frees them on close. A new check intests/test_ui_renderer.pykeeps the loop out of the lock.main(de6fb7b5) against2472246d, with a local timing patch on both sides that is not in the PR. Two builds throughdev.py ab --a-runtime, the graph popped out, 3 interleaved runs of 60 s, Windows 11 and an RTX 5080. The median hand-off is 72-76 µs onmainand 53-56 µs here, the runs' spreads apart; the p99 is 89-107 µs against 75-87 µs. The wait onpop_lockis 0 at the median and 0.1 µs at the p99 on both sides. So the gain is the copy replacing the premultiply on the present thread, and the lock change shows no number here. It is about 20 µs of a 5.3 ms frame, and the frame time shows no change (fps 187.7-190.2 on both sides).If ~20 µs a frame on Windows is too small to count as a performance change, I can retitle it as a fix: the window no longer waits forever when a paint message can't be posted, and Linux's uncovered window now redraws.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.