[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [52910] trunk/blender/intern/cycles/render : Fix #33485: cycles OSL now autodetects the presence of emission and transparent

Brecht Van Lommel brechtvanlommel at pandora.be
Wed Dec 12 07:51:07 CET 2012


Revision: 52910
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=52910
Author:   blendix
Date:     2012-12-12 06:51:06 +0000 (Wed, 12 Dec 2012)
Log Message:
-----------
Fix #33485: cycles OSL now autodetects the presence of emission and transparent
closures to enable multiple importance sampling and transparent shadows.

Modified Paths:
--------------
    trunk/blender/intern/cycles/render/graph.h
    trunk/blender/intern/cycles/render/nodes.h
    trunk/blender/intern/cycles/render/osl.cpp
    trunk/blender/intern/cycles/render/osl.h
    trunk/blender/intern/cycles/render/svm.cpp

Modified: trunk/blender/intern/cycles/render/graph.h
===================================================================
--- trunk/blender/intern/cycles/render/graph.h	2012-12-12 05:27:52 UTC (rev 52909)
+++ trunk/blender/intern/cycles/render/graph.h	2012-12-12 06:51:06 UTC (rev 52910)
@@ -183,6 +183,9 @@
 	virtual void compile(SVMCompiler& compiler) = 0;
 	virtual void compile(OSLCompiler& compiler) = 0;
 
+	virtual bool has_surface_emission() { return false; }
+	virtual bool has_surface_transparent() { return false; }
+
 	vector<ShaderInput*> inputs;
 	vector<ShaderOutput*> outputs;
 

Modified: trunk/blender/intern/cycles/render/nodes.h
===================================================================
--- trunk/blender/intern/cycles/render/nodes.h	2012-12-12 05:27:52 UTC (rev 52909)
+++ trunk/blender/intern/cycles/render/nodes.h	2012-12-12 06:51:06 UTC (rev 52910)
@@ -26,7 +26,7 @@
 CCL_NAMESPACE_BEGIN
 
 class ImageManager;
-class Shadr;
+class Shader;
 
 /* Texture Mapping */
 
@@ -220,6 +220,8 @@
 class TransparentBsdfNode : public BsdfNode {
 public:
 	SHADER_NODE_CLASS(TransparentBsdfNode)
+
+	bool has_surface_transparent() { return true; }
 };
 
 class VelvetBsdfNode : public BsdfNode {
@@ -255,6 +257,8 @@
 public:
 	SHADER_NODE_CLASS(EmissionNode)
 
+	bool has_surface_emission() { return true; }
+
 	bool total_power;
 };
 

Modified: trunk/blender/intern/cycles/render/osl.cpp
===================================================================
--- trunk/blender/intern/cycles/render/osl.cpp	2012-12-12 05:27:52 UTC (rev 52909)
+++ trunk/blender/intern/cycles/render/osl.cpp	2012-12-12 06:51:06 UTC (rev 52910)
@@ -76,12 +76,12 @@
 
 		if(progress.get_cancel()) return;
 
-		if(shader->sample_as_light && shader->has_surface_emission)
-			scene->light_manager->need_update = true;
-
 		OSLCompiler compiler((void*)this, (void*)ss, scene->image_manager);
 		compiler.background = (shader == scene->shaders[scene->default_background]);
 		compiler.compile(og, shader);
+
+		if(shader->sample_as_light && shader->has_surface_emission)
+			scene->light_manager->need_update = true;
 	}
 
 	/* setup shader engine */
@@ -202,10 +202,16 @@
 
 const char *OSLShaderManager::shader_test_loaded(const string& hash)
 {
-	set<string>::iterator it = loaded_shaders.find(hash);
-	return (it == loaded_shaders.end())? NULL: it->c_str();
+	map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
+	return (it == loaded_shaders.end())? NULL: it->first.c_str();
 }
 
