[Bf-blender-cvs] [6e29a9459bd] virtual-array-attributes: bring back type conversion

Jacques Lucke noreply at git.blender.org
Thu Apr 15 16:13:11 CEST 2021


Commit: 6e29a9459bd9811c8c87d5234bb1b07d635a3990
Author: Jacques Lucke
Date:   Thu Apr 15 12:04:31 2021 +0200
Branches: virtual-array-attributes
https://developer.blender.org/rB6e29a9459bd9811c8c87d5234bb1b07d635a3990

bring back type conversion

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

M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/nodes/NOD_type_conversions.hh
M	source/blender/nodes/intern/type_conversions.cc

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

diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 33a680fa2db..fad6f111458 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -704,13 +704,9 @@ bool GeometryComponent::attribute_exists(const blender::StringRef attribute_name
 static std::unique_ptr<blender::fn::GVArray> try_adapt_data_type(
     std::unique_ptr<blender::fn::GVArray> varray, const blender::fn::CPPType &to_type)
 {
-  const blender::fn::CPPType &from_type = varray->type();
-  if (from_type == to_type) {
-    return varray;
-  }
-
-  /* TODO: Bring back conversion. */
-  return {};
+  const blender::nodes::DataTypeConversions &conversions =
+      blender::nodes::get_implicit_type_conversions();
+  return conversions.try_convert(std::move(varray), to_type);
 }
 
 std::unique_ptr<blender::fn::GVArray> GeometryComponent::attribute_try_get_for_read(
diff --git a/source/blender/nodes/NOD_type_conversions.hh b/source/blender/nodes/NOD_type_conversions.hh
index 34225208fe6..dc24e35d0f7 100644
--- a/source/blender/nodes/NOD_type_conversions.hh
+++ b/source/blender/nodes/NOD_type_conversions.hh
@@ -72,6 +72,9 @@ class DataTypeConversions {
                                 const CPPType &to_type,
                                 const void *from_value,
                                 void *to_value) const;
+
+  std::unique_ptr<fn::GVArray> try_convert(std::unique_ptr<fn::GVArray> varray,
+                                           const CPPType &to_type) const;
 };
 
 const DataTypeConversions &get_implicit_type_conversions();
diff --git a/source/blender/nodes/intern/type_conversions.cc b/source/blender/nodes/intern/type_conversions.cc
index 1c1b7c7feb5..f06d171a079 100644
--- a/source/blender/nodes/intern/type_conversions.cc
+++ b/source/blender/nodes/intern/type_conversions.cc
@@ -234,4 +234,50 @@ void DataTypeConversions::convert_to_uninitialized(const CPPType &from_type,
   functions->convert_single_to_uninitialized(from_value, to_value);
 }
 
+class GVArray_For_ConvertedGVArray : public GVArray {
+ private:
+  std::unique_ptr<GVArray> varray_;
+  const CPPType &from_type_;
+  ConversionFunctions conversion_functions_;
+
+ public:
+  GVArray_For_ConvertedGVArray(std::unique_ptr<GVArray> varray,
+                               const CPPType &to_type,
+                               const DataTypeConversions &conversions)
+      : GVArray(to_type, varray->size()), varray_(std::move(varray)), from_type_(varray_->type())
+  {
+    conversion_functions_ = *conversions.get_conversion_functions(from_type_, to_type);
+  }
+
+ private:
+  void get_impl(const int64_t index, void *r_value) const override
+  {
+    BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer);
+    varray_->get(index, buffer);
+    conversion_functions_.convert_single_to_initialized(buffer, r_value);
+    from_type_.destruct(buffer);
+  }
+
+  void get_to_uninitialized_impl(const int64_t index, void *r_value) const override
+  {
+    BUFFER_FOR_CPP_TYPE_VALUE(from_type_, buffer);
+    varray_->get(index, buffer);
+    conversion_functions_.convert_single_to_uninitialized(buffer, r_value);
+    from_type_.destruct(buffer);
+  }
+};
+
+std::unique_ptr<fn::GVArray> DataTypeConversions::try_convert(std::unique_ptr<fn::GVArray> varray,
+                                                              const CPPType &to_type) const
+{
+  const CPPType &from_type = varray->type();
+  if (from_type == to_type) {
+    return varray;
+  }
+  if (!this->is_convertible(from_type, to_type)) {
+    return {};
+  }
+  return std::make_unique<GVArray_For_ConvertedGVArray>(std::move(varray), to_type, *this);
+}
+
 }  // namespace blender::nodes



More information about the Bf-blender-cvs mailing list