BsGLIndexBuffer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "BsGLIndexBuffer.h"
  2. #include "BsGLHardwareBufferManager.h"
  3. #include "BsRenderStats.h"
  4. #include "BsException.h"
  5. namespace BansheeEngine
  6. {
  7. GLIndexBufferCore::GLIndexBufferCore(IndexType idxType, UINT32 numIndexes, GpuBufferUsage usage)
  8. :IndexBufferCore(idxType, numIndexes, usage), mZeroLocked(false)
  9. { }
  10. GLIndexBufferCore::~GLIndexBufferCore()
  11. {
  12. glDeleteBuffers(1, &mBufferId);
  13. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_IndexBuffer);
  14. }
  15. void GLIndexBufferCore::initialize()
  16. {
  17. glGenBuffers(1, &mBufferId );
  18. if (!mBufferId)
  19. {
  20. BS_EXCEPT(InternalErrorException, "Cannot create GL index buffer");
  21. }
  22. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  23. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mSizeInBytes, NULL,
  24. GLHardwareBufferCoreManager::getGLUsage(mUsage));
  25. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_IndexBuffer);
  26. IndexBufferCore::initialize();
  27. }
  28. void* GLIndexBufferCore::lockImpl(UINT32 offset, UINT32 length, GpuLockOptions options)
  29. {
  30. GLenum access = 0;
  31. if(mIsLocked)
  32. {
  33. BS_EXCEPT(InternalErrorException,
  34. "Invalid attempt to lock an index buffer that has already been locked");
  35. }
  36. #if BS_PROFILING_ENABLED
  37. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  38. {
  39. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_IndexBuffer);
  40. }
  41. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  42. {
  43. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_IndexBuffer);
  44. }
  45. #endif
  46. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  47. if ((options == GBL_WRITE_ONLY) || (options == GBL_WRITE_ONLY_NO_OVERWRITE) || (options == GBL_WRITE_ONLY_DISCARD))
  48. {
  49. access = GL_MAP_WRITE_BIT;
  50. if(options == GBL_WRITE_ONLY_DISCARD)
  51. access |= GL_MAP_INVALIDATE_BUFFER_BIT;
  52. else if(options == GBL_WRITE_ONLY_NO_OVERWRITE)
  53. access |= GL_MAP_UNSYNCHRONIZED_BIT;
  54. }
  55. else if (options == GBL_READ_ONLY)
  56. access = GL_MAP_READ_BIT;
  57. else
  58. access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  59. void* pBuffer = nullptr;
  60. if (length > 0)
  61. {
  62. pBuffer = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, offset, length, access);
  63. if (pBuffer == nullptr)
  64. {
  65. BS_EXCEPT(InternalErrorException, "Index Buffer: Out of memory");
  66. }
  67. mZeroLocked = false;
  68. }
  69. else
  70. mZeroLocked = true;
  71. void* retPtr = static_cast<void*>(static_cast<unsigned char*>(pBuffer));
  72. mIsLocked = true;
  73. return retPtr;
  74. }
  75. void GLIndexBufferCore::unlockImpl()
  76. {
  77. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  78. if (!mZeroLocked)
  79. {
  80. if (!glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER))
  81. {
  82. BS_EXCEPT(InternalErrorException, "Buffer data corrupted, please reload");
  83. }
  84. }
  85. mIsLocked = false;
  86. }
  87. void GLIndexBufferCore::readData(UINT32 offset, UINT32 length,
  88. void* pDest)
  89. {
  90. void* bufferData = lock(offset, length, GBL_READ_ONLY);
  91. memcpy(pDest, bufferData, length);
  92. unlock();
  93. }
  94. void GLIndexBufferCore::writeData(UINT32 offset, UINT32 length,
  95. const void* pSource, BufferWriteType writeFlags)
  96. {
  97. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  98. if(writeFlags == BufferWriteType::Discard)
  99. lockOption = GBL_WRITE_ONLY_DISCARD;
  100. else if(writeFlags == BufferWriteType::NoOverwrite)
  101. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  102. void* bufferData = lock(offset, length, lockOption);
  103. memcpy(bufferData, pSource, length);
  104. unlock();
  105. }
  106. }