performance.cpp 25 KB

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