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