Skip to content

OLD-9189: restore old upstream PR 9189 - FramePacer: at most latency frames in flight while armed (mailbox replace) - #671

Merged
aslan2142 merged 3 commits into
ProjectLeonida:mainfrom
aslan2142:frame-pacer-bounded-queue
Nov 25, 2026
Merged

aslan2142 merged 3 commits into
ProjectLeonida:mainfrom
aslan2142:frame-pacer-bounded-queue

Conversation

@aslan2142

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: OLD-9189 #14 (comment)

Restoration of my original pull request, old upstream PR 9189 in ItsNotRexed/Leonida, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Rebased onto main at 21625bc; the commit applied without changes. The only edit is in the commit message, where an old issue number is now plain text. The uncap mode added to main since then (LEONIDA_PRESENT_UNCAP) is untouched: an uncapped present still returns before it reaches the queue.

What and why

  • The bug: on main, commit_present_token and note_untokened_present add one queued frame for every present, each one period after the last.
    • So presents with no WaitFrameEventX between them grow the queue without limit, and the next wait blocks until the first of them is shown.
    • Example: at latency 2, 1000 blank presents leave 1000 frames in flight, and the next wait passes 999 periods later.
  • The fix (runtime/src/frame_pacer.h): at most latency frames are in flight while the event is armed.
    • A present into a full queue replaces the newest queued frame and takes its vblank, like a mailbox.
    • The replaced frame is never shown, so it has no display statistic. The older frames keep their vblanks.
    • The next wait passes when the oldest frame is shown.
  • A lowered latency: if SetFrameIntervalX sets a latency below the number of frames already queued, the next present cuts the queue back to the new latency from the newest end.
  • Unchanged: presents made before the event is armed stay unpaced, and a frame queued behind another still cannot be shown at once.
  • frame_pacer_test now covers:
    • 1000 untokened presents after one wait, at latency 1, 2 and 3;
    • tokened presents beyond the latency: the older frames keep their vblanks, and the replaced frame has no display statistic;
    • one present per wait;
    • a lowered latency.
  • xbox_present_test: it expected a blank present after a frame at latency 1 to leave 2 frames in flight. That is now 1.
  • docs/d3d12_vulkan.md: documents the limit.

Tested

Linux, with gcc for the native build and clang 23 targeting mingw x64, run under Wine 11.22. MSVC is left to CI.

Build frame_pacer_test (gcc, Linux) frame_pacer_test (clang mingw, Wine) xbox_present_test (clang mingw, Wine)
main pass pass pass
main with only this PR's test changes fails: 128 of 399 checks fails: 128 of 399 checks fails at the changed in-flight check
this branch pass, 399 of 399 checks pass, 399 of 399 checks pass

GPUs tested: AMD Radeon RX 7900 XTX (RADV NAVI31) and AMD Ryzen 9 7950X3D integrated GPU (RADV RAPHAEL_MENDOCINO), both Mesa 26.3.1, Arch Linux, through Wine 11.22 (xbox_present_test only; frame_pacer_test needs no GPU)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)

AI-assisted (Claude Code, Claude Opus 5.5).

…eplace)

commit_present_token and note_untokened_present appended a slot for every
present, each one period after the last, so presents made with no
WaitFrameEventX between them grew the queue without limit and the next wait
blocked until the first of them was shown: at latency 2, 1000 blank presents
left 1000 frames in flight and the next wait passed 999 periods later.

A present into a full queue now replaces the newest queued frame and takes
its vblank, like a mailbox: the replaced frame is never shown (no display
statistic), the older frames keep their vblanks, and the queue never reaches
further than `latency` vblanks ahead, so the next wait passes when the oldest
frame is shown. A queue longer than a lowered latency is cut back the same
way at the next present. Presents before the event is armed stay unpaced
(old upstream PR 4419), and a frame behind a queued one still cannot be
shown at once.

frame_pacer_test: 1000 untokened presents after one wait at latency 1, 2
and 3, tokened presents beyond the latency (the older ones keep their
vblanks, the replaced one has no display statistic), the one-present-per-wait
cadence, and a lowered latency. xbox_present_test expected the blank present
after a frame at latency 1 to make 2 frames in flight; it is 1 now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aslan2142 and others added 2 commits November 24, 2026 13:28
…ida#891, BL-6)

# Conflicts:
#	runtime/src/frame_pacer.h

Both branches added a private helper in the same place: queue_flip_locked (the bounded queue) and
record_present_timing_locked (BL-6 pacing statistics). Both are kept; note_untokened_present records its timing
first and then queues through queue_flip_locked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The pacing statistics are recorded at PresentX, before the frame is queued, so a frame replaced in a full queue
(never shown) still counts as a present with its real present-to-present interval; the replacement adds and removes
no interval. frame_pacer_test pins that at latency 1: four presents, two replaced, three intervals with known
nearest-rank percentiles and mean, two origin-to-present latencies (the replaced token included), and the shown
frames keep their vblanks. docs/d3d12_vulkan.md says the intervals are between PresentX calls, not frames shown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142

Copy link
Copy Markdown
Collaborator Author

Merged main after #891 (BL-6) to clear the conflict.

Conflict

Dropped frames and the BL-6 statistics

  • runtime: frame pacing drift and latency percentile reporting (BL-6) #891 records the interval at PresentX, before the frame is queued. A frame the bounded queue replaces was still presented, so it counts with its real present-to-present interval. The replacement adds no interval and removes none.
  • So the statistics stay meaningful: they describe the title's present cadence, not the frames shown. A burst of presents between two waits shows up as short intervals, with or without this PR.
  • No code change. docs/d3d12_vulkan.md now says this next to the BL-6 text.
  • New case in frame_pacer_test, at latency 1: four presents, two of them replaced. It checks frames 4, samples 3, mean, drift, nearest-rank p50/p95/p99, two origin-to-present latencies (the replaced token included), and that the frames that are shown keep their vblanks.

Tests (Linux, portable runtime subset)

  • ctest: 74/74 passed, 2 skipped (shader_library_corpus, vk_sync_validation), the same as main alone. Vulkan tests ran on the AMD iGPU (RDNA2, RADV).
  • frame_pacer_test: passes, including the new case and this PR's bounded-queue cases.
  • python -I tests/test_boot_trace.py: 33/33.
  • check_no_game_data.py on every changed file: clean.
  • xbox_present_test is Windows-only and was not run here.

AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142
aslan2142 merged commit a0723a9 into ProjectLeonida:main Nov 25, 2026
10 of 13 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator Author

Merged: FramePacer keeps at most latency frames in flight while armed (mailbox replace).

AI-assisted (Claude Code, Claude Opus 5.5).

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.

1 participant