Visual Computing Library
Loading...
Searching...
No Matches
imgui_stats_drawer.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IMGUI_IMGUI_STATS_DRAWER_H
24#define VCL_IMGUI_IMGUI_STATS_DRAWER_H
25
26#include <imgui.h>
27#include <vclib/render/drawers/plain_drawer.h>
28
29#include <algorithm>
30#include <vector>
31
32#ifdef VCLIB_RENDER_BACKEND_BGFX
33#include <bgfx/bgfx.h>
34#elif defined(VCLIB_RENDER_BACKEND_OPENGL2)
35// include OpenGL headers
36#ifdef __APPLE__
37#include <OpenGL/gl.h>
38#else
39#ifdef _WIN32
40// dirty trick to avoid including all headers distributed by Khronos
41#define GL_SHADING_LANGUAGE_VERSION 0x8B8C
42#include <windows.h>
43#endif
44#include <GL/gl.h>
45#endif
46#endif
47
48namespace vcl::imgui {
49
50template<typename DerivedDrawer>
51class ImguiStatsDrawer : public vcl::PlainDrawer<DerivedDrawer>
52{
53 // frame times
54 static constexpr size_t FRAME_COUNT = 256;
55 std::vector<float> mFrameTimes = std::vector<float>(FRAME_COUNT, 0.0f);
56
57#ifdef VCLIB_RENDER_BACKEND_OPENGL2
58 // OpenGL information
59 const GLubyte* mGlRendererString = nullptr;
60 const GLubyte* mGlVendorString = nullptr;
61 const GLubyte* mGlVersionString = nullptr;
62 const GLubyte* mGlslVersionString = nullptr;
63#endif
64
65public:
66 using vcl::PlainDrawer<DerivedDrawer>::PlainDrawer;
68
69 void onDraw(uint viewId)
70 {
71 Base::onDraw(viewId);
72
73 ImGuiIO& io = ImGui::GetIO();
74 ImGui::Begin("Stats", nullptr);
75
76#ifdef VCLIB_RENDER_BACKEND_BGFX
77 // map renderer type to string
78 static const std::unordered_map<uint16_t, const char*> sVendorIdNames =
79 {
80 {BGFX_PCI_ID_NONE, "None" },
82 {BGFX_PCI_ID_AMD, "AMD" },
83 {BGFX_PCI_ID_APPLE, "Apple" },
84 {BGFX_PCI_ID_INTEL, "Intel" },
85 {BGFX_PCI_ID_NVIDIA, "NVIDIA" },
86 {BGFX_PCI_ID_MICROSOFT, "Microsoft"},
87 {BGFX_PCI_ID_ARM, "ARM" },
88 };
89
90 ImGui::SeparatorText("BGFX");
91 // get bgfx information
92 ImGui::Text(
93 "Renderer: %s",
94 bgfx::getRendererName(bgfx::getCaps()->rendererType));
95 auto it = sVendorIdNames.find(bgfx::getCaps()->vendorId);
96 ImGui::Text(
97 "Vendor ID: %s",
98 it != sVendorIdNames.end() ? it->second : "Unknown");
99 ImGui::Text("Device ID: %d", bgfx::getCaps()->deviceId);
100 ImGui::Text("GPUs: %d", bgfx::getCaps()->numGPUs);
101
102 ImGui::Separator();
103
104 // get bgfx stats
105 const bgfx::Stats* stats = bgfx::getStats();
106 ImGui::Text("Submitted calls:");
107 ImGui::Text(
108 "\t%d draw - %d compute - %d blit",
109 stats->numDraw,
110 stats->numCompute,
111 stats->numBlit);
112 ImGui::Text("Backbuffer size: %d x %d", stats->width, stats->height);
113 if (stats->gpuMemoryUsed > 0) {
114 ImGui::Text(
115 "GPU mem: %d / %d MB",
116 int(stats->gpuMemoryUsed / (1024 * 1024)),
117 int(stats->gpuMemoryMax / (1024 * 1024)));
118 }
119 ImGui::Text(
120 "Texture memory: %d MB",
121 int(stats->textureMemoryUsed / (1024 * 1024)));
122 ImGui::Text(
123 "RT memory: %d MB", int(stats->rtMemoryUsed / (1024 * 1024)));
124
125 // frame times
126 const double toMsCpu = 1000.0 / stats->cpuTimerFreq;
127 const double toMsGpu = 1000.0 / stats->gpuTimerFreq;
128 // const double frameMs = double(stats->cpuTimeFrame)*toMsCpu;
129
130 ImGui::Text(
131 "Submit CPU %0.3f, GPU %0.3f (L: %d) ms",
132 double(stats->cpuTimeEnd - stats->cpuTimeBegin) * toMsCpu,
133 double(stats->gpuTimeEnd - stats->gpuTimeBegin) * toMsGpu,
134 stats->maxGpuLatency);
135
136#elif defined(VCLIB_RENDER_BACKEND_OPENGL2)
137 // Display OpenGL information
138 if (mGlRendererString == nullptr) {
143 }
144 ImGui::SeparatorText("OpenGL");
145 ImGui::Text("Renderer: %s", mGlRendererString);
146 ImGui::Text("Vendor: %s", mGlVendorString);
147 ImGui::Text("Version: %s", mGlVersionString);
148 ImGui::Text("GLSL Version: %s", mGlslVersionString);
149
150#endif
151 ImGui::Separator();
152
153 // Update frame times
154 std::rotate(
155 mFrameTimes.begin(), mFrameTimes.begin() + 1, mFrameTimes.end());
156 mFrameTimes.back() = io.Framerate;
157
158 // Display ImGui information
159 ImGui::Text(
160 "Display size: %g x %g", io.DisplaySize.x, io.DisplaySize.y);
161 ImGui::Text(
162 "Framebuffer scale: %g x %g",
163 io.DisplayFramebufferScale.x,
164 io.DisplayFramebufferScale.y);
165 ImGui::Separator();
166
167 // Display framerate plot
168 ImGui::PlotLines(
169 "FPS",
170 mFrameTimes.data(),
171 int(mFrameTimes.size()),
172 0,
173 nullptr,
174 -5.0f,
175 120.0f,
176 ImVec2(200, 0));
177
178 // Display framerate
179 ImGui::Text(
180 "%.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
181
182 ImGui::End();
183 }
184};
185
186} // namespace vcl::imgui
187
188#endif // VCL_IMGUI_IMGUI_STATS_DRAWER_H
Definition plain_drawer.h:32
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition imgui_stats_drawer.h:52