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