editor_visual_profiler.cpp 24 KB

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