[Bf-blender-cvs] [909dc85f2f5] functions-experimental-refactor: node functions

Jacques Lucke noreply at git.blender.org
Tue Oct 15 15:56:33 CEST 2019


Commit: 909dc85f2f52e41e2897ae65e6c923ba082212fc
Author: Jacques Lucke
Date:   Tue Oct 8 12:12:50 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rB909dc85f2f52e41e2897ae65e6c923ba082212fc

node functions

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

M	source/blender/blenkernel/BKE_generic_array_ref.h
A	source/blender/blenkernel/BKE_node_functions.h
M	source/blender/blenkernel/BKE_tuple.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/cpp_function.cc
M	source/blender/blenkernel/intern/generic_array_ref.cc
A	source/blender/blenkernel/intern/node_functions.cc
M	source/blender/blenlib/BLI_lazy_init_cxx.h

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

diff --git a/source/blender/blenkernel/BKE_generic_array_ref.h b/source/blender/blenkernel/BKE_generic_array_ref.h
index 16f9abde3f1..e24614d16a1 100644
--- a/source/blender/blenkernel/BKE_generic_array_ref.h
+++ b/source/blender/blenkernel/BKE_generic_array_ref.h
@@ -69,7 +69,7 @@ class GenericMutableArrayRef {
   template<typename T> MutableArrayRef<T> get_ref()
   {
     BLI_assert(GET_TYPE<T>().is_same_or_generalization(m_type));
-    return ArrayRef<T>((const T *)m_buffer, m_size);
+    return MutableArrayRef<T>((T *)m_buffer, m_size);
   }
 };
 
@@ -101,8 +101,18 @@ class MutableArrayRefCPPType final : public CPPType {
   }
 };
 
-ArrayRefCPPType &GET_TYPE_generic_array_ref(CPPType &base);
-MutableArrayRefCPPType &GET_TYPE_generic_mutable_array_ref(CPPType &base);
+ArrayRefCPPType &GET_TYPE_array_ref(CPPType &base);
+MutableArrayRefCPPType &GET_TYPE_mutable_array_ref(CPPType &base);
+
+template<typename T> ArrayRefCPPType &GET_TYPE_array_ref()
+{
+  return GET_TYPE_array_ref(GET_TYPE<T>());
+}
+
+template<typename T> MutableArrayRefCPPType &GET_TYPE_mutable_array_ref()
+{
+  return GET_TYPE_mutable_array_ref(GET_TYPE<T>());
+}
 
 }  // namespace BKE
 
