From 4e78b89e487e9b9707d583c6b2578ad122c59d5e Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sat, 11 Sep 2021 13:05:20 +0200 Subject: 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 --- source/blender/functions/FN_field.hh | 59 +++++++++++++----- .../blender/functions/FN_multi_function_builder.hh | 7 ++- source/blender/functions/intern/field.cc | 72 ++++++++++++++++++++++ .../functions/intern/multi_function_builder.cc | 21 ++++++- .../functions/tests/FN_multi_function_test.cc | 2 +- 5 files changed, 141 insertions(+), 20 deletions(-) (limited to 'source/blender/functions') 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 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 inputs_; public: - FieldOperation(std::unique_ptr function, Vector inputs = {}) - : FieldNode(false), owned_function_(std::move(function)), inputs_(std::move(inputs)) - { - function_ = owned_function_.get(); - } - - FieldOperation(const MultiFunction &function, Vector inputs = {}) - : FieldNode(false), function_(&function), inputs_(std::move(inputs)) - { - } + FieldOperation(std::unique_ptr function, Vector inputs = {}); + FieldOperation(const MultiFunction &function, Vector inputs = {}); Span inputs() const { @@ -247,6 +268,8 @@ class FieldOperation : public FieldNode { BLI_assert_unreachable(); return CPPType::get(); } + + void foreach_field_input(FunctionRef 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 { IndexMask mask, ResourceScope &scope) const = 0; + virtual std::string socket_inspection_name() const + { + return debug_name_; + } + blender::StringRef debug_name() const { return debug_name_; @@ -289,6 +314,8 @@ class FieldInput : public FieldNode { UNUSED_VARS_NDEBUG(output_index); return *type_; } + + void foreach_field_input(FunctionRef foreach_fn) const override; }; /** @@ -453,4 +480,6 @@ template Field make_constant_field(T value) return Field{GField{std::move(operation), 0}}; } +GField make_field_constant_if_possible(GField field); + } // namespace blender::fn diff --git a/source/blender/functions/FN_multi_function_builder.hh b/source/blender/functions/FN_multi_function_builder.hh index d13615ced07..0ce05cbca30 100644 --- a/source/blender/functions/FN_multi_function_builder.hh +++ b/source/blender/functions/FN_multi_function_builder.hh @@ -326,18 +326,21 @@ template class CustomMF_Convert : public MultiFuncti /** * A multi-function that outputs the same value every time. The value is not owned by an instance - * of this function. The caller is responsible for destructing and freeing the value. + * of this function. If #make_value_copy is false, the caller is responsible for destructing and + * freeing the value. */ class CustomMF_GenericConstant : public MultiFunction { private: const CPPType &type_; const void *value_; MFSignature signature_; + bool owns_value_; template friend class CustomMF_Constant; public: - CustomMF_GenericConstant(const CPPType &type, const void *value); + CustomMF_GenericConstant(const CPPType &type, const void *value, bool make_value_copy); + ~CustomMF_GenericConstant(); void call(IndexMask mask, MFParams params, MFContext context) const override; uint64_t hash() const override; bool equals(const MultiFunction &other) const override; diff --git a/source/blender/functions/intern/field.cc b/source/blender/functions/intern/field.cc index fa7dea97b7f..6133658d8e3 100644 --- a/source/blender/functions/intern/field.cc +++ b/source/blender/functions/intern/field.cc @@ -462,6 +462,29 @@ void evaluate_constant_field(const GField &field, void *r_value) varrays[0]->get_to_uninitialized(0, r_value); } +/** + * If the field depends on some input, the same field is returned. + * Otherwise the field is evaluated and a new field is created that just computes this constant. + * + * Making the field constant has two benefits: + * - The field-tree becomes a single node, which is more efficient when the field is evaluated many + * times. + * - Memory of the input fields may be freed. + */ +GField make_field_constant_if_possible(GField field) +{ + if (field.node().depends_on_input()) { + return field; + } + const CPPType &type = field.cpp_type(); + BUFFER_FOR_CPP_TYPE_VALUE(type, buffer); + evaluate_constant_field(field, buffer); + auto constant_fn = std::make_unique(type, buffer, true); + type.destruct(buffer); + auto operation = std::make_shared(std::move(constant_fn)); + return GField{operation, 0}; +} + const GVArray *FieldContext::get_varray_for_input(const FieldInput &field_input, IndexMask mask, ResourceScope &scope) const @@ -471,6 +494,55 @@ const GVArray *FieldContext::get_varray_for_input(const FieldInput &field_input, return field_input.get_varray_for_context(*this, mask, scope); } +/* -------------------------------------------------------------------- + * FieldOperation. + */ + +FieldOperation::FieldOperation(std::unique_ptr function, + Vector inputs) + : FieldOperation(*function, std::move(inputs)) +{ + owned_function_ = std::move(function); +} + +static bool any_field_depends_on_input(Span fields) +{ + for (const GField &field : fields) { + if (field.node().depends_on_input()) { + return true; + } + } + return false; +} + +FieldOperation::FieldOperation(const MultiFunction &function, Vector inputs) + : FieldNode(false, any_field_depends_on_input(inputs)), + function_(&function), + inputs_(std::move(inputs)) +{ +} + +void FieldOperation::foreach_field_input(FunctionRef foreach_fn) const +{ + for (const GField &field : inputs_) { + field.node().foreach_field_input(foreach_fn); + } +} + +/* -------------------------------------------------------------------- + * FieldInput. + */ + +FieldInput::FieldInput(const CPPType &type, std::string debug_name) + : FieldNode(true, true), type_(&type), debug_name_(std::move(debug_name)) +{ +} + +void FieldInput::foreach_field_input(FunctionRef foreach_fn) const +{ + foreach_fn(*this); +} + /* -------------------------------------------------------------------- * FieldEvaluator. */ diff --git a/source/blender/functions/intern/multi_function_builder.cc b/source/blender/functions/intern/multi_function_builder.cc index 180d1f17a54..f891f162820 100644 --- a/source/blender/functions/intern/multi_function_builder.cc +++ b/source/blender/functions/intern/multi_function_builder.cc @@ -20,9 +20,18 @@ namespace blender::fn { -CustomMF_GenericConstant::CustomMF_GenericConstant(const CPPType &type, const void *value) - : type_(type), value_(value) +CustomMF_GenericConstant::CustomMF_GenericConstant(const CPPType &type, + const void *value, + bool make_value_copy) + : type_(type), owns_value_(make_value_copy) { + if (make_value_copy) { + void *copied_value = MEM_mallocN_aligned(type.size(), type.alignment(), __func__); + type.copy_construct(value, copied_value); + value = copied_value; + } + value_ = value; + MFSignatureBuilder signature{"Constant " + type.name()}; std::stringstream ss; type.print_or_default(value, ss, type.name()); @@ -31,6 +40,14 @@ CustomMF_GenericConstant::CustomMF_GenericConstant(const CPPType &type, const vo this->set_signature(&signature_); } +CustomMF_GenericConstant::~CustomMF_GenericConstant() +{ + if (owns_value_) { + signature_.param_types[0].data_type().single_type().destruct((void *)value_); + MEM_freeN((void *)value_); + } +} + void CustomMF_GenericConstant::call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const diff --git a/source/blender/functions/tests/FN_multi_function_test.cc b/source/blender/functions/tests/FN_multi_function_test.cc index 91c72a51dd6..204dbfab6aa 100644 --- a/source/blender/functions/tests/FN_multi_function_test.cc +++ b/source/blender/functions/tests/FN_multi_function_test.cc @@ -263,7 +263,7 @@ TEST(multi_function, CustomMF_Constant) TEST(multi_function, CustomMF_GenericConstant) { int value = 42; - CustomMF_GenericConstant fn{CPPType::get(), (const void *)&value}; + CustomMF_GenericConstant fn{CPPType::get(), (const void *)&value, false}; EXPECT_EQ(fn.param_name(0), "42"); Array outputs(4, 0); -- cgit v1.2.3