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