Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / kernel / kernel_accumulate.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 CCL_NAMESPACE_BEGIN
18
19 /* BSDF Eval
20  *
21  * BSDF evaluation result, split per BSDF type. This is used to accumulate
22  * render passes separately. */
23
24 ccl_device float3 shader_bsdf_transparency(KernelGlobals *kg,
25                                            const ShaderData *sd);
26
27 ccl_device_inline void bsdf_eval_init(BsdfEval *eval, ClosureType type, float3 value, int use_light_pass)
28 {
29 #ifdef __PASSES__
30         eval->use_light_pass = use_light_pass;
31
32         if(eval->use_light_pass) {
33                 eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
34                 eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
35                 eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
36                 eval->transparent = make_float3(0.0f, 0.0f, 0.0f);
37                 eval->subsurface = make_float3(0.0f, 0.0f, 0.0f);
38                 eval->scatter = make_float3(0.0f, 0.0f, 0.0f);
39
40                 if(type == CLOSURE_BSDF_TRANSPARENT_ID)
41                         eval->transparent = value;
42                 else if(CLOSURE_IS_BSDF_DIFFUSE(type))
43                         eval->diffuse = value;
44                 else if(CLOSURE_IS_BSDF_GLOSSY(type))
45                         eval->glossy = value;
46                 else if(CLOSURE_IS_BSDF_TRANSMISSION(type))
47                         eval->transmission = value;
48                 else if(CLOSURE_IS_BSDF_BSSRDF(type))
49                         eval->subsurface = value;
50                 else if(CLOSURE_IS_PHASE(type))
51                         eval->scatter = value;
52         }
53         else
54 #endif
55         {
56                 eval->diffuse = value;
57         }
58 #ifdef __SHADOW_TRICKS__
59         eval->sum_no_mis = make_float3(0.0f, 0.0f, 0.0f);
60 #endif
61 }
62
63 ccl_device_inline void bsdf_eval_accum(BsdfEval *eval, ClosureType type, float3 value, float mis_weight)
64 {
65 #ifdef __SHADOW_TRICKS__
66         eval->sum_no_mis += value;
67 #endif
68         value *= mis_weight;
69 #ifdef __PASSES__
70         if(eval->use_light_pass) {
71                 if(CLOSURE_IS_BSDF_DIFFUSE(type))
72                         eval->diffuse += value;
73                 else if(CLOSURE_IS_BSDF_GLOSSY(type))
74                         eval->glossy += value;
75                 else if(CLOSURE_IS_BSDF_TRANSMISSION(type))
76                         eval->transmission += value;
77                 else if(CLOSURE_IS_BSDF_BSSRDF(type))
78                         eval->subsurface += value;
79                 else if(CLOSURE_IS_PHASE(type))
80                         eval->scatter += value;
81
82                 /* skipping transparent, this function is used by for eval(), will be zero then */
83         }
84         else
85 #endif
86         {
87                 eval->diffuse += value;
88         }
89 }
90
91 ccl_device_inline bool bsdf_eval_is_zero(BsdfEval *eval)
92 {
93 #ifdef __PASSES__
94         if(eval->use_light_pass) {
95                 return is_zero(eval->diffuse)
96                         && is_zero(eval->glossy)
97                         && is_zero(eval->transmission)
98                         && is_zero(eval->transparent)
99                         && is_zero(eval->subsurface)
100                         && is_zero(eval->scatter);
101         }
102         else
103 #endif
104         {
105                 return is_zero(eval->diffuse);
106         }
107 }
108
109 ccl_device_inline void bsdf_eval_mis(BsdfEval *eval, float value)
110 {
111 #ifdef __PASSES__
112         if(eval->use_light_pass) {
113                 eval->diffuse *= value;
114                 eval->glossy *= value;
115                 eval->transmission *= value;
116                 eval->subsurface *= value;
117                 eval->scatter *= value;
118
119                 /* skipping transparent, this function is used by for eval(), will be zero then */
120         }
121         else
122 #endif
123         {
124                 eval->diffuse *= value;
125         }
126 }
127
128 ccl_device_inline void bsdf_eval_mul(BsdfEval *eval, float value)
129 {
130 #ifdef __SHADOW_TRICKS__
131         eval->sum_no_mis *= value;
132 #endif
133         bsdf_eval_mis(eval, value);
134 }
135
136 ccl_device_inline void bsdf_eval_mul3(BsdfEval *eval, float3 value)
137 {
138 #ifdef __SHADOW_TRICKS__
139         eval->sum_no_mis *= value;
140 #endif
141 #ifdef __PASSES__
142         if(eval->use_light_pass) {
143                 eval->diffuse *= value;
144                 eval->glossy *= value;
145                 eval->transmission *= value;
146                 eval->subsurface *= value;
147                 eval->scatter *= value;
148
149                 /* skipping transparent, this function is used by for eval(), will be zero then */
150         }
151         else
152                 eval->diffuse *= value;
153 #else
154         eval->diffuse *= value;
155 #endif
156 }
157
158 ccl_device_inline float3 bsdf_eval_sum(const BsdfEval *eval)
159 {
160 #ifdef __PASSES__
161         if(eval->use_light_pass) {
162                 return eval->diffuse + eval->glossy + eval->transmission + eval->subsurface + eval->scatter;
163         }
164         else
165 #endif
166         return eval->diffuse;
167 }
168
169 /* Path Radiance
170  *
171  * We accumulate different render passes separately. After summing at the end
172  * to get the combined result, it should be identical. We definite directly
173  * visible as the first non-transparent hit, while indirectly visible are the
174  * bounces after that. */
175
176 ccl_device_inline void path_radiance_init(PathRadiance *L, int use_light_pass)
177 {
178         /* clear all */
179 #ifdef __PASSES__
180         L->use_light_pass = use_light_pass;
181
182         if(use_light_pass) {
183                 L->indirect = make_float3(0.0f, 0.0f, 0.0f);
184                 L->direct_throughput = make_float3(0.0f, 0.0f, 0.0f);
185                 L->direct_emission = make_float3(0.0f, 0.0f, 0.0f);
186
187                 L->color_diffuse = make_float3(0.0f, 0.0f, 0.0f);
188                 L->color_glossy = make_float3(0.0f, 0.0f, 0.0f);
189                 L->color_transmission = make_float3(0.0f, 0.0f, 0.0f);
190                 L->color_subsurface = make_float3(0.0f, 0.0f, 0.0f);
191                 L->color_scatter = make_float3(0.0f, 0.0f, 0.0f);
192
193                 L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
194                 L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
195                 L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
196                 L->direct_subsurface = make_float3(0.0f, 0.0f, 0.0f);
197                 L->direct_scatter = make_float3(0.0f, 0.0f, 0.0f);
198
199                 L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
200                 L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
201                 L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
202                 L->indirect_subsurface = make_float3(0.0f, 0.0f, 0.0f);
203                 L->indirect_scatter = make_float3(0.0f, 0.0f, 0.0f);
204
205                 L->path_diffuse = make_float3(0.0f, 0.0f, 0.0f);
206                 L->path_glossy = make_float3(0.0f, 0.0f, 0.0f);
207                 L->path_transmission = make_float3(0.0f, 0.0f, 0.0f);
208                 L->path_subsurface = make_float3(0.0f, 0.0f, 0.0f);
209                 L->path_scatter = make_float3(0.0f, 0.0f, 0.0f);
210
211                 L->transparent = 0.0f;
212                 L->emission = make_float3(0.0f, 0.0f, 0.0f);
213                 L->background = make_float3(0.0f, 0.0f, 0.0f);
214                 L->ao = make_float3(0.0f, 0.0f, 0.0f);
215                 L->shadow = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
216                 L->mist = 0.0f;
217         }
218         else
219 #endif
220         {
221                 L->transparent = 0.0f;
222                 L->emission = make_float3(0.0f, 0.0f, 0.0f);
223         }
224
225 #ifdef __SHADOW_TRICKS__
226         L->path_total = make_float3(0.0f, 0.0f, 0.0f);
227         L->path_total_shaded = make_float3(0.0f, 0.0f, 0.0f);
228         L->shadow_background_color = make_float3(0.0f, 0.0f, 0.0f);
229         L->shadow_throughput = 0.0f;
230         L->shadow_transparency = 1.0f;
231         L->has_shadow_catcher = 0;
232 #endif
233
234 #ifdef __DENOISING_FEATURES__
235         L->denoising_normal = make_float3(0.0f, 0.0f, 0.0f);
236         L->denoising_albedo = make_float3(0.0f, 0.0f, 0.0f);
237         L->denoising_depth = 0.0f;
238 #endif
239
240 #ifdef __KERNEL_DEBUG__
241         L->debug_data.num_bvh_traversed_nodes = 0;
242         L->debug_data.num_bvh_traversed_instances = 0;
243         L->debug_data.num_bvh_intersections = 0;
244         L->debug_data.num_ray_bounces = 0;
245 #endif
246 }
247
248 ccl_device_inline void path_radiance_bsdf_bounce(PathRadiance *L, ccl_addr_space float3 *throughput,
249         BsdfEval *bsdf_eval, float bsdf_pdf, int bounce, int bsdf_label)
250 {
251         float inverse_pdf = 1.0f/bsdf_pdf;
252
253 #ifdef __PASSES__
254         if(L->use_light_pass) {
255                 if(bounce == 0 && !(bsdf_label & LABEL_TRANSPARENT)) {
256                         /* first on directly visible surface */
257                         float3 value = *throughput*inverse_pdf;
258
259                         L->path_diffuse = bsdf_eval->diffuse*value;
260                         L->path_glossy = bsdf_eval->glossy*value;
261                         L->path_transmission = bsdf_eval->transmission*value;
262                         L->path_subsurface = bsdf_eval->subsurface*value;
263                         L->path_scatter = bsdf_eval->scatter*value;
264
265                         *throughput = L->path_diffuse + L->path_glossy + L->path_transmission + L->path_subsurface + L->path_scatter;
266                         
267                         L->direct_throughput = *throughput;
268                 }
269                 else {
270                         /* transparent bounce before first hit, or indirectly visible through BSDF */
271                         float3 sum = (bsdf_eval_sum(bsdf_eval) + bsdf_eval->transparent) * inverse_pdf;
272                         *throughput *= sum;
273                 }
274         }
275         else
276 #endif
277         {
278                 *throughput *= bsdf_eval->diffuse*inverse_pdf;
279         }
280 }
281
282 ccl_device_inline void path_radiance_accum_emission(PathRadiance *L,
283                                                     ccl_addr_space PathState *state,
284                                                     float3 throughput,
285                                                     float3 value)
286 {
287 #ifdef __SHADOW_TRICKS__
288         if(state->flag & PATH_RAY_SHADOW_CATCHER) {
289                 return;
290         }
291 #endif
292
293 #ifdef __PASSES__
294         if(L->use_light_pass) {
295                 if(state->bounce == 0)
296                         L->emission += throughput*value;
297                 else if(state->bounce == 1)
298                         L->direct_emission += throughput*value;
299                 else
300                         L->indirect += throughput*value;
301         }
302         else
303 #endif
304         {
305                 L->emission += throughput*value;
306         }
307 }
308
309 ccl_device_inline void path_radiance_accum_ao(PathRadiance *L,
310                                               ccl_addr_space PathState *state,
311                                               float3 throughput,
312                                               float3 alpha,
313                                               float3 bsdf,
314                                               float3 ao)
315 {
316 #ifdef __SHADOW_TRICKS__
317         if(state->flag & PATH_RAY_STORE_SHADOW_INFO) {
318                 float3 light = throughput * bsdf;
319                 L->path_total += light;
320                 L->path_total_shaded += ao * light;
321
322                 if(state->flag & PATH_RAY_SHADOW_CATCHER) {
323                         return;
324                 }
325         }
326 #endif
327
328 #ifdef __PASSES__
329         if(L->use_light_pass) {
330                 if(state->bounce == 0) {
331                         /* directly visible lighting */
332                         L->direct_diffuse += throughput*bsdf*ao;
333                         L->ao += alpha*throughput*ao;
334                 }
335                 else {
336                         /* indirectly visible lighting after BSDF bounce */
337                         L->indirect += throughput*bsdf*ao;
338                 }
339         }
340         else
341 #endif
342         {
343                 L->emission += throughput*bsdf*ao;
344         }
345 }
346
347 ccl_device_inline void path_radiance_accum_total_ao(
348         PathRadiance *L,
349         ccl_addr_space PathState *state,
350         float3 throughput,
351         float3 bsdf)
352 {
353 #ifdef __SHADOW_TRICKS__
354         if(state->flag & PATH_RAY_STORE_SHADOW_INFO) {
355                 L->path_total += throughput * bsdf;
356         }
357 #else
358         (void) L;
359         (void) state;
360         (void) throughput;
361         (void) bsdf;
362 #endif
363 }
364
365 ccl_device_inline void path_radiance_accum_light(PathRadiance *L,
366                                                  ccl_addr_space PathState *state,
367                                                  float3 throughput,
368                                                  BsdfEval *bsdf_eval,
369                                                  float3 shadow,
370                                                  float shadow_fac,
371                                                  bool is_lamp)
372 {
373 #ifdef __SHADOW_TRICKS__
374         if(state->flag & PATH_RAY_STORE_SHADOW_INFO) {
375                 float3 light = throughput * bsdf_eval->sum_no_mis;
376                 L->path_total += light;
377                 L->path_total_shaded += shadow * light;
378
379                 if(state->flag & PATH_RAY_SHADOW_CATCHER) {
380                         return;
381                 }
382         }
383 #endif
384
385 #ifdef __PASSES__
386         if(L->use_light_pass) {
387                 if(state->bounce == 0) {
388                         /* directly visible lighting */
389                         L->direct_diffuse += throughput*bsdf_eval->diffuse*shadow;
390                         L->direct_glossy += throughput*bsdf_eval->glossy*shadow;
391                         L->direct_transmission += throughput*bsdf_eval->transmission*shadow;
392                         L->direct_subsurface += throughput*bsdf_eval->subsurface*shadow;
393                         L->direct_scatter += throughput*bsdf_eval->scatter*shadow;
394
395                         if(is_lamp) {
396                                 L->shadow.x += shadow.x*shadow_fac;
397                                 L->shadow.y += shadow.y*shadow_fac;
398                                 L->shadow.z += shadow.z*shadow_fac;
399                         }
400                 }
401                 else {
402                         /* indirectly visible lighting after BSDF bounce */
403                         L->indirect += throughput*bsdf_eval_sum(bsdf_eval)*shadow;
404                 }
405         }
406         else
407 #endif
408         {
409                 L->emission += throughput*bsdf_eval->diffuse*shadow;
410         }
411 }
412
413 ccl_device_inline void path_radiance_accum_total_light(
414         PathRadiance *L,
415         ccl_addr_space PathState *state,
416         float3 throughput,
417         const BsdfEval *bsdf_eval)
418 {
419 #ifdef __SHADOW_TRICKS__
420         if(state->flag & PATH_RAY_STORE_SHADOW_INFO) {
421                 L->path_total += throughput * bsdf_eval->sum_no_mis;
422         }
423 #else
424         (void) L;
425         (void) state;
426         (void) throughput;
427         (void) bsdf_eval;
428 #endif
429 }
430
431 ccl_device_inline void path_radiance_accum_background(
432         PathRadiance *L,
433         ccl_addr_space PathState *state,
434         float3 throughput,
435         float3 value)
436 {
437
438 #ifdef __SHADOW_TRICKS__
439         if(state->flag & PATH_RAY_STORE_SHADOW_INFO) {
440                 L->path_total += throughput * value;
441                 L->path_total_shaded += throughput * value * L->shadow_transparency;
442
443                 if(state->flag & PATH_RAY_SHADOW_CATCHER) {
444                         return;
445                 }
446         }
447 #endif
448
449 #ifdef __PASSES__
450         if(L->use_light_pass) {
451                 if(state->bounce == 0)
452                         L->background += throughput*value;
453                 else if(state->bounce == 1)
454                         L->direct_emission += throughput*value;
455                 else
456                         L->indirect += throughput*value;
457         }
458         else
459 #endif
460         {
461                 L->emission += throughput*value;
462         }
463
464 #ifdef __DENOISING_FEATURES__
465         L->denoising_albedo += state->denoising_feature_weight * value;
466 #endif  /* __DENOISING_FEATURES__ */
467 }
468
469 ccl_device_inline void path_radiance_accum_transparent(
470         PathRadiance *L,
471         ccl_addr_space PathState *state,
472         float3 throughput)
473 {
474         L->transparent += average(throughput);
475 }
476
477 #ifdef __SHADOW_TRICKS__
478 ccl_device_inline void path_radiance_accum_shadowcatcher(
479         PathRadiance *L,
480         float3 throughput,
481         float3 background)
482 {
483         L->shadow_throughput += average(throughput);
484         L->shadow_background_color += throughput * background;
485         L->has_shadow_catcher = 1;
486 }
487 #endif
488
489 ccl_device_inline void path_radiance_sum_indirect(PathRadiance *L)
490 {
491 #ifdef __PASSES__
492         /* this division is a bit ugly, but means we only have to keep track of
493          * only a single throughput further along the path, here we recover just
494          * the indirect path that is not influenced by any particular BSDF type */
495         if(L->use_light_pass) {
496                 L->direct_emission = safe_divide_color(L->direct_emission, L->direct_throughput);
497                 L->direct_diffuse += L->path_diffuse*L->direct_emission;
498                 L->direct_glossy += L->path_glossy*L->direct_emission;
499                 L->direct_transmission += L->path_transmission*L->direct_emission;
500                 L->direct_subsurface += L->path_subsurface*L->direct_emission;
501                 L->direct_scatter += L->path_scatter*L->direct_emission;
502
503                 L->indirect = safe_divide_color(L->indirect, L->direct_throughput);
504                 L->indirect_diffuse += L->path_diffuse*L->indirect;
505                 L->indirect_glossy += L->path_glossy*L->indirect;
506                 L->indirect_transmission += L->path_transmission*L->indirect;
507                 L->indirect_subsurface += L->path_subsurface*L->indirect;
508                 L->indirect_scatter += L->path_scatter*L->indirect;
509         }
510 #endif
511 }
512
513 ccl_device_inline void path_radiance_reset_indirect(PathRadiance *L)
514 {
515 #ifdef __PASSES__
516         if(L->use_light_pass) {
517                 L->path_diffuse = make_float3(0.0f, 0.0f, 0.0f);
518                 L->path_glossy = make_float3(0.0f, 0.0f, 0.0f);
519                 L->path_transmission = make_float3(0.0f, 0.0f, 0.0f);
520                 L->path_subsurface = make_float3(0.0f, 0.0f, 0.0f);
521                 L->path_scatter = make_float3(0.0f, 0.0f, 0.0f);
522
523                 L->direct_emission = make_float3(0.0f, 0.0f, 0.0f);
524                 L->indirect = make_float3(0.0f, 0.0f, 0.0f);
525         }
526 #endif
527 }
528
529 ccl_device_inline void path_radiance_copy_indirect(PathRadiance *L,
530                                                    const PathRadiance *L_src)
531 {
532 #ifdef __PASSES__
533         if(L->use_light_pass) {
534                 L->path_diffuse = L_src->path_diffuse;
535                 L->path_glossy = L_src->path_glossy;
536                 L->path_transmission = L_src->path_transmission;
537                 L->path_subsurface = L_src->path_subsurface;
538                 L->path_scatter = L_src->path_scatter;
539
540                 L->direct_emission = L_src->direct_emission;
541                 L->indirect = L_src->indirect;
542         }
543 #endif
544 }
545
546 #ifdef __SHADOW_TRICKS__
547 ccl_device_inline void path_radiance_sum_shadowcatcher(KernelGlobals *kg,
548                                                        PathRadiance *L,
549                                                        float3 *L_sum,
550                                                        float *alpha)
551 {
552         /* Calculate current shadow of the path. */
553         float path_total = average(L->path_total);
554         float shadow;
555
556         if(path_total == 0.0f) {
557                 shadow = L->shadow_transparency;
558         }
559         else {
560                 float path_total_shaded = average(L->path_total_shaded);
561                 shadow = path_total_shaded / path_total;
562         }
563
564         /* Calculate final light sum and transparency for shadow catcher object. */
565         if(kernel_data.background.transparent) {
566                 *alpha -= L->shadow_throughput * shadow;
567         }
568         else {
569                 L->shadow_background_color *= shadow;
570                 *L_sum += L->shadow_background_color;
571         }
572 }
573 #endif
574
575 ccl_device_inline float3 path_radiance_clamp_and_sum(KernelGlobals *kg, PathRadiance *L, float *alpha)
576 {
577         float3 L_sum;
578         /* Light Passes are used */
579 #ifdef __PASSES__
580         float3 L_direct, L_indirect;
581         float clamp_direct = kernel_data.integrator.sample_clamp_direct;
582         float clamp_indirect = kernel_data.integrator.sample_clamp_indirect;
583         if(L->use_light_pass) {
584                 path_radiance_sum_indirect(L);
585
586                 L_direct = L->direct_diffuse + L->direct_glossy + L->direct_transmission + L->direct_subsurface + L->direct_scatter + L->emission;
587                 L_indirect = L->indirect_diffuse + L->indirect_glossy + L->indirect_transmission + L->indirect_subsurface + L->indirect_scatter;
588
589                 if(!kernel_data.background.transparent)
590                         L_direct += L->background;
591
592                 L_sum = L_direct + L_indirect;
593                 float sum = fabsf((L_sum).x) + fabsf((L_sum).y) + fabsf((L_sum).z);
594
595                 /* Reject invalid value */
596                 if(!isfinite_safe(sum)) {
597                         kernel_assert(!"Non-finite sum in path_radiance_clamp_and_sum!");
598                         L_sum = make_float3(0.0f, 0.0f, 0.0f);
599
600                         L->direct_diffuse = make_float3(0.0f, 0.0f, 0.0f);
601                         L->direct_glossy = make_float3(0.0f, 0.0f, 0.0f);
602                         L->direct_transmission = make_float3(0.0f, 0.0f, 0.0f);
603                         L->direct_subsurface = make_float3(0.0f, 0.0f, 0.0f);
604                         L->direct_scatter = make_float3(0.0f, 0.0f, 0.0f);
605
606                         L->indirect_diffuse = make_float3(0.0f, 0.0f, 0.0f);
607                         L->indirect_glossy = make_float3(0.0f, 0.0f, 0.0f);
608                         L->indirect_transmission = make_float3(0.0f, 0.0f, 0.0f);
609                         L->indirect_subsurface = make_float3(0.0f, 0.0f, 0.0f);
610                         L->indirect_scatter = make_float3(0.0f, 0.0f, 0.0f);
611
612                         L->emission = make_float3(0.0f, 0.0f, 0.0f);
613                 }
614
615                 /* Clamp direct and indirect samples */
616 #ifdef __CLAMP_SAMPLE__
617                 else if(sum > clamp_direct || sum > clamp_indirect) {
618                         float scale;
619
620                         /* Direct */
621                         float sum_direct = fabsf(L_direct.x) + fabsf(L_direct.y) + fabsf(L_direct.z);
622                         if(sum_direct > clamp_direct) {
623                                 scale = clamp_direct/sum_direct;
624                                 L_direct *= scale;
625
626                                 L->direct_diffuse *= scale;
627                                 L->direct_glossy *= scale;
628                                 L->direct_transmission *= scale;
629                                 L->direct_subsurface *= scale;
630                                 L->direct_scatter *= scale;
631                                 L->emission *= scale;
632                                 L->background *= scale;
633                         }
634
635                         /* Indirect */
636                         float sum_indirect = fabsf(L_indirect.x) + fabsf(L_indirect.y) + fabsf(L_indirect.z);
637                         if(sum_indirect > clamp_indirect) {
638                                 scale = clamp_indirect/sum_indirect;
639                                 L_indirect *= scale;
640
641                                 L->indirect_diffuse *= scale;
642                                 L->indirect_glossy *= scale;
643                                 L->indirect_transmission *= scale;
644                                 L->indirect_subsurface *= scale;
645                                 L->indirect_scatter *= scale;
646                         }
647
648                         /* Sum again, after clamping */
649                         L_sum = L_direct + L_indirect;
650                 }
651 #endif
652         }
653
654         /* No Light Passes */
655         else
656 #endif
657         {
658                 L_sum = L->emission;
659
660                 /* Reject invalid value */
661                 float sum = fabsf((L_sum).x) + fabsf((L_sum).y) + fabsf((L_sum).z);
662                 if(!isfinite_safe(sum)) {
663                         kernel_assert(!"Non-finite final sum in path_radiance_clamp_and_sum!");
664                         L_sum = make_float3(0.0f, 0.0f, 0.0f);
665                 }
666         }
667
668         /* Compute alpha. */
669         *alpha = 1.0f - L->transparent;
670
671         /* Add shadow catcher contributions. */
672 #ifdef __SHADOW_TRICKS__
673         if(L->has_shadow_catcher) {
674                 path_radiance_sum_shadowcatcher(kg, L, &L_sum, alpha);
675         }
676 #endif  /* __SHADOW_TRICKS__ */
677
678         return L_sum;
679 }
680
681 ccl_device_inline void path_radiance_split_denoising(KernelGlobals *kg, PathRadiance *L, float3 *noisy, float3 *clean)
682 {
683 #ifdef __PASSES__
684         kernel_assert(L->use_light_pass);
685
686         *clean = L->emission + L->background;
687         *noisy = L->direct_scatter + L->indirect_scatter;
688
689 #  define ADD_COMPONENT(flag, component)     \
690         if(kernel_data.film.denoising_flags & flag) \
691                 *clean += component;                 \
692         else                                     \
693                 *noisy += component;
694
695         ADD_COMPONENT(DENOISING_CLEAN_DIFFUSE_DIR,      L->direct_diffuse);
696         ADD_COMPONENT(DENOISING_CLEAN_DIFFUSE_IND,      L->indirect_diffuse);
697         ADD_COMPONENT(DENOISING_CLEAN_GLOSSY_DIR,       L->direct_glossy);
698         ADD_COMPONENT(DENOISING_CLEAN_GLOSSY_IND,       L->indirect_glossy);
699         ADD_COMPONENT(DENOISING_CLEAN_TRANSMISSION_DIR, L->direct_transmission);
700         ADD_COMPONENT(DENOISING_CLEAN_TRANSMISSION_IND, L->indirect_transmission);
701         ADD_COMPONENT(DENOISING_CLEAN_SUBSURFACE_DIR,   L->direct_subsurface);
702         ADD_COMPONENT(DENOISING_CLEAN_SUBSURFACE_IND,   L->indirect_subsurface);
703 #  undef ADD_COMPONENT
704 #else
705         *noisy = L->emission;
706         *clean = make_float3(0.0f, 0.0f, 0.0f);
707 #endif
708
709 #ifdef __SHADOW_TRICKS__
710         if(L->has_shadow_catcher) {
711                 *noisy += L->shadow_background_color;
712         }
713 #endif
714
715         *noisy = ensure_finite3(*noisy);
716         *clean = ensure_finite3(*clean);
717 }
718
719 ccl_device_inline void path_radiance_accum_sample(PathRadiance *L, PathRadiance *L_sample)
720 {
721 #ifdef __SPLIT_KERNEL__
722 #  define safe_float3_add(f, v) \
723         do { \
724                 ccl_global float *p = (ccl_global float*)(&(f)); \
725                 atomic_add_and_fetch_float(p+0, (v).x); \
726                 atomic_add_and_fetch_float(p+1, (v).y); \
727                 atomic_add_and_fetch_float(p+2, (v).z); \
728         } while(0)
729 #  define safe_float_add(f, v) \
730                 atomic_add_and_fetch_float(&(f), (v))
731 #else
732 #  define safe_float3_add(f, v) (f) += (v)
733 #  define safe_float_add(f, v) (f) += (v)
734 #endif  /* __SPLIT_KERNEL__ */
735
736 #ifdef __PASSES__
737         safe_float3_add(L->direct_diffuse, L_sample->direct_diffuse);
738         safe_float3_add(L->direct_glossy, L_sample->direct_glossy);
739         safe_float3_add(L->direct_transmission, L_sample->direct_transmission);
740         safe_float3_add(L->direct_subsurface, L_sample->direct_subsurface);
741         safe_float3_add(L->direct_scatter, L_sample->direct_scatter);
742
743         safe_float3_add(L->indirect_diffuse, L_sample->indirect_diffuse);
744         safe_float3_add(L->indirect_glossy, L_sample->indirect_glossy);
745         safe_float3_add(L->indirect_transmission, L_sample->indirect_transmission);
746         safe_float3_add(L->indirect_subsurface, L_sample->indirect_subsurface);
747         safe_float3_add(L->indirect_scatter, L_sample->indirect_scatter);
748
749         safe_float3_add(L->background, L_sample->background);
750         safe_float3_add(L->ao, L_sample->ao);
751         safe_float3_add(L->shadow, L_sample->shadow);
752         safe_float_add(L->mist, L_sample->mist);
753 #endif  /* __PASSES__ */
754         safe_float3_add(L->emission, L_sample->emission);
755
756 #undef safe_float_add
757 #undef safe_float3_add
758 }
759
760 CCL_NAMESPACE_END