Code cleanup: store branch factor in PathState.
[blender.git] / intern / cycles / kernel / split / kernel_holdout_emission_blurring_pathtermination_ao.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 the logic to process "material of type holdout",
20  * indirect primitive emission, bsdf blurring, probabilistic path termination
21  * and AO.
22  *
23  * This kernels determines the rays for which a shadow_blocked() function
24  * associated with AO should be executed. Those rays for which a
25  * shadow_blocked() function for AO must be executed are marked with flag
26  * RAY_SHADOW_RAY_CAST_ao and enqueued into the queue
27  * QUEUE_SHADOW_RAY_CAST_AO_RAYS
28  *
29  * Ray state of rays that are terminated in this kernel are changed to RAY_UPDATE_BUFFER
30  *
31  * Note on Queues:
32  * This kernel fetches rays from the queue QUEUE_ACTIVE_AND_REGENERATED_RAYS
33  * and processes only the rays of state RAY_ACTIVE.
34  * There are different points in this kernel where a ray may terminate and
35  * reach RAY_UPDATE_BUFFER state. These rays are enqueued into
36  * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue. These rays will still be present
37  * in QUEUE_ACTIVE_AND_REGENERATED_RAYS queue, but since their ray-state has
38  * been changed to RAY_UPDATE_BUFFER, there is no problem.
39  *
40  * State of queues when this kernel is called:
41  * At entry,
42  *   - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE and
43  *     RAY_REGENERATED rays
44  *   - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
45  *     RAY_TO_REGENERATE rays.
46  *   - QUEUE_SHADOW_RAY_CAST_AO_RAYS will be empty.
47  * At exit,
48  *   - QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE,
49  *     RAY_REGENERATED and RAY_UPDATE_BUFFER rays.
50  *   - QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with
51  *     RAY_TO_REGENERATE and RAY_UPDATE_BUFFER rays.
52  *   - QUEUE_SHADOW_RAY_CAST_AO_RAYS will be filled with rays marked with
53  *     flag RAY_SHADOW_RAY_CAST_AO
54  */
55
56 ccl_device void kernel_holdout_emission_blurring_pathtermination_ao(
57         KernelGlobals *kg,
58         ccl_local_param BackgroundAOLocals *locals)
59 {
60         if(ccl_local_id(0) == 0 && ccl_local_id(1) == 0) {
61                 locals->queue_atomics_bg = 0;
62                 locals->queue_atomics_ao = 0;
63         }
64         ccl_barrier(CCL_LOCAL_MEM_FENCE);
65
66 #ifdef __AO__
67         char enqueue_flag = 0;
68 #endif
69         int ray_index = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
70         ray_index = get_ray_index(kg, ray_index,
71                                   QUEUE_ACTIVE_AND_REGENERATED_RAYS,
72                                   kernel_split_state.queue_data,
73                                   kernel_split_params.queue_size,
74                                   0);
75
76 #ifdef __COMPUTE_DEVICE_GPU__
77         /* If we are executing on a GPU device, we exit all threads that are not
78          * required.
79          *
80          * If we are executing on a CPU device, then we need to keep all threads
81          * active since we have barrier() calls later in the kernel. CPU devices,
82          * expect all threads to execute barrier statement.
83          */
84         if(ray_index == QUEUE_EMPTY_SLOT) {
85                 return;
86         }
87 #endif  /* __COMPUTE_DEVICE_GPU__ */
88
89 #ifndef __COMPUTE_DEVICE_GPU__
90         if(ray_index != QUEUE_EMPTY_SLOT) {
91 #endif
92
93         int stride = kernel_split_params.stride;
94
95         ccl_global PathState *state = 0x0;
96         float3 throughput;
97
98         ccl_global char *ray_state = kernel_split_state.ray_state;
99         ShaderData *sd = &kernel_split_state.sd[ray_index];
100
101         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
102                 uint work_index = kernel_split_state.work_array[ray_index];
103                 uint pixel_x, pixel_y, tile_x, tile_y;
104                 get_work_pixel_tile_position(kg, &pixel_x, &pixel_y,
105                                         &tile_x, &tile_y,
106                                         work_index,
107                                         ray_index);
108
109                 ccl_global float *buffer = kernel_split_params.buffer;
110                 buffer += (kernel_split_params.offset + pixel_x + pixel_y * stride) * kernel_data.film.pass_stride;
111
112                 ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
113                 ShaderData *emission_sd = &kernel_split_state.sd_DL_shadow[ray_index];
114                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
115
116                 throughput = kernel_split_state.throughput[ray_index];
117                 state = &kernel_split_state.path_state[ray_index];
118
119                 if(!kernel_path_shader_apply(kg,
120                                              sd,
121                                              state,
122                                              ray,
123                                              throughput,
124                                              emission_sd,
125                                              L,
126                                              buffer))
127                 {
128                         kernel_split_path_end(kg, ray_index);
129                 }
130         }
131
132         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
133                 /* Path termination. this is a strange place to put the termination, it's
134                  * mainly due to the mixed in MIS that we use. gives too many unneeded
135                  * shader evaluations, only need emission if we are going to terminate.
136                  */
137                 float probability = path_state_continuation_probability(kg, state, throughput);
138
139                 if(probability == 0.0f) {
140                         kernel_split_path_end(kg, ray_index);
141                 }
142                 else if(probability < 1.0f) {
143                         float terminate = path_state_rng_1D_for_decision(kg, state, PRNG_TERMINATE);
144                         if(terminate >= probability) {
145                                 kernel_split_path_end(kg, ray_index);
146                         }
147                         else {
148                                 kernel_split_state.throughput[ray_index] = throughput/probability;
149                         }
150                 }
151
152                 if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
153                         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
154                         kernel_update_denoising_features(kg, sd, state, L);
155                 }
156         }
157
158 #ifdef __AO__
159         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
160                 /* ambient occlusion */
161                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
162                         enqueue_flag = 1;
163                 }
164         }
165 #endif  /* __AO__ */
166
167 #ifndef __COMPUTE_DEVICE_GPU__
168         }
169 #endif
170
171 #ifdef __AO__
172         /* Enqueue to-shadow-ray-cast rays. */
173         enqueue_ray_index_local(ray_index,
174                                 QUEUE_SHADOW_RAY_CAST_AO_RAYS,
175                                 enqueue_flag,
176                                 kernel_split_params.queue_size,
177                                 &locals->queue_atomics_ao,
178                                 kernel_split_state.queue_data,
179                                 kernel_split_params.queue_index);
180 #endif
181 }
182
183 CCL_NAMESPACE_END