Code cleanup: store branch factor in PathState.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Sun, 10 Sep 2017 12:09:12 +0000 (14:09 +0200)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Wed, 13 Sep 2017 13:24:14 +0000 (15:24 +0200)
intern/cycles/kernel/kernel_bake.h
intern/cycles/kernel/kernel_path.h
intern/cycles/kernel/kernel_path_branched.h
intern/cycles/kernel/kernel_path_state.h
intern/cycles/kernel/kernel_random.h
intern/cycles/kernel/kernel_types.h
intern/cycles/kernel/split/kernel_branched.h
intern/cycles/kernel/split/kernel_do_volume.h
intern/cycles/kernel/split/kernel_holdout_emission_blurring_pathtermination_ao.h
intern/cycles/kernel/split/kernel_split_data_types.h

index ef6f1969941fb9ca88e04418a1aa75486b1f0cef..b05f6e9ed5ebbd7ef2ebaf53c8692c1056759688 100644 (file)
@@ -102,7 +102,6 @@ ccl_device_inline void compute_light_pass(KernelGlobals *kg,
                                                             &emission_sd,
                                                             &ray,
                                                             throughput,
-                                                            state.num_samples,
                                                             &state,
                                                             &L_sample);
                                        kernel_path_subsurface_accum_indirect(&ss_indirect, &L_sample);
