Code cleanup: abstract shadow catcher logic more into accumulation code.
[blender.git] / intern / cycles / kernel / kernel_path_branched.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 #ifdef __BRANCHED_PATH__
20
21 ccl_device_inline void kernel_branched_path_ao(KernelGlobals *kg,
22                                                ShaderData *sd,
23                                                ShaderData *emission_sd,
24                                                PathRadiance *L,
25                                                ccl_addr_space PathState *state,
26                                                float3 throughput)
27 {
28         int num_samples = kernel_data.integrator.ao_samples;
29         float num_samples_inv = 1.0f/num_samples;
30         float ao_factor = kernel_data.background.ao_factor;
31         float3 ao_N;
32         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
33         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
34
35         for(int j = 0; j < num_samples; j++) {
36                 float bsdf_u, bsdf_v;
37                 path_branched_rng_2D(kg, state->rng_hash, state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
38
39                 float3 ao_D;
40                 float ao_pdf;
41
42                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
43
44                 if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
45                         Ray light_ray;
46                         float3 ao_shadow;
47
48                         light_ray.P = ray_offset(sd->P, sd->Ng);
49                         light_ray.D = ao_D;
50                         light_ray.t = kernel_data.background.ao_distance;
51                         light_ray.time = sd->time;
52                         light_ray.dP = sd->dP;
53                         light_ray.dD = differential3_zero();
54
55                         if(!shadow_blocked(kg, sd, emission_sd, state, &light_ray, &ao_shadow)) {
56                                 path_radiance_accum_ao(L, state, throughput*num_samples_inv, ao_alpha, ao_bsdf, ao_shadow);
57                         }
58                         else {
59                                 path_radiance_accum_total_ao(L, state, throughput*num_samples_inv, ao_bsdf);
60                         }
61                 }
62         }
63 }
64
65 #ifndef __SPLIT_KERNEL__
66
67 /* bounce off surface and integrate indirect light */
68 ccl_device_noinline void kernel_branched_path_surface_indirect_light(KernelGlobals *kg,
69         ShaderData *sd, ShaderData *indirect_sd, ShaderData *emission_sd,
70         float3 throughput, float num_samples_adjust, PathState *state, PathRadiance *L)
71 {
72         float sum_sample_weight = 0.0f;
73 #ifdef __DENOISING_FEATURES__
74         if(state->denoising_feature_weight > 0.0f) {
75                 for(int i = 0; i < sd->num_closure; i++) {
76                         const ShaderClosure *sc = &sd->closure[i];
77
78                         /* transparency is not handled here, but in outer loop */
79                         if(!CLOSURE_IS_BSDF(sc->type) || CLOSURE_IS_BSDF_TRANSPARENT(sc->type)) {
80                                 continue;
81                         }
82
83                         sum_sample_weight += sc->sample_weight;
84                 }
85         }
86         else {
87                 sum_sample_weight = 1.0f;
88         }
89 #endif  /* __DENOISING_FEATURES__ */
90
91         for(int i = 0; i < sd->num_closure; i++) {
92                 const ShaderClosure *sc = &sd->closure[i];
93
94                 /* transparency is not handled here, but in outer loop */
95                 if(!CLOSURE_IS_BSDF(sc->type) || CLOSURE_IS_BSDF_TRANSPARENT(sc->type)) {
96                         continue;
97                 }
98
99                 int num_samples;
100
101                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
102                         num_samples = kernel_data.integrator.diffuse_samples;
103                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
104                         num_samples = 1;
105                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
106                         num_samples = kernel_data.integrator.glossy_samples;
107                 else
108                         num_samples = kernel_data.integrator.transmission_samples;
109
110                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
111
112                 float num_samples_inv = num_samples_adjust/num_samples;
113
114                 for(int j = 0; j < num_samples; j++) {
115                         PathState ps = *state;
116                         float3 tp = throughput;
117                         Ray bsdf_ray;
118 #ifdef __SHADOW_TRICKS__
119                         float shadow_transparency = L->shadow_transparency;
120 #endif
121
122                         ps.rng_hash = cmj_hash(state->rng_hash, i);
123
124                         if(!kernel_branched_path_surface_bounce(kg,
125                                                                 sd,
126                                                                 sc,
127                                                                 j,
128                                                                 num_samples,
129                                                                 &tp,
130                                                                 &ps,
131                                                                 L,
132                                                                 &bsdf_ray,
133                                                                 sum_sample_weight))
134                         {
135                                 continue;
136                         }
137
138                         ps.rng_hash = state->rng_hash;
139
140                         kernel_path_indirect(kg,
141                                              indirect_sd,
142                                              emission_sd,
143                                              &bsdf_ray,
144                                              tp*num_samples_inv,
145                                              num_samples,
146                                              &ps,
147                                              L);
148
149                         /* for render passes, sum and reset indirect light pass variables
150                          * for the next samples */
151                         path_radiance_sum_indirect(L);
152                         path_radiance_reset_indirect(L);
153
154 #ifdef __SHADOW_TRICKS__
155                         L->shadow_transparency = shadow_transparency;
156 #endif
157                 }
158         }
159 }
160
161 #ifdef __SUBSURFACE__
162 ccl_device void kernel_branched_path_subsurface_scatter(KernelGlobals *kg,
163                                                         ShaderData *sd,
164                                                         ShaderData *indirect_sd,
165                                                         ShaderData *emission_sd,
166                                                         PathRadiance *L,
167                                                         PathState *state,
168                                                         Ray *ray,
169                                                         float3 throughput)
170 {
171         for(int i = 0; i < sd->num_closure; i++) {
172                 ShaderClosure *sc = &sd->closure[i];
173
174                 if(!CLOSURE_IS_BSSRDF(sc->type))
175                         continue;
176
177                 /* set up random number generator */
178                 uint lcg_state = lcg_state_init(state, 0x68bc21eb);
179                 int num_samples = kernel_data.integrator.subsurface_samples;
180                 float num_samples_inv = 1.0f/num_samples;
181                 uint bssrdf_rng_hash = cmj_hash(state->rng_hash, i);
182
183                 /* do subsurface scatter step with copy of shader data, this will
184                  * replace the BSSRDF with a diffuse BSDF closure */
185                 for(int j = 0; j < num_samples; j++) {
186                         SubsurfaceIntersection ss_isect;
187                         float bssrdf_u, bssrdf_v;
188                         path_branched_rng_2D(kg, bssrdf_rng_hash, state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
189                         int num_hits = subsurface_scatter_multi_intersect(kg,
190                                                                           &ss_isect,
191                                                                           sd,
192                                                                           sc,
193                                                                           &lcg_state,
194                                                                           bssrdf_u, bssrdf_v,
195                                                                           true);
196 #ifdef __VOLUME__
197                         Ray volume_ray = *ray;
198                         bool need_update_volume_stack =
199                                 kernel_data.integrator.use_volumes &&
200                                 sd->object_flag & SD_OBJECT_INTERSECTS_VOLUME;
201 #endif  /* __VOLUME__ */
202
203                         /* compute lighting with the BSDF closure */
204                         for(int hit = 0; hit < num_hits; hit++) {
205                                 ShaderData bssrdf_sd = *sd;
206                                 subsurface_scatter_multi_setup(kg,
207                                                                &ss_isect,
208                                                                hit,
209                                                                &bssrdf_sd,
210                                                                state,
211                                                                state->flag,
212                                                                sc,
213                                                                true);
214
215                                 PathState hit_state = *state;
216
217                                 path_state_branch(&hit_state, j, num_samples);
218
219 #ifdef __VOLUME__
220                                 if(need_update_volume_stack) {
221                                         /* Setup ray from previous surface point to the new one. */
222                                         float3 P = ray_offset(bssrdf_sd.P, -bssrdf_sd.Ng);
223                                         volume_ray.D = normalize_len(P - volume_ray.P,
224                                                                      &volume_ray.t);
225
226                                         kernel_volume_stack_update_for_subsurface(
227                                             kg,
228                                             emission_sd,
229                                             &volume_ray,
230                                             hit_state.volume_stack);
231                                 }
232 #endif  /* __VOLUME__ */
233
234 #ifdef __EMISSION__
235                                 /* direct light */
236                                 if(kernel_data.integrator.use_direct_light) {
237                                         int all = (kernel_data.integrator.sample_all_lights_direct) ||
238                                                   (state->flag & PATH_RAY_SHADOW_CATCHER);
239                                         kernel_branched_path_surface_connect_light(
240                                                 kg,
241                                                 &bssrdf_sd,
242                                                 emission_sd,
243                                                 &hit_state,
244                                                 throughput,
245                                                 num_samples_inv,
246                                                 L,
247                                                 all);
248                                 }
249 #endif  /* __EMISSION__ */
250
251                                 /* indirect light */
252                                 kernel_branched_path_surface_indirect_light(
253                                         kg,
254                                         &bssrdf_sd,
255                                         indirect_sd,
256                                         emission_sd,
257                                         throughput,
258                                         num_samples_inv,
259                                         &hit_state,
260                                         L);
261                         }
262                 }
263         }
264 }
265 #endif  /* __SUBSURFACE__ */
266
267 ccl_device void kernel_branched_path_integrate(KernelGlobals *kg,
268                                                uint rng_hash,
269                                                int sample,
270                                                Ray ray,
271                                                ccl_global float *buffer,
272                                                PathRadiance *L)
273 {
274         /* initialize */
275         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
276
277         path_radiance_init(L, kernel_data.film.use_light_pass);
278
279         /* shader data memory used for both volumes and surfaces, saves stack space */
280         ShaderData sd;
281         /* shader data used by emission, shadows, volume stacks, indirect path */
282         ShaderData emission_sd, indirect_sd;
283
284         PathState state;
285         path_state_init(kg, &emission_sd, &state, rng_hash, sample, &ray);
286
287         /* Main Loop
288          * Here we only handle transparency intersections from the camera ray.
289          * Indirect bounces are handled in kernel_branched_path_surface_indirect_light().
290          */
291         for(;;) {
292                 /* Find intersection with objects in scene. */
293                 Intersection isect;
294                 bool hit = kernel_path_scene_intersect(kg, &state, &ray, &isect, L);
295
296 #ifdef __VOLUME__
297                 /* Sanitize volume stack. */
298                 if(!hit) {
299                         kernel_volume_clean_stack(kg, state.volume_stack);
300                 }
301                 /* volume attenuation, emission, scatter */
302                 if(state.volume_stack[0].shader != SHADER_NONE) {
303                         Ray volume_ray = ray;
304                         volume_ray.t = (hit)? isect.t: FLT_MAX;
305                         
306                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
307
308 #ifdef __VOLUME_DECOUPLED__
309                         /* decoupled ray marching only supported on CPU */
310
311                         /* cache steps along volume for repeated sampling */
312                         VolumeSegment volume_segment;
313
314                         shader_setup_from_volume(kg, &sd, &volume_ray);
315                         kernel_volume_decoupled_record(kg, &state,
316                                 &volume_ray, &sd, &volume_segment, heterogeneous);
317
318                         /* direct light sampling */
319                         if(volume_segment.closure_flag & SD_SCATTER) {
320                                 volume_segment.sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
321
322                                 int all = kernel_data.integrator.sample_all_lights_direct;
323
324                                 kernel_branched_path_volume_connect_light(kg, &sd,
325                                         &emission_sd, throughput, &state, L, all,
326                                         &volume_ray, &volume_segment);
327
328                                 /* indirect light sampling */
329                                 int num_samples = kernel_data.integrator.volume_samples;
330                                 float num_samples_inv = 1.0f/num_samples;
331
332                                 for(int j = 0; j < num_samples; j++) {
333                                         PathState ps = state;
334                                         Ray pray = ray;
335                                         float3 tp = throughput;
336
337                                         /* branch RNG state */
338                                         path_state_branch(&ps, j, num_samples);
339
340                                         /* scatter sample. if we use distance sampling and take just one
341                                          * sample for direct and indirect light, we could share this
342                                          * computation, but makes code a bit complex */
343                                         float rphase = path_state_rng_1D_for_decision(kg, &ps, PRNG_PHASE);
344                                         float rscatter = path_state_rng_1D_for_decision(kg, &ps, PRNG_SCATTER_DISTANCE);
345
346                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
347                                                 &ps, &pray, &sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
348
349                                         if(result == VOLUME_PATH_SCATTERED &&
350                                            kernel_path_volume_bounce(kg,
351                                                                      &sd,
352                                                                      &tp,
353                                                                      &ps,
354                                                                      L,
355                                                                      &pray))
356                                         {
357                                                 kernel_path_indirect(kg,
358                                                                      &indirect_sd,
359                                                                      &emission_sd,
360                                                                      &pray,
361                                                                      tp*num_samples_inv,
362                                                                      num_samples,
363                                                                      &ps,
364                                                                      L);
365
366                                                 /* for render passes, sum and reset indirect light pass variables
367                                                  * for the next samples */
368                                                 path_radiance_sum_indirect(L);
369                                                 path_radiance_reset_indirect(L);
370                                         }
371                                 }
372                         }
373
374                         /* emission and transmittance */
375                         if(volume_segment.closure_flag & SD_EMISSION)
376                                 path_radiance_accum_emission(L, &state, throughput, volume_segment.accum_emission);
377                         throughput *= volume_segment.accum_transmittance;
378
379                         /* free cached steps */
380                         kernel_volume_decoupled_free(kg, &volume_segment);
381 #else
382                         /* GPU: no decoupled ray marching, scatter probalistically */
383                         int num_samples = kernel_data.integrator.volume_samples;
384                         float num_samples_inv = 1.0f/num_samples;
385
386                         /* todo: we should cache the shader evaluations from stepping
387                          * through the volume, for now we redo them multiple times */
388
389                         for(int j = 0; j < num_samples; j++) {
390                                 PathState ps = state;
391                                 Ray pray = ray;
392                                 float3 tp = throughput * num_samples_inv;
393
394                                 /* branch RNG state */
395                                 path_state_branch(&ps, j, num_samples);
396
397                                 VolumeIntegrateResult result = kernel_volume_integrate(
398                                         kg, &ps, &sd, &volume_ray, L, &tp, heterogeneous);
399
400 #ifdef __VOLUME_SCATTER__
401                                 if(result == VOLUME_PATH_SCATTERED) {
402                                         /* todo: support equiangular, MIS and all light sampling.
403                                          * alternatively get decoupled ray marching working on the GPU */
404                                         kernel_path_volume_connect_light(kg, &sd, &emission_sd, tp, &state, L);
405
406                                         if(kernel_path_volume_bounce(kg,
407                                                                      &sd,
408                                                                      &tp,
409                                                                      &ps,
410                                                                      L,
411                                                                      &pray))
412                                         {
413                                                 kernel_path_indirect(kg,
414                                                                      &indirect_sd,
415                                                                      &emission_sd,
416                                                                      &pray,
417                                                                      tp,
418                                                                      num_samples,
419                                                                      &ps,
420                                                                      L);
421
422                                                 /* for render passes, sum and reset indirect light pass variables
423                                                  * for the next samples */
424                                                 path_radiance_sum_indirect(L);
425                                                 path_radiance_reset_indirect(L);
426                                         }
427                                 }
428 #endif  /* __VOLUME_SCATTER__ */
429                         }
430
431                         /* todo: avoid this calculation using decoupled ray marching */
432                         kernel_volume_shadow(kg, &emission_sd, &state, &volume_ray, &throughput);
433 #endif  /* __VOLUME_DECOUPLED__ */
434                 }
435 #endif  /* __VOLUME__ */
436
437                 /* Shade background. */
438                 if(!hit) {
439                         kernel_path_background(kg, &state, &ray, throughput, &emission_sd, L);
440                         break;
441                 }
442
443                 /* Setup and evaluate shader. */
444                 shader_setup_from_ray(kg, &sd, &isect, &ray);
445                 shader_eval_surface(kg, &sd, &state, 0.0f, state.flag);
446                 shader_merge_closures(&sd);
447
448                 /* Apply shadow catcher, holdout, emission. */
449                 if(!kernel_path_shader_apply(kg,
450                                              &sd,
451                                              &state,
452                                              &ray,
453                                              throughput,
454                                              &emission_sd,
455                                              L,
456                                              buffer))
457                 {
458                         break;
459                 }
460
461                 /* transparency termination */
462                 if(state.flag & PATH_RAY_TRANSPARENT) {
463                         /* path termination. this is a strange place to put the termination, it's
464                          * mainly due to the mixed in MIS that we use. gives too many unneeded
465                          * shader evaluations, only need emission if we are going to terminate */
466                         float probability = path_state_continuation_probability(kg, &state, throughput);
467
468                         if(probability == 0.0f) {
469                                 break;
470                         }
471                         else if(probability != 1.0f) {
472                                 float terminate = path_state_rng_1D_for_decision(kg, &state, PRNG_TERMINATE);
473
474                                 if(terminate >= probability)
475                                         break;
476
477                                 throughput /= probability;
478                         }
479                 }
480
481                 kernel_update_denoising_features(kg, &sd, &state, L);
482
483 #ifdef __AO__
484                 /* ambient occlusion */
485                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
486                         kernel_branched_path_ao(kg, &sd, &emission_sd, L, &state, throughput);
487                 }
488 #endif  /* __AO__ */
489
490 #ifdef __SUBSURFACE__
491                 /* bssrdf scatter to a different location on the same object */
492                 if(sd.flag & SD_BSSRDF) {
493                         kernel_branched_path_subsurface_scatter(kg, &sd, &indirect_sd, &emission_sd,
494                                                                 L, &state, &ray, throughput);
495                 }
496 #endif  /* __SUBSURFACE__ */
497
498                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
499                         PathState hit_state = state;
500
501 #ifdef __EMISSION__
502                         /* direct light */
503                         if(kernel_data.integrator.use_direct_light) {
504                                 int all = (kernel_data.integrator.sample_all_lights_direct) ||
505                                           (state.flag & PATH_RAY_SHADOW_CATCHER);
506                                 kernel_branched_path_surface_connect_light(kg,
507                                         &sd, &emission_sd, &hit_state, throughput, 1.0f, L, all);
508                         }
509 #endif  /* __EMISSION__ */
510
511                         /* indirect light */
512                         kernel_branched_path_surface_indirect_light(kg,
513                                 &sd, &indirect_sd, &emission_sd, throughput, 1.0f, &hit_state, L);
514
515                         /* continue in case of transparency */
516                         throughput *= shader_bsdf_transparency(kg, &sd);
517
518                         if(is_zero(throughput))
519                                 break;
520                 }
521
522                 /* Update Path State */
523                 state.flag |= PATH_RAY_TRANSPARENT;
524                 state.transparent_bounce++;
525
526                 ray.P = ray_offset(sd.P, -sd.Ng);
527                 ray.t -= sd.ray_length; /* clipping works through transparent */
528
529
530 #ifdef __RAY_DIFFERENTIALS__
531                 ray.dP = sd.dP;
532                 ray.dD.dx = -sd.dI.dx;
533                 ray.dD.dy = -sd.dI.dy;
534 #endif  /* __RAY_DIFFERENTIALS__ */
535
536 #ifdef __VOLUME__
537                 /* enter/exit volume */
538                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
539 #endif  /* __VOLUME__ */
540         }
541 }
542
543 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
544         ccl_global float *buffer, ccl_global uint *rng_state,
545         int sample, int x, int y, int offset, int stride)
546 {
547         /* buffer offset */
548         int index = offset + x + y*stride;
549         int pass_stride = kernel_data.film.pass_stride;
550
551         rng_state += index;
552         buffer += index*pass_stride;
553
554         /* initialize random numbers and ray */
555         uint rng_hash;
556         Ray ray;
557
558         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng_hash, &ray);
559
560         /* integrate */
561         PathRadiance L;
562
563         if(ray.t != 0.0f) {
564                 kernel_branched_path_integrate(kg, rng_hash, sample, ray, buffer, &L);
565                 kernel_write_result(kg, buffer, sample, &L);
566         }
567         else {
568                 kernel_write_result(kg, buffer, sample, NULL);
569         }
570 }
571
572 #endif  /* __SPLIT_KERNEL__ */
573
574 #endif  /* __BRANCHED_PATH__ */
575
576 CCL_NAMESPACE_END