Cycles: OpenCL split kernel cleanup, move casts from .h files to .cl files
[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 sd_shadow : sd_shadow is neither input nor output to this kernel. sd_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         KernelGlobals *kg,
50         ccl_constant KernelData *data,
51         ShaderData *sd_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                 ccl_global PathState *state = &PathState_coop[ray_index];
69                 ccl_global Ray *light_ray_dl_global = &LightRay_dl_coop[ray_index];
70                 ccl_global Ray *light_ray_ao_global = &LightRay_ao_coop[ray_index];
71                 Intersection *isect_ao_global = &Intersection_coop_AO[ray_index];
72                 Intersection *isect_dl_global = &Intersection_coop_DL[ray_index];
73
74                 ccl_global Ray *light_ray_global =
75                         shadow_blocked_type == RAY_SHADOW_RAY_CAST_AO
76                                  ? light_ray_ao_global
77                                  : light_ray_dl_global;
78                 Intersection *isect_global =
79                         RAY_SHADOW_RAY_CAST_AO ? isect_ao_global : isect_dl_global;
80
81                 float3 shadow;
82                 update_path_radiance = !(shadow_blocked(kg,
83                                                         state,
84                                                         light_ray_global,
85                                                         &shadow,
86                                                         sd_shadow,
87                                                         isect_global));
88
89                 /* We use light_ray_global's P and t to store shadow and
90                  * update_path_radiance.
91                  */
92                 light_ray_global->P = shadow;
93                 light_ray_global->t = update_path_radiance;
94         }
95 }