Blender V5.3
bsdf_principled_hair_huang.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* This code implements the paper [A Microfacet-based Hair Scattering
6 * Model](https://onlinelibrary.wiley.com/doi/full/10.1111/cgf.14588) by Weizhen Huang, Matthias B.
7 * Hullin and Johannes Hanika. */
8
9#pragma once
10
11#include "kernel/constants.h"
12#include "kernel/types.h"
13
18#include "kernel/sample/lcg.h"
19
21
23 /* Optional modulation factors. */
24 float R, TT, TRT;
25
26 /* Local coordinate system. X is stored as `bsdf->N`. */
28
29 /* Incident direction in local coordinate system. */
31
32 /* Projected radius from the view direction. */
33 float radius;
34
35 /* Squared Eccentricity. */
36 float e2;
37
38 /* The projected width of half a pixel at `sd->P` in `h` space. */
40
41 /* Valid integration interval, pre-divided by the radius so the range is [-1, 1]. */
43};
44
47
48 /* Absorption coefficient. */
50
51 /* Microfacet distribution roughness. */
52 float roughness;
53
54 /* Cuticle tilt angle. */
55 float tilt;
56
57 /* Index of refraction. */
58 float eta;
59
60 /* The ratio of the minor axis to the major axis. */
62
63 /* Azimuthal offset. */
64 float h;
65
66 /* Extra closure for optional modulation factors and local coordinate system. */
68};
69
70static_assert(sizeof(ShaderClosure) >= sizeof(HuangHairBSDF), "HuangHairBSDF is too large!");
71static_assert(sizeof(ShaderClosure) >= sizeof(HuangHairExtra), "HuangHairExtra is too large!");
72
73/* -------------------------------------------------------------------- */
76
77/* TODO(weizhen): add a namespace `huanghair` for the below utility functions. */
78
79/* Returns `sin(theta)` of the given direction. */
81{
82 return w.y;
83}
84
85/* Returns `cos(theta)` of the given direction. */
87{
88 return safe_sqrtf(sqr(w.x) + sqr(w.z));
89}
90
91/* Returns `tan(theta)` of the given direction. */
93{
94 return sin_theta(w) / cos_theta(w);
95}
96
97/* Returns `sin(phi)` and `cos(phi)` of the given direction. */
99{
100 return w.x / cos_theta(w);
101}
102
103/* Check whether the hair cross-section is circular. */
105{
106 return b == 1.0f;
107}
108
110{
111 const float c = cos_theta(w);
112 return make_float2(w.x / c, w.z / c);
113}
114
115/* Extract the theta coordinate from the given direction.
116 * -pi < theta < pi */
118{
119 return atan2f(sin_theta(w), cos_theta(w));
120}
121
122/* Extract the phi coordinate from the given direction, assuming `phi(wi) == 0`.
123 * -pi < phi < pi */
125{
126 return atan2f(w.x, w.z);
127}
128
129/* Extract theta and phi coordinates from the given direction, assuming `phi(wi) == 0`.
130 * -pi/2 < theta < pi/2, -pi < phi < pi */
135
136/* Conversion between `gamma` and `phi`. Notations see Figure 5 in the paper. */
137ccl_device_inline float to_phi(const float gamma, const float b)
138{
139 if (is_circular(b)) {
140 return gamma;
141 }
142 float sin_gamma;
143 float cos_gamma;
144 fast_sincosf(gamma, &sin_gamma, &cos_gamma);
145 return atan2f(b * sin_gamma, cos_gamma);
146}
147
148ccl_device_inline float to_gamma(const float phi, const float b)
149{
150 if (is_circular(b)) {
151 return phi;
152 }
153 float sin_phi;
154 float cos_phi;
155 fast_sincosf(phi, &sin_phi, &cos_phi);
156 return atan2f(sin_phi, b * cos_phi);
157}
158
159/* Intersecting `wi` with the ellipse defined by `x = sin_gamma, y = b * cos_gamma` results in
160 * equation `h = -cos_phi_i * sin_gamma + b * sin_phi_i * cos_gamma`. */
161ccl_device_inline float phi_to_h(const float phi, const float b, const float3 wi)
162{
163 if (is_circular(b)) {
164 return -fast_sinf(phi);
165 }
166
167 float sin_gamma, cos_gamma;
168 fast_sincosf(to_gamma(phi, b), &sin_gamma, &cos_gamma);
169 const float2 sin_cos_phi_i = sincos_phi(wi);
170 return -sin_cos_phi_i.y * sin_gamma + b * sin_cos_phi_i.x * cos_gamma;
171}
172
173/* Solve for `gamma` in equation `h = -cos_phi_i * sin_gamma + b * sin_phi_i * cos_gamma`.
174 * Also, make use of `r = sqrt(sqr(cos_phi_i) + sqr(b * sin_phi_i))` to pre-map `h` to [-1, 1]. */
175ccl_device_inline float h_to_gamma(const float h_div_r, const float b, const float3 wi)
176{
177 return is_circular(b) ? -asinf(h_div_r) : atan2f(wi.z, -b * wi.x) - acosf(-h_div_r);
178}
179
180/* Jacobian |d_gamma/d_h|, used for changing variable in the integration. */
181ccl_device_inline float d_gamma_d_h(const float2 sincos_phi_i, const float gamma, const float b)
182{
183 if (is_circular(b)) {
184 return safe_divide(1.0f, fast_cosf(gamma));
185 }
186
187 float sin_gamma, cos_gamma;
188 fast_sincosf(gamma, &sin_gamma, &cos_gamma);
189 return safe_divide(1.0f, sincos_phi_i.y * cos_gamma + b * sincos_phi_i.x * sin_gamma);
190}
191
192/* Compute the coordinate on the ellipse, given `gamma` and the aspect ratio between the minor axis
193 * and the major axis. */
194ccl_device_inline float2 to_point(const float gamma, const float b)
195{
196 float sin_gamma;
197 float cos_gamma;
198 fast_sincosf(gamma, &sin_gamma, &cos_gamma);
199 return make_float2(sin_gamma, b * cos_gamma);
200}
201
202/* Compute the vector direction given by `theta` and `gamma`. */
203ccl_device_inline float3 sphg_dir(const float theta, const float gamma, const float b)
204{
205 float sin_theta;
206 float cos_theta;
207 float sin_gamma;
208 float cos_gamma;
209 float sin_phi;
210 float cos_phi;
211
213 fast_sincosf(gamma, &sin_gamma, &cos_gamma);
214
215 if (is_circular(b) || fabsf(cos_gamma) < 1e-6f) {
216 sin_phi = sin_gamma;
217 cos_phi = cos_gamma;
218 }
219 else {
220 const float tan_gamma = sin_gamma / cos_gamma;
221 const float tan_phi = b * tan_gamma;
222 cos_phi = signf(cos_gamma) * inversesqrtf(sqr(tan_phi) + 1.0f);
223 sin_phi = cos_phi * tan_phi;
224 }
225 return make_float3(sin_phi * cos_theta, sin_theta, cos_phi * cos_theta);
226}
227
228ccl_device_inline float arc_length(const float e2, const float gamma)
229{
230 return e2 == 0 ? 1.0f : sqrtf(1.0f - e2 * sqr(sinf(gamma)));
231}
232
234{
235 return bsdf->extra->radius > bsdf->extra->pixel_coverage;
236}
237
239
240#ifdef __HAIR__
241/* Set up the hair closure. */
242ccl_device int bsdf_hair_huang_setup(ccl_private ShaderData *sd,
244 const uint32_t path_flag)
245{
246 bsdf->type = CLOSURE_BSDF_HAIR_HUANG_ID;
247
248 bsdf->roughness = clamp(bsdf->roughness, 0.001f, 1.0f);
249
250 /* Negate to keep it consistent with principled hair BSDF. */
251 bsdf->tilt = -bsdf->tilt;
252
253 /* Compute local frame. The Y axis is aligned with the curve tangent; the X axis is perpendicular
254 * to the ray direction for circular cross-sections, or aligned with the major axis for
255 * elliptical cross-sections. */
256 bsdf->extra->Y = safe_normalize(sd->dPdu);
257 const float3 X = safe_normalize(cross(sd->dPdu, sd->wi));
258
259 /* h from -1..0..1 means the rays goes from grazing the hair, to hitting it at the center, to
260 * grazing the other edge. This is the cosine of the angle between `sd->N` and `X`. */
261 bsdf->h = ((sd->type & PRIMITIVE_CURVE) == PRIMITIVE_CURVE_RIBBON) ? -sd->v : -dot(X, sd->N);
262
263 kernel_assert(fabsf(bsdf->h) < 1.0f + 1e-4f);
265
266 if (bsdf->aspect_ratio != 1.0f && (sd->type & PRIMITIVE_CURVE)) {
267 /* Adjust `bsdf->N` to be orthogonal to `sd->dPdu`. */
268 bsdf->N = safe_normalize(cross(sd->dPdu, safe_normalize(cross(bsdf->N, sd->dPdu))));
269 /* Align local frame with the curve normal. */
270 if (bsdf->aspect_ratio > 1.0f) {
271 /* Switch major and minor axis. */
272 bsdf->aspect_ratio = 1.0f / bsdf->aspect_ratio;
273 const float3 minor_axis = safe_normalize(cross(sd->dPdu, bsdf->N));
274 bsdf->N = safe_normalize(cross(minor_axis, sd->dPdu));
275 }
276 }
277 else {
278 /* Align local frame with the ray direction so that `phi_i == 0`. */
279 bsdf->N = X;
280 }
281
282 /* Fill extra closure. */
283 if (is_zero(bsdf->N) || !isfinite_safe(bsdf->N)) {
284 /* Construct arbitrary local coordinate system. The implementation should ensure smooth
285 * transition along the hair shaft. */
286 make_orthonormals(bsdf->extra->Y, &bsdf->extra->Z, &bsdf->N);
287 }
288 else {
289 bsdf->extra->Z = safe_normalize(cross(bsdf->N, sd->dPdu));
290 }
291
292 const float3 I = to_local(sd->wi, bsdf->N, bsdf->extra->Y, bsdf->extra->Z);
293 bsdf->extra->wi = I;
294 bsdf->extra->e2 = 1.0f - sqr(bsdf->aspect_ratio);
295 bsdf->extra->radius = bsdf->extra->e2 == 0 ?
296 1.0f :
297 sqrtf(1.0f - bsdf->extra->e2 * sqr(I.x) / (sqr(I.x) + sqr(I.z)));
298
299 /* Treat as transparent material if intersection lies outside of the projected radius. */
300 if (fabsf(bsdf->h) >= bsdf->extra->radius) {
301 /* Remove allocated closures. */
302 sd->num_closure--;
303 sd->num_closure_left += 2;
304 /* Allocate transparent closure. */
305 bsdf_transparent_setup(sd, bsdf->weight, path_flag);
306 return 0;
307 }
308
310}
311
312#endif /* __HAIR__ */
313
314/* Albedo correction, treat as glass. `rough` has already applied square root. */
316 const float mu,
317 const float rough,
318 const float ior)
319{
320 const bool inv_table = (ior < 1.0f);
321 const int ofs = inv_table ? kernel_data.tables.ggx_glass_inv_E : kernel_data.tables.ggx_glass_E;
322 const float z = sqrtf(fabsf((ior - 1.0f) / (ior + 1.0f)));
323 const float y = mu_to_y_index(mu);
324 return 1.0f /
327}
328
329/* Sample microfacets from a tilted mesonormal. */
330ccl_device_inline float3 sample_wh(const float roughness,
331 const float3 wi,
332 const float3 wm,
333 const float2 rand)
334{
335 /* Coordinate transformation for microfacet sampling. */
336 float3 s;
337 float3 t;
338 make_orthonormals(wm, &s, &t);
339
340 const float3 wi_wm = to_local(wi, s, t, wm);
341 const float3 wh_wm = microfacet_ggx_sample_vndf(wi_wm, roughness, roughness, rand);
342 const float3 wh = to_global(wh_wm, s, t, wm);
343 return wh;
344}
345
346/* Check micronormal/mesonormal direct visibility from direction `v`. */
348{
349 return (dot(v, h) > 0.0f && dot(v, m) > 0.0f);
350}
351
352/* Check micronormal/mesonormal direct visibility from directions `wi` and `wo`. */
354 const float3 wo,
355 const float3 m,
356 const float3 h)
357{
358 return microfacet_visible(wi, m, h) && microfacet_visible(wo, m, h);
359}
360
361/* Combined shadowing-masking term divided by the shadowing-masking in the incoming direction. */
362ccl_device_inline float bsdf_Go(const float alpha2, const float cos_NI, const float cos_NO)
363{
364 const float lambdaI = bsdf_lambda<MicrofacetType::GGX>(alpha2, cos_NI);
365 const float lambdaO = bsdf_lambda<MicrofacetType::GGX>(alpha2, cos_NO);
366 return (1.0f + lambdaI) / (1.0f + lambdaI + lambdaO);
367}
368
370 const ccl_private ShaderClosure *sc,
371 const float3 wi,
372 const float3 wo)
373{
375
376 if (bsdf->extra->R <= 0.0f) {
377 return zero_float3();
378 }
379
380 /* Get minor axis, assuming major axis is 1. */
381 const float b = bsdf->aspect_ratio;
382
383 const float3 wh = normalize(wi + wo);
384
385 const float roughness = bsdf->roughness;
386 const float roughness2 = sqr(roughness);
387
388 const float2 sincos_phi_i = sincos_phi(wi);
389
390 /* Maximal sample resolution. */
391 float res = roughness * 0.7f;
392
393 const float h_range = bsdf->extra->h.length();
394
395 /* Number of intervals should be even. */
396 const size_t intervals = 2 * (size_t)ceilf(h_range / res * 0.5f);
397
398 /* Modified resolution based on numbers of intervals. */
399 res = h_range / float(intervals);
400
401 /* Integrate using Composite Simpson's 1/3 rule. */
402 float integral = 0.0f;
403 for (size_t i = 0; i <= intervals; i++) {
404 const float h = bsdf->extra->h.min + i * res;
405 const float gamma_m = h_to_gamma(h, b, wi);
406 const float3 wm = sphg_dir(bsdf->tilt, gamma_m, b);
407
408 if (microfacet_visible(wi, wo, make_float3(wm.x, 0.0f, wm.z), wh)) {
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);
412 const float G = bsdf_G<MicrofacetType::GGX>(roughness2, cos_mi, dot(wm, wo));
413 integral += weight * bsdf_D<MicrofacetType::GGX>(roughness2, dot(wm, wh)) * G *
414 arc_length(bsdf->extra->e2, gamma_m) *
415 bsdf_hair_huang_energy_scale(kg, cos_mi, sqrtf(roughness), bsdf->eta);
416 }
417 }
418
419 /* Simpson coefficient */
420 integral *= (2.0f / 3.0f * res);
421
422 const float F = fresnel_dielectric_cos(dot(wi, wh), bsdf->eta);
423
424 return make_spectrum(bsdf->extra->R * 0.25f * F * integral);
425}
426
427/* Approximate components beyond TRT (starting TRRT) by summing up a geometric series. Attenuations
428 * are approximated from previous interactions. */
429ccl_device Spectrum bsdf_hair_huang_eval_trrt(const float T, const float R, const Spectrum A)
430{
431 /* `T` could be zero due to total internal reflection. Clamp to avoid numerical issues. */
432 const float T_avg = max(1.0f - R, 1e-5f);
433 const Spectrum TRRT_avg = T * sqr(R) * T_avg * A * A * A;
434 return TRRT_avg / (one_spectrum() - A * (1.0f - T_avg));
435}
436
437/* Evaluate components beyond R using numerical integration. TT and TRT are computed via combined
438 * Monte Carlo-Simpson integration; components beyond TRRT are integrated via Simpson's method. */
440 const ccl_private ShaderClosure *sc,
441 const float3 wi,
442 const float3 wo,
443 ccl_private uint *rng_quadrature)
444{
446
447 if (bsdf->extra->TT <= 0.0f && bsdf->extra->TRT <= 0.0f) {
448 return zero_spectrum();
449 }
450
451 /* Get minor axis, assuming major axis is 1. */
452 const float b = bsdf->aspect_ratio;
453
454 const Spectrum mu_a = bsdf->sigma;
455 const float eta = bsdf->eta;
456 const float inv_eta = 1.0f / eta;
457
458 const float roughness = bsdf->roughness;
459 const float roughness2 = sqr(roughness);
460 const float sqrt_roughness = sqrtf(roughness);
461
462 const float2 sincos_phi_i = sincos_phi(wi);
463
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;
468
469 Spectrum S_tt = zero_spectrum();
470 Spectrum S_trt = zero_spectrum();
471 Spectrum S_trrt = zero_spectrum();
472 for (size_t i = 0; i <= intervals; i++) {
473 const float h = bsdf->extra->h.min + i * res;
474 const float gamma_mi = h_to_gamma(h, b, wi);
475
476 const float3 wmi = sphg_dir(bsdf->tilt, gamma_mi, b);
477 const float3 wmi_ = sphg_dir(0.0f, gamma_mi, b);
478
479 /* Sample `wh1`. */
480 const float2 sample1 = make_float2(lcg_step_float(rng_quadrature),
481 lcg_step_float(rng_quadrature));
482
483 const float3 wh1 = sample_wh(roughness, wi, wmi, sample1);
484 const float cos_hi1 = dot(wi, wh1);
485 if (!(cos_hi1 > 0.0f)) {
486 continue;
487 }
488
489 const float cos_mi1 = dot(wi, wmi);
490 float cos_theta_t1;
491 const float T1 = 1.0f - fresnel_dielectric(cos_hi1, eta, &cos_theta_t1);
492 const float scale1 = bsdf_hair_huang_energy_scale(kg, cos_mi1, sqrt_roughness, eta);
493
494 /* Refraction at the first interface. */
495 const float3 wt = refract_angle(wi, wh1, cos_theta_t1, inv_eta);
496 const float phi_t = dir_phi(wt);
497 const float gamma_mt = 2.0f * to_phi(phi_t, b) - gamma_mi;
498 const float3 wmt = sphg_dir(-bsdf->tilt, gamma_mt, b);
499 const float3 wmt_ = sphg_dir(0.0f, gamma_mt, b);
500
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);
504 if (!microfacet_visible(wi, -wt, wmi, wh1) || !microfacet_visible(wi, -wt, wmi_, wh1)) {
505 continue;
506 }
507
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;
510
511 const Spectrum A_t = exp(mu_a / cos_theta(wt) *
512 (is_circular(b) ?
513 2.0f * cosf(gamma_mi - phi_t) :
514 -len(to_point(gamma_mi, b) - to_point(gamma_mt + M_PI_F, b))));
515
516 const float scale2 = bsdf_hair_huang_energy_scale(kg, cos_mi2, sqrt_roughness, inv_eta);
517
518 /* TT */
519 if (bsdf->extra->TT > 0.0f) {
520 if (dot(wo, wt) >= inv_eta - 1e-5f) { /* Total internal reflection otherwise. */
521 float3 wh2 = -wt + inv_eta * wo;
522 const float rcp_norm_wh2 = 1.0f / len(wh2);
523 wh2 *= rcp_norm_wh2;
524 const float cos_mh2 = dot(wmt, wh2);
525 if (cos_mh2 >= 0.0f) { /* Microfacet visibility from macronormal. */
526 const float cos_hi2 = dot(-wt, wh2);
527 const float cos_ho2 = dot(-wo, wh2);
528 const float cos_mo2 = dot(-wo, wmt);
529
530 const float T2 = (1.0f - fresnel_dielectric_cos(cos_hi2, inv_eta)) * scale2;
531 const float D2 = bsdf_D<MicrofacetType::GGX>(roughness2, cos_mh2);
532 const float G2 = bsdf_G<MicrofacetType::GGX>(roughness2, cos_mi2, cos_mo2);
533
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);
536
537 if (isfinite_safe(result)) {
538 S_tt += bsdf->extra->TT * result * arc_length(bsdf->extra->e2, gamma_mt);
539 }
540 }
541 }
542 }
543
544 /* TRT and beyond. */
545 if (bsdf->extra->TRT > 0.0f) {
546 /* Sample `wh2`. */
547 const float2 sample2 = make_float2(lcg_step_float(rng_quadrature),
548 lcg_step_float(rng_quadrature));
549 const float3 wh2 = sample_wh(roughness, -wt, wmt, sample2);
550 const float cos_hi2 = dot(-wt, wh2);
551 if (!(cos_hi2 > 0.0f)) {
552 continue;
553 }
554 const float R2 = fresnel_dielectric_cos(cos_hi2, inv_eta);
555
556 const float3 wtr = -reflect(wt, wh2);
557 if (dot(-wtr, wo) < inv_eta - 1e-5f) {
558 /* Total internal reflection. */
559 S_trrt += weight * bsdf_hair_huang_eval_trrt(T1, R2, A_t);
560 continue;
561 }
562
563 if (!microfacet_visible(-wt, -wtr, wmt, wh2) || !microfacet_visible(-wt, -wtr, wmt_, wh2)) {
564 continue;
565 }
566
567 const float phi_tr = dir_phi(wtr);
568 const float gamma_mtr = gamma_mi - 2.0f * (to_phi(phi_t, b) - to_phi(phi_tr, b)) + M_PI_F;
569 const float3 wmtr = sphg_dir(-bsdf->tilt, gamma_mtr, b);
570 const float3 wmtr_ = sphg_dir(0.0f, gamma_mtr, b);
571
572 float3 wh3 = wtr + inv_eta * wo;
573 const float rcp_norm_wh3 = 1.0f / len(wh3);
574 wh3 *= rcp_norm_wh3;
575 const float cos_mh3 = dot(wmtr, wh3);
576 if (cos_mh3 < 0.0f || !microfacet_visible(wtr, -wo, wmtr, wh3) ||
577 !microfacet_visible(wtr, -wo, wmtr_, wh3))
578 {
579 S_trrt += weight * bsdf_hair_huang_eval_trrt(T1, R2, A_t);
580 continue;
581 }
582
583 const float cos_hi3 = dot(wh3, wtr);
584 const float cos_ho3 = dot(wh3, -wo);
585 const float cos_mi3 = dot(wmtr, wtr);
586
587 const float T3 = (1.0f - fresnel_dielectric_cos(cos_hi3, inv_eta)) *
588 bsdf_hair_huang_energy_scale(kg, cos_mi3, sqrt_roughness, inv_eta);
589 const float D3 = bsdf_D<MicrofacetType::GGX>(roughness2, cos_mh3);
590
591 const Spectrum A_tr = exp(mu_a / cos_theta(wtr) *
592 -(is_circular(b) ?
593 2.0f * fabsf(cosf(phi_tr - gamma_mt)) :
594 len(to_point(gamma_mtr, b) - to_point(gamma_mt, b))));
595
596 const float cos_mo2 = dot(wmt, -wtr);
597 const float G2o = bsdf_Go(roughness2, cos_mi2, cos_mo2);
598 const float G3 = bsdf_G<MicrofacetType::GGX>(roughness2, cos_mi3, dot(wmtr, -wo));
599
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 *
602 sqr(rcp_norm_wh3);
603
604 if (isfinite_safe(result)) {
605 S_trt += bsdf->extra->TRT * result * arc_length(bsdf->extra->e2, gamma_mtr);
606 }
607
608 S_trrt += weight * bsdf_hair_huang_eval_trrt(T1, R2, A_t);
609 }
610 }
611
612 /* TRRT+ terms, following the approach in [A practical and controllable hair and fur model for
613 * production path tracing](https://doi.org/10.1145/2775280.2792559) by Chiang, Matt Jen-Yuan, et
614 * al. */
615 const float M = longitudinal_scattering(
616 sin_theta(wi), cos_theta(wi), sin_theta(wo), cos_theta(wo), 4.0f * bsdf->roughness);
617 const float N = M_1_2PI_F;
618
619 const float simpson_coeff = 2.0f / 3.0f * res;
620
621 return ((S_tt + S_trt) * sqr(inv_eta) + S_trrt * M * N * M_2_PI_F) * simpson_coeff;
622}
623
625 const ccl_private ShaderClosure *sc,
626 ccl_private ShaderData *sd,
627 const float3 rand,
628 ccl_private Spectrum *eval,
630 ccl_private float *pdf,
631 ccl_private float2 *sampled_roughness)
632{
634
635 const float roughness = bsdf->roughness;
636 *sampled_roughness = make_float2(roughness, roughness);
637
638 kernel_assert(fabsf(bsdf->h) < bsdf->extra->radius);
639
640 /* Generate samples. */
641 float sample_lobe = rand.x;
642 const float sample_h = rand.y;
643 const float2 sample_h1 = make_float2(rand.z, lcg_step_float(&sd->lcg_state));
644 const float2 sample_h2 = make_float2(lcg_step_float(&sd->lcg_state),
645 lcg_step_float(&sd->lcg_state));
646 const float2 sample_h3 = make_float2(lcg_step_float(&sd->lcg_state),
647 lcg_step_float(&sd->lcg_state));
648
649 /* Get `wi` in local coordinate. */
650 const float3 wi = bsdf->extra->wi;
651
652 /* Get minor axis, assuming major axis is 1. */
653 const float b = bsdf->aspect_ratio;
654
655 /* Sample `h` for farfield model, as the computed intersection might have numerical issues. */
656 const float h_div_r = is_nearfield(bsdf) ? bsdf->h / bsdf->extra->radius :
657 (sample_h * 2.0f - 1.0f);
658 const float gamma_mi = h_to_gamma(h_div_r, b, wi);
659
660 /* Macronormal. */
661 const float3 wmi_ = sphg_dir(0, gamma_mi, b);
662
663 /* Mesonormal. */
664 float st;
665 float ct;
666 fast_sincosf(bsdf->tilt, &st, &ct);
667 const float3 wmi = make_float3(wmi_.x * ct, st, wmi_.z * ct);
668 const float cos_mi1 = dot(wmi, wi);
669
670 if (cos_mi1 < 0.0f || dot(wmi_, wi) < 0.0f) {
671 /* Macro/mesonormal invisible. */
672 *pdf = 0.0f;
673 return LABEL_NONE;
674 }
675
676 /* Sample R lobe. */
677 const float roughness2 = sqr(roughness);
678 const float sqrt_roughness = sqrtf(roughness);
679 const float3 wh1 = sample_wh(roughness, wi, wmi, sample_h1);
680 const float3 wr = -reflect(wi, wh1);
681
682 /* Ensure that this is a valid sample. */
683 if (!microfacet_visible(wi, wmi_, wh1)) {
684 *pdf = 0.0f;
685 return LABEL_NONE;
686 }
687
688 float cos_theta_t1;
689 const float R1 = fresnel_dielectric(dot(wi, wh1), bsdf->eta, &cos_theta_t1);
690 const float scale1 = bsdf_hair_huang_energy_scale(kg, cos_mi1, sqrt_roughness, bsdf->eta);
691 const float R = bsdf->extra->R * R1 * scale1 * microfacet_visible(wr, wmi_, wh1) *
692 bsdf_Go(roughness2, cos_mi1, dot(wmi, wr));
693
694 /* Sample TT lobe. */
695 const float inv_eta = 1.0f / bsdf->eta;
696 const float3 wt = refract_angle(wi, wh1, cos_theta_t1, inv_eta);
697 const float phi_t = dir_phi(wt);
698
699 const float gamma_mt = 2.0f * to_phi(phi_t, b) - gamma_mi;
700 const float3 wmt = sphg_dir(-bsdf->tilt, gamma_mt, b);
701 const float3 wmt_ = sphg_dir(0.0f, gamma_mt, b);
702
703 const float3 wh2 = sample_wh(roughness, -wt, wmt, sample_h2);
704
705 const float3 wtr = -reflect(wt, wh2);
706
707 float3 wh3;
708 float3 wtt;
709 float3 wtrt;
710 float3 wmtr;
711 float3 wtrrt;
712 Spectrum TT = zero_spectrum();
713 Spectrum TRT = zero_spectrum();
714 Spectrum TRRT = zero_spectrum();
715 const float cos_mi2 = dot(-wt, wmt);
716
717 if (cos_mi2 > 0.0f && microfacet_visible(-wt, wmi_, wh1) && microfacet_visible(-wt, wmt_, wh2)) {
718 const Spectrum mu_a = bsdf->sigma;
719 const Spectrum A_t = exp(mu_a / cos_theta(wt) *
720 (is_circular(b) ?
721 2.0f * cosf(phi_t - gamma_mi) :
722 -len(to_point(gamma_mi, b) - to_point(gamma_mt + M_PI_F, b))));
723
724 float cos_theta_t2;
725 const float R2 = fresnel_dielectric(dot(-wt, wh2), inv_eta, &cos_theta_t2);
726 const float T1 = (1.0f - R1) * scale1 * bsdf_Go(roughness2, cos_mi1, dot(wmi, -wt));
727 const float T2 = 1.0f - R2;
728 const float scale2 = bsdf_hair_huang_energy_scale(kg, cos_mi2, sqrt_roughness, inv_eta);
729
730 wtt = refract_angle(-wt, wh2, cos_theta_t2, bsdf->eta);
731
732 if (dot(wmt, -wtt) > 0.0f && T2 > 0.0f && microfacet_visible(-wtt, wmt_, wh2)) {
733 TT = bsdf->extra->TT * T1 * A_t * T2 * scale2 * bsdf_Go(roughness2, cos_mi2, dot(wmt, -wtt));
734 }
735
736 /* Sample TRT lobe. */
737 const float phi_tr = dir_phi(wtr);
738 const float gamma_mtr = gamma_mi - 2.0f * (to_phi(phi_t, b) - to_phi(phi_tr, b)) + M_PI_F;
739 wmtr = sphg_dir(-bsdf->tilt, gamma_mtr, b);
740
741 wh3 = sample_wh(roughness, wtr, wmtr, sample_h3);
742
743 float cos_theta_t3;
744 const float R3 = fresnel_dielectric(dot(wtr, wh3), inv_eta, &cos_theta_t3);
745
746 wtrt = refract_angle(wtr, wh3, cos_theta_t3, bsdf->eta);
747
748 const float cos_mi3 = dot(wmtr, wtr);
749 if (cos_mi3 > 0.0f) {
750 const Spectrum A_tr = exp(mu_a / cos_theta(wtr) *
751 -(is_circular(b) ?
752 2.0f * fabsf(cosf(phi_tr - gamma_mt)) :
753 len(to_point(gamma_mt, b) - to_point(gamma_mtr, b))));
754
755 const Spectrum TR = T1 * R2 * scale2 * A_t * A_tr *
756 bsdf_hair_huang_energy_scale(kg, cos_mi3, sqrt_roughness, inv_eta) *
757 bsdf_Go(roughness2, cos_mi2, dot(wmt, -wtr));
758
759 const float T3 = 1.0f - R3;
760
761 if (T3 > 0.0f && microfacet_visible(wtr, -wtrt, make_float3(wmtr.x, 0.0f, wmtr.z), wh3)) {
762 TRT = bsdf->extra->TRT * TR * make_spectrum(T3) *
763 bsdf_Go(roughness2, cos_mi3, dot(wmtr, -wtrt));
764 }
765
766 /* Sample TRRT+ terms, following the approach in [A practical and controllable hair and fur
767 * model for production path tracing](https://doi.org/10.1145/2775280.2792559) by Chiang,
768 * Matt Jen-Yuan, et al. */
769
770 /* Sample `theta_o`. */
771 const float rand_theta = max(lcg_step_float(&sd->lcg_state), 1e-5f);
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) +
776 cos_from_sin(fac) *
777 cosf(M_2PI_F * lcg_step_float(&sd->lcg_state)) * cos_theta(wi);
778 const float cos_theta_o = cos_from_sin(sin_theta_o);
779
780 /* Sample `phi_o`. */
781 const float phi_o = M_2PI_F * lcg_step_float(&sd->lcg_state);
782 float sin_phi_o;
783 float cos_phi_o;
784 fast_sincosf(phi_o, &sin_phi_o, &cos_phi_o);
785
786 /* Compute outgoing direction. */
787 wtrrt = make_float3(sin_phi_o * cos_theta_o, sin_theta_o, cos_phi_o * cos_theta_o);
788
789 /* Compute residual term by summing up the geometric series `A * T + A^2 * R * T + ...`.
790 * Attenuations are approximated from previous interactions. */
791 const Spectrum A_avg = sqrt(A_t * A_tr);
792 /* `T` could be zero due to total internal reflection. Clamp to avoid numerical issues. */
793 const float T_avg = max(0.5f * (T2 + T3), 1e-5f);
794 const Spectrum A_res = A_avg * T_avg / (one_spectrum() - A_avg * (1.0f - T_avg));
795
796 TRRT = TR * R3 * A_res * bsdf_Go(roughness2, cos_mi3, dot(wmtr, -reflect(wtr, wh3)));
797 }
798 }
799
800 /* Select lobe based on energy. */
801 const float r = R;
802 const float tt = average(TT);
803 const float trt = average(TRT);
804 const float trrt = average(TRRT);
805 const float total_energy = r + tt + trt + trrt;
806
807 if (total_energy == 0.0f) {
808 *pdf = 0.0f;
809 return LABEL_NONE;
810 }
811
812 float3 local_O;
813
814 sample_lobe *= total_energy;
815 if (sample_lobe < r) {
816 local_O = wr;
817 *eval = make_spectrum(total_energy);
818 }
819 else if (sample_lobe < (r + tt)) {
820 local_O = wtt;
821 *eval = TT / tt * total_energy;
822 }
823 else if (sample_lobe < (r + tt + trt)) {
824 local_O = wtrt;
825 *eval = TRT / trt * total_energy;
826 }
827 else {
828 local_O = wtrrt;
829 *eval = TRRT / trrt * make_spectrum(total_energy);
830 }
831
832 /* Transform `wo` to global coordinate system. */
833 *wo = to_global(local_O, bsdf->N, bsdf->extra->Y, bsdf->extra->Z);
834
835 /* Ensure the same pdf is returned for BSDF and emitter sampling. The importance sampling pdf is
836 * already factored in the value so this value is only used for MIS. */
837 *pdf = 1.0f;
838
840}
841
843 ccl_private ShaderData *sd,
844 const ccl_private ShaderClosure *sc,
845 const float3 wo,
846 ccl_private float *pdf)
847{
849
850 kernel_assert(fabsf(bsdf->h) < bsdf->extra->radius);
851
852 /* Transform `wi`/`wo` from global coordinate system to local. */
853 const float3 local_I = bsdf->extra->wi;
854 const float3 local_O = to_local(wo, bsdf->N, bsdf->extra->Y, bsdf->extra->Z);
855
856 /* TODO: better estimation of the pdf */
857 *pdf = 1.0f;
858
859 /* Early detection of `dot(wo, wmo) < 0`. */
860 const float tan_tilt = tanf(bsdf->tilt);
861 if (tan_tilt * tan_theta(local_O) < -1.0f) {
862 return zero_spectrum();
863 }
864
865 /* Compute visible azimuthal range from the incoming direction. */
866 const float half_span = acosf(fmaxf(-tan_tilt * tan_theta(local_I), 0.0f));
867 if (isnan_safe(half_span)) {
868 /* Early detection of `dot(wi, wmi) < 0`. */
869 return zero_spectrum();
870 }
871 const float r = bsdf->extra->radius;
872 const float b = bsdf->aspect_ratio;
873 const float phi_i = is_circular(b) ? 0.0f : dir_phi(local_I);
874
875 Interval<float> h = {phi_to_h(phi_i + half_span, b, local_I),
876 phi_to_h(phi_i - half_span, b, local_I)};
877
878 /* Length of the integral interval. */
879 float dh = 2.0f;
880
881 if (is_nearfield(bsdf)) {
882 /* Reduce the integration interval to the subset that's visible to the current pixel.
883 * Inspired by [An Efficient and Practical Near and Far Field Fur Reflectance Model]
884 * (https://sites.cs.ucsb.edu/~lingqi/publications/paper_fur2.pdf) by Ling-Qi Yan, Henrik Wann
885 * Jensen and Ravi Ramamoorthi. */
886 const float half_pixel = bsdf->extra->pixel_coverage;
887 const Interval<float> nearfield_h = intervals_intersection(
888 Interval<float>{-r, r}, {bsdf->h - half_pixel, bsdf->h + half_pixel});
889
890 dh = nearfield_h.length() / r;
891 h = intervals_intersection(h, nearfield_h);
892 }
893
894 /* Pre-divide by radius for easier conversion to `gamma`. */
895 h /= r;
896
897 /* Clamp for numerical stability at the boundaries. */
898 h = intervals_intersection(h, {-0.999f, 0.999f});
899
900 if (h.is_empty()) {
901 /* No overlap between the valid range and the visible range. Can happen at grazing `theta`
902 * angles. */
903 return zero_spectrum();
904 }
905
906 bsdf->extra->h = h;
907
908 const float projected_area = cos_theta(local_I) * dh;
909
910 return (bsdf_hair_huang_eval_r(kg, sc, local_I, local_O) +
911 bsdf_hair_huang_eval_residual(kg, sc, local_I, local_O, &sd->lcg_state)) /
912 projected_area;
913}
914
915/* Implements Filter Glossy by capping the effective roughness. */
916ccl_device void bsdf_hair_huang_blur(ccl_private ShaderClosure *sc, const float roughness)
917{
919
920 bsdf->roughness = fmaxf(roughness, bsdf->roughness);
921}
922
923/* Hair Albedo. Computed by summing up geometric series, assuming circular cross-section and
924 * specular reflection. */
926 const ccl_private ShaderClosure *sc)
927{
929
930 const float3 wmi = make_float3(bsdf->h, 0.0f, cos_from_sin(bsdf->h));
931 float cos_t;
932 const float f = fresnel_dielectric(dot(wmi, bsdf->extra->wi), bsdf->eta, &cos_t);
933 const float3 wt = refract_angle(bsdf->extra->wi, wmi, cos_t, 1.0f / bsdf->eta);
934 const Spectrum A = exp(2.0f * bsdf->sigma * cos_t / (1.0f - sqr(wt.y)));
935
936 return safe_divide(A - 2.0f * f * A + f, one_spectrum() - f * A);
937}
938
#define TR
Definition boxpack_2d.cc:77
#define A
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)
Definition bsdf_util.h:103
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define GGX_GLASS_E_RES_MU
Definition constants.h:13
#define GGX_GLASS_E_RES_ROUGH
Definition constants.h:12
#define GGX_GLASS_E_RES_IOR
Definition constants.h:14
#define kernel_assert(cond)
#define kernel_data
#define M_2_PI_F
#define ccl_device_forceinline
#define one_spectrum
#define zero_spectrum
#define make_spectrum(f)
#define ccl_private
#define ccl_device_inline
#define M_1_2PI_F
#define logf(x)
#define expf(x)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define asinf(x)
#define acosf(x)
#define exp
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
#define sqrt
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)
bool is_zero(float3x3 a)
float safe_divide(float a, float b)
float average(float2 a)
VecT safe_normalize(VecT vector)
@ CLOSURE_BSDF_HAIR_HUANG_ID
@ SR_BSDF_HAS_TRANSMISSION
@ SR_BSDF_HAS_EVAL
@ SR_BSDF
@ PRIMITIVE_CURVE_RIBBON
@ PRIMITIVE_CURVE
@ LABEL_NONE
@ LABEL_GLOSSY
@ LABEL_REFLECT
ccl_device float lcg_step_float(T rng)
Definition lcg.h:22
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)
Definition math_base.h:356
ccl_device_inline float signf(const float f)
Definition math_base.h:502
ccl_device_inline float inversesqrtf(const float f)
Definition math_base.h:576
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device_inline Interval< T > intervals_intersection(const ccl_private Interval< T > &first, const ccl_private Interval< T > &second)
Definition math_base.h:947
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
Definition math_fast.h:144
ccl_device float fast_sinf(float x)
Definition math_fast.h:83
ccl_device float fast_cosf(float x)
Definition math_fast.h:118
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define M
#define N
#define T
#define F
#define R
#define T2
Definition md5.cpp:21
#define T3
Definition md5.cpp:22
#define T1
Definition md5.cpp:20
#define G(x, y, z)
#define X(TYPE)
#define I
#define sqr
#define fabsf
#define sqrtf
#define ccl_device
#define M_2PI_F
#define fmaxf
#define sinf
#define make_float2
#define tanf
#define M_PI_F
#define cosf
#define ceilf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ccl_private HuangHairExtra * extra
ccl_device_inline_method T length() const
Definition math_base.h:931
ccl_device_inline_method bool is_empty() const
Definition math_base.h:920
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ccl_device_inline float2 to_local(const T p, const T X, const T Y)
Definition transform.h:33
CCL_NAMESPACE_BEGIN ccl_device_inline T to_global(const float2 p, const T X, const T Y)
Definition transform.h:21
float3 Spectrum