[Bf-blender-cvs] [4b3fc7bb04b] temp-lineart-contained: LineArt: Modifier stack cache wip

YimingWu noreply at git.blender.org
Wed Apr 21 12:00:38 CEST 2021


Commit: 4b3fc7bb04b064f9cfafeeebd54aa9f64e1064f6
Author: YimingWu
Date:   Wed Apr 21 18:00:23 2021 +0800
Branches: temp-lineart-contained
https://developer.blender.org/rB4b3fc7bb04b064f9cfafeeebd54aa9f64e1064f6

LineArt: Modifier stack cache wip

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

M	source/blender/blenkernel/intern/gpencil_modifier.c
M	source/blender/gpencil_modifiers/MOD_gpencil_lineart.h
M	source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c
M	source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
M	source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c
M	source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
M	source/blender/gpencil_modifiers/intern/lineart/lineart_ops.c
M	source/blender/makesdna/DNA_gpencil_modifier_types.h
M	source/blender/makesdna/DNA_gpencil_types.h

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

diff --git a/source/blender/blenkernel/intern/gpencil_modifier.c b/source/blender/blenkernel/intern/gpencil_modifier.c
index 1f13f008464..0250121ee23 100644
--- a/source/blender/blenkernel/intern/gpencil_modifier.c
+++ b/source/blender/blenkernel/intern/gpencil_modifier.c
@@ -55,6 +55,7 @@
 #include "DEG_depsgraph.h"
 #include "DEG_depsgraph_query.h"
 
+#include "MOD_gpencil_lineart.h"
 #include "MOD_gpencil_modifiertypes.h"
 
 #include "BLO_read_write.h"
@@ -165,6 +166,21 @@ bool BKE_gpencil_has_geometry_modifiers(Object *ob)
   return false;
 }
 
+/**
+ * Check if object has grease pencil Geometry modifiers.
+ * \param ob: Grease pencil object
+ * \return True if exist
+ */
+bool BKE_gpencil_has_lineart_modifiers(Object *ob)
+{
+  LISTBASE_FOREACH (GpencilModifierData *, md, &ob->greasepencil_modifiers) {
+    if (md->type == eGpencilModifierType_Lineart) {
+      return true;
+    }
+  }
+  return false;
+}
+
 /**
  * Check if object has grease pencil Time modifiers.
  * \param ob: Grease pencil object
@@ -771,6 +787,7 @@ void BKE_gpencil_modifiers_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
   BKE_gpencil_lattice_init(ob);
 
   const bool time_remap = BKE_gpencil_has_time_modifiers(ob);
+  const bool has_lineart = BKE_gpencil_has_lineart_modifiers(ob);
 
   LISTBASE_FOREACH (GpencilModifierData *, md, &ob->greasepencil_modifiers) {
 
@@ -806,6 +823,11 @@ void BKE_gpencil_modifiers_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
 
   /* Clear any lattice data. */
   BKE_gpencil_lattice_clear(ob);
+
+  if (has_lineart) {
+    MOD_lineart_clear_cache(gpd->runtime.lineart_cache);
+    gpd->runtime.lineart_cache = NULL;
+  }
 }
 
 void BKE_gpencil_modifier_blend_write(BlendWriter *writer, ListBase *modbase)
diff --git a/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h b/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h
index 685f0cb36cb..417e65e9d3f 100644
--- a/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h
+++ b/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h
@@ -29,3 +29,7 @@ void OBJECT_OT_lineart_clear(struct wmOperatorType *ot);
 void OBJECT_OT_lineart_clear_all(struct wmOperatorType *ot);
 
 void WM_operatortypes_lineart(void);
