dcb577e176f93ae770433971f9611c82358a7125
[blender-staging.git] / intern / cycles / kernel / kernel_path_surface.h
1 /*
2  * Copyright 2011-2013 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 #if defined(__BRANCHED_PATH__) || defined(__SUBSURFACE__) || defined(__SHADOW_TRICKS__) || defined(__BAKING__)
20 /* branched path tracing: connect path directly to position on one or more lights and add it to L */
21 ccl_device_noinline void kernel_branched_path_surface_connect_light(
22         KernelGlobals *kg,
23         RNG *rng,
24         ShaderData *sd,
25         ShaderData *emission_sd,
26         ccl_addr_space PathState *state,
27         float3 throughput,
28         float num_samples_adjust,
29         PathRadiance *L,
30         int sample_all_lights)
31 {
32 #ifdef __EMISSION__
33         /* sample illumination from lights to find path contribution */
34         if(!(sd->flag & SD_BSDF_HAS_EVAL))
35                 return;
36
37         Ray light_ray;
38         BsdfEval L_light;
39         bool is_lamp;
40
41 #  ifdef __OBJECT_MOTION__
42         light_ray.time = sd->time;
43 #  endif
44
45         if(sample_all_lights) {
46                 /* lamp sampling */
47                 for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
48                         if(UNLIKELY(light_select_reached_max_bounces(kg, i, state->bounce)))
49                                 continue;
50
51                         int num_samples = ceil_to_int(num_samples_adjust*light_select_num_samples(kg, i));
52                         float num_samples_inv = num_samples_adjust/(num_samples*kernel_data.integrator.num_all_lights);
53                         RNG lamp_rng = cmj_hash(*rng, i);
54
55                         for(int j = 0; j < num_samples; j++) {
56                                 float light_u, light_v;
57                                 path_branched_rng_2D(kg, &lamp_rng, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
58                                 float terminate = path_branched_rng_light_termination(kg, &lamp_rng, state, j, num_samples);
59
60                                 LightSample ls;
61                                 if(lamp_light_sample(kg, i, light_u, light_v, sd->P, &ls)) {
62                                         /* The sampling probability returned by lamp_light_sample assumes that all lights were sampled.
63                                          * However, this code only samples lamps, so if the scene also had mesh lights, the real probability is twice as high. */
64                                         if(kernel_data.integrator.pdf_triangles != 0.0f)
65                                                 ls.pdf *= 2.0f;
66
67                                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
68                                                 /* trace shadow ray */
69                                                 float3 shadow;
70
71                                                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
72                                                         /* accumulate */
73                                                         path_radiance_accum_light(L, state, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
74                                                 }
75                                                 else {
76                                                         path_radiance_accum_total_light(L, state, throughput*num_samples_inv, &L_light);
77                                                 }
78                                         }
79                                 }
80                         }
81                 }
82
83                 /* mesh light sampling */
84                 if(kernel_data.integrator.pdf_triangles != 0.0f) {
85                         int num_samples = ceil_to_int(num_samples_adjust*kernel_data.integrator.mesh_light_samples);
86                         float num_samples_inv = num_samples_adjust/num_samples;
87
88                         for(int j = 0; j < num_samples; j++) {
89                                 float light_t = path_branched_rng_1D(kg, rng, state, j, num_samples, PRNG_LIGHT);
90                                 float light_u, light_v;
91                                 path_branched_rng_2D(kg, rng, state, j, num_samples, PRNG_LIGHT_U, &light_u, &light_v);
92                                 float terminate = path_branched_rng_light_termination(kg, rng, state, j, num_samples);
93
94                                 /* only sample triangle lights */
95                                 if(kernel_data.integrator.num_all_lights)
96                                         light_t = 0.5f*light_t;
97
98                                 LightSample ls;
99                                 if(light_sample(kg, light_t, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
100                                         /* Same as above, probability needs to be corrected since the sampling was forced to select a mesh light. */
101                                         if(kernel_data.integrator.num_all_lights)
102                                                 ls.pdf *= 2.0f;
103
104                                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
105                                                 /* trace shadow ray */
106                                                 float3 shadow;
107
108                                                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
109                                                         /* accumulate */
110                                                         path_radiance_accum_light(L, state, throughput*num_samples_inv, &L_light, shadow, num_samples_inv, is_lamp);
111                                                 }
112                                                 else {
113                                                         path_radiance_accum_total_light(L, state, throughput*num_samples_inv, &L_light);
114                                                 }
115                                         }
116                                 }
117                         }
118                 }
119         }
120         else {
121                 /* sample one light at random */
122                 float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
123                 float light_u, light_v;
124                 path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
125                 float terminate = path_state_rng_light_termination(kg, rng, state);
126
127                 LightSample ls;
128                 if(light_sample(kg, light_t, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
129                         /* sample random light */
130                         if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
131                                 /* trace shadow ray */
132                                 float3 shadow;
133
134                                 if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
135                                         /* accumulate */
136                                         path_radiance_accum_light(L, state, throughput*num_samples_adjust, &L_light, shadow, num_samples_adjust, is_lamp);
137                                 }
138                                 else {
139                                         path_radiance_accum_total_light(L, state, throughput*num_samples_adjust, &L_light);
140                                 }
141                         }
142                 }
143         }
144 #endif
145 }
146
147 /* branched path tracing: bounce off or through surface to with new direction stored in ray */
148 ccl_device bool kernel_branched_path_surface_bounce(
149         KernelGlobals *kg,
150         RNG *rng,
151         ShaderData *sd,
152         const ShaderClosure *sc,
153         int sample,
154         int num_samples,
155         ccl_addr_space float3 *throughput,
156         ccl_addr_space PathState *state,
157         PathRadiance *L,
158         ccl_addr_space Ray *ray,
159         float sum_sample_weight)
160 {
161         /* sample BSDF */
162         float bsdf_pdf;
163         BsdfEval bsdf_eval;
164         float3 bsdf_omega_in;
165         differential3 bsdf_domega_in;
166         float bsdf_u, bsdf_v;
167         path_branched_rng_2D(kg, rng, state, sample, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
168         int label;
169
170         label = shader_bsdf_sample_closure(kg, sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
171                 &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
172
173         if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
174                 return false;
175
176         /* modify throughput */
177         path_radiance_bsdf_bounce(L, throughput, &bsdf_eval, bsdf_pdf, state->bounce, label);
178
179 #ifdef __DENOISING_FEATURES__
180         state->denoising_feature_weight *= sc->sample_weight / (sum_sample_weight * num_samples);
181 #endif
182
183         /* modify path state */
184         path_state_next(kg, state, label);
185
186         /* setup ray */
187         ray->P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
188         ray->D = normalize(bsdf_omega_in);
189         ray->t = FLT_MAX;
190 #ifdef __RAY_DIFFERENTIALS__
191         ray->dP = sd->dP;
192         ray->dD = bsdf_domega_in;
193 #endif
194 #ifdef __OBJECT_MOTION__
195         ray->time = sd->time;
196 #endif
197
198 #ifdef __VOLUME__
199         /* enter/exit volume */
200         if(label & LABEL_TRANSMIT)
201                 kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
202 #endif
203
204         /* branch RNG state */
205         path_state_branch(state, sample, num_samples);
206
207         /* set MIS state */
208         state->min_ray_pdf = fminf(bsdf_pdf, FLT_MAX);
209         state->ray_pdf = bsdf_pdf;
210 #ifdef __LAMP_MIS__
211         state->ray_t = 0.0f;
212 #endif
213
214         return true;
215 }
216
217 #endif
218
219 /* path tracing: connect path directly to position on a light and add it to L */
220 ccl_device_inline void kernel_path_surface_connect_light(KernelGlobals *kg, RNG *rng,
221         ShaderData *sd, ShaderData *emission_sd, float3 throughput, ccl_addr_space PathState *state,
222         PathRadiance *L)
223 {
224 #ifdef __EMISSION__
225         if(!(kernel_data.integrator.use_direct_light && (sd->flag & SD_BSDF_HAS_EVAL)))
226                 return;
227
228 #ifdef __SHADOW_TRICKS__
229         if(state->flag & PATH_RAY_SHADOW_CATCHER) {
230                 kernel_branched_path_surface_connect_light(kg,
231                                                            rng,
232                                                            sd,
233                                                            emission_sd,
234                                                            state,
235                                                            throughput,
236                                                            1.0f,
237                                                            L,
238                                                            1);
239                 return;
240         }
241 #endif
242
243         /* sample illumination from lights to find path contribution */
244         float light_t = path_state_rng_1D(kg, rng, state, PRNG_LIGHT);
245         float light_u, light_v;
246         path_state_rng_2D(kg, rng, state, PRNG_LIGHT_U, &light_u, &light_v);
247
248         Ray light_ray;
249         BsdfEval L_light;
250         bool is_lamp;
251
252 #ifdef __OBJECT_MOTION__
253         light_ray.time = sd->time;
254 #endif
255
256         LightSample ls;
257         if(light_sample(kg, light_t, light_u, light_v, sd->time, sd->P, state->bounce, &ls)) {
258                 float terminate = path_state_rng_light_termination(kg, rng, state);
259                 if(direct_emission(kg, sd, emission_sd, &ls, state, &light_ray, &L_light, &is_lamp, terminate)) {
260                         /* trace shadow ray */
261                         float3 shadow;
262
263                         if(!shadow_blocked(kg, emission_sd, state, &light_ray, &shadow)) {
264                                 /* accumulate */
265                                 path_radiance_accum_light(L, state, throughput, &L_light, shadow, 1.0f, is_lamp);
266                         }
267                         else {
268                                 path_radiance_accum_total_light(L, state, throughput, &L_light);
269                         }
270                 }
271         }
272 #endif
273 }
274
275 /* path tracing: bounce off or through surface to with new direction stored in ray */
276 ccl_device bool kernel_path_surface_bounce(KernelGlobals *kg,
277                                            RNG *rng,
278                                            ShaderData *sd,
279                                            ccl_addr_space float3 *throughput,
280                                            ccl_addr_space PathState *state,
281                                            PathRadiance *L,
282                                            ccl_addr_space Ray *ray)
283 {
284         /* no BSDF? we can stop here */
285         if(sd->flag & SD_BSDF) {
286                 /* sample BSDF */
287                 float bsdf_pdf;
288                 BsdfEval bsdf_eval;
289                 float3 bsdf_omega_in;
290                 differential3 bsdf_domega_in;
291                 float bsdf_u, bsdf_v;
292                 path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
293                 int label;
294
295                 label = shader_bsdf_sample(kg, sd, bsdf_u, bsdf_v, &bsdf_eval,
296                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
297
298                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
299                         return false;
300
301                 /* modify throughput */
302                 path_radiance_bsdf_bounce(L, throughput, &bsdf_eval, bsdf_pdf, state->bounce, label);
303
304                 /* set labels */
305                 if(!(label & LABEL_TRANSPARENT)) {
306                         state->ray_pdf = bsdf_pdf;
307 #ifdef __LAMP_MIS__
308                         state->ray_t = 0.0f;
309 #endif
310                         state->min_ray_pdf = fminf(bsdf_pdf, state->min_ray_pdf);
311                 }
312
313                 /* update path state */
314                 path_state_next(kg, state, label);
315
316                 /* setup ray */
317                 ray->P = ray_offset(sd->P, (label & LABEL_TRANSMIT)? -sd->Ng: sd->Ng);
318                 ray->D = normalize(bsdf_omega_in);
319
320                 if(state->bounce == 0)
321                         ray->t -= sd->ray_length; /* clipping works through transparent */
322                 else
323                         ray->t = FLT_MAX;
324
325 #ifdef __RAY_DIFFERENTIALS__
326                 ray->dP = sd->dP;
327                 ray->dD = bsdf_domega_in;
328 #endif
329
330 #ifdef __VOLUME__
331                 /* enter/exit volume */
332                 if(label & LABEL_TRANSMIT)
333                         kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
334 #endif
335                 return true;
336         }
337 #ifdef __VOLUME__
338         else if(sd->flag & SD_HAS_ONLY_VOLUME) {
339                 /* no surface shader but have a volume shader? act transparent */
340
341                 /* update path state, count as transparent */
342                 path_state_next(kg, state, LABEL_TRANSPARENT);
343
344                 if(state->bounce == 0)
345                         ray->t -= sd->ray_length; /* clipping works through transparent */
346                 else
347                         ray->t = FLT_MAX;
348
349                 /* setup ray position, direction stays unchanged */
350                 ray->P = ray_offset(sd->P, -sd->Ng);
351 #ifdef __RAY_DIFFERENTIALS__
352                 ray->dP = sd->dP;
353 #endif
354
355                 /* enter/exit volume */
356                 kernel_volume_stack_enter_exit(kg, sd, state->volume_stack);
357                 return true;
358         }
359 #endif
360         else {
361                 /* no bsdf or volume? */
362                 return false;
363         }
364 }
365
366 CCL_NAMESPACE_END
367