mirror of
https://github.com/yuzu-emu/yuzu.git
synced 2024-11-15 08:40:06 +00:00
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.
This commit is contained in:
parent
b285c2a4ed
commit
fb1979d7e2
@ -94,6 +94,7 @@ public:
|
||||
}
|
||||
|
||||
PerfStats perf_stats;
|
||||
FrameLimiter frame_limiter;
|
||||
|
||||
private:
|
||||
/**
|
||||
|
@ -8,17 +8,13 @@
|
||||
#include "common/color.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/math_util.h"
|
||||
#include "common/microprofile.h"
|
||||
#include "common/thread.h"
|
||||
#include "common/timer.h"
|
||||
#include "common/vector_math.h"
|
||||
#include "core/core_timing.h"
|
||||
#include "core/hle/service/gsp_gpu.h"
|
||||
#include "core/hw/gpu.h"
|
||||
#include "core/hw/hw.h"
|
||||
#include "core/memory.h"
|
||||
#include "core/settings.h"
|
||||
#include "core/tracer/recorder.h"
|
||||
#include "video_core/command_processor.h"
|
||||
#include "video_core/debug_utils/debug_utils.h"
|
||||
@ -35,16 +31,6 @@ Regs g_regs;
|
||||
const u64 frame_ticks = BASE_CLOCK_RATE_ARM11 / SCREEN_REFRESH_RATE;
|
||||
/// Event id for CoreTiming
|
||||
static int vblank_event;
|
||||
/// Total number of frames drawn
|
||||
static u64 frame_count;
|
||||
/// Start clock for frame limiter
|
||||
static u32 time_point;
|
||||
/// Total delay caused by slow frames
|
||||
static float time_delay;
|
||||
constexpr float FIXED_FRAME_TIME = 1000.0f / SCREEN_REFRESH_RATE;
|
||||
// Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher
|
||||
// values increases time needed to limit frame rate after spikes
|
||||
constexpr float MAX_LAG_TIME = 18;
|
||||
|
||||
template <typename T>
|
||||
inline void Read(T& var, const u32 raw_addr) {
|
||||
@ -522,24 +508,8 @@ template void Write<u32>(u32 addr, const u32 data);
|
||||
template void Write<u16>(u32 addr, const u16 data);
|
||||
template void Write<u8>(u32 addr, const u8 data);
|
||||
|
||||
static void FrameLimiter() {
|
||||
time_delay += FIXED_FRAME_TIME;
|
||||
time_delay = MathUtil::Clamp(time_delay, -MAX_LAG_TIME, MAX_LAG_TIME);
|
||||
s32 desired_time = static_cast<s32>(time_delay);
|
||||
s32 elapsed_time = static_cast<s32>(Common::Timer::GetTimeMs() - time_point);
|
||||
|
||||
if (elapsed_time < desired_time) {
|
||||
Common::SleepCurrentThread(desired_time - elapsed_time);
|
||||
}
|
||||
|
||||
u32 frame_time = Common::Timer::GetTimeMs() - time_point;
|
||||
|
||||
time_delay -= frame_time;
|
||||
}
|
||||
|
||||
/// Update hardware
|
||||
static void VBlankCallback(u64 userdata, int cycles_late) {
|
||||
frame_count++;
|
||||
VideoCore::g_renderer->SwapBuffers();
|
||||
|
||||
// Signal to GSP that GPU interrupt has occurred
|
||||
@ -550,12 +520,6 @@ static void VBlankCallback(u64 userdata, int cycles_late) {
|
||||
Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC0);
|
||||
Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC1);
|
||||
|
||||
if (!Settings::values.use_vsync && Settings::values.toggle_framelimit) {
|
||||
FrameLimiter();
|
||||
}
|
||||
|
||||
time_point = Common::Timer::GetTimeMs();
|
||||
|
||||
// Reschedule recurrent event
|
||||
CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event);
|
||||
}
|
||||
@ -590,9 +554,6 @@ void Init() {
|
||||
framebuffer_sub.color_format.Assign(Regs::PixelFormat::RGB8);
|
||||
framebuffer_sub.active_fb = 0;
|
||||
|
||||
frame_count = 0;
|
||||
time_point = Common::Timer::GetTimeMs();
|
||||
|
||||
vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback);
|
||||
CoreTiming::ScheduleEvent(frame_ticks, vblank_event);
|
||||
|
||||
|
@ -4,11 +4,16 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include "common/math_util.h"
|
||||
#include "core/hw/gpu.h"
|
||||
#include "core/perf_stats.h"
|
||||
#include "core/settings.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using DoubleSecs = std::chrono::duration<double, std::chrono::seconds::period>;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::microseconds;
|
||||
|
||||
namespace Core {
|
||||
|
||||
@ -69,4 +74,32 @@ double PerfStats::GetLastFrameTimeScale() {
|
||||
return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
|
||||
}
|
||||
|
||||
void FrameLimiter::DoFrameLimiting(u64 current_system_time_us) {
|
||||
// Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher
|
||||
// values increases time needed to limit frame rate after spikes.
|
||||
constexpr microseconds MAX_LAG_TIME_US = 25ms;
|
||||
|
||||
if (!Settings::values.toggle_framelimit) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto now = Clock::now();
|
||||
|
||||
frame_limiting_delta_err += microseconds(current_system_time_us - previous_system_time_us);
|
||||
frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime);
|
||||
frame_limiting_delta_err =
|
||||
MathUtil::Clamp(frame_limiting_delta_err, -MAX_LAG_TIME_US, MAX_LAG_TIME_US);
|
||||
|
||||
if (frame_limiting_delta_err > microseconds::zero()) {
|
||||
std::this_thread::sleep_for(frame_limiting_delta_err);
|
||||
|
||||
auto now_after_sleep = Clock::now();
|
||||
frame_limiting_delta_err -= duration_cast<microseconds>(now_after_sleep - now);
|
||||
now = now_after_sleep;
|
||||
}
|
||||
|
||||
previous_system_time_us = current_system_time_us;
|
||||
previous_walltime = now;
|
||||
}
|
||||
|
||||
} // namespace Core
|
||||
|
@ -55,4 +55,20 @@ private:
|
||||
u32 game_frames = 0;
|
||||
};
|
||||
|
||||
class FrameLimiter {
|
||||
public:
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
|
||||
void DoFrameLimiting(u64 current_system_time_us);
|
||||
|
||||
private:
|
||||
/// Emulated system time (in microseconds) at the last limiter invocation
|
||||
u64 previous_system_time_us = 0;
|
||||
/// Walltime at the last limiter invocation
|
||||
Clock::time_point previous_walltime = Clock::now();
|
||||
|
||||
/// Accumulated difference between walltime and emulated time
|
||||
std::chrono::microseconds frame_limiting_delta_err{0};
|
||||
};
|
||||
|
||||
} // namespace Core
|
||||
|
@ -10,8 +10,8 @@
|
||||
#include "common/assert.h"
|
||||
#include "common/bit_field.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/synchronized_wrapper.h"
|
||||
#include "core/core.h"
|
||||
#include "core/core_timing.h"
|
||||
#include "core/frontend/emu_window.h"
|
||||
#include "core/hw/gpu.h"
|
||||
#include "core/hw/hw.h"
|
||||
@ -151,10 +151,10 @@ void RendererOpenGL::SwapBuffers() {
|
||||
render_window->PollEvents();
|
||||
render_window->SwapBuffers();
|
||||
|
||||
prev_state.Apply();
|
||||
|
||||
Core::System::GetInstance().frame_limiter.DoFrameLimiting(CoreTiming::GetGlobalTimeUs());
|
||||
Core::System::GetInstance().perf_stats.BeginSystemFrame();
|
||||
|
||||
prev_state.Apply();
|
||||
RefreshRasterizerSetting();
|
||||
|
||||
if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
|
||||
|
Loading…
Reference in New Issue
Block a user