From 15bc6a15095425c5dfe9544ace03799becc47404 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 17 Jul 2021 22:31:11 -0400 Subject: [PATCH] [client] imgui: support registering new graphs Currently, our struct ll doesn't support removing elements from the middle, so we will not be removing anything for now. --- client/include/app.h | 4 ++ client/src/app.c | 110 ++++++++++++++++++++++--------------------- client/src/main.c | 5 ++ client/src/main.h | 3 +- 4 files changed, 68 insertions(+), 54 deletions(-) diff --git a/client/include/app.h b/client/include/app.h index 0bc7b50a..d56a2ca3 100644 --- a/client/include/app.h +++ b/client/include/app.h @@ -24,6 +24,7 @@ #include #include +#include "common/ringbuffer.h" #include "common/types.h" #include "interface/displayserver.h" @@ -78,6 +79,9 @@ void app_glSetSwapInterval(int interval); void app_glSwapBuffers(void); #endif +typedef struct ImGuiGraph * GraphHandle; +GraphHandle app_registerGraph(const char * name, RingBuffer buffer); +void app_unregisterGraph(GraphHandle handle); bool app_renderImGui(void); void app_clipboardRelease(void); diff --git a/client/src/app.c b/client/src/app.c index 688a62ad..d366d41d 100644 --- a/client/src/app.c +++ b/client/src/app.c @@ -620,8 +620,33 @@ void app_showFPS(bool showFPS) g_state.lgr->on_show_fps(g_state.lgrData, showFPS); } -bool rbCalcMetrics(int index, float * value, float * udata) +struct ImGuiGraph { + const char * name; + RingBuffer buffer; + bool enabled; +}; + +GraphHandle app_registerGraph(const char * name, RingBuffer buffer) +{ + struct ImGuiGraph * graph = malloc(sizeof(struct ImGuiGraph)); + graph->name = name; + graph->buffer = buffer; + graph->enabled = true; + ll_push(g_state.graphs, graph); + return graph; +} + +void app_unregisterGraph(GraphHandle handle) +{ + handle->enabled = false; +} + +static bool rbCalcMetrics(int index, void * value_, void * udata_) +{ + float * value = value_; + float * udata = udata_; + if (index == 0) { udata[0] = *value; @@ -662,61 +687,40 @@ bool app_renderImGui(void) ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar ); - - float renderMetrics[4] = {}; - float frameMetrics [4] = {}; - ringbuffer_forEach(g_state.renderTimings, (RingBufferIterator)rbCalcMetrics, - renderMetrics); - ringbuffer_forEach(g_state.frameTimings , (RingBufferIterator)rbCalcMetrics, - frameMetrics); - - if (renderMetrics[2] > 0.0f) + GraphHandle graph; + for (ll_reset(g_state.graphs); ll_walk(g_state.graphs, (void **)&graph); ) { - renderMetrics[2] /= ringbuffer_getCount(g_state.renderTimings); - renderMetrics[3] = 1000.0f / renderMetrics[2]; + if (!graph->enabled) + continue; + + float metrics[4] = {}; + ringbuffer_forEach(graph->buffer, rbCalcMetrics, metrics); + + if (metrics[2] > 0.0f) + { + metrics[2] /= ringbuffer_getCount(graph->buffer); + metrics[3] = 1000.0f / metrics[2]; + } + + char title[64]; + const ImVec2 size = {400.0f, 100.0f}; + + snprintf(title, sizeof(title), + "%s: min:%4.2f max:%4.2f avg:%4.2f/%4.2fHz", + graph->name, metrics[0], metrics[1], metrics[2], metrics[3]); + + igPlotLinesFloatPtr( + "", + (float *)ringbuffer_getValues(graph->buffer), + ringbuffer_getLength(graph->buffer), + ringbuffer_getStart (graph->buffer), + title, + 0.0f, + 50.0f, + size, + sizeof(float)); } - if (frameMetrics[2] > 0.0f) - { - frameMetrics[2] /= ringbuffer_getCount(g_state.frameTimings ); - frameMetrics[3] = 1000.0f / frameMetrics[2]; - } - - char buffer[64]; - const ImVec2 size = {400.0f, 100.0f}; - - snprintf(buffer, sizeof(buffer), - "RENDER: min:%4.2f max:%4.2f avg:%4.2f/%4.2fHz", - renderMetrics[0], renderMetrics[1], renderMetrics[2], - renderMetrics[3]); - - igPlotLinesFloatPtr( - "", - (float *)ringbuffer_getValues(g_state.renderTimings), - ringbuffer_getLength(g_state.renderTimings), - ringbuffer_getStart (g_state.renderTimings), - buffer, - 0.0f, - 50.0f, - size, - sizeof(float)); - - snprintf(buffer, sizeof(buffer), - "UPLOAD: min:%4.2f max:%4.2f avg:%4.2f/%4.2fHz", - frameMetrics[0], frameMetrics[1], frameMetrics[2], - frameMetrics[3]); - - igPlotLinesFloatPtr( - "", - (float *)ringbuffer_getValues(g_state.frameTimings), - ringbuffer_getLength(g_state.frameTimings), - ringbuffer_getStart (g_state.frameTimings), - buffer, - 0.0f, - 50.0f, - size, - sizeof(float)); - igEnd(); igRender(); diff --git a/client/src/main.c b/client/src/main.c index 4385f287..92247e29 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -720,10 +720,15 @@ static int lg_run(void) ImFontAtlas_GetTexDataAsRGBA32(g_state.io->Fonts, &text_pixels, &text_w, &text_h, NULL); + g_state.graphs = ll_new(); + // initialize metrics ringbuffers g_state.renderTimings = ringbuffer_new(256, sizeof(float)); g_state.frameTimings = ringbuffer_new(256, sizeof(float)); + app_registerGraph("RENDER", g_state.renderTimings); + app_registerGraph("UPLOAD", g_state.frameTimings); + // search for the best displayserver ops to use for(int i = 0; i < LG_DISPLAYSERVER_COUNT; ++i) if (LG_DisplayServers[i]->probe()) diff --git a/client/src/main.h b/client/src/main.h index 816ddc41..3133a943 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -47,7 +47,8 @@ struct AppState { enum RunState state; - ImGuiIO * io; + ImGuiIO * io; + struct ll * graphs; struct LG_DisplayServerOps * ds; bool dsInitialized;