Cycles: option to make background visible through glass transparent.
[blender-staging.git] / intern / cycles / kernel / kernel_path.h
index 86b2aa00776acc5c00e1e7b4f6a8283214393fe0..1e98bca66ad5ae6a48ee6c82c3d977d5020d9732 100644 (file)
@@ -59,6 +59,11 @@ ccl_device_forceinline bool kernel_path_scene_intersect(
 {
        uint visibility = path_state_ray_visibility(kg, state);
 
+       if(path_state_ao_bounce(kg, state)) {
+               visibility = PATH_RAY_SHADOW;
+               ray->t = kernel_data.background.ao_distance;
+       }
+
 #ifdef __HAIR__
        float difl = 0.0f, extmax = 0.0f;
        uint lcg_state = 0;
@@ -74,11 +79,6 @@ ccl_device_forceinline bool kernel_path_scene_intersect(
                lcg_state = lcg_state_init_addrspace(state, 0x51633e2d);
        }
 
-       if(path_state_ao_bounce(kg, state)) {
-               visibility = PATH_RAY_SHADOW;
-               ray->t = kernel_data.background.ao_distance;
-       }
-
        bool hit = scene_intersect(kg, *ray, visibility, isect, &lcg_state, difl, extmax);
 #else
        bool hit = scene_intersect(kg, *ray, visibility, isect, NULL, 0.0f, 0.0f);
@@ -132,22 +132,28 @@ ccl_device_forceinline void kernel_path_background(
        ccl_addr_space PathState *state,
        ccl_addr_space Ray *ray,
        float3 throughput,
-       ShaderData *emission_sd,
+       ShaderData *sd,
        PathRadiance *L)
 {
        /* eval background shader if nothing hit */
-       if(kernel_data.background.transparent && (state->flag & PATH_RAY_CAMERA)) {
+       if(kernel_data.background.transparent && (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
                L->transparent += average(throughput);
 
 #ifdef __PASSES__
-               if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
+               if(!(kernel_data.film.light_pass_flag & PASSMASK(BACKGROUND)))
 #endif  /* __PASSES__ */
                        return;
        }
 
+       /* When using the ao bounces approximation, adjust background
+        * shader intensity with ao factor. */
+       if(path_state_ao_bounce(kg, state)) {
+               throughput *= kernel_data.background.ao_bounces_factor;
+       }
+
 #ifdef __BACKGROUND__
        /* sample background shader */
-       float3 L_background = indirect_background(kg, emission_sd, state, ray);
+       float3 L_background = indirect_background(kg, sd, state, ray);
        path_radiance_accum_background(L, state, throughput, L_background);
 #endif  /* __BACKGROUND__ */
 }
@@ -170,87 +176,90 @@ ccl_device_forceinline VolumeIntegrateResult kernel_path_volume(
        if(!hit) {
                kernel_volume_clean_stack(kg, state->volume_stack);
        }
+
+       if(state->volume_stack[0].shader == SHADER_NONE) {
+               return VOLUME_PATH_ATTENUATED;
+       }
+
        /* volume attenuation, emission, scatter */
-       if(state->volume_stack[0].shader != SHADER_NONE) {
-               Ray volume_ray = *ray;
-               volume_ray.t = (hit)? isect->t: FLT_MAX;
+       Ray volume_ray = *ray;
+       volume_ray.t = (hit)? isect->t: FLT_MAX;
 
-               bool heterogeneous = volume_stack_is_heterogeneous(kg, state->volume_stack);
+       bool heterogeneous = volume_stack_is_heterogeneous(kg, state->volume_stack);
 
 #  ifdef __VOLUME_DECOUPLED__
-               int sampling_method = volume_stack_sampling_method(kg, state->volume_stack);
-               bool direct = (state->flag & PATH_RAY_CAMERA) != 0;
-               bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, direct, sampling_method);
+       int sampling_method = volume_stack_sampling_method(kg, state->volume_stack);
+       bool direct = (state->flag & PATH_RAY_CAMERA) != 0;
+       bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, direct, sampling_method);
 
-               if(decoupled) {
-                       /* cache steps along volume for repeated sampling */
-                       VolumeSegment volume_segment;
+       if(decoupled) {
+               /* cache steps along volume for repeated sampling */
+               VolumeSegment volume_segment;
 
-                       shader_setup_from_volume(kg, sd, &volume_ray);
-                       kernel_volume_decoupled_record(kg, state,
-                               &volume_ray, sd, &volume_segment, heterogeneous);
+               shader_setup_from_volume(kg, sd, &volume_ray);
+               kernel_volume_decoupled_record(kg, state,
+                       &volume_ray, sd, &volume_segment, heterogeneous);
 
-                       volume_segment.sampling_method = sampling_method;
+               volume_segment.sampling_method = sampling_method;
 
-                       /* emission */
-                       if(volume_segment.closure_flag & SD_EMISSION)
-                               path_radiance_accum_emission(L, state, *throughput, volume_segment.accum_emission);
+               /* emission */
+               if(volume_segment.closure_flag & SD_EMISSION)
+                       path_radiance_accum_emission(L, state, *throughput, volume_segment.accum_emission);
 
-                       /* scattering */
-                       VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
+               /* scattering */
+               VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
 
-                       if(volume_segment.closure_flag & SD_SCATTER) {
-                               int all = kernel_data.integrator.sample_all_lights_indirect;
+               if(volume_segment.closure_flag & SD_SCATTER) {
+                       int all = kernel_data.integrator.sample_all_lights_indirect;
 
-                               /* direct light sampling */
-                               kernel_branched_path_volume_connect_light(kg, sd,
-                                       emission_sd, *throughput, state, L, all,
-                                       &volume_ray, &volume_segment);
+                       /* direct light sampling */
+                       kernel_branched_path_volume_connect_light(kg, sd,
+                               emission_sd, *throughput, state, L, all,
+                               &volume_ray, &volume_segment);
 
-                               /* indirect sample. if we use distance sampling and take just
-                                * one sample for direct and indirect light, we could share
-                                * this computation, but makes code a bit complex */
-                               float rphase = path_state_rng_1D(kg, state, PRNG_PHASE);
-                               float rscatter = path_state_rng_1D(kg, state, PRNG_SCATTER_DISTANCE);
+                       /* indirect sample. if we use distance sampling and take just
+                        * one sample for direct and indirect light, we could share
+                        * this computation, but makes code a bit complex */
+                       float rphase = path_state_rng_1D(kg, state, PRNG_PHASE_CHANNEL);
+                       float rscatter = path_state_rng_1D(kg, state, PRNG_SCATTER_DISTANCE);
 
-                               result = kernel_volume_decoupled_scatter(kg,
-                                       state, &volume_ray, sd, throughput,
-                                       rphase, rscatter, &volume_segment, NULL, true);
-                       }
+                       result = kernel_volume_decoupled_scatter(kg,
+                               state, &volume_ray, sd, throughput,
+                               rphase, rscatter, &volume_segment, NULL, true);
+               }
 
-                       /* free cached steps */
-                       kernel_volume_decoupled_free(kg, &volume_segment);
+               /* free cached steps */
+               kernel_volume_decoupled_free(kg, &volume_segment);
 
-                       if(result == VOLUME_PATH_SCATTERED) {
-                               if(kernel_path_volume_bounce(kg, sd, throughput, state, L, ray))
-                                       return VOLUME_PATH_SCATTERED;
-                               else
-                                       return VOLUME_PATH_MISSED;
-                       }
-                       else {
-                               *throughput *= volume_segment.accum_transmittance;
-                       }
+               if(result == VOLUME_PATH_SCATTERED) {
+                       if(kernel_path_volume_bounce(kg, sd, throughput, state, &L->state, ray))
+                               return VOLUME_PATH_SCATTERED;
+                       else
+                               return VOLUME_PATH_MISSED;
                }
-               else
+               else {
+                       *throughput *= volume_segment.accum_transmittance;
+               }
+       }
+       else
 #  endif  /* __VOLUME_DECOUPLED__ */
-               {
-                       /* integrate along volume segment with distance sampling */
-                       VolumeIntegrateResult result = kernel_volume_integrate(
-                               kg, state, sd, &volume_ray, L, throughput, heterogeneous);
+       {
+               /* integrate along volume segment with distance sampling */
+               VolumeIntegrateResult result = kernel_volume_integrate(
+                       kg, state, sd, &volume_ray, L, throughput, heterogeneous);
 
 #  ifdef __VOLUME_SCATTER__
-                       if(result == VOLUME_PATH_SCATTERED) {
-                               /* direct lighting */
-                               kernel_path_volume_connect_light(kg, sd, emission_sd, *throughput, state, L);
-
-                               /* indirect light bounce */
-                               if(kernel_path_volume_bounce(kg, sd, throughput, state, L, ray))
-                                       return VOLUME_PATH_SCATTERED;
-                               else
-                                       return VOLUME_PATH_MISSED;
-                       }
-#  endif  /* __VOLUME_SCATTER__ */
+               if(result == VOLUME_PATH_SCATTERED) {
+                       /* direct lighting */
+                       kernel_path_volume_connect_light(kg, sd, emission_sd, *throughput, state, L);
+
+                       /* indirect light bounce */
+                       if(kernel_path_volume_bounce(kg, sd, throughput, state, &L->state, ray))
+                               return VOLUME_PATH_SCATTERED;
+                       else
+                               return VOLUME_PATH_MISSED;
                }
+#  endif  /* __VOLUME_SCATTER__ */
        }
 
        return VOLUME_PATH_ATTENUATED;
@@ -271,7 +280,7 @@ ccl_device_forceinline bool kernel_path_shader_apply(
 {
 #ifdef __SHADOW_TRICKS__
        if((sd->object_flag & SD_OBJECT_SHADOW_CATCHER)) {
-               if(state->flag & PATH_RAY_CAMERA) {
+               if(state->flag & PATH_RAY_TRANSPARENT_BACKGROUND) {
                        state->flag |= (PATH_RAY_SHADOW_CATCHER |
                                                   PATH_RAY_STORE_SHADOW_INFO);
 
@@ -293,7 +302,7 @@ ccl_device_forceinline bool kernel_path_shader_apply(
 #ifdef __HOLDOUT__
        if(((sd->flag & SD_HOLDOUT) ||
                (sd->object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
-          (state->flag & PATH_RAY_CAMERA))
+          (state->flag & PATH_RAY_TRANSPARENT_BACKGROUND))
        {
                if(kernel_data.background.transparent) {
                        float3 holdout_weight;
@@ -398,7 +407,7 @@ ccl_device void kernel_path_indirect(KernelGlobals *kg,
                bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
 
                /* Find intersection with lamps and compute emission for MIS. */
-               kernel_path_lamp_emission(kg, state, ray, throughput, &isect, emission_sd, L);
+               kernel_path_lamp_emission(kg, state, ray, throughput, &isect, sd, L);
 
 #ifdef __VOLUME__
                /* Volume integration. */
@@ -422,7 +431,7 @@ ccl_device void kernel_path_indirect(KernelGlobals *kg,
 
                /* Shade background. */
                if(!hit) {
-                       kernel_path_background(kg, state, ray, throughput, emission_sd, L);
+                       kernel_path_background(kg, state, ray, throughput, sd, L);
                        break;
                }
                else if(path_state_ao_bounce(kg, state)) {
@@ -434,11 +443,8 @@ ccl_device void kernel_path_indirect(KernelGlobals *kg,
                                      sd,
                                      &isect,
                                      ray);
-               float rbsdf = path_state_rng_1D(kg, state, PRNG_BSDF);
-               shader_eval_surface(kg, sd, state, rbsdf, state->flag);
-#ifdef __BRANCHED_PATH__
-               shader_merge_closures(sd);
-#endif  /* __BRANCHED_PATH__ */
+               shader_eval_surface(kg, sd, state, state->flag, kernel_data.integrator.max_closures);
+               shader_prepare_closures(sd, state);
 
                /* Apply shadow catcher, holdout, emission. */
                if(!kernel_path_shader_apply(kg,
@@ -483,17 +489,18 @@ ccl_device void kernel_path_indirect(KernelGlobals *kg,
                /* bssrdf scatter to a different location on the same object, replacing
                 * the closures with a diffuse BSDF */
                if(sd->flag & SD_BSSRDF) {
-                       const ShaderClosure *sc = shader_bssrdf_pick(sd, &throughput);
+                       float bssrdf_u, bssrdf_v;
+                       path_state_rng_2D(kg,
+                                         state,
+                                         PRNG_BSDF_U,
+                                         &bssrdf_u, &bssrdf_v);
+
+                       const ShaderClosure *sc = shader_bssrdf_pick(sd, &throughput, &bssrdf_u);
 
                        /* do bssrdf scatter step if we picked a bssrdf closure */
                        if(sc) {
                                uint lcg_state = lcg_state_init(state, 0x68bc21eb);
 
-                               float bssrdf_u, bssrdf_v;
-                               path_state_rng_2D(kg,
-                                                 state,
-                                                 PRNG_BSDF_U,
-                                                 &bssrdf_u, &bssrdf_v);
                                subsurface_scatter_step(kg,
                                                        sd,
                                                        state,
@@ -521,7 +528,7 @@ ccl_device void kernel_path_indirect(KernelGlobals *kg,
                }
 #endif  /* defined(__EMISSION__) */
 
-               if(!kernel_path_surface_bounce(kg, sd, &throughput, state, L, ray))
+               if(!kernel_path_surface_bounce(kg, sd, &throughput, state, &L->state, ray))
                        break;
        }
 }
@@ -554,7 +561,7 @@ ccl_device_forceinline void kernel_path_integrate(
                bool hit = kernel_path_scene_intersect(kg, state, ray, &isect, L);
 
                /* Find intersection with lamps and compute emission for MIS. */
-               kernel_path_lamp_emission(kg, state, ray, throughput, &isect, emission_sd, L);
+               kernel_path_lamp_emission(kg, state, ray, throughput, &isect, &sd, L);
 
 #ifdef __VOLUME__
                /* Volume integration. */
@@ -578,7 +585,7 @@ ccl_device_forceinline void kernel_path_integrate(
 
                /* Shade background. */
                if(!hit) {
-                       kernel_path_background(kg, state, ray, throughput, emission_sd, L);
+                       kernel_path_background(kg, state, ray, throughput, &sd, L);
                        break;
                }
                else if(path_state_ao_bounce(kg, state)) {
@@ -587,8 +594,8 @@ ccl_device_forceinline void kernel_path_integrate(
 
                /* Setup and evaluate shader. */
                shader_setup_from_ray(kg, &sd, &isect, ray);
-               float rbsdf = path_state_rng_1D(kg, state, PRNG_BSDF);
-               shader_eval_surface(kg, &sd, state, rbsdf, state->flag);
+               shader_eval_surface(kg, &sd, state, state->flag, kernel_data.integrator.max_closures);
+               shader_prepare_closures(&sd, state);
 
                /* Apply shadow catcher, holdout, emission. */
                if(!kernel_path_shader_apply(kg,
@@ -650,13 +657,11 @@ ccl_device_forceinline void kernel_path_integrate(
                kernel_path_surface_connect_light(kg, &sd, emission_sd, throughput, state, L);
 
                /* compute direct lighting and next bounce */
-               if(!kernel_path_surface_bounce(kg, &sd, &throughput, state, L, ray))
+               if(!kernel_path_surface_bounce(kg, &sd, &throughput, state, &L->state, ray))
                        break;
        }
 
 #ifdef __SUBSURFACE__
-               kernel_path_subsurface_accum_indirect(&ss_indirect, L);
-
                /* Trace indirect subsurface rays by restarting the loop. this uses less
                 * stack memory than invoking kernel_path_indirect.
                 */
@@ -676,24 +681,22 @@ ccl_device_forceinline void kernel_path_integrate(
 }
 
 ccl_device void kernel_path_trace(KernelGlobals *kg,
-       ccl_global float *buffer, ccl_global uint *rng_state,
+       ccl_global float *buffer,
        int sample, int x, int y, int offset, int stride)
 {
        /* buffer offset */
        int index = offset + x + y*stride;
        int pass_stride = kernel_data.film.pass_stride;
 
-       rng_state += index;
        buffer += index*pass_stride;
 
        /* Initialize random numbers and sample ray. */
        uint rng_hash;
        Ray ray;
 
-       kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng_hash, &ray);
+       kernel_path_trace_setup(kg, sample, x, y, &rng_hash, &ray);
 
        if(ray.t == 0.0f) {
-               kernel_write_result(kg, buffer, sample, NULL);
                return;
        }
 
@@ -703,9 +706,11 @@ ccl_device void kernel_path_trace(KernelGlobals *kg,
        PathRadiance L;
        path_radiance_init(&L, kernel_data.film.use_light_pass);
 
-       ShaderData emission_sd;
+       ShaderDataTinyStorage emission_sd_storage;
+       ShaderData *emission_sd = AS_SHADER_DATA(&emission_sd_storage);
+
        PathState state;
-       path_state_init(kg, &emission_sd, &state, rng_hash, sample, &ray);
+       path_state_init(kg, emission_sd, &state, rng_hash, sample, &ray);
 
        /* Integrate. */
        kernel_path_integrate(kg,
@@ -714,7 +719,7 @@ ccl_device void kernel_path_trace(KernelGlobals *kg,
                              &ray,
                              &L,
                              buffer,
-                             &emission_sd);
+                             emission_sd);
 
        kernel_write_result(kg, buffer, sample, &L);
 }