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