Code refactor: add WorkTile struct for passing work to kernel.
[blender-staging.git] / intern / cycles / kernel / split / kernel_buffer_update.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 /* This kernel takes care of rays that hit the background (sceneintersect
20  * kernel), and for the rays of state RAY_UPDATE_BUFFER it updates the ray's
21  * accumulated radiance in the output buffer. This kernel also takes care of
22  * rays that have been determined to-be-regenerated.
23  *
24  * We will empty QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue in this kernel.
25  *
26  * Typically all rays that are in state RAY_HIT_BACKGROUND, RAY_UPDATE_BUFFER
27  * will be eventually set to RAY_TO_REGENERATE state in this kernel.
28  * Finally all rays of ray_state RAY_TO_REGENERATE will be regenerated and put
29  * in queue QUEUE_ACTIVE_AND_REGENERATED_RAYS.
30  *
31  * State of queues when this kernel is called:
32  * At entry,
33  *   - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE rays.
34  *   - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
35  *     RAY_UPDATE_BUFFER, RAY_HIT_BACKGROUND, RAY_TO_REGENERATE rays.
36  * At exit,
37  *   - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE and
38  *     RAY_REGENERATED rays.
39  *   - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be empty.
40  */
41 ccl_device void kernel_buffer_update(KernelGlobals *kg,
42                                      ccl_local_param unsigned int *local_queue_atomics)
43 {
44         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
45                 *local_queue_atomics = 0;
46         }
47         ccl_barrier(CCL_LOCAL_MEM_FENCE);
48
49         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
50         if(ray_index == 0) {
51                 /* We will empty this queue in this kernel. */
52                 kernel_split_params.queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
53         }
54         char enqueue_flag = 0;
55         ray_index = get_ray_index(kg, ray_index,
56                                   QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
57                                   kernel_split_state.queue_data,
58                                   kernel_split_params.queue_size,
59                                   1);
60
61 #ifdef __COMPUTE_DEVICE_GPU__
62         /* If we are executing on a GPU device, we exit all threads that are not
63          * required.
64          *
65          * If we are executing on a CPU device, then we need to keep all threads
66          * active since we have barrier() calls later in the kernel. CPU devices,
67          * expect all threads to execute barrier statement.
68          */
69         if(ray_index == QUEUE_EMPTY_SLOT) {
70                 return;
71         }
72 #endif
73
74 #ifndef __COMPUTE_DEVICE_GPU__
75         if(ray_index != QUEUE_EMPTY_SLOT) {
76 #endif
77
78         ccl_global char *ray_state = kernel_split_state.ray_state;
79         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
80         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
81         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
82         ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
83
84         if(IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER)) {
85                 uint sample = state->sample;
86                 uint buffer_offset = kernel_split_state.buffer_offset[ray_index];
87                 ccl_global float *buffer = kernel_split_params.tile.buffer + buffer_offset;
88
89                 /* accumulate result in output buffer */
90                 kernel_write_result(kg, buffer, sample, L);
91
92                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_TO_REGENERATE);
93         }
94
95         if(IS_STATE(ray_state, ray_index, RAY_TO_REGENERATE)) {
96                 /* We have completed current work; So get next work */
97                 ccl_global uint *work_pools = kernel_split_params.work_pools;
98                 uint total_work_size = kernel_split_params.total_work_size;
99                 uint work_index;
100
101                 if(!get_next_work(kg, work_pools, total_work_size, ray_index, &work_index)) {
102                         /* If work is invalid, this means no more work is available and the thread may exit */
103                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_INACTIVE);
104                 }
105
106                 if(IS_STATE(ray_state, ray_index, RAY_TO_REGENERATE)) {
107                         ccl_global WorkTile *tile = &kernel_split_params.tile;
108                         uint x, y, sample;
109                         get_work_pixel(tile, work_index, &x, &y, &sample);
110
111                         /* Remap rng_state to current pixel. */
112                         ccl_global uint *rng_state = kernel_split_params.tile.rng_state;
113                         rng_state += tile->offset + x + y*tile->stride;
114
115                         /* Store buffer offset for writing to passes. */
116                         uint buffer_offset = (tile->offset + x + y*tile->stride) * kernel_data.film.pass_stride;
117                         ccl_global float *buffer = tile->buffer + buffer_offset;
118                         kernel_split_state.buffer_offset[ray_index] = buffer_offset;
119
120                         /* Initialize random numbers and ray. */
121                         uint rng_hash;
122                         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng_hash, ray);
123
124                         if(ray->t != 0.0f) {
125                                 /* Initialize throughput, path radiance, Ray, PathState;
126                                  * These rays proceed with path-iteration.
127                                  */
128                                 *throughput = make_float3(1.0f, 1.0f, 1.0f);
129                                 path_radiance_init(L, kernel_data.film.use_light_pass);
130                                 path_state_init(kg, &kernel_split_state.sd_DL_shadow[ray_index], state, rng_hash, sample, ray);
131 #ifdef __SUBSURFACE__
132                                 kernel_path_subsurface_init_indirect(&kernel_split_state.ss_rays[ray_index]);
133 #endif
134                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
135                                 enqueue_flag = 1;
136                         }
137                         else {
138                                 /* These rays do not participate in path-iteration. */
139                                 float4 L_rad = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
140                                 /* Accumulate result in output buffer. */
141                                 kernel_write_pass_float4(buffer, sample, L_rad);
142
143                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_TO_REGENERATE);
144                         }
145                 }
146         }
147
148 #ifndef __COMPUTE_DEVICE_GPU__
149         }
150 #endif
151
152         /* Enqueue RAY_REGENERATED rays into QUEUE_ACTIVE_AND_REGENERATED_RAYS;
153          * These rays will be made active during next SceneIntersectkernel.
154          */
155         enqueue_ray_index_local(ray_index,
156                                 QUEUE_ACTIVE_AND_REGENERATED_RAYS,
157                                 enqueue_flag,
158                                 kernel_split_params.queue_size,
159                                 local_queue_atomics,
160                                 kernel_split_state.queue_data,
161                                 kernel_split_params.queue_index);
162 }
163
164 CCL_NAMESPACE_END