Cycles: experimental correlated multi-jittered sampling pattern that can be used
[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);
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         int num_samples = kernel_data.integrator.aa_samples;
253
254         path_state_init(&state);
255
256         /* path iteration */
257         for(;; rng_offset += PRNG_BOUNCE_NUM) {
258                 /* intersect scene */
259                 Intersection isect;
260                 uint visibility = path_state_ray_visibility(kg, &state);
261
262 #ifdef __HAIR__
263                 float difl = 0.0f, extmax = 0.0f;
264                 uint lcg_state = 0;
265
266                 if(kernel_data.bvh.have_curves) {
267                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
268                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
269                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
270                                 difl = kernel_data.curve_kernel_data.minimum_width * len(pixdiff) * 0.5f;
271                         }
272
273                         extmax = kernel_data.curve_kernel_data.maximum_width;
274                         lcg_state = lcg_init(rng + rng_offset + sample*0x51633e2d);
275                 }
276
277                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
278 #else
279                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
280 #endif
281
282 #ifdef __LAMP_MIS__
283                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
284                         /* ray starting from previous non-transparent bounce */
285                         Ray light_ray;
286
287                         light_ray.P = ray.P - ray_t*ray.D;
288                         ray_t += isect.t;
289                         light_ray.D = ray.D;
290                         light_ray.t = ray_t;
291                         light_ray.time = ray.time;
292                         light_ray.dD = ray.dD;
293                         light_ray.dP = ray.dP;
294
295                         /* intersect with lamp */
296                         float light_t = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT);
297                         float3 emission;
298
299                         if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission))
300                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
301                 }
302 #endif
303
304                 if(!hit) {
305                         /* eval background shader if nothing hit */
306                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
307                                 L_transparent += average(throughput);
308
309 #ifdef __PASSES__
310                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
311 #endif
312                                         break;
313                         }
314
315 #ifdef __BACKGROUND__
316                         /* sample background shader */
317                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
318                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
319 #endif
320
321                         break;
322                 }
323
324                 /* setup shading */
325                 ShaderData sd;
326                 shader_setup_from_ray(kg, &sd, &isect, &ray);
327                 float rbsdf = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF);
328                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
329
330                 /* holdout */
331 #ifdef __HOLDOUT__
332                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
333                         if(kernel_data.background.transparent) {
334                                 float3 holdout_weight;
335                                 
336                                 if(sd.flag & SD_HOLDOUT_MASK)
337                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
338                                 else
339                                         holdout_weight = shader_holdout_eval(kg, &sd);
340
341                                 /* any throughput is ok, should all be identical here */
342                                 L_transparent += average(holdout_weight*throughput);
343                         }
344
345                         if(sd.flag & SD_HOLDOUT_MASK)
346                                 break;
347                 }
348 #endif
349
350                 /* holdout mask objects do not write data passes */
351                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
352
353                 /* blurring of bsdf after bounces, for rays that have a small likelihood
354                  * of following this particular path (diffuse, rough glossy) */
355                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
356                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
357
358                         if(blur_pdf < 1.0f) {
359                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
360                                 shader_bsdf_blur(kg, &sd, blur_roughness);
361                         }
362                 }
363
364 #ifdef __EMISSION__
365                 /* emission */
366                 if(sd.flag & SD_EMISSION) {
367                         /* todo: is isect.t wrong here for transparent surfaces? */
368                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
369                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
370                 }
371 #endif
372
373                 /* path termination. this is a strange place to put the termination, it's
374                  * mainly due to the mixed in MIS that we use. gives too many unneeded
375                  * shader evaluations, only need emission if we are going to terminate */
376                 float probability = path_state_terminate_probability(kg, &state, throughput);
377
378                 if(probability == 0.0f) {
379                         break;
380                 }
381                 else if(probability != 1.0f) {
382                         float terminate = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_TERMINATE);
383
384                         if(terminate >= probability)
385                                 break;
386
387                         throughput /= probability;
388                 }
389
390 #ifdef __SUBSURFACE__
391                 /* bssrdf scatter to a different location on the same object, replacing
392                  * the closures with a diffuse BSDF */
393                 if(sd.flag & SD_BSSRDF) {
394                         float bssrdf_probability;
395                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
396
397                         /* modify throughput for picking bssrdf or bsdf */
398                         throughput *= bssrdf_probability;
399
400                         /* do bssrdf scatter step if we picked a bssrdf closure */
401                         if(sc) {
402                                 uint lcg_state = lcg_init(rng + rng_offset + sample*0x68bc21eb);
403                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, false);
404                         }
405                 }
406 #endif
407
408 #ifdef __AO__
409                 /* ambient occlusion */
410                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
411                         /* todo: solve correlation */
412                         float2 bsdf_uv = path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF_U);
413                         float bsdf_u = bsdf_uv.x;
414                         float bsdf_v = bsdf_uv.y;
415
416                         float ao_factor = kernel_data.background.ao_factor;
417                         float3 ao_N;
418                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
419                         float3 ao_D;
420                         float ao_pdf;
421
422                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
423
424                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
425                                 Ray light_ray;
426                                 float3 ao_shadow;
427
428                                 light_ray.P = ray_offset(sd.P, sd.Ng);
429                                 light_ray.D = ao_D;
430                                 light_ray.t = kernel_data.background.ao_distance;
431 #ifdef __OBJECT_MOTION__
432                                 light_ray.time = sd.time;
433 #endif
434                                 light_ray.dP = sd.dP;
435                                 light_ray.dD = differential3_zero();
436
437                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
438                                         path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
439                         }
440                 }
441 #endif
442
443 #ifdef __EMISSION__
444                 if(kernel_data.integrator.use_direct_light) {
445                         /* sample illumination from lights to find path contribution */
446                         if(sd.flag & SD_BSDF_HAS_EVAL) {
447                                 float light_t = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT);
448 #ifdef __MULTI_CLOSURE__
449                                 float light_o = 0.0f;
450 #else
451                                 float light_o = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT_F);
452 #endif
453                                 float2 light_uv = path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT_U);
454                                 float light_u = light_uv.x;
455                                 float light_v = light_uv.y;
456
457                                 Ray light_ray;
458                                 BsdfEval L_light;
459                                 bool is_lamp;
460
461 #ifdef __OBJECT_MOTION__
462                                 light_ray.time = sd.time;
463 #endif
464
465                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
466                                         /* trace shadow ray */
467                                         float3 shadow;
468
469                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
470                                                 /* accumulate */
471                                                 path_radiance_accum_light(&L, throughput, &L_light, shadow, 1.0f, state.bounce, is_lamp);
472                                         }
473                                 }
474                         }
475                 }
476 #endif
477
478                 /* no BSDF? we can stop here */
479                 if(!(sd.flag & SD_BSDF))
480                         break;
481
482                 /* sample BSDF */
483                 float bsdf_pdf;
484                 BsdfEval bsdf_eval;
485                 float3 bsdf_omega_in;
486                 differential3 bsdf_domega_in;
487                 float2 bsdf_uv = path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF_U);
488                 float bsdf_u = bsdf_uv.x;
489                 float bsdf_v = bsdf_uv.y;
490                 int label;
491
492                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
493                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
494
495                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
496                         break;
497
498                 /* modify throughput */
499                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
500
501                 /* set labels */
502                 if(!(label & LABEL_TRANSPARENT)) {
503                         ray_pdf = bsdf_pdf;
504 #ifdef __LAMP_MIS__
505                         ray_t = 0.0f;
506 #endif
507                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
508                 }
509
510                 /* update path state */
511                 path_state_next(kg, &state, label);
512
513                 /* setup ray */
514                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
515                 ray.D = bsdf_omega_in;
516
517                 if(state.bounce == 0)
518                         ray.t -= sd.ray_length; /* clipping works through transparent */
519                 else
520                         ray.t = FLT_MAX;
521
522 #ifdef __RAY_DIFFERENTIALS__
523                 ray.dP = sd.dP;
524                 ray.dD = bsdf_domega_in;
525 #endif
526         }
527
528         float3 L_sum = path_radiance_sum(kg, &L);
529
530 #ifdef __CLAMP_SAMPLE__
531         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
532 #endif
533
534         kernel_write_light_passes(kg, buffer, &L, sample);
535
536         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
537 }
538
539 #ifdef __NON_PROGRESSIVE__
540
541 __device void kernel_path_indirect(KernelGlobals *kg, RNG rng, int sample, Ray ray, __global float *buffer,
542         float3 throughput, int num_samples, int num_total_samples,
543         float min_ray_pdf, float ray_pdf, PathState state, int rng_offset, PathRadiance *L)
544 {
545 #ifdef __LAMP_MIS__
546         float ray_t = 0.0f;
547 #endif
548
549         /* path iteration */
550         for(;; rng_offset += PRNG_BOUNCE_NUM) {
551                 /* intersect scene */
552                 Intersection isect;
553                 uint visibility = path_state_ray_visibility(kg, &state);
554 #ifdef __HAIR__
555                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
556 #else
557                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
558 #endif
559
560 #ifdef __LAMP_MIS__
561                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
562                         /* ray starting from previous non-transparent bounce */
563                         Ray light_ray;
564
565                         light_ray.P = ray.P - ray_t*ray.D;
566                         ray_t += isect.t;
567                         light_ray.D = ray.D;
568                         light_ray.t = ray_t;
569                         light_ray.time = ray.time;
570                         light_ray.dD = ray.dD;
571                         light_ray.dP = ray.dP;
572
573                         /* intersect with lamp */
574                         float light_t = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT);
575                         float3 emission;
576
577                         if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission))
578                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
579                 }
580 #endif
581
582                 if(!hit) {
583 #ifdef __BACKGROUND__
584                         /* sample background shader */
585                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
586                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
587 #endif
588
589                         break;
590                 }
591
592                 /* setup shading */
593                 ShaderData sd;
594                 shader_setup_from_ray(kg, &sd, &isect, &ray);
595                 float rbsdf = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF);
596                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
597                 shader_merge_closures(kg, &sd);
598
599                 /* blurring of bsdf after bounces, for rays that have a small likelihood
600                  * of following this particular path (diffuse, rough glossy) */
601                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
602                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
603
604                         if(blur_pdf < 1.0f) {
605                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
606                                 shader_bsdf_blur(kg, &sd, blur_roughness);
607                         }
608                 }
609
610 #ifdef __EMISSION__
611                 /* emission */
612                 if(sd.flag & SD_EMISSION) {
613                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
614                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
615                 }
616 #endif
617
618                 /* path termination. this is a strange place to put the termination, it's
619                  * mainly due to the mixed in MIS that we use. gives too many unneeded
620                  * shader evaluations, only need emission if we are going to terminate */
621                 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
622
623                 if(probability == 0.0f) {
624                         break;
625                 }
626                 else if(probability != 1.0f) {
627                         float terminate = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_TERMINATE);
628
629                         if(terminate >= probability)
630                                 break;
631
632                         throughput /= probability;
633                 }
634
635 #ifdef __SUBSURFACE__
636                 /* bssrdf scatter to a different location on the same object, replacing
637                  * the closures with a diffuse BSDF */
638                 if(sd.flag & SD_BSSRDF) {
639                         float bssrdf_probability;
640                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
641
642                         /* modify throughput for picking bssrdf or bsdf */
643                         throughput *= bssrdf_probability;
644
645                         /* do bssrdf scatter step if we picked a bssrdf closure */
646                         if(sc) {
647                                 uint lcg_state = lcg_init(rng + rng_offset + sample*0x68bc21eb);
648                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, false);
649                         }
650                 }
651 #endif
652
653 #ifdef __AO__
654                 /* ambient occlusion */
655                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
656                         /* todo: solve correlation */
657                         float2 bsdf_uv = path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF_U);
658                         float bsdf_u = bsdf_uv.x;
659                         float bsdf_v = bsdf_uv.y;
660
661                         float ao_factor = kernel_data.background.ao_factor;
662                         float3 ao_N;
663                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
664                         float3 ao_D;
665                         float ao_pdf;
666
667                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
668
669                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
670                                 Ray light_ray;
671                                 float3 ao_shadow;
672
673                                 light_ray.P = ray_offset(sd.P, sd.Ng);
674                                 light_ray.D = ao_D;
675                                 light_ray.t = kernel_data.background.ao_distance;
676 #ifdef __OBJECT_MOTION__
677                                 light_ray.time = sd.time;
678 #endif
679                                 light_ray.dP = sd.dP;
680                                 light_ray.dD = differential3_zero();
681
682                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
683                                         path_radiance_accum_ao(L, throughput, ao_bsdf, ao_shadow, state.bounce);
684                         }
685                 }
686 #endif
687
688 #ifdef __EMISSION__
689                 if(kernel_data.integrator.use_direct_light) {
690                         /* sample illumination from lights to find path contribution */
691                         if(sd.flag & SD_BSDF_HAS_EVAL) {
692                                 float light_t = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT);
693 #ifdef __MULTI_CLOSURE__
694                                 float light_o = 0.0f;
695 #else
696                                 float light_o = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT_F);
697 #endif
698                                 float2 light_uv = path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT_U);
699                                 float light_u = light_uv.x;
700                                 float light_v = light_uv.y;
701
702                                 Ray light_ray;
703                                 BsdfEval L_light;
704                                 bool is_lamp;
705
706 #ifdef __OBJECT_MOTION__
707                                 light_ray.time = sd.time;
708 #endif
709
710                                 /* sample random light */
711                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
712                                         /* trace shadow ray */
713                                         float3 shadow;
714
715                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
716                                                 /* accumulate */
717                                                 path_radiance_accum_light(L, throughput, &L_light, shadow, 1.0f, state.bounce, is_lamp);
718                                         }
719                                 }
720                         }
721                 }
722 #endif
723
724                 /* no BSDF? we can stop here */
725                 if(!(sd.flag & SD_BSDF))
726                         break;
727
728                 /* sample BSDF */
729                 float bsdf_pdf;
730                 BsdfEval bsdf_eval;
731                 float3 bsdf_omega_in;
732                 differential3 bsdf_domega_in;
733                 float2 bsdf_uv = path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF_U);
734                 float bsdf_u = bsdf_uv.x;
735                 float bsdf_v = bsdf_uv.y;
736                 int label;
737
738                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
739                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
740
741                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
742                         break;
743
744                 /* modify throughput */
745                 path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
746
747                 /* set labels */
748                 if(!(label & LABEL_TRANSPARENT)) {
749                         ray_pdf = bsdf_pdf;
750 #ifdef __LAMP_MIS__
751                         ray_t = 0.0f;
752 #endif
753                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
754                 }
755
756                 /* update path state */
757                 path_state_next(kg, &state, label);
758
759                 /* setup ray */
760                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
761                 ray.D = bsdf_omega_in;
762                 ray.t = FLT_MAX;
763 #ifdef __RAY_DIFFERENTIALS__
764                 ray.dP = sd.dP;
765                 ray.dD = bsdf_domega_in;
766 #endif
767         }
768 }
769
770 __device_noinline void kernel_path_non_progressive_lighting(KernelGlobals *kg, RNG rng, int sample,
771         ShaderData *sd, float3 throughput, float num_samples_adjust,
772         float min_ray_pdf, float ray_pdf, PathState state,
773         int rng_offset, PathRadiance *L, __global float *buffer)
774 {
775         int aa_samples = kernel_data.integrator.aa_samples;
776
777 #ifdef __AO__
778         /* ambient occlusion */
779         if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
780                 int num_samples = ceil_to_int(kernel_data.integrator.ao_samples*num_samples_adjust);
781                 float num_samples_inv = num_samples_adjust/num_samples;
782                 float ao_factor = kernel_data.background.ao_factor;
783                 float3 ao_N;
784                 float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
785
786                 for(int j = 0; j < num_samples; j++) {
787                         /* todo: solve correlation */
788                         float2 bsdf_uv = path_rng_2D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_BSDF_U);
789                         float bsdf_u = bsdf_uv.x;
790                         float bsdf_v = bsdf_uv.y;
791
792                         float3 ao_D;
793                         float ao_pdf;
794
795                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
796
797                         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
798                                 Ray light_ray;
799                                 float3 ao_shadow;
800
801                                 light_ray.P = ray_offset(sd->P, sd->Ng);
802                                 light_ray.D = ao_D;
803                                 light_ray.t = kernel_data.background.ao_distance;
804 #ifdef __OBJECT_MOTION__
805                                 light_ray.time = sd->time;
806 #endif
807                                 light_ray.dP = sd->dP;
808                                 light_ray.dD = differential3_zero();
809
810                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
811                                         path_radiance_accum_ao(L, throughput*num_samples_inv, ao_bsdf, ao_shadow, state.bounce);
812                         }
813                 }
814         }
815 #endif
816
817
818 #ifdef __EMISSION__
819         /* sample illumination from lights to find path contribution */
820         if(sd->flag & SD_BSDF_HAS_EVAL) {
821                 Ray light_ray;
822                 BsdfEval L_light;
823                 bool is_lamp;
824
825 #ifdef __OBJECT_MOTION__
826                 light_ray.time = sd->time;
827 #endif
828
829                 /* lamp sampling */
830                 for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
831                         int num_samples = ceil_to_int(num_samples_adjust*light_select_num_samples(kg, i));
832                         float num_samples_inv = num_samples_adjust/(num_samples*kernel_data.integrator.num_all_lights);
833                         RNG lamp_rng = cmj_hash(rng, i);
834
835                         if(kernel_data.integrator.pdf_triangles != 0.0f)
836                                 num_samples_inv *= 0.5f;
837
838                         for(int j = 0; j < num_samples; j++) {
839                                 float2 light_uv = path_rng_2D(kg, lamp_rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT_U);
840                                 float light_u = light_uv.x;
841                                 float light_v = light_uv.y;
842
843                                 if(direct_emission(kg, sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
844                                         /* trace shadow ray */
845                                         float3 shadow;
846
847                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
848                                                 /* accumulate */
849                                                 path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state.bounce, is_lamp);
850                                         }
851                                 }
852                         }
853                 }
854
855                 /* mesh light sampling */
856                 if(kernel_data.integrator.pdf_triangles != 0.0f) {
857                         int num_samples = ceil_to_int(num_samples_adjust*kernel_data.integrator.mesh_light_samples);
858                         float num_samples_inv = num_samples_adjust/num_samples;
859
860                         if(kernel_data.integrator.num_all_lights)
861                                 num_samples_inv *= 0.5f;
862
863                         for(int j = 0; j < num_samples; j++) {
864                                 float light_t = path_rng_1D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT);
865                                 float2 light_uv = path_rng_2D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT_U);
866                                 float light_u = light_uv.x;
867                                 float light_v = light_uv.y;
868
869                                 /* only sample triangle lights */
870                                 if(kernel_data.integrator.num_all_lights)
871                                         light_t = 0.5f*light_t;
872
873                                 if(direct_emission(kg, sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
874                                         /* trace shadow ray */
875                                         float3 shadow;
876
877                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
878                                                 /* accumulate */
879                                                 path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state.bounce, is_lamp);
880                                         }
881                                 }
882                         }
883                 }
884         }
885 #endif
886
887         for(int i = 0; i< sd->num_closure; i++) {
888                 const ShaderClosure *sc = &sd->closure[i];
889
890                 if(!CLOSURE_IS_BSDF(sc->type))
891                         continue;
892                 /* transparency is not handled here, but in outer loop */
893                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
894                         continue;
895
896                 int num_samples;
897
898                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
899                         num_samples = kernel_data.integrator.diffuse_samples;
900                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
901                         num_samples = kernel_data.integrator.glossy_samples;
902                 else
903                         num_samples = kernel_data.integrator.transmission_samples;
904
905                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
906
907                 float num_samples_inv = num_samples_adjust/num_samples;
908                 RNG bsdf_rng = cmj_hash(rng, i);
909
910                 for(int j = 0; j < num_samples; j++) {
911                         /* sample BSDF */
912                         float bsdf_pdf;
913                         BsdfEval bsdf_eval;
914                         float3 bsdf_omega_in;
915                         differential3 bsdf_domega_in;
916                         float2 bsdf_uv = path_rng_2D(kg, bsdf_rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_BSDF_U);
917                         float bsdf_u = bsdf_uv.x;
918                         float bsdf_v = bsdf_uv.y;
919                         int label;
920
921                         label = shader_bsdf_sample_closure(kg, sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
922                                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
923
924                         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
925                                 continue;
926
927                         /* modify throughput */
928                         float3 tp = throughput;
929                         path_radiance_bsdf_bounce(L, &tp, &bsdf_eval, bsdf_pdf, state.bounce, label);
930
931                         /* set labels */
932                         float min_ray_pdf = FLT_MAX;
933
934                         if(!(label & LABEL_TRANSPARENT))
935                                 min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
936
937                         /* modify path state */
938                         PathState ps = state;
939                         path_state_next(kg, &ps, label);
940
941                         /* setup ray */
942                         Ray bsdf_ray;
943
944                         bsdf_ray.P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
945                         bsdf_ray.D = bsdf_omega_in;
946                         bsdf_ray.t = FLT_MAX;
947 #ifdef __RAY_DIFFERENTIALS__
948                         bsdf_ray.dP = sd->dP;
949                         bsdf_ray.dD = bsdf_domega_in;
950 #endif
951 #ifdef __OBJECT_MOTION__
952                         bsdf_ray.time = sd->time;
953 #endif
954
955                         kernel_path_indirect(kg, rng, sample*num_samples + j, bsdf_ray, buffer,
956                                 tp*num_samples_inv, num_samples, aa_samples*num_samples,
957                                 min_ray_pdf, bsdf_pdf, ps, rng_offset+PRNG_BOUNCE_NUM, L);
958
959                         /* for render passes, sum and reset indirect light pass variables
960                          * for the next samples */
961                         path_radiance_sum_indirect(L);
962                         path_radiance_reset_indirect(L);
963                 }
964         }
965 }
966
967 __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG rng, int sample, Ray ray, __global float *buffer)
968 {
969         /* initialize */
970         PathRadiance L;
971         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
972         float L_transparent = 0.0f;
973
974         path_radiance_init(&L, kernel_data.film.use_light_pass);
975
976         float ray_pdf = 0.0f;
977         PathState state;
978         int rng_offset = PRNG_BASE_NUM;
979         int aa_samples = kernel_data.integrator.aa_samples;
980
981         path_state_init(&state);
982
983         for(;; rng_offset += PRNG_BOUNCE_NUM) {
984                 /* intersect scene */
985                 Intersection isect;
986                 uint visibility = path_state_ray_visibility(kg, &state);
987
988 #ifdef __HAIR__
989                 float difl = 0.0f, extmax = 0.0f;
990                 uint lcg_state = 0;
991
992                 if(kernel_data.bvh.have_curves) {
993                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
994                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
995                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
996                                 difl = kernel_data.curve_kernel_data.minimum_width * len(pixdiff) * 0.5f;
997                         }
998
999                         extmax = kernel_data.curve_kernel_data.maximum_width;
1000                         lcg_state = lcg_init(rng + rng_offset + sample*0x51633e2d);
1001                 }
1002
1003                 if(!scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax)) {
1004 #else
1005                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
1006 #endif
1007                         /* eval background shader if nothing hit */
1008                         if(kernel_data.background.transparent) {
1009                                 L_transparent += average(throughput);
1010
1011 #ifdef __PASSES__
1012                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
1013 #endif
1014                                         break;
1015                         }
1016
1017 #ifdef __BACKGROUND__
1018                         /* sample background shader */
1019                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
1020                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
1021 #endif
1022
1023                         break;
1024                 }
1025
1026                 /* setup shading */
1027                 ShaderData sd;
1028                 shader_setup_from_ray(kg, &sd, &isect, &ray);
1029                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
1030                 shader_merge_closures(kg, &sd);
1031
1032                 /* holdout */
1033 #ifdef __HOLDOUT__
1034                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
1035                         if(kernel_data.background.transparent) {
1036                                 float3 holdout_weight;
1037                                 
1038                                 if(sd.flag & SD_HOLDOUT_MASK)
1039                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
1040                                 else
1041                                         holdout_weight = shader_holdout_eval(kg, &sd);
1042
1043                                 /* any throughput is ok, should all be identical here */
1044                                 L_transparent += average(holdout_weight*throughput);
1045                         }
1046
1047                         if(sd.flag & SD_HOLDOUT_MASK)
1048                                 break;
1049                 }
1050 #endif
1051
1052                 /* holdout mask objects do not write data passes */
1053                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
1054
1055 #ifdef __EMISSION__
1056                 /* emission */
1057                 if(sd.flag & SD_EMISSION) {
1058                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
1059                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
1060                 }
1061 #endif
1062
1063                 /* transparency termination */
1064                 if(state.flag & PATH_RAY_TRANSPARENT) {
1065                         /* path termination. this is a strange place to put the termination, it's
1066                          * mainly due to the mixed in MIS that we use. gives too many unneeded
1067                          * shader evaluations, only need emission if we are going to terminate */
1068                         float probability = path_state_terminate_probability(kg, &state, throughput);
1069
1070                         if(probability == 0.0f) {
1071                                 break;
1072                         }
1073                         else if(probability != 1.0f) {
1074                                 float terminate = path_rng_1D(kg, rng, sample, aa_samples, rng_offset + PRNG_TERMINATE);
1075
1076                                 if(terminate >= probability)
1077                                         break;
1078
1079                                 throughput /= probability;
1080                         }
1081                 }
1082
1083 #ifdef __SUBSURFACE__
1084                 /* bssrdf scatter to a different location on the same object */
1085                 if(sd.flag & SD_BSSRDF) {
1086                         for(int i = 0; i< sd.num_closure; i++) {
1087                                 ShaderClosure *sc = &sd.closure[i];
1088
1089                                 if(!CLOSURE_IS_BSSRDF(sc->type))
1090                                         continue;
1091
1092                                 /* set up random number generator */
1093                                 uint lcg_state = lcg_init(rng + rng_offset + sample*0x68bc21eb);
1094                                 int num_samples = kernel_data.integrator.subsurface_samples;
1095                                 float num_samples_inv = 1.0f/num_samples;
1096
1097                                 /* do subsurface scatter step with copy of shader data, this will
1098                                  * replace the BSSRDF with a diffuse BSDF closure */
1099                                 for(int j = 0; j < num_samples; j++) {
1100                                         ShaderData bssrdf_sd = sd;
1101                                         subsurface_scatter_step(kg, &bssrdf_sd, state.flag, sc, &lcg_state, true);
1102
1103                                         /* compute lighting with the BSDF closure */
1104                                         kernel_path_non_progressive_lighting(kg, rng, sample*num_samples + j,
1105                                                 &bssrdf_sd, throughput, num_samples_inv,
1106                                                 ray_pdf, ray_pdf, state, rng_offset, &L, buffer);
1107                                 }
1108                         }
1109                 }
1110 #endif
1111
1112                 /* lighting */
1113                 kernel_path_non_progressive_lighting(kg, rng, sample, &sd, throughput,
1114                         1.0f, ray_pdf, ray_pdf, state, rng_offset, &L, buffer);
1115
1116                 /* continue in case of transparency */
1117                 throughput *= shader_bsdf_transparency(kg, &sd);
1118
1119                 if(is_zero(throughput))
1120                         break;
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         float3 L_sum = path_radiance_sum(kg, &L);
1128
1129 #ifdef __CLAMP_SAMPLE__
1130         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
1131 #endif
1132
1133         kernel_write_light_passes(kg, buffer, &L, sample);
1134
1135         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1136 }
1137
1138 #endif
1139
1140 __device void kernel_path_trace(KernelGlobals *kg,
1141         __global float *buffer, __global uint *rng_state,
1142         int sample, int x, int y, int offset, int stride)
1143 {
1144         /* buffer offset */
1145         int index = offset + x + y*stride;
1146         int pass_stride = kernel_data.film.pass_stride;
1147
1148         rng_state += index;
1149         buffer += index*pass_stride;
1150
1151         /* initialize random numbers */
1152         RNG rng;
1153
1154         float filter_u;
1155         float filter_v;
1156         int num_samples = kernel_data.integrator.aa_samples;
1157
1158         path_rng_init(kg, rng_state, sample, num_samples, &rng, x, y, &filter_u, &filter_v);
1159
1160         /* sample camera ray */
1161         Ray ray;
1162
1163         float lens_u = 0.0f, lens_v = 0.0f;
1164
1165         if(kernel_data.cam.aperturesize > 0.0f) {
1166                 float2 lens_uv = path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U);
1167                 lens_u = lens_uv.x;
1168                 lens_v = lens_uv.y;
1169         }
1170
1171         float time = 0.0f;
1172
1173 #ifdef __CAMERA_MOTION__
1174         if(kernel_data.cam.shuttertime != -1.0f)
1175                 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1176 #endif
1177
1178         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, &ray);
1179
1180         /* integrate */
1181         float4 L;
1182
1183         if (ray.t != 0.0f) {
1184 #ifdef __NON_PROGRESSIVE__
1185                 if(kernel_data.integrator.progressive)
1186 #endif
1187                         L = kernel_path_progressive(kg, rng, sample, ray, buffer);
1188 #ifdef __NON_PROGRESSIVE__
1189                 else
1190                         L = kernel_path_non_progressive(kg, rng, sample, ray, buffer);
1191 #endif
1192         }
1193         else
1194                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1195
1196         /* accumulate result in output buffer */
1197         kernel_write_pass_float4(buffer, sample, L);
1198
1199         path_rng_end(kg, rng_state, rng);
1200 }
1201
1202 CCL_NAMESPACE_END
1203