[Bf-blender-cvs] [4e78b89e487] master: Geometry Nodes: add field support for socket inspection

Jacques Lucke noreply at git.blender.org
Sat Sep 11 13:08:19 CEST 2021


Commit: 4e78b89e487e9b9707d583c6b2578ad122c59d5e
Author: Jacques Lucke
Date:   Sat Sep 11 13:05:20 2021 +0200
Branches: master
https://developer.blender.org/rB4e78b89e487e9b9707d583c6b2578ad122c59d5e

Geometry Nodes: add field support for socket inspection

Since fields were committed to master, socket inspection did
not work correctly for all socket types anymore. Now the same
functionality as before is back. Furthermore, fields that depend
on some input will now show the inputs in the socket inspection.

I added support for evaluating constant fields more immediately.
This has the benefit that the same constant field is not evaluated
more than once. It also helps with making the field independent
of the multi-functions that it uses. We might still want to change
the ownership handling for the multi-functions of nodes a bit,
but that can be done separately.

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

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

M	source/blender/blenkernel/BKE_geometry_set.hh
M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/editors/space_node/node_draw.cc
M	source/blender/functions/FN_field.hh
M	source/blender/functions/FN_multi_function_builder.hh
M	source/blender/functions/intern/field.cc
M	source/blender/functions/intern/multi_function_builder.cc
M	source/blender/functions/tests/FN_multi_function_test.cc
M	source/blender/modifiers/intern/MOD_nodes_evaluator.cc

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

diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh
index c3d594d7dcd..3da35cb4fe1 100644
--- a/source/blender/blenkernel/BKE_geometry_set.hh
+++ b/source/blender/blenkernel/BKE_geometry_set.hh
@@ -657,10 +657,17 @@ class AttributeFieldInput : public fn::FieldInput {
   {
   }
 
+  StringRefNull attribute_name() const
+  {
+    return name_;
+  }
+
   const GVArray *get_varray_for_context(const fn::FieldContext &context,
                                         IndexMask mask,
                                         ResourceScope &scope) const override;
 
+  std::string socket_inspection_name() const override;
+
   uint64_t hash() const override;
   bool is_equal_to(const fn::FieldNode &other) const override;
 };
@@ -683,6 +690,8 @@ class AnonymousAttributeFieldInput : public fn::FieldInput {
                                         IndexMask mask,
                                         ResourceScope &scope) const override;
 
+  std::string socket_inspection_name() const override;
+
   uint64_t hash() const override;
   bool is_equal_to(const fn::FieldNode &other) const override;
 };
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 08bd5dc5981..2a5bb99a18b 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -32,6 +32,8 @@
 #include "BLI_float2.hh"
 #include "BLI_span.hh"
 
+#include "BLT_translation.h"
+
 #include "CLG_log.h"
 
 #include "NOD_type_conversions.hh"
@@ -1318,6 +1320,13 @@ const GVArray *AttributeFieldInput::get_varray_for_context(const fn::FieldContex
   return nullptr;
 }
 
