[Bf-blender-cvs] [ce25e3e5814] master: Cycles: Cleanup: Add general-purpose conversion between sin and cos

Lukas Stockner noreply at git.blender.org
Tue Jan 24 18:00:06 CET 2023


Commit: ce25e3e58145b703f5aeacc9f906b7ac2ca3c9c8
Author: Lukas Stockner
Date:   Tue Jan 24 17:33:10 2023 +0100
Branches: master
https://developer.blender.org/rBce25e3e58145b703f5aeacc9f906b7ac2ca3c9c8

Cycles: Cleanup: Add general-purpose conversion between sin and cos

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

M	intern/cycles/kernel/closure/bsdf_hair_principled.h
M	intern/cycles/kernel/closure/bsdf_microfacet.h
M	intern/cycles/kernel/closure/bsdf_microfacet_multi.h
M	intern/cycles/kernel/closure/volume.h
M	intern/cycles/kernel/geom/curve_intersect.h
M	intern/cycles/kernel/integrator/mnee.h
M	intern/cycles/kernel/integrator/subsurface_random_walk.h
M	intern/cycles/kernel/light/area.h
M	intern/cycles/kernel/light/tree.h
M	intern/cycles/kernel/light/triangle.h
M	intern/cycles/kernel/sample/mapping.h
M	intern/cycles/util/math.h

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

