2021-06-09 03:43:19 +02:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "thread.h"
|
2021-06-10 23:06:40 +02:00
|
|
|
#include "util_env.h"
|
2021-06-09 03:43:19 +02:00
|
|
|
#include "util_fps_limiter.h"
|
2022-09-15 13:41:03 +02:00
|
|
|
#include "util_sleep.h"
|
2021-06-09 03:43:19 +02:00
|
|
|
#include "util_string.h"
|
|
|
|
|
|
|
|
#include "./log/log.h"
|
|
|
|
|
2022-08-16 08:56:01 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2021-06-09 03:43:19 +02:00
|
|
|
namespace dxvk {
|
|
|
|
|
|
|
|
FpsLimiter::FpsLimiter() {
|
2021-06-10 23:06:40 +02:00
|
|
|
std::string env = env::getEnvVar("DXVK_FRAME_RATE");
|
|
|
|
|
|
|
|
if (!env.empty()) {
|
|
|
|
try {
|
|
|
|
setTargetFrameRate(std::stod(env));
|
|
|
|
m_envOverride = true;
|
|
|
|
} catch (const std::invalid_argument&) {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 03:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FpsLimiter::~FpsLimiter() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FpsLimiter::setTargetFrameRate(double frameRate) {
|
2021-06-28 19:19:29 +02:00
|
|
|
std::lock_guard<dxvk::mutex> lock(m_mutex);
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2021-06-10 23:06:40 +02:00
|
|
|
if (!m_envOverride) {
|
2024-06-06 10:31:20 +02:00
|
|
|
TimerDuration interval = frameRate != 0.0
|
2022-08-16 08:46:40 +00:00
|
|
|
? TimerDuration(int64_t(double(TimerDuration::period::den) / frameRate))
|
|
|
|
: TimerDuration::zero();
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2024-06-06 10:31:20 +02:00
|
|
|
if (m_targetInterval != interval) {
|
|
|
|
m_targetInterval = interval;
|
|
|
|
|
|
|
|
m_heuristicFrameCount = 0;
|
|
|
|
m_heuristicEnable = false;
|
|
|
|
|
|
|
|
if (m_targetInterval != TimerDuration::zero() && !m_initialized)
|
|
|
|
initialize();
|
|
|
|
}
|
2021-06-10 23:06:40 +02:00
|
|
|
}
|
2021-06-09 03:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-06 09:20:04 +02:00
|
|
|
void FpsLimiter::delay() {
|
2024-06-06 10:31:20 +02:00
|
|
|
std::unique_lock<dxvk::mutex> lock(m_mutex);
|
|
|
|
auto interval = m_targetInterval;
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2024-06-06 10:31:20 +02:00
|
|
|
if (interval == TimerDuration::zero())
|
2021-06-09 03:43:19 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto t0 = m_lastFrame;
|
|
|
|
auto t1 = dxvk::high_resolution_clock::now();
|
|
|
|
|
2024-06-06 10:31:20 +02:00
|
|
|
if (interval < TimerDuration::zero()) {
|
|
|
|
interval = -interval;
|
|
|
|
|
|
|
|
if (!testRefreshHeuristic(interval, t1))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subsequent code must not access any class members
|
|
|
|
// that can be written by setTargetFrameRate
|
|
|
|
lock.unlock();
|
|
|
|
|
2022-08-16 08:46:40 +00:00
|
|
|
auto frameTime = std::chrono::duration_cast<TimerDuration>(t1 - t0);
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2024-06-06 10:31:20 +02:00
|
|
|
if (frameTime * 100 > interval * 103 - m_deviation * 100) {
|
2021-06-09 03:43:19 +02:00
|
|
|
// If we have a slow frame, reset the deviation since we
|
|
|
|
// do not want to compensate for low performance later on
|
2022-08-16 08:46:40 +00:00
|
|
|
m_deviation = TimerDuration::zero();
|
2021-06-09 03:43:19 +02:00
|
|
|
} else {
|
|
|
|
// Don't call sleep if the amount of time to sleep is shorter
|
|
|
|
// than the time the function calls are likely going to take
|
2024-06-06 10:31:20 +02:00
|
|
|
TimerDuration sleepDuration = interval - m_deviation - frameTime;
|
2022-09-15 13:41:03 +02:00
|
|
|
t1 = Sleep::sleepFor(t1, sleepDuration);
|
2021-06-09 03:43:19 +02:00
|
|
|
|
|
|
|
// Compensate for any sleep inaccuracies in the next frame, and
|
|
|
|
// limit cumulative deviation in order to avoid stutter in case we
|
|
|
|
// have a number of slow frames immediately followed by a fast one.
|
2022-08-16 08:46:40 +00:00
|
|
|
frameTime = std::chrono::duration_cast<TimerDuration>(t1 - t0);
|
2024-06-06 10:31:20 +02:00
|
|
|
m_deviation += frameTime - interval;
|
|
|
|
m_deviation = std::min(m_deviation, interval / 16);
|
2021-06-09 03:43:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_lastFrame = t1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-06 10:31:20 +02:00
|
|
|
bool FpsLimiter::testRefreshHeuristic(TimerDuration interval, TimePoint now) {
|
|
|
|
if (m_heuristicEnable)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Use a sliding window to determine whether the current
|
|
|
|
// frame rate is higher than the targeted refresh rate
|
|
|
|
uint32_t heuristicWindow = m_heuristicFrameTimes.size();
|
|
|
|
auto windowStart = m_heuristicFrameTimes[m_heuristicFrameCount % heuristicWindow];
|
|
|
|
auto windowDuration = std::chrono::duration_cast<TimerDuration>(now - windowStart);
|
|
|
|
|
|
|
|
m_heuristicFrameTimes[m_heuristicFrameCount % heuristicWindow] = now;
|
|
|
|
m_heuristicFrameCount += 1;
|
|
|
|
|
|
|
|
// The first window of frames may contain faster frames as the
|
|
|
|
// internal swap chain queue fills up, so we should ignore it.
|
|
|
|
if (m_heuristicFrameCount < 2 * heuristicWindow)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Test whether we should engage the frame rate limiter. It will
|
|
|
|
// stay enabled until the refresh rate or vsync enablement change.
|
|
|
|
m_heuristicEnable = (103 * windowDuration) < (100 * heuristicWindow) * interval;
|
|
|
|
|
|
|
|
if (m_heuristicEnable) {
|
|
|
|
double got = (double(heuristicWindow) * double(TimerDuration::period::den))
|
|
|
|
/ (double(windowDuration.count()) * double(TimerDuration::period::num));
|
|
|
|
double refresh = double(TimerDuration::period::den) / (double(TimerDuration::period::num) * double(interval.count()));
|
|
|
|
|
|
|
|
Logger::info(str::format("Detected frame rate (~", uint32_t(got), ") higher than selected refresh rate of ~",
|
|
|
|
uint32_t(refresh), " Hz.\n", "Engaging frame rate limiter."));
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_heuristicEnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-09 03:43:19 +02:00
|
|
|
void FpsLimiter::initialize() {
|
2022-08-16 08:52:24 +00:00
|
|
|
m_lastFrame = dxvk::high_resolution_clock::now();
|
|
|
|
m_initialized = true;
|
|
|
|
}
|
|
|
|
|
2021-06-09 03:43:19 +02:00
|
|
|
}
|