[Bf-blender-cvs] [a963c7d48da] master: Code refactor: improve attribute handling for optional volume attributes.

Brecht Van Lommel noreply at git.blender.org
Fri Feb 23 19:02:20 CET 2018


Commit: a963c7d48dade70474ec3b40bea5e99ebdbbc5d4
Author: Brecht Van Lommel
Date:   Sun Feb 18 03:20:39 2018 +0100
Branches: master
https://developer.blender.org/rBa963c7d48dade70474ec3b40bea5e99ebdbbc5d4

Code refactor: improve attribute handling for optional volume attributes.

A volume shader should be able to request attributes, and still be rendered
as homogeneous if no volume attributes are available for the object.

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

M	intern/cycles/kernel/kernel_types.h
M	intern/cycles/kernel/kernel_volume.h
M	intern/cycles/render/attribute.cpp
M	intern/cycles/render/attribute.h
M	intern/cycles/render/graph.h
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.h
M	intern/cycles/render/object.cpp
M	intern/cycles/render/osl.cpp
M	intern/cycles/render/osl.h
M	intern/cycles/render/shader.cpp
M	intern/cycles/render/shader.h
M	intern/cycles/render/svm.cpp
M	intern/cycles/render/svm.h

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

diff --git a/intern/cycles/kernel/kernel_types.h b/intern/cycles/kernel/kernel_types.h
index 26b5a27807c..d967edca75d 100644
--- a/intern/cycles/kernel/kernel_types.h
+++ b/intern/cycles/kernel/kernel_types.h
@@ -925,19 +925,22 @@ enum ShaderDataFlag {
 	SD_HAS_DISPLACEMENT       = (1 << 26),
 	/* Has constant emission (value stored in __shader_flag) */
 	SD_HAS_CONSTANT_EMISSION  = (1 << 27),
+	/* Needs to access attributes */
+	SD_NEED_ATTRIBUTES        = (1 << 28),
 
 	SD_SHADER_FLAGS = (SD_USE_MIS |
 	                   SD_HAS_TRANSPARENT_SHADOW |
 	                   SD_HAS_VOLUME |
 	                   SD_HAS_ONLY_VOLUME |
-	                   SD_HETEROGENEOUS_VOLUME|
+	                   SD_HETEROGENEOUS_VOLUME |
 	                   SD_HAS_BSSRDF_BUMP |
 	                   SD_VOLUME_EQUIANGULAR |
 	                   SD_VOLUME_MIS |
 	                   SD_VOLUME_CUBIC |
 	                   SD_HAS_BUMP |
 	                   SD_HAS_DISPLACEMENT |
-	                   SD_HAS_CONSTANT_EMISSION)
+	                   SD_HAS_CONSTANT_EMISSION |
+	                   SD_NEED_ATTRIBUTES)
 };
 
 	/* Object flags. */
@@ -958,6 +961,8 @@ enum ShaderDataObjectFlag {
 	SD_OBJECT_HAS_VERTEX_MOTION      = (1 << 6),
 	/* object is used to catch shadows */
 	SD_OBJECT_SHADOW_CATCHER         = (1 << 7),
+	/* object has volume attributes */
+	SD_OBJECT_HAS_VOLUME_ATTRIBUTES  = (1 << 8),
 
 	SD_OBJECT_FLAGS = (SD_OBJECT_HOLDOUT_MASK |
 	                   SD_OBJECT_MOTION |
@@ -965,7 +970,8 @@ enum ShaderDataObjectFlag {
 	                   SD_OBJECT_NEGATIVE_SCALE_APPLIED |
 	                   SD_OBJECT_HAS_VOLUME |
 	                   SD_OBJECT_INTERSECTS_VOLUME |
-	                   SD_OBJECT_SHADOW_CATCHER)
+	                   SD_OBJECT_SHADOW_CATCHER |
+	                   SD_OBJECT_HAS_VOLUME_ATTRIBUTES)
 };
 
 typedef ccl_addr_space struct ShaderData {
diff --git a/intern/cycles/kernel/kernel_volume.h b/intern/cycles/kernel/kernel_volume.h
index 3274e05f98e..058e7dccafd 100644
--- a/intern/cycles/kernel/kernel_volume.h
+++ b/intern/cycles/kernel/kernel_volume.h
@@ -106,8 +106,21 @@ ccl_device bool volume_stack_is_heterogeneous(KernelGlobals *kg, ccl_addr_space
 	for(int i = 0; stack[i].shader != SHADER_NONE; i++) {
 		int shader_flag = kernel_tex_fetch(__shader_flag, (stack[i].shader & SHADER_MASK)*SHADER_SIZE);
 
-		if(shader_flag & SD_HETEROGENEOUS_VOLUME)
+		if(shader_flag & SD_HETEROGENEOUS_VOLUME) {
 			return true;
+		}
+		else if(shader_flag & SD_NEED_ATTRIBUTES) {
+			/* We want to render world or objects without any volume grids
+			 * as homogenous, but can only verify this at runtime since other
+			 * heterogenous volume objects may be using the same shader. */
+			int object = stack[i].object;
+			if(object != OBJECT_NONE) {
+				int object_flag = kernel_tex_fetch(__object_flag, object);
+				if(object_flag & SD_OBJECT_HAS_VOLUME_ATTRIBUTES) {
+					return true;
+				}
+			}
+		}
 	}
 
 	return false;
diff --git a/intern/cycles/render/attribute.cpp b/intern/cycles/render/attribute.cpp
index f959b1fef8b..6816f8ca3f3 100644
--- a/intern/cycles/render/attribute.cpp
+++ b/intern/cycles/render/attribute.cpp
@@ -298,9 +298,13 @@ const char *Attribute::standard_name(AttributeStandard std)
 
 AttributeStandard Attribute::name_standard(const char *name)
 {
-	for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
-		if(strcmp(name, Attribute::standard_name((AttributeStandard)std)) == 0)
-			return (AttributeStandard)std;
+	if(name) {
+		for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++) {
+			if(strcmp(name, Attribute::standard_name((AttributeStandard)std)) == 0) {
+				return (AttributeStandard)std;
+			}
+		}
+	}
 
 	return ATTR_STD_NONE;
 }
@@ -615,9 +619,11 @@ bool AttributeRequestSet::modified(const AttributeRequestSet& other)
 
 void AttributeRequestSet::add(ustring name)
 {
-	foreach(AttributeRequest& req, requests)
-		if(req.name == name)
+	foreach(AttributeRequest& req, requests) {
+		if(req.name == name) {
 			return;
+		}
+	}
 
 	requests.push_back(AttributeRequest(name));
 }
@@ -641,6 +647,22 @@ void AttributeRequestSet::add(AttributeRequestSet& reqs)
 	}
 }
 
