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