gfxGLPrimitiveBuffer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gfx/gl/gfxGLDevice.h"
  23. #include "gfx/gl/gfxGLPrimitiveBuffer.h"
  24. #include "gfx/gl/gfxGLEnumTranslate.h"
  25. #include "gfx/gl/tGL/tGL.h"
  26. #include "gfx/gl/gfxGLUtils.h"
  27. #include "gfx/gl/gfxGLCircularVolatileBuffer.h"
  28. GLCircularVolatileBuffer* getCircularVolatileIndexBuffer()
  29. {
  30. static GLCircularVolatileBuffer sCircularVolatileIndexBuffer(GL_ELEMENT_ARRAY_BUFFER);
  31. return &sCircularVolatileIndexBuffer;
  32. }
  33. GFXGLPrimitiveBuffer::GFXGLPrimitiveBuffer(GFXDevice *device, U32 indexCount, U32 primitiveCount, GFXBufferType bufferType) :
  34. GFXPrimitiveBuffer(device, indexCount, primitiveCount, bufferType),
  35. mBufferOffset(0),
  36. mZombieCache(NULL),
  37. lockedIndexEnd(0),
  38. lockedIndexStart(0)
  39. {
  40. if( mBufferType == GFXBufferTypeVolatile )
  41. {
  42. mBuffer = getCircularVolatileIndexBuffer()->getHandle();
  43. return;
  44. }
  45. // Generate a buffer and allocate the needed memory
  46. glGenBuffers(1, &mBuffer);
  47. PRESERVE_INDEX_BUFFER();
  48. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  49. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(U16), NULL, GFXGLBufferType[bufferType]);
  50. }
  51. GFXGLPrimitiveBuffer::~GFXGLPrimitiveBuffer()
  52. {
  53. // This is heavy handed, but it frees the buffer memory
  54. if( mBufferType != GFXBufferTypeVolatile )
  55. glDeleteBuffers(1, &mBuffer);
  56. if( mZombieCache )
  57. delete [] mZombieCache;
  58. }
  59. void GFXGLPrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
  60. {
  61. if( mBufferType == GFXBufferTypeVolatile )
  62. {
  63. AssertFatal(indexStart == 0, "");
  64. getCircularVolatileIndexBuffer()->lock( mIndexCount * sizeof(U16), 0, mBufferOffset, *indexPtr );
  65. }
  66. else
  67. {
  68. mFrameAllocator.lock( mIndexCount * sizeof(U16) );
  69. *indexPtr = (void*)(mFrameAllocator.getlockedPtr() + (indexStart * sizeof(U16)) );
  70. }
  71. lockedIndexStart = indexStart;
  72. lockedIndexEnd = indexEnd;
  73. }
  74. void GFXGLPrimitiveBuffer::unlock()
  75. {
  76. PROFILE_SCOPE(GFXGLPrimitiveBuffer_unlock);
  77. if( mBufferType == GFXBufferTypeVolatile )
  78. {
  79. getCircularVolatileIndexBuffer()->unlock();
  80. }
  81. else
  82. {
  83. U32 offset = lockedIndexStart * sizeof(U16);
  84. U32 length = (lockedIndexEnd - lockedIndexStart) * sizeof(U16);
  85. // Preserve previous binding
  86. PRESERVE_INDEX_BUFFER();
  87. // Bind ourselves
  88. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  89. if( !lockedIndexStart && lockedIndexEnd == mIndexCount)
  90. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), NULL, GFXGLBufferType[mBufferType]); // orphan the buffer
  91. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, length, mFrameAllocator.getlockedPtr() + offset );
  92. mFrameAllocator.unlock();
  93. }
  94. lockedIndexStart = 0;
  95. lockedIndexEnd = 0;
  96. }
  97. void GFXGLPrimitiveBuffer::prepare()
  98. {
  99. // Bind
  100. GFXGLDevice* glDevice = static_cast<GFXGLDevice*>(mDevice);
  101. glDevice->setPB(this);
  102. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  103. glDevice->getOpenglCache()->setCacheBinded(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  104. }
  105. void GFXGLPrimitiveBuffer::finish()
  106. {
  107. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  108. static_cast<GFXGLDevice*>(mDevice)->getOpenglCache()->setCacheBinded(GL_ELEMENT_ARRAY_BUFFER, 0);
  109. }
  110. GLvoid* GFXGLPrimitiveBuffer::getBuffer()
  111. {
  112. // NULL specifies no offset into the hardware buffer
  113. return (GLvoid*)(uintptr_t)mBufferOffset;
  114. }
  115. void GFXGLPrimitiveBuffer::zombify()
  116. {
  117. if(mZombieCache)
  118. return;
  119. mZombieCache = new U8[mIndexCount * sizeof(U16)];
  120. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  121. glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, mIndexCount * sizeof(U16), mZombieCache);
  122. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  123. glDeleteBuffers(1, &mBuffer);
  124. mBuffer = 0;
  125. }
  126. void GFXGLPrimitiveBuffer::resurrect()
  127. {
  128. if(!mZombieCache)
  129. return;
  130. glGenBuffers(1, &mBuffer);
  131. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  132. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), mZombieCache, GFXGLBufferType[mBufferType]);
  133. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  134. delete[] mZombieCache;
  135. mZombieCache = NULL;
  136. }
  137. namespace
  138. {
  139. bool onGFXDeviceSignal( GFXDevice::GFXDeviceEventType type )
  140. {
  141. if( GFX->getAdapterType() == OpenGL && GFXDevice::deEndOfFrame == type )
  142. getCircularVolatileIndexBuffer()->protectUsedRange();
  143. return true;
  144. }
  145. }
  146. MODULE_BEGIN( GFX_GL_PrimitiveBuffer )
  147. MODULE_INIT_AFTER( gfx )
  148. MODULE_SHUTDOWN_BEFORE( gfx )
  149. MODULE_INIT
  150. {
  151. GFXDevice::getDeviceEventSignal( ).notify( &onGFXDeviceSignal );
  152. }
  153. MODULE_SHUTDOWN
  154. {
  155. GFXDevice::getDeviceEventSignal( ).remove( &onGFXDeviceSignal );
  156. }
  157. MODULE_END