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