Cycles: Remove sum_all_radiance kernel
[blender-staging.git] / intern / cycles / kernel / split / kernel_background_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 /* Note on kernel_background_buffer_update kernel.
20  * This is the fourth kernel in the ray tracing logic, and the third
21  * of the path iteration kernels. This kernel takes care of rays that hit
22  * the background (sceneintersect kernel), and for the rays of
23  * state RAY_UPDATE_BUFFER it updates the ray's accumulated radiance in
24  * the output buffer. This kernel also takes care of rays that have been determined
25  * to-be-regenerated.
26  *
27  * We will empty QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue in this kernel
28  *
29  * Typically all rays that are in state RAY_HIT_BACKGROUND, RAY_UPDATE_BUFFER
30  * will be eventually set to RAY_TO_REGENERATE state in this kernel. Finally all rays of ray_state
31  * RAY_TO_REGENERATE will be regenerated and put in queue QUEUE_ACTIVE_AND_REGENERATED_RAYS.
32  *
33  * The input and output are as follows,
34  *
35  * rng_coop ---------------------------------------------|--- kernel_background_buffer_update --|--- PathRadiance_coop
36  * throughput_coop --------------------------------------|                                      |--- L_transparent_coop
37  * per_sample_output_buffers ----------------------------|                                      |--- per_sample_output_buffers
38  * Ray_coop ---------------------------------------------|                                      |--- ray_state
39  * PathState_coop ---------------------------------------|                                      |--- Queue_data (QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS)
40  * L_transparent_coop -----------------------------------|                                      |--- Queue_data (QUEUE_ACTIVE_AND_REGENERATED_RAYS)
41  * ray_state --------------------------------------------|                                      |--- Queue_index (QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS)
42  * Queue_data (QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS) ----|                                      |--- Queue_index (QUEUE_ACTIVE_AND_REGENERATED_RAYS)
43  * Queue_index (QUEUE_ACTIVE_AND_REGENERATED_RAYS) ------|                                      |--- work_array
44  * parallel_samples -------------------------------------|                                      |--- PathState_coop
45  * end_sample -------------------------------------------|                                      |--- throughput_coop
46  * kg (globals) -----------------------------------------|                                      |--- rng_coop
47  * rng_state --------------------------------------------|                                      |--- Ray
48  * PathRadiance_coop ------------------------------------|                                      |
49  * sw ---------------------------------------------------|                                      |
50  * sh ---------------------------------------------------|                                      |
51  * sx ---------------------------------------------------|                                      |
52  * sy ---------------------------------------------------|                                      |
53  * stride -----------------------------------------------|                                      |
54  * work_array -------------------------------------------|                                      |--- work_array
55  * queuesize --------------------------------------------|                                      |
56  * start_sample -----------------------------------------|                                      |--- work_pool_wgs
57  * work_pool_wgs ----------------------------------------|                                      |
58  * num_samples ------------------------------------------|                                      |
59  *
60  * note on sd : sd argument is neither an input nor an output for this kernel. It is just filled and consumed here itself.
61  * Note on Queues :
62  * This kernel fetches rays from QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue.
63  *
64  * State of queues when this kernel is called :
65  * At entry,
66  * QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE rays
67  * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with RAY_UPDATE_BUFFER, RAY_HIT_BACKGROUND, RAY_TO_REGENERATE rays
68  * At exit,
69  * QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE and RAY_REGENERATED rays
70  * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be empty
71  */
72 ccl_device void kernel_background_buffer_update(KernelGlobals *kg)
73 {
74         ccl_local unsigned int local_queue_atomics;
75         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
76                 local_queue_atomics = 0;
77         }
78         ccl_barrier(CCL_LOCAL_MEM_FENCE);
79
80         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
81         if(ray_index == 0) {
82                 /* We will empty this queue in this kernel. */
83                 kernel_split_params.queue_index[QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS] = 0;
84         }
85         char enqueue_flag = 0;
86         ray_index = get_ray_index(kg, ray_index,
87                                   QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS,
88                                   kernel_split_state.queue_data,
89                                   kernel_split_params.queue_size,
90                                   1);
91
92 #ifdef __COMPUTE_DEVICE_GPU__
93         /* If we are executing on a GPU device, we exit all threads that are not
94          * required.
95          *
96          * If we are executing on a CPU device, then we need to keep all threads
97          * active since we have barrier() calls later in the kernel. CPU devices,
98          * expect all threads to execute barrier statement.
99          */
100         if(ray_index == QUEUE_EMPTY_SLOT) {
101                 return;
102         }
103 #endif
104
105 #ifndef __COMPUTE_DEVICE_GPU__
106         if(ray_index != QUEUE_EMPTY_SLOT) {
107 #endif
108
109         ccl_global uint *rng_state = kernel_split_params.rng_state;
110         int stride = kernel_split_params.stride;
111
112         ccl_global char *ray_state = kernel_split_state.ray_state;
113 #ifdef __KERNEL_DEBUG__
114         DebugData *debug_data = &kernel_split_state.debug_data[ray_index];
115 #endif
116         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
117         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
118         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
119         ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
120         ccl_global float *L_transparent = &kernel_split_state.L_transparent[ray_index];
121         ccl_global uint *rng = &kernel_split_state.rng[ray_index];
122         ccl_global float *buffer = kernel_split_params.buffer;
123
124         unsigned int work_index;
125         ccl_global uint *initial_rng;
126
127         unsigned int sample;
128         unsigned int tile_x;
129         unsigned int tile_y;
130         unsigned int pixel_x;
131         unsigned int pixel_y;
132
133         work_index = kernel_split_state.work_array[ray_index];
134         sample = get_work_sample(kg, work_index, ray_index) + kernel_split_params.start_sample;
135         get_work_pixel_tile_position(kg, &pixel_x, &pixel_y,
136                                 &tile_x, &tile_y,
137                                 work_index,
138                                 ray_index);
139         initial_rng = rng_state;
140
141         rng_state += kernel_split_params.offset + pixel_x + pixel_y*stride;
142         buffer += (kernel_split_params.offset + pixel_x + pixel_y*stride) * kernel_data.film.pass_stride;
143
144         if(IS_STATE(ray_state, ray_index, RAY_HIT_BACKGROUND)) {
145                 /* eval background shader if nothing hit */
146                 if(kernel_data.background.transparent && (state->flag & PATH_RAY_CAMERA)) {
147                         *L_transparent = (*L_transparent) + average((*throughput));
148 #ifdef __PASSES__
149                         if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
150 #endif
151                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
152                 }
153
154                 if(IS_STATE(ray_state, ray_index, RAY_HIT_BACKGROUND)) {
155 #ifdef __BACKGROUND__
156                         /* sample background shader */
157                         float3 L_background = indirect_background(kg, &kernel_split_state.sd_DL_shadow[ray_index], state, ray);
158                         path_radiance_accum_background(L, (*throughput), L_background, state->bounce);
159 #endif
160                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
161                 }
162         }
163
164         if(IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER)) {
165                 float3 L_sum = path_radiance_clamp_and_sum(kg, L);
166                 kernel_write_light_passes(kg, buffer, L, sample);
167 #ifdef __KERNEL_DEBUG__
168                 kernel_write_debug_passes(kg, buffer, state, debug_data, sample);
169 #endif
170                 float4 L_rad = make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - (*L_transparent));
171
172                 /* accumulate result in output buffer */
173                 kernel_write_pass_float4(buffer, sample, L_rad);
174                 path_rng_end(kg, rng_state, *rng);
175
176                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_TO_REGENERATE);
177         }
178
179         if(IS_STATE(ray_state, ray_index, RAY_TO_REGENERATE)) {
180                 /* We have completed current work; So get next work */
181                 int valid_work = get_next_work(kg, &work_index, ray_index);
182                 if(!valid_work) {
183                         /* If work is invalid, this means no more work is available and the thread may exit */
184                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_INACTIVE);
185                 }
186
187                 if(IS_STATE(ray_state, ray_index, RAY_TO_REGENERATE)) {
188                         kernel_split_state.work_array[ray_index] = work_index;
189                         /* Get the sample associated with the current work */
190                         sample = get_work_sample(kg, work_index, ray_index) + kernel_split_params.start_sample;
191                         /* Get pixel and tile position associated with current work */
192                         get_work_pixel_tile_position(kg, &pixel_x, &pixel_y, &tile_x, &tile_y, work_index, ray_index);
193
194                         /* Remap rng_state according to the current work */
195                         rng_state = initial_rng + kernel_split_params.offset + pixel_x + pixel_y*stride;
196                         /* Remap buffer according to the current work */
197                         buffer += (kernel_split_params.offset + pixel_x + pixel_y*stride) * kernel_data.film.pass_stride;
198
199                         /* Initialize random numbers and ray. */
200                         kernel_path_trace_setup(kg, rng_state, sample, pixel_x, pixel_y, rng, ray);
201
202                         if(ray->t != 0.0f) {
203                                 /* Initialize throughput, L_transparent, Ray, PathState;
204                                  * These rays proceed with path-iteration.
205                                  */
206                                 *throughput = make_float3(1.0f, 1.0f, 1.0f);
207                                 *L_transparent = 0.0f;
208                                 path_radiance_init(L, kernel_data.film.use_light_pass);
209                                 path_state_init(kg, &kernel_split_state.sd_DL_shadow[ray_index], state, rng, sample, ray);
210 #ifdef __KERNEL_DEBUG__
211                                 debug_data_init(debug_data);
212 #endif
213                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
214                                 enqueue_flag = 1;
215                         }
216                         else {
217                                 /* These rays do not participate in path-iteration. */
218                                 float4 L_rad = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
219                                 /* Accumulate result in output buffer. */
220                                 kernel_write_pass_float4(buffer, sample, L_rad);
221                                 path_rng_end(kg, rng_state, *rng);
222
223                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_TO_REGENERATE);
224                         }
225                 }
226         }
227
228 #ifndef __COMPUTE_DEVICE_GPU__
229         }
230 #endif
231
232         /* Enqueue RAY_REGENERATED rays into QUEUE_ACTIVE_AND_REGENERATED_RAYS;
233          * These rays will be made active during next SceneIntersectkernel.
234          */
235         enqueue_ray_index_local(ray_index,
236                                 QUEUE_ACTIVE_AND_REGENERATED_RAYS,
237                                 enqueue_flag,
238                                 kernel_split_params.queue_size,
239                                 &local_queue_atomics,
240                                 kernel_split_state.queue_data,
241                                 kernel_split_params.queue_index);
242 }
243
244 CCL_NAMESPACE_END
245