Merge branch 'master' into blender2.8
[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->rng_offset, state->sample, 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.rng_offset, state.sample, 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                                         PathState ps = state;
341                                         Ray pray = ray;
342                                         float3 tp = throughput;
343
344                                         /* branch RNG state */
345                                         path_state_branch(&ps, j, num_samples);
346
347                                         /* scatter sample. if we use distance sampling and take just one
348                                          * sample for direct and indirect light, we could share this
349                                          * computation, but makes code a bit complex */
350                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &ps, PRNG_PHASE);
351                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &ps, PRNG_SCATTER_DISTANCE);
352
353                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
354                                                 &ps, &pray, &sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
355
356                                         (void)result;
357                                         kernel_assert(result == VOLUME_PATH_SCATTERED);
358
359                                         if(kernel_path_volume_bounce(kg,
360                                                                      rng,
361                                                                      &sd,
362                                                                      &tp,
363                                                                      &ps,
364                                                                      &L,
365                                                                      &pray))
366                                         {
367                                                 kernel_path_indirect(kg,
368                                                                      &indirect_sd,
369                                                                      &emission_sd,
370                                                                      rng,
371                                                                      &pray,
372                                                                      tp*num_samples_inv,
373                                                                      num_samples,
374                                                                      &ps,
375                                                                      &L);
376
377                                                 /* for render passes, sum and reset indirect light pass variables
378                                                  * for the next samples */
379                                                 path_radiance_sum_indirect(&L);
380                                                 path_radiance_reset_indirect(&L);
381                                         }
382                                 }
383                         }
384
385                         /* emission and transmittance */
386                         if(volume_segment.closure_flag & SD_EMISSION)
387                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
388                         throughput *= volume_segment.accum_transmittance;
389
390                         /* free cached steps */
391                         kernel_volume_decoupled_free(kg, &volume_segment);
392 #else
393                         /* GPU: no decoupled ray marching, scatter probalistically */
394                         int num_samples = kernel_data.integrator.volume_samples;
395                         float num_samples_inv = 1.0f/num_samples;
396
397                         /* todo: we should cache the shader evaluations from stepping
398                          * through the volume, for now we redo them multiple times */
399
400                         for(int j = 0; j < num_samples; j++) {
401                                 PathState ps = state;
402                                 Ray pray = ray;
403                                 float3 tp = throughput * num_samples_inv;
404
405                                 /* branch RNG state */
406                                 path_state_branch(&ps, j, num_samples);
407
408                                 VolumeIntegrateResult result = kernel_volume_integrate(
409                                         kg, &ps, &sd, &volume_ray, &L, &tp, rng, heterogeneous);
410
411 #ifdef __VOLUME_SCATTER__
412                                 if(result == VOLUME_PATH_SCATTERED) {
413                                         /* todo: support equiangular, MIS and all light sampling.
414                                          * alternatively get decoupled ray marching working on the GPU */
415                                         kernel_path_volume_connect_light(kg, rng, &sd, &emission_sd, tp, &state, &L);
416
417                                         if(kernel_path_volume_bounce(kg,
418                                                                      rng,
419                                                                      &sd,
420                                                                      &tp,
421                                                                      &ps,
422                                                                      &L,
423                                                                      &pray))
424                                         {
425                                                 kernel_path_indirect(kg,
426                                                                      &indirect_sd,
427                                                                      &emission_sd,
428                                                                      rng,
429                                                                      &pray,
430                                                                      tp,
431                                                                      num_samples,
432                                                                      &ps,
433                                                                      &L);
434
435                                                 /* for render passes, sum and reset indirect light pass variables
436                                                  * for the next samples */
437                                                 path_radiance_sum_indirect(&L);
438                                                 path_radiance_reset_indirect(&L);
439                                         }
440                                 }
441 #endif  /* __VOLUME_SCATTER__ */
442                         }
443
444                         /* todo: avoid this calculation using decoupled ray marching */
445                         kernel_volume_shadow(kg, &emission_sd, &state, &volume_ray, &throughput);
446 #endif  /* __VOLUME_DECOUPLED__ */
447                 }
448 #endif  /* __VOLUME__ */
449
450                 if(!hit) {
451                         /* eval background shader if nothing hit */
452                         if(kernel_data.background.transparent) {
453                                 L_transparent += average(throughput);
454
455 #ifdef __PASSES__
456                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
457 #endif  /* __PASSES__ */
458                                         break;
459                         }
460
461 #ifdef __BACKGROUND__
462                         /* sample background shader */
463                         float3 L_background = indirect_background(kg, &emission_sd, &state, &ray);
464                         path_radiance_accum_background(&L, &state, throughput, L_background);
465 #endif  /* __BACKGROUND__ */
466
467                         break;
468                 }
469
470                 /* setup shading */
471                 shader_setup_from_ray(kg, &sd, &isect, &ray);
472                 shader_eval_surface(kg, &sd, rng, &state, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
473                 shader_merge_closures(&sd);
474
475 #ifdef __SHADOW_TRICKS__
476                 if((sd.object_flag & SD_OBJECT_SHADOW_CATCHER)) {
477                         if(state.flag & PATH_RAY_CAMERA) {
478                                 state.flag |= (PATH_RAY_SHADOW_CATCHER | PATH_RAY_SHADOW_CATCHER_ONLY);
479                                 state.catcher_object = sd.object;
480                                 if(!kernel_data.background.transparent) {
481                                         L.shadow_color = indirect_background(kg, &emission_sd, &state, &ray);
482                                 }
483                         }
484                 }
485                 else {
486                         state.flag &= ~PATH_RAY_SHADOW_CATCHER_ONLY;
487                 }
488 #endif  /* __SHADOW_TRICKS__ */
489
490                 /* holdout */
491 #ifdef __HOLDOUT__
492                 if((sd.flag & SD_HOLDOUT) || (sd.object_flag & SD_OBJECT_HOLDOUT_MASK)) {
493                         if(kernel_data.background.transparent) {
494                                 float3 holdout_weight;
495                                 if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
496                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
497                                 }
498                                 else {
499                                         holdout_weight = shader_holdout_eval(kg, &sd);
500                                 }
501                                 /* any throughput is ok, should all be identical here */
502                                 L_transparent += average(holdout_weight*throughput);
503                         }
504                         if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
505                                 break;
506                         }
507                 }
508 #endif  /* __HOLDOUT__ */
509
510                 /* holdout mask objects do not write data passes */
511                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
512
513 #ifdef __EMISSION__
514                 /* emission */
515                 if(sd.flag & SD_EMISSION) {
516                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
517                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
518                 }
519 #endif  /* __EMISSION__ */
520
521                 /* transparency termination */
522                 if(state.flag & PATH_RAY_TRANSPARENT) {
523                         /* path termination. this is a strange place to put the termination, it's
524                          * mainly due to the mixed in MIS that we use. gives too many unneeded
525                          * shader evaluations, only need emission if we are going to terminate */
526                         float probability = path_state_terminate_probability(kg, &state, throughput);
527
528                         if(probability == 0.0f) {
529                                 break;
530                         }
531                         else if(probability != 1.0f) {
532                                 float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
533
534                                 if(terminate >= probability)
535                                         break;
536
537                                 throughput /= probability;
538                         }
539                 }
540
541 #ifdef __AO__
542                 /* ambient occlusion */
543                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
544                         kernel_branched_path_ao(kg, &sd, &emission_sd, &L, &state, rng, throughput);
545                 }
546 #endif  /* __AO__ */
547
548 #ifdef __SUBSURFACE__
549                 /* bssrdf scatter to a different location on the same object */
550                 if(sd.flag & SD_BSSRDF) {
551                         kernel_branched_path_subsurface_scatter(kg, &sd, &indirect_sd, &emission_sd,
552                                                                 &L, &state, rng, &ray, throughput);
553                 }
554 #endif  /* __SUBSURFACE__ */
555
556                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
557                         PathState hit_state = state;
558
559 #ifdef __EMISSION__
560                         /* direct light */
561                         if(kernel_data.integrator.use_direct_light) {
562                                 int all = (kernel_data.integrator.sample_all_lights_direct) ||
563                                           (state.flag & PATH_RAY_SHADOW_CATCHER);
564                                 kernel_branched_path_surface_connect_light(kg, rng,
565                                         &sd, &emission_sd, &hit_state, throughput, 1.0f, &L, all);
566                         }
567 #endif  /* __EMISSION__ */
568
569                         /* indirect light */
570                         kernel_branched_path_surface_indirect_light(kg, rng,
571                                 &sd, &indirect_sd, &emission_sd, throughput, 1.0f, &hit_state, &L);
572
573                         /* continue in case of transparency */
574                         throughput *= shader_bsdf_transparency(kg, &sd);
575
576                         if(is_zero(throughput))
577                                 break;
578                 }
579
580                 /* Update Path State */
581                 state.flag |= PATH_RAY_TRANSPARENT;
582                 state.transparent_bounce++;
583
584                 ray.P = ray_offset(sd.P, -sd.Ng);
585                 ray.t -= sd.ray_length; /* clipping works through transparent */
586
587
588 #ifdef __RAY_DIFFERENTIALS__
589                 ray.dP = sd.dP;
590                 ray.dD.dx = -sd.dI.dx;
591                 ray.dD.dy = -sd.dI.dy;
592 #endif  /* __RAY_DIFFERENTIALS__ */
593
594 #ifdef __VOLUME__
595                 /* enter/exit volume */
596                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
597 #endif  /* __VOLUME__ */
598         }
599
600         float3 L_sum;
601 #ifdef __SHADOW_TRICKS__
602         if(state.flag & PATH_RAY_SHADOW_CATCHER) {
603                 L_sum = path_radiance_sum_shadowcatcher(kg, &L, &L_transparent);
604         }
605         else
606 #endif  /* __SHADOW_TRICKS__ */
607         {
608                 L_sum = path_radiance_clamp_and_sum(kg, &L);
609         }
610
611         kernel_write_light_passes(kg, buffer, &L, sample);
612
613 #ifdef __KERNEL_DEBUG__
614         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
615 #endif  /* __KERNEL_DEBUG__ */
616
617         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
618 }
619
620 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
621         ccl_global float *buffer, ccl_global uint *rng_state,
622         int sample, int x, int y, int offset, int stride)
623 {
624         /* buffer offset */
625         int index = offset + x + y*stride;
626         int pass_stride = kernel_data.film.pass_stride;
627
628         rng_state += index;
629         buffer += index*pass_stride;
630
631         /* initialize random numbers and ray */
632         RNG rng;
633         Ray ray;
634
635         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
636
637         /* integrate */
638         float4 L;
639
640         if(ray.t != 0.0f)
641                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
642         else
643                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
644
645         /* accumulate result in output buffer */
646         kernel_write_pass_float4(buffer, sample, L);
647
648         path_rng_end(kg, rng_state, rng);
649 }
650
651 #endif  /* __BRANCHED_PATH__ */
652
653 CCL_NAMESPACE_END
654