gfxGLPrimitiveBuffer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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), mZombieCache(NULL),
  35. mBufferOffset(0)
  36. {
  37. if( mBufferType == GFXBufferTypeVolatile )
  38. {
  39. mBuffer = getCircularVolatileIndexBuffer()->getHandle();
  40. return;
  41. }
  42. // Generate a buffer and allocate the needed memory
  43. glGenBuffers(1, &mBuffer);
  44. PRESERVE_INDEX_BUFFER();
  45. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  46. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(U16), NULL, GFXGLBufferType[bufferType]);
  47. }
  48. GFXGLPrimitiveBuffer::~GFXGLPrimitiveBuffer()
  49. {
  50. // This is heavy handed, but it frees the buffer memory
  51. if( mBufferType != GFXBufferTypeVolatile )
  52. glDeleteBuffers(1, &mBuffer);
  53. if( mZombieCache )
  54. delete [] mZombieCache;
  55. }
  56. void GFXGLPrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
  57. {
  58. if( mBufferType == GFXBufferTypeVolatile )
  59. {
  60. AssertFatal(indexStart == 0, "");
  61. getCircularVolatileIndexBuffer()->lock( mIndexCount * sizeof(U16), 0, mBufferOffset, *indexPtr );
  62. }
  63. else
  64. {
  65. mFrameAllocator.lock( mIndexCount * sizeof(U16) );
  66. *indexPtr = (void*)(mFrameAllocator.getlockedPtr() + (indexStart * sizeof(U16)) );
  67. }
  68. lockedIndexStart = indexStart;
  69. lockedIndexEnd = indexEnd;
  70. }
  71. void GFXGLPrimitiveBuffer::unlock()
  72. {
  73. PROFILE_SCOPE(GFXGLPrimitiveBuffer_unlock);
  74. if( mBufferType == GFXBufferTypeVolatile )
  75. {
  76. getCircularVolatileIndexBuffer()->unlock();
  77. }
  78. else
  79. {
  80. U32 offset = lockedIndexStart * sizeof(U16);
  81. U32 length = (lockedIndexEnd - lockedIndexStart) * sizeof(U16);
  82. // Preserve previous binding
  83. PRESERVE_INDEX_BUFFER();
  84. // Bind ourselves
  85. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  86. if( !lockedIndexStart && lockedIndexEnd == mIndexCount)
  87. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), NULL, GFXGLBufferType[mBufferType]); // orphan the buffer
  88. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, length, mFrameAllocator.getlockedPtr() + offset );
  89. mFrameAllocator.unlock();
  90. }
  91. lockedIndexStart = 0;
  92. lockedIndexEnd = 0;
  93. }
  94. void GFXGLPrimitiveBuffer::prepare()
  95. {
  96. // Bind
  97. GFXGLDevice* glDevice = static_cast<GFXGLDevice*>(mDevice);
  98. glDevice->setPB(this);
  99. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  100. glDevice->getOpenglCache()->setCacheBinded(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  101. }
  102. void GFXGLPrimitiveBuffer::finish()
  103. {
  104. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  105. static_cast<GFXGLDevice*>(mDevice)->getOpenglCache()->setCacheBinded(GL_ELEMENT_ARRAY_BUFFER, 0);
  106. }
  107. GLvoid* GFXGLPrimitiveBuffer::getBuffer()
  108. {
  109. // NULL specifies no offset into the hardware buffer
  110. return (GLvoid*)mBufferOffset;
  111. }
  112. void GFXGLPrimitiveBuffer::zombify()
  113. {
  114. if(mZombieCache)
  115. return;
  116. mZombieCache = new U8[mIndexCount * sizeof(U16)];
  117. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  118. glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, mIndexCount * sizeof(U16), mZombieCache);
  119. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  120. glDeleteBuffers(1, &mBuffer);
  121. mBuffer = 0;
  122. }
  123. void GFXGLPrimitiveBuffer::resurrect()
  124. {
  125. if(!mZombieCache)
  126. return;
  127. glGenBuffers(1, &mBuffer);
  128. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  129. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), mZombieCache, GFXGLBufferType[mBufferType]);
  130. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  131. delete[] mZombieCache;
  132. mZombieCache = NULL;
  133. }
  134. namespace
  135. {
  136. bool onGFXDeviceSignal( GFXDevice::GFXDeviceEventType type )
  137. {
  138. if( GFX->getAdapterType() == OpenGL && GFXDevice::deEndOfFrame == type )
  139. getCircularVolatileIndexBuffer()->protectUsedRange();
  140. return true;
  141. }
  142. }
  143. MODULE_BEGIN( GFX_GL_PrimitiveBuffer )
  144. MODULE_INIT_AFTER( gfx )
  145. MODULE_SHUTDOWN_BEFORE( gfx )
  146. MODULE_INIT
  147. {
  148. GFXDevice::getDeviceEventSignal( ).notify( &onGFXDeviceSignal );
  149. }
  150. MODULE_SHUTDOWN
  151. {
  152. GFXDevice::getDeviceEventSignal( ).remove( &onGFXDeviceSignal );
  153. }
  154. MODULE_END