BsGLIndexBuffer.cpp 3.7 KB

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