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