BsGLIndexBuffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "BsGLIndexBuffer.h"
  2. #include "BsGLHardwareBufferManager.h"
  3. #include "BsException.h"
  4. namespace BansheeEngine
  5. {
  6. GLIndexBuffer::GLIndexBuffer(IndexType idxType, UINT32 numIndexes, GpuBufferUsage usage)
  7. : IndexBuffer(idxType, numIndexes, usage, false)
  8. { }
  9. GLIndexBuffer::~GLIndexBuffer()
  10. { }
  11. void GLIndexBuffer::initialize_internal()
  12. {
  13. glGenBuffers(1, &mBufferId );
  14. if (!mBufferId)
  15. {
  16. BS_EXCEPT(InternalErrorException,
  17. "Cannot create GL index buffer");
  18. }
  19. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  20. // Initialise buffer and set usage
  21. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mSizeInBytes, NULL,
  22. GLHardwareBufferManager::getGLUsage(mUsage));
  23. IndexBuffer::initialize_internal();
  24. }
  25. void GLIndexBuffer::destroy_internal()
  26. {
  27. glDeleteBuffers(1, &mBufferId);
  28. IndexBuffer::destroy_internal();
  29. }
  30. void* GLIndexBuffer::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. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  39. if ((options == GBL_WRITE_ONLY) || (options == GBL_WRITE_ONLY_NO_OVERWRITE) || (options == GBL_WRITE_ONLY_DISCARD))
  40. {
  41. access = GL_MAP_WRITE_BIT;
  42. if(options == GBL_WRITE_ONLY_DISCARD)
  43. access |= GL_MAP_INVALIDATE_BUFFER_BIT;
  44. else if(options == GBL_WRITE_ONLY_NO_OVERWRITE)
  45. access |= GL_MAP_UNSYNCHRONIZED_BIT;
  46. }
  47. else if (options == GBL_READ_ONLY)
  48. access = GL_MAP_READ_BIT;
  49. else
  50. access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  51. void* pBuffer = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, offset, length, access);
  52. if(pBuffer == 0)
  53. {
  54. BS_EXCEPT(InternalErrorException, "Index Buffer: Out of memory");
  55. }
  56. void* retPtr = static_cast<void*>(static_cast<unsigned char*>(pBuffer));
  57. mIsLocked = true;
  58. return retPtr;
  59. }
  60. void GLIndexBuffer::unlockImpl()
  61. {
  62. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  63. if(!glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER))
  64. {
  65. BS_EXCEPT(InternalErrorException, "Buffer data corrupted, please reload");
  66. }
  67. mIsLocked = false;
  68. }
  69. void GLIndexBuffer::readData(UINT32 offset, UINT32 length,
  70. void* pDest)
  71. {
  72. void* bufferData = lock(offset, length, GBL_READ_ONLY);
  73. memcpy(pDest, bufferData, length);
  74. unlock();
  75. }
  76. void GLIndexBuffer::writeData(UINT32 offset, UINT32 length,
  77. const void* pSource, BufferWriteType writeFlags)
  78. {
  79. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  80. if(writeFlags == BufferWriteType::Discard)
  81. lockOption = GBL_WRITE_ONLY_DISCARD;
  82. else if(writeFlags == BufferWriteType::NoOverwrite)
  83. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  84. void* bufferData = lock(offset, length, lockOption);
  85. memcpy(bufferData, pSource, length);
  86. unlock();
  87. }
  88. }