VertexCache.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // vertex cache calls should only be made by the front end
  21. const int NUM_VERTEX_FRAMES = 2;
  22. typedef enum {
  23. TAG_FREE,
  24. TAG_USED,
  25. TAG_FIXED, // for the temp buffers
  26. TAG_TEMP // in frame temp area, not static area
  27. } vertBlockTag_t;
  28. typedef struct vertCache_s {
  29. GLuint vbo;
  30. void *virtMem; // only one of vbo / virtMem will be set
  31. bool indexBuffer; // holds indexes instead of vertexes
  32. int offset;
  33. int size; // may be larger than the amount asked for, due
  34. // to round up and minimum fragment sizes
  35. int tag; // a tag of 0 is a free block
  36. struct vertCache_s ** user; // will be set to zero when purged
  37. struct vertCache_s *next, *prev; // may be on the static list or one of the frame lists
  38. int frameUsed; // it can't be purged if near the current frame
  39. } vertCache_t;
  40. class idVertexCache {
  41. public:
  42. void Init();
  43. void Shutdown();
  44. // just for gfxinfo printing
  45. bool IsFast();
  46. // called when vertex programs are enabled or disabled, because
  47. // the cached data is no longer valid
  48. void PurgeAll();
  49. // Tries to allocate space for the given data in fast vertex
  50. // memory, and copies it over.
  51. // Alloc does NOT do a touch, which allows purging of things
  52. // created at level load time even if a frame hasn't passed yet.
  53. // These allocations can be purged, which will zero the pointer.
  54. void Alloc( void *data, int bytes, vertCache_t **buffer, bool indexBuffer = false );
  55. // This will be a real pointer with virtual memory,
  56. // but it will be an int offset cast to a pointer of ARB_vertex_buffer_object
  57. void * Position( vertCache_t *buffer );
  58. // if r_useIndexBuffers is enabled, but you need to draw something without
  59. // an indexCache, this must be called to reset GL_ELEMENT_ARRAY_BUFFER_ARB
  60. void UnbindIndex();
  61. // automatically freed at the end of the next frame
  62. // used for specular texture coordinates and gui drawing, which
  63. // will change every frame.
  64. // will return NULL if the vertex cache is completely full
  65. // As with Position(), this may not actually be a pointer you can access.
  66. vertCache_t * AllocFrameTemp( void *data, int bytes );
  67. // notes that a buffer is used this frame, so it can't be purged
  68. // out from under the GPU
  69. void Touch( vertCache_t *buffer );
  70. // this block won't have to zero a buffer pointer when it is purged,
  71. // but it must still wait for the frames to pass, in case the GPU
  72. // is still referencing it
  73. void Free( vertCache_t *buffer );
  74. // updates the counter for determining which temp space to use
  75. // and which blocks can be purged
  76. // Also prints debugging info when enabled
  77. void EndFrame();
  78. // listVertexCache calls this
  79. void List();
  80. private:
  81. void InitMemoryBlocks( int size );
  82. void ActuallyFree( vertCache_t *block );
  83. static idCVar r_showVertexCache;
  84. static idCVar r_vertexBufferMegs;
  85. int staticCountTotal;
  86. int staticAllocTotal; // for end of frame purging
  87. int staticAllocThisFrame; // debug counter
  88. int staticCountThisFrame;
  89. int dynamicAllocThisFrame;
  90. int dynamicCountThisFrame;
  91. int currentFrame; // for purgable block tracking
  92. int listNum; // currentFrame % NUM_VERTEX_FRAMES, determines which tempBuffers to use
  93. bool virtualMemory; // not fast stuff
  94. bool allocatingTempBuffer; // force GL_STREAM_DRAW_ARB
  95. vertCache_t *tempBuffers[NUM_VERTEX_FRAMES]; // allocated at startup
  96. bool tempOverflow; // had to alloc a temp in static memory
  97. idBlockAlloc<vertCache_t,1024> headerAllocator;
  98. vertCache_t freeStaticHeaders; // head of doubly linked list
  99. vertCache_t freeDynamicHeaders; // head of doubly linked list
  100. vertCache_t dynamicHeaders; // head of doubly linked list
  101. vertCache_t deferredFreeList; // head of doubly linked list
  102. vertCache_t staticHeaders; // head of doubly linked list in MRU order,
  103. // staticHeaders.next is most recently used
  104. int frameBytes; // for each of NUM_VERTEX_FRAMES frames
  105. };
  106. extern idVertexCache vertexCache;