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