From e641b3777cbefb18a9cf4b95353d650b3a2abf65 Mon Sep 17 00:00:00 2001 From: ReddestDream Date: Fri, 31 Jan 2025 11:29:26 -0500 Subject: [PATCH] Update gfx_sdl2.cpp to fix macOS Metal Slowdown. (#807) Fixes stutter/inability to lock 60 consistently on macOS using Metal. May also provide performance benefits under OpenGL and Linux. This is the weird 57-58 fps issue noticed on Metal graphics on Macs for a long while. Thanks to @Spodi, @Malkierian, ProxySaw, and Fenrir for their help on Discord getting this figured out! :D --- src/graphic/Fast3D/gfx_sdl2.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/graphic/Fast3D/gfx_sdl2.cpp b/src/graphic/Fast3D/gfx_sdl2.cpp index 228dc8e..b8546fb 100644 --- a/src/graphic/Fast3D/gfx_sdl2.cpp +++ b/src/graphic/Fast3D/gfx_sdl2.cpp @@ -615,7 +615,7 @@ static inline void sync_framerate_with_timer() { const int64_t next = previous_time + 10 * FRAME_INTERVAL_US_NUMERATOR / FRAME_INTERVAL_US_DENOMINATOR; int64_t left = next - t; -#ifdef _WIN32 +#if defined(_WIN32) || defined(__APPLE__) // We want to exit a bit early, so we can busy-wait the rest to never miss the deadline left -= 15000UL; #endif @@ -632,10 +632,14 @@ static inline void sync_framerate_with_timer() { #endif } -#ifdef _WIN32 +#if defined(_WIN32) || defined(__APPLE__) t = qpc_to_100ns(SDL_GetPerformanceCounter()); while (t < next) { - YieldProcessor(); // TODO: Find a way for other compilers, OSes and architectures +#ifdef _WIN32 + YieldProcessor(); +#elif defined(__APPLE__) + sched_yield(); +#endif t = qpc_to_100ns(SDL_GetPerformanceCounter()); } #endif