win: improve fps timing and fix glfw vsync

main
Dominik Madarász 2024-02-11 01:01:49 +01:00
parent 80f773ec01
commit c7c347fa90
5 changed files with 174 additions and 180 deletions

View File

@ -56017,14 +56017,12 @@ static void swapBuffersWGL(_GLFWwindow* window)
{ {
if (!window->monitor) if (!window->monitor)
{ {
if (IsWindowsVistaOrGreater()) // HACK: Use DwmFlush when desktop composition is enabled on Windows Vista and 7
if (!IsWindows8OrGreater() && IsWindowsVistaOrGreater())
{ {
// DWM Composition is always enabled on Win8+ BOOL enabled = FALSE;
BOOL enabled = IsWindows8OrGreater();
// HACK: Use DwmFlush when desktop composition is enabled if (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled)
if (enabled ||
(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
{ {
int count = abs(window->context.wgl.interval); int count = abs(window->context.wgl.interval);
while (count--) while (count--)
@ -56039,20 +56037,19 @@ static void swapBuffersWGL(_GLFWwindow* window)
static void swapIntervalWGL(int interval) static void swapIntervalWGL(int interval)
{ {
_GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot); _GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot);
assert(window != NULL);
window->context.wgl.interval = interval; window->context.wgl.interval = interval;
if (!window->monitor) if (!window->monitor)
{ {
if (IsWindowsVistaOrGreater()) // HACK: Disable WGL swap interval when desktop composition is enabled on Windows
// Vista and 7 to avoid interfering with DWM vsync
if (!IsWindows8OrGreater() && IsWindowsVistaOrGreater())
{ {
// DWM Composition is always enabled on Win8+ BOOL enabled = FALSE;
BOOL enabled = IsWindows8OrGreater();
// HACK: Disable WGL swap interval when desktop composition is enabled to if (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled)
// avoid interfering with DWM vsync
if (enabled ||
(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
interval = 0; interval = 0;
} }
} }
@ -379093,7 +379090,8 @@ int fps__timing_thread(void *arg) {
int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess; int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess;
uint64_t took = -time_ns(); uint64_t took = -time_ns();
#if is(win32) #if is(win32)
timeBeginPeriod(1); Sleep( tt > 0 ? tt/1e6 : 0 ); do_once timeBeginPeriod(1);
sleep_ns( (float)tt );
#else #else
sleep_ns( (float)tt ); sleep_ns( (float)tt );
#endif #endif
@ -379452,7 +379450,7 @@ bool window_create_from_handle(void *handle, float scale, unsigned flags) {
//glEnable(GL_TEXTURE_2D); //glEnable(GL_TEXTURE_2D);
// 0:disable vsync, 1:enable vsync, <0:adaptive (allow vsync when framerate is higher than syncrate and disable vsync when framerate drops below syncrate) // 0:disable vsync, 1:enable vsync, <0:adaptive (allow vsync when framerate is higher than syncrate and disable vsync when framerate drops below syncrate)
flags |= optioni("--vsync", 1) || flag("--vsync") ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED; flags |= optioni("--vsync", 0) || flag("--vsync") ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED;
flags |= optioni("--vsync-adaptive", 0) || flag("--vsync-adaptive") ? WINDOW_VSYNC_ADAPTIVE : 0; flags |= optioni("--vsync-adaptive", 0) || flag("--vsync-adaptive") ? WINDOW_VSYNC_ADAPTIVE : 0;
int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || glfwExtensionSupported("EXT_swap_control_tear"); int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || glfwExtensionSupported("EXT_swap_control_tear");
int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE); int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE);

View File

@ -23097,14 +23097,12 @@ static void swapBuffersWGL(_GLFWwindow* window)
{ {
if (!window->monitor) if (!window->monitor)
{ {
if (IsWindowsVistaOrGreater()) // HACK: Use DwmFlush when desktop composition is enabled on Windows Vista and 7
if (!IsWindows8OrGreater() && IsWindowsVistaOrGreater())
{ {
// DWM Composition is always enabled on Win8+ BOOL enabled = FALSE;
BOOL enabled = IsWindows8OrGreater();
// HACK: Use DwmFlush when desktop composition is enabled if (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled)
if (enabled ||
(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
{ {
int count = abs(window->context.wgl.interval); int count = abs(window->context.wgl.interval);
while (count--) while (count--)
@ -23119,20 +23117,19 @@ static void swapBuffersWGL(_GLFWwindow* window)
static void swapIntervalWGL(int interval) static void swapIntervalWGL(int interval)
{ {
_GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot); _GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot);
assert(window != NULL);
window->context.wgl.interval = interval; window->context.wgl.interval = interval;
if (!window->monitor) if (!window->monitor)
{ {
if (IsWindowsVistaOrGreater()) // HACK: Disable WGL swap interval when desktop composition is enabled on Windows
// Vista and 7 to avoid interfering with DWM vsync
if (!IsWindows8OrGreater() && IsWindowsVistaOrGreater())
{ {
// DWM Composition is always enabled on Win8+ BOOL enabled = FALSE;
BOOL enabled = IsWindows8OrGreater();
// HACK: Disable WGL swap interval when desktop composition is enabled to if (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled)
// avoid interfering with DWM vsync
if (enabled ||
(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
interval = 0; interval = 0;
} }
} }

View File

@ -15,7 +15,8 @@ int fps__timing_thread(void *arg) {
int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess; int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess;
uint64_t took = -time_ns(); uint64_t took = -time_ns();
#if is(win32) #if is(win32)
timeBeginPeriod(1); Sleep( tt > 0 ? tt/1e6 : 0 ); do_once timeBeginPeriod(1);
sleep_ns( (float)tt );
#else #else
sleep_ns( (float)tt ); sleep_ns( (float)tt );
#endif #endif
@ -374,7 +375,7 @@ bool window_create_from_handle(void *handle, float scale, unsigned flags) {
//glEnable(GL_TEXTURE_2D); //glEnable(GL_TEXTURE_2D);
// 0:disable vsync, 1:enable vsync, <0:adaptive (allow vsync when framerate is higher than syncrate and disable vsync when framerate drops below syncrate) // 0:disable vsync, 1:enable vsync, <0:adaptive (allow vsync when framerate is higher than syncrate and disable vsync when framerate drops below syncrate)
flags |= optioni("--vsync", 1) || flag("--vsync") ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED; flags |= optioni("--vsync", 0) || flag("--vsync") ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED;
flags |= optioni("--vsync-adaptive", 0) || flag("--vsync-adaptive") ? WINDOW_VSYNC_ADAPTIVE : 0; flags |= optioni("--vsync-adaptive", 0) || flag("--vsync-adaptive") ? WINDOW_VSYNC_ADAPTIVE : 0;
int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || glfwExtensionSupported("EXT_swap_control_tear"); int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || glfwExtensionSupported("EXT_swap_control_tear");
int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE); int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE);

View File

@ -37132,14 +37132,12 @@ static void swapBuffersWGL(_GLFWwindow* window)
{ {
if (!window->monitor) if (!window->monitor)
{ {
if (IsWindowsVistaOrGreater()) // HACK: Use DwmFlush when desktop composition is enabled on Windows Vista and 7
if (!IsWindows8OrGreater() && IsWindowsVistaOrGreater())
{ {
// DWM Composition is always enabled on Win8+ BOOL enabled = FALSE;
BOOL enabled = IsWindows8OrGreater();
// HACK: Use DwmFlush when desktop composition is enabled if (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled)
if (enabled ||
(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
{ {
int count = abs(window->context.wgl.interval); int count = abs(window->context.wgl.interval);
while (count--) while (count--)
@ -37154,20 +37152,19 @@ static void swapBuffersWGL(_GLFWwindow* window)
static void swapIntervalWGL(int interval) static void swapIntervalWGL(int interval)
{ {
_GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot); _GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot);
assert(window != NULL);
window->context.wgl.interval = interval; window->context.wgl.interval = interval;
if (!window->monitor) if (!window->monitor)
{ {
if (IsWindowsVistaOrGreater()) // HACK: Disable WGL swap interval when desktop composition is enabled on Windows
// Vista and 7 to avoid interfering with DWM vsync
if (!IsWindows8OrGreater() && IsWindowsVistaOrGreater())
{ {
// DWM Composition is always enabled on Win8+ BOOL enabled = FALSE;
BOOL enabled = IsWindows8OrGreater();
// HACK: Disable WGL swap interval when desktop composition is enabled to if (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled)
// avoid interfering with DWM vsync
if (enabled ||
(SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
interval = 0; interval = 0;
} }
} }

View File

@ -26234,7 +26234,8 @@ int fps__timing_thread(void *arg) {
int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess; int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess;
uint64_t took = -time_ns(); uint64_t took = -time_ns();
#if is(win32) #if is(win32)
timeBeginPeriod(1); Sleep( tt > 0 ? tt/1e6 : 0 ); do_once timeBeginPeriod(1);
sleep_ns( (float)tt );
#else #else
sleep_ns( (float)tt ); sleep_ns( (float)tt );
#endif #endif
@ -26593,7 +26594,7 @@ bool window_create_from_handle(void *handle, float scale, unsigned flags) {
//glEnable(GL_TEXTURE_2D); //glEnable(GL_TEXTURE_2D);
// 0:disable vsync, 1:enable vsync, <0:adaptive (allow vsync when framerate is higher than syncrate and disable vsync when framerate drops below syncrate) // 0:disable vsync, 1:enable vsync, <0:adaptive (allow vsync when framerate is higher than syncrate and disable vsync when framerate drops below syncrate)
flags |= optioni("--vsync", 1) || flag("--vsync") ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED; flags |= optioni("--vsync", 0) || flag("--vsync") ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED;
flags |= optioni("--vsync-adaptive", 0) || flag("--vsync-adaptive") ? WINDOW_VSYNC_ADAPTIVE : 0; flags |= optioni("--vsync-adaptive", 0) || flag("--vsync-adaptive") ? WINDOW_VSYNC_ADAPTIVE : 0;
int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || glfwExtensionSupported("EXT_swap_control_tear"); int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || glfwExtensionSupported("EXT_swap_control_tear");
int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE); int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE);