Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bsdf_hair.h « closure « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a29f7c444ae6226ac69cc503edb384f97295c76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* SPDX-License-Identifier: BSD-3-Clause
 *
 * Adapted from Open Shading Language
 * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
 * All Rights Reserved.
 *
 * Modifications Copyright 2011-2022 Blender Foundation. */

#pragma once

CCL_NAMESPACE_BEGIN

typedef struct HairBsdf {
  SHADER_CLOSURE_BASE;

  float3 T;
  float roughness1;
  float roughness2;
  float offset;
} HairBsdf;

static_assert(sizeof(ShaderClosure) >= sizeof(HairBsdf), "HairBsdf is too large!");

ccl_device int bsdf_hair_reflection_setup(ccl_private HairBsdf *bsdf)
{
  bsdf->type = CLOSURE_BSDF_HAIR_REFLECTION_ID;
  bsdf->roughness1 = clamp(bsdf->roughness1, 0.001f, 1.0f);
  bsdf->roughness2 = clamp(bsdf->roughness2, 0.001f, 1.0f);
  return SD_BSDF | SD_BSDF_HAS_EVAL;
}

ccl_device int bsdf_hair_transmission_setup(ccl_private HairBsdf *bsdf)
{
  bsdf->type = CLOSURE_BSDF_HAIR_TRANSMISSION_ID;
  bsdf->roughness1 = clamp(bsdf->roughness1, 0.001f, 1.0f);
  bsdf->roughness2 = clamp(bsdf->roughness2, 0.001f, 1.0f);
  return SD_BSDF | SD_BSDF_HAS_EVAL;
}

ccl_device Spectrum bsdf_hair_reflection_eval_reflect(ccl_private const ShaderClosure *sc,
                                                      const float3 I,
                                                      const float3 omega_in,
                                                      ccl_private float *pdf)
{
  ccl_private const HairBsdf *bsdf = (ccl_private const HairBsdf *)sc;
  float offset = bsdf->offset;
  float3 Tg = bsdf->T;
  float roughness1 = bsdf->roughness1;
  float roughness2 = bsdf->roughness2;

  float Iz = dot(Tg, I);
  float3 locy = normalize(I - Tg * Iz);

  float theta_r = M_PI_2_F - fast_acosf(Iz);

  float omega_in_z = dot(Tg, omega_in);
  float3 omega_in_y = normalize(omega_in - Tg * omega_in_z);

  float theta_i = M_PI_2_F - fast_acosf(omega_in_z);
  float cosphi_i = dot(omega_in_y, locy);

  if (M_PI_2_F - fabsf(theta_i) < 0.001f || cosphi_i < 0.0f) {
    *pdf = 0.0f;
    return zero_spectrum();
  }

  float roughness1_inv = 1.0f / roughness1;
  float roughness2_inv = 1.0f / roughness2;
  float phi_i = fast_acosf(cosphi_i) * roughness2_inv;
  phi_i = fabsf(phi_i) < M_PI_F ? phi_i : M_PI_F;
  float costheta_i = fast_cosf(theta_i);

  float a_R = fast_atan2f(((M_PI_2_F + theta_r) * 0.5f - offset) * roughness1_inv, 1.0f);
  float b_R = fast_atan2f(((-M_PI_2_F + theta_r) * 0.5f - offset) * roughness1_inv, 1.0f);

  float theta_h = (theta_i + theta_r) * 0.5f;
  float t = theta_h - offset;

  float phi_pdf = fast_cosf(phi_i * 0.5f) * 0.25f * roughness2_inv;
  float theta_pdf = roughness1 /
                    (2 * (t * t + roughness1 * roughness1) * (a_R - b_R) * costheta_i);
  *pdf = phi_pdf * theta_pdf;

  return make_spectrum(*pdf);
}

ccl_device Spectrum bsdf_hair_transmission_eval_reflect(ccl_private const ShaderClosure *sc,
                                                        const float3 I,
                                                        const float3 omega_in,
                                                        ccl_private float *pdf)
{
  *pdf = 0.0f;
  return zero_spectrum();
}

ccl_device Spectrum bsdf_hair_reflection_eval_transmit(ccl_private const ShaderClosure *sc,
                                                       const float3 I,
                                                       const float3 omega_in,
                                                       ccl_private float *pdf)
{
  *pdf = 0.0f;
  return zero_spectrum();
}

ccl_device Spectrum bsdf_hair_transmission_eval_transmit(ccl_private const ShaderClosure *sc,
                                                         const float3 I,
                                                         const float3 omega_in,
                                                         ccl_private float *pdf)
{
  ccl_private const HairBsdf *bsdf = (ccl_private const HairBsdf *)sc;
  float offset = bsdf->offset;
  float3 Tg = bsdf->T;
  float roughness1 = bsdf->roughness1;
  float roughness2 = bsdf->roughness2;
  float Iz = dot(Tg, I);
  float3 locy = normalize(I - Tg * Iz);

  float theta_r = M_PI_2_F - fast_acosf(Iz);

  float omega_in_z = dot(Tg, omega_in);
  float3 omega_in_y = normalize(omega_in - Tg * omega_in_z);

  float theta_i = M_PI_2_F - fast_acosf(omega_in_z);
  float phi_i = fast_acosf(dot(omega_in_y, locy));

  if (M_PI_2_F - fabsf(theta_i) < 0.001f) {
    *pdf = 0.0f;
    return zero_spectrum();
  }

  float costheta_i = fast_cosf(theta_i);

  float roughness1_inv = 1.0f / roughness1;
  float a_TT = fast_atan2f(((M_PI_2_F + theta_r) / 2 - offset) * roughness1_inv, 1.0f);
  float b_TT = fast_atan2f(((-M_PI_2_F + theta_r) / 2 - offset) * roughness1_inv, 1.0f);
  float c_TT = 2 * fast_atan2f(M_PI_2_F / roughness2, 1.0f);

  float theta_h = (theta_i + theta_r) / 2;
  float t = theta_h - offset;
  float phi = fabsf(phi_i);

  float p = M_PI_F - phi;
  float theta_pdf = roughness1 /
                    (2 * (t * t + roughness1 * roughness1) * (a_TT - b_TT) * costheta_i);
  float phi_pdf = roughness2 / (c_TT * (p * p + roughness2 * roughness2));

  *pdf = phi_pdf * theta_pdf;
  return make_spectrum(*pdf);
}

