From faaf900eb72743c63afc05aa6b121ac0ec816514 Mon Sep 17 00:00:00 2001 From: Idiot Date: Thu, 6 Jul 2023 14:53:07 -0500 Subject: [PATCH] More precise std::chrono throttle implementation. --- external/glslang | 2 +- vulkan/std_chrono_throttle.cpp | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/external/glslang b/external/glslang index 9c7fd1a3..3ebb72cc 160000 --- a/external/glslang +++ b/external/glslang @@ -1 +1 @@ -Subproject commit 9c7fd1a33e5cecbe465e1cd70170167d5e40d398 +Subproject commit 3ebb72cc7429f0ab8218104dc3687c659c0f364d diff --git a/vulkan/std_chrono_throttle.cpp b/vulkan/std_chrono_throttle.cpp index 86b95540..d6c53a1a 100644 --- a/vulkan/std_chrono_throttle.cpp +++ b/vulkan/std_chrono_throttle.cpp @@ -25,18 +25,44 @@ void Throttle::wait_for_frame() std::this_thread::sleep_for(time_to_wait); } +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#endif void Throttle::wait_for_frame_and_rebase_time() { +#ifdef _WIN32 + static HANDLE timer = nullptr; + + if (timer == nullptr) + timer = CreateWaitableTimer(nullptr, true, nullptr); +#endif + auto time_to_wait = remaining(); if (time_to_wait < -frame_duration_us / 10) reset(); - if (time_to_wait.count() > 0) + if (time_to_wait.count() > 1000) { - std::this_thread::sleep_for(time_to_wait); - advance(); +#ifdef _WIN32 + LARGE_INTEGER li; + li.QuadPart = -(time_to_wait.count() - 1000) * 10; + SetWaitableTimer(timer, &li, 0, nullptr, nullptr, false); + WaitForSingleObject(timer, INFINITE); +#else + std::this_thread::sleep_for(time_to_wait - 1000); +#endif } + + time_to_wait = remaining(); + while (time_to_wait.count() > 0) + { + std::this_thread::yield(); + time_to_wait = remaining(); + } + + advance(); } void Throttle::advance()