Cysles: Avoid having ShaderData on the stack
[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                 SubsurfaceIntersection ss_isect;
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_intersect(kg,
345                                                                   &ss_isect,
346                                                                   sd,
347                                                                   sc,
348                                                                   &lcg_state,
349                                                                   bssrdf_u, bssrdf_v,
350                                                                   false);
351 #ifdef __VOLUME__
352                 Ray volume_ray = *ray;
353                 bool need_update_volume_stack = kernel_data.integrator.use_volumes &&
354                                                 ccl_fetch(sd, flag) & SD_OBJECT_INTERSECTS_VOLUME;
355 #endif
356
357                 /* compute lighting with the BSDF closure */
358                 for(int hit = 0; hit < num_hits; hit++) {
359                         /* NOTE: We reuse the existing ShaderData, we assume the path
360                          * integration loop stops when this function returns true.
361                          */
362                         subsurface_scatter_multi_setup(kg,
363                                                        &ss_isect,
364                                                        hit,
365                                                        sd,
366                                                        state->flag,
367                                                        sc,
368                                                        false);
369
370                         float3 tp = *throughput;
371                         PathState hit_state = *state;
372                         Ray hit_ray = *ray;
373
374                         hit_state.rng_offset += PRNG_BOUNCE_NUM;
375
376                         kernel_path_surface_connect_light(kg, rng, sd, tp, state, L);
377
378                         if(kernel_path_surface_bounce(kg, rng, sd, &tp, &hit_state, L, &hit_ray)) {
379 #ifdef __LAMP_MIS__
380                                 hit_state.ray_t = 0.0f;
381 #endif
382
383 #ifdef __VOLUME__
384                                 if(need_update_volume_stack) {
385                                         /* Setup ray from previous surface point to the new one. */
386                                         volume_ray.D = normalize_len(hit_ray.P - volume_ray.P,
387                                                                      &volume_ray.t);
388
389                                         kernel_volume_stack_update_for_subsurface(
390                                             kg,
391                                             &volume_ray,
392                                             hit_state.volume_stack);
393
394                                         /* Move volume ray forward. */
395                                         volume_ray.P = hit_ray.P;
396                                 }
397 #endif
398
399                                 kernel_path_indirect(kg, rng, hit_ray, tp, state->num_samples, hit_state, L);
400
401                                 /* for render passes, sum and reset indirect light pass variables
402                                  * for the next samples */
403                                 path_radiance_sum_indirect(L);
404                                 path_radiance_reset_indirect(L);
405                         }
406                 }
407                 return true;
408         }
409         return false;
410 }
411 #endif
412
413 ccl_device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
414 {
415         /* initialize */
416         PathRadiance L;
417         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
418         float L_transparent = 0.0f;
419
420         path_radiance_init(&L, kernel_data.film.use_light_pass);
421
422         PathState state;
423         path_state_init(kg, &state, rng, sample, &ray);
424
425 #ifdef __KERNEL_DEBUG__
426         DebugData debug_data;
427         debug_data_init(&debug_data);
428 #endif
429
430         /* path iteration */
431         for(;;) {
432                 /* intersect scene */
433                 Intersection isect;
434                 uint visibility = path_state_ray_visibility(kg, &state);
435
436 #ifdef __HAIR__
437                 float difl = 0.0f, extmax = 0.0f;
438                 uint lcg_state = 0;
439
440                 if(kernel_data.bvh.have_curves) {
441                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
442                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
443                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
444                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
445                         }
446
447                         extmax = kernel_data.curve.maximum_width;
448                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
449                 }
450
451                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
452 #else
453                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
454 #endif
455
456 #ifdef __KERNEL_DEBUG__
457                 if(state.flag & PATH_RAY_CAMERA) {
458                         debug_data.num_bvh_traversal_steps += isect.num_traversal_steps;
459                         debug_data.num_bvh_traversed_instances += isect.num_traversed_instances;
460                 }
461                 debug_data.num_ray_bounces++;
462 #endif
463
464 #ifdef __LAMP_MIS__
465                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
466                         /* ray starting from previous non-transparent bounce */
467                         Ray light_ray;
468
469                         light_ray.P = ray.P - state.ray_t*ray.D;
470                         state.ray_t += isect.t;
471                         light_ray.D = ray.D;
472                         light_ray.t = state.ray_t;
473                         light_ray.time = ray.time;
474                         light_ray.dD = ray.dD;
475                         light_ray.dP = ray.dP;
476
477                         /* intersect with lamp */
478                         float3 emission;
479
480                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
481                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
482                 }
483 #endif
484
485 #ifdef __VOLUME__
486                 /* volume attenuation, emission, scatter */
487                 if(state.volume_stack[0].shader != SHADER_NONE) {
488                         Ray volume_ray = ray;
489                         volume_ray.t = (hit)? isect.t: FLT_MAX;
490
491                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
492
493 #ifdef __VOLUME_DECOUPLED__
494                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
495                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
496
497                         if(decoupled) {
498                                 /* cache steps along volume for repeated sampling */
499                                 VolumeSegment volume_segment;
500                                 ShaderData volume_sd;
501
502                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
503                                 kernel_volume_decoupled_record(kg, &state,
504                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
505
506                                 volume_segment.sampling_method = sampling_method;
507
508                                 /* emission */
509                                 if(volume_segment.closure_flag & SD_EMISSION)
510                                         path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
511
512                                 /* scattering */
513                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
514
515                                 if(volume_segment.closure_flag & SD_SCATTER) {
516                                         bool all = false;
517
518                                         /* direct light sampling */
519                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
520                                                 throughput, &state, &L, all, &volume_ray, &volume_segment);
521
522                                         /* indirect sample. if we use distance sampling and take just
523                                          * one sample for direct and indirect light, we could share
524                                          * this computation, but makes code a bit complex */
525                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
526                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
527
528                                         result = kernel_volume_decoupled_scatter(kg,
529                                                 &state, &volume_ray, &volume_sd, &throughput,
530                                                 rphase, rscatter, &volume_segment, NULL, true);
531                                 }
532
533                                 /* free cached steps */
534                                 kernel_volume_decoupled_free(kg, &volume_segment);
535
536                                 if(result == VOLUME_PATH_SCATTERED) {
537                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
538                                                 continue;
539                                         else
540                                                 break;
541                                 }
542                                 else {
543                                         throughput *= volume_segment.accum_transmittance;
544                                 }
545                         }
546                         else 
547 #endif
548                         {
549                                 /* integrate along volume segment with distance sampling */
550                                 ShaderData volume_sd;
551                                 VolumeIntegrateResult result = kernel_volume_integrate(
552                                         kg, &state, &volume_sd, &volume_ray, &L, &throughput, rng, heterogeneous);
553
554 #ifdef __VOLUME_SCATTER__
555                                 if(result == VOLUME_PATH_SCATTERED) {
556                                         /* direct lighting */
557                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, &L);
558
559                                         /* indirect light bounce */
560                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray))
561                                                 continue;
562                                         else
563                                                 break;
564                                 }
565 #endif
566                         }
567                 }
568 #endif
569
570                 if(!hit) {
571                         /* eval background shader if nothing hit */
572                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
573                                 L_transparent += average(throughput);
574
575 #ifdef __PASSES__
576                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
577 #endif
578                                         break;
579                         }
580
581 #ifdef __BACKGROUND__
582                         /* sample background shader */
583                         float3 L_background = indirect_background(kg, &state, &ray);
584                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
585 #endif
586
587                         break;
588                 }
589
590                 /* setup shading */
591                 ShaderData sd;
592                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
593                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
594                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
595
596                 /* holdout */
597 #ifdef __HOLDOUT__
598                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
599                         if(kernel_data.background.transparent) {
600                                 float3 holdout_weight;
601                                 
602                                 if(sd.flag & SD_HOLDOUT_MASK)
603                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
604                                 else
605                                         holdout_weight = shader_holdout_eval(kg, &sd);
606
607                                 /* any throughput is ok, should all be identical here */
608                                 L_transparent += average(holdout_weight*throughput);
609                         }
610
611                         if(sd.flag & SD_HOLDOUT_MASK)
612                                 break;
613                 }
614 #endif
615
616                 /* holdout mask objects do not write data passes */
617                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
618
619                 /* blurring of bsdf after bounces, for rays that have a small likelihood
620                  * of following this particular path (diffuse, rough glossy) */
621                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
622                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
623
624                         if(blur_pdf < 1.0f) {
625                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
626                                 shader_bsdf_blur(kg, &sd, blur_roughness);
627                         }
628                 }
629
630 #ifdef __EMISSION__
631                 /* emission */
632                 if(sd.flag & SD_EMISSION) {
633                         /* todo: is isect.t wrong here for transparent surfaces? */
634                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
635                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
636                 }
637 #endif
638
639                 /* path termination. this is a strange place to put the termination, it's
640                  * mainly due to the mixed in MIS that we use. gives too many unneeded
641                  * shader evaluations, only need emission if we are going to terminate */
642                 float probability = path_state_terminate_probability(kg, &state, throughput);
643
644                 if(probability == 0.0f) {
645                         break;
646                 }
647                 else if(probability != 1.0f) {
648                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
649
650                         if(terminate >= probability)
651                                 break;
652
653                         throughput /= probability;
654                 }
655
656 #ifdef __AO__
657                 /* ambient occlusion */
658                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
659                         kernel_path_ao(kg, &sd, &L, &state, rng, throughput);
660                 }
661 #endif
662
663 #ifdef __SUBSURFACE__
664                 /* bssrdf scatter to a different location on the same object, replacing
665                  * the closures with a diffuse BSDF */
666                 if(sd.flag & SD_BSSRDF) {
667                         if(kernel_path_subsurface_scatter(kg, &sd, &L, &state, rng, &ray, &throughput))
668                                 break;
669                 }
670 #endif
671
672                 /* direct lighting */
673                 kernel_path_surface_connect_light(kg, rng, &sd, throughput, &state, &L);
674
675                 /* compute direct lighting and next bounce */
676                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
677                         break;
678         }
679
680         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
681
682         kernel_write_light_passes(kg, buffer, &L, sample);
683
684 #ifdef __KERNEL_DEBUG__
685         kernel_write_debug_passes(kg, buffer, &state, &debug_data, sample);
686 #endif
687
688         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
689 }
690
691 ccl_device void kernel_path_trace(KernelGlobals *kg,
692         ccl_global float *buffer, ccl_global uint *rng_state,
693         int sample, int x, int y, int offset, int stride)
694 {
695         /* buffer offset */
696         int index = offset + x + y*stride;
697         int pass_stride = kernel_data.film.pass_stride;
698
699         rng_state += index;
700         buffer += index*pass_stride;
701
702         /* initialize random numbers and ray */
703         RNG rng;
704         Ray ray;
705
706         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
707
708         /* integrate */
709         float4 L;
710
711         if(ray.t != 0.0f)
712                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
713         else
714                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
715
716         /* accumulate result in output buffer */
717         kernel_write_pass_float4(buffer, sample, L);
718
719         path_rng_end(kg, rng_state, rng);
720 }
721
722 CCL_NAMESPACE_END
723