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