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