832b0e5b2652e035f7028c91d45c0e3680a8a898
[blender.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,
102                                                    sd,
103                                                    AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]),
104                                                    &ls,
105                                                    state,
106                                                    &light_ray,
107                                                    &L_light,
108                                                    &is_lamp,
109                                                    terminate))
110                                 {
111                                         /* Write intermediate data to global memory to access from
112                                          * the next kernel.
113                                          */
114                                         kernel_split_state.light_ray[ray_index] = light_ray;
115                                         kernel_split_state.bsdf_eval[ray_index] = L_light;
116                                         kernel_split_state.is_lamp[ray_index] = is_lamp;
117                                         /* Mark ray state for next shadow kernel. */
118                                         enqueue_flag = 1;
119                                 }
120                         }
121                 }
122 #endif  /* __EMISSION__ */
123         }
124
125 #ifdef __EMISSION__
126         /* Enqueue RAY_SHADOW_RAY_CAST_DL rays. */
127         enqueue_ray_index_local(ray_index,
128                                 QUEUE_SHADOW_RAY_CAST_DL_RAYS,
129                                 enqueue_flag,
130                                 kernel_split_params.queue_size,
131                                 local_queue_atomics,
132                                 kernel_split_state.queue_data,
133                                 kernel_split_params.queue_index);
134 #endif
135
136 #ifdef __BRANCHED_PATH__
137         /* Enqueue RAY_LIGHT_INDIRECT_NEXT_ITER rays
138          * this is the last kernel before next_iteration_setup that uses local atomics so we do this here
139          */
140         ccl_barrier(CCL_LOCAL_MEM_FENCE);
141         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
142                 *local_queue_atomics = 0;
143         }
144         ccl_barrier(CCL_LOCAL_MEM_FENCE);
145
146         ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
147         enqueue_ray_index_local(ray_index,
148                                 QUEUE_LIGHT_INDIRECT_ITER,
149                                 IS_STATE(kernel_split_state.ray_state, ray_index, RAY_LIGHT_INDIRECT_NEXT_ITER),
150                                 kernel_split_params.queue_size,
151                                 local_queue_atomics,
152                                 kernel_split_state.queue_data,
153                                 kernel_split_params.queue_index);
154
155 #endif  /* __BRANCHED_PATH__ */
156 }
157
158 CCL_NAMESPACE_END