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