Cycles: avoid making NaNs in Vector Math node by normalizing zero vectors.
authorAlexander Gavrilov <angavrilov@gmail.com>
Tue, 9 Aug 2016 10:20:08 +0000 (13:20 +0300)
committerAlexander Gavrilov <angavrilov@gmail.com>
Tue, 9 Aug 2016 10:20:22 +0000 (13:20 +0300)
Since inputs are user controlled, the node can't assume they aren't zero.

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

index 3f7d18a02fe3c0d87f03a7091799b28dfcaab3c5..6d13a0d8e0291ca86087b513207ceafe3802f5bb 100644 (file)
@@ -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;
index 13aba0646d27630849b8655f1226785a0fb86c2d..89a882d9b9dd5430f9c1682070966256c90f3fed 100644 (file)
@@ -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)