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