[Bf-blender-cvs] [864a2d67e80] temp-vert-normals-cleanup: Add comments, other minor improvements

Hans Goudey noreply at git.blender.org
Fri Nov 26 18:53:59 CET 2021


Commit: 864a2d67e804f4c18e4b0a087a2722ae226bd5d6
Author: Hans Goudey
Date:   Fri Nov 26 12:53:49 2021 -0500
Branches: temp-vert-normals-cleanup
https://developer.blender.org/rB864a2d67e804f4c18e4b0a087a2722ae226bd5d6

Add comments, other minor improvements

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

M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/mesh.cc
M	source/blender/blenkernel/intern/mesh_normals.cc
M	source/blender/blenkernel/intern/mesh_runtime.c
M	source/blender/blenkernel/intern/object_update.c
M	source/blender/blenkernel/intern/pbvh.c
M	source/blender/blenkernel/intern/pbvh_intern.h
M	source/blender/blenkernel/intern/shrinkwrap.c
M	source/blender/blenloader/intern/versioning_300.c
M	source/blender/modifiers/intern/MOD_mask.cc
M	source/blender/modifiers/intern/MOD_normal_edit.c
M	source/blender/modifiers/intern/MOD_ocean.c
M	source/blender/modifiers/intern/MOD_util.c
M	source/blender/nodes/geometry/nodes/node_geo_input_normal.cc

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

diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index b64caa9a98c..5e8f0cd9191 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -275,7 +275,20 @@ void BKE_mesh_recalc_looptri_with_normals(const struct MLoop *mloop,
 
 /* *** mesh_normals.cc *** */
 
+/**
+ * Tag mesh vertex and face normals to be recalculated when/if they are needed later.
+ *
+ * \note Dirty tagged normals are the default state of a new mesh, so tagging them
+ * dirty explicitly is not always necessary if the mesh is created locally.
+ */
 void BKE_mesh_normals_tag_dirty(struct Mesh *mesh);
+
+/**
+ * Calculate face normals directly into a result array.
+ *
+ * \note Usually #BKE_mesh_ensure_face_normals is the preferred way to access face normals,
+ * since they may already be calculated and cached on the mesh.
+ */
 void BKE_mesh_calc_normals_poly(const struct MVert *mvert,
                                 int mvert_len,
                                 const struct MLoop *mloop,
@@ -283,15 +296,41 @@ void BKE_mesh_calc_normals_poly(const struct MVert *mvert,
                                 const struct MPoly *mpoly,
                                 int mpoly_len,
                                 float (*r_poly_normals)[3]);
+
+/**
+ * Calculate vertex and face normals, storing the result in custom data layers on the mesh.
+ *
+ * \note It is usually preferrable to calculate normals lazily with
+ * #BKE_mesh_ensure_vertex_normals, but some areas (perhaps unnecessarily)
+ * can also calculate them eagerly.
+ */
 void BKE_mesh_calc_normals(struct Mesh *me);
 
 /**
  * Check that a mesh with non-dirty normals has vertex and face custom data layers.
  */
 void BKE_mesh_assert_normals_dirty_or_calculated(const struct Mesh *mesh);
+
+/**
+ * Make sure the vertex normal data layer exists and return it.
+ * Used for manually assigning vertex normals. Clears the dirty flag.
+ */
 float (*BKE_mesh_vertex_normals_for_write(struct Mesh *mesh))[3];
+
+/**
+ * Make sure the face normal data layer exists and return it.
+ * Used for manually assigning face normals. Clears the dirty flag.
+ */
 float (*BKE_mesh_face_normals_for_write(struct Mesh *mesh))[3];
+
+/**
+ * \warning May still return null if the mesh is empty.
+ */
 const float (*BKE_mesh_ensure_vertex_normals(const struct Mesh *mesh))[3];
+
+/**
+ * \warning May still return null if the mesh is empty.
+ */
 const float (*BKE_mesh_ensure_face_normals(const struct Mesh *mesh))[3];
 void BKE_mesh_ensure_normals_for_display(struct Mesh *mesh);
 void BKE_mesh_calc_normals_looptri(struct MVert *mverts,
diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc
index 99b07d86e8e..da1190ab6ea 100644
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@ -336,8 +336,7 @@ static void mesh_blend_read_data(BlendDataReader *reader, ID *id)
     }
   }
 
-  /* TODO: Decide whether to store normal layers in files. If we decide not to,
-   * we should always tag normals dirty when reading mesh data. */
+  /* Note: Theoretically we could avoid storing normal layers in files. */
   if (!CustomData_has_layer(&mesh->vdata, CD_NORMAL) ||
       !CustomData_has_layer(&mesh->pdata, CD_NORMAL)) {
     BKE_mesh_normals_tag_dirty(mesh);
diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc
index e92c7dd3836..195c31da012 100644
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@ -120,10 +120,6 @@ void BKE_mesh_normals_tag_dirty(Mesh *mesh)
   mesh->runtime.cd_dirty_poly |= CD_MASK_NORMAL;
 }
 
-/**
- * Make sure the vertex normal data layer exists and return it.
- * Used for manually assigning vertex normals. Clears the dirty flag.
- */
 float (*BKE_mesh_vertex_normals_for_write(Mesh *mesh))[3]
 {
   mesh->runtime.cd_dirty_vert &= ~CD_MASK_NORMAL;
@@ -131,10 +127,6 @@ float (*BKE_mesh_vertex_normals_for_write(Mesh *mesh))[3]
       &mesh->vdata, CD_NORMAL, CD_CALLOC, nullptr, mesh->totvert);
 }
 