+
+struct LineartCache;
+
+void MOD_lineart_clear_cache(struct LineartCache *lc);
diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c
index a2f35c8e575..5242f3575bd 100644
--- a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c
+++ b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c
@@ -88,7 +88,7 @@ static void generate_strokes_actual(
   }
 
   MOD_lineart_gpencil_generate(
-      lmd->render_buffer,
+      lmd->cache,
       depsgraph,
       ob,
       gpl,
@@ -156,12 +156,13 @@ static void generateStrokes(GpencilModifierData *md, Depsgraph *depsgraph, Objec
     return;
   }
 
-  MOD_lineart_compute_feature_lines(depsgraph, lmd);
+  if (!gpd->runtime.lineart_cache) {
+    MOD_lineart_compute_feature_lines(depsgraph, lmd, &gpd->runtime.lineart_cache);
+    MOD_lineart_destroy_render_data(lmd);
+  }
 
   generate_strokes_actual(md, depsgraph, ob, gpl, gpf);
 
-  MOD_lineart_destroy_render_data(lmd);
-
   WM_main_add_notifier(NA_EDITED | NC_GPENCIL, NULL);
 }
 
@@ -182,11 +183,12 @@ static void bakeModifier(Main *UNUSED(bmain),
     return;
   }
 
-  MOD_lineart_compute_feature_lines(depsgraph, lmd);
+  if (!gpd->runtime.lineart_cache) {
+    MOD_lineart_compute_feature_lines(depsgraph, lmd, &gpd->runtime.lineart_cache);
+    MOD_lineart_destroy_render_data(lmd);
+  }
 
   generate_strokes_actual(md, depsgraph, ob, gpl, gpf);
-
-  MOD_lineart_destroy_render_data(lmd);
 }
 
 static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
diff --git a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
index 4825e4e8cfa..53dd2033f4a 100644
--- a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
+++ b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
@@ -249,9 +249,9 @@ typedef struct LineartRenderBuffer {
   ListBase wasted_cuts;
   SpinLock lock_cuts;
 
-  /** Separate memory pool for chain data, this goes to the cache, so when we free the main pool,
-   * chains will still be available. */
-  LineartStaticMemPool chain_data_pool;
+  /* This is just a reference to LineartCache::chain_data_pool, which is not cleared after line art
+   * completes which serves as a cache. */
+  LineartStaticMemPool *chain_data_pool;
 
   /*  Render status */
   double view_vector[3];
@@ -329,6 +329,20 @@ typedef struct LineartRenderBuffer {
 
 } LineartRenderBuffer;
 
+typedef struct LineartCache {
+  /** Separate memory pool for chain data, this goes to the cache, so when we free the main pool,
+   * chains will still be available. */
+  LineartStaticMemPool chain_data_pool;
+
+  /** A copy of rb->Chains after calculation is done, then we can destroy rb. */
+  ListBase chains;
+
+  /** Cache only contains edge types specified in this variable.
+   * TODO: it's a fixed value (LRT_EDGE_FLAG_ALL_TYPE) right now, allow further selections in the
+   * future. */
+  char rb_edge_types;
+} LineartCache;
+
 #define DBL_TRIANGLE_LIM 1e-8
 #define DBL_EDGE_LIM 1e-9
 
@@ -591,7 +605,8 @@ int MOD_lineart_chain_count(const LineartLineChain *rlc);
 void MOD_lineart_chain_clear_picked_flag(struct LineartRenderBuffer *rb);
 
 bool MOD_lineart_compute_feature_lines(struct Depsgraph *depsgraph,
-                                       struct LineartGpencilModifierData *lmd);
+                                       struct LineartGpencilModifierData *lmd,
+                                       LineartCache **cached_result);
 
 struct Scene;
 
