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