[Bf-blender-cvs] [c987f6e6e10] upstream_cycles_texture_cache: Cycles: improved texture differentials for bump maps, added texture differntials to displacement maps

Stefan Werner noreply at git.blender.org
Wed Jun 27 14:05:18 CEST 2018


Commit: c987f6e6e10660ebbb2632ad2a8e0cbc209f82d8
Author: Stefan Werner
Date:   Wed Aug 9 22:06:29 2017 +0200
Branches: upstream_cycles_texture_cache
https://developer.blender.org/rBc987f6e6e10660ebbb2632ad2a8e0cbc209f82d8

Cycles: improved texture differentials for bump maps, added texture differntials to displacement maps

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

M	intern/cycles/kernel/closure/volume.h
M	intern/cycles/kernel/kernel_shader.h
M	intern/cycles/kernel/kernel_subsurface.h
M	intern/cycles/render/graph.cpp

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

diff --git a/intern/cycles/kernel/closure/volume.h b/intern/cycles/kernel/closure/volume.h
index 31e617d6bfe..39e352381eb 100644
--- a/intern/cycles/kernel/closure/volume.h
+++ b/intern/cycles/kernel/closure/volume.h
@@ -83,7 +83,7 @@ ccl_device float3 volume_henyey_greenstein_eval_phase(const ShaderClosure *sc, c
 	return make_float3(*pdf, *pdf, *pdf);
 }
 
