Commit: 1643dc5b88644b8efba82884f8ffdfbb6654f745
Author: Brecht Van Lommel
Date:   Sun Jul 15 15:51:17 2018 +0200
Branches: soc-2018-hair-shader
https://developer.blender.org/rB1643dc5b88644b8efba82884f8ffdfbb6654f745

Cleanup: code style fixes, tweak comments and variable naming.

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

M       intern/cycles/kernel/closure/bsdf_hair_principled.h
M       intern/cycles/kernel/kernel_compat_opencl.h
M       intern/cycles/kernel/osl/osl_closures.cpp
M       intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
M       intern/cycles/kernel/svm/svm_closure.h
M       intern/cycles/render/nodes.cpp
M       intern/cycles/render/nodes.h
M       source/blender/blenkernel/BKE_node.h
M       source/blender/nodes/NOD_static_types.h
M       source/blender/nodes/shader/nodes/node_shader_bsdf_hair_principled.c

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

diff --git a/intern/cycles/kernel/closure/bsdf_hair_principled.h 
b/intern/cycles/kernel/closure/bsdf_hair_principled.h
index fc08eeaeee8..71f6f4d16c8 100644
--- a/intern/cycles/kernel/closure/bsdf_hair_principled.h
+++ b/intern/cycles/kernel/closure/bsdf_hair_principled.h
@@ -67,8 +67,12 @@ ccl_device_inline float delta_phi(int p, float gamma_o, 
float gamma_t)
 /* Remaps the given angle to [-pi, pi]. */
 ccl_device_inline float wrap_angle(float a)
 {
-       while(a > M_PI_F) a -= M_2PI_F;
-       while(a < -M_PI_F) a += M_2PI_F;
+       while(a > M_PI_F) {
+               a -= M_2PI_F;
+       }
+       while(a < -M_PI_F) {
+               a += M_2PI_F;
+       }
        return a;
 }
 
@@ -83,9 +87,13 @@ ccl_device_inline float logistic(float x, float s)
 ccl_device_inline float logistic_cdf(float x, float s)
 {
        float arg = -x/s;
-       // exp overflows if arg >= 89.0f
-       if(arg > 88.0f) return 0.0f;
-       return 1.0f / (1.0f + expf(arg));
+       /* expf() overflows if arg >= 89.0. */
+       if(arg > 88.0f) {
+               return 0.0f;
+       }
+       else {
+               return 1.0f / (1.0f + expf(arg));
+       }
 }
 
 /* Numerical approximation to the Bessel function of the first kind. */
