[Bf-blender-cvs] [f86331a0338] master: Geometry Nodes: deduplicate virtual array implementations

Jacques Lucke noreply at git.blender.org
Fri Nov 26 14:48:51 CET 2021


Commit: f86331a0338dc2351e914b45ccbd743b6cccbb19
Author: Jacques Lucke
Date:   Fri Nov 26 14:47:02 2021 +0100
Branches: master
https://developer.blender.org/rBf86331a0338dc2351e914b45ccbd743b6cccbb19

Geometry Nodes: deduplicate virtual array implementations

For some underlying data (e.g. spans) we had two virtual array
implementations. One for the mutable and one for the immutable
case. Now that most code does not deal with the virtual array
implementations directly anymore (since rBrBd4c868da9f97a),
we can get away with sharing one implementation for both cases.
This means that we have to do a `const_cast` in a few places, but
this is an implementation detail that does not leak into "user code"
(only when explicitly casting a `VArrayImpl` to a `VMutableArrayImpl`,
which should happen nowhere).

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

M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/blenkernel/intern/geometry_component_curve.cc
M	source/blender/blenkernel/intern/geometry_component_mesh.cc
M	source/blender/blenlib/BLI_virtual_array.hh
M	source/blender/functions/FN_generic_virtual_array.hh
M	source/blender/functions/intern/generic_virtual_array.cc

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

diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 3f2c1f13337..47ddb2494d0 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -50,7 +50,7 @@ using blender::bke::AttributeIDRef;
 using blender::bke::OutputAttribute;
 using blender::fn::GMutableSpan;
 using blender::fn::GSpan;
