gfxGLPrimitiveBuffer.cpp 5.7 KB

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