Cycles: First implementation of shadow catcher
[blender.git] / intern / cycles / kernel / kernel_path_branched.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 CCL_NAMESPACE_BEGIN
18
19 #ifdef __BRANCHED_PATH__
20
21 ccl_device_inline void kernel_branched_path_ao(KernelGlobals *kg,
22                                                ShaderData *sd,
23                                                ShaderData *emission_sd,
24                                                PathRadiance *L,
25                                                PathState *state,
26                                                RNG *rng,
27                                                float3 throughput)
28 {
29         int num_samples = kernel_data.integrator.ao_samples;
30         float num_samples_inv = 1.0f/num_samples;
31         float ao_factor = kernel_data.background.ao_factor;
32         float3 ao_N;
33         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
34         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
35
36         for(int j = 0; j < num_samples; j++) {
37                 float bsdf_u, bsdf_v;
38                 path_branched_rng_2D(kg, rng, state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
39
40                 float3 ao_D;
41                 float ao_pdf;
42
43                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
44
45                 if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
46                         Ray light_ray;
47                         float3 ao_shadow;
48
49                         light_ray.P = ray_offset(sd->P, sd->Ng);
50                         light_ray.D = ao_D;
51                         light_ray.t = kernel_data.background.ao_distance;
52 #ifdef __OBJECT_MOTION__
53                         light_ray.time = sd->time;
54 #endif  /* __OBJECT_MOTION__ */
55                         light_ray.dP = sd->dP;
56                         light_ray.dD = differential3_zero();
57
58                         if(!shadow_blocked(kg, emission_sd, state, &light_ray, &ao_shadow)) {
59                                 path_radiance_accum_ao(L, throughput*num_samples_inv, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
60                         }
61                         else {
62                                 path_radiance_accum_total_ao(L, throughput*num_samples_inv, ao_bsdf);
63                         }
64                 }
65         }
66 }
67
68
69 /* bounce off surface and integrate indirect light */
70 ccl_device_noinline void kernel_branched_path_surface_indirect_light(KernelGlobals *kg,
71         RNG *rng, ShaderData *sd, ShaderData *indirect_sd, ShaderData *emission_sd,
72         float3 throughput, float num_samples_adjust, PathState *state, PathRadiance *L)
73 {
74         for(int i = 0; i < sd->num_closure; i++) {
75                 const ShaderClosure *sc = &sd->closure[i];
76
77                 if(!CLOSURE_IS_BSDF(sc->type))
78                         continue;
79                 /* transparency is not handled here, but in outer loop */
80                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
81                         continue;
82
83                 int num_samples;
84
85                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
86                         num_samples = kernel_data.integrator.diffuse_samples;
87                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
88                         num_samples = 1;
89                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
90                         num_samples = kernel_data.integrator.glossy_samples;
91                 else
92                         num_samples = kernel_data.integrator.transmission_samples;
93
94                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
95
96                 float num_samples_inv = num_samples_adjust/num_samples;
97                 RNG bsdf_rng = cmj_hash(*rng, i);
98
99                 for(int j = 0; j < num_samples; j++) {
100                         PathState ps = *state;
101                         float3 tp = throughput;
102                         Ray bsdf_ray;
103
104                         if(!kernel_branched_path_surface_bounce(kg,
105                                                                 &bsdf_rng,
106                                                                 sd,
107                                                                 sc,
108                                                                 j,
109                                                                 num_samples,
110                                                                 &tp,
111                                                                 &ps,
112                                                                 L,
113                                                                 &bsdf_ray))
114                         {
115                                 continue;
116                         }
117
118                         kernel_path_indirect(kg,
119                                              indirect_sd,
120                                              emission_sd,
121                                              rng,
122                                              &bsdf_ray,
123                                              tp*num_samples_inv,
124                                              num_samples,
125                                              &ps,
126                                              L);
127
128                         /* for render passes, sum and reset indirect light pass variables
129                          * for the next samples */
130                         path_radiance_sum_indirect(L);
131                         path_radiance_reset_indirect(L);
132                 }
133         }
134 }
135
136 #ifdef __SUBSURFACE__
137 ccl_device void kernel_branched_path_subsurface_scatter(KernelGlobals *kg,
138                                                         ShaderData *sd,
139                                                         ShaderData *indirect_sd,
140                                                         ShaderData *emission_sd,
141                                                         PathRadiance *L,
142                                                         PathState *state,
143                                                         RNG *rng,
144                                                         Ray *ray,
145                                                         float3 throughput)
146 {
147         for(int i = 0; i < sd->num_closure; i++) {
148                 ShaderClosure *sc = &sd->closure[i];
149
150                 if(!CLOSURE_IS_BSSRDF(sc->type))
151                         continue;
152
153                 /* set up random number generator */
154                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
155                 int num_samples = kernel_data.integrator.subsurface_samples;
156                 float num_samples_inv = 1.0f/num_samples;
157                 RNG bssrdf_rng = cmj_hash(*rng, i);
158
159                 /* do subsurface scatter step with copy of shader data, this will
160                  * replace the BSSRDF with a diffuse BSDF closure */
161                 for(int j = 0; j < num_samples; j++) {
162                         SubsurfaceIntersection ss_isect;
163                         float bssrdf_u, bssrdf_v;
164                         path_branched_rng_2D(kg, &bssrdf_rng, state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
165                         int num_hits = subsurface_scatter_multi_intersect(kg,
166                                                                           &ss_isect,
167                                                                           sd,
168                                                                           sc,
169                                                                           &lcg_state,
170                                                                           bssrdf_u, bssrdf_v,
171                                                                           true);
172 #ifdef __VOLUME__
173                         Ray volume_ray = *ray;
174                         bool need_update_volume_stack =
175                                 kernel_data.integrator.use_volumes &&
176                                 sd->object_flag & SD_OBJECT_INTERSECTS_VOLUME;
177 #endif  /* __VOLUME__ */
178
179                         /* compute lighting with the BSDF closure */
180                         for(int hit = 0; hit < num_hits; hit++) {
181                                 ShaderData bssrdf_sd = *sd;
182                                 subsurface_scatter_multi_setup(kg,
183                                                                &ss_isect,
184                                                                hit,
185                                                                &bssrdf_sd,
186                                                                state,
187                                                                state->flag,
188                                                                sc,
189                                                                true);
190
191                                 PathState hit_state = *state;
192
193                                 path_state_branch(&hit_state, j, num_samples);
194
195 #ifdef __VOLUME__
196                                 if(need_update_volume_stack) {
197                                         /* Setup ray from previous surface point to the new one. */
198                                         float3 P = ray_offset(bssrdf_sd.P, -bssrdf_sd.Ng);
199                                         volume_ray.D = normalize_len(P - volume_ray.P,
200                                                                      &volume_ray.t);
201
202                                         kernel_volume_stack_update_for_subsurface(
203                                             kg,
204                                             emission_sd,
205                                             &volume_ray,
206                                             hit_state.volume_stack);
207                                 }
208 #endif  /* __VOLUME__ */
209
210 #ifdef __EMISSION__
211                                 /* direct light */
212                                 if(kernel_data.integrator.use_direct_light) {
213                                         int all = (kernel_data.integrator.sample_all_lights_direct) ||
214                                                   (state->flag & PATH_RAY_SHADOW_CATCHER);
215                                         kernel_branched_path_surface_connect_light(
216                                                 kg,
217                                                 rng,
218                                                 &bssrdf_sd,
219                                                 emission_sd,
220                                                 &hit_state,
221                                                 throughput,
222                                                 num_samples_inv,
223                                                 L,
224                                                 all);
225                                 }
226 #endif  /* __EMISSION__ */
227
228                                 /* indirect light */
229                                 kernel_branched_path_surface_indirect_light(
230                                         kg,
231                                         rng,
232                                         &bssrdf_sd,
233                                         indirect_sd,
234                                         emission_sd,
235                                         throughput,
236                                         num_samples_inv,
237                                         &hit_state,
238                                         L);
239                         }
240                 }
241         }
242 }
243 #endif  /* __SUBSURFACE__ */
244
245 ccl_device float4 kernel_branched_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
246 {
247         /* initialize */
248         PathRadiance L;
249         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
250         float L_transparent = 0.0f;
251
252         path_radiance_init(&L, kernel_data.film.use_light_pass);
253
254         /* shader data memory used for both volumes and surfaces, saves stack space */
255         ShaderData sd;
256         /* shader data used by emission, shadows, volume stacks, indirect path */
257         ShaderData emission_sd, indirect_sd;
258
259         PathState state;
260         path_state_init(kg, &emission_sd, &state, rng, sample, &ray);
261
262 #ifdef __KERNEL_DEBUG__
263         DebugData debug_data;
264         debug_data_init(&debug_data);
265 #endif  /* __KERNEL_DEBUG__ */
266
267         /* Main Loop
268          * Here we only handle transparency intersections from the camera ray.
269          * Indirect bounces are handled in kernel_branched_path_surface_indirect_light().
270          */
271         for(;;) {
272                 /* intersect scene */
273                 Intersection isect;
274                 uint visibility = path_state_ray_visibility(kg, &state);
275
276 #ifdef __HAIR__
277                 float difl = 0.0f, extmax = 0.0f;
278                 uint lcg_state = 0;
279
280                 if(kernel_data.bvh.have_curves) {
281                         if(kernel_data.cam.resolution == 1) {
282                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
283                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
284                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
285                         }
286
287                         extmax = kernel_data.curve.maximum_width;
288                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
289                 }
290
291                 bool hit = scene_intersect(kg, ray, visibility, &isect, &lcg_state, difl, extmax);
292 #else
293                 bool hit = scene_intersect(kg, ray, visibility, &isect, NULL, 0.0f, 0.0f);
294 #endif  /* __HAIR__ */
295
296 #ifdef __KERNEL_DEBUG__
297                 debug_data.num_bvh_traversed_nodes += isect.num_traversed_nodes;
298                 debug_data.num_bvh_traversed_instances += isect.num_traversed_instances;
299                 debug_data.num_bvh_intersections += isect.num_intersections;
300                 debug_data.num_ray_bounces++;
301 #endif  /* __KERNEL_DEBUG__ */
302
303 #ifdef __VOLUME__
304                 /* Sanitize volume stack. */
305                 if(!hit) {
306                         kernel_volume_clean_stack(kg, state.volume_stack);
307                 }
308                 /* volume attenuation, emission, scatter */
309                 if(state.volume_stack[0].shader != SHADER_NONE) {
310                         Ray volume_ray = ray;
311                         volume_ray.t = (hit)? isect.t: FLT_MAX;
312                         
313                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
314
315 #ifdef __VOLUME_DECOUPLED__
316                         /* decoupled ray marching only supported on CPU */
317
318                         /* cache steps along volume for repeated sampling */
319                         VolumeSegment volume_segment;
320
321                         shader_setup_from_volume(kg, &sd, &volume_ray);
322                         kernel_volume_decoupled_record(kg, &state,
323                                 &volume_ray, &sd, &volume_segment, heterogeneous);
324
325                         /* direct light sampling */
326                         if(volume_segment.closure_flag & SD_SCATTER) {
327                                 volume_segment.sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
328
329                                 int all = kernel_data.integrator.sample_all_lights_direct;
330
331                                 kernel_branched_path_volume_connect_light(kg, rng, &sd,
332                                         &emission_sd, throughput, &state, &L, all,
333                                         &volume_ray, &volume_segment);
334
335                                 /* indirect light sampling */
336                                 int num_samples = kernel_data.integrator.volume_samples;
337                                 float num_samples_inv = 1.0f/num_samples;
338
339                                 for(int j = 0; j < num_samples; j++) {
340                                         /* workaround to fix correlation bug in T38710, can find better solution
341                                          * in random number generator later, for now this is done here to not impact
342                                          * performance of rendering without volumes */
343                                         RNG tmp_rng = cmj_hash(*rng, state.rng_offset);
344
345                                         PathState ps = state;
346                                         Ray pray = ray;
347                                         float3 tp = throughput;
348
349                                         /* branch RNG state */
350                                         path_state_branch(&ps, j, num_samples);
351
352                                         /* scatter sample. if we use distance sampling and take just one
353                                          * sample for direct and indirect light, we could share this
354                                          * computation, but makes code a bit complex */
355                                         float rphase = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_PHASE);
356                                         float rscatter = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_SCATTER_DISTANCE);
357
358                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
359                                                 &ps, &pray, &sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
360
361                                         (void)result;
362                                         kernel_assert(result == VOLUME_PATH_SCATTERED);
363
364                                         if(kernel_path_volume_bounce(kg,
365                                                                      rng,
366                                                                      &sd,
367                                                                      &tp,
368                                                                      &ps,
369                                                                      &L,
370                                                                      &pray))
371                                         {
372                                                 kernel_path_indirect(kg,
373                                                                      &indirect_sd,
374                                                                      &emission_sd,
375                                                                      rng,
376                                                                      &pray,
377                                                                      tp*num_samples_inv,
378                                                                      num_samples,
379                                                                      &ps,
380                                                                      &L);
381
382                                                 /* for render passes, sum and reset indirect light pass variables
383                                                  * for the next samples */
384                                                 path_radiance_sum_indirect(&L);
385                                                 path_radiance_reset_indirect(&L);
386                                         }
387                                 }
388                         }
389
390                         /* emission and transmittance */
391                         if(volume_segment.closure_flag & SD_EMISSION)
392                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
393                         throughput *= volume_segment.accum_transmittance;
394
395                         /* free cached steps */
396                         kernel_volume_decoupled_free(kg, &volume_segment);
397 #else
398                         /* GPU: no decoupled ray marching, scatter probalistically */
399                         int num_samples = kernel_data.integrator.volume_samples;
400                         float num_samples_inv = 1.0f/num_samples;
401
402                         /* todo: we should cache the shader evaluations from stepping
403                          * through the volume, for now we redo them multiple times */
404
405                         for(int j = 0; j < num_samples; j++) {
406                                 PathState ps = state;
407                                 Ray pray = ray;
408                                 float3 tp = throughput * num_samples_inv;
409
410                                 /* branch RNG state */
411                                 path_state_branch(&ps, j, num_samples);
412
413                                 VolumeIntegrateResult result = kernel_volume_integrate(
414                                         kg, &ps, &sd, &volume_ray, &L, &tp, rng, heterogeneous);
415
416 #ifdef __VOLUME_SCATTER__
417                                 if(result == VOLUME_PATH_SCATTERED) {
418                                         /* todo: support equiangular, MIS and all light sampling.
419                                          * alternatively get decoupled ray marching working on the GPU */
420                                         kernel_path_volume_connect_light(kg, rng, &sd, &emission_sd, tp, &state, &L);
421
422                                         if(kernel_path_volume_bounce(kg,
423                                                                      rng,
424                                                                      &sd,
425                                                                      &tp,
426                                                                      &ps,
427                                                                      &L,
428                                                                      &pray))
429                                         {
430                                                 kernel_path_indirect(kg,
431                                                                      &indirect_sd,
432                                                                      &emission_sd,
433                                                                      rng,
434                                                                      &pray,
435                                                                      tp,
436                                                                      num_samples,
437                                                                      &ps,
438                                                                      &L);
439
440                                                 /* for render passes, sum and reset indirect light pass variables
441                                                  * for the next samples */
442                                                 path_radiance_sum_indirect(&L);
443                                                 path_radiance_reset_indirect(&L);
444                                         }
445                                 }
446 #endif  /* __VOLUME_SCATTER__ */
447                         }
448
449                         /* todo: avoid this calculation using decoupled ray marching */
450                         kernel_volume_shadow(kg, &emission_sd, &state, &volume_ray, &throughput);
451 #endif  /* __VOLUME_DECOUPLED__ */
452                 }
453 #endif  /* __VOLUME__ */
454
455                 if(!hit) {
456                         /* eval background shader if nothing hit */
457                         if(kernel_data.background.transparent) {
458                                 L_transparent += average(throughput);
459
460 #ifdef __PASSES__
461                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
462 #endif  /* __PASSES__ */
463                                         break;
464                         }
465
466 #ifdef __BACKGROUND__
467                         /* sample background shader */
468                         float3 L_background = indirect_background(kg, &emission_sd, &state, &ray);
469                         path_radiance_accum_background(&L, &state, throughput, L_background);
470 #endif  /* __BACKGROUND__ */
471
472                         break;
473                 }
474
475                 /* setup shading */
476                 shader_setup_from_ray(kg, &sd, &isect, &ray);
477                 shader_eval_surface(kg, &sd, rng, &state, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
478                 shader_merge_closures(&sd);
479
480 #ifdef __SHADOW_TRICKS__
481                 if((sd.object_flag & SD_OBJECT_SHADOW_CATCHER)) {
482                         if(state.flag & PATH_RAY_CAMERA) {
483                                 state.flag |= (PATH_RAY_SHADOW_CATCHER | PATH_RAY_SHADOW_CATCHER_ONLY);
484                                 state.catcher_object = sd.object;
485                                 if(!kernel_data.background.transparent) {
486                                         L.shadow_color = indirect_background(kg, &emission_sd, &state, &ray);
487                                 }
488                         }
489                 }
490                 else {
491                         state.flag &= ~PATH_RAY_SHADOW_CATCHER_ONLY;
492                 }
493 #endif  /* __SHADOW_TRICKS__ */
494
495                 /* holdout */
496 #ifdef __HOLDOUT__
497                 if((sd.flag & SD_HOLDOUT) || (sd.object_flag & SD_OBJECT_HOLDOUT_MASK)) {
498                         if(kernel_data.background.transparent) {
499                                 float3 holdout_weight;
500                                 if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
501                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
502                                 }
503                                 else {
504                                         holdout_weight = shader_holdout_eval(kg, &sd);
505                                 }
506                                 /* any throughput is ok, should all be identical here */
507                                 L_transparent += average(holdout_weight*throughput);
508                         }
509                         if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
510                                 break;
511                         }
512                 }
513 #endif  /* __HOLDOUT__ */
514
515                 /* holdout mask objects do not write data passes */
516                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
517
518 #ifdef __EMISSION__
519                 /* emission */
520                 if(sd.flag & SD_EMISSION) {
521                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
522                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
523                 }
524 #endif  /* __EMISSION__ */
525
526                 /* transparency termination */
527                 if(state.flag & PATH_RAY_TRANSPARENT) {
528                         /* path termination. this is a strange place to put the termination, it's
529                          * mainly due to the mixed in MIS that we use. gives too many unneeded
530                          * shader evaluations, only need emission if we are going to terminate */
531                         float probability = path_state_terminate_probability(kg, &state, throughput);
532
533                         if(probability == 0.0f) {
534                                 break;
535                         }
536                         else if(probability != 1.0f) {
537                                 float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
538
539                                 if(terminate >= probability)
540                                         break;
541
542                                 throughput /= probability;
543                         }
544                 }
545
546 #ifdef __AO__
547                 /* ambient occlusion */
548                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
549                         kernel_branched_path_ao(kg, &sd, &emission_sd, &L, &state, rng, throughput);
550                 }
551 #endif  /* __AO__ */
552
553 #ifdef __SUBSURFACE__
554                 /* bssrdf scatter to a different location on the same object */
555                 if(sd.flag & SD_BSSRDF) {
556                         kernel_branched_path_subsurface_scatter(kg, &sd, &indirect_sd, &emission_sd,
557                                                                 &L, &state, rng, &ray, throughput);
558                 }
559 #endif  /* __SUBSURFACE__ */
560
561                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
562                         PathState hit_state = state;
563
564 #ifdef __EMISSION__
565                         /* direct light */
566                         if(kernel_data.integrator.use_direct_light) {
567                                 int all = (kernel_data.integrator.sample_all_lights_direct) ||
568                                           (state.flag & PATH_RAY_SHADOW_CATCHER);
569                                 kernel_branched_path_surface_connect_light(kg, rng,
570                                         &sd, &emission_sd, &hit_state, throughput, 1.0f, &L, all);
571                         }
572 #endif  /* __EMISSION__ */
573
574                         /* indirect light */
575                         kernel_branched_path_surface_indirect_light(kg, rng,
576                                 &sd, &indirect_sd, &emission_sd, throughput, 1.0f, &hit_state, &L);
577
578                         /* continue in case of transparency */
579                         throughput *= shader_bsdf_transparency(kg, &sd);
580
581                         if(is_zero(throughput))
582                                 break;
583                 }
584
585                 /* Update Path State */
586                 state.flag |= PATH_RAY_TRANSPARENT;
587                 state.transparent_bounce++;
588
589                 ray.P = ray_offset(sd.P, -sd.Ng);
590                 ray.t -= sd.ray_length; /* clipping works through transparent */
591
592
593 #ifdef __RAY_DIFFERENTIALS__
594                 ray.dP = sd.dP;
595                 ray.dD.dx = -sd.dI.dx;
596                 ray.dD.dy = -sd.dI.dy;
597 #endif  /* __RAY_DIFFERENTIALS__ */
598
599 #ifdef __VOLUME__
600                 /* enter/exit volume */
601                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
602 #endif  /* __VOLUME__ */
603         }
604
605         float3 L_sum;
606 #ifdef __SHADOW_TRICKS__
607         if(state.flag & PATH_RAY_SHADOW_CATCHER) {
608                 L_sum = path_radiance_sum_shadowcatcher(kg, &L, &L_transparent);
609         }
610         else
611 #endif  /* __SHADOW_TRICKS__ */
612         {
613                 L_sum = path_radiance_clamp_and_sum(kg, &L);
614         }
615
616         kernel_write_light_passes(kg, buffer, &L, sample);
617
618 #ifdef __KERNEL_DEBUG__
619         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
620 #endif  /* __KERNEL_DEBUG__ */
621
622         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
623 }
624
625 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
626         ccl_global float *buffer, ccl_global uint *rng_state,
627         int sample, int x, int y, int offset, int stride)
628 {
629         /* buffer offset */
630         int index = offset + x + y*stride;
631         int pass_stride = kernel_data.film.pass_stride;
632
633         rng_state += index;
634         buffer += index*pass_stride;
635
636         /* initialize random numbers and ray */
637         RNG rng;
638         Ray ray;
639
640         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
641
642         /* integrate */
643         float4 L;
644
645         if(ray.t != 0.0f)
646                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
647         else
648                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
649
650         /* accumulate result in output buffer */
651         kernel_write_pass_float4(buffer, sample, L);
652
653         path_rng_end(kg, rng_state, rng);
654 }
655
656 #endif  /* __BRANCHED_PATH__ */
657
658 CCL_NAMESPACE_END
659