[Bf-blender-cvs] [12014ccfbc4] temp-geometry-nodes-fields-prototype: progress

Jacques Lucke noreply at git.blender.org
Wed Jul 28 14:14:19 CEST 2021


Commit: 12014ccfbc4233386c267921cf0fee9cb5e21279
Author: Jacques Lucke
Date:   Sat Jul 10 12:14:48 2021 +0200
Branches: temp-geometry-nodes-fields-prototype
https://developer.blender.org/rB12014ccfbc4233386c267921cf0fee9cb5e21279

progress

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

M	source/blender/blenkernel/BKE_field.hh
M	source/blender/blenlib/BLI_hash.hh
M	source/blender/blenlib/BLI_user_counter.hh
M	source/blender/modifiers/intern/MOD_nodes_evaluator.cc
M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
M	source/blender/nodes/intern/node_geometry_exec.cc

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

diff --git a/source/blender/blenkernel/BKE_field.hh b/source/blender/blenkernel/BKE_field.hh
index c36af5455e4..867a3a1f541 100644
--- a/source/blender/blenkernel/BKE_field.hh
+++ b/source/blender/blenkernel/BKE_field.hh
@@ -20,8 +20,11 @@
  * \ingroup bke
  */
 
+#include <atomic>
+
 #include "BLI_function_ref.hh"
 #include "BLI_map.hh"
+#include "BLI_user_counter.hh"
 #include "BLI_vector.hh"
 #include "BLI_virtual_array.hh"
 
