tr_frontend_main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. /*
  24. ==========================================================================================
  25. FRAME MEMORY ALLOCATION
  26. ==========================================================================================
  27. */
  28. static const unsigned int NUM_FRAME_DATA = 2;
  29. static const unsigned int FRAME_ALLOC_ALIGNMENT = 128;
  30. static const unsigned int MAX_FRAME_MEMORY = 64 * 1024 * 1024; // larger so that we can noclip on PC for dev purposes
  31. idFrameData smpFrameData[NUM_FRAME_DATA];
  32. idFrameData * frameData;
  33. unsigned int smpFrame;
  34. //#define TRACK_FRAME_ALLOCS
  35. #if defined( TRACK_FRAME_ALLOCS )
  36. idSysInterlockedInteger frameAllocTypeCount[FRAME_ALLOC_MAX];
  37. int frameHighWaterTypeCount[FRAME_ALLOC_MAX];
  38. #endif
  39. /*
  40. ====================
  41. R_ToggleSmpFrame
  42. ====================
  43. */
  44. void R_ToggleSmpFrame() {
  45. // update the highwater mark
  46. if ( frameData->frameMemoryAllocated.GetValue() > frameData->highWaterAllocated ) {
  47. frameData->highWaterAllocated = frameData->frameMemoryAllocated.GetValue();
  48. #if defined( TRACK_FRAME_ALLOCS )
  49. frameData->highWaterUsed = frameData->frameMemoryUsed.GetValue();
  50. for ( int i = 0; i < FRAME_ALLOC_MAX; i++ ) {
  51. frameHighWaterTypeCount[i] = frameAllocTypeCount[i].GetValue();
  52. }
  53. #endif
  54. }
  55. // switch to the next frame
  56. smpFrame++;
  57. frameData = &smpFrameData[smpFrame % NUM_FRAME_DATA];
  58. // reset the memory allocation
  59. const unsigned int bytesNeededForAlignment = FRAME_ALLOC_ALIGNMENT - ( (unsigned int)frameData->frameMemory & ( FRAME_ALLOC_ALIGNMENT - 1 ) );
  60. frameData->frameMemoryAllocated.SetValue( bytesNeededForAlignment );
  61. frameData->frameMemoryUsed.SetValue( 0 );
  62. #if defined( TRACK_FRAME_ALLOCS )
  63. for ( int i = 0; i < FRAME_ALLOC_MAX; i++ ) {
  64. frameAllocTypeCount[i].SetValue( 0 );
  65. }
  66. #endif
  67. // clear the command chain and make a RC_NOP command the only thing on the list
  68. frameData->cmdHead = frameData->cmdTail = (emptyCommand_t *)R_FrameAlloc( sizeof( *frameData->cmdHead ), FRAME_ALLOC_DRAW_COMMAND );
  69. frameData->cmdHead->commandId = RC_NOP;
  70. frameData->cmdHead->next = NULL;
  71. }
  72. /*
  73. =====================
  74. R_ShutdownFrameData
  75. =====================
  76. */
  77. void R_ShutdownFrameData() {
  78. frameData = NULL;
  79. for ( int i = 0; i < NUM_FRAME_DATA; i++ ) {
  80. Mem_Free16( smpFrameData[i].frameMemory );
  81. smpFrameData[i].frameMemory = NULL;
  82. }
  83. }
  84. /*
  85. =====================
  86. R_InitFrameData
  87. =====================
  88. */
  89. void R_InitFrameData() {
  90. R_ShutdownFrameData();
  91. for ( int i = 0; i < NUM_FRAME_DATA; i++ ) {
  92. smpFrameData[i].frameMemory = (byte *) Mem_Alloc16( MAX_FRAME_MEMORY, TAG_RENDER );
  93. }
  94. // must be set before calling R_ToggleSmpFrame()
  95. frameData = &smpFrameData[ 0 ];
  96. R_ToggleSmpFrame();
  97. }
  98. /*
  99. ================
  100. R_FrameAlloc
  101. This data will be automatically freed when the
  102. current frame's back end completes.
  103. This should only be called by the front end. The
  104. back end shouldn't need to allocate memory.
  105. All temporary data, like dynamic tesselations
  106. and local spaces are allocated here.
  107. All memory is cache-line-cleared for the best performance.
  108. ================
  109. */
  110. void *R_FrameAlloc( int bytes, frameAllocType_t type ) {
  111. #if defined( TRACK_FRAME_ALLOCS )
  112. frameData->frameMemoryUsed.Add( bytes );
  113. frameAllocTypeCount[type].Add( bytes );
  114. #endif
  115. bytes = ( bytes + FRAME_ALLOC_ALIGNMENT - 1 ) & ~ ( FRAME_ALLOC_ALIGNMENT - 1 );
  116. // thread safe add
  117. int end = frameData->frameMemoryAllocated.Add( bytes );
  118. if ( end > MAX_FRAME_MEMORY ) {
  119. idLib::Error( "R_FrameAlloc ran out of memory. bytes = %d, end = %d, highWaterAllocated = %d\n", bytes, end, frameData->highWaterAllocated );
  120. }
  121. byte * ptr = frameData->frameMemory + end - bytes;
  122. // cache line clear the memory
  123. for ( int offset = 0; offset < bytes; offset += CACHE_LINE_SIZE ) {
  124. ZeroCacheLine( ptr, offset );
  125. }
  126. return ptr;
  127. }
  128. /*
  129. ==================
  130. R_ClearedFrameAlloc
  131. ==================
  132. */
  133. void *R_ClearedFrameAlloc( int bytes, frameAllocType_t type ) {
  134. // NOTE: every allocation is cache line cleared
  135. return R_FrameAlloc( bytes, type );
  136. }
  137. /*
  138. ==========================================================================================
  139. FONT-END STATIC MEMORY ALLOCATION
  140. ==========================================================================================
  141. */
  142. /*
  143. =================
  144. R_StaticAlloc
  145. =================
  146. */
  147. void *R_StaticAlloc( int bytes, const memTag_t tag ) {
  148. tr.pc.c_alloc++;
  149. void * buf = Mem_Alloc( bytes, tag );
  150. // don't exit on failure on zero length allocations since the old code didn't
  151. if ( buf == NULL && bytes != 0 ) {
  152. common->FatalError( "R_StaticAlloc failed on %i bytes", bytes );
  153. }
  154. return buf;
  155. }
  156. /*
  157. =================
  158. R_ClearedStaticAlloc
  159. =================
  160. */
  161. void *R_ClearedStaticAlloc( int bytes ) {
  162. void * buf = R_StaticAlloc( bytes );
  163. memset( buf, 0, bytes );
  164. return buf;
  165. }
  166. /*
  167. =================
  168. R_StaticFree
  169. =================
  170. */
  171. void R_StaticFree( void *data ) {
  172. tr.pc.c_free++;
  173. Mem_Free( data );
  174. }
  175. /*
  176. ==========================================================================================
  177. FONT-END RENDERING
  178. ==========================================================================================
  179. */
  180. /*
  181. =================
  182. R_SortDrawSurfs
  183. =================
  184. */
  185. static void R_SortDrawSurfs( drawSurf_t ** drawSurfs, const int numDrawSurfs ) {
  186. #if 1
  187. uint64 * indices = (uint64 *) _alloca16( numDrawSurfs * sizeof( indices[0] ) );
  188. // sort the draw surfs based on:
  189. // 1. sort value (largest first)
  190. // 2. depth (smallest first)
  191. // 3. index (largest first)
  192. assert( numDrawSurfs <= 0xFFFF );
  193. for ( int i = 0; i < numDrawSurfs; i++ ) {
  194. float sort = SS_POST_PROCESS - drawSurfs[i]->sort;
  195. assert( sort >= 0.0f );
  196. uint64 dist = 0;
  197. if ( drawSurfs[i]->frontEndGeo != NULL ) {
  198. float min = 0.0f;
  199. float max = 1.0f;
  200. idRenderMatrix::DepthBoundsForBounds( min, max, drawSurfs[i]->space->mvp, drawSurfs[i]->frontEndGeo->bounds );
  201. dist = idMath::Ftoui16( min * 0xFFFF );
  202. }
  203. indices[i] = ( ( numDrawSurfs - i ) & 0xFFFF ) | ( dist << 16 ) | ( (uint64) ( *(uint32 *)&sort ) << 32 );
  204. }
  205. const int64 MAX_LEVELS = 128;
  206. int64 lo[MAX_LEVELS];
  207. int64 hi[MAX_LEVELS];
  208. // Keep the top of the stack in registers to avoid load-hit-stores.
  209. register int64 st_lo = 0;
  210. register int64 st_hi = numDrawSurfs - 1;
  211. register int64 level = 0;
  212. for ( ; ; ) {
  213. register int64 i = st_lo;
  214. register int64 j = st_hi;
  215. if ( j - i >= 4 && level < MAX_LEVELS - 1 ) {
  216. register uint64 pivot = indices[( i + j ) / 2];
  217. do {
  218. while ( indices[i] > pivot ) i++;
  219. while ( indices[j] < pivot ) j--;
  220. if ( i > j ) break;
  221. uint64 h = indices[i]; indices[i] = indices[j]; indices[j] = h;
  222. } while ( ++i <= --j );
  223. // No need for these iterations because we are always sorting unique values.
  224. //while ( indices[j] == pivot && st_lo < j ) j--;
  225. //while ( indices[i] == pivot && i < st_hi ) i++;
  226. assert( level < MAX_LEVELS - 1 );
  227. lo[level] = i;
  228. hi[level] = st_hi;
  229. st_hi = j;
  230. level++;
  231. } else {
  232. for( ; i < j; j-- ) {
  233. register int64 m = i;
  234. for ( int64 k = i + 1; k <= j; k++ ) {
  235. if ( indices[k] < indices[m] ) {
  236. m = k;
  237. }
  238. }
  239. uint64 h = indices[m]; indices[m] = indices[j]; indices[j] = h;
  240. }
  241. if ( --level < 0 ) {
  242. break;
  243. }
  244. st_lo = lo[level];
  245. st_hi = hi[level];
  246. }
  247. }
  248. drawSurf_t ** newDrawSurfs = (drawSurf_t **) indices;
  249. for ( int i = 0; i < numDrawSurfs; i++ ) {
  250. newDrawSurfs[i] = drawSurfs[numDrawSurfs - ( indices[i] & 0xFFFF )];
  251. }
  252. memcpy( drawSurfs, newDrawSurfs, numDrawSurfs * sizeof( drawSurfs[0] ) );
  253. #else
  254. struct local_t {
  255. static int R_QsortSurfaces( const void *a, const void *b ) {
  256. const drawSurf_t * ea = *(drawSurf_t **)a;
  257. const drawSurf_t * eb = *(drawSurf_t **)b;
  258. if ( ea->sort < eb->sort ) {
  259. return -1;
  260. }
  261. if ( ea->sort > eb->sort ) {
  262. return 1;
  263. }
  264. return 0;
  265. }
  266. };
  267. // Add a sort offset so surfaces with equal sort orders still deterministically
  268. // draw in the order they were added, at least within a given model.
  269. float sorfOffset = 0.0f;
  270. for ( int i = 0; i < numDrawSurfs; i++ ) {
  271. drawSurf[i]->sort += sorfOffset;
  272. sorfOffset += 0.000001f;
  273. }
  274. // sort the drawsurfs
  275. qsort( drawSurfs, numDrawSurfs, sizeof( drawSurfs[0] ), local_t::R_QsortSurfaces );
  276. #endif
  277. }
  278. /*
  279. ================
  280. R_RenderView
  281. A view may be either the actual camera view,
  282. a mirror / remote location, or a 3D view on a gui surface.
  283. Parms will typically be allocated with R_FrameAlloc
  284. ================
  285. */
  286. void R_RenderView( viewDef_t *parms ) {
  287. // save view in case we are a subview
  288. viewDef_t * oldView = tr.viewDef;
  289. tr.viewDef = parms;
  290. // setup the matrix for world space to eye space
  291. R_SetupViewMatrix( tr.viewDef );
  292. // we need to set the projection matrix before doing
  293. // portal-to-screen scissor calculations
  294. R_SetupProjectionMatrix( tr.viewDef );
  295. // setup render matrices for faster culling
  296. idRenderMatrix::Transpose( *(idRenderMatrix *)tr.viewDef->projectionMatrix, tr.viewDef->projectionRenderMatrix );
  297. idRenderMatrix viewRenderMatrix;
  298. idRenderMatrix::Transpose( *(idRenderMatrix *)tr.viewDef->worldSpace.modelViewMatrix, viewRenderMatrix );
  299. idRenderMatrix::Multiply( tr.viewDef->projectionRenderMatrix, viewRenderMatrix, tr.viewDef->worldSpace.mvp );
  300. // the planes of the view frustum are needed for portal visibility culling
  301. idRenderMatrix::GetFrustumPlanes( tr.viewDef->frustum, tr.viewDef->worldSpace.mvp, false, true );
  302. // the DOOM 3 frustum planes point outside the frustum
  303. for ( int i = 0; i < 6; i++ ) {
  304. tr.viewDef->frustum[i] = - tr.viewDef->frustum[i];
  305. }
  306. // remove the Z-near to avoid portals from being near clipped
  307. tr.viewDef->frustum[4][3] -= r_znear.GetFloat();
  308. // identify all the visible portal areas, and create view lights and view entities
  309. // for all the the entityDefs and lightDefs that are in the visible portal areas
  310. static_cast<idRenderWorldLocal *>(parms->renderWorld)->FindViewLightsAndEntities();
  311. // wait for any shadow volume jobs from the previous frame to finish
  312. tr.frontEndJobList->Wait();
  313. // make sure that interactions exist for all light / entity combinations that are visible
  314. // add any pre-generated light shadows, and calculate the light shader values
  315. R_AddLights();
  316. // adds ambient surfaces and create any necessary interaction surfaces to add to the light lists
  317. R_AddModels();
  318. // build up the GUIs on world surfaces
  319. R_AddInGameGuis( tr.viewDef->drawSurfs, tr.viewDef->numDrawSurfs );
  320. // any viewLight that didn't have visible surfaces can have it's shadows removed
  321. R_OptimizeViewLightsList();
  322. // sort all the ambient surfaces for translucency ordering
  323. R_SortDrawSurfs( tr.viewDef->drawSurfs, tr.viewDef->numDrawSurfs );
  324. // generate any subviews (mirrors, cameras, etc) before adding this view
  325. if ( R_GenerateSubViews( tr.viewDef->drawSurfs, tr.viewDef->numDrawSurfs ) ) {
  326. // if we are debugging subviews, allow the skipping of the main view draw
  327. if ( r_subviewOnly.GetBool() ) {
  328. return;
  329. }
  330. }
  331. // write everything needed to the demo file
  332. if ( common->WriteDemo() ) {
  333. static_cast<idRenderWorldLocal *>(parms->renderWorld)->WriteVisibleDefs( tr.viewDef );
  334. }
  335. // add the rendering commands for this viewDef
  336. R_AddDrawViewCmd( parms, false );
  337. // restore view in case we are a subview
  338. tr.viewDef = oldView;
  339. }
  340. /*
  341. ================
  342. R_RenderPostProcess
  343. Because R_RenderView may be called by subviews we have to make sure the post process
  344. pass happens after the active view and its subviews is done rendering.
  345. ================
  346. */
  347. void R_RenderPostProcess( viewDef_t *parms ) {
  348. viewDef_t * oldView = tr.viewDef;
  349. R_AddDrawPostProcess( parms );
  350. tr.viewDef = oldView;
  351. }