RenderLog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "tr_local.h"
  22. /*
  23. ================================================================================================
  24. Contains the RenderLog implementation.
  25. TODO: Emit statistics to the logfile at the end of views and frames.
  26. ================================================================================================
  27. */
  28. idCVar r_logLevel( "r_logLevel", "2", CVAR_INTEGER, "1 = blocks only, 2 = everything", 1, 2 );
  29. static const int LOG_LEVEL_BLOCKS_ONLY = 1;
  30. static const int LOG_LEVEL_EVERYTHING = 2;
  31. const char * renderLogMainBlockLabels[] = {
  32. ASSERT_ENUM_STRING( MRB_NONE, 0 ),
  33. ASSERT_ENUM_STRING( MRB_BEGIN_DRAWING_VIEW, 1 ),
  34. ASSERT_ENUM_STRING( MRB_FILL_DEPTH_BUFFER, 2 ),
  35. ASSERT_ENUM_STRING( MRB_DRAW_INTERACTIONS, 3 ),
  36. ASSERT_ENUM_STRING( MRB_DRAW_SHADER_PASSES, 4 ),
  37. ASSERT_ENUM_STRING( MRB_FOG_ALL_LIGHTS, 5 ),
  38. ASSERT_ENUM_STRING( MRB_DRAW_SHADER_PASSES_POST, 6 ),
  39. ASSERT_ENUM_STRING( MRB_DRAW_DEBUG_TOOLS, 7 ),
  40. ASSERT_ENUM_STRING( MRB_CAPTURE_COLORBUFFER, 8 ),
  41. ASSERT_ENUM_STRING( MRB_POSTPROCESS, 9 ),
  42. ASSERT_ENUM_STRING( MRB_GPU_SYNC, 10 ),
  43. ASSERT_ENUM_STRING( MRB_END_FRAME, 11 ),
  44. ASSERT_ENUM_STRING( MRB_BINK_FRAME, 12 ),
  45. ASSERT_ENUM_STRING( MRB_BINK_NEXT_FRAME, 13 ),
  46. ASSERT_ENUM_STRING( MRB_TOTAL, 14 ),
  47. ASSERT_ENUM_STRING( MRB_MAX, 15 )
  48. };
  49. extern uint64 Sys_Microseconds();
  50. /*
  51. ================================================================================================
  52. PIX events on all platforms
  53. ================================================================================================
  54. */
  55. /*
  56. ================================================
  57. pixEvent_t
  58. ================================================
  59. */
  60. struct pixEvent_t {
  61. char name[256];
  62. uint64 cpuTime;
  63. uint64 gpuTime;
  64. };
  65. idCVar r_pix( "r_pix", "0", CVAR_INTEGER, "print GPU/CPU event timing" );
  66. static const int MAX_PIX_EVENTS = 256;
  67. // defer allocation of this until needed, so we don't waste lots of memory
  68. pixEvent_t * pixEvents; // [MAX_PIX_EVENTS]
  69. int numPixEvents;
  70. int numPixLevels;
  71. static GLuint timeQueryIds[MAX_PIX_EVENTS];
  72. /*
  73. ========================
  74. PC_BeginNamedEvent
  75. FIXME: this is not thread safe on the PC
  76. ========================
  77. */
  78. void PC_BeginNamedEvent( const char *szName, ... ) {
  79. #if 0
  80. if ( !r_pix.GetBool() ) {
  81. return;
  82. }
  83. if ( !pixEvents ) {
  84. // lazy allocation to not waste memory
  85. pixEvents = (pixEvent_t *)Mem_ClearedAlloc( sizeof( *pixEvents ) * MAX_PIX_EVENTS, TAG_CRAP );
  86. }
  87. if ( numPixEvents >= MAX_PIX_EVENTS ) {
  88. idLib::FatalError( "PC_BeginNamedEvent: event overflow" );
  89. }
  90. if ( ++numPixLevels > 1 ) {
  91. return; // only get top level timing information
  92. }
  93. if ( !qglGetQueryObjectui64vEXT ) {
  94. return;
  95. }
  96. GL_CheckErrors();
  97. if ( timeQueryIds[0] == 0 ) {
  98. qglGenQueriesARB( MAX_PIX_EVENTS, timeQueryIds );
  99. }
  100. qglFinish();
  101. qglBeginQueryARB( GL_TIME_ELAPSED_EXT, timeQueryIds[numPixEvents] );
  102. GL_CheckErrors();
  103. pixEvent_t *ev = &pixEvents[numPixEvents++];
  104. strncpy( ev->name, szName, sizeof( ev->name ) - 1 );
  105. ev->cpuTime = Sys_Microseconds();
  106. #endif
  107. }
  108. /*
  109. ========================
  110. PC_EndNamedEvent
  111. ========================
  112. */
  113. void PC_EndNamedEvent() {
  114. #if 0
  115. if ( !r_pix.GetBool() ) {
  116. return;
  117. }
  118. if ( numPixLevels <= 0 ) {
  119. idLib::FatalError( "PC_EndNamedEvent: level underflow" );
  120. }
  121. if ( --numPixLevels > 0 ) {
  122. // only do timing on top level events
  123. return;
  124. }
  125. if ( !qglGetQueryObjectui64vEXT ) {
  126. return;
  127. }
  128. pixEvent_t *ev = &pixEvents[numPixEvents-1];
  129. ev->cpuTime = Sys_Microseconds() - ev->cpuTime;
  130. GL_CheckErrors();
  131. qglEndQueryARB( GL_TIME_ELAPSED_EXT );
  132. GL_CheckErrors();
  133. #endif
  134. }
  135. /*
  136. ========================
  137. PC_EndFrame
  138. ========================
  139. */
  140. void PC_EndFrame() {
  141. #if 0
  142. if ( !r_pix.GetBool() ) {
  143. return;
  144. }
  145. int64 totalGPU = 0;
  146. int64 totalCPU = 0;
  147. idLib::Printf( "----- GPU Events -----\n" );
  148. for ( int i = 0 ; i < numPixEvents ; i++ ) {
  149. pixEvent_t *ev = &pixEvents[i];
  150. int64 gpuTime = 0;
  151. qglGetQueryObjectui64vEXT( timeQueryIds[i], GL_QUERY_RESULT, (GLuint64EXT *)&gpuTime );
  152. ev->gpuTime = gpuTime;
  153. idLib::Printf( "%2d: %1.2f (GPU) %1.3f (CPU) = %s\n", i, ev->gpuTime / 1000000.0f, ev->cpuTime / 1000.0f, ev->name );
  154. totalGPU += ev->gpuTime;
  155. totalCPU += ev->cpuTime;
  156. }
  157. idLib::Printf( "%2d: %1.2f (GPU) %1.3f (CPU) = total\n", numPixEvents, totalGPU / 1000000.0f, totalCPU / 1000.0f );
  158. memset( pixEvents, 0, numPixLevels * sizeof( pixEvents[0] ) );
  159. numPixEvents = 0;
  160. numPixLevels = 0;
  161. #endif
  162. }
  163. /*
  164. ================================================================================================
  165. idRenderLog
  166. ================================================================================================
  167. */
  168. idRenderLog renderLog;
  169. #if !defined( STUB_RENDER_LOG )
  170. /*
  171. ========================
  172. idRenderLog::idRenderLog
  173. ========================
  174. */
  175. idRenderLog::idRenderLog() {
  176. activeLevel = 0;
  177. indentString[0] = '\0';
  178. indentLevel = 0;
  179. logFile = NULL;
  180. frameStartTime = 0;
  181. closeBlockTime = 0;
  182. logLevel = 0;
  183. }
  184. /*
  185. ========================
  186. idRenderLog::StartFrame
  187. ========================
  188. */
  189. void idRenderLog::StartFrame() {
  190. if ( r_logFile.GetInteger() == 0 ) {
  191. return;
  192. }
  193. // open a new logfile
  194. indentLevel = 0;
  195. indentString[0] = '\0';
  196. activeLevel = r_logLevel.GetInteger();
  197. struct tm *newtime;
  198. time_t aclock;
  199. char ospath[ MAX_OSPATH ];
  200. char qpath[128];
  201. sprintf( qpath, "renderlogPC_%04i.txt", r_logFile.GetInteger() );
  202. idStr finalPath = fileSystem->RelativePathToOSPath( qpath );
  203. sprintf( ospath, "%s", finalPath.c_str() );
  204. /*
  205. for ( int i = 0; i < 9999 ; i++ ) {
  206. char qpath[128];
  207. sprintf( qpath, "renderlog_%04i.txt", r_logFile.GetInteger() );
  208. idStr finalPath = fileSystem->RelativePathToOSPath( qpath );
  209. fileSystem->RelativePathToOSPath( qpath, ospath, MAX_OSPATH ,FSPATH_BASE );
  210. if ( !fileSystem->FileExists( finalPath.c_str() ) ) {
  211. break; // use this name
  212. }
  213. }
  214. */
  215. common->SetRefreshOnPrint( false ); // problems are caused if this print causes a refresh...
  216. if ( logFile != NULL ) {
  217. fileSystem->CloseFile( logFile );
  218. logFile = NULL;
  219. }
  220. logFile = fileSystem->OpenFileWrite( ospath );
  221. if ( logFile == NULL ) {
  222. idLib::Warning( "Failed to open logfile %s", ospath );
  223. return;
  224. }
  225. idLib::Printf( "Opened logfile %s\n", ospath );
  226. // write the time out to the top of the file
  227. time( &aclock );
  228. newtime = localtime( &aclock );
  229. const char *str = asctime( newtime );
  230. logFile->Printf( "// %s", str );
  231. logFile->Printf( "// %s\n\n", com_version.GetString() );
  232. frameStartTime = Sys_Microseconds();
  233. closeBlockTime = frameStartTime;
  234. OpenBlock( "Frame" );
  235. }
  236. /*
  237. ========================
  238. idRenderLog::EndFrame
  239. ========================
  240. */
  241. void idRenderLog::EndFrame() {
  242. PC_EndFrame();
  243. if ( logFile != NULL ) {
  244. if ( r_logFile.GetInteger() == 1 ) {
  245. Close();
  246. }
  247. // log is open, so decrement r_logFile and stop if it is zero
  248. r_logFile.SetInteger( r_logFile.GetInteger() - 1 );
  249. idLib::Printf( "Frame logged.\n" );
  250. return;
  251. }
  252. }
  253. /*
  254. ========================
  255. idRenderLog::Close
  256. ========================
  257. */
  258. void idRenderLog::Close() {
  259. if ( logFile != NULL ) {
  260. CloseBlock();
  261. idLib::Printf( "Closing logfile\n" );
  262. fileSystem->CloseFile( logFile );
  263. logFile = NULL;
  264. activeLevel = 0;
  265. }
  266. }
  267. /*
  268. ========================
  269. idRenderLog::OpenMainBlock
  270. ========================
  271. */
  272. void idRenderLog::OpenMainBlock( renderLogMainBlock_t block ) {
  273. }
  274. /*
  275. ========================
  276. idRenderLog::CloseMainBlock
  277. ========================
  278. */
  279. void idRenderLog::CloseMainBlock() {
  280. }
  281. /*
  282. ========================
  283. idRenderLog::OpenBlock
  284. ========================
  285. */
  286. void idRenderLog::OpenBlock( const char *label ) {
  287. // Allow the PIX functionality even when logFile is not running.
  288. PC_BeginNamedEvent( label );
  289. if ( logFile != NULL ) {
  290. LogOpenBlock( RENDER_LOG_INDENT_MAIN_BLOCK, label, NULL );
  291. }
  292. }
  293. /*
  294. ========================
  295. idRenderLog::CloseBlock
  296. ========================
  297. */
  298. void idRenderLog::CloseBlock() {
  299. PC_EndNamedEvent();
  300. if ( logFile != NULL ) {
  301. LogCloseBlock( RENDER_LOG_INDENT_MAIN_BLOCK );
  302. }
  303. }
  304. /*
  305. ========================
  306. idRenderLog::Printf
  307. ========================
  308. */
  309. void idRenderLog::Printf( const char *fmt, ... ) {
  310. if ( activeLevel <= LOG_LEVEL_BLOCKS_ONLY ) {
  311. return;
  312. }
  313. if ( logFile == NULL ) {
  314. return;
  315. }
  316. va_list marker;
  317. logFile->Printf( "%s", indentString );
  318. va_start( marker, fmt );
  319. logFile->VPrintf( fmt, marker );
  320. va_end( marker );
  321. // logFile->Flush(); this makes it take waaaay too long
  322. }
  323. /*
  324. ========================
  325. idRenderLog::LogOpenBlock
  326. ========================
  327. */
  328. void idRenderLog::LogOpenBlock( renderLogIndentLabel_t label, const char * fmt, va_list args ) {
  329. uint64 now = Sys_Microseconds();
  330. if ( logFile != NULL ) {
  331. if ( now - closeBlockTime >= 1000 ) {
  332. logFile->Printf( "%s%1.1f msec gap from last closeblock\n", indentString, ( now - closeBlockTime ) * ( 1.0f / 1000.0f ) );
  333. }
  334. logFile->Printf( "%s", indentString );
  335. logFile->VPrintf( fmt, args );
  336. logFile->Printf( " {\n" );
  337. }
  338. Indent( label );
  339. if ( logLevel >= MAX_LOG_LEVELS ) {
  340. idLib::Warning( "logLevel %d >= MAX_LOG_LEVELS", logLevel );
  341. }
  342. logLevel++;
  343. }
  344. /*
  345. ========================
  346. idRenderLog::LogCloseBlock
  347. ========================
  348. */
  349. void idRenderLog::LogCloseBlock( renderLogIndentLabel_t label ) {
  350. closeBlockTime = Sys_Microseconds();
  351. assert( logLevel > 0 );
  352. logLevel--;
  353. Outdent( label );
  354. if ( logFile != NULL ) {
  355. }
  356. }
  357. #else // !STUB_RENDER_LOG
  358. /*
  359. ========================
  360. idRenderLog::OpenBlock
  361. ========================
  362. */
  363. void idRenderLog::OpenBlock( const char *label ) {
  364. PC_BeginNamedEvent( label );
  365. }
  366. /*
  367. ========================
  368. idRenderLog::CloseBlock
  369. ========================
  370. */
  371. void idRenderLog::CloseBlock() {
  372. PC_EndNamedEvent();
  373. }
  374. #endif // !STUB_RENDER_LOG