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