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