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,26 +21,65 @@ int main(int argc, char* argv[]) {
glEnable(GL_DEPTH_TEST);
// First call to setup game
{
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
{
PerfSampler("PollEvents");
InputSystem.PollEvents();
}
{
PerfSampler("Update");
while (lag >= MS_PER_UPDATE) {
Update();
lag -= MS_PER_UPDATE;
}
}
{
PerfSampler("Render");
Render();
}
{
PerfSampler("GUI-Render");
GUISystem.Render();
}
{
PerfSampler("BufferSwap");
MainWindow.SwapBuffers();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
}
// Shutdown game
{
PerfSampler("Stop");
Stop();
}
// Shutdown Services

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;
}
static uint64_t GetPrecisionTime();
static void PerfomanceSamplerInit();
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) {
static void Update();
static void ShowStats();
ES.frameTime = (float)1000 / ES.frames;
ES.FPS = ES.frames;
ES.frames = 0;
ES.lastSampleTime = now;
}
}
};
class PerfSampler {
public:
inline void 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(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>();
@ -80,17 +74,11 @@ 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>();
@ -107,12 +95,8 @@ void ImmediateGraphicsDraw()
});
ImGui::End();
}
/*
@ -121,8 +105,11 @@ void ImmediateGraphicsDraw()
*/
void Update()
{
renderer.Render(scene);
}
void Render()
{
renderer.Render(scene);
}
/*