blob: e5f396a094708b597bbf2b40783ad9df458f7f20 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// Copyright 2012 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "VideoCommon/FPSCounter.h"
#include <cmath>
#include <iomanip>
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/Timer.h"
#include "Core/Core.h"
#include "VideoCommon/VideoConfig.h"
static constexpr double US_TO_MS = 1000.0;
static constexpr double US_TO_S = 1000000.0;
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)
SetPaused(true);
else if (state == Core::State::Running)
SetPaused(false);
});
}
FPSCounter::~FPSCounter()
{
Core::RemoveOnStateChangedCallback(&m_on_state_changed_handle);
}
void FPSCounter::LogRenderTimeToFile(s64 val)
{
if (!m_bench_file.is_open())
{
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 / US_TO_MS) << std::endl;
}
void FPSCounter::Update()
{
if (m_paused)
return;
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;
m_dt_total += diff;
m_dt_queue.push_back(diff);
while (window <= m_dt_total - m_dt_queue.front())
{
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)
{
m_paused = paused;
if (m_paused)
{
m_last_time_pause = Common::Timer::NowUs();
}
else
{
const s64 time = Common::Timer::NowUs();
const s64 diff = time - m_last_time_pause;
m_last_time += diff;
}
}
|