BsGLGpuBuffer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLGpuBuffer.h"
  4. #include "Debug/BsDebug.h"
  5. #include "Profiling/BsRenderStats.h"
  6. #include "BsGLPixelFormat.h"
  7. #include "BsGLHardwareBufferManager.h"
  8. #include "BsGLCommandBuffer.h"
  9. namespace bs { namespace ct
  10. {
  11. GLGpuBuffer::GLGpuBuffer(const GPU_BUFFER_DESC& desc, GpuDeviceFlags deviceMask)
  12. : GpuBuffer(desc, deviceMask), mTextureID(0), mFormat(0)
  13. {
  14. if(desc.type == GBT_APPENDCONSUME || desc.type == GBT_INDIRECTARGUMENT || desc.type == GBT_RAW)
  15. LOGERR("Only standard and structured buffers are supported on OpenGL.");
  16. if (desc.useCounter)
  17. LOGERR("Buffer counters not supported on OpenGL.");
  18. assert((deviceMask == GDF_DEFAULT || deviceMask == GDF_PRIMARY) && "Multiple GPUs not supported natively on OpenGL.");
  19. // Note: Implement OpenGL shader storage buffers, append/consume buffers, transform feedback buffers,
  20. // indirect argument buffers and counter buffers
  21. mFormat = GLPixelUtil::getBufferFormat(desc.format);
  22. }
  23. GLGpuBuffer::~GLGpuBuffer()
  24. {
  25. glDeleteTextures(1, &mTextureID);
  26. BS_CHECK_GL_ERROR();
  27. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuBuffer);
  28. }
  29. void GLGpuBuffer::initialize()
  30. {
  31. // Create buffer
  32. if(mProperties.getType() == GBT_STRUCTURED)
  33. {
  34. const auto& props = getProperties();
  35. UINT32 size = props.getElementCount() * props.getElementSize();
  36. mBuffer.initialize(GL_SHADER_STORAGE_BUFFER, size, props.getUsage());
  37. }
  38. else
  39. {
  40. const auto& props = getProperties();
  41. UINT32 size = props.getElementCount() * props.getElementSize();
  42. mBuffer.initialize(GL_TEXTURE_BUFFER, size, props.getUsage());
  43. // Create texture
  44. glGenTextures(1, &mTextureID);
  45. BS_CHECK_GL_ERROR();
  46. glBindTexture(GL_TEXTURE_BUFFER, mTextureID);
  47. BS_CHECK_GL_ERROR();
  48. glTexBuffer(GL_TEXTURE_BUFFER, mFormat, mBuffer.getGLBufferId());
  49. BS_CHECK_GL_ERROR();
  50. }
  51. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuBuffer);
  52. GpuBuffer::initialize();
  53. }
  54. void* GLGpuBuffer::lock(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx)
  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_GpuBuffer);
  60. }
  61. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD
  62. || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  63. {
  64. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  65. }
  66. #endif
  67. return mBuffer.lock(offset, length, options);
  68. }
  69. void GLGpuBuffer::unlock()
  70. {
  71. mBuffer.unlock();
  72. }
  73. void GLGpuBuffer::readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx, UINT32 queueIdx)
  74. {
  75. mBuffer.readData(offset, length, dest);
  76. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_GpuBuffer);
  77. }
  78. void GLGpuBuffer::writeData(UINT32 offset, UINT32 length, const void* source, BufferWriteType writeFlags,
  79. UINT32 queueIdx)
  80. {
  81. mBuffer.writeData(offset, length, source, writeFlags);
  82. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  83. }
  84. void GLGpuBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset, UINT32 length,
  85. bool discardWholeBuffer, const SPtr<CommandBuffer>& commandBuffer)
  86. {
  87. auto executeRef = [this](HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset, UINT32 length)
  88. {
  89. GLGpuBuffer& glSrcBuffer = static_cast<GLGpuBuffer&>(srcBuffer);
  90. glSrcBuffer.mBuffer.copyData(mBuffer, srcOffset, dstOffset, length);
  91. };
  92. if (commandBuffer == nullptr)
  93. executeRef(srcBuffer, srcOffset, dstOffset, length);
  94. else
  95. {
  96. auto execute = [&]() { executeRef(srcBuffer, srcOffset, dstOffset, length); };
  97. SPtr<GLCommandBuffer> cb = std::static_pointer_cast<GLCommandBuffer>(commandBuffer);
  98. cb->queueCommand(execute);
  99. }
  100. }
  101. }}