Blender  V2.93
bsdf_ashikhmin_shirley.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2014 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __BSDF_ASHIKHMIN_SHIRLEY_H__
18 #define __BSDF_ASHIKHMIN_SHIRLEY_H__
19 
20 /*
21 ASHIKHMIN SHIRLEY BSDF
22 
23 Implementation of
24 Michael Ashikhmin and Peter Shirley: "An Anisotropic Phong BRDF Model" (2000)
25 
26 The Fresnel factor is missing to get a separable bsdf (intensity*color), as is
27 the case with all other microfacet-based BSDF implementations in Cycles.
28 
29 Other than that, the implementation directly follows the paper.
30 */
31 
33 
35 {
36  bsdf->alpha_x = clamp(bsdf->alpha_x, 1e-4f, 1.0f);
37  bsdf->alpha_y = clamp(bsdf->alpha_y, 1e-4f, 1.0f);
38 
40  return SD_BSDF | SD_BSDF_HAS_EVAL;
41 }
42 
44 {
45  MicrofacetBsdf *bsdf = (MicrofacetBsdf *)sc;
46 
47  bsdf->alpha_x = fmaxf(roughness, bsdf->alpha_x);
48  bsdf->alpha_y = fmaxf(roughness, bsdf->alpha_y);
49 }
50 
52 {
53  return 2.0f / (roughness * roughness) - 2.0f;
54 }
55 
57  const float3 I,
58  const float3 omega_in,
59  float *pdf)
60 {
61  const MicrofacetBsdf *bsdf = (const MicrofacetBsdf *)sc;
62  float3 N = bsdf->N;
63 
64  float NdotI = dot(N, I); /* in Cycles/OSL convention I is omega_out */
65  float NdotO = dot(N, omega_in); /* and consequently we use for O omaga_in ;) */
66 
67  float out = 0.0f;
68 
69  if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f)
70  return make_float3(0.0f, 0.0f, 0.0f);
71 
72  if (NdotI > 0.0f && NdotO > 0.0f) {
73  NdotI = fmaxf(NdotI, 1e-6f);
74  NdotO = fmaxf(NdotO, 1e-6f);
75  float3 H = normalize(omega_in + I);
76  float HdotI = fmaxf(fabsf(dot(H, I)), 1e-6f);
77  float HdotN = fmaxf(dot(H, N), 1e-6f);
78 
79  /* pump from original paper
80  * (first derivative disc., but cancels the HdotI in the pdf nicely) */
81  float pump = 1.0f / fmaxf(1e-6f, (HdotI * fmaxf(NdotO, NdotI)));
82  /* pump from d-brdf paper */
83  /*float pump = 1.0f / fmaxf(1e-4f, ((NdotO + NdotI) * (NdotO*NdotI))); */
84 
87 
88  if (n_x == n_y) {
89  /* isotropic */
90  float e = n_x;
91  float lobe = powf(HdotN, e);
92  float norm = (n_x + 1.0f) / (8.0f * M_PI_F);
93 
94  out = NdotO * norm * lobe * pump;
95  /* this is p_h / 4(H.I) (conversion from 'wh measure' to 'wi measure', eq. 8 in paper). */
96  *pdf = norm * lobe / HdotI;
97  }
98  else {
99  /* anisotropic */
100  float3 X, Y;
101  make_orthonormals_tangent(N, bsdf->T, &X, &Y);
102 
103  float HdotX = dot(H, X);
104  float HdotY = dot(H, Y);
105  float lobe;
106  if (HdotN < 1.0f) {
107  float e = (n_x * HdotX * HdotX + n_y * HdotY * HdotY) / (1.0f - HdotN * HdotN);
108  lobe = powf(HdotN, e);
109  }
110  else {
111  lobe = 1.0f;
112  }
113  float norm = sqrtf((n_x + 1.0f) * (n_y + 1.0f)) / (8.0f * M_PI_F);
114 
115  out = NdotO * norm * lobe * pump;
116  *pdf = norm * lobe / HdotI;
117  }
118  }
119 
120  return make_float3(out, out, out);
121 }
122 
124  const float3 I,
125  const float3 omega_in,
126  float *pdf)
127 {
128  return make_float3(0.0f, 0.0f, 0.0f);
129 }
130 
132  float n_x, float n_y, float randu, float randv, float *phi, float *cos_theta)
133 {
134  *phi = atanf(sqrtf((n_x + 1.0f) / (n_y + 1.0f)) * tanf(M_PI_2_F * randu));
135  float cos_phi = cosf(*phi);
136  float sin_phi = sinf(*phi);
137  *cos_theta = powf(randv, 1.0f / (n_x * cos_phi * cos_phi + n_y * sin_phi * sin_phi + 1.0f));
138 }
139 
141  float3 Ng,
142  float3 I,
143  float3 dIdx,
144  float3 dIdy,
145  float randu,
146  float randv,
147  float3 *eval,
148  float3 *omega_in,
149  float3 *domega_in_dx,
150  float3 *domega_in_dy,
151  float *pdf)
152 {
153  const MicrofacetBsdf *bsdf = (const MicrofacetBsdf *)sc;
154  float3 N = bsdf->N;
156 
157  float NdotI = dot(N, I);
158  if (NdotI > 0.0f) {
159 
162 
163  /* get x,y basis on the surface for anisotropy */
164  float3 X, Y;
165 
166  if (n_x == n_y)
167  make_orthonormals(N, &X, &Y);
168  else
169  make_orthonormals_tangent(N, bsdf->T, &X, &Y);
170 
171  /* sample spherical coords for h in tangent space */
172  float phi;
173  float cos_theta;
174  if (n_x == n_y) {
175  /* isotropic sampling */
176  phi = M_2PI_F * randu;
177  cos_theta = powf(randv, 1.0f / (n_x + 1.0f));
178  }
179  else {
180  /* anisotropic sampling */
181  if (randu < 0.25f) { /* first quadrant */
182  float remapped_randu = 4.0f * randu;
184  n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
185  }
186  else if (randu < 0.5f) { /* second quadrant */
187  float remapped_randu = 4.0f * (.5f - randu);
189  n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
190  phi = M_PI_F - phi;
191  }
192  else if (randu < 0.75f) { /* third quadrant */
193  float remapped_randu = 4.0f * (randu - 0.5f);
195  n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
196  phi = M_PI_F + phi;
197  }
198  else { /* fourth quadrant */
199  float remapped_randu = 4.0f * (1.0f - randu);
201  n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
202  phi = 2.0f * M_PI_F - phi;
203  }
204  }
205 
206  /* get half vector in tangent space */
207  float sin_theta = sqrtf(fmaxf(0.0f, 1.0f - cos_theta * cos_theta));
208  float cos_phi = cosf(phi);
209  float sin_phi = sinf(phi); /* no sqrt(1-cos^2) here b/c it causes artifacts */
210  float3 h = make_float3(sin_theta * cos_phi, sin_theta * sin_phi, cos_theta);
211 
212  /* half vector to world space */
213  float3 H = h.x * X + h.y * Y + h.z * N;
214  float HdotI = dot(H, I);
215  if (HdotI < 0.0f)
216  H = -H;
217 
218  /* reflect I on H to get omega_in */
219  *omega_in = -I + (2.0f * HdotI) * H;
220 
221  if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f) {
222  /* Some high number for MIS. */
223  *pdf = 1e6f;
224  *eval = make_float3(1e6f, 1e6f, 1e6f);
226  }
227  else {
228  /* leave the rest to eval_reflect */
229  *eval = bsdf_ashikhmin_shirley_eval_reflect(sc, I, *omega_in, pdf);
230  }
231 
232 #ifdef __RAY_DIFFERENTIALS__
233  /* just do the reflection thing for now */
234  *domega_in_dx = (2.0f * dot(N, dIdx)) * N - dIdx;
235  *domega_in_dy = (2.0f * dot(N, dIdy)) * N - dIdy;
236 #endif
237  }
238 
239  return label;
240 }
241 
243 
244 #endif /* __BSDF_ASHIKHMIN_SHIRLEY_H__ */
#define X
Definition: GeomUtils.cpp:213
#define Y
Definition: GeomUtils.cpp:214
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ccl_device void bsdf_ashikhmin_shirley_blur(ShaderClosure *sc, float roughness)
ccl_device_forceinline float3 bsdf_ashikhmin_shirley_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
CCL_NAMESPACE_BEGIN ccl_device int bsdf_ashikhmin_shirley_setup(MicrofacetBsdf *bsdf)
ccl_device_inline float bsdf_ashikhmin_shirley_roughness_to_exponent(float roughness)
ccl_device int bsdf_ashikhmin_shirley_sample(const ShaderClosure *sc, float3 Ng, float3 I, float3 dIdx, float3 dIdy, float randu, float randv, float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
ccl_device_inline void bsdf_ashikhmin_shirley_sample_first_quadrant(float n_x, float n_y, float randu, float randv, float *phi, float *cos_theta)
ccl_device float3 bsdf_ashikhmin_shirley_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition: btVector3.h:263
const char * label
#define ccl_device_forceinline
#define sinf(x)
#define cosf(x)
#define ccl_device
#define ccl_device_inline
#define tanf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
#define atanf(x)
#define fabsf(x)
#define sqrtf(x)
#define make_float3(x, y, z)
ccl_device void make_orthonormals_tangent(const float3 N, const float3 T, float3 *a, float3 *b)
@ SD_BSDF_HAS_EVAL
Definition: kernel_types.h:849
@ SD_BSDF
Definition: kernel_types.h:847
@ LABEL_SINGULAR
Definition: kernel_types.h:332
@ LABEL_GLOSSY
Definition: kernel_types.h:331
@ LABEL_REFLECT
Definition: kernel_types.h:329
ShaderClosure
Definition: kernel_types.h:831
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
#define I
params N
float z
Definition: sky_float3.h:35
float y
Definition: sky_float3.h:35
float x
Definition: sky_float3.h:35
@ CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID
Definition: svm_types.h:550
#define M_PI_2_F
Definition: util_math.h:46
ccl_device_inline void make_orthonormals(const float3 N, float3 *a, float3 *b)
Definition: util_math.h:477
#define M_2PI_F
Definition: util_math.h:69
#define M_PI_F
Definition: util_math.h:43
ccl_device_inline int clamp(int a, int mn, int mx)
Definition: util_math.h:283
ccl_device_inline float2 normalize(const float2 &a)
ccl_device_inline float dot(const float2 &a, const float2 &b)
#define H(x, y, z)