Cycles: Cleanup, indent nested preprocessor directives
[blender-staging.git] / intern / cycles / kernel / closure / bsdf.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 #include "../closure/bsdf_ashikhmin_velvet.h"
18 #include "../closure/bsdf_diffuse.h"
19 #include "../closure/bsdf_oren_nayar.h"
20 #include "../closure/bsdf_phong_ramp.h"
21 #include "../closure/bsdf_diffuse_ramp.h"
22 #include "../closure/bsdf_microfacet.h"
23 #include "../closure/bsdf_reflection.h"
24 #include "../closure/bsdf_refraction.h"
25 #include "../closure/bsdf_transparent.h"
26 #include "../closure/bsdf_ashikhmin_shirley.h"
27 #include "../closure/bsdf_toon.h"
28 #include "../closure/bsdf_hair.h"
29 #ifdef __SUBSURFACE__
30 #  include "../closure/bssrdf.h"
31 #endif
32 #ifdef __VOLUME__
33 #  include "../closure/volume.h"
34 #endif
35
36 CCL_NAMESPACE_BEGIN
37
38 ccl_device int bsdf_sample(KernelGlobals *kg, const ShaderData *sd, const ShaderClosure *sc, float randu, float randv, float3 *eval, float3 *omega_in, differential3 *domega_in, float *pdf)
39 {
40         int label;
41
42 #ifdef __OSL__
43         if(kg->osl && sc->prim)
44                 return OSLShader::bsdf_sample(sd, sc, randu, randv, *eval, *omega_in, *domega_in, *pdf);
45 #endif
46
47         switch(sc->type) {
48                 case CLOSURE_BSDF_DIFFUSE_ID:
49                 case CLOSURE_BSDF_BSSRDF_ID:
50                         label = bsdf_diffuse_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
51                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
52                         break;
53 #ifdef __SVM__
54                 case CLOSURE_BSDF_OREN_NAYAR_ID:
55                         label = bsdf_oren_nayar_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
56                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
57                         break;
58                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
59                         label = bsdf_phong_ramp_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
60                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
61                         break;
62                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
63                         label = bsdf_diffuse_ramp_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
64                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
65                         break;*/
66                 case CLOSURE_BSDF_TRANSLUCENT_ID:
67                         label = bsdf_translucent_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
68                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
69                         break;
70                 case CLOSURE_BSDF_REFLECTION_ID:
71                         label = bsdf_reflection_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
72                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
73                         break;
74                 case CLOSURE_BSDF_REFRACTION_ID:
75                         label = bsdf_refraction_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
76                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
77                         break;
78                 case CLOSURE_BSDF_TRANSPARENT_ID:
79                         label = bsdf_transparent_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
80                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
81                         break;
82                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
83                 case CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID:
84                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
85                         label = bsdf_microfacet_ggx_sample(kg, sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
86                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
87                         break;
88                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
89                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID:
90                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
91                         label = bsdf_microfacet_beckmann_sample(kg, sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
92                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
93                         break;
94                 case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID:
95                 case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID:
96                         label = bsdf_ashikhmin_shirley_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
97                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
98                         break;
99                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
100                         label = bsdf_ashikhmin_velvet_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
101                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
102                         break;
103                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
104                         label = bsdf_diffuse_toon_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
105                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
106                         break;
107                 case CLOSURE_BSDF_GLOSSY_TOON_ID:
108                         label = bsdf_glossy_toon_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
109                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
110                         break;
111                 case CLOSURE_BSDF_HAIR_REFLECTION_ID:
112                         label = bsdf_hair_reflection_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
113                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
114                         break;
115                 case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
116                         label = bsdf_hair_transmission_sample(sc, ccl_fetch(sd, Ng), ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv,
117                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
118                         break;
119 #endif
120 #ifdef __VOLUME__
121                 case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
122                         label = volume_henyey_greenstein_sample(sc, ccl_fetch(sd, I), ccl_fetch(sd, dI).dx, ccl_fetch(sd, dI).dy, randu, randv, eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
123                         break;
124 #endif
125                 default:
126                         label = LABEL_NONE;
127                         break;
128         }
129
130         return label;
131 }
132
133 ccl_device float3 bsdf_eval(KernelGlobals *kg, const ShaderData *sd, const ShaderClosure *sc, const float3 omega_in, float *pdf)
134 {
135         float3 eval;
136
137 #ifdef __OSL__
138         if(kg->osl && sc->prim)
139                 return OSLShader::bsdf_eval(sd, sc, omega_in, *pdf);
140 #endif
141
142         if(dot(ccl_fetch(sd, Ng), omega_in) >= 0.0f) {
143                 switch(sc->type) {
144                         case CLOSURE_BSDF_DIFFUSE_ID:
145                         case CLOSURE_BSDF_BSSRDF_ID:
146                                 eval = bsdf_diffuse_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
147                                 break;
148 #ifdef __SVM__
149                         case CLOSURE_BSDF_OREN_NAYAR_ID:
150                                 eval = bsdf_oren_nayar_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
151                                 break;
152                         /*case CLOSURE_BSDF_PHONG_RAMP_ID:
153                                 eval = bsdf_phong_ramp_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
154                                 break;
155                         case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
156                                 eval = bsdf_diffuse_ramp_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
157                                 break;*/
158                         case CLOSURE_BSDF_TRANSLUCENT_ID:
159                                 eval = bsdf_translucent_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
160                                 break;
161                         case CLOSURE_BSDF_REFLECTION_ID:
162                                 eval = bsdf_reflection_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
163                                 break;
164                         case CLOSURE_BSDF_REFRACTION_ID:
165                                 eval = bsdf_refraction_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
166                                 break;
167                         case CLOSURE_BSDF_TRANSPARENT_ID:
168                                 eval = bsdf_transparent_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
169                                 break;
170                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
171                         case CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID:
172                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
173                                 eval = bsdf_microfacet_ggx_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
174                                 break;
175                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
176                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID:
177                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
178                                 eval = bsdf_microfacet_beckmann_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
179                                 break;
180                         case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID:
181                         case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID:
182                                 eval = bsdf_ashikhmin_shirley_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
183                                 break;
184                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
185                                 eval = bsdf_ashikhmin_velvet_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
186                                 break;
187                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
188                                 eval = bsdf_diffuse_toon_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
189                                 break;
190                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
191                                 eval = bsdf_glossy_toon_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
192                                 break;
193                         case CLOSURE_BSDF_HAIR_REFLECTION_ID:
194                                 eval = bsdf_hair_reflection_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
195                                 break;
196                         case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
197                                 eval = bsdf_hair_transmission_eval_reflect(sc, ccl_fetch(sd, I), omega_in, pdf);
198                                 break;
199 #endif
200 #ifdef __VOLUME__
201                         case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
202                                 eval = volume_henyey_greenstein_eval_phase(sc, ccl_fetch(sd, I), omega_in, pdf);
203                                 break;
204 #endif
205                         default:
206                                 eval = make_float3(0.0f, 0.0f, 0.0f);
207                                 break;
208                 }
209         }
210         else {
211                 switch(sc->type) {
212                         case CLOSURE_BSDF_DIFFUSE_ID:
213                         case CLOSURE_BSDF_BSSRDF_ID:
214                                 eval = bsdf_diffuse_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
215                                 break;
216 #ifdef __SVM__
217                         case CLOSURE_BSDF_OREN_NAYAR_ID:
218                                 eval = bsdf_oren_nayar_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
219                                 break;
220                         case CLOSURE_BSDF_TRANSLUCENT_ID:
221                                 eval = bsdf_translucent_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
222                                 break;
223                         case CLOSURE_BSDF_REFLECTION_ID:
224                                 eval = bsdf_reflection_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
225                                 break;
226                         case CLOSURE_BSDF_REFRACTION_ID:
227                                 eval = bsdf_refraction_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
228                                 break;
229                         case CLOSURE_BSDF_TRANSPARENT_ID:
230                                 eval = bsdf_transparent_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
231                                 break;
232                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
233                         case CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID:
234                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
235                                 eval = bsdf_microfacet_ggx_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
236                                 break;
237                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
238                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID:
239                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
240                                 eval = bsdf_microfacet_beckmann_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
241                                 break;
242                         case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID:
243                         case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID:
244                                 eval = bsdf_ashikhmin_shirley_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
245                                 break;
246                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
247                                 eval = bsdf_ashikhmin_velvet_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
248                                 break;
249                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
250                                 eval = bsdf_diffuse_toon_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
251                                 break;
252                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
253                                 eval = bsdf_glossy_toon_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
254                                 break;
255                         case CLOSURE_BSDF_HAIR_REFLECTION_ID:
256                                 eval = bsdf_hair_reflection_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
257                                 break;
258                         case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
259                                 eval = bsdf_hair_transmission_eval_transmit(sc, ccl_fetch(sd, I), omega_in, pdf);
260                                 break;
261 #endif
262 #ifdef __VOLUME__
263                         case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
264                                 eval = volume_henyey_greenstein_eval_phase(sc, ccl_fetch(sd, I), omega_in, pdf);
265                                 break;
266 #endif
267                         default:
268                                 eval = make_float3(0.0f, 0.0f, 0.0f);
269                                 break;
270                 }
271         }
272
273         return eval;
274 }
275
276 ccl_device void bsdf_blur(KernelGlobals *kg, ShaderClosure *sc, float roughness)
277 {
278 /* ToDo: do we want to blur volume closures? */
279
280 #ifdef __OSL__
281         if(kg->osl && sc->prim) {
282                 OSLShader::bsdf_blur(sc, roughness);
283                 return;
284         }
285 #endif
286
287 #ifdef __SVM__
288         switch(sc->type) {
289                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
290                 case CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID:
291                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
292                         bsdf_microfacet_ggx_blur(sc, roughness);
293                         break;
294                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
295                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID:
296                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
297                         bsdf_microfacet_beckmann_blur(sc, roughness);
298                         break;
299                 case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID:
300                 case CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID:
301                         bsdf_ashikhmin_shirley_blur(sc, roughness);
302                         break;
303                 default:
304                         break;
305         }
306 #endif
307 }
308
309 CCL_NAMESPACE_END
310