Fix Cycles shadow catcher objects influencing each other.
[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         unsigned int work_index;
96         unsigned int pixel_x;
97         unsigned int pixel_y;
98
99         unsigned int tile_x;
100         unsigned int tile_y;
101         unsigned int sample;
102
103         RNG rng = kernel_split_state.rng[ray_index];
104         ccl_global PathState *state = 0x0;
105         float3 throughput;
106
107         ccl_global char *ray_state = kernel_split_state.ray_state;
108         ShaderData *sd = &kernel_split_state.sd[ray_index];
109         ccl_global float *buffer = kernel_split_params.buffer;
110
111         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
112
113                 throughput = kernel_split_state.throughput[ray_index];
114                 state = &kernel_split_state.path_state[ray_index];
115
116                 work_index = kernel_split_state.work_array[ray_index];
117                 sample = get_work_sample(kg, work_index, ray_index) + kernel_split_params.start_sample;
118                 get_work_pixel_tile_position(kg, &pixel_x, &pixel_y,
119                                         &tile_x, &tile_y,
120                                         work_index,
121                                         ray_index);
122
123                 buffer += (kernel_split_params.offset + pixel_x + pixel_y * stride) * kernel_data.film.pass_stride;
124
125 #ifdef __SHADOW_TRICKS__
126                 if((sd->object_flag & SD_OBJECT_SHADOW_CATCHER)) {
127                         if(state->flag & PATH_RAY_CAMERA) {
128                                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
129                                 state->flag |= (PATH_RAY_SHADOW_CATCHER |
130                                                 PATH_RAY_SHADOW_CATCHER_ONLY |
131                                                 PATH_RAY_STORE_SHADOW_INFO);
132                                 if(!kernel_data.background.transparent) {
133                                         ccl_global Ray *ray = &kernel_split_state.ray[ray_index];
134                                         L->shadow_background_color = indirect_background(
135                                                 kg,
136                                                 &kernel_split_state.sd_DL_shadow[ray_index],
137                                                 state,
138                                                 ray);
139                                 }
140                                 L->shadow_radiance_sum = path_radiance_clamp_and_sum(kg, L);
141                                 L->shadow_throughput = average(throughput);
142                         }
143                 }
144                 else {
145                         state->flag &= ~PATH_RAY_SHADOW_CATCHER_ONLY;
146                 }
147 #endif  /* __SHADOW_TRICKS__ */
148
149                 /* holdout */
150 #ifdef __HOLDOUT__
151                 if(((sd->flag & SD_HOLDOUT) ||
152                     (sd->object_flag & SD_OBJECT_HOLDOUT_MASK)) &&
153                    (state->flag & PATH_RAY_CAMERA))
154                 {
155                         if(kernel_data.background.transparent) {
156                                 float3 holdout_weight;
157                                 if(sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
158                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
159                                 }
160                                 else {
161                                         holdout_weight = shader_holdout_eval(kg, sd);
162                                 }
163                                 /* any throughput is ok, should all be identical here */
164                                 kernel_split_state.L_transparent[ray_index] += average(holdout_weight*throughput);
165                         }
166                         if(sd->object_flag & SD_OBJECT_HOLDOUT_MASK) {
167                                 kernel_split_path_end(kg, ray_index);
168                         }
169                 }
170 #endif  /* __HOLDOUT__ */
171         }
172
173         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
174                 PathRadiance *L = &kernel_split_state.path_radiance[ray_index];
175
176 #ifdef __BRANCHED_PATH__
177                 if(!IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT))
178 #endif  /* __BRANCHED_PATH__ */
179                 {
180                         /* Holdout mask objects do not write data passes. */
181                         kernel_write_data_passes(kg,
182                                                      buffer,
183                                                      L,
184                                                      sd,
185                                                      sample,
186                                                      state,
187                                                      throughput);
188                 }
189
190                 /* Blurring of bsdf after bounces, for rays that have a small likelihood
191                  * of following this particular path (diffuse, rough glossy.
192                  */
193 #ifndef __BRANCHED_PATH__
194                 if(kernel_data.integrator.filter_glossy != FLT_MAX)
195 #else
196                 if(kernel_data.integrator.filter_glossy != FLT_MAX &&
197                    (!kernel_data.integrator.branched || IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)))
198 #endif  /* __BRANCHED_PATH__ */
199                 {
200                         float blur_pdf = kernel_data.integrator.filter_glossy*state->min_ray_pdf;
201                         if(blur_pdf < 1.0f) {
202                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
203                                 shader_bsdf_blur(kg, sd, blur_roughness);
204                         }
205                 }
206
207 #ifdef __EMISSION__
208                 /* emission */
209                 if(sd->flag & SD_EMISSION) {
210                         /* TODO(sergey): is isect.t wrong here for transparent surfaces? */
211                         float3 emission = indirect_primitive_emission(
212                                 kg,
213                                 sd,
214                                 kernel_split_state.isect[ray_index].t,
215                                 state->flag,
216                                 state->ray_pdf);
217                         path_radiance_accum_emission(L, throughput, emission, state->bounce);
218                 }
219 #endif  /* __EMISSION__ */
220
221                 /* Path termination. this is a strange place to put the termination, it's
222                  * mainly due to the mixed in MIS that we use. gives too many unneeded
223                  * shader evaluations, only need emission if we are going to terminate.
224                  */
225 #ifndef __BRANCHED_PATH__
226                 float probability = path_state_terminate_probability(kg, state, throughput);
227 #else
228                 float probability = 1.0f;
229
230                 if(!kernel_data.integrator.branched) {
231                         probability = path_state_terminate_probability(kg, state, throughput);
232                 }
233                 else if(IS_FLAG(ray_state, ray_index, RAY_BRANCHED_INDIRECT)) {
234                         int num_samples = kernel_split_state.branched_state[ray_index].num_samples;
235                         probability = path_state_terminate_probability(kg, state, throughput*num_samples);
236                 }
237                 else if(state->flag & PATH_RAY_TRANSPARENT) {
238                         probability = path_state_terminate_probability(kg, state, throughput);
239                 }
240 #endif
241
242                 if(probability == 0.0f) {
243                         kernel_split_path_end(kg, ray_index);
244                 }
245
246                 if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
247                         if(probability != 1.0f) {
248                                 float terminate = path_state_rng_1D_for_decision(kg, &rng, state, PRNG_TERMINATE);
249                                 if(terminate >= probability) {
250                                         kernel_split_path_end(kg, ray_index);
251                                 }
252                                 else {
253                                         kernel_split_state.throughput[ray_index] = throughput/probability;
254                                 }
255                         }
256
257                         kernel_update_denoising_features(kg, sd, state, L);
258                 }
259         }
260
261 #ifdef __AO__
262         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
263                 /* ambient occlusion */
264                 if(kernel_data.integrator.use_ambient_occlusion || (sd->flag & SD_AO)) {
265                         enqueue_flag = 1;
266                 }
267         }
268 #endif  /* __AO__ */
269
270         kernel_split_state.rng[ray_index] = rng;
271
272 #ifndef __COMPUTE_DEVICE_GPU__
273         }
274 #endif
275
276 #ifdef __AO__
277         /* Enqueue to-shadow-ray-cast rays. */
278         enqueue_ray_index_local(ray_index,
279                                 QUEUE_SHADOW_RAY_CAST_AO_RAYS,
280                                 enqueue_flag,
281                                 kernel_split_params.queue_size,
282                                 &locals->queue_atomics_ao,
283                                 kernel_split_state.queue_data,
284                                 kernel_split_params.queue_index);
285 #endif
286 }
287
288 CCL_NAMESPACE_END