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