BsGLIndexBuffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mSizeInBytes, NULL,
  21. GLHardwareBufferManager::getGLUsage(mUsage));
  22. IndexBuffer::initialize_internal();
  23. }
  24. void GLIndexBuffer::destroy_internal()
  25. {
  26. glDeleteBuffers(1, &mBufferId);
  27. IndexBuffer::destroy_internal();
  28. }
  29. void* GLIndexBuffer::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. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  38. if ((options == GBL_WRITE_ONLY) || (options == GBL_WRITE_ONLY_NO_OVERWRITE) || (options == GBL_WRITE_ONLY_DISCARD))
  39. {
  40. access = GL_MAP_WRITE_BIT;
  41. if(options == GBL_WRITE_ONLY_DISCARD)
  42. access |= GL_MAP_INVALIDATE_BUFFER_BIT;
  43. else if(options == GBL_WRITE_ONLY_NO_OVERWRITE)
  44. access |= GL_MAP_UNSYNCHRONIZED_BIT;
  45. }
  46. else if (options == GBL_READ_ONLY)
  47. access = GL_MAP_READ_BIT;
  48. else
  49. access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  50. void* pBuffer = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, offset, length, access);
  51. if(pBuffer == 0)
  52. {
  53. BS_EXCEPT(InternalErrorException, "Index Buffer: Out of memory");
  54. }
  55. void* retPtr = static_cast<void*>(static_cast<unsigned char*>(pBuffer));
  56. mIsLocked = true;
  57. return retPtr;
  58. }
  59. void GLIndexBuffer::unlockImpl()
  60. {
  61. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  62. if(!glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER))
  63. {
  64. BS_EXCEPT(InternalErrorException, "Buffer data corrupted, please reload");
  65. }
  66. mIsLocked = false;
  67. }
  68. void GLIndexBuffer::readData(UINT32 offset, UINT32 length,
  69. void* pDest)
  70. {
  71. void* bufferData = lock(offset, length, GBL_READ_ONLY);
  72. memcpy(pDest, bufferData, length);
  73. unlock();
  74. }
  75. void GLIndexBuffer::writeData(UINT32 offset, UINT32 length,
  76. const void* pSource, BufferWriteType writeFlags)
  77. {
  78. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  79. if(writeFlags == BufferWriteType::Discard)
  80. lockOption = GBL_WRITE_ONLY_DISCARD;
  81. else if(writeFlags == BufferWriteType::NoOverwrite)
  82. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  83. void* bufferData = lock(offset, length, lockOption);
  84. memcpy(bufferData, pSource, length);
  85. unlock();
  86. }
  87. }