BsGLGpuBuffer.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_INDIRECTARGUMENT)
  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. #if BS_OPENGL_4_2 || BS_OPENGLES_3_1
  35. const auto& props = getProperties();
  36. UINT32 size = props.getElementCount() * props.getElementSize();
  37. mBuffer.initialize(GL_SHADER_STORAGE_BUFFER, size, props.getUsage());
  38. #else
  39. LOGWRN("SSBOs are not supported on the current OpenGL version.");
  40. #endif
  41. }
  42. else
  43. {
  44. const auto& props = getProperties();
  45. UINT32 size = props.getElementCount() * props.getElementSize();
  46. mBuffer.initialize(GL_TEXTURE_BUFFER, size, props.getUsage());
  47. // Create texture
  48. glGenTextures(1, &mTextureID);
  49. BS_CHECK_GL_ERROR();
  50. glBindTexture(GL_TEXTURE_BUFFER, mTextureID);
  51. BS_CHECK_GL_ERROR();
  52. glTexBuffer(GL_TEXTURE_BUFFER, mFormat, mBuffer.getGLBufferId());
  53. BS_CHECK_GL_ERROR();
  54. }
  55. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuBuffer);
  56. GpuBuffer::initialize();
  57. }
  58. void* GLGpuBuffer::lock(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx)
  59. {
  60. #if BS_PROFILING_ENABLED
  61. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  62. {
  63. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_GpuBuffer);
  64. }
  65. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD
  66. || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  67. {
  68. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  69. }
  70. #endif
  71. return mBuffer.lock(offset, length, options);
  72. }
  73. void GLGpuBuffer::unlock()
  74. {
  75. mBuffer.unlock();
  76. }
  77. void GLGpuBuffer::readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx, UINT32 queueIdx)
  78. {
  79. mBuffer.readData(offset, length, dest);
  80. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_GpuBuffer);
  81. }
  82. void GLGpuBuffer::writeData(UINT32 offset, UINT32 length, const void* source, BufferWriteType writeFlags,
  83. UINT32 queueIdx)
  84. {
  85. mBuffer.writeData(offset, length, source, writeFlags);
  86. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  87. }
  88. void GLGpuBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset, UINT32 length,
  89. bool discardWholeBuffer, const SPtr<CommandBuffer>& commandBuffer)
  90. {
  91. auto executeRef = [this](HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset, UINT32 length)
  92. {
  93. GLGpuBuffer& glSrcBuffer = static_cast<GLGpuBuffer&>(srcBuffer);
  94. glSrcBuffer.mBuffer.copyData(mBuffer, srcOffset, dstOffset, length);
  95. };
  96. if (commandBuffer == nullptr)
  97. executeRef(srcBuffer, srcOffset, dstOffset, length);
  98. else
  99. {
  100. auto execute = [&]() { executeRef(srcBuffer, srcOffset, dstOffset, length); };
  101. SPtr<GLCommandBuffer> cb = std::static_pointer_cast<GLCommandBuffer>(commandBuffer);
  102. cb->queueCommand(execute);
  103. }
  104. }
  105. }}