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