Cycles: Optimize of volume stack update when sampling SSS
[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 "osl_shader.h"
19 #endif
20
21 #include "kernel_random.h"
22 #include "kernel_projection.h"
23 #include "kernel_montecarlo.h"
24 #include "kernel_differential.h"
25 #include "kernel_camera.h"
26
27 #include "geom/geom.h"
28
29 #include "kernel_accumulate.h"
30 #include "kernel_shader.h"
31 #include "kernel_light.h"
32 #include "kernel_passes.h"
33
34 #ifdef __SUBSURFACE__
35 #include "kernel_subsurface.h"
36 #endif
37
38 #ifdef __VOLUME__
39 #include "kernel_volume.h"
40 #endif
41
42 #include "kernel_path_state.h"
43 #include "kernel_shadow.h"
44 #include "kernel_emission.h"
45 #include "kernel_path_surface.h"
46 #include "kernel_path_volume.h"
47
48 #ifdef __KERNEL_DEBUG__
49 #include "kernel_debug.h"
50 #endif
51
52 CCL_NAMESPACE_BEGIN
53
54 ccl_device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, Ray ray,
55         float3 throughput, int num_samples, PathState state, PathRadiance *L)
56 {
57         /* path iteration */
58         for(;;) {
59                 /* intersect scene */
60                 Intersection isect;
61                 uint visibility = path_state_ray_visibility(kg, &state);
62                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
63
64 #ifdef __LAMP_MIS__
65                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
66                         /* ray starting from previous non-transparent bounce */
67                         Ray light_ray;
68
69                         light_ray.P = ray.P - state.ray_t*ray.D;
70                         state.ray_t += isect.t;
71                         light_ray.D = ray.D;
72                         light_ray.t = state.ray_t;
73                         light_ray.time = ray.time;
74                         light_ray.dD = ray.dD;
75                         light_ray.dP = ray.dP;
76
77                         /* intersect with lamp */
78                         float3 emission;
79
80                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
81                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
82                 }
83 #endif
84
85 #ifdef __VOLUME__
86                 /* volume attenuation, emission, scatter */
87                 if(state.volume_stack[0].shader != SHADER_NONE) {
88                         Ray volume_ray = ray;
89                         volume_ray.t = (hit)? isect.t: FLT_MAX;
90
91                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
92
93 #ifdef __VOLUME_DECOUPLED__
94                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
95                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, false, sampling_method);
96
97                         if(decoupled) {
98                                 /* cache steps along volume for repeated sampling */
99                                 VolumeSegment volume_segment;
100                                 ShaderData volume_sd;
101
102                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
103                                 kernel_volume_decoupled_record(kg, &state,
104                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
105                                 
106                                 volume_segment.sampling_method = sampling_method;
107
108                                 /* emission */
109                                 if(volume_segment.closure_flag & SD_EMISSION)
110                                         path_radiance_accum_emission(L, throughput, volume_segment.accum_emission, state.bounce);
111
112                                 /* scattering */
113                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
114
115                                 if(volume_segment.closure_flag & SD_SCATTER) {
116                                         bool all = kernel_data.integrator.sample_all_lights_indirect;
117
118                                         /* direct light sampling */
119                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
120                                                 throughput, &state, L, 1.0f, all, &volume_ray, &volume_segment);
121
122                                         /* indirect sample. if we use distance sampling and take just
123                                          * one sample for direct and indirect light, we could share
124                                          * this computation, but makes code a bit complex */
125                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
126                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
127
128                                         result = kernel_volume_decoupled_scatter(kg,
129                                                 &state, &volume_ray, &volume_sd, &throughput,
130                                                 rphase, rscatter, &volume_segment, NULL, true);
131                                 }
132
133                                 if(result != VOLUME_PATH_SCATTERED)
134                                         throughput *= volume_segment.accum_transmittance;
135
136                                 /* free cached steps */
137                                 kernel_volume_decoupled_free(kg, &volume_segment);
138
139                                 if(result == VOLUME_PATH_SCATTERED) {
140                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, L, &ray))
141                                                 continue;
142                                         else
143                                                 break;
144                                 }
145                         }
146                         else
147 #endif
148                         {
149                                 /* integrate along volume segment with distance sampling */
150                                 ShaderData volume_sd;
151                                 VolumeIntegrateResult result = kernel_volume_integrate(
152                                         kg, &state, &volume_sd, &volume_ray, L, &throughput, rng, heterogeneous);
153
154 #ifdef __VOLUME_SCATTER__
155                                 if(result == VOLUME_PATH_SCATTERED) {
156                                         /* direct lighting */
157                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, L);
158
159                                         /* indirect light bounce */
160                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, L, &ray))
161                                                 continue;
162                                         else
163                                                 break;
164                                 }
165 #endif
166                         }
167                 }
168 #endif
169
170                 if(!hit) {
171 #ifdef __BACKGROUND__
172                         /* sample background shader */
173                         float3 L_background = indirect_background(kg, &state, &ray);
174                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
175 #endif
176
177                         break;
178                 }
179
180                 /* setup shading */
181                 ShaderData sd;
182                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
183                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
184                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
185 #ifdef __BRANCHED_PATH__
186                 shader_merge_closures(&sd);
187 #endif
188
189                 /* blurring of bsdf after bounces, for rays that have a small likelihood
190                  * of following this particular path (diffuse, rough glossy) */
191                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
192                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
193
194                         if(blur_pdf < 1.0f) {
195                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
196                                 shader_bsdf_blur(kg, &sd, blur_roughness);
197                         }
198                 }
199
200 #ifdef __EMISSION__
201                 /* emission */
202                 if(sd.flag & SD_EMISSION) {
203                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
204                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
205                 }
206 #endif
207
208                 /* path termination. this is a strange place to put the termination, it's
209                  * mainly due to the mixed in MIS that we use. gives too many unneeded
210                  * shader evaluations, only need emission if we are going to terminate */
211                 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
212
213                 if(probability == 0.0f) {
214                         break;
215                 }
216                 else if(probability != 1.0f) {
217                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
218
219                         if(terminate >= probability)
220                                 break;
221
222                         throughput /= probability;
223                 }
224
225 #ifdef __AO__
226                 /* ambient occlusion */
227                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
228                         float bsdf_u, bsdf_v;
229                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
230
231                         float ao_factor = kernel_data.background.ao_factor;
232                         float3 ao_N;
233                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
234                         float3 ao_D;
235                         float ao_pdf;
236                         float3 ao_alpha = make_float3(0.0f, 0.0f, 0.0f);
237
238                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
239
240                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
241                                 Ray light_ray;
242                                 float3 ao_shadow;
243
244                                 light_ray.P = ray_offset(sd.P, sd.Ng);
245                                 light_ray.D = ao_D;
246                                 light_ray.t = kernel_data.background.ao_distance;
247 #ifdef __OBJECT_MOTION__
248                                 light_ray.time = sd.time;
249 #endif
250                                 light_ray.dP = sd.dP;
251                                 light_ray.dD = differential3_zero();
252
253                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
254                                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
255                         }
256                 }
257 #endif
258
259 #ifdef __SUBSURFACE__
260                 /* bssrdf scatter to a different location on the same object, replacing
261                  * the closures with a diffuse BSDF */
262                 if(sd.flag & SD_BSSRDF) {
263                         float bssrdf_probability;
264                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
265
266                         /* modify throughput for picking bssrdf or bsdf */
267                         throughput *= bssrdf_probability;
268
269                         /* do bssrdf scatter step if we picked a bssrdf closure */
270                         if(sc) {
271                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
272
273                                 float bssrdf_u, bssrdf_v;
274                                 path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
275                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
276
277                                 state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
278                         }
279                 }
280 #endif
281
282 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
283                 if(kernel_data.integrator.use_direct_light) {
284                         bool all = kernel_data.integrator.sample_all_lights_indirect;
285                         kernel_branched_path_surface_connect_light(kg, rng, &sd, &state, throughput, 1.0f, L, all);
286                 }
287 #endif
288
289                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, L, &ray))
290                         break;
291         }
292 }
293
294 ccl_device void kernel_path_ao(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
295 {
296         /* todo: solve correlation */
297         float bsdf_u, bsdf_v;
298
299         path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
300
301         float ao_factor = kernel_data.background.ao_factor;
302         float3 ao_N;
303         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
304         float3 ao_D;
305         float ao_pdf;
306         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
307
308         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
309
310         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
311                 Ray light_ray;
312                 float3 ao_shadow;
313
314                 light_ray.P = ray_offset(sd->P, sd->Ng);
315                 light_ray.D = ao_D;
316                 light_ray.t = kernel_data.background.ao_distance;
317 #ifdef __OBJECT_MOTION__
318                 light_ray.time = sd->time;
319 #endif
320                 light_ray.dP = sd->dP;
321                 light_ray.dD = differential3_zero();
322
323                 if(!shadow_blocked(kg, state, &light_ray, &ao_shadow))
324                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
325         }
326 }
327
328 ccl_device void kernel_branched_path_ao(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
329 {
330         int num_samples = kernel_data.integrator.ao_samples;
331         float num_samples_inv = 1.0f/num_samples;
332         float ao_factor = kernel_data.background.ao_factor;
333         float3 ao_N;
334         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
335         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
336
337         for(int j = 0; j < num_samples; j++) {
338                 float bsdf_u, bsdf_v;
339                 path_branched_rng_2D(kg, rng, state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
340
341                 float3 ao_D;
342                 float ao_pdf;
343
344                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
345
346                 if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
347                         Ray light_ray;
348                         float3 ao_shadow;
349
350                         light_ray.P = ray_offset(sd->P, sd->Ng);
351                         light_ray.D = ao_D;
352                         light_ray.t = kernel_data.background.ao_distance;
353 #ifdef __OBJECT_MOTION__
354                         light_ray.time = sd->time;
355 #endif
356                         light_ray.dP = sd->dP;
357                         light_ray.dD = differential3_zero();
358
359                         if(!shadow_blocked(kg, state, &light_ray, &ao_shadow))
360                                 path_radiance_accum_ao(L, throughput*num_samples_inv, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
361                 }
362         }
363 }
364
365 #ifdef __SUBSURFACE__
366
367 #ifdef __VOLUME__
368 ccl_device void kernel_path_subsurface_update_volume_stack(KernelGlobals *kg,
369                                                            Ray *ray,
370                                                            VolumeStack *stack)
371 {
372         kernel_assert(kernel_data.integrator.use_volumes);
373
374         Ray volume_ray = *ray;
375         Intersection isect;
376         const float3 Pend = volume_ray.P + volume_ray.D*volume_ray.t;
377
378         while(scene_intersect_volume(kg, &volume_ray, &isect))
379         {
380                 ShaderData sd;
381                 shader_setup_from_ray(kg, &sd, &isect, &volume_ray, 0, 0);
382                 kernel_volume_stack_enter_exit(kg, &sd, stack);
383
384                 /* Move ray forward. */
385                 volume_ray.P = ray_offset(sd.P, -sd.Ng);
386                 volume_ray.t -= sd.ray_length;
387         }
388 }
389 #endif
390
391 ccl_device bool kernel_path_subsurface_scatter(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, Ray *ray, float3 *throughput)
392 {
393         float bssrdf_probability;
394         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
395
396         /* modify throughput for picking bssrdf or bsdf */
397         *throughput *= bssrdf_probability;
398
399         /* do bssrdf scatter step if we picked a bssrdf closure */
400         if(sc) {
401                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
402
403                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
404                 float bssrdf_u, bssrdf_v;
405                 path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
406                 int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
407 #ifdef __VOLUME__
408                 Ray volume_ray = *ray;
409                 bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
410                                                 sd->flag & SD_OBJECT_INTERSECTS_VOLUME;
411 #endif
412
413                 /* compute lighting with the BSDF closure */
414                 for(int hit = 0; hit < num_hits; hit++) {
415                         float3 tp = *throughput;
416                         PathState hit_state = *state;
417                         Ray hit_ray = *ray;
418
419                         hit_state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
420                         hit_state.rng_offset += PRNG_BOUNCE_NUM;
421                         
422                         kernel_path_surface_connect_light(kg, rng, &bssrdf_sd[hit], tp, state, L);
423
424                         if(kernel_path_surface_bounce(kg, rng, &bssrdf_sd[hit], &tp, &hit_state, L, &hit_ray)) {
425 #ifdef __LAMP_MIS__
426                                 hit_state.ray_t = 0.0f;
427 #endif
428
429 #ifdef __VOLUME__
430                                 if(need_update_volume_stack) {
431                                         /* Setup ray from previous surface point to the new one. */
432                                         volume_ray.D = normalize_len(hit_ray.P - volume_ray.P,
433                                                                      &volume_ray.t);
434
435                                         kernel_path_subsurface_update_volume_stack(
436                                             kg,
437                                             &volume_ray,
438                                             hit_state.volume_stack);
439
440                                         /* Move volume ray forward. */
441                                         volume_ray.P = hit_ray.P;
442                                 }
443 #endif
444
445                                 kernel_path_indirect(kg, rng, hit_ray, tp, state->num_samples, hit_state, L);
446
447                                 /* for render passes, sum and reset indirect light pass variables
448                                  * for the next samples */
449                                 path_radiance_sum_indirect(L);
450                                 path_radiance_reset_indirect(L);
451                         }
452                 }
453                 return true;
454         }
455         return false;
456 }
457 #endif
458
459 ccl_device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
460 {
461         /* initialize */
462         PathRadiance L;
463         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
464         float L_transparent = 0.0f;
465
466         path_radiance_init(&L, kernel_data.film.use_light_pass);
467
468         PathState state;
469         path_state_init(kg, &state, rng, sample, &ray);
470
471 #ifdef __KERNEL_DEBUG__
472         DebugData debug_data;
473         debug_data_init(&debug_data);
474 #endif
475
476         /* path iteration */
477         for(;;) {
478                 /* intersect scene */
479                 Intersection isect;
480                 uint visibility = path_state_ray_visibility(kg, &state);
481
482 #ifdef __HAIR__
483                 float difl = 0.0f, extmax = 0.0f;
484                 uint lcg_state = 0;
485
486                 if(kernel_data.bvh.have_curves) {
487                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
488                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
489                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
490                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
491                         }
492
493                         extmax = kernel_data.curve.maximum_width;
494                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
495                 }
496
497                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
498 #else
499                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
500 #endif
501
502 #ifdef __KERNEL_DEBUG__
503                 if(state.flag & PATH_RAY_CAMERA) {
504                         debug_data.num_bvh_traversal_steps += isect.num_traversal_steps;
505                 }
506 #endif
507
508 #ifdef __LAMP_MIS__
509                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
510                         /* ray starting from previous non-transparent bounce */
511                         Ray light_ray;
512
513                         light_ray.P = ray.P - state.ray_t*ray.D;
514                         state.ray_t += isect.t;
515                         light_ray.D = ray.D;
516                         light_ray.t = state.ray_t;
517                         light_ray.time = ray.time;
518                         light_ray.dD = ray.dD;
519                         light_ray.dP = ray.dP;
520
521                         /* intersect with lamp */
522                         float3 emission;
523
524                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
525                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
526                 }
527 #endif
528
529 #ifdef __VOLUME__
530                 /* volume attenuation, emission, scatter */
531                 if(state.volume_stack[0].shader != SHADER_NONE) {
532                         Ray volume_ray = ray;
533                         volume_ray.t = (hit)? isect.t: FLT_MAX;
534
535                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
536
537 #ifdef __VOLUME_DECOUPLED__
538                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
539                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
540
541                         if(decoupled) {
542                                 /* cache steps along volume for repeated sampling */
543                                 VolumeSegment volume_segment;
544                                 ShaderData volume_sd;
545
546                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
547                                 kernel_volume_decoupled_record(kg, &state,
548                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
549
550                                 volume_segment.sampling_method = sampling_method;
551
552                                 /* emission */
553                                 if(volume_segment.closure_flag & SD_EMISSION)
554                                         path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
555
556                                 /* scattering */
557                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
558
559                                 if(volume_segment.closure_flag & SD_SCATTER) {
560                                         bool all = false;
561
562                                         /* direct light sampling */
563                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
564                                                 throughput, &state, &L, 1.0f, all, &volume_ray, &volume_segment);
565
566                                         /* indirect sample. if we use distance sampling and take just
567                                          * one sample for direct and indirect light, we could share
568                                          * this computation, but makes code a bit complex */
569                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
570                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
571
572                                         result = kernel_volume_decoupled_scatter(kg,
573                                                 &state, &volume_ray, &volume_sd, &throughput,
574                                                 rphase, rscatter, &volume_segment, NULL, true);
575                                 }
576
577                                 if(result != VOLUME_PATH_SCATTERED)
578                                         throughput *= volume_segment.accum_transmittance;
579
580                                 /* free cached steps */
581                                 kernel_volume_decoupled_free(kg, &volume_segment);
582
583                                 if(result == VOLUME_PATH_SCATTERED) {
584                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
585                                                 continue;
586                                         else
587                                                 break;
588                                 }
589                         }
590                         else 
591 #endif
592                         {
593                                 /* integrate along volume segment with distance sampling */
594                                 ShaderData volume_sd;
595                                 VolumeIntegrateResult result = kernel_volume_integrate(
596                                         kg, &state, &volume_sd, &volume_ray, &L, &throughput, rng, heterogeneous);
597
598 #ifdef __VOLUME_SCATTER__
599                                 if(result == VOLUME_PATH_SCATTERED) {
600                                         /* direct lighting */
601                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, &L);
602
603                                         /* indirect light bounce */
604                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
605                                                 continue;
606                                         else
607                                                 break;
608                                 }
609 #endif
610                         }
611                 }
612 #endif
613
614                 if(!hit) {
615                         /* eval background shader if nothing hit */
616                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
617                                 L_transparent += average(throughput);
618
619 #ifdef __PASSES__
620                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
621 #endif
622                                         break;
623                         }
624
625 #ifdef __BACKGROUND__
626                         /* sample background shader */
627                         float3 L_background = indirect_background(kg, &state, &ray);
628                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
629 #endif
630
631                         break;
632                 }
633
634                 /* setup shading */
635                 ShaderData sd;
636                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
637                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
638                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
639
640                 /* holdout */
641 #ifdef __HOLDOUT__
642                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
643                         if(kernel_data.background.transparent) {
644                                 float3 holdout_weight;
645                                 
646                                 if(sd.flag & SD_HOLDOUT_MASK)
647                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
648                                 else
649                                         holdout_weight = shader_holdout_eval(kg, &sd);
650
651                                 /* any throughput is ok, should all be identical here */
652                                 L_transparent += average(holdout_weight*throughput);
653                         }
654
655                         if(sd.flag & SD_HOLDOUT_MASK)
656                                 break;
657                 }
658 #endif
659
660                 /* holdout mask objects do not write data passes */
661                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
662
663                 /* blurring of bsdf after bounces, for rays that have a small likelihood
664                  * of following this particular path (diffuse, rough glossy) */
665                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
666                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
667
668                         if(blur_pdf < 1.0f) {
669                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
670                                 shader_bsdf_blur(kg, &sd, blur_roughness);
671                         }
672                 }
673
674 #ifdef __EMISSION__
675                 /* emission */
676                 if(sd.flag & SD_EMISSION) {
677                         /* todo: is isect.t wrong here for transparent surfaces? */
678                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
679                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
680                 }
681 #endif
682
683                 /* path termination. this is a strange place to put the termination, it's
684                  * mainly due to the mixed in MIS that we use. gives too many unneeded
685                  * shader evaluations, only need emission if we are going to terminate */
686                 float probability = path_state_terminate_probability(kg, &state, throughput);
687
688                 if(probability == 0.0f) {
689                         break;
690                 }
691                 else if(probability != 1.0f) {
692                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
693
694                         if(terminate >= probability)
695                                 break;
696
697                         throughput /= probability;
698                 }
699
700 #ifdef __AO__
701                 /* ambient occlusion */
702                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
703                         kernel_path_ao(kg, &sd, &L, &state, rng, throughput);
704                 }
705 #endif
706
707 #ifdef __SUBSURFACE__
708                 /* bssrdf scatter to a different location on the same object, replacing
709                  * the closures with a diffuse BSDF */
710                 if(sd.flag & SD_BSSRDF) {
711                         if(kernel_path_subsurface_scatter(kg, &sd, &L, &state, rng, &ray, &throughput))
712                                 break;
713                 }
714 #endif
715
716                 /* direct lighting */
717                 kernel_path_surface_connect_light(kg, rng, &sd, throughput, &state, &L);
718
719                 /* compute direct lighting and next bounce */
720                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
721                         break;
722         }
723
724         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
725
726         kernel_write_light_passes(kg, buffer, &L, sample);
727
728 #ifdef __KERNEL_DEBUG__
729         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
730 #endif
731
732         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
733 }
734
735 #ifdef __BRANCHED_PATH__
736
737 /* branched path tracing: bounce off surface and integrate indirect light */
738 ccl_device_noinline void kernel_branched_path_surface_indirect_light(KernelGlobals *kg,
739         RNG *rng, ShaderData *sd, float3 throughput, float num_samples_adjust,
740         PathState *state, PathRadiance *L)
741 {
742         for(int i = 0; i< sd->num_closure; i++) {
743                 const ShaderClosure *sc = &sd->closure[i];
744
745                 if(!CLOSURE_IS_BSDF(sc->type))
746                         continue;
747                 /* transparency is not handled here, but in outer loop */
748                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
749                         continue;
750
751                 int num_samples;
752
753                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
754                         num_samples = kernel_data.integrator.diffuse_samples;
755                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
756                         num_samples = 1;
757                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
758                         num_samples = kernel_data.integrator.glossy_samples;
759                 else
760                         num_samples = kernel_data.integrator.transmission_samples;
761
762                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
763
764                 float num_samples_inv = num_samples_adjust/num_samples;
765                 RNG bsdf_rng = cmj_hash(*rng, i);
766
767                 for(int j = 0; j < num_samples; j++) {
768                         PathState ps = *state;
769                         float3 tp = throughput;
770                         Ray bsdf_ray;
771
772                         if(!kernel_branched_path_surface_bounce(kg, &bsdf_rng, sd, sc, j, num_samples, &tp, &ps, L, &bsdf_ray))
773                                 continue;
774
775                         kernel_path_indirect(kg, rng, bsdf_ray, tp*num_samples_inv, num_samples, ps, L);
776
777                         /* for render passes, sum and reset indirect light pass variables
778                          * for the next samples */
779                         path_radiance_sum_indirect(L);
780                         path_radiance_reset_indirect(L);
781                 }
782         }
783 }
784
785 #ifdef __SUBSURFACE__
786 ccl_device void kernel_branched_path_subsurface_scatter(KernelGlobals *kg,
787                                                         ShaderData *sd,
788                                                         PathRadiance *L,
789                                                         PathState *state,
790                                                         RNG *rng,
791                                                         Ray *ray,
792                                                         float3 throughput)
793 {
794         for(int i = 0; i< sd->num_closure; i++) {
795                 ShaderClosure *sc = &sd->closure[i];
796
797                 if(!CLOSURE_IS_BSSRDF(sc->type))
798                         continue;
799
800                 /* set up random number generator */
801                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
802                 int num_samples = kernel_data.integrator.subsurface_samples;
803                 float num_samples_inv = 1.0f/num_samples;
804                 RNG bssrdf_rng = cmj_hash(*rng, i);
805
806                 state->flag |= PATH_RAY_BSSRDF_ANCESTOR;
807
808                 /* do subsurface scatter step with copy of shader data, this will
809                  * replace the BSSRDF with a diffuse BSDF closure */
810                 for(int j = 0; j < num_samples; j++) {
811                         ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
812                         float bssrdf_u, bssrdf_v;
813                         path_branched_rng_2D(kg, &bssrdf_rng, state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
814                         int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, true);
815 #ifdef __VOLUME__
816                         Ray volume_ray = *ray;
817                         bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
818                                                         sd->flag & SD_OBJECT_INTERSECTS_VOLUME;
819 #endif
820
821                         /* compute lighting with the BSDF closure */
822                         for(int hit = 0; hit < num_hits; hit++) {
823                                 PathState hit_state = *state;
824
825                                 path_state_branch(&hit_state, j, num_samples);
826
827 #ifdef __VOLUME__
828                                 if(need_update_volume_stack) {
829                                         /* Setup ray from previous surface point to the new one. */
830                                         float3 P = ray_offset(bssrdf_sd[hit].P, -bssrdf_sd[hit].Ng);
831                                         volume_ray.D = normalize_len(P - volume_ray.P,
832                                                                      &volume_ray.t);
833
834                                         kernel_path_subsurface_update_volume_stack(
835                                             kg,
836                                             &volume_ray,
837                                             hit_state.volume_stack);
838
839                                         /* Move volume ray forward. */
840                                         volume_ray.P = P;
841                                 }
842 #endif
843
844 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
845                                 /* direct light */
846                                 if(kernel_data.integrator.use_direct_light) {
847                                         bool all = kernel_data.integrator.sample_all_lights_direct;
848                                         kernel_branched_path_surface_connect_light(kg, rng,
849                                                 &bssrdf_sd[hit], &hit_state, throughput, num_samples_inv, L, all);
850                                 }
851 #endif
852
853                                 /* indirect light */
854                                 kernel_branched_path_surface_indirect_light(kg, rng,
855                                         &bssrdf_sd[hit], throughput, num_samples_inv,
856                                         &hit_state, L);
857                         }
858                 }
859
860                 state->flag &= ~PATH_RAY_BSSRDF_ANCESTOR;
861         }
862 }
863 #endif
864
865 ccl_device float4 kernel_branched_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
866 {
867         /* initialize */
868         PathRadiance L;
869         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
870         float L_transparent = 0.0f;
871
872         path_radiance_init(&L, kernel_data.film.use_light_pass);
873
874         PathState state;
875         path_state_init(kg, &state, rng, sample, &ray);
876
877 #ifdef __KERNEL_DEBUG__
878         DebugData debug_data;
879         debug_data_init(&debug_data);
880 #endif
881
882         for(;;) {
883                 /* intersect scene */
884                 Intersection isect;
885                 uint visibility = path_state_ray_visibility(kg, &state);
886
887 #ifdef __HAIR__
888                 float difl = 0.0f, extmax = 0.0f;
889                 uint lcg_state = 0;
890
891                 if(kernel_data.bvh.have_curves) {
892                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
893                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
894                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
895                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
896                         }
897
898                         extmax = kernel_data.curve.maximum_width;
899                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
900                 }
901
902                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
903 #else
904                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
905 #endif
906
907 #ifdef __KERNEL_DEBUG__
908                 if(state.flag & PATH_RAY_CAMERA) {
909                         debug_data.num_bvh_traversal_steps += isect.num_traversal_steps;
910                 }
911 #endif
912
913 #ifdef __VOLUME__
914                 /* volume attenuation, emission, scatter */
915                 if(state.volume_stack[0].shader != SHADER_NONE) {
916                         Ray volume_ray = ray;
917                         volume_ray.t = (hit)? isect.t: FLT_MAX;
918                         
919                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
920
921 #ifdef __VOLUME_DECOUPLED__
922                         /* decoupled ray marching only supported on CPU */
923
924                         /* cache steps along volume for repeated sampling */
925                         VolumeSegment volume_segment;
926                         ShaderData volume_sd;
927
928                         shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
929                         kernel_volume_decoupled_record(kg, &state,
930                                 &volume_ray, &volume_sd, &volume_segment, heterogeneous);
931
932                         /* direct light sampling */
933                         if(volume_segment.closure_flag & SD_SCATTER) {
934                                 volume_segment.sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
935
936                                 bool all = kernel_data.integrator.sample_all_lights_direct;
937
938                                 kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
939                                         throughput, &state, &L, 1.0f, all, &volume_ray, &volume_segment);
940
941                                 /* indirect light sampling */
942                                 int num_samples = kernel_data.integrator.volume_samples;
943                                 float num_samples_inv = 1.0f/num_samples;
944
945                                 for(int j = 0; j < num_samples; j++) {
946                                         /* workaround to fix correlation bug in T38710, can find better solution
947                                          * in random number generator later, for now this is done here to not impact
948                                          * performance of rendering without volumes */
949                                         RNG tmp_rng = cmj_hash(*rng, state.rng_offset);
950
951                                         PathState ps = state;
952                                         Ray pray = ray;
953                                         float3 tp = throughput;
954
955                                         /* branch RNG state */
956                                         path_state_branch(&ps, j, num_samples);
957
958                                         /* scatter sample. if we use distance sampling and take just one
959                                          * sample for direct and indirect light, we could share this
960                                          * computation, but makes code a bit complex */
961                                         float rphase = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_PHASE);
962                                         float rscatter = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_SCATTER_DISTANCE);
963
964                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
965                                                 &ps, &pray, &volume_sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
966                                                 
967                                         (void)result;
968                                         kernel_assert(result == VOLUME_PATH_SCATTERED);
969
970                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray)) {
971                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
972
973                                                 /* for render passes, sum and reset indirect light pass variables
974                                                  * for the next samples */
975                                                 path_radiance_sum_indirect(&L);
976                                                 path_radiance_reset_indirect(&L);
977                                         }
978                                 }
979                         }
980
981                         /* emission and transmittance */
982                         if(volume_segment.closure_flag & SD_EMISSION)
983                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
984                         throughput *= volume_segment.accum_transmittance;
985
986                         /* free cached steps */
987                         kernel_volume_decoupled_free(kg, &volume_segment);
988 #else
989                         /* GPU: no decoupled ray marching, scatter probalistically */
990                         int num_samples = kernel_data.integrator.volume_samples;
991                         float num_samples_inv = 1.0f/num_samples;
992
993                         /* todo: we should cache the shader evaluations from stepping
994                          * through the volume, for now we redo them multiple times */
995
996                         for(int j = 0; j < num_samples; j++) {
997                                 PathState ps = state;
998                                 Ray pray = ray;
999                                 ShaderData volume_sd;
1000                                 float3 tp = throughput * num_samples_inv;
1001
1002                                 /* branch RNG state */
1003                                 path_state_branch(&ps, j, num_samples);
1004
1005                                 VolumeIntegrateResult result = kernel_volume_integrate(
1006                                         kg, &ps, &volume_sd, &volume_ray, &L, &tp, rng, heterogeneous);
1007                                 
1008 #ifdef __VOLUME_SCATTER__
1009                                 if(result == VOLUME_PATH_SCATTERED) {
1010                                         /* todo: support equiangular, MIS and all light sampling.
1011                                          * alternatively get decoupled ray marching working on the GPU */
1012                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, tp, &state, &L);
1013
1014                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray)) {
1015                                                 kernel_path_indirect(kg, rng, pray, tp, num_samples, ps, &L);
1016
1017                                                 /* for render passes, sum and reset indirect light pass variables
1018                                                  * for the next samples */
1019                                                 path_radiance_sum_indirect(&L);
1020                                                 path_radiance_reset_indirect(&L);
1021                                         }
1022                                 }
1023 #endif
1024                         }
1025
1026                         /* todo: avoid this calculation using decoupled ray marching */
1027                         kernel_volume_shadow(kg, &state, &volume_ray, &throughput);
1028 #endif
1029                 }
1030 #endif
1031
1032                 if(!hit) {
1033                         /* eval background shader if nothing hit */
1034                         if(kernel_data.background.transparent) {
1035                                 L_transparent += average(throughput);
1036
1037 #ifdef __PASSES__
1038                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
1039 #endif
1040                                         break;
1041                         }
1042
1043 #ifdef __BACKGROUND__
1044                         /* sample background shader */
1045                         float3 L_background = indirect_background(kg, &state, &ray);
1046                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
1047 #endif
1048
1049                         break;
1050                 }
1051
1052                 /* setup shading */
1053                 ShaderData sd;
1054                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
1055                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
1056                 shader_merge_closures(&sd);
1057
1058                 /* holdout */
1059 #ifdef __HOLDOUT__
1060                 if(sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) {
1061                         if(kernel_data.background.transparent) {
1062                                 float3 holdout_weight;
1063                                 
1064                                 if(sd.flag & SD_HOLDOUT_MASK)
1065                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
1066                                 else
1067                                         holdout_weight = shader_holdout_eval(kg, &sd);
1068
1069                                 /* any throughput is ok, should all be identical here */
1070                                 L_transparent += average(holdout_weight*throughput);
1071                         }
1072
1073                         if(sd.flag & SD_HOLDOUT_MASK)
1074                                 break;
1075                 }
1076 #endif
1077
1078                 /* holdout mask objects do not write data passes */
1079                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
1080
1081 #ifdef __EMISSION__
1082                 /* emission */
1083                 if(sd.flag & SD_EMISSION) {
1084                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
1085                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
1086                 }
1087 #endif
1088
1089                 /* transparency termination */
1090                 if(state.flag & PATH_RAY_TRANSPARENT) {
1091                         /* path termination. this is a strange place to put the termination, it's
1092                          * mainly due to the mixed in MIS that we use. gives too many unneeded
1093                          * shader evaluations, only need emission if we are going to terminate */
1094                         float probability = path_state_terminate_probability(kg, &state, throughput);
1095
1096                         if(probability == 0.0f) {
1097                                 break;
1098                         }
1099                         else if(probability != 1.0f) {
1100                                 float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
1101
1102                                 if(terminate >= probability)
1103                                         break;
1104
1105                                 throughput /= probability;
1106                         }
1107                 }
1108
1109 #ifdef __AO__
1110                 /* ambient occlusion */
1111                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
1112                         kernel_branched_path_ao(kg, &sd, &L, &state, rng, throughput);
1113                 }
1114 #endif
1115
1116 #ifdef __SUBSURFACE__
1117                 /* bssrdf scatter to a different location on the same object */
1118                 if(sd.flag & SD_BSSRDF) {
1119                         kernel_branched_path_subsurface_scatter(kg, &sd, &L, &state,
1120                                                                 rng, &ray, throughput);
1121                 }
1122 #endif
1123
1124                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
1125                         PathState hit_state = state;
1126
1127 #ifdef __EMISSION__
1128                         /* direct light */
1129                         if(kernel_data.integrator.use_direct_light) {
1130                                 bool all = kernel_data.integrator.sample_all_lights_direct;
1131                                 kernel_branched_path_surface_connect_light(kg, rng,
1132                                         &sd, &hit_state, throughput, 1.0f, &L, all);
1133                         }
1134 #endif
1135
1136                         /* indirect light */
1137                         kernel_branched_path_surface_indirect_light(kg, rng,
1138                                 &sd, throughput, 1.0f, &hit_state, &L);
1139
1140                         /* continue in case of transparency */
1141                         throughput *= shader_bsdf_transparency(kg, &sd);
1142
1143                         if(is_zero(throughput))
1144                                 break;
1145                 }
1146
1147                 path_state_next(kg, &state, LABEL_TRANSPARENT);
1148                 ray.P = ray_offset(sd.P, -sd.Ng);
1149                 ray.t -= sd.ray_length; /* clipping works through transparent */
1150
1151
1152 #ifdef __RAY_DIFFERENTIALS__
1153                 ray.dP = sd.dP;
1154                 ray.dD.dx = -sd.dI.dx;
1155                 ray.dD.dy = -sd.dI.dy;
1156 #endif
1157
1158 #ifdef __VOLUME__
1159                 /* enter/exit volume */
1160                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
1161 #endif
1162         }
1163
1164         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
1165
1166         kernel_write_light_passes(kg, buffer, &L, sample);
1167
1168 #ifdef __KERNEL_DEBUG__
1169         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
1170 #endif
1171
1172         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1173 }
1174
1175 #endif
1176
1177 ccl_device_inline void kernel_path_trace_setup(KernelGlobals *kg, ccl_global uint *rng_state, int sample, int x, int y, RNG *rng, Ray *ray)
1178 {
1179         float filter_u;
1180         float filter_v;
1181
1182         int num_samples = kernel_data.integrator.aa_samples;
1183
1184         path_rng_init(kg, rng_state, sample, num_samples, rng, x, y, &filter_u, &filter_v);
1185
1186         /* sample camera ray */
1187
1188         float lens_u = 0.0f, lens_v = 0.0f;
1189
1190         if(kernel_data.cam.aperturesize > 0.0f)
1191                 path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1192
1193         float time = 0.0f;
1194
1195 #ifdef __CAMERA_MOTION__
1196         if(kernel_data.cam.shuttertime != -1.0f)
1197                 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1198 #endif
1199
1200         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, ray);
1201 }
1202
1203 ccl_device void kernel_path_trace(KernelGlobals *kg,
1204         ccl_global float *buffer, ccl_global uint *rng_state,
1205         int sample, int x, int y, int offset, int stride)
1206 {
1207         /* buffer offset */
1208         int index = offset + x + y*stride;
1209         int pass_stride = kernel_data.film.pass_stride;
1210
1211         rng_state += index;
1212         buffer += index*pass_stride;
1213
1214         /* initialize random numbers and ray */
1215         RNG rng;
1216         Ray ray;
1217
1218         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1219
1220         /* integrate */
1221         float4 L;
1222
1223         if(ray.t != 0.0f)
1224                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
1225         else
1226                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1227
1228         /* accumulate result in output buffer */
1229         kernel_write_pass_float4(buffer, sample, L);
1230
1231         path_rng_end(kg, rng_state, rng);
1232 }
1233
1234 #ifdef __BRANCHED_PATH__
1235 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
1236         ccl_global float *buffer, ccl_global uint *rng_state,
1237         int sample, int x, int y, int offset, int stride)
1238 {
1239         /* buffer offset */
1240         int index = offset + x + y*stride;
1241         int pass_stride = kernel_data.film.pass_stride;
1242
1243         rng_state += index;
1244         buffer += index*pass_stride;
1245
1246         /* initialize random numbers and ray */
1247         RNG rng;
1248         Ray ray;
1249
1250         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1251
1252         /* integrate */
1253         float4 L;
1254
1255         if(ray.t != 0.0f)
1256                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
1257         else
1258                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1259
1260         /* accumulate result in output buffer */
1261         kernel_write_pass_float4(buffer, sample, L);
1262
1263         path_rng_end(kg, rng_state, rng);
1264 }
1265 #endif
1266
1267 CCL_NAMESPACE_END
1268