gfxGLPrimitiveBuffer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/ggl/ggl.h"
  26. #include "gfx/gl/gfxGLUtils.h"
  27. GFXGLPrimitiveBuffer::GFXGLPrimitiveBuffer(GFXDevice *device, U32 indexCount, U32 primitiveCount, GFXBufferType bufferType) :
  28. GFXPrimitiveBuffer(device, indexCount, primitiveCount, bufferType), mZombieCache(NULL)
  29. {
  30. PRESERVE_INDEX_BUFFER();
  31. // Generate a buffer and allocate the needed memory
  32. glGenBuffers(1, &mBuffer);
  33. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  34. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(U16), NULL, GFXGLBufferType[bufferType]);
  35. }
  36. GFXGLPrimitiveBuffer::~GFXGLPrimitiveBuffer()
  37. {
  38. // This is heavy handed, but it frees the buffer memory
  39. glDeleteBuffersARB(1, &mBuffer);
  40. if( mZombieCache )
  41. delete [] mZombieCache;
  42. }
  43. void GFXGLPrimitiveBuffer::lock(U32 indexStart, U32 indexEnd, void **indexPtr)
  44. {
  45. // Preserve previous binding
  46. PRESERVE_INDEX_BUFFER();
  47. // Bind ourselves and map
  48. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  49. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), NULL, GFXGLBufferType[mBufferType]);
  50. // Offset the buffer to indexStart
  51. *indexPtr = (void*)((U8*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY) + (indexStart * sizeof(U16)));
  52. }
  53. void GFXGLPrimitiveBuffer::unlock()
  54. {
  55. // Preserve previous binding
  56. PRESERVE_INDEX_BUFFER();
  57. // Bind ourselves and unmap
  58. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  59. bool res = glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  60. AssertFatal(res, "GFXGLPrimitiveBuffer::unlock - shouldn't fail!");
  61. }
  62. void GFXGLPrimitiveBuffer::prepare()
  63. {
  64. // Bind
  65. static_cast<GFXGLDevice*>(mDevice)->setPB(this);
  66. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  67. }
  68. void GFXGLPrimitiveBuffer::finish()
  69. {
  70. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  71. }
  72. GLvoid* GFXGLPrimitiveBuffer::getBuffer()
  73. {
  74. // NULL specifies no offset into the hardware buffer
  75. return (GLvoid*)NULL;
  76. }
  77. void GFXGLPrimitiveBuffer::zombify()
  78. {
  79. if(mZombieCache)
  80. return;
  81. mZombieCache = new U8[mIndexCount * sizeof(U16)];
  82. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  83. glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, mIndexCount * sizeof(U16), mZombieCache);
  84. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  85. glDeleteBuffers(1, &mBuffer);
  86. mBuffer = 0;
  87. }
  88. void GFXGLPrimitiveBuffer::resurrect()
  89. {
  90. if(!mZombieCache)
  91. return;
  92. glGenBuffers(1, &mBuffer);
  93. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffer);
  94. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndexCount * sizeof(U16), mZombieCache, GFXGLBufferType[mBufferType]);
  95. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  96. delete[] mZombieCache;
  97. mZombieCache = NULL;
  98. }