2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "VideoCommon/FPSCounter.h"
|
|
|
|
|
2022-10-26 19:17:15 +00:00
|
|
|
#include <cmath>
|
2017-06-30 07:25:32 +00:00
|
|
|
#include <iomanip>
|
2014-07-03 00:19:08 +00:00
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Timer.h"
|
2020-12-31 12:03:20 +00:00
|
|
|
#include "Core/Core.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2022-10-26 19:17:15 +00:00
|
|
|
static constexpr double US_TO_MS = 1000.0;
|
|
|
|
static constexpr double US_TO_S = 1000000.0;
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2022-10-26 19:17:15 +00:00
|
|
|
static constexpr double FPS_SAMPLE_RC_RATIO = 0.25;
|
|
|
|
|
|
|
|
FPSCounter::FPSCounter(const char* log_name)
|
2012-10-04 03:41:02 +00:00
|
|
|
{
|
2022-07-18 03:43:47 +00:00
|
|
|
m_last_time = Common::Timer::NowUs();
|
2022-10-26 19:17:15 +00:00
|
|
|
m_log_name = log_name;
|
2020-12-31 12:03:20 +00:00
|
|
|
|
|
|
|
m_on_state_changed_handle = Core::AddOnStateChangedCallback([this](Core::State state) {
|
|
|
|
if (state == Core::State::Paused)
|
|
|
|
SetPaused(true);
|
|
|
|
else if (state == Core::State::Running)
|
|
|
|
SetPaused(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
FPSCounter::~FPSCounter()
|
|
|
|
{
|
|
|
|
Core::RemoveOnStateChangedCallback(&m_on_state_changed_handle);
|
2012-10-04 03:41:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 19:17:15 +00:00
|
|
|
void FPSCounter::LogRenderTimeToFile(s64 val)
|
2012-10-04 03:41:02 +00:00
|
|
|
{
|
2014-07-13 11:04:25 +00:00
|
|
|
if (!m_bench_file.is_open())
|
2017-12-05 20:23:35 +00:00
|
|
|
{
|
2022-10-26 19:17:15 +00:00
|
|
|
File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + m_log_name, std::ios_base::out);
|
2017-12-05 20:23:35 +00:00
|
|
|
}
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2022-10-26 19:17:15 +00:00
|
|
|
m_bench_file << std::fixed << std::setprecision(8) << (val / US_TO_MS) << std::endl;
|
2012-10-04 03:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:57:54 +00:00
|
|
|
void FPSCounter::Update()
|
2012-10-04 03:41:02 +00:00
|
|
|
{
|
2022-10-26 19:17:15 +00:00
|
|
|
if (m_paused)
|
|
|
|
return;
|
2017-06-30 07:25:32 +00:00
|
|
|
|
2022-10-26 19:17:15 +00:00
|
|
|
const s64 time = Common::Timer::NowUs();
|
|
|
|
const s64 diff = std::max<s64>(0, time - m_last_time);
|
|
|
|
const s64 window = std::max(1, g_ActiveConfig.iPerfSampleUSec);
|
|
|
|
|
|
|
|
m_raw_dt = diff / US_TO_S;
|
2017-06-30 07:25:32 +00:00
|
|
|
m_last_time = time;
|
|
|
|
|
2022-10-26 19:17:15 +00:00
|
|
|
m_dt_total += diff;
|
|
|
|
m_dt_queue.push_back(diff);
|
|
|
|
|
|
|
|
while (window <= m_dt_total - m_dt_queue.front())
|
2014-07-09 15:03:17 +00:00
|
|
|
{
|
2022-10-26 19:17:15 +00:00
|
|
|
m_dt_total -= m_dt_queue.front();
|
|
|
|
m_dt_queue.pop_front();
|
2014-07-09 15:03:17 +00:00
|
|
|
}
|
2022-10-26 19:17:15 +00:00
|
|
|
|
|
|
|
// This frame count takes into account frames that are partially in the sample window
|
|
|
|
const double fps = (m_dt_queue.size() * US_TO_S) / m_dt_total;
|
|
|
|
const double rc = FPS_SAMPLE_RC_RATIO * std::min(window, m_dt_total) / US_TO_S;
|
|
|
|
const double a = std::max(0.0, 1.0 - std::exp(-m_raw_dt / rc));
|
|
|
|
|
|
|
|
// Sometimes euler averages can break when the average is inf/nan
|
|
|
|
// This small check makes sure that if it does break, it gets fixed
|
|
|
|
if (std::isfinite(m_avg_fps))
|
|
|
|
m_avg_fps += a * (fps - m_avg_fps);
|
|
|
|
else
|
|
|
|
m_avg_fps = fps;
|
|
|
|
|
|
|
|
if (g_ActiveConfig.bLogRenderTimeToFile)
|
|
|
|
LogRenderTimeToFile(diff);
|
2014-07-03 00:19:08 +00:00
|
|
|
}
|
2020-12-31 12:03:20 +00:00
|
|
|
|
|
|
|
void FPSCounter::SetPaused(bool paused)
|
|
|
|
{
|
2022-10-26 19:17:15 +00:00
|
|
|
m_paused = paused;
|
|
|
|
if (m_paused)
|
2020-12-31 12:03:20 +00:00
|
|
|
{
|
2022-07-18 03:43:47 +00:00
|
|
|
m_last_time_pause = Common::Timer::NowUs();
|
2020-12-31 12:03:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-26 19:17:15 +00:00
|
|
|
const s64 time = Common::Timer::NowUs();
|
|
|
|
const s64 diff = time - m_last_time_pause;
|
2020-12-31 12:03:20 +00:00
|
|
|
m_last_time += diff;
|
|
|
|
}
|
|
|
|
}
|