From 0dc257abe4e76cd656ef66d385deee52e61b5abb Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 13 Jan 2025 19:51:33 +1000 Subject: [PATCH] System: Set time constraints for GPU thread on MacOS Fixes erratic frame times. --- src/common/threading.cpp | 2 +- src/common/threading.h | 2 +- src/core/system.cpp | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/common/threading.cpp b/src/common/threading.cpp index 025e1b991..6081342b0 100644 --- a/src/common/threading.cpp +++ b/src/common/threading.cpp @@ -296,7 +296,7 @@ bool Threading::ThreadHandle::IsCallingThread() const #ifdef __APPLE__ -bool Threading::ThreadHandle::SetTimeConstraints(bool enabled, u64 period, u64 typical_time, u64 maximum_time) +bool Threading::ThreadHandle::SetTimeConstraints(bool enabled, u64 period, u64 typical_time, u64 maximum_time) const { const mach_port_t mach_thread_id = pthread_mach_thread_np((pthread_t)m_native_handle); if (!enabled) diff --git a/src/common/threading.h b/src/common/threading.h index 82bd52e61..fdd53bca7 100644 --- a/src/common/threading.h +++ b/src/common/threading.h @@ -58,7 +58,7 @@ public: #ifdef __APPLE__ /// Only available on MacOS, sets a period/maximum time for the scheduler. - bool SetTimeConstraints(bool enabled, u64 period, u64 typical_time, u64 maximum_time); + bool SetTimeConstraints(bool enabled, u64 period, u64 typical_time, u64 maximum_time) const; #endif protected: diff --git a/src/core/system.cpp b/src/core/system.cpp index 8abe303f5..7104987a2 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -3558,10 +3558,19 @@ void System::UpdateSpeedLimiterState() constexpr u64 MAX_FRAME_TIME_NS = 7000000; static u64 last_scheduler_period = 0; const u64 new_scheduler_period = s_state.optimal_frame_pacing ? s_state.frame_period : 0; - if (s_state.cpu_thread_handle && new_scheduler_period != last_scheduler_period) + if (new_scheduler_period != last_scheduler_period) { - s_state.cpu_thread_handle.SetTimeConstraints(s_state.optimal_frame_pacing, new_scheduler_period, MAX_FRAME_TIME_NS, - new_scheduler_period); + if (s_state.cpu_thread_handle) + { + s_state.cpu_thread_handle.SetTimeConstraints(s_state.optimal_frame_pacing, new_scheduler_period, + MAX_FRAME_TIME_NS, new_scheduler_period); + } + const Threading::ThreadHandle& gpu_thread = GPUThread::Internal::GetThreadHandle(); + if (gpu_thread) + { + gpu_thread.SetTimeConstraints(s_state.optimal_frame_pacing, new_scheduler_period, MAX_FRAME_TIME_NS, + new_scheduler_period); + } } #endif }