Performance sampler added

This commit is contained in:
Nigel Barink 2022-10-30 16:25:18 +01:00
parent f0984b6117
commit 65ae892951
5 changed files with 160 additions and 77 deletions

View File

@ -0,0 +1,70 @@
#include "src/PerfCounter.h"
#include <imgui.h>
#include <iostream>
EngineStatistics ES;
uint64_t EngineInstrumentation::GetPrecisionTime() {
using namespace std::chrono; // REMINDER: This is kinda ugly but safes line width
return duration_cast<milliseconds>(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<nanoseconds>(end.time_since_epoch()).count() -
duration_cast<nanoseconds>(startTime.time_since_epoch()).count();
auto ms = durationInuSeconds * 0.001f;
std::cout << "[" << name << "]" << "Took: " << durationInuSeconds << " us (" << ms << " ms)" << std::endl;
}

View File

@ -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;

View File

@ -20,5 +20,6 @@
extern BarinkEngine::Renderer renderer;
extern void Start();
extern void Update();
extern void Render();
extern void ImmediateGraphicsDraw();
extern void Stop();

View File

@ -1,52 +1,37 @@
#pragma once
#include <string>
#include <vector>
#include <chrono>
#include <imgui.h>
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::milliseconds>(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::milliseconds>(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();
}
PerfSampler(const std::string& name);
~PerfSampler();
void Stop();
private:
const std::string& name;
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
};

View File

@ -7,6 +7,7 @@
#include "GUI.h"
#include "Util.h"
#include <entt/entt.hpp>
#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<BarinkEngine::LightComponent>();
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<BarinkEngine::IdentifierComponent>();
@ -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<BarinkEngine::Render3DComponent>();
auto& b = cube.GetComponent<BarinkEngine::TransformComponent>();
@ -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()
{
}
}