-/**
- * Make sure the face normal data layer exists and return it.
- * Used for manually assigning face normals. Clears the dirty flag.
- */
 float (*BKE_mesh_face_normals_for_write(Mesh *mesh))[3]
 {
   mesh->runtime.cd_dirty_poly &= ~CD_MASK_NORMAL;
@@ -189,8 +181,7 @@ static void mesh_calc_normals_poly_and_vertex(MutableSpan<MVert> mverts,
       const MPoly &poly = polys[i];
       Span<MLoop> ml = loops.slice(poly.loopstart, poly.totloop);
 
-      float3 pnor_temp;
-      float3 &pnor = poly_normals.is_empty() ? pnor_temp : poly_normals[i];
+      float3 &pnor = poly_normals[i];
 
       const int i_end = poly.totloop - 1;
 
@@ -262,18 +253,10 @@ static void mesh_calc_normals_poly_and_vertex(MutableSpan<MVert> mverts,
 /** \name Mesh Normal Calculation
  * \{ */
 
-/**
- * \warning May still return null if the mesh is empty.
- */
 const float (*BKE_mesh_ensure_vertex_normals(const Mesh *mesh))[3]
 {
   if (!(mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL ||
         mesh->runtime.cd_dirty_poly & CD_MASK_NORMAL)) {
-    if (!CustomData_has_layer(&mesh->vdata, CD_NORMAL)) {
-      Mesh *me = const_cast<Mesh *>(mesh);
-      MEM_freeN(me);
-      MEM_freeN(me);
-    }
     BLI_assert(CustomData_has_layer(&mesh->vdata, CD_NORMAL) || mesh->totvert == 0);
     return (const float(*)[3])CustomData_get_layer(&mesh->vdata, CD_NORMAL);
   }
@@ -378,25 +361,9 @@ void BKE_mesh_calc_normals(Mesh *mesh)
 void BKE_mesh_assert_normals_dirty_or_calculated(const Mesh *mesh)
 {
   if (!(mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL)) {
-    /* Meshes with non-dirty normals should always have a normal custom data layer. */
-    if (!CustomData_has_layer(&mesh->vdata, CD_NORMAL)) {
-      Mesh *me = const_cast<Mesh *>(mesh);
-      MEM_freeN(me);
-      /* Cause a use after free, to quickly tell where the offending mesh was allocated.
-       * TODO: Remove. */
-      MEM_freeN(me);
-    }
     BLI_assert(CustomData_has_layer(&mesh->vdata, CD_NORMAL));
   }
   if (!(mesh->runtime.cd_dirty_poly & CD_MASK_NORMAL)) {
-    /* Meshes with non-dirty normals should always have a normal custom data layer. */
-    if (!CustomData_has_layer(&mesh->pdata, CD_NORMAL)) {
-      Mesh *me = const_cast<Mesh *>(mesh);
-      MEM_freeN(me);
-      /* Cause a use after free, to quickly tell where the offending mesh was allocated.
-       * TODO: Remove. */
-      MEM_freeN(me);
-    }
     BLI_assert(CustomData_has_layer(&mesh->vdata, CD_NORMAL));
   }
 }
diff --git a/source/blender/blenkernel/intern/mesh_runtime.c b/source/blender/blenkernel/intern/mesh_runtime.c
index fa914f62e82..be2bcfcea91 100644
--- a/source/blender/blenkernel/intern/mesh_runtime.c
+++ b/source/blender/blenkernel/intern/mesh_runtime.c
@@ -52,10 +52,10 @@
 static void mesh_runtime_init_mutexes(Mesh *mesh)
 {
   mesh->runtime.eval_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime eval_mutex");
-  mesh->runtime.normals_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime normals_mutex");
-  mesh->runtime.render_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime render_mutex");
   BLI_mutex_init(mesh->runtime.eval_mutex);
+  mesh->runtime.normals_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime normals_mutex");
   BLI_mutex_init(mesh->runtime.normals_mutex);
+  mesh->runtime.render_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime render_mutex");
   BLI_mutex_init(mesh->runtime.render_mutex);
 }
 
diff --git a/source/blender/blenkernel/intern/object_update.c b/source/blender/blenkernel/intern/object_update.c
index fd0a7da477e..6a6d5bd8f2d 100644
--- a/source/blender/blenkernel/intern/object_update.c
+++ b/source/blender/blenkernel/intern/object_update.c
@@ -184,7 +184,7 @@ void BKE_object_handle_data_update(Depsgraph *depsgraph, Scene *scene, Object *o
       cddata_masks.pmask |= CD_MASK_PROP_ALL;
       cddata_masks.lmask |= CD_MASK_PROP_ALL;
 
-      /* Also carry over normal layers to avoid recomputation. */
+      /* Also copy over normal layers to avoid recomputation. */
       cddata_masks.pmask |= CD_MASK_NORMAL;
       cddata_masks.vmask |= CD_MASK_NORMAL;
 
diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c
index 960c22c40b6..0c4f9f02af5 100644
--- a/source/blender/blenkernel/intern/pbvh.c
+++ b/source/blender/blenkernel/intern/pbvh.c
@@ -1098,9 +1098,6 @@ static void pbvh_update_normals_store_task_cb(void *__restrict userdata,
 
 static void pbvh_faces_update_normals(PBVH *pbvh, PBVHNode **nodes, int totnode)
 {
-  /* could be per node to save some memory, but also means
-   * we have to store for each vertex which node it is in */
-
   /* subtle assumptions:
    * - We know that for all edited vertices, the nodes with faces
    *   adjacent to these vertices have been marked with PBVH_UpdateNormals.
@@ -2978,7 +2975,7 @@ void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int m
 
   vi->mask = NULL;
   if (pbvh->type == PBVH_FACES) {
-    /* Cast awat const because sculpt/paint code can adjust normals when restoring mesh data. */
+    /* Cast away const because sculpt/paint code can adjust normals when restoring mesh data. */
     vi->vert_normals = (float(*)[3])BKE_mesh_ensure_vertex_normals(pbvh->mesh);
 
     vi->vmask = CustomData_get_layer(pbvh->vdata, CD_PAINT_MASK);
diff --git a/source/blender/blenkernel/intern/pbvh_intern.h b/source/blender/blenkernel/intern/pbvh_intern.h
index d674641ffeb..584ee84ce3f 100644
--- a/source/blender/blenkernel/intern/pbvh_intern.h
+++ b/source/blender/blenkernel/intern/pbvh_intern.h
@@ -131,7 +131,7 @@ struct PBVH {
   /* Mesh data */
   const struct Mesh *mesh;
 
-  /* Normals can be updated for drawing. */
+  /* Note: Normals are not const because they can be updated for drawing by sculpt code. */
   float (*vert_normals)[3];
   MVert *verts;
   const MPoly *mpoly;
diff --git a/source/blender/blenkernel/intern/shrinkwrap.c b/source/blender/blenkernel/intern/shrinkwrap.c
index 5e79d9ba9c4..8d19bfca3f4 100644
--- a/source/blender/blenkernel/intern/shrinkwrap.c
+++ b/source/blender/blenkernel/intern/shrinkwrap.c

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list