@@ -137,7 +140,7 @@ class FieldInputs {
 
 template<typename T> class FieldOutput {
  private:
-  VArray<T> *varray_ = nullptr;
+  const VArray<T> *varray_ = nullptr;
   VArrayPtr<T> varray_owned_;
 
  public:
@@ -181,6 +184,9 @@ class GFieldOutput {
 };
 
 class GField {
+ private:
+  mutable std::atomic<int> users_ = 1;
+
  public:
   virtual ~GField() = default;
 
@@ -199,6 +205,19 @@ class GField {
   virtual const CPPType &output_type() const = 0;
 
   virtual GFieldOutput evaluate_generic(IndexMask mask, const FieldInputs &inputs) const = 0;
+
+  void user_add() const
+  {
+    users_.fetch_add(1);
+  }
+
+  void user_remove() const
+  {
+    const int new_users = users_.fetch_sub(1) - 1;
+    if (new_users == 0) {
+      delete this;
+    }
+  }
 };
 
 template<typename T> class Field : public GField {
@@ -209,9 +228,9 @@ template<typename T> class Field : public GField {
   {
     FieldOutput<T> output = this->evaluate(mask, inputs);
     if (output.varray_owned()) {
-      return std::make_unique<fn::GVArray_For_OwnedVArray>(std::move(output.varray_owned()));
+      return {std::make_unique<fn::GVArray_For_OwnedVArray<T>>(std::move(output.varray_owned()))};
     }
-    return std::make_unique<fn::GVArray_For_VArray>(output.varray_ref());
+    return {std::make_unique<fn::GVArray_For_VArray<T>>(output.varray_ref())};
   }
 
   const CPPType &output_type() const override
@@ -229,20 +248,20 @@ template<typename T> class ConstantField : public Field<T> {
   {
   }
 
-  FieldOutput<T> evaluate(IndexMask mask, const FieldInputs &inputs) const
+  FieldOutput<T> evaluate(IndexMask mask, const FieldInputs &UNUSED(inputs)) const
   {
-    return std::make_unique<VArray_For_Single<T>>(value_, mask.min_array_size());
+    return {std::make_unique<VArray_For_Single<T>>(value_, mask.min_array_size())};
   }
 };
 
-template<typename T, typename KeyT> class VArrayField : public Field<T> {
+template<typename T, typename KeyT> class VArrayInputField : public Field<T> {
  private:
   T default_value_;
   KeyT key_;
 
  public:
   template<typename... Args>
-  VArrayField(T default_value, Args &&... args)
+  VArrayInputField(T default_value, Args &&... args)
       : default_value_(std::move(default_value)), key_(std::forward<Args>(args)...)
   {
   }
@@ -252,7 +271,7 @@ template<typename T, typename KeyT> class VArrayField : public Field<T> {
     callback(key_);
   }
 
-  FieldOutput<T> evaluate(IndexMask mask, const FieldInputs &inputs) const
+  FieldOutput<T> evaluate(IndexMask mask, const FieldInputs &inputs) const override
   {
     const VArrayFieldInputValue<T> *input = inputs.get<VArrayFieldInputValue<T>>(key_);
     if (input == nullptr) {
@@ -262,6 +281,24 @@ template<typename T, typename KeyT> class VArrayField : public Field<T> {
   }
 };
 
+template<typename T, typename VArrayT> class VArrayField : public Field<T> {
+ private:
+  T default_value_;
+  VArrayT varray_;
+
+ public:
+  template<typename... Args>
+  VArrayField(T default_value, Args &&... args)
+      : default_value_(std::move(default_value)), varray_(std::forward<Args>(args)...)
+  {
+  }
+
+  FieldOutput<T> evaluate(IndexMask UNUSED(mask), const FieldInputs &UNUSED(inputs)) const override
+  {
+    return {varray_};
+  }
+};
+
 class MultiFunctionField : public GField {
  private:
   Vector<std::shared_ptr<GField>> input_fields_;
@@ -288,8 +325,6 @@ class MultiFunctionField : public GField {
 
     ResourceScope &scope = params.resource_scope();
 
-    const CPPType &output_type = this->output_type();
-
     Vector<GMutableSpan> outputs;
     int output_span_index = -1;
 
@@ -337,4 +372,6 @@ class MultiFunctionField : public GField {
   }
 };
 
+template<typename T> using FieldPtr = UserCounter<Field<T>>;
+
 }  // namespace blender::bke
diff --git a/source/blender/blenlib/BLI_hash.hh b/source/blender/blenlib/BLI_hash.hh
index c19b94a4ebf..11ff7d040aa 100644
--- a/source/blender/blenlib/BLI_hash.hh
+++ b/source/blender/blenlib/BLI_hash.hh
@@ -250,6 +250,13 @@ template<typename T> struct DefaultHash<std::unique_ptr<T>> {
   }
 };
 
+template<typename T> struct DefaultHash<std::shared_ptr<T>> {
+  uint64_t operator()(const std::shared_ptr<T> &value) const
+  {
+    return get_default_hash(value.get());
+  }
+};
+
 template<typename T> struct DefaultHash<std::reference_wrapper<T>> {
   uint64_t operator()(const std::reference_wrapper<T> &value) const
   {
diff --git a/source/blender/blenlib/BLI_user_counter.hh b/source/blender/blenlib/BLI_user_counter.hh
index 3e6d5af4c3f..8cebadeac4c 100644
--- a/source/blender/blenlib/BLI_user_counter.hh
+++ b/source/blender/blenlib/BLI_user_counter.hh
@@ -84,12 +84,24 @@ template<typename T> class UserCounter {
     return data_;
   }
 
+  const T *operator->() const
+  {
+    BLI_assert(data_ != nullptr);
+    return data_;
+  }
+
   T &operator*()
   {
     BLI_assert(data_ != nullptr);
     return *data_;
   }
 
+  const T &operator*() const
+  {
+    BLI_assert(data_ != nullptr);
+    return *data_;
+  }
+
   operator bool() const
   {
     return data_ != nullptr;
diff --git a/source/blender/modifiers/intern/MOD_nodes_evaluator.cc b/source/blender/modifiers/intern/MOD_nodes_evaluator.cc
index 18cc1ce6c86..036a620a039 100644
--- a/source/blender/modifiers/intern/MOD_nodes_evaluator.cc
+++ b/source/blender/modifiers/intern/MOD_nodes_evaluator.cc
@@ -21,6 +21,7 @@
 
 #include "DEG_depsgraph_query.h"
 
+#include "FN_cpp_type_make.hh"
 #include "FN_generic_value_map.hh"
 #include "FN_multi_function.hh"
 
@@ -30,8 +31,11 @@
 #include "BLI_task.hh"
 #include "BLI_vector_set.hh"
 
+MAKE_CPP_TYPE(FloatFieldPtr, blender::bke::FieldPtr<float>, CPPTypeFlags::BasicType);
+
 namespace blender::modifiers::geometry_nodes {
 
+using bke::FieldPtr;
 using fn::CPPType;
 using fn::GValueMap;
 using nodes::GeoNodeExecParams;
@@ -302,6 +306,9 @@ static const CPPType *get_socket_cpp_type(const SocketRef &socket)
   if (!type->has_special_member_functions()) {
     return nullptr;
   }
+  if (type->is<float>()) {
+    return &CPPType::get<FieldPtr<float>>();
+  }
   return type;
 }
 
@@ -310,6 +317,17 @@ static const CPPType *get_socket_cpp_type(const DSocket socket)
   return get_socket_cpp_type(*socket.socket_ref());
 }
 
+static void get_socket_value(const SocketRef &socket, void *r_value)
+{
+  if (socket.typeinfo()->type == SOCK_FLOAT) {
+    float value;
+    socket.typeinfo()->get_cpp_value(*socket.bsocket(), &value);
+    new (r_value) FieldPtr<float>(new bke::ConstantField<float>(value));
+    return;
+  }
+  blender::nodes::socket_cpp_value_get(*socket.bsocket(), r_value);
+}
+
 static bool node_supports_laziness(const DNode node)
 {
   return node->typeinfo()->geometry_node_execute_supports_laziness;
@@ -1363,10 +1381,9 @@ class GeometryNodesEvaluator {
   {
     LinearAllocator<> &allocator = local_allocators_.local();
 
-    bNodeSocket *bsocket = socket->bsocket();
     const CPPType &type = *get_socket_cpp_type(socket);
     void *buffer = allocator.allocate(type.size(), type.alignment());
-    blender::nodes::socket_cpp_value_get(*bsocket, buffer);
+    get_socket_value(*socket.socket_ref(), buffer);
 
     if (type == required_type) {
       return {type, buffer};
diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh
index d6a23051c0b..98df31cb739 100644
--- a/source/blender/nodes/NOD_geometry_exec.hh
+++ b/source/blender/nodes/NOD_geometry_exec.hh
@@ -19,6 +19,7 @@
 #include "FN_generic_value_map.hh"
 
 #include "BKE_attribute_access.hh"
+#include "BKE_field.hh"
 #include "BKE_geometry_set.hh"
 #include "BKE_geometry_set_instances.hh"
 
@@ -32,6 +33,7 @@ struct ModifierData;
 
 namespace blender::nodes {
 
+using bke::FieldPtr;
 using bke::geometry_set_realize_instances;
 using bke::OutputAttribute;
 using bke::OutputAttribute_Typed;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc b/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
index 389abe3b2aa..ba5d1136348 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
@@ -98,10 +98,18 @@ static void fill_attribute(GeometryComponent &component, const GeoNodeExecParams
     return;
   }
 
+  const int domain_size = attribute->size();
+
   switch (data_type) {
     case CD_PROP_FLOAT: {
-      const float value = params.get_input<float>("Value_001");
-      attribute->fill(&value);
+      const FieldPtr<float> &value_field = params.get_input<FieldPtr<float>>("Value_001");
+      bke::FieldInputs field_inputs = value_field->prepare_inputs();
+      bke::FieldOutput<float> field_output = value_field->evaluate(IndexMask(domain_size),
+                                                                   field_inputs);
+      for (const int i : IndexRange(domain_size)) {
+        const float value = field_output.varray_ref()[i];
+        attribute->set_by_copy(i, &value);
+      }
       break;
     }
     case CD_PROP_FLOAT3: {
diff --git a/source/blender/nodes/intern/node_geometry_exec.cc b/source/blender/nodes/intern/node_geometry_exec.cc
index 5755a14f14d..cf156d82752 100644
--- a/source/blender/nodes/intern/node_geometry_exec.cc
+++ b/source/blender/nodes/intern/node_geometry_exec.cc
@@ -215,12 +215,13 @@ void GeoNodeExecParams::check_input_access(StringRef identifier,
     BLI_assert_unreachable();
   }
   else if (requested_type != nullptr) {
-    const CPPType &expected_type = *socket_cpp_type_get(*found_socket->typeinfo);
-    if (*requested_type != expected_type) {
-      std::cout << "The requested type '" << requested_type->name() << "' is incorrect. Expected '"
-                << expected_type.name() << "'.\n";
-      BLI_assert_unreachable();
-    }
+    // const CPPType &expected_type = *socket

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list