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