-ccl_device float3 henyey_greenstrein_sample(float3 D, float g, float randu, float randv, float *pdf)
+ccl_device float3 henyey_greenstrein_sample(float3 D, float g, float randu, float randv, float *pdf, float3 dIdx, float3 dIdy, float3 *domega_in_dx, float3 *domega_in_dy)
 {
 	/* match pdf for small g */
 	float cos_theta;
@@ -111,6 +111,17 @@ ccl_device float3 henyey_greenstrein_sample(float3 D, float g, float randu, floa
 	make_orthonormals(D, &T, &B);
 	dir = dir.x * T + dir.y * B + dir.z * D;
 
+#ifdef __RAY_DIFFERENTIALS__
+	if(domega_in_dx && domega_in_dy) {
+		float cos_phi = cosf(phi);
+		float sin_phi = sinf(phi);
+		make_orthonormals(D - dIdx, &T, &B);
+		*domega_in_dx = sin_theta * cos_phi * T + sin_theta * sin_phi * B + cos_theta * (D - dIdx) - dir;
+		make_orthonormals(D - dIdy, &T, &B);
+		*domega_in_dy = sin_theta * cos_phi * T + sin_theta * sin_phi * B + cos_theta * (D - dIdy) - dir;
+	}
+#endif
+
 	return dir;
 }
 
@@ -121,16 +132,9 @@ ccl_device int volume_henyey_greenstein_sample(const ShaderClosure *sc, float3 I
 	float g = volume->g;
 
 	/* note that I points towards the viewer and so is used negated */
-	*omega_in = henyey_greenstrein_sample(-I, g, randu, randv, pdf);
+	*omega_in = henyey_greenstrein_sample(-I, g, randu, randv, pdf, dIdx, dIdy, domega_in_dx, domega_in_dy);
 	*eval = make_float3(*pdf, *pdf, *pdf); /* perfect importance sampling */
-
-#ifdef __RAY_DIFFERENTIALS__
-	make_orthonormals(-I-dIdx, &T, &B);
-	*domega_in_dx = sin_theta * cos_phi * T + sin_theta * sin_phi * B + cos_theta * (-I-dIdx) - *omega_in;
-	make_orthonormals(-I-dIdy, &T, &B);
-	*domega_in_dy = sin_theta * cos_phi * T + sin_theta * sin_phi * B + cos_theta * (-I-dIdy) - *omega_in ;
-#endif
-
+	
 	return LABEL_VOLUME_SCATTER;
 }
 
diff --git a/intern/cycles/kernel/kernel_shader.h b/intern/cycles/kernel/kernel_shader.h
index b6d4d9b2407..5a27913d4c3 100644
--- a/intern/cycles/kernel/kernel_shader.h
+++ b/intern/cycles/kernel/kernel_shader.h
@@ -382,7 +382,7 @@ ccl_device_inline void shader_setup_from_sample(KernelGlobals *kg,
 #  endif
 #endif
 	}
-	if(sd->type & PRIMITIVE_LAMP) {
+	else if(sd->type & PRIMITIVE_LAMP) {
 #ifdef __DPDU__
 		lamp_light_dPdudv(kg, lamp, sd->u, sd->v, &sd->dPdu, &sd->dPdv);
 #endif
@@ -424,7 +424,7 @@ ccl_device_inline void shader_setup_from_sample(KernelGlobals *kg,
 #ifdef __RAY_DIFFERENTIALS__
 	if(dI) {
 		sd->dI = *dI;
-		differential_transfer(&sd->dP, differential3_zero(), I, *dI, Ng, t);
+		differential_transfer(&sd->dP, sd->dP, I, *dI, Ng, t);
 		differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
 	}
 	else {
@@ -446,12 +446,16 @@ ccl_device void shader_setup_from_displace(KernelGlobals *kg, ShaderData *sd,
 	int shader;
 
 	triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
+	triangle_dPdudv(kg, prim, &sd->dP.dx, &sd->dP.dy);
 
 	/* force smooth shading for displacement */
 	shader |= SHADER_SMOOTH_NORMAL;
 
+	I = -Ng;
+	differential3 dI = differential3_zero();
+
 	shader_setup_from_sample(kg, sd,
-	                         P, Ng, I, NULL,
+	                         P, Ng, I, &dI,
 	                         shader, object, prim,
 	                         u, v, 0.0f, 0.5f,
 	                         !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
diff --git a/intern/cycles/kernel/kernel_subsurface.h b/intern/cycles/kernel/kernel_subsurface.h
index e8553d84547..f8cf4ccfebf 100644
--- a/intern/cycles/kernel/kernel_subsurface.h
+++ b/intern/cycles/kernel/kernel_subsurface.h
@@ -428,7 +428,7 @@ ccl_device_noinline bool subsurface_random_walk(
 			const float anisotropy = 0.0f;
 			float scatter_u, scatter_v;
 			path_state_rng_2D(kg, state, PRNG_BSDF_U, &scatter_u, &scatter_v);
-			ray->D = henyey_greenstrein_sample(ray->D, anisotropy, scatter_u, scatter_v, NULL);
+			ray->D = henyey_greenstrein_sample(ray->D, anisotropy, scatter_u, scatter_v, NULL, make_float3(0.0f, 0.0f, 0.0f), make_float3(0.0f, 0.0f, 0.0f), NULL, NULL);
 		}
 
 		/* Sample color channel, use MIS with balance heuristic. */
diff --git a/intern/cycles/render/graph.cpp b/intern/cycles/render/graph.cpp
index bc3f227d44e..0554aa74bd4 100644
--- a/intern/cycles/render/graph.cpp
+++ b/intern/cycles/render/graph.cpp
@@ -923,14 +923,37 @@ void ShaderGraph::add_differentials()
 			copy_nodes(nodes_vector, nodes_dx);
 			copy_nodes(nodes_vector, nodes_dy);
 
-			/* mark nodes to indicate they are used for differential computation, so
-			 that any texture coordinates are shifted by dx/dy when sampling */
+			/* First: Nodes that have no bump are set to center, others are left untouched. */
 			foreach(ShaderNode *node, nodes_vector)
-				node->bump = SHADER_BUMP_CENTER;
-			foreach(NodePair& pair, nodes_dx)
-				pair.second->bump = SHADER_BUMP_DX;
-			foreach(NodePair& pair, nodes_dy)
-				pair.second->bump = SHADER_BUMP_DY;
+				node->bump = node->bump == SHADER_BUMP_NONE ? SHADER_BUMP_CENTER : node->bump;
+
+			/* Second: Nodes that have no bump are set DX, others are shifted by one. */
+			foreach(NodePair& pair, nodes_dx) {
+				switch(pair.second->bump) {
+					case SHADER_BUMP_DX:
+						pair.second->bump = SHADER_BUMP_DY;
+						break;
+					case SHADER_BUMP_DY:
+						pair.second->bump = SHADER_BUMP_CENTER;
+						break;
+					default:
+						pair.second->bump = SHADER_BUMP_DX;
+				}
+			}
+			
+			/* Second: Nodes that have no bump are set DY, others are shifted by two. */
+			foreach(NodePair& pair, nodes_dy) {
+				switch(pair.second->bump) {
+					case SHADER_BUMP_DX:
+						pair.second->bump = SHADER_BUMP_CENTER;
+						break;
+					case SHADER_BUMP_DY:
+						pair.second->bump = SHADER_BUMP_DX;
+						break;
+					default:
+						pair.second->bump = SHADER_BUMP_DY;
+				}
+			}
 
 			ShaderOutput *out = vector_input->link;
 			ShaderOutput *out_dx = nodes_dx[out->parent]->output(out->name());



More information about the Bf-blender-cvs mailing list