Bugfix, irc collection:
[blender.git] / intern / cycles / kernel / svm / svm_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 svm_bsdf_sample(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         switch(sc->type) {
40                 case CLOSURE_BSDF_DIFFUSE_ID:
41                         label = bsdf_diffuse_sample(sc, sd->Ng, sd->I, sd->dI.dx, sd->dI.dy, randu, randv,
42                                 eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
43                         break;
44 #ifdef __SVM__
45                 case CLOSURE_BSDF_OREN_NAYAR_ID:
46                         label = bsdf_oren_nayar_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                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
50                         label = bsdf_phong_ramp_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                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
54                         label = bsdf_diffuse_ramp_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_TRANSLUCENT_ID:
58                         label = bsdf_translucent_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_REFLECTION_ID:
62                         label = bsdf_reflection_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_REFRACTION_ID:
66                         label = bsdf_refraction_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_TRANSPARENT_ID:
70                         label = bsdf_transparent_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_MICROFACET_GGX_ID:
74                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
75                         label = bsdf_microfacet_ggx_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_BECKMANN_ID:
79                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
80                         label = bsdf_microfacet_beckmann_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 #ifdef __ANISOTROPIC__
84                 case CLOSURE_BSDF_WARD_ID:
85                         label = bsdf_ward_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 #endif
89                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
90                         label = bsdf_ashikhmin_velvet_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                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
94                         label = bsdf_westin_backscatter_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                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
98                         label = bsdf_westin_sheen_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 #endif
102                 default:
103                         label = LABEL_NONE;
104                         break;
105         }
106
107         return label;
108 }
109
110 __device float3 svm_bsdf_eval(const ShaderData *sd, const ShaderClosure *sc, const float3 omega_in, float *pdf)
111 {
112         float3 eval;
113
114         if(dot(sd->Ng, omega_in) >= 0.0f) {
115                 switch(sc->type) {
116                         case CLOSURE_BSDF_DIFFUSE_ID:
117                                 eval = bsdf_diffuse_eval_reflect(sc, sd->I, omega_in, pdf);
118                                 break;
119 #ifdef __SVM__
120                         case CLOSURE_BSDF_OREN_NAYAR_ID:
121                                 eval = bsdf_oren_nayar_eval_reflect(sc, sd->I, omega_in, pdf);
122                                 break;
123                         /*case CLOSURE_BSDF_PHONG_RAMP_ID:
124                                 eval = bsdf_phong_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
125                                 break;
126                         case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
127                                 eval = bsdf_diffuse_ramp_eval_reflect(sc, sd->I, omega_in, pdf);
128                                 break;*/
129                         case CLOSURE_BSDF_TRANSLUCENT_ID:
130                                 eval = bsdf_translucent_eval_reflect(sc, sd->I, omega_in, pdf);
131                                 break;
132                         case CLOSURE_BSDF_REFLECTION_ID:
133                                 eval = bsdf_reflection_eval_reflect(sc, sd->I, omega_in, pdf);
134                                 break;
135                         case CLOSURE_BSDF_REFRACTION_ID:
136                                 eval = bsdf_refraction_eval_reflect(sc, sd->I, omega_in, pdf);
137                                 break;
138                         case CLOSURE_BSDF_TRANSPARENT_ID:
139                                 eval = bsdf_transparent_eval_reflect(sc, sd->I, omega_in, pdf);
140                                 break;
141                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
142                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
143                                 eval = bsdf_microfacet_ggx_eval_reflect(sc, sd->I, omega_in, pdf);
144                                 break;
145                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
146                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
147                                 eval = bsdf_microfacet_beckmann_eval_reflect(sc, sd->I, omega_in, pdf);
148                                 break;
149 #ifdef __ANISOTROPIC__
150                         case CLOSURE_BSDF_WARD_ID:
151                                 eval = bsdf_ward_eval_reflect(sc, sd->I, omega_in, pdf);
152                                 break;
153 #endif
154                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
155                                 eval = bsdf_ashikhmin_velvet_eval_reflect(sc, sd->I, omega_in, pdf);
156                                 break;
157                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
158                                 eval = bsdf_westin_backscatter_eval_reflect(sc, sd->I, omega_in, pdf);
159                                 break;
160                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
161                                 eval = bsdf_westin_sheen_eval_reflect(sc, sd->I, omega_in, pdf);
162                                 break;
163 #endif
164                         default:
165                                 eval = make_float3(0.0f, 0.0f, 0.0f);
166                                 break;
167                 }
168         }
169         else {
170                 switch(sc->type) {
171                         case CLOSURE_BSDF_DIFFUSE_ID:
172                                 eval = bsdf_diffuse_eval_transmit(sc, sd->I, omega_in, pdf);
173                                 break;
174 #ifdef __SVM__
175                         case CLOSURE_BSDF_OREN_NAYAR_ID:
176                                 eval = bsdf_oren_nayar_eval_transmit(sc, sd->I, omega_in, pdf);
177                                 break;
178                         case CLOSURE_BSDF_TRANSLUCENT_ID:
179                                 eval = bsdf_translucent_eval_transmit(sc, sd->I, omega_in, pdf);
180                                 break;
181                         case CLOSURE_BSDF_REFLECTION_ID:
182                                 eval = bsdf_reflection_eval_transmit(sc, sd->I, omega_in, pdf);
183                                 break;
184                         case CLOSURE_BSDF_REFRACTION_ID:
185                                 eval = bsdf_refraction_eval_transmit(sc, sd->I, omega_in, pdf);
186                                 break;
187                         case CLOSURE_BSDF_TRANSPARENT_ID:
188                                 eval = bsdf_transparent_eval_transmit(sc, sd->I, omega_in, pdf);
189                                 break;
190                         case CLOSURE_BSDF_MICROFACET_GGX_ID:
191                         case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
192                                 eval = bsdf_microfacet_ggx_eval_transmit(sc, sd->I, omega_in, pdf);
193                                 break;
194                         case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
195                         case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
196                                 eval = bsdf_microfacet_beckmann_eval_transmit(sc, sd->I, omega_in, pdf);
197                                 break;
198 #ifdef __ANISOTROPIC__
199                         case CLOSURE_BSDF_WARD_ID:
200                                 eval = bsdf_ward_eval_transmit(sc, sd->I, omega_in, pdf);
201                                 break;
202 #endif
203                         case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
204                                 eval = bsdf_ashikhmin_velvet_eval_transmit(sc, sd->I, omega_in, pdf);
205                                 break;
206                         case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
207                                 eval = bsdf_westin_backscatter_eval_transmit(sc, sd->I, omega_in, pdf);
208                                 break;
209                         case CLOSURE_BSDF_WESTIN_SHEEN_ID:
210                                 eval = bsdf_westin_sheen_eval_transmit(sc, sd->I, omega_in, pdf);
211                                 break;
212 #endif
213                         default:
214                                 eval = make_float3(0.0f, 0.0f, 0.0f);
215                                 break;
216                 }
217         }
218
219         return eval;
220 }
221
222 __device void svm_bsdf_blur(ShaderClosure *sc, float roughness)
223 {
224         switch(sc->type) {
225                 case CLOSURE_BSDF_DIFFUSE_ID:
226                         bsdf_diffuse_blur(sc, roughness);
227                         break;
228 #ifdef __SVM__
229                 case CLOSURE_BSDF_OREN_NAYAR_ID:
230                         bsdf_oren_nayar_blur(sc, roughness);
231                         break;
232                 /*case CLOSURE_BSDF_PHONG_RAMP_ID:
233                         bsdf_phong_ramp_blur(sc, roughness);
234                         break;
235                 case CLOSURE_BSDF_DIFFUSE_RAMP_ID:
236                         bsdf_diffuse_ramp_blur(sc, roughness);
237                         break;*/
238                 case CLOSURE_BSDF_TRANSLUCENT_ID:
239                         bsdf_translucent_blur(sc, roughness);
240                         break;
241                 case CLOSURE_BSDF_REFLECTION_ID:
242                         bsdf_reflection_blur(sc, roughness);
243                         break;
244                 case CLOSURE_BSDF_REFRACTION_ID:
245                         bsdf_refraction_blur(sc, roughness);
246                         break;
247                 case CLOSURE_BSDF_TRANSPARENT_ID:
248                         bsdf_transparent_blur(sc, roughness);
249                         break;
250                 case CLOSURE_BSDF_MICROFACET_GGX_ID:
251                 case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
252                         bsdf_microfacet_ggx_blur(sc, roughness);
253                         break;
254                 case CLOSURE_BSDF_MICROFACET_BECKMANN_ID:
255                 case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID:
256                         bsdf_microfacet_beckmann_blur(sc, roughness);
257                         break;
258 #ifdef __ANISOTROPIC__
259                 case CLOSURE_BSDF_WARD_ID:
260                         bsdf_ward_blur(sc, roughness);
261                         break;
262 #endif
263                 case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID:
264                         bsdf_ashikhmin_velvet_blur(sc, roughness);
265                         break;
266                 case CLOSURE_BSDF_WESTIN_BACKSCATTER_ID:
267                         bsdf_westin_backscatter_blur(sc, roughness);
268                         break;
269                 case CLOSURE_BSDF_WESTIN_SHEEN_ID:
270                         bsdf_westin_sheen_blur(sc, roughness);
271                         break;
272 #endif
273                 default:
274                         break;
275         }
276 }
277
278 CCL_NAMESPACE_END
279