ClangFormat: apply to source, most of intern
[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(KernelGlobals *kg,
54                                                         ccl_addr_space PathState *state,
55                                                         Ray *ray,
56                                                         Intersection *isect,
57                                                         PathRadiance *L)
58 {
59   PROFILING_INIT(kg, PROFILING_SCENE_INTERSECT);
60
61   uint visibility = path_state_ray_visibility(kg, state);
62
63   if (path_state_ao_bounce(kg, state)) {
64     visibility = PATH_RAY_SHADOW;
65     ray->t = kernel_data.background.ao_distance;
66   }
67
68 #ifdef __HAIR__
69   float difl = 0.0f, extmax = 0.0f;
70   uint lcg_state = 0;
71
72   if (kernel_data.bvh.have_curves) {
73     if ((kernel_data.cam.resolution == 1) && (state->flag & PATH_RAY_CAMERA)) {
74       float3 pixdiff = ray->dD.dx + ray->dD.dy;
75       /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
76       difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
77     }
78
79     extmax = kernel_data.curve.maximum_width;
80     lcg_state = lcg_state_init_addrspace(state, 0x51633e2d);
81   }
82
83   bool hit = scene_intersect(kg, *ray, visibility, isect, &lcg_state, difl, extmax);
84 #else
85   bool hit = scene_intersect(kg, *ray, visibility, isect, NULL, 0.0f, 0.0f);
86 #endif /* __HAIR__ */
87
88 #ifdef __KERNEL_DEBUG__
89   if (state->flag & PATH_RAY_CAMERA) {
90     L->debug_data.num_bvh_traversed_nodes += isect->num_traversed_nodes;
91     L->debug_data.num_bvh_traversed_instances += isect->num_traversed_instances;
92     L->debug_data.num_bvh_intersections += isect->num_intersections;
93   }
94   L->debug_data.num_ray_bounces++;
95 #endif /* __KERNEL_DEBUG__ */
96
97   return hit;
98 }
99
100 ccl_device_forceinline void kernel_path_lamp_emission(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   PROFILING_INIT(kg, PROFILING_INDIRECT_EMISSION);
109
110 #ifdef __LAMP_MIS__
111   if (kernel_data.integrator.use_lamp_mis && !(state->flag & PATH_RAY_CAMERA)) {
112     /* ray starting from previous non-transparent bounce */
113     Ray light_ray;
114
115     light_ray.P = ray->P - state->ray_t * ray->D;
116     state->ray_t += isect->t;
117     light_ray.D = ray->D;
118     light_ray.t = state->ray_t;
119     light_ray.time = ray->time;
120     light_ray.dD = ray->dD;
121     light_ray.dP = ray->dP;
122
123     /* intersect with lamp */
124     float3 emission;
125
126     if (indirect_lamp_emission(kg, emission_sd, state, &light_ray, &emission))
127       path_radiance_accum_emission(L, state, throughput, emission);
128   }
129 #endif /* __LAMP_MIS__ */
130 }
131
132 ccl_device_forceinline void kernel_path_background(KernelGlobals *kg,
133                                                    ccl_addr_space PathState *state,
134                                                    ccl_addr_space Ray *ray,
135                                                    float3 throughput,
136                                                    ShaderData *sd,
137                                                    PathRadiance *L)
138 {
139   /* eval background shader if nothing hit */
140   if (kernel_data.background.transparent && (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
141     L->transparent += average(throughput);
142
143 #ifdef __PASSES__
144     if (!(kernel_data.film.light_pass_flag & PASSMASK(BACKGROUND)))
145 #endif /* __PASSES__ */
146       return;
147   }
148
149   /* When using the ao bounces approximation, adjust background
150    * shader intensity with ao factor. */
151   if (path_state_ao_bounce(kg, state)) {
152     throughput *= kernel_data.background.ao_bounces_factor;
153   }
154
155 #ifdef __BACKGROUND__
156   /* sample background shader */
157   float3 L_background = indirect_background(kg, sd, state, ray);
158   path_radiance_accum_background(L, state, throughput, L_background);
159 #endif /* __BACKGROUND__ */
160 }
161
162 #ifndef __SPLIT_KERNEL__
163
164 #  ifdef __VOLUME__
165 ccl_device_forceinline VolumeIntegrateResult kernel_path_volume(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   PROFILING_INIT(kg, PROFILING_VOLUME);
176
177   /* Sanitize volume stack. */
178   if (!hit) {
179     kernel_volume_clean_stack(kg, state->volume_stack);
180   }
181
182   if (state->volume_stack[0].shader == SHADER_NONE) {
183     return VOLUME_PATH_ATTENUATED;
184   }
185
186   /* volume attenuation, emission, scatter */
187   Ray volume_ray = *ray;
188   volume_ray.t = (hit) ? isect->t : FLT_MAX;
189
190   bool heterogeneous = volume_stack_is_heterogeneous(kg, state->volume_stack);
191
192 #    ifdef __VOLUME_DECOUPLED__
193   int sampling_method = volume_stack_sampling_method(kg, state->volume_stack);
194   bool direct = (state->flag & PATH_RAY_CAMERA) != 0;
195   bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, direct, sampling_method);
196
197   if (decoupled) {
198     /* cache steps along volume for repeated sampling */
199     VolumeSegment volume_segment;
200
201     shader_setup_from_volume(kg, sd, &volume_ray);
202     kernel_volume_decoupled_record(kg, state, &volume_ray, sd, &volume_segment, heterogeneous);
203
204     volume_segment.sampling_method = sampling_method;
205
206     /* emission */
207     if (volume_segment.closure_flag & SD_EMISSION)
208       path_radiance_accum_emission(L, state, *throughput, volume_segment.accum_emission);
209
210     /* scattering */
211     VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
212
213     if (volume_segment.closure_flag & SD_SCATTER) {
214       int all = kernel_data.integrator.sample_all_lights_indirect;
215
216       /* direct light sampling */
217       kernel_branched_path_volume_connect_light(
218           kg, sd, emission_sd, *throughput, state, L, all, &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(
227           kg, state, &volume_ray, sd, throughput, rphase, rscatter, &volume_segment, NULL, true);
228     }
229
230     /* free cached steps */
231     kernel_volume_decoupled_free(kg, &volume_segment);
232
233     if (result == VOLUME_PATH_SCATTERED) {
234       if (kernel_path_volume_bounce(kg, sd, throughput, state, &L->state, ray))
235         return VOLUME_PATH_SCATTERED;
236       else
237         return VOLUME_PATH_MISSED;
238     }
239     else {
240       *throughput *= volume_segment.accum_transmittance;
241     }
242   }
243   else
244 #    endif /* __VOLUME_DECOUPLED__ */
245   {
246     /* integrate along volume segment with distance sampling */
247     VolumeIntegrateResult result = kernel_volume_integrate(
248         kg, state, sd, &volume_ray, L, throughput, heterogeneous);
249
250 #    ifdef __VOLUME_SCATTER__
251     if (result == VOLUME_PATH_SCATTERED) {
252       /* direct lighting */
253       kernel_path_volume_connect_light(kg, sd, emission_sd, *throughput, state, L);
254
255       /* indirect light bounce */
256       if (kernel_path_volume_bounce(kg, sd, throughput, state, &L->state, ray))
257         return VOLUME_PATH_SCATTERED;
258       else
259         return VOLUME_PATH_MISSED;
260     }
261 #    endif /* __VOLUME_SCATTER__ */
262   }
263
264   return VOLUME_PATH_ATTENUATED;
265 }
266 #  endif /* __VOLUME__ */
267
268 #endif /* __SPLIT_KERNEL__ */
269
270 ccl_device_forceinline bool kernel_path_shader_apply(KernelGlobals *kg,
271                                                      ShaderData *sd,
272                                                      ccl_addr_space PathState *state,
273                                                      ccl_addr_space Ray *ray,
274                                                      float3 throughput,
275                                                      ShaderData *emission_sd,
276                                                      PathRadiance *L,
277                                                      ccl_global float *buffer)
278 {
279   PROFILING_INIT(kg, PROFILING_SHADER_APPLY);
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 | PATH_RAY_STORE_SHADOW_INFO);
285
286       float3 bg = make_float3(0.0f, 0.0f, 0.0f);
287       if (!kernel_data.background.transparent) {
288         bg = indirect_background(kg, emission_sd, state, ray);
289       }
290       path_radiance_accum_shadowcatcher(L, throughput, bg);
291     }
292   }
293   else if (state->flag & PATH_RAY_SHADOW_CATCHER) {
294     /* Only update transparency after shadow catcher bounce. */
295     L->shadow_transparency *= average(shader_bsdf_transparency(kg, sd));
296   }
297 #endif /* __SHADOW_TRICKS__ */
298
299   /* holdout */
300 #ifdef __HOLDOUT__
301   if (((sd->flag & SD_HOLDOUT) || (sd->object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
302       (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
303     if (kernel_data.background.transparent) {
304       float3 holdout_weight;
305       if (sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
306         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
307       }
308       else {
309         holdout_weight = shader_holdout_eval(kg, sd);
310       }
311       /* any throughput is ok, should all be identical here */
312       L->transparent += average(holdout_weight * throughput);
313     }
314
315     if (sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
316       return false;
317     }
318   }
319 #endif /* __HOLDOUT__ */
320
321   /* holdout mask objects do not write data passes */
322   kernel_write_data_passes(kg, buffer, L, sd, state, throughput);
323
324   /* blurring of bsdf after bounces, for rays that have a small likelihood
325    * of following this particular path (diffuse, rough glossy) */
326   if (kernel_data.integrator.filter_glossy != FLT_MAX) {
327     float blur_pdf = kernel_data.integrator.filter_glossy * state->min_ray_pdf;
328
329     if (blur_pdf < 1.0f) {
330       float blur_roughness = sqrtf(1.0f - blur_pdf) * 0.5f;
331       shader_bsdf_blur(kg, sd, blur_roughness);
332     }
333   }
334
335 #ifdef __EMISSION__
336   /* emission */
337   if (sd->flag & SD_EMISSION) {
338     float3 emission = indirect_primitive_emission(
339         kg, sd, sd->ray_length, state->flag, state->ray_pdf);
340     path_radiance_accum_emission(L, state, throughput, emission);
341   }
342 #endif /* __EMISSION__ */
343
344   return true;
345 }
346
347 ccl_device_noinline void kernel_path_ao(KernelGlobals *kg,
348                                         ShaderData *sd,
349                                         ShaderData *emission_sd,
350                                         PathRadiance *L,
351                                         ccl_addr_space PathState *state,
352                                         float3 throughput,
353                                         float3 ao_alpha)
354 {
355   PROFILING_INIT(kg, PROFILING_AO);
356
357   /* todo: solve correlation */
358   float bsdf_u, bsdf_v;
359
360   path_state_rng_2D(kg, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
361
362   float ao_factor = kernel_data.background.ao_factor;
363   float3 ao_N;
364   float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
365   float3 ao_D;
366   float ao_pdf;
367
368   sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
369
370   if (dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
371     Ray light_ray;
372     float3 ao_shadow;
373
374     light_ray.P = ray_offset(sd->P, sd->Ng);
375     light_ray.D = ao_D;
376     light_ray.t = kernel_data.background.ao_distance;
377     light_ray.time = sd->time;
378     light_ray.dP = sd->dP;
379     light_ray.dD = differential3_zero();
380
381     if (!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &ao_shadow)) {
382       path_radiance_accum_ao(L, state, throughput, ao_alpha, ao_bsdf, ao_shadow);
383     }
384     else {
385       path_radiance_accum_total_ao(L, state, throughput, ao_bsdf);
386     }
387   }
388 }
389
390 #ifndef __SPLIT_KERNEL__
391
392 #  if defined(__BRANCHED_PATH__) || defined(__BAKING__)
393
394 ccl_device void kernel_path_indirect(KernelGlobals *kg,
395                                      ShaderData *sd,
396                                      ShaderData *emission_sd,
397                                      Ray *ray,
398                                      float3 throughput,
399                                      PathState *state,
400                                      PathRadiance *L)
401 {
402 #    ifdef __SUBSURFACE__
403   SubsurfaceIndirectRays ss_indirect;
404   kernel_path_subsurface_init_indirect(&ss_indirect);
405
406   for (;;) {
407 #    endif /* __SUBSURFACE__ */
408
409     /* path iteration */
410     for (;;) {
411       /* Find intersection with objects in scene. */
412       Intersection isect;
413       bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
414
415       /* Find intersection with lamps and compute emission for MIS. */
416       kernel_path_lamp_emission(kg, state, ray, throughput, &isect, sd, L);
417
418 #    ifdef __VOLUME__
419       /* Volume integration. */
420       VolumeIntegrateResult result = kernel_path_volume(
421           kg, sd, state, ray, &throughput, &isect, hit, emission_sd, L);
422
423       if (result == VOLUME_PATH_SCATTERED) {
424         continue;
425       }
426       else if (result == VOLUME_PATH_MISSED) {
427         break;
428       }
429 #    endif /* __VOLUME__*/
430
431       /* Shade background. */
432       if (!hit) {
433         kernel_path_background(kg, state, ray, throughput, sd, L);
434         break;
435       }
436       else if (path_state_ao_bounce(kg, state)) {
437         break;
438       }
439
440       /* Setup shader data. */
441       shader_setup_from_ray(kg, sd, &isect, ray);
442
443       /* Skip most work for volume bounding surface. */
444 #    ifdef __VOLUME__
445       if (!(sd->flag & SD_HAS_ONLY_VOLUME)) {
446 #    endif
447
448         /* Evaluate shader. */
449         shader_eval_surface(kg, sd, state, state->flag);
450         shader_prepare_closures(sd, state);
451
452         /* Apply shadow catcher, holdout, emission. */
453         if (!kernel_path_shader_apply(kg, sd, state, ray, throughput, emission_sd, L, NULL)) {
454           break;
455         }
456
457         /* path termination. this is a strange place to put the termination, it's
458      * mainly due to the mixed in MIS that we use. gives too many unneeded
459      * shader evaluations, only need emission if we are going to terminate */
460         float probability = path_state_continuation_probability(kg, state, throughput);
461
462         if (probability == 0.0f) {
463           break;
464         }
465         else if (probability != 1.0f) {
466           float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
467
468           if (terminate >= probability)
469             break;
470
471           throughput /= probability;
472         }
473
474         kernel_update_denoising_features(kg, sd, state, L);
475
476 #    ifdef __AO__
477         /* ambient occlusion */
478         if (kernel_data.integrator.use_ambient_occlusion) {
479           kernel_path_ao(kg, sd, emission_sd, L, state, throughput, make_float3(0.0f, 0.0f, 0.0f));
480         }
481 #    endif /* __AO__ */
482
483 #    ifdef __SUBSURFACE__
484         /* bssrdf scatter to a different location on the same object, replacing
485      * the closures with a diffuse BSDF */
486         if (sd->flag & SD_BSSRDF) {
487           if (kernel_path_subsurface_scatter(
488                   kg, sd, emission_sd, L, state, ray, &throughput, &ss_indirect)) {
489             break;
490           }
491         }
492 #    endif /* __SUBSURFACE__ */
493
494 #    if defined(__EMISSION__)
495         if (kernel_data.integrator.use_direct_light) {
496           int all = (kernel_data.integrator.sample_all_lights_indirect) ||
497                     (state->flag & PATH_RAY_SHADOW_CATCHER);
498           kernel_branched_path_surface_connect_light(
499               kg, sd, emission_sd, state, throughput, 1.0f, L, all);
500         }
501 #    endif /* defined(__EMISSION__) */
502
503 #    ifdef __VOLUME__
504       }
505 #    endif
506
507       if (!kernel_path_surface_bounce(kg, sd, &throughput, state, &L->state, ray))
508         break;
509     }
510
511 #    ifdef __SUBSURFACE__
512     /* Trace indirect subsurface rays by restarting the loop. this uses less
513      * stack memory than invoking kernel_path_indirect.
514      */
515     if (ss_indirect.num_rays) {
516       kernel_path_subsurface_setup_indirect(kg, &ss_indirect, state, ray, L, &throughput);
517     }
518     else {
519       break;
520     }
521   }
522 #    endif /* __SUBSURFACE__ */
523 }
524
525 #  endif /* defined(__BRANCHED_PATH__) || defined(__BAKING__) */
526
527 ccl_device_forceinline void kernel_path_integrate(KernelGlobals *kg,
528                                                   PathState *state,
529                                                   float3 throughput,
530                                                   Ray *ray,
531                                                   PathRadiance *L,
532                                                   ccl_global float *buffer,
533                                                   ShaderData *emission_sd)
534 {
535   PROFILING_INIT(kg, PROFILING_PATH_INTEGRATE);
536
537   /* Shader data memory used for both volumes and surfaces, saves stack space. */
538   ShaderData sd;
539
540 #  ifdef __SUBSURFACE__
541   SubsurfaceIndirectRays ss_indirect;
542   kernel_path_subsurface_init_indirect(&ss_indirect);
543
544   for (;;) {
545 #  endif /* __SUBSURFACE__ */
546
547     /* path iteration */
548     for (;;) {
549       /* Find intersection with objects in scene. */
550       Intersection isect;
551       bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
552
553       /* Find intersection with lamps and compute emission for MIS. */
554       kernel_path_lamp_emission(kg, state, ray, throughput, &isect, &sd, L);
555
556 #  ifdef __VOLUME__
557       /* Volume integration. */
558       VolumeIntegrateResult result = kernel_path_volume(
559           kg, &sd, state, ray, &throughput, &isect, hit, emission_sd, L);
560
561       if (result == VOLUME_PATH_SCATTERED) {
562         continue;
563       }
564       else if (result == VOLUME_PATH_MISSED) {
565         break;
566       }
567 #  endif /* __VOLUME__*/
568
569       /* Shade background. */
570       if (!hit) {
571         kernel_path_background(kg, state, ray, throughput, &sd, L);
572         break;
573       }
574       else if (path_state_ao_bounce(kg, state)) {
575         break;
576       }
577
578       /* Setup shader data. */
579       shader_setup_from_ray(kg, &sd, &isect, ray);
580
581       /* Skip most work for volume bounding surface. */
582 #  ifdef __VOLUME__
583       if (!(sd.flag & SD_HAS_ONLY_VOLUME)) {
584 #  endif
585
586         /* Evaluate shader. */
587         shader_eval_surface(kg, &sd, state, state->flag);
588         shader_prepare_closures(&sd, state);
589
590         /* Apply shadow catcher, holdout, emission. */
591         if (!kernel_path_shader_apply(kg, &sd, state, ray, throughput, emission_sd, L, buffer)) {
592           break;
593         }
594
595         /* path termination. this is a strange place to put the termination, it's
596      * mainly due to the mixed in MIS that we use. gives too many unneeded
597      * shader evaluations, only need emission if we are going to terminate */
598         float probability = path_state_continuation_probability(kg, state, throughput);
599
600         if (probability == 0.0f) {
601           break;
602         }
603         else if (probability != 1.0f) {
604           float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
605           if (terminate >= probability)
606             break;
607
608           throughput /= probability;
609         }
610
611         kernel_update_denoising_features(kg, &sd, state, L);
612
613 #  ifdef __AO__
614         /* ambient occlusion */
615         if (kernel_data.integrator.use_ambient_occlusion) {
616           kernel_path_ao(kg, &sd, emission_sd, L, state, throughput, shader_bsdf_alpha(kg, &sd));
617         }
618 #  endif /* __AO__ */
619
620 #  ifdef __SUBSURFACE__
621         /* bssrdf scatter to a different location on the same object, replacing
622      * the closures with a diffuse BSDF */
623         if (sd.flag & SD_BSSRDF) {
624           if (kernel_path_subsurface_scatter(
625                   kg, &sd, emission_sd, L, state, ray, &throughput, &ss_indirect)) {
626             break;
627           }
628         }
629 #  endif /* __SUBSURFACE__ */
630
631         /* direct lighting */
632         kernel_path_surface_connect_light(kg, &sd, emission_sd, throughput, state, L);
633
634 #  ifdef __VOLUME__
635       }
636 #  endif
637
638       /* compute direct lighting and next bounce */
639       if (!kernel_path_surface_bounce(kg, &sd, &throughput, state, &L->state, ray))
640         break;
641     }
642
643 #  ifdef __SUBSURFACE__
644     /* Trace indirect subsurface rays by restarting the loop. this uses less
645      * stack memory than invoking kernel_path_indirect.
646      */
647     if (ss_indirect.num_rays) {
648       kernel_path_subsurface_setup_indirect(kg, &ss_indirect, state, ray, L, &throughput);
649     }
650     else {
651       break;
652     }
653   }
654 #  endif /* __SUBSURFACE__ */
655 }
656
657 ccl_device void kernel_path_trace(
658     KernelGlobals *kg, ccl_global float *buffer, int sample, int x, int y, int offset, int stride)
659 {
660   PROFILING_INIT(kg, PROFILING_RAY_SETUP);
661
662   /* buffer offset */
663   int index = offset + x + y * stride;
664   int pass_stride = kernel_data.film.pass_stride;
665
666   buffer += index * pass_stride;
667
668   /* Initialize random numbers and sample ray. */
669   uint rng_hash;
670   Ray ray;
671
672   kernel_path_trace_setup(kg, sample, x, y, &rng_hash, &ray);
673
674   if (ray.t == 0.0f) {
675     return;
676   }
677
678   /* Initialize state. */
679   float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
680
681   PathRadiance L;
682   path_radiance_init(&L, kernel_data.film.use_light_pass);
683
684   ShaderDataTinyStorage emission_sd_storage;
685   ShaderData *emission_sd = AS_SHADER_DATA(&emission_sd_storage);
686
687   PathState state;
688   path_state_init(kg, emission_sd, &state, rng_hash, sample, &ray);
689
690   /* Integrate. */
691   kernel_path_integrate(kg, &state, throughput, &ray, &L, buffer, emission_sd);
692
693   kernel_write_result(kg, buffer, sample, &L);
694 }
695
696 #endif /* __SPLIT_KERNEL__ */
697
698 CCL_NAMESPACE_END