Fix #33824: cycles non-progressive render mode did not do correct path termination,
[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 #ifdef __QBVH__
32 #include "kernel_qbvh.h"
33 #else
34 #include "kernel_bvh.h"
35 #endif
36 #include "kernel_accumulate.h"
37 #include "kernel_camera.h"
38 #include "kernel_shader.h"
39 #include "kernel_light.h"
40 #include "kernel_emission.h"
41 #include "kernel_random.h"
42 #include "kernel_passes.h"
43
44 CCL_NAMESPACE_BEGIN
45
46 typedef struct PathState {
47         uint 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);
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         bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect);
168
169 #ifdef __TRANSPARENT_SHADOWS__
170         if(result && kernel_data.integrator.transparent_shadows) {
171                 /* transparent shadows work in such a way to try to minimize overhead
172                  * in cases where we don't need them. after a regular shadow ray is
173                  * cast we check if the hit primitive was potentially transparent, and
174                  * only in that case start marching. this gives on extra ray cast for
175                  * the cases were we do want transparency.
176                  *
177                  * also note that for this to work correct, multi close sampling must
178                  * be used, since we don't pass a random number to shader_eval_surface */
179                 if(shader_transparent_shadow(kg, &isect)) {
180                         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
181                         float3 Pend = ray->P + ray->D*ray->t;
182                         int bounce = state->transparent_bounce;
183
184                         for(;;) {
185                                 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
186                                         return true;
187                                 }
188                                 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
189                                         /* todo: get random number somewhere for probabilistic terminate */
190 #if 0
191                                         float probability = average(throughput);
192                                         float terminate = 0.0f;
193
194                                         if(terminate >= probability)
195                                                 return true;
196
197                                         throughput /= probability;
198 #endif
199                                 }
200
201                                 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect)) {
202                                         *shadow *= throughput;
203                                         return false;
204                                 }
205
206                                 if(!shader_transparent_shadow(kg, &isect))
207                                         return true;
208
209                                 ShaderData sd;
210                                 shader_setup_from_ray(kg, &sd, &isect, ray);
211                                 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW, SHADER_CONTEXT_SHADOW);
212
213                                 throughput *= shader_bsdf_transparency(kg, &sd);
214
215                                 ray->P = ray_offset(sd.P, -sd.Ng);
216                                 if(ray->t != FLT_MAX)
217                                         ray->D = normalize_len(Pend - ray->P, &ray->t);
218
219                                 shader_release(kg, &sd);
220
221                                 bounce++;
222                         }
223                 }
224         }
225 #endif
226
227         return result;
228 }
229
230 __device float4 kernel_path_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
231 {
232         /* initialize */
233         PathRadiance L;
234         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
235         float L_transparent = 0.0f;
236
237         path_radiance_init(&L, kernel_data.film.use_light_pass);
238
239         float min_ray_pdf = FLT_MAX;
240         float ray_pdf = 0.0f;
241 #ifdef __LAMP_MIS__
242         float ray_t = 0.0f;
243 #endif
244         PathState state;
245         int rng_offset = PRNG_BASE_NUM;
246
247         path_state_init(&state);
248
249         /* path iteration */
250         for(;; rng_offset += PRNG_BOUNCE_NUM) {
251                 /* intersect scene */
252                 Intersection isect;
253                 uint visibility = path_state_ray_visibility(kg, &state);
254                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
255
256 #ifdef __LAMP_MIS__
257                 if(kernel_data.integrator.pdf_lights > 0.0f && !(state.flag & PATH_RAY_CAMERA)) {
258                         /* ray starting from previous non-transparent bounce */
259                         Ray light_ray;
260
261                         light_ray.P = ray.P - ray_t*ray.D;
262                         ray_t += isect.t;
263                         light_ray.D = ray.D;
264                         light_ray.t = ray_t;
265                         light_ray.time = ray.time;
266
267                         /* intersect with lamp */
268                         float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
269                         float3 emission;
270
271                         if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission))
272                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
273                 }
274 #endif
275
276                 if(!hit) {
277                         /* eval background shader if nothing hit */
278                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
279                                 L_transparent += average(throughput);
280
281 #ifdef __PASSES__
282                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
283 #endif
284                                         break;
285                         }
286
287 #ifdef __BACKGROUND__
288                         /* sample background shader */
289                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
290                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
291 #endif
292
293                         break;
294                 }
295
296                 /* setup shading */
297                 ShaderData sd;
298                 shader_setup_from_ray(kg, &sd, &isect, &ray);
299                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
300                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
301
302                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
303
304                 /* blurring of bsdf after bounces, for rays that have a small likelihood
305                  * of following this particular path (diffuse, rough glossy) */
306                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
307                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
308
309                         if(blur_pdf < 1.0f) {
310                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
311                                 shader_bsdf_blur(kg, &sd, blur_roughness);
312                         }
313                 }
314
315                 /* holdout */
316 #ifdef __HOLDOUT__
317                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
318                         if(kernel_data.background.transparent) {
319                                 float3 holdout_weight;
320                                 
321                                 if(sd.flag & SD_HOLDOUT_MASK)
322                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
323                                 else
324                                         holdout_weight = shader_holdout_eval(kg, &sd);
325
326                                 /* any throughput is ok, should all be identical here */
327                                 L_transparent += average(holdout_weight*throughput);
328                         }
329
330                         if(sd.flag & SD_HOLDOUT_MASK) {
331                                 shader_release(kg, &sd);
332                                 break;
333                         }
334                 }
335 #endif
336
337 #ifdef __EMISSION__
338                 /* emission */
339                 if(sd.flag & SD_EMISSION) {
340                         /* todo: is isect.t wrong here for transparent surfaces? */
341                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
342                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
343                 }
344 #endif
345
346                 /* path termination. this is a strange place to put the termination, it's
347                  * mainly due to the mixed in MIS that we use. gives too many unneeded
348                  * shader evaluations, only need emission if we are going to terminate */
349                 float probability = path_state_terminate_probability(kg, &state, throughput);
350                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
351
352                 if(terminate >= probability) {
353                         shader_release(kg, &sd);
354                         break;
355                 }
356
357                 throughput /= probability;
358
359 #ifdef __AO__
360                 /* ambient occlusion */
361                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
362                         /* todo: solve correlation */
363                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
364                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
365
366                         float ao_factor = kernel_data.background.ao_factor;
367                         float3 ao_N;
368                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
369                         float3 ao_D;
370                         float ao_pdf;
371
372                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
373
374                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
375                                 Ray light_ray;
376                                 float3 ao_shadow;
377
378                                 light_ray.P = ray_offset(sd.P, sd.Ng);
379                                 light_ray.D = ao_D;
380                                 light_ray.t = kernel_data.background.ao_distance;
381 #ifdef __OBJECT_MOTION__
382                                 light_ray.time = sd.time;
383 #endif
384
385                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
386                                         path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
387                         }
388                 }
389 #endif
390
391 #ifdef __EMISSION__
392                 if(kernel_data.integrator.use_direct_light) {
393                         /* sample illumination from lights to find path contribution */
394                         if(sd.flag & SD_BSDF_HAS_EVAL) {
395                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
396                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
397                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
398                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
399
400                                 Ray light_ray;
401                                 BsdfEval L_light;
402                                 int lamp;
403
404 #ifdef __OBJECT_MOTION__
405                                 light_ray.time = sd.time;
406 #endif
407
408                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &lamp)) {
409                                         /* trace shadow ray */
410                                         float3 shadow;
411
412                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
413                                                 /* accumulate */
414                                                 bool is_lamp = (lamp != ~0);
415                                                 path_radiance_accum_light(&L, throughput, &L_light, shadow, state.bounce, is_lamp);
416                                         }
417                                 }
418                         }
419                 }
420 #endif
421
422                 /* no BSDF? we can stop here */
423                 if(!(sd.flag & SD_BSDF)) {
424                         shader_release(kg, &sd);
425                         break;
426                 }
427
428                 /* sample BSDF */
429                 float bsdf_pdf;
430                 BsdfEval bsdf_eval;
431                 float3 bsdf_omega_in;
432                 differential3 bsdf_domega_in;
433                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
434                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
435                 int label;
436
437                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
438                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
439
440                 shader_release(kg, &sd);
441
442                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
443                         break;
444
445                 /* modify throughput */
446                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
447
448                 /* set labels */
449                 if(!(label & LABEL_TRANSPARENT)) {
450                         ray_pdf = bsdf_pdf;
451 #ifdef __LAMP_MIS__
452                         ray_t = 0.0f;
453 #endif
454                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
455                 }
456
457                 /* update path state */
458                 path_state_next(kg, &state, label);
459
460                 /* setup ray */
461                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
462                 ray.D = bsdf_omega_in;
463
464                 if(state.bounce == 0)
465                         ray.t -= sd.ray_length; /* clipping works through transparent */
466                 else
467                         ray.t = FLT_MAX;
468
469 #ifdef __RAY_DIFFERENTIALS__
470                 ray.dP = sd.dP;
471                 ray.dD = bsdf_domega_in;
472 #endif
473         }
474
475         float3 L_sum = path_radiance_sum(kg, &L);
476
477 #ifdef __CLAMP_SAMPLE__
478         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
479 #endif
480
481         kernel_write_light_passes(kg, buffer, &L, sample);
482
483         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
484 }
485
486 #ifdef __NON_PROGRESSIVE__
487
488 __device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer,
489         float3 throughput, float throughput_normalize,
490         float min_ray_pdf, float ray_pdf, PathState state, int rng_offset, PathRadiance *L)
491 {
492 #ifdef __LAMP_MIS__
493         float ray_t = 0.0f;
494 #endif
495
496         /* path iteration */
497         for(;; rng_offset += PRNG_BOUNCE_NUM) {
498                 /* intersect scene */
499                 Intersection isect;
500                 uint visibility = path_state_ray_visibility(kg, &state);
501                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
502
503 #ifdef __LAMP_MIS__
504                 if(kernel_data.integrator.pdf_lights > 0.0f && !(state.flag & PATH_RAY_CAMERA)) {
505                         /* ray starting from previous non-transparent bounce */
506                         Ray light_ray;
507
508                         light_ray.P = ray.P - ray_t*ray.D;
509                         ray_t += isect.t;
510                         light_ray.D = ray.D;
511                         light_ray.t = ray_t;
512                         light_ray.time = ray.time;
513
514                         /* intersect with lamp */
515                         float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
516                         float3 emission;
517
518                         if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission))
519                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
520                 }
521 #endif
522
523                 if(!hit) {
524 #ifdef __BACKGROUND__
525                         /* sample background shader */
526                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
527                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
528 #endif
529
530                         break;
531                 }
532
533                 /* setup shading */
534                 ShaderData sd;
535                 shader_setup_from_ray(kg, &sd, &isect, &ray);
536                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
537                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
538                 shader_merge_closures(kg, &sd);
539
540                 /* blurring of bsdf after bounces, for rays that have a small likelihood
541                  * of following this particular path (diffuse, rough glossy) */
542                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
543                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
544
545                         if(blur_pdf < 1.0f) {
546                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
547                                 shader_bsdf_blur(kg, &sd, blur_roughness);
548                         }
549                 }
550
551 #ifdef __EMISSION__
552                 /* emission */
553                 if(sd.flag & SD_EMISSION) {
554                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
555                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
556                 }
557 #endif
558
559                 /* path termination. this is a strange place to put the termination, it's
560                  * mainly due to the mixed in MIS that we use. gives too many unneeded
561                  * shader evaluations, only need emission if we are going to terminate */
562                 float probability = path_state_terminate_probability(kg, &state, throughput*throughput_normalize);
563                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
564
565                 if(terminate >= probability) {
566                         shader_release(kg, &sd);
567                         break;
568                 }
569
570                 throughput /= probability;
571
572 #ifdef __AO__
573                 /* ambient occlusion */
574                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
575                         /* todo: solve correlation */
576                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
577                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
578
579                         float ao_factor = kernel_data.background.ao_factor;
580                         float3 ao_N;
581                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
582                         float3 ao_D;
583                         float ao_pdf;
584
585                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
586
587                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
588                                 Ray light_ray;
589                                 float3 ao_shadow;
590
591                                 light_ray.P = ray_offset(sd.P, sd.Ng);
592                                 light_ray.D = ao_D;
593                                 light_ray.t = kernel_data.background.ao_distance;
594 #ifdef __OBJECT_MOTION__
595                                 light_ray.time = sd.time;
596 #endif
597
598                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
599                                         path_radiance_accum_ao(L, throughput, ao_bsdf, ao_shadow, state.bounce);
600                         }
601                 }
602 #endif
603
604 #ifdef __EMISSION__
605                 if(kernel_data.integrator.use_direct_light) {
606                         /* sample illumination from lights to find path contribution */
607                         if(sd.flag & SD_BSDF_HAS_EVAL) {
608                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
609                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
610                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
611                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
612
613                                 Ray light_ray;
614                                 BsdfEval L_light;
615                                 int lamp;
616
617 #ifdef __OBJECT_MOTION__
618                                 light_ray.time = sd.time;
619 #endif
620
621                                 /* sample random light */
622                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &lamp)) {
623                                         /* trace shadow ray */
624                                         float3 shadow;
625
626                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
627                                                 /* accumulate */
628                                                 bool is_lamp = (lamp != ~0);
629                                                 path_radiance_accum_light(L, throughput, &L_light, shadow, state.bounce, is_lamp);
630                                         }
631                                 }
632                         }
633                 }
634 #endif
635
636                 /* no BSDF? we can stop here */
637                 if(!(sd.flag & SD_BSDF)) {
638                         shader_release(kg, &sd);
639                         break;
640                 }
641
642                 /* sample BSDF */
643                 float bsdf_pdf;
644                 BsdfEval bsdf_eval;
645                 float3 bsdf_omega_in;
646                 differential3 bsdf_domega_in;
647                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
648                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
649                 int label;
650
651                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
652                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
653
654                 shader_release(kg, &sd);
655
656                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
657                         break;
658
659                 /* modify throughput */
660                 path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
661
662                 /* set labels */
663                 if(!(label & LABEL_TRANSPARENT)) {
664                         ray_pdf = bsdf_pdf;
665 #ifdef __LAMP_MIS__
666                         ray_t = 0.0f;
667 #endif
668                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
669                 }
670
671                 /* update path state */
672                 path_state_next(kg, &state, label);
673
674                 /* setup ray */
675                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
676                 ray.D = bsdf_omega_in;
677                 ray.t = FLT_MAX;
678 #ifdef __RAY_DIFFERENTIALS__
679                 ray.dP = sd.dP;
680                 ray.dD = bsdf_domega_in;
681 #endif
682         }
683 }
684
685 __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
686 {
687         /* initialize */
688         PathRadiance L;
689         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
690         float L_transparent = 0.0f;
691
692         path_radiance_init(&L, kernel_data.film.use_light_pass);
693
694         float ray_pdf = 0.0f;
695         PathState state;
696         int rng_offset = PRNG_BASE_NUM;
697
698         path_state_init(&state);
699
700         for(;; rng_offset += PRNG_BOUNCE_NUM) {
701                 /* intersect scene */
702                 Intersection isect;
703                 uint visibility = path_state_ray_visibility(kg, &state);
704
705                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
706                         /* eval background shader if nothing hit */
707                         if(kernel_data.background.transparent) {
708                                 L_transparent += average(throughput);
709
710 #ifdef __PASSES__
711                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
712 #endif
713                                         break;
714                         }
715
716 #ifdef __BACKGROUND__
717                         /* sample background shader */
718                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
719                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
720 #endif
721
722                         break;
723                 }
724
725                 /* setup shading */
726                 ShaderData sd;
727                 shader_setup_from_ray(kg, &sd, &isect, &ray);
728                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
729                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
730                 shader_merge_closures(kg, &sd);
731
732                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
733
734                 /* holdout */
735 #ifdef __HOLDOUT__
736                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
737                         if(kernel_data.background.transparent) {
738                                 float3 holdout_weight;
739                                 
740                                 if(sd.flag & SD_HOLDOUT_MASK)
741                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
742                                 else
743                                         holdout_weight = shader_holdout_eval(kg, &sd);
744
745                                 /* any throughput is ok, should all be identical here */
746                                 L_transparent += average(holdout_weight*throughput);
747                         }
748
749                         if(sd.flag & SD_HOLDOUT_MASK) {
750                                 shader_release(kg, &sd);
751                                 break;
752                         }
753                 }
754 #endif
755
756 #ifdef __EMISSION__
757                 /* emission */
758                 if(sd.flag & SD_EMISSION) {
759                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
760                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
761                 }
762 #endif
763
764                 /* transparency termination */
765                 if(state.flag & PATH_RAY_TRANSPARENT) {
766                         /* path termination. this is a strange place to put the termination, it's
767                          * mainly due to the mixed in MIS that we use. gives too many unneeded
768                          * shader evaluations, only need emission if we are going to terminate */
769                         float probability = path_state_terminate_probability(kg, &state, throughput);
770                         float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
771
772                         if(terminate >= probability) {
773                                 shader_release(kg, &sd);
774                                 break;
775                         }
776
777                         throughput /= probability;
778                 }
779
780 #ifdef __AO__
781                 /* ambient occlusion */
782                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
783                         int num_samples = kernel_data.integrator.ao_samples;
784                         float num_samples_inv = 1.0f/num_samples;
785                         float ao_factor = kernel_data.background.ao_factor;
786                         float3 ao_N;
787                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
788
789                         for(int j = 0; j < num_samples; j++) {
790                                 /* todo: solve correlation */
791                                 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
792                                 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
793
794                                 float3 ao_D;
795                                 float ao_pdf;
796
797                                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
798
799                                 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
800                                         Ray light_ray;
801                                         float3 ao_shadow;
802
803                                         light_ray.P = ray_offset(sd.P, sd.Ng);
804                                         light_ray.D = ao_D;
805                                         light_ray.t = kernel_data.background.ao_distance;
806 #ifdef __OBJECT_MOTION__
807                                         light_ray.time = sd.time;
808 #endif
809
810                                         if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
811                                                 path_radiance_accum_ao(&L, throughput*num_samples_inv, ao_bsdf, ao_shadow, state.bounce);
812                                 }
813                         }
814                 }
815 #endif
816
817 #ifdef __EMISSION__
818                 /* sample illumination from lights to find path contribution */
819                 if(sd.flag & SD_BSDF_HAS_EVAL) {
820                         Ray light_ray;
821                         BsdfEval L_light;
822                         int lamp;
823
824 #ifdef __OBJECT_MOTION__
825                         light_ray.time = sd.time;
826 #endif
827
828                         /* lamp sampling */
829                         for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
830                                 int num_samples = light_select_num_samples(kg, i);
831                                 float num_samples_inv = 1.0f/(num_samples*kernel_data.integrator.num_all_lights);
832
833                                 if(kernel_data.integrator.pdf_triangles != 0.0f)
834                                         num_samples_inv *= 0.5f;
835
836                                 for(int j = 0; j < num_samples; j++) {
837                                         float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
838                                         float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
839
840                                         if(direct_emission(kg, &sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &lamp)) {
841                                                 /* trace shadow ray */
842                                                 float3 shadow;
843
844                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
845                                                         /* accumulate */
846                                                         bool is_lamp = (lamp != ~0);
847                                                         path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
848                                                 }
849                                         }
850                                 }
851                         }
852
853                         /* mesh light sampling */
854                         if(kernel_data.integrator.pdf_triangles != 0.0f) {
855                                 int num_samples = kernel_data.integrator.mesh_light_samples;
856                                 float num_samples_inv = 1.0f/num_samples;
857
858                                 if(kernel_data.integrator.num_all_lights)
859                                         num_samples_inv *= 0.5f;
860
861                                 for(int j = 0; j < num_samples; j++) {
862                                         float light_t = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT);
863                                         float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
864                                         float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
865
866                                         /* only sample triangle lights */
867                                         if(kernel_data.integrator.num_all_lights)
868                                                 light_t = 0.5f*light_t;
869
870                                         if(direct_emission(kg, &sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &lamp)) {
871                                                 /* trace shadow ray */
872                                                 float3 shadow;
873
874                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
875                                                         /* accumulate */
876                                                         bool is_lamp = (lamp != ~0);
877                                                         path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
878                                                 }
879                                         }
880                                 }
881                         }
882                 }
883 #endif
884
885                 for(int i = 0; i< sd.num_closure; i++) {
886                         const ShaderClosure *sc = &sd.closure[i];
887
888                         if(!CLOSURE_IS_BSDF(sc->type))
889                                 continue;
890                         /* transparency is not handled here, but in outer loop */
891                         if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
892                                 continue;
893
894                         int num_samples;
895
896                         if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
897                                 num_samples = kernel_data.integrator.diffuse_samples;
898                         else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
899                                 num_samples = kernel_data.integrator.glossy_samples;
900                         else
901                                 num_samples = kernel_data.integrator.transmission_samples;
902
903                         float num_samples_inv = 1.0f/num_samples;
904
905                         for(int j = 0; j < num_samples; j++) {
906                                 /* sample BSDF */
907                                 float bsdf_pdf;
908                                 BsdfEval bsdf_eval;
909                                 float3 bsdf_omega_in;
910                                 differential3 bsdf_domega_in;
911                                 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
912                                 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
913                                 int label;
914
915                                 label = shader_bsdf_sample_closure(kg, &sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
916                                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
917
918                                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
919                                         continue;
920
921                                 /* modify throughput */
922                                 float3 tp = throughput;
923                                 path_radiance_bsdf_bounce(&L, &tp, &bsdf_eval, bsdf_pdf, state.bounce, label);
924
925                                 /* set labels */
926                                 float min_ray_pdf = FLT_MAX;
927
928                                 if(!(label & LABEL_TRANSPARENT))
929                                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
930
931                                 /* modify path state */
932                                 PathState ps = state;
933                                 path_state_next(kg, &ps, label);
934
935                                 /* setup ray */
936                                 Ray bsdf_ray;
937
938                                 bsdf_ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
939                                 bsdf_ray.D = bsdf_omega_in;
940                                 bsdf_ray.t = FLT_MAX;
941 #ifdef __RAY_DIFFERENTIALS__
942                                 bsdf_ray.dP = sd.dP;
943                                 bsdf_ray.dD = bsdf_domega_in;
944 #endif
945 #ifdef __OBJECT_MOTION__
946                                 bsdf_ray.time = sd.time;
947 #endif
948
949                                 kernel_path_indirect(kg, rng, sample*num_samples + j, bsdf_ray, buffer,
950                                         tp*num_samples_inv, num_samples,
951                                         min_ray_pdf, bsdf_pdf, ps, rng_offset+PRNG_BOUNCE_NUM, &L);
952                         }
953                 }
954
955                 /* continue in case of transparency */
956                 throughput *= shader_bsdf_transparency(kg, &sd);
957                 shader_release(kg, &sd);
958
959                 if(is_zero(throughput))
960                         break;
961
962                 path_state_next(kg, &state, LABEL_TRANSPARENT);
963                 ray.P = ray_offset(sd.P, -sd.Ng);
964                 ray.t -= sd.ray_length; /* clipping works through transparent */
965         }
966
967         float3 L_sum = path_radiance_sum(kg, &L);
968
969 #ifdef __CLAMP_SAMPLE__
970         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
971 #endif
972
973         kernel_write_light_passes(kg, buffer, &L, sample);
974
975         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
976 }
977
978 #endif
979
980 __device void kernel_path_trace(KernelGlobals *kg,
981         __global float *buffer, __global uint *rng_state,
982         int sample, int x, int y, int offset, int stride)
983 {
984         /* buffer offset */
985         int index = offset + x + y*stride;
986         int pass_stride = kernel_data.film.pass_stride;
987
988         rng_state += index;
989         buffer += index*pass_stride;
990
991         /* initialize random numbers */
992         RNG rng;
993
994         float filter_u;
995         float filter_v;
996
997         path_rng_init(kg, rng_state, sample, &rng, x, y, &filter_u, &filter_v);
998
999         /* sample camera ray */
1000         Ray ray;
1001
1002         float lens_u = path_rng(kg, &rng, sample, PRNG_LENS_U);
1003         float lens_v = path_rng(kg, &rng, sample, PRNG_LENS_V);
1004
1005 #ifdef __CAMERA_MOTION__
1006         float time = path_rng(kg, &rng, sample, PRNG_TIME);
1007 #else
1008         float time = 0.0f;
1009 #endif
1010
1011         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, &ray);
1012
1013         /* integrate */
1014         float4 L;
1015
1016         if (ray.t != 0.0f) {
1017 #ifdef __NON_PROGRESSIVE__
1018                 if(kernel_data.integrator.progressive)
1019 #endif
1020                         L = kernel_path_progressive(kg, &rng, sample, ray, buffer);
1021 #ifdef __NON_PROGRESSIVE__
1022                 else
1023                         L = kernel_path_non_progressive(kg, &rng, sample, ray, buffer);
1024 #endif
1025         }
1026         else
1027                 L = make_float4(0.f, 0.f, 0.f, 0.f);
1028
1029         /* accumulate result in output buffer */
1030         kernel_write_pass_float4(buffer, sample, L);
1031
1032         path_rng_end(kg, rng_state, rng);
1033 }
1034
1035 CCL_NAMESPACE_END
1036