Cycles: change __device and similar qualifiers to ccl_device in kernel code.
[blender.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 #ifdef __ANISOTROPIC__
27 #include "../closure/bsdf_ward.h"
28 #endif
29 #include "../closure/bsdf_westin.h"
30 #include "../closure/bsdf_toon.h"
31 #include "../closure/bsdf_hair.h"
32 #ifdef __SUBSURFACE__
33 #include "../closure/bssrdf.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, sd->Ng, sd->I, sd->dI.dx, 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, sd->Ng, sd->I, sd->dI.dx, 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, sd->Ng, sd->I, sd->dI.dx, 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, sd->Ng, sd->I, sd->dI.dx, 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, sd->Ng, sd->I, sd->dI.dx, 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, sd->Ng, sd->I, sd->dI.dx, 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, sd->Ng, sd->I, sd->dI.dx, 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, sd->Ng, sd->I, sd->dI.dx, 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_REFRACTION_ID:
84                         label = bsdf_microfacet_ggx_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
85                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
86                         break;
87                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
88                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
89                         label = bsdf_microfacet_beckmann_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
90                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
91                         break;
92 #ifdef __ANISOTROPIC__
93                 case CLOSURE_BSDF_WARD_ID:
94                         label = bsdf_ward_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
95                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
96                         break;
97 #endif
98                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
99                         label = bsdf_ashikhmin_velvet_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
100                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
101                         break;
102                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
103                         label = bsdf_diffuse_toon_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
104                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
105                         break;
106                 case CLOSURE_BSDF_GLOSSY_TOON_ID:
107                         label = bsdf_glossy_toon_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
108                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
109                         break;
110                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
111                         label = bsdf_westin_backscatter_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
112                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
113                         break;
114                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
115                         label = bsdf_westin_sheen_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
116                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
117                         break;
118                 case CLOSURE_BSDF_HAIR_REFLECTION_ID:
119                         label = bsdf_hair_reflection_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
120                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
121                         break;
122                 case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
123                         label = bsdf_hair_transmission_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
124                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
125                         break;
126 #endif
127                 default:
128                         label = LABEL_NONE;
129                         break;
130         }
131
132         return label;
133 }
134
135 ccl_device float3 bsdf_eval(KernelGlobals *kg, const ShaderData *sd, const ShaderClosure *sc, const float3 omega_in, float *pdf)
136 {
137         float3 eval;
138
139 #ifdef __OSL__
140         if(kg->osl && sc->prim)
141                 return OSLShader::bsdf_eval(sd, sc, omega_in, *pdf);
142 #endif
143
144         if(dot(sd->Ng, omega_in) >= 0.0f) {
145                 switch(sc->type) {
146                         case CLOSURE_BSDF_DIFFUSE_ID:
147                         case CLOSURE_BSDF_BSSRDF_ID:
148                                 eval = bsdf_diffuse_eval_reflect(sc, sd->I, omega_in, pdf);
149                                 break;
150 #ifdef __SVM__
151                         case CLOSURE_BSDF_OREN_NAYAR_ID:
152                                 eval = bsdf_oren_nayar_eval_reflect(sc, sd->I, omega_in, pdf);
153                                 break;
154                         /*case CLOSURE_BSDF_PHONG_RAMP_ID:
155                                 eval = bsdf_phong_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
156                                 break;
157                         case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
158                                 eval = bsdf_diffuse_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
159                                 break;*/
160                         case CLOSURE_BSDF_TRANSLUCENT_ID:
161                                 eval = bsdf_translucent_eval_reflect(sc, sd->I, omega_in, pdf);
162                                 break;
163                         case CLOSURE_BSDF_REFLECTION_ID:
164                                 eval = bsdf_reflection_eval_reflect(sc, sd->I, omega_in, pdf);
165                                 break;
166                         case CLOSURE_BSDF_REFRACTION_ID:
167                                 eval = bsdf_refraction_eval_reflect(sc, sd->I, omega_in, pdf);
168                                 break;
169                         case CLOSURE_BSDF_TRANSPARENT_ID:
170                                 eval = bsdf_transparent_eval_reflect(sc, sd->I, omega_in, pdf);
171                                 break;
172                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
173                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
174                                 eval = bsdf_microfacet_ggx_eval_reflect(sc, sd->I, omega_in, pdf);
175                                 break;
176                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
177                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
178                                 eval = bsdf_microfacet_beckmann_eval_reflect(sc, sd->I, omega_in, pdf);
179                                 break;
180 #ifdef __ANISOTROPIC__
181                         case CLOSURE_BSDF_WARD_ID:
182                                 eval = bsdf_ward_eval_reflect(sc, sd->I, omega_in, pdf);
183                                 break;
184 #endif
185                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
186                                 eval = bsdf_ashikhmin_velvet_eval_reflect(sc, sd->I, omega_in, pdf);
187                                 break;
188                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
189                                 eval = bsdf_diffuse_toon_eval_reflect(sc, sd->I, omega_in, pdf);
190                                 break;
191                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
192                                 eval = bsdf_glossy_toon_eval_reflect(sc, sd->I, omega_in, pdf);
193                                 break;
194                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
195                                 eval = bsdf_westin_backscatter_eval_reflect(sc, sd->I, omega_in, pdf);
196                                 break;
197                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
198                                 eval = bsdf_westin_sheen_eval_reflect(sc, sd->I, omega_in, pdf);
199                                 break;
200                         case CLOSURE_BSDF_HAIR_REFLECTION_ID:
201                                 eval = bsdf_hair_reflection_eval_reflect(sc, sd->I, omega_in, pdf);
202                                 break;
203                         case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
204                                 eval = bsdf_hair_transmission_eval_reflect(sc, sd->I, omega_in, pdf);
205                                 break;
206 #endif
207                         default:
208                                 eval = make_float3(0.0f, 0.0f, 0.0f);
209                                 break;
210                 }
211         }
212         else {
213                 switch(sc->type) {
214                         case CLOSURE_BSDF_DIFFUSE_ID:
215                         case CLOSURE_BSDF_BSSRDF_ID:
216                                 eval = bsdf_diffuse_eval_transmit(sc, sd->I, omega_in, pdf);
217                                 break;
218 #ifdef __SVM__
219                         case CLOSURE_BSDF_OREN_NAYAR_ID:
220                                 eval = bsdf_oren_nayar_eval_transmit(sc, sd->I, omega_in, pdf);
221                                 break;
222                         case CLOSURE_BSDF_TRANSLUCENT_ID:
223                                 eval = bsdf_translucent_eval_transmit(sc, sd->I, omega_in, pdf);
224                                 break;
225                         case CLOSURE_BSDF_REFLECTION_ID:
226                                 eval = bsdf_reflection_eval_transmit(sc, sd->I, omega_in, pdf);
227                                 break;
228                         case CLOSURE_BSDF_REFRACTION_ID:
229                                 eval = bsdf_refraction_eval_transmit(sc, sd->I, omega_in, pdf);
230                                 break;
231                         case CLOSURE_BSDF_TRANSPARENT_ID:
232                                 eval = bsdf_transparent_eval_transmit(sc, sd->I, omega_in, pdf);
233                                 break;
234                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
235                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
236                                 eval = bsdf_microfacet_ggx_eval_transmit(sc, sd->I, omega_in, pdf);
237                                 break;
238                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
239                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
240                                 eval = bsdf_microfacet_beckmann_eval_transmit(sc, sd->I, omega_in, pdf);
241                                 break;
242 #ifdef __ANISOTROPIC__
243                         case CLOSURE_BSDF_WARD_ID:
244                                 eval = bsdf_ward_eval_transmit(sc, sd->I, omega_in, pdf);
245                                 break;
246 #endif
247                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
248                                 eval = bsdf_ashikhmin_velvet_eval_transmit(sc, sd->I, omega_in, pdf);
249                                 break;
250                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
251                                 eval = bsdf_diffuse_toon_eval_transmit(sc, sd->I, omega_in, pdf);
252                                 break;
253                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
254                                 eval = bsdf_glossy_toon_eval_transmit(sc, sd->I, omega_in, pdf);
255                                 break;
256                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
257                                 eval = bsdf_westin_backscatter_eval_transmit(sc, sd->I, omega_in, pdf);
258                                 break;
259                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
260                                 eval = bsdf_westin_sheen_eval_transmit(sc, sd->I, omega_in, pdf);
261                                 break;
262                         case CLOSURE_BSDF_HAIR_REFLECTION_ID:
263                                 eval = bsdf_hair_reflection_eval_transmit(sc, sd->I, omega_in, pdf);
264                                 break;
265                         case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
266                                 eval = bsdf_hair_transmission_eval_transmit(sc, sd->I, omega_in, pdf);
267                                 break;
268 #endif
269                         default:
270                                 eval = make_float3(0.0f, 0.0f, 0.0f);
271                                 break;
272                 }
273         }
274
275         return eval;
276 }
277
278 ccl_device void bsdf_blur(KernelGlobals *kg, ShaderClosure *sc, float roughness)
279 {
280 #ifdef __OSL__
281         if(kg->osl && sc->prim) {
282                 OSLShader::bsdf_blur(sc, roughness);
283                 return;
284         }
285 #endif
286
287         switch(sc->type) {
288                 case CLOSURE_BSDF_DIFFUSE_ID:
289                 case CLOSURE_BSDF_BSSRDF_ID:
290                         bsdf_diffuse_blur(sc, roughness);
291                         break;
292 #ifdef __SVM__
293                 case CLOSURE_BSDF_OREN_NAYAR_ID:
294                         bsdf_oren_nayar_blur(sc, roughness);
295                         break;
296                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
297                         bsdf_phong_ramp_blur(sc, roughness);
298                         break;
299                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
300                         bsdf_diffuse_ramp_blur(sc, roughness);
301                         break;*/
302                 case CLOSURE_BSDF_TRANSLUCENT_ID:
303                         bsdf_translucent_blur(sc, roughness);
304                         break;
305                 case CLOSURE_BSDF_REFLECTION_ID:
306                         bsdf_reflection_blur(sc, roughness);
307                         break;
308                 case CLOSURE_BSDF_REFRACTION_ID:
309                         bsdf_refraction_blur(sc, roughness);
310                         break;
311                 case CLOSURE_BSDF_TRANSPARENT_ID:
312                         bsdf_transparent_blur(sc, roughness);
313                         break;
314                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
315                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
316                         bsdf_microfacet_ggx_blur(sc, roughness);
317                         break;
318                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
319                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
320                         bsdf_microfacet_beckmann_blur(sc, roughness);
321                         break;
322 #ifdef __ANISOTROPIC__
323                 case CLOSURE_BSDF_WARD_ID:
324                         bsdf_ward_blur(sc, roughness);
325                         break;
326 #endif
327                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
328                         bsdf_ashikhmin_velvet_blur(sc, roughness);
329                         break;
330                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
331                         bsdf_diffuse_toon_blur(sc, roughness);
332                         break;
333                 case CLOSURE_BSDF_GLOSSY_TOON_ID:
334                         bsdf_glossy_toon_blur(sc, roughness);
335                         break;
336                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
337                         bsdf_westin_backscatter_blur(sc, roughness);
338                         break;
339                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
340                         bsdf_westin_sheen_blur(sc, roughness);
341                         break;
342                 case CLOSURE_BSDF_HAIR_REFLECTION_ID:
343                 case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
344                         bsdf_hair_reflection_blur(sc, roughness);
345                         break;
346 #endif
347                 default:
348                         break;
349         }
350 }
351
352 CCL_NAMESPACE_END
353