@@ -604,7 +619,7 @@ LineartBoundingArea *MOD_lineart_get_bounding_area(LineartRenderBuffer *rb, doub
 struct bGPDlayer;
 struct bGPDframe;
 
-void MOD_lineart_gpencil_generate(LineartRenderBuffer *rb,
+void MOD_lineart_gpencil_generate(LineartCache *cache,
                                   struct Depsgraph *depsgraph,
                                   struct Object *ob,
                                   struct bGPDlayer *gpl,
diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c b/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c
index d936509e5c5..bee002f8217 100644
--- a/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c
+++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c
@@ -74,7 +74,7 @@ static LineartEdge *lineart_line_get_connected(LineartBoundingArea *ba,
 static LineartLineChain *lineart_chain_create(LineartRenderBuffer *rb)
 {
   LineartLineChain *rlc;
-  rlc = lineart_mem_aquire(&rb->chain_data_pool, sizeof(LineartLineChain));
+  rlc = lineart_mem_aquire(rb->chain_data_pool, sizeof(LineartLineChain));
 
   BLI_addtail(&rb->chains, rlc);
 
@@ -119,7 +119,7 @@ static LineartLineChainItem *lineart_chain_append_point(LineartRenderBuffer *rb,
     return old_rlci;
   }
 
-  rlci = lineart_mem_aquire(&rb->chain_data_pool, sizeof(LineartLineChainItem));
+  rlci = lineart_mem_aquire(rb->chain_data_pool, sizeof(LineartLineChainItem));
 
   copy_v2_v2(rlci->pos, fbcoord);
   copy_v3_v3(rlci->gpos, gpos);
@@ -149,7 +149,7 @@ static LineartLineChainItem *lineart_chain_prepend_point(LineartRenderBuffer *rb
     return rlc->chain.first;
   }
 
-  rlci = lineart_mem_aquire(&rb->chain_data_pool, sizeof(LineartLineChainItem));
+  rlci = lineart_mem_aquire(rb->chain_data_pool, sizeof(LineartLineChainItem));
 
   copy_v2_v2(rlci->pos, fbcoord);
   copy_v3_v3(rlci->gpos, gpos);
diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
index 34f46cca40a..ce0264b682d 100644
--- a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
+++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
@@ -21,6 +21,7 @@
  * \ingroup editors
  */
 
+#include "MOD_gpencil_lineart.h"
 #include "MOD_lineart.h"
 
 #include "BLI_linklist.h"
@@ -110,6 +111,8 @@ static bool lineart_triangle_edge_image_space_occlusion(SpinLock *spl,
 
 static void lineart_add_edge_to_list(LineartRenderBuffer *rb, LineartEdge *e);
 
+static LineartCache *lineart_init_cache(void);
+
 static void lineart_discard_segment(LineartRenderBuffer *rb, LineartLineSegment *rls)
 {
   BLI_spin_lock(&rb->lock_cuts);
@@ -2803,18 +2806,17 @@ static void lineart_destroy_render_data(LineartRenderBuffer *rb)
   BLI_spin_end(&rb->render_data_pool.lock_mem);
 
   lineart_mem_destroy(&rb->render_data_pool);
-  lineart_mem_destroy(&rb->chain_data_pool);
 }
 
 void MOD_lineart_destroy_render_data(LineartGpencilModifierData *lmd)
 {
-  LineartRenderBuffer *rb = lmd->render_buffer;
+  LineartRenderBuffer *rb = lmd->render_buffer_onetime;
 
   lineart_destroy_render_data(rb);
 
   if (rb) {
     MEM_freeN(rb);
-    lmd->render_buffer = NULL;
+    lmd->render_buffer_onetime = NULL;
   }
 
   if (G.debug_value == 4000) {
@@ -2822,14 +2824,32 @@ void MOD_lineart_destroy_render_data(LineartGpencilModifierData *lmd)
   }
 }
 
+static LineartCache *lineart_init_cache()
+{
+  LineartCache *lc = MEM_callocN(sizeof(LineartCache *), "Lineart Cache");
+  return lc;
+}
+
+void MOD_lineart_clear_cache(struct LineartCache *lc)
+{
+  if (!lc) {
+    return;
+  }
+  lineart_mem_destroy(&lc->chain_data_pool);
+  MEM_freeN(lc);
+}
+
 static LineartRenderBuffer *lineart_create_render_buffer(Scene *scene,
-                                                         LineartGpencilModifierData *lmd)
+                                                         LineartGpencilModifierData *lmd,
+                                                         LineartCache *lc)
 {
   LineartRenderBuffer *rb = MEM_callocN(sizeof(LineartRenderBuffer), "Line Art render buffer");
 
-  lmd->render_buffer = rb;
+  lmd->cache = lc;
+  lmd->render_buffer_onetime = rb;
+  lc->rb_edge_types = LRT_EDGE_FLAG_ALL_TYPE;
 
-  if (!scene || !scene->camera) {
+  if (!scene || !scene->camera || !lc) {
     return NULL;
   }
   Camera *c = scene->camera->data;
@@ -2877,6 +2897,8 @@ static LineartRenderBuffer *lineart_create_re

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list