From 7fb4c45046917e68bbfe8c2561ca6ef26465c4af Mon Sep 17 00:00:00 2001 From: SirMangler Date: Sun, 3 Jan 2021 19:00:32 +0000 Subject: [PATCH] FPS Counter and debug overlay CVAR --- Editor/ViewManager.cpp | 3 +++ Runtime/Graphics/CGraphics.cpp | 17 +++++++++++++++++ Runtime/Graphics/CGraphics.hpp | 8 ++++++++ 3 files changed, 28 insertions(+) diff --git a/Editor/ViewManager.cpp b/Editor/ViewManager.cpp index 553b2a88e..fb2b6fc4b 100644 --- a/Editor/ViewManager.cpp +++ b/Editor/ViewManager.cpp @@ -66,6 +66,9 @@ void ViewManager::TestGameView::think() { if (m_cvarCommons.m_debugOverlayShowFrameCounter->toBoolean()) overlayText += fmt::format(FMT_STRING("Frame: {}\n"), g_StateManager->GetUpdateFrameIndex()); + if (m_cvarCommons.m_debugOverlayShowFramerate->toBoolean()) + overlayText += fmt::format(FMT_STRING("FPS: {}\n"), urde::CGraphics::GetFPS()); + if (m_cvarCommons.m_debugOverlayShowInGameTime->toBoolean()) { double igt = g_GameState->GetTotalPlayTime(); u32 ms = u64(igt * 1000) % 1000; diff --git a/Runtime/Graphics/CGraphics.cpp b/Runtime/Graphics/CGraphics.cpp index 48171d31c..18cdec78e 100644 --- a/Runtime/Graphics/CGraphics.cpp +++ b/Runtime/Graphics/CGraphics.cpp @@ -34,6 +34,9 @@ SViewport g_Viewport = { 0, 0, 640, 480, 640 / 2.f, 480 / 2.f, 0.0f, }; u32 CGraphics::g_FrameCounter = 0; +u32 CGraphics::g_Framerate = 0; +u32 CGraphics::g_FramesPast = 0; +frame_clock::time_point CGraphics::g_FrameStartTime = frame_clock::now(); const std::array CGraphics::skCubeBasisMats{{ /* Right */ @@ -126,6 +129,8 @@ void CGraphics::EndScene() { CLineRenderer::UpdateBuffers(); ++g_FrameCounter; + + UpdateFPSCounter(); } void CGraphics::SetAlphaCompare(ERglAlphaFunc comp0, u8 ref0, ERglAlphaOp op, ERglAlphaFunc comp1, u8 ref1) {} @@ -444,6 +449,18 @@ void CGraphics::TickRenderTimings() { g_DefaultSeconds = g_RenderTimings / 60.f; } +static constexpr u64 FPS_REFRESH_RATE = 1000; +void CGraphics::UpdateFPSCounter() { + ++g_FramesPast; + + std::chrono::duration timeElapsed = frame_clock::now() - g_FrameStartTime; + if (timeElapsed.count() > FPS_REFRESH_RATE) { + g_Framerate = g_FramesPast; + g_FrameStartTime = frame_clock::now(); + g_FramesPast = 0; + } +} + boo::IGraphicsDataFactory::Platform CGraphics::g_BooPlatform = boo::IGraphicsDataFactory::Platform::Null; boo::IGraphicsDataFactory* CGraphics::g_BooFactory = nullptr; boo::IGraphicsCommandQueue* CGraphics::g_BooMainCommandQueue = nullptr; diff --git a/Runtime/Graphics/CGraphics.hpp b/Runtime/Graphics/CGraphics.hpp index cf4eb982c..b85a51dce 100644 --- a/Runtime/Graphics/CGraphics.hpp +++ b/Runtime/Graphics/CGraphics.hpp @@ -2,6 +2,7 @@ #include #include +#include #include "Runtime/RetroTypes.hpp" @@ -18,6 +19,8 @@ #include #include +using frame_clock = std::chrono::high_resolution_clock; + namespace urde { extern hecl::CVar* g_disableLighting; class CLight; @@ -309,7 +312,12 @@ public: static float GetSecondsMod900(); static void TickRenderTimings(); static u32 g_FrameCounter; + static u32 g_Framerate; + static u32 g_FramesPast; + static frame_clock::time_point g_FrameStartTime; static u32 GetFrameCounter() { return g_FrameCounter; } + static u32 GetFPS() { return g_Framerate; } + static void UpdateFPSCounter(); static boo::IGraphicsDataFactory::Platform g_BooPlatform; static const boo::SystemChar* g_BooPlatformName;