performance.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /**************************************************************************/
  2. /* performance.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 "performance.h"
  31. #include "core/os/os.h"
  32. #include "core/variant/typed_array.h"
  33. #include "scene/main/node.h"
  34. #include "scene/main/scene_tree.h"
  35. #include "servers/audio_server.h"
  36. #ifndef NAVIGATION_2D_DISABLED
  37. #include "servers/navigation_server_2d.h"
  38. #endif // NAVIGATION_2D_DISABLED
  39. #ifndef NAVIGATION_3D_DISABLED
  40. #include "servers/navigation_server_3d.h"
  41. #endif // NAVIGATION_3D_DISABLED
  42. #include "servers/rendering_server.h"
  43. #ifndef PHYSICS_2D_DISABLED
  44. #include "servers/physics_server_2d.h"
  45. #endif // PHYSICS_2D_DISABLED
  46. #ifndef PHYSICS_3D_DISABLED
  47. #include "servers/physics_server_3d.h"
  48. #endif // PHYSICS_3D_DISABLED
  49. Performance *Performance::singleton = nullptr;
  50. void Performance::_bind_methods() {
  51. ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
  52. ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
  53. ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
  54. ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
  55. ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
  56. ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
  57. ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
  58. BIND_ENUM_CONSTANT(TIME_FPS);
  59. BIND_ENUM_CONSTANT(TIME_PROCESS);
  60. BIND_ENUM_CONSTANT(TIME_PHYSICS_PROCESS);
  61. BIND_ENUM_CONSTANT(TIME_NAVIGATION_PROCESS);
  62. BIND_ENUM_CONSTANT(MEMORY_STATIC);
  63. BIND_ENUM_CONSTANT(MEMORY_STATIC_MAX);
  64. BIND_ENUM_CONSTANT(MEMORY_MESSAGE_BUFFER_MAX);
  65. BIND_ENUM_CONSTANT(OBJECT_COUNT);
  66. BIND_ENUM_CONSTANT(OBJECT_RESOURCE_COUNT);
  67. BIND_ENUM_CONSTANT(OBJECT_NODE_COUNT);
  68. BIND_ENUM_CONSTANT(OBJECT_ORPHAN_NODE_COUNT);
  69. BIND_ENUM_CONSTANT(RENDER_TOTAL_OBJECTS_IN_FRAME);
  70. BIND_ENUM_CONSTANT(RENDER_TOTAL_PRIMITIVES_IN_FRAME);
  71. BIND_ENUM_CONSTANT(RENDER_TOTAL_DRAW_CALLS_IN_FRAME);
  72. BIND_ENUM_CONSTANT(RENDER_VIDEO_MEM_USED);
  73. BIND_ENUM_CONSTANT(RENDER_TEXTURE_MEM_USED);
  74. BIND_ENUM_CONSTANT(RENDER_BUFFER_MEM_USED);
  75. BIND_ENUM_CONSTANT(PHYSICS_2D_ACTIVE_OBJECTS);
  76. BIND_ENUM_CONSTANT(PHYSICS_2D_COLLISION_PAIRS);
  77. BIND_ENUM_CONSTANT(PHYSICS_2D_ISLAND_COUNT);
  78. #ifndef _3D_DISABLED
  79. BIND_ENUM_CONSTANT(PHYSICS_3D_ACTIVE_OBJECTS);
  80. BIND_ENUM_CONSTANT(PHYSICS_3D_COLLISION_PAIRS);
  81. BIND_ENUM_CONSTANT(PHYSICS_3D_ISLAND_COUNT);
  82. #endif // _3D_DISABLED
  83. BIND_ENUM_CONSTANT(AUDIO_OUTPUT_LATENCY);
  84. #if !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
  85. BIND_ENUM_CONSTANT(NAVIGATION_ACTIVE_MAPS);
  86. BIND_ENUM_CONSTANT(NAVIGATION_REGION_COUNT);
  87. BIND_ENUM_CONSTANT(NAVIGATION_AGENT_COUNT);
  88. BIND_ENUM_CONSTANT(NAVIGATION_LINK_COUNT);
  89. BIND_ENUM_CONSTANT(NAVIGATION_POLYGON_COUNT);
  90. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_COUNT);
  91. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_MERGE_COUNT);
  92. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_CONNECTION_COUNT);
  93. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_FREE_COUNT);
  94. BIND_ENUM_CONSTANT(NAVIGATION_OBSTACLE_COUNT);
  95. #endif // !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
  96. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_CANVAS);
  97. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_MESH);
  98. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_SURFACE);
  99. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_DRAW);
  100. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_SPECIALIZATION);
  101. #ifndef NAVIGATION_2D_DISABLED
  102. BIND_ENUM_CONSTANT(NAVIGATION_2D_ACTIVE_MAPS);
  103. BIND_ENUM_CONSTANT(NAVIGATION_2D_REGION_COUNT);
  104. BIND_ENUM_CONSTANT(NAVIGATION_2D_AGENT_COUNT);
  105. BIND_ENUM_CONSTANT(NAVIGATION_2D_LINK_COUNT);
  106. BIND_ENUM_CONSTANT(NAVIGATION_2D_POLYGON_COUNT);
  107. BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_COUNT);
  108. BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_MERGE_COUNT);
  109. BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_CONNECTION_COUNT);
  110. BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_FREE_COUNT);
  111. BIND_ENUM_CONSTANT(NAVIGATION_2D_OBSTACLE_COUNT);
  112. #endif // NAVIGATION_2D_DISABLED
  113. #ifndef NAVIGATION_3D_DISABLED
  114. BIND_ENUM_CONSTANT(NAVIGATION_3D_ACTIVE_MAPS);
  115. BIND_ENUM_CONSTANT(NAVIGATION_3D_REGION_COUNT);
  116. BIND_ENUM_CONSTANT(NAVIGATION_3D_AGENT_COUNT);
  117. BIND_ENUM_CONSTANT(NAVIGATION_3D_LINK_COUNT);
  118. BIND_ENUM_CONSTANT(NAVIGATION_3D_POLYGON_COUNT);
  119. BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_COUNT);
  120. BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_MERGE_COUNT);
  121. BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_CONNECTION_COUNT);
  122. BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_FREE_COUNT);
  123. BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT);
  124. #endif // NAVIGATION_3D_DISABLED
  125. BIND_ENUM_CONSTANT(MONITOR_MAX);
  126. }
  127. int Performance::_get_node_count() const {
  128. MainLoop *ml = OS::get_singleton()->get_main_loop();
  129. SceneTree *sml = Object::cast_to<SceneTree>(ml);
  130. if (!sml) {
  131. return 0;
  132. }
  133. return sml->get_node_count();
  134. }
  135. String Performance::get_monitor_name(Monitor p_monitor) const {
  136. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  137. static const char *names[MONITOR_MAX] = {
  138. PNAME("time/fps"),
  139. PNAME("time/process"),
  140. PNAME("time/physics_process"),
  141. PNAME("time/navigation_process"),
  142. PNAME("memory/static"),
  143. PNAME("memory/static_max"),
  144. PNAME("memory/msg_buf_max"),
  145. PNAME("object/objects"),
  146. PNAME("object/resources"),
  147. PNAME("object/nodes"),
  148. PNAME("object/orphan_nodes"),
  149. PNAME("raster/total_objects_drawn"),
  150. PNAME("raster/total_primitives_drawn"),
  151. PNAME("raster/total_draw_calls"),
  152. PNAME("video/video_mem"),
  153. PNAME("video/texture_mem"),
  154. PNAME("video/buffer_mem"),
  155. PNAME("physics_2d/active_objects"),
  156. PNAME("physics_2d/collision_pairs"),
  157. PNAME("physics_2d/islands"),
  158. PNAME("physics_3d/active_objects"),
  159. PNAME("physics_3d/collision_pairs"),
  160. PNAME("physics_3d/islands"),
  161. PNAME("audio/driver/output_latency"),
  162. #if !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
  163. PNAME("navigation/active_maps"),
  164. PNAME("navigation/regions"),
  165. PNAME("navigation/agents"),
  166. PNAME("navigation/links"),
  167. PNAME("navigation/polygons"),
  168. PNAME("navigation/edges"),
  169. PNAME("navigation/edges_merged"),
  170. PNAME("navigation/edges_connected"),
  171. PNAME("navigation/edges_free"),
  172. PNAME("navigation/obstacles"),
  173. #endif // !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
  174. PNAME("pipeline/compilations_canvas"),
  175. PNAME("pipeline/compilations_mesh"),
  176. PNAME("pipeline/compilations_surface"),
  177. PNAME("pipeline/compilations_draw"),
  178. PNAME("pipeline/compilations_specialization"),
  179. #ifndef NAVIGATION_2D_DISABLED
  180. PNAME("navigation_2d/active_maps"),
  181. PNAME("navigation_2d/regions"),
  182. PNAME("navigation_2d/agents"),
  183. PNAME("navigation_2d/links"),
  184. PNAME("navigation_2d/polygons"),
  185. PNAME("navigation_2d/edges"),
  186. PNAME("navigation_2d/edges_merged"),
  187. PNAME("navigation_2d/edges_connected"),
  188. PNAME("navigation_2d/edges_free"),
  189. PNAME("navigation_2d/obstacles"),
  190. #endif // NAVIGATION_2D_DISABLED
  191. #ifndef NAVIGATION_3D_DISABLED
  192. PNAME("navigation_3d/active_maps"),
  193. PNAME("navigation_3d/regions"),
  194. PNAME("navigation_3d/agents"),
  195. PNAME("navigation_3d/links"),
  196. PNAME("navigation_3d/polygons"),
  197. PNAME("navigation_3d/edges"),
  198. PNAME("navigation_3d/edges_merged"),
  199. PNAME("navigation_3d/edges_connected"),
  200. PNAME("navigation_3d/edges_free"),
  201. PNAME("navigation_3d/obstacles"),
  202. #endif // NAVIGATION_3D_DISABLED
  203. };
  204. static_assert(std::size(names) == MONITOR_MAX);
  205. return names[p_monitor];
  206. }
  207. double Performance::get_monitor(Monitor p_monitor) const {
  208. int info = 0;
  209. switch (p_monitor) {
  210. case TIME_FPS:
  211. return Engine::get_singleton()->get_frames_per_second();
  212. case TIME_PROCESS:
  213. return _process_time;
  214. case TIME_PHYSICS_PROCESS:
  215. return _physics_process_time;
  216. case TIME_NAVIGATION_PROCESS:
  217. return _navigation_process_time;
  218. case MEMORY_STATIC:
  219. return Memory::get_mem_usage();
  220. case MEMORY_STATIC_MAX:
  221. return Memory::get_mem_max_usage();
  222. case MEMORY_MESSAGE_BUFFER_MAX:
  223. return MessageQueue::get_singleton()->get_max_buffer_usage();
  224. case OBJECT_COUNT:
  225. return ObjectDB::get_object_count();
  226. case OBJECT_RESOURCE_COUNT:
  227. return ResourceCache::get_cached_resource_count();
  228. case OBJECT_NODE_COUNT:
  229. return _get_node_count();
  230. case OBJECT_ORPHAN_NODE_COUNT:
  231. return Node::orphan_node_count;
  232. case RENDER_TOTAL_OBJECTS_IN_FRAME:
  233. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME);
  234. case RENDER_TOTAL_PRIMITIVES_IN_FRAME:
  235. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME);
  236. case RENDER_TOTAL_DRAW_CALLS_IN_FRAME:
  237. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME);
  238. case RENDER_VIDEO_MEM_USED:
  239. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_VIDEO_MEM_USED);
  240. case RENDER_TEXTURE_MEM_USED:
  241. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TEXTURE_MEM_USED);
  242. case RENDER_BUFFER_MEM_USED:
  243. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_BUFFER_MEM_USED);
  244. case PIPELINE_COMPILATIONS_CANVAS:
  245. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_CANVAS);
  246. case PIPELINE_COMPILATIONS_MESH:
  247. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_MESH);
  248. case PIPELINE_COMPILATIONS_SURFACE:
  249. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SURFACE);
  250. case PIPELINE_COMPILATIONS_DRAW:
  251. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW);
  252. case PIPELINE_COMPILATIONS_SPECIALIZATION:
  253. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION);
  254. #ifndef PHYSICS_2D_DISABLED
  255. case PHYSICS_2D_ACTIVE_OBJECTS:
  256. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
  257. case PHYSICS_2D_COLLISION_PAIRS:
  258. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
  259. case PHYSICS_2D_ISLAND_COUNT:
  260. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
  261. #else
  262. case PHYSICS_2D_ACTIVE_OBJECTS:
  263. return 0;
  264. case PHYSICS_2D_COLLISION_PAIRS:
  265. return 0;
  266. case PHYSICS_2D_ISLAND_COUNT:
  267. return 0;
  268. #endif // PHYSICS_2D_DISABLED
  269. #ifndef PHYSICS_3D_DISABLED
  270. case PHYSICS_3D_ACTIVE_OBJECTS:
  271. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
  272. case PHYSICS_3D_COLLISION_PAIRS:
  273. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
  274. case PHYSICS_3D_ISLAND_COUNT:
  275. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
  276. #else
  277. case PHYSICS_3D_ACTIVE_OBJECTS:
  278. return 0;
  279. case PHYSICS_3D_COLLISION_PAIRS:
  280. return 0;
  281. case PHYSICS_3D_ISLAND_COUNT:
  282. return 0;
  283. #endif // PHYSICS_3D_DISABLED
  284. case AUDIO_OUTPUT_LATENCY:
  285. return AudioServer::get_singleton()->get_output_latency();
  286. case NAVIGATION_ACTIVE_MAPS:
  287. #ifndef NAVIGATION_2D_DISABLED
  288. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS);
  289. #endif // NAVIGATION_2D_DISABLED
  290. #ifndef NAVIGATION_3D_DISABLED
  291. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS);
  292. #endif // NAVIGATION_3D_DISABLED
  293. return info;
  294. case NAVIGATION_REGION_COUNT:
  295. #ifndef NAVIGATION_2D_DISABLED
  296. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_REGION_COUNT);
  297. #endif // NAVIGATION_2D_DISABLED
  298. #ifndef NAVIGATION_3D_DISABLED
  299. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_REGION_COUNT);
  300. #endif // NAVIGATION_3D_DISABLED
  301. return info;
  302. case NAVIGATION_AGENT_COUNT:
  303. #ifndef NAVIGATION_2D_DISABLED
  304. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_AGENT_COUNT);
  305. #endif // NAVIGATION_2D_DISABLED
  306. #ifndef NAVIGATION_3D_DISABLED
  307. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_AGENT_COUNT);
  308. #endif // NAVIGATION_3D_DISABLED
  309. return info;
  310. case NAVIGATION_LINK_COUNT:
  311. #ifndef NAVIGATION_2D_DISABLED
  312. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_LINK_COUNT);
  313. #endif // NAVIGATION_2D_DISABLED
  314. #ifndef NAVIGATION_3D_DISABLED
  315. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_LINK_COUNT);
  316. #endif // NAVIGATION_3D_DISABLED
  317. return info;
  318. case NAVIGATION_POLYGON_COUNT:
  319. #ifndef NAVIGATION_2D_DISABLED
  320. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_POLYGON_COUNT);
  321. #endif // NAVIGATION_2D_DISABLED
  322. #ifndef NAVIGATION_3D_DISABLED
  323. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT);
  324. #endif // NAVIGATION_3D_DISABLED
  325. return info;
  326. case NAVIGATION_EDGE_COUNT:
  327. #ifndef NAVIGATION_2D_DISABLED
  328. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_COUNT);
  329. #endif // NAVIGATION_2D_DISABLED
  330. #ifndef NAVIGATION_3D_DISABLED
  331. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_COUNT);
  332. #endif // NAVIGATION_3D_DISABLED
  333. return info;
  334. case NAVIGATION_EDGE_MERGE_COUNT:
  335. #ifndef NAVIGATION_2D_DISABLED
  336. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_MERGE_COUNT);
  337. #endif // NAVIGATION_2D_DISABLED
  338. #ifndef NAVIGATION_3D_DISABLED
  339. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT);
  340. #endif // NAVIGATION_3D_DISABLED
  341. return info;
  342. case NAVIGATION_EDGE_CONNECTION_COUNT:
  343. #ifndef NAVIGATION_2D_DISABLED
  344. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_CONNECTION_COUNT);
  345. #endif // NAVIGATION_2D_DISABLED
  346. #ifndef NAVIGATION_3D_DISABLED
  347. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT);
  348. #endif // NAVIGATION_3D_DISABLED
  349. return info;
  350. case NAVIGATION_EDGE_FREE_COUNT:
  351. #ifndef NAVIGATION_2D_DISABLED
  352. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_FREE_COUNT);
  353. #endif // NAVIGATION_2D_DISABLED
  354. #ifndef NAVIGATION_3D_DISABLED
  355. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT);
  356. #endif // NAVIGATION_3D_DISABLED
  357. return info;
  358. case NAVIGATION_OBSTACLE_COUNT:
  359. #ifndef NAVIGATION_2D_DISABLED
  360. info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_OBSTACLE_COUNT);
  361. #endif // NAVIGATION_2D_DISABLED
  362. #ifndef NAVIGATION_3D_DISABLED
  363. info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_OBSTACLE_COUNT);
  364. #endif // NAVIGATION_3D_DISABLED
  365. return info;
  366. #ifndef NAVIGATION_2D_DISABLED
  367. case NAVIGATION_2D_ACTIVE_MAPS:
  368. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS);
  369. case NAVIGATION_2D_REGION_COUNT:
  370. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_REGION_COUNT);
  371. case NAVIGATION_2D_AGENT_COUNT:
  372. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_AGENT_COUNT);
  373. case NAVIGATION_2D_LINK_COUNT:
  374. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_LINK_COUNT);
  375. case NAVIGATION_2D_POLYGON_COUNT:
  376. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_POLYGON_COUNT);
  377. case NAVIGATION_2D_EDGE_COUNT:
  378. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_COUNT);
  379. case NAVIGATION_2D_EDGE_MERGE_COUNT:
  380. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_MERGE_COUNT);
  381. case NAVIGATION_2D_EDGE_CONNECTION_COUNT:
  382. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_CONNECTION_COUNT);
  383. case NAVIGATION_2D_EDGE_FREE_COUNT:
  384. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_FREE_COUNT);
  385. case NAVIGATION_2D_OBSTACLE_COUNT:
  386. return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_OBSTACLE_COUNT);
  387. #endif // NAVIGATION_2D_DISABLED
  388. #ifndef NAVIGATION_3D_DISABLED
  389. case NAVIGATION_3D_ACTIVE_MAPS:
  390. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS);
  391. case NAVIGATION_3D_REGION_COUNT:
  392. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_REGION_COUNT);
  393. case NAVIGATION_3D_AGENT_COUNT:
  394. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_AGENT_COUNT);
  395. case NAVIGATION_3D_LINK_COUNT:
  396. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_LINK_COUNT);
  397. case NAVIGATION_3D_POLYGON_COUNT:
  398. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT);
  399. case NAVIGATION_3D_EDGE_COUNT:
  400. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_COUNT);
  401. case NAVIGATION_3D_EDGE_MERGE_COUNT:
  402. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT);
  403. case NAVIGATION_3D_EDGE_CONNECTION_COUNT:
  404. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT);
  405. case NAVIGATION_3D_EDGE_FREE_COUNT:
  406. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT);
  407. case NAVIGATION_3D_OBSTACLE_COUNT:
  408. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_OBSTACLE_COUNT);
  409. #endif // NAVIGATION_3D_DISABLED
  410. default: {
  411. }
  412. }
  413. return 0;
  414. }
  415. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  416. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  417. // ugly
  418. static const MonitorType types[MONITOR_MAX] = {
  419. MONITOR_TYPE_QUANTITY,
  420. MONITOR_TYPE_TIME,
  421. MONITOR_TYPE_TIME,
  422. MONITOR_TYPE_TIME,
  423. MONITOR_TYPE_MEMORY,
  424. MONITOR_TYPE_MEMORY,
  425. MONITOR_TYPE_MEMORY,
  426. MONITOR_TYPE_QUANTITY,
  427. MONITOR_TYPE_QUANTITY,
  428. MONITOR_TYPE_QUANTITY,
  429. MONITOR_TYPE_QUANTITY,
  430. MONITOR_TYPE_QUANTITY,
  431. MONITOR_TYPE_QUANTITY,
  432. MONITOR_TYPE_QUANTITY,
  433. MONITOR_TYPE_MEMORY,
  434. MONITOR_TYPE_MEMORY,
  435. MONITOR_TYPE_MEMORY,
  436. MONITOR_TYPE_QUANTITY,
  437. MONITOR_TYPE_QUANTITY,
  438. MONITOR_TYPE_QUANTITY,
  439. MONITOR_TYPE_QUANTITY,
  440. MONITOR_TYPE_QUANTITY,
  441. MONITOR_TYPE_QUANTITY,
  442. MONITOR_TYPE_TIME,
  443. MONITOR_TYPE_QUANTITY,
  444. MONITOR_TYPE_QUANTITY,
  445. MONITOR_TYPE_QUANTITY,
  446. MONITOR_TYPE_QUANTITY,
  447. MONITOR_TYPE_QUANTITY,
  448. MONITOR_TYPE_QUANTITY,
  449. MONITOR_TYPE_QUANTITY,
  450. MONITOR_TYPE_QUANTITY,
  451. MONITOR_TYPE_QUANTITY,
  452. MONITOR_TYPE_QUANTITY,
  453. MONITOR_TYPE_QUANTITY,
  454. MONITOR_TYPE_QUANTITY,
  455. MONITOR_TYPE_QUANTITY,
  456. MONITOR_TYPE_QUANTITY,
  457. MONITOR_TYPE_QUANTITY,
  458. MONITOR_TYPE_QUANTITY,
  459. MONITOR_TYPE_QUANTITY,
  460. MONITOR_TYPE_QUANTITY,
  461. MONITOR_TYPE_QUANTITY,
  462. MONITOR_TYPE_QUANTITY,
  463. MONITOR_TYPE_QUANTITY,
  464. MONITOR_TYPE_QUANTITY,
  465. MONITOR_TYPE_QUANTITY,
  466. MONITOR_TYPE_QUANTITY,
  467. MONITOR_TYPE_QUANTITY,
  468. MONITOR_TYPE_QUANTITY,
  469. MONITOR_TYPE_QUANTITY,
  470. MONITOR_TYPE_QUANTITY,
  471. MONITOR_TYPE_QUANTITY,
  472. MONITOR_TYPE_QUANTITY,
  473. MONITOR_TYPE_QUANTITY,
  474. MONITOR_TYPE_QUANTITY,
  475. MONITOR_TYPE_QUANTITY,
  476. MONITOR_TYPE_QUANTITY,
  477. MONITOR_TYPE_QUANTITY,
  478. };
  479. static_assert((sizeof(types) / sizeof(MonitorType)) == MONITOR_MAX);
  480. return types[p_monitor];
  481. }
  482. void Performance::set_process_time(double p_pt) {
  483. _process_time = p_pt;
  484. }
  485. void Performance::set_physics_process_time(double p_pt) {
  486. _physics_process_time = p_pt;
  487. }
  488. void Performance::set_navigation_process_time(double p_pt) {
  489. _navigation_process_time = p_pt;
  490. }
  491. void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
  492. ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
  493. _monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
  494. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  495. }
  496. void Performance::remove_custom_monitor(const StringName &p_id) {
  497. ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  498. _monitor_map.erase(p_id);
  499. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  500. }
  501. bool Performance::has_custom_monitor(const StringName &p_id) {
  502. return _monitor_map.has(p_id);
  503. }
  504. Variant Performance::get_custom_monitor(const StringName &p_id) {
  505. ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  506. bool error;
  507. String error_message;
  508. Variant return_value = _monitor_map[p_id].call(error, error_message);
  509. ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message);
  510. return return_value;
  511. }
  512. TypedArray<StringName> Performance::get_custom_monitor_names() {
  513. if (!_monitor_map.size()) {
  514. return TypedArray<StringName>();
  515. }
  516. TypedArray<StringName> return_array;
  517. return_array.resize(_monitor_map.size());
  518. int index = 0;
  519. for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
  520. return_array.set(index, i.key);
  521. index++;
  522. }
  523. return return_array;
  524. }
  525. uint64_t Performance::get_monitor_modification_time() {
  526. return _monitor_modification_time;
  527. }
  528. Performance::Performance() {
  529. _process_time = 0;
  530. _physics_process_time = 0;
  531. _navigation_process_time = 0;
  532. _monitor_modification_time = 0;
  533. singleton = this;
  534. }
  535. Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
  536. _callable = p_callable;
  537. _arguments = p_arguments;
  538. }
  539. Performance::MonitorCall::MonitorCall() {
  540. }
  541. Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
  542. Vector<const Variant *> arguments_mem;
  543. arguments_mem.resize(_arguments.size());
  544. for (int i = 0; i < _arguments.size(); i++) {
  545. arguments_mem.write[i] = &_arguments[i];
  546. }
  547. const Variant **args = (const Variant **)arguments_mem.ptr();
  548. int argc = _arguments.size();
  549. Variant return_value;
  550. Callable::CallError error;
  551. _callable.callp(args, argc, return_value, error);
  552. r_error = (error.error != Callable::CallError::CALL_OK);
  553. if (r_error) {
  554. r_error_message = Variant::get_callable_error_text(_callable, args, argc, error);
  555. }
  556. return return_value;
  557. }