diff --git a/intern/cycles/kernel/closure/bsdf_hair_principled.h b/intern/cycles/kernel/closure/bsdf_hair_principled.h
index a9e8abe0475..f7cf3b716f6 100644
--- a/intern/cycles/kernel/closure/bsdf_hair_principled.h
+++ b/intern/cycles/kernel/closure/bsdf_hair_principled.h
@@ -41,11 +41,6 @@ static_assert(sizeof(ShaderClosure) >= sizeof(PrincipledHairBSDF),
 static_assert(sizeof(ShaderClosure) >= sizeof(PrincipledHairExtra),
               "PrincipledHairExtra is too large!");
 
-ccl_device_inline float cos_from_sin(const float s)
-{
-  return safe_sqrtf(1.0f - s * s);
-}
-
 /* Gives the change in direction in the normal plane for the given angles and p-th-order
  * scattering. */
 ccl_device_inline float delta_phi(int p, float gamma_o, float gamma_t)
diff --git a/intern/cycles/kernel/closure/bsdf_microfacet.h b/intern/cycles/kernel/closure/bsdf_microfacet.h
index 80c47bc9542..db53a29cfc3 100644
--- a/intern/cycles/kernel/closure/bsdf_microfacet.h
+++ b/intern/cycles/kernel/closure/bsdf_microfacet.h
@@ -200,7 +200,7 @@ ccl_device_forceinline float3 microfacet_sample_stretched(KernelGlobals kg,
 
   if (wi_.z < 0.99999f) {
     costheta_ = wi_.z;
-    sintheta_ = safe_sqrtf(1.0f - costheta_ * costheta_);
+    sintheta_ = sin_from_cos(costheta_);
 
     float invlen = 1.0f / sintheta_;
     cosphi_ = wi_.x * invlen;
diff --git a/intern/cycles/kernel/closure/bsdf_microfacet_multi.h b/intern/cycles/kernel/closure/bsdf_microfacet_multi.h
index 51fcd3340c6..defc46a389f 100644
--- a/intern/cycles/kernel/closure/bsdf_microfacet_multi.h
+++ b/intern/cycles/kernel/closure/bsdf_microfacet_multi.h
@@ -43,7 +43,7 @@ ccl_device_forceinline float2 mf_sampleP22_11(const float cosI,
     return make_float2(r * cosf(phi), r * sinf(phi));
   }
 
-  const float sinI = safe_sqrtf(1.0f - cosI * cosI);
+  const float sinI = sin_from_cos(cosI);
   const float tanI = sinI / cosI;
   const float projA = 0.5f * (cosI + 1.0f);
   if (projA < 0.0001f)
diff --git a/intern/cycles/kernel/closure/volume.h b/intern/cycles/kernel/closure/volume.h
index e6af2c01fcc..a9a28c2fa4a 100644
--- a/intern/cycles/kernel/closure/volume.h
+++ b/intern/cycles/kernel/closure/volume.h
@@ -88,7 +88,7 @@ henyey_greenstrein_sample(float3 D, float g, float randu, float randv, ccl_priva
     }
   }
 
-  float sin_theta = safe_sqrtf(1.0f - cos_theta * cos_theta);
+  float sin_theta = sin_from_cos(cos_theta);
   float phi = M_2PI_F * randv;
   float3 dir = make_float3(sin_theta * cosf(phi), sin_theta * sinf(phi), cos_theta);
 
diff --git a/intern/cycles/kernel/geom/curve_intersect.h b/intern/cycles/kernel/geom/curve_intersect.h
index 747d4ba4c3f..e8ce0c58c1e 100644
--- a/intern/cycles/kernel/geom/curve_intersect.h
+++ b/intern/cycles/kernel/geom/curve_intersect.h
@@ -720,7 +720,7 @@ ccl_device_inline void curve_shader_setup(KernelGlobals kg,
     const float3 tangent = normalize(dPdu);
     const float3 bitangent = normalize(cross(tangent, -D));
     const float sine = sd->v;
-    const float cosine = safe_sqrtf(1.0f - sine * sine);
+    const float cosine = cos_from_sin(sine);
 
     sd->N = normalize(sine * bitangent - cosine * normalize(cross(tangent, bitangent)));
 #  if 0
diff --git a/intern/cycles/kernel/integrator/mnee.h b/intern/cycles/kernel/integrator/mnee.h
index b6b07ad83a1..f50e3bc6e0c 100644
--- a/intern/cycles/kernel/integrator/mnee.h
+++ b/intern/cycles/kernel/integrator/mnee.h
@@ -704,9 +704,9 @@ ccl_device_forceinline bool mnee_compute_transfer_matrix(ccl_private const Shade
     float ilo = -eta * ilh;
 
     float cos_theta = dot(wo, m.n);
-    float sin_theta = safe_sqrtf(1.f - sqr(cos_theta));
+    float sin_theta = sin_from_cos(cos_theta);
     float cos_phi = dot(wo, s);
-    float sin_phi = safe_sqrtf(1.f - sqr(cos_phi));
+    float sin_phi = sin_from_cos(cos_phi);
 
     /* Wo = (cos_phi * sin_theta) * s + (sin_phi * sin_theta) * t + cos_theta * n. */
     float3 dH_dtheta = ilo * (cos_theta * (cos_phi * s + sin_phi * t) - sin_theta * m.n);
diff --git a/intern/cycles/kernel/integrator/subsurface_random_walk.h b/intern/cycles/kernel/integrator/subsurface_random_walk.h
index fdcb66c32f5..70e2920349f 100644
--- a/intern/cycles/kernel/integrator/subsurface_random_walk.h
+++ b/intern/cycles/kernel/integrator/subsurface_random_walk.h
@@ -136,7 +136,7 @@ ccl_device_forceinline float diffusion_length_dwivedi(float alpha)
 
 ccl_device_forceinline float3 direction_from_cosine(float3 D, float cos_theta, float randv)
 {
-  float sin_theta = safe_sqrtf(1.0f - cos_theta * cos_theta);
+  float sin_theta = sin_from_cos(cos_theta);
   float phi = M_2PI_F * randv;
   float3 dir = make_float3(sin_theta * cosf(phi), sin_theta * sinf(phi), cos_theta);
 
diff --git a/intern/cycles/kernel/light/area.h b/intern/cycles/kernel/light/area.h
index 9c0ca0c8a70..a4badf907a0 100644
--- a/intern/cycles/kernel/light/area.h
+++ b/intern/cycles/kernel/light/area.h
@@ -102,7 +102,7 @@ ccl_device float area_light_spread_attenuation(const float3 D,
     /* The factor M_PI_F comes from integrating the radiance over the hemisphere */
     return (cos_a > 0.9999997f) ? M_PI_F : 0.0f;
   }
-  const float sin_a = safe_sqrtf(1.0f - sqr(cos_a));
+  const float sin_a = sin_from_cos(cos_a);
   const float tan_a = sin_a / cos_a;
   return max((tan_half_spread - tan_a) * normalize_spread, 0.0f);
 }
diff --git a/intern/cycles/kernel/light/tree.h b/intern/cycles/kernel/light/tree.h
index 423879bcddc..441e9758088 100644
--- a/intern/cycles/kernel/light/tree.h
+++ b/intern/cycles/kernel/light/tree.h
@@ -47,11 +47,6 @@ ccl_device float light_tree_cos_bounding_box_angle(const BoundingBox bbox,
   return cos_theta_u;
 }
 
-ccl_device_forceinline float sin_from_cos(const float c)
-{
-  return safe_sqrtf(1.0f - sqr(c));
-}
-
 /* Compute vector v as in Fig .8. P_v is the corresponding point along the ray. */
 ccl_device float3 compute_v(
     const float3 centroid, const float3 P, const float3 D, const float3 bcone_axis, const float t)
diff --git a/intern/cycles/kernel/light/triangle.h b/intern/cycles/kernel/light/triangle.h
index 298a94566fc..b6724c6d6e1 100644
--- a/intern/cycles/kernel/light/triangle.h
+++ b/intern/cycles/kernel/light/triangle.h
@@ -218,7 +218,7 @@ ccl_device_forceinline bool triangle_light_sample(KernelGlobals kg,
     /* Finally, select a random point along the edge of the new triangle
      * That point on the spherical triangle is the sampled ray direction */
     const float z = 1.0f - randv * (1.0f - dot(C_, B));
-    ls->D = z * B + safe_sqrtf(1.0f - z * z) * safe_normalize(C_ - dot(C_, B) * B);
+    ls->D = z * B + sin_from_cos(z) * safe_normalize(C_ - dot(C_, B) * B);
 
     /* calculate intersection with the planar triangle */
     if (!ray_triangle_intersect(
diff --git a/intern/cycles/kernel/sample/mapping.h b/intern/cycles/kernel/sample/mapping.h
index 94e691aa1a8..1cd7bce11d2 100644
--- a/intern/cycles/kernel/sample/mapping.h
+++ b/intern/cycles/kernel/sample/mapping.h
@@ -67,17 +67,18 @@ ccl_device_inline void sample_uniform_cone(const float3 N,
                                            ccl_private float3 *wo,
                                            ccl_private float *pdf)
 {
-  float zMin = cosf(angle);
-  float z = zMin - zMin * randu + randu;
-  float r = safe_sqrtf(1.0f - sqr(z));
-  float phi = M_2PI_F * randv;
-  float x = r * cosf(phi);
-  float y = r * sinf(phi);
+  const float cosThetaMin = cosf(angle);
+  const float cosTheta = mix(cosThetaMin, 1.0f, randu);
+  const float sinTheta = sin_from_cos(cosTheta);
+  const float phi = M_2PI_F * randv;
+  const float x = sinTheta * cosf(phi);
+  const float y = sinTheta * sinf(phi);
+  const float z = cosTheta;
 
   float3 T, B;
   make_orthonormals(N, &T, &B);
   *wo = x * T + y * B + z * N;
-  *pdf = M_1_2PI_F / (1.0f - zMin);
+  *pdf = M_1_2PI_F / (1.0f - cosThetaMin);
 }
 
 ccl_device_inline float pdf_uniform_cone(const float3 N, float3 D, float angle)
diff --git a/intern/cycles/util/math.h b/intern/cycles/util/math.h
index 2eeb4aebd54..3618daa4ccb 100644
--- a/intern/cycles/util/math.h
+++ b/intern/cycles/util/math.h
@@ -750,6 +750,16 @@ ccl_device_inline float sqr(float a)
   return a * a;
 }
 
+ccl_device_inline float sin_from_cos(const float c)
+{
+  return safe_sqrtf(1.0f - sqr(c));
+}
+
+ccl_device_inline float cos_from_sin(const float s)
+{
+  return safe_sqrtf(1.0f - sqr(s));
+}
+
 ccl_device_inline float pow20(float a)
 {
   return sqr(sqr(sqr(sqr(a)) * a));



More information about the Bf-blender-cvs mailing list