[Bf-blender-cvs] [872cbbf84ff] tmp-overlay-engine: Cleanup: Port back the custom shape ghash to revert perf regression

Clément Foucault noreply at git.blender.org
Tue Nov 19 00:45:37 CET 2019


Commit: 872cbbf84ffb7525d2fb5d67167a7f4d2a3bfd73
Author: Clément Foucault
Date:   Tue Nov 19 00:44:35 2019 +0100
Branches: tmp-overlay-engine
https://developer.blender.org/rB872cbbf84ffb7525d2fb5d67167a7f4d2a3bfd73

Cleanup: Port back the custom shape ghash to revert perf regression

Using the DRW instancing is much slower because the modelmat ubo is too
small to contain many obmat of the same GPUBatch type.

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

M	source/blender/draw/engines/overlay/overlay_armature.c
M	source/blender/draw/engines/overlay/overlay_engine.c
M	source/blender/draw/engines/overlay/overlay_private.h
M	source/blender/draw/engines/overlay/shaders/armature_shape_outline_vert.glsl
M	source/blender/draw/engines/overlay/shaders/armature_shape_solid_vert.glsl

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

diff --git a/source/blender/draw/engines/overlay/overlay_armature.c b/source/blender/draw/engines/overlay/overlay_armature.c
index dd64dbc22bc..7dc4c4b4a35 100644
--- a/source/blender/draw/engines/overlay/overlay_armature.c
+++ b/source/blender/draw/engines/overlay/overlay_armature.c
@@ -82,6 +82,7 @@ typedef struct ArmatureDrawContext {
   DRWShadingGroup *custom_solid;
   DRWShadingGroup *custom_outline;
   DRWShadingGroup *custom_wire;
+  GHash *custom_shapes_ghash;
 
   OVERLAY_ExtraCallBuffers *extras;
 
@@ -132,12 +133,14 @@ void OVERLAY_armature_cache_init(OVERLAY_Data *vedata)
   for (int i = 0; i < 2; i++) {
     struct GPUShader *sh;
     struct GPUVertFormat *format;
-    DRWShadingGroup *grp = NULL, *grp_sub = NULL;
+    DRWShadingGroup *grp = NULL;
 
     OVERLAY_InstanceFormats *formats = OVERLAY_shader_instance_formats_get();
     OVERLAY_ArmatureCallBuffers *cb = &pd->armature_call_buffers[i];
     DRWPass **p_armature_ps = &psl->armature_ps[i];
 
+    cb->custom_shapes_ghash = BLI_ghash_ptr_new(__func__);
+
     state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK |
             (pd->armature.transparent ? DRW_STATE_BLEND_ALPHA : DRW_STATE_WRITE_DEPTH);
     DRW_PASS_CREATE(*p_armature_ps, state | pd->clipping_state);
@@ -162,26 +165,17 @@ void OVERLAY_armature_cache_init(OVERLAY_Data *vedata)
       cb->point_outline = BUF_INSTANCE(grp, format, DRW_cache_bone_point_wire_outline_get());
 
       sh = OVERLAY_shader_armature_shape(false);
-      grp = DRW_shgroup_create(sh, armature_ps);
+      cb->custom_solid = grp = DRW_shgroup_create(sh, armature_ps);
       DRW_shgroup_uniform_block_persistent(grp, "globalsBlock", G_draw.block_ubo);
-      DRW_shgroup_uniform_bool_copy(grp, "customShape", false);
       cb->box_solid = BUF_INSTANCE(grp, format, DRW_cache_bone_box_get());
       cb->octa_solid = BUF_INSTANCE(grp, format, DRW_cache_bone_octahedral_get());
 
-      cb->custom_solid = grp_sub = DRW_shgroup_create_sub(grp);
-      DRW_shgroup_uniform_bool_copy(grp_sub, "customShape", true);
-
       sh = OVERLAY_shader_armature_shape(true);
-      grp = DRW_shgroup_create(sh, armature_ps);
+      cb->custom_outline = grp = DRW_shgroup_create(sh, armature_ps);
       DRW_shgroup_state_disable(grp, DRW_STATE_CULL_BACK);
       DRW_shgroup_uniform_block_persistent(grp, "globalsBlock", G_draw.block_ubo);
-      DRW_shgroup_uniform_bool_copy(grp, "customShape", false);
       cb->box_outline = BUF_INSTANCE(grp, format, DRW_cache_bone_box_wire_get());
       cb->octa_outline = BUF_INSTANCE(grp, format, DRW_cache_bone_octahedral_wire_get());
-
-      cb->custom_outline = grp_sub = DRW_shgroup_create_sub(grp);
-      DRW_shgroup_state_disable(grp_sub, DRW_STATE_CULL_BACK);
-      DRW_shgroup_uniform_bool_copy(grp_sub, "customShape", true);
     }
     {
       format = formats->instance_extra;
@@ -452,6 +446,19 @@ static void drw_shgroup_bone_envelope(ArmatureDrawContext *ctx,
 extern void drw_batch_cache_validate(Object *custom);
 extern void drw_batch_cache_generate_requested(Object *custom);
 
+BLI_INLINE DRWCallBuffer *custom_bone_instance_shgroup(ArmatureDrawContext *ctx,
+                                                       DRWShadingGroup *grp,
+                                                       struct GPUBatch *custom_geom)
+{
+  DRWCallBuffer *buf = BLI_ghash_lookup(ctx->custom_shapes_ghash, custom_geom);
+  if (buf == NULL) {
+    OVERLAY_InstanceFormats *formats = OVERLAY_shader_instance_formats_get();
+    buf = DRW_shgroup_call_buffer_instance(grp, formats->instance_bone, custom_geom);
+    BLI_ghash_insert(ctx->custom_shapes_ghash, custom_geom, buf);
+  }
+  return buf;
+}
+
 static void drw_shgroup_bone_custom_solid(ArmatureDrawContext *ctx,
                                           const float (*bone_mat)[4],
                                           const float bone_color[4],
@@ -467,26 +474,30 @@ static void drw_shgroup_bone_custom_solid(ArmatureDrawContext *ctx,
   struct GPUBatch *edges = DRW_cache_object_edge_detection_get(custom, NULL);
   struct GPUBatch *ledges = DRW_cache_object_loose_edges_get(custom);
   BoneInstanceData inst_data;
+  DRWCallBuffer *buf;
 
   if (surf || edges || ledges) {
     mul_m4_m4m4(inst_data.mat, ctx->ob->obmat, bone_mat);
   }
 
   if (surf && ctx->custom_solid) {
+    buf = custom_bone_instance_shgroup(ctx, ctx->custom_solid, surf);
     OVERLAY_bone_instance_data_set_color_hint(&inst_data, hint_color);
     OVERLAY_bone_instance_data_set_color(&inst_data, bone_color);
-    DRW_shgroup_call_obmat(ctx->custom_solid, surf, inst_data.mat);
+    DRW_buffer_add_entry_struct(buf, inst_data.mat);
   }
 
   if (edges && ctx->custom_outline) {
+    buf = custom_bone_instance_shgroup(ctx, ctx->custom_outline, edges);
     OVERLAY_bone_instance_data_set_color(&inst_data, outline_color);
-    DRW_shgroup_call_obmat(ctx->custom_outline, edges, inst_data.mat);
+    DRW_buffer_add_entry_struct(buf, inst_data.mat);
   }
 
   if (ledges) {
+    buf = custom_bone_instance_shgroup(ctx, ctx->custom_wire, ledges);
     OVERLAY_bone_instance_data_set_color_hint(&inst_data, outline_color);
     OVERLAY_bone_instance_data_set_color(&inst_data, outline_color);
-    DRW_shgroup_call_obmat(ctx->custom_wire, ledges, inst_data.mat);
+    DRW_buffer_add_entry_struct(buf, inst_data.mat);
   }
 
   /* TODO(fclem) needs to be moved elsewhere. */
@@ -505,11 +516,12 @@ static void drw_shgroup_bone_custom_wire(ArmatureDrawContext *ctx,
   struct GPUBatch *geom = DRW_cache_object_all_edges_get(custom);
 
   if (geom) {
+    DRWCallBuffer *buf = custom_bone_instance_shgroup(ctx, ctx->custom_wire, geom);
     BoneInstanceData inst_data;
     mul_m4_m4m4(inst_data.mat, ctx->ob->obmat, bone_mat);
     OVERLAY_bone_instance_data_set_color_hint(&inst_data, color);
     OVERLAY_bone_instance_data_set_color(&inst_data, color);
-    DRW_shgroup_call_obmat(ctx->custom_wire, geom, inst_data.mat);
+    DRW_buffer_add_entry_struct(buf, inst_data.mat);
   }
 
   /* TODO(fclem) needs to be moved elsewhere. */
@@ -2129,6 +2141,7 @@ static void armature_context_setup(ArmatureDrawContext *ctx,
   ctx->custom_solid = (is_filled) ? cb->custom_solid : NULL;
   ctx->custom_outline = cb->custom_outline;
   ctx->custom_wire = cb->custom_solid; /* Use same shader. */
+  ctx->custom_shapes_ghash = cb->custom_shapes_ghash;
   ctx->transparent = pd->armature.transparent;
   ctx->show_relations = pd->armature.show_relations;
   ctx->const_color = const_color;
@@ -2186,6 +2199,18 @@ void OVERLAY_armature_cache_populate(OVERLAY_Data *vedata, Object *ob)
   draw_armature_pose(&arm_ctx);
 }
 
+void OVERLAY_armature_cache_finish(OVERLAY_Data *vedata)
+{
+  OVERLAY_PrivateData *pd = vedata->stl->pd;
+
+  for (int i = 0; i < 2; i++) {
+    if (pd->armature_call_buffers[i].custom_shapes_ghash) {
+      /* TODO(fclem): Do not free it for each frame but reuse it. Avoiding alloc cost. */
+      BLI_ghash_free(pd->armature_call_buffers[i].custom_shapes_ghash, NULL, NULL);
+    }
+  }
+}
+
 void OVERLAY_armature_draw(OVERLAY_Data *vedata)
 {
   OVERLAY_PassList *psl = vedata->psl;
diff --git a/source/blender/draw/engines/overlay/overlay_engine.c b/source/blender/draw/engines/overlay/overlay_engine.c
index 7efd8943eee..40e6d2c39b2 100644
--- a/source/blender/draw/engines/overlay/overlay_engine.c
+++ b/source/blender/draw/engines/overlay/overlay_engine.c
@@ -317,6 +317,7 @@ static void OVERLAY_cache_populate(void *vedata, Object *ob)
 
 static void OVERLAY_cache_finish(void *vedata)
 {
+  OVERLAY_armature_cache_finish(vedata);
   OVERLAY_image_cache_finish(vedata);
 }
 
diff --git a/source/blender/draw/engines/overlay/overlay_private.h b/source/blender/draw/engines/overlay/overlay_private.h
index c64f4352c05..9bdae3e533e 100644
--- a/source/blender/draw/engines/overlay/overlay_private.h
+++ b/source/blender/draw/engines/overlay/overlay_private.h
@@ -190,6 +190,7 @@ typedef struct OVERLAY_ArmatureCallBuffers {
 
   DRWShadingGroup *custom_solid;
   DRWShadingGroup *custom_outline;
+  GHash *custom_shapes_ghash;
 } OVERLAY_ArmatureCallBuffers;
 
 typedef struct OVERLAY_PrivateData {
@@ -350,6 +351,7 @@ void OVERLAY_armature_cache_init(OVERLAY_Data *vedata);
 void OVERLAY_armature_cache_populate(OVERLAY_Data *vedata, Object *ob);
 void OVERLAY_edit_armature_cache_populate(OVERLAY_Data *vedata, Object *ob);
 void OVERLAY_pose_armature_cache_populate(OVERLAY_Data *vedata, Object *ob);
+void OVERLAY_armature_cache_finish(OVERLAY_Data *vedata);
 void OVERLAY_armature_draw(OVERLAY_Data *vedata);
 
 void OVERLAY_bone_instance_data_set_color_hint(BoneInstanceData *data, const float hint_color[4]);
diff --git a/source/blender/draw/engines/overlay/shaders/armature_shape_outline_vert.glsl b/source/blender/draw/engines/overlay/shaders/armature_shape_outline_vert.glsl
index 859700d5876..cd9368a997a 100644
--- a/source/blender/draw/engines/overlay/shaders/armature_shape_outline_vert.glsl
+++ b/source/blender/draw/engines/overlay/shaders/armature_shape_outline_vert.glsl
@@ -1,6 +1,4 @@
 
-uniform bool customShape = false;
-
 /* ---- Instantiated Attrs ---- */
 in vec3 pos;
 in vec3 snor;
@@ -24,8 +22,7 @@ vec2 proj(vec4 pos)
 void main()
 {
   vec4 bone_color, state_color;
-  mat4 model_mat = customShape ? ModelMatrix : inst_obmat;
-  model_mat = extract_matrix_packed_data(model_mat, state_color, bone_color);
+  mat4 model_mat = extract_matrix_packed_data(inst_obmat, state_color, bone_color);
 
   vec4 worldPosition = model_mat * vec4(pos, 1.0);
   vec4 viewpos = ViewMatrix * worldPosition;
diff --git a/source/blender/draw/engines/overlay/shaders/armature_shape_solid_vert.glsl b/source/blender/draw/engines/overlay/shaders/armature_shape_solid_vert.glsl
index 2719ee159c8..8284bd43adc 100644
--- a/source/blender/draw/engines/overlay/shaders/armature_shape_solid_vert.glsl
+++ b/source/blender/draw/engines/overlay/shaders/armature_shape_solid_vert.glsl
@@ -1,6 +1,4 @@
 
-uniform bool customShape = false;
-
 /* ---- Instantiated Attrs ---- */
 in vec3 pos;
 in vec3 nor;
@@ -13,8 +11,7 @@ out vec4 finalColor;
 void main()
 {
   vec4 bone_color, state_color;
-  mat4 model_mat = customShape ? ModelMatrix : inst_obmat;
-  model_mat = extract_matrix_pack

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list