[Bf-blender-cvs] [80bd960d915] refactor-mesh-corner-normals-lazy: Merge branch 'master' into refactor-mesh-corner-normals-lazy

Hans Goudey noreply at git.blender.org
Tue Nov 29 19:35:42 CET 2022


Commit: 80bd960d9155e28cdd4df05697ccb57c8c095429
Author: Hans Goudey
Date:   Tue Nov 29 12:35:35 2022 -0600
Branches: refactor-mesh-corner-normals-lazy
https://developer.blender.org/rB80bd960d9155e28cdd4df05697ccb57c8c095429

Merge branch 'master' into refactor-mesh-corner-normals-lazy

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



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

diff --cc source/blender/blenkernel/BKE_mesh.h
index 30e39749df4,a3f74f0fb7c..3f2d9f14436
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@@ -450,8 -438,15 +442,17 @@@ bool BKE_mesh_vertex_normals_are_dirty(
   */
  bool BKE_mesh_poly_normals_are_dirty(const struct Mesh *mesh);
  
 +bool BKE_mesh_corner_normals_are_dirty(const struct Mesh *mesh);
 +
+ void BKE_mesh_calc_poly_normal(const struct MPoly *mpoly,
+                                const struct MLoop *loopstart,
+                                const struct MVert *mvarray,
+                                float r_no[3]);
+ void BKE_mesh_calc_poly_normal_coords(const struct MPoly *mpoly,
+                                       const struct MLoop *loopstart,
+                                       const float (*vertex_coords)[3],
+                                       float r_no[3]);
+ 
  /**
   * Calculate face normals directly into a result array.
   *
diff --cc source/blender/blenkernel/BKE_mesh_types.h
index 65d1e0fcb13,93b65d1561f..2ed26556151
--- a/source/blender/blenkernel/BKE_mesh_types.h
+++ b/source/blender/blenkernel/BKE_mesh_types.h
@@@ -139,13 -155,17 +155,19 @@@ struct MeshRuntime 
     * #CustomData because they can be calculated on a `const` mesh, and adding custom data layers on
     * a `const` mesh is not thread-safe.
     */
-   bool vert_normals_dirty = false;
-   bool poly_normals_dirty = false;
+   bool vert_normals_dirty = true;
+   bool poly_normals_dirty = true;
 +  bool corner_normals_dirty = false;
    float (*vert_normals)[3] = nullptr;
    float (*poly_normals)[3] = nullptr;
 +  float (*corner_normals)[3] = nullptr;
  
+   /**
+    * A cache of data about the loose edges. Can be shared with other data-blocks with unchanged
+    * topology. Accessed with #Mesh::loose_edges().
+    */
+   SharedCache<LooseEdgeCache> loose_edges_cache;
+ 
    /**
     * A #BLI_bitmap containing tags for the center vertices of subdivided polygons, set by the
     * subdivision surface modifier and used by drawing code instead of polygon center face dots.
diff --cc source/blender/blenkernel/intern/mesh.cc
index 5b5d504a743,deaa9aec3f7..ccf3b688424
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@@ -1815,13 -1816,15 +1802,11 @@@ void BKE_mesh_calc_normals_split_ex(Mes
                                polys.size(),
                                use_split_normals,
                                split_angle,
+                               nullptr,
                                r_lnors_spacearr,
-                               clnors,
-                               nullptr);
- 
-   BKE_mesh_assert_normals_dirty_or_calculated(mesh);
+                               clnors);
  }
  
 -void BKE_mesh_calc_normals_split(Mesh *mesh)
 -{
 -  BKE_mesh_calc_normals_split_ex(mesh, nullptr, ensure_corner_normal_layer(*mesh));
 -}
  
  /* Split faces helper functions. */
  
diff --cc source/blender/blenkernel/intern/mesh_normals.cc
index 4f4cf4a5d99,e7788d82769..cb1ba9efafd
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@@ -97,6 -99,6 +99,7 @@@ void BKE_mesh_normals_tag_dirty(Mesh *m
  {
    mesh->runtime->vert_normals_dirty = true;
    mesh->runtime->poly_normals_dirty = true;
++  mesh->runtime->corner_normals_dirty = true;
  }
  
  float (*BKE_mesh_vertex_normals_for_write(Mesh *mesh))[3]
@@@ -144,15 -134,9 +147,21 @@@ void BKE_mesh_vertex_normals_clear_dirt
  void BKE_mesh_poly_normals_clear_dirty(Mesh *mesh)
  {
    mesh->runtime->poly_normals_dirty = false;
-   BKE_mesh_assert_normals_dirty_or_calculated(mesh);
+   BLI_assert(mesh->runtime->poly_normals || mesh->totpoly == 0);
+ }
+ 
++void BKE_mesh_corner_normals_clear_dirty(Mesh *mesh)
++{
++  mesh->runtime->corner_normals_dirty = false;
++  BLI_assert(mesh->runtime->corner_normals || mesh->totloop == 0);
 +}
 +
 +void BKE_mesh_corner_normals_clear_dirty(Mesh *mesh)
 +{
 +  mesh->runtime->corner_normals_dirty = false;
-   BKE_mesh_assert_normals_dirty_or_calculated(mesh);
++  BLI_assert(mesh->runtime->corner_normals || mesh->totloop == 0);
 +}
 +
  bool BKE_mesh_vertex_normals_are_dirty(const Mesh *mesh)
  {
    return mesh->runtime->vert_normals_dirty;
@@@ -461,60 -497,6 +522,61 @@@ const float (*BKE_mesh_poly_normals_ens
    return poly_normals;
  }
  
 +const float (*BKE_mesh_corner_normals_ensure(const Mesh *mesh))[3]
 +{
 +  if (!BKE_mesh_corner_normals_are_dirty(mesh)) {
 +    BLI_assert(mesh->runtime->corner_normals != nullptr || mesh->totcorner == 0);
 +    return mesh->runtime->corner_normals;
 +  }
 +
 +  if (mesh->totpoly == 0) {
 +    return nullptr;
 +  }
 +
 +  const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(mesh);
 +  const float(*poly_normals)[3] = BKE_mesh_poly_normals_ensure(mesh);
 +
 +  std::lock_guard lock{mesh->runtime->normals_mutex};
 +  if (!BKE_mesh_corner_normals_are_dirty(mesh)) {
 +    BLI_assert(mesh->runtime->corner_normals != nullptr);
 +    return mesh->runtime->corner_normals;
 +  }
 +
 +  float(*corner_normals)[3];
 +
 +  /* Isolate task because a mutex is locked and computing normals is multi-threaded. */
 +  blender::threading::isolate_task([&]() {
 +    Mesh &mesh_mutable = *const_cast<Mesh *>(mesh);
 +    const Span<MVert> verts = mesh_mutable.verts();
 +    const Span<MEdge> edges = mesh_mutable.edges();
 +    const Span<MPoly> polys = mesh_mutable.polys();
 +    const Span<MLoop> loops = mesh_mutable.loops();
 +
 +    corner_normals = BKE_mesh_corner_normals_for_write(&mesh_mutable);
-     const short(*custom_nors_dst)[2] = CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL);
++    const short(*custom_nors_dst)[2] = (const short(*)[2])CustomData_get_layer(
++        &mesh->ldata, CD_CUSTOMLOOPNORMAL);
 +
 +    BKE_mesh_normals_loop_split(verts.data(),
 +                                vert_normals,
 +                                verts.size(),
 +                                edges.data(),
 +                                edges.size(),
 +                                loops.data(),
 +                                corner_normals,
 +                                loops.size(),
 +                                polys.data(),
 +                                poly_normals,
 +                                polys.size(),
 +                                use_split_normals,
 +                                split_angle,
 +                                r_lnors_spacearr,
 +                                clnors,
 +                                nullptr);
 +
 +    BKE_mesh_corner_normals_clear_dirty(&mesh_mutable);
 +  });
 +}
 +
  void BKE_mesh_ensure_normals_for_display(Mesh *mesh)
  {
    switch (mesh->runtime->wrapper_type) {
diff --cc source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
index 32357edad28,5c3216b085b..ee7c06b6a07
--- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
@@@ -176,11 -176,11 +176,6 @@@ int OBJMesh::ith_smooth_group(const in
    return poly_smooth_groups_[poly_index];
  }
  
- void OBJMesh::ensure_mesh_edges() const
 -void OBJMesh::ensure_mesh_normals() const
--{
-   BKE_mesh_calc_edges_loose(export_mesh_eval_);
 -  BKE_mesh_calc_normals_split(export_mesh_eval_);
--}
--
  void OBJMesh::calc_smooth_groups(const bool use_bitflags)
  {
    const Span<MEdge> edges = export_mesh_eval_->edges();
diff --cc source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
index eca711e7373,e1c0e67c174..b7cc95f5c14
--- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
@@@ -131,8 -131,8 +131,6 @@@ class OBJMesh : NonCopyable 
     */
    const Material *get_object_material(int16_t mat_nr) const;
  
-   void ensure_mesh_edges() const;
 -  void ensure_mesh_normals() const;
--
    /**
     * Calculate smooth groups of a smooth-shaded object.
     * \return A polygon aligned array of smooth group numbers.



More information about the Bf-blender-cvs mailing list