Merging r50224 through r50225 from trunk into soc-2011-tomato
[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                                 bounce++;
213                         }
214                 }
215         }
216 #endif
217
218         return result;
219 }
220
221 __device float4 kernel_path_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
222 {
223         /* initialize */
224         PathRadiance L;
225         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
226         float L_transparent = 0.0f;
227
228         path_radiance_init(&L, kernel_data.film.use_light_pass);
229
230         float min_ray_pdf = FLT_MAX;
231         float ray_pdf = 0.0f;
232         PathState state;
233         int rng_offset = PRNG_BASE_NUM;
234
235         path_state_init(&state);
236
237         /* path iteration */
238         for(;; rng_offset += PRNG_BOUNCE_NUM) {
239                 /* intersect scene */
240                 Intersection isect;
241                 uint visibility = path_state_ray_visibility(kg, &state);
242
243                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
244                         /* eval background shader if nothing hit */
245                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
246                                 L_transparent += average(throughput);
247
248 #ifdef __PASSES__
249                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
250 #endif
251                                         break;
252                         }
253
254 #ifdef __BACKGROUND__
255                         /* sample background shader */
256                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
257                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
258 #endif
259
260                         break;
261                 }
262
263                 /* setup shading */
264                 ShaderData sd;
265                 shader_setup_from_ray(kg, &sd, &isect, &ray);
266                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
267                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
268
269                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
270
271                 /* blurring of bsdf after bounces, for rays that have a small likelihood
272                  * of following this particular path (diffuse, rough glossy) */
273                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
274                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
275
276                         if(blur_pdf < 1.0f) {
277                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
278                                 shader_bsdf_blur(kg, &sd, blur_roughness);
279                         }
280                 }
281
282                 /* holdout */
283 #ifdef __HOLDOUT__
284                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
285                         if(kernel_data.background.transparent) {
286                                 float3 holdout_weight;
287                                 
288                                 if(sd.flag & SD_HOLDOUT_MASK)
289                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
290                                 else
291                                         holdout_weight = shader_holdout_eval(kg, &sd);
292
293                                 /* any throughput is ok, should all be identical here */
294                                 L_transparent += average(holdout_weight*throughput);
295                         }
296
297                         if(sd.flag & SD_HOLDOUT_MASK)
298                                 break;
299                 }
300 #endif
301
302 #ifdef __EMISSION__
303                 /* emission */
304                 if(sd.flag & SD_EMISSION) {
305                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
306                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
307                 }
308 #endif
309
310                 /* path termination. this is a strange place to put the termination, it's
311                  * mainly due to the mixed in MIS that we use. gives too many unneeded
312                  * shader evaluations, only need emission if we are going to terminate */
313                 float probability = path_state_terminate_probability(kg, &state, throughput);
314                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
315
316                 if(terminate >= probability)
317                         break;
318
319                 throughput /= probability;
320
321 #ifdef __AO__
322                 /* ambient occlusion */
323                 if(kernel_data.integrator.use_ambient_occlusion) {
324                         /* todo: solve correlation */
325                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
326                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
327
328                         float3 ao_D;
329                         float ao_pdf;
330
331                         sample_cos_hemisphere(sd.N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
332
333                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
334                                 Ray light_ray;
335                                 float3 ao_shadow;
336
337                                 light_ray.P = ray_offset(sd.P, sd.Ng);
338                                 light_ray.D = ao_D;
339                                 light_ray.t = kernel_data.background.ao_distance;
340 #ifdef __MOTION__
341                                 light_ray.time = sd.time;
342 #endif
343
344                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow)) {
345                                         float3 ao_bsdf = shader_bsdf_diffuse(kg, &sd)*kernel_data.background.ao_factor;
346                                         path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
347                                 }
348                         }
349                 }
350 #endif
351
352 #ifdef __EMISSION__
353                 if(kernel_data.integrator.use_direct_light) {
354                         /* sample illumination from lights to find path contribution */
355                         if(sd.flag & SD_BSDF_HAS_EVAL) {
356                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
357                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
358                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
359                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
360
361                                 Ray light_ray;
362                                 BsdfEval L_light;
363                                 bool is_lamp;
364
365 #ifdef __MOTION__
366                                 light_ray.time = sd.time;
367 #endif
368
369                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
370                                         /* trace shadow ray */
371                                         float3 shadow;
372
373                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
374                                                 /* accumulate */
375                                                 path_radiance_accum_light(&L, throughput, &L_light, shadow, state.bounce, is_lamp);
376                                         }
377                                 }
378                         }
379                 }
380 #endif
381
382                 /* no BSDF? we can stop here */
383                 if(!(sd.flag & SD_BSDF))
384                         break;
385
386                 /* sample BSDF */
387                 float bsdf_pdf;
388                 BsdfEval bsdf_eval;
389                 float3 bsdf_omega_in;
390                 differential3 bsdf_domega_in;
391                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
392                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
393                 int label;
394
395                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
396                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
397
398                 shader_release(kg, &sd);
399
400                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
401                         break;
402
403                 /* modify throughput */
404                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
405
406                 /* set labels */
407                 if(!(label & LABEL_TRANSPARENT)) {
408                         ray_pdf = bsdf_pdf;
409                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
410                 }
411
412                 /* update path state */
413                 path_state_next(kg, &state, label);
414
415                 /* setup ray */
416                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
417                 ray.D = bsdf_omega_in;
418                 ray.t = FLT_MAX;
419 #ifdef __RAY_DIFFERENTIALS__
420                 ray.dP = sd.dP;
421                 ray.dD = bsdf_domega_in;
422 #endif
423         }
424
425         float3 L_sum = path_radiance_sum(kg, &L);
426
427 #ifdef __CLAMP_SAMPLE__
428         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
429 #endif
430
431         kernel_write_light_passes(kg, buffer, &L, sample);
432
433         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
434 }
435
436 #ifdef __NON_PROGRESSIVE__
437
438 __device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer,
439         float3 throughput, float min_ray_pdf, float ray_pdf, PathState state, int rng_offset, PathRadiance *L)
440 {
441         /* path iteration */
442         for(;; rng_offset += PRNG_BOUNCE_NUM) {
443                 /* intersect scene */
444                 Intersection isect;
445                 uint visibility = path_state_ray_visibility(kg, &state);
446
447                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
448 #ifdef __BACKGROUND__
449                         /* sample background shader */
450                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
451                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
452 #endif
453
454                         break;
455                 }
456
457                 /* setup shading */
458                 ShaderData sd;
459                 shader_setup_from_ray(kg, &sd, &isect, &ray);
460                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
461                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
462                 shader_merge_closures(kg, &sd);
463
464                 /* blurring of bsdf after bounces, for rays that have a small likelihood
465                  * of following this particular path (diffuse, rough glossy) */
466                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
467                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
468
469                         if(blur_pdf < 1.0f) {
470                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
471                                 shader_bsdf_blur(kg, &sd, blur_roughness);
472                         }
473                 }
474
475 #ifdef __EMISSION__
476                 /* emission */
477                 if(sd.flag & SD_EMISSION) {
478                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
479                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
480                 }
481 #endif
482
483                 /* path termination. this is a strange place to put the termination, it's
484                  * mainly due to the mixed in MIS that we use. gives too many unneeded
485                  * shader evaluations, only need emission if we are going to terminate */
486                 float probability = path_state_terminate_probability(kg, &state, throughput);
487                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
488
489                 if(terminate >= probability)
490                         break;
491
492                 throughput /= probability;
493
494 #ifdef __AO__
495                 /* ambient occlusion */
496                 if(kernel_data.integrator.use_ambient_occlusion) {
497                         /* todo: solve correlation */
498                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
499                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
500
501                         float3 ao_D;
502                         float ao_pdf;
503
504                         sample_cos_hemisphere(sd.N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
505
506                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
507                                 Ray light_ray;
508                                 float3 ao_shadow;
509
510                                 light_ray.P = ray_offset(sd.P, sd.Ng);
511                                 light_ray.D = ao_D;
512                                 light_ray.t = kernel_data.background.ao_distance;
513 #ifdef __MOTION__
514                                 light_ray.time = sd.time;
515 #endif
516
517                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow)) {
518                                         float3 ao_bsdf = shader_bsdf_diffuse(kg, &sd)*kernel_data.background.ao_factor;
519                                         path_radiance_accum_ao(L, throughput, ao_bsdf, ao_shadow, state.bounce);
520                                 }
521                         }
522                 }
523 #endif
524
525 #ifdef __EMISSION__
526                 if(kernel_data.integrator.use_direct_light) {
527                         /* sample illumination from lights to find path contribution */
528                         if(sd.flag & SD_BSDF_HAS_EVAL) {
529                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
530                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
531                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
532                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
533
534                                 Ray light_ray;
535                                 BsdfEval L_light;
536                                 bool is_lamp;
537
538 #ifdef __MOTION__
539                                 light_ray.time = sd.time;
540 #endif
541
542                                 /* sample random light */
543                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
544                                         /* trace shadow ray */
545                                         float3 shadow;
546
547                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
548                                                 /* accumulate */
549                                                 path_radiance_accum_light(L, throughput, &L_light, shadow, state.bounce, is_lamp);
550                                         }
551                                 }
552                         }
553                 }
554 #endif
555
556                 /* no BSDF? we can stop here */
557                 if(!(sd.flag & SD_BSDF))
558                         break;
559
560                 /* sample BSDF */
561                 float bsdf_pdf;
562                 BsdfEval bsdf_eval;
563                 float3 bsdf_omega_in;
564                 differential3 bsdf_domega_in;
565                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
566                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
567                 int label;
568
569                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
570                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
571
572                 shader_release(kg, &sd);
573
574                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
575                         break;
576
577                 /* modify throughput */
578                 path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
579
580                 /* set labels */
581                 if(!(label & LABEL_TRANSPARENT)) {
582                         ray_pdf = bsdf_pdf;
583                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
584                 }
585
586                 /* update path state */
587                 path_state_next(kg, &state, label);
588
589                 /* setup ray */
590                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
591                 ray.D = bsdf_omega_in;
592                 ray.t = FLT_MAX;
593 #ifdef __RAY_DIFFERENTIALS__
594                 ray.dP = sd.dP;
595                 ray.dD = bsdf_domega_in;
596 #endif
597         }
598 }
599
600 __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
601 {
602         /* initialize */
603         PathRadiance L;
604         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
605         float L_transparent = 0.0f;
606
607         path_radiance_init(&L, kernel_data.film.use_light_pass);
608
609         float ray_pdf = 0.0f;
610         PathState state;
611         int rng_offset = PRNG_BASE_NUM;
612
613         path_state_init(&state);
614
615         for(;; rng_offset += PRNG_BOUNCE_NUM) {
616                 /* intersect scene */
617                 Intersection isect;
618                 uint visibility = path_state_ray_visibility(kg, &state);
619
620                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
621                         /* eval background shader if nothing hit */
622                         if(kernel_data.background.transparent) {
623                                 L_transparent += average(throughput);
624
625 #ifdef __PASSES__
626                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
627 #endif
628                                         break;
629                         }
630
631 #ifdef __BACKGROUND__
632                         /* sample background shader */
633                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
634                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
635 #endif
636
637                         break;
638                 }
639
640                 /* setup shading */
641                 ShaderData sd;
642                 shader_setup_from_ray(kg, &sd, &isect, &ray);
643                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
644                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
645                 shader_merge_closures(kg, &sd);
646
647                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
648
649                 /* holdout */
650 #ifdef __HOLDOUT__
651                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
652                         if(kernel_data.background.transparent) {
653                                 float3 holdout_weight;
654                                 
655                                 if(sd.flag & SD_HOLDOUT_MASK)
656                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
657                                 else
658                                         holdout_weight = shader_holdout_eval(kg, &sd);
659
660                                 /* any throughput is ok, should all be identical here */
661                                 L_transparent += average(holdout_weight*throughput);
662                         }
663
664                         if(sd.flag & SD_HOLDOUT_MASK)
665                                 break;
666                 }
667 #endif
668
669 #ifdef __EMISSION__
670                 /* emission */
671                 if(sd.flag & SD_EMISSION) {
672                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
673                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
674                 }
675 #endif
676
677                 /* transparency termination */
678                 if(state.flag & PATH_RAY_TRANSPARENT) {
679                         /* path termination. this is a strange place to put the termination, it's
680                          * mainly due to the mixed in MIS that we use. gives too many unneeded
681                          * shader evaluations, only need emission if we are going to terminate */
682                         float probability = path_state_terminate_probability(kg, &state, throughput);
683                         float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
684
685                         if(terminate >= probability)
686                                 break;
687
688                         throughput /= probability;
689                 }
690
691 #ifdef __AO__
692                 /* ambient occlusion */
693                 if(kernel_data.integrator.use_ambient_occlusion) {
694                         int num_samples = kernel_data.integrator.ao_samples;
695                         float num_samples_inv = 1.0f/num_samples;
696                         float ao_factor = kernel_data.background.ao_factor;
697
698                         for(int j = 0; j < num_samples; j++) {
699                                 /* todo: solve correlation */
700                                 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
701                                 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
702
703                                 float3 ao_D;
704                                 float ao_pdf;
705
706                                 sample_cos_hemisphere(sd.N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
707
708                                 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
709                                         Ray light_ray;
710                                         float3 ao_shadow;
711
712                                         light_ray.P = ray_offset(sd.P, sd.Ng);
713                                         light_ray.D = ao_D;
714                                         light_ray.t = kernel_data.background.ao_distance;
715 #ifdef __MOTION__
716                                         light_ray.time = sd.time;
717 #endif
718
719                                         if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow)) {
720                                                 float3 ao_bsdf = shader_bsdf_diffuse(kg, &sd)*ao_factor;
721                                                 path_radiance_accum_ao(&L, throughput*num_samples_inv, ao_bsdf, ao_shadow, state.bounce);
722                                         }
723                                 }
724                         }
725                 }
726 #endif
727
728 #ifdef __EMISSION__
729                 /* sample illumination from lights to find path contribution */
730                 if(sd.flag & SD_BSDF_HAS_EVAL) {
731                         Ray light_ray;
732                         BsdfEval L_light;
733                         bool is_lamp;
734
735 #ifdef __MOTION__
736                         light_ray.time = sd.time;
737 #endif
738
739                         /* lamp sampling */
740                         for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
741                                 int num_samples = light_select_num_samples(kg, i);
742                                 float num_samples_inv = 1.0f/(num_samples*kernel_data.integrator.num_all_lights);
743
744                                 if(kernel_data.integrator.pdf_triangles != 0.0f)
745                                         num_samples_inv *= 0.5f;
746
747                                 for(int j = 0; j < num_samples; j++) {
748                                         float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
749                                         float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
750
751                                         if(direct_emission(kg, &sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
752                                                 /* trace shadow ray */
753                                                 float3 shadow;
754
755                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
756                                                         /* accumulate */
757                                                         path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
758                                                 }
759                                         }
760                                 }
761                         }
762
763                         /* mesh light sampling */
764                         if(kernel_data.integrator.pdf_triangles != 0.0f) {
765                                 int num_samples = kernel_data.integrator.mesh_light_samples;
766                                 float num_samples_inv = 1.0f/num_samples;
767
768                                 if(kernel_data.integrator.num_all_lights)
769                                         num_samples_inv *= 0.5f;
770
771                                 for(int j = 0; j < num_samples; j++) {
772                                         float light_t = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT);
773                                         float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
774                                         float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
775
776                                         /* only sample triangle lights */
777                                         if(kernel_data.integrator.num_all_lights)
778                                                 light_t = 0.5f*light_t;
779
780                                         if(direct_emission(kg, &sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
781                                                 /* trace shadow ray */
782                                                 float3 shadow;
783
784                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
785                                                         /* accumulate */
786                                                         path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
787                                                 }
788                                         }
789                                 }
790                         }
791                 }
792 #endif
793
794                 for(int i = 0; i< sd.num_closure; i++) {
795                         const ShaderClosure *sc = &sd.closure[i];
796
797                         if(!CLOSURE_IS_BSDF(sc->type))
798                                 continue;
799                         /* transparency is not handled here, but in outer loop */
800                         if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
801                                 continue;
802
803                         int num_samples;
804
805                         if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
806                                 num_samples = kernel_data.integrator.diffuse_samples;
807                         else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
808                                 num_samples = kernel_data.integrator.glossy_samples;
809                         else
810                                 num_samples = kernel_data.integrator.transmission_samples;
811
812                         float num_samples_inv = 1.0f/num_samples;
813
814                         for(int j = 0; j < num_samples; j++) {
815                                 /* sample BSDF */
816                                 float bsdf_pdf;
817                                 BsdfEval bsdf_eval;
818                                 float3 bsdf_omega_in;
819                                 differential3 bsdf_domega_in;
820                                 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
821                                 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
822                                 int label;
823
824                                 label = shader_bsdf_sample_closure(kg, &sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
825                                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
826
827                                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
828                                         continue;
829
830                                 /* modify throughput */
831                                 float3 tp = throughput;
832                                 path_radiance_bsdf_bounce(&L, &tp, &bsdf_eval, bsdf_pdf, state.bounce, label);
833
834                                 /* set labels */
835                                 float min_ray_pdf = FLT_MAX;
836
837                                 if(!(label & LABEL_TRANSPARENT))
838                                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
839
840                                 /* modify path state */
841                                 PathState ps = state;
842                                 path_state_next(kg, &ps, label);
843
844                                 /* setup ray */
845                                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
846                                 ray.D = bsdf_omega_in;
847                                 ray.t = FLT_MAX;
848 #ifdef __RAY_DIFFERENTIALS__
849                                 ray.dP = sd.dP;
850                                 ray.dD = bsdf_domega_in;
851 #endif
852
853                                 kernel_path_indirect(kg, rng, sample*num_samples, ray, buffer,
854                                         tp*num_samples_inv, min_ray_pdf, bsdf_pdf, ps, rng_offset+PRNG_BOUNCE_NUM, &L);
855                         }
856                 }
857
858                 /* continue in case of transparency */
859                 throughput *= shader_bsdf_transparency(kg, &sd);
860                 shader_release(kg, &sd);
861
862                 if(is_zero(throughput))
863                         break;
864
865                 path_state_next(kg, &state, LABEL_TRANSPARENT);
866                 ray.P = ray_offset(sd.P, -sd.Ng);
867         }
868
869         float3 L_sum = path_radiance_sum(kg, &L);
870
871 #ifdef __CLAMP_SAMPLE__
872         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
873 #endif
874
875         kernel_write_light_passes(kg, buffer, &L, sample);
876
877         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
878 }
879
880 #endif
881
882 __device void kernel_path_trace(KernelGlobals *kg,
883         __global float *buffer, __global uint *rng_state,
884         int sample, int x, int y, int offset, int stride)
885 {
886         /* buffer offset */
887         int index = offset + x + y*stride;
888         int pass_stride = kernel_data.film.pass_stride;
889
890         rng_state += index;
891         buffer += index*pass_stride;
892
893         /* initialize random numbers */
894         RNG rng;
895
896         float filter_u;
897         float filter_v;
898
899         path_rng_init(kg, rng_state, sample, &rng, x, y, &filter_u, &filter_v);
900
901         /* sample camera ray */
902         Ray ray;
903
904         float lens_u = path_rng(kg, &rng, sample, PRNG_LENS_U);
905         float lens_v = path_rng(kg, &rng, sample, PRNG_LENS_V);
906
907 #ifdef __MOTION__
908         float time = path_rng(kg, &rng, sample, PRNG_TIME);
909 #else
910         float time = 0.0f;
911 #endif
912
913         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, &ray);
914
915         /* integrate */
916         float4 L;
917
918         if (ray.t != 0.0f) {
919 #ifdef __NON_PROGRESSIVE__
920                 if(kernel_data.integrator.progressive)
921 #endif
922                         L = kernel_path_progressive(kg, &rng, sample, ray, buffer);
923 #ifdef __NON_PROGRESSIVE__
924                 else
925                         L = kernel_path_non_progressive(kg, &rng, sample, ray, buffer);
926 #endif
927         }
928         else
929                 L = make_float4(0.f, 0.f, 0.f, 0.f);
930
931         /* accumulate result in output buffer */
932         kernel_write_pass_float4(buffer, sample, L);
933
934         path_rng_end(kg, rng_state, rng);
935 }
936
937 CCL_NAMESPACE_END
938