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