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