Commit: 56d0a554a86e7ff3269ba9f1c4201559d5944b71
Author: Clément Foucault
Date:   Tue Jul 28 15:38:46 2020 +0200
Branches: master
https://developer.blender.org/rB56d0a554a86e7ff3269ba9f1c4201559d5944b71

GPU: Move gpu_uniformbuffer.c to C++

This also rewrite and simplify the module a bit.

===================================================================

M       source/blender/draw/intern/draw_manager_exec.c
M       source/blender/gpu/CMakeLists.txt
M       source/blender/gpu/GPU_uniformbuffer.h
R056    source/blender/gpu/intern/gpu_uniformbuffer.c   
source/blender/gpu/intern/gpu_uniformbuffer.cc

===================================================================

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
similarity index 56%
rename from source/blender/gpu/intern/gpu_uniformbuffer.c
rename to 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 Li

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to