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