gfxGLVertexBuffer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "platform/platform.h"
  23. #include "gfx/gl/gfxGLVertexBuffer.h"
  24. #include "gfx/gl/gfxGLDevice.h"
  25. #include "gfx/gl/gfxGLEnumTranslate.h"
  26. #include "gfx/gl/gfxGLUtils.h"
  27. #include "gfx/gl/gfxGLVertexAttribLocation.h"
  28. #include "gfx/gl/gfxGLCircularVolatileBuffer.h"
  29. GLCircularVolatileBuffer* getCircularVolatileVertexBuffer()
  30. {
  31. static GLCircularVolatileBuffer sCircularVolatileVertexBuffer(GL_ARRAY_BUFFER);
  32. return &sCircularVolatileVertexBuffer;
  33. }
  34. GFXGLVertexBuffer::GFXGLVertexBuffer( GFXDevice *device,
  35. U32 numVerts,
  36. const GFXVertexFormat *vertexFormat,
  37. U32 vertexSize,
  38. GFXBufferType bufferType )
  39. : GFXVertexBuffer( device, numVerts, vertexFormat, vertexSize, bufferType ),
  40. mBufferOffset(0),
  41. mBufferVertexOffset(0),
  42. mZombieCache(NULL)
  43. {
  44. if( mBufferType == GFXBufferTypeVolatile )
  45. {
  46. mBuffer = getCircularVolatileVertexBuffer()->getHandle();
  47. return;
  48. }
  49. // Generate a buffer
  50. glGenBuffers(1, &mBuffer);
  51. //and allocate the needed memory
  52. PRESERVE_VERTEX_BUFFER();
  53. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  54. glBufferData(GL_ARRAY_BUFFER, numVerts * vertexSize, NULL, GFXGLBufferType[bufferType]);
  55. }
  56. GFXGLVertexBuffer::~GFXGLVertexBuffer()
  57. {
  58. // While heavy handed, this does delete the buffer and frees the associated memory.
  59. if( mBufferType != GFXBufferTypeVolatile )
  60. glDeleteBuffers(1, &mBuffer);
  61. if( mZombieCache )
  62. delete [] mZombieCache;
  63. }
  64. void GFXGLVertexBuffer::lock( U32 vertexStart, U32 vertexEnd, void **vertexPtr )
  65. {
  66. PROFILE_SCOPE(GFXGLVertexBuffer_lock);
  67. if( mBufferType == GFXBufferTypeVolatile )
  68. {
  69. AssertFatal(vertexStart == 0, "");
  70. if( GFXGL->mCapabilities.vertexAttributeBinding )
  71. {
  72. getCircularVolatileVertexBuffer()->lock( mNumVerts * mVertexSize, 0, mBufferOffset, *vertexPtr );
  73. }
  74. else
  75. {
  76. getCircularVolatileVertexBuffer()->lock( mNumVerts * mVertexSize, mVertexSize, mBufferOffset, *vertexPtr );
  77. mBufferVertexOffset = mBufferOffset / mVertexSize;
  78. }
  79. }
  80. else
  81. {
  82. mFrameAllocator.lock( mNumVerts * mVertexSize );
  83. lockedVertexPtr = (void*)(mFrameAllocator.getlockedPtr() + (vertexStart * mVertexSize));
  84. *vertexPtr = lockedVertexPtr;
  85. }
  86. lockedVertexStart = vertexStart;
  87. lockedVertexEnd = vertexEnd;
  88. }
  89. void GFXGLVertexBuffer::unlock()
  90. {
  91. PROFILE_SCOPE(GFXGLVertexBuffer_unlock);
  92. if( mBufferType == GFXBufferTypeVolatile )
  93. {
  94. getCircularVolatileVertexBuffer()->unlock();
  95. }
  96. else
  97. {
  98. U32 offset = lockedVertexStart * mVertexSize;
  99. U32 length = (lockedVertexEnd - lockedVertexStart) * mVertexSize;
  100. PRESERVE_VERTEX_BUFFER();
  101. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  102. if( !lockedVertexStart && lockedVertexEnd == mNumVerts)
  103. glBufferData(GL_ARRAY_BUFFER, mNumVerts * mVertexSize, NULL, GFXGLBufferType[mBufferType]); // orphan the buffer
  104. glBufferSubData(GL_ARRAY_BUFFER, offset, length, mFrameAllocator.getlockedPtr() + offset );
  105. mFrameAllocator.unlock();
  106. }
  107. lockedVertexStart = 0;
  108. lockedVertexEnd = 0;
  109. lockedVertexPtr = NULL;
  110. }
  111. void GFXGLVertexBuffer::prepare()
  112. {
  113. AssertFatal(0, "GFXGLVertexBuffer::prepare - use GFXGLVertexBuffer::prepare(U32 stream, U32 divisor)");
  114. }
  115. void GFXGLVertexBuffer::prepare(U32 stream, U32 divisor)
  116. {
  117. if( GFXGL->mCapabilities.vertexAttributeBinding )
  118. {
  119. glBindVertexBuffer( stream, mBuffer, mBufferOffset, mVertexSize );
  120. glVertexBindingDivisor( stream, divisor );
  121. return;
  122. }
  123. }
  124. void GFXGLVertexBuffer::finish()
  125. {
  126. }
  127. GLvoid* GFXGLVertexBuffer::getBuffer()
  128. {
  129. // NULL specifies no offset into the hardware buffer
  130. return (GLvoid*)NULL;
  131. }
  132. void GFXGLVertexBuffer::zombify()
  133. {
  134. if(mZombieCache || !mBuffer)
  135. return;
  136. mZombieCache = new U8[mNumVerts * mVertexSize];
  137. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  138. glGetBufferSubData(GL_ARRAY_BUFFER, 0, mNumVerts * mVertexSize, mZombieCache);
  139. glBindBuffer(GL_ARRAY_BUFFER, 0);
  140. glDeleteBuffers(1, &mBuffer);
  141. mBuffer = 0;
  142. }
  143. void GFXGLVertexBuffer::resurrect()
  144. {
  145. if(!mZombieCache)
  146. return;
  147. glGenBuffers(1, &mBuffer);
  148. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  149. glBufferData(GL_ARRAY_BUFFER, mNumVerts * mVertexSize, mZombieCache, GFXGLBufferType[mBufferType]);
  150. glBindBuffer(GL_ARRAY_BUFFER, 0);
  151. delete[] mZombieCache;
  152. mZombieCache = NULL;
  153. }
  154. namespace
  155. {
  156. bool onGFXDeviceSignal( GFXDevice::GFXDeviceEventType type )
  157. {
  158. if( GFX->getAdapterType() == OpenGL && GFXDevice::deEndOfFrame == type )
  159. getCircularVolatileVertexBuffer()->protectUsedRange();
  160. return true;
  161. }
  162. }
  163. MODULE_BEGIN( GFX_GL_VertexBuffer )
  164. MODULE_INIT_AFTER( gfx )
  165. MODULE_SHUTDOWN_BEFORE( gfx )
  166. MODULE_INIT
  167. {
  168. GFXDevice::getDeviceEventSignal().notify( &onGFXDeviceSignal );
  169. }
  170. MODULE_SHUTDOWN
  171. {
  172. GFXDevice::getDeviceEventSignal( ).remove( &onGFXDeviceSignal );
  173. }
  174. MODULE_END