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