Cycles / HSV Separator and Combine node:
[blender.git] / intern / cycles / kernel / kernel_path.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifdef __OSL__
20 #include "osl_shader.h"
21 #endif
22
23 #include "kernel_differential.h"
24 #include "kernel_montecarlo.h"
25 #include "kernel_projection.h"
26 #include "kernel_object.h"
27 #include "kernel_triangle.h"
28 #include "kernel_curve.h"
29 #include "kernel_primitive.h"
30 #include "kernel_projection.h"
31 #include "kernel_random.h"
32 #include "kernel_bvh.h"
33 #include "kernel_accumulate.h"
34 #include "kernel_camera.h"
35 #include "kernel_shader.h"
36 #include "kernel_light.h"
37 #include "kernel_emission.h"
38 #include "kernel_passes.h"
39
40 #ifdef __SUBSURFACE__
41 #include "kernel_subsurface.h"
42 #endif
43
44 CCL_NAMESPACE_BEGIN
45
46 typedef struct PathState {
47         int flag;
48         int bounce;
49
50         int diffuse_bounce;
51         int glossy_bounce;
52         int transmission_bounce;
53         int transparent_bounce;
54 } PathState;
55
56 __device_inline void path_state_init(PathState *state)
57 {
58         state->flag = PATH_RAY_CAMERA|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
59         state->bounce = 0;
60         state->diffuse_bounce = 0;
61         state->glossy_bounce = 0;
62         state->transmission_bounce = 0;
63         state->transparent_bounce = 0;
64 }
65
66 __device_inline void path_state_next(KernelGlobals *kg, PathState *state, int label)
67 {
68         /* ray through transparent keeps same flags from previous ray and is
69          * not counted as a regular bounce, transparent has separate max */
70         if(label & LABEL_TRANSPARENT) {
71                 state->flag |= PATH_RAY_TRANSPARENT;
72                 state->transparent_bounce++;
73
74                 if(!kernel_data.integrator.transparent_shadows)
75                         state->flag |= PATH_RAY_MIS_SKIP;
76
77                 return;
78         }
79
80         state->bounce++;
81
82         /* reflection/transmission */
83         if(label & LABEL_REFLECT) {
84                 state->flag |= PATH_RAY_REFLECT;
85                 state->flag &= ~(PATH_RAY_TRANSMIT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
86
87                 if(label & LABEL_DIFFUSE)
88                         state->diffuse_bounce++;
89                 else
90                         state->glossy_bounce++;
91         }
92         else {
93                 kernel_assert(label & LABEL_TRANSMIT);
94
95                 state->flag |= PATH_RAY_TRANSMIT;
96                 state->flag &= ~(PATH_RAY_REFLECT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
97
98                 state->transmission_bounce++;
99         }
100
101         /* diffuse/glossy/singular */
102         if(label & LABEL_DIFFUSE) {
103                 state->flag |= PATH_RAY_DIFFUSE;
104                 state->flag &= ~(PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
105         }
106         else if(label & LABEL_GLOSSY) {
107                 state->flag |= PATH_RAY_GLOSSY;
108                 state->flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
109         }
110         else {
111                 kernel_assert(label & LABEL_SINGULAR);
112
113                 state->flag |= PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
114                 state->flag &= ~PATH_RAY_DIFFUSE;
115         }
116 }
117
118 __device_inline uint path_state_ray_visibility(KernelGlobals *kg, PathState *state)
119 {
120         uint flag = state->flag;
121
122         /* for visibility, diffuse/glossy are for reflection only */
123         if(flag & PATH_RAY_TRANSMIT)
124                 flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY);
125         /* for camera visibility, use render layer flags */
126         if(flag & PATH_RAY_CAMERA)
127                 flag |= kernel_data.integrator.layer_flag;
128
129         return flag;
130 }
131
132 __device_inline float path_state_terminate_probability(KernelGlobals *kg, PathState *state, const float3 throughput)
133 {
134         if(state->flag & PATH_RAY_TRANSPARENT) {
135                 /* transparent rays treated separately */
136                 if(state->transparent_bounce >= kernel_data.integrator.transparent_max_bounce)
137                         return 0.0f;
138                 else if(state->transparent_bounce <= kernel_data.integrator.transparent_min_bounce)
139                         return 1.0f;
140         }
141         else {
142                 /* other rays */
143                 if((state->bounce >= kernel_data.integrator.max_bounce) ||
144                    (state->diffuse_bounce >= kernel_data.integrator.max_diffuse_bounce) ||
145                    (state->glossy_bounce >= kernel_data.integrator.max_glossy_bounce) ||
146                    (state->transmission_bounce >= kernel_data.integrator.max_transmission_bounce))
147                 {
148                         return 0.0f;
149                 }
150                 else if(state->bounce <= kernel_data.integrator.min_bounce) {
151                         return 1.0f;
152                 }
153         }
154
155         /* probalistic termination */
156         return average(throughput); /* todo: try using max here */
157 }
158
159 __device_inline bool shadow_blocked(KernelGlobals *kg, PathState *state, Ray *ray, float3 *shadow)
160 {
161         *shadow = make_float3(1.0f, 1.0f, 1.0f);
162
163         if(ray->t == 0.0f)
164                 return false;
165         
166         Intersection isect;
167 #ifdef __HAIR__
168         bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect, NULL, 0.0f, 0.0f);
169 #else
170         bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect);
171 #endif
172
173 #ifdef __TRANSPARENT_SHADOWS__
174         if(result && kernel_data.integrator.transparent_shadows) {
175                 /* transparent shadows work in such a way to try to minimize overhead
176                  * in cases where we don't need them. after a regular shadow ray is
177                  * cast we check if the hit primitive was potentially transparent, and
178                  * only in that case start marching. this gives on extra ray cast for
179                  * the cases were we do want transparency.
180                  *
181                  * also note that for this to work correct, multi close sampling must
182                  * be used, since we don't pass a random number to shader_eval_surface */
183                 if(shader_transparent_shadow(kg, &isect)) {
184                         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
185                         float3 Pend = ray->P + ray->D*ray->t;
186                         int bounce = state->transparent_bounce;
187
188                         for(;;) {
189                                 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
190                                         return true;
191                                 }
192                                 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
193                                         /* todo: get random number somewhere for probabilistic terminate */
194 #if 0
195                                         float probability = average(throughput);
196                                         float terminate = 0.0f;
197
198                                         if(terminate >= probability)
199                                                 return true;
200
201                                         throughput /= probability;
202 #endif
203                                 }
204
205 #ifdef __HAIR__
206                                 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect, NULL, 0.0f, 0.0f)) {
207 #else
208                                 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect)) {
209 #endif
210                                         *shadow *= throughput;
211                                         return false;
212                                 }
213
214                                 if(!shader_transparent_shadow(kg, &isect))
215                                         return true;
216
217                                 ShaderData sd;
218                                 shader_setup_from_ray(kg, &sd, &isect, ray, state->bounce+1);
219                                 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW, SHADER_CONTEXT_SHADOW);
220
221                                 throughput *= shader_bsdf_transparency(kg, &sd);
222
223                                 ray->P = ray_offset(sd.P, -sd.Ng);
224                                 if(ray->t != FLT_MAX)
225                                         ray->D = normalize_len(Pend - ray->P, &ray->t);
226
227                                 bounce++;
228                         }
229                 }
230         }
231 #endif
232
233         return result;
234 }
235
236 __device float4 kernel_path_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
237 {
238         /* initialize */
239         PathRadiance L;
240         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
241         float L_transparent = 0.0f;
242
243         path_radiance_init(&L, kernel_data.film.use_light_pass);
244
245         float min_ray_pdf = FLT_MAX;
246         float ray_pdf = 0.0f;
247 #ifdef __LAMP_MIS__
248         float ray_t = 0.0f;
249 #endif
250         PathState state;
251         int rng_offset = PRNG_BASE_NUM;
252 #ifdef __CMJ__
253         int num_samples = kernel_data.integrator.aa_samples;
254 #else
255         int num_samples = 0;
256 #endif
257
258         path_state_init(&state);
259
260         /* path iteration */
261         for(;; rng_offset += PRNG_BOUNCE_NUM) {
262                 /* intersect scene */
263                 Intersection isect;
264                 uint visibility = path_state_ray_visibility(kg, &state);
265
266 #ifdef __HAIR__
267                 float difl = 0.0f, extmax = 0.0f;
268                 uint lcg_state = 0;
269
270                 if(kernel_data.bvh.have_curves) {
271                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
272                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
273                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
274                                 difl = kernel_data.curve_kernel_data.minimum_width * len(pixdiff) * 0.5f;
275                         }
276
277                         extmax = kernel_data.curve_kernel_data.maximum_width;
278                         lcg_state = lcg_init(*rng + rng_offset + sample*0x51633e2d);
279                 }
280
281                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
282 #else
283                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
284 #endif
285
286 #ifdef __LAMP_MIS__
287                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
288                         /* ray starting from previous non-transparent bounce */
289                         Ray light_ray;
290
291                         light_ray.P = ray.P - ray_t*ray.D;
292                         ray_t += isect.t;
293                         light_ray.D = ray.D;
294                         light_ray.t = ray_t;
295                         light_ray.time = ray.time;
296                         light_ray.dD = ray.dD;
297                         light_ray.dP = ray.dP;
298
299                         /* intersect with lamp */
300                         float light_t = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT);
301                         float3 emission;
302
303                         if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission, state.bounce))
304                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
305                 }
306 #endif
307
308                 if(!hit) {
309                         /* eval background shader if nothing hit */
310                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
311                                 L_transparent += average(throughput);
312
313 #ifdef __PASSES__
314                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
315 #endif
316                                         break;
317                         }
318
319 #ifdef __BACKGROUND__
320                         /* sample background shader */
321                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf, state.bounce);
322                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
323 #endif
324
325                         break;
326                 }
327
328                 /* setup shading */
329                 ShaderData sd;
330                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
331                 float rbsdf = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF);
332                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
333
334                 /* holdout */
335 #ifdef __HOLDOUT__
336                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
337                         if(kernel_data.background.transparent) {
338                                 float3 holdout_weight;
339                                 
340                                 if(sd.flag & SD_HOLDOUT_MASK)
341                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
342                                 else
343                                         holdout_weight = shader_holdout_eval(kg, &sd);
344
345                                 /* any throughput is ok, should all be identical here */
346                                 L_transparent += average(holdout_weight*throughput);
347                         }
348
349                         if(sd.flag & SD_HOLDOUT_MASK)
350                                 break;
351                 }
352 #endif
353
354                 /* holdout mask objects do not write data passes */
355                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
356
357                 /* blurring of bsdf after bounces, for rays that have a small likelihood
358                  * of following this particular path (diffuse, rough glossy) */
359                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
360                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
361
362                         if(blur_pdf < 1.0f) {
363                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
364                                 shader_bsdf_blur(kg, &sd, blur_roughness);
365                         }
366                 }
367
368 #ifdef __EMISSION__
369                 /* emission */
370                 if(sd.flag & SD_EMISSION) {
371                         /* todo: is isect.t wrong here for transparent surfaces? */
372                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
373                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
374                 }
375 #endif
376
377                 /* path termination. this is a strange place to put the termination, it's
378                  * mainly due to the mixed in MIS that we use. gives too many unneeded
379                  * shader evaluations, only need emission if we are going to terminate */
380                 float probability = path_state_terminate_probability(kg, &state, throughput);
381
382                 if(probability == 0.0f) {
383                         break;
384                 }
385                 else if(probability != 1.0f) {
386                         float terminate = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_TERMINATE);
387
388                         if(terminate >= probability)
389                                 break;
390
391                         throughput /= probability;
392                 }
393
394 #ifdef __SUBSURFACE__
395                 /* bssrdf scatter to a different location on the same object, replacing
396                  * the closures with a diffuse BSDF */
397                 if(sd.flag & SD_BSSRDF) {
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_init(*rng + rng_offset + sample*0x68bc21eb);
407                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, false);
408                         }
409                 }
410 #endif
411
412 #ifdef __AO__
413                 /* ambient occlusion */
414                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
415                         /* todo: solve correlation */
416                         float bsdf_u, bsdf_v;
417                         path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
418
419                         float ao_factor = kernel_data.background.ao_factor;
420                         float3 ao_N;
421                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
422                         float3 ao_D;
423                         float ao_pdf;
424
425                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
426
427                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
428                                 Ray light_ray;
429                                 float3 ao_shadow;
430
431                                 light_ray.P = ray_offset(sd.P, sd.Ng);
432                                 light_ray.D = ao_D;
433                                 light_ray.t = kernel_data.background.ao_distance;
434 #ifdef __OBJECT_MOTION__
435                                 light_ray.time = sd.time;
436 #endif
437                                 light_ray.dP = sd.dP;
438                                 light_ray.dD = differential3_zero();
439
440                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
441                                         path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
442                         }
443                 }
444 #endif
445
446 #ifdef __EMISSION__
447                 if(kernel_data.integrator.use_direct_light) {
448                         /* sample illumination from lights to find path contribution */
449                         if(sd.flag & SD_BSDF_HAS_EVAL) {
450                                 float light_t = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT);
451 #ifdef __MULTI_CLOSURE__
452                                 float light_o = 0.0f;
453 #else
454                                 float light_o = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT_F);
455 #endif
456                                 float light_u, light_v;
457                                 path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT_U, &light_u, &light_v);
458
459                                 Ray light_ray;
460                                 BsdfEval L_light;
461                                 bool is_lamp;
462
463 #ifdef __OBJECT_MOTION__
464                                 light_ray.time = sd.time;
465 #endif
466
467                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
468                                         /* trace shadow ray */
469                                         float3 shadow;
470
471                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
472                                                 /* accumulate */
473                                                 path_radiance_accum_light(&L, throughput, &L_light, shadow, 1.0f, state.bounce, is_lamp);
474                                         }
475                                 }
476                         }
477                 }
478 #endif
479
480                 /* no BSDF? we can stop here */
481                 if(!(sd.flag & SD_BSDF))
482                         break;
483
484                 /* sample BSDF */
485                 float bsdf_pdf;
486                 BsdfEval bsdf_eval;
487                 float3 bsdf_omega_in;
488                 differential3 bsdf_domega_in;
489                 float bsdf_u, bsdf_v;
490                 path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
491                 int label;
492
493                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
494                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
495
496                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
497                         break;
498
499                 /* modify throughput */
500                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
501
502                 /* set labels */
503                 if(!(label & LABEL_TRANSPARENT)) {
504                         ray_pdf = bsdf_pdf;
505 #ifdef __LAMP_MIS__
506                         ray_t = 0.0f;
507 #endif
508                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
509                 }
510
511                 /* update path state */
512                 path_state_next(kg, &state, label);
513
514                 /* setup ray */
515                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
516                 ray.D = bsdf_omega_in;
517
518                 if(state.bounce == 0)
519                         ray.t -= sd.ray_length; /* clipping works through transparent */
520                 else
521                         ray.t = FLT_MAX;
522
523 #ifdef __RAY_DIFFERENTIALS__
524                 ray.dP = sd.dP;
525                 ray.dD = bsdf_domega_in;
526 #endif
527         }
528
529         float3 L_sum = path_radiance_sum(kg, &L);
530
531 #ifdef __CLAMP_SAMPLE__
532         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
533 #endif
534
535         kernel_write_light_passes(kg, buffer, &L, sample);
536
537         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
538 }
539
540 #ifdef __NON_PROGRESSIVE__
541
542 __device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer,
543         float3 throughput, int num_samples, int num_total_samples,
544         float min_ray_pdf, float ray_pdf, PathState state, int rng_offset, PathRadiance *L)
545 {
546 #ifdef __LAMP_MIS__
547         float ray_t = 0.0f;
548 #endif
549
550         /* path iteration */
551         for(;; rng_offset += PRNG_BOUNCE_NUM) {
552                 /* intersect scene */
553                 Intersection isect;
554                 uint visibility = path_state_ray_visibility(kg, &state);
555 #ifdef __HAIR__
556                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
557 #else
558                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
559 #endif
560
561 #ifdef __LAMP_MIS__
562                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
563                         /* ray starting from previous non-transparent bounce */
564                         Ray light_ray;
565
566                         light_ray.P = ray.P - ray_t*ray.D;
567                         ray_t += isect.t;
568                         light_ray.D = ray.D;
569                         light_ray.t = ray_t;
570                         light_ray.time = ray.time;
571                         light_ray.dD = ray.dD;
572                         light_ray.dP = ray.dP;
573
574                         /* intersect with lamp */
575                         float light_t = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT);
576                         float3 emission;
577
578                         if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission, state.bounce))
579                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
580                 }
581 #endif
582
583                 if(!hit) {
584 #ifdef __BACKGROUND__
585                         /* sample background shader */
586                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf, state.bounce);
587                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
588 #endif
589
590                         break;
591                 }
592
593                 /* setup shading */
594                 ShaderData sd;
595                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
596                 float rbsdf = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF);
597                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
598                 shader_merge_closures(kg, &sd);
599
600                 /* blurring of bsdf after bounces, for rays that have a small likelihood
601                  * of following this particular path (diffuse, rough glossy) */
602                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
603                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
604
605                         if(blur_pdf < 1.0f) {
606                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
607                                 shader_bsdf_blur(kg, &sd, blur_roughness);
608                         }
609                 }
610
611 #ifdef __EMISSION__
612                 /* emission */
613                 if(sd.flag & SD_EMISSION) {
614                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
615                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
616                 }
617 #endif
618
619                 /* path termination. this is a strange place to put the termination, it's
620                  * mainly due to the mixed in MIS that we use. gives too many unneeded
621                  * shader evaluations, only need emission if we are going to terminate */
622                 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
623
624                 if(probability == 0.0f) {
625                         break;
626                 }
627                 else if(probability != 1.0f) {
628                         float terminate = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_TERMINATE);
629
630                         if(terminate >= probability)
631                                 break;
632
633                         throughput /= probability;
634                 }
635
636 #ifdef __SUBSURFACE__
637                 /* bssrdf scatter to a different location on the same object, replacing
638                  * the closures with a diffuse BSDF */
639                 if(sd.flag & SD_BSSRDF) {
640                         float bssrdf_probability;
641                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
642
643                         /* modify throughput for picking bssrdf or bsdf */
644                         throughput *= bssrdf_probability;
645
646                         /* do bssrdf scatter step if we picked a bssrdf closure */
647                         if(sc) {
648                                 uint lcg_state = lcg_init(*rng + rng_offset + sample*0x68bc21eb);
649                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, false);
650                         }
651                 }
652 #endif
653
654 #ifdef __AO__
655                 /* ambient occlusion */
656                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
657                         float bsdf_u, bsdf_v;
658                         path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
659
660                         float ao_factor = kernel_data.background.ao_factor;
661                         float3 ao_N;
662                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
663                         float3 ao_D;
664                         float ao_pdf;
665
666                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
667
668                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
669                                 Ray light_ray;
670                                 float3 ao_shadow;
671
672                                 light_ray.P = ray_offset(sd.P, sd.Ng);
673                                 light_ray.D = ao_D;
674                                 light_ray.t = kernel_data.background.ao_distance;
675 #ifdef __OBJECT_MOTION__
676                                 light_ray.time = sd.time;
677 #endif
678                                 light_ray.dP = sd.dP;
679                                 light_ray.dD = differential3_zero();
680
681                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
682                                         path_radiance_accum_ao(L, throughput, ao_bsdf, ao_shadow, state.bounce);
683                         }
684                 }
685 #endif
686
687 #ifdef __EMISSION__
688                 if(kernel_data.integrator.use_direct_light) {
689                         /* sample illumination from lights to find path contribution */
690                         if(sd.flag & SD_BSDF_HAS_EVAL) {
691                                 float light_t = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT);
692 #ifdef __MULTI_CLOSURE__
693                                 float light_o = 0.0f;
694 #else
695                                 float light_o = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT_F);
696 #endif
697                                 float light_u, light_v;
698                                 path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT_U, &light_u, &light_v);
699
700                                 Ray light_ray;
701                                 BsdfEval L_light;
702                                 bool is_lamp;
703
704 #ifdef __OBJECT_MOTION__
705                                 light_ray.time = sd.time;
706 #endif
707
708                                 /* sample random light */
709                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
710                                         /* trace shadow ray */
711                                         float3 shadow;
712
713                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
714                                                 /* accumulate */
715                                                 path_radiance_accum_light(L, throughput, &L_light, shadow, 1.0f, state.bounce, is_lamp);
716                                         }
717                                 }
718                         }
719                 }
720 #endif
721
722                 /* no BSDF? we can stop here */
723                 if(!(sd.flag & SD_BSDF))
724                         break;
725
726                 /* sample BSDF */
727                 float bsdf_pdf;
728                 BsdfEval bsdf_eval;
729                 float3 bsdf_omega_in;
730                 differential3 bsdf_domega_in;
731                 float bsdf_u, bsdf_v;
732                 path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
733                 int label;
734
735                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
736                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
737
738                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
739                         break;
740
741                 /* modify throughput */
742                 path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
743
744                 /* set labels */
745                 if(!(label & LABEL_TRANSPARENT)) {
746                         ray_pdf = bsdf_pdf;
747 #ifdef __LAMP_MIS__
748                         ray_t = 0.0f;
749 #endif
750                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
751                 }
752
753                 /* update path state */
754                 path_state_next(kg, &state, label);
755
756                 /* setup ray */
757                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
758                 ray.D = bsdf_omega_in;
759                 ray.t = FLT_MAX;
760 #ifdef __RAY_DIFFERENTIALS__
761                 ray.dP = sd.dP;
762                 ray.dD = bsdf_domega_in;
763 #endif
764         }
765 }
766
767 __device_noinline void kernel_path_non_progressive_lighting(KernelGlobals *kg, RNG *rng, int sample,
768         ShaderData *sd, float3 throughput, float num_samples_adjust,
769         float min_ray_pdf, float ray_pdf, PathState state,
770         int rng_offset, PathRadiance *L, __global float *buffer)
771 {
772 #ifdef __CMJ__
773         int aa_samples = kernel_data.integrator.aa_samples;
774 #else
775         int aa_samples = 0;
776 #endif
777
778 #ifdef __AO__
779         /* ambient occlusion */
780         if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
781                 int num_samples = ceil_to_int(kernel_data.integrator.ao_samples*num_samples_adjust);
782                 float num_samples_inv = num_samples_adjust/num_samples;
783                 float ao_factor = kernel_data.background.ao_factor;
784                 float3 ao_N;
785                 float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
786
787                 for(int j = 0; j < num_samples; j++) {
788                         float bsdf_u, bsdf_v;
789                         path_rng_2D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
790
791                         float3 ao_D;
792                         float ao_pdf;
793
794                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
795
796                         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
797                                 Ray light_ray;
798                                 float3 ao_shadow;
799
800                                 light_ray.P = ray_offset(sd->P, sd->Ng);
801                                 light_ray.D = ao_D;
802                                 light_ray.t = kernel_data.background.ao_distance;
803 #ifdef __OBJECT_MOTION__
804                                 light_ray.time = sd->time;
805 #endif
806                                 light_ray.dP = sd->dP;
807                                 light_ray.dD = differential3_zero();
808
809                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
810                                         path_radiance_accum_ao(L, throughput*num_samples_inv, ao_bsdf, ao_shadow, state.bounce);
811                         }
812                 }
813         }
814 #endif
815
816
817 #ifdef __EMISSION__
818         /* sample illumination from lights to find path contribution */
819         if(sd->flag & SD_BSDF_HAS_EVAL) {
820                 Ray light_ray;
821                 BsdfEval L_light;
822                 bool is_lamp;
823
824 #ifdef __OBJECT_MOTION__
825                 light_ray.time = sd->time;
826 #endif
827
828                 /* lamp sampling */
829                 for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
830                         int num_samples = ceil_to_int(num_samples_adjust*light_select_num_samples(kg, i));
831                         float num_samples_inv = num_samples_adjust/(num_samples*kernel_data.integrator.num_all_lights);
832                         RNG lamp_rng = cmj_hash(*rng, i);
833
834                         if(kernel_data.integrator.pdf_triangles != 0.0f)
835                                 num_samples_inv *= 0.5f;
836
837                         for(int j = 0; j < num_samples; j++) {
838                                 float light_u, light_v;
839                                 path_rng_2D(kg, &lamp_rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT_U, &light_u, &light_v);
840
841                                 if(direct_emission(kg, sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
842                                         /* trace shadow ray */
843                                         float3 shadow;
844
845                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
846                                                 /* accumulate */
847                                                 path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state.bounce, is_lamp);
848                                         }
849                                 }
850                         }
851                 }
852
853                 /* mesh light sampling */
854                 if(kernel_data.integrator.pdf_triangles != 0.0f) {
855                         int num_samples = ceil_to_int(num_samples_adjust*kernel_data.integrator.mesh_light_samples);
856                         float num_samples_inv = num_samples_adjust/num_samples;
857
858                         if(kernel_data.integrator.num_all_lights)
859                                 num_samples_inv *= 0.5f;
860
861                         for(int j = 0; j < num_samples; j++) {
862                                 float light_t = path_rng_1D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT);
863                                 float light_u, light_v;
864                                 path_rng_2D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT_U, &light_u, &light_v);
865
866                                 /* only sample triangle lights */
867                                 if(kernel_data.integrator.num_all_lights)
868                                         light_t = 0.5f*light_t;
869
870                                 if(direct_emission(kg, sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
871                                         /* trace shadow ray */
872                                         float3 shadow;
873
874                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
875                                                 /* accumulate */
876                                                 path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state.bounce, is_lamp);
877                                         }
878                                 }
879                         }
880                 }
881         }
882 #endif
883
884         for(int i = 0; i< sd->num_closure; i++) {
885                 const ShaderClosure *sc = &sd->closure[i];
886
887                 if(!CLOSURE_IS_BSDF(sc->type))
888                         continue;
889                 /* transparency is not handled here, but in outer loop */
890                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
891                         continue;
892
893                 int num_samples;
894
895                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
896                         num_samples = kernel_data.integrator.diffuse_samples;
897                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
898                         num_samples = kernel_data.integrator.glossy_samples;
899                 else
900                         num_samples = kernel_data.integrator.transmission_samples;
901
902                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
903
904                 float num_samples_inv = num_samples_adjust/num_samples;
905                 RNG bsdf_rng = cmj_hash(*rng, i);
906
907                 for(int j = 0; j < num_samples; j++) {
908                         /* sample BSDF */
909                         float bsdf_pdf;
910                         BsdfEval bsdf_eval;
911                         float3 bsdf_omega_in;
912                         differential3 bsdf_domega_in;
913                         float bsdf_u, bsdf_v;
914                         path_rng_2D(kg, &bsdf_rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
915                         int label;
916
917                         label = shader_bsdf_sample_closure(kg, sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
918                                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
919
920                         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
921                                 continue;
922
923                         /* modify throughput */
924                         float3 tp = throughput;
925                         path_radiance_bsdf_bounce(L, &tp, &bsdf_eval, bsdf_pdf, state.bounce, label);
926
927                         /* set labels */
928                         float min_ray_pdf = fminf(bsdf_pdf, FLT_MAX);
929
930                         /* modify path state */
931                         PathState ps = state;
932                         path_state_next(kg, &ps, label);
933
934                         /* setup ray */
935                         Ray bsdf_ray;
936
937                         bsdf_ray.P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
938                         bsdf_ray.D = bsdf_omega_in;
939                         bsdf_ray.t = FLT_MAX;
940 #ifdef __RAY_DIFFERENTIALS__
941                         bsdf_ray.dP = sd->dP;
942                         bsdf_ray.dD = bsdf_domega_in;
943 #endif
944 #ifdef __OBJECT_MOTION__
945                         bsdf_ray.time = sd->time;
946 #endif
947
948                         kernel_path_indirect(kg, rng, sample*num_samples + j, bsdf_ray, buffer,
949                                 tp*num_samples_inv, num_samples, aa_samples*num_samples,
950                                 min_ray_pdf, bsdf_pdf, ps, rng_offset+PRNG_BOUNCE_NUM, 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 __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
961 {
962         /* initialize */
963         PathRadiance L;
964         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
965         float L_transparent = 0.0f;
966
967         path_radiance_init(&L, kernel_data.film.use_light_pass);
968
969         float ray_pdf = 0.0f;
970         PathState state;
971         int rng_offset = PRNG_BASE_NUM;
972 #ifdef __CMJ__
973         int aa_samples = kernel_data.integrator.aa_samples;
974 #else
975         int aa_samples = 0;
976 #endif
977
978         path_state_init(&state);
979
980         for(;; rng_offset += PRNG_BOUNCE_NUM) {
981                 /* intersect scene */
982                 Intersection isect;
983                 uint visibility = path_state_ray_visibility(kg, &state);
984
985 #ifdef __HAIR__
986                 float difl = 0.0f, extmax = 0.0f;
987                 uint lcg_state = 0;
988
989                 if(kernel_data.bvh.have_curves) {
990                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
991                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
992                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
993                                 difl = kernel_data.curve_kernel_data.minimum_width * len(pixdiff) * 0.5f;
994                         }
995
996                         extmax = kernel_data.curve_kernel_data.maximum_width;
997                         lcg_state = lcg_init(*rng + rng_offset + sample*0x51633e2d);
998                 }
999
1000                 if(!scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax)) {
1001 #else
1002                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
1003 #endif
1004                         /* eval background shader if nothing hit */
1005                         if(kernel_data.background.transparent) {
1006                                 L_transparent += average(throughput);
1007
1008 #ifdef __PASSES__
1009                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
1010 #endif
1011                                         break;
1012                         }
1013
1014 #ifdef __BACKGROUND__
1015                         /* sample background shader */
1016                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf, state.bounce);
1017                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
1018 #endif
1019
1020                         break;
1021                 }
1022
1023                 /* setup shading */
1024                 ShaderData sd;
1025                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
1026                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
1027                 shader_merge_closures(kg, &sd);
1028
1029                 /* holdout */
1030 #ifdef __HOLDOUT__
1031                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
1032                         if(kernel_data.background.transparent) {
1033                                 float3 holdout_weight;
1034                                 
1035                                 if(sd.flag & SD_HOLDOUT_MASK)
1036                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
1037                                 else
1038                                         holdout_weight = shader_holdout_eval(kg, &sd);
1039
1040                                 /* any throughput is ok, should all be identical here */
1041                                 L_transparent += average(holdout_weight*throughput);
1042                         }
1043
1044                         if(sd.flag & SD_HOLDOUT_MASK)
1045                                 break;
1046                 }
1047 #endif
1048
1049                 /* holdout mask objects do not write data passes */
1050                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
1051
1052 #ifdef __EMISSION__
1053                 /* emission */
1054                 if(sd.flag & SD_EMISSION) {
1055                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
1056                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
1057                 }
1058 #endif
1059
1060                 /* transparency termination */
1061                 if(state.flag & PATH_RAY_TRANSPARENT) {
1062                         /* path termination. this is a strange place to put the termination, it's
1063                          * mainly due to the mixed in MIS that we use. gives too many unneeded
1064                          * shader evaluations, only need emission if we are going to terminate */
1065                         float probability = path_state_terminate_probability(kg, &state, throughput);
1066
1067                         if(probability == 0.0f) {
1068                                 break;
1069                         }
1070                         else if(probability != 1.0f) {
1071                                 float terminate = path_rng_1D(kg, rng, sample, aa_samples, rng_offset + PRNG_TERMINATE);
1072
1073                                 if(terminate >= probability)
1074                                         break;
1075
1076                                 throughput /= probability;
1077                         }
1078                 }
1079
1080 #ifdef __SUBSURFACE__
1081                 /* bssrdf scatter to a different location on the same object */
1082                 if(sd.flag & SD_BSSRDF) {
1083                         for(int i = 0; i< sd.num_closure; i++) {
1084                                 ShaderClosure *sc = &sd.closure[i];
1085
1086                                 if(!CLOSURE_IS_BSSRDF(sc->type))
1087                                         continue;
1088
1089                                 /* set up random number generator */
1090                                 uint lcg_state = lcg_init(*rng + rng_offset + sample*0x68bc21eb);
1091                                 int num_samples = kernel_data.integrator.subsurface_samples;
1092                                 float num_samples_inv = 1.0f/num_samples;
1093
1094                                 /* do subsurface scatter step with copy of shader data, this will
1095                                  * replace the BSSRDF with a diffuse BSDF closure */
1096                                 for(int j = 0; j < num_samples; j++) {
1097                                         ShaderData bssrdf_sd = sd;
1098                                         subsurface_scatter_step(kg, &bssrdf_sd, state.flag, sc, &lcg_state, true);
1099
1100                                         /* compute lighting with the BSDF closure */
1101                                         kernel_path_non_progressive_lighting(kg, rng, sample*num_samples + j,
1102                                                 &bssrdf_sd, throughput, num_samples_inv,
1103                                                 ray_pdf, ray_pdf, state, rng_offset, &L, buffer);
1104                                 }
1105                         }
1106                 }
1107 #endif
1108
1109                 /* lighting */
1110                 kernel_path_non_progressive_lighting(kg, rng, sample, &sd, throughput,
1111                         1.0f, ray_pdf, ray_pdf, state, rng_offset, &L, buffer);
1112
1113                 /* continue in case of transparency */
1114                 throughput *= shader_bsdf_transparency(kg, &sd);
1115
1116                 if(is_zero(throughput))
1117                         break;
1118
1119                 path_state_next(kg, &state, LABEL_TRANSPARENT);
1120                 ray.P = ray_offset(sd.P, -sd.Ng);
1121                 ray.t -= sd.ray_length; /* clipping works through transparent */
1122         }
1123
1124         float3 L_sum = path_radiance_sum(kg, &L);
1125
1126 #ifdef __CLAMP_SAMPLE__
1127         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
1128 #endif
1129
1130         kernel_write_light_passes(kg, buffer, &L, sample);
1131
1132         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1133 }
1134
1135 #endif
1136
1137 __device void kernel_path_trace(KernelGlobals *kg,
1138         __global float *buffer, __global uint *rng_state,
1139         int sample, int x, int y, int offset, int stride)
1140 {
1141         /* buffer offset */
1142         int index = offset + x + y*stride;
1143         int pass_stride = kernel_data.film.pass_stride;
1144
1145         rng_state += index;
1146         buffer += index*pass_stride;
1147
1148         /* initialize random numbers */
1149         RNG rng;
1150
1151         float filter_u;
1152         float filter_v;
1153 #ifdef __CMJ__
1154         int num_samples = kernel_data.integrator.aa_samples;
1155 #else
1156         int num_samples = 0;
1157 #endif
1158
1159         path_rng_init(kg, rng_state, sample, num_samples, &rng, x, y, &filter_u, &filter_v);
1160
1161         /* sample camera ray */
1162         Ray ray;
1163
1164         float lens_u = 0.0f, lens_v = 0.0f;
1165
1166         if(kernel_data.cam.aperturesize > 0.0f)
1167                 path_rng_2D(kg, &rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1168
1169         float time = 0.0f;
1170
1171 #ifdef __CAMERA_MOTION__
1172         if(kernel_data.cam.shuttertime != -1.0f)
1173                 time = path_rng_1D(kg, &rng, sample, num_samples, PRNG_TIME);
1174 #endif
1175
1176         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, &ray);
1177
1178         /* integrate */
1179         float4 L;
1180
1181         if (ray.t != 0.0f) {
1182 #ifdef __NON_PROGRESSIVE__
1183                 if(kernel_data.integrator.progressive)
1184 #endif
1185                         L = kernel_path_progressive(kg, &rng, sample, ray, buffer);
1186 #ifdef __NON_PROGRESSIVE__
1187                 else
1188                         L = kernel_path_non_progressive(kg, &rng, sample, ray, buffer);
1189 #endif
1190         }
1191         else
1192                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1193
1194         /* accumulate result in output buffer */
1195         kernel_write_pass_float4(buffer, sample, L);
1196
1197         path_rng_end(kg, rng_state, rng);
1198 }
1199
1200 CCL_NAMESPACE_END
1201