[Bf-blender-cvs] [23d92659c7e] soc-2018-hair-shader: Automagically get a RNG even if the user doesn't socket Random

L. E. Segovia noreply at git.blender.org
Sun Jun 17 18:02:33 CEST 2018


Commit: 23d92659c7e9a603f40e121f671ca35b101a1831
Author: L. E. Segovia
Date:   Sun Jun 17 16:02:02 2018 +0000
Branches: soc-2018-hair-shader
https://developer.blender.org/rB23d92659c7e9a603f40e121f671ca35b101a1831

Automagically get a RNG even if the user doesn't socket Random

Note that, after this change, **only setting Randomization to 0 will disable it**.

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

M	intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
M	intern/cycles/kernel/svm/svm_closure.h
M	intern/cycles/render/graph.cpp
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.h

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

diff --git a/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl b/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
index 5cba4f00ec8..307162fc165 100644
--- a/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
+++ b/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
@@ -35,14 +35,26 @@ shader node_principled_hair_bsdf(
 	float RoughnessRandomization = 0.0,
 	float UndercoatRoughness = 1.0,
 	float IOR = 1.55,
+	string AttrRandom = "none",
 	float Random = 0.0,
 
 	output closure color BSDF = 0)
 {
 	color Sigma;
 
-	float factor_random_color = (1.0-ColorRandomization) + Random*ColorRandomization;
-	float factor_random_roughness = (1.0-RoughnessRandomization) + Random*RoughnessRandomization;
+	float RandomValue = 0.0;
+	printf("Attribute: %s\n", AttrRandom);
+
+	if (AttrRandom != "none") {
+		printf("asdasd");
+		getattribute(AttrRandom, RandomValue);
+	}
+	else {
+		RandomValue = Random;
+	}
+
+	float factor_random_color = 1.0 + 2.0*(RandomValue - 0.5)*ColorRandomization;
+	float factor_random_roughness = 1.0 + 2.0*(RandomValue - 0.5)*RoughnessRandomization;
 
 	float AdjustedRoughness = Roughness*factor_random_roughness;
 	float AdjustedRadialRoughness = RadialRoughness*factor_random_roughness;
diff --git a/intern/cycles/kernel/svm/svm_closure.h b/intern/cycles/kernel/svm/svm_closure.h
index f5b82a5200d..6389cfccf5f 100644
--- a/intern/cycles/kernel/svm/svm_closure.h
+++ b/intern/cycles/kernel/svm/svm_closure.h
@@ -725,6 +725,7 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *
 		case CLOSURE_BSDF_HAIR_PRINCIPLED_ID: {
 			uint4 data_node2 = read_node(kg, offset);
 			uint4 data_node3 = read_node(kg, offset);
+			uint4 data_node4 = read_node(kg, offset);
 
 			float3 weight = sd->svm_closure_weight * mix_weight;
 
@@ -743,11 +744,19 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *
 			
 			uint tint_ofs, random_ofs, color_randomization_ofs, roughness_randomization_ofs;
 			decode_node_uchar4(data_node3.x, &tint_ofs, &random_ofs, &color_randomization_ofs, &roughness_randomization_ofs);
-			float random = (stack_valid(random_ofs)) ? stack_load_float(stack, random_ofs) : __uint_as_float(data_node3.y);
 			float color_randomization = (stack_valid(color_randomization_ofs)) ? stack_load_float(stack, color_randomization_ofs) : __uint_as_float(data_node3.z);
 			color_randomization = clamp(color_randomization, 0.0f, 1.0f);
 			float roughness_randomization = (stack_valid(roughness_randomization_ofs)) ? stack_load_float(stack, roughness_randomization_ofs) : __uint_as_float(data_node3.w);
 
+			const AttributeDescriptor attr_descr_random = find_attribute(kg, sd, data_node4.y);
+			float random = 0.0f;
+			if (attr_descr_random.offset != ATTR_STD_NOT_FOUND) {
+				random = primitive_attribute_float(kg, sd, attr_descr_random, NULL, NULL);
+			}
+			else {
+				random = (stack_valid(random_ofs)) ? stack_load_float(stack, random_ofs) : __uint_as_float(data_node3.y);
+			}
+
 			float factor_random_color = 1.0f + 2.0f*(random - 0.5f)*color_randomization;
 			float factor_random_roughness = 1.0f + 2.0f*(random - 0.5f)*roughness_randomization;
 
diff --git a/intern/cycles/render/graph.cpp b/intern/cycles/render/graph.cpp
index de46463aff2..7b4d2f2db5f 100644
--- a/intern/cycles/render/graph.cpp
+++ b/intern/cycles/render/graph.cpp
@@ -1092,7 +1092,7 @@ int ShaderGraph::get_num_closures()
 			num_closures += VOLUME_STACK_SIZE;
 		}
 		else if(closure_type == CLOSURE_BSDF_HAIR_PRINCIPLED_ID) {
-			num_closures += 3;
+			num_closures += 4;
 		}
 		else {
 			++num_closures;
diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp
index f69ce974a46..b497f37fcc2 100644
--- a/intern/cycles/render/nodes.cpp
+++ b/intern/cycles/render/nodes.cpp
@@ -3080,6 +3080,15 @@ PrincipledHairBsdfNode::PrincipledHairBsdfNode()
 	closure = CLOSURE_BSDF_HAIR_PRINCIPLED_ID;
 }
 
+void PrincipledHairBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
+{
+	socketedRandomSource = input("Random")->link;
+	if(!socketedRandomSource) {
+		attributes->add(ATTR_STD_CURVE_RANDOM);
+	}
+	ShaderNode::attributes(shader, attributes);
+}
+
 void PrincipledHairBsdfNode::compile(SVMCompiler& compiler)
 {
 	compiler.add_node(NODE_CLOSURE_SET_WEIGHT, make_float3(1.0f, 1.0f, 1.0f));
@@ -3099,6 +3108,7 @@ void PrincipledHairBsdfNode::compile(SVMCompiler& compiler)
 	int absorption_coefficient_ofs = compiler.stack_assign(input("Absorption Coefficient"));
 
 	ShaderInput *random_in = input("Random");
+	int attr_random = socketedRandomSource ? SVM_STACK_INVALID : compiler.attribute(ATTR_STD_CURVE_RANDOM);
 
 	compiler.add_node(NODE_CLOSURE_BSDF,
 		compiler.encode_uchar4(closure,
@@ -3126,7 +3136,7 @@ void PrincipledHairBsdfNode::compile(SVMCompiler& compiler)
 		__float_as_uint(primary_reflection_roughness),
 		__float_as_uint(eumelanin),
 		__float_as_uint(pheomelanin));
-	
+
 	compiler.add_node(
 		compiler.encode_uchar4(
 			tint_ofs,
@@ -3136,10 +3146,23 @@ void PrincipledHairBsdfNode::compile(SVMCompiler& compiler)
 		__float_as_uint(random),
 		__float_as_uint(color_randomization),
 		__float_as_uint(roughness_randomization));
+
+	compiler.add_node(
+		compiler.encode_uchar4(
+			SVM_STACK_INVALID,
+			SVM_STACK_INVALID,
+			SVM_STACK_INVALID,
+			SVM_STACK_INVALID),
+		attr_random,
+		SVM_STACK_INVALID,
+		SVM_STACK_INVALID);
 }
 
 void PrincipledHairBsdfNode::compile(OSLCompiler& compiler)
 {
+	if (!socketedRandomSource) {
+		compiler.parameter("AttrRandom", "geom:curve_random");
+	}
 	compiler.parameter(this, "parametrization");
 	compiler.add(this, "node_principled_hair_bsdf");
 }
diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h
index de431ac8f4a..8c9d73dd42f 100644
--- a/intern/cycles/render/nodes.h
+++ b/intern/cycles/render/nodes.h
@@ -605,6 +605,7 @@ public:
 class PrincipledHairBsdfNode : public BsdfBaseNode {
 public:
 	SHADER_NODE_CLASS(PrincipledHairBsdfNode)
+	void attributes(Shader *shader, AttributeRequestSet *attributes);
 
 	float roughness_u;
 	float roughness_v;
@@ -622,6 +623,7 @@ public:
 	float3 normal;
 	float surface_mix_weight;
 	float random;
+	bool socketedRandomSource;
 	NodePrincipledHairParametrization parametrization;
 };



More information about the Bf-blender-cvs mailing list