[Bf-blender-cvs] [2330dae] object_nodes: Added optional automatic datatype conversion for nodes.

Lukas Tönne noreply at git.blender.org
Tue Nov 24 09:44:00 CET 2015


Commit: 2330dae3ef61f65d0e52234c966bbc6c950149bc
Author: Lukas Tönne
Date:   Wed Nov 4 14:08:59 2015 +0100
Branches: object_nodes
https://developer.blender.org/rB2330dae3ef61f65d0e52234c966bbc6c950149bc

Added optional automatic datatype conversion for nodes.

This only applies to a handful of common types that can be unambiguously converted.
In any other case the link is considered invalid (proper error handling still needed).

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

M	source/blender/blenvm/BVM_api.h
M	source/blender/blenvm/bvm/bvm_eval.cc
M	source/blender/blenvm/bvm/bvm_opcode.h
M	source/blender/blenvm/compile/bvm_nodegraph.cc
M	source/blender/blenvm/compile/bvm_nodegraph.h
M	source/blender/blenvm/intern/bvm_api.cc
M	source/blender/makesrna/intern/rna_blenvm.c

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

diff --git a/source/blender/blenvm/BVM_api.h b/source/blender/blenvm/BVM_api.h
index b24b217..2f0cdfd 100644
--- a/source/blender/blenvm/BVM_api.h
+++ b/source/blender/blenvm/BVM_api.h
@@ -72,7 +72,8 @@ int BVM_compile_get_object_index(struct BVMCompileContext *context, struct Objec
 struct BVMNodeInstance *BVM_nodegraph_add_node(struct BVMNodeGraph *graph, const char *type, const char *name);
 void BVM_nodegraph_add_link(struct BVMNodeGraph *graph,
                             struct BVMNodeInstance *from_node, const char *from_socket,
-                            struct BVMNodeInstance *to_node, const char *to_socket);
+                            struct BVMNodeInstance *to_node, const char *to_socket,
+                            bool autoconvert);
 void BVM_nodegraph_set_output_link(struct BVMNodeGraph *graph,
                                    const char *name, struct BVMNodeInstance *node, const char *socket);
 
diff --git a/source/blender/blenvm/bvm/bvm_eval.cc b/source/blender/blenvm/bvm/bvm_eval.cc
index af44688..f8a98cb 100644
--- a/source/blender/blenvm/bvm/bvm_eval.cc
+++ b/source/blender/blenvm/bvm/bvm_eval.cc
@@ -86,6 +86,18 @@ static void eval_op_value_pointer(float *stack, PointerRNA value, StackIndex off
 	stack_store_pointer(stack, offset, value);
 }
 
