Added auto scaling graphs to performance window

This commit is contained in:
OusmBlueNinja 2024-12-25 17:52:19 -06:00
parent 42fa83f695
commit 3328d3c2fa
2 changed files with 78 additions and 11 deletions

View File

@ -1,6 +1,6 @@
#include "PerformanceWindow.h"
#include "imgui.h"
#include <algorithm> // for std::move, etc.
#include <algorithm> // for std::max_element, etc.
extern int LoaddedAssets;
@ -14,6 +14,10 @@ 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
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.
static void PushValueToHistory(float* historyArray, int historySize, float newValue)
{
@ -25,6 +29,9 @@ static void PushValueToHistory(float* historyArray, int historySize, float newVa
// 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)
{
@ -34,9 +41,10 @@ void PerformanceWindow::UpdatePerformanceStats(int newCallCount, int newTriangle
void PerformanceWindow::Show(float fps, float ms)
{
// Get current time from ImGui's internal clock
// 1) Get current time from ImGui's internal clock
double currentTime = ImGui::GetTime();
// If at least 1ms (0.001s) has passed, push new data
// 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;
@ -48,6 +56,36 @@ void PerformanceWindow::Show(float fps, float ms)
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;
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;
if (maxMs < 1.0f) maxMs = 1.0f;
s_MsScale = maxMs;
}
// Optional style adjustments
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 10));
ImGui::Begin("Performance");
@ -62,24 +100,51 @@ void PerformanceWindow::Show(float fps, float ms)
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.8f, 1.0f), "| ms: %.3f", ms);
// Graphs for FPS + MS
ImGui::PlotLines("FPS (60 frames)", s_FpsHistory, IM_ARRAYSIZE(s_FpsHistory),
0, nullptr, 0.0f, 1000.0f, ImVec2(0, 60));
ImGui::PlotHistogram("ms/frame", s_MsHistory, IM_ARRAYSIZE(s_MsHistory),
0, nullptr, 0.0f, 25.0f, ImVec2(0, 60));
// min = 0, max = s_FpsScale or s_MsScale
ImGui::PlotLines("FPS (60 frames)",
s_FpsHistory,
IM_ARRAYSIZE(s_FpsHistory),
0,
nullptr,
0.0f,
s_FpsScale,
ImVec2(0, 60));
ImGui::PlotHistogram("ms/frame",
s_MsHistory,
IM_ARRAYSIZE(s_MsHistory),
0,
nullptr,
0.0f,
s_MsScale,
ImVec2(0, 60));
ImGui::Separator();
// Show OpenGL calls + Triangles
ImGui::Text("OpenGL Calls: %d", m_OpenGLCallCount);
ImGui::PlotLines("GL Calls (60 frames)", s_CallsHistory, IM_ARRAYSIZE(s_CallsHistory),
0, nullptr, 0.0f, 300.0f, ImVec2(0, 50));
ImGui::PlotLines("GL Calls (60 frames)",
s_CallsHistory,
IM_ARRAYSIZE(s_CallsHistory),
0,
nullptr,
0.0f,
300.0f,
ImVec2(0, 50));
ImGui::Text("Triangles: %d", m_TriangleCount);
ImGui::PlotHistogram("Triangles (60 frames)", s_TriangleHistory, IM_ARRAYSIZE(s_TriangleHistory),
0, nullptr, 0.0f, 5000.0f, ImVec2(0, 50));
ImGui::PlotHistogram("Triangles (60 frames)",
s_TriangleHistory,
IM_ARRAYSIZE(s_TriangleHistory),
0,
nullptr,
0.0f,
5000.0f,
ImVec2(0, 50));
ImGui::Separator();
// Show asset count
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "Assets: %d", LoaddedAssets);
ImGui::End();

View File

@ -14,6 +14,8 @@ public:
void UpdatePerformanceStats(int newCallCount, int newTriangleCount);
private:
float max_ms;
// These are static so they're shared across all instances
static int m_OpenGLCallCount;
static int m_TriangleCount;