From 30660b1f451eca8a0aa45310d6bb4e1eef932b03 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Thu, 25 Jan 2024 05:30:28 -0500 Subject: [PATCH] Add an average framerate for the previous 120 frames --- src/engine/Average.hpp | 40 ++++++++++++++++++++++++++++++++++++ src/engine/CMakeLists.txt | 8 ++++---- src/engine/EngineContext.cpp | 21 +++++++++++++++---- 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/engine/Average.hpp diff --git a/src/engine/Average.hpp b/src/engine/Average.hpp new file mode 100644 index 0000000..1e2c85d --- /dev/null +++ b/src/engine/Average.hpp @@ -0,0 +1,40 @@ +// +// Created by kj16609 on 1/25/24. +// + +#pragma once + +#include +#include + +template < typename T, std::uint64_t max_count = 100 > + requires std::is_integral_v< T > || std::is_floating_point_v< T > +class Average +{ + std::array< T, max_count > data {}; + + public: + + std::array< T, max_count >& getData() { return data; } + + consteval std::uint64_t count() const { return max_count; } + + void push( const T t ) + { + std::array< T, max_count > shift_array {}; + for ( std::uint64_t i = 1; i < max_count; ++i ) + { + shift_array[ i - 1 ] = data[ i ]; + } + + shift_array[ max_count - 1 ] = t; + data = shift_array; + } + + T average() const + { + const T accum { std::accumulate( data.begin(), data.end(), T() ) }; + + return accum / static_cast< T >( data.size() ); + } +}; diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index b926c13..8c4ffb9 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -26,8 +26,8 @@ target_include_directories(FGLEngine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..) set_target_properties(FGLEngine PROPERTIES COMPILE_FLAGS ${FGL_FLAGS}) string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_UPPER_BUILD_TYPE) -#if (CMAKE_UPPER_BUILD_TYPE STREQUAL "DEBUG") -# target_compile_definitions(FGLEngine PRIVATE ENABLE_IMGUI=1) -#else () +if (CMAKE_UPPER_BUILD_TYPE STREQUAL "DEBUG") + target_compile_definitions(FGLEngine PRIVATE ENABLE_IMGUI=1) +else () target_compile_definitions(FGLEngine PRIVATE ENABLE_IMGUI=0) -#endif () +endif () diff --git a/src/engine/EngineContext.cpp b/src/engine/EngineContext.cpp index 8f275c9..d30fab6 100644 --- a/src/engine/EngineContext.cpp +++ b/src/engine/EngineContext.cpp @@ -15,6 +15,7 @@ #include #include "KeyboardMovementController.hpp" +#include "engine/Average.hpp" #include "engine/buffers/UniqueFrameSuballocation.hpp" #include "engine/descriptors/Descriptor.hpp" #include "engine/descriptors/DescriptorPool.hpp" @@ -46,6 +47,8 @@ namespace fgl::engine loadGameObjects(); } + static Average< float, 120 > rolling_ms_average; + void EngineContext::run() { using namespace fgl::literals::size_literals; @@ -99,15 +102,28 @@ namespace fgl::engine auto current_time { std::chrono::high_resolution_clock::now() }; + auto previous_frame_start { std::chrono::high_resolution_clock::now() }; + while ( !m_window.shouldClose() ) { ZoneScopedN( "Poll" ); glfwPollEvents(); const auto new_time { std::chrono::high_resolution_clock::now() }; + + { + //Calculate time change from previous frame and add to accumulator + const auto time_diff { new_time - previous_frame_start }; + rolling_ms_average.push( + static_cast< float >( std::chrono::duration_cast< std::chrono::microseconds >( time_diff ).count() ) + / 1000.0f ); + previous_frame_start = new_time; + } + auto delta_time { std::chrono::duration< float, std::chrono::seconds::period >( new_time - current_time ).count() }; + current_time = new_time; delta_time = glm::min( delta_time, MAX_DELTA_TIME ); @@ -163,10 +179,7 @@ namespace fgl::engine ImGui::Text( "Frame Time" ); ImGui::SameLine(); ImGui::Text( "%.3f ms", 1000.0f / ImGui::GetIO().Framerate ); - - ImGui::Text( "Frame: " ); - ImGui::SameLine(); - ImGui::Text( "%i", frame_info.frame_idx ); + ImGui::Text( "Average rolling frametime: %.3f ms", rolling_ms_average.average() ); if ( ImGui::CollapsingHeader( "Camera" ) ) {