[Bf-blender-cvs] [e09df00e832] nodes_playground: make nodes separate classes

Jacques Lucke noreply at git.blender.org
Sun Jan 6 17:25:41 CET 2019


Commit: e09df00e8320e323d86b5197811dce440226f2b5
Author: Jacques Lucke
Date:   Sat Jan 5 13:10:22 2019 +0100
Branches: nodes_playground
https://developer.blender.org/rBe09df00e8320e323d86b5197811dce440226f2b5

make nodes separate classes

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

M	source/blender/modifiers/intern/node_compiler.cpp
M	source/blender/modifiers/intern/node_compiler.hpp
M	source/blender/modifiers/intern/node_compiler_testing.cpp

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

diff --git a/source/blender/modifiers/intern/node_compiler.cpp b/source/blender/modifiers/intern/node_compiler.cpp
index 740292c4efd..e10ee63c923 100644
--- a/source/blender/modifiers/intern/node_compiler.cpp
+++ b/source/blender/modifiers/intern/node_compiler.cpp
@@ -54,7 +54,7 @@ std::string AnySocket::debug_name() const
 std::string Node::debug_id() const
 {
 	std::stringstream ss;
-	ss << this->debug_name << " at " << (void *)this;
+	ss << "Node at " << (void *)this;
 	return ss.str();
 }
 
@@ -204,7 +204,7 @@ llvm::Value *DataFlowGraph::generateCodeForSocket(
 		}
 
 		std::vector<llvm::Value *> output_values;
