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