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"
31 #include "kernel_random.h"
32 #include "kernel_bvh.h"
33 #include "kernel_accumulate.h"
34 #include "kernel_camera.h"
35 #include "kernel_shader.h"
36 #include "kernel_light.h"
37 #include "kernel_emission.h"
38 #include "kernel_passes.h"
41 #include "kernel_subsurface.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); /* todo: try using max here */
159 __device_inline bool shadow_blocked(KernelGlobals *kg, PathState *state, Ray *ray, float3 *shadow)
161 *shadow = make_float3(1.0f, 1.0f, 1.0f);
168 bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect, NULL, 0.0f, 0.0f);
170 bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect);
173 #ifdef __TRANSPARENT_SHADOWS__
174 if(result && kernel_data.integrator.transparent_shadows) {
175 /* transparent shadows work in such a way to try to minimize overhead
176 * in cases where we don't need them. after a regular shadow ray is
177 * cast we check if the hit primitive was potentially transparent, and
178 * only in that case start marching. this gives on extra ray cast for
179 * the cases were we do want transparency.
181 * also note that for this to work correct, multi close sampling must
182 * be used, since we don't pass a random number to shader_eval_surface */
183 if(shader_transparent_shadow(kg, &isect)) {
184 float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
185 float3 Pend = ray->P + ray->D*ray->t;
186 int bounce = state->transparent_bounce;
189 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
192 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
193 /* todo: get random number somewhere for probabilistic terminate */
195 float probability = average(throughput);
196 float terminate = 0.0f;
198 if(terminate >= probability)
201 throughput /= probability;
206 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect, NULL, 0.0f, 0.0f)) {
208 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect)) {
210 *shadow *= throughput;
214 if(!shader_transparent_shadow(kg, &isect))
218 shader_setup_from_ray(kg, &sd, &isect, ray, state->bounce+1);
219 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW, SHADER_CONTEXT_SHADOW);
221 throughput *= shader_bsdf_transparency(kg, &sd);
223 ray->P = ray_offset(sd.P, -sd.Ng);
224 if(ray->t != FLT_MAX)
225 ray->D = normalize_len(Pend - ray->P, &ray->t);
236 __device float4 kernel_path_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
240 float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
241 float L_transparent = 0.0f;
243 path_radiance_init(&L, kernel_data.film.use_light_pass);
245 float min_ray_pdf = FLT_MAX;
246 float ray_pdf = 0.0f;
251 int rng_offset = PRNG_BASE_NUM;
253 int num_samples = kernel_data.integrator.aa_samples;
258 path_state_init(&state);
261 for(;; rng_offset += PRNG_BOUNCE_NUM) {
262 /* intersect scene */
264 uint visibility = path_state_ray_visibility(kg, &state);
267 float difl = 0.0f, extmax = 0.0f;
270 if(kernel_data.bvh.have_curves) {
271 if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {
272 float3 pixdiff = ray.dD.dx + ray.dD.dy;
273 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
274 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
277 extmax = kernel_data.curve.maximum_width;
278 lcg_state = lcg_init(*rng + rng_offset + sample*0x51633e2d);
281 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
283 bool hit = scene_intersect(kg, &ray, visibility, &isect);
287 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
288 /* ray starting from previous non-transparent bounce */
291 light_ray.P = ray.P - ray_t*ray.D;
295 light_ray.time = ray.time;
296 light_ray.dD = ray.dD;
297 light_ray.dP = ray.dP;
299 /* intersect with lamp */
300 float light_t = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT);
303 if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission, state.bounce))
304 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
309 /* eval background shader if nothing hit */
310 if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
311 L_transparent += average(throughput);
314 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
319 #ifdef __BACKGROUND__
320 /* sample background shader */
321 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf, state.bounce);
322 path_radiance_accum_background(&L, throughput, L_background, state.bounce);
330 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
331 float rbsdf = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF);
332 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
336 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
337 if(kernel_data.background.transparent) {
338 float3 holdout_weight;
340 if(sd.flag & SD_HOLDOUT_MASK)
341 holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
343 holdout_weight = shader_holdout_eval(kg, &sd);
345 /* any throughput is ok, should all be identical here */
346 L_transparent += average(holdout_weight*throughput);
349 if(sd.flag & SD_HOLDOUT_MASK)
354 /* holdout mask objects do not write data passes */
355 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
357 /* blurring of bsdf after bounces, for rays that have a small likelihood
358 * of following this particular path (diffuse, rough glossy) */
359 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
360 float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
362 if(blur_pdf < 1.0f) {
363 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
364 shader_bsdf_blur(kg, &sd, blur_roughness);
370 if(sd.flag & SD_EMISSION) {
371 /* todo: is isect.t wrong here for transparent surfaces? */
372 float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
373 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
377 /* path termination. this is a strange place to put the termination, it's
378 * mainly due to the mixed in MIS that we use. gives too many unneeded
379 * shader evaluations, only need emission if we are going to terminate */
380 float probability = path_state_terminate_probability(kg, &state, throughput);
382 if(probability == 0.0f) {
385 else if(probability != 1.0f) {
386 float terminate = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_TERMINATE);
388 if(terminate >= probability)
391 throughput /= probability;
394 #ifdef __SUBSURFACE__
395 /* bssrdf scatter to a different location on the same object, replacing
396 * the closures with a diffuse BSDF */
397 if(sd.flag & SD_BSSRDF) {
398 float bssrdf_probability;
399 ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
401 /* modify throughput for picking bssrdf or bsdf */
402 throughput *= bssrdf_probability;
404 /* do bssrdf scatter step if we picked a bssrdf closure */
406 uint lcg_state = lcg_init(*rng + rng_offset + sample*0x68bc21eb);
407 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, false);
413 /* ambient occlusion */
414 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
415 /* todo: solve correlation */
416 float bsdf_u, bsdf_v;
417 path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
419 float ao_factor = kernel_data.background.ao_factor;
421 float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
425 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
427 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
431 light_ray.P = ray_offset(sd.P, sd.Ng);
433 light_ray.t = kernel_data.background.ao_distance;
434 #ifdef __OBJECT_MOTION__
435 light_ray.time = sd.time;
437 light_ray.dP = sd.dP;
438 light_ray.dD = differential3_zero();
440 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
441 path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
447 if(kernel_data.integrator.use_direct_light) {
448 /* sample illumination from lights to find path contribution */
449 if(sd.flag & SD_BSDF_HAS_EVAL) {
450 float light_t = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT);
451 #ifdef __MULTI_CLOSURE__
452 float light_o = 0.0f;
454 float light_o = path_rng_1D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT_F);
456 float light_u, light_v;
457 path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_LIGHT_U, &light_u, &light_v);
463 #ifdef __OBJECT_MOTION__
464 light_ray.time = sd.time;
467 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
468 /* trace shadow ray */
471 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
473 path_radiance_accum_light(&L, throughput, &L_light, shadow, 1.0f, state.bounce, is_lamp);
480 /* no BSDF? we can stop here */
481 if(!(sd.flag & SD_BSDF))
487 float3 bsdf_omega_in;
488 differential3 bsdf_domega_in;
489 float bsdf_u, bsdf_v;
490 path_rng_2D(kg, rng, sample, num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
493 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
494 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
496 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
499 /* modify throughput */
500 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
503 if(!(label & LABEL_TRANSPARENT)) {
508 min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
511 /* update path state */
512 path_state_next(kg, &state, label);
515 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
516 ray.D = bsdf_omega_in;
518 if(state.bounce == 0)
519 ray.t -= sd.ray_length; /* clipping works through transparent */
523 #ifdef __RAY_DIFFERENTIALS__
525 ray.dD = bsdf_domega_in;
529 float3 L_sum = path_radiance_sum(kg, &L);
531 #ifdef __CLAMP_SAMPLE__
532 path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
535 kernel_write_light_passes(kg, buffer, &L, sample);
537 return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
540 #ifdef __NON_PROGRESSIVE__
542 __device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer,
543 float3 throughput, int num_samples, int num_total_samples,
544 float min_ray_pdf, float ray_pdf, PathState state, int rng_offset, PathRadiance *L)
551 for(;; rng_offset += PRNG_BOUNCE_NUM) {
552 /* intersect scene */
554 uint visibility = path_state_ray_visibility(kg, &state);
556 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
558 bool hit = scene_intersect(kg, &ray, visibility, &isect);
562 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
563 /* ray starting from previous non-transparent bounce */
566 light_ray.P = ray.P - ray_t*ray.D;
570 light_ray.time = ray.time;
571 light_ray.dD = ray.dD;
572 light_ray.dP = ray.dP;
574 /* intersect with lamp */
575 float light_t = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT);
578 if(indirect_lamp_emission(kg, &light_ray, state.flag, ray_pdf, light_t, &emission, state.bounce))
579 path_radiance_accum_emission(L, throughput, emission, state.bounce);
584 #ifdef __BACKGROUND__
585 /* sample background shader */
586 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf, state.bounce);
587 path_radiance_accum_background(L, throughput, L_background, state.bounce);
595 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
596 float rbsdf = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF);
597 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
598 shader_merge_closures(kg, &sd);
600 /* blurring of bsdf after bounces, for rays that have a small likelihood
601 * of following this particular path (diffuse, rough glossy) */
602 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
603 float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
605 if(blur_pdf < 1.0f) {
606 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
607 shader_bsdf_blur(kg, &sd, blur_roughness);
613 if(sd.flag & SD_EMISSION) {
614 float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
615 path_radiance_accum_emission(L, throughput, emission, state.bounce);
619 /* path termination. this is a strange place to put the termination, it's
620 * mainly due to the mixed in MIS that we use. gives too many unneeded
621 * shader evaluations, only need emission if we are going to terminate */
622 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
624 if(probability == 0.0f) {
627 else if(probability != 1.0f) {
628 float terminate = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_TERMINATE);
630 if(terminate >= probability)
633 throughput /= probability;
636 #ifdef __SUBSURFACE__
637 /* bssrdf scatter to a different location on the same object, replacing
638 * the closures with a diffuse BSDF */
639 if(sd.flag & SD_BSSRDF) {
640 float bssrdf_probability;
641 ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
643 /* modify throughput for picking bssrdf or bsdf */
644 throughput *= bssrdf_probability;
646 /* do bssrdf scatter step if we picked a bssrdf closure */
648 uint lcg_state = lcg_init(*rng + rng_offset + sample*0x68bc21eb);
649 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, false);
655 /* ambient occlusion */
656 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
657 float bsdf_u, bsdf_v;
658 path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
660 float ao_factor = kernel_data.background.ao_factor;
662 float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
666 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
668 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
672 light_ray.P = ray_offset(sd.P, sd.Ng);
674 light_ray.t = kernel_data.background.ao_distance;
675 #ifdef __OBJECT_MOTION__
676 light_ray.time = sd.time;
678 light_ray.dP = sd.dP;
679 light_ray.dD = differential3_zero();
681 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
682 path_radiance_accum_ao(L, throughput, ao_bsdf, ao_shadow, state.bounce);
688 if(kernel_data.integrator.use_direct_light) {
689 /* sample illumination from lights to find path contribution */
690 if(sd.flag & SD_BSDF_HAS_EVAL) {
691 float light_t = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT);
692 #ifdef __MULTI_CLOSURE__
693 float light_o = 0.0f;
695 float light_o = path_rng_1D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT_F);
697 float light_u, light_v;
698 path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_LIGHT_U, &light_u, &light_v);
704 #ifdef __OBJECT_MOTION__
705 light_ray.time = sd.time;
708 /* sample random light */
709 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
710 /* trace shadow ray */
713 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
715 path_radiance_accum_light(L, throughput, &L_light, shadow, 1.0f, state.bounce, is_lamp);
722 /* no BSDF? we can stop here */
723 if(!(sd.flag & SD_BSDF))
729 float3 bsdf_omega_in;
730 differential3 bsdf_domega_in;
731 float bsdf_u, bsdf_v;
732 path_rng_2D(kg, rng, sample, num_total_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
735 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
736 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
738 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
741 /* modify throughput */
742 path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
745 if(!(label & LABEL_TRANSPARENT)) {
750 min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
753 /* update path state */
754 path_state_next(kg, &state, label);
757 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
758 ray.D = bsdf_omega_in;
760 #ifdef __RAY_DIFFERENTIALS__
762 ray.dD = bsdf_domega_in;
767 __device_noinline void kernel_path_non_progressive_lighting(KernelGlobals *kg, RNG *rng, int sample,
768 ShaderData *sd, float3 throughput, float num_samples_adjust,
769 float min_ray_pdf, float ray_pdf, PathState state,
770 int rng_offset, PathRadiance *L, __global float *buffer)
773 int aa_samples = kernel_data.integrator.aa_samples;
779 /* ambient occlusion */
780 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
781 int num_samples = ceil_to_int(kernel_data.integrator.ao_samples*num_samples_adjust);
782 float num_samples_inv = num_samples_adjust/num_samples;
783 float ao_factor = kernel_data.background.ao_factor;
785 float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
787 for(int j = 0; j < num_samples; j++) {
788 float bsdf_u, bsdf_v;
789 path_rng_2D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
794 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
796 if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
800 light_ray.P = ray_offset(sd->P, sd->Ng);
802 light_ray.t = kernel_data.background.ao_distance;
803 #ifdef __OBJECT_MOTION__
804 light_ray.time = sd->time;
806 light_ray.dP = sd->dP;
807 light_ray.dD = differential3_zero();
809 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
810 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 = ceil_to_int(num_samples_adjust*light_select_num_samples(kg, i));
831 float num_samples_inv = num_samples_adjust/(num_samples*kernel_data.integrator.num_all_lights);
832 RNG lamp_rng = cmj_hash(*rng, i);
834 if(kernel_data.integrator.pdf_triangles != 0.0f)
835 num_samples_inv *= 0.5f;
837 for(int j = 0; j < num_samples; j++) {
838 float light_u, light_v;
839 path_rng_2D(kg, &lamp_rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT_U, &light_u, &light_v);
841 if(direct_emission(kg, sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp, state.bounce)) {
842 /* trace shadow ray */
845 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
847 path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state.bounce, is_lamp);
853 /* mesh light sampling */
854 if(kernel_data.integrator.pdf_triangles != 0.0f) {
855 int num_samples = ceil_to_int(num_samples_adjust*kernel_data.integrator.mesh_light_samples);
856 float num_samples_inv = num_samples_adjust/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_1D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT);
863 float light_u, light_v;
864 path_rng_2D(kg, rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_LIGHT_U, &light_u, &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, &is_lamp, state.bounce)) {
871 /* trace shadow ray */
874 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
876 path_radiance_accum_light(L, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, state.bounce, is_lamp);
884 for(int i = 0; i< sd->num_closure; i++) {
885 const ShaderClosure *sc = &sd->closure[i];
887 if(!CLOSURE_IS_BSDF(sc->type))
889 /* transparency is not handled here, but in outer loop */
890 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
895 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type) || CLOSURE_IS_BSDF_BSSRDF(sc->type))
896 num_samples = kernel_data.integrator.diffuse_samples;
897 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
898 num_samples = kernel_data.integrator.glossy_samples;
900 num_samples = kernel_data.integrator.transmission_samples;
902 num_samples = ceil_to_int(num_samples_adjust*num_samples);
904 float num_samples_inv = num_samples_adjust/num_samples;
905 RNG bsdf_rng = cmj_hash(*rng, i);
907 for(int j = 0; j < num_samples; j++) {
911 float3 bsdf_omega_in;
912 differential3 bsdf_domega_in;
913 float bsdf_u, bsdf_v;
914 path_rng_2D(kg, &bsdf_rng, sample*num_samples + j, aa_samples*num_samples, rng_offset + PRNG_BSDF_U, &bsdf_u, &bsdf_v);
917 label = shader_bsdf_sample_closure(kg, sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
918 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
920 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
923 /* modify throughput */
924 float3 tp = throughput;
925 path_radiance_bsdf_bounce(L, &tp, &bsdf_eval, bsdf_pdf, state.bounce, label);
928 float min_ray_pdf = fminf(bsdf_pdf, FLT_MAX);
930 /* modify path state */
931 PathState ps = state;
932 path_state_next(kg, &ps, label);
937 bsdf_ray.P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
938 bsdf_ray.D = bsdf_omega_in;
939 bsdf_ray.t = FLT_MAX;
940 #ifdef __RAY_DIFFERENTIALS__
941 bsdf_ray.dP = sd->dP;
942 bsdf_ray.dD = bsdf_domega_in;
944 #ifdef __OBJECT_MOTION__
945 bsdf_ray.time = sd->time;
948 kernel_path_indirect(kg, rng, sample*num_samples + j, bsdf_ray, buffer,
949 tp*num_samples_inv, num_samples, aa_samples*num_samples,
950 min_ray_pdf, bsdf_pdf, ps, rng_offset+PRNG_BOUNCE_NUM, L);
952 /* for render passes, sum and reset indirect light pass variables
953 * for the next samples */
954 path_radiance_sum_indirect(L);
955 path_radiance_reset_indirect(L);
960 __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
964 float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
965 float L_transparent = 0.0f;
967 path_radiance_init(&L, kernel_data.film.use_light_pass);
969 float ray_pdf = 0.0f;
971 int rng_offset = PRNG_BASE_NUM;
973 int aa_samples = kernel_data.integrator.aa_samples;
978 path_state_init(&state);
980 for(;; rng_offset += PRNG_BOUNCE_NUM) {
981 /* intersect scene */
983 uint visibility = path_state_ray_visibility(kg, &state);
986 float difl = 0.0f, extmax = 0.0f;
989 if(kernel_data.bvh.have_curves) {
990 if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {
991 float3 pixdiff = ray.dD.dx + ray.dD.dy;
992 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
993 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
996 extmax = kernel_data.curve.maximum_width;
997 lcg_state = lcg_init(*rng + rng_offset + sample*0x51633e2d);
1000 if(!scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax)) {
1002 if(!scene_intersect(kg, &ray, visibility, &isect)) {
1004 /* eval background shader if nothing hit */
1005 if(kernel_data.background.transparent) {
1006 L_transparent += average(throughput);
1009 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
1014 #ifdef __BACKGROUND__
1015 /* sample background shader */
1016 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf, state.bounce);
1017 path_radiance_accum_background(&L, throughput, L_background, state.bounce);
1025 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce);
1026 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
1027 shader_merge_closures(kg, &sd);
1031 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
1032 if(kernel_data.background.transparent) {
1033 float3 holdout_weight;
1035 if(sd.flag & SD_HOLDOUT_MASK)
1036 holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
1038 holdout_weight = shader_holdout_eval(kg, &sd);
1040 /* any throughput is ok, should all be identical here */
1041 L_transparent += average(holdout_weight*throughput);
1044 if(sd.flag & SD_HOLDOUT_MASK)
1049 /* holdout mask objects do not write data passes */
1050 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
1054 if(sd.flag & SD_EMISSION) {
1055 float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, ray_pdf);
1056 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
1060 /* transparency termination */
1061 if(state.flag & PATH_RAY_TRANSPARENT) {
1062 /* path termination. this is a strange place to put the termination, it's
1063 * mainly due to the mixed in MIS that we use. gives too many unneeded
1064 * shader evaluations, only need emission if we are going to terminate */
1065 float probability = path_state_terminate_probability(kg, &state, throughput);
1067 if(probability == 0.0f) {
1070 else if(probability != 1.0f) {
1071 float terminate = path_rng_1D(kg, rng, sample, aa_samples, rng_offset + PRNG_TERMINATE);
1073 if(terminate >= probability)
1076 throughput /= probability;
1080 #ifdef __SUBSURFACE__
1081 /* bssrdf scatter to a different location on the same object */
1082 if(sd.flag & SD_BSSRDF) {
1083 for(int i = 0; i< sd.num_closure; i++) {
1084 ShaderClosure *sc = &sd.closure[i];
1086 if(!CLOSURE_IS_BSSRDF(sc->type))
1089 /* set up random number generator */
1090 uint lcg_state = lcg_init(*rng + rng_offset + sample*0x68bc21eb);
1091 int num_samples = kernel_data.integrator.subsurface_samples;
1092 float num_samples_inv = 1.0f/num_samples;
1094 /* do subsurface scatter step with copy of shader data, this will
1095 * replace the BSSRDF with a diffuse BSDF closure */
1096 for(int j = 0; j < num_samples; j++) {
1097 ShaderData bssrdf_sd = sd;
1098 subsurface_scatter_step(kg, &bssrdf_sd, state.flag, sc, &lcg_state, true);
1100 /* compute lighting with the BSDF closure */
1101 kernel_path_non_progressive_lighting(kg, rng, sample*num_samples + j,
1102 &bssrdf_sd, throughput, num_samples_inv,
1103 ray_pdf, ray_pdf, state, rng_offset, &L, buffer);
1110 kernel_path_non_progressive_lighting(kg, rng, sample, &sd, throughput,
1111 1.0f, ray_pdf, ray_pdf, state, rng_offset, &L, buffer);
1113 /* continue in case of transparency */
1114 throughput *= shader_bsdf_transparency(kg, &sd);
1116 if(is_zero(throughput))
1119 path_state_next(kg, &state, LABEL_TRANSPARENT);
1120 ray.P = ray_offset(sd.P, -sd.Ng);
1121 ray.t -= sd.ray_length; /* clipping works through transparent */
1124 float3 L_sum = path_radiance_sum(kg, &L);
1126 #ifdef __CLAMP_SAMPLE__
1127 path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
1130 kernel_write_light_passes(kg, buffer, &L, sample);
1132 return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1137 __device_inline void kernel_path_trace_setup(KernelGlobals *kg, __global uint *rng_state, int sample, int x, int y, RNG *rng, Ray *ray)
1142 int num_samples = kernel_data.integrator.aa_samples;
1144 int num_samples = 0;
1147 path_rng_init(kg, rng_state, sample, num_samples, rng, x, y, &filter_u, &filter_v);
1149 /* sample camera ray */
1151 float lens_u = 0.0f, lens_v = 0.0f;
1153 if(kernel_data.cam.aperturesize > 0.0f)
1154 path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1158 #ifdef __CAMERA_MOTION__
1159 if(kernel_data.cam.shuttertime != -1.0f)
1160 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1163 camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, ray);
1166 __device void kernel_path_trace_progressive(KernelGlobals *kg,
1167 __global float *buffer, __global uint *rng_state,
1168 int sample, int x, int y, int offset, int stride)
1171 int index = offset + x + y*stride;
1172 int pass_stride = kernel_data.film.pass_stride;
1175 buffer += index*pass_stride;
1177 /* initialize random numbers and ray */
1181 kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1187 L = kernel_path_progressive(kg, &rng, sample, ray, buffer);
1189 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1191 /* accumulate result in output buffer */
1192 kernel_write_pass_float4(buffer, sample, L);
1194 path_rng_end(kg, rng_state, rng);
1197 #ifdef __NON_PROGRESSIVE__
1198 __device void kernel_path_trace_non_progressive(KernelGlobals *kg,
1199 __global float *buffer, __global uint *rng_state,
1200 int sample, int x, int y, int offset, int stride)
1203 int index = offset + x + y*stride;
1204 int pass_stride = kernel_data.film.pass_stride;
1207 buffer += index*pass_stride;
1209 /* initialize random numbers and ray */
1213 kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1219 L = kernel_path_non_progressive(kg, &rng, sample, ray, buffer);
1221 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1223 /* accumulate result in output buffer */
1224 kernel_write_pass_float4(buffer, sample, L);
1226 path_rng_end(kg, rng_state, rng);