Fix T54105: random walk SSS missing in branched indirect paths.
[blender-staging.git] / intern / cycles / kernel / split / kernel_split_common.h
1 /*
2  * Copyright 2011-2015 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef  __KERNEL_SPLIT_H__
18 #define  __KERNEL_SPLIT_H__
19
20 #include "kernel/kernel_math.h"
21 #include "kernel/kernel_types.h"
22
23 #include "kernel/split/kernel_split_data.h"
24
25 #include "kernel/kernel_globals.h"
26
27 #ifdef __OSL__
28 #  include "kernel/osl/osl_shader.h"
29 #endif
30
31 #ifdef __KERNEL_OPENCL__
32 #  include "kernel/kernels/opencl/kernel_opencl_image.h"
33 #endif
34 #ifdef __KERNEL_CUDA__
35 #  include "kernel/kernels/cuda/kernel_cuda_image.h"
36 #endif
37 #ifdef __KERNEL_CPU__
38 #  include "kernel/kernels/cpu/kernel_cpu_image.h"
39 #endif
40
41 #include "util/util_atomic.h"
42
43 #include "kernel/kernel_path.h"
44 #ifdef __BRANCHED_PATH__
45 #  include "kernel/kernel_path_branched.h"
46 #endif
47
48 #include "kernel/kernel_queues.h"
49 #include "kernel/kernel_work_stealing.h"
50
51 #ifdef __BRANCHED_PATH__
52 #  include "kernel/split/kernel_branched.h"
53 #endif
54
55 CCL_NAMESPACE_BEGIN
56
57 ccl_device_inline void kernel_split_path_end(KernelGlobals *kg, int ray_index)
58 {
59         ccl_global char *ray_state = kernel_split_state.ray_state;
60
61 #ifdef __BRANCHED_PATH__
62         ccl_addr_space SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
63
64         if(ss_indirect->num_rays) {
65                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
66         }
67         else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT_SHARED)) {
68                 int orig_ray = kernel_split_state.branched_state[ray_index].original_ray;
69
70                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
71                 PathRadiance *orig_ray_L = &kernel_split_state.path_radiance[orig_ray];
72
73                 path_radiance_sum_indirect(L);
74                 path_radiance_accum_sample(orig_ray_L, L);
75
76                 atomic_fetch_and_dec_uint32((ccl_global uint*)&kernel_split_state.branched_state[orig_ray].shared_sample_count);
77
78                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_INACTIVE);
79         }
80         else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_LIGHT_INDIRECT)) {
81                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_LIGHT_INDIRECT_NEXT_ITER);
82         }
83         else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_VOLUME_INDIRECT)) {
84                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_VOLUME_INDIRECT_NEXT_ITER);
85         }
86         else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_SUBSURFACE_INDIRECT)) {
87                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER);
88         }
89         else {
90                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
91         }
92 #else
93         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
94 #endif
95 }
96
97 CCL_NAMESPACE_END
98
99 #endif  /* __KERNEL_SPLIT_H__ */