BsGLIndexBuffer.cpp 3.3 KB

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