BsGLVertexBuffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "BsGLHardwareBufferManager.h"
  2. #include "BsGLVertexBuffer.h"
  3. #include "BsGLVertexArrayObjectManager.h"
  4. #include "BsRenderStats.h"
  5. #include "BsException.h"
  6. namespace BansheeEngine
  7. {
  8. GLVertexBuffer::GLVertexBuffer(UINT32 vertexSize,
  9. UINT32 numVertices, GpuBufferUsage usage)
  10. : VertexBuffer(vertexSize, numVertices, usage, false)
  11. {
  12. }
  13. GLVertexBuffer::~GLVertexBuffer()
  14. {
  15. }
  16. void GLVertexBuffer::initialize_internal()
  17. {
  18. glGenBuffers(1, &mBufferId);
  19. if (!mBufferId)
  20. {
  21. BS_EXCEPT(InternalErrorException,
  22. "Cannot create GL vertex buffer");
  23. }
  24. glBindBuffer(GL_ARRAY_BUFFER, mBufferId);
  25. glBufferData(GL_ARRAY_BUFFER, mSizeInBytes, NULL,
  26. GLHardwareBufferManager::getGLUsage(mUsage));
  27. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_VertexBuffer);
  28. VertexBuffer::initialize_internal();
  29. }
  30. void GLVertexBuffer::destroy_internal()
  31. {
  32. glDeleteBuffers(1, &mBufferId);
  33. while (mVAObjects.size() > 0)
  34. GLVertexArrayObjectManager::instance().notifyBufferDestroyed(mVAObjects[0]);
  35. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_VertexBuffer);
  36. VertexBuffer::destroy_internal();
  37. }
  38. void GLVertexBuffer::registerVAO(const GLVertexArrayObject& vao)
  39. {
  40. mVAObjects.push_back(vao);
  41. }
  42. void GLVertexBuffer::unregisterVAO(const GLVertexArrayObject& vao)
  43. {
  44. auto iterFind = std::find(mVAObjects.begin(), mVAObjects.end(), vao);
  45. if (iterFind != mVAObjects.end())
  46. mVAObjects.erase(iterFind);
  47. }
  48. void* GLVertexBuffer::lockImpl(UINT32 offset, UINT32 length, GpuLockOptions options)
  49. {
  50. GLenum access = 0;
  51. if(mIsLocked)
  52. {
  53. BS_EXCEPT(InternalErrorException,
  54. "Invalid attempt to lock a vertex buffer that has already been locked");
  55. }
  56. #if BS_PROFILING_ENABLED
  57. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  58. {
  59. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_VertexBuffer);
  60. }
  61. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  62. {
  63. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_VertexBuffer);
  64. }
  65. #endif
  66. glBindBuffer(GL_ARRAY_BUFFER, mBufferId);
  67. if ((options == GBL_WRITE_ONLY) || (options == GBL_WRITE_ONLY_NO_OVERWRITE) || (options == GBL_WRITE_ONLY_DISCARD))
  68. {
  69. access = GL_MAP_WRITE_BIT;
  70. if(options == GBL_WRITE_ONLY_DISCARD)
  71. access |= GL_MAP_INVALIDATE_BUFFER_BIT;
  72. else if(options == GBL_WRITE_ONLY_NO_OVERWRITE)
  73. access |= GL_MAP_UNSYNCHRONIZED_BIT;
  74. }
  75. else if (options == GBL_READ_ONLY)
  76. access = GL_MAP_READ_BIT;
  77. else
  78. access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  79. void* buffer = glMapBufferRange(GL_ARRAY_BUFFER, offset, length, access);
  80. if(buffer == nullptr)
  81. {
  82. BS_EXCEPT(InternalErrorException, "Cannot map vertex buffer.");
  83. }
  84. void* retPtr = static_cast<void*>(static_cast<unsigned char*>(buffer));
  85. mIsLocked = true;
  86. return retPtr;
  87. }
  88. void GLVertexBuffer::unlockImpl(void)
  89. {
  90. glBindBuffer(GL_ARRAY_BUFFER, mBufferId);
  91. if(!glUnmapBuffer(GL_ARRAY_BUFFER))
  92. {
  93. BS_EXCEPT(InternalErrorException, "Buffer data corrupted, please reload.");
  94. }
  95. mIsLocked = false;
  96. }
  97. void GLVertexBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  98. {
  99. void* bufferData = lock(offset, length, GBL_READ_ONLY);
  100. memcpy(pDest, bufferData, length);
  101. unlock();
  102. }
  103. void GLVertexBuffer::writeData(UINT32 offset, UINT32 length,
  104. const void* pSource, BufferWriteType writeFlags)
  105. {
  106. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  107. if(writeFlags == BufferWriteType::Discard)
  108. lockOption = GBL_WRITE_ONLY_DISCARD;
  109. else if(writeFlags == BufferWriteType::NoOverwrite)
  110. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  111. void* bufferData = lock(offset, length, lockOption);
  112. memcpy(bufferData, pSource, length);
  113. unlock();
  114. }
  115. }