@@ -121,7 +120,7 @@ ccl_device_inline void compute_light_pass(KernelGlobals *kg,
                                state.ray_t = 0.0f;
 #endif
                                /* compute indirect light */
-                               kernel_path_indirect(kg, &indirect_sd, &emission_sd, &ray, throughput, 1, &state, &L_sample);
+                               kernel_path_indirect(kg, &indirect_sd, &emission_sd, &ray, throughput, &state, &L_sample);
 
                                /* sum and reset indirect light pass variables for the next samples */
                                path_radiance_sum_indirect(&L_sample);
index 6ad2a00272be448630f8062735ada10ac7ec3a9d..fc157feb28c5d2c43cec9ae608bcafc33267b646 100644 (file)
@@ -388,7 +388,6 @@ ccl_device void kernel_path_indirect(KernelGlobals *kg,
                                      ShaderData *emission_sd,
                                      Ray *ray,
                                      float3 throughput,
-                                     int num_samples,
                                      PathState *state,
                                      PathRadiance *L)
 {
@@ -455,10 +454,7 @@ ccl_device void kernel_path_indirect(KernelGlobals *kg,
                /* path termination. this is a strange place to put the termination, it's
                 * mainly due to the mixed in MIS that we use. gives too many unneeded
                 * shader evaluations, only need emission if we are going to terminate */
-               float probability =
-                       path_state_continuation_probability(kg,
-                                                        state,
-                                                        throughput*num_samples);
+               float probability = path_state_continuation_probability(kg, state, throughput);
 
                if(probability == 0.0f) {
                        break;
index b908cbbe97027417be5c10b977f0a86cb729518e..3994d8d49546b949a60149f8c32429a272ae3bb2 100644 (file)
@@ -142,7 +142,6 @@ ccl_device_noinline void kernel_branched_path_surface_indirect_light(KernelGloba
                                             emission_sd,
                                             &bsdf_ray,
                                             tp*num_samples_inv,
-                                            num_samples,
                                             &ps,
                                             L);
 
@@ -359,7 +358,6 @@ ccl_device void kernel_branched_path_integrate(KernelGlobals *kg,
                                                                     &emission_sd,
                                                                     &pray,
                                                                     tp*num_samples_inv,
-                                                                    num_samples,
                                                                     &ps,
                                                                     L);
 
@@ -415,7 +413,6 @@ ccl_device void kernel_branched_path_integrate(KernelGlobals *kg,
                                                                     &emission_sd,
                                                                     &pray,
                                                                     tp,
-                                                                    num_samples,
                                                                     &ps,
                                                                     L);
 
index bedcd655bc6c1643f69ac010f047ca93c3715ab5..bb09b4ac08036e4dc2d73dadc84f1b5cb18ed67c 100644 (file)
@@ -29,6 +29,7 @@ ccl_device_inline void path_state_init(KernelGlobals *kg,
        state->rng_offset = PRNG_BASE_NUM;
        state->sample = sample;
        state->num_samples = kernel_data.integrator.aa_samples;
+       state->branch_factor = 1.0f;
 
        state->bounce = 0;
        state->diffuse_bounce = 0;
@@ -157,7 +158,9 @@ ccl_device_inline uint path_state_ray_visibility(KernelGlobals *kg, ccl_addr_spa
        return flag;
 }
 
-ccl_device_inline float path_state_continuation_probability(KernelGlobals *kg, ccl_addr_space PathState *state, const float3 throughput)
+ccl_device_inline float path_state_continuation_probability(KernelGlobals *kg,
+                                                            ccl_addr_space PathState *state,
+                                                            const float3 throughput)
 {
        if(state->flag & PATH_RAY_TRANSPARENT) {
                /* Transparent rays are treated separately with own max bounces. */
@@ -201,7 +204,7 @@ ccl_device_inline float path_state_continuation_probability(KernelGlobals *kg, c
 
        /* Probalistic termination: use sqrt() to roughly match typical view
         * transform and do path termination a bit later on average. */
-       return min(sqrtf(max3(fabs(throughput))), 1.0f);
+       return min(sqrtf(max3(fabs(throughput)) * state->branch_factor), 1.0f);
 }
 
 /* TODO(DingTo): Find more meaningful name for this */
@@ -224,5 +227,20 @@ ccl_device_inline bool path_state_ao_bounce(KernelGlobals *kg, ccl_addr_space Pa
     return (bounce > kernel_data.integrator.ao_bounces);
 }
 
+ccl_device_inline void path_state_branch(ccl_addr_space PathState *state,
+                                         int branch,
+                                         int num_branches)
+{
+       state->rng_offset += PRNG_BOUNCE_NUM;
+
+       if(num_branches > 1) {
+               /* Path is splitting into a branch, adjust so that each branch
+                * still gets a unique sample from the same sequence. */
+               state->sample = state->sample*num_branches + branch;
+               state->num_samples = state->num_samples*num_branches;
+               state->branch_factor *= num_branches;
+       }
+}
+
 CCL_NAMESPACE_END
 
index 221d92f5de1b897d6265b5be129226c20c387917..b35ed3bd27965168c789cc794b9833715c83db5a 100644 (file)
@@ -296,17 +296,6 @@ ccl_device_inline float path_branched_rng_light_termination(
        return 0.0f;
 }
 
-ccl_device_inline void path_state_branch(ccl_addr_space PathState *state,
-                                         int branch,
-                                         int num_branches)
-{
-       /* path is splitting into a branch, adjust so that each branch
-        * still gets a unique sample from the same sequence */
-       state->rng_offset += PRNG_BOUNCE_NUM;
-       state->sample = state->sample*num_branches + branch;
-       state->num_samples = state->num_samples*num_branches;
-}
-
 ccl_device_inline uint lcg_state_init(PathState *state,
                                       uint scramble)
 {
index e3d2ae15f5cef096f8c9bdb4062a03da86b0ca76..1b4e926ca2872b1695d52fa378376246324d06f0 100644 (file)
@@ -1008,9 +1008,10 @@ typedef struct PathState {
 
        /* random number generator state */
        uint rng_hash;          /* per pixel hash */
-       int rng_offset;                 /* dimension offset */
-       int sample;                     /* path sample number */
-       int num_samples;                /* total number of times this path will be sampled */
+       int rng_offset;         /* dimension offset */
+       int sample;             /* path sample number */
+       int num_samples;        /* total number of times this path will be sampled */
+       float branch_factor;    /* number of branches in indirect paths */
 
        /* bounce counting */
        int bounce;
index 9fe4ec18e9eb9d533419b9fccf8158f17e0e1504..2c390593ba1458844745167f48c3bc599589076f 100644 (file)
@@ -188,7 +188,6 @@ ccl_device_noinline bool kernel_split_branched_path_surface_indirect_light_iter(
                        /* update state for next iteration */
                        branched_state->next_closure = i;
                        branched_state->next_sample = j+1;
-                       branched_state->num_samples = num_samples;
 
                        /* start the indirect path */
                        *tp *= num_samples_inv;
index 478d83d633e3b9babdde5daa1d5dfaf2165f6328..2975aa200045de3ee30ea048151b0e276705947d 100644 (file)
@@ -72,7 +72,6 @@ ccl_device_noinline bool kernel_split_branched_path_volume_indirect_light_iter(K
                        /* start the indirect path */
                        branched_state->next_closure = 0;
                        branched_state->next_sample = j+1;
-                       branched_state->num_samples = num_samples;
 
                        /* Attempting to share too many samples is slow for volumes as it causes us to
                         * loop here more and have many calls to kernel_volume_integrate which evaluates
index 733c25f5aa44479f94d98dc9721fb038525dedc8..9036b1e473d5ccc1897a1a07ab5575244dbad8e8 100644 (file)
@@ -134,38 +134,22 @@ ccl_device void kernel_holdout_emission_blurring_pathtermination_ao(
                 * mainly due to the mixed in MIS that we use. gives too many unneeded
                 * shader evaluations, only need emission if we are going to terminate.
                 */
-#ifndef __BRANCHED_PATH__
                float probability = path_state_continuation_probability(kg, state, throughput);
-#else
-               float probability = 1.0f;
-
-               if(!kernel_data.integrator.branched) {
-                       probability = path_state_continuation_probability(kg, state, throughput);
-               }
-               else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
-                       int num_samples = kernel_split_state.branched_state[ray_index].num_samples;
-                       probability = path_state_continuation_probability(kg, state, throughput*num_samples);
-               }
-               else if(state->flag & PATH_RAY_TRANSPARENT) {
-                       probability = path_state_continuation_probability(kg, state, throughput);
-               }
-#endif
 
                if(probability == 0.0f) {
                        kernel_split_path_end(kg, ray_index);
                }
-
-               if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
-                       if(probability != 1.0f) {
-                               float terminate = path_state_rng_1D_for_decision(kg, state, PRNG_TERMINATE);
-                               if(terminate >= probability) {
-                                       kernel_split_path_end(kg, ray_index);
-                               }
-                               else {
-                                       kernel_split_state.throughput[ray_index] = throughput/probability;
-                               }
+               else if(probability < 1.0f) {
+                       float terminate = path_state_rng_1D_for_decision(kg, state, PRNG_TERMINATE);
+                       if(terminate >= probability) {
+                               kernel_split_path_end(kg, ray_index);
+                       }
+                       else {
+                               kernel_split_state.throughput[ray_index] = throughput/probability;
                        }
+               }
 
+               if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
                        PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
                        kernel_update_denoising_features(kg, sd, state, L);
                }
index 3eae884d47955b4d56288febe102cf481b450394..e08afc22b20cf8320bea8f84866c8b763f7350f4 100644 (file)
@@ -72,7 +72,6 @@ typedef ccl_global struct SplitBranchedState {
        /* indirect loop state */
        int next_closure;
        int next_sample;
-       int num_samples;
 
 #ifdef __SUBSURFACE__
        int ss_next_closure;