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