[Bf-blender-cvs] [7b873b0] master: Add safe_normalize to cycles, avoid checking length first

Campbell Barton noreply at git.blender.org
Sat Nov 8 13:37:35 CET 2014


Commit: 7b873b06627d5c56f443d5634d6064e118657082
Author: Campbell Barton
Date:   Sat Nov 8 13:35:21 2014 +0100
Branches: master
https://developer.blender.org/rB7b873b06627d5c56f443d5634d6064e118657082

Add safe_normalize to cycles, avoid checking length first

This won't give any big speedup,
just avoids redundant sqrtf and may be useful in future.

Differential Revision: https://developer.blender.org/D880

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

M	intern/cycles/render/light.cpp
M	intern/cycles/subd/subd_dice.cpp
M	intern/cycles/util/util_math.h

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

diff --git a/intern/cycles/render/light.cpp b/intern/cycles/render/light.cpp
index a129a0f..8d1cec1 100644
--- a/intern/cycles/render/light.cpp
+++ b/intern/cycles/render/light.cpp
@@ -536,9 +536,8 @@ void LightManager::device_update_points(Device *device, DeviceScene *dscene, Sce
 			float area = M_PI_F*radius*radius;
 			float invarea = (area > 0.0f)? 1.0f/area: 1.0f;
 			float3 dir = light->dir;
-			
-			if(len(dir) > 0.0f)
-				dir = normalize(dir);
+
+			dir = safe_normalize(dir);
 
 			if(light->use_mis && area > 0.0f)
 				shader_id |= SHADER_USE_MIS;
@@ -585,8 +584,7 @@ void LightManager::device_update_points(Device *device, DeviceScene *dscene, Sce
 			float invarea = (area > 0.0f)? 1.0f/area: 1.0f;
 			float3 dir = light->dir;
 			
-			if(len(dir) > 0.0f)
-				dir = normalize(dir);
+			dir = safe_normalize(dir);
 
 			if(light->use_mis && area > 0.0f)
 				shader_id |= SHADER_USE_MIS;
@@ -606,8 +604,7 @@ void LightManager::device_update_points(Device *device, DeviceScene *dscene, Sce
 			float spot_smooth = (1.0f - spot_angle)*light->spot_smooth;
 			float3 dir = light->dir;
 			
-			if(len(dir) > 0.0f)
-				dir = normalize(dir);
+			dir = safe_normalize(dir);
 
 			if(light->use_mis && radius > 0.0f)
 				shader_id |= SHADER_USE_MIS;
diff --git a/intern/cycles/subd/subd_dice.cpp b/intern/cycles/subd/subd_dice.cpp
index 05ff5ca..6bd18d0 100644
--- a/intern/cycles/subd/subd_dice.cpp
+++ b/intern/cycles/subd/subd_dice.cpp
@@ -117,8 +117,8 @@ void EdgeDice::stitch_triangles(Patch *patch, vector<int>& outer, vector<int>& i
 		}
 		else {
 			/* length of diagonals */
-			float len1 = len(mesh_P[inner[i]] - mesh_P[outer[j+1]]);
-			float len2 = len(mesh_P[outer[j]] - mesh_P[inner[i+1]]);
+			float len1 = len_squared(mesh_P[inner[i]] - mesh_P[outer[j+1]]);
+			float len2 = len_squared(mesh_P[outer[j]] - mesh_P[inner[i+1]]);
 
 			/* use smallest diagonal */
 			if(len1 < len2)
diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h
index c332e17..7800554 100644
--- a/intern/cycles/util/util_math.h
+++ b/intern/cycles/util/util_math.h
@@ -314,6 +314,12 @@ ccl_device_inline float2 normalize_len(const float2 a, float *t)
 	return a/(*t);
 }
 
+ccl_device_inline float2 safe_normalize(const float2 a)
+{
+	float t = len(a);
+	return (t)? a/t: a;
+}
+
 ccl_device_inline bool operator==(const float2 a, const float2 b)
 {
 	return (a.x == b.x && a.y == b.y);
@@ -510,6 +516,12 @@ ccl_device_inline float3 normalize_len(const float3 a, float *t)
 	return a/(*t);
 }
 
+ccl_device_inline float3 safe_normalize(const float3 a)
+{
+	float t = len(a);
+	return (t)? a/t: a;
+}
+
 #ifndef __KERNEL_OPENCL__
 
 ccl_device_inline bool operator==(const float3 a, const float3 b)
@@ -817,6 +829,12 @@ ccl_device_inline float4 normalize(const float4 a)
 	return a/len(a);
 }
 
+ccl_device_inline float4 safe_normalize(const float4 a)
+{
+	float t = len(a);
+	return (t)? a/t: a;
+}
+
 ccl_device_inline float4 min(float4 a, float4 b)
 {
 #ifdef __KERNEL_SSE__




More information about the Bf-blender-cvs mailing list