From 401e239589d3f3f6a4547267b33d9f44e2348dc3 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 24 Jul 2021 06:06:27 -0400 Subject: [PATCH] Add sleep fallback to Window::step() if glfwSwapBuffers() fails to block until monitor refresh. --- src/Window.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Window.cpp b/src/Window.cpp index 2d5665ea..c772ad41 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -483,6 +483,17 @@ void Window::step() { } glfwSwapBuffers(win); + + // On some platforms, glfwSwapBuffers() doesn't wait on monitor refresh, so we have to sleep as a fallback. + if (internal->frameSwapInterval > 0) { + double frameDurationExpected = internal->frameSwapInterval / internal->monitorRefreshRate * 0.90; + double swapBuffersTime = system::getTime(); + double frameDurationRemaining = frameDurationExpected - (swapBuffersTime - frameTime); + if (frameDurationRemaining > 0.0) { + std::this_thread::sleep_for(std::chrono::duration(frameDurationRemaining)); + } + } + t5 = system::getTime(); // DEBUG("pre-step %6.1f step %6.1f draw %6.1f nvgEndFrame %6.1f glfwSwapBuffers %6.1f total %6.1f",