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 | |
parent | 4544761a2d5cc5dfc42f5e374a9ec67144d4dd40 (diff) |
GPU/GL: Add StorageBuf implementation
Almost 1:1 identical to UniformBuf implementation.
Diffstat (limited to 'source/blender/gpu')
-rw-r--r-- | source/blender/gpu/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/blender/gpu/GPU_storage_buffer.h | 61 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_storage_buffer.cc | 108 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_storage_buffer_private.hh | 77 | ||||
-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 |
9 files changed, 455 insertions, 0 deletions
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 3ad43ef05af..ad59da4c072 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -71,6 +71,7 @@ set(SRC intern/gpu_shader_interface.cc intern/gpu_shader_log.cc intern/gpu_state.cc + intern/gpu_storage_buffer.cc intern/gpu_texture.cc intern/gpu_uniform_buffer.cc intern/gpu_vertex_buffer.cc @@ -92,6 +93,7 @@ set(SRC opengl/gl_shader_interface.cc opengl/gl_shader_log.cc opengl/gl_state.cc + opengl/gl_storage_buffer.cc opengl/gl_texture.cc opengl/gl_uniform_buffer.cc opengl/gl_vertex_array.cc diff --git a/source/blender/gpu/GPU_storage_buffer.h b/source/blender/gpu/GPU_storage_buffer.h new file mode 100644 index 00000000000..f768181e826 --- /dev/null +++ b/source/blender/gpu/GPU_storage_buffer.h @@ -0,0 +1,61 @@ +/* + * 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) 2005 Blender Foundation. + * All rights reserved. + */ + +/** \file + * \ingroup gpu + * + * Storage buffers API. Used to handle many way bigger buffers than Uniform buffers update at once. + * Make sure that the data structure is compatible with what the implementation expect. + * (see "7.8 Shader Buffer Variables and Shader Storage Blocks" from the OpenGL spec for more info + * about std430 layout) + * Rule of thumb: Padding to 16bytes, don't use vec3. + */ + +#pragma once + +#include "GPU_vertex_buffer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct ListBase; + +/** Opaque type hiding blender::gpu::StorageBuf. */ +typedef struct GPUStorageBuf GPUStorageBuf; + +GPUStorageBuf *GPU_storagebuf_create_ex(size_t size, + const void *data, + GPUUsageType usage, + const char *name); + +#define GPU_storagebuf_create(size) \ + GPU_storagebuf_create_ex(size, NULL, GPU_USAGE_DYNAMIC, __func__); + +void GPU_storagebuf_free(GPUStorageBuf *ubo); + +void GPU_storagebuf_update(GPUStorageBuf *ubo, const void *data); + +void GPU_storagebuf_bind(GPUStorageBuf *ubo, int slot); +void GPU_storagebuf_unbind(GPUStorageBuf *ubo); +void GPU_storagebuf_unbind_all(void); + +#ifdef __cplusplus +} +#endif diff --git a/source/blender/gpu/intern/gpu_storage_buffer.cc b/source/blender/gpu/intern/gpu_storage_buffer.cc new file mode 100644 index 00000000000..adbc71319b3 --- /dev/null +++ b/source/blender/gpu/intern/gpu_storage_buffer.cc @@ -0,0 +1,108 @@ +/* + * 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 "MEM_guardedalloc.h" +#include <cstring> + +#include "BLI_blenlib.h" +#include "BLI_math_base.h" + +#include "gpu_backend.hh" +#include "gpu_node_graph.h" + +#include "GPU_material.h" +#include "GPU_vertex_buffer.h" /* For GPUUsageType. */ + +#include "GPU_storage_buffer.h" +#include "gpu_storage_buffer_private.hh" + +/* -------------------------------------------------------------------- */ +/** \name Creation & Deletion + * \{ */ + +namespace blender::gpu { + +StorageBuf::StorageBuf(size_t size, const char *name) +{ + /* Make sure that UBO is padded to size of vec4 */ + BLI_assert((size % 16) == 0); + + size_in_bytes_ = size; + + BLI_strncpy(name_, name, sizeof(name_)); +} + +StorageBuf::~StorageBuf() +{ + MEM_SAFE_FREE(data_); +} + +} // namespace blender::gpu + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name C-API + * \{ */ + +using namespace blender::gpu; + +GPUStorageBuf *GPU_storagebuf_create_ex(size_t size, + const void *data, + GPUUsageType usage, + const char *name) +{ + StorageBuf *ubo = GPUBackend::get()->storagebuf_alloc(size, usage, name); + /* Direct init. */ + if (data != nullptr) { + ubo->update(data); + } + return wrap(ubo); +} + +void GPU_storagebuf_free(GPUStorageBuf *ubo) +{ + delete unwrap(ubo); +} + +void GPU_storagebuf_update(GPUStorageBuf *ubo, const void *data) +{ + unwrap(ubo)->update(data); +} + +void GPU_storagebuf_bind(GPUStorageBuf *ubo, int slot) +{ + unwrap(ubo)->bind(slot); +} + +void GPU_storagebuf_unbind(GPUStorageBuf *ubo) +{ + unwrap(ubo)->unbind(); +} + +void GPU_storagebuf_unbind_all() +{ + /* FIXME */ +} + +/** \} */ diff --git a/source/blender/gpu/intern/gpu_storage_buffer_private.hh b/source/blender/gpu/intern/gpu_storage_buffer_private.hh new file mode 100644 index 00000000000..46b7b2e9555 --- /dev/null +++ b/source/blender/gpu/intern/gpu_storage_buffer_private.hh @@ -0,0 +1,77 @@ +/* + * 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. + * + * Copyright 2020, Blender Foundation. + */ + +/** \file + * \ingroup gpu + */ + +#pragma once + +#include "BLI_sys_types.h" + +struct GPUStorageBuf; + +namespace blender { +namespace gpu { + +#ifdef DEBUG +# define DEBUG_NAME_LEN 64 +#else +# define DEBUG_NAME_LEN 8 +#endif + +/** + * Implementation of Storage Buffers. + * Base class which is then specialized for each implementation (GL, VK, ...). + */ +class StorageBuf { + protected: + /** Data size in bytes. */ + size_t size_in_bytes_; + /** Continuous memory block to copy to GPU. This data is owned by the StorageBuf. */ + void *data_ = NULL; + /** Debugging name */ + char name_[DEBUG_NAME_LEN]; + + public: + StorageBuf(size_t size, const char *name); + virtual ~StorageBuf(); + + virtual void update(const void *data) = 0; + virtual void bind(int slot) = 0; + virtual void unbind() = 0; +}; + +/* Syntactic sugar. */ +static inline GPUStorageBuf *wrap(StorageBuf *vert) +{ + return reinterpret_cast<GPUStorageBuf *>(vert); +} +static inline StorageBuf *unwrap(GPUStorageBuf *vert) +{ + return reinterpret_cast<StorageBuf *>(vert); +} +static inline const StorageBuf *unwrap(const GPUStorageBuf *vert) +{ + return reinterpret_cast<const StorageBuf *>(vert); +} + +#undef DEBUG_NAME_LEN + +} // namespace gpu +} // namespace blender 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 |