performance.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* performance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "performance.h"
  30. #include "os/os.h"
  31. #include "servers/visual_server.h"
  32. #include "servers/physics_2d_server.h"
  33. #include "servers/physics_server.h"
  34. #include "message_queue.h"
  35. #include "scene/main/scene_main_loop.h"
  36. Performance *Performance::singleton=NULL;
  37. void Performance::_bind_methods() {
  38. ObjectTypeDB::bind_method(_MD("get_monitor","monitor"),&Performance::get_monitor);
  39. BIND_CONSTANT( TIME_FPS );
  40. BIND_CONSTANT( TIME_PROCESS );
  41. BIND_CONSTANT( TIME_FIXED_PROCESS );
  42. BIND_CONSTANT( MEMORY_STATIC );
  43. BIND_CONSTANT( MEMORY_DYNAMIC );
  44. BIND_CONSTANT( MEMORY_STATIC_MAX );
  45. BIND_CONSTANT( MEMORY_DYNAMIC_MAX );
  46. BIND_CONSTANT( MEMORY_MESSAGE_BUFFER_MAX );
  47. BIND_CONSTANT( OBJECT_COUNT );
  48. BIND_CONSTANT( OBJECT_RESOURCE_COUNT );
  49. BIND_CONSTANT( OBJECT_NODE_COUNT );
  50. BIND_CONSTANT( RENDER_OBJECTS_IN_FRAME );
  51. BIND_CONSTANT( RENDER_VERTICES_IN_FRAME );
  52. BIND_CONSTANT( RENDER_MATERIAL_CHANGES_IN_FRAME );
  53. BIND_CONSTANT( RENDER_SHADER_CHANGES_IN_FRAME );
  54. BIND_CONSTANT( RENDER_SURFACE_CHANGES_IN_FRAME );
  55. BIND_CONSTANT( RENDER_DRAW_CALLS_IN_FRAME );
  56. BIND_CONSTANT( RENDER_USAGE_VIDEO_MEM_TOTAL );
  57. BIND_CONSTANT( RENDER_VIDEO_MEM_USED );
  58. BIND_CONSTANT( RENDER_TEXTURE_MEM_USED );
  59. BIND_CONSTANT( RENDER_VERTEX_MEM_USED );
  60. BIND_CONSTANT( PHYSICS_2D_ACTIVE_OBJECTS );
  61. BIND_CONSTANT( PHYSICS_2D_COLLISION_PAIRS );
  62. BIND_CONSTANT( PHYSICS_2D_ISLAND_COUNT );
  63. BIND_CONSTANT( PHYSICS_3D_ACTIVE_OBJECTS );
  64. BIND_CONSTANT( PHYSICS_3D_COLLISION_PAIRS );
  65. BIND_CONSTANT( PHYSICS_3D_ISLAND_COUNT );
  66. BIND_CONSTANT( MONITOR_MAX );
  67. }
  68. String Performance::get_monitor_name(Monitor p_monitor) const {
  69. ERR_FAIL_INDEX_V(p_monitor,MONITOR_MAX,String());
  70. static const char* names[MONITOR_MAX]={
  71. "time/fps",
  72. "time/process",
  73. "time/fixed_process",
  74. "memory/static",
  75. "memory/dynamic",
  76. "memory/static_max",
  77. "memory/dynamic_max",
  78. "memory/msg_buf_max",
  79. "object/objects",
  80. "object/resources",
  81. "object/nodes",
  82. "raster/objects_drawn",
  83. "raster/vertices_drawn",
  84. "raster/mat_changes",
  85. "raster/shader_changes",
  86. "raster/surface_changes",
  87. "raster/draw_calls",
  88. "video/video_mem",
  89. "video/texure_mem",
  90. "video/vertex_mem",
  91. "video/video_mem_max",
  92. "physics_2d/active_objects",
  93. "physics_2d/collision_pairs",
  94. "physics_2d/islands",
  95. "physics_3d/active_objects",
  96. "physics_3d/collision_pairs",
  97. "physics_3d/islands",
  98. };
  99. return names[p_monitor];
  100. }
  101. float Performance::get_monitor(Monitor p_monitor) const {
  102. switch(p_monitor) {
  103. case TIME_FPS: return OS::get_singleton()->get_frames_per_second();
  104. case TIME_PROCESS: return _process_time;
  105. case TIME_FIXED_PROCESS: return _fixed_process_time;
  106. case MEMORY_STATIC: return Memory::get_static_mem_usage();
  107. case MEMORY_DYNAMIC: return Memory::get_dynamic_mem_usage();
  108. case MEMORY_STATIC_MAX: return Memory::get_static_mem_max_usage();
  109. case MEMORY_DYNAMIC_MAX: return Memory::get_dynamic_mem_available();
  110. case MEMORY_MESSAGE_BUFFER_MAX: return MessageQueue::get_singleton()->get_max_buffer_usage();
  111. case OBJECT_COUNT: return ObjectDB::get_object_count();
  112. case OBJECT_RESOURCE_COUNT: return ResourceCache::get_cached_resource_count();
  113. case OBJECT_NODE_COUNT: {
  114. MainLoop *ml = OS::get_singleton()->get_main_loop();
  115. if (!ml)
  116. return 0;
  117. SceneTree *sml = ml->cast_to<SceneTree>();
  118. if (!sml)
  119. return 0;
  120. return sml->get_node_count();
  121. };
  122. case RENDER_OBJECTS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_OBJECTS_IN_FRAME);
  123. case RENDER_VERTICES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_VERTICES_IN_FRAME);
  124. case RENDER_MATERIAL_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME);;
  125. case RENDER_SHADER_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME);;
  126. case RENDER_SURFACE_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME);;
  127. case RENDER_DRAW_CALLS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_DRAW_CALLS_IN_FRAME);
  128. case RENDER_VIDEO_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VIDEO_MEM_USED);
  129. case RENDER_TEXTURE_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED);
  130. case RENDER_VERTEX_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VERTEX_MEM_USED);
  131. case RENDER_USAGE_VIDEO_MEM_TOTAL: return VS::get_singleton()->get_render_info(VS::INFO_USAGE_VIDEO_MEM_TOTAL);
  132. case PHYSICS_2D_ACTIVE_OBJECTS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ACTIVE_OBJECTS);
  133. case PHYSICS_2D_COLLISION_PAIRS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_COLLISION_PAIRS);
  134. case PHYSICS_2D_ISLAND_COUNT: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ISLAND_COUNT);
  135. case PHYSICS_3D_ACTIVE_OBJECTS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ACTIVE_OBJECTS);
  136. case PHYSICS_3D_COLLISION_PAIRS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_COLLISION_PAIRS);
  137. case PHYSICS_3D_ISLAND_COUNT: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ISLAND_COUNT);
  138. default: {}
  139. }
  140. return 0;
  141. }
  142. void Performance::set_process_time(float p_pt) {
  143. _process_time=p_pt;
  144. }
  145. void Performance::set_fixed_process_time(float p_pt) {
  146. _fixed_process_time=p_pt;
  147. }
  148. Performance::Performance() {
  149. _process_time=0;
  150. _fixed_process_time=0;
  151. singleton=this;
  152. }