From 08723659386e463219d60447ef83ebdc5cf3f2f9 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 12 Mar 2021 17:18:48 +0100 Subject: [PATCH] Add config flag: SUPPORT_WINMM_HIGHRES_TIMER #1641 Useful to avoid WinMM requirement and useful to avoid possible performance issues. --- src/config.h | 3 +++ src/core.c | 18 +++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/config.h b/src/config.h index d0c8cfdd9..aecff8e5d 100644 --- a/src/config.h +++ b/src/config.h @@ -40,6 +40,9 @@ #define SUPPORT_SSH_KEYBOARD_RPI 1 // Draw a mouse pointer on screen #define SUPPORT_MOUSE_CURSOR_NATIVE 1 +// Setting a higher resolution can improve the accuracy of time-out intervals in wait functions. +// However, it can also reduce overall system performance, because the thread scheduler switches tasks more often. +#define SUPPORT_WINMM_HIGHRES_TIMER 1 // Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used //#define SUPPORT_BUSY_WAIT_LOOP 1 // Use a half-busy wait loop, in this case frame sleeps for some time and runs a busy-wait-loop at the end diff --git a/src/core.c b/src/core.c index c4b52b874..4ee2f6315 100644 --- a/src/core.c +++ b/src/core.c @@ -197,7 +197,7 @@ #define GLFW_EXPOSE_NATIVE_WIN32 #include "GLFW/glfw3native.h" // WARNING: It requires customization to avoid windows.h inclusion! - #if !defined(SUPPORT_BUSY_WAIT_LOOP) + #if defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP) // NOTE: Those functions require linking with winmm library unsigned int __stdcall timeBeginPeriod(unsigned int uPeriod); unsigned int __stdcall timeEndPeriod(unsigned int uPeriod); @@ -575,7 +575,7 @@ static int FindNearestConnectorMode(const drmModeConnector *connector, uint widt #endif // PLATFORM_RPI || PLATFORM_DRM #if defined(_WIN32) - // NOTE: We include Sleep() function signature here to avoid windows.h inclusion + // NOTE: We include Sleep() function signature here to avoid windows.h inclusion (kernel32 lib) void __stdcall Sleep(unsigned long msTimeout); // Required for Wait() #endif @@ -819,7 +819,7 @@ void CloseWindow(void) glfwTerminate(); #endif -#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32) && !defined(PLATFORM_UWP) +#if defined(_WIN32) && defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP) && !defined(PLATFORM_UWP) timeEndPeriod(1); // Restore time period #endif @@ -4204,10 +4204,14 @@ static void SetupFramebuffer(int width, int height) // Initialize hi-resolution timer static void InitTimer(void) { - srand((unsigned int)time(NULL)); // Initialize random seed + srand((unsigned int)time(NULL)); // Initialize random seed -#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32) && !defined(PLATFORM_UWP) - timeBeginPeriod(1); // Setup high-resolution timer to 1ms (granularity of 1-2 ms) +// Setting a higher resolution can improve the accuracy of time-out intervals in wait functions. +// However, it can also reduce overall system performance, because the thread scheduler switches tasks more often. +// High resolutions can also prevent the CPU power management system from entering power-saving modes. +// Setting a higher resolution does not improve the accuracy of the high-resolution performance counter. +#if defined(_WIN32) && defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP) && !defined(PLATFORM_UWP) + timeBeginPeriod(1); // Setup high-resolution timer to 1ms (granularity of 1-2 ms) #endif #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) @@ -4220,7 +4224,7 @@ static void InitTimer(void) else TRACELOG(LOG_WARNING, "TIMER: Hi-resolution timer not available"); #endif - CORE.Time.previous = GetTime(); // Get time as double + CORE.Time.previous = GetTime(); // Get time as double } // Wait for some milliseconds (stop program execution)