Code cleanup: make L_transparent part of PathRadiance.
[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 uint *rng_state = kernel_split_params.rng_state;
79         int stride = kernel_split_params.stride;
80
81         ccl_global char *ray_state = kernel_split_state.ray_state;
82         ccl_global PathState *state = &kernel_split_state.path_state[ray_index];
83         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
84         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
85         ccl_global float3 *throughput = &kernel_split_state.throughput[ray_index];
86         RNG rng = kernel_split_state.rng[ray_index];
87         ccl_global float *buffer = kernel_split_params.buffer;
88
89         unsigned int work_index;
90         ccl_global uint *initial_rng;
91
92         unsigned int sample;
93         unsigned int tile_x;
94         unsigned int tile_y;
95         unsigned int pixel_x;
96         unsigned int pixel_y;
97
98         work_index = kernel_split_state.work_array[ray_index];
99         sample = get_work_sample(kg, work_index, ray_index) + kernel_split_params.start_sample;
100         get_work_pixel_tile_position(kg, &pixel_x, &pixel_y,
101                                 &tile_x, &tile_y,
102                                 work_index,
103                                 ray_index);
104         initial_rng = rng_state;
105
106         rng_state += kernel_split_params.offset + pixel_x + pixel_y*stride;
107         buffer += (kernel_split_params.offset + pixel_x + pixel_y*stride) * kernel_data.film.pass_stride;
108
109         if(IS_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER)) {
110                 /* accumulate result in output buffer */
111                 bool is_shadow_catcher = (state->flag & PATH_RAY_SHADOW_CATCHER);
112                 kernel_write_result(kg, buffer, sample, L, is_shadow_catcher);
113
114                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_TO_REGENERATE);
115         }
116
117         if(IS_STATE(ray_state, ray_index, RAY_TO_REGENERATE)) {
118                 /* We have completed current work; So get next work */
119                 int valid_work = get_next_work(kg, &work_index, ray_index);
120                 if(!valid_work) {
121                         /* If work is invalid, this means no more work is available and the thread may exit */
122                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_INACTIVE);
123                 }
124
125                 if(IS_STATE(ray_state, ray_index, RAY_TO_REGENERATE)) {
126                         kernel_split_state.work_array[ray_index] = work_index;
127                         /* Get the sample associated with the current work */
128                         sample = get_work_sample(kg, work_index, ray_index) + kernel_split_params.start_sample;
129                         /* Get pixel and tile position associated with current work */
130                         get_work_pixel_tile_position(kg, &pixel_x, &pixel_y, &tile_x, &tile_y, work_index, ray_index);
131
132                         /* Remap rng_state according to the current work */
133                         rng_state = initial_rng + kernel_split_params.offset + pixel_x + pixel_y*stride;
134                         /* Remap buffer according to the current work */
135                         buffer += (kernel_split_params.offset + pixel_x + pixel_y*stride) * kernel_data.film.pass_stride;
136
137                         /* Initialize random numbers and ray. */
138                         kernel_path_trace_setup(kg, rng_state, sample, pixel_x, pixel_y, &rng, ray);
139
140                         if(ray->t != 0.0f) {
141                                 /* Initialize throughput, path radiance, Ray, PathState;
142                                  * These rays proceed with path-iteration.
143                                  */
144                                 *throughput = make_float3(1.0f, 1.0f, 1.0f);
145                                 path_radiance_init(L, kernel_data.film.use_light_pass);
146                                 path_state_init(kg, &kernel_split_state.sd_DL_shadow[ray_index], state, &rng, sample, ray);
147 #ifdef __SUBSURFACE__
148                                 kernel_path_subsurface_init_indirect(&kernel_split_state.ss_rays[ray_index]);
149 #endif
150                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_REGENERATED);
151                                 enqueue_flag = 1;
152                         }
153                         else {
154                                 /* These rays do not participate in path-iteration. */
155                                 float4 L_rad = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
156                                 /* Accumulate result in output buffer. */
157                                 kernel_write_pass_float4(buffer, sample, L_rad);
158
159                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_TO_REGENERATE);
160                         }
161                 }
162         }
163         kernel_split_state.rng[ray_index] = rng;
164
165 #ifndef __COMPUTE_DEVICE_GPU__
166         }
167 #endif
168
169         /* Enqueue RAY_REGENERATED rays into QUEUE_ACTIVE_AND_REGENERATED_RAYS;
170          * These rays will be made active during next SceneIntersectkernel.
171          */
172         enqueue_ray_index_local(ray_index,
173                                 QUEUE_ACTIVE_AND_REGENERATED_RAYS,
174                                 enqueue_flag,
175                                 kernel_split_params.queue_size,
176                                 local_queue_atomics,
177                                 kernel_split_state.queue_data,
178                                 kernel_split_params.queue_index);
179 }
180
181 CCL_NAMESPACE_END