Cycles: glossy and anisotropic BSDF changes
[blender.git] / intern / cycles / kernel / closure / bsdf_ashikhmin_shirley.h
1 /*
2  * Copyright 2011-2013 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         /* store roughness. could already convert to exponent to save some cycles
37          * in eval, but this is more consistent with other bsdfs and shader_blur. */
38         sc->data0 = clamp(sc->data0, 1e-4f, 1.0f);
39         sc->data1 = sc->data0;
40
41         sc->type = CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID;
42         return SD_BSDF | SD_BSDF_HAS_EVAL | SD_BSDF_GLOSSY;
43 }
44
45 ccl_device int bsdf_ashikhmin_shirley_aniso_setup(ShaderClosure *sc)
46 {
47         /* store roughness. could already convert to exponent to save some cycles
48          * in eval, but this is more consistent with other bsdfs and shader_blur. */
49         sc->data0 = clamp(sc->data0, 1e-4f, 1.0f);
50         sc->data1 = clamp(sc->data1, 1e-4f, 1.0f);
51
52         sc->type = CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID;
53         return SD_BSDF | SD_BSDF_HAS_EVAL | SD_BSDF_GLOSSY;
54 }
55
56 ccl_device void bsdf_ashikhmin_shirley_blur(ShaderClosure *sc, float roughness)
57 {
58         sc->data0 = fmaxf(roughness, sc->data0); /* clamp roughness */
59         sc->data1 = fmaxf(roughness, sc->data1);
60 }
61
62 ccl_device_inline float bsdf_ashikhmin_shirley_roughness_to_exponent(float roughness)
63 {
64         return 2.0f / (roughness*roughness) - 2.0f;
65 }
66
67 ccl_device float3 bsdf_ashikhmin_shirley_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
68 {
69         float3 N = sc->N;
70
71         float NdotI = dot(N, I);           /* in Cycles/OSL convention I is omega_out    */
72         float NdotO = dot(N, omega_in);    /* and consequently we use for O omaga_in ;)  */
73
74         float out = 0.0f;
75
76         if (NdotI > 0.0f && NdotO > 0.0f) {
77                 NdotI = fmaxf(NdotI, 1e-6f);
78                 NdotO = fmaxf(NdotO, 1e-6f);
79                 float3 H = normalize(omega_in + I);
80                 float HdotI = fmaxf(dot(H, I), 1e-6f);
81                 float HdotN = fmaxf(dot(H, N), 1e-6f);
82
83                 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) */
84                 /*float pump = 1.0f / fmaxf(1e-4f, ((NdotO + NdotI) * (NdotO*NdotI))); */ /* pump from d-brdf paper */
85
86                 float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data0);
87                 float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data1);
88
89                 if (n_x == n_y) {  /* => isotropic case */
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                         *pdf = norm * lobe / HdotI; /* this is p_h / 4(H.I)  (conversion from 'wh measure' to 'wi measure', eq. 8 in paper) */
96                 }
97                 else {             /* => ANisotropic case */
98                         float3 X, Y;
99                         make_orthonormals_tangent(N, sc->T, &X, &Y);
100
101                         float HdotX = dot(H, X);
102                         float HdotY = dot(H, Y);
103                         float e = (n_x * HdotX*HdotX + n_y * HdotY*HdotY) / (1.0f - HdotN*HdotN);
104                         float lobe = powf(HdotN, e);
105                         float norm = sqrtf((n_x + 1.0f)*(n_y + 1.0f)) / (8.0f * M_PI_F);
106                         
107                         out = NdotO * norm * lobe * pump;
108                         *pdf = norm * lobe / HdotI;
109                 }
110         }
111
112         return make_float3(out, out, out);
113 }
114
115 ccl_device float3 bsdf_ashikhmin_shirley_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
116 {
117         return make_float3(0.0f, 0.0f, 0.0f);
118 }
119
120 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)
121 {
122         *phi = atanf(sqrtf((n_x + 1.0f) / (n_y + 1.0f)) * tanf(M_PI_2_F * randu));
123         float cos_phi = cosf(*phi);
124         float sin_phi = sinf(*phi);
125         *cos_theta = powf(randv, 1.0f / (n_x * cos_phi*cos_phi + n_y * sin_phi*sin_phi + 1.0f));
126 }
127
128 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)
129 {
130         float3 N = sc->N;
131
132         float NdotI = dot(N, I);
133         if (NdotI > 0.0f) {
134
135                 float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data0);
136                 float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(sc->data1);
137
138                 /* get x,y basis on the surface for anisotropy */
139                 float3 X, Y;
140
141                 if(n_x == n_y)
142                         make_orthonormals(N, &X, &Y);
143                 else
144                         make_orthonormals_tangent(N, sc->T, &X, &Y);
145
146                 /* sample spherical coords for h in tangent space */
147                 float phi;
148                 float cos_theta;
149                 if (n_x == n_y) {  /* => simple isotropic sampling */
150                         phi = M_2PI_F * randu;
151                         cos_theta = powf(randv, 1.0f / (n_x + 1.0f));
152                 }
153                 else {             /* => more complex 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                 /* (could maybe optimize a few things by manual inlining, but I doubt it would make much difference) */
195                 *eval = bsdf_ashikhmin_shirley_eval_reflect(sc, I, *omega_in, pdf);
196
197 #ifdef __RAY_DIFFERENTIALS__
198                 /* just do the reflection thing for now */
199                 *domega_in_dx = (2.0f * dot(N, dIdx)) * N - dIdx;
200                 *domega_in_dy = (2.0f * dot(N, dIdy)) * N - dIdy;
201 #endif
202         }
203
204         return LABEL_REFLECT | LABEL_GLOSSY;
205 }
206
207
208 CCL_NAMESPACE_END
209
210 #endif /* __BSDF_ASHIKHMIN_SHIRLEY_H__ */