Skip to content

Commit b314bc3

Browse files
committed
Add F10 hotkey for Dear ImGui developer tools
1 parent e07f05a commit b314bc3

7 files changed

Lines changed: 109 additions & 26 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ endif ()
99

1010
if (MSVC)
1111
add_compile_options("/Zi")
12-
add_link_options("/DEBUG")
12+
add_link_options("/DEBUG:FULL")
1313
endif ()
1414

1515
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

engine/render.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,7 @@ class r_IRenderer {
9191
virtual int VirtualScreenHeight() = 0;
9292
virtual float VirtualScreenScaleFactor() = 0;
9393
virtual int VirtualMap(int properValue) = 0;
94-
virtual int VirtualUnmap(int mappedValue) =0;
94+
virtual int VirtualUnmap(int mappedValue) = 0;
95+
96+
virtual void ToggleDebugImGui() = 0;
9597
};

engine/render/r_main.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ void r_layer_c::Render()
329329
}
330330
return std::less<r_tex_c const*>{}(tex, rhs.tex);
331331
}
332+
333+
bool operator == (BatchKey const& rhs) const {
334+
return !(*this < rhs) && !(rhs < *this);
335+
}
332336
};
333337

334338
if (renderer->glPushGroupMarkerEXT)
@@ -873,6 +877,23 @@ static int layerCompFunc(const void* va, const void* vb)
873877

