Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / kernel / split / kernel_subsurface_scatter.h
index 1dffe1b179e0e95a92ca4e9a72e6ab5b9766509a..887c3e313d1fb6ea0f8ca68e26fa95c7b36752c7 100644 (file)
@@ -38,9 +38,8 @@ ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_it
        SplitBranchedState *branched_state = &kernel_split_state.branched_state[ray_index];
 
        ShaderData *sd = &branched_state->sd;
-       RNG rng = kernel_split_state.rng[ray_index];
        PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
-       ShaderData *emission_sd = &kernel_split_state.sd_DL_shadow[ray_index];
+       ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
 
        for(int i = branched_state->ss_next_closure; i < sd->num_closure; i++) {
                ShaderClosure *sc = &sd->closure[i];
@@ -52,22 +51,20 @@ ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_it
                if(branched_state->ss_next_sample == 0 && branched_state->next_hit == 0 &&
                   branched_state->next_closure == 0 && branched_state->next_sample == 0)
                {
-                       branched_state->lcg_state = lcg_state_init(&rng,
-                                                                  branched_state->path_state.rng_offset,
-                                                                  branched_state->path_state.sample,
-                                                                  0x68bc21eb);
+                       branched_state->lcg_state = lcg_state_init_addrspace(&branched_state->path_state,
+                                                                            0x68bc21eb);
                }
                int num_samples = kernel_data.integrator.subsurface_samples;
                float num_samples_inv = 1.0f/num_samples;
-               RNG bssrdf_rng = cmj_hash(rng, i);
+               uint bssrdf_rng_hash = cmj_hash(branched_state->path_state.rng_hash, i);
 
                /* do subsurface scatter step with copy of shader data, this will
                 * replace the BSSRDF with a diffuse BSDF closure */
                for(int j = branched_state->ss_next_sample; j < num_samples; j++) {
-                       ccl_global SubsurfaceIntersection *ss_isect = &branched_state->ss_isect;
+                       ccl_global LocalIntersection *ss_isect = &branched_state->ss_isect;
                        float bssrdf_u, bssrdf_v;
                        path_branched_rng_2D(kg,
-                                            &bssrdf_rng,
+                                            bssrdf_rng_hash,
                                             &branched_state->path_state,
                                             j,
                                             num_samples,
@@ -77,8 +74,8 @@ ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_it
 
                        /* intersection is expensive so avoid doing multiple times for the same input */
                        if(branched_state->next_hit == 0 && branched_state->next_closure == 0 && branched_state->next_sample == 0) {
-                               RNG lcg_state = branched_state->lcg_state;
-                               SubsurfaceIntersection ss_isect_private;
+                               uint lcg_state = branched_state->lcg_state;
+                               LocalIntersection ss_isect_private;
 
                                branched_state->num_hits = subsurface_scatter_multi_intersect(kg,
                                                                                              &ss_isect_private,
@@ -101,11 +98,11 @@ ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_it
 
                        /* compute lighting with the BSDF closure */
                        for(int hit = branched_state->next_hit; hit < branched_state->num_hits; hit++) {
-                               ShaderData *bssrdf_sd = &kernel_split_state.sd[ray_index];
+                               ShaderData *bssrdf_sd = kernel_split_sd(sd, ray_index);
                                *bssrdf_sd = *sd; /* note: copy happens each iteration of inner loop, this is
                                                   * important as the indirect path will write into bssrdf_sd */
 
-                               SubsurfaceIntersection ss_isect_private = *ss_isect;
+                               LocalIntersection ss_isect_private = *ss_isect;
                                subsurface_scatter_multi_setup(kg,
                                                               &ss_isect_private,
                                                               hit,
@@ -152,7 +149,6 @@ ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_it
                                                int all = (kernel_data.integrator.sample_all_lights_direct) ||
                                                              (branched_state->path_state.flag & PATH_RAY_SHADOW_CATCHER);
                                                kernel_branched_path_surface_connect_light(kg,
-                                                                                          &rng,
                                                                                           bssrdf_sd,
                                                                                           emission_sd,
                                                                                           hit_state,
@@ -169,6 +165,7 @@ ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_it
                                                                                          ray_index,
                                                                                          num_samples_inv,
                                                                                          bssrdf_sd,
+                                                                                         false,
                                                                                          false))
                                {
                                        branched_state->ss_next_closure = i;
@@ -187,6 +184,13 @@ ccl_device_noinline bool kernel_split_branched_path_subsurface_indirect_light_it
                branched_state->ss_next_sample = 0;
        }
 
+       branched_state->ss_next_closure = sd->num_closure;
+
+       branched_state->waiting_on_shared_samples = (branched_state->shared_sample_count > 0);
+       if(branched_state->waiting_on_shared_samples) {
+               return true;
+       }
+
        kernel_split_branched_path_indirect_loop_end(kg, ray_index);
 
        return false;
@@ -221,12 +225,11 @@ ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
        if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
                ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
                PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
-               RNG rng = kernel_split_state.rng[ray_index];
                ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
                ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
                ccl_global SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
-               ShaderData *sd = &kernel_split_state.sd[ray_index];
-               ShaderData *emission_sd = &kernel_split_state.sd_DL_shadow[ray_index];
+               ShaderData *sd = kernel_split_sd(sd, ray_index);
+               ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
 
                if(sd->flag & SD_BSSRDF) {
 
@@ -238,7 +241,6 @@ ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
                                                                  emission_sd,
                                                                  L,
                                                                  state,
-                                                                 &rng,
                                                                  ray,
                                                                  throughput,
                                                                  ss_indirect))
@@ -248,30 +250,25 @@ ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
 #ifdef __BRANCHED_PATH__
                        }
                        else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
-                               float bssrdf_probability;
-                               ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
+                               float bssrdf_u, bssrdf_v;
+                               path_state_rng_2D(kg,
+                                                 state,
+                                                 PRNG_BSDF_U,
+                                                 &bssrdf_u, &bssrdf_v);
 
-                               /* modify throughput for picking bssrdf or bsdf */
-                               *throughput *= bssrdf_probability;
+                               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(&rng, state->rng_offset, state->sample, 0x68bc21eb);
-
-                                       float bssrdf_u, bssrdf_v;
-                                       path_state_rng_2D(kg,
-                                                             &rng,
-                                                             state,
-                                                             PRNG_BSDF_U,
-                                                             &bssrdf_u, &bssrdf_v);
+                                       uint lcg_state = lcg_state_init_addrspace(state, 0x68bc21eb);
                                        subsurface_scatter_step(kg,
-                                                                   sd,
-                                                                   state,
-                                                                   state->flag,
-                                                                   sc,
-                                                                   &lcg_state,
-                                                                   bssrdf_u, bssrdf_v,
-                                                                   false);
+                                                               sd,
+                                                               state,
+                                                               state->flag,
+                                                               sc,
+                                                               &lcg_state,
+                                                               bssrdf_u, bssrdf_v,
+                                                               false);
                                }
                        }
                        else {
@@ -283,7 +280,6 @@ ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
                        }
 #endif
                }
-               kernel_split_state.rng[ray_index] = rng;
        }
 
 #  ifdef __BRANCHED_PATH__