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