[Bf-blender-cvs] [f431be224fa] master: Curves: Cache the number of curves of each type

Hans Goudey noreply at git.blender.org
Mon Apr 25 20:40:14 CEST 2022


Commit: f431be224fa58374386a32dba2542ee20d2a2d61
Author: Hans Goudey
Date:   Mon Apr 25 13:39:51 2022 -0500
Branches: master
https://developer.blender.org/rBf431be224fa58374386a32dba2542ee20d2a2d61

Curves: Cache the number of curves of each type

Remembering the number of curves of every type makes it fast to know
whether processing specific to a single curve type has to be done.
This information was accessed in quite a few places, so this should be
an overall reduction in overhead for the new curves type.

The cache is computed eagerly, in other words every time after changing
the curve types. In order to reduce verbosity I added helper functions
for some common ways to set the types.

Differential Revision: https://developer.blender.org/D14732

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

M	source/blender/blenkernel/BKE_curves.hh
M	source/blender/blenkernel/intern/curve_eval.cc
M	source/blender/blenkernel/intern/curve_to_mesh_convert.cc
M	source/blender/blenkernel/intern/curves.cc
M	source/blender/blenkernel/intern/curves_geometry.cc
M	source/blender/blenkernel/intern/curves_geometry_test.cc
M	source/blender/blenkernel/intern/geometry_component_curves.cc
M	source/blender/geometry/intern/mesh_to_curve_convert.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc

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

diff --git a/source/blender/blenkernel/BKE_curves.hh b/source/blender/blenkernel/BKE_curves.hh
index 6e4d4d560f7..dea9c17d159 100644
--- a/source/blender/blenkernel/BKE_curves.hh
+++ b/source/blender/blenkernel/BKE_curves.hh
@@ -59,6 +59,12 @@ struct BasisCache {
  */
 class CurvesGeometryRuntime {
  public:
+  /**
+   * The cached number of curves with each type. Unlike other caches here, this is not computed
+   * lazily, since it is needed so often and types are not adjusted much anyway.
+   */
+  std::array<int, CURVE_TYPES_NUM> type_counts;
+
   /**
    * Cache of offsets into the evaluated array for each curve, accounting for all previous
    * evaluated points, Bezier curve vector segments, different resolutions per curve, etc.
@@ -156,15 +162,23 @@ class CurvesGeometry : public ::CurvesGeometry {
 
   /** The type (#CurveType) of each curve, or potentially a single if all are the same type. */
   VArray<int8_t> curve_types() const;
-  /** Mutable access to curve types. Call #tag_topology_changed after changing any type. */
+  /** 
+   * Mutable access to curve types. Call #tag_topology_changed and #update_curve_types after
+   * changing any type. Consider using the other methods to change types below.
+   * */
   MutableSpan<int8_t> curve_types_for_write();
+  /** Set all curve types to the value and call #update_curve_types. */
+  void fill_curve_types(CurveType type);
+  /** Set the types for the curves in the selection and call #update_curve_types. */
+  void fill_curve_types(IndexMask selection, CurveType type);
+  /** Update the cached count of curves of each type, necessary after #curve_types_for_write. */
+  void update_curve_types();
 
   bool has_curve_with_type(const CurveType type) const;
-  /** Return the number of curves with each type. */
-  std::array<int, CURVE_TYPES_NUM> count_curve_types() const;
-
   /** Return true if all of the curves have the provided type. */
   bool is_single_type(CurveType type) const;
+  /** Return the number of curves with each type. */
+  const std::array<int, CURVE_TYPES_NUM> &curve_type_counts() const;
 
   Span<float3> positions() const;
   MutableSpan<float3> positions_for_write();
@@ -624,6 +638,8 @@ Curves *curves_new_nomain(int points_num, int curves_num);
  */
 Curves *curves_new_nomain_single(int points_num, CurveType type);
 
+std::array<int, CURVE_TYPES_NUM> calculate_type_counts(const VArray<int8_t> &types);
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
@@ -649,7 +665,18 @@ inline IndexRange CurvesGeometry::curves_range() const
 
 inline bool CurvesGeometry::is_single_type(const CurveType type) const
 {
-  return this->count_curve_types()[type] == this->curves_num();
+  return this->curve_type_counts()[type] == this->curves_num();
+}
+
+inline bool CurvesGeometry::has_curve_with_type(const CurveType type) const
+{
+  return this->curve_type_counts()[type] > 0;
+}
+
+inline const std::array<int, CURVE_TYPES_NUM> &CurvesGeometry::curve_type_counts() const
+{
+  BLI_assert(this->runtime->type_counts == calculate_type_counts(this->curve_types()));
+  return this->runtime->type_counts;
 }
 
 inline IndexRange CurvesGeometry::points_for_curve(const int index) const
diff --git a/source/blender/blenkernel/intern/curve_eval.cc b/source/blender/blenkernel/intern/curve_eval.cc
index d1ec9499298..3d9dd3ecf31 100644
--- a/source/blender/blenkernel/intern/curve_eval.cc
+++ b/source/blender/blenkernel/intern/curve_eval.cc
@@ -523,6 +523,8 @@ Curves *curve_eval_to_curves(const CurveEval &curve_eval)
     }
   }
 
