[Bf-blender-cvs] [27d0062d495] temp-vert-normals-cleanup: The branch finally compiles and opens the startup file

Hans Goudey noreply at git.blender.org
Tue Oct 5 20:59:04 CEST 2021


Commit: 27d0062d49588d46e5d5228c9b22a21fe43b78c1
Author: Hans Goudey
Date:   Tue Oct 5 13:58:51 2021 -0500
Branches: temp-vert-normals-cleanup
https://developer.blender.org/rB27d0062d49588d46e5d5228c9b22a21fe43b78c1

The branch finally compiles and opens the startup file

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

M	source/blender/blenkernel/intern/mesh_normals.cc
M	source/blender/blenkernel/intern/subsurf_ccg.c
M	source/blender/blenloader/intern/versioning_300.c

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

diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc
index 643ae607525..e04d3c56db6 100644
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@ -264,8 +264,15 @@ void BKE_mesh_calc_normals_poly_and_vertex(MVert *mvert,
 /** \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->totvert == 0) {
+    return nullptr;
+  }
+
   if (!(mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL ||
         mesh->runtime.cd_dirty_poly & CD_MASK_NORMAL)) {
     BLI_assert(CustomData_has_layer(&mesh->vdata, CD_NORMAL));
@@ -294,14 +301,22 @@ const float (*BKE_mesh_ensure_vertex_normals(const Mesh *mesh))[3]
        me.totpoly},
       vert_normals);
 
-  BLI_assert(!(me.runtime.cd_dirty_vert & CD_MASK_NORMAL ||
-               mesh->runtime.cd_dirty_poly & CD_MASK_NORMAL));
+  /* Clear the dirty flags, since the normals have been calculated. */
+  me.runtime.cd_dirty_vert &= ~CD_MASK_NORMAL;
+  me.runtime.cd_dirty_poly &= ~CD_MASK_NORMAL;
 
   return (const float(*)[3])vert_normals.data();
 }
 
+/**
+ * \warning May still return null if the mesh has no faces.
+ */
 const float (*BKE_mesh_ensure_face_normals(const Mesh *mesh))[3]
 {
+  if (mesh->totpoly == 0) {
+    return nullptr;
+  }
+
   if (!(mesh->runtime.cd_dirty_poly & CD_MASK_NORMAL)) {
     BLI_assert(CustomData_has_layer(&mesh->vdata, CD_NORMAL));
     return (const float(*)[3])CustomData_get_layer(&mesh->vdata, CD_NORMAL);
@@ -322,7 +337,9 @@ const float (*BKE_mesh_ensure_face_normals(const Mesh *mesh))[3]
   BKE_mesh_calc_normals_poly(
       me.mvert, me.totvert, me.mloop, me.totloop, me.mpoly, me.totpoly, poly_normals);
 
-  BLI_assert(!(mesh->runtime.cd_dirty_poly & CD_MASK_NORMAL));
+  /* Clear the dirty flags, since the normals have been calculated. */
+  me.runtime.cd_dirty_poly &= ~CD_MASK_NORMAL;
+
   return poly_normals;
 }
 
diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c
index 0c58c8e8a5a..4ad4870448b 100644
--- a/source/blender/blenkernel/intern/subsurf_ccg.c
+++ b/source/blender/blenkernel/intern/subsurf_ccg.c
@@ -804,17 +804,11 @@ static int ccgDM_getNumLoops(DerivedMesh *dm)
   return 4 * ccgSubSurf_getNumFinalFaces(ccgdm->ss);
 }
 
-static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
+static CCGElem *get_vertex_elem(CCGDerivedMesh *ccgdm, int vertNum)
 {
-  CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
   CCGSubSurf *ss = ccgdm->ss;
-  CCGElem *vd;
-  CCGKey key;
   int i;
 
-  CCG_key_top_level(&key, ss);
-  memset(mv, 0, sizeof(*mv));
-
   if ((vertNum < ccgdm->edgeMap[0].startVert) && (ccgSubSurf_getNumFaces(ss) > 0)) {
     /* this vert comes from face data */
     int lastface = ccgSubSurf_getNumFaces(ss) - 1;
@@ -843,30 +837,24 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
 
     offset = vertNum - ccgdm->faceMap[i].startVert;
     if (offset < 1) {
-      vd = ccgSubSurf_getFaceCenterData(f);
-      copy_v3_v3(mv->co, CCG_elem_co(&key, vd));
-      normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd));
+      return ccgSubSurf_getFaceCenterData(f);
     }
-    else if (offset < gridSideEnd) {
+    if (offset < gridSideEnd) {
       offset -= 1;
       grid = offset / gridSideVerts;
       x = offset % gridSideVerts + 1;
-      vd = ccgSubSurf_getFaceGridEdgeData(ss, f, grid, x);
-      copy_v3_v3(mv->co, CCG_elem_co(&key, vd));
-      normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd));
+      return ccgSubSurf_getFaceGridEdgeData(ss, f, grid, x);
     }
