summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Yuri Kunde Schlesner2017-02-20 16:31:59 -0800
committerGravatar Yuri Kunde Schlesner2017-02-26 17:22:04 -0800
commitfb1979d7e26c20fe2b8d2c3d3dc998e5e00f2f61 (patch)
tree690461954bff040727fa2cb3b34c9d71c003ea9c /src
parentCore: Make PerfStats internally locked (diff)
downloadyuzu-fb1979d7e26c20fe2b8d2c3d3dc998e5e00f2f61.tar.gz
yuzu-fb1979d7e26c20fe2b8d2c3d3dc998e5e00f2f61.tar.xz
yuzu-fb1979d7e26c20fe2b8d2c3d3dc998e5e00f2f61.zip
Core: Re-write frame limiter
Now based on std::chrono, and also works in terms of emulated time instead of frames, so we can in the future frame-limit even when the display is disabled, etc. The frame limiter can also be enabled along with v-sync now, which should be useful for those with displays running at more than 60 Hz.
Diffstat (limited to 'src')
-rw-r--r--src/core/core.h1
-rw-r--r--src/core/hw/gpu.cpp39
-rw-r--r--src/core/perf_stats.cpp33
-rw-r--r--src/core/perf_stats.h16
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.cpp6
5 files changed, 53 insertions, 42 deletions
diff --git a/src/core/core.h b/src/core/core.h
index db3b98a05..6c9c936b5 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -94,6 +94,7 @@ public:
94 } 94 }
95 95
96 PerfStats perf_stats; 96 PerfStats perf_stats;
97 FrameLimiter frame_limiter;
97 98
98private: 99private:
99 /** 100 /**
diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp
index 7cf081aad..42809c731 100644
--- a/src/core/hw/gpu.cpp
+++ b/src/core/hw/gpu.cpp
@@ -8,17 +8,13 @@
8#include "common/color.h" 8#include "common/color.h"
9#include "common/common_types.h" 9#include "common/common_types.h"
10#include "common/logging/log.h" 10#include "common/logging/log.h"
11#include "common/math_util.h"
12#include "common/microprofile.h" 11#include "common/microprofile.h"
13#include "common/thread.h"
14#include "common/timer.h"
15#include "common/vector_math.h" 12#include "common/vector_math.h"
16#include "core/core_timing.h" 13#include "core/core_timing.h"
17#include "core/hle/service/gsp_gpu.h" 14#include "core/hle/service/gsp_gpu.h"
18#include "core/hw/gpu.h" 15#include "core/hw/gpu.h"
19#include "core/hw/hw.h" 16#include "core/hw/hw.h"
20#include "core/memory.h" 17#include "core/memory.h"
21#include "core/settings.h"
22#include "core/tracer/recorder.h" 18#include "core/tracer/recorder.h"
23#include "video_core/command_processor.h" 19#include "video_core/command_processor.h"
24#include "video_core/debug_utils/debug_utils.h" 20#include "video_core/debug_utils/debug_utils.h"
@@ -35,16 +31,6 @@ Regs g_regs;
35const u64 frame_ticks = BASE_CLOCK_RATE_ARM11 / SCREEN_REFRESH_RATE; 31const u64 frame_ticks = BASE_CLOCK_RATE_ARM11 / SCREEN_REFRESH_RATE;
36/// Event id for CoreTiming 32/// Event id for CoreTiming
37static int vblank_event; 33static int vblank_event;
38/// Total number of frames drawn
39static u64 frame_count;
40/// Start clock for frame limiter
41static u32 time_point;
42/// Total delay caused by slow frames
43static float time_delay;
44constexpr float FIXED_FRAME_TIME = 1000.0f / SCREEN_REFRESH_RATE;
45// Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher
46// values increases time needed to limit frame rate after spikes
47constexpr float MAX_LAG_TIME = 18;
48 34
49template <typename T> 35template <typename T>
50inline void Read(T& var, const u32 raw_addr) { 36inline void Read(T& var, const u32 raw_addr) {
@@ -522,24 +508,8 @@ template void Write<u32>(u32 addr, const u32 data);
522template void Write<u16>(u32 addr, const u16 data); 508template void Write<u16>(u32 addr, const u16 data);
523template void Write<u8>(u32 addr, const u8 data); 509template void Write<u8>(u32 addr, const u8 data);
524 510
525static void FrameLimiter() {
526 time_delay += FIXED_FRAME_TIME;
527 time_delay = MathUtil::Clamp(time_delay, -MAX_LAG_TIME, MAX_LAG_TIME);
528 s32 desired_time = static_cast<s32>(time_delay);
529 s32 elapsed_time = static_cast<s32>(Common::Timer::GetTimeMs() - time_point);
530
531 if (elapsed_time < desired_time) {
532 Common::SleepCurrentThread(desired_time - elapsed_time);
533 }
534
535 u32 frame_time = Common::Timer::GetTimeMs() - time_point;
536
537 time_delay -= frame_time;
538}
539
540/// Update hardware 511/// Update hardware
541static void VBlankCallback(u64 userdata, int cycles_late) { 512static void VBlankCallback(u64 userdata, int cycles_late) {
542 frame_count++;
543 VideoCore::g_renderer->SwapBuffers(); 513 VideoCore::g_renderer->SwapBuffers();
544 514
545 // Signal to GSP that GPU interrupt has occurred 515 // Signal to GSP that GPU interrupt has occurred
@@ -550,12 +520,6 @@ static void VBlankCallback(u64 userdata, int cycles_late) {
550 Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC0); 520 Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC0);
551 Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC1); 521 Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC1);
552 522
553 if (!Settings::values.use_vsync && Settings::values.toggle_framelimit) {
554 FrameLimiter();
555 }
556
557 time_point = Common::Timer::GetTimeMs();
558
559 // Reschedule recurrent event 523 // Reschedule recurrent event
560 CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event); 524 CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event);
561} 525}
@@ -590,9 +554,6 @@ void Init() {
590 framebuffer_sub.color_format.Assign(Regs::PixelFormat::RGB8); 554 framebuffer_sub.color_format.Assign(Regs::PixelFormat::RGB8);
591 framebuffer_sub.active_fb = 0; 555 framebuffer_sub.active_fb = 0;
592 556
593 frame_count = 0;
594 time_point = Common::Timer::GetTimeMs();
595
596 vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback); 557 vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback);
597 CoreTiming::ScheduleEvent(frame_ticks, vblank_event); 558 CoreTiming::ScheduleEvent(frame_ticks, vblank_event);
598 559
diff --git a/src/core/perf_stats.cpp b/src/core/perf_stats.cpp
index 06bc788bd..eb59a1332 100644
--- a/src/core/perf_stats.cpp
+++ b/src/core/perf_stats.cpp
@@ -4,11 +4,16 @@
4 4
5#include <chrono> 5#include <chrono>
6#include <mutex> 6#include <mutex>
7#include <thread>
8#include "common/math_util.h"
7#include "core/hw/gpu.h" 9#include "core/hw/gpu.h"
8#include "core/perf_stats.h" 10#include "core/perf_stats.h"
11#include "core/settings.h"
9 12
13using namespace std::chrono_literals;
10using DoubleSecs = std::chrono::duration<double, std::chrono::seconds::period>; 14using DoubleSecs = std::chrono::duration<double, std::chrono::seconds::period>;
11using std::chrono::duration_cast; 15using std::chrono::duration_cast;
16using std::chrono::microseconds;
12 17
13namespace Core { 18namespace Core {
14 19
@@ -69,4 +74,32 @@ double PerfStats::GetLastFrameTimeScale() {
69 return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH; 74 return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
70} 75}
71 76
77void FrameLimiter::DoFrameLimiting(u64 current_system_time_us) {
78 // Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher
79 // values increases time needed to limit frame rate after spikes.
80 constexpr microseconds MAX_LAG_TIME_US = 25ms;
81
82 if (!Settings::values.toggle_framelimit) {
83 return;
84 }
85
86 auto now = Clock::now();
87
88 frame_limiting_delta_err += microseconds(current_system_time_us - previous_system_time_us);
89 frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime);
90 frame_limiting_delta_err =
91 MathUtil::Clamp(frame_limiting_delta_err, -MAX_LAG_TIME_US, MAX_LAG_TIME_US);
92
93 if (frame_limiting_delta_err > microseconds::zero()) {
94 std::this_thread::sleep_for(frame_limiting_delta_err);
95
96 auto now_after_sleep = Clock::now();
97 frame_limiting_delta_err -= duration_cast<microseconds>(now_after_sleep - now);
98 now = now_after_sleep;
99 }
100
101 previous_system_time_us = current_system_time_us;
102 previous_walltime = now;
103}
104
72} // namespace Core 105} // namespace Core
diff --git a/src/core/perf_stats.h b/src/core/perf_stats.h
index 4098fc1f2..b03adab68 100644
--- a/src/core/perf_stats.h
+++ b/src/core/perf_stats.h
@@ -55,4 +55,20 @@ private:
55 u32 game_frames = 0; 55 u32 game_frames = 0;
56}; 56};
57 57
58class FrameLimiter {
59public:
60 using Clock = std::chrono::high_resolution_clock;
61
62 void DoFrameLimiting(u64 current_system_time_us);
63
64private:
65 /// Emulated system time (in microseconds) at the last limiter invocation
66 u64 previous_system_time_us = 0;
67 /// Walltime at the last limiter invocation
68 Clock::time_point previous_walltime = Clock::now();
69
70 /// Accumulated difference between walltime and emulated time
71 std::chrono::microseconds frame_limiting_delta_err{0};
72};
73
58} // namespace Core 74} // namespace Core
diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp
index b3604106c..e19375466 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.cpp
+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp
@@ -10,8 +10,8 @@
10#include "common/assert.h" 10#include "common/assert.h"
11#include "common/bit_field.h" 11#include "common/bit_field.h"
12#include "common/logging/log.h" 12#include "common/logging/log.h"
13#include "common/synchronized_wrapper.h"
14#include "core/core.h" 13#include "core/core.h"
14#include "core/core_timing.h"
15#include "core/frontend/emu_window.h" 15#include "core/frontend/emu_window.h"
16#include "core/hw/gpu.h" 16#include "core/hw/gpu.h"
17#include "core/hw/hw.h" 17#include "core/hw/hw.h"
@@ -151,10 +151,10 @@ void RendererOpenGL::SwapBuffers() {
151 render_window->PollEvents(); 151 render_window->PollEvents();
152 render_window->SwapBuffers(); 152 render_window->SwapBuffers();
153 153
154 prev_state.Apply(); 154 Core::System::GetInstance().frame_limiter.DoFrameLimiting(CoreTiming::GetGlobalTimeUs());
155
156 Core::System::GetInstance().perf_stats.BeginSystemFrame(); 155 Core::System::GetInstance().perf_stats.BeginSystemFrame();
157 156
157 prev_state.Apply();
158 RefreshRasterizerSetting(); 158 RefreshRasterizerSetting();
159 159
160 if (Pica::g_debug_context && Pica::g_debug_context->recorder) { 160 if (Pica::g_debug_context && Pica::g_debug_context->recorder) {