[Bf-blender-cvs] [708eb259e05] temp-lineart-contained: LineArt: Patch review fixes for object load.

YimingWu noreply at git.blender.org
Sat May 7 15:01:15 CEST 2022


Commit: 708eb259e056bce3e92c1f8ea85b5bb184b69c05
Author: YimingWu
Date:   Wed May 4 21:07:20 2022 +0800
Branches: temp-lineart-contained
https://developer.blender.org/rB708eb259e056bce3e92c1f8ea85b5bb184b69c05

LineArt: Patch review fixes for object load.

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

M	source/blender/blenlib/intern/BLI_mempool.c
M	source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
M	source/blender/gpencil_modifiers/intern/lineart/lineart_cpp_bridge.cpp
M	source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
M	source/blender/gpencil_modifiers/intern/lineart/lineart_intern.h

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

diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c
index bc97b71c78e..f70b5ddd766 100644
--- a/source/blender/blenlib/intern/BLI_mempool.c
+++ b/source/blender/blenlib/intern/BLI_mempool.c
@@ -355,7 +355,32 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr)
 {
   BLI_freenode *newhead = addr;
 
+#ifndef NDEBUG
+  {
+    BLI_mempool_chunk *chunk;
+    bool found = false;
+    for (chunk = pool->chunks; chunk; chunk = chunk->next) {
+      if (ARRAY_HAS_ITEM((char *)addr, (char *)CHUNK_DATA(chunk), pool->csize)) {
+        found = true;
+        break;
+      }
+    }
+    if (!found) {
+      BLI_assert_msg(0, "Attempt to free data which is not in pool.\n");
+    }
+  }
+
+  /* Enable for debugging. */
+  if (UNLIKELY(mempool_debug_memset)) {
+    memset(addr, 255, pool->esize);
+  }
+#endif
+
   if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) {
+#ifndef NDEBUG
+    /* This will detect double free's. */
+    BLI_assert(newhead->freeword != FREEWORD);
+#endif
     newhead->freeword = FREEWORD;
   }
 
diff --git a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
index 50fe1ec4075..f93a8b4767b 100644
--- a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
+++ b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h
@@ -14,14 +14,6 @@
 
 #include <math.h>
 
