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