70static_assert(
sizeof(ShaderClosure) >=
sizeof(
HuangHairBSDF),
"HuangHairBSDF is too large!");
71static_assert(
sizeof(ShaderClosure) >=
sizeof(
HuangHairExtra),
"HuangHairExtra is too large!");
145 return atan2f(
b * sin_gamma, cos_gamma);
167 float sin_gamma, cos_gamma;
170 return -sin_cos_phi_i.
y * sin_gamma +
b * sin_cos_phi_i.
x * cos_gamma;
187 float sin_gamma, cos_gamma;
189 return safe_divide(1.0f, sincos_phi_i.
y * cos_gamma +
b * sincos_phi_i.
x * sin_gamma);
220 const float tan_gamma = sin_gamma / cos_gamma;
221 const float tan_phi =
b * tan_gamma;
230 return e2 == 0 ? 1.0f :
sqrtf(1.0f - e2 *
sqr(
sinf(gamma)));
235 return bsdf->extra->radius > bsdf->extra->pixel_coverage;
244 const uint32_t path_flag)
248 bsdf->roughness =
clamp(bsdf->roughness, 0.001f, 1.0f);
251 bsdf->tilt = -bsdf->tilt;
270 if (bsdf->aspect_ratio > 1.0f) {
272 bsdf->aspect_ratio = 1.0f / bsdf->aspect_ratio;
292 const float3 I =
to_local(sd->wi, bsdf->N, bsdf->extra->Y, bsdf->extra->Z);
294 bsdf->extra->e2 = 1.0f -
sqr(bsdf->aspect_ratio);
295 bsdf->extra->radius = bsdf->extra->e2 == 0 ?
300 if (
fabsf(bsdf->h) >= bsdf->extra->radius) {
303 sd->num_closure_left += 2;
320 const bool inv_table = (ior < 1.0f);
322 const float z =
sqrtf(
fabsf((ior - 1.0f) / (ior + 1.0f)));
349 return (
dot(v, h) > 0.0f &&
dot(v, m) > 0.0f);
366 return (1.0f + lambdaI) / (1.0f + lambdaI + lambdaO);
376 if (bsdf->extra->R <= 0.0f) {
381 const float b = bsdf->aspect_ratio;
385 const float roughness = bsdf->roughness;
386 const float roughness2 =
sqr(roughness);
391 float res = roughness * 0.7f;
393 const float h_range = bsdf->extra->h.length();
396 const size_t intervals = 2 * (size_t)
ceilf(h_range / res * 0.5f);
399 res = h_range /
float(intervals);
402 float integral = 0.0f;
403 for (
size_t i = 0; i <= intervals; i++) {
404 const float h = bsdf->extra->h.min + i * res;
409 const float jacobian =
d_gamma_d_h(sincos_phi_i, gamma_m,
b);
410 const float weight = ((i == 0 || i == intervals) ? 0.5f : (i % 2 + 1)) * jacobian;
411 const float cos_mi =
dot(wm, wi);
420 integral *= (2.0f / 3.0f * res);
432 const float T_avg =
max(1.0f -
R, 1e-5f);
447 if (bsdf->extra->TT <= 0.0f && bsdf->extra->TRT <= 0.0f) {
452 const float b = bsdf->aspect_ratio;
455 const float eta = bsdf->eta;
456 const float inv_eta = 1.0f / eta;
458 const float roughness = bsdf->roughness;
459 const float roughness2 =
sqr(roughness);
460 const float sqrt_roughness =
sqrtf(roughness);
464 float res = roughness * 0.8f;
465 const float h_range = bsdf->extra->h.length();
466 const size_t intervals = 2 * (size_t)
ceilf(h_range / res * 0.5f);
467 res = h_range / intervals;
472 for (
size_t i = 0; i <= intervals; i++) {
473 const float h = bsdf->extra->h.min + i * res;
484 const float cos_hi1 =
dot(wi, wh1);
485 if (!(cos_hi1 > 0.0f)) {
489 const float cos_mi1 =
dot(wi, wmi);
496 const float phi_t =
dir_phi(wt);
497 const float gamma_mt = 2.0f *
to_phi(phi_t,
b) - gamma_mi;
501 const float cos_mo1 =
dot(-wt, wmi);
502 const float cos_mi2 =
dot(-wt, wmt);
503 const float G1o =
bsdf_Go(roughness2, cos_mi1, cos_mo1);
508 const float jacobian =
d_gamma_d_h(sincos_phi_i, gamma_mi,
b);
509 const float weight = ((i == 0 || i == intervals) ? 0.5f : (i % 2 + 1)) * jacobian;
513 2.0f *
cosf(gamma_mi - phi_t) :
519 if (bsdf->extra->TT > 0.0f) {
520 if (
dot(wo, wt) >= inv_eta - 1e-5f) {
521 float3 wh2 = -wt + inv_eta * wo;
522 const float rcp_norm_wh2 = 1.0f /
len(wh2);
524 const float cos_mh2 =
dot(wmt, wh2);
525 if (cos_mh2 >= 0.0f) {
526 const float cos_hi2 =
dot(-wt, wh2);
527 const float cos_ho2 =
dot(-wo, wh2);
528 const float cos_mo2 =
dot(-wo, wmt);
534 const Spectrum result = weight *
T1 * scale1 *
T2 * D2 * G1o * G2 * A_t / cos_mo1 *
535 cos_mi1 * cos_hi2 * cos_ho2 *
sqr(rcp_norm_wh2);
538 S_tt += bsdf->extra->TT * result *
arc_length(bsdf->extra->e2, gamma_mt);
545 if (bsdf->extra->TRT > 0.0f) {
550 const float cos_hi2 =
dot(-wt, wh2);
551 if (!(cos_hi2 > 0.0f)) {
557 if (
dot(-wtr, wo) < inv_eta - 1e-5f) {
567 const float phi_tr =
dir_phi(wtr);
572 float3 wh3 = wtr + inv_eta * wo;
573 const float rcp_norm_wh3 = 1.0f /
len(wh3);
575 const float cos_mh3 =
dot(wmtr, wh3);
583 const float cos_hi3 =
dot(wh3, wtr);
584 const float cos_ho3 =
dot(wh3, -wo);
585 const float cos_mi3 =
dot(wmtr, wtr);
596 const float cos_mo2 =
dot(wmt, -wtr);
597 const float G2o =
bsdf_Go(roughness2, cos_mi2, cos_mo2);
600 const Spectrum result = weight *
T1 * scale1 * R2 * scale2 *
T3 * D3 * G1o * G2o * G3 * A_t *
601 A_tr / (cos_mo1 * cos_mo2) * cos_mi1 * cos_mi2 * cos_hi3 * cos_ho3 *
605 S_trt += bsdf->extra->TRT * result *
arc_length(bsdf->extra->e2, gamma_mtr);
619 const float simpson_coeff = 2.0f / 3.0f * res;
621 return ((S_tt + S_trt) *
sqr(inv_eta) + S_trrt *
M *
N *
M_2_PI_F) * simpson_coeff;
636 *sampled_roughness =
make_float2(roughness, roughness);
641 float sample_lobe = rand.
x;
642 const float sample_h = rand.
y;
650 const float3 wi = bsdf->extra->wi;
653 const float b = bsdf->aspect_ratio;
656 const float h_div_r =
is_nearfield(bsdf) ? bsdf->h / bsdf->extra->radius :
657 (sample_h * 2.0f - 1.0f);
668 const float cos_mi1 =
dot(wmi, wi);
670 if (cos_mi1 < 0.0f ||
dot(wmi_, wi) < 0.0f) {
677 const float roughness2 =
sqr(roughness);
678 const float sqrt_roughness =
sqrtf(roughness);
695 const float inv_eta = 1.0f / bsdf->eta;
697 const float phi_t =
dir_phi(wt);
699 const float gamma_mt = 2.0f *
to_phi(phi_t,
b) - gamma_mi;
715 const float cos_mi2 =
dot(-wt, wmt);
721 2.0f *
cosf(phi_t - gamma_mi) :
726 const float T1 = (1.0f - R1) * scale1 *
bsdf_Go(roughness2, cos_mi1,
dot(wmi, -wt));
727 const float T2 = 1.0f - R2;
733 TT = bsdf->extra->TT *
T1 * A_t *
T2 * scale2 *
bsdf_Go(roughness2, cos_mi2,
dot(wmt, -wtt));
737 const float phi_tr =
dir_phi(wtr);
739 wmtr =
sphg_dir(-bsdf->tilt, gamma_mtr,
b);
741 wh3 =
sample_wh(roughness, wtr, wmtr, sample_h3);
748 const float cos_mi3 =
dot(wmtr, wtr);
749 if (cos_mi3 > 0.0f) {
759 const float T3 = 1.0f - R3;
763 bsdf_Go(roughness2, cos_mi3,
dot(wmtr, -wtrt));
772 const float fac = 1.0f +
773 4.0f * bsdf->roughness *
774 logf(rand_theta + (1.0f - rand_theta) *
expf(-0.5f / bsdf->roughness));
775 const float sin_theta_o = -fac *
sin_theta(wi) +
787 wtrrt =
make_float3(sin_phi_o * cos_theta_o, sin_theta_o, cos_phi_o * cos_theta_o);
793 const float T_avg =
max(0.5f * (
T2 +
T3), 1e-5f);
803 const float trt =
average(TRT);
804 const float trrt =
average(TRRT);
805 const float total_energy = r + tt + trt + trrt;
807 if (total_energy == 0.0f) {
814 sample_lobe *= total_energy;
815 if (sample_lobe < r) {
819 else if (sample_lobe < (r + tt)) {
821 *eval = TT / tt * total_energy;
823 else if (sample_lobe < (r + tt + trt)) {
825 *eval = TRT / trt * total_energy;
833 *wo =
to_global(local_O, bsdf->N, bsdf->extra->Y, bsdf->extra->Z);
853 const float3 local_I = bsdf->extra->wi;
854 const float3 local_O =
to_local(wo, bsdf->N, bsdf->extra->Y, bsdf->extra->Z);
860 const float tan_tilt =
tanf(bsdf->tilt);
861 if (tan_tilt *
tan_theta(local_O) < -1.0f) {
871 const float r = bsdf->extra->radius;
872 const float b = bsdf->aspect_ratio;
876 phi_to_h(phi_i - half_span,
b, local_I)};
886 const float half_pixel = bsdf->extra->pixel_coverage;
890 dh = nearfield_h.
length() / r;
908 const float projected_area =
cos_theta(local_I) * dh;
ccl_device_forceinline float3 microfacet_ggx_sample_vndf(const float3 wi, const float alpha_x, const float alpha_y, const float2 rand)
ccl_device_forceinline float mu_to_y_index(const float mu)
ccl_device_inline float bsdf_D(const float alpha2, const float cos_NH)
ccl_device_inline float bsdf_lambda(const float alpha2, const float cos_N)
ccl_device_inline float bsdf_G(const float alpha2, const float cos_N)
ccl_device_inline float longitudinal_scattering(float sin_theta_i, const float cos_theta_i, const float sin_theta_o, const float cos_theta_o, const float v)
ccl_device float2 sincos_phi(const float3 w)
ccl_device_inline float cos_theta(const float3 w)
ccl_device_inline float d_gamma_d_h(const float2 sincos_phi_i, const float gamma, const float b)
ccl_device_inline float arc_length(const float e2, const float gamma)
ccl_device Spectrum bsdf_hair_huang_albedo(const ccl_private ShaderData *, const ccl_private ShaderClosure *sc)
ccl_device Spectrum bsdf_hair_huang_eval_residual(KernelGlobals kg, const ccl_private ShaderClosure *sc, const float3 wi, const float3 wo, ccl_private uint *rng_quadrature)
ccl_device_inline bool is_nearfield(const ccl_private HuangHairBSDF *bsdf)
ccl_device_inline float bsdf_Go(const float alpha2, const float cos_NI, const float cos_NO)
ccl_device Spectrum bsdf_hair_huang_eval_r(KernelGlobals kg, const ccl_private ShaderClosure *sc, const float3 wi, const float3 wo)
ccl_device_inline float phi_to_h(const float phi, const float b, const float3 wi)
ccl_device_inline float3 sphg_dir(const float theta, const float gamma, const float b)
ccl_device_inline float2 to_point(const float gamma, const float b)
ccl_device_inline float to_gamma(const float phi, const float b)
ccl_device Spectrum bsdf_hair_huang_eval_trrt(const float T, const float R, const Spectrum A)
ccl_device_inline bool microfacet_visible(const float3 v, const float3 m, const float3 h)
ccl_device_inline float2 dir_sph(const float3 w)
ccl_device_inline float tan_theta(const float3 w)
ccl_device void bsdf_hair_huang_blur(ccl_private ShaderClosure *sc, const float roughness)
ccl_device_forceinline float bsdf_hair_huang_energy_scale(KernelGlobals kg, const float mu, const float rough, const float ior)
ccl_device float sin_phi(const float3 w)
ccl_device_inline float3 sample_wh(const float roughness, const float3 wi, const float3 wm, const float2 rand)
ccl_device_inline float dir_phi(const float3 w)
ccl_device int bsdf_hair_huang_sample(const KernelGlobals kg, const ccl_private ShaderClosure *sc, ccl_private ShaderData *sd, const float3 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness)
ccl_device Spectrum bsdf_hair_huang_eval(KernelGlobals kg, ccl_private ShaderData *sd, const ccl_private ShaderClosure *sc, const float3 wo, ccl_private float *pdf)
ccl_device_inline float sin_theta(const float3 w)
ccl_device_inline float to_phi(const float gamma, const float b)
ccl_device_inline bool is_circular(const float b)
ccl_device_inline float dir_theta(const float3 w)
ccl_device_inline float h_to_gamma(const float h_div_r, const float b, const float3 wi)
CCL_NAMESPACE_BEGIN ccl_device void bsdf_transparent_setup(ccl_private ShaderData *sd, const Spectrum weight, const uint32_t path_flag)
ccl_device_inline float3 refract_angle(const float3 incident, const float3 normal, const float cos_theta_t, const float inv_eta)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
#define GGX_GLASS_E_RES_MU
#define GGX_GLASS_E_RES_ROUGH
#define GGX_GLASS_E_RES_IOR
#define kernel_assert(cond)
#define ccl_device_forceinline
#define ccl_device_inline
#define CCL_NAMESPACE_END
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
constexpr T clamp(T, U, U) RET
float fresnel_dielectric_cos(float cosi, float eta)
float fresnel_dielectric(float3 Incoming, float3 Normal, float eta)
float cos_from_sin(float s)
float safe_divide(float a, float b)
VecT safe_normalize(VecT vector)
@ CLOSURE_BSDF_HAIR_HUANG_ID
@ SR_BSDF_HAS_TRANSMISSION
ccl_device float lcg_step_float(T rng)
ccl_device float lookup_table_read_3D(KernelGlobals kg, const float x, float y, float z, const int offset, const int xsize, const int ysize, const int zsize)
ccl_device_inline bool isnan_safe(const float f)
ccl_device_inline float signf(const float f)
ccl_device_inline float inversesqrtf(const float f)
ccl_device_inline float safe_sqrtf(const float f)
ccl_device_inline Interval< T > intervals_intersection(const ccl_private Interval< T > &first, const ccl_private Interval< T > &second)
ccl_device_inline bool isfinite_safe(const float f)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
ccl_device float fast_sinf(float x)
ccl_device float fast_cosf(float x)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ccl_private HuangHairExtra * extra
ccl_device_inline_method T length() const
ccl_device_inline_method bool is_empty() const