874878
void r_renderer_c::EndFrame()
875879
{
880+
static bool showDemo = false;
881+
if (debugImGui) {
882+
if (ImGui::Begin("Debug Hub", &debugImGui)) {
883+
if (ImGui::Button("Demo")) {
884+
showDemo = true;
885+
}
886+
if (ImGui::Button("Layers")) {
887+
debugLayers = true;
888+
}
889+
}
890+
ImGui::End();
891+
}
892+
893+
if (showDemo) {
894+
ImGui::ShowDemoWindow(&showDemo);
895+
}
896+
876897
r_layer_c** layerSort = new r_layer_c * [numLayer];
877898
for (int l = 0; l < numLayer; l++) {
878899
layerSort[l] = layerList[l];
@@ -896,6 +917,40 @@ void r_renderer_c::EndFrame()
896917
DrawImage(NULL, (float)VirtualScreenWidth() - w, VirtualScreenHeight() - 16.0f, w, 16);
897918
DrawStringFormat(0, VirtualScreenHeight() - 16.0f, F_RIGHT, 16, colorWhite, F_FIXED, str);
898919
}
920+
if (debugLayers) {
921+
if (ImGui::Begin("Layers", &debugLayers)) {
922+
ImGui::Text("Layers: %d", numLayer);
923+
int curOpt = r_layerOptimize->intVal;
924+
if (ImGui::SliderInt("Optimization", &curOpt, r_layerOptimize->min, r_layerOptimize->max, "%d", ImGuiSliderFlags_AlwaysClamp | ImGuiSliderFlags_NoInput)) {
925+
if (curOpt != r_layerOptimize->intVal) {
926+
r_layerOptimize->Set(curOpt);
927+
}
928+
}
929+
int totalCmd{};
930+
if (ImGui::BeginTable("Layer stats", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit)) {
931+
ImGui::TableSetupColumn("Index");
932+
ImGui::TableSetupColumn("Layer");
933+
ImGui::TableSetupColumn("Sublayer");
934+
ImGui::TableSetupColumn("Command count");
935+
ImGui::TableHeadersRow();
936+
for (int l = 0; l < numLayer; ++l) {
937+
auto layer = layerSort[l];
938+
totalCmd += layer->numCmd;
939+
ImGui::TableNextRow();
940+
ImGui::TableNextColumn();
941+
ImGui::Text("%d", l);
942+
ImGui::TableNextColumn();
943+
ImGui::Text("%d", layer->layer);
944+
ImGui::TableNextColumn();
945+
ImGui::Text("%d", layer->subLayer);
946+
ImGui::TableNextColumn();
947+
ImGui::Text("%d", layer->numCmd);
948+
}
949+
ImGui::EndTable();
950+
}
951+
}
952+
ImGui::End();
953+
}
899954
for (int l = 0; l < numLayer; l++) {
900955
layerSort[l]->Render();
901956
}
@@ -1238,6 +1293,14 @@ int r_renderer_c::VirtualUnmap(int mappedValue) {
12381293
return (int)(mappedValue * VirtualScreenScaleFactor());
12391294
}
12401295

1296+
// =====
1297+
// Debug
1298+
// =====
1299+
1300+
void r_renderer_c::ToggleDebugImGui() {
1301+
debugImGui = !debugImGui;
1302+
}
1303+
12411304
// ===========
12421305
// Screenshots
12431306
// ===========

engine/render/r_main.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
8888
int VirtualMap(int properValue);
8989
int VirtualUnmap(int mappedValue);
9090

91+
void ToggleDebugImGui();
92+
9193
// Encapsulated
9294
r_renderer_c(sys_IMain* sysHnd);
9395

@@ -154,6 +156,9 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
154156

155157
RenderTarget rttMain;
156158

159+
bool debugImGui = true;
160+
bool debugLayers = true;
161+
157162
int takeScreenshot = 0;
158163
void DoScreenshot(image_c* i, const char* ext);
159164

engine/system/win/sys_video.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ int sys_video_c::Apply(sys_vidSet_s* set)
245245
});
246246
glfwSetCursorPosCallback(wnd, [](GLFWwindow* wnd, double x, double y) {
247247
auto sys = (sys_main_c*)glfwGetWindowUserPointer(wnd);
248+
if (ImGui::GetIO().WantCaptureMouse) {
249+
return;
250+
}
248251
auto video = (sys_video_c*)sys->video;
249252
video->lastCursorPos = CursorPos{(int)x, (int)y};
250253
});
@@ -353,6 +356,9 @@ int sys_video_c::Apply(sys_vidSet_s* set)
353356
});
354357
glfwSetScrollCallback(wnd, [](GLFWwindow* wnd, double xoffset, double yoffset) {
355358
auto sys = (sys_main_c*)glfwGetWindowUserPointer(wnd);
359+
if (ImGui::GetIO().WantCaptureMouse) {
360+
return;
361+
}
356362
if (yoffset > 0) {
357363
sys->core->KeyEvent(KEY_MWHEELUP, KE_KEYDOWN);
358364
sys->core->KeyEvent(KEY_MWHEELUP, KE_KEYUP);

ui_debug.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
// =======
1212

1313
struct d_lineHit_s {
14-
char* source;
15-
char* name;
14+
char* source;
15+
char* name;
1616
int line;
1717
int count;
1818
};
1919

2020
struct d_callHit_s {
21-
char* source;
22-
char* name;
21+
char* source;
22+
char* name;
2323
int count;
2424
int lineHitNum;
2525
int lineHitSz;
@@ -30,11 +30,11 @@ struct d_callHit_s {
3030
// ui_IDebug Interface
3131
// ===================
3232

33-
class ui_debug_c: public ui_IDebug, public thread_c {
33+
class ui_debug_c : public ui_IDebug, public thread_c {
3434
public:
3535
// Interface
36-
void SetProfiling(bool enable);
37-
void ToggleProfiling();
36+
void SetProfiling(bool enable) override;
37+
void ToggleProfiling() override;
3838

3939
// Encapsulated
4040
ui_debug_c(ui_main_c* ui);
@@ -119,7 +119,7 @@ static ui_debug_c* GetDebugPtr(lua_State* L)
119119
return (ui_debug_c*)ui->debug;
120120
}
121121

122-
static void debugHook(lua_State* L, lua_Debug *ar)
122+
static void debugHook(lua_State* L, lua_Debug* ar)
123123
{
124124
ui_debug_c* d = GetDebugPtr(L);
125125
d->hookHolding = true;
@@ -133,8 +133,9 @@ static int lineComp(const void* aVoid, const void* bVoid)
133133
d_lineHit_s* b = (d_lineHit_s*)bVoid;
134134
if (a->count == b->count) {
135135
return 0;
136-
} else {
137-
return a->count > b->count? -1 : 1;
136+
}
137+
else {
138+
return a->count > b->count ? -1 : 1;
138139
}
139140
}
140141

@@ -144,8 +145,9 @@ static int callComp(const void* aVoid, const void* bVoid)
144145
d_callHit_s* b = (d_callHit_s*)bVoid;
145146
if (a->count == b->count) {
146147
return 0;
147-
} else {
148-
return a->count > b->count? -1 : 1;
148+
}
149+
else {
150+
return a->count > b->count ? -1 : 1;
149151
}
150152
}
151153

@@ -156,14 +158,14 @@ void ui_debug_c::ThreadProc()
156158
ui->sys->Sleep(1);
157159

158160
if (profiling) {
159-
if ( !ui->inLua ) {
161+
if (!ui->inLua) {
160162
continue;
161163
}
162164
hookHold = true;
163165
lua_sethook(ui->L, &debugHook, LUA_MASKLINE, 0);
164166
while (profiling && !hookHolding);
165167
lua_sethook(ui->L, &debugHook, 0, 0);
166-
if ( !profiling ) {
168+
if (!profiling) {
167169
hookHold = false;
168170
continue;
169171
}
@@ -182,7 +184,7 @@ void ui_debug_c::ThreadProc()
182184
}
183185
if (l == lineHitNum) {
184186
if (lineHitNum == lineHitSz) {
185-
lineHitSz<<= 1;
187+
lineHitSz <<= 1;
186188
trealloc(lineHits, lineHitSz);
187189
}
188190
lineHits[l].source = AllocString(dbg.source);
@@ -196,14 +198,14 @@ void ui_debug_c::ThreadProc()
196198
if (funcName && lua_getstack(ui->L, 1, &dbg) && lua_getinfo(ui->L, "Sln", &dbg) && dbg.source) {
197199
int c;
198200
for (c = 0; c < callHitNum; c++) {
199-
if ( !strcmp(funcSource, callHits[c].source) && !strcmp(funcName, callHits[c].name) ) {
201+
if (!strcmp(funcSource, callHits[c].source) && !strcmp(funcName, callHits[c].name)) {
200202
callHits[c].count++;
201203
break;
202204
}
203205
}
204206
if (c == callHitNum) {
205207
if (callHitNum == callHitSz) {
206-
callHitSz<<= 1;
208+
callHitSz <<= 1;
207209
trealloc(callHits, callHitSz);
208210
}
209211
if (callHitNum == callHitInitCount) {
@@ -230,7 +232,7 @@ void ui_debug_c::ThreadProc()
230232
}
231233
if (l == call->lineHitNum) {
232234
if (call->lineHitNum == call->lineHitSz) {
233-
call->lineHitSz<<= 1;
235+
call->lineHitSz <<= 1;
234236
trealloc(call->lineHits, call->lineHitSz);
235237
}
236238
call->lineHits[l].source = AllocString(dbg.source);
@@ -243,12 +245,13 @@ void ui_debug_c::ThreadProc()
243245
}
244246
hookHold = false;
245247
while (hookHolding);
246-
} else if (lineHitNum) {
248+
}
249+
else if (lineHitNum) {
247250
ui->sys->con->Printf("Hot lines:\n");
248251
qsort(lineHits, lineHitNum, sizeof(d_lineHit_s), lineComp);
249252
for (int l = 0; l < lineHitNum; l++) {
250253
if (l < 20) {
251-
ui->sys->con->Printf("%s(%d) in '%s': %d\n", lineHits[l].source, lineHits[l].line, lineHits[l].name? lineHits[l].name : "?", lineHits[l].count);
254+
ui->sys->con->Printf("%s(%d) in '%s': %d\n", lineHits[l].source, lineHits[l].line, lineHits[l].name ? lineHits[l].name : "?", lineHits[l].count);
252255
}
253256
delete lineHits[l].source;
254257
delete lineHits[l].name;
@@ -263,7 +266,7 @@ void ui_debug_c::ThreadProc()
263266
}
264267
for (int l = 0; l < callHits[c].lineHitNum; l++) {
265268
if (c < 10 && l < 5) {
266-
ui->sys->con->Printf("\t%s(%d) in '%s': %d\n", callHits[c].lineHits[l].source, callHits[c].lineHits[l].line, callHits[c].lineHits[l].name? callHits[c].lineHits[l].name : "?", callHits[c].lineHits[l].count);
269+
ui->sys->con->Printf("\t%s(%d) in '%s': %d\n", callHits[c].lineHits[l].source, callHits[c].lineHits[l].line, callHits[c].lineHits[l].name ? callHits[c].lineHits[l].name : "?", callHits[c].lineHits[l].count);
267270
}
268271
delete callHits[c].lineHits[l].source;
269272
delete callHits[c].lineHits[l].name;
@@ -282,7 +285,8 @@ void ui_debug_c::SetProfiling(bool enable)
282285
if (enable) {
283286
ui->sys->con->Printf("Profiling enabled.\n");
284287
profiling = true;
285-
} else {
288+
}
289+
else {
286290
ui->sys->con->Printf("Profiling finished:\n");
287291
profiling = false;
288292
while (lineHitNum || callHitNum);
@@ -291,5 +295,5 @@ void ui_debug_c::SetProfiling(bool enable)
291295

292296
void ui_debug_c::ToggleProfiling()
293297
{
294-
SetProfiling( !profiling );
295-
}
298+
SetProfiling(!profiling);
299+
}

ui_main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ void ui_main_c::KeyEvent(int key, int type)
503503
case KEY_PRINTSCRN:
504504
sys->con->Execute("screenshot");
505505
break;
506+
case KEY_F10:
507+
renderer->ToggleDebugImGui();
508+
break;
506509
case KEY_PAUSE:
507510
if (sys->IsKeyDown(KEY_SHIFT)) {
508511
debug->ToggleProfiling();

0 commit comments

Comments
 (0)