6ec75013b3a64ae9571e97e459996338f40c8c0f
[blender.git] / intern / cycles / kernel / kernels / opencl / kernel_direct_lighting.cl
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 "split/kernel_direct_lighting.h"
18
19 __kernel void kernel_ocl_path_trace_direct_lighting(
20         ccl_global char *globals,
21         ccl_constant KernelData *data,
22         ccl_global char *shader_data,           /* Required for direct lighting */
23         ccl_global char *shader_DL,             /* Required for direct lighting */
24         ccl_global uint *rng_coop,              /* Required for direct lighting */
25         ccl_global PathState *PathState_coop,   /* Required for direct lighting */
26         ccl_global int *ISLamp_coop,            /* Required for direct lighting */
27         ccl_global Ray *LightRay_coop,          /* Required for direct lighting */
28         ccl_global BsdfEval *BSDFEval_coop,     /* Required for direct lighting */
29         ccl_global char *ray_state,             /* Denotes the state of each ray */
30         ccl_global int *Queue_data,             /* Queue memory */
31         ccl_global int *Queue_index,            /* Tracks the number of elements in each queue */
32         int queuesize)                          /* Size (capacity) of each queue */
33 {
34         ccl_local unsigned int local_queue_atomics;
35         if(get_local_id(0) == 0 && get_local_id(1) == 0) {
36                 local_queue_atomics = 0;
37         }
38         barrier(CLK_LOCAL_MEM_FENCE);
39
40         char enqueue_flag = 0;
41         int ray_index = get_global_id(1) * get_global_size(0) + get_global_id(0);
42         ray_index = get_ray_index(ray_index,
43                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
44                                   Queue_data,
45                                   queuesize,
46                                   0);
47
48 #ifdef __COMPUTE_DEVICE_GPU__
49         /* If we are executing on a GPU device, we exit all threads that are not
50          * required.
51          *
52          * If we are executing on a CPU device, then we need to keep all threads
53          * active since we have barrier() calls later in the kernel. CPU devices,
54          * expect all threads to execute barrier statement.
55          */
56         if(ray_index == QUEUE_EMPTY_SLOT) {
57                 return;
58         }
59 #endif
60
61 #ifndef __COMPUTE_DEVICE_GPU__
62         if(ray_index != QUEUE_EMPTY_SLOT) {
63 #endif
64                 enqueue_flag = kernel_direct_lighting(globals,
65                                                       data,
66                                                       shader_data,
67                                                       shader_DL,
68                                                       rng_coop,
69                                                       PathState_coop,
70                                                       ISLamp_coop,
71                                                       LightRay_coop,
72                                                       BSDFEval_coop,
73                                                       ray_state,
74                                                       ray_index);
75
76 #ifndef __COMPUTE_DEVICE_GPU__
77         }
78 #endif
79
80 #ifdef __EMISSION__
81         /* Enqueue RAY_SHADOW_RAY_CAST_DL rays. */
82         enqueue_ray_index_local(ray_index,
83                                 QUEUE_SHADOW_RAY_CAST_DL_RAYS,
84                                 enqueue_flag,
85                                 queuesize,
86                                 &local_queue_atomics,
87                                 Queue_data,
88                                 Queue_index);
89 #endif
90 }