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