editor_visual_profiler.cpp 25 KB

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