[Bf-blender-cvs] [cd72396] master: Cycles: Optimization for black world backgrounds

Thomas Dinges noreply at git.blender.org
Wed Jan 21 20:16:38 CET 2015


Commit: cd723967970e3330d5461eaf8a062d6321de5d4f
Author: Thomas Dinges
Date:   Wed Jan 21 20:06:53 2015 +0100
Branches: master
https://developer.blender.org/rBcd723967970e3330d5461eaf8a062d6321de5d4f

Cycles: Optimization for black world backgrounds

* If a Background node is set to a black color or zero strength,
it now gets removed from the shader graph.

* In case the graph is empty (no background node), the kernel will skip
evaluating it and save some rendertime. This can help quite a bit in scenes,
where the majority of the image consists of a black background.

Example: http://www.pasteall.org/pic/show.php?id=82650
In this case the render is ~16% faster.

Differential Revision: https://developer.blender.org/D972

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

M	intern/cycles/render/background.cpp
M	intern/cycles/render/graph.cpp
M	intern/cycles/render/graph.h
M	intern/cycles/render/nodes.cpp

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

diff --git a/intern/cycles/render/background.cpp b/intern/cycles/render/background.cpp
index d731b33..f5e51f2 100644
--- a/intern/cycles/render/background.cpp
+++ b/intern/cycles/render/background.cpp
@@ -72,16 +72,23 @@ void Background::device_update(Device *device, DeviceScene *dscene, Scene *scene
 	else
 		kbackground->volume_shader = SHADER_NONE;
 
-	if(!(visibility & PATH_RAY_DIFFUSE))
-		kbackground->surface_shader |= SHADER_EXCLUDE_DIFFUSE;
-	if(!(visibility & PATH_RAY_GLOSSY))
-		kbackground->surface_shader |= SHADER_EXCLUDE_GLOSSY;
-	if(!(visibility & PATH_RAY_TRANSMIT))
-		kbackground->surface_shader |= SHADER_EXCLUDE_TRANSMIT;
-	if(!(visibility & PATH_RAY_VOLUME_SCATTER))
-		kbackground->surface_shader |= SHADER_EXCLUDE_SCATTER;
-	if(!(visibility & PATH_RAY_CAMERA))
-		kbackground->surface_shader |= SHADER_EXCLUDE_CAMERA;
+	/* No background node, make world shader invisible to all rays, to skip evaluation in kernel. */
+	if(scene->shaders[shader]->graph->nodes.size() <= 1) {
+		kbackground->surface_shader |= SHADER_EXCLUDE_ANY;
+	}
+	/* Background present, check visibilities */
+	else {
+		if(!(visibility & PATH_RAY_DIFFUSE))
+			kbackground->surface_shader |= SHADER_EXCLUDE_DIFFUSE;
+		if(!(visibility & PATH_RAY_GLOSSY))
+			kbackground->surface_shader |= SHADER_EXCLUDE_GLOSSY;
+		if(!(visibility & PATH_RAY_TRANSMIT))
+			kbackground->surface_shader |= SHADER_EXCLUDE_TRANSMIT;
+		if(!(visibility & PATH_RAY_VOLUME_SCATTER))
+			kbackground->surface_shader |= SHADER_EXCLUDE_SCATTER;
+		if(!(visibility & PATH_RAY_CAMERA))
+			kbackground->surface_shader |= SHADER_EXCLUDE_CAMERA;
+	}
 
 	need_update = false;
 }
diff --git a/intern/cycles/render/graph.cpp b/intern/cycles/render/graph.cpp
index 8f8a693..84fe55b 100644
--- a/intern/cycles/render/graph.cpp
+++ b/intern/cycles/render/graph.cpp
@@ -374,6 +374,28 @@ void ShaderGraph::remove_unneeded_nodes()
 			removed[proxy->id] = true;
 			any_node_removed = true;
 		}
+		else if(node->special_type == SHADER_SPECIAL_TYPE_BACKGROUND) {
+			BackgroundNode *bg = static_cast<BackgroundNode*>(node);
+
+			if(bg->outputs[0]->links.size()) {
+				/* Black color or zero strength, remove node */
+				if((!bg->inputs[0]->link && bg->inputs[0]->value == make_float3(0.0f, 0.0f, 0.0f)) ||
+				   (!bg->inputs[1]->link && bg->inputs[1]->value.x == 0.0f)) {
+					vector<ShaderInput*> inputs = bg->outputs[0]->links;
+
+					foreach(ShaderInput *sock, bg->inputs) {
+						if(sock->link)
+							disconnect(sock);
+					}
+
+					foreach(ShaderInput *input, inputs)
+						disconnect(input);
+
+					removed[bg->id] = true;
+					any_node_removed = true;
+				}
+			}
+		}
 		else if(node->special_type == SHADER_SPECIAL_TYPE_MIX_CLOSURE) {
 			MixClosureNode *mix = static_cast<MixClosureNode*>(node);
 
diff --git a/intern/cycles/render/graph.h b/intern/cycles/render/graph.h
index 9130f7f..f9ac167 100644
--- a/intern/cycles/render/graph.h
+++ b/intern/cycles/render/graph.h
@@ -79,7 +79,8 @@ enum ShaderNodeSpecialType {
 	SHADER_SPECIAL_TYPE_MIX_RGB, /* Only Mix subtype */
 	SHADER_SPECIAL_TYPE_AUTOCONVERT,
 	SHADER_SPECIAL_TYPE_GEOMETRY,
-	SHADER_SPECIAL_TYPE_SCRIPT
+	SHADER_SPECIAL_TYPE_SCRIPT,
+	SHADER_SPECIAL_TYPE_BACKGROUND,
 };
 
 /* Enum
diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp
index 625b829..653bd5d 100644
--- a/intern/cycles/render/nodes.cpp
+++ b/intern/cycles/render/nodes.cpp
@@ -1946,6 +1946,8 @@ void EmissionNode::compile(OSLCompiler& compiler)
 BackgroundNode::BackgroundNode()
 : ShaderNode("background")
 {
+	special_type = SHADER_SPECIAL_TYPE_BACKGROUND;
+
 	add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
 	add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
 	add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);




More information about the Bf-blender-cvs mailing list