gfxGLVertexBuffer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. GFXGLVertexBuffer::GFXGLVertexBuffer( GFXDevice *device,
  28. U32 numVerts,
  29. const GFXVertexFormat *vertexFormat,
  30. U32 vertexSize,
  31. GFXBufferType bufferType )
  32. : GFXVertexBuffer( device, numVerts, vertexFormat, vertexSize, bufferType ),
  33. mZombieCache(NULL)
  34. {
  35. PRESERVE_VERTEX_BUFFER();
  36. // Generate a buffer and allocate the needed memory.
  37. glGenBuffers(1, &mBuffer);
  38. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  39. glBufferData(GL_ARRAY_BUFFER, numVerts * vertexSize, NULL, GFXGLBufferType[bufferType]);
  40. glBindBuffer(GL_ARRAY_BUFFER, 0);
  41. }
  42. GFXGLVertexBuffer::~GFXGLVertexBuffer()
  43. {
  44. // While heavy handed, this does delete the buffer and frees the associated memory.
  45. glDeleteBuffers(1, &mBuffer);
  46. if( mZombieCache )
  47. delete [] mZombieCache;
  48. }
  49. void GFXGLVertexBuffer::lock( U32 vertexStart, U32 vertexEnd, void **vertexPtr )
  50. {
  51. PRESERVE_VERTEX_BUFFER();
  52. // Bind us, get a pointer into the buffer, then
  53. // offset it by vertexStart so we act like the D3D layer.
  54. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  55. glBufferData(GL_ARRAY_BUFFER, mNumVerts * mVertexSize, NULL, GFXGLBufferType[mBufferType]);
  56. *vertexPtr = (void*)((U8*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY) + (vertexStart * mVertexSize));
  57. lockedVertexStart = vertexStart;
  58. lockedVertexEnd = vertexEnd;
  59. }
  60. void GFXGLVertexBuffer::unlock()
  61. {
  62. PRESERVE_VERTEX_BUFFER();
  63. // Unmap the buffer and bind 0 to GL_ARRAY_BUFFER
  64. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  65. bool res = glUnmapBuffer(GL_ARRAY_BUFFER);
  66. AssertFatal(res, "GFXGLVertexBuffer::unlock - shouldn't fail!");
  67. lockedVertexStart = 0;
  68. lockedVertexEnd = 0;
  69. }
  70. void GFXGLVertexBuffer::prepare()
  71. {
  72. // Bind the buffer...
  73. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  74. U8* buffer = (U8*)getBuffer();
  75. // Loop thru the vertex format elements adding the array state...
  76. U32 texCoordIndex = 0;
  77. for ( U32 i=0; i < mVertexFormat.getElementCount(); i++ )
  78. {
  79. const GFXVertexElement &element = mVertexFormat.getElement( i );
  80. if ( element.isSemantic( GFXSemantic::POSITION ) )
  81. {
  82. glEnableClientState( GL_VERTEX_ARRAY );
  83. glVertexPointer( element.getSizeInBytes() / 4, GL_FLOAT, mVertexSize, buffer );
  84. buffer += element.getSizeInBytes();
  85. }
  86. else if ( element.isSemantic( GFXSemantic::NORMAL ) )
  87. {
  88. glEnableClientState( GL_NORMAL_ARRAY );
  89. glNormalPointer( GL_FLOAT, mVertexSize, buffer );
  90. buffer += element.getSizeInBytes();
  91. }
  92. else if ( element.isSemantic( GFXSemantic::COLOR ) )
  93. {
  94. glEnableClientState( GL_COLOR_ARRAY );
  95. glColorPointer( element.getSizeInBytes(), GL_UNSIGNED_BYTE, mVertexSize, buffer );
  96. buffer += element.getSizeInBytes();
  97. }
  98. else // Everything else is a texture coordinate.
  99. {
  100. glClientActiveTexture( GL_TEXTURE0 + texCoordIndex );
  101. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  102. glTexCoordPointer( element.getSizeInBytes() / 4, GL_FLOAT, mVertexSize, buffer );
  103. buffer += element.getSizeInBytes();
  104. ++texCoordIndex;
  105. }
  106. }
  107. }
  108. void GFXGLVertexBuffer::finish()
  109. {
  110. glBindBuffer(GL_ARRAY_BUFFER, 0);
  111. U32 texCoordIndex = 0;
  112. for ( U32 i=0; i < mVertexFormat.getElementCount(); i++ )
  113. {
  114. const GFXVertexElement &element = mVertexFormat.getElement( i );
  115. if ( element.isSemantic( GFXSemantic::POSITION ) )
  116. glDisableClientState( GL_VERTEX_ARRAY );
  117. else if ( element.isSemantic( GFXSemantic::NORMAL ) )
  118. glDisableClientState( GL_NORMAL_ARRAY );
  119. else if ( element.isSemantic( GFXSemantic::COLOR ) )
  120. glDisableClientState( GL_COLOR_ARRAY );
  121. else
  122. {
  123. glClientActiveTexture( GL_TEXTURE0 + texCoordIndex );
  124. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  125. ++texCoordIndex;
  126. }
  127. }
  128. }
  129. GLvoid* GFXGLVertexBuffer::getBuffer()
  130. {
  131. // NULL specifies no offset into the hardware buffer
  132. return (GLvoid*)NULL;
  133. }
  134. void GFXGLVertexBuffer::zombify()
  135. {
  136. if(mZombieCache || !mBuffer)
  137. return;
  138. mZombieCache = new U8[mNumVerts * mVertexSize];
  139. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  140. glGetBufferSubData(GL_ARRAY_BUFFER, 0, mNumVerts * mVertexSize, mZombieCache);
  141. glBindBuffer(GL_ARRAY_BUFFER, 0);
  142. glDeleteBuffers(1, &mBuffer);
  143. mBuffer = 0;
  144. }
  145. void GFXGLVertexBuffer::resurrect()
  146. {
  147. if(!mZombieCache)
  148. return;
  149. glGenBuffers(1, &mBuffer);
  150. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  151. glBufferData(GL_ARRAY_BUFFER, mNumVerts * mVertexSize, mZombieCache, GFXGLBufferType[mBufferType]);
  152. glBindBuffer(GL_ARRAY_BUFFER, 0);
  153. delete[] mZombieCache;
  154. mZombieCache = NULL;
  155. }