From 65ae8929511486ccc8963a23f2820bcf3eb88d67 Mon Sep 17 00:00:00 2001 From: nigelbarink Date: Sun, 30 Oct 2022 16:25:18 +0100 Subject: [PATCH] Performance sampler added --- BarinkEngine/PerfCounter.cpp | 70 +++++++++++++++++++++++++++++++ BarinkEngine/src/BarinkEngine.cpp | 68 +++++++++++++++++++++++------- BarinkEngine/src/BarinkEngine.h | 1 + BarinkEngine/src/PerfCounter.h | 61 ++++++++++----------------- SandboxApp/src/Sandbox.cpp | 37 ++++++---------- 5 files changed, 160 insertions(+), 77 deletions(-) create mode 100644 BarinkEngine/PerfCounter.cpp diff --git a/BarinkEngine/PerfCounter.cpp b/BarinkEngine/PerfCounter.cpp new file mode 100644 index 0000000..8774e35 --- /dev/null +++ b/BarinkEngine/PerfCounter.cpp @@ -0,0 +1,70 @@ +#include "src/PerfCounter.h" +#include +#include + +EngineStatistics ES; + +uint64_t EngineInstrumentation::GetPrecisionTime() { + using namespace std::chrono; // REMINDER: This is kinda ugly but safes line width + return duration_cast(high_resolution_clock::now().time_since_epoch()).count(); +} + +void EngineInstrumentation::PerfomanceSamplerInit() { + ES.frames = 0; + //EngineInstrumentation::lastSampleTime = GetPrecisionTime(); +} + + + +void EngineInstrumentation::Update() { +/* +uint64_t MilliSecondsPast = GetPrecisionTime() - EngineInstrumentation::lastSampleTime; + + if (MilliSecondsPast >= 1000) { + + ES.frameTime = (float)1000 / ES.frames; + ES.FPS = ES.frames; + ES.frames = 0; + //EngineInstrumentation::lastSampleTime = GetPrecisionTime(); + } +*/ + +} + +void EngineInstrumentation::ShowStats() { + ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize); + + ImGui::Text("FPS: %i", ES.FPS); + ImGui::Text("Frame Time: %f", ES.frameTime); + ImGui::Text("Verts: %i", ES.verts); + ImGui::Text("Draw Calls: %i", ES.DC); + + ImGui::End(); +} + + +PerfSampler::PerfSampler(const std::string& name ) +: name(name) +{ + using namespace std::chrono; + startTime = high_resolution_clock::now(); + +} + +PerfSampler::~PerfSampler() +{ + Stop(); +} + +void PerfSampler::Stop() +{ + using namespace std::chrono; + auto end = high_resolution_clock::now(); + auto durationInuSeconds = + duration_cast(end.time_since_epoch()).count() - + duration_cast(startTime.time_since_epoch()).count(); + + auto ms = durationInuSeconds * 0.001f; + + std::cout << "[" << name << "]" << "Took: " << durationInuSeconds << " us (" << ms << " ms)" << std::endl; +} \ No newline at end of file diff --git a/BarinkEngine/src/BarinkEngine.cpp b/BarinkEngine/src/BarinkEngine.cpp index 70ae3b5..59e2036 100644 --- a/BarinkEngine/src/BarinkEngine.cpp +++ b/BarinkEngine/src/BarinkEngine.cpp @@ -1,18 +1,19 @@ #include "BarinkEngine.h" -EngineStatistics ES; +extern EngineStatistics ES; BarinkEngine::Renderer renderer; +const unsigned int MS_PER_UPDATE = 2; int main(int argc, char* argv[]) { // Setup performance sampler - PerfomanceSamplerInit(); + EngineInstrumentation::PerfomanceSamplerInit(); // Startup services BarinkWindow MainWindow = BarinkWindow(800, 600); renderer = BarinkEngine::Renderer(); InputSystem = BarinkEngine::InputManager(); - ES = EngineStatistics(); + ES = EngineStatistics{}; InputSystem.attach(&MainWindow); GUIManager GUISystem = GUIManager(&MainWindow); @@ -20,27 +21,66 @@ int main(int argc, char* argv[]) { glEnable(GL_DEPTH_TEST); // First call to setup game - Start(); + { + PerfSampler("Start"); + Start(); + } + + + double previous = glfwGetTime(); + double lag = 0.0; // Runtime loop - while (!MainWindow.WindowShouldClose()) { - SamplePerformance(); + while (!MainWindow.WindowShouldClose()) + { + + double current = glfwGetTime(); + double elapsed = current - previous; + previous = current; + lag += elapsed; + + //EngineInstrumentation::Update(); // Todo this does nothing right now and is therefor disabled // Execute main logic - InputSystem.PollEvents(); - - Update(); + { + PerfSampler("PollEvents"); + InputSystem.PollEvents(); - GUISystem.Render(); + } - MainWindow.SwapBuffers(); + { + PerfSampler("Update"); + while (lag >= MS_PER_UPDATE) { + Update(); + lag -= MS_PER_UPDATE; + } + } - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + { + PerfSampler("Render"); + Render(); + } + + { + PerfSampler("GUI-Render"); + GUISystem.Render(); + } + + + { + PerfSampler("BufferSwap"); + MainWindow.SwapBuffers(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + } // Shutdown game - Stop(); - + { + PerfSampler("Stop"); + Stop(); + } + // Shutdown Services return 0; diff --git a/BarinkEngine/src/BarinkEngine.h b/BarinkEngine/src/BarinkEngine.h index 63f190b..913bb8a 100644 --- a/BarinkEngine/src/BarinkEngine.h +++ b/BarinkEngine/src/BarinkEngine.h @@ -20,5 +20,6 @@ extern BarinkEngine::Renderer renderer; extern void Start(); extern void Update(); +extern void Render(); extern void ImmediateGraphicsDraw(); extern void Stop(); diff --git a/BarinkEngine/src/PerfCounter.h b/BarinkEngine/src/PerfCounter.h index 40d670b..bc202e3 100644 --- a/BarinkEngine/src/PerfCounter.h +++ b/BarinkEngine/src/PerfCounter.h @@ -1,52 +1,37 @@ #pragma once +#include +#include #include -#include - - struct EngineStatistics { +struct EngineStatistics { float frameTime; uint32_t verts; uint32_t DC; - long long lastSampleTime; - long long frames; - long long FPS; + int64_t frames; + int64_t FPS; + }; -extern EngineStatistics ES; -inline void PerfomanceSamplerInit(){ +class EngineInstrumentation { +public: - ES.frames = 0; - ES.lastSampleTime = 0; - ES.lastSampleTime = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); - -} + //static int64_t lastSampleTime; -inline void SamplePerformance(void) { - ES.frames++; - ES.DC = 0; - ES.verts = 0; - unsigned long long now = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); - unsigned long long MilliSecondsPast = now - ES.lastSampleTime; - if (MilliSecondsPast >= 1000) { - - ES.frameTime = (float)1000 / ES.frames; - ES.FPS = ES.frames; - ES.frames = 0; - ES.lastSampleTime = now; - } -} + static uint64_t GetPrecisionTime(); + static void PerfomanceSamplerInit(); + static void Update(); + static void ShowStats(); +}; -inline void ShowStats() { - ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize ); +class PerfSampler { +public: - ImGui::Text("FPS: %i", ES.FPS); - ImGui::Text("Frame Time: %f", ES.frameTime); - ImGui::Text("Verts: %i", ES.verts); - ImGui::Text("Draw Calls: %i", ES.DC); - - - ImGui::End(); - -} \ No newline at end of file + PerfSampler(const std::string& name); + ~PerfSampler(); + void Stop(); +private: + const std::string& name; + std::chrono::time_point startTime; +}; \ No newline at end of file diff --git a/SandboxApp/src/Sandbox.cpp b/SandboxApp/src/Sandbox.cpp index 0c3f627..5b7bba2 100644 --- a/SandboxApp/src/Sandbox.cpp +++ b/SandboxApp/src/Sandbox.cpp @@ -7,6 +7,7 @@ #include "GUI.h" #include "Util.h" #include +#include "../../BarinkEngine/src/PerfCounter.h" /* * Define globals @@ -16,6 +17,7 @@ Scene scene; BarinkEngine::Renderable* renderable; BarinkEngine::SceneObject* object; Entity cube; + /* * Runs once at startup * - USe to initialize the game/sandbox/demo @@ -49,19 +51,11 @@ void Start() { auto AmbientLight = scene.AddEntity("AmbientLight"); AmbientLight.AddComponent(); - - renderer.Prepare(scene); - - } - - - -bool showImGuiMetrics = false; /* * Runs every frame * - Use to draw Immediate mode graphics (Not meant for HUD's ) @@ -69,7 +63,7 @@ bool showImGuiMetrics = false; void ImmediateGraphicsDraw() { // Show internal BarinkEngine stats - ShowStats(); + EngineInstrumentation::ShowStats(); ImGui::Begin("Scene view"); auto group = scene.getReg().view(); @@ -79,18 +73,12 @@ void ImmediateGraphicsDraw() }); ImGui::End(); - - + + ImGui::ShowMetricsWindow(); + ImGui::Begin("Settings"); - if (ImGui::Button("ImGui Debug")) - { - std::cout << "Click!" << std::endl; - showImGuiMetrics = true; - } - ImGui::ShowMetricsWindow(&showImGuiMetrics); - auto& a = cube.GetComponent(); auto& b = cube.GetComponent(); @@ -106,13 +94,9 @@ void ImmediateGraphicsDraw() ImGui::SliderFloat3("l-Color", &light.Color[0], 0.0f, 1.0f); }); - - + ImGui::End(); - - - } /* @@ -120,9 +104,12 @@ void ImmediateGraphicsDraw() * - Meant for game logic ( non-physics related) */ void Update() +{ +} + +void Render() { renderer.Render(scene); - } /* @@ -131,4 +118,4 @@ void Update() */ void Stop() { -} \ No newline at end of file +}