Quick fix for compiler error. Somehow the isfinite symbol got lost for SVM too now...
[blender.git] / intern / cycles / kernel / kernel_accumulate.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 CCL_NAMESPACE_BEGIN
20
21 /* BSDF Eval
22  *
23  * BSDF evaluation result, split per BSDF type. This is used to accumulate
24  * render passes separately. */
25
26 __device_inline void bsdf_eval_init(BsdfEval *eval, ClosureType type, float3 value, int use_light_pass)
27 {
28 #ifdef __PASSES__
29         eval->use_light_pass = use_light_pass;
30
31         if(eval->use_light_pass) {
32                 eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
33                 eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
34                 eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
35                 eval->transparent = make_float3(0.0f, 0.0f, 0.0f);
36
37                 if(type == CLOSURE_BSDF_TRANSPARENT_ID)
38                         eval->transparent = value;
39                 else if(CLOSURE_IS_BSDF_DIFFUSE(type))
40                         eval->diffuse = value;
41                 else if(CLOSURE_IS_BSDF_GLOSSY(type))
42                         eval->glossy = value;
43                 else
44                         eval->transmission = value;
45         }
46         else
47                 eval->diffuse = value;
48 #else
49         *eval = value;
50 #endif
51 }
52
53 __device_inline void bsdf_eval_accum(BsdfEval *eval, ClosureType type, float3 value)
54 {
55 #ifdef __PASSES__
56         if(eval->use_light_pass) {
57                 if(CLOSURE_IS_BSDF_DIFFUSE(type))
58                         eval->diffuse += value;
59                 else if(CLOSURE_IS_BSDF_GLOSSY(type))
60                         eval->glossy += value;
61                 else
62                         eval->transmission += value;
63
64                 /* skipping transparent, this function is used by for eval(), will be zero then */
65         }
66         else
67                 eval->diffuse += value;
68 #else
69         *eval += value;
70 #endif
71 }
72
73 __device_inline bool bsdf_eval_is_zero(BsdfEval *eval)
74 {
75 #ifdef __PASSES__
76         if(eval->use_light_pass) {
77                 return is_zero(eval->diffuse)
78                         && is_zero(eval->glossy)
79                         && is_zero(eval->transmission)
80                         && is_zero(eval->transparent);
81         }
82         else
83                 return is_zero(eval->diffuse);
84 #else
85         return is_zero(*eval);
86 #endif
87 }
88
89 __device_inline void bsdf_eval_mul(BsdfEval *eval, float3 value)
90 {
91 #ifdef __PASSES__
92         if(eval->use_light_pass) {
93                 eval->diffuse *= value;
94                 eval->glossy *= value;
95                 eval->transmission *= value;
96
97                 /* skipping transparent, this function is used by for eval(), will be zero then */
98         }
99         else
100                 eval->diffuse *= value;
101 #else
102         *eval *= value;
103 #endif
104 }
105
106 /* Path Radiance
107  *
108  * We accumulate different render passes separately. After summing at the end
109  * to get the combined result, it should be identical. We definte directly
110  * visible as the first non-transparent hit, while indirectly visible are the
111  * bounces after that. */
112
113 __device_inline void path_radiance_init(PathRadiance *L, int use_light_pass)
114 {
115         /* clear all */
116 #ifdef __PASSES__
117         L->use_light_pass = use_light_pass;
118
119         if(use_light_pass) {
120                 L->indirect = make_float3(0.0f, 0.0f, 0.0f);
121                 L->direct_throughput = make_float3(0.0f, 0.0f, 0.0f);
122                 L->direct_emission = make_float3(0.0f, 0.0f, 0.0f);
123
124                 L->color_diffuse = make_float3(0.0f, 0.0f, 0.0f);
125                 L->color_glossy = make_float3(0.0f, 0.0f, 0.0f);
126                 L->color_transmission = make_float3(0.0f, 0.0f, 0.0f);
127
128                 L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
129                 L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
130                 L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
131
132                 L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
133                 L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
134                 L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
135
136                 L->emission = make_float3(0.0f, 0.0f, 0.0f);
137                 L->background = make_float3(0.0f, 0.0f, 0.0f);
138                 L->ao = make_float3(0.0f, 0.0f, 0.0f);
139                 L->shadow = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
140         }
141         else
142                 L->emission = make_float3(0.0f, 0.0f, 0.0f);
143 #else
144         *L = make_float3(0.0f, 0.0f, 0.0f);
145 #endif
146 }
147
148 __device_inline void path_radiance_bsdf_bounce(PathRadiance *L, float3 *throughput,
149         BsdfEval *bsdf_eval, float bsdf_pdf, int bounce, int bsdf_label)
150 {
151         float inverse_pdf = 1.0f/bsdf_pdf;
152
153 #ifdef __PASSES__
154         if(L->use_light_pass) {
155                 if(bounce == 0 && !(bsdf_label & LABEL_TRANSPARENT)) {
156                         /* first on directly visible surface */
157                         float3 value = *throughput*inverse_pdf;
158
159                         L->indirect_diffuse = bsdf_eval->diffuse*value;
160                         L->indirect_glossy = bsdf_eval->glossy*value;
161                         L->indirect_transmission = bsdf_eval->transmission*value;
162
163                         *throughput = L->indirect_diffuse + L->indirect_glossy + L->indirect_transmission;
164                         
165                         L->direct_throughput = *throughput;
166                 }
167                 else {
168                         /* transparent bounce before first hit, or indirectly visible through BSDF */
169                         float3 sum = (bsdf_eval->diffuse + bsdf_eval->glossy + bsdf_eval->transmission + bsdf_eval->transparent)*inverse_pdf;
170                         *throughput *= sum;
171                 }
172         }
173         else
174                 *throughput *= bsdf_eval->diffuse*inverse_pdf;
175 #else
176         *throughput *= *bsdf_eval*inverse_pdf;
177 #endif
178 }
179
180 __device_inline void path_radiance_accum_emission(PathRadiance *L, float3 throughput, float3 value, int bounce)
181 {
182 #ifdef __PASSES__
183         if(L->use_light_pass) {
184                 if(bounce == 0)
185                         L->emission += throughput*value;
186                 else if(bounce == 1)
187                         L->direct_emission += throughput*value;
188                 else
189                         L->indirect += throughput*value;
190         }
191         else
192                 L->emission += throughput*value;
193 #else
194         *L += throughput*value;
195 #endif
196 }
197
198 __device_inline void path_radiance_accum_ao(PathRadiance *L, float3 throughput, float3 bsdf, float3 ao, int bounce)
199 {
200 #ifdef __PASSES__
201         if(L->use_light_pass) {
202                 if(bounce == 0) {
203                         /* directly visible lighting */
204                         L->direct_diffuse += throughput*bsdf*ao;
205                         L->ao += throughput*ao;
206                 }
207                 else {
208                         /* indirectly visible lighting after BSDF bounce */
209                         L->indirect += throughput*bsdf*ao;
210                 }
211         }
212         else
213                 L->emission += throughput*bsdf*ao;
214 #else
215         *L += throughput*bsdf*ao;
216 #endif
217 }
218
219 __device_inline void path_radiance_accum_light(PathRadiance *L, float3 throughput, BsdfEval *bsdf_eval, float3 shadow, int bounce, bool is_lamp)
220 {
221 #ifdef __PASSES__
222         if(L->use_light_pass) {
223                 if(bounce == 0) {
224                         /* directly visible lighting */
225                         L->direct_diffuse += throughput*bsdf_eval->diffuse*shadow;
226                         L->direct_glossy += throughput*bsdf_eval->glossy*shadow;
227                         L->direct_transmission += throughput*bsdf_eval->transmission*shadow;
228
229                         if(is_lamp) {
230                                 float3 sum = throughput*(bsdf_eval->diffuse + bsdf_eval->glossy + bsdf_eval->transmission);
231
232                                 L->shadow.x += shadow.x;
233                                 L->shadow.y += shadow.y;
234                                 L->shadow.z += shadow.z;
235                                 L->shadow.w += average(sum);
236                         }
237                 }
238                 else {
239                         /* indirectly visible lighting after BSDF bounce */
240                         float3 sum = bsdf_eval->diffuse + bsdf_eval->glossy + bsdf_eval->transmission;
241                         L->indirect += throughput*sum*shadow;
242                 }
243         }
244         else
245                 L->emission += throughput*bsdf_eval->diffuse*shadow;
246 #else
247         *L += throughput*(*bsdf_eval)*shadow;
248 #endif
249 }
250
251 __device_inline void path_radiance_accum_background(PathRadiance *L, float3 throughput, float3 value, int bounce)
252 {
253 #ifdef __PASSES__
254         if(L->use_light_pass) {
255                 if(bounce == 0)
256                         L->background += throughput*value;
257                 else if(bounce == 1)
258                         L->direct_emission += throughput*value;
259                 else
260                         L->indirect += throughput*value;
261         }
262         else
263                 L->emission += throughput*value;
264 #else
265         *L += throughput*value;
266 #endif
267 }
268
269 __device_inline float3 path_radiance_sum(KernelGlobals *kg, PathRadiance *L)
270 {
271 #ifdef __PASSES__
272         if(L->use_light_pass) {
273                 /* this division is a bit ugly, but means we only have to keep track of
274                  * only a single throughput further along the path, here we recover just
275                  * the indirect parth that is not influenced by any particular BSDF type */
276                 L->direct_emission = safe_divide_color(L->direct_emission, L->direct_throughput);
277                 L->direct_diffuse += L->indirect_diffuse*L->direct_emission;
278                 L->direct_glossy += L->indirect_glossy*L->direct_emission;
279                 L->direct_transmission += L->indirect_transmission*L->direct_emission;
280
281                 L->indirect = safe_divide_color(L->indirect, L->direct_throughput);
282                 L->indirect_diffuse *= L->indirect;
283                 L->indirect_glossy *= L->indirect;
284                 L->indirect_transmission *= L->indirect;
285
286                 float3 L_sum = L->emission
287                         + L->direct_diffuse + L->direct_glossy + L->direct_transmission
288                         + L->indirect_diffuse + L->indirect_glossy + L->indirect_transmission;
289
290                 if(!kernel_data.background.transparent)
291                         L_sum += L->background;
292
293                 return L_sum;
294         }
295         else
296                 return L->emission;
297 #else
298         return *L;
299 #endif
300 }
301
302 __device_inline void path_radiance_clamp(PathRadiance *L, float3 *L_sum, float clamp)
303 {
304         using std::isfinite;
305
306         float sum = fabsf((*L_sum).x) + fabsf((*L_sum).y) + fabsf((*L_sum).z);
307
308         if(!isfinite(sum)) {
309                 /* invalid value, reject */
310                 *L_sum = make_float3(0.0f, 0.0f, 0.0f);
311
312 #ifdef __PASSES__
313                 if(L->use_light_pass) {
314                         L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
315                         L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
316                         L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
317
318                         L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
319                         L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
320                         L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
321
322                         L->emission = make_float3(0.0f, 0.0f, 0.0f);
323                 }
324 #endif
325         }
326         else if(sum > clamp) {
327                 /* value to high, scale down */
328                 float scale = clamp/sum;
329
330                 *L_sum *= scale;
331
332 #ifdef __PASSES__
333                 if(L->use_light_pass) {
334                         L->direct_diffuse *= scale;
335                         L->direct_glossy *= scale;
336                         L->direct_transmission *= scale;
337
338                         L->indirect_diffuse *= scale;
339                         L->indirect_glossy *= scale;
340                         L->indirect_transmission *= scale;
341
342                         L->emission *= scale;
343                 }
344 #endif
345         }
346 }
347
348 CCL_NAMESPACE_END
349