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