+static void eval_op_float_to_int(float *stack, StackIndex offset_from, StackIndex offset_to)
+{
+	float f = stack_load_float(stack, offset_from);
+	stack_store_int(stack, offset_to, (int)f);
+}
+
+static void eval_op_int_to_float(float *stack, StackIndex offset_from, StackIndex offset_to)
+{
+	int i = stack_load_int(stack, offset_from);
+	stack_store_float(stack, offset_to, (float)i);
+}
+
 static void eval_op_pass_float(float *stack, StackIndex offset_from, StackIndex offset_to)
 {
 	float f = stack_load_float(stack, offset_from);
@@ -443,6 +455,18 @@ void EvalContext::eval_instructions(const EvalGlobals *globals, const EvalData *
 				eval_op_value_pointer(stack, value, offset);
 				break;
 			}
+			case OP_FLOAT_TO_INT: {
+				StackIndex offset_from = expr->read_stack_index(&instr);
+				StackIndex offset_to = expr->read_stack_index(&instr);
+				eval_op_float_to_int(stack, offset_from, offset_to);
+				break;
+			}
+			case OP_INT_TO_FLOAT: {
+				StackIndex offset_from = expr->read_stack_index(&instr);
+				StackIndex offset_to = expr->read_stack_index(&instr);
+				eval_op_int_to_float(stack, offset_from, offset_to);
+				break;
+			}
 			case OP_PASS_FLOAT: {
 				StackIndex offset_from = expr->read_stack_index(&instr);
 				StackIndex offset_to = expr->read_stack_index(&instr);
diff --git a/source/blender/blenvm/bvm/bvm_opcode.h b/source/blender/blenvm/bvm/bvm_opcode.h
index 3a200d3..a4bacb6 100644
--- a/source/blender/blenvm/bvm/bvm_opcode.h
+++ b/source/blender/blenvm/bvm/bvm_opcode.h
@@ -42,6 +42,8 @@ enum OpCode {
 	OP_VALUE_INT,
 	OP_VALUE_MATRIX44,
 	OP_VALUE_POINTER,
+	OP_FLOAT_TO_INT,
+	OP_INT_TO_FLOAT,
 	OP_PASS_FLOAT,
 	OP_PASS_FLOAT3,
 	OP_PASS_FLOAT4,
diff --git a/source/blender/blenvm/compile/bvm_nodegraph.cc b/source/blender/blenvm/compile/bvm_nodegraph.cc
index 4b09a28..6981c2f 100644
--- a/source/blender/blenvm/compile/bvm_nodegraph.cc
+++ b/source/blender/blenvm/compile/bvm_nodegraph.cc
@@ -709,6 +709,8 @@ OpCode get_opcode_from_node_type(const string &node)
 	if (node == STRINGIFY(name)) \
 		return OP_##name
 	
+	NODETYPE(FLOAT_TO_INT);
+	NODETYPE(INT_TO_FLOAT);
 	NODETYPE(PASS_FLOAT);
 	NODETYPE(PASS_FLOAT3);
 	NODETYPE(PASS_FLOAT4);
@@ -768,6 +770,14 @@ void register_opcode_node_types()
 {
 	NodeType *nt;
 	
+	nt = NodeGraph::add_node_type("FLOAT_TO_INT");
+	nt->add_input("value", BVM_FLOAT, 0.0f);
+	nt->add_output("value", BVM_INT, 0);
+	
+	nt = NodeGraph::add_node_type("INT_TO_FLOAT");
+	nt->add_input("value", BVM_INT, 0);
+	nt->add_output("value", BVM_FLOAT, 0.0f);
+	
 	nt = NodeGraph::add_node_type("PASS_FLOAT");
 	nt->add_input("value", BVM_FLOAT, 0.0f);
 	nt->add_output("value", BVM_FLOAT, 0.0f);
diff --git a/source/blender/blenvm/compile/bvm_nodegraph.h b/source/blender/blenvm/compile/bvm_nodegraph.h
index 1eb12de..238c585 100644
--- a/source/blender/blenvm/compile/bvm_nodegraph.h
+++ b/source/blender/blenvm/compile/bvm_nodegraph.h
@@ -204,7 +204,7 @@ struct NodeGraph {
 	typedef std::pair<string, NodeType> NodeTypeMapPair;
 	typedef std::map<string, NodeInstance> NodeInstanceMap;
 	typedef std::pair<string, NodeInstance> NodeInstanceMapPair;
-	
+	typedef std::pair<NodeInstance*, string> SocketPair;
 	
 	static NodeTypeMap node_types;
 	
@@ -220,7 +220,8 @@ struct NodeGraph {
 	
 	template <typename FromT, typename ToT>
 	bool add_link(NodeInstance *from_node, FromT from,
-	              NodeInstance *to_node, ToT to)
+	              NodeInstance *to_node, ToT to,
+	              bool autoconvert=true)
 	{
 		if (!to_node || !from_node)
 			return false;
@@ -230,16 +231,24 @@ struct NodeGraph {
 		if (!from_socket || !to_socket)
 			return false;
 		
-		to_node->set_input_link(to_socket->name, from_node, from_socket);
+		SocketPair converted = (autoconvert) ?
+		                           add_type_converter(SocketPair(from_node, from), to_socket->type) :
+		                           SocketPair(from_node, from);
+		if (!converted.first)
+			return false;
+		
+		const NodeSocket *conv_socket = converted.first->type->find_output(converted.second);
+		to_node->set_input_link(to_socket->name, converted.first, conv_socket);
 		
 		return true;
 	}
 	
 	template <typename FromT, typename ToT>
 	bool add_link(const string &from_node, FromT from,
-	              const string &to_node, ToT to)
+	              const string &to_node, ToT to,
+	              bool autoconvert=true)
 	{
-		return add_link(get_node(from_node), from, get_node(to_node), to);
+		return add_link(get_node(from_node), from, get_node(to_node), to, autoconvert);
 	}
 	
 	const NodeGraphInput *get_input(int index) const;
@@ -260,6 +269,132 @@ struct NodeGraph {
 	void dump(std::ostream &stream = std::cout);
 	void dump_graphviz(FILE *f, const string &label);
 	
+protected:
+	SocketPair add_float_converter(const SocketPair &from, BVMType to_type)
+	{
+		SocketPair result(NULL, "");
+		switch (to_type) {
+			case BVM_FLOAT3: {
+				NodeInstance *node = add_node("SET_FLOAT3");
+				add_link(from.first, from.second, node, "value_x");
+				add_link(from.first, from.second, node, "value_y");
+				add_link(from.first, from.second, node, "value_z");
+				result = SocketPair(node, "value");
+				break;
+			}
+			case BVM_FLOAT4: {
+				NodeInstance *node = add_node("SET_FLOAT4");
+				add_link(from.first, from.second, node, "value_x");
+				add_link(from.first, from.second, node, "value_y");
+				add_link(from.first, from.second, node, "value_z");
+				add_link(from.first, from.second, node, "value_w");
+				result = SocketPair(node, "value");
+				break;
+			}
+			case BVM_INT: {
+				NodeInstance *node = add_node("FLOAT_TO_INT");
+				add_link(from.first, from.second, node, "value");
+				result = SocketPair(node, "value");
+				break;
+			}
+			default:
+				break;
+		}
+		return result;
+	}
+	
+	SocketPair add_float3_converter(const SocketPair &from, BVMType to_type)
+	{
+		SocketPair result(NULL, "");
+		switch (to_type) {
+			case BVM_FLOAT4: {
+				NodeInstance *node_x = add_node("GET_ELEM_FLOAT3");
+				node_x->set_input_value("index", 0);
+				add_link(from.first, from.second, node_x, "value");
+				NodeInstance *node_y = add_node("GET_ELEM_FLOAT3");
+				node_y->set_input_value("index", 1);
+				add_link(from.first, from.second, node_y, "value");
+				NodeInstance *node_z = add_node("GET_ELEM_FLOAT3");
+				node_z->set_input_value("index", 2);
+				add_link(from.first, from.second, node_z, "value");
+				
+				NodeInstance *node = add_node("SET_FLOAT4");
+				add_link(node_x, "value", node, "value_x");
+				add_link(node_y, "value", node, "value_y");
+				add_link(node_z, "value", node, "value_z");
+				node->set_input_value("value_w", 1.0f);
+				result = SocketPair(node, "value");
+				break;
+			}
+			default:
+				break;
+		}
+		return result;
+	}
+	
+	SocketPair add_float4_converter(const SocketPair &from, BVMType to_type)
+	{
+		SocketPair result(NULL, "");
+		switch (to_type) {
+			case BVM_FLOAT3: {
+				NodeInstance *node_x = add_node("GET_ELEM_FLOAT4");
+				node_x->set_input_value("index", 0);
+				add_link(from.first, from.second, node_x, "value");
+				NodeInstance *node_y = add_node("GET_ELEM_FLOAT4");
+				node_y->set_input_value("index", 1);
+				add_link(from.first, from.second, node_y, "value");
+				NodeInstance *node_z = add_node("GET_ELEM_FLOAT4");
+				node_z->set_input_value("index", 2);
+				add_link(from.first, from.second, node_z, "value");
+				
+				NodeInstance *node = add_node("SET_FLOAT3");
+				add_link(node_x, "value", node, "value_x");
+				add_link(node_y, "value", node, "value_y");
+				add_link(node_z, "value", node, "value_z");
+				result = SocketPair(node, "value");
+				break;
+			}
+			default:
+				break;
+		}
+		return result;
+	}
+	
+	SocketPair add_int_converter(const SocketPair &from, BVMType to_type)
+	{
+		SocketPair result(NULL, "");
+		return result;
+	}
+	
+	SocketPair add_matrix44_converter(const SocketPair &from, BVMType to_type)
+	{
+		SocketPair result(NULL, "");
+		return result;
+	}
+	
+	SocketPair add_type_converter(const SocketPair &from, BVMType to_type)
+	{
+		SocketPair result(NULL, "");
+		const NodeSocket *from_socket = from.first->type->find_output(from.second);
+		BVMType from_type = from_socket->type;
+		
+		if (from_type == to_type) {
+			result = from;
+		}
+		else {
+			switch (from_type) {
+				case BVM_FLOAT: result = add_float_converter(from, to_type); break;
+				case BVM_FLOAT3: result = add_float3_converter(from, to_type); break;
+				case BVM_FLOAT4: result = add_float4_converter(from, to_type); break;
+				case BVM_INT: result = add_int_converter(from, to_type); break;
+				case BVM_MATRIX44: result = add_matrix44_converter(from, to_type); break;
+				default: break;
+			}
+		}
+		return result;
+	}
+	
+public:
 	NodeInstanceMap nodes;
 	InputList inputs;
 	OutputList outputs;
diff --git a/source/blender/blenvm/intern/bvm_api.cc b/source/blender/blenvm/intern/bvm_api.cc
index 7fb11b4..ae14673 100644
--

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list