Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/debugger/editor_visual_profiler.cpp
20886 views
1
/**************************************************************************/
2
/* editor_visual_profiler.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "editor_visual_profiler.h"
32
33
#include "core/io/image.h"
34
#include "core/string/translation_server.h"
35
#include "editor/editor_string_names.h"
36
#include "editor/run/editor_run_bar.h"
37
#include "editor/settings/editor_settings.h"
38
#include "editor/themes/editor_scale.h"
39
#include "scene/gui/flow_container.h"
40
#include "scene/resources/image_texture.h"
41
42
void EditorVisualProfiler::set_hardware_info(const String &p_cpu_name, const String &p_gpu_name) {
43
cpu_name = p_cpu_name;
44
gpu_name = p_gpu_name;
45
queue_redraw();
46
}
47
48
void EditorVisualProfiler::add_frame_metric(const Metric &p_metric) {
49
++last_metric;
50
if (last_metric >= frame_metrics.size()) {
51
last_metric = 0;
52
}
53
54
frame_metrics.write[last_metric] = p_metric;
55
56
List<String> stack;
57
for (int i = 0; i < frame_metrics[last_metric].areas.size(); i++) {
58
String name = frame_metrics[last_metric].areas[i].name;
59
frame_metrics.write[last_metric].areas.write[i].color_cache = _get_color_from_signature(name);
60
String full_name;
61
62
if (name[0] == '<') {
63
stack.pop_back();
64
}
65
66
if (stack.size()) {
67
full_name = stack.back()->get() + name;
68
} else {
69
full_name = name;
70
}
71
72
if (name[0] == '>') {
73
stack.push_back(full_name + "/");
74
}
75
76
frame_metrics.write[last_metric].areas.write[i].fullpath_cache = full_name;
77
}
78
79
updating_frame = true;
80
clear_button->set_disabled(false);
81
cursor_metric_edit->set_max(frame_metrics[last_metric].frame_number);
82
cursor_metric_edit->set_min(MAX(int64_t(frame_metrics[last_metric].frame_number) - frame_metrics.size(), 0));
83
84
if (!seeking) {
85
cursor_metric_edit->set_value(frame_metrics[last_metric].frame_number);
86
if (hover_metric != -1) {
87
hover_metric++;
88
if (hover_metric >= frame_metrics.size()) {
89
hover_metric = 0;
90
}
91
}
92
}
93
updating_frame = false;
94
95
if (frame_delay->is_stopped()) {
96
frame_delay->set_wait_time(0.1);
97
frame_delay->start();
98
}
99
100
if (plot_delay->is_stopped()) {
101
plot_delay->set_wait_time(0.1);
102
plot_delay->start();
103
}
104
}
105
106
void EditorVisualProfiler::clear() {
107
int metric_size = EDITOR_GET("debugger/profiler_frame_history_size");
108
metric_size = CLAMP(metric_size, 60, 10000);
109
frame_metrics.clear();
110
frame_metrics.resize(metric_size);
111
last_metric = -1;
112
variables->clear();
113
//activate->set_pressed(false);
114
115
graph_limit = 1000.0f / CLAMP(int(EDITOR_GET("debugger/profiler_target_fps")), 1, 1000);
116
117
updating_frame = true;
118
cursor_metric_edit->set_min(0);
119
cursor_metric_edit->set_max(0);
120
cursor_metric_edit->set_value(0);
121
updating_frame = false;
122
hover_metric = -1;
123
seeking = false;
124
}
125
126
String EditorVisualProfiler::_get_time_as_text(float p_time) {
127
const String &lang = _get_locale();
128
129
int dmode = display_mode->get_selected();
130
131
if (dmode == DISPLAY_FRAME_TIME) {
132
return TranslationServer::get_singleton()->format_number(String::num(p_time, 2), lang) + " " + TTR("ms");
133
} else if (dmode == DISPLAY_FRAME_PERCENT) {
134
return TranslationServer::get_singleton()->format_number(String::num(p_time * 100 / graph_limit, 2), lang) + " " + TranslationServer::get_singleton()->get_percent_sign(lang);
135
}
136
137
return "err";
138
}
139
140
Color EditorVisualProfiler::_get_color_from_signature(const StringName &p_signature) const {
141
Color bc = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
142
double rot = Math::abs(double(p_signature.hash()) / double(0x7FFFFFFF));
143
Color c;
144
c.set_hsv(rot, bc.get_s(), bc.get_v());
145
return c.lerp(get_theme_color(SNAME("base_color"), EditorStringName(Editor)), 0.07);
146
}
147
148
void EditorVisualProfiler::_item_selected() {
149
if (updating_frame) {
150
return;
151
}
152
153
TreeItem *item = variables->get_selected();
154
if (!item) {
155
return;
156
}
157
selected_area = item->get_metadata(0);
158
_update_plot();
159
}
160
161
void EditorVisualProfiler::_update_plot() {
162
const int w = graph->get_size().width + 1; // `+1` is to prevent from crashing when visual profiler is auto started.
163
const int h = graph->get_size().height + 1;
164
165
bool reset_texture = false;
166
167
const int desired_len = w * h * 4;
168
169
if (graph_image.size() != desired_len) {
170
reset_texture = true;
171
graph_image.resize(desired_len);
172
}
173
174
uint8_t *wr = graph_image.ptrw();
175
const Color background_color = get_theme_color("dark_color_2", EditorStringName(Editor));
176
177
// Clear the previous frame and set the background color.
178
for (int i = 0; i < desired_len; i += 4) {
179
wr[i + 0] = Math::fast_ftoi(background_color.r * 255);
180
wr[i + 1] = Math::fast_ftoi(background_color.g * 255);
181
wr[i + 2] = Math::fast_ftoi(background_color.b * 255);
182
wr[i + 3] = 255;
183
}
184
185
//find highest value
186
187
float highest_cpu = 0;
188
float highest_gpu = 0;
189
190
for (int i = 0; i < frame_metrics.size(); i++) {
191
const Metric &m = frame_metrics[i];
192
if (!m.valid) {
193
continue;
194
}
195
196
if (m.areas.size()) {
197
highest_cpu = MAX(highest_cpu, m.areas[m.areas.size() - 1].cpu_time);
198
highest_gpu = MAX(highest_gpu, m.areas[m.areas.size() - 1].gpu_time);
199
}
200
}
201
202
if (highest_cpu > 0 || highest_gpu > 0) {
203
if (frame_relative->is_pressed()) {
204
highest_cpu = MAX(graph_limit, highest_cpu);
205
highest_gpu = MAX(graph_limit, highest_gpu);
206
}
207
208
if (linked->is_pressed()) {
209
float highest = MAX(highest_cpu, highest_gpu);
210
highest_cpu = highest_gpu = highest;
211
}
212
213
//means some data exists..
214
highest_cpu *= 1.2; //leave some upper room
215
highest_gpu *= 1.2; //leave some upper room
216
graph_height_cpu = highest_cpu;
217
graph_height_gpu = highest_gpu;
218
219
Vector<Color> columnv_cpu;
220
columnv_cpu.resize(h);
221
Color *column_cpu = columnv_cpu.ptrw();
222
223
Vector<Color> columnv_gpu;
224
columnv_gpu.resize(h);
225
Color *column_gpu = columnv_gpu.ptrw();
226
227
int half_w = w / 2;
228
for (int i = 0; i < half_w; i++) {
229
for (int j = 0; j < h; j++) {
230
column_cpu[j] = Color(0, 0, 0, 0);
231
column_gpu[j] = Color(0, 0, 0, 0);
232
}
233
234
int current = i * frame_metrics.size() / half_w;
235
int next = (i + 1) * frame_metrics.size() / half_w;
236
if (next > frame_metrics.size()) {
237
next = frame_metrics.size();
238
}
239
if (next == current) {
240
next = current + 1; //just because for loop must work
241
}
242
243
for (int j = current; j < next; j++) {
244
//wrap
245
int idx = last_metric + 1 + j;
246
while (idx >= frame_metrics.size()) {
247
idx -= frame_metrics.size();
248
}
249
250
int area_count = frame_metrics[idx].areas.size();
251
const Metric::Area *areas = frame_metrics[idx].areas.ptr();
252
int prev_cpu = 0;
253
int prev_gpu = 0;
254
for (int k = 1; k < area_count; k++) {
255
int ofs_cpu = int(areas[k].cpu_time * h / highest_cpu);
256
ofs_cpu = CLAMP(ofs_cpu, 0, h - 1);
257
Color color = selected_area == areas[k - 1].fullpath_cache ? Color(1, 1, 1, 1) : areas[k - 1].color_cache;
258
259
for (int l = prev_cpu; l < ofs_cpu; l++) {
260
column_cpu[h - l - 1] += color;
261
}
262
prev_cpu = ofs_cpu;
263
264
int ofs_gpu = int(areas[k].gpu_time * h / highest_gpu);
265
ofs_gpu = CLAMP(ofs_gpu, 0, h - 1);
266
for (int l = prev_gpu; l < ofs_gpu; l++) {
267
column_gpu[h - l - 1] += color;
268
}
269
270
prev_gpu = ofs_gpu;
271
}
272
}
273
274
//plot CPU
275
for (int j = 0; j < h; j++) {
276
uint8_t r, g, b;
277
278
if (column_cpu[j].a == 0) {
279
r = Math::fast_ftoi(background_color.r * 255);
280
g = Math::fast_ftoi(background_color.g * 255);
281
b = Math::fast_ftoi(background_color.b * 255);
282
} else {
283
r = CLAMP((column_cpu[j].r / column_cpu[j].a) * 255.0, 0, 255);
284
g = CLAMP((column_cpu[j].g / column_cpu[j].a) * 255.0, 0, 255);
285
b = CLAMP((column_cpu[j].b / column_cpu[j].a) * 255.0, 0, 255);
286
}
287
288
int widx = (j * w + i) * 4;
289
wr[widx + 0] = r;
290
wr[widx + 1] = g;
291
wr[widx + 2] = b;
292
wr[widx + 3] = 255;
293
}
294
//plot GPU
295
for (int j = 0; j < h; j++) {
296
uint8_t r, g, b;
297
298
if (column_gpu[j].a == 0) {
299
r = Math::fast_ftoi(background_color.r * 255);
300
g = Math::fast_ftoi(background_color.g * 255);
301
b = Math::fast_ftoi(background_color.b * 255);
302
} else {
303
r = CLAMP((column_gpu[j].r / column_gpu[j].a) * 255.0, 0, 255);
304
g = CLAMP((column_gpu[j].g / column_gpu[j].a) * 255.0, 0, 255);
305
b = CLAMP((column_gpu[j].b / column_gpu[j].a) * 255.0, 0, 255);
306
}
307
308
int widx = (j * w + w / 2 + i) * 4;
309
wr[widx + 0] = r;
310
wr[widx + 1] = g;
311
wr[widx + 2] = b;
312
wr[widx + 3] = 255;
313
}
314
}
315
}
316
317
Ref<Image> img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, graph_image);
318
319
if (reset_texture) {
320
if (graph_texture.is_null()) {
321
graph_texture.instantiate();
322
}
323
graph_texture->set_image(img);
324
}
325
326
graph_texture->update(img);
327
328
graph->set_texture(graph_texture);
329
graph->queue_redraw();
330
}
331
332
void EditorVisualProfiler::_update_frame(bool p_focus_selected) {
333
int cursor_metric = _get_cursor_index();
334
335
Ref<Texture> track_icon = get_editor_theme_icon(SNAME("TrackColor"));
336
337
ERR_FAIL_INDEX(cursor_metric, frame_metrics.size());
338
339
updating_frame = true;
340
variables->clear();
341
342
TreeItem *root = variables->create_item();
343
const Metric &m = frame_metrics[cursor_metric];
344
345
List<TreeItem *> stack;
346
List<TreeItem *> categories;
347
348
TreeItem *ensure_selected = nullptr;
349
350
for (int i = 1; i < m.areas.size() - 1; i++) {
351
TreeItem *parent = stack.size() ? stack.back()->get() : root;
352
353
String name = m.areas[i].name;
354
355
float cpu_time = m.areas[i].cpu_time;
356
float gpu_time = m.areas[i].gpu_time;
357
if (i < m.areas.size() - 1) {
358
cpu_time = m.areas[i + 1].cpu_time - cpu_time;
359
gpu_time = m.areas[i + 1].gpu_time - gpu_time;
360
}
361
362
if (name.begins_with(">")) {
363
TreeItem *category = variables->create_item(parent);
364
365
stack.push_back(category);
366
categories.push_back(category);
367
368
name = name.substr(1);
369
370
category->set_text(0, name);
371
category->set_metadata(1, cpu_time);
372
category->set_metadata(2, gpu_time);
373
continue;
374
}
375
376
if (name.begins_with("<")) {
377
stack.pop_back();
378
continue;
379
}
380
TreeItem *category = variables->create_item(parent);
381
382
for (TreeItem *E : stack) {
383
float total_cpu = E->get_metadata(1);
384
float total_gpu = E->get_metadata(2);
385
total_cpu += cpu_time;
386
total_gpu += gpu_time;
387
E->set_metadata(1, total_cpu);
388
E->set_metadata(2, total_gpu);
389
}
390
391
category->set_icon(0, track_icon);
392
category->set_icon_modulate(0, m.areas[i].color_cache);
393
category->set_selectable(0, true);
394
category->set_metadata(0, m.areas[i].fullpath_cache);
395
category->set_text(0, m.areas[i].name);
396
category->set_text(1, _get_time_as_text(cpu_time));
397
category->set_metadata(1, m.areas[i].cpu_time);
398
category->set_text(2, _get_time_as_text(gpu_time));
399
category->set_metadata(2, m.areas[i].gpu_time);
400
401
if (selected_area == m.areas[i].fullpath_cache) {
402
category->select(0);
403
if (p_focus_selected) {
404
ensure_selected = category;
405
}
406
}
407
}
408
409
for (TreeItem *E : categories) {
410
float total_cpu = E->get_metadata(1);
411
float total_gpu = E->get_metadata(2);
412
E->set_text(1, _get_time_as_text(total_cpu));
413
E->set_text(2, _get_time_as_text(total_gpu));
414
}
415
416
if (ensure_selected) {
417
variables->ensure_cursor_is_visible();
418
}
419
updating_frame = false;
420
}
421
422
void EditorVisualProfiler::_activate_pressed() {
423
if (activate->is_pressed()) {
424
activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
425
activate->set_text(TTRC("Stop"));
426
_clear_pressed(); //always clear on start
427
clear_button->set_disabled(false);
428
} else {
429
activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));
430
activate->set_text(TTRC("Start"));
431
}
432
emit_signal(SNAME("enable_profiling"), activate->is_pressed());
433
}
434
435
void EditorVisualProfiler::_clear_pressed() {
436
clear_button->set_disabled(true);
437
clear();
438
_update_plot();
439
}
440
441
void EditorVisualProfiler::_autostart_toggled(bool p_toggled_on) {
442
EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_visual_profiler", p_toggled_on);
443
EditorRunBar::get_singleton()->update_profiler_autostart_indicator();
444
}
445
446
void EditorVisualProfiler::_notification(int p_what) {
447
switch (p_what) {
448
case NOTIFICATION_TRANSLATION_CHANGED: {
449
if (is_ready()) {
450
_update_frame();
451
}
452
[[fallthrough]];
453
}
454
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
455
case NOTIFICATION_THEME_CHANGED: {
456
activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));
457
clear_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));
458
} break;
459
}
460
}
461
462
void EditorVisualProfiler::_graph_tex_draw() {
463
if (last_metric < 0) {
464
return;
465
}
466
467
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
468
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
469
const Color color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
470
471
if (seeking) {
472
int max_frames = frame_metrics.size();
473
int frame = cursor_metric_edit->get_value() - (frame_metrics[last_metric].frame_number - max_frames + 1);
474
if (frame < 0) {
475
frame = 0;
476
}
477
478
int half_width = graph->get_size().x / 2;
479
int cur_x = frame * half_width / max_frames;
480
481
graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), color * Color(1, 1, 1));
482
graph->draw_line(Vector2(cur_x + half_width, 0), Vector2(cur_x + half_width, graph->get_size().y), color * Color(1, 1, 1));
483
}
484
485
if (graph_height_cpu > 0) {
486
int frame_y = graph->get_size().y - graph_limit * graph->get_size().y / graph_height_cpu - 1;
487
488
int half_width = graph->get_size().x / 2;
489
490
graph->draw_line(Vector2(0, frame_y), Vector2(half_width, frame_y), color * Color(1, 1, 1, 0.5));
491
492
const String limit_str = String::num(graph_limit, 2) + " ms";
493
graph->draw_string(font, Vector2(half_width - font->get_string_size(limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x - 2, frame_y - 2), limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));
494
}
495
496
if (graph_height_gpu > 0) {
497
int frame_y = graph->get_size().y - graph_limit * graph->get_size().y / graph_height_gpu - 1;
498
499
int half_width = graph->get_size().x / 2;
500
501
graph->draw_line(Vector2(half_width, frame_y), Vector2(graph->get_size().x, frame_y), color * Color(1, 1, 1, 0.5));
502
503
const String limit_str = String::num(graph_limit, 2) + " ms";
504
graph->draw_string(font, Vector2(half_width * 2 - font->get_string_size(limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x - 2, frame_y - 2), limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));
505
}
506
507
graph->draw_string(font, Vector2(font->get_string_size("X", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x, font->get_ascent(font_size) + 2), "CPU: " + cpu_name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));
508
graph->draw_string(font, Vector2(font->get_string_size("X", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x + graph->get_size().width / 2, font->get_ascent(font_size) + 2), "GPU: " + gpu_name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));
509
}
510
511
void EditorVisualProfiler::_graph_tex_mouse_exit() {
512
hover_metric = -1;
513
graph->queue_redraw();
514
}
515
516
void EditorVisualProfiler::_cursor_metric_changed(double) {
517
if (updating_frame) {
518
return;
519
}
520
521
graph->queue_redraw();
522
_update_frame();
523
}
524
525
void EditorVisualProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {
526
if (last_metric < 0) {
527
return;
528
}
529
530
Ref<InputEventMouse> me = p_ev;
531
Ref<InputEventMouseButton> mb = p_ev;
532
Ref<InputEventMouseMotion> mm = p_ev;
533
534
if (
535
(mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) ||
536
(mm.is_valid())) {
537
int half_w = graph->get_size().width / 2;
538
int x = me->get_position().x;
539
if (x > half_w) {
540
x -= half_w;
541
}
542
x = x * frame_metrics.size() / half_w;
543
544
bool show_hover = x >= 0 && x < frame_metrics.size();
545
546
if (x < 0) {
547
x = 0;
548
}
549
550
if (x >= frame_metrics.size()) {
551
x = frame_metrics.size() - 1;
552
}
553
554
int metric = frame_metrics.size() - x - 1;
555
metric = last_metric - metric;
556
while (metric < 0) {
557
metric += frame_metrics.size();
558
}
559
560
if (show_hover) {
561
hover_metric = metric;
562
563
} else {
564
hover_metric = -1;
565
}
566
567
if (mb.is_valid() || mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
568
//cursor_metric=x;
569
updating_frame = true;
570
571
//metric may be invalid, so look for closest metric that is valid, this makes snap feel better
572
bool valid = false;
573
for (int i = 0; i < frame_metrics.size(); i++) {
574
if (frame_metrics[metric].valid) {
575
valid = true;
576
break;
577
}
578
579
metric++;
580
if (metric >= frame_metrics.size()) {
581
metric = 0;
582
}
583
}
584
585
if (!valid) {
586
return;
587
}
588
589
cursor_metric_edit->set_value(frame_metrics[metric].frame_number);
590
591
updating_frame = false;
592
593
if (activate->is_pressed()) {
594
if (!seeking) {
595
// Break request is not required, just stop profiling
596
}
597
}
598
599
seeking = true;
600
601
if (!frame_delay->is_processing()) {
602
frame_delay->set_wait_time(0.1);
603
frame_delay->start();
604
}
605
606
bool touched_cpu = me->get_position().x < graph->get_size().width * 0.5;
607
608
const Metric::Area *areas = frame_metrics[metric].areas.ptr();
609
int area_count = frame_metrics[metric].areas.size();
610
float posy = (1.0 - (me->get_position().y / graph->get_size().height)) * (touched_cpu ? graph_height_cpu : graph_height_gpu);
611
int last_valid = -1;
612
bool found = false;
613
for (int i = 0; i < area_count - 1; i++) {
614
if (areas[i].name[0] != '<' && areas[i].name[0] != '>') {
615
last_valid = i;
616
}
617
float h = touched_cpu ? areas[i + 1].cpu_time : areas[i + 1].gpu_time;
618
619
if (h > posy) {
620
found = true;
621
break;
622
}
623
}
624
625
StringName area_found;
626
if (found && last_valid != -1) {
627
area_found = areas[last_valid].fullpath_cache;
628
}
629
630
if (area_found != selected_area) {
631
selected_area = area_found;
632
_update_frame(true);
633
_update_plot();
634
}
635
}
636
637
graph->queue_redraw();
638
}
639
}
640
641
int EditorVisualProfiler::_get_cursor_index() const {
642
if (last_metric < 0) {
643
return 0;
644
}
645
if (!frame_metrics[last_metric].valid) {
646
return 0;
647
}
648
649
int diff = (frame_metrics[last_metric].frame_number - cursor_metric_edit->get_value());
650
651
int idx = last_metric - diff;
652
while (idx < 0) {
653
idx += frame_metrics.size();
654
}
655
656
return idx;
657
}
658
659
void EditorVisualProfiler::disable_seeking() {
660
seeking = false;
661
graph->queue_redraw();
662
}
663
664
void EditorVisualProfiler::_combo_changed(int) {
665
_update_frame();
666
_update_plot();
667
}
668
669
void EditorVisualProfiler::_bind_methods() {
670
ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));
671
}
672
673
void EditorVisualProfiler::_update_button_text() {
674
if (activate->is_pressed()) {
675
activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
676
activate->set_text(TTRC("Stop"));
677
} else {
678
activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));
679
activate->set_text(TTRC("Start"));
680
}
681
}
682
683
void EditorVisualProfiler::set_enabled(bool p_enable) {
684
activate->set_disabled(!p_enable);
685
}
686
687
void EditorVisualProfiler::set_profiling(bool p_profiling) {
688
activate->set_pressed(p_profiling);
689
_update_button_text();
690
emit_signal(SNAME("enable_profiling"), activate->is_pressed());
691
}
692
693
bool EditorVisualProfiler::is_profiling() {
694
return activate->is_pressed();
695
}
696
697
Vector<Vector<String>> EditorVisualProfiler::get_data_as_csv() const {
698
Vector<Vector<String>> res;
699
#if 0
700
if (frame_metrics.is_empty()) {
701
return res;
702
}
703
704
// signatures
705
Vector<String> signatures;
706
const Vector<EditorFrameProfiler::Metric::Category> &categories = frame_metrics[0].categories;
707
708
for (int j = 0; j < categories.size(); j++) {
709
const EditorFrameProfiler::Metric::Category &c = categories[j];
710
signatures.push_back(c.signature);
711
712
for (int k = 0; k < c.items.size(); k++) {
713
signatures.push_back(c.items[k].signature);
714
}
715
}
716
res.push_back(signatures);
717
718
// values
719
Vector<String> values;
720
values.resize(signatures.size());
721
722
int index = last_metric;
723
724
for (int i = 0; i < frame_metrics.size(); i++) {
725
++index;
726
727
if (index >= frame_metrics.size()) {
728
index = 0;
729
}
730
731
if (!frame_metrics[index].valid) {
732
continue;
733
}
734
int it = 0;
735
const Vector<EditorFrameProfiler::Metric::Category> &frame_cat = frame_metrics[index].categories;
736
737
for (int j = 0; j < frame_cat.size(); j++) {
738
const EditorFrameProfiler::Metric::Category &c = frame_cat[j];
739
values.write[it++] = String::num_real(c.total_time);
740
741
for (int k = 0; k < c.items.size(); k++) {
742
values.write[it++] = String::num_real(c.items[k].total);
743
}
744
}
745
res.push_back(values);
746
}
747
#endif
748
return res;
749
}
750
751
EditorVisualProfiler::EditorVisualProfiler() {
752
HBoxContainer *hb = memnew(HBoxContainer);
753
hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);
754
add_child(hb);
755
756
FlowContainer *container = memnew(FlowContainer);
757
container->set_h_size_flags(SIZE_EXPAND_FILL);
758
container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);
759
container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);
760
hb->add_child(container);
761
762
activate = memnew(Button);
763
activate->set_toggle_mode(true);
764
activate->set_disabled(true);
765
activate->set_text(TTRC("Start"));
766
activate->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_activate_pressed));
767
container->add_child(activate);
768
769
clear_button = memnew(Button);
770
clear_button->set_text(TTRC("Clear"));
771
clear_button->set_disabled(true);
772
clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_clear_pressed));
773
container->add_child(clear_button);
774
775
CheckBox *autostart_checkbox = memnew(CheckBox);
776
autostart_checkbox->set_text(TTRC("Autostart"));
777
autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_visual_profiler", false));
778
autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorVisualProfiler::_autostart_toggled));
779
container->add_child(autostart_checkbox);
780
781
HBoxContainer *hb_measure = memnew(HBoxContainer);
782
hb_measure->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);
783
container->add_child(hb_measure);
784
785
hb_measure->add_child(memnew(Label(TTRC("Measure:"))));
786
787
display_mode = memnew(OptionButton);
788
display_mode->set_accessibility_name(TTRC("Measure:"));
789
display_mode->add_item(TTRC("Frame Time (ms)"));
790
display_mode->add_item(TTRC("Frame %"));
791
display_mode->connect(SceneStringName(item_selected), callable_mp(this, &EditorVisualProfiler::_combo_changed));
792
793
hb_measure->add_child(display_mode);
794
795
frame_relative = memnew(CheckBox(TTRC("Fit to Frame")));
796
frame_relative->set_pressed(true);
797
container->add_child(frame_relative);
798
frame_relative->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_update_plot));
799
linked = memnew(CheckBox(TTRC("Linked")));
800
linked->set_pressed(true);
801
container->add_child(linked);
802
linked->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_update_plot));
803
804
HBoxContainer *hb_frame = memnew(HBoxContainer);
805
hb_frame->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);
806
hb_frame->set_v_size_flags(SIZE_SHRINK_BEGIN);
807
hb->add_child(hb_frame);
808
809
hb_frame->add_child(memnew(Label(TTRC("Frame #:"))));
810
811
cursor_metric_edit = memnew(SpinBox);
812
cursor_metric_edit->set_accessibility_name(TTRC("Frame #:"));
813
cursor_metric_edit->set_h_size_flags(SIZE_FILL);
814
hb_frame->add_child(cursor_metric_edit);
815
cursor_metric_edit->connect(SceneStringName(value_changed), callable_mp(this, &EditorVisualProfiler::_cursor_metric_changed));
816
817
h_split = memnew(HSplitContainer);
818
add_child(h_split);
819
h_split->set_v_size_flags(SIZE_EXPAND_FILL);
820
821
variables = memnew(Tree);
822
variables->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
823
variables->set_hide_folding(true);
824
h_split->add_child(variables);
825
variables->set_hide_root(true);
826
variables->set_columns(3);
827
variables->set_column_titles_visible(true);
828
variables->set_column_title(0, TTRC("Name"));
829
variables->set_column_expand(0, true);
830
variables->set_column_clip_content(0, true);
831
variables->set_column_custom_minimum_width(0, 60);
832
variables->set_column_title(1, TTRC("CPU"));
833
variables->set_column_expand(1, false);
834
variables->set_column_clip_content(1, true);
835
variables->set_column_custom_minimum_width(1, 75 * EDSCALE);
836
variables->set_column_title(2, TTRC("GPU"));
837
variables->set_column_expand(2, false);
838
variables->set_column_clip_content(2, true);
839
variables->set_column_custom_minimum_width(2, 75 * EDSCALE);
840
variables->set_theme_type_variation("TreeSecondary");
841
variables->connect("cell_selected", callable_mp(this, &EditorVisualProfiler::_item_selected));
842
843
graph = memnew(TextureRect);
844
graph->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
845
graph->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
846
graph->set_mouse_filter(MOUSE_FILTER_STOP);
847
graph->connect(SceneStringName(draw), callable_mp(this, &EditorVisualProfiler::_graph_tex_draw));
848
graph->connect(SceneStringName(gui_input), callable_mp(this, &EditorVisualProfiler::_graph_tex_input));
849
graph->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorVisualProfiler::_graph_tex_mouse_exit));
850
851
h_split->add_child(graph);
852
graph->set_h_size_flags(SIZE_EXPAND_FILL);
853
854
int metric_size = CLAMP(int(EDITOR_GET("debugger/profiler_frame_history_size")), 60, 10000);
855
frame_metrics.resize(metric_size);
856
857
graph_limit = 1000.0f / CLAMP(int(EDITOR_GET("debugger/profiler_target_fps")), 1, 1000);
858
859
frame_delay = memnew(Timer);
860
frame_delay->set_wait_time(0.1);
861
frame_delay->set_one_shot(true);
862
add_child(frame_delay);
863
frame_delay->connect("timeout", callable_mp(this, &EditorVisualProfiler::_update_frame).bind(false));
864
865
plot_delay = memnew(Timer);
866
plot_delay->set_wait_time(0.1);
867
plot_delay->set_one_shot(true);
868
add_child(plot_delay);
869
plot_delay->connect("timeout", callable_mp(this, &EditorVisualProfiler::_update_plot));
870
}
871
872