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