-typedef struct EdgeFacePair {
-  int v1;
-  int v2;
-  int f1;
-  int f2;
-  uint16_t eflag;
-} EdgeFacePair;
-
 typedef struct LineartStaticMemPoolNode {
   Link item;
   size_t size;
@@ -177,7 +169,7 @@ typedef struct LineartEdgeChainItem {
   /** For restoring position to 3d space. */
   float gpos[3];
   float normal[3];
-  short line_type;
+  uint16_t line_type;
   char occlusion;
   unsigned char material_mask_bits;
   unsigned char intersection_mask;
@@ -195,11 +187,11 @@ typedef struct LineartChainRegisterEntry {
   char is_left;
 } LineartChainRegisterEntry;
 
-typedef struct LineartAdjacentItem {
+typedef struct LineartAdjacentEdge {
   unsigned int v1;
   unsigned int v2;
   unsigned int e;
-} LineartAdjacentItem;
+} LineartAdjacentEdge;
 
 enum eLineArtTileRecursiveLimit {
   /* If tile gets this small, it's already much smaller than a pixel. No need to continue
diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpp_bridge.cpp b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpp_bridge.cpp
index a8464f8ea66..06961bfdb40 100644
--- a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpp_bridge.cpp
+++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpp_bridge.cpp
@@ -3,14 +3,17 @@
 #include "MOD_lineart.h"
 #include "lineart_intern.h"
 
-static bool cmp_adjacent_items(const LineartAdjacentItem &p1, const LineartAdjacentItem &p2)
+static bool cmp_adjacent_items(const LineartAdjacentEdge &p1, const LineartAdjacentEdge &p2)
 {
-  int a = (int)p1.v1 - (int)p2.v1;
-  int b = (int)p1.v2 - (int)p2.v2;
+  int a = p1.v1 - p2.v1;
+  int b = p1.v2 - p2.v2;
+  /* parallel_sort() requires cmp() to return true when the first element needs to appear before
+   * the second element in the sorted array, false otherwise (strict weak ordering), see
+   * https://en.cppreference.com/w/cpp/named_req/Compare. */
   return a < 0 ? true : (a == 0 ? b < 0 : false);
 }
 
-void lineart_sort_adjacent_items(LineartAdjacentItem *ai, int length)
+void lineart_sort_adjacent_items(LineartAdjacentEdge *ai, int length)
 {
   blender::parallel_sort(ai, ai + length - 1, cmp_adjacent_items);
 }
diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
index 4995aab05ea..519a8098be2 100644
--- a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
+++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c
@@ -1503,41 +1503,13 @@ typedef struct EdgeFeatData {
   int freestyle_face_index;
   bool use_freestyle_edge;
   int freestyle_edge_index;
-  LineartEdgeNeighbor *en;
+  LineartEdgeNeighbor *edge_nabr;
 } EdgeFeatData;
 
 typedef struct EdgeFeatReduceData {
   int feat_edges;
 } EdgeFeatReduceData;
 
-typedef struct LooseEdgeData {
-  int loose_count;
-  int loose_max;
-  MEdge **loose_array;
-  SpinLock loose_lock;
-  Mesh *me;
-} LooseEdgeData;
-
-static void lineart_add_loose_edge(LooseEdgeData *loose_data, MEdge *e)
-{
-  BLI_spin_lock(&loose_data->loose_lock);
-  if (loose_data->loose_count >= loose_data->loose_max) {
-    if (!loose_data->loose_array) {
-      loose_data->loose_max = 100;
-    }
-    MEdge **new_arr = MEM_callocN(sizeof(MEdge *) * loose_data->loose_max * 2, "loose edge array");
-    if (loose_data->loose_array) {
-      memcpy(new_arr, loose_data->loose_array, sizeof(MEdge *) * loose_data->loose_max);
-      MEM_freeN(loose_data->loose_array);
-    }
-    loose_data->loose_array = new_arr;
-    loose_data->loose_max *= 2;
-  }
-  loose_data->loose_array[loose_data->loose_count] = e;
-  loose_data->loose_count++;
-  BLI_spin_unlock(&loose_data->loose_lock);
-}
-
 static void feat_data_sum_reduce(const void *__restrict UNUSED(userdata),
                                  void *__restrict chunk_join,
                                  void *__restrict chunk)
@@ -1554,39 +1526,42 @@ static void lineart_identify_mlooptri_feature_edges(void *__restrict userdata,
   EdgeFeatData *e_feat_data = (EdgeFeatData *)userdata;
   EdgeFeatReduceData *reduce_data = (EdgeFeatReduceData *)tls->userdata_chunk;
   Mesh *me = e_feat_data->me;
-  LineartEdgeNeighbor *en = e_feat_data->en;
+  LineartEdgeNeighbor *edge_nabr = e_feat_data->edge_nabr;
   const MLoopTri *mlooptri = e_feat_data->mlooptri;
 
   uint16_t edge_flag_result = 0;
 
-  /* Only add one from two pairs of mlooptri edges. */
-  if (i < en[i].e) {
+  /* Because the Edge Neighbour array contains loop edge pairs, we only need to process the first
+   * edge in the pair. Otherwise we would add the same edge that the loops represent twice. */
+  if (i < edge_nabr[i].e) {
     return;
   }
 
-  FreestyleFace *fel, *fer;
   bool face_mark_filtered = false;
   bool enable_face_mark = (e_feat_data->use_freestyle_face && e_feat_data->rb->filter_face_mark);
   bool only_contour = false;
   if (enable_face_mark) {
+    FreestyleFace *ff1, *ff2;
     int index = e_feat_data->freestyle_face_index;
     if (index > -1) {
-      fel = &((FreestyleFace *)me->pdata.layers[index].data)[mlooptri[i / 3].poly];
+      ff1 = &((FreestyleFace *)me->pdata.layers[index].data)[mlooptri[i / 3].poly];
     }
-    if (en[i].e > -1) {
-      fer = &((FreestyleFace *)me->pdata.layers[index].data)[mlooptri[en[i].e / 3].poly];
+    if (edge_nabr[i].e > -1) {
+      ff2 = &((FreestyleFace *)me->pdata.layers[index].data)[mlooptri[edge_nabr[i].e / 3].poly];
     }
     else {
-      /* Handles mesh boundary case */
-      fer = fel;
+      /* Handle mesh boundary cases: We want mesh boundaries to respect
+       * `filter_face_mark_boundaries` option the same way as face mark boundaries, and the code
+       * path is simper when it's assuming both ff1 and ff2 not NULL.   */
+      ff2 = ff1;
     }
     if (e_feat_data->rb->filter_face_mark_boundaries ^ e_feat_data->rb->filter_face_mark_invert) {
-      if ((fel->flag & FREESTYLE_FACE_MARK) || (fer->flag & FREESTYLE_FACE_MARK)) {
+      if ((ff1->flag & FREESTYLE_FACE_MARK) || (ff2->flag & FREESTYLE_FACE_MARK)) {
         face_mark_filtered = true;
       }
     }
     else {
-      if ((fel->flag & FREESTYLE_FACE_MARK) && (fer->flag & FREESTYLE_FACE_MARK) && (fer != fel)) {
+      if ((ff1->flag & FREESTYLE_FACE_MARK) && (ff2->flag & FREESTYLE_FACE_MARK) && (ff2 != ff1)) {
         face_mark_filtered = true;
       }
     }
@@ -1594,7 +1569,7 @@ static void lineart_identify_mlooptri_feature_edges(void *__restrict userdata,
       face_mark_filtered = !face_mark_filtered;
     }
     if (!face_mark_filtered) {
-      en[i].flags = LRT_EDGE_FLAG_INHIBIT;
+      edge_nabr[i].flags = LRT_EDGE_FLAG_INHIBIT;
       if (e_feat_data->rb->filter_face_mark_keep_contour) {
         only_contour = true;
       }
@@ -1604,124 +1579,166 @@ static void lineart_identify_mlooptri_feature_edges(void *__restrict userdata,
   if (enable_face_mark && !face_mark_filtered && !only_contour) {
     return;
   }
-  else {
 
-    /* Mesh boundary */
-    if (en[i].e == -1) {
-      en[i].flags = LRT_EDGE_FLAG_CONTOUR;
-      reduce_data->feat_edges += 1;
-      return;
-    }
+  /* Mesh boundary */
+  if (edge_nabr[i].e == -1) {
+    edge_nabr[i].flags = LRT_EDGE_FLAG_CONTOUR;
+    reduce_data->feat_edges += 1;
+    return;
+  }
+
+  LineartTriangle *tri1, *tri2;
+  LineartVert *vert;
+  LineartRenderBuffer *rb = e_feat_data->rb;
 
-    LineartTriangle *tri1, *tri2;
-    LineartVert *vert;
-    LineartRenderBuffer *rb = e_feat_data->rb;
+  int f1 = i / 3, f2 = edge_nabr[i].e / 3;
 
-    int f1 = i / 3, f2 = en[i].e / 3;
+  /* The mesh should already be triangulated now, so we can assume each face is a triangle. */
+  tri1 = lineart_triangle_from_index(rb, e_feat_data->tri_array, f1);
+  tri2 = lineart_triangle_from_index(rb, e_feat_data->tri_array, f2);
 
-    /* The mesh should already be triangulated now, so we can assume each face is a triangle. */
-    tri1 = lineart_triangle_from_index(rb, e_feat_data->tri_array, f1);
-    tri2 = lineart_triangle_from_index(rb, e_feat_data->tri_array, f2);
+  vert = &e_feat_data->v_array[edge_nabr[i].v1];
 
-    vert = &e_feat_data->v_array[en[i].v1];
+  double view_vector_persp[3];
+  double *view_vector = view_vector_persp;
+  double dot_1 = 0, dot_2 = 0;
+  double result;
+  bool material_back_face = ((tri1->flags | tri2->flags) & LRT_TRIANGLE_MAT_BACK_FACE_CULLING);
 
-    double vv[3];
-    double *view_vector = vv;
-    double dot_1 = 0, dot_2 = 0;
-    double result;
-    bool material_back_face = ((tri1->flags | tri2->flags) & LRT_TRIANGLE_MAT_BACK_FACE_CULLING);
+  if (rb->use_contour || rb->use_back_face_culling || material_back_face) {
+    if (rb->cam_is_persp) {
+      sub_v3_v3v3_db(view_vector, rb->camera_pos, vert->gloc);
+    }
+    else {
+      view_vector = rb->view_vector;
+    }
 
-    if (rb->use_contour || rb->use_back_fac

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list