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