-    else if (offset < gridInternalEnd) {
+    if (offset < gridInternalEnd) {
       offset -= gridSideEnd;
       grid = offset / gridInternalVerts;
       offset %= gridInternalVerts;
       y = offset / gridSideVerts + 1;
       x = offset % gridSideVerts + 1;
-      vd = ccgSubSurf_getFaceGridData(ss, f, grid, x, y);
-      copy_v3_v3(mv->co, CCG_elem_co(&key, vd));
-      normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd));
+      return ccgSubSurf_getFaceGridData(ss, f, grid, x, y);
     }
   }
-  else if ((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) {
+  if ((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) {
     /* this vert comes from edge data */
     CCGEdge *e;
     int lastedge = ccgSubSurf_getNumEdges(ss) - 1;
@@ -880,9 +868,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
     e = ccgdm->edgeMap[i].edge;
 
     x = vertNum - ccgdm->edgeMap[i].startVert + 1;
-    vd = ccgSubSurf_getEdgeData(ss, e, x);
-    copy_v3_v3(mv->co, CCG_elem_co(&key, vd));
-    normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd));
+    return ccgSubSurf_getEdgeData(ss, e, x);
   }
   else {
     /* this vert comes from vert data */
@@ -890,26 +876,35 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
     i = vertNum - ccgdm->vertMap[0].startVert;
 
     v = ccgdm->vertMap[i].vert;
-    vd = ccgSubSurf_getVertData(ss, v);
-    copy_v3_v3(mv->co, CCG_elem_co(&key, vd));
-    normal_float_to_short_v3(mv->no, CCG_elem_no(&key, vd));
+    return ccgSubSurf_getVertData(ss, v);
   }
+  return NULL;
 }
 
 static void ccgDM_getFinalVertCo(DerivedMesh *dm, int vertNum, float r_co[3])
 {
-  MVert mvert;
+  CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
+  CCGSubSurf *ss = ccgdm->ss;
 
-  ccgDM_getFinalVert(dm, vertNum, &mvert);
-  copy_v3_v3(r_co, mvert.co);
+  CCGElem *vd = get_vertex_elem(ccgdm, vertNum);
+  if (vd != NULL) {
+    CCGKey key;
+    CCG_key_top_level(&key, ss);
+    copy_v3_v3(r_co, CCG_elem_co(&key, vd));
+  }
 }
 
 static void ccgDM_getFinalVertNo(DerivedMesh *dm, int vertNum, float r_no[3])
 {
-  MVert mvert;
+  CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
+  CCGSubSurf *ss = ccgdm->ss;
 
-  ccgDM_getFinalVert(dm, vertNum, &mvert);
-  normal_short_to_float_v3(r_no, mvert.no);
+  CCGElem *vd = get_vertex_elem(ccgdm, vertNum);
+  if (vd != NULL) {
+    CCGKey key;
+    CCG_key_top_level(&key, ss);
+    copy_v3_v3(r_no, CCG_elem_no(&key, vd));
+  }
 }
 
 /* Translate GridHidden into the ME_HIDE flag for MVerts. Assumes
@@ -1000,7 +995,6 @@ void subsurf_copy_grid_paint_mask(DerivedMesh *dm,
 BLI_INLINE void ccgDM_to_MVert(MVert *mv, const CCGKey *key, CCGElem *elem)
 {
   copy_v3_v3(mv->co, CCG_elem_co(key, elem));
-  normal_float_to_short_v3(mv->no, CCG_elem_no(key, elem));
   mv->flag = mv->bweight = 0;
 }
 
diff --git a/source/blender/blenloader/intern/versioning_300.c b/source/blender/blenloader/intern/versioning_300.c
index 95fa3058931..1e8a72a96f6 100644
--- a/source/blender/blenloader/intern/versioning_300.c
+++ b/source/blender/blenloader/intern/versioning_300.c
@@ -41,6 +41,7 @@
 #include "DNA_lineart_types.h"
 #include "DNA_listBase.h"
 #include "DNA_material_types.h"
+#include "DNA_mesh_types.h"
 #include "DNA_modifier_types.h"
 #include "DNA_text_types.h"
 #include "DNA_workspace_types.h"
@@ -55,6 +56,7 @@
 #include "BKE_idprop.h"
 #include "BKE_lib_id.h"
 #include "BKE_main.h"
+#include "BKE_mesh.h"
 #include "BKE_node.h"
 
 #include "RNA_access.h"
@@ -1719,5 +1721,10 @@ void blo_do_versions_300(FileData *fd, Library *UNUSED(lib), Main *bmain)
    */
   {
     /* Keep this block, even when empty. */
+    if (!DNA_struct_elem_find(fd->filesdna, "MVert", "short *", "no")) {
+      LISTBASE_FOREACH (Mesh *, mesh, &bmain->meshes) {
+        BKE_mesh_normals_tag_dirty(mesh);
+      }
+    }
   }
 }



More information about the Bf-blender-cvs mailing list