2023-02-23 18:00:06 +01:00
|
|
|
#include "std_chrono_throttle.hpp"
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
void Throttle::set_frame_rate(double frame_rate)
|
|
|
|
{
|
|
|
|
max_frame_rate = frame_rate;
|
|
|
|
frame_duration = 1.0 / max_frame_rate;
|
|
|
|
frame_duration_us = microseconds(int64_t(frame_duration * 1000000.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
microseconds Throttle::remaining()
|
|
|
|
{
|
|
|
|
auto now = time_point_cast<microseconds>(steady_clock::now());
|
|
|
|
auto diff = (now - then);
|
|
|
|
return frame_duration_us - diff;
|
|
|
|
}
|
|
|
|
|
2023-07-07 20:38:58 +02:00
|
|
|
#if defined(_WIN32)
|
2023-07-06 21:53:07 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
2023-07-07 20:38:58 +02:00
|
|
|
void Throttle::wait_for_frame()
|
2023-02-23 22:24:01 +01:00
|
|
|
{
|
2023-08-05 23:59:56 +02:00
|
|
|
static UINT (WINAPI *NtDelayExecution)(BOOL, LARGE_INTEGER *) = nullptr;
|
|
|
|
static UINT (WINAPI *NtQueryTimerResolution) (ULONG *, ULONG *, ULONG *) = nullptr;
|
|
|
|
static UINT (WINAPI *NtSetTimerResolution) (ULONG, BOOL, ULONG *) = nullptr;
|
|
|
|
static int timer_resolution = 12500;
|
2023-07-06 21:53:07 +02:00
|
|
|
|
2023-08-05 23:59:56 +02:00
|
|
|
if (NtDelayExecution == nullptr)
|
|
|
|
{
|
|
|
|
HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
|
|
|
|
|
|
|
|
if (ntdll)
|
|
|
|
{
|
|
|
|
NtDelayExecution = reinterpret_cast<typeof NtDelayExecution>(GetProcAddress(ntdll, "NtDelayExecution"));
|
|
|
|
NtQueryTimerResolution = reinterpret_cast<typeof NtQueryTimerResolution>(GetProcAddress(ntdll, "NtQueryTimerResolution"));
|
|
|
|
NtSetTimerResolution = reinterpret_cast<typeof NtSetTimerResolution>(GetProcAddress(ntdll, "NtSetTimerResolution"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NtQueryTimerResolution)
|
|
|
|
{
|
|
|
|
ULONG min, max, current;
|
|
|
|
NtQueryTimerResolution(&min, &max, ¤t);
|
|
|
|
|
|
|
|
if (NtSetTimerResolution)
|
|
|
|
NtSetTimerResolution(max, true, ¤t);
|
|
|
|
|
|
|
|
timer_resolution = current * 5 / 4;
|
|
|
|
}
|
|
|
|
}
|
2023-07-06 21:53:07 +02:00
|
|
|
|
2023-02-23 22:24:01 +01:00
|
|
|
auto time_to_wait = remaining();
|
|
|
|
|
2023-08-05 23:59:56 +02:00
|
|
|
if (time_to_wait < -(frame_duration_us / 8))
|
2023-07-07 20:49:10 +02:00
|
|
|
{
|
2023-02-23 22:24:01 +01:00
|
|
|
reset();
|
2023-07-07 20:49:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-02-23 22:24:01 +01:00
|
|
|
|
2023-08-05 23:59:56 +02:00
|
|
|
if (NtDelayExecution)
|
2023-02-23 22:24:01 +01:00
|
|
|
{
|
2023-08-05 23:59:56 +02:00
|
|
|
if (time_to_wait.count() * 10 > timer_resolution)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.QuadPart = -(time_to_wait.count() * 10 - timer_resolution);
|
|
|
|
NtDelayExecution(false, &li);
|
|
|
|
}
|
2023-07-07 20:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
time_to_wait = remaining();
|
|
|
|
while (time_to_wait.count() > 0)
|
|
|
|
{
|
|
|
|
std::this_thread::yield();
|
|
|
|
time_to_wait = remaining();
|
2023-02-23 22:24:01 +01:00
|
|
|
}
|
2023-07-07 20:38:58 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
void Throttle::wait_for_frame()
|
|
|
|
{
|
|
|
|
auto time_to_wait = remaining();
|
|
|
|
|
2023-07-07 21:13:17 +02:00
|
|
|
if (time_to_wait < -frame_duration_us)
|
2023-07-07 20:49:10 +02:00
|
|
|
{
|
2023-07-07 20:38:58 +02:00
|
|
|
reset();
|
2023-07-07 20:49:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-07-07 20:38:58 +02:00
|
|
|
|
|
|
|
if (time_to_wait.count() > 1000)
|
2023-08-05 23:59:56 +02:00
|
|
|
std::this_thread::sleep_for(time_to_wait - 1000us);
|
2023-07-06 21:53:07 +02:00
|
|
|
|
|
|
|
time_to_wait = remaining();
|
|
|
|
while (time_to_wait.count() > 0)
|
|
|
|
{
|
|
|
|
std::this_thread::yield();
|
|
|
|
time_to_wait = remaining();
|
|
|
|
}
|
2023-07-07 20:38:58 +02:00
|
|
|
}
|
|
|
|
#endif
|
2023-07-06 21:53:07 +02:00
|
|
|
|
2023-07-07 20:38:58 +02:00
|
|
|
void Throttle::wait_for_frame_and_rebase_time()
|
|
|
|
{
|
|
|
|
wait_for_frame();
|
2023-07-06 21:53:07 +02:00
|
|
|
advance();
|
2023-02-23 22:24:01 +01:00
|
|
|
}
|
|
|
|
|
2023-02-23 18:00:06 +01:00
|
|
|
void Throttle::advance()
|
|
|
|
{
|
|
|
|
then += frame_duration_us;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Throttle::reset()
|
|
|
|
{
|
|
|
|
auto now = time_point_cast<microseconds>(steady_clock::now());
|
|
|
|
then = now;
|
|
|
|
}
|