Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Foucault <foucault.clem@gmail.com>2020-07-28 16:38:46 +0300
committerClément Foucault <foucault.clem@gmail.com>2020-07-29 16:03:02 +0300
commit56d0a554a86e7ff3269ba9f1c4201559d5944b71 (patch)
tree3157525108eb29eadef42b94c0c41eda694fac95
parentf84342d7e19f34bb445423cc8bf8267c1675a643 (diff)
GPU: Move gpu_uniformbuffer.c to C++
This also rewrite and simplify the module a bit.
-rw-r--r--source/blender/draw/intern/draw_manager_exec.c7
-rw-r--r--source/blender/gpu/CMakeLists.txt2
-rw-r--r--source/blender/gpu/GPU_uniformbuffer.h3
-rw-r--r--source/blender/gpu/intern/gpu_uniformbuffer.cc (renamed from source/blender/gpu/intern/gpu_uniformbuffer.c)308
4 files changed, 122 insertions, 198 deletions
diff --git a/source/blender/draw/intern/draw_manager_exec.c b/source/blender/draw/intern/draw_manager_exec.c
index b6f51ada5a1..805a49cf11e 100644
--- a/source/blender/draw/intern/draw_manager_exec.c
+++ b/source/blender/draw/intern/draw_manager_exec.c
@@ -446,6 +446,7 @@ void DRW_state_reset(void)
DRW_state_reset_ex(DRW_STATE_DEFAULT);
GPU_texture_unbind_all();
+ GPU_uniformbuffer_unbind_all();
/* Should stay constant during the whole rendering. */
GPU_point_size(5);
@@ -773,10 +774,11 @@ static bool ubo_bindings_validate(DRWShadingGroup *shgroup)
DRWPass *parent_pass = DRW_memblock_elem_from_handle(DST.vmempool->passes,
&shgroup->pass_handle);
- printf("Pass : %s, Shader : %s, Block : %s\n",
+ printf("Pass : %s, Shader : %s, Block : %s, Binding %d\n",
parent_pass->name,
shgroup->shader->name,
- blockname);
+ blockname,
+ binding);
}
}
# endif
@@ -1106,6 +1108,7 @@ static void draw_shgroup(DRWShadingGroup *shgroup, DRWState pass_state)
/* Unbinding can be costly. Skip in normal condition. */
if (G.debug & G_DEBUG_GPU) {
GPU_texture_unbind_all();
+ GPU_uniformbuffer_unbind_all();
}
}
GPU_shader_bind(shgroup->shader);
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index fd04f56bd13..03676c4afff 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -81,7 +81,7 @@ set(SRC
intern/gpu_texture.c
intern/gpu_texture_image.cc
intern/gpu_texture_smoke.cc
- intern/gpu_uniformbuffer.c
+ intern/gpu_uniformbuffer.cc
intern/gpu_vertex_buffer.cc
intern/gpu_vertex_format.cc
intern/gpu_viewport.c
diff --git a/source/blender/gpu/GPU_uniformbuffer.h b/source/blender/gpu/GPU_uniformbuffer.h
index b221ae035d3..6862c1d960d 100644
--- a/source/blender/gpu/GPU_uniformbuffer.h
+++ b/source/blender/gpu/GPU_uniformbuffer.h
@@ -42,8 +42,7 @@ void GPU_uniformbuffer_dynamic_update(GPUUniformBuffer *ubo_);
void GPU_uniformbuffer_bind(GPUUniformBuffer *ubo, int number);
void GPU_uniformbuffer_unbind(GPUUniformBuffer *ubo);
-
-int GPU_uniformbuffer_bindpoint(GPUUniformBuffer *ubo);
+void GPU_uniformbuffer_unbind_all(void);
bool GPU_uniformbuffer_is_empty(GPUUniformBuffer *ubo);
bool GPU_uniformbuffer_is_dirty(GPUUniformBuffer *ubo);
diff --git a/source/blender/gpu/intern/gpu_uniformbuffer.c b/source/blender/gpu/intern/gpu_uniformbuffer.cc
index 130e8fe7da1..b2e31894f9d 100644
--- a/source/blender/gpu/intern/gpu_uniformbuffer.c
+++ b/source/blender/gpu/intern/gpu_uniformbuffer.cc
@@ -25,6 +25,7 @@
#include <string.h>
#include "BLI_blenlib.h"
+#include "BLI_math_base.h"
#include "gpu_context_private.h"
#include "gpu_node_graph.h"
@@ -34,217 +35,63 @@
#include "GPU_material.h"
#include "GPU_uniformbuffer.h"
-typedef enum eGPUUniformBufferFlag {
- GPU_UBO_FLAG_INITIALIZED = (1 << 0),
- GPU_UBO_FLAG_DIRTY = (1 << 1),
-} eGPUUniformBufferFlag;
-
-typedef enum eGPUUniformBufferType {
- GPU_UBO_STATIC = 0,
- GPU_UBO_DYNAMIC = 1,
-} eGPUUniformBufferType;
-
-struct GPUUniformBuffer {
- int size; /* in bytes */
- GLuint bindcode; /* opengl identifier for UBO */
- int bindpoint; /* current binding point */
- eGPUUniformBufferType type;
-};
-
-#define GPUUniformBufferStatic GPUUniformBuffer
-
-typedef struct GPUUniformBufferDynamic {
- GPUUniformBuffer buffer;
- void *data; /* Continuous memory block to copy to GPU. */
- char flag;
-} GPUUniformBufferDynamic;
-
-/* Prototypes */
-static eGPUType get_padded_gpu_type(struct LinkData *link);
-static void gpu_uniformbuffer_inputs_sort(struct ListBase *inputs);
-
-/* Only support up to this type, if you want to extend it, make sure the
- * padding logic is correct for the new types. */
-#define MAX_UBO_GPU_TYPE GPU_MAT4
-
-static void gpu_uniformbuffer_initialize(GPUUniformBuffer *ubo, const void *data)
-{
- glBindBuffer(GL_UNIFORM_BUFFER, ubo->bindcode);
- glBufferData(GL_UNIFORM_BUFFER, ubo->size, data, GL_DYNAMIC_DRAW);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
-}
+typedef struct GPUUniformBuffer {
+ /** Data size in bytes. */
+ int size;
+ /** GL handle for UBO. */
+ GLuint bindcode;
+ /** Current binding point. */
+ int bindpoint;
+ /** Continuous memory block to copy to GPU. Is own by the GPUUniformBuffer. */
+ void *data;
+} GPUUniformBuffer;
GPUUniformBuffer *GPU_uniformbuffer_create(int size, const void *data, char err_out[256])
{
/* Make sure that UBO is padded to size of vec4 */
BLI_assert((size % 16) == 0);
- GPUUniformBuffer *ubo = MEM_callocN(sizeof(GPUUniformBufferStatic), "GPUUniformBufferStatic");
- ubo->size = size;
- ubo->bindpoint = -1;
-
- /* Generate Buffer object */
- ubo->bindcode = GPU_buf_alloc();
-
- if (!ubo->bindcode) {
- if (err_out) {
- BLI_strncpy(err_out, "GPUUniformBuffer: UBO create failed", 256);
- }
- GPU_uniformbuffer_free(ubo);
- return NULL;
- }
-
- if (ubo->size > GPU_max_ubo_size()) {
+ if (size > GPU_max_ubo_size()) {
if (err_out) {
BLI_strncpy(err_out, "GPUUniformBuffer: UBO too big", 256);
}
- GPU_uniformbuffer_free(ubo);
- return NULL;
- }
-
- gpu_uniformbuffer_initialize(ubo, data);
- return ubo;
-}
-
-/**
- * Create dynamic UBO from parameters
- * Return NULL if failed to create or if \param inputs: is empty.
- *
- * \param inputs: ListBase of #BLI_genericNodeN(#GPUInput).
- */
-GPUUniformBuffer *GPU_uniformbuffer_dynamic_create(ListBase *inputs, char err_out[256])
-{
- /* There is no point on creating an UBO if there is no arguments. */
- if (BLI_listbase_is_empty(inputs)) {
return NULL;
}
- GPUUniformBufferDynamic *ubo = MEM_callocN(sizeof(GPUUniformBufferDynamic),
- "GPUUniformBufferDynamic");
- ubo->buffer.type = GPU_UBO_DYNAMIC;
- ubo->buffer.bindpoint = -1;
- ubo->flag = GPU_UBO_FLAG_DIRTY;
-
- /* Generate Buffer object. */
- ubo->buffer.bindcode = GPU_buf_alloc();
-
- if (!ubo->buffer.bindcode) {
- if (err_out) {
- BLI_strncpy(err_out, "GPUUniformBuffer: UBO create failed", 256);
- }
- GPU_uniformbuffer_free(&ubo->buffer);
- return NULL;
- }
-
- if (ubo->buffer.size > GPU_max_ubo_size()) {
- if (err_out) {
- BLI_strncpy(err_out, "GPUUniformBuffer: UBO too big", 256);
- }
- GPU_uniformbuffer_free(&ubo->buffer);
- return NULL;
- }
-
- /* Make sure we comply to the ubo alignment requirements. */
- gpu_uniformbuffer_inputs_sort(inputs);
-
- LISTBASE_FOREACH (LinkData *, link, inputs) {
- const eGPUType gputype = get_padded_gpu_type(link);
- ubo->buffer.size += gputype * sizeof(float);
- }
-
- /* Round up to size of vec4 */
- ubo->buffer.size = ((ubo->buffer.size + 15) / 16) * 16;
-
- /* Allocate the data. */
- ubo->data = MEM_mallocN(ubo->buffer.size, __func__);
+ GPUUniformBuffer *ubo = (GPUUniformBuffer *)MEM_mallocN(sizeof(GPUUniformBuffer), __func__);
+ ubo->size = size;
+ ubo->data = NULL;
+ ubo->bindcode = 0;
+ ubo->bindpoint = -1;
- /* Now that we know the total ubo size we can start populating it. */
- float *offset = ubo->data;
- LISTBASE_FOREACH (LinkData *, link, inputs) {
- GPUInput *input = link->data;
- memcpy(offset, input->vec, input->type * sizeof(float));
- offset += get_padded_gpu_type(link);
+ /* Direct init. */
+ if (data != NULL) {
+ GPU_uniformbuffer_update(ubo, data);
}
- /* Note since we may create the UBOs in the CPU in a different thread than the main drawing one,
- * we don't create the UBO in the GPU here. This will happen when we first bind the UBO.
- */
-
- return &ubo->buffer;
-}
-
-/**
- * Free the data
- */
-static void gpu_uniformbuffer_dynamic_free(GPUUniformBuffer *ubo_)
-{
- BLI_assert(ubo_->type == GPU_UBO_DYNAMIC);
- GPUUniformBufferDynamic *ubo = (GPUUniformBufferDynamic *)ubo_;
-
- ubo->buffer.size = 0;
- if (ubo->data) {
- MEM_freeN(ubo->data);
- }
+ return ubo;
}
void GPU_uniformbuffer_free(GPUUniformBuffer *ubo)
{
- if (ubo->type == GPU_UBO_DYNAMIC) {
- gpu_uniformbuffer_dynamic_free(ubo);
- }
-
+ MEM_SAFE_FREE(ubo->data);
GPU_buf_free(ubo->bindcode);
MEM_freeN(ubo);
}
-static void gpu_uniformbuffer_update(GPUUniformBuffer *ubo, const void *data)
-{
- glBindBuffer(GL_UNIFORM_BUFFER, ubo->bindcode);
- glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo->size, data);
- glBindBuffer(GL_UNIFORM_BUFFER, 0);
-}
-
-void GPU_uniformbuffer_update(GPUUniformBuffer *ubo, const void *data)
-{
- BLI_assert(ubo->type == GPU_UBO_STATIC);
- gpu_uniformbuffer_update(ubo, data);
-}
-
-/**
- * We need to recalculate the internal data, and re-generate it
- * from its populated items.
- */
-void GPU_uniformbuffer_dynamic_update(GPUUniformBuffer *ubo_)
-{
- BLI_assert(ubo_->type == GPU_UBO_DYNAMIC);
- GPUUniformBufferDynamic *ubo = (GPUUniformBufferDynamic *)ubo_;
-
- if (ubo->flag & GPU_UBO_FLAG_INITIALIZED) {
- gpu_uniformbuffer_update(ubo_, ubo->data);
- }
- else {
- ubo->flag |= GPU_UBO_FLAG_INITIALIZED;
- gpu_uniformbuffer_initialize(ubo_, ubo->data);
- }
-
- ubo->flag &= ~GPU_UBO_FLAG_DIRTY;
-}
-
/**
* We need to pad some data types (vec3) on the C side
* To match the GPU expected memory block alignment.
*/
static eGPUType get_padded_gpu_type(LinkData *link)
{
- GPUInput *input = link->data;
+ GPUInput *input = (GPUInput *)link->data;
eGPUType gputype = input->type;
-
/* Unless the vec3 is followed by a float we need to treat it as a vec4. */
if (gputype == GPU_VEC3 && (link->next != NULL) &&
(((GPUInput *)link->next->data)->type != GPU_FLOAT)) {
gputype = GPU_VEC4;
}
-
return gputype;
}
@@ -254,8 +101,9 @@ static eGPUType get_padded_gpu_type(LinkData *link)
*/
static int inputs_cmp(const void *a, const void *b)
{
- const LinkData *link_a = a, *link_b = b;
- const GPUInput *input_a = link_a->data, *input_b = link_b->data;
+ const LinkData *link_a = (const LinkData *)a, *link_b = (const LinkData *)b;
+ const GPUInput *input_a = (const GPUInput *)link_a->data;
+ const GPUInput *input_b = (const GPUInput *)link_b->data;
return input_a->type < input_b->type ? 1 : 0;
}
@@ -265,15 +113,19 @@ static int inputs_cmp(const void *a, const void *b)
*/
static void gpu_uniformbuffer_inputs_sort(ListBase *inputs)
{
+/* Only support up to this type, if you want to extend it, make sure the
+ * padding logic is correct for the new types. */
+#define MAX_UBO_GPU_TYPE GPU_MAT4
+
/* Order them as mat4, vec4, vec3, vec2, float. */
BLI_listbase_sort(inputs, inputs_cmp);
/* Creates a lookup table for the different types; */
LinkData *inputs_lookup[MAX_UBO_GPU_TYPE + 1] = {NULL};
- eGPUType cur_type = MAX_UBO_GPU_TYPE + 1;
+ eGPUType cur_type = static_cast<eGPUType>(MAX_UBO_GPU_TYPE + 1);
LISTBASE_FOREACH (LinkData *, link, inputs) {
- GPUInput *input = link->data;
+ GPUInput *input = (GPUInput *)link->data;
if (input->type == GPU_MAT3) {
/* Alignment for mat3 is not handled currently, so not supported */
@@ -319,6 +171,74 @@ static void gpu_uniformbuffer_inputs_sort(ListBase *inputs)
link = link_next;
}
+#undef MAX_UBO_GPU_TYPE
+}
+
+/**
+ * Create dynamic UBO from parameters
+ * Return NULL if failed to create or if \param inputs: is empty.
+ *
+ * \param inputs: ListBase of #BLI_genericNodeN(#GPUInput).
+ */
+GPUUniformBuffer *GPU_uniformbuffer_dynamic_create(ListBase *inputs, char err_out[256])
+{
+ /* There is no point on creating an UBO if there is no arguments. */
+ if (BLI_listbase_is_empty(inputs)) {
+ return NULL;
+ }
+ /* Make sure we comply to the ubo alignment requirements. */
+ gpu_uniformbuffer_inputs_sort(inputs);
+
+ size_t buffer_size = 0;
+
+ LISTBASE_FOREACH (LinkData *, link, inputs) {
+ const eGPUType gputype = get_padded_gpu_type(link);
+ buffer_size += gputype * sizeof(float);
+ }
+ /* Round up to size of vec4. (Opengl Requirement) */
+ size_t alignment = sizeof(float[4]);
+ buffer_size = divide_ceil_u(buffer_size, alignment) * alignment;
+ void *data = MEM_mallocN(buffer_size, __func__);
+
+ /* Now that we know the total ubo size we can start populating it. */
+ float *offset = (float *)data;
+ LISTBASE_FOREACH (LinkData *, link, inputs) {
+ GPUInput *input = (GPUInput *)link->data;
+ memcpy(offset, input->vec, input->type * sizeof(float));
+ offset += get_padded_gpu_type(link);
+ }
+
+ /* Pass data as NULL for late init. */
+ GPUUniformBuffer *ubo = GPU_uniformbuffer_create(buffer_size, NULL, err_out);
+ /* Data will be update just before binding. */
+ ubo->data = data;
+ return ubo;
+}
+
+static void gpu_uniformbuffer_init(GPUUniformBuffer *ubo)
+{
+ BLI_assert(ubo->bindcode == 0);
+ ubo->bindcode = GPU_buf_alloc();
+
+ if (ubo->bindcode == 0) {
+ fprintf(stderr, "GPUUniformBuffer: UBO create failed");
+ BLI_assert(0);
+ return;
+ }
+
+ glBindBuffer(GL_UNIFORM_BUFFER, ubo->bindcode);
+ glBufferData(GL_UNIFORM_BUFFER, ubo->size, NULL, GL_DYNAMIC_DRAW);
+}
+
+void GPU_uniformbuffer_update(GPUUniformBuffer *ubo, const void *data)
+{
+ if (ubo->bindcode == 0) {
+ gpu_uniformbuffer_init(ubo);
+ }
+
+ glBindBuffer(GL_UNIFORM_BUFFER, ubo->bindcode);
+ glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo->size, data);
+ glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
void GPU_uniformbuffer_bind(GPUUniformBuffer *ubo, int number)
@@ -328,28 +248,30 @@ void GPU_uniformbuffer_bind(GPUUniformBuffer *ubo, int number)
return;
}
- if (ubo->type == GPU_UBO_DYNAMIC) {
- GPUUniformBufferDynamic *ubo_dynamic = (GPUUniformBufferDynamic *)ubo;
- if (ubo_dynamic->flag & GPU_UBO_FLAG_DIRTY) {
- GPU_uniformbuffer_dynamic_update(ubo);
- }
+ if (ubo->bindcode == 0) {
+ gpu_uniformbuffer_init(ubo);
}
- if (ubo->bindcode != 0) {
- glBindBufferBase(GL_UNIFORM_BUFFER, number, ubo->bindcode);
+ if (ubo->data != NULL) {
+ GPU_uniformbuffer_update(ubo, ubo->data);
+ MEM_SAFE_FREE(ubo->data);
}
+ glBindBufferBase(GL_UNIFORM_BUFFER, number, ubo->bindcode);
ubo->bindpoint = number;
}
void GPU_uniformbuffer_unbind(GPUUniformBuffer *ubo)
{
- ubo->bindpoint = -1;
+#ifndef NDEBUG
+ glBindBufferBase(GL_UNIFORM_BUFFER, ubo->bindpoint, 0);
+#endif
+ ubo->bindpoint = 0;
}
-int GPU_uniformbuffer_bindpoint(GPUUniformBuffer *ubo)
+void GPU_uniformbuffer_unbind_all(void)
{
- return ubo->bindpoint;
-}
-
-#undef MAX_UBO_GPU_TYPE
+ for (int i = 0; i < GPU_max_ubo_binds(); i++) {
+ glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
+ }
+} \ No newline at end of file