81024f0cf99d2668531030180abc12f067071455
[blender.git] / intern / cycles / kernel / split / kernel_next_iteration_setup.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 CCL_NAMESPACE_BEGIN
18
19 /*This kernel takes care of setting up ray for the next iteration of
20  * path-iteration and accumulating radiance corresponding to AO and
21  * direct-lighting
22  *
23  * Ray state of rays that are terminated in this kernel are changed
24  * to RAY_UPDATE_BUFFER.
25  *
26  * Note on queues:
27  * This kernel fetches rays from the queue QUEUE_ACTIVE_AND_REGENERATED_RAYS
28  * and processes only the rays of state RAY_ACTIVE.
29  * There are different points in this kernel where a ray may terminate and
30  * reach RAY_UPDATE_BUFF state. These rays are enqueued into
31  * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue. These rays will still be present
32  * in QUEUE_ACTIVE_AND_REGENERATED_RAYS queue, but since their ray-state has
33  * been changed to RAY_UPDATE_BUFF, there is no problem.
34  *
35  * State of queues when this kernel is called:
36  * At entry,
37  *   - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE,
38  *     RAY_REGENERATED, RAY_UPDATE_BUFFER rays.
39  *   - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
40  *     RAY_TO_REGENERATE and RAY_UPDATE_BUFFER rays.
41  * At exit,
42  *   - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE,
43  *     RAY_REGENERATED and more RAY_UPDATE_BUFFER rays.
44  *   - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
45  *     RAY_TO_REGENERATE and more RAY_UPDATE_BUFFER rays.
46  */
47
48 #ifdef __BRANCHED_PATH__
49 ccl_device_inline void kernel_split_branched_indirect_light_init(KernelGlobals *kg, int ray_index)
50 {
51         kernel_split_branched_path_indirect_loop_init(kg, ray_index);
52
53         ADD_RAY_FLAG(kernel_split_state.ray_state, ray_index, RAY_BRANCHED_LIGHT_INDIRECT);
54 }
55
56 ccl_device void kernel_split_branched_indirect_light_end(KernelGlobals *kg, int ray_index)
57 {
58         kernel_split_branched_path_indirect_loop_end(kg, ray_index);
59
60         ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
61         ShaderData *sd = kernel_split_sd(sd, ray_index);
62         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
63         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
64
65         /* continue in case of transparency */
66         *throughput *= shader_bsdf_transparency(kg, sd);
67
68         if(is_zero(*throughput)) {
69                 kernel_split_path_end(kg, ray_index);
70         }
71         else {
72                 /* Update Path State */
73                 path_state_next(kg, state, LABEL_TRANSPARENT);
74
75                 ray->P = ray_offset(sd->P, -sd->Ng);
76                 ray->t -= sd->ray_length; /* clipping works through transparent */
77
78 #  ifdef __RAY_DIFFERENTIALS__
79                 ray->dP = sd->dP;
80                 ray->dD.dx = -sd->dI.dx;
81                 ray->dD.dy = -sd->dI.dy;
82 #  endif  /* __RAY_DIFFERENTIALS__ */
83
84 #  ifdef __VOLUME__
85                 /* enter/exit volume */
86                 kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
87 #  endif  /* __VOLUME__ */
88         }
89 }
90 #endif  /* __BRANCHED_PATH__ */
91
92 ccl_device void kernel_next_iteration_setup(KernelGlobals *kg,
93                                             ccl_local_param unsigned int *local_queue_atomics)
94 {
95         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
96                 *local_queue_atomics = 0;
97         }
98         ccl_barrier(CCL_LOCAL_MEM_FENCE);
99
100         if(ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
101                 /* If we are here, then it means that scene-intersect kernel
102                 * has already been executed atleast once. From the next time,
103                 * scene-intersect kernel may operate on queues to fetch ray index
104                 */
105                 *kernel_split_params.use_queues_flag = 1;
106
107                 /* Mark queue indices of QUEUE_SHADOW_RAY_CAST_AO_RAYS and
108                  * QUEUE_SHADOW_RAY_CAST_DL_RAYS queues that were made empty during the
109                  * previous kernel.
110                  */
111                 kernel_split_params.queue_index[QUEUE_SHADOW_RAY_CAST_AO_RAYS] = 0;
112                 kernel_split_params.queue_index[QUEUE_SHADOW_RAY_CAST_DL_RAYS] = 0;
113         }
114
115         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
116         ray_index = get_ray_index(kg, ray_index,
117                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
118                                   kernel_split_state.queue_data,
119                                   kernel_split_params.queue_size,
120                                   0);
121
122         ccl_global char *ray_state = kernel_split_state.ray_state;
123
124         bool active = IS_STATE(ray_state, ray_index, RAY_ACTIVE);
125         if(active) {
126                 ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
127                 ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
128                 ShaderData *sd = kernel_split_sd(sd, ray_index);
129                 ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
130                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
131
132 #ifdef __BRANCHED_PATH__
133                 if(!kernel_data.integrator.branched || IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
134 #endif
135                         /* Compute direct lighting and next bounce. */
136                         if(!kernel_path_surface_bounce(kg, sd, throughput, state, &L->state, ray)) {
137                                 kernel_split_path_end(kg, ray_index);
138                         }
139 #ifdef __BRANCHED_PATH__
140                 }
141                 else {
142                         kernel_split_branched_indirect_light_init(kg, ray_index);
143
144                         if(kernel_split_branched_path_surface_indirect_light_iter(kg,
145                                                                                   ray_index,
146                                                                                   1.0f,
147                                                                                   kernel_split_sd(branched_state_sd, ray_index),
148                                                                                   true,
149                                                                                   true))
150                         {
151                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
152                         }
153                         else {
154                                 kernel_split_branched_indirect_light_end(kg, ray_index);
155                         }
156                 }
157 #endif  /* __BRANCHED_PATH__ */
158         }
159
160         /* Enqueue RAY_UPDATE_BUFFER rays. */
161         enqueue_ray_index_local(ray_index,
162                                 QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
163                                 IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER) && active,
164                                 kernel_split_params.queue_size,
165                                 local_queue_atomics,
166                                 kernel_split_state.queue_data,
167                                 kernel_split_params.queue_index);
168
169 #ifdef __BRANCHED_PATH__
170         /* iter loop */
171         if(ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
172                 kernel_split_params.queue_index[QUEUE_LIGHT_INDIRECT_ITER] = 0;
173         }
174
175         ray_index = get_ray_index(kg, ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0),
176                                   QUEUE_LIGHT_INDIRECT_ITER,
177                                   kernel_split_state.queue_data,
178                                   kernel_split_params.queue_size,
179                                   1);
180
181         if(IS_STATE(ray_state, ray_index, RAY_LIGHT_INDIRECT_NEXT_ITER)) {
182                 /* for render passes, sum and reset indirect light pass variables
183                  * for the next samples */
184                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
185
186                 path_radiance_sum_indirect(L);
187                 path_radiance_reset_indirect(L);
188
189                 if(kernel_split_branched_path_surface_indirect_light_iter(kg,
190                                                                           ray_index,
191                                                                           1.0f,
192                                                                           kernel_split_sd(branched_state_sd, ray_index),
193                                                                           true,
194                                                                           true))
195                 {
196                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
197                 }
198                 else {
199                         kernel_split_branched_indirect_light_end(kg, ray_index);
200                 }
201         }
202
203 #  ifdef __VOLUME__
204         /* Enqueue RAY_VOLUME_INDIRECT_NEXT_ITER rays */
205         ccl_barrier(CCL_LOCAL_MEM_FENCE);
206         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
207                 *local_queue_atomics = 0;
208         }
209         ccl_barrier(CCL_LOCAL_MEM_FENCE);
210
211         ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
212         enqueue_ray_index_local(ray_index,
213                                 QUEUE_VOLUME_INDIRECT_ITER,
214                                 IS_STATE(kernel_split_state.ray_state, ray_index, RAY_VOLUME_INDIRECT_NEXT_ITER),
215                                 kernel_split_params.queue_size,
216                                 local_queue_atomics,
217                                 kernel_split_state.queue_data,
218                                 kernel_split_params.queue_index);
219
220 #  endif  /* __VOLUME__ */
221
222 #  ifdef __SUBSURFACE__
223         /* Enqueue RAY_SUBSURFACE_INDIRECT_NEXT_ITER rays */
224         ccl_barrier(CCL_LOCAL_MEM_FENCE);
225         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
226                 *local_queue_atomics = 0;
227         }
228         ccl_barrier(CCL_LOCAL_MEM_FENCE);
229
230         ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
231         enqueue_ray_index_local(ray_index,
232                                 QUEUE_SUBSURFACE_INDIRECT_ITER,
233                                 IS_STATE(kernel_split_state.ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER),
234                                 kernel_split_params.queue_size,
235                                 local_queue_atomics,
236                                 kernel_split_state.queue_data,
237                                 kernel_split_params.queue_index);
238 #  endif  /* __SUBSURFACE__ */
239 #endif  /* __BRANCHED_PATH__ */
240 }
241
242 CCL_NAMESPACE_END