Code cleanup: store branch factor in PathState.
[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 #ifdef __SUBSURFACE__
36 #  include "kernel/kernel_subsurface.h"
37 #endif
38
39 #ifdef __VOLUME__
40 #  include "kernel/kernel_volume.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 #ifdef __HAIR__
63         float difl = 0.0f, extmax = 0.0f;
64         uint lcg_state = 0;
65
66         if(kernel_data.bvh.have_curves) {
67                 if((kernel_data.cam.resolution == 1) && (state->flag & PATH_RAY_CAMERA)) {
68                         float3 pixdiff = ray->dD.dx + ray->dD.dy;
69                         /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
70                         difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
71                 }
72
73                 extmax = kernel_data.curve.maximum_width;
74                 lcg_state = lcg_state_init_addrspace(state, 0x51633e2d);
75         }
76
77         if(path_state_ao_bounce(kg, state)) {
78                 visibility = PATH_RAY_SHADOW;
79                 ray->t = kernel_data.background.ao_distance;
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 *emission_sd,
136         PathRadiance *L)
137 {
138         /* eval background shader if nothing hit */
139         if(kernel_data.background.transparent && (state->flag & PATH_RAY_CAMERA)) {
140                 L->transparent += average(throughput);
141
142 #ifdef __PASSES__
143                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
144 #endif  /* __PASSES__ */
145                         return;
146         }
147
148 #ifdef __BACKGROUND__
149         /* sample background shader */
150         float3 L_background = indirect_background(kg, emission_sd, state, ray);
151         path_radiance_accum_background(L, state, throughput, L_background);
152 #endif  /* __BACKGROUND__ */
153 }
154
155 #ifndef __SPLIT_KERNEL__
156
157 ccl_device_forceinline VolumeIntegrateResult kernel_path_volume(
158         KernelGlobals *kg,
159         ShaderData *sd,
160         PathState *state,
161         Ray *ray,
162         float3 *throughput,
163         ccl_addr_space Intersection *isect,
164         bool hit,
165         ShaderData *emission_sd,
166         PathRadiance *L)
167 {
168 #ifdef __VOLUME__
169         /* Sanitize volume stack. */
170         if(!hit) {
171                 kernel_volume_clean_stack(kg, state->volume_stack);
172         }
173         /* volume attenuation, emission, scatter */
174         if(state->volume_stack[0].shader != SHADER_NONE) {
175                 Ray volume_ray = *ray;
176                 volume_ray.t = (hit)? isect->t: FLT_MAX;
177
178                 bool heterogeneous = volume_stack_is_heterogeneous(kg, state->volume_stack);
179
180 #  ifdef __VOLUME_DECOUPLED__
181                 int sampling_method = volume_stack_sampling_method(kg, state->volume_stack);
182                 bool direct = (state->flag & PATH_RAY_CAMERA) != 0;
183                 bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, direct, sampling_method);
184
185                 if(decoupled) {
186                         /* cache steps along volume for repeated sampling */
187                         VolumeSegment volume_segment;
188
189                         shader_setup_from_volume(kg, sd, &volume_ray);
190                         kernel_volume_decoupled_record(kg, state,
191                                 &volume_ray, sd, &volume_segment, heterogeneous);
192
193                         volume_segment.sampling_method = sampling_method;
194
195                         /* emission */
196                         if(volume_segment.closure_flag & SD_EMISSION)
197                                 path_radiance_accum_emission(L, state, *throughput, volume_segment.accum_emission);
198
199                         /* scattering */
200                         VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
201
202                         if(volume_segment.closure_flag & SD_SCATTER) {
203                                 int all = kernel_data.integrator.sample_all_lights_indirect;
204
205                                 /* direct light sampling */
206                                 kernel_branched_path_volume_connect_light(kg, sd,
207                                         emission_sd, *throughput, state, L, all,
208                                         &volume_ray, &volume_segment);
209
210                                 /* indirect sample. if we use distance sampling and take just
211                                  * one sample for direct and indirect light, we could share
212                                  * this computation, but makes code a bit complex */
213                                 float rphase = path_state_rng_1D_for_decision(kg, state, PRNG_PHASE);
214                                 float rscatter = path_state_rng_1D_for_decision(kg, state, PRNG_SCATTER_DISTANCE);
215
216                                 result = kernel_volume_decoupled_scatter(kg,
217                                         state, &volume_ray, sd, throughput,
218                                         rphase, rscatter, &volume_segment, NULL, true);
219                         }
220
221                         /* free cached steps */
222                         kernel_volume_decoupled_free(kg, &volume_segment);
223
224                         if(result == VOLUME_PATH_SCATTERED) {
225                                 if(kernel_path_volume_bounce(kg, sd, throughput, state, L, ray))
226                                         return VOLUME_PATH_SCATTERED;
227                                 else
228                                         return VOLUME_PATH_MISSED;
229                         }
230                         else {
231                                 *throughput *= volume_segment.accum_transmittance;
232                         }
233                 }
234                 else
235 #  endif  /* __VOLUME_DECOUPLED__ */
236                 {
237                         /* integrate along volume segment with distance sampling */
238                         VolumeIntegrateResult result = kernel_volume_integrate(
239                                 kg, state, sd, &volume_ray, L, throughput, heterogeneous);
240
241 #  ifdef __VOLUME_SCATTER__
242                         if(result == VOLUME_PATH_SCATTERED) {
243                                 /* direct lighting */
244                                 kernel_path_volume_connect_light(kg, sd, emission_sd, *throughput, state, L);
245
246                                 /* indirect light bounce */
247                                 if(kernel_path_volume_bounce(kg, sd, throughput, state, L, ray))
248                                         return VOLUME_PATH_SCATTERED;
249                                 else
250                                         return VOLUME_PATH_MISSED;
251                         }
252 #  endif  /* __VOLUME_SCATTER__ */
253                 }
254         }
255 #endif  /* __VOLUME__ */
256
257         return VOLUME_PATH_ATTENUATED;
258 }
259
260 #endif /* __SPLIT_KERNEL__ */
261
262 ccl_device_forceinline bool kernel_path_shader_apply(
263         KernelGlobals *kg,
264         ShaderData *sd,
265         ccl_addr_space PathState *state,
266         ccl_addr_space Ray *ray,
267         float3 throughput,
268         ShaderData *emission_sd,
269         PathRadiance *L,
270         ccl_global float *buffer)
271 {
272 #ifdef __SHADOW_TRICKS__
273         if((sd->object_flag & SD_OBJECT_SHADOW_CATCHER)) {
274                 if(state->flag & PATH_RAY_CAMERA) {
275                         state->flag |= (PATH_RAY_SHADOW_CATCHER |
276                                                    PATH_RAY_STORE_SHADOW_INFO);
277
278                         float3 bg = make_float3(0.0f, 0.0f, 0.0f);
279                         if(!kernel_data.background.transparent) {
280                                 bg = indirect_background(kg, emission_sd, state, ray);
281                         }
282                         path_radiance_accum_shadowcatcher(L, throughput, bg);
283                 }
284         }
285         else if(state->flag & PATH_RAY_SHADOW_CATCHER) {
286                 /* Only update transparency after shadow catcher bounce. */
287                 L->shadow_transparency *=
288                                 average(shader_bsdf_transparency(kg, sd));
289         }
290 #endif  /* __SHADOW_TRICKS__ */
291
292         /* holdout */
293 #ifdef __HOLDOUT__
294         if(((sd->flag & SD_HOLDOUT) ||
295                 (sd->object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
296            (state->flag & PATH_RAY_CAMERA))
297         {
298                 if(kernel_data.background.transparent) {
299                         float3 holdout_weight;
300                         if(sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
301                                 holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
302                         }
303                         else {
304                                 holdout_weight = shader_holdout_eval(kg, sd);
305                         }
306                         /* any throughput is ok, should all be identical here */
307                         L->transparent += average(holdout_weight*throughput);
308                 }
309
310                 if(sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
311                         return false;
312                 }
313         }
314 #endif  /* __HOLDOUT__ */
315
316         /* holdout mask objects do not write data passes */
317         kernel_write_data_passes(kg, buffer, L, sd, state, throughput);
318
319         /* blurring of bsdf after bounces, for rays that have a small likelihood
320          * of following this particular path (diffuse, rough glossy) */
321         if(kernel_data.integrator.filter_glossy != FLT_MAX) {
322                 float blur_pdf = kernel_data.integrator.filter_glossy*state->min_ray_pdf;
323
324                 if(blur_pdf < 1.0f) {
325                         float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
326                         shader_bsdf_blur(kg, sd, blur_roughness);
327                 }
328         }
329
330 #ifdef __EMISSION__
331         /* emission */
332         if(sd->flag & SD_EMISSION) {
333                 float3 emission = indirect_primitive_emission(kg, sd, sd->ray_length, state->flag, state->ray_pdf);
334                 path_radiance_accum_emission(L, state, throughput, emission);
335         }
336 #endif  /* __EMISSION__ */
337
338         return true;
339 }
340
341 ccl_device_noinline void kernel_path_ao(KernelGlobals *kg,
342                                         ShaderData *sd,
343                                         ShaderData *emission_sd,
344                                         PathRadiance *L,
345                                         ccl_addr_space PathState *state,
346                                         float3 throughput,
347                                         float3 ao_alpha)
348 {
349         /* todo: solve correlation */
350         float bsdf_u, bsdf_v;
351
352         path_state_rng_2D(kg, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
353
354         float ao_factor = kernel_data.background.ao_factor;
355         float3 ao_N;
356         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
357         float3 ao_D;
358         float ao_pdf;
359
360         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
361
362         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
363                 Ray light_ray;
364                 float3 ao_shadow;
365
366                 light_ray.P = ray_offset(sd->P, sd->Ng);
367                 light_ray.D = ao_D;
368                 light_ray.t = kernel_data.background.ao_distance;
369                 light_ray.time = sd->time;
370                 light_ray.dP = sd->dP;
371                 light_ray.dD = differential3_zero();
372
373                 if(!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &ao_shadow)) {
374                         path_radiance_accum_ao(L, state, throughput, ao_alpha, ao_bsdf, ao_shadow);
375                 }
376                 else {
377                         path_radiance_accum_total_ao(L, state, throughput, ao_bsdf);
378                 }
379         }
380 }
381
382 #ifndef __SPLIT_KERNEL__
383
384 #if defined(__BRANCHED_PATH__) || defined(__BAKING__)
385
386 ccl_device void kernel_path_indirect(KernelGlobals *kg,
387                                      ShaderData *sd,
388                                      ShaderData *emission_sd,
389                                      Ray *ray,
390                                      float3 throughput,
391                                      PathState *state,
392                                      PathRadiance *L)
393 {
394         /* path iteration */
395         for(;;) {
396                 /* Find intersection with objects in scene. */
397                 Intersection isect;
398                 bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
399
400                 /* Find intersection with lamps and compute emission for MIS. */
401                 kernel_path_lamp_emission(kg, state, ray, throughput, &isect, emission_sd, L);
402
403                 /* Volume integration. */
404                 VolumeIntegrateResult result = kernel_path_volume(kg,
405                                                                    sd,
406                                                                    state,
407                                                                    ray,
408                                                                    &throughput,
409                                                                    &isect,
410                                                                    hit,
411                                                                    emission_sd,
412                                                                    L);
413
414                 if(result == VOLUME_PATH_SCATTERED) {
415                         continue;
416                 }
417                 else if(result == VOLUME_PATH_MISSED) {
418                         break;
419                 }
420
421                 /* Shade background. */
422                 if(!hit) {
423                         kernel_path_background(kg, state, ray, throughput, emission_sd, L);
424                         break;
425                 }
426                 else if(path_state_ao_bounce(kg, state)) {
427                         break;
428                 }
429
430                 /* Setup and evaluate shader. */
431                 shader_setup_from_ray(kg,
432                                       sd,
433                                       &isect,
434                                       ray);
435                 float rbsdf = path_state_rng_1D_for_decision(kg, state, PRNG_BSDF);
436                 shader_eval_surface(kg, sd, state, rbsdf, state->flag);
437 #ifdef __BRANCHED_PATH__
438                 shader_merge_closures(sd);
439 #endif  /* __BRANCHED_PATH__ */
440
441                 /* Apply shadow catcher, holdout, emission. */
442                 if(!kernel_path_shader_apply(kg,
443                                              sd,
444                                              state,
445                                              ray,
446                                              throughput,
447                                              emission_sd,
448                                              L,
449                                              NULL))
450                 {
451                         break;
452                 }
453
454                 /* path termination. this is a strange place to put the termination, it's
455                  * mainly due to the mixed in MIS that we use. gives too many unneeded
456                  * shader evaluations, only need emission if we are going to terminate */
457                 float probability = path_state_continuation_probability(kg, state, throughput);
458
459                 if(probability == 0.0f) {
460                         break;
461                 }
462                 else if(probability != 1.0f) {
463                         float terminate = path_state_rng_1D_for_decision(kg, state, PRNG_TERMINATE);
464
465                         if(terminate >= probability)
466                                 break;
467
468                         throughput /= probability;
469                 }
470
471                 kernel_update_denoising_features(kg, sd, state, L);
472
473 #ifdef __AO__
474                 /* ambient occlusion */
475                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
476                         kernel_path_ao(kg, sd, emission_sd, L, state, throughput, make_float3(0.0f, 0.0f, 0.0f));
477                 }
478 #endif  /* __AO__ */
479
480 #ifdef __SUBSURFACE__
481                 /* bssrdf scatter to a different location on the same object, replacing
482                  * the closures with a diffuse BSDF */
483                 if(sd->flag & SD_BSSRDF) {
484                         float bssrdf_probability;
485                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
486
487                         /* modify throughput for picking bssrdf or bsdf */
488                         throughput *= bssrdf_probability;
489
490                         /* do bssrdf scatter step if we picked a bssrdf closure */
491                         if(sc) {
492                                 uint lcg_state = lcg_state_init(state, 0x68bc21eb);
493
494                                 float bssrdf_u, bssrdf_v;
495                                 path_state_rng_2D(kg,
496                                                   state,
497                                                   PRNG_BSDF_U,
498                                                   &bssrdf_u, &bssrdf_v);
499                                 subsurface_scatter_step(kg,
500                                                         sd,
501                                                         state,
502                                                         state->flag,
503                                                         sc,
504                                                         &lcg_state,
505                                                         bssrdf_u, bssrdf_v,
506                                                         false);
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, ray))
527                         break;
528         }
529 }
530
531 #endif /* defined(__BRANCHED_PATH__) || defined(__BAKING__) */
532
533 ccl_device_forceinline void kernel_path_integrate(
534         KernelGlobals *kg,
535         PathState *state,
536         float3 throughput,
537         Ray *ray,
538         PathRadiance *L,
539         ccl_global float *buffer,
540         ShaderData *emission_sd)
541 {
542         /* Shader data memory used for both volumes and surfaces, saves stack space. */
543         ShaderData sd;
544
545 #ifdef __SUBSURFACE__
546         SubsurfaceIndirectRays ss_indirect;
547         kernel_path_subsurface_init_indirect(&ss_indirect);
548
549         for(;;) {
550 #endif  /* __SUBSURFACE__ */
551
552         /* path iteration */
553         for(;;) {
554                 /* Find intersection with objects in scene. */
555                 Intersection isect;
556                 bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
557
558                 /* Find intersection with lamps and compute emission for MIS. */
559                 kernel_path_lamp_emission(kg, state, ray, throughput, &isect, emission_sd, L);
560
561                 /* Volume integration. */
562                 VolumeIntegrateResult result = kernel_path_volume(kg,
563                                                                    &sd,
564                                                                    state,
565                                                                    ray,
566                                                                    &throughput,
567                                                                    &isect,
568                                                                    hit,
569                                                                    emission_sd,
570                                                                    L);
571
572                 if(result == VOLUME_PATH_SCATTERED) {
573                         continue;
574                 }
575                 else if(result == VOLUME_PATH_MISSED) {
576                         break;
577                 }
578
579                 /* Shade background. */
580                 if(!hit) {
581                         kernel_path_background(kg, state, ray, throughput, emission_sd, L);
582                         break;
583                 }
584                 else if(path_state_ao_bounce(kg, state)) {
585                         break;
586                 }
587
588                 /* Setup and evaluate shader. */
589                 shader_setup_from_ray(kg, &sd, &isect, ray);
590                 float rbsdf = path_state_rng_1D_for_decision(kg, state, PRNG_BSDF);
591                 shader_eval_surface(kg, &sd, state, rbsdf, state->flag);
592
593                 /* Apply shadow catcher, holdout, emission. */
594                 if(!kernel_path_shader_apply(kg,
595                                              &sd,
596                                              state,
597                                              ray,
598                                              throughput,
599                                              emission_sd,
600                                              L,
601                                              buffer))
602                 {
603                         break;
604                 }
605
606                 /* path termination. this is a strange place to put the termination, it's
607                  * mainly due to the mixed in MIS that we use. gives too many unneeded
608                  * shader evaluations, only need emission if we are going to terminate */
609                 float probability = path_state_continuation_probability(kg, state, throughput);
610
611                 if(probability == 0.0f) {
612                         break;
613                 }
614                 else if(probability != 1.0f) {
615                         float terminate = path_state_rng_1D_for_decision(kg, state, PRNG_TERMINATE);
616                         if(terminate >= probability)
617                                 break;
618
619                         throughput /= probability;
620                 }
621
622                 kernel_update_denoising_features(kg, &sd, state, L);
623
624 #ifdef __AO__
625                 /* ambient occlusion */
626                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
627                         kernel_path_ao(kg, &sd, emission_sd, L, state, throughput, shader_bsdf_alpha(kg, &sd));
628                 }
629 #endif  /* __AO__ */
630
631 #ifdef __SUBSURFACE__
632                 /* bssrdf scatter to a different location on the same object, replacing
633                  * the closures with a diffuse BSDF */
634                 if(sd.flag & SD_BSSRDF) {
635                         if(kernel_path_subsurface_scatter(kg,
636                                                           &sd,
637                                                           emission_sd,
638                                                           L,
639                                                           state,
640                                                           ray,
641                                                           &throughput,
642                                                           &ss_indirect))
643                         {
644                                 break;
645                         }
646                 }
647 #endif  /* __SUBSURFACE__ */
648
649                 /* direct lighting */
650                 kernel_path_surface_connect_light(kg, &sd, emission_sd, throughput, state, L);
651
652                 /* compute direct lighting and next bounce */
653                 if(!kernel_path_surface_bounce(kg, &sd, &throughput, state, L, ray))
654                         break;
655         }
656
657 #ifdef __SUBSURFACE__
658                 kernel_path_subsurface_accum_indirect(&ss_indirect, L);
659
660                 /* Trace indirect subsurface rays by restarting the loop. this uses less
661                  * stack memory than invoking kernel_path_indirect.
662                  */
663                 if(ss_indirect.num_rays) {
664                         kernel_path_subsurface_setup_indirect(kg,
665                                                               &ss_indirect,
666                                                               state,
667                                                               ray,
668                                                               L,
669                                                               &throughput);
670                 }
671                 else {
672                         break;
673                 }
674         }
675 #endif  /* __SUBSURFACE__ */
676 }
677
678 ccl_device void kernel_path_trace(KernelGlobals *kg,
679         ccl_global float *buffer, ccl_global uint *rng_state,
680         int sample, int x, int y, int offset, int stride)
681 {
682         /* buffer offset */
683         int index = offset + x + y*stride;
684         int pass_stride = kernel_data.film.pass_stride;
685
686         rng_state += index;
687         buffer += index*pass_stride;
688
689         /* Initialize random numbers and sample ray. */
690         uint rng_hash;
691         Ray ray;
692
693         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng_hash, &ray);
694
695         if(ray.t == 0.0f) {
696                 kernel_write_result(kg, buffer, sample, NULL);
697                 return;
698         }
699
700         /* Initialize state. */
701         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
702
703         PathRadiance L;
704         path_radiance_init(&L, kernel_data.film.use_light_pass);
705
706         ShaderData emission_sd;
707         PathState state;
708         path_state_init(kg, &emission_sd, &state, rng_hash, sample, &ray);
709
710         /* Integrate. */
711         kernel_path_integrate(kg,
712                               &state,
713                               throughput,
714                               &ray,
715                               &L,
716                               buffer,
717                               &emission_sd);
718
719         kernel_write_result(kg, buffer, sample, &L);
720 }
721
722 #endif  /* __SPLIT_KERNEL__ */
723
724 CCL_NAMESPACE_END
725