CmGLHardwareBufferManager.cpp 9.2 KB

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