2024-12-25 21:44:33 +00:00
|
|
|
#include "PerformanceWindow.h"
|
|
|
|
#include "imgui.h"
|
2024-12-25 23:52:19 +00:00
|
|
|
#include <algorithm> // for std::max_element, etc.
|
2024-12-26 04:00:13 +00:00
|
|
|
#include "Engine/ThemeManager.h"
|
2025-01-01 07:36:53 +00:00
|
|
|
#include "Engine/ScopedTimer.h"
|
2024-12-25 23:01:05 +00:00
|
|
|
|
2024-12-27 18:19:20 +00:00
|
|
|
extern int LoadedAssets;
|
2024-12-27 01:34:34 +00:00
|
|
|
extern int g_GPU_Triangles_drawn_to_screen;
|
2025-01-03 23:14:25 +00:00
|
|
|
extern int g_GPU_Draw_Calls;
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
|
2025-01-01 07:36:53 +00:00
|
|
|
const char *polygonModeOptions[] = {"Fill", "Wireframe", "Points"};
|
2024-12-27 06:09:43 +00:00
|
|
|
const int numPolygonModes = sizeof(polygonModeOptions) / sizeof(polygonModeOptions[0]);
|
|
|
|
|
|
|
|
int polygonMode = 0;
|
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
// Initialize static members
|
|
|
|
int PerformanceWindow::m_OpenGLCallCount = 0;
|
2024-12-26 04:00:13 +00:00
|
|
|
int PerformanceWindow::m_TriangleCount = 0;
|
2024-12-25 23:01:05 +00:00
|
|
|
|
|
|
|
// We'll store up to 60 data points for each stat.
|
2024-12-26 04:00:13 +00:00
|
|
|
static float s_FpsHistory[60] = {0.0f};
|
|
|
|
static float s_MsHistory[60] = {0.0f};
|
|
|
|
static float s_CallsHistory[60] = {0.0f};
|
2024-12-25 23:01:05 +00:00
|
|
|
static float s_TriangleHistory[60] = {0.0f};
|
|
|
|
|
2024-12-25 23:52:19 +00:00
|
|
|
// Current dynamic max scale for FPS and ms
|
|
|
|
static float s_FpsScale = 120.0f; // default starting scale for FPS
|
2024-12-26 04:00:13 +00:00
|
|
|
static float s_MsScale = 25.0f; // default starting scale for ms
|
2024-12-25 23:52:19 +00:00
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
// This function shifts the old values left and appends a new value at the end.
|
2024-12-26 04:00:13 +00:00
|
|
|
static void PushValueToHistory(float *historyArray, int historySize, float newValue)
|
2024-12-25 23:01:05 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < historySize - 1; i++)
|
|
|
|
historyArray[i] = historyArray[i + 1];
|
|
|
|
historyArray[historySize - 1] = newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll track when we last pushed data to our history.
|
|
|
|
static double s_LastPushTime = 0.0;
|
|
|
|
|
2024-12-25 23:52:19 +00:00
|
|
|
// We'll also track when we last updated the scale
|
|
|
|
static double s_LastScaleUpdate = 0.0;
|
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
// Update counters from the outside
|
|
|
|
void PerformanceWindow::UpdatePerformanceStats(int newCallCount, int newTriangleCount)
|
|
|
|
{
|
|
|
|
m_OpenGLCallCount = newCallCount;
|
2024-12-26 04:00:13 +00:00
|
|
|
m_TriangleCount = newTriangleCount;
|
2024-12-27 01:34:34 +00:00
|
|
|
|
|
|
|
g_GPU_Triangles_drawn_to_screen = 0;
|
2025-01-03 23:14:25 +00:00
|
|
|
g_GPU_Draw_Calls = 0;
|
2024-12-25 23:01:05 +00:00
|
|
|
}
|
2024-12-25 21:44:33 +00:00
|
|
|
|
|
|
|
void PerformanceWindow::Show(float fps, float ms)
|
|
|
|
{
|
2025-01-01 07:36:53 +00:00
|
|
|
SCOPE_TIMER("PerformanceWindow::Show");
|
|
|
|
|
2024-12-25 23:52:19 +00:00
|
|
|
// 1) Get current time from ImGui's internal clock
|
2024-12-26 04:00:13 +00:00
|
|
|
double currentTime = ImGui::GetTime();
|
2024-12-25 23:52:19 +00:00
|
|
|
|
|
|
|
// 2) If at least 0.05s has passed, push new data (about 20 updates per second)
|
2024-12-25 23:01:05 +00:00
|
|
|
if ((currentTime - s_LastPushTime) >= 0.05)
|
|
|
|
{
|
|
|
|
s_LastPushTime = currentTime;
|
|
|
|
|
|
|
|
// Push new values into our history arrays
|
2024-12-26 04:00:13 +00:00
|
|
|
PushValueToHistory(s_FpsHistory, 60, fps);
|
|
|
|
PushValueToHistory(s_MsHistory, 60, ms);
|
|
|
|
PushValueToHistory(s_CallsHistory, 60, (float)m_OpenGLCallCount);
|
2024-12-25 23:01:05 +00:00
|
|
|
PushValueToHistory(s_TriangleHistory, 60, (float)m_TriangleCount);
|
|
|
|
}
|
|
|
|
|
2024-12-25 23:52:19 +00:00
|
|
|
// 3) Every 1 second, recalculate the max scale for FPS and ms
|
|
|
|
if ((currentTime - s_LastScaleUpdate) >= 1.0)
|
|
|
|
{
|
|
|
|
s_LastScaleUpdate = currentTime;
|
|
|
|
|
|
|
|
// Find the maximum in s_FpsHistory
|
|
|
|
float maxFps = 0.0f;
|
|
|
|
for (int i = 0; i < 60; i++)
|
|
|
|
{
|
|
|
|
if (s_FpsHistory[i] > maxFps)
|
|
|
|
maxFps = s_FpsHistory[i];
|
|
|
|
}
|
|
|
|
// Scale it by +15%, ensure it's not below 1.0
|
|
|
|
maxFps *= 1.15f;
|
2024-12-26 04:00:13 +00:00
|
|
|
if (maxFps < 1.0f)
|
|
|
|
maxFps = 1.0f;
|
2024-12-25 23:52:19 +00:00
|
|
|
s_FpsScale = maxFps;
|
|
|
|
|
|
|
|
// Find the maximum in s_MsHistory
|
|
|
|
float maxMs = 0.0f;
|
|
|
|
for (int i = 0; i < 60; i++)
|
|
|
|
{
|
|
|
|
if (s_MsHistory[i] > maxMs)
|
|
|
|
maxMs = s_MsHistory[i];
|
|
|
|
}
|
|
|
|
// Scale it by +15%, ensure it's not below 1.0
|
|
|
|
maxMs *= 1.15f;
|
2024-12-26 04:00:13 +00:00
|
|
|
if (maxMs < 1.0f)
|
|
|
|
maxMs = 1.0f;
|
2024-12-25 23:52:19 +00:00
|
|
|
s_MsScale = maxMs;
|
|
|
|
}
|
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
// Optional style adjustments
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 10));
|
2024-12-29 02:50:39 +00:00
|
|
|
ImGui::Begin("Performance##performance");
|
2024-12-25 23:01:05 +00:00
|
|
|
|
|
|
|
// Colored header
|
|
|
|
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.2f, 1.0f), "Performance Stats");
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
// Show current FPS/ms
|
|
|
|
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "FPS: %.1f", fps);
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.8f, 1.0f), "| ms: %.3f", ms);
|
|
|
|
|
|
|
|
// Graphs for FPS + MS
|
2024-12-25 23:52:19 +00:00
|
|
|
// min = 0, max = s_FpsScale or s_MsScale
|
2024-12-26 04:00:13 +00:00
|
|
|
ImGui::PlotLines("FPS",
|
|
|
|
s_FpsHistory,
|
2024-12-25 23:52:19 +00:00
|
|
|
IM_ARRAYSIZE(s_FpsHistory),
|
2024-12-26 04:00:13 +00:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0.0f,
|
|
|
|
s_FpsScale,
|
2024-12-25 23:52:19 +00:00
|
|
|
ImVec2(0, 60));
|
2024-12-26 04:00:13 +00:00
|
|
|
|
|
|
|
ImGui::PlotHistogram("ms/frame",
|
|
|
|
s_MsHistory,
|
2024-12-25 23:52:19 +00:00
|
|
|
IM_ARRAYSIZE(s_MsHistory),
|
2024-12-26 04:00:13 +00:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0.0f,
|
|
|
|
s_MsScale,
|
2024-12-25 23:52:19 +00:00
|
|
|
ImVec2(0, 60));
|
2024-12-25 23:01:05 +00:00
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
// Show OpenGL calls + Triangles
|
|
|
|
ImGui::Text("OpenGL Calls: %d", m_OpenGLCallCount);
|
2024-12-26 04:00:13 +00:00
|
|
|
ImGui::PlotLines("GL Calls",
|
|
|
|
s_CallsHistory,
|
2024-12-25 23:52:19 +00:00
|
|
|
IM_ARRAYSIZE(s_CallsHistory),
|
2024-12-26 04:00:13 +00:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0.0f,
|
|
|
|
300.0f,
|
2024-12-25 23:52:19 +00:00
|
|
|
ImVec2(0, 50));
|
2024-12-25 23:01:05 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
ImGui::Text("Indices: %d", m_TriangleCount);
|
|
|
|
ImGui::PlotHistogram("Indices",
|
2024-12-26 04:00:13 +00:00
|
|
|
s_TriangleHistory,
|
2024-12-25 23:52:19 +00:00
|
|
|
IM_ARRAYSIZE(s_TriangleHistory),
|
2024-12-26 04:00:13 +00:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0.0f,
|
2025-01-01 07:36:53 +00:00
|
|
|
m_TriangleCount * 2.5,
|
2024-12-25 23:52:19 +00:00
|
|
|
ImVec2(0, 50));
|
2024-12-25 23:01:05 +00:00
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
ImGui::Separator();
|
2024-12-25 23:35:38 +00:00
|
|
|
|
2024-12-25 23:52:19 +00:00
|
|
|
// Show asset count
|
2024-12-27 18:19:20 +00:00
|
|
|
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "Assets: %d", LoadedAssets);
|
2024-12-25 23:35:38 +00:00
|
|
|
|
2024-12-26 04:00:13 +00:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2025-01-01 06:56:47 +00:00
|
|
|
const char *options[] = {"Bootsrap", "Duck Red", "Windark", "Deep Dark", "Tesseract Black"};
|
2024-12-26 04:00:13 +00:00
|
|
|
static int current_option = -1; // No selection initially
|
|
|
|
|
|
|
|
const char *preview_value = (current_option >= 0 && current_option < 3) ? options[current_option] : "Select an option";
|
|
|
|
if (ImGui::BeginCombo("Theme", preview_value))
|
|
|
|
{
|
|
|
|
for (int n = 0; n < IM_ARRAYSIZE(options); n++)
|
|
|
|
{
|
|
|
|
bool is_selected = (current_option == n);
|
|
|
|
if (ImGui::Selectable(options[n], is_selected))
|
|
|
|
{
|
2025-01-01 07:36:53 +00:00
|
|
|
current_option = n; // Update current option
|
2024-12-26 04:00:13 +00:00
|
|
|
ThemeManager_ChangeTheme(n); // Call the function with the selected option
|
|
|
|
}
|
|
|
|
// Set the initial focus when opening the combo (optional)
|
|
|
|
if (is_selected)
|
|
|
|
ImGui::SetItemDefaultFocus();
|
|
|
|
}
|
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
2024-12-27 06:09:43 +00:00
|
|
|
if (ImGui::Combo("Polygon Mode", &polygonMode, polygonModeOptions, numPolygonModes))
|
|
|
|
{
|
|
|
|
switch (polygonMode)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-12-26 04:00:13 +00:00
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
ImGui::End();
|
2024-12-26 04:00:13 +00:00
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
ImGui::PopStyleVar();
|
2024-12-25 21:44:33 +00:00
|
|
|
}
|