+void AttributeRequestSet::add_standard(ustring name)
+{
+	if(!name) {
+		return;
+	}
+
+	AttributeStandard std = Attribute::name_standard(name.c_str());
+
+	if(std) {
+		add(std);
+	}
+	else {
+		add(name);
+	}
+}
+
 bool AttributeRequestSet::find(ustring name)
 {
 	foreach(AttributeRequest& req, requests)
diff --git a/intern/cycles/render/attribute.h b/intern/cycles/render/attribute.h
index d15ee401a72..9e23345675d 100644
--- a/intern/cycles/render/attribute.h
+++ b/intern/cycles/render/attribute.h
@@ -159,6 +159,7 @@ public:
 	void add(ustring name);
 	void add(AttributeStandard std);
 	void add(AttributeRequestSet& reqs);
+	void add_standard(ustring name);
 
 	bool find(ustring name);
 	bool find(AttributeStandard std);
diff --git a/intern/cycles/render/graph.h b/intern/cycles/render/graph.h
index 7ed292b5b96..2c134932b3c 100644
--- a/intern/cycles/render/graph.h
+++ b/intern/cycles/render/graph.h
@@ -156,6 +156,7 @@ public:
 	virtual bool has_bssrdf_bump() { return false; }
 	virtual bool has_spatial_varying() { return false; }
 	virtual bool has_object_dependency() { return false; }
+	virtual bool has_attribute_dependency() { return false; }
 	virtual bool has_integrator_dependency() { return false; }
 	virtual bool has_volume_support() { return false; }
 	virtual bool has_raytrace() { return false; }
diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp
index 56c9e669a5c..60c536f1ec1 100644
--- a/intern/cycles/render/nodes.cpp
+++ b/intern/cycles/render/nodes.cpp
@@ -208,7 +208,7 @@ NODE_DEFINE(ImageTextureNode)
 
 	TEXTURE_MAPPING_DEFINE(ImageTextureNode);
 
-	SOCKET_STRING(filename, "Filename", ustring(""));
+	SOCKET_STRING(filename, "Filename", ustring());
 
 	static NodeEnum color_space_enum;
 	color_space_enum.insert("none", NODE_COLOR_SPACE_NONE);
@@ -419,7 +419,7 @@ NODE_DEFINE(EnvironmentTextureNode)
 
 	TEXTURE_MAPPING_DEFINE(EnvironmentTextureNode);
 
-	SOCKET_STRING(filename, "Filename", ustring(""));
+	SOCKET_STRING(filename, "Filename", ustring());
 
 	static NodeEnum color_space_enum;
 	color_space_enum.insert("none", NODE_COLOR_SPACE_NONE);
@@ -1348,7 +1348,7 @@ NODE_DEFINE(PointDensityTextureNode)
 {
 	NodeType* type = NodeType::add("point_density_texture", create, NodeType::SHADER);
 
-	SOCKET_STRING(filename, "Filename", ustring(""));
+	SOCKET_STRING(filename, "Filename", ustring());
 
 	static NodeEnum space_enum;
 	space_enum.insert("object", NODE_TEX_VOXEL_SPACE_OBJECT);
@@ -3167,7 +3167,7 @@ NODE_DEFINE(UVMapNode)
 {
 	NodeType* type = NodeType::add("uvmap", create, NodeType::SHADER);
 
-	SOCKET_IN_STRING(attribute, "attribute", ustring(""));
+	SOCKET_STRING(attribute, "attribute", ustring());
 	SOCKET_IN_BOOLEAN(from_dupli, "from dupli", false);
 
 	SOCKET_OUT_POINT(UV, "UV");
@@ -4465,7 +4465,7 @@ NODE_DEFINE(AttributeNode)
 {
 	NodeType* type = NodeType::add("attribute", create, NodeType::SHADER);
 
-	SOCKET_STRING(attribute, "Attribute", ustring(""));
+	SOCKET_STRING(attribute, "Attribute", ustring());
 
 	SOCKET_OUT_COLOR(color, "Color");
 	SOCKET_OUT_VECTOR(vector, "Vector");
@@ -4486,16 +4486,12 @@ void AttributeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
 	ShaderOutput *fac_out = output("Fac");
 
 	if(!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty()) {
-		AttributeStandard std = Attribute::name_standard(attribute.c_str());
-
-		if(std != ATTR_STD_NONE)
-			attributes->add(std);
-		else
-			attributes->add(attribute);
+		attributes->add_standard(attribute);
 	}
 
-	if(shader->has_volume)
+	if(shader->has_volume) {
 		attributes->add(ATTR_STD_GENERATED_TRANSFORM);
+	}
 
 	ShaderNode::attributes(shader, attributes);
 }
@@ -4506,13 +4502,7 @@ void AttributeNode::compile(SVMCompiler& compiler)
 	ShaderOutput *vector_out = output("Vector");
 	ShaderOutput *fac_out = output("Fac");
 	ShaderNodeType attr_node = NODE_ATTR;
-	AttributeStandard std = Attribute::name_standard(attribute.c_str());
-	int attr;
-
-	if(std != ATTR_STD_NONE)
-		attr = compiler.attribute(std);
-	else
-		attr = compiler.attribute(attribute);
+	int attr = compiler.attribute_standard(attribute);;
 
 	if(bump == SHADER_BUMP_DX)
 		attr_node = NODE_ATTR_BUMP_DX;
@@ -5470,7 +5460,7 @@ NODE_DEFINE(NormalMapNode)
 	space_enum.insert("blender_world", NODE_NORMAL_MAP_BLENDER_WORLD);
 	SOCKET_ENUM(space, "Space", space_enum, NODE_TANGENT_RADIAL);
 
-	SOCKET_STRING(attribute, "Attribute", ustring(""));
+	SOCKET_STRING(attribute, "Attribute", ustring());
 
 	SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
 	SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
@@ -5489,7 +5479,7 @@ NormalMapNode::NormalMapNode()
 void NormalMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
 {
 	if(shader->has_surface && space == NODE_NORMAL_MAP_TANGENT) {
-		if(attribute == ustring("")) {
+		if(attribute.empty()) {
 			attributes->add(ATTR_STD_UV_TANGENT);
 			attributes->add(ATTR_STD_UV_TANGENT_SIGN);
 		}
@@ -5512,7 +5502,7 @@ void NormalMapNode::compile(SVMCompiler& compiler)
 	int attr = 0, attr_sign = 0;
 
 	if(space == NODE_NORMAL_MAP_TANGENT) {
-		if(attribute == ustring("")) {
+		if(attribute.empty()) {
 			attr = compiler.attribute(ATTR_STD_UV_TANGENT);
 			attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
 		}
@@ -5534,7 +5524,7 @@ void NormalMapNode::compile(SVMCompiler& compiler)
 void NormalMapNode::compile(OSLCompiler& compiler)
 {
 	if(space == NODE_NORMAL_MAP_TANGENT) {
-		if(attribute == ustring("")) {
+		if(attribute.empty()) {
 			compiler.parameter("attr_name", ustring("geom:tangent"));
 			compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
 		}
@@ -5565,7 +5555,7 @@ NODE_DEFINE(TangentNode)
 	axis_enum.insert("z", NODE_TANGENT_AXIS_Z);
 	SOCKET_ENUM(axis, "Axis", axis_enum, NODE_TANGENT_AXIS_X);
 
-	SOCKET_STRING(attribute, "Attribute", ustring(""));
+	SOCKET_STRING(attribute, "Attribute", ustring());
 
 	SOCKET_IN_NORMAL(normal_osl, "NormalIn", make_float3(0.0f, 0.0f, 0.0f), SocketType

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list