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