Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / kernel / split / kernel_subsurface_scatter.h
1 /*
2  * Copyright 2011-2017 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 CCL_NAMESPACE_BEGIN
18
19
20 ccl_device void kernel_subsurface_scatter(KernelGlobals *kg)
21 {
22 #ifdef __SUBSURFACE__
23
24         ccl_local unsigned int local_queue_atomics;
25         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
26                 local_queue_atomics = 0;
27         }
28         ccl_barrier(CCL_LOCAL_MEM_FENCE);
29
30         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
31         ray_index = get_ray_index(kg, ray_index,
32                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
33                                   kernel_split_state.queue_data,
34                                   kernel_split_params.queue_size,
35                                   0);
36
37 #ifdef __COMPUTE_DEVICE_GPU__
38         /* If we are executing on a GPU device, we exit all threads that are not
39          * required.
40          *
41          * If we are executing on a CPU device, then we need to keep all threads
42          * active since we have barrier() calls later in the kernel. CPU devices,
43          * expect all threads to execute barrier statement.
44          */
45         if(ray_index == QUEUE_EMPTY_SLOT) {
46                 return;
47         }
48 #endif
49
50 #ifndef __COMPUTE_DEVICE_GPU__
51         if(ray_index != QUEUE_EMPTY_SLOT) {
52 #endif
53
54
55         char enqueue_flag = 0;
56         ccl_global char *ray_state = kernel_split_state.ray_state;
57         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
58         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
59         ccl_global RNG *rng = &kernel_split_state.rng[ray_index];
60         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
61         ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
62         ccl_global SubsurfaceIndirectRays *ss_indirect = &kernel_split_state.ss_rays[ray_index];
63         ShaderData *sd = &kernel_split_state.sd[ray_index];
64         ShaderData *emission_sd = &kernel_split_state.sd_DL_shadow[ray_index];
65
66         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
67                 if(sd->flag & SD_BSSRDF) {
68                         if(kernel_path_subsurface_scatter(kg,
69                                                           sd,
70                                                           emission_sd,
71                                                           L,
72                                                           state,
73                                                           rng,
74                                                           ray,
75                                                           throughput,
76                                                           ss_indirect)) {
77                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
78                                 enqueue_flag = 1;
79                         }
80                 }
81         }
82
83 #ifndef __COMPUTE_DEVICE_GPU__
84         }
85 #endif
86
87         /* Enqueue RAY_UPDATE_BUFFER rays. */
88         enqueue_ray_index_local(ray_index,
89                                 QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
90                                 enqueue_flag,
91                                 kernel_split_params.queue_size,
92                                 &local_queue_atomics,
93                                 kernel_split_state.queue_data,
94                                 kernel_split_params.queue_index);
95
96 #endif  /* __SUBSURFACE__ */
97
98 }
99
100 CCL_NAMESPACE_END