-using blender::fn::GVMutableArrayImpl_For_GMutableSpan;
+using blender::fn::GVArrayImpl_For_GSpan;
 
 namespace blender::bke {
 
@@ -1200,8 +1200,7 @@ blender::fn::GVArray GeometryComponent::attribute_get_for_read(const AttributeID
   return blender::fn::GVArray::ForSingle(*type, domain_size, default_value);
 }
 
-class GVMutableAttribute_For_OutputAttribute
-    : public blender::fn::GVMutableArrayImpl_For_GMutableSpan {
+class GVMutableAttribute_For_OutputAttribute : public blender::fn::GVArrayImpl_For_GSpan {
  public:
   GeometryComponent *component;
   std::string attribute_name;
@@ -1210,7 +1209,7 @@ class GVMutableAttribute_For_OutputAttribute
   GVMutableAttribute_For_OutputAttribute(GMutableSpan data,
                                          GeometryComponent &component,
                                          const AttributeIDRef &attribute_id)
-      : blender::fn::GVMutableArrayImpl_For_GMutableSpan(data), component(&component)
+      : blender::fn::GVArrayImpl_For_GSpan(data), component(&component)
   {
     if (attribute_id.is_named()) {
       this->attribute_name = attribute_id.name();
diff --git a/source/blender/blenkernel/intern/geometry_component_curve.cc b/source/blender/blenkernel/intern/geometry_component_curve.cc
index 598c61fd877..a0d7bfe135a 100644
--- a/source/blender/blenkernel/intern/geometry_component_curve.cc
+++ b/source/blender/blenkernel/intern/geometry_component_curve.cc
@@ -713,47 +713,16 @@ static bool remove_point_attribute(GeometryComponent &component,
   return layer_freed;
 }
 
-/**
- * Virtual array for any control point data accessed with spans and an offset array.
- */
-template<typename T> class VArray_For_SplinePoints : public VArrayImpl<T> {
- private:
-  const Array<Span<T>> data_;
-  Array<int> offsets_;
-
- public:
-  VArray_For_SplinePoints(Array<Span<T>> data, Array<int> offsets)
-      : VArrayImpl<T>(offsets.last()), data_(std::move(data)), offsets_(std::move(offsets))
-  {
-  }
-
-  T get(const int64_t index) const final
-  {
-    const PointIndices indices = lookup_point_indices(offsets_, index);
-    return data_[indices.spline_index][indices.point_index];
-  }
-
-  void materialize(const IndexMask mask, MutableSpan<T> r_span) const final
-  {
-    point_attribute_materialize(data_.as_span(), offsets_, mask, r_span);
-  }
-
-  void materialize_to_uninitialized(const IndexMask mask, MutableSpan<T> r_span) const final
-  {
-    point_attribute_materialize_to_uninitialized(data_.as_span(), offsets_, mask, r_span);
-  }
-};
-
 /**
  * Mutable virtual array for any control point data accessed with spans and an offset array.
  */
-template<typename T> class VMutableArray_For_SplinePoints final : public VMutableArrayImpl<T> {
+template<typename T> class VArrayImpl_For_SplinePoints final : public VMutableArrayImpl<T> {
  private:
   Array<MutableSpan<T>> data_;
   Array<int> offsets_;
 
  public:
-  VMutableArray_For_SplinePoints(Array<MutableSpan<T>> data, Array<int> offsets)
+  VArrayImpl_For_SplinePoints(Array<MutableSpan<T>> data, Array<int> offsets)
       : VMutableArrayImpl<T>(offsets.last()), data_(std::move(data)), offsets_(std::move(offsets))
   {
   }
@@ -791,16 +760,17 @@ template<typename T> class VMutableArray_For_SplinePoints final : public VMutabl
   }
 };
 
-template<typename T> VArray<T> point_data_varray(Array<Span<T>> spans, Array<int> offsets)
+template<typename T> VArray<T> point_data_varray(Array<MutableSpan<T>> spans, Array<int> offsets)
 {
-  return VArray<T>::template For<VArray_For_SplinePoints<T>>(std::move(spans), std::move(offsets));
+  return VArray<T>::template For<VArrayImpl_For_SplinePoints<T>>(std::move(spans),
+                                                                 std::move(offsets));
 }
 
 template<typename T>
-VMutableArray<T> point_data_varray(Array<MutableSpan<T>> spans, Array<int> offsets)
+VMutableArray<T> point_data_varray_mutable(Array<MutableSpan<T>> spans, Array<int> offsets)
 {
-  return VMutableArray<T>::template For<VMutableArray_For_SplinePoints<T>>(std::move(spans),
-                                                                           std::move(offsets));
+  return VMutableArray<T>::template For<VArrayImpl_For_SplinePoints<T>>(std::move(spans),
+                                                                        std::move(offsets));
 }
 
 /**
@@ -811,13 +781,13 @@ VMutableArray<T> point_data_varray(Array<MutableSpan<T>> spans, Array<int> offse
  * \note There is no need to check the handle type to avoid changing auto handles, since
  * retrieving write access to the position data will mark them for recomputation anyway.
  */
-class VMutableArray_For_SplinePosition final : public VMutableArrayImpl<float3> {
+class VArrayImpl_For_SplinePosition final : public VMutableArrayImpl<float3> {
  private:
   MutableSpan<SplinePtr> splines_;
   Array<int> offsets_;
 
  public:
-  VMutableArray_For_SplinePosition(MutableSpan<SplinePtr> splines, Array<int> offsets)
+  VArrayImpl_For_SplinePosition(MutableSpan<SplinePtr> splines, Array<int> offsets)
       : VMutableArrayImpl<float3>(offsets.last()), splines_(splines), offsets_(std::move(offsets))
   {
   }
@@ -889,104 +859,16 @@ class VMutableArray_For_SplinePosition final : public VMutableArrayImpl<float3>
   }
 };
 
-class VArray_For_BezierHandle final : public VArrayImpl<float3> {
- private:
-  Span<SplinePtr> splines_;
-  Array<int> offsets_;
-  bool is_right_;
-
- public:
-  VArray_For_BezierHandle(Span<SplinePtr> splines, Array<int> offsets, const bool is_right)
-      : VArrayImpl<float3>(offsets.last()),
-        splines_(std::move(splines)),
-        offsets_(std::move(offsets)),
-        is_right_(is_right)
-  {
-  }
-
-  static float3 get_internal(const int64_t index,
-                             Span<SplinePtr> splines,
-                             Span<int> offsets,
-                             const bool is_right)
-  {
-    const PointIndices indices = lookup_point_indices(offsets, index);
-    const Spline &spline = *splines[indices.spline_index];
-    if (spline.type() == Spline::Type::Bezier) {
-      const BezierSpline &bezier_spline = static_cast<const BezierSpline &>(spline);
-      return is_right ? bezier_spline.handle_positions_right()[indices.point_index] :
-                        bezier_spline.handle_positions_left()[indices.point_index];
-    }
-    return float3(0);
-  }
-
-  float3 get(const int64_t index) const final
-  {
-    return get_internal(index, splines_, offsets_, is_right_);
-  }
-
-  /**
-   * Utility so we can pass handle positions to the materialize functions above.
-   *
-   * \note This relies on the ability of the materialize implementations to
-   * handle empty spans, since only Bezier splines have handles.
-   */
-  static Array<Span<float3>> get_handle_spans(Span<SplinePtr> splines, const bool is_right)
-  {
-    Array<Span<float3>> spans(splines.size());
-    for (const int i : spans.index_range()) {
-      if (splines[i]->type() == Spline::Type::Bezier) {
-        BezierSpline &bezier_spline = static_cast<BezierSpline &>(*splines[i]);
-        spans[i] = is_right ? bezier_spline.handle_positions_right() :
-                              bezier_spline.handle_positions_left();
-      }
-      else {
-        spans[i] = {};
-      }
-    }
-    return spans;
-  }
-
-  static void materialize_internal(const IndexMask mask,
-                                   Span<SplinePtr> splines,
-                                   Span<int> offsets,
-                                   const bool is_right,
-                                   MutableSpan<float3> r_span)
-  {
-    Array<Span<float3>> spans = get_handle_spans(splines, is_right);
-    point_attribute_materialize(spans.as_span(), offsets, mask, r_span);
-  }
-
-  static void materialize_to_uninitialized_internal(const IndexMask mask,
-                                                    Span<SplinePtr> splines,
-                                                    Span<int> offsets,
-                                                    const bool is_right,
-                                                    MutableSpan<float3> r_span)
-  {
-    Array<Span<float3>> spans = get_handle_spans(splines, is_right);
-    point_attribute_materialize_to_uninitialized(spans.as_span(), offsets, mask, r_span);
-  }
-
-  void materialize(const IndexMask mask, MutableSpan<float3> r_span) const final
-  {
-    materialize_internal(mask, splines_, offsets_, is_right_, r_span);
-  }
-
-  void materialize_to_uninitialized(const IndexMask mask, MutableSpan<float3> r_span) const final
-  {
-    materialize_to_uninitialized_internal(mask, splines_, offsets_, is_right_, r_span);
-  }
-};
-
-class VMutableArray_For_BezierHandles final : public VMutableArrayImpl<float3> {
+class VArrayImpl_For_BezierHandles final : public VMutableArrayImpl<float3> {
  private:
   MutableSpan<SplinePtr> splines_;
   Array<int> offsets_;
   bool is_right_;
 
  public:
-  VMutableArray_For_BezierHandles(MutableSpan<SplinePtr> splines,
-                                  Array<int> offsets,
-                                  const bool is_right)
+  VArrayImpl_For_BezierHandles(MutableSpan<SplinePtr> splines,
+                               Array<int> offsets,
+                               const bool is_right)
       : VMutableArrayImpl<float3>(offsets.last()),
         splines_(splines),
         offsets_(std::move(offsets)),
@@ -996,7 +878,14 @@ class VMutableArray_For_BezierHandles final : public VMutableArrayImpl<float3> {
 
   float3 get(const int64_t index) const final
   {
-    return VArray_For_BezierHandle::get_internal(index, splines_, offsets_, is_right_);
+    const PointIndices indices = lookup_point_indices(offsets_, index);
+    const Spline &spline = *splines_[indices.spline_index];
+    if (spline

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list