+  geometry.update_curve_types();
+
   normal_mode.save();
   nurbs_weight.save();
   nurbs_order.save();
diff --git a/source/blender/blenkernel/intern/curve_to_mesh_convert.cc b/source/blender/blenkernel/intern/curve_to_mesh_convert.cc
index d7fd8f7a2b6..0dc46f87537 100644
--- a/source/blender/blenkernel/intern/curve_to_mesh_convert.cc
+++ b/source/blender/blenkernel/intern/curve_to_mesh_convert.cc
@@ -229,19 +229,10 @@ struct CurvesInfo {
   /* Make sure these are spans because they are potentially accessed many times. */
   VArray_Span<bool> main_cyclic;
   VArray_Span<bool> profile_cyclic;
-
-  /* TODO: Remove once these are cached on #CurvesGeometry. */
-  std::array<int, CURVE_TYPES_NUM> main_type_counts;
-  std::array<int, CURVE_TYPES_NUM> profile_type_counts;
 };
 static CurvesInfo get_curves_info(const CurvesGeometry &main, const CurvesGeometry &profile)
 {
-  return {main,
-          profile,
-          main.cyclic(),
-          profile.cyclic(),
-          main.count_curve_types(),
-          profile.count_curve_types()};
+  return {main, profile, main.cyclic(), profile.cyclic()};
 }
 
 struct ResultOffsets {
@@ -360,7 +351,7 @@ static bool should_add_attribute_to_mesh(const CurveComponent &curve_component,
 
 static GSpan evaluated_attribute_if_necessary(const GVArray &src,
                                               const CurvesGeometry &curves,
-                                              const Span<int> type_counts,
+                                              const std::array<int, CURVE_TYPES_NUM> &type_counts,
                                               Vector<std::byte> &buffer)
 {
   if (type_counts[CURVE_TYPE_POLY] == curves.curves_num() && src.is_span()) {
@@ -691,7 +682,7 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main,
     radii = evaluated_attribute_if_necessary(
                 main_component.attribute_get_for_read<float>("radius", ATTR_DOMAIN_POINT, 1.0f),
                 main,
-                curves_info.main_type_counts,
+                main.curve_type_counts(),
                 eval_buffer)
                 .typed<float>();
   }
@@ -707,7 +698,7 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main,
                         verts.slice(info.vert_range));
   });
 
