Code cleanup: remove shader context.
[blender-staging.git] / intern / cycles / kernel / kernel_path.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 #ifdef __OSL__
18 #  include "kernel/osl/osl_shader.h"
19 #endif
20
21 #include "kernel/kernel_random.h"
22 #include "kernel/kernel_projection.h"
23 #include "kernel/kernel_montecarlo.h"
24 #include "kernel/kernel_differential.h"
25 #include "kernel/kernel_camera.h"
26
27 #include "kernel/geom/geom.h"
28 #include "kernel/bvh/bvh.h"
29
30 #include "kernel/kernel_accumulate.h"
31 #include "kernel/kernel_shader.h"
32 #include "kernel/kernel_light.h"
33 #include "kernel/kernel_passes.h"
34
35 #ifdef __SUBSURFACE__
36 #  include "kernel/kernel_subsurface.h"
37 #endif
38
39 #ifdef __VOLUME__
40 #  include "kernel/kernel_volume.h"
41 #endif
42
43 #include "kernel/kernel_path_state.h"
44 #include "kernel/kernel_shadow.h"
45 #include "kernel/kernel_emission.h"
46 #include "kernel/kernel_path_common.h"
47 #include "kernel/kernel_path_surface.h"
48 #include "kernel/kernel_path_volume.h"
49 #include "kernel/kernel_path_subsurface.h"
50
51 CCL_NAMESPACE_BEGIN
52
53 ccl_device_noinline void kernel_path_ao(KernelGlobals *kg,
54                                         ShaderData *sd,
55                                         ShaderData *emission_sd,
56                                         PathRadiance *L,
57                                         ccl_addr_space PathState *state,
58                                         float3 throughput,
59                                         float3 ao_alpha)
60 {
61         /* todo: solve correlation */
62         float bsdf_u, bsdf_v;
63
64         path_state_rng_2D(kg, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
65
66         float ao_factor = kernel_data.background.ao_factor;
67         float3 ao_N;
68         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
69         float3 ao_D;
70         float ao_pdf;
71
72         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
73
74         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
75                 Ray light_ray;
76                 float3 ao_shadow;
77
78                 light_ray.P = ray_offset(sd->P, sd->Ng);
79                 light_ray.D = ao_D;
80                 light_ray.t = kernel_data.background.ao_distance;
81 #ifdef __OBJECT_MOTION__
82                 light_ray.time = sd->time;
83 #endif  /* __OBJECT_MOTION__ */
84                 light_ray.dP = sd->dP;
85                 light_ray.dD = differential3_zero();
86
87                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &ao_shadow)) {
88                         path_radiance_accum_ao(L, state, throughput, ao_alpha, ao_bsdf, ao_shadow);
89                 }
90                 else {
91                         path_radiance_accum_total_ao(L, state, throughput, ao_bsdf);
92                 }
93         }
94 }
95
96 #ifndef __SPLIT_KERNEL__
97
98 #if defined(__BRANCHED_PATH__) || defined(__BAKING__)
99
100 ccl_device void kernel_path_indirect(KernelGlobals *kg,
101                                      ShaderData *sd,
102                                      ShaderData *emission_sd,
103                                      Ray *ray,
104                                      float3 throughput,
105                                      int num_samples,
106                                      PathState *state,
107                                      PathRadiance *L)
108 {
109         /* path iteration */
110         for(;;) {
111                 /* intersect scene */
112                 Intersection isect;
113                 uint visibility = path_state_ray_visibility(kg, state);
114                 if(state->bounce > kernel_data.integrator.ao_bounces) {
115                         visibility = PATH_RAY_SHADOW;
116                         ray->t = kernel_data.background.ao_distance;
117                 }
118                 bool hit = scene_intersect(kg,
119                                            *ray,
120                                            visibility,
121                                            &isect,
122                                            NULL,
123                                            0.0f, 0.0f);
124
125 #ifdef __LAMP_MIS__
126                 if(kernel_data.integrator.use_lamp_mis && !(state->flag & PATH_RAY_CAMERA)) {
127                         /* ray starting from previous non-transparent bounce */
128                         Ray light_ray;
129
130                         light_ray.P = ray->P - state->ray_t*ray->D;
131                         state->ray_t += isect.t;
132                         light_ray.D = ray->D;
133                         light_ray.t = state->ray_t;
134                         light_ray.time = ray->time;
135                         light_ray.dD = ray->dD;
136                         light_ray.dP = ray->dP;
137
138                         /* intersect with lamp */
139                         float3 emission;
140                         if(indirect_lamp_emission(kg, emission_sd, state, &light_ray, &emission)) {
141                                 path_radiance_accum_emission(L,
142                                                              throughput,
143                                                              emission,
144                                                              state->bounce);
145                         }
146                 }
147 #endif  /* __LAMP_MIS__ */
148
149 #ifdef __VOLUME__
150                 /* Sanitize volume stack. */
151                 if(!hit) {
152                         kernel_volume_clean_stack(kg, state->volume_stack);
153                 }
154                 /* volume attenuation, emission, scatter */
155                 if(state->volume_stack[0].shader != SHADER_NONE) {
156                         Ray volume_ray = *ray;
157                         volume_ray.t = (hit)? isect.t: FLT_MAX;
158
159                         bool heterogeneous =
160                                 volume_stack_is_heterogeneous(kg,
161                                                               state->volume_stack);
162
163 #  ifdef __VOLUME_DECOUPLED__
164                         int sampling_method =
165                                 volume_stack_sampling_method(kg,
166                                                              state->volume_stack);
167                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, false, sampling_method);
168
169                         if(decoupled) {
170                                 /* cache steps along volume for repeated sampling */
171                                 VolumeSegment volume_segment;
172
173                                 shader_setup_from_volume(kg,
174                                                          sd,
175                                                          &volume_ray);
176                                 kernel_volume_decoupled_record(kg,
177                                                                state,
178                                                                &volume_ray,
179                                                                sd,
180                                                                &volume_segment,
181                                                                heterogeneous);
182
183                                 volume_segment.sampling_method = sampling_method;
184
185                                 /* emission */
186                                 if(volume_segment.closure_flag & SD_EMISSION) {
187                                         path_radiance_accum_emission(L,
188                                                                      throughput,
189                                                                      volume_segment.accum_emission,
190                                                                      state->bounce);
191                                 }
192
193                                 /* scattering */
194                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
195
196                                 if(volume_segment.closure_flag & SD_SCATTER) {
197                                         int all = kernel_data.integrator.sample_all_lights_indirect;
198
199                                         /* direct light sampling */
200                                         kernel_branched_path_volume_connect_light(kg,
201                                                                                   sd,
202                                                                                   emission_sd,
203                                                                                   throughput,
204                                                                                   state,
205                                                                                   L,
206                                                                                   all,
207                                                                                   &volume_ray,
208                                                                                   &volume_segment);
209
210                                         /* indirect sample. if we use distance sampling and take just
211                                          * one sample for direct and indirect light, we could share
212                                          * this computation, but makes code a bit complex */
213                                         float rphase = path_state_rng_1D_for_decision(kg, state, PRNG_PHASE);
214                                         float rscatter = path_state_rng_1D_for_decision(kg, state, PRNG_SCATTER_DISTANCE);
215
216                                         result = kernel_volume_decoupled_scatter(kg,
217                                                                                  state,
218                                                                                  &volume_ray,
219                                                                                  sd,
220                                                                                  &throughput,
221                                                                                  rphase,
222                                                                                  rscatter,
223                                                                                  &volume_segment,
224                                                                                  NULL,
225                                                                                  true);
226                                 }
227
228                                 /* free cached steps */
229                                 kernel_volume_decoupled_free(kg, &volume_segment);
230
231                                 if(result == VOLUME_PATH_SCATTERED) {
232                                         if(kernel_path_volume_bounce(kg,
233                                                                      sd,
234                                                                      &throughput,
235                                                                      state,
236                                                                      L,
237                                                                      ray))
238                                         {
239                                                 continue;
240                                         }
241                                         else {
242                                                 break;
243                                         }
244                                 }
245                                 else {
246                                         throughput *= volume_segment.accum_transmittance;
247                                 }
248                         }
249                         else
250 #  endif  /* __VOLUME_DECOUPLED__ */
251                         {
252                                 /* integrate along volume segment with distance sampling */
253                                 VolumeIntegrateResult result = kernel_volume_integrate(
254                                         kg, state, sd, &volume_ray, L, &throughput, heterogeneous);
255
256 #  ifdef __VOLUME_SCATTER__
257                                 if(result == VOLUME_PATH_SCATTERED) {
258                                         /* direct lighting */
259                                         kernel_path_volume_connect_light(kg,
260                                                                          sd,
261                                                                          emission_sd,
262                                                                          throughput,
263                                                                          state,
264                                                                          L);
265
266                                         /* indirect light bounce */
267                                         if(kernel_path_volume_bounce(kg,
268                                                                      sd,
269                                                                      &throughput,
270                                                                      state,
271                                                                      L,
272                                                                      ray))
273                                         {
274                                                 continue;
275                                         }
276                                         else {
277                                                 break;
278                                         }
279                                 }
280 #  endif  /* __VOLUME_SCATTER__ */
281                         }
282                 }
283 #endif  /* __VOLUME__ */
284
285                 if(!hit) {
286 #ifdef __BACKGROUND__
287                         /* sample background shader */
288                         float3 L_background = indirect_background(kg, emission_sd, state, ray);
289                         path_radiance_accum_background(L,
290                                                        state,
291                                                        throughput,
292                                                        L_background);
293 #endif  /* __BACKGROUND__ */
294
295                         break;
296                 }
297                 else if(state->bounce > kernel_data.integrator.ao_bounces) {
298                         break;
299                 }
300
301                 /* setup shading */
302                 shader_setup_from_ray(kg,
303                                       sd,
304                                       &isect,
305                                       ray);
306                 float rbsdf = path_state_rng_1D_for_decision(kg, state, PRNG_BSDF);
307                 shader_eval_surface(kg, sd, state, rbsdf, state->flag);
308 #ifdef __BRANCHED_PATH__
309                 shader_merge_closures(sd);
310 #endif  /* __BRANCHED_PATH__ */
311
312 #ifdef __SHADOW_TRICKS__
313                 if(!(sd->object_flag & SD_OBJECT_SHADOW_CATCHER) &&
314                    (state->flag & PATH_RAY_SHADOW_CATCHER))
315                 {
316                         /* Only update transparency after shadow catcher bounce. */
317                         L->shadow_transparency *=
318                                 average(shader_bsdf_transparency(kg, sd));
319                 }
320 #endif  /* __SHADOW_TRICKS__ */
321
322                 /* blurring of bsdf after bounces, for rays that have a small likelihood
323                  * of following this particular path (diffuse, rough glossy) */
324                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
325                         float blur_pdf = kernel_data.integrator.filter_glossy*state->min_ray_pdf;
326
327                         if(blur_pdf < 1.0f) {
328                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
329                                 shader_bsdf_blur(kg, sd, blur_roughness);
330                         }
331                 }
332
333 #ifdef __EMISSION__
334                 /* emission */
335                 if(sd->flag & SD_EMISSION) {
336                         float3 emission = indirect_primitive_emission(kg,
337                                                                       sd,
338                                                                       isect.t,
339                                                                       state->flag,
340                                                                       state->ray_pdf);
341                         path_radiance_accum_emission(L, throughput, emission, state->bounce);
342                 }
343 #endif  /* __EMISSION__ */
344
345                 /* path termination. this is a strange place to put the termination, it's
346                  * mainly due to the mixed in MIS that we use. gives too many unneeded
347                  * shader evaluations, only need emission if we are going to terminate */
348                 float probability =
349                         path_state_continuation_probability(kg,
350                                                          state,
351                                                          throughput*num_samples);
352
353                 if(probability == 0.0f) {
354                         break;
355                 }
356                 else if(probability != 1.0f) {
357                         float terminate = path_state_rng_1D_for_decision(kg, state, PRNG_TERMINATE);
358
359                         if(terminate >= probability)
360                                 break;
361
362                         throughput /= probability;
363                 }
364
365                 kernel_update_denoising_features(kg, sd, state, L);
366
367 #ifdef __AO__
368                 /* ambient occlusion */
369                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
370                         kernel_path_ao(kg, sd, emission_sd, L, state, throughput, make_float3(0.0f, 0.0f, 0.0f));
371                 }
372 #endif  /* __AO__ */
373
374 #ifdef __SUBSURFACE__
375                 /* bssrdf scatter to a different location on the same object, replacing
376                  * the closures with a diffuse BSDF */
377                 if(sd->flag & SD_BSSRDF) {
378                         float bssrdf_probability;
379                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
380
381                         /* modify throughput for picking bssrdf or bsdf */
382                         throughput *= bssrdf_probability;
383
384                         /* do bssrdf scatter step if we picked a bssrdf closure */
385                         if(sc) {
386                                 uint lcg_state = lcg_state_init(state, 0x68bc21eb);
387
388                                 float bssrdf_u, bssrdf_v;
389                                 path_state_rng_2D(kg,
390                                                   state,
391                                                   PRNG_BSDF_U,
392                                                   &bssrdf_u, &bssrdf_v);
393                                 subsurface_scatter_step(kg,
394                                                         sd,
395                                                         state,
396                                                         state->flag,
397                                                         sc,
398                                                         &lcg_state,
399                                                         bssrdf_u, bssrdf_v,
400                                                         false);
401                         }
402                 }
403 #endif  /* __SUBSURFACE__ */
404
405 #if defined(__EMISSION__)
406                 if(kernel_data.integrator.use_direct_light) {
407                         int all = (kernel_data.integrator.sample_all_lights_indirect) ||
408                                   (state->flag & PATH_RAY_SHADOW_CATCHER);
409                         kernel_branched_path_surface_connect_light(kg,
410                                                                    sd,
411                                                                    emission_sd,
412                                                                    state,
413                                                                    throughput,
414                                                                    1.0f,
415                                                                    L,
416                                                                    all);
417                 }
418 #endif  /* defined(__EMISSION__) */
419
420                 if(!kernel_path_surface_bounce(kg, sd, &throughput, state, L, ray))
421                         break;
422         }
423 }
424
425 #endif /* defined(__BRANCHED_PATH__) || defined(__BAKING__) */
426
427 ccl_device_inline void kernel_path_integrate(KernelGlobals *kg,
428                                              uint rng_hash,
429                                              int sample,
430                                              Ray ray,
431                                              ccl_global float *buffer,
432                                              PathRadiance *L,
433                                              bool *is_shadow_catcher)
434 {
435         /* initialize */
436         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
437
438         path_radiance_init(L, kernel_data.film.use_light_pass);
439
440         /* shader data memory used for both volumes and surfaces, saves stack space */
441         ShaderData sd;
442         /* shader data used by emission, shadows, volume stacks */
443         ShaderData emission_sd;
444
445         PathState state;
446         path_state_init(kg, &emission_sd, &state, rng_hash, sample, &ray);
447
448 #ifdef __SUBSURFACE__
449         SubsurfaceIndirectRays ss_indirect;
450         kernel_path_subsurface_init_indirect(&ss_indirect);
451
452         for(;;) {
453 #endif  /* __SUBSURFACE__ */
454
455         /* path iteration */
456         for(;;) {
457                 /* intersect scene */
458                 Intersection isect;
459                 uint visibility = path_state_ray_visibility(kg, &state);
460
461 #ifdef __HAIR__
462                 float difl = 0.0f, extmax = 0.0f;
463                 uint lcg_state = 0;
464
465                 if(kernel_data.bvh.have_curves) {
466                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
467                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
468                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
469                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
470                         }
471
472                         extmax = kernel_data.curve.maximum_width;
473                         lcg_state = lcg_state_init(&state, 0x51633e2d);
474                 }
475
476                 if(state.bounce > kernel_data.integrator.ao_bounces) {
477                         visibility = PATH_RAY_SHADOW;
478                         ray.t = kernel_data.background.ao_distance;
479                 }
480
481                 bool hit = scene_intersect(kg, ray, visibility, &isect, &lcg_state, difl, extmax);
482 #else
483                 bool hit = scene_intersect(kg, ray, visibility, &isect, NULL, 0.0f, 0.0f);
484 #endif  /* __HAIR__ */
485
486 #ifdef __KERNEL_DEBUG__
487                 if(state.flag & PATH_RAY_CAMERA) {
488                         L->debug_data.num_bvh_traversed_nodes += isect.num_traversed_nodes;
489                         L->debug_data.num_bvh_traversed_instances += isect.num_traversed_instances;
490                         L->debug_data.num_bvh_intersections += isect.num_intersections;
491                 }
492                 L->debug_data.num_ray_bounces++;
493 #endif  /* __KERNEL_DEBUG__ */
494
495 #ifdef __LAMP_MIS__
496                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
497                         /* ray starting from previous non-transparent bounce */
498                         Ray light_ray;
499
500                         light_ray.P = ray.P - state.ray_t*ray.D;
501                         state.ray_t += isect.t;
502                         light_ray.D = ray.D;
503                         light_ray.t = state.ray_t;
504                         light_ray.time = ray.time;
505                         light_ray.dD = ray.dD;
506                         light_ray.dP = ray.dP;
507
508                         /* intersect with lamp */
509                         float3 emission;
510
511                         if(indirect_lamp_emission(kg, &emission_sd, &state, &light_ray, &emission))
512                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
513                 }
514 #endif  /* __LAMP_MIS__ */
515
516 #ifdef __VOLUME__
517                 /* Sanitize volume stack. */
518                 if(!hit) {
519                         kernel_volume_clean_stack(kg, state.volume_stack);
520                 }
521                 /* volume attenuation, emission, scatter */
522                 if(state.volume_stack[0].shader != SHADER_NONE) {
523                         Ray volume_ray = ray;
524                         volume_ray.t = (hit)? isect.t: FLT_MAX;
525
526                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
527
528 #  ifdef __VOLUME_DECOUPLED__
529                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
530                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
531
532                         if(decoupled) {
533                                 /* cache steps along volume for repeated sampling */
534                                 VolumeSegment volume_segment;
535
536                                 shader_setup_from_volume(kg, &sd, &volume_ray);
537                                 kernel_volume_decoupled_record(kg, &state,
538                                         &volume_ray, &sd, &volume_segment, heterogeneous);
539
540                                 volume_segment.sampling_method = sampling_method;
541
542                                 /* emission */
543                                 if(volume_segment.closure_flag & SD_EMISSION)
544                                         path_radiance_accum_emission(L, throughput, volume_segment.accum_emission, state.bounce);
545
546                                 /* scattering */
547                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
548
549                                 if(volume_segment.closure_flag & SD_SCATTER) {
550                                         int all = false;
551
552                                         /* direct light sampling */
553                                         kernel_branched_path_volume_connect_light(kg, &sd,
554                                                 &emission_sd, throughput, &state, L, all,
555                                                 &volume_ray, &volume_segment);
556
557                                         /* indirect sample. if we use distance sampling and take just
558                                          * one sample for direct and indirect light, we could share
559                                          * this computation, but makes code a bit complex */
560                                         float rphase = path_state_rng_1D_for_decision(kg, &state, PRNG_PHASE);
561                                         float rscatter = path_state_rng_1D_for_decision(kg, &state, PRNG_SCATTER_DISTANCE);
562
563                                         result = kernel_volume_decoupled_scatter(kg,
564                                                 &state, &volume_ray, &sd, &throughput,
565                                                 rphase, rscatter, &volume_segment, NULL, true);
566                                 }
567
568                                 /* free cached steps */
569                                 kernel_volume_decoupled_free(kg, &volume_segment);
570
571                                 if(result == VOLUME_PATH_SCATTERED) {
572                                         if(kernel_path_volume_bounce(kg, &sd, &throughput, &state, L, &ray))
573                                                 continue;
574                                         else
575                                                 break;
576                                 }
577                                 else {
578                                         throughput *= volume_segment.accum_transmittance;
579                                 }
580                         }
581                         else
582 #  endif  /* __VOLUME_DECOUPLED__ */
583                         {
584                                 /* integrate along volume segment with distance sampling */
585                                 VolumeIntegrateResult result = kernel_volume_integrate(
586                                         kg, &state, &sd, &volume_ray, L, &throughput, heterogeneous);
587
588 #  ifdef __VOLUME_SCATTER__
589                                 if(result == VOLUME_PATH_SCATTERED) {
590                                         /* direct lighting */
591                                         kernel_path_volume_connect_light(kg, &sd, &emission_sd, throughput, &state, L);
592
593                                         /* indirect light bounce */
594                                         if(kernel_path_volume_bounce(kg, &sd, &throughput, &state, L, &ray))
595                                                 continue;
596                                         else
597                                                 break;
598                                 }
599 #  endif  /* __VOLUME_SCATTER__ */
600                         }
601                 }
602 #endif  /* __VOLUME__ */
603
604                 if(!hit) {
605                         /* eval background shader if nothing hit */
606                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
607                                 L->transparent += average(throughput);
608
609 #ifdef __PASSES__
610                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
611 #endif  /* __PASSES__ */
612                                         break;
613                         }
614
615 #ifdef __BACKGROUND__
616                         /* sample background shader */
617                         float3 L_background = indirect_background(kg, &emission_sd, &state, &ray);
618                         path_radiance_accum_background(L, &state, throughput, L_background);
619 #endif  /* __BACKGROUND__ */
620
621                         break;
622                 }
623                 else if(state.bounce > kernel_data.integrator.ao_bounces) {
624                         break;
625                 }
626
627                 /* setup shading */
628                 shader_setup_from_ray(kg, &sd, &isect, &ray);
629                 float rbsdf = path_state_rng_1D_for_decision(kg, &state, PRNG_BSDF);
630                 shader_eval_surface(kg, &sd, &state, rbsdf, state.flag);
631
632 #ifdef __SHADOW_TRICKS__
633                 if((sd.object_flag & SD_OBJECT_SHADOW_CATCHER)) {
634                         if(state.flag & PATH_RAY_CAMERA) {
635                                 state.flag |= (PATH_RAY_SHADOW_CATCHER |
636                                                PATH_RAY_STORE_SHADOW_INFO);
637                                 if(!kernel_data.background.transparent) {
638                                         L->shadow_background_color =
639                                                 indirect_background(kg, &emission_sd, &state, &ray);
640                                 }
641                                 L->shadow_radiance_sum = path_radiance_clamp_and_sum(kg, L);
642                                 L->shadow_throughput = average(throughput);
643                         }
644                 }
645                 else if(state.flag & PATH_RAY_SHADOW_CATCHER) {
646                         /* Only update transparency after shadow catcher bounce. */
647                         L->shadow_transparency *=
648                                 average(shader_bsdf_transparency(kg, &sd));
649                 }
650 #endif  /* __SHADOW_TRICKS__ */
651
652                 /* holdout */
653 #ifdef __HOLDOUT__
654                 if(((sd.flag & SD_HOLDOUT) ||
655                     (sd.object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
656                    (state.flag & PATH_RAY_CAMERA))
657                 {
658                         if(kernel_data.background.transparent) {
659                                 float3 holdout_weight;
660                                 if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
661                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
662                                 }
663                                 else {
664                                         holdout_weight = shader_holdout_eval(kg, &sd);
665                                 }
666                                 /* any throughput is ok, should all be identical here */
667                                 L->transparent += average(holdout_weight*throughput);
668                         }
669
670                         if(sd.object_flag & SD_OBJECT_HOLDOUT_MASK) {
671                                 break;
672                         }
673                 }
674 #endif  /* __HOLDOUT__ */
675
676                 /* holdout mask objects do not write data passes */
677                 kernel_write_data_passes(kg, buffer, L, &sd, sample, &state, throughput);
678
679                 /* blurring of bsdf after bounces, for rays that have a small likelihood
680                  * of following this particular path (diffuse, rough glossy) */
681                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
682                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
683
684                         if(blur_pdf < 1.0f) {
685                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
686                                 shader_bsdf_blur(kg, &sd, blur_roughness);
687                         }
688                 }
689
690 #ifdef __EMISSION__
691                 /* emission */
692                 if(sd.flag & SD_EMISSION) {
693                         /* todo: is isect.t wrong here for transparent surfaces? */
694                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
695                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
696                 }
697 #endif  /* __EMISSION__ */
698
699                 /* path termination. this is a strange place to put the termination, it's
700                  * mainly due to the mixed in MIS that we use. gives too many unneeded
701                  * shader evaluations, only need emission if we are going to terminate */
702                 float probability = path_state_continuation_probability(kg, &state, throughput);
703
704                 if(probability == 0.0f) {
705                         break;
706                 }
707                 else if(probability != 1.0f) {
708                         float terminate = path_state_rng_1D_for_decision(kg, &state, PRNG_TERMINATE);
709                         if(terminate >= probability)
710                                 break;
711
712                         throughput /= probability;
713                 }
714
715                 kernel_update_denoising_features(kg, &sd, &state, L);
716
717 #ifdef __AO__
718                 /* ambient occlusion */
719                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
720                         kernel_path_ao(kg, &sd, &emission_sd, L, &state, throughput, shader_bsdf_alpha(kg, &sd));
721                 }
722 #endif  /* __AO__ */
723
724 #ifdef __SUBSURFACE__
725                 /* bssrdf scatter to a different location on the same object, replacing
726                  * the closures with a diffuse BSDF */
727                 if(sd.flag & SD_BSSRDF) {
728                         if(kernel_path_subsurface_scatter(kg,
729                                                           &sd,
730                                                           &emission_sd,
731                                                           L,
732                                                           &state,
733                                                           &ray,
734                                                           &throughput,
735                                                           &ss_indirect))
736                         {
737                                 break;
738                         }
739                 }
740 #endif  /* __SUBSURFACE__ */
741
742                 /* direct lighting */
743                 kernel_path_surface_connect_light(kg, &sd, &emission_sd, throughput, &state, L);
744
745                 /* compute direct lighting and next bounce */
746                 if(!kernel_path_surface_bounce(kg, &sd, &throughput, &state, L, &ray))
747                         break;
748         }
749
750 #ifdef __SUBSURFACE__
751                 kernel_path_subsurface_accum_indirect(&ss_indirect, L);
752
753                 /* Trace indirect subsurface rays by restarting the loop. this uses less
754                  * stack memory than invoking kernel_path_indirect.
755                  */
756                 if(ss_indirect.num_rays) {
757                         kernel_path_subsurface_setup_indirect(kg,
758                                                               &ss_indirect,
759                                                               &state,
760                                                               &ray,
761                                                               L,
762                                                               &throughput);
763                 }
764                 else {
765                         break;
766                 }
767         }
768 #endif  /* __SUBSURFACE__ */
769
770 #ifdef __SHADOW_TRICKS__
771         *is_shadow_catcher = (state.flag & PATH_RAY_SHADOW_CATCHER) != 0;
772 #endif  /* __SHADOW_TRICKS__ */
773 }
774
775 ccl_device void kernel_path_trace(KernelGlobals *kg,
776         ccl_global float *buffer, ccl_global uint *rng_state,
777         int sample, int x, int y, int offset, int stride)
778 {
779         /* buffer offset */
780         int index = offset + x + y*stride;
781         int pass_stride = kernel_data.film.pass_stride;
782
783         rng_state += index;
784         buffer += index*pass_stride;
785
786         /* initialize random numbers and ray */
787         uint rng_hash;
788         Ray ray;
789
790         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng_hash, &ray);
791
792         /* integrate */
793         PathRadiance L;
794         bool is_shadow_catcher;
795
796         if(ray.t != 0.0f) {
797                 kernel_path_integrate(kg, rng_hash, sample, ray, buffer, &L, &is_shadow_catcher);
798                 kernel_write_result(kg, buffer, sample, &L, is_shadow_catcher);
799         }
800         else {
801                 kernel_write_result(kg, buffer, sample, NULL, false);
802         }
803 }
804
805 #endif  /* __SPLIT_KERNEL__ */
806
807 CCL_NAMESPACE_END
808