summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/FPSCounter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Core/VideoCommon/FPSCounter.cpp')
-rw-r--r--Source/Core/VideoCommon/FPSCounter.cpp63
1 files changed, 42 insertions, 21 deletions
diff --git a/Source/Core/VideoCommon/FPSCounter.cpp b/Source/Core/VideoCommon/FPSCounter.cpp
index e38305eb7f..e5f396a094 100644
--- a/Source/Core/VideoCommon/FPSCounter.cpp
+++ b/Source/Core/VideoCommon/FPSCounter.cpp
@@ -3,7 +3,7 @@
#include "VideoCommon/FPSCounter.h"
-#include <fstream>
+#include <cmath>
#include <iomanip>
#include "Common/CommonTypes.h"
@@ -12,11 +12,15 @@
#include "Core/Core.h"
#include "VideoCommon/VideoConfig.h"
-static constexpr u64 FPS_REFRESH_INTERVAL_US = 250000;
+static constexpr double US_TO_MS = 1000.0;
+static constexpr double US_TO_S = 1000000.0;
-FPSCounter::FPSCounter()
+static constexpr double FPS_SAMPLE_RC_RATIO = 0.25;
+
+FPSCounter::FPSCounter(const char* log_name)
{
m_last_time = Common::Timer::NowUs();
+ m_log_name = log_name;
m_on_state_changed_handle = Core::AddOnStateChangedCallback([this](Core::State state) {
if (state == Core::State::Paused)
@@ -31,47 +35,64 @@ FPSCounter::~FPSCounter()
Core::RemoveOnStateChangedCallback(&m_on_state_changed_handle);
}
-void FPSCounter::LogRenderTimeToFile(u64 val)
+void FPSCounter::LogRenderTimeToFile(s64 val)
{
if (!m_bench_file.is_open())
{
- File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + "render_time.txt",
- std::ios_base::out);
+ File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + m_log_name, std::ios_base::out);
}
- m_bench_file << std::fixed << std::setprecision(8) << (val / 1000.0) << std::endl;
+ m_bench_file << std::fixed << std::setprecision(8) << (val / US_TO_MS) << std::endl;
}
void FPSCounter::Update()
{
- const u64 time = Common::Timer::NowUs();
- const u64 diff = time - m_last_time;
- m_time_diff_secs = static_cast<double>(diff / 1000000.0);
- if (g_ActiveConfig.bLogRenderTimeToFile)
- LogRenderTimeToFile(diff);
+ if (m_paused)
+ return;
- m_frame_counter++;
- m_time_since_update += diff;
+ 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;
m_last_time = time;
- if (m_time_since_update >= FPS_REFRESH_INTERVAL_US)
+ m_dt_total += diff;
+ m_dt_queue.push_back(diff);
+
+ while (window <= m_dt_total - m_dt_queue.front())
{
- m_fps = m_frame_counter / (m_time_since_update / 1000000.0);
- m_frame_counter = 0;
- m_time_since_update = 0;
+ m_dt_total -= m_dt_queue.front();
+ m_dt_queue.pop_front();
}
+
+ // 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);
}
void FPSCounter::SetPaused(bool paused)
{
- if (paused)
+ m_paused = paused;
+ if (m_paused)
{
m_last_time_pause = Common::Timer::NowUs();
}
else
{
- const u64 time = Common::Timer::NowUs();
- const u64 diff = time - m_last_time_pause;
+ const s64 time = Common::Timer::NowUs();
+ const s64 diff = time - m_last_time_pause;
m_last_time += diff;
}
}