Performance sampler added
This commit is contained in:
		
							
								
								
									
										70
									
								
								BarinkEngine/PerfCounter.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								BarinkEngine/PerfCounter.cpp
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,18 +1,19 @@
 | 
				
			|||||||
#include "BarinkEngine.h"
 | 
					#include "BarinkEngine.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
EngineStatistics ES;
 | 
					extern EngineStatistics ES;
 | 
				
			||||||
BarinkEngine::Renderer renderer;
 | 
					BarinkEngine::Renderer renderer;
 | 
				
			||||||
 | 
					const unsigned int MS_PER_UPDATE = 2;
 | 
				
			||||||
int main(int argc, char* argv[]) {
 | 
					int main(int argc, char* argv[]) {
 | 
				
			||||||
	// Setup performance sampler 
 | 
						// Setup performance sampler 
 | 
				
			||||||
	PerfomanceSamplerInit();
 | 
						EngineInstrumentation::PerfomanceSamplerInit();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Startup services 
 | 
						// Startup services 
 | 
				
			||||||
	BarinkWindow MainWindow =  BarinkWindow(800, 600);
 | 
						BarinkWindow MainWindow =  BarinkWindow(800, 600);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	renderer = BarinkEngine::Renderer();
 | 
						renderer = BarinkEngine::Renderer();
 | 
				
			||||||
	InputSystem = BarinkEngine::InputManager();
 | 
						InputSystem = BarinkEngine::InputManager();
 | 
				
			||||||
	ES = EngineStatistics();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ES = EngineStatistics{};
 | 
				
			||||||
	InputSystem.attach(&MainWindow);
 | 
						InputSystem.attach(&MainWindow);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	GUIManager GUISystem = GUIManager(&MainWindow);
 | 
						GUIManager GUISystem = GUIManager(&MainWindow);
 | 
				
			||||||
@ -20,27 +21,66 @@ int main(int argc, char* argv[]) {
 | 
				
			|||||||
	glEnable(GL_DEPTH_TEST);
 | 
						glEnable(GL_DEPTH_TEST);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// First call to setup game
 | 
						// First call to setup game
 | 
				
			||||||
	Start();
 | 
						{
 | 
				
			||||||
 | 
							PerfSampler("Start");
 | 
				
			||||||
 | 
							Start();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						double previous = glfwGetTime();
 | 
				
			||||||
 | 
						double lag = 0.0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Runtime loop
 | 
						// Runtime loop
 | 
				
			||||||
	while (!MainWindow.WindowShouldClose()) {
 | 
						while (!MainWindow.WindowShouldClose()) 
 | 
				
			||||||
		SamplePerformance();
 | 
						{
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
							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 
 | 
							// Execute main logic 
 | 
				
			||||||
		InputSystem.PollEvents();
 | 
							{
 | 
				
			||||||
		
 | 
								PerfSampler("PollEvents");
 | 
				
			||||||
		Update();
 | 
								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
 | 
						// Shutdown game
 | 
				
			||||||
	Stop();
 | 
						{
 | 
				
			||||||
 | 
							PerfSampler("Stop");
 | 
				
			||||||
 | 
							Stop();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	// Shutdown Services
 | 
						// Shutdown Services
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
 | 
				
			|||||||
@ -20,5 +20,6 @@
 | 
				
			|||||||
extern BarinkEngine::Renderer renderer;
 | 
					extern BarinkEngine::Renderer renderer;
 | 
				
			||||||
extern void Start();
 | 
					extern void Start();
 | 
				
			||||||
extern void Update();
 | 
					extern void Update();
 | 
				
			||||||
 | 
					extern void Render();
 | 
				
			||||||
extern void ImmediateGraphicsDraw();
 | 
					extern void ImmediateGraphicsDraw();
 | 
				
			||||||
extern void Stop();
 | 
					extern void Stop();
 | 
				
			||||||
 | 
				
			|||||||
@ -1,52 +1,37 @@
 | 
				
			|||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <vector>
 | 
				
			||||||
#include <chrono>
 | 
					#include <chrono>
 | 
				
			||||||
#include <imgui.h>
 | 
					struct EngineStatistics {
 | 
				
			||||||
 | 
					 | 
				
			||||||
 struct EngineStatistics {
 | 
					 | 
				
			||||||
	float frameTime;
 | 
						float frameTime;
 | 
				
			||||||
	uint32_t verts;
 | 
						uint32_t verts;
 | 
				
			||||||
	uint32_t DC;
 | 
						uint32_t DC;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	long long lastSampleTime;
 | 
						int64_t frames;
 | 
				
			||||||
	long long frames;
 | 
						int64_t  FPS;
 | 
				
			||||||
	long long  FPS;
 | 
					
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
extern EngineStatistics ES;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
inline void PerfomanceSamplerInit(){
 | 
					class EngineInstrumentation {
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ES.frames = 0;
 | 
						//static int64_t lastSampleTime;
 | 
				
			||||||
	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) {
 | 
						static uint64_t GetPrecisionTime();
 | 
				
			||||||
	ES.frames++; 
 | 
						static void PerfomanceSamplerInit();
 | 
				
			||||||
	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 void Update();
 | 
				
			||||||
 | 
						static void ShowStats();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
inline void ShowStats() {
 | 
					class PerfSampler {
 | 
				
			||||||
	ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize );
 | 
					public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ImGui::Text("FPS: %i",			ES.FPS);
 | 
						PerfSampler(const std::string& name);
 | 
				
			||||||
	ImGui::Text("Frame Time: %f",	ES.frameTime);
 | 
						~PerfSampler();
 | 
				
			||||||
	ImGui::Text("Verts: %i",			ES.verts);
 | 
						void Stop();
 | 
				
			||||||
	ImGui::Text("Draw Calls: %i",	ES.DC);
 | 
					private:
 | 
				
			||||||
 | 
						const std::string& name;
 | 
				
			||||||
 | 
						std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
 | 
				
			||||||
	ImGui::End();
 | 
					};
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -7,6 +7,7 @@
 | 
				
			|||||||
#include "GUI.h"
 | 
					#include "GUI.h"
 | 
				
			||||||
#include "Util.h"
 | 
					#include "Util.h"
 | 
				
			||||||
#include <entt/entt.hpp>
 | 
					#include <entt/entt.hpp>
 | 
				
			||||||
 | 
					#include "../../BarinkEngine/src/PerfCounter.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
* Define globals
 | 
					* Define globals
 | 
				
			||||||
@ -16,6 +17,7 @@ Scene scene;
 | 
				
			|||||||
BarinkEngine::Renderable* renderable;
 | 
					BarinkEngine::Renderable* renderable;
 | 
				
			||||||
BarinkEngine::SceneObject* object;
 | 
					BarinkEngine::SceneObject* object;
 | 
				
			||||||
Entity cube;
 | 
					Entity cube;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
* Runs once at startup 
 | 
					* Runs once at startup 
 | 
				
			||||||
* - USe to initialize the game/sandbox/demo
 | 
					* - USe to initialize the game/sandbox/demo
 | 
				
			||||||
@ -49,19 +51,11 @@ void Start() {
 | 
				
			|||||||
    auto AmbientLight = scene.AddEntity("AmbientLight");
 | 
					    auto AmbientLight = scene.AddEntity("AmbientLight");
 | 
				
			||||||
    AmbientLight.AddComponent<BarinkEngine::LightComponent>();
 | 
					    AmbientLight.AddComponent<BarinkEngine::LightComponent>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    renderer.Prepare(scene);
 | 
					    renderer.Prepare(scene);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
bool showImGuiMetrics = false;
 | 
					 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
* Runs every frame
 | 
					* Runs every frame
 | 
				
			||||||
* - Use to draw Immediate mode graphics (Not meant for HUD's )
 | 
					* - Use to draw Immediate mode graphics (Not meant for HUD's )
 | 
				
			||||||
@ -69,7 +63,7 @@ bool showImGuiMetrics = false;
 | 
				
			|||||||
void ImmediateGraphicsDraw() 
 | 
					void ImmediateGraphicsDraw() 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    // Show internal BarinkEngine stats
 | 
					    // Show internal BarinkEngine stats
 | 
				
			||||||
    ShowStats();
 | 
					    EngineInstrumentation::ShowStats();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ImGui::Begin("Scene view");
 | 
					    ImGui::Begin("Scene view");
 | 
				
			||||||
    auto group = scene.getReg().view<BarinkEngine::IdentifierComponent>();
 | 
					    auto group = scene.getReg().view<BarinkEngine::IdentifierComponent>();
 | 
				
			||||||
@ -79,18 +73,12 @@ void ImmediateGraphicsDraw()
 | 
				
			|||||||
        
 | 
					        
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    ImGui::End();
 | 
					    ImGui::End();
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    
 | 
					    ImGui::ShowMetricsWindow();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ImGui::Begin("Settings");
 | 
					    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& a = cube.GetComponent<BarinkEngine::Render3DComponent>();
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        auto& b = cube.GetComponent<BarinkEngine::TransformComponent>();
 | 
					        auto& b = cube.GetComponent<BarinkEngine::TransformComponent>();
 | 
				
			||||||
@ -106,13 +94,9 @@ void ImmediateGraphicsDraw()
 | 
				
			|||||||
            ImGui::SliderFloat3("l-Color", &light.Color[0], 0.0f, 1.0f);
 | 
					            ImGui::SliderFloat3("l-Color", &light.Color[0], 0.0f, 1.0f);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
       
 | 
					    
 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImGui::End();
 | 
					    ImGui::End();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
   
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
@ -120,9 +104,12 @@ void ImmediateGraphicsDraw()
 | 
				
			|||||||
* - Meant for game logic ( non-physics related) 
 | 
					* - Meant for game logic ( non-physics related) 
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
void Update()
 | 
					void Update()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Render() 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    renderer.Render(scene);
 | 
					    renderer.Render(scene);
 | 
				
			||||||
   
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
@ -131,4 +118,4 @@ void Update()
 | 
				
			|||||||
*/
 | 
					*/
 | 
				
			||||||
void Stop()
 | 
					void Stop()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user