diff --git a/source/blender/blenkernel/BKE_node_functions.h b/source/blender/blenkernel/BKE_node_functions.h
new file mode 100644
index 00000000000..41bfd713e0f
--- /dev/null
+++ b/source/blender/blenkernel/BKE_node_functions.h
@@ -0,0 +1,24 @@
+#ifndef __BKE_NODE_FUNCTIONS_H__
+#define __BKE_NODE_FUNCTIONS_H__
+
+#include "BKE_virtual_node_tree_cxx.h"
+#include "BKE_cpp_function.h"
+
+#include "BLI_optional.h"
+
+namespace BKE {
+
+using BLI::Optional;
+
+struct FunctionForNode {
+  CPPFunction *function;
+  bool is_newly_allocated;
+};
+
+Optional<FunctionForNode> get_vnode_array_function(VirtualNode *vnode);
+
+void init_vnode_array_functions();
+
+};  // namespace BKE
+
+#endif /* __BKE_NODE_FUNCTIONS_H__ */
diff --git a/source/blender/blenkernel/BKE_tuple.h b/source/blender/blenkernel/BKE_tuple.h
index d0c058b7358..7385ecfd359 100644
--- a/source/blender/blenkernel/BKE_tuple.h
+++ b/source/blender/blenkernel/BKE_tuple.h
@@ -191,7 +191,8 @@ class TupleRef {
 
   template<typename T> void set(uint index, const T &value)
   {
-    BLI_STATIC_ASSERT(std::is_trivial<T>::value, "can only be used with trivial types");
+    BLI_STATIC_ASSERT(std::is_trivially_copyable<T>::value,
+                      "can only be used with trivially copyable types");
     this->copy_in<T>(index, value);
   }
 
@@ -247,7 +248,8 @@ class TupleRef {
 
   template<typename T> T get(uint index) const
   {
-    BLI_STATIC_ASSERT(std::is_trivial<T>::value, "can only be used with trivial types");
+    BLI_STATIC_ASSERT(std::is_trivially_copyable<T>::value,
+                      "can only be used with trivially copyable types");
     return this->copy_out<T>(index);
   }
 
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index ad339f865d1..015954dd550 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -173,6 +173,7 @@ set(SRC
   intern/nla.c
   intern/node.c
   intern/node_tree.cpp
+  intern/node_functions.cc
   intern/object.c
   intern/object_deform.c
   intern/object_dupli.c
diff --git a/source/blender/blenkernel/intern/cpp_function.cc b/source/blender/blenkernel/intern/cpp_function.cc
index 98bf4f349d9..750878f0c80 100644
--- a/source/blender/blenkernel/intern/cpp_function.cc
+++ b/source/blender/blenkernel/intern/cpp_function.cc
@@ -35,7 +35,7 @@ class AddFloatsFunction : public CPPFunction {
 class AddFloatsArray : public CPPFunction {
   void signature(SignatureBuilderCPP &signature) override
   {
-    signature.add_input("A", GET_TYPE_generic_array_ref(GET_TYPE<float>()));
+    signature.add_input("A", GET_TYPE_array_ref(GET_TYPE<float>()));
     signature.add_output("B", GET_TYPE<float>());
   }
 
diff --git a/source/blender/blenkernel/intern/generic_array_ref.cc b/source/blender/blenkernel/intern/generic_array_ref.cc
index fdd48698f5b..cec8e39f077 100644
--- a/source/blender/blenkernel/intern/generic_array_ref.cc
+++ b/source/blender/blenkernel/intern/generic_array_ref.cc
@@ -25,7 +25,7 @@ BLI_LAZY_INIT_STATIC(MutableArrayRefTypeMapping, get_mutable_type_mapping)
 static std::mutex map_mutex__immutable;
 static std::mutex map_mutex__mutable;
 
-ArrayRefCPPType &GET_TYPE_generic_array_ref(CPPType &base)
+ArrayRefCPPType &GET_TYPE_array_ref(CPPType &base)
 {
   std::lock_guard<std::mutex> lock(map_mutex__immutable);
 
@@ -38,7 +38,7 @@ ArrayRefCPPType &GET_TYPE_generic_array_ref(CPPType &base)
   return *type;
 }
 
-MutableArrayRefCPPType &GET_TYPE_generic_mutable_array_ref(CPPType &base)
+MutableArrayRefCPPType &GET_TYPE_mutable_array_ref(CPPType &base)
 {
   std::lock_guard<std::mutex> lock(map_mutex__mutable);
 
diff --git a/source/blender/blenkernel/intern/node_functions.cc b/source/blender/blenkernel/intern/node_functions.cc
new file mode 100644
index 00000000000..d79d894ebd7
--- /dev/null
+++ b/source/blender/blenkernel/intern/node_functions.cc
@@ -0,0 +1,79 @@
+#include "BKE_node_functions.h"
+#include "BKE_generic_array_ref.h"
+
+#include "BLI_math_cxx.h"
+#include "BLI_lazy_init_cxx.h"
+#include "BLI_string_map.h"
+
+namespace BKE {
+
+using BLI::float3;
+using BLI::StringMap;
+using CreateFunctionCB = std::unique_ptr<CPPFunction> (*)(VirtualNode *vnode);
+
+BLI_LAZY_INIT_STATIC_VAR(StringMap<std::unique_ptr<CPPFunction>>, get_cached_functions)
+BLI_LAZY_INIT_STATIC_VAR(StringMap<CreateFunctionCB>, get_function_builders)
+
+class ArrayRefFunction_AddFloats : public CPPFunction {
+  void signature(SignatureBuilderCPP &signature) override
+  {
+    signature.add_input("Indices", GET_TYPE_array_ref<uint>());
+    signature.add_input("A", GET_TYPE_array_ref<float>());
+    signature.add_input("B", GET_TYPE_array_ref<float>());
+    signature.add_input("Result", GET_TYPE_mutable_array_ref<float>());
+  }
+
+  void call(TupleRef &fn_in, TupleRef &UNUSED(fn_out)) const override
+  {
+    auto indices = fn_in.get<GenericArrayRef>(0).get_ref<uint>();
+    auto a = fn_in.get<GenericArrayRef>(1).get_ref<float>();
+    auto b = fn_in.get<GenericArrayRef>(2).get_ref<float>();
+    auto result = fn_in.get<GenericMutableArrayRef>(3).get_ref<float>();
+
+    for (uint i : indices) {
+      result[i] = a[i] + b[i];
+    }
+  }
+};
+
+class ArrayRefFunction_VectorDistance : public CPPFunction {
+  void signature(SignatureBuilderCPP &signature) override
+  {
+    signature.add_input("Indices", GET_TYPE_array_ref<uint>());
+    signature.add_input("A", GET_TYPE_array_ref<float3>());
+    signature.add_input("B", GET_TYPE_array_ref<float3>());
+    signature.add_input("Result", GET_TYPE_mutable_array_ref<float>());
+  }
+
+  void call(TupleRef &fn_in, TupleRef &UNUSED(fn_out)) const override
+  {
+    auto indices = fn_in.get<GenericArrayRef>(0).get_ref<uint>();
+    auto a = fn_in.get<GenericArrayRef>(1).get_ref<float3>();
+    auto b = fn_in.get<GenericArrayRef>(2).get_ref<float3>();
+    auto result = fn_in.get<GenericMutableArrayRef>(3).get_ref<float>();
+
+    for (uint i : indices) {
+      result[i] = float3::distance(a[i], b[i]);
+    }
+  }
+};
+
+void init_vnode_array_functions()
+{
+  auto &cached_functions = get_cached_functions();
+  auto &callbacks = get_function_builders();
+}
+
+Optional<FunctionForNode> get_vnode_array_function(VirtualNode *vnode)
+{
+  {
+    auto &cached_functions = get_cached_functions();
+    std::unique_ptr<CPPFunction> *function = cached_functions.lookup_ptr(vnode->idname());
+    if (function != nullptr) {
+      return FunctionForNode{(*function).get(), false};
+    }
+  }
+  return {};
+}
+
+}  // namespace BKE
\ No newline at end of file
diff --git a/source/blender/blenlib/BLI_lazy_init_cxx.h b/source/blender/blenlib/BLI_lazy_init_cxx.h
index 277637520c0..0ca9e35c112 100644
--- a/source/blender/blenlib/BLI_lazy_init_cxx.h
+++ b/source/blender/blenlib/BLI_lazy_init_cxx.h
@@ -77,3 +77,9 @@ void lazy_init_register(std::function<void()> free_func, const char *name);
     return value; \
   } \
   std::unique_ptr<TYPE> NAME##_impl(void)
+
+#define BLI_LAZY_INIT_STATIC_VAR(type, func_name) \
+  BLI_LAZY_INIT_STATIC(type, func_name) \
+  { \
+    return {}; \
+  }
\ No newline at end of file



More information about the Bf-blender-cvs mailing list