@@ -99,7 +107,9 @@ ccl_device_inline float bessel_I0(float x)
        for(int i = 2; i < 10; i++) {
                i_fac_2 *= i*i;
                float newval = val + pow_x_2i / (pow_4_i * i_fac_2);
-               if(val == newval) return val;
+               if(val == newval) {
+                       return val;
+               }
                val = newval;
                pow_x_2i *= x;
                pow_4_i *= 4;
@@ -111,8 +121,8 @@ ccl_device_inline float bessel_I0(float x)
 ccl_device_inline float log_bessel_I0(float x)
 {
        if (x > 12.0f) {
-               // log(1/x) == -log(x) iff x > 0.
-               // This is only used with positive cosines
+               /* log(1/x) == -log(x) iff x > 0.
+                * This is only used with positive cosines */
                return x + 0.5f * (1.f / (8.0f * x) - M_LN_2PI_F - logf(x));
        }
        else {
@@ -193,7 +203,7 @@ ccl_device int bsdf_principled_hair_setup(ShaderData *sd, 
PrincipledHairBSDF *bs
        bsdf->v = sqr(0.726f*bsdf->v + 0.812f*sqr(bsdf->v) + 
3.700f*pow20(bsdf->v));
        bsdf->s =    (0.265f*bsdf->s + 1.194f*sqr(bsdf->s) + 
5.372f*pow22(bsdf->s))*M_SQRT_PI_8_F;
        bsdf->m0_roughness = sqr(0.726f*bsdf->m0_roughness + 
0.812f*sqr(bsdf->m0_roughness) + 3.700f*pow20(bsdf->m0_roughness));
-       
+
        /* Compute local frame, aligned to curve tangent and ray direction. */
        float3 X = safe_normalize(sd->dPdu);
        float3 Y = safe_normalize(cross(X, sd->I));
@@ -412,13 +422,19 @@ ccl_device int bsdf_principled_hair_sample(KernelGlobals 
*kg, const ShaderClosur
 
        int p = 0;
        for(; p < 3; p++) {
-               if(u[0].x < Ap[p].w) break;
+               if(u[0].x < Ap[p].w) {
+                       break;
+               }
                u[0].x -= Ap[p].w;
        }
 
        float v = bsdf->v;
-       if(p == 1) v *= 0.25f;
-       if(p >= 2) v *= 4.0f;
+       if(p == 1) {
+               v *= 0.25f;
+       }
+       if(p >= 2) {
+               v *= 4.0f;
+       }
 
        u[1].x = max(u[1].x, 1e-5f);
        float fac = 1.0f + v*logf(u[1].x + (1.0f - u[1].x)*expf(-2.0f/v));
diff --git a/intern/cycles/kernel/kernel_compat_opencl.h 
b/intern/cycles/kernel/kernel_compat_opencl.h
index 8cb4a57d5b9..3f7e264fbee 100644
--- a/intern/cycles/kernel/kernel_compat_opencl.h
+++ b/intern/cycles/kernel/kernel_compat_opencl.h
@@ -123,7 +123,7 @@
 #define fmaxf(x, y) fmax(((float)(x)), ((float)(y)))
 #define fminf(x, y) fmin(((float)(x)), ((float)(y)))
 #define fmodf(x, y) fmod((float)(x), (float)(y))
-#define sinhf(x)sinh(((float)(x)))
+#define sinhf(x) sinh(((float)(x)))
 
 #ifndef __CL_USE_NATIVE__
 #  define sinf(x) native_sin(((float)(x)))
diff --git a/intern/cycles/kernel/osl/osl_closures.cpp 
b/intern/cycles/kernel/osl/osl_closures.cpp
index 00c9d9198ae..e8a720ad91d 100644
--- a/intern/cycles/kernel/osl/osl_closures.cpp
+++ b/intern/cycles/kernel/osl/osl_closures.cpp
@@ -4,7 +4,7 @@
  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
  * All Rights Reserved.
  *
- * Modifications Copyright 2011, 2018, Blender Foundation.
+ * Modifications Copyright 2011-2018, Blender Foundation.
  * 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
diff --git a/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl 
b/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
index 907d2f190db..1004e545c08 100644
--- a/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
+++ b/intern/cycles/kernel/shaders/node_principled_hair_bsdf.osl
@@ -16,17 +16,21 @@
 
 #include "stdosl.h"
 
-color log3 (color a) {
+color log3(color a)
+{
        return color(log(a[0]), log(a[1]), log(a[2]));
 }
 
-color sigma_from_concentration(float Eumelanin, float Pheomelanin) {
-       return Eumelanin*color(0.506, 0.841, 1.653) + Pheomelanin*color(0.343, 
0.733, 1.924);
+color sigma_from_concentration(float eumelanin, float pheomelanin)
+{
+       return eumelanin*color(0.506, 0.841, 1.653) + pheomelanin*color(0.343, 
0.733, 1.924);
 }
 
-color sigma_from_reflectance(color Color, float AzimuthalRoughness) {
-       float roughness_fac = (((((0.245*AzimuthalRoughness) + 
5.574)*AzimuthalRoughness - 10.73)*AzimuthalRoughness + 
2.532)*AzimuthalRoughness - 0.215)*AzimuthalRoughness + 5.969;
-       return log3(Color)/roughness_fac;
+color sigma_from_reflectance(color c, float azimuthal_roughness)
+{
+       float roughness_fac = (((((0.245*azimuthal_roughness) + 
5.574)*azimuthal_roughness - 10.73)*azimuthal_roughness + 
2.532)*azimuthal_roughness - 0.215)*azimuthal_roughness + 5.969;
+       color sigma = log3(c) / roughness_fac;
+       return sigma * sigma;
 }
 
 shader node_principled_hair_bsdf(
@@ -49,7 +53,7 @@ shader node_principled_hair_bsdf(
 
        output closure color BSDF = 0)
 {
-       color sigma;
+       /* Get random value from curve in none is specified. */
        float random_value = 0.0;
 
        if (AttrRandom != "none") {
@@ -59,36 +63,44 @@ shader node_principled_hair_bsdf(
                random_value = Random;
        }
 
-       float factor_random_color = 1.0 + 2.0*(random_value - 0.5)*RandomColor;
+       /* Compute roughness. */
        float factor_random_roughness = 1.0 + 2.0*(random_value - 
0.5)*RandomRoughness;
        float m0_roughness = 1.0 - clamp(Coat, 0.0, 1.0);
+       float roughness = Roughness*factor_random_roughness;
+       float radial_roughness = RadialRoughness*factor_random_roughness;
 
-       float adjusted_roughness = Roughness*factor_random_roughness;
-       float adjusted_radial_roughness = 
RadialRoughness*factor_random_roughness;
-       float melanin_qty = -log(max(1.0 - Melanin*factor_random_color, 
0.0001));
-       float adjusted_eumelanin = melanin_qty*(1.0-MelaninRedness);
-       float adjusted_pheomelanin = melanin_qty*MelaninRedness;
+       /* Compute absorption. */
+       color sigma;
 
        if (parametrization == "Absorption coefficient") {
                sigma = AbsorptionCoefficient;
        }
        else if (parametrization == "Melanin concentration") {
-               color melanin_sigma = 
sigma_from_concentration(adjusted_eumelanin, adjusted_pheomelanin);
-               color tint_sigma = sigma_from_reflectance(Tint, 
adjusted_radial_roughness);
-               tint_sigma *= tint_sigma;
+               /* Randomize melanin. */
+               float factor_random_color = 1.0 + 2.0*(random_value - 0.5) * 
RandomColor;
+               float melanin = Melanin * factor_random_color;
+
+               /* Map melanin 0..inf from more perceptually linear 0..1. */
+               melanin = -log(max(1.0 - melanin, 0.0001));
+
+               /* Benedikt Bitterli's melanin ratio remapping. */
+               float eumelanin = melanin * (1.0 - MelaninRedness);
+               float pheomelanin = melanin * MelaninRedness;
+               color melanin_sigma = sigma_from_concentration(eumelanin, 
pheomelanin);
+
+               /* Optional tint. */
+               color tint_sigma = sigma_from_reflectance(Tint, 
radial_roughness);
                sigma = melanin_sigma + tint_sigma;
        }
        else if (parametrization == "Direct coloring"){
-               sigma = sigma_from_reflectance(Color, 
adjusted_radial_roughness);
-               sigma *= sigma;
+               sigma = sigma_from_reflectance(Color, radial_roughness);
        }
        else {
-               // Falling back to Benedikt Bitterli's brownish hair with 
Tungsten (via PHEOmelanin concentration)
-               // This gives the exact amount set as default above
+               /* Fallback to brownish hair, same as defaults for melanin. */
                sigma = sigma_from_concentration(0.0, 0.8054375);
        }
 
        //printf("Info: color %f, incoming eumelanin %f, incoming pheomelanin 
%f, incoming sigma %f, incoming color range %f, incoming normal %f, 
parametrization %s, resulting sigma %f, Longitudinal %f, Azimuthal %f, 
roughness range %f, Scale deviation %f, IOR %f\n", Color, Melanin, 
MelaninRedness, AbsorptionCoefficient, Normal, parametrization, sigma, 
RandomColor, Roughness, RadialRoughness, RandomRoughness, Offset, IOR);
 
-       BSDF = principled_hair(Normal, sigma, adjusted_roughness, 
adjusted_radial_roughness, m0_roughness, Offset, IOR);
+       BSDF = principled_hair(Normal, sigma, roughness, radial_roughness, 
m0_roughness, Offset, IOR);
 }
diff --git a/intern/cycles/kernel/svm/svm_closure.h 
b/intern/cycles/kernel/svm/svm_closure.h
index 9df3a74d7be..8404fb8715d 100644
--- a/intern/cycles/kernel/svm/svm_closure.h
+++ b/intern/cycles/kernel/svm/svm_closure.h
@@ -16,7 +16,7 @@
 
 CCL_NAMESPACE_BEGIN
 
-/* Helper functions */
+/* Hair Melanin */
 
 ccl_device_inline float3 sigma_from_concentration(float eumelanin, float 
pheomelanin)
 {
@@ -26,7 +26,8 @@ ccl_device_inline float3 sigma_from_concentration(float 
eumelanin, float pheomel
 ccl_device_inline float3 sigma_from_reflectance(float3 color, float 
azimuthal_roughness)
 {
        float roughness_fac = (((((0.245f*azimuthal_roughness) + 
5.574f)*azimuthal_roughness - 10.73f)*azimuthal_roughness + 
2.532f)*azimuthal_roughness - 0.215f)*azimuthal_roughness + 5.969f;
-       return log3(color) / roughness_fac;
+       float3 sigma = log3(color) / roughness_fac;
+       return sigma * sigma;
 }
 
 /* Closure Nodes */
@@ -256,7 +257,7 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, 
ShaderData *sd, float *
                                        float3 spec_weight = weight * 
specular_weight;
 
                                        MicrofacetBsdf *bsdf = 
(MicrofacetBsdf*)bsdf_alloc(sd, sizeof(MicrofacetBsdf), spec_weight);
-                                       if(!bsdf){
+                                       if(!bsdf) {
                                                break;
                                        }
 
@@ -746,21 +747,12 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, 
ShaderData *sd, float *
                        decode_node_uchar4(data_node.y, &offset_ofs, &ior_ofs, 
&color_ofs, &parametrization);
                        float alpha = (stack_valid(offset_ofs))? 
stack_load_float(stack, offset_ofs): __uint_as_float(data_node.z);
                        float ior = (stack_valid(ior_ofs))? 
stack_load_float(stack, ior_ofs): __uint_as_float(data_node.w);
-                       float3 color = stack_load_float3(s

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to