-		node->build_ir(input_values, builder, output_values, r_builder);
+		node->buildLLVMIR(input_values, builder, output_values, r_builder);
 
 		for (uint i = 0; i < node->outputs().size(); i++) {
 			values.add(node->Output(i), output_values[i]);
diff --git a/source/blender/modifiers/intern/node_compiler.hpp b/source/blender/modifiers/intern/node_compiler.hpp
index 84bcb288e2b..f0b8f8514cc 100644
--- a/source/blender/modifiers/intern/node_compiler.hpp
+++ b/source/blender/modifiers/intern/node_compiler.hpp
@@ -89,45 +89,67 @@ typedef std::function<void(
 	std::vector<llvm::Value *> &inputs, llvm::IRBuilder<> *builder,
 	std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)> IRBuilderFunction;
 
-class Node final {
+struct NodeSockets {
 private:
-	std::string debug_name;
-	std::vector<SocketInfo> _inputs;
-	std::vector<SocketInfo> _outputs;
-	IRBuilderFunction generateCode;
+	using sockets_t = std::vector<SocketInfo>;
+	sockets_t sockets;
 
 public:
-	static Node *FromIRBuilderFunction(
-		const std::string &debug_name,
-		const std::vector<SocketInfo> &inputs,
-		const std::vector<SocketInfo> &outputs,
-		const IRBuilderFunction &generateCode)
-	{
-		Node *node = new Node();
-		node->debug_name = debug_name;
-		node->_inputs = inputs;
-		node->_outputs = outputs;
-		node->generateCode = generateCode;
-		return node;
-	}
+	using const_iterator = typename sockets_t::const_iterator;
+
+	NodeSockets() {}
+
+	inline void add(SocketInfo socket)
+	{ this->sockets.push_back(socket); }
+
+	inline void add(std::string debug_name, Type *type)
+	{ this->sockets.push_back(SocketInfo(debug_name, type)); }
+
+	inline uint size() const
+	{ return this->sockets.size(); }
+
+	const SocketInfo &operator[](const int index) const
+	{ return this->sockets[index]; }
 
-	std::string debug_id() const;
+	const_iterator begin() const
+	{ return this->sockets.begin(); }
+	const_iterator end() const
+	{ return this->sockets.end(); }
+};
+
+class Node {
+protected:
+	NodeSockets m_inputs, m_outputs;
+public:
+	inline const NodeSockets &inputs()
+	{ return this->m_inputs; }
+	inline const NodeSockets &outputs()
+	{ return this->m_outputs; }
+
+	virtual std::string debug_id() const;
 
-	inline const std::vector<SocketInfo> &inputs()
-	{ return this->_inputs; }
-	inline const std::vector<SocketInfo> &outputs()
-	{ return this->_outputs; }
+	virtual void buildLLVMIR(
+		std::vector<llvm::Value *> &inputs, llvm::IRBuilder<> *builder,
+		std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder) = 0;
 
-	inline AnySocket Input(uint index)
+	inline AnySocket Input(const uint index)
 	{ return AnySocket::NewInput(this, index); }
-	inline AnySocket Output(uint index)
+	inline AnySocket Output(const uint index)
 	{ return AnySocket::NewOutput(this, index); }
+};
+
+class SingleBuilderNode : public Node {
+	virtual void buildLLVMIR(
+		llvm::IRBuilder<> *builder,
+		std::vector<llvm::Value *> &inputs,
+		std::vector<llvm::Value *> &r_outputs) = 0;
 
-	void build_ir(
+	void buildLLVMIR(
 		std::vector<llvm::Value *> &inputs, llvm::IRBuilder<> *builder,
 		std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)
 	{
-		this->generateCode(inputs, builder, r_outputs, r_builder);
+		this->buildLLVMIR(builder, inputs, r_outputs);
+		*r_builder = builder;
 	}
 };
 
diff --git a/source/blender/modifiers/intern/node_compiler_testing.cpp b/source/blender/modifiers/intern/node_compiler_testing.cpp
index b4e03fdf337..58dec8c4bd8 100644
--- a/source/blender/modifiers/intern/node_compiler_testing.cpp
+++ b/source/blender/modifiers/intern/node_compiler_testing.cpp
@@ -25,56 +25,66 @@ public:
 
 auto *type_int32 = new IntegerType(32);
 
-static void generateCode_AddNode(
-	std::vector<llvm::Value *> &inputs, llvm::IRBuilder<> *builder,
-	std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)
-{
-	llvm::Value *result = builder->CreateAdd(inputs[0], inputs[1]);
-	r_outputs.push_back(result);
-	*r_builder = builder;
-}
+class IntInputNode : public NC::SingleBuilderNode {
+private:
+	int number;
 
-static NC::Node *new_add_node()
-{
-	return NC::Node::FromIRBuilderFunction("Add",
-		{ NC::SocketInfo("A", type_int32),
-		  NC::SocketInfo("B", type_int32) },
-		{ NC::SocketInfo("Result", type_int32) },
-		generateCode_AddNode
-	);
-}
+public:
+	IntInputNode(int number)
+		: number(number)
+	{
+		this->m_outputs.add("Value", type_int32);
+	}
 
-static NC::Node *new_int_node(int value)
-{
-	return NC::Node::FromIRBuilderFunction("Int",
-		{},
-		{ NC::SocketInfo("Value", type_int32) },
-		[value](
-			std::vector<llvm::Value *> &UNUSED(inputs), llvm::IRBuilder<> *builder,
-			std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)
-			{
-				r_outputs.push_back(builder->getInt32(value));
-				*r_builder = builder;
-			}
-	);
-}
+	void buildLLVMIR(
+		llvm::IRBuilder<> *builder,
+		std::vector<llvm::Value *> &UNUSED(inputs),
+		std::vector<llvm::Value *> &r_outputs)
+	{
+		r_outputs.push_back(builder->getInt32(this->number));
+	}
+};
+
+class IntRefInputNode : public NC::SingleBuilderNode {
+private:
+	int *pointer;
+
+public:
+	IntRefInputNode(int *pointer)
+		: pointer(pointer)
+	{
+		this->m_outputs.add("Value", type_int32);
+	}
+
+	void buildLLVMIR(
+		llvm::IRBuilder<> *builder,
+		std::vector<llvm::Value *> &UNUSED(inputs),
+		std::vector<llvm::Value *> &r_outputs)
+	{
+		auto address_int = builder->getInt64((size_t)this->pointer);
+		auto address = builder->CreateIntToPtr(address_int, llvm::Type::getInt32PtrTy(builder->getContext()));
+		r_outputs.push_back(builder->CreateLoad(address));
+	}
+};
+
+class AddIntegersNode : public NC::SingleBuilderNode {
+public:
+	AddIntegersNode()
+	{
+		this->m_inputs.add("A", type_int32);
+		this->m_inputs.add("B", type_int32);
+		this->m_outputs.add("Result", type_int32);
+	}
+
+	void buildLLVMIR(
+		llvm::IRBuilder<> *builder,
+		std::vector<llvm::Value *> &inputs,
+		std::vector<llvm::Value *> &r_outputs)
+	{
+		r_outputs.push_back(builder->CreateAdd(inputs[0], inputs[1]));
+	}
+};
 
-static NC::Node *new_int_ref_node(int *value)
-{
-	return NC::Node::FromIRBuilderFunction("Int Ref",
-		{},
-		{ NC::SocketInfo("Value", type_int32) },
-		[value](
-			std::vector<llvm::Value *> &UNUSED(inputs), llvm::IRBuilder<> *builder,
-			std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)
-			{
-				auto address_int = builder->getInt64((uint64_t)value);
-				auto address = builder->CreateIntToPtr(address_int, llvm::Type::getInt32PtrTy(builder->getContext()));
-				r_outputs.push_back(builder->CreateLoad(address));
-				*r_builder = builder;
-			}
-	);
-}
 
 extern "C" {
 	void run_tests(void);
@@ -84,13 +94,13 @@ void run_tests()
 {
 	int test_value = 1000;
 
-	auto in1 = new_int_node(1);
-	auto in2 = new_int_ref_node(&test_value);
-	auto in3 = new_int_node(10);
+	auto in1 = new IntInputNode(1);
+	auto in2 = new IntRefInputNode(&test_value);
+	auto in3 = new IntInputNode(10);
 
-	auto add1 = new_add_node();
-	auto add2 = new_add_node();
-	auto add3 = new_add_node();
+	auto add1 = new AddIntegersNode();
+	auto add2 = new AddIntegersNode();
+	auto add3 = new AddIntegersNode();
 
 	NC::DataFlowGraph graph;
 	graph.nodes.push_back(in1);
@@ -108,12 +118,12 @@ void run_tests()
 	graph.links.links.push_back(NC::Link(add2->Output(0), add3->Input(1)));
 
 
-	NC::SocketArraySet inputs = { in1->Output(0), in2->Output(0), in3->Output(0) };
+	NC::SocketArraySet inputs = { in1->Output(0), in2->Output(0) };
 	NC::SocketArraySet outputs = { add3->Output(0) };
 	NC::DataFlowCallable *callable = graph.generateCallable("Hello", inputs, outputs);
 
 	callable->printCode();
-	int result = ((int (*)(int, int, int))callable->getFunctionPointer())(10, 25, 100);
+	int result = ((int (*)(int, int))callable->getFunctionPointer())(10, 25);
 	std::cout << result << std::endl;
 
 	// NC::SocketSet inputs = { add1->Input(0), add1->Input(1), add2->Input(1) };



More information about the Bf-blender-cvs mailing list