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