Cleanup: Style fixes for closures, mainly bitflags and conditions.
[blender.git] / intern / cycles / kernel / closure / bsdf_ashikhmin_shirley.h
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
32 CCL_NAMESPACE_BEGIN
33
34 ccl_device int bsdf_ashikhmin_shirley_setup(ShaderClosure *sc)
35 {
36         sc->data0 = clamp(sc->data0, 1e-4f, 1.0f);
37         sc->data1 = sc->data0;
38
39         sc->type = CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID;
40         return SD_BSDF|SD_BSDF_HAS_EVAL|SD_BSDF_GLOSSY;
41 }
42
43 ccl_device int bsdf_ashikhmin_shirley_aniso_setup(ShaderClosure *sc)
44 {
45         sc->data0 = clamp(sc->data0, 1e-4f, 1.0f);
46         sc->data1 = clamp(sc->data1, 1e-4f, 1.0f);
47
48         sc->type = CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID;
49         return SD_BSDF|SD_BSDF_HAS_EVAL|SD_BSDF_GLOSSY;
50 }
51
52 ccl_device void bsdf_ashikhmin_shirley_blur(ShaderClosure *sc, float roughness)
53 {
54         sc->data0 = fmaxf(roughness, sc->data0); /* clamp roughness */
55         sc->data1 = fmaxf(roughness, sc->data1);
56 }
57
58 ccl_device_inline float bsdf_ashikhmin_shirley_roughness_to_exponent(float roughness)
59 {
60         return 2.0f / (roughness*roughness) - 2.0f;
61 }
62
63 ccl_device float3 bsdf_ashikhmin_shirley_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
64 {
65         float3 N = sc->N;
66
67         float NdotI = dot(N, I);           /* in Cycles/OSL convention I is omega_out    */
68         float NdotO = dot(N, omega_in);    /* and consequently we use for O omaga_in ;)  */
69
70         float out = 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                 float pump = 1.0f / fmaxf(1e-6f, (HdotI*fmaxf(NdotO, NdotI))); /* pump from original paper (first derivative disc., but cancels the HdotI in the pdf nicely) */
80                 /*float pump = 1.0f / fmaxf(1e-4f, ((NdotO + NdotI) * (NdotO*NdotI))); */ /* pump from d-brdf paper */
81
82                 float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data0);
83                 float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data1);
84
85                 if(n_x == n_y) {
86                         /* isotropic */
87                         float e = n_x;
88                         float lobe = powf(HdotN, e);
89                         float norm = (n_x + 1.0f) / (8.0f * M_PI_F);
90
91                         out = NdotO * norm * lobe * pump;
92                         *pdf = norm * lobe / HdotI; /* this is p_h / 4(H.I)  (conversion from 'wh measure' to 'wi measure', eq. 8 in paper) */
93                 }
94                 else {
95                         /* anisotropic */
96                         float3 X, Y;
97                         make_orthonormals_tangent(N, sc->T, &X, &Y);
98
99                         float HdotX = dot(H, X);
100                         float HdotY = dot(H, Y);
101                         float e = (n_x * HdotX*HdotX + n_y * HdotY*HdotY) / (1.0f - HdotN*HdotN);
102                         float lobe = powf(HdotN, e);
103                         float norm = sqrtf((n_x + 1.0f)*(n_y + 1.0f)) / (8.0f * M_PI_F);
104                         
105                         out = NdotO * norm * lobe * pump;
106                         *pdf = norm * lobe / HdotI;
107                 }
108         }
109
110         return make_float3(out, out, out);
111 }
112
113 ccl_device float3 bsdf_ashikhmin_shirley_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
114 {
115         return make_float3(0.0f, 0.0f, 0.0f);
116 }
117
118 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)
119 {
120         *phi = atanf(sqrtf((n_x + 1.0f) / (n_y + 1.0f)) * tanf(M_PI_2_F * randu));
121         float cos_phi = cosf(*phi);
122         float sin_phi = sinf(*phi);
123         *cos_theta = powf(randv, 1.0f / (n_x * cos_phi*cos_phi + n_y * sin_phi*sin_phi + 1.0f));
124 }
125
126 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)
127 {
128         float3 N = sc->N;
129
130         float NdotI = dot(N, I);
131         if(NdotI > 0.0f) {
132
133                 float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data0);
134                 float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data1);
135
136                 /* get x,y basis on the surface for anisotropy */
137                 float3 X, Y;
138
139                 if(n_x == n_y)
140                         make_orthonormals(N, &X, &Y);
141                 else
142                         make_orthonormals_tangent(N, sc->T, &X, &Y);
143
144                 /* sample spherical coords for h in tangent space */
145                 float phi;
146                 float cos_theta;
147                 if(n_x == n_y) {
148                         /* isotropic sampling */
149                         phi = M_2PI_F * randu;
150                         cos_theta = powf(randv, 1.0f / (n_x + 1.0f));
151                 }
152                 else {
153                         /* anisotropic sampling */
154                         if(randu < 0.25f) {      /* first quadrant */
155                                 float remapped_randu = 4.0f * randu;
156                                 bsdf_ashikhmin_shirley_sample_first_quadrant(n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
157                         }
158                         else if(randu < 0.5f) {  /* second quadrant */
159                                 float remapped_randu = 4.0f * (.5f - randu);
160                                 bsdf_ashikhmin_shirley_sample_first_quadrant(n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
161                                 phi = M_PI_F - phi;
162                         }
163                         else if(randu < 0.75f) { /* third quadrant */
164                                 float remapped_randu = 4.0f * (randu - 0.5f);
165                                 bsdf_ashikhmin_shirley_sample_first_quadrant(n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
166                                 phi = M_PI_F + phi;
167                         }
168                         else {                   /* fourth quadrant */
169                                 float remapped_randu = 4.0f * (1.0f - randu);
170                                 bsdf_ashikhmin_shirley_sample_first_quadrant(n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
171                                 phi = 2.0f * M_PI_F - phi;
172                         }
173                 }
174
175                 /* get half vector in tangent space */
176                 float sin_theta = sqrtf(fmaxf(0.0f, 1.0f - cos_theta*cos_theta));
177                 float cos_phi = cosf(phi);
178                 float sin_phi = sinf(phi); /* no sqrt(1-cos^2) here b/c it causes artifacts */
179                 float3 h = make_float3(
180                         sin_theta * cos_phi,
181                         sin_theta * sin_phi,
182                         cos_theta
183                         );
184
185                 /* half vector to world space */
186                 float3 H = h.x*X + h.y*Y + h.z*N;
187                 float HdotI = dot(H, I);
188                 if(HdotI < 0.0f) H = -H;
189
190                 /* reflect I on H to get omega_in */
191                 *omega_in = -I + (2.0f * HdotI) * H;
192
193                 /* leave the rest to eval_reflect */
194                 *eval = bsdf_ashikhmin_shirley_eval_reflect(sc, I, *omega_in, pdf);
195
196 #ifdef __RAY_DIFFERENTIALS__
197                 /* just do the reflection thing for now */
198                 *domega_in_dx = (2.0f * dot(N, dIdx)) * N - dIdx;
199                 *domega_in_dy = (2.0f * dot(N, dIdy)) * N - dIdy;
200 #endif
201         }
202
203         return LABEL_REFLECT|LABEL_GLOSSY;
204 }
205
206
207 CCL_NAMESPACE_END
208
209 #endif /* __BSDF_ASHIKHMIN_SHIRLEY_H__ */