+std::string AttributeFieldInput::socket_inspection_name() const
+{
+  std::stringstream ss;
+  ss << TIP_("Attribute: ") << name_;
+  return ss.str();
+}
+
 uint64_t AttributeFieldInput::hash() const
 {
   return get_default_hash_2(name_, type_);
@@ -1346,6 +1355,13 @@ const GVArray *AnonymousAttributeFieldInput::get_varray_for_context(
   return nullptr;
 }
 
+std::string AnonymousAttributeFieldInput::socket_inspection_name() const
+{
+  std::stringstream ss;
+  ss << TIP_("Anonymous Attribute: ") << debug_name_;
+  return ss.str();
+}
+
 uint64_t AnonymousAttributeFieldInput::hash() const
 {
   return get_default_hash_2(anonymous_id_.get(), type_);
diff --git a/source/blender/editors/space_node/node_draw.cc b/source/blender/editors/space_node/node_draw.cc
index 5b4e3b3b6f5..aa241071425 100644
--- a/source/blender/editors/space_node/node_draw.cc
+++ b/source/blender/editors/space_node/node_draw.cc
@@ -41,10 +41,12 @@
 #include "BLI_span.hh"
 #include "BLI_string_ref.hh"
 #include "BLI_vector.hh"
+#include "BLI_vector_set.hh"
 
 #include "BLT_translation.h"
 
 #include "BKE_context.h"
+#include "BKE_geometry_set.hh"
 #include "BKE_idtype.h"
 #include "BKE_lib_id.h"
 #include "BKE_main.h"
@@ -78,6 +80,8 @@
 
 #include "NOD_geometry_nodes_eval_log.hh"
 
+#include "FN_field_cpp_type.hh"
+
 #include "node_intern.h" /* own include */
 
 #ifdef WITH_COMPOSITOR
@@ -88,7 +92,11 @@ using blender::Map;
 using blender::Set;
 using blender::Span;
 using blender::Vector;
+using blender::VectorSet;
 using blender::fn::CPPType;
+using blender::fn::FieldCPPType;
+using blender::fn::FieldInput;
+using blender::fn::GField;
 using blender::fn::GPointer;
 namespace geo_log = blender::nodes::geometry_nodes_eval_log;
 
@@ -850,31 +858,70 @@ static void create_inspection_string_for_generic_value(const geo_log::GenericVal
   };
 
   const GPointer value = value_log.value();
-  if (value.is_type<int>()) {
-    ss << *value.get<int>() << TIP_(" (Integer)");
-  }
-  else if (value.is_type<float>()) {
-    ss << *value.get<float>() << TIP_(" (Float)");
-  }
-  else if (value.is_type<blender::float3>()) {
-    ss << *value.get<blender::float3>() << TIP_(" (Vector)");
-  }
-  else if (value.is_type<bool>()) {
-    ss << (*value.get<bool>() ? TIP_("True") : TIP_("False")) << TIP_(" (Boolean)");
-  }
-  else if (value.is_type<std::string>()) {
-    ss << *value.get<std::string>() << TIP_(" (String)");
+  const CPPType &type = *value.type();
+  if (const FieldCPPType *field_type = dynamic_cast<const FieldCPPType *>(&type)) {
+    const CPPType &base_type = field_type->field_type();
+    BUFFER_FOR_CPP_TYPE_VALUE(base_type, buffer);
+    const GField &field = field_type->get_gfield(value.get());
+    if (field.node().depends_on_input()) {
+      if (base_type.is<int>()) {
+        ss << TIP_("Integer Field");
+      }
+      else if (base_type.is<float>()) {
+        ss << TIP_("Float Field");
+      }
+      else if (base_type.is<blender::float3>()) {
+        ss << TIP_("Vector Field");
+      }
+      else if (base_type.is<bool>()) {
+        ss << TIP_("Boolean Field");
+      }
+      else if (base_type.is<std::string>()) {
+        ss << TIP_("String Field");
+      }
+      ss << TIP_(" based on:\n");
+
+      /* Use vector set to deduplicate inputs. */
+      VectorSet<std::reference_wrapper<const FieldInput>> field_inputs;
+      field.node().foreach_field_input(
+          [&](const FieldInput &field_input) { field_inputs.add(field_input); });
+      for (const FieldInput &field_input : field_inputs) {
+        ss << "\u2022 " << field_input.socket_inspection_name();
+        if (field_input != field_inputs.as_span().last().get()) {
+          ss << ".\n";
+        }
+      }
+    }
+    else {
+      blender::fn::evaluate_constant_field(field, buffer);
+      if (base_type.is<int>()) {
+        ss << *(int *)buffer << TIP_(" (Integer)");
+      }
+      else if (base_type.is<float>()) {
+        ss << *(float *)buffer << TIP_(" (Float)");
+      }
+      else if (base_type.is<blender::float3>()) {
+        ss << *(blender::float3 *)buffer << TIP_(" (Vector)");
+      }
+      else if (base_type.is<bool>()) {
+        ss << ((*(bool *)buffer) ? TIP_("True") : TIP_("False")) << TIP_(" (Boolean)");
+      }
+      else if (base_type.is<std::string>()) {
+        ss << *(std::string *)buffer << TIP_(" (String)");
+      }
+      base_type.destruct(buffer);
+    }
   }
-  else if (value.is_type<Object *>()) {
+  else if (type.is<Object *>()) {
     id_to_inspection_string((ID *)*value.get<Object *>(), ID_OB);
   }
-  else if (value.is_type<Material *>()) {
+  else if (type.is<Material *>()) {
     id_to_inspection_string((ID *)*value.get<Material *>(), ID_MA);
   }
-  else if (value.is_type<Tex *>()) {
+  else if (type.is<Tex *>()) {
     id_to_inspection_string((ID *)*value.get<Tex *>(), ID_TE);
   }
-  else if (value.is_type<Collection *>()) {
+  else if (type.is<Collection *>()) {
     id_to_inspection_string((ID *)*value.get<Collection *>(), ID_GR);
   }
 }
diff --git a/source/blender/functions/FN_field.hh b/source/blender/functions/FN_field.hh
index 79a6faf499b..976d260d91b 100644
--- a/source/blender/functions/FN_field.hh
+++ b/source/blender/functions/FN_field.hh
@@ -46,6 +46,7 @@
  * they share common sub-fields and a common context.
  */
 
+#include "BLI_function_ref.hh"
 #include "BLI_string_ref.hh"
 #include "BLI_vector.hh"
 
@@ -57,15 +58,27 @@
 
 namespace blender::fn {
 
+class FieldInput;
+
 /**
  * A node in a field-tree. It has at least one output that can be referenced by fields.
  */
 class FieldNode {
  private:
   bool is_input_;
+  /**
+   * True when this node is a #FieldInput or (potentially indirectly) depends on one. This could
+   * always be derived again later by traversing the field-tree, but keeping track of it while the
+   * field is built is cheaper.
+   *
+   * If this is false, the field is constant. Note that even when this is true, the field may be
+   * constant when all inputs are constant.
+   */
+  bool depends_on_input_;
 
  public:
-  FieldNode(bool is_input) : is_input_(is_input)
+  FieldNode(bool is_input, bool depends_on_input)
+      : is_input_(is_input), depends_on_input_(depends_on_input)
   {
   }
 
@@ -83,6 +96,17 @@ class FieldNode {
     return !is_input_;
   }
 
+  bool depends_on_input() const
+  {
+    return depends_on_input_;
+  }
+
+  /**
+   * Invoke callback for every field input. It might be called multiple times for the same input.
+   * The caller is responsible for deduplication if required.
+   */
+  virtual void foreach_field_input(FunctionRef<void(const FieldInput &)> foreach_fn) const = 0;
+
   virtual uint64_t hash() const
   {
     return get_default_hash(this);
@@ -93,6 +117,11 @@ class FieldNode {
     return a.is_equal_to(b);
   }
 
+  friend bool operator!=(const FieldNode &a, const FieldNode &b)
+  {
+    return !(a == b);
+  }
+
   virtual bool is_equal_to(const FieldNode &other) const
   {
     return this == &other;
@@ -211,16 +240,8 @@ class FieldOperation : public FieldNode {
   blender::Vector<GField> inputs_;
 
  public:
-  FieldOperation(std::unique_ptr<const MultiFunction> function, Vector<GField> inputs = {})
-      : FieldNode(false), owned_function_(std::move(function)), inputs_(std::move(inputs))
-  {
-    function_ = owned_function_.get();
-  }
-
-  FieldOperation(const MultiFunction &function, Vector<GField> inputs = {})
-      : FieldNode(false), function_(&function), inputs_(std::move(inputs))
-  {
-  }
+  FieldOperation(std::unique_ptr<const MultiFunction> function, Vector<GField> inputs = {});
+  FieldOperation(const MultiFunction &function, Vector<GField> inputs = {});
 
   Span<GField> inputs() const
   {
@@ -247,6 +268,8 @@ class FieldOperation : public FieldNode {
     BLI_assert_unreachable();
     return CPPType::get<float>();
   }
+
+  void foreach_field_input(FunctionRef<void(const FieldInput &)> foreach_fn) const override;
 };
 
 class FieldContext;
@@ -260,10 +283,7 @@ class FieldInput : public FieldNode {
   std::string debug_name_;
 
  public:
-  FieldInput(const CPPType &type, std::string debug_name = "")
-      : FieldNode(true), type_(&type), debug_name_(std::move(debug_name))
-  {
-  }
+  FieldInput(const CPPType &type, std::string debug_name = "");
 
   /**
    * Get the value of this specific input based on the given context. The returned virtual array,
@@ -273,6 +293,11 @@ class FieldInput : public FieldNode {
                                                 IndexM

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list