[Bf-blender-cvs] [c3a3b2e9b1c] functions: new Switch node

Jacques Lucke noreply at git.blender.org
Sun Mar 24 16:17:09 CET 2019


Commit: c3a3b2e9b1cd1fa7a44a0a246bb5dff0c8aaf291
Author: Jacques Lucke
Date:   Sun Mar 24 16:16:59 2019 +0100
Branches: functions
https://developer.blender.org/rBc3a3b2e9b1cd1fa7a44a0a246bb5dff0c8aaf291

new Switch node

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

A	release/scripts/startup/function_nodes/nodes/switch.py
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/backends/tuple_call/tuple.hpp
M	source/blender/functions/core/function.hpp
M	source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
M	source/blender/functions/functions/lists.cpp
A	source/blender/functions/functions/switch.cpp
A	source/blender/functions/functions/switch.hpp

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

diff --git a/release/scripts/startup/function_nodes/nodes/switch.py b/release/scripts/startup/function_nodes/nodes/switch.py
new file mode 100644
index 00000000000..e0a44039784
--- /dev/null
+++ b/release/scripts/startup/function_nodes/nodes/switch.py
@@ -0,0 +1,28 @@
+import bpy
+from bpy.props import *
+from .. base import FunctionNode
+from .. socket_decl import FixedSocketDecl
+
+class SwitchNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_SwitchNode"
+    bl_label = "Switch"
+
+    data_type: StringProperty(
+        default="Float",
+        update=FunctionNode.refresh
+    )
+
+    def get_sockets(self):
+        return [
+            FixedSocketDecl("condition", "Condition", "Boolean"),
+            FixedSocketDecl("true", "True", self.data_type),
+            FixedSocketDecl("false", "False", self.data_type),
+        ], [
+            FixedSocketDecl("result", "Result", self.data_type),
+        ]
+
+    def draw(self, layout):
+        self.invoke_type_selection(layout, "set_type", "Change Type")
+
+    def set_type(self, data_type):
+        self.data_type = data_type
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 7de4916e4bf..144a772f095 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -86,6 +86,8 @@ set(SRC
 	functions/lists.cpp
 	functions/simple_conversions.hpp
 	functions/simple_conversions.cpp
+	functions/switch.hpp
+	functions/switch.cpp
 
 	frontends/data_flow_nodes/builder.hpp
 	frontends/data_flow_nodes/builder.cpp
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index 03b4ad977d0..4da5b8b27e0 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -5,4 +5,5 @@
 #include "functions/scalar_math.hpp"
 #include "functions/vectors.hpp"
 #include "functions/lists.hpp"
-#include "functions/simple_conversions.hpp"
\ No newline at end of file
+#include "functions/simple_conversions.hpp"
+#include "functions/switch.hpp"
\ No newline at end of file
diff --git a/source/blender/functions/backends/tuple_call/tuple.hpp b/source/blender/functions/backends/tuple_call/tuple.hpp
index fd17ebd1fac..10241f76a53 100644
--- a/source/blender/functions/backends/tuple_call/tuple.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple.hpp
@@ -155,6 +155,24 @@ namespace FN {
 			m_initialized[index] = true;
 		}
 
+		inline void move_in__dynamic(uint index, void *src)
+		{
+			BLI_assert(index < m_meta->element_amount());
+			BLI_assert(src != nullptr);
+
+			void *dst = this->element_ptr(index);
+			auto *type_info = m_meta->type_infos()[index];
+
+			if (m_initialized[index]) {
+				type_info->copy_to_initialized(src, dst);
+			}
+			else {
+				type_info->copy_to_uninitialized(src, dst);
+			}
+
+			m_initialized[index] = true;
+		}
+
 		template<typename T>
 		inline void set(uint index, const T &value)
 		{
@@ -188,6 +206,19 @@ namespace FN {
 			return tmp;
 		}
 
+		inline void relocate_out__dynamic(uint index, void *dst) const
+		{
+			BLI_assert(index < m_meta->element_amount());
+			BLI_assert(m_initialized[index]);
+			BLI_assert(dst != nullptr);
+
+			void *src = this->element_ptr(index);
+			auto *type_info = m_meta->type_infos()[index];
+			type_info->copy_to_uninitialized(src, dst);
+			type_info->destruct_type(src);
+			m_initialized[index] = false;
+		}
+
 		template<typename T>
 		inline T get(uint index) const
 		{
diff --git a/source/blender/functions/core/function.hpp b/source/blender/functions/core/function.hpp
index d40c374f79a..e4eed570e3d 100644
--- a/source/blender/functions/core/function.hpp
+++ b/source/blender/functions/core/function.hpp
@@ -91,5 +91,6 @@ namespace FN {
 	};
 
 	using SharedFunction = AutoRefCount<Function>;
+	using FunctionPerType = SmallMap<SharedType, SharedFunction>;
 
 } /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp b/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
index 661e6bc190b..77dcbb53fa6 100644
--- a/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/inserters/nodes.cpp
@@ -151,6 +151,17 @@ namespace FN { namespace DataFlowNodes {
 		builder.map_sockets(node, bnode);
 	}
 
+	static void insert_switch_node(
+		Builder &builder,
+		const BuilderContext &ctx,
+		bNode *bnode)
+	{
+		SharedType &data_type = ctx.type_from_rna(bnode, "data_type");
+		auto fn = Functions::bool_switch(data_type);
+		Node *node = builder.insert_function(fn);
+		builder.map_sockets(node, bnode);
+	}
+
 	void register_node_inserters(GraphInserters &inserters)
 	{
 		inserters.reg_node_function("fn_CombineVectorNode", Functions::combine_vector);
@@ -165,6 +176,7 @@ namespace FN { namespace DataFlowNodes {
 		inserters.reg_node_inserter("fn_GetListElementNode", insert_get_list_element_node);
 		inserters.reg_node_inserter("fn_PackListNode", insert_pack_list_node);
 		inserters.reg_node_inserter("fn_CallNode", insert_call_node);
+		inserters.reg_node_inserter("fn_SwitchNode", insert_switch_node);
 	}
 
 } }
\ No newline at end of file
diff --git a/source/blender/functions/functions/lists.cpp b/source/blender/functions/functions/lists.cpp
index 6d539012758..07c2643cf20 100644
--- a/source/blender/functions/functions/lists.cpp
+++ b/source/blender/functions/functions/lists.cpp
@@ -133,8 +133,6 @@ namespace FN { namespace Functions {
 	/* Build List Functions
 	 *************************************/
 
-	using FunctionPerType = SmallMap<SharedType, SharedFunction>;
-
 	struct ListFunctions {
 		FunctionPerType m_create_empty;
 		FunctionPerType m_append;
diff --git a/source/blender/functions/functions/switch.cpp b/source/blender/functions/functions/switch.cpp
new file mode 100644
index 00000000000..05020f13665
--- /dev/null
+++ b/source/blender/functions/functions/switch.cpp
@@ -0,0 +1,62 @@
+#include "switch.hpp"
+
+#include "FN_types.hpp"
+#include "FN_tuple_call.hpp"
+
+#include "BLI_lazy_init.hpp"
+
+namespace FN { namespace Functions {
+
+	using namespace Types;
+
+	class BoolSwitch : public TupleCallBody {
+	private:
+		SharedType m_data_type;
+		CPPTypeInfo *m_type_info;
+
+	public:
+		BoolSwitch(SharedType data_type)
+			: m_data_type(data_type),
+			  m_type_info(data_type->extension<CPPTypeInfo>()) {}
+
+		void call(Tuple &fn_in, Tuple &fn_out) const override
+		{
+			bool condition = fn_in.get<bool>(0);
+			uint size = m_type_info->size_of_type();
+			void *value = alloca(size);
+			if (condition) {
+				fn_in.relocate_out__dynamic(1, value);
+				fn_out.move_in__dynamic(0, value);
+			}
+			else {
+				fn_in.relocate_out__dynamic(2, value);
+				fn_out.move_in__dynamic(0, value);
+			}
+		}
+	};
+
+	static SharedFunction build_bool_switch_function(SharedType &data_type)
+	{
+		std::string name = "Switch " + data_type->name();
+		auto fn = SharedFunction::New(name, Signature({
+			InputParameter("Condition", get_bool_type()),
+			InputParameter("True", data_type),
+			InputParameter("False", data_type),
+		}, {
+			OutputParameter("Result", data_type),
+		}));
+		fn->add_body(new BoolSwitch(data_type));
+		return fn;
+	}
+
+	SharedFunction &bool_switch(SharedType &data_type)
+	{
+		static FunctionPerType functions;
+		if (!functions.contains(data_type)) {
+			SharedFunction fn = build_bool_switch_function(data_type);
+			functions.add(data_type, fn);
+		}
+		return functions.lookup_ref(data_type);
+	}
+
+} } /* namespace FN::Functions */
\ No newline at end of file
diff --git a/source/blender/functions/functions/switch.hpp b/source/blender/functions/functions/switch.hpp
new file mode 100644
index 00000000000..eedfee5765a
--- /dev/null
+++ b/source/blender/functions/functions/switch.hpp
@@ -0,0 +1,9 @@
+#pragma once
+
+#include "FN_core.hpp"
+
+namespace FN { namespace Functions {
+
+	SharedFunction &bool_switch(SharedType &data_type);
+
+} } /* namespace FN::Functions */
\ No newline at end of file



More information about the Bf-blender-cvs mailing list