Cycles: Split path initialization into own kernel
[blender-staging.git] / intern / cycles / kernel / split / kernel_data_init.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 /* Note on kernel_data_initialization kernel
20  * This kernel Initializes structures needed in path-iteration kernels.
21  *
22  * Note on Queues :
23  * All slots in queues are initialized to queue empty slot;
24  * The number of elements in the queues is initialized to 0;
25  */
26
27 #ifndef __KERNEL_CPU__
28 ccl_device void kernel_data_init(
29 #else
30 void KERNEL_FUNCTION_FULL_NAME(data_init)(
31 #endif
32         KernelGlobals *kg,
33         ccl_constant KernelData *data,
34         ccl_global void *split_data_buffer,
35         int num_elements,
36         ccl_global char *ray_state,
37         ccl_global uint *rng_state,
38
39 #ifdef __KERNEL_OPENCL__
40 #define KERNEL_TEX(type, ttype, name)                                   \
41         ccl_global type *name,
42 #include "../kernel_textures.h"
43 #endif
44
45         int start_sample,
46         int end_sample,
47         int sx, int sy, int sw, int sh, int offset, int stride,
48         ccl_global int *Queue_index,                 /* Tracks the number of elements in queues */
49         int queuesize,                               /* size (capacity) of the queue */
50         ccl_global char *use_queues_flag,            /* flag to decide if scene-intersect kernel should use queues to fetch ray index */
51         ccl_global unsigned int *work_pools,      /* Work pool for each work group */
52         unsigned int num_samples,
53         ccl_global float *buffer)
54 {
55 #ifdef __KERNEL_OPENCL__
56         kg->data = data;
57 #endif
58
59         kernel_split_params.x = sx;
60         kernel_split_params.y = sy;
61         kernel_split_params.w = sw;
62         kernel_split_params.h = sh;
63
64         kernel_split_params.offset = offset;
65         kernel_split_params.stride = stride;
66
67         kernel_split_params.rng_state = rng_state;
68
69         kernel_split_params.start_sample = start_sample;
70         kernel_split_params.end_sample = end_sample;
71
72         kernel_split_params.work_pools = work_pools;
73         kernel_split_params.num_samples = num_samples;
74
75         kernel_split_params.queue_index = Queue_index;
76         kernel_split_params.queue_size = queuesize;
77         kernel_split_params.use_queues_flag = use_queues_flag;
78
79         kernel_split_params.buffer = buffer;
80
81         split_data_init(&kernel_split_state, num_elements, split_data_buffer, ray_state);
82
83 #ifdef __KERNEL_OPENCL__
84 #define KERNEL_TEX(type, ttype, name) \
85         kg->name = name;
86 #include "../kernel_textures.h"
87 #endif
88
89         int thread_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
90
91         /* Initialize queue data and queue index. */
92         if(thread_index < queuesize) {
93                 /* Initialize active ray queue. */
94                 kernel_split_state.queue_data[QUEUE_ACTIVE_AND_REGENERATED_RAYS * queuesize + thread_index] = QUEUE_EMPTY_SLOT;
95                 /* Initialize background and buffer update queue. */
96                 kernel_split_state.queue_data[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS * queuesize + thread_index] = QUEUE_EMPTY_SLOT;
97                 /* Initialize shadow ray cast of AO queue. */
98                 kernel_split_state.queue_data[QUEUE_SHADOW_RAY_CAST_AO_RAYS * queuesize + thread_index] = QUEUE_EMPTY_SLOT;
99                 /* Initialize shadow ray cast of direct lighting queue. */
100                 kernel_split_state.queue_data[QUEUE_SHADOW_RAY_CAST_DL_RAYS * queuesize + thread_index] = QUEUE_EMPTY_SLOT;
101         }
102
103         if(thread_index == 0) {
104                 Queue_index[QUEUE_ACTIVE_AND_REGENERATED_RAYS] = 0;
105                 Queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
106                 Queue_index[QUEUE_SHADOW_RAY_CAST_AO_RAYS] = 0;
107                 Queue_index[QUEUE_SHADOW_RAY_CAST_DL_RAYS] = 0;
108                 /* The scene-intersect kernel should not use the queues very first time.
109                  * since the queue would be empty.
110                  */
111                 *use_queues_flag = 0;
112         }
113 }
114
115 CCL_NAMESPACE_END
116