Skip to content

FramePacer: a frame is due PeriodInIntervals vblanks after its origin's, not always one (ImmediateThresholdPercent tore on-time frames) - #8133

Closed
cynbun wants to merge 1 commit into
ProjectLeonida:mainfrom
cynbun:framepacer-period-deadline
Closed

cynbun wants to merge 1 commit into
ProjectLeonida:mainfrom
cynbun:framepacer-period-deadline

Conversation

@cynbun

@cynbun cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #7694 (comment) (finding and claim are both mine).

Depends on: none (based on main dde82f27e). Overlaps: #5701, #5797, #7135 and #7685 also change runtime/src/frame_pacer.h. None of them touches due_us, so no hunks are shared.

What and why

FramePacer set every frame's deadline to the vblank right after its origin's vblank: runtime/src/frame_pacer.h:413, record.due_us = record.origin_us - offset_us_ + period_us_. present_timing (L286) and commit_present_token (L314) measure lateness from that deadline, and decide_present_sync turns lateness into a mid-scan flip when ImmediateThresholdPercent is above 0. The third SetFrameIntervalX argument only limited the frames in flight.

Reference. Microsoft's public FramePacing sample (MIT, Xbox-GDK-Samples Samples/Graphics/FramePacing):

  • RegisterFrameEvents passes periodInIntervals as that argument.
  • The readme says SetFrameIntervalX "sets the target frame interval and period".
  • The sample computes a frame's deadline as origin.SignalTime + interval.LengthInMicroseconds * interval.PeriodInIntervals - origin.IntervalOffsetInMicroseconds (FramePacing.cpp lines 440 and 1511). That is the vblank PeriodInIntervals intervals after the vblank the origin follows.

Its default is a period of 2 with 3 buffers. That fits SimpleDeviceAndSwapChain, which passes the buffer count minus 1. @Stalkerino measured the retail title passing 3 with a 33,333 µs interval (#7135).

With a period above 1, a frame presented inside its budget counted as late. A threshold above 0 then flipped it mid-scan, and the statistics recorded a PercentScanned for it. For example, with a 16 ms interval, period 3 and a 4 ms offset, the frame of origin 1 (20 ms) is due at 64 ms. On main it counted as late from 32 ms. At 36 ms it was 4 ms late, so a 30 % threshold flipped it mid-scan with PercentScanned 25.

Fix

  • frame_pacer.h: due_us = origin - offset + period_us * PeriodInIntervals. It is still fixed when the token is issued, so Runtime: keep the original PresentX frame deadline after rescheduling #2114's rescheduling behaviour is kept. The frames-in-flight limit, the flip times of on-time frames (still the next vblank after the present) and everything else are unchanged. A period of 1 behaves exactly as before.
  • Comments in frame_pacer.h and xbox_present.h, and docs/d3d12_vulkan.md §4.5 (item 4 of the call sequence and the ImmediateThresholdPercent bullet), now name the argument the period in intervals and give the sample's deadline.

Tests

  • frame_pacer_test, new case (fake clock, no GPU): period 3 with a quarter-interval offset. The test checks:

    • not late at 2.5 and 3.5 intervals, or exactly at the deadline;
    • a quarter interval late after the deadline, which tears at 30 % with PercentScanned 25;
    • a second frame presented 1.5 intervals after its origin with threshold 100 is on time and is shown at the next vblank.

    This case fails on main (line 459: pacer.present_timing(token).late_us == 0) and passes with the fix.

  • Two existing tear cases used latency 2 but expected the one-interval deadline. They now use a period of 1 and keep their expectations.

  • xbox_present_test and d3d12_boot_movie_test use a period of 1 throughout, so nothing changes there.

  • Native Linux (RTX 5080, NVIDIA 615.78.08, GCC, -DLEONIDA_VULKAN_HEADERS_FETCH=ON -DLEONIDA_REQUIRE_VULKAN=ON), ctest -C RelWithDebInfo --output-on-failure -j1 --timeout 300: 64/64 with the fix, 63/64 on main with the new test (only frame_pacer fails). shader_library_corpus and vk_sync_validation skip in both runs.

  • Windows cross build (clang-cl + xwin, -DLEONIDA_SDL3_FETCH=ON) under Wine 11.20 in a fresh prefix, same RTX 5080, ctest -C RelWithDebInfo --output-on-failure -j1 --timeout 300: 220 passed, 19 failed, 30 skipped of 269 with the fix, including frame_pacer, xbox_present and vk_presenter_sync. The same 19 fail on main, re-run there under the same prefix: gameinput_host, device_proxy, device_proxy_trace, file_system, run_startup_cmdline, run_tiny_game_abort_exit_codes, vk_pipeline_check_cli, d3d12_noop_copy, d3d12_vulkan_device, d3d12_indirect_dispatch_count, d3d12_clear, d3d12_depth_resolve, d3d12_typeless_depth, d3d12_residency, d3d12_heap_properties, d3d12_present, dxgi_swapchain, d3d12_indirect_rt and d3d12_clear_formats. On main, frame_pacer fails too, at the same line 459. The run was split into two parts, tests 1-67 and 68-269, after the first ctest process was stopped from outside.

  • python -I scripts/check_no_game_data.py on the four files and git diff --check: clean.

No game data. The retail executable wasn't run for this; the title's own ImmediateThresholdPercent isn't measured.

AI-assisted (Claude Code).

cc @shayderrr

🤖 Generated with Claude Code

…'s, not always one

SetFrameIntervalX's third argument is the period in intervals. The public FramePacing
sample (MIT, Xbox-GDK-Samples) passes `periodInIntervals` there and computes a frame's
deadline as origin + LengthInMicroseconds * PeriodInIntervals - IntervalOffsetInMicroseconds.
The pacer put every deadline one interval after the origin's vblank, so with a period
above 1 a frame presented inside its budget counted as late, and a non-zero
ImmediateThresholdPercent flipped it mid-scan (PercentScanned in the statistics).

The deadline now uses the period (still fixed when the token is issued); the
frames-in-flight limit is unchanged. frame_pacer_test gets a period-3 case (fails on
main), and the two tear cases that assumed a one-interval deadline under latency 2
now use a period of 1. Docs section 4.5 and the header comments follow.

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

Copy link
Copy Markdown

Independent native Windows check of 17267e95 with MSVC 19.44 and Windows SDK 10.0.26100.0. The stock frame_pacer CTest passed without skips.

With a three-interval period and a quarter-interval origin offset, the fake-clock frame stayed on time through its deadline. A quarter interval late, the 30% threshold selected immediate presentation and reported 25% scanned. The next on-time frame waited for vblank and reported 0%. Rescheduling the origin also preserved the deadline of an already-issued token.

CPU-only synthetic test. AI-assisted.

@AirKyzzZ

Copy link
Copy Markdown
Contributor

Tested 17267e95f on RTX 2060 (NVIDIA 566.14), Windows 10 22H2, clang-cl 23.1.6 with the MSVC 14.44 STL, RelWithDebInfo: pass.

Built frame_pacer_test and ran ctest -R ^frame_pacer$: passes. With main's runtime/src/frame_pacer.h and runtime/shims/xbox_present.h and this test it fails at line 459: pacer.present_timing(token).late_us == 0.

No game files. AI-assisted (Claude Code).

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.

4 participants