Avoid accumulating timing errors between frames

This commit is contained in:
MaranBr 2026-02-10 15:05:42 -04:00
parent 9da9cfa38f
commit 32d2d41ecb
2 changed files with 4 additions and 19 deletions

View File

@ -120,16 +120,11 @@ public:
/// Waits for the given GPU tick, optionally pacing frames.
void Wait(u64 tick, double target_fps = 0.0) {
if (Settings::values.use_speed_limit.GetValue() && target_fps > 0.0) {
const auto frame_duration = std::chrono::duration<double>(1.0 / target_fps);
auto now = std::chrono::steady_clock::now();
if (next_frame_time == std::chrono::steady_clock::time_point{}) {
next_frame_time = now + std::chrono::duration_cast<std::chrono::nanoseconds>(frame_duration);
}
while (now > next_frame_time) {
next_frame_time += std::chrono::duration_cast<std::chrono::nanoseconds>(frame_duration);
}
const std::chrono::duration<double> frame_duration_sec(1.0 / target_fps);
const auto now = std::chrono::steady_clock::now();
const auto frames_elapsed = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count() / std::chrono::duration_cast<std::chrono::nanoseconds>(frame_duration_sec).count());
const auto next_frame_time = std::chrono::steady_clock::time_point(std::chrono::nanoseconds((frames_elapsed + 1) * std::chrono::duration_cast<std::chrono::nanoseconds>(frame_duration_sec).count()));
std::this_thread::sleep_until(next_frame_time);
next_frame_time += std::chrono::duration_cast<std::chrono::nanoseconds>(frame_duration);
}
if (tick >= master_semaphore->CurrentTick()) {
Flush();
@ -137,11 +132,6 @@ public:
master_semaphore->Wait(tick);
}
/// Resets the frame pacing state by updating the next frame time to now.
void ResetFramePacing() {
next_frame_time = std::chrono::steady_clock::now();
}
/// Returns the master timeline semaphore.
[[nodiscard]] MasterSemaphore& GetMasterSemaphore() const noexcept {
return *master_semaphore;
@ -296,8 +286,6 @@ private:
std::condition_variable_any event_cv;
std::jthread worker_thread;
std::chrono::steady_clock::time_point next_frame_time{};
#ifdef _WIN32
std::unique_ptr<HighResolutionTimer> high_res_timer;
#endif

View File

@ -146,9 +146,6 @@ void Swapchain::Create(
{
is_outdated = false;
is_suboptimal = false;
scheduler.ResetFramePacing();
width = width_;
height = height_;
#ifdef ANDROID