Cycles / SSS:
[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                 case CLOSURE_BSDF_BSSRDF_ID:
49                         label = bsdf_diffuse_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
50                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
51                         break;
52 #ifdef __SVM__
53                 case CLOSURE_BSDF_OREN_NAYAR_ID:
54                         label = bsdf_oren_nayar_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
55                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
56                         break;
57                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
58                         label = bsdf_phong_ramp_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
59                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
60                         break;
61                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
62                         label = bsdf_diffuse_ramp_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
63                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
64                         break;*/
65                 case CLOSURE_BSDF_TRANSLUCENT_ID:
66                         label = bsdf_translucent_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
67                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
68                         break;
69                 case CLOSURE_BSDF_REFLECTION_ID:
70                         label = bsdf_reflection_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
71                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
72                         break;
73                 case CLOSURE_BSDF_REFRACTION_ID:
74                         label = bsdf_refraction_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
75                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
76                         break;
77                 case CLOSURE_BSDF_TRANSPARENT_ID:
78                         label = bsdf_transparent_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
79                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
80                         break;
81                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
82                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
83                         label = bsdf_microfacet_ggx_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
84                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
85                         break;
86                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
87                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
88                         label = bsdf_microfacet_beckmann_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
89                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
90                         break;
91 #ifdef __ANISOTROPIC__
92                 case CLOSURE_BSDF_WARD_ID:
93                         label = bsdf_ward_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
94                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
95                         break;
96 #endif
97                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
98                         label = bsdf_ashikhmin_velvet_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
99                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
100                         break;
101                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
102                         label = bsdf_diffuse_toon_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
103                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
104                         break;
105                 case CLOSURE_BSDF_GLOSSY_TOON_ID:
106                         label = bsdf_glossy_toon_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
107                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
108                         break;
109                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
110                         label = bsdf_westin_backscatter_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
111                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
112                         break;
113                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
114                         label = bsdf_westin_sheen_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
115                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
116                         break;
117 #endif
118                 default:
119                         label = LABEL_NONE;
120                         break;
121         }
122
123         return label;
124 }
125
126 __device float3 bsdf_eval(KernelGlobals *kg, const ShaderData *sd, const ShaderClosure *sc, const float3 omega_in, float *pdf)
127 {
128         float3 eval;
129
130 #ifdef __OSL__
131         if(kg->osl && sc->prim)
132                 return OSLShader::bsdf_eval(sd, sc, omega_in, *pdf);
133 #endif
134
135         if(dot(sd->Ng, omega_in) >= 0.0f) {
136                 switch(sc->type) {
137                         case CLOSURE_BSDF_DIFFUSE_ID:
138                         case CLOSURE_BSDF_BSSRDF_ID:
139                                 eval = bsdf_diffuse_eval_reflect(sc, sd->I, omega_in, pdf);
140                                 break;
141 #ifdef __SVM__
142                         case CLOSURE_BSDF_OREN_NAYAR_ID:
143                                 eval = bsdf_oren_nayar_eval_reflect(sc, sd->I, omega_in, pdf);
144                                 break;
145                         /*case CLOSURE_BSDF_PHONG_RAMP_ID:
146                                 eval = bsdf_phong_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
147                                 break;
148                         case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
149                                 eval = bsdf_diffuse_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
150                                 break;*/
151                         case CLOSURE_BSDF_TRANSLUCENT_ID:
152                                 eval = bsdf_translucent_eval_reflect(sc, sd->I, omega_in, pdf);
153                                 break;
154                         case CLOSURE_BSDF_REFLECTION_ID:
155                                 eval = bsdf_reflection_eval_reflect(sc, sd->I, omega_in, pdf);
156                                 break;
157                         case CLOSURE_BSDF_REFRACTION_ID:
158                                 eval = bsdf_refraction_eval_reflect(sc, sd->I, omega_in, pdf);
159                                 break;
160                         case CLOSURE_BSDF_TRANSPARENT_ID:
161                                 eval = bsdf_transparent_eval_reflect(sc, sd->I, omega_in, pdf);
162                                 break;
163                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
164                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
165                                 eval = bsdf_microfacet_ggx_eval_reflect(sc, sd->I, omega_in, pdf);
166                                 break;
167                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
168                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
169                                 eval = bsdf_microfacet_beckmann_eval_reflect(sc, sd->I, omega_in, pdf);
170                                 break;
171 #ifdef __ANISOTROPIC__
172                         case CLOSURE_BSDF_WARD_ID:
173                                 eval = bsdf_ward_eval_reflect(sc, sd->I, omega_in, pdf);
174                                 break;
175 #endif
176                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
177                                 eval = bsdf_ashikhmin_velvet_eval_reflect(sc, sd->I, omega_in, pdf);
178                                 break;
179                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
180                                 eval = bsdf_diffuse_toon_eval_reflect(sc, sd->I, omega_in, pdf);
181                                 break;
182                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
183                                 eval = bsdf_glossy_toon_eval_reflect(sc, sd->I, omega_in, pdf);
184                                 break;
185                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
186                                 eval = bsdf_westin_backscatter_eval_reflect(sc, sd->I, omega_in, pdf);
187                                 break;
188                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
189                                 eval = bsdf_westin_sheen_eval_reflect(sc, sd->I, omega_in, pdf);
190                                 break;
191 #endif
192                         default:
193                                 eval = make_float3(0.0f, 0.0f, 0.0f);
194                                 break;
195                 }
196         }
197         else {
198                 switch(sc->type) {
199                         case CLOSURE_BSDF_DIFFUSE_ID:
200                         case CLOSURE_BSDF_BSSRDF_ID:
201                                 eval = bsdf_diffuse_eval_transmit(sc, sd->I, omega_in, pdf);
202                                 break;
203 #ifdef __SVM__
204                         case CLOSURE_BSDF_OREN_NAYAR_ID:
205                                 eval = bsdf_oren_nayar_eval_transmit(sc, sd->I, omega_in, pdf);
206                                 break;
207                         case CLOSURE_BSDF_TRANSLUCENT_ID:
208                                 eval = bsdf_translucent_eval_transmit(sc, sd->I, omega_in, pdf);
209                                 break;
210                         case CLOSURE_BSDF_REFLECTION_ID:
211                                 eval = bsdf_reflection_eval_transmit(sc, sd->I, omega_in, pdf);
212                                 break;
213                         case CLOSURE_BSDF_REFRACTION_ID:
214                                 eval = bsdf_refraction_eval_transmit(sc, sd->I, omega_in, pdf);
215                                 break;
216                         case CLOSURE_BSDF_TRANSPARENT_ID:
217                                 eval = bsdf_transparent_eval_transmit(sc, sd->I, omega_in, pdf);
218                                 break;
219                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
220                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
221                                 eval = bsdf_microfacet_ggx_eval_transmit(sc, sd->I, omega_in, pdf);
222                                 break;
223                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
224                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
225                                 eval = bsdf_microfacet_beckmann_eval_transmit(sc, sd->I, omega_in, pdf);
226                                 break;
227 #ifdef __ANISOTROPIC__
228                         case CLOSURE_BSDF_WARD_ID:
229                                 eval = bsdf_ward_eval_transmit(sc, sd->I, omega_in, pdf);
230                                 break;
231 #endif
232                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
233                                 eval = bsdf_ashikhmin_velvet_eval_transmit(sc, sd->I, omega_in, pdf);
234                                 break;
235                         case CLOSURE_BSDF_DIFFUSE_TOON_ID:
236                                 eval = bsdf_diffuse_toon_eval_transmit(sc, sd->I, omega_in, pdf);
237                                 break;
238                         case CLOSURE_BSDF_GLOSSY_TOON_ID:
239                                 eval = bsdf_glossy_toon_eval_transmit(sc, sd->I, omega_in, pdf);
240                                 break;
241                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
242                                 eval = bsdf_westin_backscatter_eval_transmit(sc, sd->I, omega_in, pdf);
243                                 break;
244                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
245                                 eval = bsdf_westin_sheen_eval_transmit(sc, sd->I, omega_in, pdf);
246                                 break;
247 #endif
248                         default:
249                                 eval = make_float3(0.0f, 0.0f, 0.0f);
250                                 break;
251                 }
252         }
253
254         return eval;
255 }
256
257 __device void bsdf_blur(KernelGlobals *kg, ShaderClosure *sc, float roughness)
258 {
259 #ifdef __OSL__
260         if(kg->osl && sc->prim) {
261                 OSLShader::bsdf_blur(sc, roughness);
262                 return;
263         }
264 #endif
265
266         switch(sc->type) {
267                 case CLOSURE_BSDF_DIFFUSE_ID:
268                 case CLOSURE_BSDF_BSSRDF_ID:
269                         bsdf_diffuse_blur(sc, roughness);
270                         break;
271 #ifdef __SVM__
272                 case CLOSURE_BSDF_OREN_NAYAR_ID:
273                         bsdf_oren_nayar_blur(sc, roughness);
274                         break;
275                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
276                         bsdf_phong_ramp_blur(sc, roughness);
277                         break;
278                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
279                         bsdf_diffuse_ramp_blur(sc, roughness);
280                         break;*/
281                 case CLOSURE_BSDF_TRANSLUCENT_ID:
282                         bsdf_translucent_blur(sc, roughness);
283                         break;
284                 case CLOSURE_BSDF_REFLECTION_ID:
285                         bsdf_reflection_blur(sc, roughness);
286                         break;
287                 case CLOSURE_BSDF_REFRACTION_ID:
288                         bsdf_refraction_blur(sc, roughness);
289                         break;
290                 case CLOSURE_BSDF_TRANSPARENT_ID:
291                         bsdf_transparent_blur(sc, roughness);
292                         break;
293                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
294                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
295                         bsdf_microfacet_ggx_blur(sc, roughness);
296                         break;
297                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
298                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
299                         bsdf_microfacet_beckmann_blur(sc, roughness);
300                         break;
301 #ifdef __ANISOTROPIC__
302                 case CLOSURE_BSDF_WARD_ID:
303                         bsdf_ward_blur(sc, roughness);
304                         break;
305 #endif
306                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
307                         bsdf_ashikhmin_velvet_blur(sc, roughness);
308                         break;
309                 case CLOSURE_BSDF_DIFFUSE_TOON_ID:
310                         bsdf_diffuse_toon_blur(sc, roughness);
311                         break;
312                 case CLOSURE_BSDF_GLOSSY_TOON_ID:
313                         bsdf_glossy_toon_blur(sc, roughness);
314                         break;
315                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
316                         bsdf_westin_backscatter_blur(sc, roughness);
317                         break;
318                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
319                         bsdf_westin_sheen_blur(sc, roughness);
320                         break;
321 #endif
322                 default:
323                         break;
324         }
325 }
326
327 CCL_NAMESPACE_END
328