28351c2b1ae1f4be2c870d3ffebb7ac287180852
[blender.git] / intern / cycles / kernel / split / kernel_shadow_blocked.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 #include "kernel_split_common.h"
18
19 /* Note on kernel_shadow_blocked kernel.
20  * This is the ninth kernel in the ray tracing logic. This is the eighth
21  * of the path iteration kernels. This kernel takes care of "shadow ray cast"
22  * logic of the direct lighting and AO  part of ray tracing.
23  *
24  * The input and output are as follows,
25  *
26  * PathState_coop ----------------------------------|--- kernel_shadow_blocked --|
27  * LightRay_dl_coop --------------------------------|                            |--- LightRay_dl_coop
28  * LightRay_ao_coop --------------------------------|                            |--- LightRay_ao_coop
29  * ray_state ---------------------------------------|                            |--- ray_state
30  * Queue_data(QUEUE_SHADOW_RAY_CAST_AO_RAYS &       |                            |--- Queue_data (QUEUE_SHADOW_RAY_CAST_AO_RAYS & QUEUE_SHADOW_RAY_CAST_AO_RAYS)
31               QUEUE_SHADOW_RAY_CAST_DL_RAYS) -------|                            |
32  * Queue_index(QUEUE_SHADOW_RAY_CAST_AO_RAYS&
33               QUEUE_SHADOW_RAY_CAST_DL_RAYS) -------|                            |
34  * kg (globals + data) -----------------------------|                            |
35  * queuesize ---------------------------------------|                            |
36  *
37  * Note on shader_shadow : shader_shadow is neither input nor output to this kernel. shader_shadow is filled and consumed in this kernel itself.
38  * Note on queues :
39  * The kernel fetches from QUEUE_SHADOW_RAY_CAST_AO_RAYS and QUEUE_SHADOW_RAY_CAST_DL_RAYS queues. We will empty
40  * these queues this kernel.
41  * State of queues when this kernel is called :
42  * state of queues QUEUE_ACTIVE_AND_REGENERATED_RAYS and QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be same
43  * before and after this kernel call.
44  * QUEUE_SHADOW_RAY_CAST_AO_RAYS & QUEUE_SHADOW_RAY_CAST_DL_RAYS will be filled with rays marked with flags RAY_SHADOW_RAY_CAST_AO
45  * and RAY_SHADOW_RAY_CAST_DL respectively, during kernel entry.
46  * QUEUE_SHADOW_RAY_CAST_AO_RAYS and QUEUE_SHADOW_RAY_CAST_DL_RAYS will be empty at kernel exit.
47  */
48 ccl_device void kernel_shadow_blocked(
49         ccl_global char *globals,
50         ccl_constant KernelData *data,
51         ccl_global char *shader_shadow,        /* Required for shadow blocked */
52         ccl_global PathState *PathState_coop,  /* Required for shadow blocked */
53         ccl_global Ray *LightRay_dl_coop,      /* Required for direct lighting's shadow blocked */
54         ccl_global Ray *LightRay_ao_coop,      /* Required for AO's shadow blocked */
55         Intersection *Intersection_coop_AO,
56         Intersection *Intersection_coop_DL,
57         ccl_global char *ray_state,
58         int total_num_rays,
59         char shadow_blocked_type,
60         int ray_index)
61 {
62         /* Flag determining if we need to update L. */
63         char update_path_radiance = 0;
64
65         if(IS_FLAG(ray_state, ray_index, RAY_SHADOW_RAY_CAST_DL) ||
66            IS_FLAG(ray_state, ray_index, RAY_SHADOW_RAY_CAST_AO))
67         {
68                 /* Load kernel global structure. */
69                 KernelGlobals *kg = (KernelGlobals *)globals;
70                 ShaderData *sd_shadow  = (ShaderData *)shader_shadow;
71
72                 ccl_global PathState *state = &PathState_coop[ray_index];
73                 ccl_global Ray *light_ray_dl_global = &LightRay_dl_coop[ray_index];
74                 ccl_global Ray *light_ray_ao_global = &LightRay_ao_coop[ray_index];
75                 Intersection *isect_ao_global = &Intersection_coop_AO[ray_index];
76                 Intersection *isect_dl_global = &Intersection_coop_DL[ray_index];
77
78                 ccl_global Ray *light_ray_global =
79                         shadow_blocked_type == RAY_SHADOW_RAY_CAST_AO
80                                  ? light_ray_ao_global
81                                  : light_ray_dl_global;
82                 Intersection *isect_global =
83                         RAY_SHADOW_RAY_CAST_AO ? isect_ao_global : isect_dl_global;
84
85                 float3 shadow;
86                 update_path_radiance = !(shadow_blocked(kg,
87                                                         state,
88                                                         light_ray_global,
89                                                         &shadow,
90                                                         sd_shadow,
91                                                         isect_global));
92
93                 /* We use light_ray_global's P and t to store shadow and
94                  * update_path_radiance.
95                  */
96                 light_ray_global->P = shadow;
97                 light_ray_global->t = update_path_radiance;
98         }
99 }