[Bf-blender-cvs] [72fd685] object_nodes: Experimental (and very messy) code for JIT compiling and running a simple stub function for texnodes on the LLVM backend.

Lukas Tönne noreply at git.blender.org
Mon Apr 4 17:35:22 CEST 2016


Commit: 72fd685f2f0c0261a86529745443b52cc70ef2c4
Author: Lukas Tönne
Date:   Mon Apr 4 17:34:08 2016 +0200
Branches: object_nodes
https://developer.blender.org/rB72fd685f2f0c0261a86529745443b52cc70ef2c4

Experimental (and very messy) code for JIT compiling and running a simple stub function for texnodes on the LLVM backend.

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

M	build_files/cmake/Modules/FindLLVM.cmake
M	source/blender/blenvm/BVM_api.h
M	source/blender/blenvm/compile/bvm_function.h
M	source/blender/blenvm/intern/bvm_api.cc
M	source/blender/blenvm/intern/function.cc
M	source/blender/blenvm/intern/function.h
M	source/blender/blenvm/intern/function_cache.cc
M	source/blender/blenvm/llvm/CMakeLists.txt
A	source/blender/blenvm/llvm/llvm_codegen.cc
A	source/blender/blenvm/llvm/llvm_codegen.h
M	source/blender/blenvm/llvm/llvm_engine.cc
M	source/blender/blenvm/llvm/llvm_engine.h
M	source/blender/blenvm/llvm/llvm_function.cc
M	source/blender/blenvm/llvm/llvm_function.h
M	source/blender/blenvm/llvm/llvm_headers.h
M	source/blender/blenvm/util/util_thread.h
M	source/blender/render/intern/source/render_texture.c

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

diff --git a/build_files/cmake/Modules/FindLLVM.cmake b/build_files/cmake/Modules/FindLLVM.cmake
index 112a837..7ef9807 100644
--- a/build_files/cmake/Modules/FindLLVM.cmake
+++ b/build_files/cmake/Modules/FindLLVM.cmake
@@ -87,6 +87,9 @@ if(LLVM_LIBRARY AND LLVM_ROOT_DIR AND LLVM_LIBPATH)
 		                OUTPUT_VARIABLE LLVM_LIBRARY
 		                OUTPUT_STRIP_TRAILING_WHITESPACE)
 		string(REPLACE " " ";" LLVM_LIBRARY "${LLVM_LIBRARY}")
+
+		# libterminfo is missing in llvm-config --libfiles
+		set(LLVM_LIBRARY ${LLVM_LIBRARY} tinfo)
 	endif()
 endif()
 
diff --git a/source/blender/blenvm/BVM_api.h b/source/blender/blenvm/BVM_api.h
index 30ce919..10cb383 100644
--- a/source/blender/blenvm/BVM_api.h
+++ b/source/blender/blenvm/BVM_api.h
@@ -123,11 +123,7 @@ void BVM_function_bvm_cache_set(void *key, struct BVMFunction *fn);
 void BVM_function_bvm_cache_remove(void *key);
 void BVM_function_bvm_cache_clear(void);
 
-struct BVMFunction *BVM_function_llvm_cache_acquire(void *key);
-void BVM_function_llvm_cache_release(struct BVMFunction *fn);
-void BVM_function_llvm_cache_set(void *key, struct BVMFunction *fn);
-void BVM_function_llvm_cache_remove(void *key);
-void BVM_function_llvm_cache_clear(void);
+void BVM_function_llvm_release(struct BVMFunction *fn);
 
 /* ------------------------------------------------------------------------- */
 
@@ -145,13 +141,19 @@ void BVM_eval_forcefield_bvm(struct BVMEvalGlobals *globals, struct BVMEvalConte
 struct Tex;
 struct TexResult;
 
-struct BVMFunction *BVM_gen_texture_function_bvm(struct bNodeTree *btree);
+struct BVMFunction *BVM_gen_texture_function_bvm(struct bNodeTree *btree, bool use_cache);
+struct BVMFunction *BVM_gen_texture_function_llvm(struct bNodeTree *btree, bool use_cache);
+
 void BVM_debug_texture_nodes(struct bNodeTree *btree, FILE *debug_file, const char *label, BVMDebugMode mode);
 
 void BVM_eval_texture_bvm(struct BVMEvalContext *context, struct BVMFunction *fn,
-                      struct TexResult *target,
-                      float coord[3], float dxt[3], float dyt[3], int osatex,
-                      short which_output, int cfra, int preview);
+                          struct TexResult *target,
+                          float coord[3], float dxt[3], float dyt[3], int osatex,
+                          short which_output, int cfra, int preview);
+void BVM_eval_texture_llvm(struct BVMEvalContext *context, struct BVMFunction *fn,
+                           struct TexResult *target,
+                           float coord[3], float dxt[3], float dyt[3], int osatex,
+                           short which_output, int cfra, int preview);
 
 /* ------------------------------------------------------------------------- */
 
