CmGLHardwareBufferManager.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLHardwareBufferManager.h"
  25. #include "CmGLVertexBuffer.h"
  26. #include "CmGLIndexBuffer.h"
  27. #include "CmGLGpuBuffer.h"
  28. #include "CmHardwareBuffer.h"
  29. #include "CmGLGpuParamBlock.h"
  30. #include "CmRenderSystem.h"
  31. #include "CmRenderSystemCapabilities.h"
  32. namespace CamelotEngine {
  33. //-----------------------------------------------------------------------
  34. // Scratch pool management (32 bit structure)
  35. struct GLScratchBufferAlloc
  36. {
  37. /// Size in bytes
  38. UINT32 size: 31;
  39. /// Free? (pack with size)
  40. UINT32 free: 1;
  41. };
  42. #define SCRATCH_POOL_SIZE 1 * 1024 * 1024
  43. #define SCRATCH_ALIGNMENT 32
  44. //---------------------------------------------------------------------
  45. GLHardwareBufferManager::GLHardwareBufferManager()
  46. : mScratchBufferPool(NULL), mMapBufferThreshold(CM_GL_DEFAULT_MAP_BUFFER_THRESHOLD)
  47. {
  48. // Init scratch pool
  49. // TODO make it a configurable size?
  50. // 32-bit aligned buffer
  51. mScratchBufferPool = static_cast<char*>(_aligned_malloc(SCRATCH_POOL_SIZE, SCRATCH_ALIGNMENT));
  52. GLScratchBufferAlloc* ptrAlloc = (GLScratchBufferAlloc*)mScratchBufferPool;
  53. ptrAlloc->size = SCRATCH_POOL_SIZE - sizeof(GLScratchBufferAlloc);
  54. ptrAlloc->free = 1;
  55. // non-Win32 machines are having issues glBufferSubData, looks like buffer corruption
  56. // disable for now until we figure out where the problem lies
  57. # if CM_PLATFORM != CM_PLATFORM_WIN32
  58. mMapBufferThreshold = 0;
  59. # endif
  60. // Win32 machines with ATI GPU are having issues glMapBuffer, looks like buffer corruption
  61. // disable for now until we figure out where the problem lies
  62. # if CM_PLATFORM == CM_PLATFORM_WIN32
  63. if (CamelotEngine::RenderSystem::instancePtr()->getCapabilities()->getVendor() == GPU_ATI)
  64. {
  65. mMapBufferThreshold = 0xffffffffUL /* maximum unsigned long value */;
  66. }
  67. # endif
  68. }
  69. //-----------------------------------------------------------------------
  70. GLHardwareBufferManager::~GLHardwareBufferManager()
  71. {
  72. _aligned_free(mScratchBufferPool);
  73. }
  74. //-----------------------------------------------------------------------
  75. VertexBufferPtr GLHardwareBufferManager::createVertexBufferImpl(
  76. UINT32 vertexSize, UINT32 numVerts, GpuBufferUsage usage, bool streamOut)
  77. {
  78. return VertexBufferPtr(new GLVertexBuffer(this, vertexSize, numVerts, usage));
  79. }
  80. //-----------------------------------------------------------------------
  81. IndexBufferPtr
  82. GLHardwareBufferManager::createIndexBufferImpl(
  83. IndexBuffer::IndexType itype, UINT32 numIndexes,
  84. GpuBufferUsage usage)
  85. {
  86. return IndexBufferPtr(new GLIndexBuffer(this, itype, numIndexes, usage));
  87. }
  88. //---------------------------------------------------------------------
  89. GpuParamBlockPtr GLHardwareBufferManager::createGpuParamBlockImpl(const GpuParamBlockDesc& paramDesc, GpuParamBlockUsage usage)
  90. {
  91. return GpuParamBlockPtr(new GLGpuParamBlock(paramDesc, usage));
  92. }
  93. //---------------------------------------------------------------------
  94. GpuBufferPtr GLHardwareBufferManager::createGpuBufferImpl(UINT32 elementCount, UINT32 elementSize,
  95. GpuBufferType type, GpuBufferUsage usage, bool randomGpuWrite, bool useCounter)
  96. {
  97. return GpuBufferPtr(new GLGpuBuffer(elementCount, elementSize, type, usage, randomGpuWrite, useCounter));
  98. }
  99. //---------------------------------------------------------------------
  100. GLenum GLHardwareBufferManager::getGLUsage(unsigned int usage)
  101. {
  102. switch(usage)
  103. {
  104. case GBU_STATIC:
  105. return GL_STATIC_DRAW_ARB;
  106. case GBU_DYNAMIC:
  107. return GL_DYNAMIC_DRAW_ARB;
  108. default:
  109. return GL_DYNAMIC_DRAW_ARB;
  110. };
  111. }
  112. //---------------------------------------------------------------------
  113. GLenum GLHardwareBufferManager::getGLType(unsigned int type)
  114. {
  115. switch(type)
  116. {
  117. case VET_FLOAT1:
  118. case VET_FLOAT2:
  119. case VET_FLOAT3:
  120. case VET_FLOAT4:
  121. return GL_FLOAT;
  122. case VET_SHORT1:
  123. case VET_SHORT2:
  124. case VET_SHORT3:
  125. case VET_SHORT4:
  126. return GL_SHORT;
  127. case VET_COLOR:
  128. case VET_COLOR_ABGR:
  129. case VET_COLOR_ARGB:
  130. case VET_UBYTE4:
  131. return GL_UNSIGNED_BYTE;
  132. default:
  133. return 0;
  134. };
  135. }
  136. //---------------------------------------------------------------------
  137. //---------------------------------------------------------------------
  138. void* GLHardwareBufferManager::allocateScratch(UINT32 size)
  139. {
  140. // simple forward link search based on alloc sizes
  141. // not that fast but the list should never get that long since not many
  142. // locks at once (hopefully)
  143. CM_LOCK_MUTEX(mScratchMutex)
  144. // Alignment - round up the size to 32 bits
  145. // control blocks are 32 bits too so this packs nicely
  146. if (size % 4 != 0)
  147. {
  148. size += 4 - (size % 4);
  149. }
  150. UINT32 bufferPos = 0;
  151. while (bufferPos < SCRATCH_POOL_SIZE)
  152. {
  153. GLScratchBufferAlloc* pNext = (GLScratchBufferAlloc*)(mScratchBufferPool + bufferPos);
  154. // Big enough?
  155. if (pNext->free && pNext->size >= size)
  156. {
  157. // split? And enough space for control block
  158. if(pNext->size > size + sizeof(GLScratchBufferAlloc))
  159. {
  160. UINT32 offset = (UINT32)sizeof(GLScratchBufferAlloc) + size;
  161. GLScratchBufferAlloc* pSplitAlloc = (GLScratchBufferAlloc*)
  162. (mScratchBufferPool + bufferPos + offset);
  163. pSplitAlloc->free = 1;
  164. // split size is remainder minus new control block
  165. pSplitAlloc->size = pNext->size - size - sizeof(GLScratchBufferAlloc);
  166. // New size of current
  167. pNext->size = size;
  168. }
  169. // allocate and return
  170. pNext->free = 0;
  171. // return pointer just after this control block (++ will do that for us)
  172. return ++pNext;
  173. }
  174. bufferPos += (UINT32)sizeof(GLScratchBufferAlloc) + pNext->size;
  175. }
  176. // no available alloc
  177. return 0;
  178. }
  179. //---------------------------------------------------------------------
  180. void GLHardwareBufferManager::deallocateScratch(void* ptr)
  181. {
  182. CM_LOCK_MUTEX(mScratchMutex)
  183. // Simple linear search dealloc
  184. UINT32 bufferPos = 0;
  185. GLScratchBufferAlloc* pLast = 0;
  186. while (bufferPos < SCRATCH_POOL_SIZE)
  187. {
  188. GLScratchBufferAlloc* pCurrent = (GLScratchBufferAlloc*)(mScratchBufferPool + bufferPos);
  189. // Pointers match?
  190. if ((mScratchBufferPool + bufferPos + sizeof(GLScratchBufferAlloc))
  191. == ptr)
  192. {
  193. // dealloc
  194. pCurrent->free = 1;
  195. // merge with previous
  196. if (pLast && pLast->free)
  197. {
  198. // adjust buffer pos
  199. bufferPos -= (pLast->size + (UINT32)sizeof(GLScratchBufferAlloc));
  200. // merge free space
  201. pLast->size += pCurrent->size + sizeof(GLScratchBufferAlloc);
  202. pCurrent = pLast;
  203. }
  204. // merge with next
  205. UINT32 offset = bufferPos + pCurrent->size + (UINT32)sizeof(GLScratchBufferAlloc);
  206. if (offset < SCRATCH_POOL_SIZE)
  207. {
  208. GLScratchBufferAlloc* pNext = (GLScratchBufferAlloc*)(
  209. mScratchBufferPool + offset);
  210. if (pNext->free)
  211. {
  212. pCurrent->size += pNext->size + sizeof(GLScratchBufferAlloc);
  213. }
  214. }
  215. // done
  216. return;
  217. }
  218. bufferPos += (UINT32)sizeof(GLScratchBufferAlloc) + pCurrent->size;
  219. pLast = pCurrent;
  220. }
  221. // Should never get here unless there's a corruption
  222. assert (false && "Memory deallocation error");
  223. }
  224. //---------------------------------------------------------------------
  225. const UINT32 GLHardwareBufferManager::getGLMapBufferThreshold() const
  226. {
  227. return mMapBufferThreshold;
  228. }
  229. //---------------------------------------------------------------------
  230. void GLHardwareBufferManager::setGLMapBufferThreshold( const UINT32 value )
  231. {
  232. mMapBufferThreshold = value;
  233. }
  234. }