Fixed perfomance counter

* Added vert-count and draw call count
* extracted performance function to seperate header as inline functions
This commit is contained in:
Nigel Barink 2022-05-29 21:31:21 +02:00
parent 02727c74bb
commit d9f0f40ad9
4 changed files with 65 additions and 57 deletions

View File

@ -1,36 +1,18 @@
#include "BarinkEngine.h" #include "BarinkEngine.h"
#include <imgui.h>
#include <chrono>
extern void Start();
extern void Update();
extern void ImmediateGraphicsDraw();
extern void Stop();
using namespace BarinkEngine;
struct EngineStatistics{
unsigned int lastSampleTime;
unsigned int frames;
float frameTime;
unsigned int FPS;
};
EngineStatistics* ES; EngineStatistics* ES;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
ES = new EngineStatistics(); // Setup performance sampler
ES->frames = 0; PerfomanceSamplerInit();
ES->lastSampleTime = 0;
// Startup services // Startup services
BarinkWindow MainWindow = BarinkWindow(800, 600); BarinkWindow MainWindow = BarinkWindow(800, 600);
Renderer renderer = Renderer(); BarinkEngine::Renderer renderer = BarinkEngine::Renderer();
InputManager InputSystem = InputManager(); BarinkEngine::InputManager InputSystem = BarinkEngine::InputManager();
InputSystem.attach(&MainWindow); InputSystem.attach(&MainWindow);
@ -39,27 +21,14 @@ int main(int argc, char* argv[]) {
// First call to setup game // First call to setup game
Start(); Start();
ES->lastSampleTime = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now().time_since_epoch()).count();
// Runtime loop // Runtime loop
while (!MainWindow.WindowShouldClose()) { while (!MainWindow.WindowShouldClose()) {
ES->frames++; SamplePerformance();
unsigned int now = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now().time_since_epoch()).count();
unsigned int MilliSecondsPast = ES->lastSampleTime - now;
if ( MilliSecondsPast >= 1000) {
ES->FPS = 1000 / ES->frames ;
ES->frameTime = ES->frames / 1000;
ES->frames = 0;
ES->lastSampleTime = now;
}
// Execute main logic
InputSystem.PollEvents(); InputSystem.PollEvents();
Update(); Update();
@ -86,13 +55,3 @@ int main(int argc, char* argv[]) {
return 0; return 0;
} }
void ShowStats() {
ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove );
ImGui::Text("Frames: %i", ES->frames);
ImGui::Text("FPS: %i", ES->FPS);
ImGui::Text("Frame Time: %f", ES->frameTime);
ImGui::End();
}

View File

@ -1,10 +1,5 @@
#pragma once #pragma once
#include <iostream>
#include <string>
#include <filesystem>
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "graphics/Shader.h" #include "graphics/Shader.h"
#include "graphics/Window.h" #include "graphics/Window.h"
#include "graphics/Camera.h" #include "graphics/Camera.h"
@ -15,7 +10,9 @@
#include "Graphics/Renderer.h" #include "Graphics/Renderer.h"
#include "Graphics/GUI/GUIManager.h" #include "Graphics/GUI/GUIManager.h"
#include "Scene.h" #include "Scene.h"
#include "PerfCounter.h"
extern void Start();
extern void Update();
void ShowStats(); extern void ImmediateGraphicsDraw();
extern void Stop();

View File

@ -0,0 +1,50 @@
#pragma once
#include <chrono>
#include <imgui.h>
struct EngineStatistics {
uint32_t lastSampleTime;
float frameTime;
uint32_t verts;
uint32_t DC;
uint64_t frames;
uint64_t FPS;
};
extern EngineStatistics* ES;
inline void PerfomanceSamplerInit(){
ES = new EngineStatistics();
ES->frames = 0;
ES->lastSampleTime = 0;
ES->lastSampleTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
inline void SamplePerformance(void) {
ES->frames++;
ES->DC = 0;
ES->verts = 0;
unsigned int now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
unsigned int MilliSecondsPast = now - ES->lastSampleTime;
if (MilliSecondsPast >= 1000) {
ES->frameTime = (float)1000 / ES->frames;
ES->FPS = ES->frames;
ES->frames = 0;
ES->lastSampleTime = now;
}
}
inline void ShowStats() {
ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
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();
}

View File

@ -1,6 +1,6 @@
#include "Graphics/Renderable.h" #include "Graphics/Renderable.h"
#include "AssetManager/ModelImporter.h" #include "AssetManager/ModelImporter.h"
#include "PerfCounter.h"
Renderable* Renderable::Load() Renderable* Renderable::Load()
@ -47,6 +47,8 @@ void Renderable::Draw()
{ {
VAO.Bind(); VAO.Bind();
elementBuffer.Bind(true); elementBuffer.Bind(true);
ES->verts = meshes[0].vertices.size();
ES->DC++;
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL); glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
VAO.Unbind(); VAO.Unbind();