[Bf-blender-cvs] [a7f6f90] master: Cycles: avoid making NaNs in Vector Math node by normalizing zero vectors.

Alexander Gavrilov noreply at git.blender.org
Tue Aug 9 12:21:04 CEST 2016


Commit: a7f6f900f373bda23d90e86a407e0d36fbdd8a03
Author: Alexander Gavrilov
Date:   Tue Aug 9 13:20:08 2016 +0300
Branches: master
https://developer.blender.org/rBa7f6f900f373bda23d90e86a407e0d36fbdd8a03

Cycles: avoid making NaNs in Vector Math node by normalizing zero vectors.

Since inputs are user controlled, the node can't assume they aren't zero.

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

M	intern/cycles/kernel/svm/svm_math_util.h
M	intern/cycles/util/util_math.h

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

diff --git a/intern/cycles/kernel/svm/svm_math_util.h b/intern/cycles/kernel/svm/svm_math_util.h
index 3f7d18a..6d13a0d 100644
--- a/intern/cycles/kernel/svm/svm_math_util.h
+++ b/intern/cycles/kernel/svm/svm_math_util.h
@@ -32,21 +32,17 @@ ccl_device void svm_vector_math(float *Fac, float3 *Vector, NodeVectorMath type,
 		*Fac = average_fac(*Vector);
 	}
 	else if(type == NODE_VECTOR_MATH_AVERAGE) {
-		*Fac = len(Vector1 + Vector2);
-		*Vector = normalize(Vector1 + Vector2);
+		*Vector = safe_normalize_len(Vector1 + Vector2, Fac);
 	}
 	else if(type == NODE_VECTOR_MATH_DOT_PRODUCT) {
 		*Fac = dot(Vector1, Vector2);
 		*Vector = make_float3(0.0f, 0.0f, 0.0f);
 	}
 	else if(type == NODE_VECTOR_MATH_CROSS_PRODUCT) {
-		float3 c = cross(Vector1, Vector2);
-		*Fac = len(c);
-		*Vector = normalize(c);
+		*Vector = safe_normalize_len(cross(Vector1, Vector2), Fac);
 	}
 	else if(type == NODE_VECTOR_MATH_NORMALIZE) {
-		*Fac = len(Vector1);
-		*Vector = normalize(Vector1);
+		*Vector = safe_normalize_len(Vector1, Fac);
 	}
 	else {
 		*Fac = 0.0f;
diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h
index 13aba06..89a882d 100644
--- a/intern/cycles/util/util_math.h
+++ b/intern/cycles/util/util_math.h
@@ -572,6 +572,12 @@ ccl_device_inline float3 safe_normalize(const float3 a)
 	return (t != 0.0f)? a/t: a;
 }
 
+ccl_device_inline float3 safe_normalize_len(const float3 a, float *t)
+{
+	*t = len(a);
+	return (*t != 0.0f)? a/(*t): a;
+}
+
 #ifndef __KERNEL_OPENCL__
 
 ccl_device_inline bool operator==(const float3 a, const float3 b)




More information about the Bf-blender-cvs mailing list