diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-02-09 01:17:31 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-03-18 22:49:41 +0300 |
commit | bacfd55a0e4ca5a59ee46824c500992a3dddc6b6 (patch) | |
tree | 1c1a52204dcdf2312f975b0d03cc2b20f8c214ae /source/blender/gpu/opengl | |
parent | 4544761a2d5cc5dfc42f5e374a9ec67144d4dd40 (diff) |
GPU/GL: Add StorageBuf implementation
Almost 1:1 identical to UniformBuf implementation.
Diffstat (limited to 'source/blender/gpu/opengl')
-rw-r--r-- | source/blender/gpu/opengl/gl_backend.cc | 4 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_backend.hh | 6 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_context.hh | 2 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_storage_buffer.cc | 133 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_storage_buffer.hh | 62 |
5 files changed, 207 insertions, 0 deletions
diff --git a/source/blender/gpu/opengl/gl_backend.cc b/source/blender/gpu/opengl/gl_backend.cc index 9885b16966b..fe730ba3c05 100644 --- a/source/blender/gpu/opengl/gl_backend.cc +++ b/source/blender/gpu/opengl/gl_backend.cc @@ -426,6 +426,8 @@ GLint GLContext::max_cubemap_size = 0; GLint GLContext::max_texture_3d_size = 0; GLint GLContext::max_ubo_binds = 0; GLint GLContext::max_ubo_size = 0; +GLint GLContext::max_ssbo_binds = 0; +GLint GLContext::max_ssbo_size = 0; /** Extensions. */ @@ -493,7 +495,9 @@ void GLBackend::capabilities_init() glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &GLContext::max_texture_3d_size); glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &GLContext::max_cubemap_size); glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS, &GLContext::max_ubo_binds); + glGetIntegerv(GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS, &GLContext::max_ssbo_binds); glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &GLContext::max_ubo_size); + glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &GLContext::max_ssbo_size); GLContext::base_instance_support = GLEW_ARB_base_instance; GLContext::clear_texture_support = GLEW_ARB_clear_texture; GLContext::copy_image_support = GLEW_ARB_copy_image; diff --git a/source/blender/gpu/opengl/gl_backend.hh b/source/blender/gpu/opengl/gl_backend.hh index 9d637d7b6af..d7edd162f33 100644 --- a/source/blender/gpu/opengl/gl_backend.hh +++ b/source/blender/gpu/opengl/gl_backend.hh @@ -19,6 +19,7 @@ #include "gl_index_buffer.hh" #include "gl_query.hh" #include "gl_shader.hh" +#include "gl_storage_buffer.hh" #include "gl_texture.hh" #include "gl_uniform_buffer.hh" #include "gl_vertex_buffer.hh" @@ -101,6 +102,11 @@ class GLBackend : public GPUBackend { return new GLUniformBuf(size, name); }; + StorageBuf *storagebuf_alloc(int size, GPUUsageType usage, const char *name) override + { + return new GLStorageBuf(size, usage, name); + }; + VertBuf *vertbuf_alloc() override { return new GLVertBuf(); diff --git a/source/blender/gpu/opengl/gl_context.hh b/source/blender/gpu/opengl/gl_context.hh index 369b667cbcc..54ef1b69e22 100644 --- a/source/blender/gpu/opengl/gl_context.hh +++ b/source/blender/gpu/opengl/gl_context.hh @@ -45,6 +45,8 @@ class GLContext : public Context { static GLint max_texture_3d_size; static GLint max_ubo_size; static GLint max_ubo_binds; + static GLint max_ssbo_size; + static GLint max_ssbo_binds; /** Extensions. */ diff --git a/source/blender/gpu/opengl/gl_storage_buffer.cc b/source/blender/gpu/opengl/gl_storage_buffer.cc new file mode 100644 index 00000000000..c24f9841a09 --- /dev/null +++ b/source/blender/gpu/opengl/gl_storage_buffer.cc @@ -0,0 +1,133 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2020 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + */ + +#include "BKE_global.h" + +#include "BLI_string.h" + +#include "gpu_backend.hh" +#include "gpu_context_private.hh" + +#include "gl_backend.hh" +#include "gl_debug.hh" +#include "gl_storage_buffer.hh" +#include "gl_vertex_buffer.hh" + +namespace blender::gpu { + +/* -------------------------------------------------------------------- */ +/** \name Creation & Deletion + * \{ */ + +GLStorageBuf::GLStorageBuf(size_t size, GPUUsageType usage, const char *name) + : StorageBuf(size, name) +{ + usage_ = usage; + /* Do not create ubo GL buffer here to allow allocation from any thread. */ + BLI_assert(size <= GLContext::max_ssbo_size); +} + +GLStorageBuf::~GLStorageBuf() +{ + GLContext::buf_free(ssbo_id_); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Data upload / update + * \{ */ + +void GLStorageBuf::init() +{ + BLI_assert(GLContext::get()); + + glGenBuffers(1, &ssbo_id_); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_id_); + glBufferData(GL_SHADER_STORAGE_BUFFER, size_in_bytes_, nullptr, to_gl(this->usage_)); + + debug::object_label(GL_SHADER_STORAGE_BUFFER, ssbo_id_, name_); +} + +void GLStorageBuf::update(const void *data) +{ + if (ssbo_id_ == 0) { + this->init(); + } + glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_id_); + glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, size_in_bytes_, data); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Usage + * \{ */ + +void GLStorageBuf::bind(int slot) +{ + if (slot >= GLContext::max_ssbo_binds) { + fprintf( + stderr, + "Error: Trying to bind \"%s\" ssbo to slot %d which is above the reported limit of %d.", + name_, + slot, + GLContext::max_ssbo_binds); + return; + } + + if (ssbo_id_ == 0) { + this->init(); + } + + if (data_ != nullptr) { + this->update(data_); + MEM_SAFE_FREE(data_); + } + + slot_ = slot; + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, slot_, ssbo_id_); + +#ifdef DEBUG + BLI_assert(slot < 16); + /* TODO */ + // GLContext::get()->bound_ssbo_slots |= 1 << slot; +#endif +} + +void GLStorageBuf::unbind() +{ +#ifdef DEBUG + /* NOTE: This only unbinds the last bound slot. */ + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, slot_, 0); + /* Hope that the context did not change. */ + /* TODO */ + // GLContext::get()->bound_ssbo_slots &= ~(1 << slot_); +#endif + slot_ = 0; +} + +/** \} */ + +} // namespace blender::gpu diff --git a/source/blender/gpu/opengl/gl_storage_buffer.hh b/source/blender/gpu/opengl/gl_storage_buffer.hh new file mode 100644 index 00000000000..3280db4e521 --- /dev/null +++ b/source/blender/gpu/opengl/gl_storage_buffer.hh @@ -0,0 +1,62 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2020 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + */ + +#pragma once + +#include "MEM_guardedalloc.h" + +#include "gpu_storage_buffer_private.hh" + +#include "glew-mx.h" + +namespace blender { +namespace gpu { + +/** + * Implementation of Storage Buffers using OpenGL. + */ +class GLStorageBuf : public StorageBuf { + private: + /** Slot to which this UBO is currently bound. -1 if not bound. */ + int slot_ = -1; + /** OpenGL Object handle. */ + GLuint ssbo_id_ = 0; + /** Usage type. */ + GPUUsageType usage_; + + public: + GLStorageBuf(size_t size, GPUUsageType usage, const char *name); + ~GLStorageBuf(); + + void update(const void *data) override; + void bind(int slot) override; + void unbind() override; + + private: + void init(); + + MEM_CXX_CLASS_ALLOC_FUNCS("GLStorageBuf"); +}; + +} // namespace gpu +} // namespace blender |