Skip to content

Commit

Permalink
Timing Profiler (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
fszewczyk authored Jan 25, 2025
1 parent 85fc5b6 commit d5ec9db
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(
STATIC

${CMAKE_CURRENT_LIST_DIR}/Logger.cpp
${CMAKE_CURRENT_LIST_DIR}/Profiler.cpp
)

target_include_directories(
Expand Down
50 changes: 50 additions & 0 deletions src/Common/Profiler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <Common/Profiler.hpp>

namespace shkyera {

ProfileBlock& ProfileBlock::operator+=(const ProfileBlock& other)
{
totalLengthInNanoSeconds += other.totalLengthInNanoSeconds;
numberOfCalls += other.numberOfCalls;
return *this;
}

ProfileGuard::ProfileGuard(std::string&& name) : mProfileName(std::move(name)), mStartTime(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch())) {}

ProfileGuard::~ProfileGuard()
{
const auto endTime = std::chrono::high_resolution_clock::now();
const auto guardDuration = endTime - mStartTime;
const auto guardDurationInNanoSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(guardDuration);
Profiler::getInstance().addBlock(std::move(mProfileName), guardDurationInNanoSeconds);
}

Profiler& Profiler::getInstance()
{
static Profiler p;
return p;
}

void Profiler::clear()
{
std::unique_lock lock(mMutex);
mProfileBlocks.clear();
}

void Profiler::addBlock(std::string&& name, std::chrono::nanoseconds time)
{
const auto threadId = std::this_thread::get_id();
ProfileBlock newBlock { .totalLengthInNanoSeconds = static_cast<double>(time.count()), .numberOfCalls = 1 };

std::unique_lock lock(mMutex);
mProfileBlocks[threadId][name] += newBlock;
}

Profiler::BlocksPerThread Profiler::getProfiles()
{
std::unique_lock lock(mMutex);
return mProfileBlocks;
}


}
52 changes: 52 additions & 0 deletions src/Common/Profiler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <chrono>
#include <thread>
#include <mutex>
#include <map>
#include <string>

namespace shkyera {

struct ProfileBlock {
double totalLengthInNanoSeconds = 0;
size_t numberOfCalls = 0;

ProfileBlock& operator+=(const ProfileBlock& other);
};

class ProfileGuard {
public:
ProfileGuard(std::string&& name);
~ProfileGuard();

private:
std::string mProfileName;
std::chrono::high_resolution_clock::time_point mStartTime;
};

class Profiler {
public:
using BlocksPerThread = std::map<std::thread::id, std::map<std::string, ProfileBlock>>;
static Profiler& getInstance();

void clear();
void addBlock(std::string&& name, std::chrono::nanoseconds time);
BlocksPerThread getProfiles();

private:
Profiler() = default;

std::mutex mMutex;
BlocksPerThread mProfileBlocks;
};

#define SHKYERA_PROFILE(name) ProfileGuard __SHKYERA_UNIQUE_NAME(__LINE__) (name)
#define SHKYERA_READ_PROFILE Profiler::getInstance().getProfiles()
#define SHKYERA_CLEAR_PROFILE Profiler::getInstance().clear()

#define __SHKYERA_UNIQUE_NAME(LINE) __SHKYERA_CONCAT(profileGuard_, LINE)
#define __SHKYERA_CONCAT(X, Y) __SHKYERA_CONCAT_IMPL(X, Y)
#define __SHKYERA_CONCAT_IMPL(X, Y) X##Y

}
3 changes: 3 additions & 0 deletions src/InputManager/InputManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <InputManager/InputManager.hpp>
#include <Common/Profiler.hpp>

namespace shkyera {

Expand Down Expand Up @@ -72,6 +73,8 @@ void InputManager::unregisterMouseButtonDownCallback(MouseButton button) {
}

void InputManager::processInput(GLFWwindow* window) {
SHKYERA_PROFILE("InputManager::processInput");

for (const auto& [key, callbacks] : _keyCallbacks) {
if (glfwGetKey(window, key) == GLFW_PRESS) {
for (const auto& callback : callbacks) {
Expand Down
3 changes: 3 additions & 0 deletions src/Systems/GizmoSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Systems/GizmoSystem.hpp>
#include <Common/Logger.hpp>
#include <Common/Profiler.hpp>

#include <AssetManager/AssetManager.hpp>
#include <AssetManager/Mesh.hpp>
Expand Down Expand Up @@ -178,6 +179,8 @@ GizmoSystem::~GizmoSystem()

void GizmoSystem::update()
{
SHKYERA_PROFILE("GizmoSystem::update");

selectEntity();
styleOnHover();

Expand Down
31 changes: 30 additions & 1 deletion src/Systems/RenderingSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Systems/RenderingSystem.hpp>
#include <Common/Logger.hpp>
#include <Common/Profiler.hpp>
#include <AssetManager/AssetManager.hpp>
#include <Rendering/Utils.hpp>
#include <Components/TransformComponent.hpp>
Expand Down Expand Up @@ -103,6 +104,8 @@ RenderingSystem::RenderingSystem(std::shared_ptr<Registry> registry)

void RenderingSystem::setSize(uint32_t width, uint32_t height)
{
SHKYERA_PROFILE("RenderingSystem::setSize");

_litModelsFrameBuffer.setSize(width, height);
_toneMappedFrameBuffer.setSize(width, height);
_silhouetteFrameBuffer.setSize(width, height);
Expand Down Expand Up @@ -130,6 +133,8 @@ GLuint RenderingSystem::getRenderFrameBuffer()

void RenderingSystem::clearFrameBuffers()
{
SHKYERA_PROFILE("RenderingSystem::clearFrameBuffers");

_litModelsFrameBuffer.clear();
_toneMappedFrameBuffer.clear();
_bloomedFrameBuffer.clear();
Expand All @@ -155,6 +160,8 @@ void RenderingSystem::clearFrameBuffers()

void RenderingSystem::render()
{
SHKYERA_PROFILE("RenderingSystem::render");

_mostRecentFrameBufferPtr = &_litModelsFrameBuffer;

// Rendering Preparation
Expand All @@ -178,11 +185,13 @@ void RenderingSystem::render()

void RenderingSystem::renderOutline(const std::unordered_set<Entity>& entities)
{
if(entities.empty())
if(std::none_of(entities.begin(), entities.end(), [this](auto e) { return _registry->hasComponent<NameComponent>(e); }))
{
return;
}

SHKYERA_PROFILE("RenderingSystem::renderOutline");

for(const auto& entity : entities)
{
const auto& children = _registry->getHierarchy().getChildren(entity);
Expand Down Expand Up @@ -255,6 +264,8 @@ void RenderingSystem::renderOutline(const std::unordered_set<Entity>& entities)

void RenderingSystem::renderDirectionalLightShadowMaps()
{
SHKYERA_PROFILE("RenderingSystem::renderDirectionalLightShadowMaps");

glEnable(GL_DEPTH_TEST);

const auto& cameraTransform = _registry->getComponent<TransformComponent>(_registry->getCamera());
Expand Down Expand Up @@ -321,6 +332,8 @@ void RenderingSystem::renderDirectionalLightShadowMaps()

void RenderingSystem::renderPointLightShadowMaps()
{
SHKYERA_PROFILE("RenderingSystem::renderPointLightShadowMaps");

glEnable(GL_DEPTH_TEST);

const auto& cameraTransform = _registry->getComponent<TransformComponent>(_registry->getCamera());
Expand Down Expand Up @@ -400,6 +413,8 @@ void RenderingSystem::renderPointLightShadowMaps()

void RenderingSystem::renderSpotLightShadowMaps()
{
SHKYERA_PROFILE("RenderingSystem::renderSpotLightShadowMaps");

glEnable(GL_DEPTH_TEST);

const auto& cameraTransform = _registry->getComponent<TransformComponent>(_registry->getCamera());
Expand Down Expand Up @@ -463,6 +478,8 @@ void RenderingSystem::renderSpotLightShadowMaps()

void RenderingSystem::renderModels()
{
SHKYERA_PROFILE("RenderingSystem::renderModels");

glEnable(GL_DEPTH_TEST);

// ********* Rendering the shadow maps *********
Expand Down Expand Up @@ -579,6 +596,8 @@ void RenderingSystem::renderModels()

void RenderingSystem::renderBloom()
{
SHKYERA_PROFILE("RenderingSystem::renderBloom");

glDisable(GL_DEPTH_TEST);

// Downscaling Pass
Expand Down Expand Up @@ -669,6 +688,8 @@ void RenderingSystem::renderBloom()

void RenderingSystem::toneMapping()
{
SHKYERA_PROFILE("RenderingSystem::toneMapping");

glDisable(GL_DEPTH_TEST);

utils::applyShaderToFrameBuffer(
Expand All @@ -684,6 +705,8 @@ void RenderingSystem::toneMapping()

void RenderingSystem::renderWireframes()
{
SHKYERA_PROFILE("RenderingSystem::renderWireframes");

glEnable(GL_DEPTH_TEST);

_mostRecentFrameBufferPtr->bind();
Expand Down Expand Up @@ -711,6 +734,8 @@ void RenderingSystem::renderWireframes()

void RenderingSystem::renderSkybox()
{
SHKYERA_PROFILE("RenderingSystem::renderSkybox");

const auto& cameraTransform = _registry->getComponent<TransformComponent>(_registry->getCamera());
const glm::mat4& viewMatrix = _registry->getComponent<CameraComponent>(_registry->getCamera()).getViewMatrix(cameraTransform);
const glm::mat4& projectionMatrix = _registry->getComponent<CameraComponent>(_registry->getCamera()).getProjectionMatrix();
Expand Down Expand Up @@ -739,6 +764,8 @@ void RenderingSystem::renderSkybox()

void RenderingSystem::renderOverlayModels()
{
SHKYERA_PROFILE("RenderingSystem::renderOverlayModels");

glDisable(GL_DEPTH_TEST);

_mostRecentFrameBufferPtr->bind();
Expand Down Expand Up @@ -766,6 +793,8 @@ void RenderingSystem::renderOverlayModels()

void RenderingSystem::antiAliasing()
{
SHKYERA_PROFILE("RenderingSystem::antiAliasing");

utils::applyShaderToFrameBuffer(
_antiAliasedFrameBuffer,
_antiAliasingShaderProgram,
Expand Down
2 changes: 1 addition & 1 deletion src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ add_library(
${CMAKE_CURRENT_LIST_DIR}/Components/AmbientLightComponentUI.cpp
${CMAKE_CURRENT_LIST_DIR}/Components/WireframeComponentUI.cpp

${CMAKE_CURRENT_LIST_DIR}/Widgets/PreviewWidget.cpp
${CMAKE_CURRENT_LIST_DIR}/Widgets/ObjectsWidget.cpp
${CMAKE_CURRENT_LIST_DIR}/Widgets/ConsoleWidget.cpp
${CMAKE_CURRENT_LIST_DIR}/Widgets/PropertiesWidget.cpp
${CMAKE_CURRENT_LIST_DIR}/Widgets/ProfilerWidget.cpp
${CMAKE_CURRENT_LIST_DIR}/Widgets/FilesystemWidget.cpp
${CMAKE_CURRENT_LIST_DIR}/Widgets/SceneWidget.cpp
)
Expand Down
13 changes: 10 additions & 3 deletions src/ui/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <UI/Common/Style.hpp>
#include <UI/Widgets/ConsoleWidget.hpp>
#include <UI/Widgets/FilesystemWidget.hpp>
#include <UI/Widgets/ProfilerWidget.hpp>
#include <UI/Widgets/ObjectsWidget.hpp>
#include <UI/Widgets/PreviewWidget.hpp>
#include <UI/Widgets/PropertiesWidget.hpp>
#include <UI/Widgets/SceneWidget.hpp>
#include <UI/UI.hpp>
Expand Down Expand Up @@ -93,11 +93,11 @@ void UI::initializeSystems() {
}

void UI::initializeWidgets() {
_widgets.emplace_back(std::make_unique<ConsoleWidget>("Console"));

_widgets.emplace_back(std::make_unique<ConsoleWidget>("Console"));
_widgets.emplace_back(std::make_unique<PropertiesWidget>(_registry));
_widgets.emplace_back(std::make_unique<CameraPropertiesWidget>(_registry));
_widgets.emplace_back(std::make_unique<EnvironmentPropertiesWidget>(_registry));
_widgets.emplace_back(std::make_unique<ProfilerWidget>("Profiler"));

_widgets.emplace_back(std::make_unique<SceneWidget>(_registry));

Expand Down Expand Up @@ -187,6 +187,8 @@ void UI::styleImgui() {
}

void UI::beginFrame() {
SHKYERA_PROFILE("UI::beginFrame");

glfwPollEvents();

glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
Expand Down Expand Up @@ -241,6 +243,7 @@ void UI::beginFrame() {
ImGui::DockBuilderDockWindow("Scene", dock_id_left_up_right);
ImGui::DockBuilderDockWindow("Properties", dock_id_right);
ImGui::DockBuilderDockWindow("Scene Camera", dock_id_right);
ImGui::DockBuilderDockWindow("Profiler", dock_id_right);
ImGui::DockBuilderDockWindow("Environment", dock_id_right);
ImGui::DockBuilderDockWindow("Assets", dock_id_left_bottom);
ImGui::DockBuilderDockWindow("Console", dock_id_left_bottom);
Expand All @@ -252,6 +255,8 @@ void UI::beginFrame() {
}

void UI::renderFrame() {
SHKYERA_PROFILE("UI::renderFrame");

const auto& windowSize = ImGui::GetWindowSize();
InputManager::getInstance().setCoordinateSystem(InputManager::CoordinateSystem::ABSOLUTE, {0, 0}, {windowSize.x, windowSize.y});
InputManager::getInstance().processInput(_window);
Expand Down Expand Up @@ -284,6 +289,8 @@ void UI::renderFrame() {
}

void UI::endFrame() {
SHKYERA_PROFILE("UI::endFrame");

ImGui::Render();

int display_w, display_h;
Expand Down
13 changes: 0 additions & 13 deletions src/ui/widgets/PreviewWidget.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/ui/widgets/PreviewWidget.hpp

This file was deleted.

Loading

0 comments on commit d5ec9db

Please sign in to comment.