Cycles: Expose image image extension mapping to the image manager
[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_random.h"
22 #include "kernel_projection.h"
23 #include "kernel_montecarlo.h"
24 #include "kernel_differential.h"
25 #include "kernel_camera.h"
26
27 #include "geom/geom.h"
28
29 #include "kernel_accumulate.h"
30 #include "kernel_shader.h"
31 #include "kernel_light.h"
32 #include "kernel_passes.h"
33
34 #ifdef __SUBSURFACE__
35 #include "kernel_subsurface.h"
36 #endif
37
38 #ifdef __VOLUME__
39 #include "kernel_volume.h"
40 #endif
41
42 #include "kernel_path_state.h"
43 #include "kernel_shadow.h"
44 #include "kernel_emission.h"
45 #include "kernel_path_common.h"
46 #include "kernel_path_surface.h"
47 #include "kernel_path_volume.h"
48
49 #ifdef __KERNEL_DEBUG__
50 #include "kernel_debug.h"
51 #endif
52
53 CCL_NAMESPACE_BEGIN
54
55 ccl_device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, Ray ray,
56         float3 throughput, int num_samples, PathState state, PathRadiance *L)
57 {
58         /* path iteration */
59         for(;;) {
60                 /* intersect scene */
61                 Intersection isect;
62                 uint visibility = path_state_ray_visibility(kg, &state);
63                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
64
65 #ifdef __LAMP_MIS__
66                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
67                         /* ray starting from previous non-transparent bounce */
68                         Ray light_ray;
69
70                         light_ray.P = ray.P - state.ray_t*ray.D;
71                         state.ray_t += isect.t;
72                         light_ray.D = ray.D;
73                         light_ray.t = state.ray_t;
74                         light_ray.time = ray.time;
75                         light_ray.dD = ray.dD;
76                         light_ray.dP = ray.dP;
77
78                         /* intersect with lamp */
79                         float3 emission;
80
81                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
82                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
83                 }
84 #endif
85
86 #ifdef __VOLUME__
87                 /* volume attenuation, emission, scatter */
88                 if(state.volume_stack[0].shader != SHADER_NONE) {
89                         Ray volume_ray = ray;
90                         volume_ray.t = (hit)? isect.t: FLT_MAX;
91
92                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
93
94 #ifdef __VOLUME_DECOUPLED__
95                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
96                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, false, sampling_method);
97
98                         if(decoupled) {
99                                 /* cache steps along volume for repeated sampling */
100                                 VolumeSegment volume_segment;
101                                 ShaderData volume_sd;
102
103                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
104                                 kernel_volume_decoupled_record(kg, &state,
105                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
106                                 
107                                 volume_segment.sampling_method = sampling_method;
108
109                                 /* emission */
110                                 if(volume_segment.closure_flag & SD_EMISSION)
111                                         path_radiance_accum_emission(L, throughput, volume_segment.accum_emission, state.bounce);
112
113                                 /* scattering */
114                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
115
116                                 if(volume_segment.closure_flag & SD_SCATTER) {
117                                         bool all = kernel_data.integrator.sample_all_lights_indirect;
118
119                                         /* direct light sampling */
120                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
121                                                 throughput, &state, L, all, &volume_ray, &volume_segment);
122
123                                         /* indirect sample. if we use distance sampling and take just
124                                          * one sample for direct and indirect light, we could share
125                                          * this computation, but makes code a bit complex */
126                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
127                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
128
129                                         result = kernel_volume_decoupled_scatter(kg,
130                                                 &state, &volume_ray, &volume_sd, &throughput,
131                                                 rphase, rscatter, &volume_segment, NULL, true);
132                                 }
133
134                                 /* free cached steps */
135                                 kernel_volume_decoupled_free(kg, &volume_segment);
136
137                                 if(result == VOLUME_PATH_SCATTERED) {
138                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, L, &ray))
139                                                 continue;
140                                         else
141                                                 break;
142                                 }
143                                 else {
144                                         throughput *= volume_segment.accum_transmittance;
145                                 }
146                         }
147                         else
148 #endif
149                         {
150                                 /* integrate along volume segment with distance sampling */
151                                 ShaderData volume_sd;
152                                 VolumeIntegrateResult result = kernel_volume_integrate(
153                                         kg, &state, &volume_sd, &volume_ray, L, &throughput, rng, heterogeneous);
154
155 #ifdef __VOLUME_SCATTER__
156                                 if(result == VOLUME_PATH_SCATTERED) {
157                                         /* direct lighting */
158                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, L);
159
160                                         /* indirect light bounce */
161                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, L, &ray))
162                                                 continue;
163                                         else
164                                                 break;
165                                 }
166 #endif
167                         }
168                 }
169 #endif
170
171                 if(!hit) {
172 #ifdef __BACKGROUND__
173                         /* sample background shader */
174                         float3 L_background = indirect_background(kg, &state, &ray);
175                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
176 #endif
177
178                         break;
179                 }
180
181                 /* setup shading */
182                 ShaderData sd;
183                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
184                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
185                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
186 #ifdef __BRANCHED_PATH__
187                 shader_merge_closures(&sd);
188 #endif
189
190                 /* blurring of bsdf after bounces, for rays that have a small likelihood
191                  * of following this particular path (diffuse, rough glossy) */
192                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
193                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
194
195                         if(blur_pdf < 1.0f) {
196                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
197                                 shader_bsdf_blur(kg, &sd, blur_roughness);
198                         }
199                 }
200
201 #ifdef __EMISSION__
202                 /* emission */
203                 if(sd.flag & SD_EMISSION) {
204                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
205                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
206                 }
207 #endif
208
209                 /* path termination. this is a strange place to put the termination, it's
210                  * mainly due to the mixed in MIS that we use. gives too many unneeded
211                  * shader evaluations, only need emission if we are going to terminate */
212                 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
213
214                 if(probability == 0.0f) {
215                         break;
216                 }
217                 else if(probability != 1.0f) {
218                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
219
220                         if(terminate >= probability)
221                                 break;
222
223                         throughput /= probability;
224                 }
225
226 #ifdef __AO__
227                 /* ambient occlusion */
228                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
229                         float bsdf_u, bsdf_v;
230                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
231
232                         float ao_factor = kernel_data.background.ao_factor;
233                         float3 ao_N;
234                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
235                         float3 ao_D;
236                         float ao_pdf;
237                         float3 ao_alpha = make_float3(0.0f, 0.0f, 0.0f);
238
239                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
240
241                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
242                                 Ray light_ray;
243                                 float3 ao_shadow;
244
245                                 light_ray.P = ray_offset(sd.P, sd.Ng);
246                                 light_ray.D = ao_D;
247                                 light_ray.t = kernel_data.background.ao_distance;
248 #ifdef __OBJECT_MOTION__
249                                 light_ray.time = sd.time;
250 #endif
251                                 light_ray.dP = sd.dP;
252                                 light_ray.dD = differential3_zero();
253
254                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
255                                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
256                         }
257                 }
258 #endif
259
260 #ifdef __SUBSURFACE__
261                 /* bssrdf scatter to a different location on the same object, replacing
262                  * the closures with a diffuse BSDF */
263                 if(sd.flag & SD_BSSRDF) {
264                         float bssrdf_probability;
265                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
266
267                         /* modify throughput for picking bssrdf or bsdf */
268                         throughput *= bssrdf_probability;
269
270                         /* do bssrdf scatter step if we picked a bssrdf closure */
271                         if(sc) {
272                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
273
274                                 float bssrdf_u, bssrdf_v;
275                                 path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
276                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
277                         }
278                 }
279 #endif
280
281 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
282                 if(kernel_data.integrator.use_direct_light) {
283                         bool all = kernel_data.integrator.sample_all_lights_indirect;
284                         kernel_branched_path_surface_connect_light(kg, rng, &sd, &state, throughput, 1.0f, L, all);
285                 }
286 #endif
287
288                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, L, &ray))
289                         break;
290         }
291 }
292
293 ccl_device void kernel_path_ao(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
294 {
295         /* todo: solve correlation */
296         float bsdf_u, bsdf_v;
297
298         path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
299
300         float ao_factor = kernel_data.background.ao_factor;
301         float3 ao_N;
302         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
303         float3 ao_D;
304         float ao_pdf;
305         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
306
307         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
308
309         if(dot(ccl_fetch(sd, Ng), ao_D) > 0.0f && ao_pdf != 0.0f) {
310                 Ray light_ray;
311                 float3 ao_shadow;
312
313                 light_ray.P = ray_offset(ccl_fetch(sd, P), ccl_fetch(sd, Ng));
314                 light_ray.D = ao_D;
315                 light_ray.t = kernel_data.background.ao_distance;
316 #ifdef __OBJECT_MOTION__
317                 light_ray.time = ccl_fetch(sd, time);
318 #endif
319                 light_ray.dP = ccl_fetch(sd, dP);
320                 light_ray.dD = differential3_zero();
321
322                 if(!shadow_blocked(kg, state, &light_ray, &ao_shadow))
323                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
324         }
325 }
326
327 #ifdef __SUBSURFACE__
328
329 ccl_device bool kernel_path_subsurface_scatter(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, Ray *ray, float3 *throughput)
330 {
331         float bssrdf_probability;
332         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
333
334         /* modify throughput for picking bssrdf or bsdf */
335         *throughput *= bssrdf_probability;
336
337         /* do bssrdf scatter step if we picked a bssrdf closure */
338         if(sc) {
339                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
340
341                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
342                 float bssrdf_u, bssrdf_v;
343                 path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
344                 int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
345 #ifdef __VOLUME__
346                 Ray volume_ray = *ray;
347                 bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
348                                                 ccl_fetch(sd, flag) & SD_OBJECT_INTERSECTS_VOLUME;
349 #endif
350
351                 /* compute lighting with the BSDF closure */
352                 for(int hit = 0; hit < num_hits; hit++) {
353                         float3 tp = *throughput;
354                         PathState hit_state = *state;
355                         Ray hit_ray = *ray;
356
357                         hit_state.rng_offset += PRNG_BOUNCE_NUM;
358                         
359                         kernel_path_surface_connect_light(kg, rng, &bssrdf_sd[hit], tp, state, L);
360
361                         if(kernel_path_surface_bounce(kg, rng, &bssrdf_sd[hit], &tp, &hit_state, L, &hit_ray)) {
362 #ifdef __LAMP_MIS__
363                                 hit_state.ray_t = 0.0f;
364 #endif
365
366 #ifdef __VOLUME__
367                                 if(need_update_volume_stack) {
368                                         /* Setup ray from previous surface point to the new one. */
369                                         volume_ray.D = normalize_len(hit_ray.P - volume_ray.P,
370                                                                      &volume_ray.t);
371
372                                         kernel_volume_stack_update_for_subsurface(
373                                             kg,
374                                             &volume_ray,
375                                             hit_state.volume_stack);
376
377                                         /* Move volume ray forward. */
378                                         volume_ray.P = hit_ray.P;
379                                 }
380 #endif
381
382                                 kernel_path_indirect(kg, rng, hit_ray, tp, state->num_samples, hit_state, L);
383
384                                 /* for render passes, sum and reset indirect light pass variables
385                                  * for the next samples */
386                                 path_radiance_sum_indirect(L);
387                                 path_radiance_reset_indirect(L);
388                         }
389                 }
390                 return true;
391         }
392         return false;
393 }
394 #endif
395
396 ccl_device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
397 {
398         /* initialize */
399         PathRadiance L;
400         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
401         float L_transparent = 0.0f;
402
403         path_radiance_init(&L, kernel_data.film.use_light_pass);
404
405         PathState state;
406         path_state_init(kg, &state, rng, sample, &ray);
407
408 #ifdef __KERNEL_DEBUG__
409         DebugData debug_data;
410         debug_data_init(&debug_data);
411 #endif
412
413         /* path iteration */
414         for(;;) {
415                 /* intersect scene */
416                 Intersection isect;
417                 uint visibility = path_state_ray_visibility(kg, &state);
418
419 #ifdef __HAIR__
420                 float difl = 0.0f, extmax = 0.0f;
421                 uint lcg_state = 0;
422
423                 if(kernel_data.bvh.have_curves) {
424                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
425                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
426                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
427                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
428                         }
429
430                         extmax = kernel_data.curve.maximum_width;
431                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
432                 }
433
434                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
435 #else
436                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
437 #endif
438
439 #ifdef __KERNEL_DEBUG__
440                 if(state.flag & PATH_RAY_CAMERA) {
441                         debug_data.num_bvh_traversal_steps += isect.num_traversal_steps;
442                         debug_data.num_bvh_traversed_instances += isect.num_traversed_instances;
443                 }
444                 debug_data.num_ray_bounces++;
445 #endif
446
447 #ifdef __LAMP_MIS__
448                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
449                         /* ray starting from previous non-transparent bounce */
450                         Ray light_ray;
451
452                         light_ray.P = ray.P - state.ray_t*ray.D;
453                         state.ray_t += isect.t;
454                         light_ray.D = ray.D;
455                         light_ray.t = state.ray_t;
456                         light_ray.time = ray.time;
457                         light_ray.dD = ray.dD;
458                         light_ray.dP = ray.dP;
459
460                         /* intersect with lamp */
461                         float3 emission;
462
463                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
464                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
465                 }
466 #endif
467
468 #ifdef __VOLUME__
469                 /* volume attenuation, emission, scatter */
470                 if(state.volume_stack[0].shader != SHADER_NONE) {
471                         Ray volume_ray = ray;
472                         volume_ray.t = (hit)? isect.t: FLT_MAX;
473
474                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
475
476 #ifdef __VOLUME_DECOUPLED__
477                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
478                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
479
480                         if(decoupled) {
481                                 /* cache steps along volume for repeated sampling */
482                                 VolumeSegment volume_segment;
483                                 ShaderData volume_sd;
484
485                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
486                                 kernel_volume_decoupled_record(kg, &state,
487                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
488
489                                 volume_segment.sampling_method = sampling_method;
490
491                                 /* emission */
492                                 if(volume_segment.closure_flag & SD_EMISSION)
493                                         path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
494
495                                 /* scattering */
496                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
497
498                                 if(volume_segment.closure_flag & SD_SCATTER) {
499                                         bool all = false;
500
501                                         /* direct light sampling */
502                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
503                                                 throughput, &state, &L, all, &volume_ray, &volume_segment);
504
505                                         /* indirect sample. if we use distance sampling and take just
506                                          * one sample for direct and indirect light, we could share
507                                          * this computation, but makes code a bit complex */
508                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
509                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
510
511                                         result = kernel_volume_decoupled_scatter(kg,
512                                                 &state, &volume_ray, &volume_sd, &throughput,
513                                                 rphase, rscatter, &volume_segment, NULL, true);
514                                 }
515
516                                 /* free cached steps */
517                                 kernel_volume_decoupled_free(kg, &volume_segment);
518
519                                 if(result == VOLUME_PATH_SCATTERED) {
520                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
521                                                 continue;
522                                         else
523                                                 break;
524                                 }
525                                 else {
526                                         throughput *= volume_segment.accum_transmittance;
527                                 }
528                         }
529                         else 
530 #endif
531                         {
532                                 /* integrate along volume segment with distance sampling */
533                                 ShaderData volume_sd;
534                                 VolumeIntegrateResult result = kernel_volume_integrate(
535                                         kg, &state, &volume_sd, &volume_ray, &L, &throughput, rng, heterogeneous);
536
537 #ifdef __VOLUME_SCATTER__
538                                 if(result == VOLUME_PATH_SCATTERED) {
539                                         /* direct lighting */
540                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, &L);
541
542                                         /* indirect light bounce */
543                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
544                                                 continue;
545                                         else
546                                                 break;
547                                 }
548 #endif
549                         }
550                 }
551 #endif
552
553                 if(!hit) {
554                         /* eval background shader if nothing hit */
555                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
556                                 L_transparent += average(throughput);
557
558 #ifdef __PASSES__
559                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
560 #endif
561                                         break;
562                         }
563
564 #ifdef __BACKGROUND__
565                         /* sample background shader */
566                         float3 L_background = indirect_background(kg, &state, &ray);
567                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
568 #endif
569
570                         break;
571                 }
572
573                 /* setup shading */
574                 ShaderData sd;
575                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
576                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
577                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
578
579                 /* holdout */
580 #ifdef __HOLDOUT__
581                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
582                         if(kernel_data.background.transparent) {
583                                 float3 holdout_weight;
584                                 
585                                 if(sd.flag & SD_HOLDOUT_MASK)
586                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
587                                 else
588                                         holdout_weight = shader_holdout_eval(kg, &sd);
589
590                                 /* any throughput is ok, should all be identical here */
591                                 L_transparent += average(holdout_weight*throughput);
592                         }
593
594                         if(sd.flag & SD_HOLDOUT_MASK)
595                                 break;
596                 }
597 #endif
598
599                 /* holdout mask objects do not write data passes */
600                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
601
602                 /* blurring of bsdf after bounces, for rays that have a small likelihood
603                  * of following this particular path (diffuse, rough glossy) */
604                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
605                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
606
607                         if(blur_pdf < 1.0f) {
608                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
609                                 shader_bsdf_blur(kg, &sd, blur_roughness);
610                         }
611                 }
612
613 #ifdef __EMISSION__
614                 /* emission */
615                 if(sd.flag & SD_EMISSION) {
616                         /* todo: is isect.t wrong here for transparent surfaces? */
617                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
618                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
619                 }
620 #endif
621
622                 /* path termination. this is a strange place to put the termination, it's
623                  * mainly due to the mixed in MIS that we use. gives too many unneeded
624                  * shader evaluations, only need emission if we are going to terminate */
625                 float probability = path_state_terminate_probability(kg, &state, throughput);
626
627                 if(probability == 0.0f) {
628                         break;
629                 }
630                 else if(probability != 1.0f) {
631                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
632
633                         if(terminate >= probability)
634                                 break;
635
636                         throughput /= probability;
637                 }
638
639 #ifdef __AO__
640                 /* ambient occlusion */
641                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
642                         kernel_path_ao(kg, &sd, &L, &state, rng, throughput);
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                         if(kernel_path_subsurface_scatter(kg, &sd, &L, &state, rng, &ray, &throughput))
651                                 break;
652                 }
653 #endif
654
655                 /* direct lighting */
656                 kernel_path_surface_connect_light(kg, rng, &sd, throughput, &state, &L);
657
658                 /* compute direct lighting and next bounce */
659                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
660                         break;
661         }
662
663         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
664
665         kernel_write_light_passes(kg, buffer, &L, sample);
666
667 #ifdef __KERNEL_DEBUG__
668         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
669 #endif
670
671         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
672 }
673
674 ccl_device void kernel_path_trace(KernelGlobals *kg,
675         ccl_global float *buffer, ccl_global uint *rng_state,
676         int sample, int x, int y, int offset, int stride)
677 {
678         /* buffer offset */
679         int index = offset + x + y*stride;
680         int pass_stride = kernel_data.film.pass_stride;
681
682         rng_state += index;
683         buffer += index*pass_stride;
684
685         /* initialize random numbers and ray */
686         RNG rng;
687         Ray ray;
688
689         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
690
691         /* integrate */
692         float4 L;
693
694         if(ray.t != 0.0f)
695                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
696         else
697                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
698
699         /* accumulate result in output buffer */
700         kernel_write_pass_float4(buffer, sample, L);
701
702         path_rng_end(kg, rng_state, rng);
703 }
704
705 CCL_NAMESPACE_END
706