BsGLIndexBuffer.cpp 3.8 KB

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