Cycles: Make OpenCL folks happy to use __KERNEL_DEBUG__
[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(kg, &volume_ray, PATH_RAY_ALL_VISIBILITY,
379                               &isect, NULL, 0.0f, 0.0f))
380         {
381                 ShaderData sd;
382                 shader_setup_from_ray(kg, &sd, &isect, &volume_ray, 0, 0);
383                 kernel_volume_stack_enter_exit(kg, &sd, stack);
384
385                 /* Move ray forward. */
386                 volume_ray.P = ray_offset(sd.P, -sd.Ng);
387                 volume_ray.D = normalize_len(Pend - volume_ray.P,
388                                              &volume_ray.t);
389
390                 /* TODO(sergey): Find a faster way detecting that ray_offset moved
391                  * us pass through the end point.
392                  */
393                 if(dot(ray->D, volume_ray.D) < 0.0f) {
394                         break;
395                 }
396         }
397 }
398 #endif
399
400 ccl_device bool kernel_path_subsurface_scatter(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, Ray *ray, float3 *throughput)
401 {
402         float bssrdf_probability;
403         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
404
405         /* modify throughput for picking bssrdf or bsdf */
406         *throughput *= bssrdf_probability;
407
408         /* do bssrdf scatter step if we picked a bssrdf closure */
409         if(sc) {
410                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
411
412                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
413                 float bssrdf_u, bssrdf_v;
414                 path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
415                 int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
416 #ifdef __VOLUME__
417                 Ray volume_ray = *ray;
418                 bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
419                                                 sd->flag & SD_OBJECT_INTERSECTS_VOLUME;
420 #endif
421
422                 /* compute lighting with the BSDF closure */
423                 for(int hit = 0; hit < num_hits; hit++) {
424                         float3 tp = *throughput;
425                         PathState hit_state = *state;
426                         Ray hit_ray = *ray;
427
428                         hit_state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
429                         hit_state.rng_offset += PRNG_BOUNCE_NUM;
430                         
431                         kernel_path_surface_connect_light(kg, rng, &bssrdf_sd[hit], tp, state, L);
432
433                         if(kernel_path_surface_bounce(kg, rng, &bssrdf_sd[hit], &tp, &hit_state, L, &hit_ray)) {
434 #ifdef __LAMP_MIS__
435                                 hit_state.ray_t = 0.0f;
436 #endif
437
438 #ifdef __VOLUME__
439                                 if(need_update_volume_stack) {
440                                         /* Setup ray from previous surface point to the new one. */
441                                         volume_ray.D = normalize_len(hit_ray.P - volume_ray.P,
442                                                                      &volume_ray.t);
443
444                                         kernel_path_subsurface_update_volume_stack(
445                                             kg,
446                                             &volume_ray,
447                                             hit_state.volume_stack);
448
449                                         /* Move volume ray forward. */
450                                         volume_ray.P = hit_ray.P;
451                                 }
452 #endif
453
454                                 kernel_path_indirect(kg, rng, hit_ray, tp, state->num_samples, hit_state, L);
455
456                                 /* for render passes, sum and reset indirect light pass variables
457                                  * for the next samples */
458                                 path_radiance_sum_indirect(L);
459                                 path_radiance_reset_indirect(L);
460                         }
461                 }
462                 return true;
463         }
464         return false;
465 }
466 #endif
467
468 ccl_device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
469 {
470         /* initialize */
471         PathRadiance L;
472         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
473         float L_transparent = 0.0f;
474
475         path_radiance_init(&L, kernel_data.film.use_light_pass);
476
477         PathState state;
478         path_state_init(kg, &state, rng, sample, &ray);
479
480 #ifdef __KERNEL_DEBUG__
481         DebugData debug_data;
482         debug_data_init(&debug_data);
483 #endif
484
485         /* path iteration */
486         for(;;) {
487                 /* intersect scene */
488                 Intersection isect;
489                 uint visibility = path_state_ray_visibility(kg, &state);
490
491 #ifdef __HAIR__
492                 float difl = 0.0f, extmax = 0.0f;
493                 uint lcg_state = 0;
494
495                 if(kernel_data.bvh.have_curves) {
496                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
497                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
498                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
499                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
500                         }
501
502                         extmax = kernel_data.curve.maximum_width;
503                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
504                 }
505
506                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
507 #else
508                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
509 #endif
510
511 #ifdef __KERNEL_DEBUG__
512                 if(state.flag & PATH_RAY_CAMERA) {
513                         debug_data.num_bvh_traversal_steps += isect.num_traversal_steps;
514                 }
515 #endif
516
517 #ifdef __LAMP_MIS__
518                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
519                         /* ray starting from previous non-transparent bounce */
520                         Ray light_ray;
521
522                         light_ray.P = ray.P - state.ray_t*ray.D;
523                         state.ray_t += isect.t;
524                         light_ray.D = ray.D;
525                         light_ray.t = state.ray_t;
526                         light_ray.time = ray.time;
527                         light_ray.dD = ray.dD;
528                         light_ray.dP = ray.dP;
529
530                         /* intersect with lamp */
531                         float3 emission;
532
533                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
534                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
535                 }
536 #endif
537
538 #ifdef __VOLUME__
539                 /* volume attenuation, emission, scatter */
540                 if(state.volume_stack[0].shader != SHADER_NONE) {
541                         Ray volume_ray = ray;
542                         volume_ray.t = (hit)? isect.t: FLT_MAX;
543
544                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
545
546 #ifdef __VOLUME_DECOUPLED__
547                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
548                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
549
550                         if(decoupled) {
551                                 /* cache steps along volume for repeated sampling */
552                                 VolumeSegment volume_segment;
553                                 ShaderData volume_sd;
554
555                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
556                                 kernel_volume_decoupled_record(kg, &state,
557                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
558
559                                 volume_segment.sampling_method = sampling_method;
560
561                                 /* emission */
562                                 if(volume_segment.closure_flag & SD_EMISSION)
563                                         path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
564
565                                 /* scattering */
566                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
567
568                                 if(volume_segment.closure_flag & SD_SCATTER) {
569                                         bool all = false;
570
571                                         /* direct light sampling */
572                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
573                                                 throughput, &state, &L, 1.0f, all, &volume_ray, &volume_segment);
574
575                                         /* indirect sample. if we use distance sampling and take just
576                                          * one sample for direct and indirect light, we could share
577                                          * this computation, but makes code a bit complex */
578                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
579                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
580
581                                         result = kernel_volume_decoupled_scatter(kg,
582                                                 &state, &volume_ray, &volume_sd, &throughput,
583                                                 rphase, rscatter, &volume_segment, NULL, true);
584                                 }
585
586                                 if(result != VOLUME_PATH_SCATTERED)
587                                         throughput *= volume_segment.accum_transmittance;
588
589                                 /* free cached steps */
590                                 kernel_volume_decoupled_free(kg, &volume_segment);
591
592                                 if(result == VOLUME_PATH_SCATTERED) {
593                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
594                                                 continue;
595                                         else
596                                                 break;
597                                 }
598                         }
599                         else 
600 #endif
601                         {
602                                 /* integrate along volume segment with distance sampling */
603                                 ShaderData volume_sd;
604                                 VolumeIntegrateResult result = kernel_volume_integrate(
605                                         kg, &state, &volume_sd, &volume_ray, &L, &throughput, rng, heterogeneous);
606
607 #ifdef __VOLUME_SCATTER__
608                                 if(result == VOLUME_PATH_SCATTERED) {
609                                         /* direct lighting */
610                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, &L);
611
612                                         /* indirect light bounce */
613                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
614                                                 continue;
615                                         else
616                                                 break;
617                                 }
618 #endif
619                         }
620                 }
621 #endif
622
623                 if(!hit) {
624                         /* eval background shader if nothing hit */
625                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
626                                 L_transparent += average(throughput);
627
628 #ifdef __PASSES__
629                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
630 #endif
631                                         break;
632                         }
633
634 #ifdef __BACKGROUND__
635                         /* sample background shader */
636                         float3 L_background = indirect_background(kg, &state, &ray);
637                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
638 #endif
639
640                         break;
641                 }
642
643                 /* setup shading */
644                 ShaderData sd;
645                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
646                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
647                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
648
649                 /* holdout */
650 #ifdef __HOLDOUT__
651                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
652                         if(kernel_data.background.transparent) {
653                                 float3 holdout_weight;
654                                 
655                                 if(sd.flag & SD_HOLDOUT_MASK)
656                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
657                                 else
658                                         holdout_weight = shader_holdout_eval(kg, &sd);
659
660                                 /* any throughput is ok, should all be identical here */
661                                 L_transparent += average(holdout_weight*throughput);
662                         }
663
664                         if(sd.flag & SD_HOLDOUT_MASK)
665                                 break;
666                 }
667 #endif
668
669                 /* holdout mask objects do not write data passes */
670                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
671
672                 /* blurring of bsdf after bounces, for rays that have a small likelihood
673                  * of following this particular path (diffuse, rough glossy) */
674                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
675                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
676
677                         if(blur_pdf < 1.0f) {
678                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
679                                 shader_bsdf_blur(kg, &sd, blur_roughness);
680                         }
681                 }
682
683 #ifdef __EMISSION__
684                 /* emission */
685                 if(sd.flag & SD_EMISSION) {
686                         /* todo: is isect.t wrong here for transparent surfaces? */
687                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
688                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
689                 }
690 #endif
691
692                 /* path termination. this is a strange place to put the termination, it's
693                  * mainly due to the mixed in MIS that we use. gives too many unneeded
694                  * shader evaluations, only need emission if we are going to terminate */
695                 float probability = path_state_terminate_probability(kg, &state, throughput);
696
697                 if(probability == 0.0f) {
698                         break;
699                 }
700                 else if(probability != 1.0f) {
701                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
702
703                         if(terminate >= probability)
704                                 break;
705
706                         throughput /= probability;
707                 }
708
709 #ifdef __AO__
710                 /* ambient occlusion */
711                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
712                         kernel_path_ao(kg, &sd, &L, &state, rng, throughput);
713                 }
714 #endif
715
716 #ifdef __SUBSURFACE__
717                 /* bssrdf scatter to a different location on the same object, replacing
718                  * the closures with a diffuse BSDF */
719                 if(sd.flag & SD_BSSRDF) {
720                         if(kernel_path_subsurface_scatter(kg, &sd, &L, &state, rng, &ray, &throughput))
721                                 break;
722                 }
723 #endif
724
725                 /* direct lighting */
726                 kernel_path_surface_connect_light(kg, rng, &sd, throughput, &state, &L);
727
728                 /* compute direct lighting and next bounce */
729                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
730                         break;
731         }
732
733         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
734
735         kernel_write_light_passes(kg, buffer, &L, sample);
736
737 #ifdef __KERNEL_DEBUG__
738         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
739 #endif
740
741         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
742 }
743
744 #ifdef __BRANCHED_PATH__
745
746 /* branched path tracing: bounce off surface and integrate indirect light */
747 ccl_device_noinline void kernel_branched_path_surface_indirect_light(KernelGlobals *kg,
748         RNG *rng, ShaderData *sd, float3 throughput, float num_samples_adjust,
749         PathState *state, PathRadiance *L)
750 {
751         for(int i = 0; i< sd->num_closure; i++) {
752                 const ShaderClosure *sc = &sd->closure[i];
753
754                 if(!CLOSURE_IS_BSDF(sc->type))
755                         continue;
756                 /* transparency is not handled here, but in outer loop */
757                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
758                         continue;
759
760                 int num_samples;
761
762                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
763                         num_samples = kernel_data.integrator.diffuse_samples;
764                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
765                         num_samples = 1;
766                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
767                         num_samples = kernel_data.integrator.glossy_samples;
768                 else
769                         num_samples = kernel_data.integrator.transmission_samples;
770
771                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
772
773                 float num_samples_inv = num_samples_adjust/num_samples;
774                 RNG bsdf_rng = cmj_hash(*rng, i);
775
776                 for(int j = 0; j < num_samples; j++) {
777                         PathState ps = *state;
778                         float3 tp = throughput;
779                         Ray bsdf_ray;
780
781                         if(!kernel_branched_path_surface_bounce(kg, &bsdf_rng, sd, sc, j, num_samples, &tp, &ps, L, &bsdf_ray))
782                                 continue;
783
784                         kernel_path_indirect(kg, rng, bsdf_ray, tp*num_samples_inv, num_samples, ps, L);
785
786                         /* for render passes, sum and reset indirect light pass variables
787                          * for the next samples */
788                         path_radiance_sum_indirect(L);
789                         path_radiance_reset_indirect(L);
790                 }
791         }
792 }
793
794 #ifdef __SUBSURFACE__
795 ccl_device void kernel_branched_path_subsurface_scatter(KernelGlobals *kg,
796                                                         ShaderData *sd,
797                                                         PathRadiance *L,
798                                                         PathState *state,
799                                                         RNG *rng,
800                                                         Ray *ray,
801                                                         float3 throughput)
802 {
803         for(int i = 0; i< sd->num_closure; i++) {
804                 ShaderClosure *sc = &sd->closure[i];
805
806                 if(!CLOSURE_IS_BSSRDF(sc->type))
807                         continue;
808
809                 /* set up random number generator */
810                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
811                 int num_samples = kernel_data.integrator.subsurface_samples;
812                 float num_samples_inv = 1.0f/num_samples;
813                 RNG bssrdf_rng = cmj_hash(*rng, i);
814
815                 state->flag |= PATH_RAY_BSSRDF_ANCESTOR;
816
817                 /* do subsurface scatter step with copy of shader data, this will
818                  * replace the BSSRDF with a diffuse BSDF closure */
819                 for(int j = 0; j < num_samples; j++) {
820                         ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
821                         float bssrdf_u, bssrdf_v;
822                         path_branched_rng_2D(kg, &bssrdf_rng, state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
823                         int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, true);
824 #ifdef __VOLUME__
825                         Ray volume_ray = *ray;
826                         bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
827                                                         sd->flag & SD_OBJECT_INTERSECTS_VOLUME;
828 #endif
829
830                         /* compute lighting with the BSDF closure */
831                         for(int hit = 0; hit < num_hits; hit++) {
832                                 PathState hit_state = *state;
833
834                                 path_state_branch(&hit_state, j, num_samples);
835
836 #ifdef __VOLUME__
837                                 if(need_update_volume_stack) {
838                                         /* Setup ray from previous surface point to the new one. */
839                                         float3 P = ray_offset(bssrdf_sd[hit].P, -bssrdf_sd[hit].Ng);
840                                         volume_ray.D = normalize_len(P - volume_ray.P,
841                                                                      &volume_ray.t);
842
843                                         kernel_path_subsurface_update_volume_stack(
844                                             kg,
845                                             &volume_ray,
846                                             hit_state.volume_stack);
847
848                                         /* Move volume ray forward. */
849                                         volume_ray.P = P;
850                                 }
851 #endif
852
853 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
854                                 /* direct light */
855                                 if(kernel_data.integrator.use_direct_light) {
856                                         bool all = kernel_data.integrator.sample_all_lights_direct;
857                                         kernel_branched_path_surface_connect_light(kg, rng,
858                                                 &bssrdf_sd[hit], &hit_state, throughput, num_samples_inv, L, all);
859                                 }
860 #endif
861
862                                 /* indirect light */
863                                 kernel_branched_path_surface_indirect_light(kg, rng,
864                                         &bssrdf_sd[hit], throughput, num_samples_inv,
865                                         &hit_state, L);
866                         }
867                 }
868
869                 state->flag &= ~PATH_RAY_BSSRDF_ANCESTOR;
870         }
871 }
872 #endif
873
874 ccl_device float4 kernel_branched_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
875 {
876         /* initialize */
877         PathRadiance L;
878         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
879         float L_transparent = 0.0f;
880
881         path_radiance_init(&L, kernel_data.film.use_light_pass);
882
883         PathState state;
884         path_state_init(kg, &state, rng, sample, &ray);
885
886 #ifdef __KERNEL_DEBUG__
887         DebugData debug_data;
888         debug_data_init(&debug_data);
889 #endif
890
891         for(;;) {
892                 /* intersect scene */
893                 Intersection isect;
894                 uint visibility = path_state_ray_visibility(kg, &state);
895
896 #ifdef __HAIR__
897                 float difl = 0.0f, extmax = 0.0f;
898                 uint lcg_state = 0;
899
900                 if(kernel_data.bvh.have_curves) {
901                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
902                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
903                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
904                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
905                         }
906
907                         extmax = kernel_data.curve.maximum_width;
908                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
909                 }
910
911                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
912 #else
913                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
914 #endif
915
916 #ifdef __KERNEL_DEBUG__
917                 if(state.flag & PATH_RAY_CAMERA) {
918                         debug_data.num_bvh_traversal_steps += isect.num_traversal_steps;
919                 }
920 #endif
921
922 #ifdef __VOLUME__
923                 /* volume attenuation, emission, scatter */
924                 if(state.volume_stack[0].shader != SHADER_NONE) {
925                         Ray volume_ray = ray;
926                         volume_ray.t = (hit)? isect.t: FLT_MAX;
927                         
928                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
929
930 #ifdef __VOLUME_DECOUPLED__
931                         /* decoupled ray marching only supported on CPU */
932
933                         /* cache steps along volume for repeated sampling */
934                         VolumeSegment volume_segment;
935                         ShaderData volume_sd;
936
937                         shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
938                         kernel_volume_decoupled_record(kg, &state,
939                                 &volume_ray, &volume_sd, &volume_segment, heterogeneous);
940
941                         /* direct light sampling */
942                         if(volume_segment.closure_flag & SD_SCATTER) {
943                                 volume_segment.sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
944
945                                 bool all = kernel_data.integrator.sample_all_lights_direct;
946
947                                 kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
948                                         throughput, &state, &L, 1.0f, all, &volume_ray, &volume_segment);
949
950                                 /* indirect light sampling */
951                                 int num_samples = kernel_data.integrator.volume_samples;
952                                 float num_samples_inv = 1.0f/num_samples;
953
954                                 for(int j = 0; j < num_samples; j++) {
955                                         /* workaround to fix correlation bug in T38710, can find better solution
956                                          * in random number generator later, for now this is done here to not impact
957                                          * performance of rendering without volumes */
958                                         RNG tmp_rng = cmj_hash(*rng, state.rng_offset);
959
960                                         PathState ps = state;
961                                         Ray pray = ray;
962                                         float3 tp = throughput;
963
964                                         /* branch RNG state */
965                                         path_state_branch(&ps, j, num_samples);
966
967                                         /* scatter sample. if we use distance sampling and take just one
968                                          * sample for direct and indirect light, we could share this
969                                          * computation, but makes code a bit complex */
970                                         float rphase = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_PHASE);
971                                         float rscatter = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_SCATTER_DISTANCE);
972
973                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
974                                                 &ps, &pray, &volume_sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
975                                                 
976                                         (void)result;
977                                         kernel_assert(result == VOLUME_PATH_SCATTERED);
978
979                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray)) {
980                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
981
982                                                 /* for render passes, sum and reset indirect light pass variables
983                                                  * for the next samples */
984                                                 path_radiance_sum_indirect(&L);
985                                                 path_radiance_reset_indirect(&L);
986                                         }
987                                 }
988                         }
989
990                         /* emission and transmittance */
991                         if(volume_segment.closure_flag & SD_EMISSION)
992                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
993                         throughput *= volume_segment.accum_transmittance;
994
995                         /* free cached steps */
996                         kernel_volume_decoupled_free(kg, &volume_segment);
997 #else
998                         /* GPU: no decoupled ray marching, scatter probalistically */
999                         int num_samples = kernel_data.integrator.volume_samples;
1000                         float num_samples_inv = 1.0f/num_samples;
1001
1002                         /* todo: we should cache the shader evaluations from stepping
1003                          * through the volume, for now we redo them multiple times */
1004
1005                         for(int j = 0; j < num_samples; j++) {
1006                                 PathState ps = state;
1007                                 Ray pray = ray;
1008                                 ShaderData volume_sd;
1009                                 float3 tp = throughput * num_samples_inv;
1010
1011                                 /* branch RNG state */
1012                                 path_state_branch(&ps, j, num_samples);
1013
1014                                 VolumeIntegrateResult result = kernel_volume_integrate(
1015                                         kg, &ps, &volume_sd, &volume_ray, &L, &tp, rng, heterogeneous);
1016                                 
1017 #ifdef __VOLUME_SCATTER__
1018                                 if(result == VOLUME_PATH_SCATTERED) {
1019                                         /* todo: support equiangular, MIS and all light sampling.
1020                                          * alternatively get decoupled ray marching working on the GPU */
1021                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, tp, &state, &L);
1022
1023                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray)) {
1024                                                 kernel_path_indirect(kg, rng, pray, tp, num_samples, ps, &L);
1025
1026                                                 /* for render passes, sum and reset indirect light pass variables
1027                                                  * for the next samples */
1028                                                 path_radiance_sum_indirect(&L);
1029                                                 path_radiance_reset_indirect(&L);
1030                                         }
1031                                 }
1032 #endif
1033                         }
1034
1035                         /* todo: avoid this calculation using decoupled ray marching */
1036                         kernel_volume_shadow(kg, &state, &volume_ray, &throughput);
1037 #endif
1038                 }
1039 #endif
1040
1041                 if(!hit) {
1042                         /* eval background shader if nothing hit */
1043                         if(kernel_data.background.transparent) {
1044                                 L_transparent += average(throughput);
1045
1046 #ifdef __PASSES__
1047                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
1048 #endif
1049                                         break;
1050                         }
1051
1052 #ifdef __BACKGROUND__
1053                         /* sample background shader */
1054                         float3 L_background = indirect_background(kg, &state, &ray);
1055                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
1056 #endif
1057
1058                         break;
1059                 }
1060
1061                 /* setup shading */
1062                 ShaderData sd;
1063                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
1064                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
1065                 shader_merge_closures(&sd);
1066
1067                 /* holdout */
1068 #ifdef __HOLDOUT__
1069                 if(sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) {
1070                         if(kernel_data.background.transparent) {
1071                                 float3 holdout_weight;
1072                                 
1073                                 if(sd.flag & SD_HOLDOUT_MASK)
1074                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
1075                                 else
1076                                         holdout_weight = shader_holdout_eval(kg, &sd);
1077
1078                                 /* any throughput is ok, should all be identical here */
1079                                 L_transparent += average(holdout_weight*throughput);
1080                         }
1081
1082                         if(sd.flag & SD_HOLDOUT_MASK)
1083                                 break;
1084                 }
1085 #endif
1086
1087                 /* holdout mask objects do not write data passes */
1088                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
1089
1090 #ifdef __EMISSION__
1091                 /* emission */
1092                 if(sd.flag & SD_EMISSION) {
1093                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
1094                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
1095                 }
1096 #endif
1097
1098                 /* transparency termination */
1099                 if(state.flag & PATH_RAY_TRANSPARENT) {
1100                         /* path termination. this is a strange place to put the termination, it's
1101                          * mainly due to the mixed in MIS that we use. gives too many unneeded
1102                          * shader evaluations, only need emission if we are going to terminate */
1103                         float probability = path_state_terminate_probability(kg, &state, throughput);
1104
1105                         if(probability == 0.0f) {
1106                                 break;
1107                         }
1108                         else if(probability != 1.0f) {
1109                                 float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
1110
1111                                 if(terminate >= probability)
1112                                         break;
1113
1114                                 throughput /= probability;
1115                         }
1116                 }
1117
1118 #ifdef __AO__
1119                 /* ambient occlusion */
1120                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
1121                         kernel_branched_path_ao(kg, &sd, &L, &state, rng, throughput);
1122                 }
1123 #endif
1124
1125 #ifdef __SUBSURFACE__
1126                 /* bssrdf scatter to a different location on the same object */
1127                 if(sd.flag & SD_BSSRDF) {
1128                         kernel_branched_path_subsurface_scatter(kg, &sd, &L, &state,
1129                                                                 rng, &ray, throughput);
1130                 }
1131 #endif
1132
1133                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
1134                         PathState hit_state = state;
1135
1136 #ifdef __EMISSION__
1137                         /* direct light */
1138                         if(kernel_data.integrator.use_direct_light) {
1139                                 bool all = kernel_data.integrator.sample_all_lights_direct;
1140                                 kernel_branched_path_surface_connect_light(kg, rng,
1141                                         &sd, &hit_state, throughput, 1.0f, &L, all);
1142                         }
1143 #endif
1144
1145                         /* indirect light */
1146                         kernel_branched_path_surface_indirect_light(kg, rng,
1147                                 &sd, throughput, 1.0f, &hit_state, &L);
1148
1149                         /* continue in case of transparency */
1150                         throughput *= shader_bsdf_transparency(kg, &sd);
1151
1152                         if(is_zero(throughput))
1153                                 break;
1154                 }
1155
1156                 path_state_next(kg, &state, LABEL_TRANSPARENT);
1157                 ray.P = ray_offset(sd.P, -sd.Ng);
1158                 ray.t -= sd.ray_length; /* clipping works through transparent */
1159
1160
1161 #ifdef __RAY_DIFFERENTIALS__
1162                 ray.dP = sd.dP;
1163                 ray.dD.dx = -sd.dI.dx;
1164                 ray.dD.dy = -sd.dI.dy;
1165 #endif
1166
1167 #ifdef __VOLUME__
1168                 /* enter/exit volume */
1169                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
1170 #endif
1171         }
1172
1173         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
1174
1175         kernel_write_light_passes(kg, buffer, &L, sample);
1176
1177 #ifdef __KERNEL_DEBUG__
1178         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
1179 #endif
1180
1181         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1182 }
1183
1184 #endif
1185
1186 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)
1187 {
1188         float filter_u;
1189         float filter_v;
1190
1191         int num_samples = kernel_data.integrator.aa_samples;
1192
1193         path_rng_init(kg, rng_state, sample, num_samples, rng, x, y, &filter_u, &filter_v);
1194
1195         /* sample camera ray */
1196
1197         float lens_u = 0.0f, lens_v = 0.0f;
1198
1199         if(kernel_data.cam.aperturesize > 0.0f)
1200                 path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1201
1202         float time = 0.0f;
1203
1204 #ifdef __CAMERA_MOTION__
1205         if(kernel_data.cam.shuttertime != -1.0f)
1206                 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1207 #endif
1208
1209         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, ray);
1210 }
1211
1212 ccl_device void kernel_path_trace(KernelGlobals *kg,
1213         ccl_global float *buffer, ccl_global uint *rng_state,
1214         int sample, int x, int y, int offset, int stride)
1215 {
1216         /* buffer offset */
1217         int index = offset + x + y*stride;
1218         int pass_stride = kernel_data.film.pass_stride;
1219
1220         rng_state += index;
1221         buffer += index*pass_stride;
1222
1223         /* initialize random numbers and ray */
1224         RNG rng;
1225         Ray ray;
1226
1227         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1228
1229         /* integrate */
1230         float4 L;
1231
1232         if(ray.t != 0.0f)
1233                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
1234         else
1235                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1236
1237         /* accumulate result in output buffer */
1238         kernel_write_pass_float4(buffer, sample, L);
1239
1240         path_rng_end(kg, rng_state, rng);
1241 }
1242
1243 #ifdef __BRANCHED_PATH__
1244 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
1245         ccl_global float *buffer, ccl_global uint *rng_state,
1246         int sample, int x, int y, int offset, int stride)
1247 {
1248         /* buffer offset */
1249         int index = offset + x + y*stride;
1250         int pass_stride = kernel_data.film.pass_stride;
1251
1252         rng_state += index;
1253         buffer += index*pass_stride;
1254
1255         /* initialize random numbers and ray */
1256         RNG rng;
1257         Ray ray;
1258
1259         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1260
1261         /* integrate */
1262         float4 L;
1263
1264         if(ray.t != 0.0f)
1265                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
1266         else
1267                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1268
1269         /* accumulate result in output buffer */
1270         kernel_write_pass_float4(buffer, sample, L);
1271
1272         path_rng_end(kg, rng_state, rng);
1273 }
1274 #endif
1275
1276 CCL_NAMESPACE_END
1277