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