CmGLIndexBuffer.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "CmGLIndexBuffer.h"
  25. #include "CmGLHardwareBufferManager.h"
  26. #include "CmException.h"
  27. namespace CamelotEngine {
  28. //---------------------------------------------------------------------
  29. GLIndexBuffer::GLIndexBuffer(HardwareBufferManager* mgr, IndexType idxType,
  30. UINT32 numIndexes, GpuBufferUsage usage)
  31. : IndexBuffer(mgr, idxType, numIndexes, usage, false)
  32. {
  33. glGenBuffersARB( 1, &mBufferId );
  34. if (!mBufferId)
  35. {
  36. CM_EXCEPT(InternalErrorException,
  37. "Cannot create GL index buffer");
  38. }
  39. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId);
  40. // Initialise buffer and set usage
  41. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL,
  42. GLHardwareBufferManager::getGLUsage(usage));
  43. //std::cerr << "creating index buffer " << mBufferId << std::endl;
  44. }
  45. //---------------------------------------------------------------------
  46. GLIndexBuffer::~GLIndexBuffer()
  47. {
  48. glDeleteBuffersARB(1, &mBufferId);
  49. }
  50. //---------------------------------------------------------------------
  51. void* GLIndexBuffer::lockImpl(UINT32 offset,
  52. UINT32 length, GpuLockOptions options)
  53. {
  54. GLenum access = 0;
  55. if(mIsLocked)
  56. {
  57. CM_EXCEPT(InternalErrorException,
  58. "Invalid attempt to lock an index buffer that has already been locked");
  59. }
  60. void* retPtr = 0;
  61. GLHardwareBufferManager* glBufManager = static_cast<GLHardwareBufferManager*>(HardwareBufferManager::instancePtr());
  62. // Try to use scratch buffers for smaller buffers
  63. if( length < glBufManager->getGLMapBufferThreshold() )
  64. {
  65. retPtr = glBufManager->allocateScratch((UINT32)length);
  66. if (retPtr)
  67. {
  68. mLockedToScratch = true;
  69. mScratchOffset = offset;
  70. mScratchSize = length;
  71. mScratchPtr = retPtr;
  72. mScratchUploadOnUnlock = (options != GBL_READ_ONLY);
  73. if (options != GBL_WRITE_ONLY_DISCARD)
  74. {
  75. // have to read back the data before returning the pointer
  76. readData(offset, length, retPtr);
  77. }
  78. }
  79. }
  80. if (!retPtr)
  81. {
  82. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  83. // Use glMapBuffer
  84. if(options == GBL_WRITE_ONLY_DISCARD)
  85. {
  86. // Discard the buffer
  87. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL,
  88. GLHardwareBufferManager::getGLUsage(mUsage));
  89. }
  90. if (options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  91. access = GL_WRITE_ONLY_ARB;
  92. else if (options == GBL_READ_ONLY)
  93. access = GL_READ_ONLY_ARB;
  94. else
  95. access = GL_READ_WRITE_ARB;
  96. void* pBuffer = glMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, access );
  97. if(pBuffer == 0)
  98. {
  99. CM_EXCEPT(InternalErrorException,
  100. "Index Buffer: Out of memory");
  101. }
  102. // return offsetted
  103. retPtr = static_cast<void*>(
  104. static_cast<unsigned char*>(pBuffer) + offset);
  105. mLockedToScratch = false;
  106. }
  107. mIsLocked = true;
  108. return retPtr;
  109. }
  110. //---------------------------------------------------------------------
  111. void GLIndexBuffer::unlockImpl(void)
  112. {
  113. if (mLockedToScratch)
  114. {
  115. if (mScratchUploadOnUnlock)
  116. {
  117. // have to write the data back to vertex buffer
  118. writeData(mScratchOffset, mScratchSize, mScratchPtr,
  119. mScratchOffset == 0 && mScratchSize == getSizeInBytes());
  120. }
  121. // deallocate from scratch buffer
  122. static_cast<GLHardwareBufferManager*>(
  123. HardwareBufferManager::instancePtr())->deallocateScratch(mScratchPtr);
  124. mLockedToScratch = false;
  125. }
  126. else
  127. {
  128. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  129. if(!glUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB ))
  130. {
  131. CM_EXCEPT(InternalErrorException,
  132. "Buffer data corrupted, please reload");
  133. }
  134. }
  135. mIsLocked = false;
  136. }
  137. //---------------------------------------------------------------------
  138. void GLIndexBuffer::readData(UINT32 offset, UINT32 length,
  139. void* pDest)
  140. {
  141. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  142. glGetBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, offset, length, pDest);
  143. }
  144. //---------------------------------------------------------------------
  145. void GLIndexBuffer::writeData(UINT32 offset, UINT32 length,
  146. const void* pSource, bool discardWholeBuffer)
  147. {
  148. glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
  149. if (offset == 0 && length == mSizeInBytes)
  150. {
  151. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, pSource,
  152. GLHardwareBufferManager::getGLUsage(mUsage));
  153. }
  154. else
  155. {
  156. if(discardWholeBuffer)
  157. {
  158. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL,
  159. GLHardwareBufferManager::getGLUsage(mUsage));
  160. }
  161. // Now update the real buffer
  162. glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, offset, length, pSource);
  163. }
  164. }
  165. }