Cycles code refactor: move geometry related kernel files into own directory.
[blender-staging.git] / intern / cycles / kernel / kernel_path.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 #ifdef __OSL__
18 #include "osl_shader.h"
19 #endif
20
21 #include "kernel_random.h"
22
23 #include "geom/geom_bvh.h"
24
25 #include "kernel_differential.h"
26 #include "kernel_montecarlo.h"
27 #include "kernel_projection.h"
28 #include "kernel_primitive.h"
29 #include "kernel_projection.h"
30 #include "kernel_accumulate.h"
31 #include "kernel_camera.h"
32 #include "kernel_shader.h"
33 #include "kernel_light.h"
34 #include "kernel_emission.h"
35 #include "kernel_passes.h"
36
37 #ifdef __SUBSURFACE__
38 #include "kernel_subsurface.h"
39 #endif
40
41 #ifdef __VOLUME__
42 #include "kernel_volume.h"
43 #endif
44
45 #include "kernel_path_state.h"
46 #include "kernel_shadow.h"
47
48 CCL_NAMESPACE_BEGIN
49
50 #ifdef __VOLUME__
51
52 ccl_device_inline bool kernel_path_integrate_scatter_lighting(KernelGlobals *kg, RNG *rng,
53         ShaderData *sd, float3 *throughput, PathState *state, PathRadiance *L, Ray *ray,
54         float num_samples_adjust)
55 {
56 #ifdef __EMISSION__
57         if(kernel_data.integrator.use_direct_light) {
58                 /* sample illumination from lights to find path contribution */
59                 if(sd->flag & SD_BSDF_HAS_EVAL) {
60                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
61 #ifdef __MULTI_CLOSURE__
62                         float light_o = 0.0f;
63 #else
64                         float light_o = path_state_rng_1D(kg, rng, state, PRNG_LIGHT_F);
65 #endif
66                         float light_u, light_v;
67                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
68
69                         Ray light_ray;
70                         BsdfEval L_light;
71                         bool is_lamp;
72
73 #ifdef __OBJECT_MOTION__
74                         light_ray.time = sd->time;
75 #endif
76
77                         if(direct_emission(kg, sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce)) {
78                                 /* trace shadow ray */
79                                 float3 shadow;
80
81                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
82                                         /* accumulate */
83                                         path_radiance_accum_light(L, *throughput * num_samples_adjust, &L_light, shadow, 1.0f, state->bounce, is_lamp);
84                                 }
85                         }
86                 }
87         }
88 #endif
89
90         /* sample phase function */
91         float phase_pdf;
92         BsdfEval phase_eval;
93         float3 phase_omega_in;
94         differential3 phase_domega_in;
95         float phase_u, phase_v;
96         path_state_rng_2D(kg, rng, state, PRNG_PHASE_U, &phase_u, &phase_v);
97         int label;
98
99         label = shader_volume_phase_sample(kg, sd, phase_u, phase_v, &phase_eval,
100                 &phase_omega_in, &phase_domega_in, &phase_pdf);
101
102         if(phase_pdf == 0.0f || bsdf_eval_is_zero(&phase_eval))
103                 return false;
104         
105         /* modify throughput */
106         path_radiance_bsdf_bounce(L, throughput, &phase_eval, phase_pdf, state->bounce, label);
107
108         /* set labels */
109         state->ray_pdf = phase_pdf;
110 #ifdef __LAMP_MIS__
111         state->ray_t = 0.0f;
112 #endif
113         state->min_ray_pdf = fminf(phase_pdf, state->min_ray_pdf);
114
115         /* update path state */
116         path_state_next(kg, state, label);
117
118         /* setup ray */
119         ray->P = sd->P;
120         ray->D = phase_omega_in;
121         ray->t = FLT_MAX;
122
123 #ifdef __RAY_DIFFERENTIALS__
124         ray->dP = sd->dP;
125         ray->dD = phase_domega_in;
126 #endif
127
128         return true;
129 }
130
131 #endif
132
133 #if defined(__BRANCHED_PATH__) || defined(__SUBSURFACE__)
134
135 ccl_device void kernel_branched_path_integrate_direct_lighting(KernelGlobals *kg, RNG *rng,
136         ShaderData *sd, PathState *state, float3 throughput, float num_samples_adjust, PathRadiance *L, bool sample_all_lights)
137 {
138         /* sample illumination from lights to find path contribution */
139         if(sd->flag & SD_BSDF_HAS_EVAL) {
140                 Ray light_ray;
141                 BsdfEval L_light;
142                 bool is_lamp;
143
144 #ifdef __OBJECT_MOTION__
145                 light_ray.time = sd->time;
146 #endif
147
148                 if(sample_all_lights) {
149                         /* lamp sampling */
150                         for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
151                                 int num_samples = ceil_to_int(num_samples_adjust*light_select_num_samples(kg, i));
152                                 float num_samples_inv = num_samples_adjust/(num_samples*kernel_data.integrator.num_all_lights);
153                                 RNG lamp_rng = cmj_hash(*rng, i);
154
155                                 if(kernel_data.integrator.pdf_triangles != 0.0f)
156                                         num_samples_inv *= 0.5f;
157
158                                 for(int j = 0; j < num_samples; j++) {
159                                         float light_u, light_v;
160                                         path_branched_rng_2D(kg, &lamp_rng, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
161
162                                         if(direct_emission(kg, sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce)) {
163                                                 /* trace shadow ray */
164                                                 float3 shadow;
165
166                                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
167                                                         /* accumulate */
168                                                         path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state->bounce, is_lamp);
169                                                 }
170                                         }
171                                 }
172                         }
173
174                         /* mesh light sampling */
175                         if(kernel_data.integrator.pdf_triangles != 0.0f) {
176                                 int num_samples = ceil_to_int(num_samples_adjust*kernel_data.integrator.mesh_light_samples);
177                                 float num_samples_inv = num_samples_adjust/num_samples;
178
179                                 if(kernel_data.integrator.num_all_lights)
180                                         num_samples_inv *= 0.5f;
181
182                                 for(int j = 0; j < num_samples; j++) {
183                                         float light_t = path_branched_rng_1D(kg, rng, state, j, num_samples, PRNG_LIGHT);
184                                         float light_u, light_v;
185                                         path_branched_rng_2D(kg, rng, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
186
187                                         /* only sample triangle lights */
188                                         if(kernel_data.integrator.num_all_lights)
189                                                 light_t = 0.5f*light_t;
190
191                                         if(direct_emission(kg, sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce)) {
192                                                 /* trace shadow ray */
193                                                 float3 shadow;
194
195                                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
196                                                         /* accumulate */
197                                                         path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state->bounce, is_lamp);
198                                                 }
199                                         }
200                                 }
201                         }
202                 }
203                 else {
204                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
205                         float light_u, light_v;
206                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
207
208                         /* sample random light */
209                         if(direct_emission(kg, sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce)) {
210                                 /* trace shadow ray */
211                                 float3 shadow;
212
213                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
214                                         /* accumulate */
215                                         path_radiance_accum_light(L, throughput, &L_light, shadow, 1.0f, state->bounce, is_lamp);
216                                 }
217                         }
218                 }
219         }
220 }
221
222 ccl_device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, Ray ray, ccl_global float *buffer,
223         float3 throughput, int num_samples, PathState state, PathRadiance *L)
224 {
225         /* path iteration */
226         for(;;) {
227                 /* intersect scene */
228                 Intersection isect;
229                 uint visibility = path_state_ray_visibility(kg, &state);
230 #ifdef __HAIR__
231                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
232 #else
233                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
234 #endif
235
236 #ifdef __LAMP_MIS__
237                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
238                         /* ray starting from previous non-transparent bounce */
239                         Ray light_ray;
240
241                         light_ray.P = ray.P - state.ray_t*ray.D;
242                         state.ray_t += isect.t;
243                         light_ray.D = ray.D;
244                         light_ray.t = state.ray_t;
245                         light_ray.time = ray.time;
246                         light_ray.dD = ray.dD;
247                         light_ray.dP = ray.dP;
248
249                         /* intersect with lamp */
250                         float light_t = path_state_rng_1D(kg, rng, &state, PRNG_LIGHT);
251                         float3 emission;
252
253                         if(indirect_lamp_emission(kg, &light_ray, state.flag, state.ray_pdf, light_t, &emission, state.bounce))
254                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
255                 }
256 #endif
257
258 #ifdef __VOLUME__
259                 /* volume attenuation, emission, scatter */
260                 if(state.volume_stack[0].shader != SHADER_NO_ID) {
261                         Ray volume_ray = ray;
262                         volume_ray.t = (hit)? isect.t: FLT_MAX;
263
264                         ShaderData volume_sd;
265                         VolumeIntegrateResult result = kernel_volume_integrate(kg, &state,
266                                 &volume_sd, &volume_ray, L, &throughput, rng);
267
268                         if(result == VOLUME_PATH_SCATTERED) {
269                                 if(kernel_path_integrate_scatter_lighting(kg, rng, &volume_sd, &throughput, &state, L, &ray, 1.0f))
270                                         continue;
271                                 else
272                                         break;
273                         }
274                 }
275 #endif
276
277                 if(!hit) {
278 #ifdef __BACKGROUND__
279                         /* sample background shader */
280                         float3 L_background = indirect_background(kg, &ray, state.flag, state.ray_pdf, state.bounce);
281                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
282 #endif
283
284                         break;
285                 }
286
287                 /* setup shading */
288                 ShaderData sd;
289                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
290                 float rbsdf = path_state_rng_1D(kg, rng, &state, PRNG_BSDF);
291                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
292 #ifdef __BRANCHED_PATH__
293                 shader_merge_closures(&sd);
294 #endif
295
296                 /* blurring of bsdf after bounces, for rays that have a small likelihood
297                  * of following this particular path (diffuse, rough glossy) */
298                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
299                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
300
301                         if(blur_pdf < 1.0f) {
302                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
303                                 shader_bsdf_blur(kg, &sd, blur_roughness);
304                         }
305                 }
306
307 #ifdef __EMISSION__
308                 /* emission */
309                 if(sd.flag & SD_EMISSION) {
310                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
311                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
312                 }
313 #endif
314
315                 /* path termination. this is a strange place to put the termination, it's
316                  * mainly due to the mixed in MIS that we use. gives too many unneeded
317                  * shader evaluations, only need emission if we are going to terminate */
318                 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
319
320                 if(probability == 0.0f) {
321                         break;
322                 }
323                 else if(probability != 1.0f) {
324                         float terminate = path_state_rng_1D(kg, rng, &state, PRNG_TERMINATE);
325
326                         if(terminate >= probability)
327                                 break;
328
329                         throughput /= probability;
330                 }
331
332 #ifdef __AO__
333                 /* ambient occlusion */
334                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
335                         float bsdf_u, bsdf_v;
336                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
337
338                         float ao_factor = kernel_data.background.ao_factor;
339                         float3 ao_N;
340                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
341                         float3 ao_D;
342                         float ao_pdf;
343                         float3 ao_alpha = make_float3(0.0f, 0.0f, 0.0f);
344
345                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
346
347                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
348                                 Ray light_ray;
349                                 float3 ao_shadow;
350
351                                 light_ray.P = ray_offset(sd.P, sd.Ng);
352                                 light_ray.D = ao_D;
353                                 light_ray.t = kernel_data.background.ao_distance;
354 #ifdef __OBJECT_MOTION__
355                                 light_ray.time = sd.time;
356 #endif
357                                 light_ray.dP = sd.dP;
358                                 light_ray.dD = differential3_zero();
359
360                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
361                                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
362                         }
363                 }
364 #endif
365
366 #ifdef __SUBSURFACE__
367                 /* bssrdf scatter to a different location on the same object, replacing
368                  * the closures with a diffuse BSDF */
369                 if(sd.flag & SD_BSSRDF) {
370                         float bssrdf_probability;
371                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
372
373                         /* modify throughput for picking bssrdf or bsdf */
374                         throughput *= bssrdf_probability;
375
376                         /* do bssrdf scatter step if we picked a bssrdf closure */
377                         if(sc) {
378                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
379
380                                 float bssrdf_u, bssrdf_v;
381                                 path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
382                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
383
384                                 state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
385                         }
386                 }
387 #endif
388
389 #ifdef __EMISSION__
390                 if(kernel_data.integrator.use_direct_light) {
391                         bool all = kernel_data.integrator.sample_all_lights_indirect;
392                         kernel_branched_path_integrate_direct_lighting(kg, rng, &sd, &state, throughput, 1.0f, L, all);
393                 }
394 #endif
395
396                 /* no BSDF? we can stop here */
397                 if(sd.flag & SD_BSDF) {
398                         /* sample BSDF */
399                         float bsdf_pdf;
400                         BsdfEval bsdf_eval;
401                         float3 bsdf_omega_in;
402                         differential3 bsdf_domega_in;
403                         float bsdf_u, bsdf_v;
404                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
405                         int label;
406
407                         label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
408                                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
409
410                         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
411                                 break;
412
413                         /* modify throughput */
414                         path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
415
416                         /* set labels */
417                         if(!(label & LABEL_TRANSPARENT)) {
418                                 state.ray_pdf = bsdf_pdf;
419 #ifdef __LAMP_MIS__
420                                 state.ray_t = 0.0f;
421 #endif
422                                 state.min_ray_pdf = fminf(bsdf_pdf, state.min_ray_pdf);
423                         }
424
425                         /* update path state */
426                         path_state_next(kg, &state, label);
427
428                         /* setup ray */
429                         ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
430                         ray.D = bsdf_omega_in;
431                         ray.t = FLT_MAX;
432 #ifdef __RAY_DIFFERENTIALS__
433                         ray.dP = sd.dP;
434                         ray.dD = bsdf_domega_in;
435 #endif
436
437 #ifdef __VOLUME__
438                         /* enter/exit volume */
439                         if(label & LABEL_TRANSMIT)
440                                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
441 #endif
442                 }
443 #ifdef __VOLUME__
444                 else if(sd.flag & SD_HAS_ONLY_VOLUME) {
445                         /* no surface shader but have a volume shader? act transparent */
446
447                         /* update path state, count as transparent */
448                         path_state_next(kg, &state, LABEL_TRANSPARENT);
449
450                         /* setup ray position, direction stays unchanged */
451                         ray.P = ray_offset(sd.P, -sd.Ng);
452 #ifdef __RAY_DIFFERENTIALS__
453                         ray.dP = sd.dP;
454 #endif
455
456                         /* enter/exit volume */
457                         kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
458                 }
459 #endif
460                 else {
461                         /* no bsdf or volume? we're done */
462                         break;
463                 }
464         }
465 }
466
467 #endif
468
469 #ifdef __SUBSURFACE__
470
471 ccl_device_inline bool kernel_path_integrate_lighting(KernelGlobals *kg, RNG *rng,
472         ShaderData *sd, float3 *throughput, PathState *state, PathRadiance *L, Ray *ray)
473 {
474 #ifdef __EMISSION__
475         if(kernel_data.integrator.use_direct_light) {
476                 /* sample illumination from lights to find path contribution */
477                 if(sd->flag & SD_BSDF_HAS_EVAL) {
478                         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
479 #ifdef __MULTI_CLOSURE__
480                         float light_o = 0.0f;
481 #else
482                         float light_o = path_state_rng_1D(kg, rng, state, PRNG_LIGHT_F);
483 #endif
484                         float light_u, light_v;
485                         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
486
487                         Ray light_ray;
488                         BsdfEval L_light;
489                         bool is_lamp;
490
491 #ifdef __OBJECT_MOTION__
492                         light_ray.time = sd->time;
493 #endif
494
495                         if(direct_emission(kg, sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp, state->bounce)) {
496                                 /* trace shadow ray */
497                                 float3 shadow;
498
499                                 if(!shadow_blocked(kg, state, &light_ray, &shadow)) {
500                                         /* accumulate */
501                                         path_radiance_accum_light(L, *throughput, &L_light, shadow, 1.0f, state->bounce, is_lamp);
502                                 }
503                         }
504                 }
505         }
506 #endif
507
508         /* no BSDF? we can stop here */
509         if(sd->flag & SD_BSDF) {
510                 /* sample BSDF */
511                 float bsdf_pdf;
512                 BsdfEval bsdf_eval;
513                 float3 bsdf_omega_in;
514                 differential3 bsdf_domega_in;
515                 float bsdf_u, bsdf_v;
516                 path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
517                 int label;
518
519                 label = shader_bsdf_sample(kg, sd, bsdf_u, bsdf_v, &bsdf_eval,
520                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
521
522                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
523                         return false;
524
525                 /* modify throughput */
526                 path_radiance_bsdf_bounce(L, throughput, &bsdf_eval, bsdf_pdf, state->bounce, label);
527
528                 /* set labels */
529                 if(!(label & LABEL_TRANSPARENT)) {
530                         state->ray_pdf = bsdf_pdf;
531 #ifdef __LAMP_MIS__
532                         state->ray_t = 0.0f;
533 #endif
534                         state->min_ray_pdf = fminf(bsdf_pdf, state->min_ray_pdf);
535                 }
536
537                 /* update path state */
538                 path_state_next(kg, state, label);
539
540                 /* setup ray */
541                 ray->P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
542                 ray->D = bsdf_omega_in;
543
544                 if(state->bounce == 0)
545                         ray->t -= sd->ray_length; /* clipping works through transparent */
546                 else
547                         ray->t = FLT_MAX;
548
549 #ifdef __RAY_DIFFERENTIALS__
550                 ray->dP = sd->dP;
551                 ray->dD = bsdf_domega_in;
552 #endif
553
554 #ifdef __VOLUME__
555                 /* enter/exit volume */
556                 if(label & LABEL_TRANSMIT)
557                         kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
558 #endif
559                 return true;
560         }
561 #ifdef __VOLUME__
562         else if(sd->flag & SD_HAS_ONLY_VOLUME) {
563                 /* no surface shader but have a volume shader? act transparent */
564
565                 /* update path state, count as transparent */
566                 path_state_next(kg, state, LABEL_TRANSPARENT);
567
568                 /* setup ray position, direction stays unchanged */
569                 ray->P = ray_offset(sd->P, -sd->Ng);
570 #ifdef __RAY_DIFFERENTIALS__
571                 ray->dP = sd->dP;
572 #endif
573
574                 /* enter/exit volume */
575                 kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
576                 return true;
577         }
578 #endif
579         else {
580                 /* no bsdf or volume? */
581                 return false;
582         }
583 }
584
585 #endif
586
587 ccl_device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
588 {
589         /* initialize */
590         PathRadiance L;
591         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
592         float L_transparent = 0.0f;
593
594         path_radiance_init(&L, kernel_data.film.use_light_pass);
595
596         PathState state;
597         path_state_init(kg, &state, rng, sample);
598
599         /* path iteration */
600         for(;;) {
601                 /* intersect scene */
602                 Intersection isect;
603                 uint visibility = path_state_ray_visibility(kg, &state);
604
605 #ifdef __HAIR__
606                 float difl = 0.0f, extmax = 0.0f;
607                 uint lcg_state = 0;
608
609                 if(kernel_data.bvh.have_curves) {
610                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
611                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
612                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
613                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
614                         }
615
616                         extmax = kernel_data.curve.maximum_width;
617                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
618                 }
619
620                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
621 #else
622                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
623 #endif
624
625 #ifdef __LAMP_MIS__
626                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
627                         /* ray starting from previous non-transparent bounce */
628                         Ray light_ray;
629
630                         light_ray.P = ray.P - state.ray_t*ray.D;
631                         state.ray_t += isect.t;
632                         light_ray.D = ray.D;
633                         light_ray.t = state.ray_t;
634                         light_ray.time = ray.time;
635                         light_ray.dD = ray.dD;
636                         light_ray.dP = ray.dP;
637
638                         /* intersect with lamp */
639                         float light_t = path_state_rng_1D(kg, rng, &state, PRNG_LIGHT);
640                         float3 emission;
641
642                         if(indirect_lamp_emission(kg, &light_ray, state.flag, state.ray_pdf, light_t, &emission, state.bounce))
643                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
644                 }
645 #endif
646
647 #ifdef __VOLUME__
648                 /* volume attenuation, emission, scatter */
649                 if(state.volume_stack[0].shader != SHADER_NO_ID) {
650                         Ray volume_ray = ray;
651                         volume_ray.t = (hit)? isect.t: FLT_MAX;
652
653                         ShaderData volume_sd;
654                         VolumeIntegrateResult result = kernel_volume_integrate(kg, &state,
655                                 &volume_sd, &volume_ray, &L, &throughput, rng);
656
657                         if(result == VOLUME_PATH_SCATTERED) {
658                                 if(kernel_path_integrate_scatter_lighting(kg, rng, &volume_sd, &throughput, &state, &L, &ray, 1.0f))
659                                         continue;
660                                 else
661                                         break;
662                         }
663                 }
664 #endif
665
666                 if(!hit) {
667                         /* eval background shader if nothing hit */
668                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
669                                 L_transparent += average(throughput);
670
671 #ifdef __PASSES__
672                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
673 #endif
674                                         break;
675                         }
676
677 #ifdef __BACKGROUND__
678                         /* sample background shader */
679                         float3 L_background = indirect_background(kg, &ray, state.flag, state.ray_pdf, state.bounce);
680                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
681 #endif
682
683                         break;
684                 }
685
686                 /* setup shading */
687                 ShaderData sd;
688                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
689                 float rbsdf = path_state_rng_1D(kg, rng, &state, PRNG_BSDF);
690                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
691
692                 /* holdout */
693 #ifdef __HOLDOUT__
694                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
695                         if(kernel_data.background.transparent) {
696                                 float3 holdout_weight;
697                                 
698                                 if(sd.flag & SD_HOLDOUT_MASK)
699                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
700                                 else
701                                         holdout_weight = shader_holdout_eval(kg, &sd);
702
703                                 /* any throughput is ok, should all be identical here */
704                                 L_transparent += average(holdout_weight*throughput);
705                         }
706
707                         if(sd.flag & SD_HOLDOUT_MASK)
708                                 break;
709                 }
710 #endif
711
712                 /* holdout mask objects do not write data passes */
713                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
714
715                 /* blurring of bsdf after bounces, for rays that have a small likelihood
716                  * of following this particular path (diffuse, rough glossy) */
717                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
718                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
719
720                         if(blur_pdf < 1.0f) {
721                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
722                                 shader_bsdf_blur(kg, &sd, blur_roughness);
723                         }
724                 }
725
726 #ifdef __EMISSION__
727                 /* emission */
728                 if(sd.flag & SD_EMISSION) {
729                         /* todo: is isect.t wrong here for transparent surfaces? */
730                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
731                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
732                 }
733 #endif
734
735                 /* path termination. this is a strange place to put the termination, it's
736                  * mainly due to the mixed in MIS that we use. gives too many unneeded
737                  * shader evaluations, only need emission if we are going to terminate */
738                 float probability = path_state_terminate_probability(kg, &state, throughput);
739
740                 if(probability == 0.0f) {
741                         break;
742                 }
743                 else if(probability != 1.0f) {
744                         float terminate = path_state_rng_1D(kg, rng, &state, PRNG_TERMINATE);
745
746                         if(terminate >= probability)
747                                 break;
748
749                         throughput /= probability;
750                 }
751
752 #ifdef __AO__
753                 /* ambient occlusion */
754                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
755                         /* todo: solve correlation */
756                         float bsdf_u, bsdf_v;
757                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
758
759                         float ao_factor = kernel_data.background.ao_factor;
760                         float3 ao_N;
761                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
762                         float3 ao_D;
763                         float ao_pdf;
764                         float3 ao_alpha = shader_bsdf_alpha(kg, &sd);
765
766                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
767
768                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
769                                 Ray light_ray;
770                                 float3 ao_shadow;
771
772                                 light_ray.P = ray_offset(sd.P, sd.Ng);
773                                 light_ray.D = ao_D;
774                                 light_ray.t = kernel_data.background.ao_distance;
775 #ifdef __OBJECT_MOTION__
776                                 light_ray.time = sd.time;
777 #endif
778                                 light_ray.dP = sd.dP;
779                                 light_ray.dD = differential3_zero();
780
781                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
782                                         path_radiance_accum_ao(&L, throughput, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
783                         }
784                 }
785 #endif
786
787 #ifdef __SUBSURFACE__
788                 /* bssrdf scatter to a different location on the same object, replacing
789                  * the closures with a diffuse BSDF */
790                 if(sd.flag & SD_BSSRDF) {
791                         float bssrdf_probability;
792                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
793
794                         /* modify throughput for picking bssrdf or bsdf */
795                         throughput *= bssrdf_probability;
796
797                         /* do bssrdf scatter step if we picked a bssrdf closure */
798                         if(sc) {
799                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
800
801                                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
802                                 float bssrdf_u, bssrdf_v;
803                                 path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
804                                 int num_hits = subsurface_scatter_multi_step(kg, &sd, bssrdf_sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
805
806                                 /* compute lighting with the BSDF closure */
807                                 for(int hit = 0; hit < num_hits; hit++) {
808                                         float3 tp = throughput;
809                                         PathState hit_state = state;
810                                         Ray hit_ray = ray;
811
812                                         hit_state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
813                                         hit_state.rng_offset += PRNG_BOUNCE_NUM;
814                                         
815                                         if(kernel_path_integrate_lighting(kg, rng, &bssrdf_sd[hit], &tp, &hit_state, &L, &hit_ray)) {
816 #ifdef __LAMP_MIS__
817                                                 hit_state.ray_t = 0.0f;
818 #endif
819
820                                                 kernel_path_indirect(kg, rng, hit_ray, buffer, tp, state.num_samples, hit_state, &L);
821
822                                                 /* for render passes, sum and reset indirect light pass variables
823                                                  * for the next samples */
824                                                 path_radiance_sum_indirect(&L);
825                                                 path_radiance_reset_indirect(&L);
826                                         }
827                                 }
828                                 break;
829                         }
830                 }
831 #endif
832                 
833                 /* The following code is the same as in kernel_path_integrate_lighting(),
834                    but for CUDA the function call is slower. */
835 #ifdef __EMISSION__
836                 if(kernel_data.integrator.use_direct_light) {
837                         /* sample illumination from lights to find path contribution */
838                         if(sd.flag & SD_BSDF_HAS_EVAL) {
839                                 float light_t = path_state_rng_1D(kg, rng, &state, PRNG_LIGHT);
840 #ifdef __MULTI_CLOSURE__
841                                 float light_o = 0.0f;
842 #else
843                                 float light_o = path_state_rng_1D(kg, rng, &state, PRNG_LIGHT_F);
844 #endif
845                                 float light_u, light_v;
846                                 path_state_rng_2D(kg, rng, &state, PRNG_LIGHT_U, &light_u, &light_v);
847
848                                 Ray light_ray;
849                                 BsdfEval L_light;
850                                 bool is_lamp;
851
852 #ifdef __OBJECT_MOTION__
853                                 light_ray.time = sd.time;
854 #endif
855
856                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
857                                         /* trace shadow ray */
858                                         float3 shadow;
859
860                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
861                                                 /* accumulate */
862                                                 path_radiance_accum_light(&L, throughput, &L_light, shadow, 1.0f, state.bounce, is_lamp);
863                                         }
864                                 }
865                         }
866                 }
867 #endif
868
869                 if(sd.flag & SD_BSDF) {
870                         /* sample BSDF */
871                         float bsdf_pdf;
872                         BsdfEval bsdf_eval;
873                         float3 bsdf_omega_in;
874                         differential3 bsdf_domega_in;
875                         float bsdf_u, bsdf_v;
876                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
877                         int label;
878
879                         label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
880                                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
881
882                         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
883                                 break;
884
885                         /* modify throughput */
886                         path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
887
888                         /* set labels */
889                         if(!(label & LABEL_TRANSPARENT)) {
890                                 state.ray_pdf = bsdf_pdf;
891 #ifdef __LAMP_MIS__
892                                 state.ray_t = 0.0f;
893 #endif
894                                 state.min_ray_pdf = fminf(bsdf_pdf, state.min_ray_pdf);
895                         }
896
897                         /* update path state */
898                         path_state_next(kg, &state, label);
899
900                         /* setup ray */
901                         ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
902                         ray.D = bsdf_omega_in;
903
904 #ifdef __RAY_DIFFERENTIALS__
905                         ray.dP = sd.dP;
906                         ray.dD = bsdf_domega_in;
907 #endif
908
909 #ifdef __VOLUME__
910                         /* enter/exit volume */
911                         if(label & LABEL_TRANSMIT)
912                                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
913 #endif
914
915                 }
916 #ifdef __VOLUME__
917                 else if(sd.flag & SD_HAS_ONLY_VOLUME) {
918                         /* no surface shader but have a volume shader? act transparent */
919
920                         /* update path state, count as transparent */
921                         path_state_next(kg, &state, LABEL_TRANSPARENT);
922
923                         /* setup ray position, direction stays unchanged */
924                         ray.P = ray_offset(sd.P, -sd.Ng);
925 #ifdef __RAY_DIFFERENTIALS__
926                         ray.dP = sd.dP;
927 #endif
928
929                         /* enter/exit volume */
930                         kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
931                 }
932 #endif
933                 else {
934                         /* no bsdf or volume? we're done */
935                         break;
936                 }
937
938                 /* adjust ray distance for clipping */
939                 if(state.bounce == 0)
940                         ray.t -= sd.ray_length; /* clipping works through transparent */
941                 else
942                         ray.t = FLT_MAX;
943         }
944
945         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
946
947         kernel_write_light_passes(kg, buffer, &L, sample);
948
949         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
950 }
951
952 #ifdef __BRANCHED_PATH__
953
954 ccl_device_noinline void kernel_branched_path_integrate_lighting(KernelGlobals *kg,
955         RNG *rng, ShaderData *sd, float3 throughput, float num_samples_adjust,
956         PathState *state, PathRadiance *L, ccl_global float *buffer)
957 {
958 #ifdef __EMISSION__
959         bool all = kernel_data.integrator.sample_all_lights_direct;
960         kernel_branched_path_integrate_direct_lighting(kg, rng, sd, state, throughput, num_samples_adjust, L, all);
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