BsGLGpuBuffer.cpp 3.4 KB

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