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