CmGLHardwareIndexBuffer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "CmGLHardwareIndexBuffer.h"
  25. #include "CmGLHardwareBufferManager.h"
  26. #include "OgreException.h"
  27. namespace CamelotEngine {
  28. //---------------------------------------------------------------------
  29. GLHardwareIndexBuffer::GLHardwareIndexBuffer(HardwareBufferManagerBase* mgr, IndexType idxType,
  30. size_t numIndexes, HardwareBuffer::Usage usage, bool useShadowBuffer)
  31. : HardwareIndexBuffer(mgr, idxType, numIndexes, usage, false, useShadowBuffer)
  32. {
  33. glGenBuffersARB( 1, &mBufferId );
  34. if (!mBufferId)
  35. {
  36. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  37. "Cannot create GL index buffer",
  38. "GLHardwareIndexBuffer::GLHardwareIndexBuffer");
  39. }
  40. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId);
  41. // Initialise buffer and set usage
  42. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL,
  43. GLHardwareBufferManager::getGLUsage(usage));
  44. //std::cerr << "creating index buffer " << mBufferId << std::endl;
  45. }
  46. //---------------------------------------------------------------------
  47. GLHardwareIndexBuffer::~GLHardwareIndexBuffer()
  48. {
  49. glDeleteBuffersARB(1, &mBufferId);
  50. }
  51. //---------------------------------------------------------------------
  52. void* GLHardwareIndexBuffer::lockImpl(size_t offset,
  53. size_t length, LockOptions options)
  54. {
  55. GLenum access = 0;
  56. if(mIsLocked)
  57. {
  58. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  59. "Invalid attempt to lock an index buffer that has already been locked",
  60. "GLHardwareIndexBuffer::lock");
  61. }
  62. void* retPtr = 0;
  63. GLHardwareBufferManager* glBufManager = static_cast<GLHardwareBufferManager*>(HardwareBufferManager::getSingletonPtr());
  64. // Try to use scratch buffers for smaller buffers
  65. if( length < glBufManager->getGLMapBufferThreshold() )
  66. {
  67. retPtr = glBufManager->allocateScratch((UINT32)length);
  68. if (retPtr)
  69. {
  70. mLockedToScratch = true;
  71. mScratchOffset = offset;
  72. mScratchSize = length;
  73. mScratchPtr = retPtr;
  74. mScratchUploadOnUnlock = (options != HBL_READ_ONLY);
  75. if (options != HBL_DISCARD)
  76. {
  77. // have to read back the data before returning the pointer
  78. readData(offset, length, retPtr);
  79. }
  80. }
  81. }
  82. if (!retPtr)
  83. {
  84. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  85. // Use glMapBuffer
  86. if(options == HBL_DISCARD)
  87. {
  88. // Discard the buffer
  89. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL,
  90. GLHardwareBufferManager::getGLUsage(mUsage));
  91. }
  92. if (mUsage & HBU_WRITE_ONLY)
  93. access = GL_WRITE_ONLY_ARB;
  94. else if (options == HBL_READ_ONLY)
  95. access = GL_READ_ONLY_ARB;
  96. else
  97. access = GL_READ_WRITE_ARB;
  98. void* pBuffer = glMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, access );
  99. if(pBuffer == 0)
  100. {
  101. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  102. "Index Buffer: Out of memory",
  103. "GLHardwareIndexBuffer::lock");
  104. }
  105. // return offsetted
  106. retPtr = static_cast<void*>(
  107. static_cast<unsigned char*>(pBuffer) + offset);
  108. mLockedToScratch = false;
  109. }
  110. mIsLocked = true;
  111. return retPtr;
  112. }
  113. //---------------------------------------------------------------------
  114. void GLHardwareIndexBuffer::unlockImpl(void)
  115. {
  116. if (mLockedToScratch)
  117. {
  118. if (mScratchUploadOnUnlock)
  119. {
  120. // have to write the data back to vertex buffer
  121. writeData(mScratchOffset, mScratchSize, mScratchPtr,
  122. mScratchOffset == 0 && mScratchSize == getSizeInBytes());
  123. }
  124. // deallocate from scratch buffer
  125. static_cast<GLHardwareBufferManager*>(
  126. HardwareBufferManager::getSingletonPtr())->deallocateScratch(mScratchPtr);
  127. mLockedToScratch = false;
  128. }
  129. else
  130. {
  131. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  132. if(!glUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB ))
  133. {
  134. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  135. "Buffer data corrupted, please reload",
  136. "GLHardwareIndexBuffer::unlock");
  137. }
  138. }
  139. mIsLocked = false;
  140. }
  141. //---------------------------------------------------------------------
  142. void GLHardwareIndexBuffer::readData(size_t offset, size_t length,
  143. void* pDest)
  144. {
  145. if(mUseShadowBuffer)
  146. {
  147. // get data from the shadow buffer
  148. void* srcData = mpShadowBuffer->lock(offset, length, HBL_READ_ONLY);
  149. memcpy(pDest, srcData, length);
  150. mpShadowBuffer->unlock();
  151. }
  152. else
  153. {
  154. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  155. glGetBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, offset, length, pDest);
  156. }
  157. }
  158. //---------------------------------------------------------------------
  159. void GLHardwareIndexBuffer::writeData(size_t offset, size_t length,
  160. const void* pSource, bool discardWholeBuffer)
  161. {
  162. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  163. // Update the shadow buffer
  164. if(mUseShadowBuffer)
  165. {
  166. void* destData = mpShadowBuffer->lock(offset, length,
  167. discardWholeBuffer ? HBL_DISCARD : HBL_NORMAL);
  168. memcpy(destData, pSource, length);
  169. mpShadowBuffer->unlock();
  170. }
  171. if (offset == 0 && length == mSizeInBytes)
  172. {
  173. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, pSource,
  174. GLHardwareBufferManager::getGLUsage(mUsage));
  175. }
  176. else
  177. {
  178. if(discardWholeBuffer)
  179. {
  180. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL,
  181. GLHardwareBufferManager::getGLUsage(mUsage));
  182. }
  183. // Now update the real buffer
  184. glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, offset, length, pSource);
  185. }
  186. }
  187. //---------------------------------------------------------------------
  188. void GLHardwareIndexBuffer::_updateFromShadow(void)
  189. {
  190. if (mUseShadowBuffer && mShadowUpdated && !mSuppressHardwareUpdate)
  191. {
  192. const void *srcData = mpShadowBuffer->lock(
  193. mLockStart, mLockSize, HBL_READ_ONLY);
  194. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId);
  195. // Update whole buffer if possible, otherwise normal
  196. if (mLockStart == 0 && mLockSize == mSizeInBytes)
  197. {
  198. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, srcData,
  199. GLHardwareBufferManager::getGLUsage(mUsage));
  200. }
  201. else
  202. {
  203. glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mLockStart, mLockSize, srcData);
  204. }
  205. mpShadowBuffer->unlock();
  206. mShadowUpdated = false;
  207. }
  208. }
  209. }