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