CmGLHardwareVertexBuffer.cpp 8.3 KB

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