[Bf-blender-cvs] [ccd176046f6] greasepencil-object: GPencil: Move modifiers evaluation logic to Depsgraph (wip)

Antonioya noreply at git.blender.org
Thu May 9 13:27:56 CEST 2019


Commit: ccd176046f61c8532aefd92bc2d7b131b141cd44
Author: Antonioya
Date:   Thu May 9 13:27:41 2019 +0200
Branches: greasepencil-object
https://developer.blender.org/rBccd176046f61c8532aefd92bc2d7b131b141cd44

GPencil: Move modifiers evaluation logic to Depsgraph (wip)

This commits is the first step to move all logic for evaluating modifiers to depsgraph evaluation and remove from the draw manager.  This was coded in the draw manager because when new grease pencil modules were written, the depsgraph was not ready.

Move all to depsgraph reduces the complexity of draw manager, and follows the standards for evaluated data.

This allows to use the evaluated data in edition modes.

Now, it's only working in selection operators, but in the future the different tools will work with the evaluated version and not with the original version as it was before.

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

M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/BKE_gpencil_modifier.h
M	source/blender/blenkernel/intern/gpencil_modifier.c
M	source/blender/blenkernel/intern/object_update.c
M	source/blender/draw/engines/gpencil/gpencil_cache_utils.c
M	source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
M	source/blender/draw/engines/gpencil/gpencil_draw_utils.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_select.c
M	source/blender/makesdna/DNA_gpencil_types.h
M	source/blender/makesdna/DNA_object_types.h

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

diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 993d4ee8386..3c84c991019 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -54,6 +54,23 @@ struct GPUVertBuf;
 struct GPUVertFormat;
 struct GpencilBatchGroup;
 
