From 294e8c920191ecd1d1931602d2fd9ffeb85adbe7 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 13 Sep 2024 20:23:57 +1000 Subject: [PATCH] System: Set realtime constraints on MacOS Significantly improves frame timing/pacing, now it is a flat line on my 2023 MBP like other platforms. --- src/core/system.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/core/system.cpp b/src/core/system.cpp index 07bb9c7e8..44f3a4c79 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -3412,6 +3412,19 @@ void System::UpdateSpeedLimiterState() if (g_settings.increase_timer_resolution) SetTimerResolutionIncreased(s_throttler_enabled); + +#ifdef __APPLE__ + // To get any resemblence of consistent frame times on MacOS, we need to tell the scheduler how often we need to run. + // Assume a maximum of 7ms for running a frame. It'll be much lower than that, Apple Silicon is fast. + constexpr u64 MAX_FRAME_TIME_NS = 7000000; + static u64 last_scheduler_period = 0; + const u64 new_scheduler_period = s_optimal_frame_pacing ? s_frame_period : 0; + if (s_cpu_thread_handle && new_scheduler_period != last_scheduler_period) + { + s_cpu_thread_handle.SetTimeConstraints(s_optimal_frame_pacing, new_scheduler_period, MAX_FRAME_TIME_NS, + new_scheduler_period); + } +#endif } void System::UpdateDisplayVSync()