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