Merge branch 'master' into blender28
[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         ccl_global PathState *state = 0x0;
94         float3 throughput;
95
96         ccl_global char *ray_state = kernel_split_state.ray_state;
97         ShaderData *sd = &kernel_split_state.sd[ray_index];
98
99         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
100                 uint buffer_offset = kernel_split_state.buffer_offset[ray_index];
101                 ccl_global float *buffer = kernel_split_params.tile.buffer + buffer_offset;
102
103                 ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
104                 ShaderData *emission_sd = &kernel_split_state.sd_DL_shadow[ray_index];
105                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
106
107                 throughput = kernel_split_state.throughput[ray_index];
108                 state = &kernel_split_state.path_state[ray_index];
109
110                 if(!kernel_path_shader_apply(kg,
111                                              sd,
112                                              state,
113                                              ray,
114                                              throughput,
115                                              emission_sd,
116                                              L,
117                                              buffer))
118                 {
119                         kernel_split_path_end(kg, ray_index);
120                 }
121         }
122
123         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
124                 /* Path termination. this is a strange place to put the termination, it's
125                  * mainly due to the mixed in MIS that we use. gives too many unneeded
126                  * shader evaluations, only need emission if we are going to terminate.
127                  */
128                 float probability = path_state_continuation_probability(kg, state, throughput);
129
130                 if(probability == 0.0f) {
131                         kernel_split_path_end(kg, ray_index);
132                 }
133                 else if(probability < 1.0f) {
134                         float terminate = path_state_rng_1D(kg, state, PRNG_TERMINATE);
135                         if(terminate >= probability) {
136                                 kernel_split_path_end(kg, ray_index);
137                         }
138                         else {
139                                 kernel_split_state.throughput[ray_index] = throughput/probability;
140                         }
141                 }
142
143                 if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
144                         PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
145                         kernel_update_denoising_features(kg, sd, state, L);
146                 }
147         }
148
149 #ifdef __AO__
150         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
151                 /* ambient occlusion */
152                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
153                         enqueue_flag = 1;
154                 }
155         }
156 #endif  /* __AO__ */
157
158 #ifndef __COMPUTE_DEVICE_GPU__
159         }
160 #endif
161
162 #ifdef __AO__
163         /* Enqueue to-shadow-ray-cast rays. */
164         enqueue_ray_index_local(ray_index,
165                                 QUEUE_SHADOW_RAY_CAST_AO_RAYS,
166                                 enqueue_flag,
167                                 kernel_split_params.queue_size,
168                                 &locals->queue_atomics_ao,
169                                 kernel_split_state.queue_data,
170                                 kernel_split_params.queue_index);
171 #endif
172 }
173
174 CCL_NAMESPACE_END