Cycles Bake
[blender-staging.git] / intern / cycles / kernel / kernel_path.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 #ifdef __OSL__
18 #include "osl_shader.h"
19 #endif
20
21 #include "kernel_random.h"
22 #include "kernel_projection.h"
23 #include "kernel_montecarlo.h"
24 #include "kernel_differential.h"
25 #include "kernel_camera.h"
26
27 #include "geom/geom.h"
28
29 #include "kernel_accumulate.h"
30 #include "kernel_shader.h"
31 #include "kernel_light.h"
32 #include "kernel_emission.h"
33 #include "kernel_passes.h"
34
35 #ifdef __SUBSURFACE__
36 #include "kernel_subsurface.h"
37 #endif
38
39 #ifdef __VOLUME__
40 #include "kernel_volume.h"
41 #endif
42
43 #include "kernel_path_state.h"
44 #include "kernel_shadow.h"
45
46 CCL_NAMESPACE_BEGIN
47
48 #ifdef __VOLUME__
49
50 ccl_device_inline bool kernel_path_integrate_scatter_lighting(KernelGlobals *kg, RNG *rng,
51         ShaderData *sd, float3 *throughput, PathState *state, PathRadiance *L, Ray *ray,
52         float num_samples_adjust)
53 {
54 #ifdef __EMISSION__
55         if(kernel_data.integrator.use_direct_light) {
56                 /* sample illumination from lights to find path contribution */
57                 if(sd->flag & SD_BSDF_HAS_EVAL) {
58                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
59                         float light_u, light_v;
60                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
61
62                         Ray light_ray;
63                         BsdfEval L_light;
64                         bool is_lamp;
65
66 #ifdef __OBJECT_MOTION__
67                         light_ray.time = sd->time;
68 #endif
69
70                         if(direct_emission(kg, sd, LAMP_NONE, light_t, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce, state->transparent_bounce)) {
71                                 /* trace shadow ray */
72                                 float3 shadow;
73
74                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
75                                         /* accumulate */
76                                         path_radiance_accum_light(L, *throughput * num_samples_adjust, &L_light, shadow, 1.0f, state->bounce, is_lamp);
77                                 }
78                         }
79                 }
80         }
81 #endif
82
83         /* sample phase function */
84         float phase_pdf;
85         BsdfEval phase_eval;
86         float3 phase_omega_in;
87         differential3 phase_domega_in;
88         float phase_u, phase_v;
89         path_state_rng_2D(kg, rng, state, PRNG_PHASE_U, &phase_u, &phase_v);
90         int label;
91
92         label = shader_volume_phase_sample(kg, sd, phase_u, phase_v, &phase_eval,
93                 &phase_omega_in, &phase_domega_in, &phase_pdf);
94
95         if(phase_pdf == 0.0f || bsdf_eval_is_zero(&phase_eval))
96                 return false;
97         
98         /* modify throughput */
99         path_radiance_bsdf_bounce(L, throughput, &phase_eval, phase_pdf, state->bounce, label);
100
101         /* set labels */
102         state->ray_pdf = phase_pdf;
103 #ifdef __LAMP_MIS__
104         state->ray_t = 0.0f;
105 #endif
106         state->min_ray_pdf = fminf(phase_pdf, state->min_ray_pdf);
107
108         /* update path state */
109         path_state_next(kg, state, label);
110
111         /* setup ray */
112         ray->P = sd->P;
113         ray->D = phase_omega_in;
114         ray->t = FLT_MAX;
115
116 #ifdef __RAY_DIFFERENTIALS__
117         ray->dP = sd->dP;
118         ray->dD = phase_domega_in;
119 #endif
120
121         return true;
122 }
123
124 #endif
125
126 #if defined(__BRANCHED_PATH__) || defined(__SUBSURFACE__)
127
128 ccl_device void kernel_branched_path_integrate_direct_lighting(KernelGlobals *kg, RNG *rng,
129         ShaderData *sd, PathState *state, float3 throughput, float num_samples_adjust, PathRadiance *L, bool sample_all_lights)
130 {
131         /* sample illumination from lights to find path contribution */
132         if(sd->flag & SD_BSDF_HAS_EVAL) {
133                 Ray light_ray;
134                 BsdfEval L_light;
135                 bool is_lamp;
136
137 #ifdef __OBJECT_MOTION__
138                 light_ray.time = sd->time;
139 #endif
140
141                 if(sample_all_lights) {
142                         /* lamp sampling */
143                         for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
144                                 int num_samples = ceil_to_int(num_samples_adjust*light_select_num_samples(kg, i));
145                                 float num_samples_inv = num_samples_adjust/(num_samples*kernel_data.integrator.num_all_lights);
146                                 RNG lamp_rng = cmj_hash(*rng, i);
147
148                                 if(kernel_data.integrator.pdf_triangles != 0.0f)
149                                         num_samples_inv *= 0.5f;
150
151                                 for(int j = 0; j < num_samples; j++) {
152                                         float light_u, light_v;
153                                         path_branched_rng_2D(kg, &lamp_rng, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
154
155                                         if(direct_emission(kg, sd, i, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce, state->transparent_bounce)) {
156                                                 /* trace shadow ray */
157                                                 float3 shadow;
158
159                                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
160                                                         /* accumulate */
161                                                         path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state->bounce, is_lamp);
162                                                 }
163                                         }
164                                 }
165                         }
166
167                         /* mesh light sampling */
168                         if(kernel_data.integrator.pdf_triangles != 0.0f) {
169                                 int num_samples = ceil_to_int(num_samples_adjust*kernel_data.integrator.mesh_light_samples);
170                                 float num_samples_inv = num_samples_adjust/num_samples;
171
172                                 if(kernel_data.integrator.num_all_lights)
173                                         num_samples_inv *= 0.5f;
174
175                                 for(int j = 0; j < num_samples; j++) {
176                                         float light_t = path_branched_rng_1D(kg, rng, state, j, num_samples, PRNG_LIGHT);
177                                         float light_u, light_v;
178                                         path_branched_rng_2D(kg, rng, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
179
180                                         /* only sample triangle lights */
181                                         if(kernel_data.integrator.num_all_lights)
182                                                 light_t = 0.5f*light_t;
183
184                                         if(direct_emission(kg, sd, LAMP_NONE, light_t, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce, state->transparent_bounce)) {
185                                                 /* trace shadow ray */
186                                                 float3 shadow;
187
188                                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
189                                                         /* accumulate */
190                                                         path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state->bounce, is_lamp);
191                                                 }
192                                         }
193                                 }
194                         }
195                 }
196                 else {
197                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
198                         float light_u, light_v;
199                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
200
201                         /* sample random light */
202                         if(direct_emission(kg, sd, LAMP_NONE, light_t, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce, state->transparent_bounce)) {
203                                 /* trace shadow ray */
204                                 float3 shadow;
205
206                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
207                                         /* accumulate */
208                                         path_radiance_accum_light(L, throughput, &L_light, shadow, 1.0f, state->bounce, is_lamp);
209                                 }
210                         }
211                 }
212         }
213 }
214
215 #endif
216
217 ccl_device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, Ray ray,
218         float3 throughput, int num_samples, PathState state, PathRadiance *L)
219 {
220         /* path iteration */
221         for(;;) {
222                 /* intersect scene */
223                 Intersection isect;
224                 uint visibility = path_state_ray_visibility(kg, &state);
225 #ifdef __HAIR__
226                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
227 #else
228                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
229 #endif
230
231 #ifdef __LAMP_MIS__
232                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
233                         /* ray starting from previous non-transparent bounce */
234                         Ray light_ray;
235
236                         light_ray.P = ray.P - state.ray_t*ray.D;
237                         state.ray_t += isect.t;
238                         light_ray.D = ray.D;
239                         light_ray.t = state.ray_t;
240                         light_ray.time = ray.time;
241                         light_ray.dD = ray.dD;
242                         light_ray.dP = ray.dP;
243
244                         /* intersect with lamp */
245                         float3 emission;
246
247                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
248                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
249                 }
250 #endif
251
252 #ifdef __VOLUME__
253                 /* volume attenuation, emission, scatter */
254                 if(state.volume_stack[0].shader != SHADER_NONE) {
255                         Ray volume_ray = ray;
256                         volume_ray.t = (hit)? isect.t: FLT_MAX;
257
258                         ShaderData volume_sd;
259                         VolumeIntegrateResult result = kernel_volume_integrate(kg, &state,
260                                 &volume_sd, &volume_ray, L, &throughput, rng);
261
262                         if(result == VOLUME_PATH_SCATTERED) {
263                                 if(kernel_path_integrate_scatter_lighting(kg, rng, &volume_sd, &throughput, &state, L, &ray, 1.0f))
264                                         continue;
265                                 else
266                                         break;
267                         }
268                 }
269 #endif
270
271                 if(!hit) {
272 #ifdef __BACKGROUND__
273                         /* sample background shader */
274                         float3 L_background = indirect_background(kg, &state, &ray);
275                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
276 #endif
277
278                         break;
279                 }
280
281                 /* setup shading */
282                 ShaderData sd;
283                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
284                 float rbsdf = path_state_rng_1D(kg, rng, &state, PRNG_BSDF);
285                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
286 #ifdef __BRANCHED_PATH__
287                 shader_merge_closures(&sd);
288 #endif
289
290                 /* blurring of bsdf after bounces, for rays that have a small likelihood
291                  * of following this particular path (diffuse, rough glossy) */
292                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
293                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
294
295                         if(blur_pdf < 1.0f) {
296                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
297                                 shader_bsdf_blur(kg, &sd, blur_roughness);
298                         }
299                 }
300
301 #ifdef __EMISSION__
302                 /* emission */
303                 if(sd.flag & SD_EMISSION) {
304                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
305                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
306                 }
307 #endif
308
309                 /* path termination. this is a strange place to put the termination, it's
310                  * mainly due to the mixed in MIS that we use. gives too many unneeded
311                  * shader evaluations, only need emission if we are going to terminate */
312                 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
313
314                 if(probability == 0.0f) {
315                         break;
316                 }
317                 else if(probability != 1.0f) {
318                         float terminate = path_state_rng_1D(kg, rng, &state, PRNG_TERMINATE);
319
320                         if(terminate >= probability)
321                                 break;
322
323                         throughput /= probability;
324                 }
325
326 #ifdef __AO__
327                 /* ambient occlusion */
328                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
329                         float bsdf_u, bsdf_v;
330                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
331
332                         float ao_factor = kernel_data.background.ao_factor;
333                         float3 ao_N;
334                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
335                         float3 ao_D;
336                         float ao_pdf;
337                         float3 ao_alpha = make_float3(0.0f, 0.0f, 0.0f);
338
339                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
340
341                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
342                                 Ray light_ray;
343                                 float3 ao_shadow;
344
345                                 light_ray.P = ray_offset(sd.P, sd.Ng);
346                                 light_ray.D = ao_D;
347                                 light_ray.t = kernel_data.background.ao_distance;
348 #ifdef __OBJECT_MOTION__
349                                 light_ray.time = sd.time;
350 #endif
351                                 light_ray.dP = sd.dP;
352                                 light_ray.dD = differential3_zero();
353
354                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
355                                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
356                         }
357                 }
358 #endif
359
360 #ifdef __SUBSURFACE__
361                 /* bssrdf scatter to a different location on the same object, replacing
362                  * the closures with a diffuse BSDF */
363                 if(sd.flag & SD_BSSRDF) {
364                         float bssrdf_probability;
365                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
366
367                         /* modify throughput for picking bssrdf or bsdf */
368                         throughput *= bssrdf_probability;
369
370                         /* do bssrdf scatter step if we picked a bssrdf closure */
371                         if(sc) {
372                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
373
374                                 float bssrdf_u, bssrdf_v;
375                                 path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
376                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
377
378                                 state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
379                         }
380                 }
381 #endif
382
383 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
384                 if(kernel_data.integrator.use_direct_light) {
385                         bool all = kernel_data.integrator.sample_all_lights_indirect;
386                         kernel_branched_path_integrate_direct_lighting(kg, rng, &sd, &state, throughput, 1.0f, L, all);
387                 }
388 #endif
389
390                 /* no BSDF? we can stop here */
391                 if(sd.flag & SD_BSDF) {
392                         /* sample BSDF */
393                         float bsdf_pdf;
394                         BsdfEval bsdf_eval;
395                         float3 bsdf_omega_in;
396                         differential3 bsdf_domega_in;
397                         float bsdf_u, bsdf_v;
398                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
399                         int label;
400
401                         label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
402                                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
403
404                         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
405                                 break;
406
407                         /* modify throughput */
408                         path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
409
410                         /* set labels */
411                         if(!(label & LABEL_TRANSPARENT)) {
412                                 state.ray_pdf = bsdf_pdf;
413 #ifdef __LAMP_MIS__
414                                 state.ray_t = 0.0f;
415 #endif
416                                 state.min_ray_pdf = fminf(bsdf_pdf, state.min_ray_pdf);
417                         }
418
419                         /* update path state */
420                         path_state_next(kg, &state, label);
421
422                         /* setup ray */
423                         ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
424                         ray.D = bsdf_omega_in;
425                         ray.t = FLT_MAX;
426 #ifdef __RAY_DIFFERENTIALS__
427                         ray.dP = sd.dP;
428                         ray.dD = bsdf_domega_in;
429 #endif
430
431 #ifdef __VOLUME__
432                         /* enter/exit volume */
433                         if(label & LABEL_TRANSMIT)
434                                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
435 #endif
436                 }
437 #ifdef __VOLUME__
438                 else if(sd.flag & SD_HAS_ONLY_VOLUME) {
439                         /* no surface shader but have a volume shader? act transparent */
440
441                         /* update path state, count as transparent */
442                         path_state_next(kg, &state, LABEL_TRANSPARENT);
443
444                         /* setup ray position, direction stays unchanged */
445                         ray.P = ray_offset(sd.P, -sd.Ng);
446 #ifdef __RAY_DIFFERENTIALS__
447                         ray.dP = sd.dP;
448 #endif
449
450                         /* enter/exit volume */
451                         kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
452                 }
453 #endif
454                 else {
455                         /* no bsdf or volume? we're done */
456                         break;
457                 }
458         }
459 }
460
461 ccl_device_inline bool kernel_path_integrate_lighting(KernelGlobals *kg, RNG *rng,
462         ShaderData *sd, float3 *throughput, PathState *state, PathRadiance *L, Ray *ray)
463 {
464 #ifdef __EMISSION__
465         if(kernel_data.integrator.use_direct_light) {
466                 /* sample illumination from lights to find path contribution */
467                 if(sd->flag & SD_BSDF_HAS_EVAL) {
468                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
469                         float light_u, light_v;
470                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
471
472                         Ray light_ray;
473                         BsdfEval L_light;
474                         bool is_lamp;
475
476 #ifdef __OBJECT_MOTION__
477                         light_ray.time = sd->time;
478 #endif
479
480                         if(direct_emission(kg, sd, LAMP_NONE, light_t, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce, state->transparent_bounce)) {
481                                 /* trace shadow ray */
482                                 float3 shadow;
483
484                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
485                                         /* accumulate */
486                                         path_radiance_accum_light(L, *throughput, &L_light, shadow, 1.0f, state->bounce, is_lamp);
487                                 }
488                         }
489                 }
490         }
491 #endif
492
493         /* no BSDF? we can stop here */
494         if(sd->flag & SD_BSDF) {
495                 /* sample BSDF */
496                 float bsdf_pdf;
497                 BsdfEval bsdf_eval;
498                 float3 bsdf_omega_in;
499                 differential3 bsdf_domega_in;
500                 float bsdf_u, bsdf_v;
501                 path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
502                 int label;
503
504                 label = shader_bsdf_sample(kg, sd, bsdf_u, bsdf_v, &bsdf_eval,
505                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
506
507                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
508                         return false;
509
510                 /* modify throughput */
511                 path_radiance_bsdf_bounce(L, throughput, &bsdf_eval, bsdf_pdf, state->bounce, label);
512
513                 /* set labels */
514                 if(!(label & LABEL_TRANSPARENT)) {
515                         state->ray_pdf = bsdf_pdf;
516 #ifdef __LAMP_MIS__
517                         state->ray_t = 0.0f;
518 #endif
519                         state->min_ray_pdf = fminf(bsdf_pdf, state->min_ray_pdf);
520                 }
521
522                 /* update path state */
523                 path_state_next(kg, state, label);
524
525                 /* setup ray */
526                 ray->P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
527                 ray->D = bsdf_omega_in;
528
529                 if(state->bounce == 0)
530                         ray->t -= sd->ray_length; /* clipping works through transparent */
531                 else
532                         ray->t = FLT_MAX;
533
534 #ifdef __RAY_DIFFERENTIALS__
535                 ray->dP = sd->dP;
536                 ray->dD = bsdf_domega_in;
537 #endif
538
539 #ifdef __VOLUME__
540                 /* enter/exit volume */
541                 if(label & LABEL_TRANSMIT)
542                         kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
543 #endif
544                 return true;
545         }
546 #ifdef __VOLUME__
547         else if(sd->flag & SD_HAS_ONLY_VOLUME) {
548                 /* no surface shader but have a volume shader? act transparent */
549
550                 /* update path state, count as transparent */
551                 path_state_next(kg, state, LABEL_TRANSPARENT);
552
553                 /* setup ray position, direction stays unchanged */
554                 ray->P = ray_offset(sd->P, -sd->Ng);
555 #ifdef __RAY_DIFFERENTIALS__
556                 ray->dP = sd->dP;
557 #endif
558
559                 /* enter/exit volume */
560                 kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
561                 return true;
562         }
563 #endif
564         else {
565                 /* no bsdf or volume? */
566                 return false;
567         }
568 }
569
570 ccl_device void kernel_path_ao(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
571 {
572         /* todo: solve correlation */
573         float bsdf_u, bsdf_v;
574
575         path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
576
577         float ao_factor = kernel_data.background.ao_factor;
578         float3 ao_N;
579         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
580         float3 ao_D;
581         float ao_pdf;
582         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
583
584         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
585
586         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
587                 Ray light_ray;
588                 float3 ao_shadow;
589
590                 light_ray.P = ray_offset(sd->P, sd->Ng);
591                 light_ray.D = ao_D;
592                 light_ray.t = kernel_data.background.ao_distance;
593 #ifdef __OBJECT_MOTION__
594                 light_ray.time = sd->time;
595 #endif
596                 light_ray.dP = sd->dP;
597                 light_ray.dD = differential3_zero();
598
599                 if(!shadow_blocked(kg, state, &light_ray, &ao_shadow))
600                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
601         }
602 }
603
604 ccl_device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
605 {
606         /* initialize */
607         PathRadiance L;
608         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
609         float L_transparent = 0.0f;
610
611         path_radiance_init(&L, kernel_data.film.use_light_pass);
612
613         PathState state;
614         path_state_init(kg, &state, rng, sample);
615
616         /* path iteration */
617         for(;;) {
618                 /* intersect scene */
619                 Intersection isect;
620                 uint visibility = path_state_ray_visibility(kg, &state);
621
622 #ifdef __HAIR__
623                 float difl = 0.0f, extmax = 0.0f;
624                 uint lcg_state = 0;
625
626                 if(kernel_data.bvh.have_curves) {
627                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
628                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
629                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
630                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
631                         }
632
633                         extmax = kernel_data.curve.maximum_width;
634                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
635                 }
636
637                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
638 #else
639                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
640 #endif
641
642 #ifdef __LAMP_MIS__
643                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
644                         /* ray starting from previous non-transparent bounce */
645                         Ray light_ray;
646
647                         light_ray.P = ray.P - state.ray_t*ray.D;
648                         state.ray_t += isect.t;
649                         light_ray.D = ray.D;
650                         light_ray.t = state.ray_t;
651                         light_ray.time = ray.time;
652                         light_ray.dD = ray.dD;
653                         light_ray.dP = ray.dP;
654
655                         /* intersect with lamp */
656                         float3 emission;
657
658                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
659                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
660                 }
661 #endif
662
663 #ifdef __VOLUME__
664                 /* volume attenuation, emission, scatter */
665                 if(state.volume_stack[0].shader != SHADER_NONE) {
666                         Ray volume_ray = ray;
667                         volume_ray.t = (hit)? isect.t: FLT_MAX;
668
669                         ShaderData volume_sd;
670                         VolumeIntegrateResult result = kernel_volume_integrate(kg, &state,
671                                 &volume_sd, &volume_ray, &L, &throughput, rng);
672
673                         if(result == VOLUME_PATH_SCATTERED) {
674                                 if(kernel_path_integrate_scatter_lighting(kg, rng, &volume_sd, &throughput, &state, &L, &ray, 1.0f))
675                                         continue;
676                                 else
677                                         break;
678                         }
679                 }
680 #endif
681
682                 if(!hit) {
683                         /* eval background shader if nothing hit */
684                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
685                                 L_transparent += average(throughput);
686
687 #ifdef __PASSES__
688                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
689 #endif
690                                         break;
691                         }
692
693 #ifdef __BACKGROUND__
694                         /* sample background shader */
695                         float3 L_background = indirect_background(kg, &state, &ray);
696                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
697 #endif
698
699                         break;
700                 }
701
702                 /* setup shading */
703                 ShaderData sd;
704                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
705                 float rbsdf = path_state_rng_1D(kg, rng, &state, PRNG_BSDF);
706                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
707
708                 /* holdout */
709 #ifdef __HOLDOUT__
710                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
711                         if(kernel_data.background.transparent) {
712                                 float3 holdout_weight;
713                                 
714                                 if(sd.flag & SD_HOLDOUT_MASK)
715                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
716                                 else
717                                         holdout_weight = shader_holdout_eval(kg, &sd);
718
719                                 /* any throughput is ok, should all be identical here */
720                                 L_transparent += average(holdout_weight*throughput);
721                         }
722
723                         if(sd.flag & SD_HOLDOUT_MASK)
724                                 break;
725                 }
726 #endif
727
728                 /* holdout mask objects do not write data passes */
729                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
730
731                 /* blurring of bsdf after bounces, for rays that have a small likelihood
732                  * of following this particular path (diffuse, rough glossy) */
733                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
734                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
735
736                         if(blur_pdf < 1.0f) {
737                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
738                                 shader_bsdf_blur(kg, &sd, blur_roughness);
739                         }
740                 }
741
742 #ifdef __EMISSION__
743                 /* emission */
744                 if(sd.flag & SD_EMISSION) {
745                         /* todo: is isect.t wrong here for transparent surfaces? */
746                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
747                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
748                 }
749 #endif
750
751                 /* path termination. this is a strange place to put the termination, it's
752                  * mainly due to the mixed in MIS that we use. gives too many unneeded
753                  * shader evaluations, only need emission if we are going to terminate */
754                 float probability = path_state_terminate_probability(kg, &state, throughput);
755
756                 if(probability == 0.0f) {
757                         break;
758                 }
759                 else if(probability != 1.0f) {
760                         float terminate = path_state_rng_1D(kg, rng, &state, PRNG_TERMINATE);
761
762                         if(terminate >= probability)
763                                 break;
764
765                         throughput /= probability;
766                 }
767
768 #ifdef __AO__
769                 /* ambient occlusion */
770                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
771                         kernel_path_ao(kg, &sd, &L, &state, rng, throughput);
772                 }
773 #endif
774
775 #ifdef __SUBSURFACE__
776                 /* bssrdf scatter to a different location on the same object, replacing
777                  * the closures with a diffuse BSDF */
778                 if(sd.flag & SD_BSSRDF) {
779                         float bssrdf_probability;
780                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
781
782                         /* modify throughput for picking bssrdf or bsdf */
783                         throughput *= bssrdf_probability;
784
785                         /* do bssrdf scatter step if we picked a bssrdf closure */
786                         if(sc) {
787                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
788
789                                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
790                                 float bssrdf_u, bssrdf_v;
791                                 path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
792                                 int num_hits = subsurface_scatter_multi_step(kg, &sd, bssrdf_sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
793
794                                 /* compute lighting with the BSDF closure */
795                                 for(int hit = 0; hit < num_hits; hit++) {
796                                         float3 tp = throughput;
797                                         PathState hit_state = state;
798                                         Ray hit_ray = ray;
799
800                                         hit_state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
801                                         hit_state.rng_offset += PRNG_BOUNCE_NUM;
802                                         
803                                         if(kernel_path_integrate_lighting(kg, rng, &bssrdf_sd[hit], &tp, &hit_state, &L, &hit_ray)) {
804 #ifdef __LAMP_MIS__
805                                                 hit_state.ray_t = 0.0f;
806 #endif
807
808                                                 kernel_path_indirect(kg, rng, hit_ray, tp, state.num_samples, hit_state, &L);
809
810                                                 /* for render passes, sum and reset indirect light pass variables
811                                                  * for the next samples */
812                                                 path_radiance_sum_indirect(&L);
813                                                 path_radiance_reset_indirect(&L);
814                                         }
815                                 }
816                                 break;
817                         }
818                 }
819 #endif
820                 
821                 /* Same as kernel_path_integrate_lighting(kg, rng, &sd, &throughput, &state, &L, &ray),
822                    but for CUDA the function call is slower. */
823 #ifdef __EMISSION__
824                 if(kernel_data.integrator.use_direct_light) {
825                         /* sample illumination from lights to find path contribution */
826                         if(sd.flag & SD_BSDF_HAS_EVAL) {
827                                 float light_t = path_state_rng_1D(kg, rng, &state, PRNG_LIGHT);
828                                 float light_u, light_v;
829                                 path_state_rng_2D(kg, rng, &state, PRNG_LIGHT_U, &light_u, &light_v);
830
831                                 Ray light_ray;
832                                 BsdfEval L_light;
833                                 bool is_lamp;
834
835 #ifdef __OBJECT_MOTION__
836                                 light_ray.time = sd.time;
837 #endif
838
839                                 if(direct_emission(kg, &sd, LAMP_NONE, light_t, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce, state.transparent_bounce)) {
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, &L_light, shadow, 1.0f, state.bounce, is_lamp);
846                                         }
847                                 }
848                         }
849                 }
850 #endif
851
852                 if(sd.flag & SD_BSDF) {
853                         /* sample BSDF */
854                         float bsdf_pdf;
855                         BsdfEval bsdf_eval;
856                         float3 bsdf_omega_in;
857                         differential3 bsdf_domega_in;
858                         float bsdf_u, bsdf_v;
859                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
860                         int label;
861
862                         label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
863                                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
864
865                         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
866                                 break;
867
868                         /* modify throughput */
869                         path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
870
871                         /* set labels */
872                         if(!(label & LABEL_TRANSPARENT)) {
873                                 state.ray_pdf = bsdf_pdf;
874 #ifdef __LAMP_MIS__
875                                 state.ray_t = 0.0f;
876 #endif
877                                 state.min_ray_pdf = fminf(bsdf_pdf, state.min_ray_pdf);
878                         }
879
880                         /* update path state */
881                         path_state_next(kg, &state, label);
882
883                         /* setup ray */
884                         ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
885                         ray.D = bsdf_omega_in;
886
887 #ifdef __RAY_DIFFERENTIALS__
888                         ray.dP = sd.dP;
889                         ray.dD = bsdf_domega_in;
890 #endif
891
892 #ifdef __VOLUME__
893                         /* enter/exit volume */
894                         if(label & LABEL_TRANSMIT)
895                                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
896 #endif
897
898                 }
899 #ifdef __VOLUME__
900                 else if(sd.flag & SD_HAS_ONLY_VOLUME) {
901                         /* no surface shader but have a volume shader? act transparent */
902
903                         /* update path state, count as transparent */
904                         path_state_next(kg, &state, LABEL_TRANSPARENT);
905
906                         /* setup ray position, direction stays unchanged */
907                         ray.P = ray_offset(sd.P, -sd.Ng);
908 #ifdef __RAY_DIFFERENTIALS__
909                         ray.dP = sd.dP;
910 #endif
911
912                         /* enter/exit volume */
913                         kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
914                 }
915 #endif
916                 else {
917                         /* no bsdf or volume? we're done */
918                         break;
919                 }
920
921                 /* adjust ray distance for clipping */
922                 if(state.bounce == 0)
923                         ray.t -= sd.ray_length; /* clipping works through transparent */
924                 else
925                         ray.t = FLT_MAX;
926         }
927
928         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
929
930         kernel_write_light_passes(kg, buffer, &L, sample);
931
932         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
933 }
934
935 #ifdef __BRANCHED_PATH__
936
937 ccl_device_noinline void kernel_branched_path_integrate_lighting(KernelGlobals *kg,
938         RNG *rng, ShaderData *sd, float3 throughput, float num_samples_adjust,
939         PathState *state, PathRadiance *L, ccl_global float *buffer)
940 {
941 #ifdef __EMISSION__
942         if(kernel_data.integrator.use_direct_light) {
943                 bool all = kernel_data.integrator.sample_all_lights_direct;
944                 kernel_branched_path_integrate_direct_lighting(kg, rng, sd, state, throughput, num_samples_adjust, L, all);
945         }
946 #endif
947
948         for(int i = 0; i< sd->num_closure; i++) {
949                 const ShaderClosure *sc = &sd->closure[i];
950
951                 if(!CLOSURE_IS_BSDF(sc->type))
952                         continue;
953                 /* transparency is not handled here, but in outer loop */
954                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
955                         continue;
956
957                 int num_samples;
958
959                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
960                         num_samples = kernel_data.integrator.diffuse_samples;
961                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
962                         num_samples = 1;
963                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
964                         num_samples = kernel_data.integrator.glossy_samples;
965                 else
966                         num_samples = kernel_data.integrator.transmission_samples;
967
968                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
969
970                 float num_samples_inv = num_samples_adjust/num_samples;
971                 RNG bsdf_rng = cmj_hash(*rng, i);
972
973                 for(int j = 0; j < num_samples; j++) {
974                         /* sample BSDF */
975                         float bsdf_pdf;
976                         BsdfEval bsdf_eval;
977                         float3 bsdf_omega_in;
978                         differential3 bsdf_domega_in;
979                         float bsdf_u, bsdf_v;
980                         path_branched_rng_2D(kg, &bsdf_rng, state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
981                         int label;
982
983                         label = shader_bsdf_sample_closure(kg, sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
984                                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
985
986                         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
987                                 continue;
988
989                         /* modify throughput */
990                         float3 tp = throughput;
991                         path_radiance_bsdf_bounce(L, &tp, &bsdf_eval, bsdf_pdf, state->bounce, label);
992
993                         /* modify path state */
994                         PathState ps = *state;
995                         path_state_next(kg, &ps, label);
996
997                         /* setup ray */
998                         Ray bsdf_ray;
999
1000                         bsdf_ray.P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
1001                         bsdf_ray.D = bsdf_omega_in;
1002                         bsdf_ray.t = FLT_MAX;
1003 #ifdef __RAY_DIFFERENTIALS__
1004                         bsdf_ray.dP = sd->dP;
1005                         bsdf_ray.dD = bsdf_domega_in;
1006 #endif
1007 #ifdef __OBJECT_MOTION__
1008                         bsdf_ray.time = sd->time;
1009 #endif
1010
1011 #ifdef __VOLUME__
1012                         /* enter/exit volume */
1013                         if(label & LABEL_TRANSMIT)
1014                                 kernel_volume_stack_enter_exit(kg, sd, ps.volume_stack);
1015 #endif
1016
1017                         /* branch RNG state */
1018                         path_state_branch(&ps, j, num_samples);
1019
1020                         /* set MIS state */
1021                         ps.min_ray_pdf = fminf(bsdf_pdf, FLT_MAX);
1022                         ps.ray_pdf = bsdf_pdf;
1023 #ifdef __LAMP_MIS__
1024                         ps.ray_t = 0.0f;
1025 #endif
1026
1027                         kernel_path_indirect(kg, rng, bsdf_ray, tp*num_samples_inv, num_samples, ps, L);
1028
1029                         /* for render passes, sum and reset indirect light pass variables
1030                          * for the next samples */
1031                         path_radiance_sum_indirect(L);
1032                         path_radiance_reset_indirect(L);
1033                 }
1034         }
1035 }
1036
1037 ccl_device float4 kernel_branched_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
1038 {
1039         /* initialize */
1040         PathRadiance L;
1041         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
1042         float L_transparent = 0.0f;
1043
1044         path_radiance_init(&L, kernel_data.film.use_light_pass);
1045
1046         PathState state;
1047         path_state_init(kg, &state, rng, sample);
1048
1049         for(;;) {
1050                 /* intersect scene */
1051                 Intersection isect;
1052                 uint visibility = path_state_ray_visibility(kg, &state);
1053
1054 #ifdef __HAIR__
1055                 float difl = 0.0f, extmax = 0.0f;
1056                 uint lcg_state = 0;
1057
1058                 if(kernel_data.bvh.have_curves) {
1059                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
1060                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
1061                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
1062                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
1063                         }
1064
1065                         extmax = kernel_data.curve.maximum_width;
1066                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
1067                 }
1068
1069                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
1070 #else
1071                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
1072 #endif
1073
1074 #ifdef __VOLUME__
1075                 /* volume attenuation, emission, scatter */
1076                 if(state.volume_stack[0].shader != SHADER_NONE) {
1077                         Ray volume_ray = ray;
1078                         volume_ray.t = (hit)? isect.t: FLT_MAX;
1079
1080 #ifdef __KERNEL_CPU__
1081                         /* decoupled ray marching only supported on CPU */
1082                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
1083
1084                         /* cache steps along volume for repeated sampling */
1085                         VolumeSegment volume_segment;
1086                         ShaderData volume_sd;
1087
1088                         shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
1089                         kernel_volume_decoupled_record(kg, &state,
1090                                 &volume_ray, &volume_sd, &volume_segment, heterogeneous);
1091
1092                         /* sample scattering */
1093                         int num_samples = kernel_data.integrator.volume_samples;
1094                         float num_samples_inv = 1.0f/num_samples;
1095
1096                         for(int j = 0; j < num_samples; j++) {
1097                                 /* workaround to fix correlation bug in T38710, can find better solution
1098                                  * in random number generator later, for now this is done here to not impact
1099                                  * performance of rendering without volumes */
1100                                 RNG tmp_rng = cmj_hash(*rng, state.rng_offset);
1101
1102                                 PathState ps = state;
1103                                 Ray pray = ray;
1104                                 float3 tp = throughput;
1105
1106                                 /* branch RNG state */
1107                                 path_state_branch(&ps, j, num_samples);
1108
1109                                 VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
1110                                         &ps, &volume_ray, &volume_sd, &tp, &tmp_rng, &volume_segment);
1111                                 
1112                                 if(result == VOLUME_PATH_SCATTERED) {
1113                                         /* todo: use all-light sampling */
1114                                         if(kernel_path_integrate_scatter_lighting(kg, rng, &volume_sd, &tp, &ps, &L, &pray, num_samples_inv)) {
1115                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
1116
1117                                                 /* for render passes, sum and reset indirect light pass variables
1118                                                  * for the next samples */
1119                                                 path_radiance_sum_indirect(&L);
1120                                                 path_radiance_reset_indirect(&L);
1121                                         }
1122                                 }
1123                         }
1124
1125                         /* emission and transmittance */
1126                         if(volume_segment.closure_flag & SD_EMISSION)
1127                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
1128                         throughput *= volume_segment.accum_transmittance;
1129
1130                         /* free cached steps */
1131                         kernel_volume_decoupled_free(kg, &volume_segment);
1132 #else
1133                         /* GPU: no decoupled ray marching, scatter probalistically */
1134                         int num_samples = kernel_data.integrator.volume_samples;
1135                         float num_samples_inv = 1.0f/num_samples;
1136
1137                         /* todo: we should cache the shader evaluations from stepping
1138                          * through the volume, for now we redo them multiple times */
1139
1140                         for(int j = 0; j < num_samples; j++) {
1141                                 PathState ps = state;
1142                                 Ray pray = ray;
1143                                 ShaderData volume_sd;
1144                                 float3 tp = throughput;
1145
1146                                 /* branch RNG state */
1147                                 path_state_branch(&ps, j, num_samples);
1148
1149                                 VolumeIntegrateResult result = kernel_volume_integrate(kg, &ps,
1150                                         &volume_sd, &volume_ray, &L, &tp, rng);
1151                                 
1152                                 if(result == VOLUME_PATH_SCATTERED) {
1153                                         /* todo: use all-light sampling */
1154                                         if(kernel_path_integrate_scatter_lighting(kg, rng, &volume_sd, &tp, &ps, &L, &pray, num_samples_inv)) {
1155                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
1156
1157                                                 /* for render passes, sum and reset indirect light pass variables
1158                                                  * for the next samples */
1159                                                 path_radiance_sum_indirect(&L);
1160                                                 path_radiance_reset_indirect(&L);
1161                                         }
1162                                 }
1163                         }
1164
1165                         /* todo: avoid this calculation using decoupled ray marching */
1166                         kernel_volume_shadow(kg, &state, &volume_ray, &throughput);
1167 #endif
1168                 }
1169 #endif
1170
1171                 if(!hit) {
1172                         /* eval background shader if nothing hit */
1173                         if(kernel_data.background.transparent) {
1174                                 L_transparent += average(throughput);
1175
1176 #ifdef __PASSES__
1177                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
1178 #endif
1179                                         break;
1180                         }
1181
1182 #ifdef __BACKGROUND__
1183                         /* sample background shader */
1184                         float3 L_background = indirect_background(kg, &state, &ray);
1185                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
1186 #endif
1187
1188                         break;
1189                 }
1190
1191                 /* setup shading */
1192                 ShaderData sd;
1193                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
1194                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
1195                 shader_merge_closures(&sd);
1196
1197                 /* holdout */
1198 #ifdef __HOLDOUT__
1199                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
1200                         if(kernel_data.background.transparent) {
1201                                 float3 holdout_weight;
1202                                 
1203                                 if(sd.flag & SD_HOLDOUT_MASK)
1204                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
1205                                 else
1206                                         holdout_weight = shader_holdout_eval(kg, &sd);
1207
1208                                 /* any throughput is ok, should all be identical here */
1209                                 L_transparent += average(holdout_weight*throughput);
1210                         }
1211
1212                         if(sd.flag & SD_HOLDOUT_MASK)
1213                                 break;
1214                 }
1215 #endif
1216
1217                 /* holdout mask objects do not write data passes */
1218                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
1219
1220 #ifdef __EMISSION__
1221                 /* emission */
1222                 if(sd.flag & SD_EMISSION) {
1223                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
1224                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
1225                 }
1226 #endif
1227
1228                 /* transparency termination */
1229                 if(state.flag & PATH_RAY_TRANSPARENT) {
1230                         /* path termination. this is a strange place to put the termination, it's
1231                          * mainly due to the mixed in MIS that we use. gives too many unneeded
1232                          * shader evaluations, only need emission if we are going to terminate */
1233                         float probability = path_state_terminate_probability(kg, &state, throughput);
1234
1235                         if(probability == 0.0f) {
1236                                 break;
1237                         }
1238                         else if(probability != 1.0f) {
1239                                 float terminate = path_state_rng_1D(kg, rng, &state, PRNG_TERMINATE);
1240
1241                                 if(terminate >= probability)
1242                                         break;
1243
1244                                 throughput /= probability;
1245                         }
1246                 }
1247
1248 #ifdef __AO__
1249                 /* ambient occlusion */
1250                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
1251                         int num_samples = kernel_data.integrator.ao_samples;
1252                         float num_samples_inv = 1.0f/num_samples;
1253                         float ao_factor = kernel_data.background.ao_factor;
1254                         float3 ao_N;
1255                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
1256                         float3 ao_alpha = shader_bsdf_alpha(kg, &sd);
1257
1258                         for(int j = 0; j < num_samples; j++) {
1259                                 float bsdf_u, bsdf_v;
1260                                 path_branched_rng_2D(kg, rng, &state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
1261
1262                                 float3 ao_D;
1263                                 float ao_pdf;
1264
1265                                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
1266
1267                                 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
1268                                         Ray light_ray;
1269                                         float3 ao_shadow;
1270
1271                                         light_ray.P = ray_offset(sd.P, sd.Ng);
1272                                         light_ray.D = ao_D;
1273                                         light_ray.t = kernel_data.background.ao_distance;
1274 #ifdef __OBJECT_MOTION__
1275                                         light_ray.time = sd.time;
1276 #endif
1277                                         light_ray.dP = sd.dP;
1278                                         light_ray.dD = differential3_zero();
1279
1280                                         if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
1281                                                 path_radiance_accum_ao(&L, throughput*num_samples_inv, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
1282                                 }
1283                         }
1284                 }
1285 #endif
1286
1287 #ifdef __SUBSURFACE__
1288                 /* bssrdf scatter to a different location on the same object */
1289                 if(sd.flag & SD_BSSRDF) {
1290                         for(int i = 0; i< sd.num_closure; i++) {
1291                                 ShaderClosure *sc = &sd.closure[i];
1292
1293                                 if(!CLOSURE_IS_BSSRDF(sc->type))
1294                                         continue;
1295
1296                                 /* set up random number generator */
1297                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
1298                                 int num_samples = kernel_data.integrator.subsurface_samples;
1299                                 float num_samples_inv = 1.0f/num_samples;
1300                                 RNG bssrdf_rng = cmj_hash(*rng, i);
1301
1302                                 state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
1303
1304                                 /* do subsurface scatter step with copy of shader data, this will
1305                                  * replace the BSSRDF with a diffuse BSDF closure */
1306                                 for(int j = 0; j < num_samples; j++) {
1307                                                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
1308                                                 float bssrdf_u, bssrdf_v;
1309                                                 path_branched_rng_2D(kg, &bssrdf_rng, &state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
1310                                                 int num_hits = subsurface_scatter_multi_step(kg, &sd, bssrdf_sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, true);
1311
1312                                                 /* compute lighting with the BSDF closure */
1313                                                 for(int hit = 0; hit < num_hits; hit++) {
1314                                                         PathState hit_state = state;
1315
1316                                                         path_state_branch(&hit_state, j, num_samples);
1317
1318                                                         kernel_branched_path_integrate_lighting(kg, rng,
1319                                                                 &bssrdf_sd[hit], throughput, num_samples_inv,
1320                                                                 &hit_state, &L, buffer);
1321                                                 }
1322                                 }
1323
1324                                 state.flag &= ~PATH_RAY_BSSRDF_ANCESTOR;
1325                         }
1326                 }
1327 #endif
1328
1329                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
1330                         PathState hit_state = state;
1331
1332                         /* lighting */
1333                         kernel_branched_path_integrate_lighting(kg, rng,
1334                                 &sd, throughput, 1.0f, &hit_state, &L, buffer);
1335
1336                         /* continue in case of transparency */
1337                         throughput *= shader_bsdf_transparency(kg, &sd);
1338
1339                         if(is_zero(throughput))
1340                                 break;
1341                 }
1342
1343                 path_state_next(kg, &state, LABEL_TRANSPARENT);
1344                 ray.P = ray_offset(sd.P, -sd.Ng);
1345                 ray.t -= sd.ray_length; /* clipping works through transparent */
1346
1347 #ifdef __VOLUME__
1348                 /* enter/exit volume */
1349                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
1350 #endif
1351         }
1352
1353         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
1354
1355         kernel_write_light_passes(kg, buffer, &L, sample);
1356
1357         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1358 }
1359
1360 #endif
1361
1362 ccl_device_inline void kernel_path_trace_setup(KernelGlobals *kg, ccl_global uint *rng_state, int sample, int x, int y, RNG *rng, Ray *ray)
1363 {
1364         float filter_u;
1365         float filter_v;
1366 #ifdef __CMJ__
1367         int num_samples = kernel_data.integrator.aa_samples;
1368 #else
1369         int num_samples = 0;
1370 #endif
1371
1372         path_rng_init(kg, rng_state, sample, num_samples, rng, x, y, &filter_u, &filter_v);
1373
1374         /* sample camera ray */
1375
1376         float lens_u = 0.0f, lens_v = 0.0f;
1377
1378         if(kernel_data.cam.aperturesize > 0.0f)
1379                 path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1380
1381         float time = 0.0f;
1382
1383 #ifdef __CAMERA_MOTION__
1384         if(kernel_data.cam.shuttertime != -1.0f)
1385                 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1386 #endif
1387
1388         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, ray);
1389 }
1390
1391 ccl_device void kernel_path_trace(KernelGlobals *kg,
1392         ccl_global float *buffer, ccl_global uint *rng_state,
1393         int sample, int x, int y, int offset, int stride)
1394 {
1395         /* buffer offset */
1396         int index = offset + x + y*stride;
1397         int pass_stride = kernel_data.film.pass_stride;
1398
1399         rng_state += index;
1400         buffer += index*pass_stride;
1401
1402         /* initialize random numbers and ray */
1403         RNG rng;
1404         Ray ray;
1405
1406         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1407
1408         /* integrate */
1409         float4 L;
1410
1411         if(ray.t != 0.0f)
1412                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
1413         else
1414                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1415
1416         /* accumulate result in output buffer */
1417         kernel_write_pass_float4(buffer, sample, L);
1418
1419         path_rng_end(kg, rng_state, rng);
1420 }
1421
1422 #ifdef __BRANCHED_PATH__
1423 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
1424         ccl_global float *buffer, ccl_global uint *rng_state,
1425         int sample, int x, int y, int offset, int stride)
1426 {
1427         /* buffer offset */
1428         int index = offset + x + y*stride;
1429         int pass_stride = kernel_data.film.pass_stride;
1430
1431         rng_state += index;
1432         buffer += index*pass_stride;
1433
1434         /* initialize random numbers and ray */
1435         RNG rng;
1436         Ray ray;
1437
1438         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1439
1440         /* integrate */
1441         float4 L;
1442
1443         if(ray.t != 0.0f)
1444                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
1445         else
1446                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1447
1448         /* accumulate result in output buffer */
1449         kernel_write_pass_float4(buffer, sample, L);
1450
1451         path_rng_end(kg, rng_state, rng);
1452 }
1453 #endif
1454
1455 CCL_NAMESPACE_END
1456