diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2020-08-21 13:30:55 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2020-08-21 15:16:42 +0300 |
commit | c4f122ac8f167c5296be9ee23344765f181e6314 (patch) | |
tree | d6a627aa2f3f4d43bd334556a0a37bd3c89ac66a /source/blender/gpu/opengl | |
parent | 7edd8a7738481b3d4f0720a173dca2a1853996d6 (diff) |
GPUUniformBuf: GL backend isolation
This is in preparation of vulkan backend. We move all opengl
functionnalities behind an abstract class.
This also cleansup the "dynamic" ubo create and rename it to
`GPU_uniformbuf_from_list()`
Contains, no functional change.
Part of T68990 Vulkan support.
Diffstat (limited to 'source/blender/gpu/opengl')
-rw-r--r-- | source/blender/gpu/opengl/gl_backend.hh | 11 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_batch.hh | 3 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_context.hh | 6 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_shader.cc | 2 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_uniform_buffer.cc | 126 | ||||
-rw-r--r-- | source/blender/gpu/opengl/gl_uniform_buffer.hh | 55 |
6 files changed, 198 insertions, 5 deletions
diff --git a/source/blender/gpu/opengl/gl_backend.hh b/source/blender/gpu/opengl/gl_backend.hh index 63a14af6612..0234c1a3f63 100644 --- a/source/blender/gpu/opengl/gl_backend.hh +++ b/source/blender/gpu/opengl/gl_backend.hh @@ -31,6 +31,7 @@ #include "gl_context.hh" #include "gl_drawlist.hh" #include "gl_shader.hh" +#include "gl_uniform_buffer.hh" namespace blender { namespace gpu { @@ -40,6 +41,11 @@ class GLBackend : public GPUBackend { GLSharedOrphanLists shared_orphan_list_; public: + static GLBackend *get(void) + { + return static_cast<GLBackend *>(GPUBackend::get()); + } + GPUContext *context_alloc(void *ghost_window) { return new GLContext(ghost_window, shared_orphan_list_); @@ -60,6 +66,11 @@ class GLBackend : public GPUBackend { return new GLShader(name); }; + UniformBuf *uniformbuf_alloc(int size, const char *name) + { + return new GLUniformBuf(size, name); + }; + /* TODO remove */ void buf_free(GLuint buf_id); void tex_free(GLuint tex_id); diff --git a/source/blender/gpu/opengl/gl_batch.hh b/source/blender/gpu/opengl/gl_batch.hh index 9a7767d679d..9399148c68d 100644 --- a/source/blender/gpu/opengl/gl_batch.hh +++ b/source/blender/gpu/opengl/gl_batch.hh @@ -35,6 +35,7 @@ namespace blender { namespace gpu { +class GLContext; class GLShaderInterface; #define GPU_VAO_STATIC_LEN 3 @@ -45,7 +46,7 @@ class GLShaderInterface; class GLVaoCache { private: /** Context for which the vao_cache_ was generated. */ - struct GLContext *context_ = NULL; + GLContext *context_ = NULL; /** Last interface this batch was drawn with. */ GLShaderInterface *interface_ = NULL; /** Cached vao for the last interface. */ diff --git a/source/blender/gpu/opengl/gl_context.hh b/source/blender/gpu/opengl/gl_context.hh index 0b762c939f1..ee8189255ca 100644 --- a/source/blender/gpu/opengl/gl_context.hh +++ b/source/blender/gpu/opengl/gl_context.hh @@ -32,13 +32,13 @@ #include "glew-mx.h" -#include "gl_batch.hh" - #include <mutex> namespace blender { namespace gpu { +class GLVaoCache; + class GLSharedOrphanLists { public: /** Mutex for the bellow structures. */ @@ -51,7 +51,7 @@ class GLSharedOrphanLists { void orphans_clear(void); }; -struct GLContext : public GPUContext { +class GLContext : public GPUContext { /* TODO(fclem) these needs to become private. */ public: /** Default VAO for procedural draw calls. */ diff --git a/source/blender/gpu/opengl/gl_shader.cc b/source/blender/gpu/opengl/gl_shader.cc index 93ed7a408c6..b2cca2ef45e 100644 --- a/source/blender/gpu/opengl/gl_shader.cc +++ b/source/blender/gpu/opengl/gl_shader.cc @@ -49,7 +49,7 @@ GLShader::GLShader(const char *name) : Shader(name) #ifndef __APPLE__ if ((G.debug & G_DEBUG_GPU) && (GLEW_VERSION_4_3 || GLEW_KHR_debug)) { char sh_name[64]; - BLI_snprintf(sh_name, sizeof(sh_name), "ShaderProgram-%s", name); + SNPRINTF(sh_name, "ShaderProgram-%s", name); glObjectLabel(GL_PROGRAM, shader_program_, -1, sh_name); } #endif diff --git a/source/blender/gpu/opengl/gl_uniform_buffer.cc b/source/blender/gpu/opengl/gl_uniform_buffer.cc new file mode 100644 index 00000000000..5115034639c --- /dev/null +++ b/source/blender/gpu/opengl/gl_uniform_buffer.cc @@ -0,0 +1,126 @@ +/* + * 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_extensions.h" + +#include "gpu_backend.hh" +#include "gpu_context_private.hh" + +#include "gl_backend.hh" +#include "gl_uniform_buffer.hh" + +namespace blender::gpu { + +/* -------------------------------------------------------------------- */ +/** \name Creation & Deletion + * \{ */ + +GLUniformBuf::GLUniformBuf(size_t size, const char *name) : UniformBuf(size, name) +{ + /* Do not create ubo GL buffer here to allow allocation from any thread. */ +} + +GLUniformBuf::~GLUniformBuf() +{ + GLBackend::get()->buf_free(ubo_id_); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Data upload / update + * \{ */ + +void GLUniformBuf::init(void) +{ + BLI_assert(GPU_context_active_get()); + + glGenBuffers(1, &ubo_id_); + glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_); + glBufferData(GL_UNIFORM_BUFFER, size_in_bytes_, NULL, GL_DYNAMIC_DRAW); + +#ifndef __APPLE__ + if ((G.debug & G_DEBUG_GPU) && (GLEW_VERSION_4_3 || GLEW_KHR_debug)) { + char sh_name[64]; + SNPRINTF(sh_name, "UBO-%s", name_); + glObjectLabel(GL_BUFFER, ubo_id_, -1, sh_name); + } +#endif +} + +void GLUniformBuf::update(const void *data) +{ + if (ubo_id_ == 0) { + this->init(); + } + glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_); + glBufferSubData(GL_UNIFORM_BUFFER, 0, size_in_bytes_, data); + glBindBuffer(GL_UNIFORM_BUFFER, 0); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Usage + * \{ */ + +void GLUniformBuf::bind(int slot) +{ + if (slot >= GPU_max_ubo_binds()) { + fprintf(stderr, + "Error: Trying to bind \"%s\" ubo to slot %d which is above the reported limit of %d.", + name_, + slot, + GPU_max_ubo_binds()); + return; + } + + if (ubo_id_ == 0) { + this->init(); + } + + if (data_ != NULL) { + this->update(data_); + MEM_SAFE_FREE(data_); + } + + slot_ = slot; + glBindBufferBase(GL_UNIFORM_BUFFER, slot_, ubo_id_); +} + +void GLUniformBuf::unbind(void) +{ +#ifdef DEBUG + /* NOTE: This only unbinds the last bound slot. */ + glBindBufferBase(GL_UNIFORM_BUFFER, slot_, 0); +#endif + slot_ = 0; +} + +/** \} */ + +} // namespace blender::gpu
\ No newline at end of file diff --git a/source/blender/gpu/opengl/gl_uniform_buffer.hh b/source/blender/gpu/opengl/gl_uniform_buffer.hh new file mode 100644 index 00000000000..9b904cb34ee --- /dev/null +++ b/source/blender/gpu/opengl/gl_uniform_buffer.hh @@ -0,0 +1,55 @@ +/* + * 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_uniform_buffer_private.hh" + +#include "glew-mx.h" + +namespace blender { +namespace gpu { + +class GLUniformBuf : public UniformBuf { + private: + int slot_ = -1; + GLuint ubo_id_ = 0; + + public: + GLUniformBuf(size_t size, const char *name); + ~GLUniformBuf(); + + void update(const void *data) override; + void bind(int slot) override; + void unbind(void) override; + + private: + void init(void); + + MEM_CXX_CLASS_ALLOC_FUNCS("GLUniformBuf"); +}; + +} // namespace gpu +} // namespace blender |