Cogs.Core
ProfileInspector.cpp
1#include "Inspectors.h"
2
3#include "Context.h"
4
5#ifdef COGS_USE_MICROPROFILE
6
7#include "../microprofile/microprofile.h"
8#include "../microprofile/microprofileui.h"
9
10#include "imgui.h"
11
12namespace
13{
14 thread_local ImDrawList * imguiDrawList = nullptr;
15 thread_local ImVec2 guiStart;
16 thread_local ImVec2 guiSize;
17
18 inline bool isBoxInside(ImVec2 p0, ImVec2 p1)
19 {
20 return (p1.x >= guiStart.x && p0.x < guiStart.x + guiSize.x) && (p1.y >= guiStart.y && p0.y < guiStart.y + guiSize.y);
21 }
22}
23
24void MicroProfileDrawText(int nX, int nY, uint32_t nColor, const char* pText, uint32_t nNumCharacters)
25{
26 // Toggle all-transparent colors that are for some reason produced by microprofile.
27 if (nColor == 0xFFFFFF) nColor = 0xFFFFFFFF;
28
29 imguiDrawList->AddText(ImVec2(nX + guiStart.x + 2, nY + guiStart.y - 4), nColor, pText, pText + nNumCharacters);
30}
31
32void MicroProfileDrawBox(int nX, int nY, int nX1, int nY1, uint32_t nColor, MicroProfileBoxType boxType)
33{
34 ImVec2 p0(nX + guiStart.x, nY + guiStart.y);
35 ImVec2 p1(nX1 + guiStart.x, nY1 + guiStart.y);
36
37 if (!isBoxInside(p0, p1)) {
38 return;
39 }
40
41 switch (boxType) {
42 case MicroProfileBoxTypeBar:
43 {
44 uint32_t cul = nColor;
45 uint32_t cur = (nColor & 0x00FFFFFF) + 0xFF000000;
46 uint32_t clr = (nColor & 0x00FFFFFF) + 0x50000000;
47 uint32_t cll = (nColor & 0x00FFFFFF) + 0x50000000;
48
49 imguiDrawList->AddRectFilledMultiColor(p0, p1, cul, cur, clr, cll);
50
51 if (nX1 - nX > 5) {
52 imguiDrawList->AddRect(p0, p1, 0x50000000);
53 }
54 break;
55 }
56 case MicroProfileBoxTypeFlat:
57 imguiDrawList->AddRectFilled(p0, p1, nColor);
58 break;
59 default:
60 assert(false);
61 }
62}
63
64void MicroProfileDrawLine2D(uint32_t nVertices, float* pVertices, uint32_t nColor)
65{
66 for (uint32_t vert = 0; vert + 1 < nVertices; ++vert) {
67 uint32_t i = 2 * vert;
68 ImVec2 posA(pVertices[i] + guiStart.x, pVertices[i + 1] + guiStart.y);
69 ImVec2 posB(pVertices[i + 2] + guiStart.x, pVertices[i + 3] + guiStart.y);
70
71 imguiDrawList->AddLine(posA, posB, nColor);
72 }
73}
74
75void Cogs::Core::profileInspector(Context * /*context*/, bool * showProfiler)
76{
77 if (!*showProfiler) return;
78
79 ImGui::SetNextWindowSize(ImVec2(1200, 700), ImGuiCond_FirstUseEver);
80 ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_FirstUseEver);
81
82 ImGui::Begin("Microprofile");
83
84 imguiDrawList = ImGui::GetWindowDrawList();
85 guiStart = ImGui::GetCursorScreenPos();
86 guiSize = ImGui::GetContentRegionAvail();
87
88 ImGui::InvisibleButton("ProfilerCanvas", guiSize);
89
90 if (ImGui::IsItemHovered()) {
91 MicroProfileMouseButton(ImGui::GetIO().MouseDown[0], ImGui::GetIO().MouseDown[1]);
92 } else {
93 MicroProfileMouseButton(0, 0);
94 }
95
96 MicroProfileMousePosition((uint32_t)(ImGui::GetIO().MousePos.x - guiStart.x), (uint32_t)(ImGui::GetIO().MousePos.y - guiStart.y), (int)ImGui::GetIO().MouseWheel);
97 MicroProfileDraw((uint32_t)guiSize.x, (uint32_t)guiSize.y);
98
99 ImGui::End();
100
101 MicroProfileFlip();
102}
103
104#else
105void Cogs::Core::profileInspector(Context* /*context*/, bool* /*showProfiler*/)
106{
107}
108#endif