Cycles: Remove unused argument from the split kernel functions
[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 #include "kernel_split_common.h"
18
19 /* Note on kernel_holdout_emission_blurring_pathtermination_ao kernel.
20  * This is the sixth kernel in the ray tracing logic. This is the fifth
21  * of the path iteration kernels. This kernel takes care of the logic to process
22  * "material of type holdout", indirect primitive emission, bsdf blurring,
23  * probabilistic path termination and AO.
24  *
25  * This kernels determines the rays for which a shadow_blocked() function associated with AO should be executed.
26  * Those rays for which a shadow_blocked() function for AO must be executed are marked with flag RAY_SHADOW_RAY_CAST_ao and
27  * enqueued into the queue 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  * The input and output are as follows,
32  *
33  * rng_coop ---------------------------------------------|--- kernel_holdout_emission_blurring_pathtermination_ao ---|--- Queue_index (QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS)
34  * throughput_coop --------------------------------------|                                                           |--- PathState_coop
35  * PathRadiance_coop ------------------------------------|                                                           |--- throughput_coop
36  * Intersection_coop ------------------------------------|                                                           |--- L_transparent_coop
37  * PathState_coop ---------------------------------------|                                                           |--- per_sample_output_buffers
38  * L_transparent_coop -----------------------------------|                                                           |--- PathRadiance_coop
39  * sd ---------------------------------------------------|                                                           |--- ShaderData
40  * ray_state --------------------------------------------|                                                           |--- ray_state
41  * Queue_data (QUEUE_ACTIVE_AND_REGENERATED_RAYS) -------|                                                           |--- Queue_data (QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS)
42  * Queue_index (QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS) ---|                                                           |--- AOAlpha_coop
43  * kg (globals) -----------------------------------------|                                                           |--- AOBSDF_coop
44  * parallel_samples -------------------------------------|                                                           |--- AOLightRay_coop
45  * per_sample_output_buffers ----------------------------|                                                           |
46  * sw ---------------------------------------------------|                                                           |
47  * sh ---------------------------------------------------|                                                           |
48  * sx ---------------------------------------------------|                                                           |
49  * sy ---------------------------------------------------|                                                           |
50  * stride -----------------------------------------------|                                                           |
51  * work_array -------------------------------------------|                                                           |
52  * queuesize --------------------------------------------|                                                           |
53  * start_sample -----------------------------------------|                                                           |
54  *
55  * Note on Queues :
56  * This kernel fetches rays from the queue QUEUE_ACTIVE_AND_REGENERATED_RAYS and processes only
57  * the rays of state RAY_ACTIVE.
58  * There are different points in this kernel where a ray may terminate and reach RAY_UPDATE_BUFFER
59  * state. These rays are enqueued into QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS queue. These rays will
60  * still be present in QUEUE_ACTIVE_AND_REGENERATED_RAYS queue, but since their ray-state has been
61  * changed to RAY_UPDATE_BUFFER, there is no problem.
62  *
63  * State of queues when this kernel is called :
64  * At entry,
65  * QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE and RAY_REGENERATED rays
66  * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with RAY_TO_REGENERATE rays.
67  * QUEUE_SHADOW_RAY_CAST_AO_RAYS will be empty.
68  * At exit,
69  * QUEUE_ACTIVE_AND_REGENERATED_RAYS will be filled with RAY_ACTIVE, RAY_REGENERATED and RAY_UPDATE_BUFFER rays
70  * QUEUE_HITBG_BUFF_UPDATE_TOREGEN_RAYS will be filled with RAY_TO_REGENERATE and RAY_UPDATE_BUFFER rays
71  * QUEUE_SHADOW_RAY_CAST_AO_RAYS will be filled with rays marked with flag RAY_SHADOW_RAY_CAST_AO
72  */
73 ccl_device void kernel_holdout_emission_blurring_pathtermination_ao(
74         KernelGlobals *kg,
75         ShaderData *sd,                        /* Required throughout the kernel except probabilistic path termination and AO */
76         ccl_global float *per_sample_output_buffers,
77         ccl_global uint *rng_coop,             /* Required for "kernel_write_data_passes" and AO */
78         ccl_global float3 *throughput_coop,    /* Required for handling holdout material and AO */
79         ccl_global float *L_transparent_coop,  /* Required for handling holdout material */
80         PathRadiance *PathRadiance_coop,       /* Required for "kernel_write_data_passes" and indirect primitive emission */
81         ccl_global PathState *PathState_coop,  /* Required throughout the kernel and AO */
82         Intersection *Intersection_coop,       /* Required for indirect primitive emission */
83         ccl_global float3 *AOAlpha_coop,       /* Required for AO */
84         ccl_global float3 *AOBSDF_coop,        /* Required for AO */
85         ccl_global Ray *AOLightRay_coop,       /* Required for AO */
86         int sw, int sh, int sx, int sy, int stride,
87         ccl_global char *ray_state,            /* Denotes the state of each ray */
88         ccl_global unsigned int *work_array,   /* Denotes the work that each ray belongs to */
89 #ifdef __WORK_STEALING__
90         unsigned int start_sample,
91 #endif
92         int parallel_samples,                  /* Number of samples to be processed in parallel */
93         int ray_index,
94         char *enqueue_flag,
95         char *enqueue_flag_AO_SHADOW_RAY_CAST)
96 {
97 #ifdef __WORK_STEALING__
98         unsigned int my_work;
99         unsigned int pixel_x;
100         unsigned int pixel_y;
101 #endif
102         unsigned int tile_x;
103         unsigned int tile_y;
104         int my_sample_tile;
105         unsigned int sample;
106
107         ccl_global RNG *rng = 0x0;
108         ccl_global PathState *state = 0x0;
109         float3 throughput;
110
111         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
112
113                 throughput = throughput_coop[ray_index];
114                 state = &PathState_coop[ray_index];
115                 rng = &rng_coop[ray_index];
116 #ifdef __WORK_STEALING__
117                 my_work = work_array[ray_index];
118                 sample = get_my_sample(my_work, sw, sh, parallel_samples, ray_index) + start_sample;
119                 get_pixel_tile_position(&pixel_x, &pixel_y,
120                                         &tile_x, &tile_y,
121                                         my_work,
122                                         sw, sh, sx, sy,
123                                         parallel_samples,
124                                         ray_index);
125                 my_sample_tile = 0;
126 #else  /* __WORK_STEALING__ */
127                 sample = work_array[ray_index];
128                 /* Buffer's stride is "stride"; Find x and y using ray_index. */
129                 int tile_index = ray_index / parallel_samples;
130                 tile_x = tile_index % sw;
131                 tile_y = tile_index / sw;
132                 my_sample_tile = ray_index - (tile_index * parallel_samples);
133 #endif  /* __WORK_STEALING__ */
134                 per_sample_output_buffers +=
135                     (((tile_x + (tile_y * stride)) * parallel_samples) + my_sample_tile) *
136                     kernel_data.film.pass_stride;
137
138                 /* holdout */
139 #ifdef __HOLDOUT__
140                 if((ccl_fetch(sd, flag) & (SD_HOLDOUT|SD_HOLDOUT_MASK)) &&
141                    (state->flag & PATH_RAY_CAMERA))
142                 {
143                         if(kernel_data.background.transparent) {
144                                 float3 holdout_weight;
145
146                                 if(ccl_fetch(sd, flag) & SD_HOLDOUT_MASK)
147                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
148                                 else
149                                         holdout_weight = shader_holdout_eval(kg, sd);
150
151                                 /* any throughput is ok, should all be identical here */
152                                 L_transparent_coop[ray_index] += average(holdout_weight*throughput);
153                         }
154
155                         if(ccl_fetch(sd, flag) & SD_HOLDOUT_MASK) {
156                                 ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
157                                 *enqueue_flag = 1;
158                         }
159                 }
160 #endif  /* __HOLDOUT__ */
161         }
162
163         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
164                 PathRadiance *L = &PathRadiance_coop[ray_index];
165                 /* Holdout mask objects do not write data passes. */
166                 kernel_write_data_passes(kg,
167                                          per_sample_output_buffers,
168                                          L,
169                                          sd,
170                                          sample,
171                                          state,
172                                          throughput);
173                 /* Blurring of bsdf after bounces, for rays that have a small likelihood
174                  * of following this particular path (diffuse, rough glossy.
175                  */
176                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
177                         float blur_pdf = kernel_data.integrator.filter_glossy*state->min_ray_pdf;
178                         if(blur_pdf < 1.0f) {
179                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
180                                 shader_bsdf_blur(kg, sd, blur_roughness);
181                         }
182                 }
183
184 #ifdef __EMISSION__
185                 /* emission */
186                 if(ccl_fetch(sd, flag) & SD_EMISSION) {
187                         /* TODO(sergey): is isect.t wrong here for transparent surfaces? */
188                         float3 emission = indirect_primitive_emission(
189                                 kg,
190                                 sd,
191                                 Intersection_coop[ray_index].t,
192                                 state->flag,
193                                 state->ray_pdf);
194                         path_radiance_accum_emission(L, throughput, emission, state->bounce);
195                 }
196 #endif  /* __EMISSION__ */
197
198                 /* Path termination. this is a strange place to put the termination, it's
199                  * mainly due to the mixed in MIS that we use. gives too many unneeded
200                  * shader evaluations, only need emission if we are going to terminate.
201                  */
202                 float probability = path_state_terminate_probability(kg, state, throughput);
203
204                 if(probability == 0.0f) {
205                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
206                         *enqueue_flag = 1;
207                 }
208
209                 if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
210                         if(probability != 1.0f) {
211                                 float terminate = path_state_rng_1D_for_decision(kg, rng, state, PRNG_TERMINATE);
212                                 if(terminate >= probability) {
213                                         ASSIGN_RAY_STATE(ray_state, ray_index, RAY_UPDATE_BUFFER);
214                                         *enqueue_flag = 1;
215                                 } else {
216                                         throughput_coop[ray_index] = throughput/probability;
217                                 }
218                         }
219                 }
220         }
221
222 #ifdef __AO__
223         if(IS_STATE(ray_state, ray_index, RAY_ACTIVE)) {
224                 /* ambient occlusion */
225                 if(kernel_data.integrator.use_ambient_occlusion ||
226                    (ccl_fetch(sd, flag) & SD_AO))
227                 {
228                         /* todo: solve correlation */
229                         float bsdf_u, bsdf_v;
230                         path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
231
232                         float ao_factor = kernel_data.background.ao_factor;
233                         float3 ao_N;
234                         AOBSDF_coop[ray_index] = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
235                         AOAlpha_coop[ray_index] = shader_bsdf_alpha(kg, sd);
236
237                         float3 ao_D;
238                         float ao_pdf;
239                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
240
241                         if(dot(ccl_fetch(sd, Ng), ao_D) > 0.0f && ao_pdf != 0.0f) {
242                                 Ray _ray;
243                                 _ray.P = ray_offset(ccl_fetch(sd, P), ccl_fetch(sd, Ng));
244                                 _ray.D = ao_D;
245                                 _ray.t = kernel_data.background.ao_distance;
246 #ifdef __OBJECT_MOTION__
247                                 _ray.time = ccl_fetch(sd, time);
248 #endif
249                                 _ray.dP = ccl_fetch(sd, dP);
250                                 _ray.dD = differential3_zero();
251                                 AOLightRay_coop[ray_index] = _ray;
252
253                                 ADD_RAY_FLAG(ray_state, ray_index, RAY_SHADOW_RAY_CAST_AO);
254                                 *enqueue_flag_AO_SHADOW_RAY_CAST = 1;
255                         }
256                 }
257         }
258 #endif  /* __AO__ */
259 }