000b241f544fea9fcf44ab2cd8eff4e2d5338308
[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         kernel_branched_path_integrate_direct_lighting(kg, rng, sd, state, throughput, num_samples_adjust, L, true);
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_NO_ID) {
1092                         Ray volume_ray = ray;
1093                         volume_ray.t = (hit)? isect.t: FLT_MAX;
1094
1095                         int num_samples = kernel_data.integrator.volume_samples;
1096                         float num_samples_inv = 1.0f/num_samples;
1097                         float3 avg_tp = make_float3(0.0f, 0.0f, 0.0f);
1098
1099                         /* todo: we should cache the shader evaluations from stepping
1100                          * through the volume, for now we redo them multiple times */
1101
1102                         for(int j = 0; j < num_samples; j++) {
1103                                 PathState ps = state;
1104                                 Ray pray = ray;
1105                                 ShaderData volume_sd;
1106                                 float3 tp = throughput;
1107
1108                                 /* branch RNG state */
1109                                 path_state_branch(&ps, j, num_samples);
1110
1111                                 VolumeIntegrateResult result = kernel_volume_integrate(kg, &ps,
1112                                         &volume_sd, &volume_ray, &L, &tp, rng);
1113                                 
1114                                 if(result == VOLUME_PATH_SCATTERED) {
1115                                         /* todo: use all-light sampling */
1116                                         if(kernel_path_integrate_scatter_lighting(kg, rng, &volume_sd, &tp, &ps, &L, &pray, num_samples_inv)) {
1117                                                 kernel_path_indirect(kg, rng, pray, buffer, tp*num_samples_inv, num_samples, ps, &L);
1118
1119                                                 /* for render passes, sum and reset indirect light pass variables
1120                                                  * for the next samples */
1121                                                 path_radiance_sum_indirect(&L);
1122                                                 path_radiance_reset_indirect(&L);
1123                                         }
1124                                 }
1125                                 else
1126                                         avg_tp += tp;
1127                         }
1128
1129                         throughput = avg_tp * num_samples_inv;
1130                 }
1131 #endif
1132
1133                 if(!hit) {
1134                         /* eval background shader if nothing hit */
1135                         if(kernel_data.background.transparent) {
1136                                 L_transparent += average(throughput);
1137
1138 #ifdef __PASSES__
1139                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
1140 #endif
1141                                         break;
1142                         }
1143
1144 #ifdef __BACKGROUND__
1145                         /* sample background shader */
1146                         float3 L_background = indirect_background(kg, &ray, state.flag, state.ray_pdf, state.bounce);
1147                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
1148 #endif
1149
1150                         break;
1151                 }
1152
1153                 /* setup shading */
1154                 ShaderData sd;
1155                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
1156                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
1157                 shader_merge_closures(&sd);
1158
1159                 /* holdout */
1160 #ifdef __HOLDOUT__
1161                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
1162                         if(kernel_data.background.transparent) {
1163                                 float3 holdout_weight;
1164                                 
1165                                 if(sd.flag & SD_HOLDOUT_MASK)
1166                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
1167                                 else
1168                                         holdout_weight = shader_holdout_eval(kg, &sd);
1169
1170                                 /* any throughput is ok, should all be identical here */
1171                                 L_transparent += average(holdout_weight*throughput);
1172                         }
1173
1174                         if(sd.flag & SD_HOLDOUT_MASK)
1175                                 break;
1176                 }
1177 #endif
1178
1179                 /* holdout mask objects do not write data passes */
1180                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
1181
1182 #ifdef __EMISSION__
1183                 /* emission */
1184                 if(sd.flag & SD_EMISSION) {
1185                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
1186                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
1187                 }
1188 #endif
1189
1190                 /* transparency termination */
1191                 if(state.flag & PATH_RAY_TRANSPARENT) {
1192                         /* path termination. this is a strange place to put the termination, it's
1193                          * mainly due to the mixed in MIS that we use. gives too many unneeded
1194                          * shader evaluations, only need emission if we are going to terminate */
1195                         float probability = path_state_terminate_probability(kg, &state, throughput);
1196
1197                         if(probability == 0.0f) {
1198                                 break;
1199                         }
1200                         else if(probability != 1.0f) {
1201                                 float terminate = path_state_rng_1D(kg, rng, &state, PRNG_TERMINATE);
1202
1203                                 if(terminate >= probability)
1204                                         break;
1205
1206                                 throughput /= probability;
1207                         }
1208                 }
1209
1210 #ifdef __AO__
1211                 /* ambient occlusion */
1212                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
1213                         int num_samples = kernel_data.integrator.ao_samples;
1214                         float num_samples_inv = 1.0f/num_samples;
1215                         float ao_factor = kernel_data.background.ao_factor;
1216                         float3 ao_N;
1217                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
1218                         float3 ao_alpha = shader_bsdf_alpha(kg, &sd);
1219
1220                         for(int j = 0; j < num_samples; j++) {
1221                                 float bsdf_u, bsdf_v;
1222                                 path_branched_rng_2D(kg, rng, &state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
1223
1224                                 float3 ao_D;
1225                                 float ao_pdf;
1226
1227                                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
1228
1229                                 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
1230                                         Ray light_ray;
1231                                         float3 ao_shadow;
1232
1233                                         light_ray.P = ray_offset(sd.P, sd.Ng);
1234                                         light_ray.D = ao_D;
1235                                         light_ray.t = kernel_data.background.ao_distance;
1236 #ifdef __OBJECT_MOTION__
1237                                         light_ray.time = sd.time;
1238 #endif
1239                                         light_ray.dP = sd.dP;
1240                                         light_ray.dD = differential3_zero();
1241
1242                                         if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
1243                                                 path_radiance_accum_ao(&L, throughput*num_samples_inv, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
1244                                 }
1245                         }
1246                 }
1247 #endif
1248
1249 #ifdef __SUBSURFACE__
1250                 /* bssrdf scatter to a different location on the same object */
1251                 if(sd.flag & SD_BSSRDF) {
1252                         for(int i = 0; i< sd.num_closure; i++) {
1253                                 ShaderClosure *sc = &sd.closure[i];
1254
1255                                 if(!CLOSURE_IS_BSSRDF(sc->type))
1256                                         continue;
1257
1258                                 /* set up random number generator */
1259                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
1260                                 int num_samples = kernel_data.integrator.subsurface_samples;
1261                                 float num_samples_inv = 1.0f/num_samples;
1262                                 RNG bssrdf_rng = cmj_hash(*rng, i);
1263
1264                                 state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
1265
1266                                 /* do subsurface scatter step with copy of shader data, this will
1267                                  * replace the BSSRDF with a diffuse BSDF closure */
1268                                 for(int j = 0; j < num_samples; j++) {
1269                                                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
1270                                                 float bssrdf_u, bssrdf_v;
1271                                                 path_branched_rng_2D(kg, &bssrdf_rng, &state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
1272                                                 int num_hits = subsurface_scatter_multi_step(kg, &sd, bssrdf_sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, true);
1273
1274                                                 /* compute lighting with the BSDF closure */
1275                                                 for(int hit = 0; hit < num_hits; hit++) {
1276                                                         PathState hit_state = state;
1277
1278                                                         path_state_branch(&hit_state, j, num_samples);
1279
1280                                                         kernel_branched_path_integrate_lighting(kg, rng,
1281                                                                 &bssrdf_sd[hit], throughput, num_samples_inv,
1282                                                                 &hit_state, &L, buffer);
1283                                                 }
1284                                 }
1285
1286                                 state.flag &= ~PATH_RAY_BSSRDF_ANCESTOR;
1287                         }
1288                 }
1289 #endif
1290
1291                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
1292                         PathState hit_state = state;
1293
1294                         /* lighting */
1295                         kernel_branched_path_integrate_lighting(kg, rng,
1296                                 &sd, throughput, 1.0f, &hit_state, &L, buffer);
1297
1298                         /* continue in case of transparency */
1299                         throughput *= shader_bsdf_transparency(kg, &sd);
1300
1301                         if(is_zero(throughput))
1302                                 break;
1303                 }
1304
1305                 path_state_next(kg, &state, LABEL_TRANSPARENT);
1306                 ray.P = ray_offset(sd.P, -sd.Ng);
1307                 ray.t -= sd.ray_length; /* clipping works through transparent */
1308
1309 #ifdef __VOLUME__
1310                 /* enter/exit volume */
1311                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
1312 #endif
1313         }
1314
1315         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
1316
1317         kernel_write_light_passes(kg, buffer, &L, sample);
1318
1319         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1320 }
1321
1322 #endif
1323
1324 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)
1325 {
1326         float filter_u;
1327         float filter_v;
1328 #ifdef __CMJ__
1329         int num_samples = kernel_data.integrator.aa_samples;
1330 #else
1331         int num_samples = 0;
1332 #endif
1333
1334         path_rng_init(kg, rng_state, sample, num_samples, rng, x, y, &filter_u, &filter_v);
1335
1336         /* sample camera ray */
1337
1338         float lens_u = 0.0f, lens_v = 0.0f;
1339
1340         if(kernel_data.cam.aperturesize > 0.0f)
1341                 path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1342
1343         float time = 0.0f;
1344
1345 #ifdef __CAMERA_MOTION__
1346         if(kernel_data.cam.shuttertime != -1.0f)
1347                 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1348 #endif
1349
1350         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, ray);
1351 }
1352
1353 ccl_device void kernel_path_trace(KernelGlobals *kg,
1354         ccl_global float *buffer, ccl_global uint *rng_state,
1355         int sample, int x, int y, int offset, int stride)
1356 {
1357         /* buffer offset */
1358         int index = offset + x + y*stride;
1359         int pass_stride = kernel_data.film.pass_stride;
1360
1361         rng_state += index;
1362         buffer += index*pass_stride;
1363
1364         /* initialize random numbers and ray */
1365         RNG rng;
1366         Ray ray;
1367
1368         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1369
1370         /* integrate */
1371         float4 L;
1372
1373         if(ray.t != 0.0f)
1374                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
1375         else
1376                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1377
1378         /* accumulate result in output buffer */
1379         kernel_write_pass_float4(buffer, sample, L);
1380
1381         path_rng_end(kg, rng_state, rng);
1382 }
1383
1384 #ifdef __BRANCHED_PATH__
1385 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
1386         ccl_global float *buffer, ccl_global uint *rng_state,
1387         int sample, int x, int y, int offset, int stride)
1388 {
1389         /* buffer offset */
1390         int index = offset + x + y*stride;
1391         int pass_stride = kernel_data.film.pass_stride;
1392
1393         rng_state += index;
1394         buffer += index*pass_stride;
1395
1396         /* initialize random numbers and ray */
1397         RNG rng;
1398         Ray ray;
1399
1400         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1401
1402         /* integrate */
1403         float4 L;
1404
1405         if(ray.t != 0.0f)
1406                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
1407         else
1408                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1409
1410         /* accumulate result in output buffer */
1411         kernel_write_pass_float4(buffer, sample, L);
1412
1413         path_rng_end(kg, rng_state, rng);
1414 }
1415 #endif
1416
1417 CCL_NAMESPACE_END
1418