From 7b873b06627d5c56f443d5634d6064e118657082 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 8 Nov 2014 13:35:21 +0100 Subject: 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 --- intern/cycles/render/light.cpp | 11 ++++------- intern/cycles/subd/subd_dice.cpp | 4 ++-- intern/cycles/util/util_math.h | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/intern/cycles/render/light.cpp b/intern/cycles/render/light.cpp index a129a0fdec5..8d1cec10187 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 05ff5ca4b65..6bd18d08ba0 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& outer, vector& 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 c332e1709db..78005546a01 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__ -- cgit v1.2.3