Added Ability to hide profiler

This commit is contained in:
OusmBlueNinja 2024-12-28 21:20:11 -06:00
parent f312ae829f
commit bed380268c
6 changed files with 160 additions and 110 deletions

View File

@ -18,7 +18,7 @@ DockId=0x00000002,0
Pos=374,27
Size=1212,770
Collapsed=0
DockId=0x0000001B,0
DockId=0x00000017,0
[Window][Performance]
Pos=8,774
@ -48,7 +48,7 @@ DockId=0x0000000B,0
Pos=374,27
Size=1202,849
Collapsed=0
DockId=0x0000001B,0
DockId=0x00000017,0
[Window][Lua Text Editor]
Pos=8,481
@ -74,9 +74,9 @@ DockId=0x00000016,0
[Window][Editor##EditorWindow]
Pos=275,27
Size=738,626
Size=1290,660
Collapsed=0
DockId=0x0000001B,0
DockId=0x00000017,0
[Window][Performance##performance]
Pos=8,720
@ -85,14 +85,14 @@ Collapsed=0
DockId=0x00000014,0
[Window][Logger##logger]
Pos=805,655
Size=760,514
Pos=275,689
Size=655,480
Collapsed=0
DockId=0x0000001E,0
DockId=0x0000001B,0
[Window][Lua Text Editor##LuaEditor]
Pos=1015,27
Size=550,626
Pos=932,689
Size=633,480
Collapsed=0
DockId=0x0000001C,0
@ -115,24 +115,24 @@ Collapsed=0
DockId=0x0000001A,0
[Window][Profiler]
Pos=275,655
Size=528,514
Pos=932,689
Size=633,480
Collapsed=0
DockId=0x0000001D,0
DockId=0x0000001C,1
[Table][0xE9E836E4,4]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000
Column 3 Weight=1.0000
Column 0 Weight=1.2999
Column 1 Weight=1.0439
Column 2 Weight=0.6474
Column 3 Weight=1.0088
[Docking][Data]
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,27 Size=1904,1142 Split=X Selected=0xF7365A5A
DockNode ID=0x00000015 Parent=0x14621557 SizeRef=1557,1142 Split=X
DockNode ID=0x00000011 Parent=0x00000015 SizeRef=265,1142 Split=X Selected=0x1D5D92B6
DockNode ID=0x00000019 Parent=0x00000011 SizeRef=172,571 Split=Y Selected=0x1D5D92B6
DockNode ID=0x00000013 Parent=0x00000019 SizeRef=326,691 Selected=0x1D5D92B6
DockNode ID=0x00000014 Parent=0x00000019 SizeRef=326,449 Selected=0x818D04BB
DockNode ID=0x00000013 Parent=0x00000019 SizeRef=326,691 HiddenTabBar=1 Selected=0x1D5D92B6
DockNode ID=0x00000014 Parent=0x00000019 SizeRef=326,449 HiddenTabBar=1 Selected=0x818D04BB
DockNode ID=0x0000001A Parent=0x00000011 SizeRef=301,571 Selected=0xD71D2CC1
DockNode ID=0x00000012 Parent=0x00000015 SizeRef=1290,1142 Split=X
DockNode ID=0x00000009 Parent=0x00000012 SizeRef=364,1142 Split=Y Selected=0x3DC5AC3F
@ -146,16 +146,14 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,27 Size=1904,1
DockNode ID=0x00000001 Parent=0x00000008 SizeRef=1202,1142 Split=Y Selected=0xDF0EC458
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1202,849 Split=Y Selected=0xDF0EC458
DockNode ID=0x0000000D Parent=0x00000003 SizeRef=1202,571 Split=Y Selected=0xDFF75B3F
DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1303,626 Split=X Selected=0xDFF75B3F
DockNode ID=0x0000001B Parent=0x00000017 SizeRef=738,626 CentralNode=1 Selected=0xDFF75B3F
DockNode ID=0x0000001C Parent=0x00000017 SizeRef=550,626 Selected=0x7D9E6BA2
DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1303,514 Split=X Selected=0x9B5D3198
DockNode ID=0x0000001D Parent=0x00000018 SizeRef=528,325 Selected=0x9B5D3198
DockNode ID=0x0000001E Parent=0x00000018 SizeRef=760,325 Selected=0x1C0788A1
DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1303,660 CentralNode=1 HiddenTabBar=1 Selected=0xDFF75B3F
DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1303,480 Split=X Selected=0x9B5D3198
DockNode ID=0x0000001B Parent=0x00000018 SizeRef=655,680 HiddenTabBar=1 Selected=0x1C0788A1
DockNode ID=0x0000001C Parent=0x00000018 SizeRef=633,680 Selected=0x9B5D3198
DockNode ID=0x0000000E Parent=0x00000003 SizeRef=1202,569 Selected=0xE98146C5
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1202,291 Selected=0x9DD4E196
DockNode ID=0x00000002 Parent=0x00000008 SizeRef=334,1142 HiddenTabBar=1 Selected=0x36DC96AB
DockNode ID=0x00000016 Parent=0x14621557 SizeRef=345,1142 Selected=0x8D0E8380
DockNode ID=0x00000016 Parent=0x14621557 SizeRef=345,1142 HiddenTabBar=1 Selected=0x8D0E8380
DockSpace ID=0xC6145A92 Pos=8,27 Size=1904,1142 Split=X
DockNode ID=0x0000000F Parent=0xC6145A92 SizeRef=301,1142 Selected=0xA8433A03
DockNode ID=0x00000010 Parent=0xC6145A92 SizeRef=1601,1142 CentralNode=1

View File

@ -2,115 +2,115 @@ Entities:
- ID: 0
Name: Player
Components:
Transform:
Position: [0, 2.79999995, -12.6000004]
Rotation: [121.932602, 121.932602, 121.932602]
Scale: [1, 1, 1]
Mesh:
vao: 2
indexCount: 15810
textureID: 1
MeshPath: assets/models/LowPolyFiatUNO.obj
Transform:
Position: [0, 2.79999995, -12.6000004]
Rotation: [466.191284, 466.191284, 466.191284]
Scale: [1, 1, 1]
- ID: 2
Name: Gun
Components:
ScriptComponent:
ScriptPath: assets/scripts/BouncingItem.lua
Transform:
Position: [-0.899999976, 0.776287973, -0.300000012]
Rotation: [-180, 75.4972076, 0]
Scale: [0.00499999989, 0.00499999989, 0.00499999989]
Mesh:
vao: 5
indexCount: 116445
textureID: 6
MeshPath: assets/models/Ak-47.obj
Transform:
Position: [-0.899999976, 0.727798522, -0.300000012]
Rotation: [-180, 321.793457, 0]
Scale: [0.00499999989, 0.00499999989, 0.00499999989]
ScriptComponent:
ScriptPath: assets/scripts/BouncingItem.lua
- ID: 3
Name: Grass Box Top
Components:
Transform:
Position: [-1.20000005, -3.4000001, -17.7000008]
Rotation: [-23.5, 15.8999996, -59.9000015]
Scale: [1, 1, 1]
Mesh:
vao: 1
indexCount: 36
textureID: 4
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [-1.20000005, -3.4000001, -17.7000008]
Rotation: [-23.5, 15.8999996, -59.9000015]
Scale: [1, 1, 1]
- ID: 4
Name: Bark Box
Components:
Transform:
Position: [8.10000038, 0.800000012, -12]
Rotation: [-17.2999992, -16.1000004, -19.2999992]
Scale: [1, 1, 1]
Mesh:
vao: 1
indexCount: 36
textureID: 5
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [8.10000038, 0.800000012, -12]
Rotation: [-17.2999992, -16.1000004, -19.2999992]
Scale: [1, 1, 1]
- ID: 5
Name: Skybox
Components:
Transform:
Position: [0, 0, 43.2000008]
Rotation: [0, 0, 0]
Scale: [100, 100, 100]
Mesh:
vao: 1
indexCount: 36
textureID: 7
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [0, 0, 43.2000008]
Rotation: [0, 0, 0]
Scale: [100, 100, 100]
- ID: 6
Name: Null Texture Box
Components:
Transform:
Position: [-6.5, -6, -18]
Rotation: [15.8000002, -18.2000008, -11.1000004]
Scale: [1, 1, 1]
Mesh:
vao: 1
indexCount: 36
textureID: 3
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [-6.5, -6, -18]
Rotation: [15.8000002, -18.2000008, -11.1000004]
Scale: [1, 1, 1]
- ID: 7
Name: Grass Box Bottom
Components:
Transform:
Position: [6.5999999, 1.79999995, -23.8999996]
Rotation: [-16.1000004, -15.8999996, -35]
Scale: [1, 1, 1]
Mesh:
vao: 1
indexCount: 36
textureID: 4
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [6.5999999, 1.79999995, -23.8999996]
Rotation: [-16.1000004, -15.8999996, -35]
Scale: [1, 1, 1]
- ID: 8
Name: Wood Box
Components:
Transform:
Position: [-7.80000019, 0.200000003, -29.7999992]
Rotation: [22.2999992, -32.7999992, 0]
Scale: [1, 1, 1]
Mesh:
vao: 1
indexCount: 36
textureID: 1
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [-7.80000019, 0.200000003, -29.7999992]
Rotation: [22.2999992, -32.7999992, 0]
Scale: [1, 1, 1]
- ID: 9
Name: Bricks
Components:
Transform:
Position: [5.5, -2.9000001, -19.5]
Rotation: [-41.4000015, -22.6000004, -52.2999992]
Scale: [1, 1, 1]
Mesh:
vao: 1
indexCount: 36
textureID: 2
MeshPath: assets/models/DefaultMesh.obj
Transform:
Position: [5.5, -2.9000001, -19.5]
Rotation: [-41.4000015, -22.6000004, -52.2999992]
Scale: [1, 1, 1]
- ID: 10
Name: Script Handler
Name: Script
Components:
ScriptComponent:
ScriptPath: assets/scripts/script.lua

View File

@ -24,8 +24,6 @@
#include "Windows/ProfilerWindow.h"
// Create an instance
// In your rendering loop
@ -124,6 +122,7 @@ bool MyEngine::Init(int width, int height, const std::string &title)
m_GameRunning = false;
m_FirstTickGameRunning = true;
m_showProfiler = false;
g_LoggerWindow = m_LoggerWindow.get();
@ -229,7 +228,6 @@ void MyEngine::Run()
while (!glfwWindowShouldClose(m_Window) && m_Running)
{
ScopedTimer frameTimer("MainLoop"); // Optional: Profile the entire loop
// Poll events
{
@ -297,7 +295,7 @@ void MyEngine::Run()
std::shared_ptr<ScriptComponent> script = Gameobject->GetComponent<ScriptComponent>();
if (script)
{ // Null Checks
ScopedTimer timer("GameObjectLuaCall: "+Gameobject->name);
ScopedTimer timer("GameObjectLuaCall: " + Gameobject->name);
script->Update(frame_delta);
}
@ -307,9 +305,8 @@ void MyEngine::Run()
// Render and show various windows
{
ScopedTimer timer("RenderGame");
m_RenderWindow->Show(&m_GameRunning); // The spinning triangle as ImGui::Image
m_RenderWindow->Show(&m_GameRunning); // The spinning triangle as ImGui::Image
}
{
ScopedTimer timer("ShowEditor");
@ -320,7 +317,10 @@ void MyEngine::Run()
m_SceneWindow->Show();
m_luaEditor->Show();
m_profilerWindow->Show();
if (m_showProfiler)
{
m_profilerWindow->Show();
}
}
// After rendering
@ -441,7 +441,11 @@ void MyEngine::ShowDockSpace()
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Tools"))
{
ImGui::Checkbox("Show Profiler", &m_showProfiler); // Add a checkbox to toggle the profiler
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Engine"))
{
@ -449,6 +453,7 @@ void MyEngine::ShowDockSpace()
{
m_GameRunning = !m_GameRunning; // Stop the engine
}
ImGui::EndMenu();
}

View File

@ -49,6 +49,7 @@ private:
bool m_GameRunning = false;
bool m_FirstTickGameRunning = true;
bool m_showProfiler = false;
// Windows
std::unique_ptr<RenderWindow> m_RenderWindow;

View File

@ -30,6 +30,25 @@ std::vector<float> ProfilerWindow::MovingAverage(const std::deque<double>& data,
return averages;
}
std::vector<float> ProfilerWindow::ExponentialMovingAverage(const std::deque<double>& data, float alpha)
{
std::vector<float> smoothedData;
if (data.empty())
return smoothedData;
float ema = static_cast<float>(data[0]); // Initialize EMA with the first value
smoothedData.push_back(ema);
for (size_t i = 1; i < data.size(); ++i)
{
ema = alpha * static_cast<float>(data[i]) + (1 - alpha) * ema;
smoothedData.push_back(ema);
}
return smoothedData;
}
// Update the history data structures with the latest profiling data
void ProfilerWindow::UpdateHistory(const std::unordered_map<std::string, ProfileResult>& data, double totalFrameTime)
{
@ -104,7 +123,7 @@ void ProfilerWindow::Show()
RenderTable(data);
// Render profiling graphs
RenderGraphs();
//RenderGraphs();
// Display total frame time (from the last update)
if (!m_TotalFrameTimeHistory.empty())
@ -192,61 +211,87 @@ void ProfilerWindow::RenderTable(const std::unordered_map<std::string, ProfileRe
}
}
// Render the profiling graphs
void ProfilerWindow::RenderGraphs()
{
ImGui::Separator();
ImGui::Text("Profiling Graphs");
// Example: Render a bar graph for the top 5 functions by total time
std::vector<std::pair<std::string, ProfileResult>> sortedData;
ImGui::Text("Profiling Graphs (Unified Multi-Line Plot)");
const auto& data = Profiler::Get().GetLastFrameData();
std::vector<std::pair<std::string, ProfileResult>> sortedData;
// Collect and sort functions by total time used
for (const auto& [name, result] : data)
{
sortedData.emplace_back(name, result);
}
// Sort and take top 5
std::sort(sortedData.begin(), sortedData.end(),
[](const std::pair<std::string, ProfileResult>& a, const std::pair<std::string, ProfileResult>& b) -> bool {
return a.second.TotalTime > b.second.TotalTime;
});
[](const std::pair<std::string, ProfileResult>& a, const std::pair<std::string, ProfileResult>& b) -> bool {
return a.second.TotalTime > b.second.TotalTime;
});
size_t displayCount = std::min<size_t>(5, sortedData.size());
size_t displayCount = std::min<size_t>(5, sortedData.size()); // Limit to top 5 functions
// Prepare data for the unified plot
std::vector<std::vector<float>> plotData(displayCount);
std::vector<std::string> functionNames;
float alpha = 0.2f; // Smoothing factor for EMA
for (size_t i = 0; i < displayCount; ++i)
{
const auto& [name, result] = sortedData[i];
double percentage = 0.0;
functionNames.push_back(name);
if (!m_TotalFrameTimeHistory.empty())
{
// Prevent division by zero
double lastTotalFrameTime = m_TotalFrameTimeHistory.back();
if (lastTotalFrameTime > 0.0)
{
percentage = (result.TotalTime / lastTotalFrameTime) * 100.0;
}
}
ImGui::PushID(static_cast<int>(i));
ImGui::Text("%s", name.c_str());
ImGui::SameLine();
ImGui::ProgressBar(static_cast<float>(percentage / 100.0f), ImVec2(-1.0f, 0.0f),
(std::to_string(percentage) + "%").c_str());
ImGui::PopID();
// Smooth each function's data using EMA
const auto& history = m_ProfileHistories[name];
plotData[i] = ExponentialMovingAverage(history.totalTimeHistory, alpha);
}
// Example: Render a line plot for total frame time with moving average
if (!m_TotalFrameTimeHistory.empty())
// Find the longest data series and the maximum value for normalization
size_t maxHistorySize = 0;
float maxValue = 0.0f;
for (const auto& series : plotData)
{
ImGui::Text("Frame Time Over Last %zu Frames (Smoothed)", m_TotalFrameTimeHistory.size());
if (!series.empty())
{
maxHistorySize = std::max(maxHistorySize, series.size());
maxValue = std::max(maxValue, *std::max_element(series.begin(), series.end()));
}
}
// Calculate moving average with a window of 10 frames
size_t windowSize = 10;
std::vector<float> smoothedFrameTimes = MovingAverage(m_TotalFrameTimeHistory, windowSize);
// Prepare the combined graph
if (maxHistorySize > 0)
{
std::vector<float> combinedGraph(maxHistorySize, 0.0f);
ImGui::PlotLines("##FrameTimeSmoothed", smoothedFrameTimes.data(), static_cast<int>(smoothedFrameTimes.size()), 0, NULL, 0.0f, 1000.0f, ImVec2(0, 80));
// Render a single unified graph with multi-line data
ImVec2 graphSize = ImVec2(0, 200); // Graph dimensions
ImGui::PlotLines(
"##UnifiedGraph",
[](void* data, int idx) -> float {
auto* plotData = static_cast<std::vector<std::vector<float>>*>(data);
float value = 0.0f;
for (const auto& series : *plotData)
{
if (idx < series.size())
value += series[idx];
}
return value;
},
static_cast<void*>(&plotData), static_cast<int>(maxHistorySize), 0, nullptr, 0.0f, maxValue, graphSize);
// Add a legend for the lines
ImGui::Separator();
for (size_t i = 0; i < functionNames.size(); ++i)
{
ImVec4 lineColor = ImVec4(0.2f + 0.2f * i, 0.2f, 1.0f - 0.2f * i, 1.0f);
ImGui::TextColored(lineColor, "%s", functionNames[i].c_str());
}
}
}

View File

@ -33,10 +33,11 @@ private:
std::chrono::steady_clock::time_point m_LastUpdateTime;
// Helper functions
void UpdateHistory(const std::unordered_map<std::string, ProfileResult>& data, double totalFrameTime);
void RenderTable(const std::unordered_map<std::string, ProfileResult>& data);
void UpdateHistory(const std::unordered_map<std::string, ProfileResult> &data, double totalFrameTime);
void RenderTable(const std::unordered_map<std::string, ProfileResult> &data);
void RenderGraphs();
// Helper for data smoothing
std::vector<float> MovingAverage(const std::deque<double>& data, size_t window);
std::vector<float> MovingAverage(const std::deque<double> &data, size_t window);
std::vector<float> ExponentialMovingAverage(const std::deque<double> &data, float alpha);
};