diff --git a/source/blender/blenvm/compile/bvm_function.h b/source/blender/blenvm/compile/bvm_function.h
index d022408..4c66da5 100644
--- a/source/blender/blenvm/compile/bvm_function.h
+++ b/source/blender/blenvm/compile/bvm_function.h
@@ -64,7 +64,7 @@ struct Argument {
 	MEM_CXX_CLASS_ALLOC_FUNCS("BVM:ReturnValue")
 };
 
-struct FunctionBVM : public Function, public InstructionList {
+struct FunctionBVM : public FunctionBase, public InstructionList {
 	typedef std::vector<Argument> ArgumentList;
 	
 	FunctionBVM();
diff --git a/source/blender/blenvm/intern/bvm_api.cc b/source/blender/blenvm/intern/bvm_api.cc
index b451362..6cc5287 100644
--- a/source/blender/blenvm/intern/bvm_api.cc
+++ b/source/blender/blenvm/intern/bvm_api.cc
@@ -62,6 +62,9 @@ extern "C" {
 #include "bvm_eval.h"
 #include "bvm_function.h"
 
+#include "llvm_codegen.h"
+#include "llvm_function.h"
+
 #ifdef WITH_LLVM
 #include "llvm/llvm_engine.h"
 #include "llvm/llvm_function.h"
@@ -70,6 +73,7 @@ extern "C" {
 #include "util_debug.h"
 #include "util_map.h"
 #include "util_thread.h"
+#include "util_string.h"
 
 namespace blenvm {
 static mesh_ptr __empty_mesh__;
@@ -355,40 +359,28 @@ void BVM_function_bvm_cache_clear(void)
 BLI_INLINE blenvm::FunctionLLVM *_FUNC_LLVM(struct BVMFunction *fn)
 { return (blenvm::FunctionLLVM *)fn; }
 
-struct BVMFunction *BVM_function_llvm_cache_acquire(void *key)
-{
-	return (BVMFunction *)blenvm::function_llvm_cache_acquire(key);
-}
+static blenvm::spin_lock llvm_lock = blenvm::spin_lock();
 
-void BVM_function_llvm_cache_release(BVMFunction *fn)
+void BVM_function_llvm_release(BVMFunction *fn)
 {
+	llvm_lock.lock();
 	blenvm::function_llvm_cache_release(_FUNC_LLVM(fn));
-}
-
-void BVM_function_llvm_cache_set(void *key, BVMFunction *fn)
-{
-	blenvm::function_llvm_cache_set(key, _FUNC_LLVM(fn));
-}
-
-void BVM_function_llvm_cache_remove(void *key)
-{
-	blenvm::function_llvm_cache_remove(key);
-}
-
-void BVM_function_llvm_cache_clear(void)
-{
-	blenvm::function_llvm_cache_clear();
+	llvm_lock.unlock();
 }
 #else
-struct BVMFunction *BVM_function_llvm_cache_acquire(void */*key*/) { return NULL; }
-void BVM_function_llvm_cache_release(BVMFunction */*fn*/) {}
-void BVM_function_llvm_cache_set(void */*key*/, BVMFunction */*fn*/) {}
-void BVM_function_llvm_cache_remove(void */*key*/) {}
-void BVM_function_llvm_cache_clear(void) {}
+void BVM_function_llvm_release(BVMFunction */*fn*/) {}
 #endif
 
 /* ------------------------------------------------------------------------- */
 
+static blenvm::string get_ntree_unique_function_name(bNodeTree *ntree)
+{
+	std::stringstream ss;
+	ss << "nodetree_" << ntree;
+//	ss << ntree->id.name << "_" << ntree;
+	return ss.str();
+}
+
 static void parse_py_nodes(bNodeTree *btree, blenvm::NodeGraph *graph)
 {
 	PointerRNA ptr;
@@ -946,6 +938,35 @@ static void parse_tex_nodes(bNodeTree *btree, blenvm::NodeGraph *graph)
 	}
 }
 
+namespace blenvm {
+
+// TODO
+struct TexNodesResult {
+	float4 color;
+	float3 normal;
+};
+//typedef TexNodesResult (*TexNodesFunc)(float3 co, float3 dxt, float3 dyt, int cfra, int osatex);
+typedef void (*TexNodesFunc)(void);
+
+static void set_texresult(TexResult *result, const float4 &color, const float3 &normal)
+{
+	result->tr = color.x;
+	result->tg = color.y;
+	result->tb = color.z;
+	result->ta = color.w;
+	
+	result->tin = (result->tr + result->tg + result->tb) / 3.0f;
+	result->talpha = true;
+	
+	if (result->nor) {
+		result->nor[0] = normal.x;
+		result->nor[1] = normal.y;
+		result->nor[2] = normal.z;
+	}
+}
+
+}
+
 static void init_texture_graph(blenvm::NodeGraph &graph)
 {
 	using namespace blenvm;
@@ -962,7 +983,7 @@ static void init_texture_graph(blenvm::NodeGraph &graph)
 	graph.add_output("normal", "FLOAT3", N);
 }
 
-struct BVMFunction *BVM_gen_texture_function_bvm(bNodeTree *btree)
+struct BVMFunction *BVM_gen_texture_function_bvm(bNodeTree *btree, bool use_cache)
 {
 	using namespace blenvm;
 	
@@ -978,6 +999,38 @@ struct BVMFunction *BVM_gen_texture_function_bvm(bNodeTree *btree)
 	return (BVMFunction *)fn;
 }
 
+struct BVMFunction *BVM_gen_texture_function_llvm(bNodeTree *btree, bool use_cache)
+{
+#ifdef WITH_LLVM
+	using namespace blenvm;
+	
+	llvm_lock.lock();
+	
+	FunctionLLVM *fn = NULL;
+	if (use_cache) {
+		fn = function_llvm_cache_acquire(btree);
+	}
+	
+	if (!fn) {
+		NodeGraph graph;
+		init_texture_graph(graph);
+		parse_tex_nodes(btree, &graph);
+		graph.finalize();
+		
+		LLVMCompiler compiler;
+		fn = compiler.compile_function(get_ntree_unique_function_name(btree), graph);
+		
+		function_llvm_cache_set(btree, fn);
+	}
+	
+	llvm_lock.unlock();
+	
+	return (BVMFunction *)fn;
+#else
+	UNUSED_VARS(btree, use_cache);
+#endif
+}
+
 void BVM_debug_texture_nodes(bNodeTree *btree, FILE *debug_file, const char *label, BVMDebugMode mode)
 {
 	using namespace blenvm;
@@ -1019,19 +1072,32 @@ void BVM_eval_texture_bvm(struct BVMEvalContext *ctx, struct BVMFunction *fn,
 	
 	_FUNC_BVM(fn)->eval(_CTX(ctx), &globals, args, results);
 	
-	target->tr = color.x;
-	target->tg = color.y;
-	target->tb = color.z;
-	target->ta = color.w;
+	set_texresult(target, color, normal);
+}
+
+void BVM_eval_texture_llvm(struct BVMEvalContext *ctx, struct BVMFunction *fn,
+                           struct TexResult *target,
+                           float coord[3], float dxt[3], float dyt[3], int osatex,
+                           short UNUSED(which_output), int cfra, int UNUSED(preview))
+{
+	using namespace blenvm;
+	
+	EvalGlobals globals;
+	TexNodesResult result;
 	
-	target->tin = (target->tr + target->tg + target->tb) / 3.0f;
-	target->talpha = true;
+	UNUSED_VARS(globals);
+#ifdef WITH_LLVM
+	TexNodesFunc fp = (TexNodesFunc)_FUNC_LLVM(fn)->ptr();
 	
-	if (target->nor) {
-		target->nor[0] = normal.x;
-		target->nor[1] = normal.y;
-		target->nor[2] = normal.z;
-	}
+	// TODO
+//	result = fp(coord, dxt, dyt, cfra, osatex);
+#else
+	UNUSED_VARS(ctx, fn, coord, dxt, dyt, cfra, osatex);
+	result.color = float4(0.0f, 0.0f, 0.0f, 0.0f);
+	result.normal = float3(0.0f, 0.0f, 1.0f);
+#endif
+	
+	set_texresult(target, result.color, result.normal);
 }
 
 /* ------------------------------------------------------------------------- */
diff --git a/source/blender/blenvm/intern/function.cc b/source/blender/blenvm/intern/function.cc
index 8165fcc..c057e3e 100644
--- a/source/blender/blenvm/intern/function.cc
+++ b/source/blender/blenvm/intern/function.cc
@@ -33,21 +33,22 @@
 
 #include "function.h"
 
+#include "util_thread.h"
+
 namespace blenvm {
 
-mutex Function::users_mutex = mutex();
-spin_lock Function::users_lock = spin_lock(Function::users_mutex);
+static spin_lock users_lock = spin_lock();
 
-Function::Function() :
+FunctionBase::FunctionBase() :
     m_users(0)
 {
 }
 
-Function::~Function()
+FunctionBase::~FunctionBase()
 {
 }
 
-void Function::retain(Function *fn)
+void FunctionBase::retain(FunctionBase *fn)
 {
 	if (fn) {
 		users_lock.lock();
@@ -56,7 +57,7 @@ void Function::retain(Function *fn)
 	}
 }
 
-bool Function::release(Function *fn)
+bool FunctionBase::release(FunctionBase *fn)
 {
 	bool released = false;
 	if (fn) {
diff --git a/source/blender/blenvm/intern/function.h b/source/blender/blenvm/intern/function.h
index fc72f83..a9b6fa3 100644
--- a/source/blender/blenvm/intern/function.h
+++ b/source/blender/blenvm/intern/function.h
@@ -34,27 +34,22 @@
 
 #include "MEM_guardedalloc.h"
 
-#include "util_thread.h"
-
 namespace blenvm {
 
-struct Function {
-	Function();
-	~Function();
+st

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list