Fix T54317: overlapping volume render bug after recent changes.
[blender-staging.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_transparent_bounce(KernelGlobals *kg, int ray_index)
57 {
58         ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
59         ShaderData *sd = kernel_split_sd(sd, ray_index);
60         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
61         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
62
63 #  ifdef __VOLUME__
64         if(!(sd->flag & SD_HAS_ONLY_VOLUME)) {
65 #  endif
66                 /* continue in case of transparency */
67                 *throughput *= shader_bsdf_transparency(kg, sd);
68
69                 if(is_zero(*throughput)) {
70                         kernel_split_path_end(kg, ray_index);
71                         return;
72                 }
73
74                 /* Update Path State */
75                 path_state_next(kg, state, LABEL_TRANSPARENT);
76 #  ifdef __VOLUME__
77         }
78         else {
79                 if(!path_state_volume_next(kg, state)) {
80                         kernel_split_path_end(kg, ray_index);
81                         return;
82                 }
83         }
84 #  endif
85
86         ray->P = ray_offset(sd->P, -sd->Ng);
87         ray->t -= sd->ray_length; /* clipping works through transparent */
88
89 #  ifdef __RAY_DIFFERENTIALS__
90         ray->dP = sd->dP;
91         ray->dD.dx = -sd->dI.dx;
92         ray->dD.dy = -sd->dI.dy;
93 #  endif  /* __RAY_DIFFERENTIALS__ */
94
95 #  ifdef __VOLUME__
96         /* enter/exit volume */
97         kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
98 #  endif  /* __VOLUME__ */
99 }
100 #endif  /* __BRANCHED_PATH__ */
101
102 ccl_device void kernel_next_iteration_setup(KernelGlobals *kg,
103                                             ccl_local_param unsigned int *local_queue_atomics)
104 {
105         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
106                 *local_queue_atomics = 0;
107         }
108         ccl_barrier(CCL_LOCAL_MEM_FENCE);
109
110         if(ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
111                 /* If we are here, then it means that scene-intersect kernel
112                 * has already been executed atleast once. From the next time,
113                 * scene-intersect kernel may operate on queues to fetch ray index
114                 */
115                 *kernel_split_params.use_queues_flag = 1;
116
117                 /* Mark queue indices of QUEUE_SHADOW_RAY_CAST_AO_RAYS and
118                  * QUEUE_SHADOW_RAY_CAST_DL_RAYS queues that were made empty during the
119                  * previous kernel.
120                  */
121                 kernel_split_params.queue_index[QUEUE_SHADOW_RAY_CAST_AO_RAYS] = 0;
122                 kernel_split_params.queue_index[QUEUE_SHADOW_RAY_CAST_DL_RAYS] = 0;
123         }
124
125         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
126         ray_index = get_ray_index(kg, ray_index,
127                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
128                                   kernel_split_state.queue_data,
129                                   kernel_split_params.queue_size,
130                                   0);
131
132         ccl_global char *ray_state = kernel_split_state.ray_state;
133
134 #  ifdef __VOLUME__
135         /* Reactivate only volume rays here, most surface work was skipped. */
136         if(IS_STATE(ray_state, ray_index, RAY_HAS_ONLY_VOLUME)) {
137                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_ACTIVE);
138         }
139 #  endif
140
141         bool active = IS_STATE(ray_state, ray_index, RAY_ACTIVE);
142         if(active) {
143                 ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
144                 ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
145                 ShaderData *sd = kernel_split_sd(sd, ray_index);
146                 ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
147                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
148
149 #ifdef __BRANCHED_PATH__
150                 if(!kernel_data.integrator.branched || IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
151 #endif
152                         /* Compute direct lighting and next bounce. */
153                         if(!kernel_path_surface_bounce(kg, sd, throughput, state, &L->state, ray)) {
154                                 kernel_split_path_end(kg, ray_index);
155                         }
156 #ifdef __BRANCHED_PATH__
157                 }
158                 else if(sd->flag & SD_HAS_ONLY_VOLUME) {
159                         kernel_split_branched_transparent_bounce(kg, ray_index);
160                 }
161                 else {
162                         kernel_split_branched_indirect_light_init(kg, ray_index);
163
164                         if(kernel_split_branched_path_surface_indirect_light_iter(kg,
165                                                                                   ray_index,
166                                                                                   1.0f,
167                                                                                   kernel_split_sd(branched_state_sd, ray_index),
168                                                                                   true,
169                                                                                   true))
170                         {
171                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
172                         }
173                         else {
174                                 kernel_split_branched_path_indirect_loop_end(kg, ray_index);
175                                 kernel_split_branched_transparent_bounce(kg, ray_index);
176                         }
177                 }
178 #endif  /* __BRANCHED_PATH__ */
179         }
180
181         /* Enqueue RAY_UPDATE_BUFFER rays. */
182         enqueue_ray_index_local(ray_index,
183                                 QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
184                                 IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER) && active,
185                                 kernel_split_params.queue_size,
186                                 local_queue_atomics,
187                                 kernel_split_state.queue_data,
188                                 kernel_split_params.queue_index);
189
190 #ifdef __BRANCHED_PATH__
191         /* iter loop */
192         if(ccl_global_id(0) == 0 && ccl_global_id(1) == 0) {
193                 kernel_split_params.queue_index[QUEUE_LIGHT_INDIRECT_ITER] = 0;
194         }
195
196         ray_index = get_ray_index(kg, ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0),
197                                   QUEUE_LIGHT_INDIRECT_ITER,
198                                   kernel_split_state.queue_data,
199                                   kernel_split_params.queue_size,
200                                   1);
201
202         if(IS_STATE(ray_state, ray_index, RAY_LIGHT_INDIRECT_NEXT_ITER)) {
203                 /* for render passes, sum and reset indirect light pass variables
204                  * for the next samples */
205                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
206
207                 path_radiance_sum_indirect(L);
208                 path_radiance_reset_indirect(L);
209
210                 if(kernel_split_branched_path_surface_indirect_light_iter(kg,
211                                                                           ray_index,
212                                                                           1.0f,
213                                                                           kernel_split_sd(branched_state_sd, ray_index),
214                                                                           true,
215                                                                           true))
216                 {
217                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
218                 }
219                 else {
220                         kernel_split_branched_path_indirect_loop_end(kg, ray_index);
221                         kernel_split_branched_transparent_bounce(kg, ray_index);
222                 }
223         }
224
225 #  ifdef __VOLUME__
226         /* Enqueue RAY_VOLUME_INDIRECT_NEXT_ITER rays */
227         ccl_barrier(CCL_LOCAL_MEM_FENCE);
228         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
229                 *local_queue_atomics = 0;
230         }
231         ccl_barrier(CCL_LOCAL_MEM_FENCE);
232
233         ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
234         enqueue_ray_index_local(ray_index,
235                                 QUEUE_VOLUME_INDIRECT_ITER,
236                                 IS_STATE(kernel_split_state.ray_state, ray_index, RAY_VOLUME_INDIRECT_NEXT_ITER),
237                                 kernel_split_params.queue_size,
238                                 local_queue_atomics,
239                                 kernel_split_state.queue_data,
240                                 kernel_split_params.queue_index);
241
242 #  endif  /* __VOLUME__ */
243
244 #  ifdef __SUBSURFACE__
245         /* Enqueue RAY_SUBSURFACE_INDIRECT_NEXT_ITER rays */
246         ccl_barrier(CCL_LOCAL_MEM_FENCE);
247         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
248                 *local_queue_atomics = 0;
249         }
250         ccl_barrier(CCL_LOCAL_MEM_FENCE);
251
252         ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
253         enqueue_ray_index_local(ray_index,
254                                 QUEUE_SUBSURFACE_INDIRECT_ITER,
255                                 IS_STATE(kernel_split_state.ray_state, ray_index, RAY_SUBSURFACE_INDIRECT_NEXT_ITER),
256                                 kernel_split_params.queue_size,
257                                 local_queue_atomics,
258                                 kernel_split_state.queue_data,
259                                 kernel_split_params.queue_index);
260 #  endif  /* __SUBSURFACE__ */
261 #endif  /* __BRANCHED_PATH__ */
262 }
263
264 CCL_NAMESPACE_END