Advertisement
Guest User

Untitled

a guest
Aug 31st, 2012
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.53 KB | None | 0 0
  1. /*
  2. ===========================================================================
  3.  
  4. Doom 3 GPL Source Code
  5. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  6.  
  7. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  8.  
  9. Doom 3 Source Code is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License, or
  12. (at your option) any later version.
  13.  
  14. Doom 3 Source Code is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  21.  
  22. In addition, the Doom 3 Source Code is also subject to certain additional terms.
  23. You should have received a copy of these additional terms immediately following
  24. the terms and conditions of the GNU General Public License which accompanied the
  25. Doom 3 Source Code. If not, please request a copy in writing from id Software
  26. at the address below.
  27.  
  28. If you have questions concerning this license or the applicable additional terms,
  29. you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120,
  30. Rockville, Maryland 20850 USA.
  31.  
  32. ===========================================================================
  33. */
  34.  
  35. #include "../idlib/precompiled.h"
  36.  
  37.  
  38. #include "tr_local.h"
  39.  
  40.  
  41. static const int FRAME_MEMORY_BYTES = 0x200000;
  42. static const int EXPAND_HEADERS = 32;
  43.  
  44. idCVar idVertexCache::r_showVertexCache ("r_showVertexCache", "0", CVAR_INTEGER | CVAR_RENDERER, "");
  45. idCVar r_reuseVertexCacheSooner ("r_reuseVertexCacheSooner", "0", CVAR_BOOL | CVAR_RENDERER, "reuse vertex buffers as soon as possible after freeing");
  46. idCVar idVertexCache::r_vertexBufferMegs ("r_vertexBufferMegs", "32", CVAR_INTEGER | CVAR_RENDERER, "");
  47.  
  48. idVertexCache vertexCache;
  49.  
  50.  
  51. GLuint gl_current_array_buffer = 0;
  52. GLuint gl_current_index_buffer = 0;
  53.  
  54. static void GL_BindBuffer (GLenum target, GLuint buffer)
  55. {
  56. if (target == GL_ARRAY_BUFFER)
  57. {
  58. if (gl_current_array_buffer != buffer)
  59. gl_current_array_buffer = buffer;
  60. else return;
  61. }
  62. else if (target == GL_ELEMENT_ARRAY_BUFFER)
  63. {
  64. if (gl_current_index_buffer != buffer)
  65. gl_current_index_buffer = buffer;
  66. else return;
  67. }
  68. else
  69. {
  70. common->Error ("GL_BindBuffer : invalid buffer target : %i\n", (int) target);
  71. return;
  72. }
  73.  
  74. glBindBuffer (target, buffer);
  75. }
  76.  
  77.  
  78. /*
  79. ==============
  80. R_ListVertexCache_f
  81. ==============
  82. */
  83. static void R_ListVertexCache_f (const idCmdArgs &args)
  84. {
  85. vertexCache.List ();
  86. }
  87.  
  88. /*
  89. ==============
  90. idVertexCache::ActuallyFree
  91. ==============
  92. */
  93. void idVertexCache::ActuallyFree (vertCache_t *block)
  94. {
  95. if (!block)
  96. {
  97. common->Error ("idVertexCache Free: NULL pointer");
  98. }
  99.  
  100. if (block->user)
  101. {
  102. // let the owner know we have purged it
  103. *block->user = NULL;
  104. block->user = NULL;
  105. }
  106.  
  107. // temp blocks are in a shared space that won't be freed
  108. if (block->tag != TAG_TEMP)
  109. {
  110. this->staticAllocTotal -= block->size;
  111. this->staticCountTotal--;
  112.  
  113. if (block->vbo)
  114. {
  115. }
  116. else if (block->virtMem)
  117. {
  118. Mem_Free (block->virtMem);
  119. block->virtMem = NULL;
  120. }
  121. }
  122.  
  123. block->tag = TAG_FREE; // mark as free
  124.  
  125. // unlink stick it back on the free list
  126. block->next->prev = block->prev;
  127. block->prev->next = block->next;
  128.  
  129. if (r_reuseVertexCacheSooner.GetBool ())
  130. {
  131. // stick it on the front of the free list so it will be reused immediately
  132. block->next = this->freeStaticHeaders.next;
  133. block->prev = &this->freeStaticHeaders;
  134. }
  135. else
  136. {
  137. // stick it on the back of the free list so it won't be reused soon (just for debugging)
  138. block->next = &this->freeStaticHeaders;
  139. block->prev = this->freeStaticHeaders.prev;
  140. }
  141.  
  142. block->next->prev = block;
  143. block->prev->next = block;
  144. }
  145.  
  146. /*
  147. ==============
  148. idVertexCache::Position
  149.  
  150. this will be a real pointer with virtual memory,
  151. but it will be an int offset cast to a pointer with
  152. ARB_vertex_buffer_object
  153.  
  154. The ARB_vertex_buffer_object will be bound
  155. ==============
  156. */
  157. void *idVertexCache::Position (vertCache_t *buffer)
  158. {
  159. if (!buffer || buffer->tag == TAG_FREE)
  160. common->FatalError ("idVertexCache::Position: bad vertCache_t");
  161.  
  162. // the ARB vertex object just uses an offset
  163. if (buffer->vbo)
  164. {
  165. if (r_showVertexCache.GetInteger () == 2)
  166. {
  167. if (buffer->tag == TAG_TEMP)
  168. common->Printf ("GL_ARRAY_BUFFER = %i + %i (%i bytes)\n", buffer->vbo, buffer->offset, buffer->size);
  169. else common->Printf ("GL_ARRAY_BUFFER = %i (%i bytes)\n", buffer->vbo, buffer->size);
  170. }
  171.  
  172. GL_BindBuffer ((buffer->indexBuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER), buffer->vbo);
  173.  
  174. return (void *) buffer->offset;
  175. }
  176. else
  177. {
  178. // virtual memory is a real pointer
  179. return (void *) ((byte *) buffer->virtMem + buffer->offset);
  180. }
  181. }
  182.  
  183.  
  184. void idVertexCache::UnbindIndex ()
  185. {
  186. GL_BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
  187. }
  188.  
  189.  
  190. //================================================================================
  191.  
  192. /*
  193. ===========
  194. idVertexCache::Init
  195. ===========
  196. */
  197. void idVertexCache::Init ()
  198. {
  199. cmdSystem->AddCommand ("listVertexCache", R_ListVertexCache_f, CMD_FL_RENDERER, "lists vertex cache");
  200.  
  201. if (r_vertexBufferMegs.GetInteger () < 8)
  202. r_vertexBufferMegs.SetInteger (8);
  203.  
  204. // use ARB_vertex_buffer_object unless explicitly disabled
  205. if (glConfig.ARBVertexBufferObjectAvailable)
  206. {
  207. virtualMemory = false;
  208. common->Printf ("using ARB_vertex_buffer_object memory\n");
  209. }
  210. else
  211. {
  212. virtualMemory = true;
  213. common->Printf ("WARNING: vertex array range in virtual memory (SLOW)\n");
  214. }
  215.  
  216. // initialize the cache memory blocks
  217. this->freeStaticHeaders.next = this->freeStaticHeaders.prev = &this->freeStaticHeaders;
  218. staticHeaders.next = staticHeaders.prev = &staticHeaders;
  219. freeDynamicHeaders.next = freeDynamicHeaders.prev = &freeDynamicHeaders;
  220. dynamicHeaders.next = dynamicHeaders.prev = &dynamicHeaders;
  221. deferredFreeList.next = deferredFreeList.prev = &deferredFreeList;
  222.  
  223. // set up the dynamic frame memory
  224. frameBytes = FRAME_MEMORY_BYTES;
  225. this->staticAllocTotal = 0;
  226.  
  227. byte *junk = (byte *) Mem_Alloc (frameBytes);
  228.  
  229. for (int i = 0; i < NUM_VERTEX_FRAMES; i++)
  230. {
  231. this->allocatingTempBuffer = true; // force the alloc to use GL_STREAM_DRAW
  232. this->Alloc (junk, this->frameBytes, &this->tempBuffers[i]);
  233. this->allocatingTempBuffer = false;
  234. this->tempBuffers[i]->tag = TAG_FIXED;
  235.  
  236. // unlink these from the static list, so they won't ever get purged
  237. this->tempBuffers[i]->next->prev = this->tempBuffers[i]->prev;
  238. this->tempBuffers[i]->prev->next = this->tempBuffers[i]->next;
  239. }
  240.  
  241. Mem_Free (junk);
  242.  
  243. this->EndFrame ();
  244. }
  245.  
  246.  
  247. /*
  248. ===========
  249. idVertexCache::PurgeAll
  250.  
  251. Used when toggling vertex programs on or off, because
  252. the cached data isn't valid
  253. ===========
  254. */
  255. void idVertexCache::PurgeAll ()
  256. {
  257. while (staticHeaders.next != &staticHeaders)
  258. {
  259. ActuallyFree (staticHeaders.next);
  260. }
  261. }
  262.  
  263. /*
  264. ===========
  265. idVertexCache::Shutdown
  266. ===========
  267. */
  268. void idVertexCache::Shutdown ()
  269. {
  270. // PurgeAll (); // !@#: also purge the temp buffers
  271.  
  272. headerAllocator.Shutdown ();
  273. }
  274.  
  275. /*
  276. ===========
  277. idVertexCache::Alloc
  278. ===========
  279. */
  280. void idVertexCache::Alloc (void *data, int size, vertCache_t **buffer, bool indexBuffer)
  281. {
  282. vertCache_t *block = NULL;
  283.  
  284. if (size <= 0)
  285. {
  286. common->Error ("idVertexCache::Alloc: size = %i\n", size);
  287. }
  288.  
  289. // if we can't find anything, it will be NULL
  290. *buffer = NULL;
  291.  
  292. // if we don't have any remaining unused headers, allocate some more
  293. if (this->freeStaticHeaders.next == &this->freeStaticHeaders)
  294. {
  295. for (int i = 0; i < EXPAND_HEADERS; i++)
  296. {
  297. block = headerAllocator.Alloc ();
  298.  
  299. if (!virtualMemory)
  300. {
  301. glGenBuffers (1, &block->vbo);
  302. block->size = 0;
  303. }
  304.  
  305. block->next = this->freeStaticHeaders.next;
  306. block->prev = &this->freeStaticHeaders;
  307. block->next->prev = block;
  308. block->prev->next = block;
  309. }
  310. }
  311.  
  312. GLenum target = (indexBuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER);
  313. GLenum usage = (allocatingTempBuffer ? GL_STREAM_DRAW : GL_STATIC_DRAW);
  314.  
  315. // try to find a matching block to replace so that we're not continually respecifying vbo data each frame
  316. for (vertCache_t *findblock = this->freeStaticHeaders.next; ; findblock = findblock->next)
  317. {
  318. if (findblock == &this->freeStaticHeaders)
  319. {
  320. block = this->freeStaticHeaders.next;
  321. break;
  322. }
  323.  
  324. if (findblock->target != target) continue;
  325. if (findblock->usage != usage) continue;
  326. if (findblock->size != size) continue;
  327.  
  328. block = findblock;
  329. break;
  330. }
  331.  
  332. // move it from the freeStaticHeaders list to the staticHeaders list
  333. block->target = target;
  334. block->usage = usage;
  335.  
  336. if (block->vbo)
  337. {
  338. // orphan the buffer in case it needs respecifying (it usually will)
  339. GL_BindBuffer (target, block->vbo);
  340. glBufferData (target, (GLsizeiptr) size, NULL, usage);
  341. glBufferData (target, (GLsizeiptr) size, data, usage);
  342. }
  343. else
  344. {
  345. block->virtMem = Mem_Alloc (size);
  346. memcpy (block->virtMem, data, size);
  347. }
  348.  
  349. block->next->prev = block->prev;
  350. block->prev->next = block->next;
  351. block->next = staticHeaders.next;
  352. block->prev = &staticHeaders;
  353. block->next->prev = block;
  354. block->prev->next = block;
  355.  
  356. block->size = size;
  357. block->offset = 0;
  358. block->tag = TAG_USED;
  359.  
  360. // save data for debugging
  361. this->staticAllocThisFrame += block->size;
  362. this->staticCountThisFrame++;
  363. this->staticCountTotal++;
  364. this->staticAllocTotal += block->size;
  365.  
  366. // this will be set to zero when it is purged
  367. block->user = buffer;
  368. *buffer = block;
  369.  
  370. // allocation doesn't imply used-for-drawing, because at level
  371. // load time lots of things may be created, but they aren't
  372. // referenced by the GPU yet, and can be purged if needed.
  373. block->frameUsed = currentFrame - NUM_VERTEX_FRAMES;
  374. block->indexBuffer = indexBuffer;
  375. }
  376.  
  377. /*
  378. ===========
  379. idVertexCache::Touch
  380. ===========
  381. */
  382. void idVertexCache::Touch (vertCache_t *block)
  383. {
  384. if (!block)
  385. {
  386. common->Error ("idVertexCache Touch: NULL pointer");
  387. }
  388.  
  389. if (block->tag == TAG_FREE)
  390. {
  391. common->FatalError ("idVertexCache Touch: freed pointer");
  392. }
  393.  
  394. if (block->tag == TAG_TEMP)
  395. {
  396. common->FatalError ("idVertexCache Touch: temporary pointer");
  397. }
  398.  
  399. block->frameUsed = currentFrame;
  400.  
  401. // move to the head of the LRU list
  402. block->next->prev = block->prev;
  403. block->prev->next = block->next;
  404.  
  405. block->next = staticHeaders.next;
  406. block->prev = &staticHeaders;
  407. staticHeaders.next->prev = block;
  408. staticHeaders.next = block;
  409. }
  410.  
  411. /*
  412. ===========
  413. idVertexCache::Free
  414. ===========
  415. */
  416. void idVertexCache::Free (vertCache_t *block)
  417. {
  418. if (!block)
  419. {
  420. return;
  421. }
  422.  
  423. if (block->tag == TAG_FREE)
  424. {
  425. common->FatalError ("idVertexCache Free: freed pointer");
  426. }
  427.  
  428. if (block->tag == TAG_TEMP)
  429. {
  430. common->FatalError ("idVertexCache Free: temporary pointer");
  431. }
  432.  
  433. // this block still can't be purged until the frame count has expired,
  434. // but it won't need to clear a user pointer when it is
  435. block->user = NULL;
  436.  
  437. block->next->prev = block->prev;
  438. block->prev->next = block->next;
  439.  
  440. block->next = deferredFreeList.next;
  441. block->prev = &deferredFreeList;
  442. deferredFreeList.next->prev = block;
  443. deferredFreeList.next = block;
  444. }
  445.  
  446. /*
  447. ===========
  448. idVertexCache::AllocFrameTemp
  449.  
  450. A frame temp allocation must never be allowed to fail due to overflow.
  451. We can't simply sync with the GPU and overwrite what we have, because
  452. there may still be future references to dynamically created surfaces.
  453. ===========
  454. */
  455. vertCache_t *idVertexCache::AllocFrameTemp (void *data, int size)
  456. {
  457. vertCache_t *block;
  458.  
  459. if (size <= 0)
  460. {
  461. common->Error ("idVertexCache::AllocFrameTemp: size = %i\n", size);
  462. }
  463.  
  464. if (dynamicAllocThisFrame + size > frameBytes)
  465. {
  466. // if we don't have enough room in the temp block, allocate a static block,
  467. // but immediately free it so it will get freed at the next frame
  468. this->tempOverflow = true;
  469. this->Alloc (data, size, &block);
  470. this->Free (block);
  471. return block;
  472. }
  473.  
  474. // this data is just going on the shared dynamic list
  475.  
  476. // if we don't have any remaining unused headers, allocate some more
  477. if (freeDynamicHeaders.next == &freeDynamicHeaders)
  478. {
  479. for (int i = 0; i < EXPAND_HEADERS; i++)
  480. {
  481. block = headerAllocator.Alloc ();
  482. block->next = freeDynamicHeaders.next;
  483. block->prev = &freeDynamicHeaders;
  484. block->next->prev = block;
  485. block->prev->next = block;
  486. }
  487. }
  488.  
  489. // move it from the freeDynamicHeaders list to the dynamicHeaders list
  490. block = freeDynamicHeaders.next;
  491. block->next->prev = block->prev;
  492. block->prev->next = block->next;
  493. block->next = dynamicHeaders.next;
  494. block->prev = &dynamicHeaders;
  495. block->next->prev = block;
  496. block->prev->next = block;
  497.  
  498. block->size = size;
  499. block->tag = TAG_TEMP;
  500. block->indexBuffer = false;
  501. block->offset = dynamicAllocThisFrame;
  502. dynamicAllocThisFrame += block->size;
  503. dynamicCountThisFrame++;
  504. block->user = NULL;
  505. block->frameUsed = 0;
  506.  
  507. // copy the data
  508. block->virtMem = tempBuffers[listNum]->virtMem;
  509.  
  510. if ((block->vbo = tempBuffers[listNum]->vbo) != 0)
  511. {
  512. GL_BindBuffer (GL_ARRAY_BUFFER, block->vbo);
  513.  
  514. // try to get an unsynchronized map if at all possible
  515. if (GLEW_ARB_map_buffer_range)
  516. {
  517. void *dst = NULL;
  518. GLbitfield access = GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_RANGE_BIT;
  519.  
  520. // if the buffer has wrapped then we orphan it
  521. if (block->offset == 0)
  522. access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT;
  523. else access = GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_RANGE_BIT;
  524. //glBufferData (GL_ARRAY_BUFFER, tempBuffers[listNum]->size, NULL, GL_STREAM_DRAW);
  525.  
  526. if ((dst = glMapBufferRange (GL_ARRAY_BUFFER, block->offset, (GLsizeiptr) size, access)) != NULL)
  527. {
  528. memcpy (dst, data, size);
  529. glUnmapBuffer (GL_ARRAY_BUFFER);
  530. }
  531. else glBufferSubData (GL_ARRAY_BUFFER, block->offset, (GLsizeiptr) size, data);
  532. }
  533. else glBufferSubData (GL_ARRAY_BUFFER, block->offset, (GLsizeiptr) size, data);
  534. }
  535. else memcpy ((byte *) block->virtMem + block->offset, data, size);
  536.  
  537. return block;
  538. }
  539.  
  540. /*
  541. ===========
  542. idVertexCache::EndFrame
  543. ===========
  544. */
  545. void idVertexCache::EndFrame ()
  546. {
  547. // display debug information
  548. if (r_showVertexCache.GetBool ())
  549. {
  550. int staticUseCount = 0;
  551. int staticUseSize = 0;
  552.  
  553. for (vertCache_t *block = staticHeaders.next; block != &staticHeaders; block = block->next)
  554. {
  555. if (block->frameUsed == currentFrame)
  556. {
  557. staticUseCount++;
  558. staticUseSize += block->size;
  559. }
  560. }
  561.  
  562. const char *frameOverflow = tempOverflow ? "(OVERFLOW)" : "";
  563.  
  564. common->Printf ("vertex dynamic:%i=%ik%s, static alloc:%i=%ik used:%i=%ik total:%i=%ik\n",
  565. dynamicCountThisFrame, dynamicAllocThisFrame / 1024, frameOverflow,
  566. this->staticCountThisFrame, this->staticAllocThisFrame / 1024,
  567. staticUseCount, staticUseSize / 1024,
  568. this->staticCountTotal, this->staticAllocTotal / 1024);
  569. }
  570.  
  571. if (!virtualMemory)
  572. {
  573. // unbind vertex buffers so normal virtual memory will be used
  574. GL_BindBuffer (GL_ARRAY_BUFFER, 0);
  575. GL_BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
  576. }
  577.  
  578. currentFrame = tr.frameCount;
  579. listNum = currentFrame % NUM_VERTEX_FRAMES;
  580. this->staticAllocThisFrame = 0;
  581. this->staticCountThisFrame = 0;
  582. dynamicAllocThisFrame = 0;
  583. dynamicCountThisFrame = 0;
  584. tempOverflow = false;
  585.  
  586. // free all the deferred free headers
  587. while (deferredFreeList.next != &deferredFreeList)
  588. {
  589. ActuallyFree (deferredFreeList.next);
  590. }
  591.  
  592. // free all the frame temp headers
  593. vertCache_t *block = dynamicHeaders.next;
  594.  
  595. if (block != &dynamicHeaders)
  596. {
  597. block->prev = &freeDynamicHeaders;
  598. dynamicHeaders.prev->next = freeDynamicHeaders.next;
  599. freeDynamicHeaders.next->prev = dynamicHeaders.prev;
  600. freeDynamicHeaders.next = block;
  601.  
  602. dynamicHeaders.next = dynamicHeaders.prev = &dynamicHeaders;
  603. }
  604. }
  605.  
  606. /*
  607. =============
  608. idVertexCache::List
  609. =============
  610. */
  611. void idVertexCache::List (void)
  612. {
  613. int numActive = 0;
  614. int numDeferred = 0;
  615. int frameStatic = 0;
  616. int totalStatic = 0;
  617. int deferredSpace = 0;
  618.  
  619. vertCache_t *block;
  620.  
  621. for (block = staticHeaders.next; block != &staticHeaders; block = block->next)
  622. {
  623. numActive++;
  624.  
  625. totalStatic += block->size;
  626.  
  627. if (block->frameUsed == currentFrame)
  628. {
  629. frameStatic += block->size;
  630. }
  631. }
  632.  
  633. int numFreeStaticHeaders = 0;
  634.  
  635. for (block = this->freeStaticHeaders.next; block != &this->freeStaticHeaders; block = block->next)
  636. {
  637. numFreeStaticHeaders++;
  638. }
  639.  
  640. int numFreeDynamicHeaders = 0;
  641.  
  642. for (block = freeDynamicHeaders.next; block != &freeDynamicHeaders; block = block->next)
  643. {
  644. numFreeDynamicHeaders++;
  645. }
  646.  
  647. common->Printf ("%i megs working set\n", r_vertexBufferMegs.GetInteger ());
  648. common->Printf ("%i dynamic temp buffers of %ik\n", NUM_VERTEX_FRAMES, frameBytes / 1024);
  649. common->Printf ("%5i active static headers\n", numActive);
  650. common->Printf ("%5i free static headers\n", numFreeStaticHeaders);
  651. common->Printf ("%5i free dynamic headers\n", numFreeDynamicHeaders);
  652.  
  653. if (!virtualMemory)
  654. {
  655. common->Printf ("Vertex cache is in ARB_vertex_buffer_object memory (FAST).\n");
  656. }
  657. else
  658. {
  659. common->Printf ("Vertex cache is in virtual memory (SLOW)\n");
  660. }
  661.  
  662. common->Printf ("Index buffers are accelerated.\n");
  663. }
  664.  
  665.  
  666. /*
  667. =============
  668. idVertexCache::IsFast
  669.  
  670. just for gfxinfo printing
  671. =============
  672. */
  673. bool idVertexCache::IsFast ()
  674. {
  675. if (virtualMemory)
  676. {
  677. return false;
  678. }
  679.  
  680. return true;
  681. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement