2 * Copyright 2011, Blender Foundation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "osl_shader.h"
23 #include "kernel_differential.h"
24 #include "kernel_montecarlo.h"
25 #include "kernel_projection.h"
26 #include "kernel_object.h"
27 #include "kernel_triangle.h"
28 #include "kernel_curve.h"
29 #include "kernel_primitive.h"
30 #include "kernel_projection.h"
32 #include "kernel_qbvh.h"
34 #include "kernel_bvh.h"
36 #include "kernel_accumulate.h"
37 #include "kernel_camera.h"
38 #include "kernel_shader.h"
39 #include "kernel_light.h"
40 #include "kernel_emission.h"
41 #include "kernel_random.h"
42 #include "kernel_passes.h"
46 typedef struct PathState {
52 int transmission_bounce;
53 int transparent_bounce;
56 __device_inline void path_state_init(PathState *state)
58 state->flag = PATH_RAY_CAMERA|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
60 state->diffuse_bounce = 0;
61 state->glossy_bounce = 0;
62 state->transmission_bounce = 0;
63 state->transparent_bounce = 0;
66 __device_inline void path_state_next(KernelGlobals *kg, PathState *state, int label)
68 /* ray through transparent keeps same flags from previous ray and is
69 * not counted as a regular bounce, transparent has separate max */
70 if(label & LABEL_TRANSPARENT) {
71 state->flag |= PATH_RAY_TRANSPARENT;
72 state->transparent_bounce++;
74 if(!kernel_data.integrator.transparent_shadows)
75 state->flag |= PATH_RAY_MIS_SKIP;
82 /* reflection/transmission */
83 if(label & LABEL_REFLECT) {
84 state->flag |= PATH_RAY_REFLECT;
85 state->flag &= ~(PATH_RAY_TRANSMIT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
87 if(label & LABEL_DIFFUSE)
88 state->diffuse_bounce++;
90 state->glossy_bounce++;
93 kernel_assert(label & LABEL_TRANSMIT);
95 state->flag |= PATH_RAY_TRANSMIT;
96 state->flag &= ~(PATH_RAY_REFLECT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
98 state->transmission_bounce++;
101 /* diffuse/glossy/singular */
102 if(label & LABEL_DIFFUSE) {
103 state->flag |= PATH_RAY_DIFFUSE;
104 state->flag &= ~(PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
106 else if(label & LABEL_GLOSSY) {
107 state->flag |= PATH_RAY_GLOSSY;
108 state->flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
111 kernel_assert(label & LABEL_SINGULAR);
113 state->flag |= PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
114 state->flag &= ~PATH_RAY_DIFFUSE;
118 __device_inline uint path_state_ray_visibility(KernelGlobals *kg, PathState *state)
120 uint flag = state->flag;
122 /* for visibility, diffuse/glossy are for reflection only */
123 if(flag & PATH_RAY_TRANSMIT)
124 flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY);
125 /* for camera visibility, use render layer flags */
126 if(flag & PATH_RAY_CAMERA)
127 flag |= kernel_data.integrator.layer_flag;
132 __device_inline float path_state_terminate_probability(KernelGlobals *kg, PathState *state, const float3 throughput)
134 if(state->flag & PATH_RAY_TRANSPARENT) {
135 /* transparent rays treated separately */
136 if(state->transparent_bounce >= kernel_data.integrator.transparent_max_bounce)
138 else if(state->transparent_bounce <= kernel_data.integrator.transparent_min_bounce)
143 if((state->bounce >= kernel_data.integrator.max_bounce) ||
144 (state->diffuse_bounce >= kernel_data.integrator.max_diffuse_bounce) ||
145 (state->glossy_bounce >= kernel_data.integrator.max_glossy_bounce) ||
146 (state->transmission_bounce >= kernel_data.integrator.max_transmission_bounce))
150 else if(state->bounce <= kernel_data.integrator.min_bounce) {
155 /* probalistic termination */
156 return average(throughput);
159 __device_inline bool shadow_blocked(KernelGlobals *kg, PathState *state, Ray *ray, float3 *shadow)
161 *shadow = make_float3(1.0f, 1.0f, 1.0f);
167 bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect);
169 #ifdef __TRANSPARENT_SHADOWS__
170 if(result && kernel_data.integrator.transparent_shadows) {
171 /* transparent shadows work in such a way to try to minimize overhead
172 * in cases where we don't need them. after a regular shadow ray is
173 * cast we check if the hit primitive was potentially transparent, and
174 * only in that case start marching. this gives on extra ray cast for
175 * the cases were we do want transparency.
177 * also note that for this to work correct, multi close sampling must
178 * be used, since we don't pass a random number to shader_eval_surface */
179 if(shader_transparent_shadow(kg, &isect)) {
180 float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
181 float3 Pend = ray->P + ray->D*ray->t;
182 int bounce = state->transparent_bounce;
185 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
188 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
189 /* todo: get random number somewhere for probabilistic terminate */
191 float probability = average(throughput);
192 float terminate = 0.0f;
194 if(terminate >= probability)
197 throughput /= probability;
201 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect)) {
202 *shadow *= throughput;
206 if(!shader_transparent_shadow(kg, &isect))
210 shader_setup_from_ray(kg, &sd, &isect, ray);
211 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW, SHADER_CONTEXT_SHADOW);
213 throughput *= shader_bsdf_transparency(kg, &sd);
215 ray->P = ray_offset(sd.P, -sd.Ng);
216 if(ray->t != FLT_MAX)
217 ray->D = normalize_len(Pend - ray->P, &ray->t);
219 shader_release(kg, &sd);
230 __device float4 kernel_path_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
234 float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
235 float L_transparent = 0.0f;
237 path_radiance_init(&L, kernel_data.film.use_light_pass);
239 float min_ray_pdf = FLT_MAX;
240 float ray_pdf = 0.0f;
245 int rng_offset = PRNG_BASE_NUM;
247 path_state_init(&state);
250 for(;; rng_offset += PRNG_BOUNCE_NUM) {
251 /* intersect scene */
253 uint visibility = path_state_ray_visibility(kg, &state);
254 bool hit = scene_intersect(kg, &ray, visibility, &isect);
257 if(kernel_data.integrator.pdf_lights > 0.0f && !(state.flag & PATH_RAY_CAMERA)) {
258 /* ray starting from previous non-transparent bounce */
261 light_ray.P = ray.P - ray_t*ray.D;
265 light_ray.time = ray.time;
267 /* intersect with lamp */
268 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
271 if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission))
272 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
277 /* eval background shader if nothing hit */
278 if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
279 L_transparent += average(throughput);
282 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
287 #ifdef __BACKGROUND__
288 /* sample background shader */
289 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
290 path_radiance_accum_background(&L, throughput, L_background, state.bounce);
298 shader_setup_from_ray(kg, &sd, &isect, &ray);
299 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
300 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
302 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
304 /* blurring of bsdf after bounces, for rays that have a small likelihood
305 * of following this particular path (diffuse, rough glossy) */
306 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
307 float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
309 if(blur_pdf < 1.0f) {
310 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
311 shader_bsdf_blur(kg, &sd, blur_roughness);
317 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
318 if(kernel_data.background.transparent) {
319 float3 holdout_weight;
321 if(sd.flag & SD_HOLDOUT_MASK)
322 holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
324 holdout_weight = shader_holdout_eval(kg, &sd);
326 /* any throughput is ok, should all be identical here */
327 L_transparent += average(holdout_weight*throughput);
330 if(sd.flag & SD_HOLDOUT_MASK) {
331 shader_release(kg, &sd);
339 if(sd.flag & SD_EMISSION) {
340 /* todo: is isect.t wrong here for transparent surfaces? */
341 float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
342 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
346 /* path termination. this is a strange place to put the termination, it's
347 * mainly due to the mixed in MIS that we use. gives too many unneeded
348 * shader evaluations, only need emission if we are going to terminate */
349 float probability = path_state_terminate_probability(kg, &state, throughput);
350 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
352 if(terminate >= probability) {
353 shader_release(kg, &sd);
357 throughput /= probability;
360 /* ambient occlusion */
361 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
362 /* todo: solve correlation */
363 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
364 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
366 float ao_factor = kernel_data.background.ao_factor;
368 float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
372 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
374 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
378 light_ray.P = ray_offset(sd.P, sd.Ng);
380 light_ray.t = kernel_data.background.ao_distance;
381 #ifdef __OBJECT_MOTION__
382 light_ray.time = sd.time;
385 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
386 path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
392 if(kernel_data.integrator.use_direct_light) {
393 /* sample illumination from lights to find path contribution */
394 if(sd.flag & SD_BSDF_HAS_EVAL) {
395 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
396 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
397 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
398 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
404 #ifdef __OBJECT_MOTION__
405 light_ray.time = sd.time;
408 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &lamp)) {
409 /* trace shadow ray */
412 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
414 bool is_lamp = (lamp != ~0);
415 path_radiance_accum_light(&L, throughput, &L_light, shadow, state.bounce, is_lamp);
422 /* no BSDF? we can stop here */
423 if(!(sd.flag & SD_BSDF)) {
424 shader_release(kg, &sd);
431 float3 bsdf_omega_in;
432 differential3 bsdf_domega_in;
433 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
434 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
437 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
438 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
440 shader_release(kg, &sd);
442 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
445 /* modify throughput */
446 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
449 if(!(label & LABEL_TRANSPARENT)) {
454 min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
457 /* update path state */
458 path_state_next(kg, &state, label);
461 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
462 ray.D = bsdf_omega_in;
464 if(state.bounce == 0)
465 ray.t -= sd.ray_length; /* clipping works through transparent */
469 #ifdef __RAY_DIFFERENTIALS__
471 ray.dD = bsdf_domega_in;
475 float3 L_sum = path_radiance_sum(kg, &L);
477 #ifdef __CLAMP_SAMPLE__
478 path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
481 kernel_write_light_passes(kg, buffer, &L, sample);
483 return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
486 #ifdef __NON_PROGRESSIVE__
488 __device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer,
489 float3 throughput, float throughput_normalize,
490 float min_ray_pdf, float ray_pdf, PathState state, int rng_offset, PathRadiance *L)
497 for(;; rng_offset += PRNG_BOUNCE_NUM) {
498 /* intersect scene */
500 uint visibility = path_state_ray_visibility(kg, &state);
501 bool hit = scene_intersect(kg, &ray, visibility, &isect);
504 if(kernel_data.integrator.pdf_lights > 0.0f && !(state.flag & PATH_RAY_CAMERA)) {
505 /* ray starting from previous non-transparent bounce */
508 light_ray.P = ray.P - ray_t*ray.D;
512 light_ray.time = ray.time;
514 /* intersect with lamp */
515 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
518 if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission))
519 path_radiance_accum_emission(L, throughput, emission, state.bounce);
524 #ifdef __BACKGROUND__
525 /* sample background shader */
526 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
527 path_radiance_accum_background(L, throughput, L_background, state.bounce);
535 shader_setup_from_ray(kg, &sd, &isect, &ray);
536 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
537 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
538 shader_merge_closures(kg, &sd);
540 /* blurring of bsdf after bounces, for rays that have a small likelihood
541 * of following this particular path (diffuse, rough glossy) */
542 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
543 float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
545 if(blur_pdf < 1.0f) {
546 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
547 shader_bsdf_blur(kg, &sd, blur_roughness);
553 if(sd.flag & SD_EMISSION) {
554 float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
555 path_radiance_accum_emission(L, throughput, emission, state.bounce);
559 /* path termination. this is a strange place to put the termination, it's
560 * mainly due to the mixed in MIS that we use. gives too many unneeded
561 * shader evaluations, only need emission if we are going to terminate */
562 float probability = path_state_terminate_probability(kg, &state, throughput*throughput_normalize);
563 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
565 if(terminate >= probability) {
566 shader_release(kg, &sd);
570 throughput /= probability;
573 /* ambient occlusion */
574 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
575 /* todo: solve correlation */
576 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
577 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
579 float ao_factor = kernel_data.background.ao_factor;
581 float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
585 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
587 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
591 light_ray.P = ray_offset(sd.P, sd.Ng);
593 light_ray.t = kernel_data.background.ao_distance;
594 #ifdef __OBJECT_MOTION__
595 light_ray.time = sd.time;
598 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
599 path_radiance_accum_ao(L, throughput, ao_bsdf, ao_shadow, state.bounce);
605 if(kernel_data.integrator.use_direct_light) {
606 /* sample illumination from lights to find path contribution */
607 if(sd.flag & SD_BSDF_HAS_EVAL) {
608 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
609 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
610 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
611 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
617 #ifdef __OBJECT_MOTION__
618 light_ray.time = sd.time;
621 /* sample random light */
622 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &lamp)) {
623 /* trace shadow ray */
626 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
628 bool is_lamp = (lamp != ~0);
629 path_radiance_accum_light(L, throughput, &L_light, shadow, state.bounce, is_lamp);
636 /* no BSDF? we can stop here */
637 if(!(sd.flag & SD_BSDF)) {
638 shader_release(kg, &sd);
645 float3 bsdf_omega_in;
646 differential3 bsdf_domega_in;
647 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
648 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
651 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
652 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
654 shader_release(kg, &sd);
656 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
659 /* modify throughput */
660 path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
663 if(!(label & LABEL_TRANSPARENT)) {
668 min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
671 /* update path state */
672 path_state_next(kg, &state, label);
675 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
676 ray.D = bsdf_omega_in;
678 #ifdef __RAY_DIFFERENTIALS__
680 ray.dD = bsdf_domega_in;
685 __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
689 float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
690 float L_transparent = 0.0f;
692 path_radiance_init(&L, kernel_data.film.use_light_pass);
694 float ray_pdf = 0.0f;
696 int rng_offset = PRNG_BASE_NUM;
698 path_state_init(&state);
700 for(;; rng_offset += PRNG_BOUNCE_NUM) {
701 /* intersect scene */
703 uint visibility = path_state_ray_visibility(kg, &state);
705 if(!scene_intersect(kg, &ray, visibility, &isect)) {
706 /* eval background shader if nothing hit */
707 if(kernel_data.background.transparent) {
708 L_transparent += average(throughput);
711 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
716 #ifdef __BACKGROUND__
717 /* sample background shader */
718 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
719 path_radiance_accum_background(&L, throughput, L_background, state.bounce);
727 shader_setup_from_ray(kg, &sd, &isect, &ray);
728 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
729 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
730 shader_merge_closures(kg, &sd);
732 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
736 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
737 if(kernel_data.background.transparent) {
738 float3 holdout_weight;
740 if(sd.flag & SD_HOLDOUT_MASK)
741 holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
743 holdout_weight = shader_holdout_eval(kg, &sd);
745 /* any throughput is ok, should all be identical here */
746 L_transparent += average(holdout_weight*throughput);
749 if(sd.flag & SD_HOLDOUT_MASK) {
750 shader_release(kg, &sd);
758 if(sd.flag & SD_EMISSION) {
759 float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
760 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
764 /* transparency termination */
765 if(state.flag & PATH_RAY_TRANSPARENT) {
766 /* path termination. this is a strange place to put the termination, it's
767 * mainly due to the mixed in MIS that we use. gives too many unneeded
768 * shader evaluations, only need emission if we are going to terminate */
769 float probability = path_state_terminate_probability(kg, &state, throughput);
770 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
772 if(terminate >= probability) {
773 shader_release(kg, &sd);
777 throughput /= probability;
781 /* ambient occlusion */
782 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
783 int num_samples = kernel_data.integrator.ao_samples;
784 float num_samples_inv = 1.0f/num_samples;
785 float ao_factor = kernel_data.background.ao_factor;
787 float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
789 for(int j = 0; j < num_samples; j++) {
790 /* todo: solve correlation */
791 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
792 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
797 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
799 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
803 light_ray.P = ray_offset(sd.P, sd.Ng);
805 light_ray.t = kernel_data.background.ao_distance;
806 #ifdef __OBJECT_MOTION__
807 light_ray.time = sd.time;
810 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
811 path_radiance_accum_ao(&L, throughput*num_samples_inv, ao_bsdf, ao_shadow, state.bounce);
818 /* sample illumination from lights to find path contribution */
819 if(sd.flag & SD_BSDF_HAS_EVAL) {
824 #ifdef __OBJECT_MOTION__
825 light_ray.time = sd.time;
829 for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
830 int num_samples = light_select_num_samples(kg, i);
831 float num_samples_inv = 1.0f/(num_samples*kernel_data.integrator.num_all_lights);
833 if(kernel_data.integrator.pdf_triangles != 0.0f)
834 num_samples_inv *= 0.5f;
836 for(int j = 0; j < num_samples; j++) {
837 float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
838 float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
840 if(direct_emission(kg, &sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &lamp)) {
841 /* trace shadow ray */
844 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
846 bool is_lamp = (lamp != ~0);
847 path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
853 /* mesh light sampling */
854 if(kernel_data.integrator.pdf_triangles != 0.0f) {
855 int num_samples = kernel_data.integrator.mesh_light_samples;
856 float num_samples_inv = 1.0f/num_samples;
858 if(kernel_data.integrator.num_all_lights)
859 num_samples_inv *= 0.5f;
861 for(int j = 0; j < num_samples; j++) {
862 float light_t = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT);
863 float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
864 float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
866 /* only sample triangle lights */
867 if(kernel_data.integrator.num_all_lights)
868 light_t = 0.5f*light_t;
870 if(direct_emission(kg, &sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &lamp)) {
871 /* trace shadow ray */
874 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
876 bool is_lamp = (lamp != ~0);
877 path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
885 for(int i = 0; i< sd.num_closure; i++) {
886 const ShaderClosure *sc = &sd.closure[i];
888 if(!CLOSURE_IS_BSDF(sc->type))
890 /* transparency is not handled here, but in outer loop */
891 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
896 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
897 num_samples = kernel_data.integrator.diffuse_samples;
898 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
899 num_samples = kernel_data.integrator.glossy_samples;
901 num_samples = kernel_data.integrator.transmission_samples;
903 float num_samples_inv = 1.0f/num_samples;
905 for(int j = 0; j < num_samples; j++) {
909 float3 bsdf_omega_in;
910 differential3 bsdf_domega_in;
911 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
912 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
915 label = shader_bsdf_sample_closure(kg, &sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
916 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
918 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
921 /* modify throughput */
922 float3 tp = throughput;
923 path_radiance_bsdf_bounce(&L, &tp, &bsdf_eval, bsdf_pdf, state.bounce, label);
926 float min_ray_pdf = FLT_MAX;
928 if(!(label & LABEL_TRANSPARENT))
929 min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
931 /* modify path state */
932 PathState ps = state;
933 path_state_next(kg, &ps, label);
938 bsdf_ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
939 bsdf_ray.D = bsdf_omega_in;
940 bsdf_ray.t = FLT_MAX;
941 #ifdef __RAY_DIFFERENTIALS__
943 bsdf_ray.dD = bsdf_domega_in;
945 #ifdef __OBJECT_MOTION__
946 bsdf_ray.time = sd.time;
949 kernel_path_indirect(kg, rng, sample*num_samples + j, bsdf_ray, buffer,
950 tp*num_samples_inv, num_samples,
951 min_ray_pdf, bsdf_pdf, ps, rng_offset+PRNG_BOUNCE_NUM, &L);
955 /* continue in case of transparency */
956 throughput *= shader_bsdf_transparency(kg, &sd);
957 shader_release(kg, &sd);
959 if(is_zero(throughput))
962 path_state_next(kg, &state, LABEL_TRANSPARENT);
963 ray.P = ray_offset(sd.P, -sd.Ng);
964 ray.t -= sd.ray_length; /* clipping works through transparent */
967 float3 L_sum = path_radiance_sum(kg, &L);
969 #ifdef __CLAMP_SAMPLE__
970 path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
973 kernel_write_light_passes(kg, buffer, &L, sample);
975 return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
980 __device void kernel_path_trace(KernelGlobals *kg,
981 __global float *buffer, __global uint *rng_state,
982 int sample, int x, int y, int offset, int stride)
985 int index = offset + x + y*stride;
986 int pass_stride = kernel_data.film.pass_stride;
989 buffer += index*pass_stride;
991 /* initialize random numbers */
997 path_rng_init(kg, rng_state, sample, &rng, x, y, &filter_u, &filter_v);
999 /* sample camera ray */
1002 float lens_u = path_rng(kg, &rng, sample, PRNG_LENS_U);
1003 float lens_v = path_rng(kg, &rng, sample, PRNG_LENS_V);
1005 #ifdef __CAMERA_MOTION__
1006 float time = path_rng(kg, &rng, sample, PRNG_TIME);
1011 camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, &ray);
1016 if (ray.t != 0.0f) {
1017 #ifdef __NON_PROGRESSIVE__
1018 if(kernel_data.integrator.progressive)
1020 L = kernel_path_progressive(kg, &rng, sample, ray, buffer);
1021 #ifdef __NON_PROGRESSIVE__
1023 L = kernel_path_non_progressive(kg, &rng, sample, ray, buffer);
1027 L = make_float4(0.f, 0.f, 0.f, 0.f);
1029 /* accumulate result in output buffer */
1030 kernel_write_pass_float4(buffer, sample, L);
1032 path_rng_end(kg, rng_state, rng);