Code cleanup: deduplicate some branched and split kernel code.
[blender-staging.git] / intern / cycles / kernel / split / kernel_direct_lighting.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 direct lighting logic.
20  * However, the "shadow ray cast" part of direct lighting is handled
21  * in the next kernel.
22  *
23  * This kernels determines the rays for which a shadow_blocked() function
24  * associated with direct lighting should be executed. Those rays for which
25  * a shadow_blocked() function for direct-lighting must be executed, are
26  * marked with flag RAY_SHADOW_RAY_CAST_DL and enqueued into the queue
27  * QUEUE_SHADOW_RAY_CAST_DL_RAYS
28  *
29  * Note on Queues:
30  * This kernel only reads from the QUEUE_ACTIVE_AND_REGENERATED_RAYS queue
31  * and processes only the rays of state RAY_ACTIVE; If a ray needs to execute
32  * the corresponding shadow_blocked part, after direct lighting, the ray is
33  * marked with RAY_SHADOW_RAY_CAST_DL flag.
34  *
35  * State of queues when this kernel is called:
36  * - State of queues QUEUE_ACTIVE_AND_REGENERATED_RAYS and
37  *   QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be same before and after this
38  *   kernel call.
39  * - QUEUE_SHADOW_RAY_CAST_DL_RAYS queue will be filled with rays for which a
40  *   shadow_blocked function must be executed, after this kernel call
41  *    Before this kernel call the QUEUE_SHADOW_RAY_CAST_DL_RAYS will be empty.
42  */
43 ccl_device void kernel_direct_lighting(KernelGlobals *kg,
44                                        ccl_local_param unsigned int *local_queue_atomics)
45 {
46         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
47                 *local_queue_atomics = 0;
48         }
49         ccl_barrier(CCL_LOCAL_MEM_FENCE);
50
51         char enqueue_flag = 0;
52         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
53         ray_index = get_ray_index(kg, ray_index,
54                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
55                                   kernel_split_state.queue_data,
56                                   kernel_split_params.queue_size,
57                                   0);
58
59         if(IS_STATE(kernel_split_state.ray_state, ray_index, RAY_ACTIVE)) {
60                 ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
61                 ShaderData *sd = &kernel_split_state.sd[ray_index];
62
63                 /* direct lighting */
64 #ifdef __EMISSION__
65                 bool flag = (kernel_data.integrator.use_direct_light &&
66                              (sd->flag & SD_BSDF_HAS_EVAL));
67
68 #  ifdef __BRANCHED_PATH__
69                 if(flag && kernel_data.integrator.branched) {
70                         flag = false;
71                         enqueue_flag = 1;
72                 }
73 #  endif  /* __BRANCHED_PATH__ */
74
75 #  ifdef __SHADOW_TRICKS__
76                 if(flag && state->flag & PATH_RAY_SHADOW_CATCHER) {
77                         flag = false;
78                         enqueue_flag = 1;
79                 }
80 #  endif  /* __SHADOW_TRICKS__ */
81
82                 if(flag) {
83                         /* Sample illumination from lights to find path contribution. */
84                         float light_u, light_v;
85                         path_state_rng_2D(kg, state, PRNG_LIGHT_U, &light_u, &light_v);
86                         float terminate = path_state_rng_light_termination(kg, state);
87
88                         LightSample ls;
89                         if(light_sample(kg,
90                                         light_u, light_v,
91                                         sd->time,
92                                         sd->P,
93                                         state->bounce,
94                                         &ls)) {
95
96                                 Ray light_ray;
97                                 light_ray.time = sd->time;
98
99                                 BsdfEval L_light;
100                                 bool is_lamp;
101                                 if(direct_emission(kg, sd, &kernel_split_state.sd_DL_shadow[ray_index], &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
102                                         /* Write intermediate data to global memory to access from
103                                          * the next kernel.
104                                          */
105                                         kernel_split_state.light_ray[ray_index] = light_ray;
106                                         kernel_split_state.bsdf_eval[ray_index] = L_light;
107                                         kernel_split_state.is_lamp[ray_index] = is_lamp;
108                                         /* Mark ray state for next shadow kernel. */
109                                         enqueue_flag = 1;
110                                 }
111                         }
112                 }
113 #endif  /* __EMISSION__ */
114         }
115
116 #ifdef __EMISSION__
117         /* Enqueue RAY_SHADOW_RAY_CAST_DL rays. */
118         enqueue_ray_index_local(ray_index,
119                                 QUEUE_SHADOW_RAY_CAST_DL_RAYS,
120                                 enqueue_flag,
121                                 kernel_split_params.queue_size,
122                                 local_queue_atomics,
123                                 kernel_split_state.queue_data,
124                                 kernel_split_params.queue_index);
125 #endif
126
127 #ifdef __BRANCHED_PATH__
128         /* Enqueue RAY_LIGHT_INDIRECT_NEXT_ITER rays
129          * this is the last kernel before next_iteration_setup that uses local atomics so we do this here
130          */
131         ccl_barrier(CCL_LOCAL_MEM_FENCE);
132         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
133                 *local_queue_atomics = 0;
134         }
135         ccl_barrier(CCL_LOCAL_MEM_FENCE);
136
137         ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
138         enqueue_ray_index_local(ray_index,
139                                 QUEUE_LIGHT_INDIRECT_ITER,
140                                 IS_STATE(kernel_split_state.ray_state, ray_index, RAY_LIGHT_INDIRECT_NEXT_ITER),
141                                 kernel_split_params.queue_size,
142                                 local_queue_atomics,
143                                 kernel_split_state.queue_data,
144                                 kernel_split_params.queue_index);
145
146 #endif  /* __BRANCHED_PATH__ */
147 }
148
149 CCL_NAMESPACE_END