9a6bdfbdffef55f2ebe8efd0b9aaabf7f4f24ce2
[blender.git] / intern / cycles / kernel / split / kernel_shadow_blocked_dl.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 /* Shadow ray cast for direct visible light. */
20 ccl_device void kernel_shadow_blocked_dl(KernelGlobals *kg)
21 {
22         unsigned int dl_queue_length = kernel_split_params.queue_index[QUEUE_SHADOW_RAY_CAST_DL_RAYS];
23         ccl_barrier(CCL_LOCAL_MEM_FENCE);
24
25         int ray_index = QUEUE_EMPTY_SLOT;
26         int thread_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
27         if(thread_index < dl_queue_length) {
28                 ray_index = get_ray_index(kg, thread_index, QUEUE_SHADOW_RAY_CAST_DL_RAYS,
29                                           kernel_split_state.queue_data, kernel_split_params.queue_size, 1);
30         }
31
32 #ifdef __BRANCHED_PATH__
33         /* TODO(mai): move this somewhere else? */
34         if(thread_index == 0) {
35                 /* Clear QUEUE_INACTIVE_RAYS before next kernel. */
36                 kernel_split_params.queue_index[QUEUE_INACTIVE_RAYS] = 0;
37         }
38 #endif  /* __BRANCHED_PATH__ */
39
40         if(ray_index == QUEUE_EMPTY_SLOT)
41                 return;
42
43         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
44         Ray ray = kernel_split_state.light_ray[ray_index];
45         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
46         ShaderData *sd = &kernel_split_state.sd[ray_index];
47         float3 throughput = kernel_split_state.throughput[ray_index];
48
49         BsdfEval L_light = kernel_split_state.bsdf_eval[ray_index];
50         ShaderData *emission_sd = AS_SHADER_DATA(&kernel_split_state.sd_DL_shadow[ray_index]);
51         bool is_lamp = kernel_split_state.is_lamp[ray_index];
52
53 #  if defined(__BRANCHED_PATH__) || defined(__SHADOW_TRICKS__)
54         bool use_branched = false;
55         int all = 0;
56
57         if(state->flag & PATH_RAY_SHADOW_CATCHER) {
58                 use_branched = true;
59                 all = 1;
60         }
61 #    if defined(__BRANCHED_PATH__)
62         else if(kernel_data.integrator.branched) {
63                 use_branched = true;
64
65                 if(IS_FLAG(kernel_split_state.ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
66                         all = (kernel_data.integrator.sample_all_lights_indirect);
67                 }
68                 else
69                 {
70                         all = (kernel_data.integrator.sample_all_lights_direct);
71                 }
72         }
73 #    endif  /* __BRANCHED_PATH__ */
74
75         if(use_branched) {
76                 kernel_branched_path_surface_connect_light(kg,
77                                                            sd,
78                                                            emission_sd,
79                                                            state,
80                                                            throughput,
81                                                            1.0f,
82                                                            L,
83                                                            all);
84         }
85         else
86 #  endif  /* defined(__BRANCHED_PATH__) || defined(__SHADOW_TRICKS__)*/
87         {
88                 /* trace shadow ray */
89                 float3 shadow;
90
91                 if(!shadow_blocked(kg,
92                                    sd,
93                                    emission_sd,
94                                    state,
95                                    &ray,
96                                    &shadow))
97                 {
98                         /* accumulate */
99                         path_radiance_accum_light(L, state, throughput, &L_light, shadow, 1.0f, is_lamp);
100                 }
101                 else {
102                         path_radiance_accum_total_light(L, state, throughput, &L_light);
103                 }
104         }
105 }
106
107 CCL_NAMESPACE_END