Cycles: better path termination for transparency.
[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 "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, sd, &isect, ray);
450                 shader_eval_surface(kg, sd, state, state->flag);
451                 shader_prepare_closures(sd, state);
452
453                 /* Apply shadow catcher, holdout, emission. */
454                 if(!kernel_path_shader_apply(kg,
455                                              sd,
456                                              state,
457                                              ray,
458                                              throughput,
459                                              emission_sd,
460                                              L,
461                                              NULL))
462                 {
463                         break;
464                 }
465
466                 /* path termination. this is a strange place to put the termination, it's
467                  * mainly due to the mixed in MIS that we use. gives too many unneeded
468                  * shader evaluations, only need emission if we are going to terminate */
469                 float probability = path_state_continuation_probability(kg, state, throughput);
470
471                 if(probability == 0.0f) {
472                         break;
473                 }
474                 else if(probability != 1.0f) {
475                         float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
476
477                         if(terminate >= probability)
478                                 break;
479
480                         throughput /= probability;
481                 }
482
483                 kernel_update_denoising_features(kg, sd, state, L);
484
485 #ifdef __AO__
486                 /* ambient occlusion */
487                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
488                         kernel_path_ao(kg, sd, emission_sd, L, state, throughput, make_float3(0.0f, 0.0f, 0.0f));
489                 }
490 #endif  /* __AO__ */
491
492
493 #ifdef __SUBSURFACE__
494                 /* bssrdf scatter to a different location on the same object, replacing
495                  * the closures with a diffuse BSDF */
496                 if(sd->flag & SD_BSSRDF) {
497                         if(kernel_path_subsurface_scatter(kg,
498                                                           sd,
499                                                           emission_sd,
500                                                           L,
501                                                           state,
502                                                           ray,
503                                                           &throughput,
504                                                           &ss_indirect))
505                         {
506                                 break;
507                         }
508                 }
509 #endif  /* __SUBSURFACE__ */
510
511 #if defined(__EMISSION__)
512                 if(kernel_data.integrator.use_direct_light) {
513                         int all = (kernel_data.integrator.sample_all_lights_indirect) ||
514                                   (state->flag & PATH_RAY_SHADOW_CATCHER);
515                         kernel_branched_path_surface_connect_light(kg,
516                                                                    sd,
517                                                                    emission_sd,
518                                                                    state,
519                                                                    throughput,
520                                                                    1.0f,
521                                                                    L,
522                                                                    all);
523                 }
524 #endif  /* defined(__EMISSION__) */
525
526                 if(!kernel_path_surface_bounce(kg, sd, &throughput, state, &L->state, ray))
527                         break;
528         }
529
530 #ifdef __SUBSURFACE__
531                 /* Trace indirect subsurface rays by restarting the loop. this uses less
532                  * stack memory than invoking kernel_path_indirect.
533                  */
534                 if(ss_indirect.num_rays) {
535                         kernel_path_subsurface_setup_indirect(kg,
536                                                               &ss_indirect,
537                                                               state,
538                                                               ray,
539                                                               L,
540                                                               &throughput);
541                 }
542                 else {
543                         break;
544                 }
545         }
546 #endif  /* __SUBSURFACE__ */
547 }
548
549 #endif /* defined(__BRANCHED_PATH__) || defined(__BAKING__) */
550
551 ccl_device_forceinline void kernel_path_integrate(
552         KernelGlobals *kg,
553         PathState *state,
554         float3 throughput,
555         Ray *ray,
556         PathRadiance *L,
557         ccl_global float *buffer,
558         ShaderData *emission_sd)
559 {
560         /* Shader data memory used for both volumes and surfaces, saves stack space. */
561         ShaderData sd;
562
563 #ifdef __SUBSURFACE__
564         SubsurfaceIndirectRays ss_indirect;
565         kernel_path_subsurface_init_indirect(&ss_indirect);
566
567         for(;;) {
568 #endif  /* __SUBSURFACE__ */
569
570         /* path iteration */
571         for(;;) {
572                 /* Find intersection with objects in scene. */
573                 Intersection isect;
574                 bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
575
576                 /* Find intersection with lamps and compute emission for MIS. */
577                 kernel_path_lamp_emission(kg, state, ray, throughput, &isect, &sd, L);
578
579 #ifdef __VOLUME__
580                 /* Volume integration. */
581                 VolumeIntegrateResult result = kernel_path_volume(kg,
582                                                                    &sd,
583                                                                    state,
584                                                                    ray,
585                                                                    &throughput,
586                                                                    &isect,
587                                                                    hit,
588                                                                    emission_sd,
589                                                                    L);
590
591                 if(result == VOLUME_PATH_SCATTERED) {
592                         continue;
593                 }
594                 else if(result == VOLUME_PATH_MISSED) {
595                         break;
596                 }
597 #endif /* __VOLUME__*/
598
599                 /* Shade background. */
600                 if(!hit) {
601                         kernel_path_background(kg, state, ray, throughput, &sd, L);
602                         break;
603                 }
604                 else if(path_state_ao_bounce(kg, state)) {
605                         break;
606                 }
607
608                 /* Setup and evaluate shader. */
609                 shader_setup_from_ray(kg, &sd, &isect, ray);
610                 shader_eval_surface(kg, &sd, state, state->flag);
611                 shader_prepare_closures(&sd, state);
612
613                 /* Apply shadow catcher, holdout, emission. */
614                 if(!kernel_path_shader_apply(kg,
615                                              &sd,
616                                              state,
617                                              ray,
618                                              throughput,
619                                              emission_sd,
620                                              L,
621                                              buffer))
622                 {
623                         break;
624                 }
625
626                 /* path termination. this is a strange place to put the termination, it's
627                  * mainly due to the mixed in MIS that we use. gives too many unneeded
628                  * shader evaluations, only need emission if we are going to terminate */
629                 float probability = path_state_continuation_probability(kg, state, throughput);
630
631                 if(probability == 0.0f) {
632                         break;
633                 }
634                 else if(probability != 1.0f) {
635                         float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
636                         if(terminate >= probability)
637                                 break;
638
639                         throughput /= probability;
640                 }
641
642                 kernel_update_denoising_features(kg, &sd, state, L);
643
644 #ifdef __AO__
645                 /* ambient occlusion */
646                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
647                         kernel_path_ao(kg, &sd, emission_sd, L, state, throughput, shader_bsdf_alpha(kg, &sd));
648                 }
649 #endif  /* __AO__ */
650
651 #ifdef __SUBSURFACE__
652                 /* bssrdf scatter to a different location on the same object, replacing
653                  * the closures with a diffuse BSDF */
654                 if(sd.flag & SD_BSSRDF) {
655                         if(kernel_path_subsurface_scatter(kg,
656                                                           &sd,
657                                                           emission_sd,
658                                                           L,
659                                                           state,
660                                                           ray,
661                                                           &throughput,
662                                                           &ss_indirect))
663                         {
664                                 break;
665                         }
666                 }
667 #endif  /* __SUBSURFACE__ */
668
669                 /* direct lighting */
670                 kernel_path_surface_connect_light(kg, &sd, emission_sd, throughput, state, L);
671
672                 /* compute direct lighting and next bounce */
673                 if(!kernel_path_surface_bounce(kg, &sd, &throughput, state, &L->state, ray))
674                         break;
675         }
676
677 #ifdef __SUBSURFACE__
678                 /* Trace indirect subsurface rays by restarting the loop. this uses less
679                  * stack memory than invoking kernel_path_indirect.
680                  */
681                 if(ss_indirect.num_rays) {
682                         kernel_path_subsurface_setup_indirect(kg,
683                                                               &ss_indirect,
684                                                               state,
685                                                               ray,
686                                                               L,
687                                                               &throughput);
688                 }
689                 else {
690                         break;
691                 }
692         }
693 #endif  /* __SUBSURFACE__ */
694 }
695
696 ccl_device void kernel_path_trace(KernelGlobals *kg,
697         ccl_global float *buffer,
698         int sample, int x, int y, int offset, int stride)
699 {
700         /* buffer offset */
701         int index = offset + x + y*stride;
702         int pass_stride = kernel_data.film.pass_stride;
703
704         buffer += index*pass_stride;
705
706         /* Initialize random numbers and sample ray. */
707         uint rng_hash;
708         Ray ray;
709
710         kernel_path_trace_setup(kg, sample, x, y, &rng_hash, &ray);
711
712         if(ray.t == 0.0f) {
713                 return;
714         }
715
716         /* Initialize state. */
717         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
718
719         PathRadiance L;
720         path_radiance_init(&L, kernel_data.film.use_light_pass);
721
722         ShaderDataTinyStorage emission_sd_storage;
723         ShaderData *emission_sd = AS_SHADER_DATA(&emission_sd_storage);
724
725         PathState state;
726         path_state_init(kg, emission_sd, &state, rng_hash, sample, &ray);
727
728         /* Integrate. */
729         kernel_path_integrate(kg,
730                               &state,
731                               throughput,
732                               &ray,
733                               &L,
734                               buffer,
735                               emission_sd);
736
737         kernel_write_result(kg, buffer, sample, &L);
738 }
739
740 #endif  /* __SPLIT_KERNEL__ */
741
742 CCL_NAMESPACE_END
743