BsGLIndexBuffer.cpp 3.8 KB

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