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