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