2021-07-22 07:27:30 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2023-10-20 04:36:34 +00:00
|
|
|
* Copyright © 2017-2023 The Looking Glass Authors
|
2021-07-22 07:27:30 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "interface/overlay.h"
|
|
|
|
#include "cimgui.h"
|
|
|
|
|
|
|
|
#include "../main.h"
|
|
|
|
|
|
|
|
#include "common/debug.h"
|
2021-07-22 10:05:21 +00:00
|
|
|
#include "overlay_utils.h"
|
2021-07-22 07:27:30 +00:00
|
|
|
|
|
|
|
struct GraphState
|
|
|
|
{
|
2021-08-04 20:40:06 +00:00
|
|
|
bool show;
|
2021-07-22 07:27:30 +00:00
|
|
|
struct ll * graphs;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct GraphState gs = {0};
|
|
|
|
|
|
|
|
struct OverlayGraph
|
|
|
|
{
|
2022-01-17 11:08:56 +00:00
|
|
|
const char * name;
|
|
|
|
RingBuffer buffer;
|
|
|
|
bool enabled;
|
|
|
|
float min;
|
|
|
|
float max;
|
|
|
|
GraphFormatFn formatFn;
|
2021-07-22 07:27:30 +00:00
|
|
|
};
|
|
|
|
|
2021-08-04 14:47:00 +00:00
|
|
|
|
2021-08-11 23:04:45 +00:00
|
|
|
static void configCallback(void * udata, int * id)
|
2021-08-04 14:47:00 +00:00
|
|
|
{
|
2022-02-28 01:38:43 +00:00
|
|
|
igCheckbox("Show timing graphs", &gs.show);
|
2021-08-04 14:47:00 +00:00
|
|
|
igSeparator();
|
|
|
|
|
|
|
|
igBeginTable("split", 2, 0, (ImVec2){}, 0);
|
|
|
|
|
|
|
|
GraphHandle graph;
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_lock(gs.graphs);
|
|
|
|
ll_forEachNL(gs.graphs, item, graph)
|
2021-08-04 14:47:00 +00:00
|
|
|
{
|
|
|
|
igTableNextColumn();
|
|
|
|
igCheckbox(graph->name, &graph->enabled);
|
|
|
|
}
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_unlock(gs.graphs);
|
2021-08-04 14:47:00 +00:00
|
|
|
|
|
|
|
igEndTable();
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:40:06 +00:00
|
|
|
static void showTimingKeybind(int sc, void * opaque)
|
|
|
|
{
|
|
|
|
gs.show ^= true;
|
2021-08-04 20:47:36 +00:00
|
|
|
app_invalidateWindow(false);
|
2021-08-04 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 14:40:13 +00:00
|
|
|
static void graphs_earlyInit(void)
|
2021-07-22 07:27:30 +00:00
|
|
|
{
|
|
|
|
gs.graphs = ll_new();
|
2022-05-25 14:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool graphs_init(void ** udata, const void * params)
|
|
|
|
{
|
2021-08-04 14:57:54 +00:00
|
|
|
app_overlayConfigRegister("Performance Metrics", configCallback, NULL);
|
2022-06-29 08:24:53 +00:00
|
|
|
app_registerKeybind(0, 'T', showTimingKeybind, NULL,
|
2021-08-04 20:40:06 +00:00
|
|
|
"Show frame timing information");
|
2021-07-22 07:27:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void graphs_free(void * udata)
|
|
|
|
{
|
2021-07-22 07:47:58 +00:00
|
|
|
struct OverlayGraph * graph;
|
|
|
|
while(ll_shift(gs.graphs, (void **)&graph))
|
|
|
|
free(graph);
|
2021-07-22 07:27:30 +00:00
|
|
|
ll_free(gs.graphs);
|
2022-05-27 05:42:49 +00:00
|
|
|
gs.graphs = NULL;
|
2021-07-22 07:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct BufferMetrics
|
|
|
|
{
|
|
|
|
float min;
|
|
|
|
float max;
|
|
|
|
float sum;
|
|
|
|
float avg;
|
|
|
|
float freq;
|
2022-01-17 11:08:56 +00:00
|
|
|
float last;
|
2021-07-22 07:27:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool rbCalcMetrics(int index, void * value_, void * udata_)
|
|
|
|
{
|
|
|
|
float * value = value_;
|
|
|
|
struct BufferMetrics * udata = udata_;
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
{
|
|
|
|
udata->min = *value;
|
|
|
|
udata->max = *value;
|
|
|
|
udata->sum = *value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (udata->min > *value)
|
|
|
|
udata->min = *value;
|
|
|
|
|
|
|
|
if (udata->max < *value)
|
|
|
|
udata->max = *value;
|
|
|
|
|
|
|
|
udata->sum += *value;
|
2022-01-17 11:08:56 +00:00
|
|
|
udata->last = *value;
|
2021-07-22 07:27:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-22 08:33:50 +00:00
|
|
|
static int graphs_render(void * udata, bool interactive,
|
|
|
|
struct Rect * windowRects, int maxRects)
|
2021-07-22 07:27:30 +00:00
|
|
|
{
|
2021-08-04 20:40:06 +00:00
|
|
|
if (!gs.show)
|
2021-07-22 08:33:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2021-07-23 04:01:10 +00:00
|
|
|
float fontSize = igGetFontSize();
|
|
|
|
|
2021-08-04 00:27:47 +00:00
|
|
|
GraphHandle graph;
|
|
|
|
int graphCount = 0;
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_lock(gs.graphs);
|
|
|
|
ll_forEachNL(gs.graphs, item, graph)
|
|
|
|
{
|
2021-08-04 00:27:47 +00:00
|
|
|
if (graph->enabled)
|
|
|
|
++graphCount;
|
2022-01-12 01:17:29 +00:00
|
|
|
}
|
2021-08-04 00:27:47 +00:00
|
|
|
|
2021-07-22 08:33:50 +00:00
|
|
|
ImVec2 pos = {0.0f, 0.0f};
|
2021-07-22 07:27:30 +00:00
|
|
|
igSetNextWindowBgAlpha(0.4f);
|
2021-07-31 05:35:53 +00:00
|
|
|
igSetNextWindowPos(pos, ImGuiCond_FirstUseEver, pos);
|
2021-07-31 10:21:34 +00:00
|
|
|
igSetNextWindowSize(
|
|
|
|
(ImVec2){
|
|
|
|
28.0f * fontSize,
|
2021-08-04 00:27:47 +00:00
|
|
|
7.0f * fontSize * graphCount
|
2021-07-31 10:21:34 +00:00
|
|
|
},
|
|
|
|
ImGuiCond_FirstUseEver);
|
2021-07-22 07:27:30 +00:00
|
|
|
|
2021-07-31 10:21:34 +00:00
|
|
|
ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav;
|
|
|
|
if (!interactive)
|
|
|
|
flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDecoration;
|
|
|
|
|
|
|
|
igBegin("Performance Metrics", NULL, flags);
|
|
|
|
|
|
|
|
ImVec2 winSize;
|
|
|
|
igGetContentRegionAvail(&winSize);
|
2021-08-04 00:27:47 +00:00
|
|
|
const float height = (winSize.y / graphCount)
|
2021-07-31 10:21:34 +00:00
|
|
|
- igGetStyle()->ItemSpacing.y;
|
2021-07-22 07:27:30 +00:00
|
|
|
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_forEachNL(gs.graphs, item, graph)
|
2021-07-22 07:27:30 +00:00
|
|
|
{
|
|
|
|
if (!graph->enabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct BufferMetrics metrics = {};
|
2021-08-09 04:06:32 +00:00
|
|
|
ringbuffer_forEach(graph->buffer, rbCalcMetrics, &metrics, false);
|
2021-07-22 07:27:30 +00:00
|
|
|
|
|
|
|
if (metrics.sum > 0.0f)
|
|
|
|
{
|
|
|
|
metrics.avg = metrics.sum / ringbuffer_getCount(graph->buffer);
|
|
|
|
metrics.freq = 1000.0f / metrics.avg;
|
|
|
|
}
|
|
|
|
|
2022-01-17 11:08:56 +00:00
|
|
|
const char * title;
|
|
|
|
if (graph->formatFn)
|
|
|
|
title = graph->formatFn(graph->name,
|
|
|
|
metrics.min, metrics.max, metrics.avg, metrics.freq, metrics.last);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static char _title[64];
|
|
|
|
snprintf(_title, sizeof(_title),
|
|
|
|
"%s: min:%4.2f max:%4.2f avg:%4.2f/%4.2fHz",
|
|
|
|
graph->name, metrics.min, metrics.max, metrics.avg, metrics.freq);
|
|
|
|
title = _title;
|
|
|
|
}
|
2021-07-22 07:27:30 +00:00
|
|
|
|
2022-01-07 13:26:12 +00:00
|
|
|
igPlotLines_FloatPtr(
|
2021-07-22 07:27:30 +00:00
|
|
|
"",
|
|
|
|
(float *)ringbuffer_getValues(graph->buffer),
|
|
|
|
ringbuffer_getLength(graph->buffer),
|
|
|
|
ringbuffer_getStart (graph->buffer),
|
|
|
|
title,
|
2021-07-29 01:56:50 +00:00
|
|
|
graph->min,
|
|
|
|
graph->max,
|
2021-07-31 10:21:34 +00:00
|
|
|
(ImVec2){ winSize.x, height },
|
2021-07-22 07:27:30 +00:00
|
|
|
sizeof(float));
|
|
|
|
};
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_unlock(gs.graphs);
|
2021-07-22 07:27:30 +00:00
|
|
|
|
2021-07-22 10:05:21 +00:00
|
|
|
overlayGetImGuiRect(windowRects);
|
2021-07-22 07:27:30 +00:00
|
|
|
igEnd();
|
2021-07-22 08:33:50 +00:00
|
|
|
return 1;
|
2021-07-22 07:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct LG_OverlayOps LGOverlayGraphs =
|
|
|
|
{
|
|
|
|
.name = "Graphs",
|
2022-05-25 14:40:13 +00:00
|
|
|
.earlyInit = graphs_earlyInit,
|
2021-07-22 07:27:30 +00:00
|
|
|
.init = graphs_init,
|
|
|
|
.free = graphs_free,
|
|
|
|
.render = graphs_render
|
|
|
|
};
|
|
|
|
|
2022-01-17 11:08:56 +00:00
|
|
|
GraphHandle overlayGraph_register(const char * name, RingBuffer buffer,
|
|
|
|
float min, float max, GraphFormatFn formatFn)
|
2021-07-22 07:27:30 +00:00
|
|
|
{
|
2021-08-15 16:18:22 +00:00
|
|
|
struct OverlayGraph * graph = malloc(sizeof(*graph));
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!graph)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-17 11:08:56 +00:00
|
|
|
graph->name = name;
|
|
|
|
graph->buffer = buffer;
|
|
|
|
graph->enabled = true;
|
|
|
|
graph->min = min;
|
|
|
|
graph->max = max;
|
|
|
|
graph->formatFn = formatFn;
|
2021-07-22 07:27:30 +00:00
|
|
|
ll_push(gs.graphs, graph);
|
|
|
|
return graph;
|
|
|
|
}
|
|
|
|
|
|
|
|
void overlayGraph_unregister(GraphHandle handle)
|
|
|
|
{
|
2022-05-27 05:42:49 +00:00
|
|
|
if (!gs.graphs)
|
|
|
|
return;
|
|
|
|
|
2022-01-17 11:09:41 +00:00
|
|
|
ll_removeData(gs.graphs, handle);
|
|
|
|
free(handle);
|
2022-01-17 11:53:52 +00:00
|
|
|
|
|
|
|
if (gs.show)
|
|
|
|
app_invalidateWindow(false);
|
2021-07-22 07:27:30 +00:00
|
|
|
}
|
2021-08-04 00:27:47 +00:00
|
|
|
|
|
|
|
void overlayGraph_iterate(void (*callback)(GraphHandle handle, const char * name,
|
|
|
|
bool * enabled, void * udata), void * udata)
|
|
|
|
{
|
|
|
|
GraphHandle graph;
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_lock(gs.graphs);
|
|
|
|
ll_forEachNL(gs.graphs, item, graph)
|
2021-08-04 00:27:47 +00:00
|
|
|
callback(graph, graph->name, &graph->enabled, udata);
|
2022-01-12 01:17:29 +00:00
|
|
|
ll_unlock(gs.graphs);
|
2021-08-04 00:27:47 +00:00
|
|
|
}
|
2022-01-17 11:49:19 +00:00
|
|
|
|
2022-01-27 23:59:12 +00:00
|
|
|
void overlayGraph_invalidate(GraphHandle handle)
|
2022-01-17 11:49:19 +00:00
|
|
|
{
|
|
|
|
if (!gs.show)
|
|
|
|
return;
|
|
|
|
|
2022-01-27 23:59:12 +00:00
|
|
|
if (handle->enabled)
|
|
|
|
app_invalidateWindow(false);
|
2022-01-17 11:49:19 +00:00
|
|
|
}
|