Code cleanup: make L_transparent part of PathRadiance.
[blender-staging.git] / intern / cycles / kernel / kernel_path.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 #ifdef __OSL__
18 #  include "kernel/osl/osl_shader.h"
19 #endif
20
21 #include "kernel/kernel_random.h"
22 #include "kernel/kernel_projection.h"
23 #include "kernel/kernel_montecarlo.h"
24 #include "kernel/kernel_differential.h"
25 #include "kernel/kernel_camera.h"
26
27 #include "kernel/geom/geom.h"
28 #include "kernel/bvh/bvh.h"
29
30 #include "kernel/kernel_accumulate.h"
31 #include "kernel/kernel_shader.h"
32 #include "kernel/kernel_light.h"
33 #include "kernel/kernel_passes.h"
34
35 #ifdef __SUBSURFACE__
36 #  include "kernel/kernel_subsurface.h"
37 #endif
38
39 #ifdef __VOLUME__
40 #  include "kernel/kernel_volume.h"
41 #endif
42
43 #include "kernel/kernel_path_state.h"
44 #include "kernel/kernel_shadow.h"
45 #include "kernel/kernel_emission.h"
46 #include "kernel/kernel_path_common.h"
47 #include "kernel/kernel_path_surface.h"
48 #include "kernel/kernel_path_volume.h"
49 #include "kernel/kernel_path_subsurface.h"
50
51 CCL_NAMESPACE_BEGIN
52
53 ccl_device_noinline void kernel_path_ao(KernelGlobals *kg,
54                                         ShaderData *sd,
55                                         ShaderData *emission_sd,
56                                         PathRadiance *L,
57                                         ccl_addr_space PathState *state,
58                                         RNG *rng,
59                                         float3 throughput,
60                                         float3 ao_alpha)
61 {
62         /* todo: solve correlation */
63         float bsdf_u, bsdf_v;
64
65         path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
66
67         float ao_factor = kernel_data.background.ao_factor;
68         float3 ao_N;
69         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
70         float3 ao_D;
71         float ao_pdf;
72
73         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
74
75         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
76                 Ray light_ray;
77                 float3 ao_shadow;
78
79                 light_ray.P = ray_offset(sd->P, sd->Ng);
80                 light_ray.D = ao_D;
81                 light_ray.t = kernel_data.background.ao_distance;
82 #ifdef __OBJECT_MOTION__
83                 light_ray.time = sd->time;
84 #endif  /* __OBJECT_MOTION__ */
85                 light_ray.dP = sd->dP;
86                 light_ray.dD = differential3_zero();
87
88                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &ao_shadow)) {
89                         path_radiance_accum_ao(L, state, throughput, ao_alpha, ao_bsdf, ao_shadow);
90                 }
91                 else {
92                         path_radiance_accum_total_ao(L, state, throughput, ao_bsdf);
93                 }
94         }
95 }
96
97 #ifndef __SPLIT_KERNEL__
98
99 #if defined(__BRANCHED_PATH__) || defined(__BAKING__)
100
101 ccl_device void kernel_path_indirect(KernelGlobals *kg,
102                                      ShaderData *sd,
103                                      ShaderData *emission_sd,
104                                      RNG *rng,
105                                      Ray *ray,
106                                      float3 throughput,
107                                      int num_samples,
108                                      PathState *state,
109                                      PathRadiance *L)
110 {
111         /* path iteration */
112         for(;;) {
113                 /* intersect scene */
114                 Intersection isect;
115                 uint visibility = path_state_ray_visibility(kg, state);
116                 if(state->bounce > kernel_data.integrator.ao_bounces) {
117                         visibility = PATH_RAY_SHADOW;
118                         ray->t = kernel_data.background.ao_distance;
119                 }
120                 bool hit = scene_intersect(kg,
121                                            *ray,
122                                            visibility,
123                                            &isect,
124                                            NULL,
125                                            0.0f, 0.0f);
126
127 #ifdef __LAMP_MIS__
128                 if(kernel_data.integrator.use_lamp_mis && !(state->flag & PATH_RAY_CAMERA)) {
129                         /* ray starting from previous non-transparent bounce */
130                         Ray light_ray;
131
132                         light_ray.P = ray->P - state->ray_t*ray->D;
133                         state->ray_t += isect.t;
134                         light_ray.D = ray->D;
135                         light_ray.t = state->ray_t;
136                         light_ray.time = ray->time;
137                         light_ray.dD = ray->dD;
138                         light_ray.dP = ray->dP;
139
140                         /* intersect with lamp */
141                         float3 emission;
142                         if(indirect_lamp_emission(kg, emission_sd, state, &light_ray, &emission)) {
143                                 path_radiance_accum_emission(L,
144                                                              throughput,
145                                                              emission,
146                                                              state->bounce);
147                         }
148                 }
149 #endif  /* __LAMP_MIS__ */
150
151 #ifdef __VOLUME__
152                 /* Sanitize volume stack. */
153                 if(!hit) {
154                         kernel_volume_clean_stack(kg, state->volume_stack);
155                 }
156                 /* volume attenuation, emission, scatter */
157                 if(state->volume_stack[0].shader != SHADER_NONE) {
158                         Ray volume_ray = *ray;
159                         volume_ray.t = (hit)? isect.t: FLT_MAX;
160
161                         bool heterogeneous =
162                                 volume_stack_is_heterogeneous(kg,
163                                                               state->volume_stack);
164
165 #  ifdef __VOLUME_DECOUPLED__
166                         int sampling_method =
167                                 volume_stack_sampling_method(kg,
168                                                              state->volume_stack);
169                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, false, sampling_method);
170
171                         if(decoupled) {
172                                 /* cache steps along volume for repeated sampling */
173                                 VolumeSegment volume_segment;
174
175                                 shader_setup_from_volume(kg,
176                                                          sd,
177                                                          &volume_ray);
178                                 kernel_volume_decoupled_record(kg,
179                                                                state,
180                                                                &volume_ray,
181                                                                sd,
182                                                                &volume_segment,
183                                                                heterogeneous);
184
185                                 volume_segment.sampling_method = sampling_method;
186
187                                 /* emission */
188                                 if(volume_segment.closure_flag & SD_EMISSION) {
189                                         path_radiance_accum_emission(L,
190                                                                      throughput,
191                                                                      volume_segment.accum_emission,
192                                                                      state->bounce);
193                                 }
194
195                                 /* scattering */
196                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
197
198                                 if(volume_segment.closure_flag & SD_SCATTER) {
199                                         int all = kernel_data.integrator.sample_all_lights_indirect;
200
201                                         /* direct light sampling */
202                                         kernel_branched_path_volume_connect_light(kg,
203                                                                                   rng,
204                                                                                   sd,
205                                                                                   emission_sd,
206                                                                                   throughput,
207                                                                                   state,
208                                                                                   L,
209                                                                                   all,
210                                                                                   &volume_ray,
211                                                                                   &volume_segment);
212
213                                         /* indirect sample. if we use distance sampling and take just
214                                          * one sample for direct and indirect light, we could share
215                                          * this computation, but makes code a bit complex */
216                                         float rphase = path_state_rng_1D_for_decision(kg, rng, state, PRNG_PHASE);
217                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, state, PRNG_SCATTER_DISTANCE);
218
219                                         result = kernel_volume_decoupled_scatter(kg,
220                                                                                  state,
221                                                                                  &volume_ray,
222                                                                                  sd,
223                                                                                  &throughput,
224                                                                                  rphase,
225                                                                                  rscatter,
226                                                                                  &volume_segment,
227                                                                                  NULL,
228                                                                                  true);
229                                 }
230
231                                 /* free cached steps */
232                                 kernel_volume_decoupled_free(kg, &volume_segment);
233
234                                 if(result == VOLUME_PATH_SCATTERED) {
235                                         if(kernel_path_volume_bounce(kg,
236                                                                      rng,
237                                                                      sd,
238                                                                      &throughput,
239                                                                      state,
240                                                                      L,
241                                                                      ray))
242                                         {
243                                                 continue;
244                                         }
245                                         else {
246                                                 break;
247                                         }
248                                 }
249                                 else {
250                                         throughput *= volume_segment.accum_transmittance;
251                                 }
252                         }
253                         else
254 #  endif  /* __VOLUME_DECOUPLED__ */
255                         {
256                                 /* integrate along volume segment with distance sampling */
257                                 VolumeIntegrateResult result = kernel_volume_integrate(
258                                         kg, state, sd, &volume_ray, L, &throughput, rng, heterogeneous);
259
260 #  ifdef __VOLUME_SCATTER__
261                                 if(result == VOLUME_PATH_SCATTERED) {
262                                         /* direct lighting */
263                                         kernel_path_volume_connect_light(kg,
264                                                                          rng,
265                                                                          sd,
266                                                                          emission_sd,
267                                                                          throughput,
268                                                                          state,
269                                                                          L);
270
271                                         /* indirect light bounce */
272                                         if(kernel_path_volume_bounce(kg,
273                                                                      rng,
274                                                                      sd,
275                                                                      &throughput,
276                                                                      state,
277                                                                      L,
278                                                                      ray))
279                                         {
280                                                 continue;
281                                         }
282                                         else {
283                                                 break;
284                                         }
285                                 }
286 #  endif  /* __VOLUME_SCATTER__ */
287                         }
288                 }
289 #endif  /* __VOLUME__ */
290
291                 if(!hit) {
292 #ifdef __BACKGROUND__
293                         /* sample background shader */
294                         float3 L_background = indirect_background(kg, emission_sd, state, ray);
295                         path_radiance_accum_background(L,
296                                                        state,
297                                                        throughput,
298                                                        L_background);
299 #endif  /* __BACKGROUND__ */
300
301                         break;
302                 }
303                 else if(state->bounce > kernel_data.integrator.ao_bounces) {
304                         break;
305                 }
306
307                 /* setup shading */
308                 shader_setup_from_ray(kg,
309                                       sd,
310                                       &isect,
311                                       ray);
312                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, state, PRNG_BSDF);
313                 shader_eval_surface(kg, sd, rng, state, rbsdf, state->flag, SHADER_CONTEXT_INDIRECT);
314 #ifdef __BRANCHED_PATH__
315                 shader_merge_closures(sd);
316 #endif  /* __BRANCHED_PATH__ */
317
318 #ifdef __SHADOW_TRICKS__
319                 if(!(sd->object_flag & SD_OBJECT_SHADOW_CATCHER) &&
320                    (state->flag & PATH_RAY_SHADOW_CATCHER))
321                 {
322                         /* Only update transparency after shadow catcher bounce. */
323                         L->shadow_transparency *=
324                                 average(shader_bsdf_transparency(kg, sd));
325                 }
326 #endif  /* __SHADOW_TRICKS__ */
327
328                 /* blurring of bsdf after bounces, for rays that have a small likelihood
329                  * of following this particular path (diffuse, rough glossy) */
330                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
331                         float blur_pdf = kernel_data.integrator.filter_glossy*state->min_ray_pdf;
332
333                         if(blur_pdf < 1.0f) {
334                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
335                                 shader_bsdf_blur(kg, sd, blur_roughness);
336                         }
337                 }
338
339 #ifdef __EMISSION__
340                 /* emission */
341                 if(sd->flag & SD_EMISSION) {
342                         float3 emission = indirect_primitive_emission(kg,
343                                                                       sd,
344                                                                       isect.t,
345                                                                       state->flag,
346                                                                       state->ray_pdf);
347                         path_radiance_accum_emission(L, throughput, emission, state->bounce);
348                 }
349 #endif  /* __EMISSION__ */
350
351                 /* path termination. this is a strange place to put the termination, it's
352                  * mainly due to the mixed in MIS that we use. gives too many unneeded
353                  * shader evaluations, only need emission if we are going to terminate */
354                 float probability =
355                         path_state_continuation_probability(kg,
356                                                          state,
357                                                          throughput*num_samples);
358
359                 if(probability == 0.0f) {
360                         break;
361                 }
362                 else if(probability != 1.0f) {
363                         float terminate = path_state_rng_1D_for_decision(kg, rng, state, PRNG_TERMINATE);
364
365                         if(terminate >= probability)
366                                 break;
367
368                         throughput /= probability;
369                 }
370
371                 kernel_update_denoising_features(kg, sd, state, L);
372
373 #ifdef __AO__
374                 /* ambient occlusion */
375                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
376                         kernel_path_ao(kg, sd, emission_sd, L, state, rng, throughput, make_float3(0.0f, 0.0f, 0.0f));
377                 }
378 #endif  /* __AO__ */
379
380 #ifdef __SUBSURFACE__
381                 /* bssrdf scatter to a different location on the same object, replacing
382                  * the closures with a diffuse BSDF */
383                 if(sd->flag & SD_BSSRDF) {
384                         float bssrdf_probability;
385                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
386
387                         /* modify throughput for picking bssrdf or bsdf */
388                         throughput *= bssrdf_probability;
389
390                         /* do bssrdf scatter step if we picked a bssrdf closure */
391                         if(sc) {
392                                 uint lcg_state = lcg_state_init(rng, state->rng_offset, state->sample, 0x68bc21eb);
393
394                                 float bssrdf_u, bssrdf_v;
395                                 path_state_rng_2D(kg,
396                                                   rng,
397                                                   state,
398                                                   PRNG_BSDF_U,
399                                                   &bssrdf_u, &bssrdf_v);
400                                 subsurface_scatter_step(kg,
401                                                         sd,
402                                                         state,
403                                                         state->flag,
404                                                         sc,
405                                                         &lcg_state,
406                                                         bssrdf_u, bssrdf_v,
407                                                         false);
408                         }
409                 }
410 #endif  /* __SUBSURFACE__ */
411
412 #if defined(__EMISSION__)
413                 if(kernel_data.integrator.use_direct_light) {
414                         int all = (kernel_data.integrator.sample_all_lights_indirect) ||
415                                   (state->flag & PATH_RAY_SHADOW_CATCHER);
416                         kernel_branched_path_surface_connect_light(kg,
417                                                                    rng,
418                                                                    sd,
419                                                                    emission_sd,
420                                                                    state,
421                                                                    throughput,
422                                                                    1.0f,
423                                                                    L,
424                                                                    all);
425                 }
426 #endif  /* defined(__EMISSION__) */
427
428                 if(!kernel_path_surface_bounce(kg, rng, sd, &throughput, state, L, ray))
429                         break;
430         }
431 }
432
433 #endif /* defined(__BRANCHED_PATH__) || defined(__BAKING__) */
434
435 ccl_device_inline void kernel_path_integrate(KernelGlobals *kg,
436                                              RNG *rng,
437                                              int sample,
438                                              Ray ray,
439                                              ccl_global float *buffer,
440                                              PathRadiance *L,
441                                              bool *is_shadow_catcher)
442 {
443         /* initialize */
444         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
445
446         path_radiance_init(L, kernel_data.film.use_light_pass);
447
448         /* shader data memory used for both volumes and surfaces, saves stack space */
449         ShaderData sd;
450         /* shader data used by emission, shadows, volume stacks */
451         ShaderData emission_sd;
452
453         PathState state;
454         path_state_init(kg, &emission_sd, &state, rng, sample, &ray);
455
456 #ifdef __SUBSURFACE__
457         SubsurfaceIndirectRays ss_indirect;
458         kernel_path_subsurface_init_indirect(&ss_indirect);
459
460         for(;;) {
461 #endif  /* __SUBSURFACE__ */
462
463         /* path iteration */
464         for(;;) {
465                 /* intersect scene */
466                 Intersection isect;
467                 uint visibility = path_state_ray_visibility(kg, &state);
468
469 #ifdef __HAIR__
470                 float difl = 0.0f, extmax = 0.0f;
471                 uint lcg_state = 0;
472
473                 if(kernel_data.bvh.have_curves) {
474                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
475                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
476                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
477                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
478                         }
479
480                         extmax = kernel_data.curve.maximum_width;
481                         lcg_state = lcg_state_init(rng, state.rng_offset, state.sample, 0x51633e2d);
482                 }
483
484                 if(state.bounce > kernel_data.integrator.ao_bounces) {
485                         visibility = PATH_RAY_SHADOW;
486                         ray.t = kernel_data.background.ao_distance;
487                 }
488
489                 bool hit = scene_intersect(kg, ray, visibility, &isect, &lcg_state, difl, extmax);
490 #else
491                 bool hit = scene_intersect(kg, ray, visibility, &isect, NULL, 0.0f, 0.0f);
492 #endif  /* __HAIR__ */
493
494 #ifdef __KERNEL_DEBUG__
495                 if(state.flag & PATH_RAY_CAMERA) {
496                         L->debug_data.num_bvh_traversed_nodes += isect.num_traversed_nodes;
497                         L->debug_data.num_bvh_traversed_instances += isect.num_traversed_instances;
498                         L->debug_data.num_bvh_intersections += isect.num_intersections;
499                 }
500                 L->debug_data.num_ray_bounces++;
501 #endif  /* __KERNEL_DEBUG__ */
502
503 #ifdef __LAMP_MIS__
504                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
505                         /* ray starting from previous non-transparent bounce */
506                         Ray light_ray;
507
508                         light_ray.P = ray.P - state.ray_t*ray.D;
509                         state.ray_t += isect.t;
510                         light_ray.D = ray.D;
511                         light_ray.t = state.ray_t;
512                         light_ray.time = ray.time;
513                         light_ray.dD = ray.dD;
514                         light_ray.dP = ray.dP;
515
516                         /* intersect with lamp */
517                         float3 emission;
518
519                         if(indirect_lamp_emission(kg, &emission_sd, &state, &light_ray, &emission))
520                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
521                 }
522 #endif  /* __LAMP_MIS__ */
523
524 #ifdef __VOLUME__
525                 /* Sanitize volume stack. */
526                 if(!hit) {
527                         kernel_volume_clean_stack(kg, state.volume_stack);
528                 }
529                 /* volume attenuation, emission, scatter */
530                 if(state.volume_stack[0].shader != SHADER_NONE) {
531                         Ray volume_ray = ray;
532                         volume_ray.t = (hit)? isect.t: FLT_MAX;
533
534                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
535
536 #  ifdef __VOLUME_DECOUPLED__
537                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
538                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
539
540                         if(decoupled) {
541                                 /* cache steps along volume for repeated sampling */
542                                 VolumeSegment volume_segment;
543
544                                 shader_setup_from_volume(kg, &sd, &volume_ray);
545                                 kernel_volume_decoupled_record(kg, &state,
546                                         &volume_ray, &sd, &volume_segment, heterogeneous);
547
548                                 volume_segment.sampling_method = sampling_method;
549
550                                 /* emission */
551                                 if(volume_segment.closure_flag & SD_EMISSION)
552                                         path_radiance_accum_emission(L, throughput, volume_segment.accum_emission, state.bounce);
553
554                                 /* scattering */
555                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
556
557                                 if(volume_segment.closure_flag & SD_SCATTER) {
558                                         int all = false;
559
560                                         /* direct light sampling */
561                                         kernel_branched_path_volume_connect_light(kg, rng, &sd,
562                                                 &emission_sd, throughput, &state, L, all,
563                                                 &volume_ray, &volume_segment);
564
565                                         /* indirect sample. if we use distance sampling and take just
566                                          * one sample for direct and indirect light, we could share
567                                          * this computation, but makes code a bit complex */
568                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
569                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
570
571                                         result = kernel_volume_decoupled_scatter(kg,
572                                                 &state, &volume_ray, &sd, &throughput,
573                                                 rphase, rscatter, &volume_segment, NULL, true);
574                                 }
575
576                                 /* free cached steps */
577                                 kernel_volume_decoupled_free(kg, &volume_segment);
578
579                                 if(result == VOLUME_PATH_SCATTERED) {
580                                         if(kernel_path_volume_bounce(kg, rng, &sd, &throughput, &state, L, &ray))
581                                                 continue;
582                                         else
583                                                 break;
584                                 }
585                                 else {
586                                         throughput *= volume_segment.accum_transmittance;
587                                 }
588                         }
589                         else
590 #  endif  /* __VOLUME_DECOUPLED__ */
591                         {
592                                 /* integrate along volume segment with distance sampling */
593                                 VolumeIntegrateResult result = kernel_volume_integrate(
594                                         kg, &state, &sd, &volume_ray, L, &throughput, rng, heterogeneous);
595
596 #  ifdef __VOLUME_SCATTER__
597                                 if(result == VOLUME_PATH_SCATTERED) {
598                                         /* direct lighting */
599                                         kernel_path_volume_connect_light(kg, rng, &sd, &emission_sd, throughput, &state, L);
600
601                                         /* indirect light bounce */
602                                         if(kernel_path_volume_bounce(kg, rng, &sd, &throughput, &state, L, &ray))
603                                                 continue;
604                                         else
605                                                 break;
606                                 }
607 #  endif  /* __VOLUME_SCATTER__ */
608                         }
609                 }
610 #endif  /* __VOLUME__ */
611
612                 if(!hit) {
613                         /* eval background shader if nothing hit */
614                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
615                                 L->transparent += average(throughput);
616
617 #ifdef __PASSES__
618                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
619 #endif  /* __PASSES__ */
620                                         break;
621                         }
622
623 #ifdef __BACKGROUND__
624                         /* sample background shader */
625                         float3 L_background = indirect_background(kg, &emission_sd, &state, &ray);
626                         path_radiance_accum_background(L, &state, throughput, L_background);
627 #endif  /* __BACKGROUND__ */
628
629                         break;
630                 }
631                 else if(state.bounce > kernel_data.integrator.ao_bounces) {
632                         break;
633                 }
634
635                 /* setup shading */
636                 shader_setup_from_ray(kg, &sd, &isect, &ray);
637                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
638                 shader_eval_surface(kg, &sd, rng, &state, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
639
640 #ifdef __SHADOW_TRICKS__
641                 if((sd.object_flag & SD_OBJECT_SHADOW_CATCHER)) {
642                         if(state.flag & PATH_RAY_CAMERA) {
643                                 state.flag |= (PATH_RAY_SHADOW_CATCHER |
644                                                PATH_RAY_STORE_SHADOW_INFO);
645                                 if(!kernel_data.background.transparent) {
646                                         L->shadow_background_color =
647                                                 indirect_background(kg, &emission_sd, &state, &ray);
648                                 }
649                                 L->shadow_radiance_sum = path_radiance_clamp_and_sum(kg, L);
650                                 L->shadow_throughput = average(throughput);
651                         }
652                 }
653                 else if(state.flag & PATH_RAY_SHADOW_CATCHER) {
654                         /* Only update transparency after shadow catcher bounce. */
655                         L->shadow_transparency *=
656                                 average(shader_bsdf_transparency(kg, &sd));
657                 }
658 #endif  /* __SHADOW_TRICKS__ */
659
660                 /* holdout */
661 #ifdef __HOLDOUT__
662                 if(((sd.flag & SD_HOLDOUT) ||
663                     (sd.object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
664                    (state.flag & PATH_RAY_CAMERA))
665                 {
666                         if(kernel_data.background.transparent) {
667                                 float3 holdout_weight;
668                                 if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
669                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
670                                 }
671                                 else {
672                                         holdout_weight = shader_holdout_eval(kg, &sd);
673                                 }
674                                 /* any throughput is ok, should all be identical here */
675                                 L->transparent += average(holdout_weight*throughput);
676                         }
677
678                         if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
679                                 break;
680                         }
681                 }
682 #endif  /* __HOLDOUT__ */
683
684                 /* holdout mask objects do not write data passes */
685                 kernel_write_data_passes(kg, buffer, L, &sd, sample, &state, throughput);
686
687                 /* blurring of bsdf after bounces, for rays that have a small likelihood
688                  * of following this particular path (diffuse, rough glossy) */
689                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
690                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
691
692                         if(blur_pdf < 1.0f) {
693                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
694                                 shader_bsdf_blur(kg, &sd, blur_roughness);
695                         }
696                 }
697
698 #ifdef __EMISSION__
699                 /* emission */
700                 if(sd.flag & SD_EMISSION) {
701                         /* todo: is isect.t wrong here for transparent surfaces? */
702                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
703                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
704                 }
705 #endif  /* __EMISSION__ */
706
707                 /* path termination. this is a strange place to put the termination, it's
708                  * mainly due to the mixed in MIS that we use. gives too many unneeded
709                  * shader evaluations, only need emission if we are going to terminate */
710                 float probability = path_state_continuation_probability(kg, &state, throughput);
711
712                 if(probability == 0.0f) {
713                         break;
714                 }
715                 else if(probability != 1.0f) {
716                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
717                         if(terminate >= probability)
718                                 break;
719
720                         throughput /= probability;
721                 }
722
723                 kernel_update_denoising_features(kg, &sd, &state, L);
724
725 #ifdef __AO__
726                 /* ambient occlusion */
727                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
728                         kernel_path_ao(kg, &sd, &emission_sd, L, &state, rng, throughput, shader_bsdf_alpha(kg, &sd));
729                 }
730 #endif  /* __AO__ */
731
732 #ifdef __SUBSURFACE__
733                 /* bssrdf scatter to a different location on the same object, replacing
734                  * the closures with a diffuse BSDF */
735                 if(sd.flag & SD_BSSRDF) {
736                         if(kernel_path_subsurface_scatter(kg,
737                                                           &sd,
738                                                           &emission_sd,
739                                                           L,
740                                                           &state,
741                                                           rng,
742                                                           &ray,
743                                                           &throughput,
744                                                           &ss_indirect))
745                         {
746                                 break;
747                         }
748                 }
749 #endif  /* __SUBSURFACE__ */
750
751                 /* direct lighting */
752                 kernel_path_surface_connect_light(kg, rng, &sd, &emission_sd, throughput, &state, L);
753
754                 /* compute direct lighting and next bounce */
755                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, L, &ray))
756                         break;
757         }
758
759 #ifdef __SUBSURFACE__
760                 kernel_path_subsurface_accum_indirect(&ss_indirect, L);
761
762                 /* Trace indirect subsurface rays by restarting the loop. this uses less
763                  * stack memory than invoking kernel_path_indirect.
764                  */
765                 if(ss_indirect.num_rays) {
766                         kernel_path_subsurface_setup_indirect(kg,
767                                                               &ss_indirect,
768                                                               &state,
769                                                               &ray,
770                                                               L,
771                                                               &throughput);
772                 }
773                 else {
774                         break;
775                 }
776         }
777 #endif  /* __SUBSURFACE__ */
778
779 #ifdef __SHADOW_TRICKS__
780         *is_shadow_catcher = (state.flag & PATH_RAY_SHADOW_CATCHER) != 0;
781 #endif  /* __SHADOW_TRICKS__ */
782 }
783
784 ccl_device void kernel_path_trace(KernelGlobals *kg,
785         ccl_global float *buffer, ccl_global uint *rng_state,
786         int sample, int x, int y, int offset, int stride)
787 {
788         /* buffer offset */
789         int index = offset + x + y*stride;
790         int pass_stride = kernel_data.film.pass_stride;
791
792         rng_state += index;
793         buffer += index*pass_stride;
794
795         /* initialize random numbers and ray */
796         RNG rng;
797         Ray ray;
798
799         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
800
801         /* integrate */
802         PathRadiance L;
803         bool is_shadow_catcher;
804
805         if(ray.t != 0.0f) {
806                 kernel_path_integrate(kg, &rng, sample, ray, buffer, &L, &is_shadow_catcher);
807                 kernel_write_result(kg, buffer, sample, &L, is_shadow_catcher);
808         }
809         else {
810                 kernel_write_result(kg, buffer, sample, NULL, false);
811         }
812 }
813
814 #endif  /* __SPLIT_KERNEL__ */
815
816 CCL_NAMESPACE_END
817