| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "glbuffer_p.h" |
| 41 | #include <graphicscontext_p.h> |
| 42 | |
| 43 | #if !defined(GL_UNIFORM_BUFFER) |
| 44 | #define GL_UNIFORM_BUFFER 0x8A11 |
| 45 | #endif |
| 46 | #if !defined(GL_ARRAY_BUFFER) |
| 47 | #define GL_ARRAY_BUFFER 0x8892 |
| 48 | #endif |
| 49 | #if !defined(GL_ELEMENT_ARRAY_BUFFER) |
| 50 | #define GL_ELEMENT_ARRAY_BUFFER 0x8893 |
| 51 | #endif |
| 52 | #if !defined(GL_SHADER_STORAGE_BUFFER) |
| 53 | #define GL_SHADER_STORAGE_BUFFER 0x90D2 |
| 54 | #endif |
| 55 | #if !defined(GL_PIXEL_PACK_BUFFER) |
| 56 | #define GL_PIXEL_PACK_BUFFER 0x88EB |
| 57 | #endif |
| 58 | #if !defined(GL_PIXEL_UNPACK_BUFFER) |
| 59 | #define GL_PIXEL_UNPACK_BUFFER 0x88EC |
| 60 | #endif |
| 61 | #if !defined(GL_DRAW_INDIRECT_BUFFER) |
| 62 | #define GL_DRAW_INDIRECT_BUFFER 0x8F3F |
| 63 | #endif |
| 64 | |
| 65 | QT_BEGIN_NAMESPACE |
| 66 | |
| 67 | namespace Qt3DRender { |
| 68 | |
| 69 | namespace Render { |
| 70 | |
| 71 | namespace OpenGL { |
| 72 | |
| 73 | // A UBO is created for each ShaderData Shader Pair |
| 74 | // That means a UBO is unique to a shader/shaderdata |
| 75 | |
| 76 | namespace { |
| 77 | |
| 78 | GLenum glBufferTypes[] = { |
| 79 | GL_ARRAY_BUFFER, |
| 80 | GL_UNIFORM_BUFFER, |
| 81 | GL_ELEMENT_ARRAY_BUFFER, |
| 82 | GL_SHADER_STORAGE_BUFFER, |
| 83 | GL_PIXEL_PACK_BUFFER, |
| 84 | GL_PIXEL_UNPACK_BUFFER, |
| 85 | GL_DRAW_INDIRECT_BUFFER |
| 86 | }; |
| 87 | |
| 88 | } // anonymous |
| 89 | |
| 90 | GLBuffer::GLBuffer() |
| 91 | : m_bufferId(0) |
| 92 | , m_isCreated(false) |
| 93 | , m_bound(false) |
| 94 | , m_lastTarget(GL_ARRAY_BUFFER) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | bool GLBuffer::bind(GraphicsContext *ctx, Type t) |
| 99 | { |
| 100 | if (m_bufferId == 0) |
| 101 | return false; |
| 102 | m_lastTarget = glBufferTypes[t]; |
| 103 | ctx->openGLContext()->functions()->glBindBuffer(target: m_lastTarget, buffer: m_bufferId); |
| 104 | m_bound = true; |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool GLBuffer::release(GraphicsContext *ctx) |
| 109 | { |
| 110 | m_bound = false; |
| 111 | ctx->openGLContext()->functions()->glBindBuffer(target: m_lastTarget, buffer: 0); |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | bool GLBuffer::create(GraphicsContext *ctx) |
| 116 | { |
| 117 | ctx->openGLContext()->functions()->glGenBuffers(n: 1, buffers: &m_bufferId); |
| 118 | m_isCreated = true; |
| 119 | return m_bufferId != 0; |
| 120 | } |
| 121 | |
| 122 | void GLBuffer::destroy(GraphicsContext *ctx) |
| 123 | { |
| 124 | ctx->openGLContext()->functions()->glDeleteBuffers(n: 1, buffers: &m_bufferId); |
| 125 | m_isCreated = false; |
| 126 | } |
| 127 | |
| 128 | void GLBuffer::allocate(GraphicsContext *ctx, uint size, bool dynamic) |
| 129 | { |
| 130 | // Either GL_STATIC_DRAW OR GL_DYNAMIC_DRAW depending on the use case |
| 131 | // TO DO: find a way to know how a buffer/QShaderData will be used to use the right usage |
| 132 | ctx->openGLContext()->functions()->glBufferData(target: m_lastTarget, size, NULL, usage: dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW); |
| 133 | } |
| 134 | |
| 135 | void GLBuffer::allocate(GraphicsContext *ctx, const void *data, uint size, bool dynamic) |
| 136 | { |
| 137 | ctx->openGLContext()->functions()->glBufferData(target: m_lastTarget, size, data, usage: dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW); |
| 138 | } |
| 139 | |
| 140 | void GLBuffer::update(GraphicsContext *ctx, const void *data, uint size, int offset) |
| 141 | { |
| 142 | ctx->openGLContext()->functions()->glBufferSubData(target: m_lastTarget, offset, size, data); |
| 143 | } |
| 144 | |
| 145 | QByteArray GLBuffer::download(GraphicsContext *ctx, uint size) |
| 146 | { |
| 147 | char *gpu_ptr = ctx->mapBuffer(target: m_lastTarget, size); |
| 148 | QByteArray data; |
| 149 | if (gpu_ptr != nullptr) { |
| 150 | data.resize(size); |
| 151 | std::copy(first: gpu_ptr, last: gpu_ptr+size, result: data.data()); |
| 152 | } |
| 153 | ctx->unmapBuffer(target: m_lastTarget); |
| 154 | return data; |
| 155 | } |
| 156 | |
| 157 | void GLBuffer::bindBufferBase(GraphicsContext *ctx, int bindingPoint, GLBuffer::Type t) |
| 158 | { |
| 159 | ctx->bindBufferBase(target: glBufferTypes[t], bindingIndex: bindingPoint, buffer: m_bufferId); |
| 160 | } |
| 161 | |
| 162 | void GLBuffer::bindBufferBase(GraphicsContext *ctx, int bindingPoint) |
| 163 | { |
| 164 | ctx->bindBufferBase(target: m_lastTarget, bindingIndex: bindingPoint, buffer: m_bufferId); |
| 165 | } |
| 166 | |
| 167 | } // namespace OpenGL |
| 168 | |
| 169 | } // namespace Render |
| 170 | |
| 171 | } // namespace Qt3DRender |
| 172 | |
| 173 | QT_END_NAMESPACE |
| 174 | |