VertexCache.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. static const int FRAME_MEMORY_BYTES = 0x200000;
  24. static const int EXPAND_HEADERS = 1024;
  25. idCVar idVertexCache::r_showVertexCache( "r_showVertexCache", "0", CVAR_INTEGER|CVAR_RENDERER, "" );
  26. idCVar idVertexCache::r_vertexBufferMegs( "r_vertexBufferMegs", "32", CVAR_INTEGER|CVAR_RENDERER, "" );
  27. idVertexCache vertexCache;
  28. /*
  29. ==============
  30. R_ListVertexCache_f
  31. ==============
  32. */
  33. static void R_ListVertexCache_f( const idCmdArgs &args ) {
  34. vertexCache.List();
  35. }
  36. /*
  37. ==============
  38. idVertexCache::ActuallyFree
  39. ==============
  40. */
  41. void idVertexCache::ActuallyFree( vertCache_t *block ) {
  42. if (!block) {
  43. common->Error( "idVertexCache Free: NULL pointer" );
  44. }
  45. if ( block->user ) {
  46. // let the owner know we have purged it
  47. *block->user = NULL;
  48. block->user = NULL;
  49. }
  50. // temp blocks are in a shared space that won't be freed
  51. if ( block->tag != TAG_TEMP ) {
  52. staticAllocTotal -= block->size;
  53. staticCountTotal--;
  54. if ( block->vbo ) {
  55. #if 0 // this isn't really necessary, it will be reused soon enough
  56. // filling with zero length data is the equivalent of freeing
  57. qglBindBufferARB(GL_ARRAY_BUFFER_ARB, block->vbo);
  58. qglBufferDataARB(GL_ARRAY_BUFFER_ARB, 0, 0, GL_DYNAMIC_DRAW_ARB);
  59. #endif
  60. } else if ( block->virtMem ) {
  61. Mem_Free( block->virtMem );
  62. block->virtMem = NULL;
  63. }
  64. }
  65. block->tag = TAG_FREE; // mark as free
  66. // unlink stick it back on the free list
  67. block->next->prev = block->prev;
  68. block->prev->next = block->next;
  69. #if 1
  70. // stick it on the front of the free list so it will be reused immediately
  71. block->next = freeStaticHeaders.next;
  72. block->prev = &freeStaticHeaders;
  73. #else
  74. // stick it on the back of the free list so it won't be reused soon (just for debugging)
  75. block->next = &freeStaticHeaders;
  76. block->prev = freeStaticHeaders.prev;
  77. #endif
  78. block->next->prev = block;
  79. block->prev->next = block;
  80. }
  81. /*
  82. ==============
  83. idVertexCache::Position
  84. this will be a real pointer with virtual memory,
  85. but it will be an int offset cast to a pointer with
  86. ARB_vertex_buffer_object
  87. The ARB_vertex_buffer_object will be bound
  88. ==============
  89. */
  90. void *idVertexCache::Position( vertCache_t *buffer ) {
  91. if ( !buffer || buffer->tag == TAG_FREE ) {
  92. common->FatalError( "idVertexCache::Position: bad vertCache_t" );
  93. }
  94. // the ARB vertex object just uses an offset
  95. if ( buffer->vbo ) {
  96. if ( r_showVertexCache.GetInteger() == 2 ) {
  97. if ( buffer->tag == TAG_TEMP ) {
  98. common->Printf( "GL_ARRAY_BUFFER_ARB = %i + %i (%i bytes)\n", buffer->vbo, buffer->offset, buffer->size );
  99. } else {
  100. common->Printf( "GL_ARRAY_BUFFER_ARB = %i (%i bytes)\n", buffer->vbo, buffer->size );
  101. }
  102. }
  103. if ( buffer->indexBuffer ) {
  104. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, buffer->vbo );
  105. } else {
  106. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, buffer->vbo );
  107. }
  108. return (void *)buffer->offset;
  109. }
  110. // virtual memory is a real pointer
  111. return (void *)((byte *)buffer->virtMem + buffer->offset);
  112. }
  113. void idVertexCache::UnbindIndex() {
  114. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
  115. }
  116. //================================================================================
  117. /*
  118. ===========
  119. idVertexCache::Init
  120. ===========
  121. */
  122. void idVertexCache::Init() {
  123. cmdSystem->AddCommand( "listVertexCache", R_ListVertexCache_f, CMD_FL_RENDERER, "lists vertex cache" );
  124. if ( r_vertexBufferMegs.GetInteger() < 8 ) {
  125. r_vertexBufferMegs.SetInteger( 8 );
  126. }
  127. virtualMemory = false;
  128. // use ARB_vertex_buffer_object unless explicitly disabled
  129. if( r_useVertexBuffers.GetInteger() && glConfig.ARBVertexBufferObjectAvailable ) {
  130. common->Printf( "using ARB_vertex_buffer_object memory\n" );
  131. } else {
  132. virtualMemory = true;
  133. r_useIndexBuffers.SetBool( false );
  134. common->Printf( "WARNING: vertex array range in virtual memory (SLOW)\n" );
  135. }
  136. // initialize the cache memory blocks
  137. freeStaticHeaders.next = freeStaticHeaders.prev = &freeStaticHeaders;
  138. staticHeaders.next = staticHeaders.prev = &staticHeaders;
  139. freeDynamicHeaders.next = freeDynamicHeaders.prev = &freeDynamicHeaders;
  140. dynamicHeaders.next = dynamicHeaders.prev = &dynamicHeaders;
  141. deferredFreeList.next = deferredFreeList.prev = &deferredFreeList;
  142. // set up the dynamic frame memory
  143. frameBytes = FRAME_MEMORY_BYTES;
  144. staticAllocTotal = 0;
  145. byte *junk = (byte *)Mem_Alloc( frameBytes );
  146. for ( int i = 0 ; i < NUM_VERTEX_FRAMES ; i++ ) {
  147. allocatingTempBuffer = true; // force the alloc to use GL_STREAM_DRAW_ARB
  148. Alloc( junk, frameBytes, &tempBuffers[i] );
  149. allocatingTempBuffer = false;
  150. tempBuffers[i]->tag = TAG_FIXED;
  151. // unlink these from the static list, so they won't ever get purged
  152. tempBuffers[i]->next->prev = tempBuffers[i]->prev;
  153. tempBuffers[i]->prev->next = tempBuffers[i]->next;
  154. }
  155. Mem_Free( junk );
  156. EndFrame();
  157. }
  158. /*
  159. ===========
  160. idVertexCache::PurgeAll
  161. Used when toggling vertex programs on or off, because
  162. the cached data isn't valid
  163. ===========
  164. */
  165. void idVertexCache::PurgeAll() {
  166. while( staticHeaders.next != &staticHeaders ) {
  167. ActuallyFree( staticHeaders.next );
  168. }
  169. }
  170. /*
  171. ===========
  172. idVertexCache::Shutdown
  173. ===========
  174. */
  175. void idVertexCache::Shutdown() {
  176. // PurgeAll(); // !@#: also purge the temp buffers
  177. headerAllocator.Shutdown();
  178. }
  179. /*
  180. ===========
  181. idVertexCache::Alloc
  182. ===========
  183. */
  184. void idVertexCache::Alloc( void *data, int size, vertCache_t **buffer, bool indexBuffer ) {
  185. vertCache_t *block;
  186. if ( size <= 0 ) {
  187. common->Error( "idVertexCache::Alloc: size = %i\n", size );
  188. }
  189. // if we can't find anything, it will be NULL
  190. *buffer = NULL;
  191. // if we don't have any remaining unused headers, allocate some more
  192. if ( freeStaticHeaders.next == &freeStaticHeaders ) {
  193. for ( int i = 0; i < EXPAND_HEADERS; i++ ) {
  194. block = headerAllocator.Alloc();
  195. block->next = freeStaticHeaders.next;
  196. block->prev = &freeStaticHeaders;
  197. block->next->prev = block;
  198. block->prev->next = block;
  199. if( !virtualMemory ) {
  200. qglGenBuffersARB( 1, & block->vbo );
  201. }
  202. }
  203. }
  204. // move it from the freeStaticHeaders list to the staticHeaders list
  205. block = freeStaticHeaders.next;
  206. block->next->prev = block->prev;
  207. block->prev->next = block->next;
  208. block->next = staticHeaders.next;
  209. block->prev = &staticHeaders;
  210. block->next->prev = block;
  211. block->prev->next = block;
  212. block->size = size;
  213. block->offset = 0;
  214. block->tag = TAG_USED;
  215. // save data for debugging
  216. staticAllocThisFrame += block->size;
  217. staticCountThisFrame++;
  218. staticCountTotal++;
  219. staticAllocTotal += block->size;
  220. // this will be set to zero when it is purged
  221. block->user = buffer;
  222. *buffer = block;
  223. // allocation doesn't imply used-for-drawing, because at level
  224. // load time lots of things may be created, but they aren't
  225. // referenced by the GPU yet, and can be purged if needed.
  226. block->frameUsed = currentFrame - NUM_VERTEX_FRAMES;
  227. block->indexBuffer = indexBuffer;
  228. // copy the data
  229. if ( block->vbo ) {
  230. if ( indexBuffer ) {
  231. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, block->vbo );
  232. qglBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, (GLsizeiptrARB)size, data, GL_STATIC_DRAW_ARB );
  233. } else {
  234. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, block->vbo );
  235. if ( allocatingTempBuffer ) {
  236. qglBufferDataARB( GL_ARRAY_BUFFER_ARB, (GLsizeiptrARB)size, data, GL_STREAM_DRAW_ARB );
  237. } else {
  238. qglBufferDataARB( GL_ARRAY_BUFFER_ARB, (GLsizeiptrARB)size, data, GL_STATIC_DRAW_ARB );
  239. }
  240. }
  241. } else {
  242. block->virtMem = Mem_Alloc( size );
  243. SIMDProcessor->Memcpy( block->virtMem, data, size );
  244. }
  245. }
  246. /*
  247. ===========
  248. idVertexCache::Touch
  249. ===========
  250. */
  251. void idVertexCache::Touch( vertCache_t *block ) {
  252. if ( !block ) {
  253. common->Error( "idVertexCache Touch: NULL pointer" );
  254. }
  255. if ( block->tag == TAG_FREE ) {
  256. common->FatalError( "idVertexCache Touch: freed pointer" );
  257. }
  258. if ( block->tag == TAG_TEMP ) {
  259. common->FatalError( "idVertexCache Touch: temporary pointer" );
  260. }
  261. block->frameUsed = currentFrame;
  262. // move to the head of the LRU list
  263. block->next->prev = block->prev;
  264. block->prev->next = block->next;
  265. block->next = staticHeaders.next;
  266. block->prev = &staticHeaders;
  267. staticHeaders.next->prev = block;
  268. staticHeaders.next = block;
  269. }
  270. /*
  271. ===========
  272. idVertexCache::Free
  273. ===========
  274. */
  275. void idVertexCache::Free( vertCache_t *block ) {
  276. if (!block) {
  277. return;
  278. }
  279. if ( block->tag == TAG_FREE ) {
  280. common->FatalError( "idVertexCache Free: freed pointer" );
  281. }
  282. if ( block->tag == TAG_TEMP ) {
  283. common->FatalError( "idVertexCache Free: temporary pointer" );
  284. }
  285. // this block still can't be purged until the frame count has expired,
  286. // but it won't need to clear a user pointer when it is
  287. block->user = NULL;
  288. block->next->prev = block->prev;
  289. block->prev->next = block->next;
  290. block->next = deferredFreeList.next;
  291. block->prev = &deferredFreeList;
  292. deferredFreeList.next->prev = block;
  293. deferredFreeList.next = block;
  294. }
  295. /*
  296. ===========
  297. idVertexCache::AllocFrameTemp
  298. A frame temp allocation must never be allowed to fail due to overflow.
  299. We can't simply sync with the GPU and overwrite what we have, because
  300. there may still be future references to dynamically created surfaces.
  301. ===========
  302. */
  303. vertCache_t *idVertexCache::AllocFrameTemp( void *data, int size ) {
  304. vertCache_t *block;
  305. if ( size <= 0 ) {
  306. common->Error( "idVertexCache::AllocFrameTemp: size = %i\n", size );
  307. }
  308. if ( dynamicAllocThisFrame + size > frameBytes ) {
  309. // if we don't have enough room in the temp block, allocate a static block,
  310. // but immediately free it so it will get freed at the next frame
  311. tempOverflow = true;
  312. Alloc( data, size, &block );
  313. Free( block);
  314. return block;
  315. }
  316. // this data is just going on the shared dynamic list
  317. // if we don't have any remaining unused headers, allocate some more
  318. if ( freeDynamicHeaders.next == &freeDynamicHeaders ) {
  319. for ( int i = 0; i < EXPAND_HEADERS; i++ ) {
  320. block = headerAllocator.Alloc();
  321. block->next = freeDynamicHeaders.next;
  322. block->prev = &freeDynamicHeaders;
  323. block->next->prev = block;
  324. block->prev->next = block;
  325. }
  326. }
  327. // move it from the freeDynamicHeaders list to the dynamicHeaders list
  328. block = freeDynamicHeaders.next;
  329. block->next->prev = block->prev;
  330. block->prev->next = block->next;
  331. block->next = dynamicHeaders.next;
  332. block->prev = &dynamicHeaders;
  333. block->next->prev = block;
  334. block->prev->next = block;
  335. block->size = size;
  336. block->tag = TAG_TEMP;
  337. block->indexBuffer = false;
  338. block->offset = dynamicAllocThisFrame;
  339. dynamicAllocThisFrame += block->size;
  340. dynamicCountThisFrame++;
  341. block->user = NULL;
  342. block->frameUsed = 0;
  343. // copy the data
  344. block->virtMem = tempBuffers[listNum]->virtMem;
  345. block->vbo = tempBuffers[listNum]->vbo;
  346. if ( block->vbo ) {
  347. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, block->vbo );
  348. qglBufferSubDataARB( GL_ARRAY_BUFFER_ARB, block->offset, (GLsizeiptrARB)size, data );
  349. } else {
  350. SIMDProcessor->Memcpy( (byte *)block->virtMem + block->offset, data, size );
  351. }
  352. return block;
  353. }
  354. /*
  355. ===========
  356. idVertexCache::EndFrame
  357. ===========
  358. */
  359. void idVertexCache::EndFrame() {
  360. // display debug information
  361. if ( r_showVertexCache.GetBool() ) {
  362. int staticUseCount = 0;
  363. int staticUseSize = 0;
  364. for ( vertCache_t *block = staticHeaders.next ; block != &staticHeaders ; block = block->next ) {
  365. if ( block->frameUsed == currentFrame ) {
  366. staticUseCount++;
  367. staticUseSize += block->size;
  368. }
  369. }
  370. const char *frameOverflow = tempOverflow ? "(OVERFLOW)" : "";
  371. common->Printf( "vertex dynamic:%i=%ik%s, static alloc:%i=%ik used:%i=%ik total:%i=%ik\n",
  372. dynamicCountThisFrame, dynamicAllocThisFrame/1024, frameOverflow,
  373. staticCountThisFrame, staticAllocThisFrame/1024,
  374. staticUseCount, staticUseSize/1024,
  375. staticCountTotal, staticAllocTotal/1024 );
  376. }
  377. #if 0
  378. // if our total static count is above our working memory limit, start purging things
  379. while ( staticAllocTotal > r_vertexBufferMegs.GetInteger() * 1024 * 1024 ) {
  380. // free the least recently used
  381. }
  382. #endif
  383. if( !virtualMemory ) {
  384. // unbind vertex buffers so normal virtual memory will be used in case
  385. // r_useVertexBuffers / r_useIndexBuffers
  386. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
  387. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
  388. }
  389. currentFrame = tr.frameCount;
  390. listNum = currentFrame % NUM_VERTEX_FRAMES;
  391. staticAllocThisFrame = 0;
  392. staticCountThisFrame = 0;
  393. dynamicAllocThisFrame = 0;
  394. dynamicCountThisFrame = 0;
  395. tempOverflow = false;
  396. // free all the deferred free headers
  397. while( deferredFreeList.next != &deferredFreeList ) {
  398. ActuallyFree( deferredFreeList.next );
  399. }
  400. // free all the frame temp headers
  401. vertCache_t *block = dynamicHeaders.next;
  402. if ( block != &dynamicHeaders ) {
  403. block->prev = &freeDynamicHeaders;
  404. dynamicHeaders.prev->next = freeDynamicHeaders.next;
  405. freeDynamicHeaders.next->prev = dynamicHeaders.prev;
  406. freeDynamicHeaders.next = block;
  407. dynamicHeaders.next = dynamicHeaders.prev = &dynamicHeaders;
  408. }
  409. }
  410. /*
  411. =============
  412. idVertexCache::List
  413. =============
  414. */
  415. void idVertexCache::List( void ) {
  416. int numActive = 0;
  417. int numDeferred = 0;
  418. int frameStatic = 0;
  419. int totalStatic = 0;
  420. int deferredSpace = 0;
  421. vertCache_t *block;
  422. for ( block = staticHeaders.next ; block != &staticHeaders ; block = block->next) {
  423. numActive++;
  424. totalStatic += block->size;
  425. if ( block->frameUsed == currentFrame ) {
  426. frameStatic += block->size;
  427. }
  428. }
  429. int numFreeStaticHeaders = 0;
  430. for ( block = freeStaticHeaders.next ; block != &freeStaticHeaders ; block = block->next ) {
  431. numFreeStaticHeaders++;
  432. }
  433. int numFreeDynamicHeaders = 0;
  434. for ( block = freeDynamicHeaders.next ; block != &freeDynamicHeaders ; block = block->next ) {
  435. numFreeDynamicHeaders++;
  436. }
  437. common->Printf( "%i megs working set\n", r_vertexBufferMegs.GetInteger() );
  438. common->Printf( "%i dynamic temp buffers of %ik\n", NUM_VERTEX_FRAMES, frameBytes / 1024 );
  439. common->Printf( "%5i active static headers\n", numActive );
  440. common->Printf( "%5i free static headers\n", numFreeStaticHeaders );
  441. common->Printf( "%5i free dynamic headers\n", numFreeDynamicHeaders );
  442. if ( !virtualMemory ) {
  443. common->Printf( "Vertex cache is in ARB_vertex_buffer_object memory (FAST).\n");
  444. } else {
  445. common->Printf( "Vertex cache is in virtual memory (SLOW)\n" );
  446. }
  447. if ( r_useIndexBuffers.GetBool() ) {
  448. common->Printf( "Index buffers are accelerated.\n" );
  449. } else {
  450. common->Printf( "Index buffers are not used.\n" );
  451. }
  452. }
  453. /*
  454. =============
  455. idVertexCache::IsFast
  456. just for gfxinfo printing
  457. =============
  458. */
  459. bool idVertexCache::IsFast() {
  460. if ( virtualMemory ) {
  461. return false;
  462. }
  463. return true;
  464. }