b2884953a223c63ae65a8cf524954af809b163c6
[blender.git] / intern / cycles / kernel / closure / bsdf.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "../closure/bsdf_ashikhmin_velvet.h"
20 #include "../closure/bsdf_diffuse.h"
21 #include "../closure/bsdf_oren_nayar.h"
22 #include "../closure/bsdf_phong_ramp.h"
23 #include "../closure/bsdf_diffuse_ramp.h"
24 #include "../closure/bsdf_microfacet.h"
25 #include "../closure/bsdf_reflection.h"
26 #include "../closure/bsdf_refraction.h"
27 #include "../closure/bsdf_transparent.h"
28 #ifdef __ANISOTROPIC__
29 #include "../closure/bsdf_ward.h"
30 #endif
31 #include "../closure/bsdf_westin.h"
32 #include "../closure/bsdf_toon.h"
33 #include "../closure/bssrdf.h"
34
35 CCL_NAMESPACE_BEGIN
36
37 __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)
38 {
39         int label;
40
41 #ifdef __OSL__
42         if(kg->osl && sc->prim)
43                 return OSLShader::bsdf_sample(sd, sc, randu, randv, *eval, *omega_in, *domega_in, *pdf);
44 #endif
45
46         switch(sc->type) {
47                 case CLOSURE_BSDF_DIFFUSE_ID:
48                         label = bsdf_diffuse_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
49                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
50                         break;
51 #ifdef __SVM__
52                 case CLOSURE_BSDF_OREN_NAYAR_ID:
53                         label = bsdf_oren_nayar_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
54                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
55                         break;
56                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
57                         label = bsdf_phong_ramp_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
58                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
59                         break;
60                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
61                         label = bsdf_diffuse_ramp_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
62                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
63                         break;*/
64                 case CLOSURE_BSDF_TRANSLUCENT_ID:
65                         label = bsdf_translucent_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
66                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
67                         break;
68                 case CLOSURE_BSDF_REFLECTION_ID:
69                         label = bsdf_reflection_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
70                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
71                         break;
72                 case CLOSURE_BSDF_REFRACTION_ID:
73                         label = bsdf_refraction_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
74                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
75                         break;
76                 case CLOSURE_BSDF_TRANSPARENT_ID:
77                         label = bsdf_transparent_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
78                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
79                         break;
80                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
81                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
82                         label = bsdf_microfacet_ggx_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
83                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
84                         break;
85                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
86                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
87                         label = bsdf_microfacet_beckmann_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
88                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
89                         break;
90 #ifdef __ANISOTROPIC__
91                 case CLOSURE_BSDF_WARD_ID:
92                         label = bsdf_ward_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
93                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
94                         break;
95 #endif
96                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
97                         label = bsdf_ashikhmin_velvet_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
98                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
99                         break;
100                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
101                         label = bsdf_diffuse_toon_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
102                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
103                         break;
104                 case CLOSURE_BSDF_GLOSSY_TOON_ID:
105                         label = bsdf_glossy_toon_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
106                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
107                         break;
108                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
109                         label = bsdf_westin_backscatter_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
110                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
111                         break;
112                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
113                         label = bsdf_westin_sheen_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
114                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
115                         break;
116 #endif
117                 default:
118                         label = LABEL_NONE;
119                         break;
120         }
121
122         return label;
123 }
124
125 __device float3 bsdf_eval(KernelGlobals *kg, const ShaderData *sd, const ShaderClosure *sc, const float3 omega_in, float *pdf)
126 {
127         float3 eval;
128
129 #ifdef __OSL__
130         if(kg->osl && sc->prim)
131                 return OSLShader::bsdf_eval(sd, sc, omega_in, *pdf);
132 #endif
133
134         if(dot(sd->Ng, omega_in) >= 0.0f) {
135                 switch(sc->type) {
136                         case CLOSURE_BSDF_DIFFUSE_ID:
137                                 eval = bsdf_diffuse_eval_reflect(sc, sd->I, omega_in, pdf);
138                                 break;
139 #ifdef __SVM__
140                         case CLOSURE_BSDF_OREN_NAYAR_ID:
141                                 eval = bsdf_oren_nayar_eval_reflect(sc, sd->I, omega_in, pdf);
142                                 break;
143                         /*case CLOSURE_BSDF_PHONG_RAMP_ID:
144                                 eval = bsdf_phong_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
145                                 break;
146                         case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
147                                 eval = bsdf_diffuse_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
148                                 break;*/
149                         case CLOSURE_BSDF_TRANSLUCENT_ID:
150                                 eval = bsdf_translucent_eval_reflect(sc, sd->I, omega_in, pdf);
151                                 break;
152                         case CLOSURE_BSDF_REFLECTION_ID:
153                                 eval = bsdf_reflection_eval_reflect(sc, sd->I, omega_in, pdf);
154                                 break;
155                         case CLOSURE_BSDF_REFRACTION_ID:
156                                 eval = bsdf_refraction_eval_reflect(sc, sd->I, omega_in, pdf);
157                                 break;
158                         case CLOSURE_BSDF_TRANSPARENT_ID:
159                                 eval = bsdf_transparent_eval_reflect(sc, sd->I, omega_in, pdf);
160                                 break;
161                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
162                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
163                                 eval = bsdf_microfacet_ggx_eval_reflect(sc, sd->I, omega_in, pdf);
164                                 break;
165                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
166                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
167                                 eval = bsdf_microfacet_beckmann_eval_reflect(sc, sd->I, omega_in, pdf);
168                                 break;
169 #ifdef __ANISOTROPIC__
170                         case CLOSURE_BSDF_WARD_ID:
171                                 eval = bsdf_ward_eval_reflect(sc, sd->I, omega_in, pdf);
172                                 break;
173 #endif
174                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
175                                 eval = bsdf_ashikhmin_velvet_eval_reflect(sc, sd->I, omega_in, pdf);
176                                 break;
177                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
178                                 eval = bsdf_diffuse_toon_eval_reflect(sc, sd->I, omega_in, pdf);
179                                 break;
180                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
181                                 eval = bsdf_glossy_toon_eval_reflect(sc, sd->I, omega_in, pdf);
182                                 break;
183                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
184                                 eval = bsdf_westin_backscatter_eval_reflect(sc, sd->I, omega_in, pdf);
185                                 break;
186                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
187                                 eval = bsdf_westin_sheen_eval_reflect(sc, sd->I, omega_in, pdf);
188                                 break;
189 #endif
190                         default:
191                                 eval = make_float3(0.0f, 0.0f, 0.0f);
192                                 break;
193                 }
194         }
195         else {
196                 switch(sc->type) {
197                         case CLOSURE_BSDF_DIFFUSE_ID:
198                                 eval = bsdf_diffuse_eval_transmit(sc, sd->I, omega_in, pdf);
199                                 break;
200 #ifdef __SVM__
201                         case CLOSURE_BSDF_OREN_NAYAR_ID:
202                                 eval = bsdf_oren_nayar_eval_transmit(sc, sd->I, omega_in, pdf);
203                                 break;
204                         case CLOSURE_BSDF_TRANSLUCENT_ID:
205                                 eval = bsdf_translucent_eval_transmit(sc, sd->I, omega_in, pdf);
206                                 break;
207                         case CLOSURE_BSDF_REFLECTION_ID:
208                                 eval = bsdf_reflection_eval_transmit(sc, sd->I, omega_in, pdf);
209                                 break;
210                         case CLOSURE_BSDF_REFRACTION_ID:
211                                 eval = bsdf_refraction_eval_transmit(sc, sd->I, omega_in, pdf);
212                                 break;
213                         case CLOSURE_BSDF_TRANSPARENT_ID:
214                                 eval = bsdf_transparent_eval_transmit(sc, sd->I, omega_in, pdf);
215                                 break;
216                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
217                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
218                                 eval = bsdf_microfacet_ggx_eval_transmit(sc, sd->I, omega_in, pdf);
219                                 break;
220                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
221                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
222                                 eval = bsdf_microfacet_beckmann_eval_transmit(sc, sd->I, omega_in, pdf);
223                                 break;
224 #ifdef __ANISOTROPIC__
225                         case CLOSURE_BSDF_WARD_ID:
226                                 eval = bsdf_ward_eval_transmit(sc, sd->I, omega_in, pdf);
227                                 break;
228 #endif
229                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
230                                 eval = bsdf_ashikhmin_velvet_eval_transmit(sc, sd->I, omega_in, pdf);
231                                 break;
232                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
233                                 eval = bsdf_diffuse_toon_eval_transmit(sc, sd->I, omega_in, pdf);
234                                 break;
235                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
236                                 eval = bsdf_glossy_toon_eval_transmit(sc, sd->I, omega_in, pdf);
237                                 break;
238                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
239                                 eval = bsdf_westin_backscatter_eval_transmit(sc, sd->I, omega_in, pdf);
240                                 break;
241                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
242                                 eval = bsdf_westin_sheen_eval_transmit(sc, sd->I, omega_in, pdf);
243                                 break;
244 #endif
245                         default:
246                                 eval = make_float3(0.0f, 0.0f, 0.0f);
247                                 break;
248                 }
249         }
250
251         return eval;
252 }
253
254 __device void bsdf_blur(KernelGlobals *kg, ShaderClosure *sc, float roughness)
255 {
256 #ifdef __OSL__
257         if(kg->osl && sc->prim) {
258                 OSLShader::bsdf_blur(sc, roughness);
259                 return;
260         }
261 #endif
262
263         switch(sc->type) {
264                 case CLOSURE_BSDF_DIFFUSE_ID:
265                         bsdf_diffuse_blur(sc, roughness);
266                         break;
267 #ifdef __SVM__
268                 case CLOSURE_BSDF_OREN_NAYAR_ID:
269                         bsdf_oren_nayar_blur(sc, roughness);
270                         break;
271                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
272                         bsdf_phong_ramp_blur(sc, roughness);
273                         break;
274                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
275                         bsdf_diffuse_ramp_blur(sc, roughness);
276                         break;*/
277                 case CLOSURE_BSDF_TRANSLUCENT_ID:
278                         bsdf_translucent_blur(sc, roughness);
279                         break;
280                 case CLOSURE_BSDF_REFLECTION_ID:
281                         bsdf_reflection_blur(sc, roughness);
282                         break;
283                 case CLOSURE_BSDF_REFRACTION_ID:
284                         bsdf_refraction_blur(sc, roughness);
285                         break;
286                 case CLOSURE_BSDF_TRANSPARENT_ID:
287                         bsdf_transparent_blur(sc, roughness);
288                         break;
289                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
290                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
291                         bsdf_microfacet_ggx_blur(sc, roughness);
292                         break;
293                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
294                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
295                         bsdf_microfacet_beckmann_blur(sc, roughness);
296                         break;
297 #ifdef __ANISOTROPIC__
298                 case CLOSURE_BSDF_WARD_ID:
299                         bsdf_ward_blur(sc, roughness);
300                         break;
301 #endif
302                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
303                         bsdf_ashikhmin_velvet_blur(sc, roughness);
304                         break;
305                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
306                         bsdf_diffuse_toon_blur(sc, roughness);
307                         break;
308                 case CLOSURE_BSDF_GLOSSY_TOON_ID:
309                         bsdf_glossy_toon_blur(sc, roughness);
310                         break;
311                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
312                         bsdf_westin_backscatter_blur(sc, roughness);
313                         break;
314                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
315                         bsdf_westin_sheen_blur(sc, roughness);
316                         break;
317 #endif
318                 default:
319                         break;
320         }
321 }
322
323 CCL_NAMESPACE_END
324