ccl_device int bsdf_hair_reflection_sample(ccl_private const ShaderClosure *sc,
                                           float3 Ng,
                                           float3 I,
                                           float randu,
                                           float randv,
                                           ccl_private Spectrum *eval,
                                           ccl_private float3 *omega_in,
                                           ccl_private float *pdf)
{
  ccl_private const HairBsdf *bsdf = (ccl_private const HairBsdf *)sc;
  float offset = bsdf->offset;
  float3 Tg = bsdf->T;
  float roughness1 = bsdf->roughness1;
  float roughness2 = bsdf->roughness2;
  float Iz = dot(Tg, I);
  float3 locy = normalize(I - Tg * Iz);
  float3 locx = cross(locy, Tg);
  float theta_r = M_PI_2_F - fast_acosf(Iz);

  float roughness1_inv = 1.0f / roughness1;
  float a_R = fast_atan2f(((M_PI_2_F + theta_r) * 0.5f - offset) * roughness1_inv, 1.0f);
  float b_R = fast_atan2f(((-M_PI_2_F + theta_r) * 0.5f - offset) * roughness1_inv, 1.0f);

  float t = roughness1 * tanf(randu * (a_R - b_R) + b_R);

  float theta_h = t + offset;
  float theta_i = 2 * theta_h - theta_r;

  float costheta_i, sintheta_i;
  fast_sincosf(theta_i, &sintheta_i, &costheta_i);

  float phi = 2 * safe_asinf(1 - 2 * randv) * roughness2;

  float phi_pdf = fast_cosf(phi * 0.5f) * 0.25f / roughness2;

  float theta_pdf = roughness1 /
                    (2 * (t * t + roughness1 * roughness1) * (a_R - b_R) * costheta_i);

  float sinphi, cosphi;
  fast_sincosf(phi, &sinphi, &cosphi);
  *omega_in = (cosphi * costheta_i) * locy - (sinphi * costheta_i) * locx + (sintheta_i)*Tg;

  *pdf = fabsf(phi_pdf * theta_pdf);
  if (M_PI_2_F - fabsf(theta_i) < 0.001f)
    *pdf = 0.0f;

  *eval = make_spectrum(*pdf);

  return LABEL_REFLECT | LABEL_GLOSSY;
}

ccl_device int bsdf_hair_transmission_sample(ccl_private const ShaderClosure *sc,
                                             float3 Ng,
                                             float3 I,
                                             float randu,
                                             float randv,
                                             ccl_private Spectrum *eval,
                                             ccl_private float3 *omega_in,
                                             ccl_private float *pdf)
{
  ccl_private const HairBsdf *bsdf = (ccl_private const HairBsdf *)sc;
  float offset = bsdf->offset;
  float3 Tg = bsdf->T;
  float roughness1 = bsdf->roughness1;
  float roughness2 = bsdf->roughness2;
  float Iz = dot(Tg, I);
  float3 locy = normalize(I - Tg * Iz);
  float3 locx = cross(locy, Tg);
  float theta_r = M_PI_2_F - fast_acosf(Iz);

  float roughness1_inv = 1.0f / roughness1;
  float a_TT = fast_atan2f(((M_PI_2_F + theta_r) / 2 - offset) * roughness1_inv, 1.0f);
  float b_TT = fast_atan2f(((-M_PI_2_F + theta_r) / 2 - offset) * roughness1_inv, 1.0f);
  float c_TT = 2 * fast_atan2f(M_PI_2_F / roughness2, 1.0f);

  float t = roughness1 * tanf(randu * (a_TT - b_TT) + b_TT);

  float theta_h = t + offset;
  float theta_i = 2 * theta_h - theta_r;

  float costheta_i, sintheta_i;
  fast_sincosf(theta_i, &sintheta_i, &costheta_i);

  float p = roughness2 * tanf(c_TT * (randv - 0.5f));
  float phi = p + M_PI_F;
  float theta_pdf = roughness1 /
                    (2 * (t * t + roughness1 * roughness1) * (a_TT - b_TT) * costheta_i);
  float phi_pdf = roughness2 / (c_TT * (p * p + roughness2 * roughness2));

  float sinphi, cosphi;
  fast_sincosf(phi, &sinphi, &cosphi);
  *omega_in = (cosphi * costheta_i) * locy - (sinphi * costheta_i) * locx + (sintheta_i)*Tg;

  *pdf = fabsf(phi_pdf * theta_pdf);
  if (M_PI_2_F - fabsf(theta_i) < 0.001f) {
    *pdf = 0.0f;
  }

  *eval = make_spectrum(*pdf);

  /* TODO(sergey): Should always be negative, but seems some precision issue
   * is involved here.
   */
  kernel_assert(dot(locy, *omega_in) < 1e-4f);

  return LABEL_TRANSMIT | LABEL_GLOSSY;
}

CCL_NAMESPACE_END