| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "BsGLGpuBuffer.h" |
| 4 | #include "Profiling/BsRenderStats.h" |
| 5 | #include "BsGLPixelFormat.h" |
| 6 | #include "BsGLCommandBuffer.h" |
| 7 | |
| 8 | namespace bs { namespace ct |
| 9 | { |
| 10 | static void deleteBuffer(HardwareBuffer* buffer) |
| 11 | { |
| 12 | bs_pool_delete(static_cast<GLHardwareBuffer*>(buffer)); |
| 13 | } |
| 14 | |
| 15 | GLGpuBuffer::GLGpuBuffer(const GPU_BUFFER_DESC& desc, GpuDeviceFlags deviceMask) |
| 16 | : GpuBuffer(desc, deviceMask) |
| 17 | { |
| 18 | assert((deviceMask == GDF_DEFAULT || deviceMask == GDF_PRIMARY) && "Multiple GPUs not supported natively on OpenGL." ); |
| 19 | |
| 20 | mFormat = GLPixelUtil::getBufferFormat(desc.format); |
| 21 | } |
| 22 | |
| 23 | GLGpuBuffer::GLGpuBuffer(const GPU_BUFFER_DESC& desc, SPtr<HardwareBuffer> underlyingBuffer) |
| 24 | : GpuBuffer(desc, std::move(underlyingBuffer)) |
| 25 | { |
| 26 | mFormat = GLPixelUtil::getBufferFormat(desc.format); |
| 27 | } |
| 28 | |
| 29 | GLGpuBuffer::~GLGpuBuffer() |
| 30 | { |
| 31 | if(mProperties.getType() != GBT_STRUCTURED) |
| 32 | { |
| 33 | glDeleteTextures(1, &mTextureID); |
| 34 | BS_CHECK_GL_ERROR(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void GLGpuBuffer::initialize() |
| 39 | { |
| 40 | mBufferDeleter = &deleteBuffer; |
| 41 | |
| 42 | // Create a buffer if not wrapping an external one |
| 43 | if(!mBuffer) |
| 44 | { |
| 45 | if (mProperties.getType() == GBT_STRUCTURED) |
| 46 | { |
| 47 | #if BS_OPENGL_4_2 || BS_OPENGLES_3_1 |
| 48 | const auto& props = getProperties(); |
| 49 | UINT32 size = props.getElementCount() * props.getElementSize(); |
| 50 | mBuffer = bs_pool_new<GLHardwareBuffer>(GL_SHADER_STORAGE_BUFFER, size, props.getUsage()); |
| 51 | #else |
| 52 | LOGWRN("SSBOs are not supported on the current OpenGL version." ); |
| 53 | #endif |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | const auto& props = getProperties(); |
| 58 | UINT32 size = props.getElementCount() * props.getElementSize(); |
| 59 | mBuffer = bs_pool_new<GLHardwareBuffer>(GL_TEXTURE_BUFFER, size, props.getUsage()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if(mProperties.getType() != GBT_STRUCTURED) |
| 64 | { |
| 65 | // Create texture with a specific format |
| 66 | glGenTextures(1, &mTextureID); |
| 67 | BS_CHECK_GL_ERROR(); |
| 68 | |
| 69 | glBindTexture(GL_TEXTURE_BUFFER, mTextureID); |
| 70 | BS_CHECK_GL_ERROR(); |
| 71 | |
| 72 | glTexBuffer(GL_TEXTURE_BUFFER, mFormat, static_cast<GLHardwareBuffer*>(mBuffer)->getGLBufferId()); |
| 73 | BS_CHECK_GL_ERROR(); |
| 74 | } |
| 75 | |
| 76 | GpuBuffer::initialize(); |
| 77 | } |
| 78 | }} |
| 79 | |