+#define GP_SIMPLIFY(scene) ((scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_ENABLE))
+#define GP_SIMPLIFY_ONPLAY(playing) \
+  (((playing == true) && (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_ON_PLAY)) || \
+   ((scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_ON_PLAY) == 0))
+#define GP_SIMPLIFY_FILL(scene, playing) \
+  ((GP_SIMPLIFY_ONPLAY(playing) && (GP_SIMPLIFY(scene)) && \
+    (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_FILL)))
+#define GP_SIMPLIFY_MODIF(scene, playing) \
+  ((GP_SIMPLIFY_ONPLAY(playing) && (GP_SIMPLIFY(scene)) && \
+    (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_MODIFIER)))
+#define GP_SIMPLIFY_FX(scene, playing) \
+  ((GP_SIMPLIFY_ONPLAY(playing) && (GP_SIMPLIFY(scene)) && \
+    (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_FX)))
+#define GP_SIMPLIFY_BLEND(scene, playing) \
+  ((GP_SIMPLIFY_ONPLAY(playing) && (GP_SIMPLIFY(scene)) && \
+    (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_BLEND)))
+
 /* GPUBatch Cache Element */
 typedef struct GpencilBatchCacheElem {
   struct GPUBatch *batch;
@@ -106,9 +123,6 @@ typedef struct GpencilBatchCache {
   int grp_used;                        /* total groups in arrays */
   int grp_size;                        /* max size of the array */
   struct GpencilBatchGroup *grp_cache; /* array of elements */
-
-  int tot_layers;
-  struct bGPDframe *derived_array; /* runtime data created by modifiers */
 } GpencilBatchCache;
 
 /* ------------ Grease-Pencil API ------------------ */
diff --git a/source/blender/blenkernel/BKE_gpencil_modifier.h b/source/blender/blenkernel/BKE_gpencil_modifier.h
index 4655abf6e02..63b1f259049 100644
--- a/source/blender/blenkernel/BKE_gpencil_modifier.h
+++ b/source/blender/blenkernel/BKE_gpencil_modifier.h
@@ -325,4 +325,8 @@ int BKE_gpencil_time_modifier(struct Depsgraph *depsgraph,
 void BKE_gpencil_lattice_init(struct Object *ob);
 void BKE_gpencil_lattice_clear(struct Object *ob);
 
+void BKE_gpencil_modifiers_calc(struct Depsgraph *depsgraph,
+                                struct Scene *scene,
+                                struct Object *ob);
+
 #endif /* __BKE_GPENCIL_MODIFIER_H__ */
diff --git a/source/blender/blenkernel/intern/gpencil_modifier.c b/source/blender/blenkernel/intern/gpencil_modifier.c
index c8a45a92758..f0bcceb50b7 100644
--- a/source/blender/blenkernel/intern/gpencil_modifier.c
+++ b/source/blender/blenkernel/intern/gpencil_modifier.c
@@ -781,6 +781,7 @@ void BKE_gpencil_subdivide(bGPDstroke *gps, int level, int flag)
       pt_final->strength = pt->strength;
       pt_final->time = pt->time;
       pt_final->flag = pt->flag;
+      pt_final->runtime.pt_orig = pt->runtime.pt_orig;
 
       if (gps->dvert != NULL) {
         dvert = &temp_dverts[i];
@@ -803,6 +804,7 @@ void BKE_gpencil_subdivide(bGPDstroke *gps, int level, int flag)
       pt_final->strength = interpf(pt->strength, next->strength, 0.5f);
       CLAMP(pt_final->strength, GPENCIL_STRENGTH_MIN, 1.0f);
       pt_final->time = interpf(pt->time, next->time, 0.5f);
+      pt_final->runtime.pt_orig = NULL;
 
       if (gps->dvert != NULL) {
         dvert = &temp_dverts[i];
@@ -848,3 +850,125 @@ void BKE_gpencil_subdivide(bGPDstroke *gps, int level, int flag)
     }
   }
 }
+
+/* Copy frame but do not assign new memory */
+static void gpencil_copy_frame(bGPDframe *gpf, bGPDframe *derived_gpf)
+{
+  derived_gpf->prev = gpf->prev;
+  derived_gpf->next = gpf->next;
+  derived_gpf->framenum = gpf->framenum;
+  derived_gpf->flag = gpf->flag;
+  derived_gpf->key_type = gpf->key_type;
+  derived_gpf->runtime = gpf->runtime;
+  copy_m4_m4(derived_gpf->runtime.viewmatrix, gpf->runtime.viewmatrix);
+
+  /* copy strokes */
+  BLI_listbase_clear(&derived_gpf->strokes);
+  for (bGPDstroke *gps_src = gpf->strokes.first; gps_src; gps_src = gps_src->next) {
+    /* make copy of source stroke */
+    bGPDstroke *gps_dst = BKE_gpencil_stroke_duplicate(gps_src);
+
+    /* Save original pointers for using in edit and select operators. */
+    gps_dst->runtime.gps_orig = gps_src;
+    for (int i = 0; i < gps_src->totpoints; i++) {
+      bGPDspoint *pt_dst = &gps_dst->points[i];
+      pt_dst->runtime.pt_orig = &gps_src->points[i];
+    }
+
+    BLI_addtail(&derived_gpf->strokes, gps_dst);
+  }
+}
+
+/* Ensure there is a derived frame */
+static void gpencil_ensure_derived_frame(
+    int idx, Object *ob, bGPDlayer *gpl, bGPDframe *gpf, bGPDframe **derived_gpf)
+{
+  /* create derived frames array data or expand */
+  bGPDframe *derived_frames = ob->runtime.derived_frames;
+  *derived_gpf = &derived_frames[idx];
+
+  /* if derived frame create a new one */
+  if (*derived_gpf != NULL) {
+    /* first clear temp data */
+    BKE_gpencil_free_frame_runtime_data(*derived_gpf);
+  }
+  /* copy data (do not assign new memory)*/
+  gpencil_copy_frame(gpf, *derived_gpf);
+}
+
+/* Calculate gpencil modifiers */
+void BKE_gpencil_modifiers_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
+{
+  /* use original data to set reference pointers to original data */
+  Object *ob_orig = DEG_get_original_object(ob);
+  bGPdata *gpd = (bGPdata *)ob_orig->data;
+  const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
+  const bool simplify_modif = GP_SIMPLIFY_MODIF(scene, false);
+  const bool is_render = (bool)(DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
+  const bool time_remap = BKE_gpencil_has_time_modifiers(ob);
+  int cfra_eval = (int)DEG_get_ctime(depsgraph);
+
+  /* Create array of derived frames equal to number of layers. */
+  ob->runtime.tot_layers = BLI_listbase_count(&gpd->layers);
+  CLAMP_MIN(ob->runtime.tot_layers, 1);
+  if (ob->runtime.derived_frames == NULL) {
+    ob->runtime.derived_frames = MEM_callocN(sizeof(struct bGPDframe) * ob->runtime.tot_layers,
+                                             __func__);
+  }
+  else {
+    ob->runtime.derived_frames = MEM_reallocN(ob->runtime.derived_frames,
+                                              sizeof(struct bGPDframe) * ob->runtime.tot_layers);
+  }
+
+  /* Init general modifiers data. */
+  if (ob->greasepencil_modifiers.first) {
+    BKE_gpencil_lattice_init(ob);
+  }
+
+  /* *****************************************************************
+   * Loop all layers, duplicate data and apply modifiers.
+   *
+   * ******************************************************************/
+  int idx = 0;
+  for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+    /* Remap frame (Time modifier) */
+    int remap_cfra = cfra_eval;
+    if ((time_remap) && (!simplify_modif)) {
+      remap_cfra = BKE_gpencil_time_modifier(depsgraph, scene, ob, gpl, cfra_eval, is_render);
+    }
+    bGPDframe *gpf = BKE_gpencil_layer_getframe(gpl, remap_cfra, GP_GETFRAME_USE_PREV);
+
+    if (gpf == NULL) {
+      idx++;
+      continue;
+    }
+
+    /* Create a duplicate data set of stroke to modify. */
+    bGPDframe *derived_gpf = NULL;
+    gpencil_ensure_derived_frame(idx, ob, gpl, gpf, &derived_gpf);
+
+    /* Skip all if some disable flag is enabled. */
+    if ((ob->greasepencil_modifiers.first == NULL) || (is_multiedit) || (simplify_modif)) {
+      idx++;
+      continue;
+    }
+
+    /* Apply geometry modifiers (create new geometry). */
+    if (BKE_gpencil_has_geometry_modifiers(ob)) {
+      BKE_gpencil_geometry_modifiers(depsgraph, ob, gpl, derived_gpf, is_render);
+    }
+
+    /* Loop all strokes and deform them. */
+    for (bGPDstroke *gps = derived_gpf->strokes.first; gps; gps = gps->next) {
+      /* Apply modifiers that only deform geometry */
+      BKE_gpencil_stroke_modifiers(depsgraph, ob, gpl, derived_gpf, gps, is_render);
+    }
+
+    idx++;
+  }
+
+  /* Clear any lattice data. */
+  if (ob->greasepencil_modifiers.first) {
+    BKE_gpencil_lattice_clear(ob);
+  }
+}
diff --git a/source/blender/blenkernel/intern/object_update.c b/source/blender/blenkernel/intern/object_update.c
index 183bc968897..0e6f3380f4d 100644
--- a/source/blender/blenkernel/intern/object_update.c
+++ b/source/blender/blenkernel/intern/object_update.c
@@ -42,6 +42,7 @@
 #include "BKE_displist.h"
 #include "BKE_editmesh.h"
 #include "BKE_effect.h"
+#include "BKE_gpencil_modifier.h"
 #include "BKE_image.h"
 #include "BKE_key.h"
 #include "BKE_layer.h"
@@ -209,6 +210,9 @@ void BKE_object_handle_data_update(Depsgraph *depsgraph, Scene *scene, Object *o
     case OB_LATTICE:
       BKE_lattice_modifiers_calc(depsgraph, scene, ob);
       break;
+    case OB_GPENCIL:
+      BKE_gpencil_modifiers_calc(depsgraph, scene, ob);
+      break;
   }
 
   /* particles */
diff --git a/source/blender/draw/engines/gpencil/gpencil_cache_utils.c b/source/blender/draw/engines/gpencil/gpencil_cache_utils.c
index 9b833a99d30..b0608343ab3 100644
--- a/source/blender/draw/engines/gpencil/gpencil_cache_utils.c
+++ b/source/blender/draw/engines/gpencil/gpencil_cache_utils.c
@@ -274,11 +274,6 @@ static GpencilBatchCache *gpencil_batch_cache_init(Object *ob, int cfra)
 
   cache->cache_frame = cfra;
 
-  /* create array of derived frames equal to number of layers */
-  cache->tot_layers = BLI_listbase_count(&gpd->layers);
-  CLAMP_MIN(cache->tot_layers, 1);
-  cache->derived_array = MEM_callocN(sizeof(struct bGPDframe) * cache->tot_layers, "Derived GPF");
-
   return cache;
 }
 
@@ -311,15 +306,6 @@ static void gpencil_batch_cache_clear(GpencilBatchCache *cache)
   MEM_SAFE_FREE(cache->grp_cache);
   cache->grp_size = 0;
   cache->grp_used = 0;
-
-  /* clear all frames derived data */
-  for (int i = 0; i < cache->tot_layers; i++) {
-    bGPDframe *derived_gpf = &cache->derived_array[i];
-    BKE_gpencil_free_frame_runtime_data(derived_gpf);
-    derived_gpf = NULL;
-  }
-  cache->tot_layers = 0;
-  MEM_SAFE_FREE(cache->derived_array);
 }
 
 /* get cache */
@@ -362,4 +348,14 @@ void DRW_gpencil_freecache(struct Object *ob)
       gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
     }
   }
+
+  /* clear all frames derived data */
+  for (int i = 0; i < ob->ru

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list