CmGLHardwareBufferManager.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "CmGLHardwareVertexBuffer.h"
  26. #include "CmGLHardwareIndexBuffer.h"
  27. #include "CmHardwareBuffer.h"
  28. #include "CmRenderSystem.h"
  29. #include "CmRenderSystemCapabilities.h"
  30. namespace CamelotEngine {
  31. //-----------------------------------------------------------------------
  32. // Scratch pool management (32 bit structure)
  33. struct GLScratchBufferAlloc
  34. {
  35. /// Size in bytes
  36. UINT32 size: 31;
  37. /// Free? (pack with size)
  38. UINT32 free: 1;
  39. };
  40. #define SCRATCH_POOL_SIZE 1 * 1024 * 1024
  41. #define SCRATCH_ALIGNMENT 32
  42. //---------------------------------------------------------------------
  43. GLHardwareBufferManagerBase::GLHardwareBufferManagerBase()
  44. : mScratchBufferPool(NULL), mMapBufferThreshold(OGRE_GL_DEFAULT_MAP_BUFFER_THRESHOLD)
  45. {
  46. // Init scratch pool
  47. // TODO make it a configurable size?
  48. // 32-bit aligned buffer
  49. mScratchBufferPool = static_cast<char*>(_aligned_malloc(SCRATCH_POOL_SIZE, SCRATCH_ALIGNMENT));
  50. GLScratchBufferAlloc* ptrAlloc = (GLScratchBufferAlloc*)mScratchBufferPool;
  51. ptrAlloc->size = SCRATCH_POOL_SIZE - sizeof(GLScratchBufferAlloc);
  52. ptrAlloc->free = 1;
  53. // non-Win32 machines are having issues glBufferSubData, looks like buffer corruption
  54. // disable for now until we figure out where the problem lies
  55. # if CM_PLATFORM != CM_PLATFORM_WIN32
  56. mMapBufferThreshold = 0;
  57. # endif
  58. // Win32 machines with ATI GPU are having issues glMapBuffer, looks like buffer corruption
  59. // disable for now until we figure out where the problem lies
  60. # if CM_PLATFORM == CM_PLATFORM_WIN32
  61. if (CamelotEngine::RenderSystem::instancePtr()->getCapabilities()->getVendor() == GPU_ATI)
  62. {
  63. mMapBufferThreshold = 0xffffffffUL /* maximum unsigned long value */;
  64. }
  65. # endif
  66. }
  67. //-----------------------------------------------------------------------
  68. GLHardwareBufferManagerBase::~GLHardwareBufferManagerBase()
  69. {
  70. destroyAllBindings();
  71. _aligned_free(mScratchBufferPool);
  72. }
  73. //-----------------------------------------------------------------------
  74. HardwareVertexBufferPtr GLHardwareBufferManagerBase::createVertexBuffer(
  75. UINT32 vertexSize, UINT32 numVerts, HardwareBuffer::Usage usage, bool streamOut)
  76. {
  77. GLHardwareVertexBuffer* buf =
  78. new GLHardwareVertexBuffer(this, vertexSize, numVerts, usage);
  79. {
  80. mVertexBuffers.insert(buf);
  81. }
  82. return HardwareVertexBufferPtr(buf);
  83. }
  84. //-----------------------------------------------------------------------
  85. HardwareIndexBufferPtr
  86. GLHardwareBufferManagerBase::createIndexBuffer(
  87. HardwareIndexBuffer::IndexType itype, UINT32 numIndexes,
  88. HardwareBuffer::Usage usage)
  89. {
  90. GLHardwareIndexBuffer* buf =
  91. new GLHardwareIndexBuffer(this, itype, numIndexes, usage);
  92. {
  93. mIndexBuffers.insert(buf);
  94. }
  95. return HardwareIndexBufferPtr(buf);
  96. }
  97. //---------------------------------------------------------------------
  98. GpuParamBlockPtr GLHardwareBufferManagerBase::createGpuParamBlock(const GpuParamBlockDesc& paramDesc)
  99. {
  100. return GpuParamBlockPtr(new GpuParamBlock(paramDesc));
  101. }
  102. //---------------------------------------------------------------------
  103. GLenum GLHardwareBufferManagerBase::getGLUsage(unsigned int usage)
  104. {
  105. switch(usage)
  106. {
  107. case HardwareBuffer::HBU_STATIC:
  108. case HardwareBuffer::HBU_STATIC_WRITE_ONLY:
  109. return GL_STATIC_DRAW_ARB;
  110. case HardwareBuffer::HBU_DYNAMIC:
  111. case HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY:
  112. return GL_DYNAMIC_DRAW_ARB;
  113. case HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE:
  114. return GL_STREAM_DRAW_ARB;
  115. default:
  116. return GL_DYNAMIC_DRAW_ARB;
  117. };
  118. }
  119. //---------------------------------------------------------------------
  120. GLenum GLHardwareBufferManagerBase::getGLType(unsigned int type)
  121. {
  122. switch(type)
  123. {
  124. case VET_FLOAT1:
  125. case VET_FLOAT2:
  126. case VET_FLOAT3:
  127. case VET_FLOAT4:
  128. return GL_FLOAT;
  129. case VET_SHORT1:
  130. case VET_SHORT2:
  131. case VET_SHORT3:
  132. case VET_SHORT4:
  133. return GL_SHORT;
  134. case VET_COLOR:
  135. case VET_COLOR_ABGR:
  136. case VET_COLOR_ARGB:
  137. case VET_UBYTE4:
  138. return GL_UNSIGNED_BYTE;
  139. default:
  140. return 0;
  141. };
  142. }
  143. //---------------------------------------------------------------------
  144. //---------------------------------------------------------------------
  145. void* GLHardwareBufferManagerBase::allocateScratch(UINT32 size)
  146. {
  147. // simple forward link search based on alloc sizes
  148. // not that fast but the list should never get that long since not many
  149. // locks at once (hopefully)
  150. CM_LOCK_MUTEX(mScratchMutex)
  151. // Alignment - round up the size to 32 bits
  152. // control blocks are 32 bits too so this packs nicely
  153. if (size % 4 != 0)
  154. {
  155. size += 4 - (size % 4);
  156. }
  157. UINT32 bufferPos = 0;
  158. while (bufferPos < SCRATCH_POOL_SIZE)
  159. {
  160. GLScratchBufferAlloc* pNext = (GLScratchBufferAlloc*)(mScratchBufferPool + bufferPos);
  161. // Big enough?
  162. if (pNext->free && pNext->size >= size)
  163. {
  164. // split? And enough space for control block
  165. if(pNext->size > size + sizeof(GLScratchBufferAlloc))
  166. {
  167. UINT32 offset = (UINT32)sizeof(GLScratchBufferAlloc) + size;
  168. GLScratchBufferAlloc* pSplitAlloc = (GLScratchBufferAlloc*)
  169. (mScratchBufferPool + bufferPos + offset);
  170. pSplitAlloc->free = 1;
  171. // split size is remainder minus new control block
  172. pSplitAlloc->size = pNext->size - size - sizeof(GLScratchBufferAlloc);
  173. // New size of current
  174. pNext->size = size;
  175. }
  176. // allocate and return
  177. pNext->free = 0;
  178. // return pointer just after this control block (++ will do that for us)
  179. return ++pNext;
  180. }
  181. bufferPos += (UINT32)sizeof(GLScratchBufferAlloc) + pNext->size;
  182. }
  183. // no available alloc
  184. return 0;
  185. }
  186. //---------------------------------------------------------------------
  187. void GLHardwareBufferManagerBase::deallocateScratch(void* ptr)
  188. {
  189. CM_LOCK_MUTEX(mScratchMutex)
  190. // Simple linear search dealloc
  191. UINT32 bufferPos = 0;
  192. GLScratchBufferAlloc* pLast = 0;
  193. while (bufferPos < SCRATCH_POOL_SIZE)
  194. {
  195. GLScratchBufferAlloc* pCurrent = (GLScratchBufferAlloc*)(mScratchBufferPool + bufferPos);
  196. // Pointers match?
  197. if ((mScratchBufferPool + bufferPos + sizeof(GLScratchBufferAlloc))
  198. == ptr)
  199. {
  200. // dealloc
  201. pCurrent->free = 1;
  202. // merge with previous
  203. if (pLast && pLast->free)
  204. {
  205. // adjust buffer pos
  206. bufferPos -= (pLast->size + (UINT32)sizeof(GLScratchBufferAlloc));
  207. // merge free space
  208. pLast->size += pCurrent->size + sizeof(GLScratchBufferAlloc);
  209. pCurrent = pLast;
  210. }
  211. // merge with next
  212. UINT32 offset = bufferPos + pCurrent->size + (UINT32)sizeof(GLScratchBufferAlloc);
  213. if (offset < SCRATCH_POOL_SIZE)
  214. {
  215. GLScratchBufferAlloc* pNext = (GLScratchBufferAlloc*)(
  216. mScratchBufferPool + offset);
  217. if (pNext->free)
  218. {
  219. pCurrent->size += pNext->size + sizeof(GLScratchBufferAlloc);
  220. }
  221. }
  222. // done
  223. return;
  224. }
  225. bufferPos += (UINT32)sizeof(GLScratchBufferAlloc) + pCurrent->size;
  226. pLast = pCurrent;
  227. }
  228. // Should never get here unless there's a corruption
  229. assert (false && "Memory deallocation error");
  230. }
  231. //---------------------------------------------------------------------
  232. const UINT32 GLHardwareBufferManagerBase::getGLMapBufferThreshold() const
  233. {
  234. return mMapBufferThreshold;
  235. }
  236. //---------------------------------------------------------------------
  237. void GLHardwareBufferManagerBase::setGLMapBufferThreshold( const UINT32 value )
  238. {
  239. mMapBufferThreshold = value;
  240. }
  241. }