-  if (curves_info.profile_type_counts[CURVE_TYPE_BEZIER] > 0) {
+  if (profile.curve_type_counts()[CURVE_TYPE_BEZIER] > 0) {
     const VArray<int8_t> curve_types = profile.curve_types();
     const VArray_Span<int8_t> handle_types_left{profile.handle_types_left()};
     const VArray_Span<int8_t> handle_types_right{profile.handle_types_right()};
@@ -752,7 +743,7 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main,
           curves_info,
           offsets,
           dst_domain,
-          evaluated_attribute_if_necessary(src, main, curves_info.main_type_counts, eval_buffer),
+          evaluated_attribute_if_necessary(src, main, main.curve_type_counts(), eval_buffer),
           dst.as_span());
     }
     else if (src_domain == ATTR_DOMAIN_CURVE) {
@@ -787,8 +778,7 @@ Mesh *curve_to_mesh_sweep(const CurvesGeometry &main,
           curves_info,
           offsets,
           dst_domain,
-          evaluated_attribute_if_necessary(
-              src, profile, curves_info.profile_type_counts, eval_buffer),
+          evaluated_attribute_if_necessary(src, profile, profile.curve_type_counts(), eval_buffer),
           dst.as_span());
     }
     else if (src_domain == ATTR_DOMAIN_CURVE) {
@@ -808,6 +798,7 @@ static CurvesGeometry get_curve_single_vert()
   CurvesGeometry curves(1, 1);
   curves.offsets_for_write().last() = 1;
   curves.positions_for_write().fill(float3(0));
+  curves.fill_curve_types(CURVE_TYPE_POLY);
 
   return curves;
 }
diff --git a/source/blender/blenkernel/intern/curves.cc b/source/blender/blenkernel/intern/curves.cc
index 5b51fb8af31..a3b40b27583 100644
--- a/source/blender/blenkernel/intern/curves.cc
+++ b/source/blender/blenkernel/intern/curves.cc
@@ -370,7 +370,7 @@ Curves *curves_new_nomain_single(const int points_num, const CurveType type)
   Curves *curves = curves_new_nomain(points_num, 1);
   CurvesGeometry &geometry = CurvesGeometry::wrap(curves->geometry);
   geometry.offsets_for_write().last() = points_num;
-  geometry.curve_types_for_write().first() = type;
+  geometry.fill_curve_types(type);
   return curves;
 }
 
diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc
index 8e97884516c..7a09b87490b 100644
--- a/source/blender/blenkernel/intern/curves_geometry.cc
+++ b/source/blender/blenkernel/intern/curves_geometry.cc
@@ -85,6 +85,9 @@ static void copy_curves_geometry(CurvesGeometry &dst, const CurvesGeometry &src)
 
   dst.tag_topology_changed();
 
+  /* Though type counts are a cache, they must be copied because they are calculated eagerly. */
+  dst.runtime->type_counts = src.runtime->type_counts;
+
   dst.update_customdata_pointers();
 }
 
@@ -237,38 +240,38 @@ MutableSpan<int8_t> CurvesGeometry::curve_types_for_write()
   return get_mutable_attribute<int8_t>(*this, ATTR_DOMAIN_CURVE, ATTR_CURVE_TYPE);
 }
 
-bool CurvesGeometry::has_curve_with_type(const CurveType type) const
+void CurvesGeometry::fill_curve_types(const CurveType type)
 {
-  const VArray<int8_t> curve_types = this->curve_types();
-  if (curve_types.is_single()) {
-    return curve_types.get_internal_single() == type;
-  }
-  if (curve_types.is_span()) {
-    return curve_types.get_internal_span().contains(type);
-  }
-  /* The curves types array should be a single value or a span. */
-  BLI_assert_unreachable();
-  return false;
+  this->curve_types_for_write().fill(type);
+  this->runtime->type_counts.fill(0);
+  this->runtime->type_counts[type] = this->curves_num();
+  this->tag_topology_changed();
 }
 
-std::array<int, CURVE_TYPES_NUM> CurvesGeometry::count_curve_types() const
+void CurvesGeometry::fill_curve_types(const IndexMask selection, const CurveType type)
 {
-  using CountsType = std::array<int, CURVE_TYPES_NUM>;
+  /* A potential performance optimization is only counting the changed indices. */
+  this->curve_types_for_write().fill_indices(selection, type);
+  this->update_curve_types();
+  this->tag_topology_changed();
+}
 
-  CountsType identity;
-  identity.fill(0);
+std::array<int, CURVE_TYPES_NUM> calculate_type_counts(const VArray<int8_t> &types)
+{
+  using CountsType = std::array<int, CURVE_TYPES_NUM>;
+  CountsType counts;
+  counts.fill(0);
 
-  const VArray<int8_t> types = this->curve_types();
   if (types.is_single()) {
-    identity[types.get_internal_single()] = this->curves_num();
-    return identity;
+    counts[types.get_inte

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list