From 90012608ce0adf9ac8a7bcf06d8bda3d385ce6c8 Mon Sep 17 00:00:00 2001 From: Cancerous Date: Sat, 25 Jan 2020 13:11:00 -0500 Subject: [PATCH] [UI] add crude fps limiter for games that are too fast --- src/xenia/gpu/command_processor.cc | 17 ++++++++++++++++- src/xenia/gpu/command_processor.h | 2 ++ src/xenia/ui/window.cc | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/xenia/gpu/command_processor.cc b/src/xenia/gpu/command_processor.cc index 0122d4ed8..381e2b30c 100644 --- a/src/xenia/gpu/command_processor.cc +++ b/src/xenia/gpu/command_processor.cc @@ -26,6 +26,8 @@ #include "xenia/kernel/kernel_state.h" #include "xenia/kernel/user_module.h" +DECLARE_bool(fps_limit); + namespace xe { namespace gpu { @@ -806,7 +808,7 @@ bool CommandProcessor::ExecutePacketType3_XE_SWAP(RingBuffer* reader, uint32_t count) { SCOPE_profile_cpu_f("gpu"); - XELOGI("XE_SWAP"); + XELOGD("XE_SWAP"); Profiler::Flip(); @@ -828,6 +830,19 @@ bool CommandProcessor::ExecutePacketType3_XE_SWAP(RingBuffer* reader, } ++counter_; + + + // crude FPS limiter until one of you does a better implementation + if(cvars::fps_limit) { + + if (xe::Clock::QueryHostSystemTime() > swap_update_time_ns_ + 16666) { + swap_update_time_ns_ = xe::Clock::QueryHostSystemTime(); + } else { + xe::threading::MaybeYield(); + xe::threading::Sleep(std::chrono::nanoseconds((swap_update_time_ns_ + + 16666) - xe::Clock::QueryHostSystemTime())); + } + } return true; } diff --git a/src/xenia/gpu/command_processor.h b/src/xenia/gpu/command_processor.h index e613d5a03..c224e4504 100644 --- a/src/xenia/gpu/command_processor.h +++ b/src/xenia/gpu/command_processor.h @@ -260,6 +260,8 @@ class CommandProcessor { std::wstring trace_stream_path_; std::wstring trace_frame_path_; + uint64_t swap_update_time_ns_ = 0; + std::atomic worker_running_; kernel::object_ref worker_thread_; diff --git a/src/xenia/ui/window.cc b/src/xenia/ui/window.cc index 0894f5328..2af2bf944 100644 --- a/src/xenia/ui/window.cc +++ b/src/xenia/ui/window.cc @@ -18,9 +18,12 @@ #include "xenia/base/logging.h" #include "xenia/base/profiling.h" #include "xenia/ui/imgui_drawer.h" +#include "xenia/cpu/thread.h" DEFINE_bool(fps_titlebar, true, "Show FPS in titlebar", "General"); +DEFINE_bool(fps_limit, false, "try to limit 2d games from being too fast", "Video"); + namespace xe { namespace ui { @@ -178,6 +181,17 @@ void Window::OnPaint(UIEvent* e) { ++frame_count_; ++fps_frame_count_; uint64_t now_ns = xe::Clock::QueryHostSystemTime(); + + // crude FPS limiter until one of you does a better implementation + if(cvars::fps_limit) { + if (now_ns > fps_update_time_ns_ + 16666) { + // do nothing + } else { + xe::threading::Sleep(std::chrono::nanoseconds((fps_update_time_ns_ + + 16666) - now_ns)); + } + } + if (now_ns > fps_update_time_ns_ + 1000 * 10000) { fps_ = static_cast( fps_frame_count_ /