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