Tesseract-Engine/src/Windows/PerformanceWindow.cpp

212 lines
6.7 KiB
C++
Raw Normal View History

#include "PerformanceWindow.h"
#include "imgui.h"
#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"
extern int LoadedAssets;
extern int g_GPU_Triangles_drawn_to_screen;
2025-01-03 23:14:25 +00:00
extern int g_GPU_Draw_Calls;
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;
// Initialize static members
int PerformanceWindow::m_OpenGLCallCount = 0;
2024-12-26 04:00:13 +00:00
int PerformanceWindow::m_TriangleCount = 0;
// 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};
static float s_TriangleHistory[60] = {0.0f};
// 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
// 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)
{
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;
// We'll also track when we last updated the scale
static double s_LastScaleUpdate = 0.0;
// 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;
g_GPU_Triangles_drawn_to_screen = 0;
2025-01-03 23:14:25 +00:00
g_GPU_Draw_Calls = 0;
}
void PerformanceWindow::Show(float fps, float ms)
{
2025-01-01 07:36:53 +00:00
SCOPE_TIMER("PerformanceWindow::Show");
// 1) Get current time from ImGui's internal clock
2024-12-26 04:00:13 +00:00
double currentTime = ImGui::GetTime();
// 2) If at least 0.05s has passed, push new data (about 20 updates per second)
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);
PushValueToHistory(s_TriangleHistory, 60, (float)m_TriangleCount);
}
// 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;
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;
s_MsScale = maxMs;
}
// Optional style adjustments
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 10));
ImGui::Begin("Performance##performance");
// 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
// min = 0, max = s_FpsScale or s_MsScale
2024-12-26 04:00:13 +00:00
ImGui::PlotLines("FPS",
s_FpsHistory,
IM_ARRAYSIZE(s_FpsHistory),
2024-12-26 04:00:13 +00:00
0,
nullptr,
0.0f,
s_FpsScale,
ImVec2(0, 60));
2024-12-26 04:00:13 +00:00
ImGui::PlotHistogram("ms/frame",
s_MsHistory,
IM_ARRAYSIZE(s_MsHistory),
2024-12-26 04:00:13 +00:00
0,
nullptr,
0.0f,
s_MsScale,
ImVec2(0, 60));
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,
IM_ARRAYSIZE(s_CallsHistory),
2024-12-26 04:00:13 +00:00
0,
nullptr,
0.0f,
300.0f,
ImVec2(0, 50));
ImGui::Text("Indices: %d", m_TriangleCount);
ImGui::PlotHistogram("Indices",
2024-12-26 04:00:13 +00:00
s_TriangleHistory,
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,
ImVec2(0, 50));
ImGui::Separator();
// Show asset count
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "Assets: %d", LoadedAssets);
2024-12-26 04:00:13 +00:00
ImGui::Separator();
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
ImGui::End();
2024-12-26 04:00:13 +00:00
ImGui::PopStyleVar();
}