+OSLShaderInfo *OSLShaderManager::shader_loaded_info(const string& hash)
+{
+	map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
+	return (it == loaded_shaders.end())? NULL: &it->second;
+}
+
 const char *OSLShaderManager::shader_load_filepath(string filepath)
 {
 	size_t len = filepath.size();
@@ -261,7 +267,8 @@
 
 	if(!path_read_text(filepath, bytecode)) {
 		fprintf(stderr, "Cycles shader graph: failed to read file %s\n", filepath.c_str());
-		loaded_shaders.insert(bytecode_hash); /* to avoid repeat tries */
+		OSLShaderInfo info;
+		loaded_shaders[bytecode_hash] = info; /* to avoid repeat tries */
 		return NULL;
 	}
 
@@ -306,7 +313,13 @@
 {
 	load_memory_shader(ss, hash.c_str(), bytecode.c_str());
 
-	return loaded_shaders.insert(hash).first->c_str();
+	/* this is a bit weak, but works */
+	OSLShaderInfo info;
+	info.has_surface_emission = (bytecode.find("\"emission\"") != string::npos);
+	info.has_surface_transparent = (bytecode.find("\"transparent\"") != string::npos);
+	loaded_shaders[hash] = info;
+
+	return loaded_shaders.find(hash)->first.c_str();
 }
 
 /* Graph Compiler */
@@ -477,6 +490,16 @@
 			ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
 		}
 	}
+
+	/* test if we shader contains specific closures */
+	OSLShaderInfo *info = ((OSLShaderManager*)manager)->shader_loaded_info(name);
+
+	if(info) {
+		if(info->has_surface_emission)
+			current_shader->has_surface_emission = true;
+		if(info->has_surface_transparent)
+			current_shader->has_surface_transparent = true;
+	}
 }
 
 void OSLCompiler::parameter(const char *name, float f)
@@ -632,9 +655,9 @@
 					node->compile(*this);
 					done.insert(node);
 
-					if(node->name == ustring("emission"))
+					if(node->has_surface_emission())
 						current_shader->has_surface_emission = true;
-					if(node->name == ustring("transparent"))
+					if(node->has_surface_transparent())
 						current_shader->has_surface_transparent = true;
 				}
 				else

Modified: trunk/blender/intern/cycles/render/osl.h
===================================================================
--- trunk/blender/intern/cycles/render/osl.h	2012-12-12 05:27:52 UTC (rev 52909)
+++ trunk/blender/intern/cycles/render/osl.h	2012-12-12 06:51:06 UTC (rev 52910)
@@ -45,6 +45,18 @@
 
 #ifdef WITH_OSL
 
+/* OSL Shader Info
+ * to auto detect closures in the shader for MIS and transparent shadows */
+
+struct OSLShaderInfo {
+	OSLShaderInfo()
+	: has_surface_emission(false), has_surface_transparent(false)
+	{}
+
+	bool has_surface_emission;
+	bool has_surface_transparent;
+};
+
 /* Shader Manage */
 
 class OSLShaderManager : public ShaderManager {
@@ -65,6 +77,7 @@
 	const char *shader_test_loaded(const string& hash);
 	const char *shader_load_bytecode(const string& hash, const string& bytecode);
 	const char *shader_load_filepath(string filepath);
+	OSLShaderInfo *shader_loaded_info(const string& hash);
 
 protected:
 	void texture_system_init();
@@ -74,7 +87,7 @@
 	OSL::TextureSystem *ts;
 	OSLRenderServices *services;
 	OSL::ErrorHandler errhandler;
-	set<string> loaded_shaders;
+	map<string, OSLShaderInfo> loaded_shaders;
 };
 
 #endif

Modified: trunk/blender/intern/cycles/render/svm.cpp
===================================================================
--- trunk/blender/intern/cycles/render/svm.cpp	2012-12-12 05:27:52 UTC (rev 52909)
+++ trunk/blender/intern/cycles/render/svm.cpp	2012-12-12 06:51:06 UTC (rev 52910)
@@ -478,9 +478,9 @@
 		stack_clear_users(node, done);
 		stack_clear_temporary(node);
 
-		if(node->name == ustring("emission"))
+		if(node->has_surface_emission())
 			current_shader->has_surface_emission = true;
-		if(node->name == ustring("transparent"))
+		if(node->has_surface_transparent())
 			current_shader->has_surface_transparent = true;
 
 		/* end node is added outside of this */
@@ -538,9 +538,9 @@
 
 		mix_weight_offset = SVM_STACK_INVALID;
 
-		if(node->name == ustring("emission"))
+		if(node->has_surface_emission())
 			current_shader->has_surface_emission = true;
-		if(node->name == ustring("transparent"))
+		if(node->has_surface_transparent())
 			current_shader->has_surface_transparent = true;
 	}
 




More information about the Bf-blender-cvs mailing list