2021-06-09 03:43:19 +02:00
|
|
|
#pragma once
|
|
|
|
|
2024-06-06 10:31:20 +02:00
|
|
|
#include <array>
|
|
|
|
|
2021-06-28 19:19:29 +02:00
|
|
|
#include "thread.h"
|
2021-06-09 03:43:19 +02:00
|
|
|
#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
|
|
|
|
*/
|
2024-08-29 14:19:56 +02:00
|
|
|
void setTargetFrameRate(double frameRate, uint32_t maxLatency);
|
2021-06-09 03:43:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \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.
|
|
|
|
*/
|
2024-06-06 09:20:04 +02:00
|
|
|
void delay();
|
2021-06-09 03:43:19 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
using TimePoint = dxvk::high_resolution_clock::time_point;
|
2022-08-16 08:57:40 +00:00
|
|
|
using TimerDuration = std::chrono::nanoseconds;
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2021-06-28 19:19:29 +02:00
|
|
|
dxvk::mutex m_mutex;
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2022-08-16 08:46:40 +00:00
|
|
|
TimerDuration m_targetInterval = TimerDuration::zero();
|
2024-06-10 19:36:17 +02:00
|
|
|
TimePoint m_nextFrame = TimePoint();
|
2024-08-29 14:19:56 +02:00
|
|
|
uint32_t m_maxLatency = 0;
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2021-06-10 23:06:40 +02:00
|
|
|
bool m_envOverride = false;
|
2021-06-09 03:43:19 +02:00
|
|
|
|
2024-08-29 14:19:56 +02:00
|
|
|
uint32_t m_heuristicFrameCount = 0;
|
|
|
|
TimePoint m_heuristicFrameTime = TimePoint();
|
|
|
|
bool m_heuristicEnable = false;
|
2024-06-06 10:31:20 +02:00
|
|
|
|
2024-08-29 14:19:56 +02:00
|
|
|
bool testRefreshHeuristic(TimerDuration interval, TimePoint now, uint32_t maxLatency);
|
2024-06-06 10:31:20 +02:00
|
|
|
|
2021-06-09 03:43:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|