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