profiler.tscript 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. $MetricsParamArray[0] = "fps ";
  23. $MetricsParamArray[1] = "shadow ";
  24. $MetricsParamArray[2] = "gfx ";
  25. $MetricsParamArray[3] = "sfx ";
  26. $MetricsParamArray[4] = "terrain ";
  27. $MetricsParamArray[5] = "groundcover ";
  28. $MetricsParamArray[6] = "forest ";
  29. $MetricsParamArray[7] = "net ";
  30. $EnableProfiler = false;
  31. $string = ""; //string used to collet the parameters for metrics function
  32. function showMetrics(%var)
  33. {
  34. $string = "";
  35. if(ppShowFps.getValue())
  36. {
  37. $string = $string @ $MetricsParamArray[0];
  38. }
  39. if(ppShowShadow.getValue())
  40. {
  41. $string = $string @ $MetricsParamArray[1];
  42. }
  43. if(ppShowGfx.getValue())
  44. {
  45. $string = $string @ $MetricsParamArray[2];
  46. }
  47. if(ppShowSfx.getValue())
  48. {
  49. $string = $string @ $MetricsParamArray[3];
  50. }
  51. if(ppShowTerrain.getValue())
  52. {
  53. $string = $string @ $MetricsParamArray[4];
  54. }
  55. if(ppShowForest.getValue())
  56. {
  57. $string = $string @ $MetricsParamArray[5];
  58. }
  59. if(ppShowGroundcover.getValue())
  60. {
  61. $string = $string @ $MetricsParamArray[6];
  62. }
  63. if(ppShowNet.getValue())
  64. {
  65. $string = $string @ $MetricsParamArray[7];
  66. }
  67. if(%var)
  68. {
  69. $EnableProfiler = !($EnableProfiler);
  70. if($EnableProfiler)
  71. {
  72. metrics($string);
  73. }
  74. else if((false == $EnableProfiler))
  75. {
  76. metrics();
  77. }
  78. }
  79. else if($EnableProfiler) //will enter only when the enable/disable button was pressed
  80. {
  81. metrics($string);
  82. }
  83. }
  84. function showMetics(%var)
  85. {
  86. if(%var)
  87. {
  88. metrics($string);
  89. }
  90. else if(true == $EnableProfiler)
  91. {
  92. $EnableProfiler = false;
  93. metrics();
  94. }
  95. }
  96. GlobalActionMap.bind(keyboard, "ctrl F2", showMetics);
  97. $guiContent = new GuiControl(FrameOverlayGui) {
  98. profile = "GuiModelessDialogProfile";
  99. horizSizing = "right";
  100. vertSizing = "bottom";
  101. position = "0 0";
  102. extent = "640 480";
  103. minExtent = "8 8";
  104. visible = "True";
  105. setFirstResponder = "True";
  106. modal = "false";
  107. helpTag = "0";
  108. noCursor = true;
  109. new GuiConsoleTextCtrl(TextOverlayControl) {
  110. profile = "GuiConsoleTextProfile";
  111. horizSizing = "right";
  112. vertSizing = "bottom";
  113. position = "5 5";
  114. extent = "130 18";
  115. minExtent = "4 4";
  116. visible = "True";
  117. setFirstResponder = "True";
  118. modal = "True";
  119. helpTag = "0";
  120. expression = "10";
  121. command = "Canvas.popDialog(FrameOverlayGui);";
  122. accelerator = "escape";
  123. };
  124. };
  125. // Note: To implement your own metrics overlay
  126. // just add a function with a name in the form
  127. // XXXXMetricsCallback which can be enabled via
  128. // metrics( XXXX )
  129. function fpsMetricsCallback()
  130. {
  131. return " | FPS |" @
  132. " " @ $fps::real @
  133. " max: " @ $fps::realMax @
  134. " min: " @ $fps::realMin @
  135. " mspf: " @ 1000 / $fps::real;
  136. }
  137. function gfxMetricsCallback()
  138. {
  139. return " | GFX |" @
  140. " PolyCount: " @ $GFXDeviceStatistics::polyCount @
  141. " DrawCalls: " @ $GFXDeviceStatistics::drawCalls @
  142. " RTChanges: " @ $GFXDeviceStatistics::renderTargetChanges;
  143. }
  144. function terrainMetricsCallback()
  145. {
  146. return " | Terrain |" @
  147. " Cells: " @ $TerrainBlock::cellsRendered @
  148. " Override Cells: " @ $TerrainBlock::overrideCells @
  149. " DrawCalls: " @ $TerrainBlock::drawCalls;
  150. }
  151. function netMetricsCallback()
  152. {
  153. return " | Net |" @
  154. " BitsSent: " @ $Stats::netBitsSent @
  155. " BitsRcvd: " @ $Stats::netBitsReceived @
  156. " GhostUpd: " @ $Stats::netGhostUpdates;
  157. }
  158. function groundCoverMetricsCallback()
  159. {
  160. return " | GroundCover |" @
  161. " Cells: " @ $GroundCover::renderedCells @
  162. " Billboards: " @ $GroundCover::renderedBillboards @
  163. " Batches: " @ $GroundCover::renderedBatches @
  164. " Shapes: " @ $GroundCover::renderedShapes;
  165. }
  166. function forestMetricsCallback()
  167. {
  168. return " | Forest |" @
  169. " Cells: " @ $Forest::totalCells @
  170. " Cells Meshed: " @ $Forest::cellsRendered @
  171. " Cells Billboarded: " @ $Forest::cellsBatched @
  172. " Meshes: " @ $Forest::cellItemsRendered @
  173. " Billboards: " @ $Forest::cellItemsBatched;
  174. }
  175. function sfxMetricsCallback()
  176. {
  177. return " | SFX |" @
  178. " Sounds: " @ $SFX::numSounds @
  179. " Lists: " @ ( $SFX::numSources - $SFX::numSounds ) @
  180. " Playing: " @ $SFX::numPlaying @
  181. " Culled: " @ $SFX::numCulled @
  182. " Voices: " @ $SFX::numVoices @
  183. " Buffers: " @ $SFX::Device::numBuffers @
  184. " Memory: " @ ( $SFX::Device::numBufferBytes / 1024.0 / 1024.0 ) @ " MB" @
  185. " Time/S: " @ $SFX::sourceUpdateTime @
  186. " Time/P: " @ $SFX::parameterUpdateTime @
  187. " Time/A: " @ $SFX::ambientUpdateTime;
  188. }
  189. function sfxSourcesMetricsCallback()
  190. {
  191. return sfxDumpSourcesToString();
  192. }
  193. function sfxStatesMetricsCallback()
  194. {
  195. return " | SFXStates |" @ sfxGetActiveStates();
  196. }
  197. function timeMetricsCallback()
  198. {
  199. return " | Time |" @
  200. " Sim Time: " @ getSimTime() @
  201. " Mod: " @ getSimTime() % 32;
  202. }
  203. function reflectMetricsCallback()
  204. {
  205. return " | REFLECT |" @
  206. " Objects: " @ $Reflect::numObjects @
  207. " Visible: " @ $Reflect::numVisible @
  208. " Occluded: " @ $Reflect::numOccluded @
  209. " Updated: " @ $Reflect::numUpdated @
  210. " Elapsed: " @ $Reflect::elapsed NL
  211. " Allocated: " @ $Reflect::renderTargetsAllocated @
  212. " Pooled: " @ $Reflect::poolSize NL
  213. " " @ getWord( $Reflect::textureStats, 1 ) TAB
  214. " " @ getWord( $Reflect::textureStats, 2 ) @ "MB" TAB
  215. " " @ getWord( $Reflect::textureStats, 0 );
  216. }
  217. function decalMetricsCallback()
  218. {
  219. return " | DECAL |" @
  220. " Batches: " @ $Decal::Batches @
  221. " Buffers: " @ $Decal::Buffers @
  222. " DecalsRendered: " @ $Decal::DecalsRendered;
  223. }
  224. function renderMetricsCallback()
  225. {
  226. return " | Render |" @
  227. " Mesh: " @ $RenderMetrics::RIT_Mesh @
  228. " MeshDL: " @ $RenderMetrics::RIT_MeshDynamicLighting @
  229. " Shadow: " @ $RenderMetrics::RIT_Shadow @
  230. " Sky: " @ $RenderMetrics::RIT_Sky @
  231. " Obj: " @ $RenderMetrics::RIT_Object @
  232. " ObjT: " @ $RenderMetrics::RIT_ObjectTranslucent @
  233. " Decal: " @ $RenderMetrics::RIT_Decal @
  234. " Water: " @ $RenderMetrics::RIT_Water @
  235. " Foliage: " @ $RenderMetrics::RIT_Foliage @
  236. " Trans: " @ $RenderMetris::RIT_Translucent @
  237. " Custom: " @ $RenderMetrics::RIT_Custom;
  238. }
  239. function shadowMetricsCallback()
  240. {
  241. return " | Shadow |" @
  242. " Active: " @ $ShadowStats::activeMaps @
  243. " Updated: " @ $ShadowStats::updatedMaps @
  244. " PolyCount: " @ $ShadowStats::polyCount @
  245. " DrawCalls: " @ $ShadowStats::drawCalls @
  246. " RTChanges: " @ $ShadowStats::rtChanges @
  247. " PoolTexCount: " @ $ShadowStats::poolTexCount @
  248. " PoolTexMB: " @ $ShadowStats::poolTexMemory @ "MB";
  249. }
  250. function basicShadowMetricsCallback()
  251. {
  252. return " | Shadow |" @
  253. " Active: " @ $BasicLightManagerStats::activePlugins @
  254. " Updated: " @ $BasicLightManagerStats::shadowsUpdated @
  255. " Elapsed Ms: " @ $BasicLightManagerStats::elapsedUpdateMs;
  256. }
  257. function lightMetricsCallback()
  258. {
  259. return " | Deferred Lights |" @
  260. " Active: " @ $lightMetrics::activeLights @
  261. " Culled: " @ $lightMetrics::culledLights;
  262. }
  263. function particleMetricsCallback()
  264. {
  265. return " | Particles |" @
  266. " # Simulated " @ $particle::numSimulated;
  267. }
  268. function partMetricsCallback()
  269. {
  270. return particleMetricsCallback();
  271. }
  272. function imposterMetricsCallback()
  273. {
  274. return " | IMPOSTER |" @
  275. " Rendered: " @ $ImposterStats::rendered @
  276. " Batches: " @ $ImposterStats::batches @
  277. " DrawCalls: " @ $ImposterStats::drawCalls @
  278. " Polys: " @ $ImposterStats::polyCount @
  279. " RtChanges: " @ $ImposterStats::rtChanges;
  280. }
  281. // alias
  282. function audioMetricsCallback()
  283. {
  284. return sfxMetricsCallback();
  285. }
  286. // alias
  287. function videoMetricsCallback()
  288. {
  289. return gfxMetricsCallback();
  290. }
  291. // Add a metrics HUD. %expr can be a vector of names where each element
  292. // must have a corresponding '<name>MetricsCallback()' function defined
  293. // that will be called on each update of the GUI control. The results
  294. // of each function are stringed together.
  295. //
  296. // Example: metrics( "fps gfx" );
  297. function metrics( %expr )
  298. {
  299. %metricsExpr = "";
  300. if( %expr !$= "" )
  301. {
  302. for( %i = 0;; %i ++ )
  303. {
  304. %name = getWord( %expr, %i );
  305. if( %name $= "" )
  306. break;
  307. else
  308. {
  309. %cb = %name @ "MetricsCallback";
  310. if( !isFunction( %cb ) )
  311. error( "metrics - undefined callback: " @ %cb );
  312. else
  313. {
  314. %cb = %cb @ "()";
  315. if( %i > 0 )
  316. %metricsExpr = %metricsExpr @ " NL ";
  317. %metricsExpr = %metricsExpr @ %cb;
  318. }
  319. }
  320. }
  321. if( %metricsExpr !$= "" )
  322. %metricsExpr = %metricsExpr @ " @ \" \"";
  323. }
  324. if( %metricsExpr !$= "" )
  325. {
  326. $GameCanvas.pushDialog( FrameOverlayGui, 1000 );
  327. TextOverlayControl.setValue( %metricsExpr );
  328. }
  329. else
  330. $GameCanvas.popDialog(FrameOverlayGui);
  331. }