1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-02-10 07:54:38 +01:00
dxvk/src/util/util_fps_limiter.h
Philip Rebohle 51f2e246fa [util,dxvk] Limit frame rate based on deadline
This should work better now that present_wait is universally supported.
2024-06-11 15:45:17 +02:00

63 lines
1.3 KiB
C++

#pragma once
#include <array>
#include "thread.h"
#include "util_time.h"
namespace dxvk {
/**
* \brief Frame rate limiter
*
* Provides functionality to stall an application
* thread in order to maintain a given frame rate.
*/
class FpsLimiter {
public:
/**
* \brief Creates frame rate limiter
*/
FpsLimiter();
~FpsLimiter();
/**
* \brief Sets target frame rate
* \param [in] frameRate Target frame rate
*/
void setTargetFrameRate(double frameRate);
/**
* \brief Stalls calling thread as necessary
*
* Blocks the calling thread if the limiter is enabled
* and the time since the last call to \ref delay is
* shorter than the target interval.
*/
void delay();
private:
using TimePoint = dxvk::high_resolution_clock::time_point;
using TimerDuration = std::chrono::nanoseconds;
dxvk::mutex m_mutex;
TimerDuration m_targetInterval = TimerDuration::zero();
TimePoint m_nextFrame = TimePoint();
bool m_envOverride = false;
uint32_t m_heuristicFrameCount = 0;
std::array<TimePoint, 16> m_heuristicFrameTimes = { };
bool m_heuristicEnable = false;
bool testRefreshHeuristic(TimerDuration interval, TimePoint now);
};
}