VertexCache.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. idVertexCache vertexCache;
  24. idCVar r_showVertexCache( "r_showVertexCache", "0", CVAR_RENDERER | CVAR_BOOL, "Print stats about the vertex cache every frame" );
  25. idCVar r_showVertexCacheTimings( "r_showVertexCache", "0", CVAR_RENDERER | CVAR_BOOL, "Print stats about the vertex cache every frame" );
  26. /*
  27. ==============
  28. ClearGeoBufferSet
  29. ==============
  30. */
  31. static void ClearGeoBufferSet( geoBufferSet_t &gbs ) {
  32. gbs.indexMemUsed.SetValue( 0 );
  33. gbs.vertexMemUsed.SetValue( 0 );
  34. gbs.jointMemUsed.SetValue( 0 );
  35. gbs.allocations = 0;
  36. }
  37. /*
  38. ==============
  39. MapGeoBufferSet
  40. ==============
  41. */
  42. static void MapGeoBufferSet( geoBufferSet_t &gbs ) {
  43. if ( gbs.mappedVertexBase == NULL ) {
  44. gbs.mappedVertexBase = (byte *)gbs.vertexBuffer.MapBuffer( BM_WRITE );
  45. }
  46. if ( gbs.mappedIndexBase == NULL ) {
  47. gbs.mappedIndexBase = (byte *)gbs.indexBuffer.MapBuffer( BM_WRITE );
  48. }
  49. if ( gbs.mappedJointBase == NULL && gbs.jointBuffer.GetAllocedSize() != 0 ) {
  50. gbs.mappedJointBase = (byte *)gbs.jointBuffer.MapBuffer( BM_WRITE );
  51. }
  52. }
  53. /*
  54. ==============
  55. UnmapGeoBufferSet
  56. ==============
  57. */
  58. static void UnmapGeoBufferSet( geoBufferSet_t &gbs ) {
  59. if ( gbs.mappedVertexBase != NULL ) {
  60. gbs.vertexBuffer.UnmapBuffer();
  61. gbs.mappedVertexBase = NULL;
  62. }
  63. if ( gbs.mappedIndexBase != NULL ) {
  64. gbs.indexBuffer.UnmapBuffer();
  65. gbs.mappedIndexBase = NULL;
  66. }
  67. if ( gbs.mappedJointBase != NULL ) {
  68. gbs.jointBuffer.UnmapBuffer();
  69. gbs.mappedJointBase = NULL;
  70. }
  71. }
  72. /*
  73. ==============
  74. AllocGeoBufferSet
  75. ==============
  76. */
  77. static void AllocGeoBufferSet( geoBufferSet_t &gbs, const int vertexBytes, const int indexBytes, const int jointBytes ) {
  78. gbs.vertexBuffer.AllocBufferObject( NULL, vertexBytes );
  79. gbs.indexBuffer.AllocBufferObject( NULL, indexBytes );
  80. if ( jointBytes != 0 ) {
  81. gbs.jointBuffer.AllocBufferObject( NULL, jointBytes / sizeof( idJointMat ) );
  82. }
  83. ClearGeoBufferSet( gbs );
  84. }
  85. /*
  86. ==============
  87. idVertexCache::Init
  88. ==============
  89. */
  90. void idVertexCache::Init( bool restart ) {
  91. currentFrame = 0;
  92. listNum = 0;
  93. mostUsedVertex = 0;
  94. mostUsedIndex = 0;
  95. mostUsedJoint = 0;
  96. for ( int i = 0; i < VERTCACHE_NUM_FRAMES; i++ ) {
  97. AllocGeoBufferSet( frameData[i], VERTCACHE_VERTEX_MEMORY_PER_FRAME, VERTCACHE_INDEX_MEMORY_PER_FRAME, VERTCACHE_JOINT_MEMORY_PER_FRAME );
  98. }
  99. AllocGeoBufferSet( staticData, STATIC_VERTEX_MEMORY, STATIC_INDEX_MEMORY, 0 );
  100. MapGeoBufferSet( frameData[listNum] );
  101. }
  102. /*
  103. ==============
  104. idVertexCache::Shutdown
  105. ==============
  106. */
  107. void idVertexCache::Shutdown() {
  108. for ( int i = 0; i < VERTCACHE_NUM_FRAMES; i++ ) {
  109. frameData[i].vertexBuffer.FreeBufferObject();
  110. frameData[i].indexBuffer.FreeBufferObject();
  111. frameData[i].jointBuffer.FreeBufferObject();
  112. }
  113. }
  114. /*
  115. ==============
  116. idVertexCache::PurgeAll
  117. ==============
  118. */
  119. void idVertexCache::PurgeAll() {
  120. Shutdown();
  121. Init( true );
  122. }
  123. /*
  124. ==============
  125. idVertexCache::FreeStaticData
  126. call on loading a new map
  127. ==============
  128. */
  129. void idVertexCache::FreeStaticData() {
  130. ClearGeoBufferSet( staticData );
  131. mostUsedVertex = 0;
  132. mostUsedIndex = 0;
  133. mostUsedJoint = 0;
  134. }
  135. /*
  136. ==============
  137. idVertexCache::ActuallyAlloc
  138. ==============
  139. */
  140. vertCacheHandle_t idVertexCache::ActuallyAlloc( geoBufferSet_t & vcs, const void * data, int bytes, cacheType_t type ) {
  141. if ( bytes == 0 ) {
  142. return (vertCacheHandle_t)0;
  143. }
  144. assert( ( ((UINT_PTR)(data)) & 15 ) == 0 );
  145. assert( ( bytes & 15 ) == 0 );
  146. // thread safe interlocked adds
  147. byte ** base = NULL;
  148. int endPos = 0;
  149. if ( type == CACHE_INDEX ) {
  150. base = &vcs.mappedIndexBase;
  151. endPos = vcs.indexMemUsed.Add( bytes );
  152. if ( endPos > vcs.indexBuffer.GetAllocedSize() ) {
  153. idLib::Error( "Out of index cache" );
  154. }
  155. } else if ( type == CACHE_VERTEX ) {
  156. base = &vcs.mappedVertexBase;
  157. endPos = vcs.vertexMemUsed.Add( bytes );
  158. if ( endPos > vcs.vertexBuffer.GetAllocedSize() ) {
  159. idLib::Error( "Out of vertex cache" );
  160. }
  161. } else if ( type == CACHE_JOINT ) {
  162. base = &vcs.mappedJointBase;
  163. endPos = vcs.jointMemUsed.Add( bytes );
  164. if ( endPos > vcs.jointBuffer.GetAllocedSize() ) {
  165. idLib::Error( "Out of joint buffer cache" );
  166. }
  167. } else {
  168. assert( false );
  169. }
  170. vcs.allocations++;
  171. int offset = endPos - bytes;
  172. // Actually perform the data transfer
  173. if ( data != NULL ) {
  174. MapGeoBufferSet( vcs );
  175. CopyBuffer( *base + offset, (const byte *)data, bytes );
  176. }
  177. vertCacheHandle_t handle = ( (uint64)(currentFrame & VERTCACHE_FRAME_MASK ) << VERTCACHE_FRAME_SHIFT ) |
  178. ( (uint64)(offset & VERTCACHE_OFFSET_MASK ) << VERTCACHE_OFFSET_SHIFT ) |
  179. ( (uint64)(bytes & VERTCACHE_SIZE_MASK ) << VERTCACHE_SIZE_SHIFT );
  180. if ( &vcs == &staticData ) {
  181. handle |= VERTCACHE_STATIC;
  182. }
  183. return handle;
  184. }
  185. /*
  186. ==============
  187. idVertexCache::GetVertexBuffer
  188. ==============
  189. */
  190. bool idVertexCache::GetVertexBuffer( vertCacheHandle_t handle, idVertexBuffer * vb ) {
  191. const int isStatic = handle & VERTCACHE_STATIC;
  192. const uint64 size = (int)( handle >> VERTCACHE_SIZE_SHIFT ) & VERTCACHE_SIZE_MASK;
  193. const uint64 offset = (int)( handle >> VERTCACHE_OFFSET_SHIFT ) & VERTCACHE_OFFSET_MASK;
  194. const uint64 frameNum = (int)( handle >> VERTCACHE_FRAME_SHIFT ) & VERTCACHE_FRAME_MASK;
  195. if ( isStatic ) {
  196. vb->Reference( staticData.vertexBuffer, offset, size );
  197. return true;
  198. }
  199. if ( frameNum != ( ( currentFrame - 1 ) & VERTCACHE_FRAME_MASK ) ) {
  200. return false;
  201. }
  202. vb->Reference( frameData[drawListNum].vertexBuffer, offset, size );
  203. return true;
  204. }
  205. /*
  206. ==============
  207. idVertexCache::GetIndexBuffer
  208. ==============
  209. */
  210. bool idVertexCache::GetIndexBuffer( vertCacheHandle_t handle, idIndexBuffer * ib ) {
  211. const int isStatic = handle & VERTCACHE_STATIC;
  212. const uint64 size = (int)( handle >> VERTCACHE_SIZE_SHIFT ) & VERTCACHE_SIZE_MASK;
  213. const uint64 offset = (int)( handle >> VERTCACHE_OFFSET_SHIFT ) & VERTCACHE_OFFSET_MASK;
  214. const uint64 frameNum = (int)( handle >> VERTCACHE_FRAME_SHIFT ) & VERTCACHE_FRAME_MASK;
  215. if ( isStatic ) {
  216. ib->Reference( staticData.indexBuffer, offset, size );
  217. return true;
  218. }
  219. if ( frameNum != ( ( currentFrame - 1 ) & VERTCACHE_FRAME_MASK ) ) {
  220. return false;
  221. }
  222. ib->Reference( frameData[drawListNum].indexBuffer, offset, size );
  223. return true;
  224. }
  225. /*
  226. ==============
  227. idVertexCache::GetJointBuffer
  228. ==============
  229. */
  230. bool idVertexCache::GetJointBuffer( vertCacheHandle_t handle, idJointBuffer * jb ) {
  231. const int isStatic = handle & VERTCACHE_STATIC;
  232. const uint64 numBytes = (int)( handle >> VERTCACHE_SIZE_SHIFT ) & VERTCACHE_SIZE_MASK;
  233. const uint64 jointOffset = (int)( handle >> VERTCACHE_OFFSET_SHIFT ) & VERTCACHE_OFFSET_MASK;
  234. const uint64 frameNum = (int)( handle >> VERTCACHE_FRAME_SHIFT ) & VERTCACHE_FRAME_MASK;
  235. const uint64 numJoints = numBytes / sizeof( idJointMat );
  236. if ( isStatic ) {
  237. jb->Reference( staticData.jointBuffer, jointOffset, numJoints );
  238. return true;
  239. }
  240. if ( frameNum != ( ( currentFrame - 1 ) & VERTCACHE_FRAME_MASK ) ) {
  241. return false;
  242. }
  243. jb->Reference( frameData[drawListNum].jointBuffer, jointOffset, numJoints );
  244. return true;
  245. }
  246. /*
  247. ==============
  248. idVertexCache::BeginBackEnd
  249. ==============
  250. */
  251. void idVertexCache::BeginBackEnd() {
  252. mostUsedVertex = Max( mostUsedVertex, frameData[listNum].vertexMemUsed.GetValue() );
  253. mostUsedIndex = Max( mostUsedIndex, frameData[listNum].indexMemUsed.GetValue() );
  254. mostUsedJoint = Max( mostUsedJoint, frameData[listNum].jointMemUsed.GetValue() );
  255. if ( r_showVertexCache.GetBool() ) {
  256. idLib::Printf( "%08d: %d allocations, %dkB vertex, %dkB index, %kB joint : %dkB vertex, %dkB index, %kB joint\n",
  257. currentFrame, frameData[listNum].allocations,
  258. frameData[listNum].vertexMemUsed.GetValue() / 1024,
  259. frameData[listNum].indexMemUsed.GetValue() / 1024,
  260. frameData[listNum].jointMemUsed.GetValue() / 1024,
  261. mostUsedVertex / 1024,
  262. mostUsedIndex / 1024,
  263. mostUsedJoint / 1024 );
  264. }
  265. // unmap the current frame so the GPU can read it
  266. const int startUnmap = Sys_Milliseconds();
  267. UnmapGeoBufferSet( frameData[listNum] );
  268. UnmapGeoBufferSet( staticData );
  269. const int endUnmap = Sys_Milliseconds();
  270. if ( endUnmap - startUnmap > 1 ) {
  271. idLib::PrintfIf( r_showVertexCacheTimings.GetBool(), "idVertexCache::unmap took %i msec\n", endUnmap - startUnmap );
  272. }
  273. drawListNum = listNum;
  274. // prepare the next frame for writing to by the CPU
  275. currentFrame++;
  276. listNum = currentFrame % VERTCACHE_NUM_FRAMES;
  277. const int startMap = Sys_Milliseconds();
  278. MapGeoBufferSet( frameData[listNum] );
  279. const int endMap = Sys_Milliseconds();
  280. if ( endMap - startMap > 1 ) {
  281. idLib::PrintfIf( r_showVertexCacheTimings.GetBool(), "idVertexCache::map took %i msec\n", endMap - startMap );
  282. }
  283. ClearGeoBufferSet( frameData[listNum] );
  284. #if 0
  285. const int startBind = Sys_Milliseconds();
  286. qglBindBufferARB( GL_ARRAY_BUFFER_ARB, (GLuint)frameData[drawListNum].vertexBuffer.GetAPIObject() );
  287. qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, (GLuint)frameData[drawListNum].indexBuffer.GetAPIObject() );
  288. const int endBind = Sys_Milliseconds();
  289. if ( endBind - startBind > 1 ) {
  290. idLib::Printf( "idVertexCache::bind took %i msec\n", endBind - startBind );
  291. }
  292. #endif
  293. }