Cycles: Remove ccl_addr_space from RNG passed to functions
[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 "osl_shader.h"
19 #endif
20
21 #include "kernel_random.h"
22 #include "kernel_projection.h"
23 #include "kernel_montecarlo.h"
24 #include "kernel_differential.h"
25 #include "kernel_camera.h"
26
27 #include "geom/geom.h"
28 #include "bvh/bvh.h"
29
30 #include "kernel_accumulate.h"
31 #include "kernel_shader.h"
32 #include "kernel_light.h"
33 #include "kernel_passes.h"
34
35 #ifdef __SUBSURFACE__
36 #  include "kernel_subsurface.h"
37 #endif
38
39 #ifdef __VOLUME__
40 #  include "kernel_volume.h"
41 #endif
42
43 #include "kernel_path_state.h"
44 #include "kernel_shadow.h"
45 #include "kernel_emission.h"
46 #include "kernel_path_common.h"
47 #include "kernel_path_surface.h"
48 #include "kernel_path_volume.h"
49 #include "kernel_path_subsurface.h"
50
51 #ifdef __KERNEL_DEBUG__
52 #  include "kernel_debug.h"
53 #endif
54
55 CCL_NAMESPACE_BEGIN
56
57 ccl_device_noinline void kernel_path_ao(KernelGlobals *kg,
58                                         ShaderData *sd,
59                                         ShaderData *emission_sd,
60                                         PathRadiance *L,
61                                         PathState *state,
62                                         RNG *rng,
63                                         float3 throughput,
64                                         float3 ao_alpha)
65 {
66         /* todo: solve correlation */
67         float bsdf_u, bsdf_v;
68
69         path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
70
71         float ao_factor = kernel_data.background.ao_factor;
72         float3 ao_N;
73         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
74         float3 ao_D;
75         float ao_pdf;
76
77         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
78
79         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
80                 Ray light_ray;
81                 float3 ao_shadow;
82
83                 light_ray.P = ray_offset(sd->P, sd->Ng);
84                 light_ray.D = ao_D;
85                 light_ray.t = kernel_data.background.ao_distance;
86 #ifdef __OBJECT_MOTION__
87                 light_ray.time = sd->time;
88 #endif  /* __OBJECT_MOTION__ */
89                 light_ray.dP = sd->dP;
90                 light_ray.dD = differential3_zero();
91
92                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &ao_shadow)) {
93                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
94                 }
95                 else {
96                         path_radiance_accum_total_ao(L, throughput, ao_bsdf);
97                 }
98         }
99 }
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_ONLY;
321                 }
322 #endif  /* __SHADOW_TRICKS__ */
323
324                 /* blurring of bsdf after bounces, for rays that have a small likelihood
325                  * of following this particular path (diffuse, rough glossy) */
326                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
327                         float blur_pdf = kernel_data.integrator.filter_glossy*state->min_ray_pdf;
328
329                         if(blur_pdf < 1.0f) {
330                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
331                                 shader_bsdf_blur(kg, sd, blur_roughness);
332                         }
333                 }
334
335 #ifdef __EMISSION__
336                 /* emission */
337                 if(sd->flag & SD_EMISSION) {
338                         float3 emission = indirect_primitive_emission(kg,
339                                                                       sd,
340                                                                       isect.t,
341                                                                       state->flag,
342                                                                       state->ray_pdf);
343                         path_radiance_accum_emission(L, throughput, emission, state->bounce);
344                 }
345 #endif  /* __EMISSION__ */
346
347                 /* path termination. this is a strange place to put the termination, it's
348                  * mainly due to the mixed in MIS that we use. gives too many unneeded
349                  * shader evaluations, only need emission if we are going to terminate */
350                 float probability =
351                         path_state_terminate_probability(kg,
352                                                          state,
353                                                          throughput*num_samples);
354
355                 if(probability == 0.0f) {
356                         break;
357                 }
358                 else if(probability != 1.0f) {
359                         float terminate = path_state_rng_1D_for_decision(kg, rng, state, PRNG_TERMINATE);
360
361                         if(terminate >= probability)
362                                 break;
363
364                         throughput /= probability;
365                 }
366
367 #ifdef __AO__
368                 /* ambient occlusion */
369                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
370                         kernel_path_ao(kg, sd, emission_sd, L, state, rng, throughput, make_float3(0.0f, 0.0f, 0.0f));
371                 }
372 #endif  /* __AO__ */
373
374 #ifdef __SUBSURFACE__
375                 /* bssrdf scatter to a different location on the same object, replacing
376                  * the closures with a diffuse BSDF */
377                 if(sd->flag & SD_BSSRDF) {
378                         float bssrdf_probability;
379                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
380
381                         /* modify throughput for picking bssrdf or bsdf */
382                         throughput *= bssrdf_probability;
383
384                         /* do bssrdf scatter step if we picked a bssrdf closure */
385                         if(sc) {
386                                 uint lcg_state = lcg_state_init(rng, state->rng_offset, state->sample, 0x68bc21eb);
387
388                                 float bssrdf_u, bssrdf_v;
389                                 path_state_rng_2D(kg,
390                                                   rng,
391                                                   state,
392                                                   PRNG_BSDF_U,
393                                                   &bssrdf_u, &bssrdf_v);
394                                 subsurface_scatter_step(kg,
395                                                         sd,
396                                                         state,
397                                                         state->flag,
398                                                         sc,
399                                                         &lcg_state,
400                                                         bssrdf_u, bssrdf_v,
401                                                         false);
402                         }
403                 }
404 #endif  /* __SUBSURFACE__ */
405
406 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
407                 if(kernel_data.integrator.use_direct_light) {
408                         int all = (kernel_data.integrator.sample_all_lights_indirect) ||
409                                   (state->flag & PATH_RAY_SHADOW_CATCHER);
410                         kernel_branched_path_surface_connect_light(kg,
411                                                                    rng,
412                                                                    sd,
413                                                                    emission_sd,
414                                                                    state,
415                                                                    throughput,
416                                                                    1.0f,
417                                                                    L,
418                                                                    all);
419                 }
420 #endif  /* defined(__EMISSION__) && defined(__BRANCHED_PATH__) */
421
422                 if(!kernel_path_surface_bounce(kg, rng, sd, &throughput, state, L, ray))
423                         break;
424         }
425 }
426
427
428 ccl_device_inline float4 kernel_path_integrate(KernelGlobals *kg,
429                                                RNG *rng,
430                                                int sample,
431                                                Ray ray,
432                                                ccl_global float *buffer)
433 {
434         /* initialize */
435         PathRadiance L;
436         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
437         float L_transparent = 0.0f;
438
439         path_radiance_init(&L, kernel_data.film.use_light_pass);
440
441         /* shader data memory used for both volumes and surfaces, saves stack space */
442         ShaderData sd;
443         /* shader data used by emission, shadows, volume stacks */
444         ShaderData emission_sd;
445
446         PathState state;
447         path_state_init(kg, &emission_sd, &state, rng, sample, &ray);
448
449 #ifdef __KERNEL_DEBUG__
450         DebugData debug_data;
451         debug_data_init(&debug_data);
452 #endif  /* __KERNEL_DEBUG__ */
453
454 #ifdef __SUBSURFACE__
455         SubsurfaceIndirectRays ss_indirect;
456         kernel_path_subsurface_init_indirect(&ss_indirect);
457
458         for(;;) {
459 #endif  /* __SUBSURFACE__ */
460
461         /* path iteration */
462         for(;;) {
463                 /* intersect scene */
464                 Intersection isect;
465                 uint visibility = path_state_ray_visibility(kg, &state);
466
467 #ifdef __HAIR__
468                 float difl = 0.0f, extmax = 0.0f;
469                 uint lcg_state = 0;
470
471                 if(kernel_data.bvh.have_curves) {
472                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
473                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
474                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
475                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
476                         }
477
478                         extmax = kernel_data.curve.maximum_width;
479                         lcg_state = lcg_state_init(rng, state.rng_offset, state.sample, 0x51633e2d);
480                 }
481
482                 if(state.bounce > kernel_data.integrator.ao_bounces) {
483                         visibility = PATH_RAY_SHADOW;
484                         ray.t = kernel_data.background.ao_distance;
485                 }
486
487                 bool hit = scene_intersect(kg, ray, visibility, &isect, &lcg_state, difl, extmax);
488 #else
489                 bool hit = scene_intersect(kg, ray, visibility, &isect, NULL, 0.0f, 0.0f);
490 #endif  /* __HAIR__ */
491
492 #ifdef __KERNEL_DEBUG__
493                 if(state.flag & PATH_RAY_CAMERA) {
494                         debug_data.num_bvh_traversed_nodes += isect.num_traversed_nodes;
495                         debug_data.num_bvh_traversed_instances += isect.num_traversed_instances;
496                         debug_data.num_bvh_intersections += isect.num_intersections;
497                 }
498                 debug_data.num_ray_bounces++;
499 #endif  /* __KERNEL_DEBUG__ */
500
501 #ifdef __LAMP_MIS__
502                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
503                         /* ray starting from previous non-transparent bounce */
504                         Ray light_ray;
505
506                         light_ray.P = ray.P - state.ray_t*ray.D;
507                         state.ray_t += isect.t;
508                         light_ray.D = ray.D;
509                         light_ray.t = state.ray_t;
510                         light_ray.time = ray.time;
511                         light_ray.dD = ray.dD;
512                         light_ray.dP = ray.dP;
513
514                         /* intersect with lamp */
515                         float3 emission;
516
517                         if(indirect_lamp_emission(kg, &emission_sd, &state, &light_ray, &emission))
518                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
519                 }
520 #endif  /* __LAMP_MIS__ */
521
522 #ifdef __VOLUME__
523                 /* Sanitize volume stack. */
524                 if(!hit) {
525                         kernel_volume_clean_stack(kg, state.volume_stack);
526                 }
527                 /* volume attenuation, emission, scatter */
528                 if(state.volume_stack[0].shader != SHADER_NONE) {
529                         Ray volume_ray = ray;
530                         volume_ray.t = (hit)? isect.t: FLT_MAX;
531
532                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
533
534 #  ifdef __VOLUME_DECOUPLED__
535                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
536                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
537
538                         if(decoupled) {
539                                 /* cache steps along volume for repeated sampling */
540                                 VolumeSegment volume_segment;
541
542                                 shader_setup_from_volume(kg, &sd, &volume_ray);
543                                 kernel_volume_decoupled_record(kg, &state,
544                                         &volume_ray, &sd, &volume_segment, heterogeneous);
545
546                                 volume_segment.sampling_method = sampling_method;
547
548                                 /* emission */
549                                 if(volume_segment.closure_flag & SD_EMISSION)
550                                         path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
551
552                                 /* scattering */
553                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
554
555                                 if(volume_segment.closure_flag & SD_SCATTER) {
556                                         int all = false;
557
558                                         /* direct light sampling */
559                                         kernel_branched_path_volume_connect_light(kg, rng, &sd,
560                                                 &emission_sd, throughput, &state, &L, all,
561                                                 &volume_ray, &volume_segment);
562
563                                         /* indirect sample. if we use distance sampling and take just
564                                          * one sample for direct and indirect light, we could share
565                                          * this computation, but makes code a bit complex */
566                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
567                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
568
569                                         result = kernel_volume_decoupled_scatter(kg,
570                                                 &state, &volume_ray, &sd, &throughput,
571                                                 rphase, rscatter, &volume_segment, NULL, true);
572                                 }
573
574                                 /* free cached steps */
575                                 kernel_volume_decoupled_free(kg, &volume_segment);
576
577                                 if(result == VOLUME_PATH_SCATTERED) {
578                                         if(kernel_path_volume_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
579                                                 continue;
580                                         else
581                                                 break;
582                                 }
583                                 else {
584                                         throughput *= volume_segment.accum_transmittance;
585                                 }
586                         }
587                         else
588 #  endif  /* __VOLUME_DECOUPLED__ */
589                         {
590                                 /* integrate along volume segment with distance sampling */
591                                 VolumeIntegrateResult result = kernel_volume_integrate(
592                                         kg, &state, &sd, &volume_ray, &L, &throughput, rng, heterogeneous);
593
594 #  ifdef __VOLUME_SCATTER__
595                                 if(result == VOLUME_PATH_SCATTERED) {
596                                         /* direct lighting */
597                                         kernel_path_volume_connect_light(kg, rng, &sd, &emission_sd, throughput, &state, &L);
598
599                                         /* indirect light bounce */
600                                         if(kernel_path_volume_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
601                                                 continue;
602                                         else
603                                                 break;
604                                 }
605 #  endif  /* __VOLUME_SCATTER__ */
606                         }
607                 }
608 #endif  /* __VOLUME__ */
609
610                 if(!hit) {
611                         /* eval background shader if nothing hit */
612                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
613                                 L_transparent += average(throughput);
614
615 #ifdef __PASSES__
616                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
617 #endif  /* __PASSES__ */
618                                         break;
619                         }
620
621 #ifdef __BACKGROUND__
622                         /* sample background shader */
623                         float3 L_background = indirect_background(kg, &emission_sd, &state, &ray);
624                         path_radiance_accum_background(&L, &state, throughput, L_background);
625 #endif  /* __BACKGROUND__ */
626
627                         break;
628                 }
629                 else if(state.bounce > kernel_data.integrator.ao_bounces) {
630                         break;
631                 }
632
633                 /* setup shading */
634                 shader_setup_from_ray(kg, &sd, &isect, &ray);
635                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
636                 shader_eval_surface(kg, &sd, rng, &state, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
637
638 #ifdef __SHADOW_TRICKS__
639                 if((sd.object_flag & SD_OBJECT_SHADOW_CATCHER)) {
640                         if(state.flag & PATH_RAY_CAMERA) {
641                                 state.flag |= (PATH_RAY_SHADOW_CATCHER | PATH_RAY_SHADOW_CATCHER_ONLY);
642                                 state.catcher_object = sd.object;
643                                 if(!kernel_data.background.transparent) {
644                                         L.shadow_color = indirect_background(kg, &emission_sd, &state, &ray);
645                                 }
646                         }
647                 }
648                 else {
649                         state.flag &= ~PATH_RAY_SHADOW_CATCHER_ONLY;
650                 }
651 #endif  /* __SHADOW_TRICKS__ */
652
653                 /* holdout */
654 #ifdef __HOLDOUT__
655                 if(((sd.flag & SD_HOLDOUT) ||
656                     (sd.object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
657                    (state.flag & PATH_RAY_CAMERA))
658                 {
659                         if(kernel_data.background.transparent) {
660                                 float3 holdout_weight;
661                                 if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
662                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
663                                 }
664                                 else {
665                                         holdout_weight = shader_holdout_eval(kg, &sd);
666                                 }
667                                 /* any throughput is ok, should all be identical here */
668                                 L_transparent += average(holdout_weight*throughput);
669                         }
670
671                         if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
672                                 break;
673                         }
674                 }
675 #endif  /* __HOLDOUT__ */
676
677                 /* holdout mask objects do not write data passes */
678                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
679
680                 /* blurring of bsdf after bounces, for rays that have a small likelihood
681                  * of following this particular path (diffuse, rough glossy) */
682                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
683                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
684
685                         if(blur_pdf < 1.0f) {
686                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
687                                 shader_bsdf_blur(kg, &sd, blur_roughness);
688                         }
689                 }
690
691 #ifdef __EMISSION__
692                 /* emission */
693                 if(sd.flag & SD_EMISSION) {
694                         /* todo: is isect.t wrong here for transparent surfaces? */
695                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
696                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
697                 }
698 #endif  /* __EMISSION__ */
699
700                 /* path termination. this is a strange place to put the termination, it's
701                  * mainly due to the mixed in MIS that we use. gives too many unneeded
702                  * shader evaluations, only need emission if we are going to terminate */
703                 float probability = path_state_terminate_probability(kg, &state, throughput);
704
705                 if(probability == 0.0f) {
706                         break;
707                 }
708                 else if(probability != 1.0f) {
709                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
710                         if(terminate >= probability)
711                                 break;
712
713                         throughput /= probability;
714                 }
715
716 #ifdef __AO__
717                 /* ambient occlusion */
718                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
719                         kernel_path_ao(kg, &sd, &emission_sd, &L, &state, rng, throughput, shader_bsdf_alpha(kg, &sd));
720                 }
721 #endif  /* __AO__ */
722
723 #ifdef __SUBSURFACE__
724                 /* bssrdf scatter to a different location on the same object, replacing
725                  * the closures with a diffuse BSDF */
726                 if(sd.flag & SD_BSSRDF) {
727                         if(kernel_path_subsurface_scatter(kg,
728                                                           &sd,
729                                                           &emission_sd,
730                                                           &L,
731                                                           &state,
732                                                           rng,
733                                                           &ray,
734                                                           &throughput,
735                                                           &ss_indirect))
736                         {
737                                 break;
738                         }
739                 }
740 #endif  /* __SUBSURFACE__ */
741
742                 /* direct lighting */
743                 kernel_path_surface_connect_light(kg, rng, &sd, &emission_sd, throughput, &state, &L);
744
745                 /* compute direct lighting and next bounce */
746                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
747                         break;
748         }
749
750 #ifdef __SUBSURFACE__
751                 kernel_path_subsurface_accum_indirect(&ss_indirect, &L);
752
753                 /* Trace indirect subsurface rays by restarting the loop. this uses less
754                  * stack memory than invoking kernel_path_indirect.
755                  */
756                 if(ss_indirect.num_rays) {
757                         kernel_path_subsurface_setup_indirect(kg,
758                                                               &ss_indirect,
759                                                               &state,
760                                                               &ray,
761                                                               &L,
762                                                               &throughput);
763                 }
764                 else {
765                         break;
766                 }
767         }
768 #endif  /* __SUBSURFACE__ */
769
770         float3 L_sum;
771 #ifdef __SHADOW_TRICKS__
772         if(state.flag & PATH_RAY_SHADOW_CATCHER) {
773                 L_sum = path_radiance_sum_shadowcatcher(kg, &L, &L_transparent);
774         }
775         else
776 #endif  /* __SHADOW_TRICKS__ */
777         {
778                 L_sum = path_radiance_clamp_and_sum(kg, &L);
779         }
780
781         kernel_write_light_passes(kg, buffer, &L, sample);
782
783 #ifdef __KERNEL_DEBUG__
784         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
785 #endif  /* __KERNEL_DEBUG__ */
786
787         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
788 }
789
790 ccl_device void kernel_path_trace(KernelGlobals *kg,
791         ccl_global float *buffer, ccl_global uint *rng_state,
792         int sample, int x, int y, int offset, int stride)
793 {
794         /* buffer offset */
795         int index = offset + x + y*stride;
796         int pass_stride = kernel_data.film.pass_stride;
797
798         rng_state += index;
799         buffer += index*pass_stride;
800
801         /* initialize random numbers and ray */
802         RNG rng;
803         Ray ray;
804
805         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
806
807         /* integrate */
808         float4 L;
809
810         if(ray.t != 0.0f)
811                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
812         else
813                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
814
815         /* accumulate result in output buffer */
816         kernel_write_pass_float4(buffer, sample, L);
817
818         path_rng_end(kg, rng_state, rng);
819 }
820
821 CCL_NAMESPACE_END
822