Cycles: Avoid redundant call to volume_stack_is_heterogeneous() for Distance Sampling.
[blender-staging.git] / intern / cycles / kernel / kernel_path.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 #ifdef __OSL__
18 #include "osl_shader.h"
19 #endif
20
21 #include "kernel_random.h"
22 #include "kernel_projection.h"
23 #include "kernel_montecarlo.h"
24 #include "kernel_differential.h"
25 #include "kernel_camera.h"
26
27 #include "geom/geom.h"
28
29 #include "kernel_accumulate.h"
30 #include "kernel_shader.h"
31 #include "kernel_light.h"
32 #include "kernel_passes.h"
33
34 #ifdef __SUBSURFACE__
35 #include "kernel_subsurface.h"
36 #endif
37
38 #ifdef __VOLUME__
39 #include "kernel_volume.h"
40 #endif
41
42 #include "kernel_path_state.h"
43 #include "kernel_shadow.h"
44 #include "kernel_emission.h"
45 #include "kernel_path_surface.h"
46 #include "kernel_path_volume.h"
47
48 CCL_NAMESPACE_BEGIN
49
50 ccl_device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, Ray ray,
51         float3 throughput, int num_samples, PathState state, PathRadiance *L)
52 {
53         /* path iteration */
54         for(;;) {
55                 /* intersect scene */
56                 Intersection isect;
57                 uint visibility = path_state_ray_visibility(kg, &state);
58 #ifdef __HAIR__
59                 bool hit = scene_intersect(kg, &ray, visibility, &isect, NULL, 0.0f, 0.0f);
60 #else
61                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
62 #endif
63
64 #ifdef __LAMP_MIS__
65                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
66                         /* ray starting from previous non-transparent bounce */
67                         Ray light_ray;
68
69                         light_ray.P = ray.P - state.ray_t*ray.D;
70                         state.ray_t += isect.t;
71                         light_ray.D = ray.D;
72                         light_ray.t = state.ray_t;
73                         light_ray.time = ray.time;
74                         light_ray.dD = ray.dD;
75                         light_ray.dP = ray.dP;
76
77                         /* intersect with lamp */
78                         float3 emission;
79
80                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
81                                 path_radiance_accum_emission(L, throughput, emission, state.bounce);
82                 }
83 #endif
84
85 #ifdef __VOLUME__
86                 /* volume attenuation, emission, scatter */
87                 if(state.volume_stack[0].shader != SHADER_NONE) {
88                         Ray volume_ray = ray;
89                         volume_ray.t = (hit)? isect.t: FLT_MAX;
90
91                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
92
93 #ifdef __VOLUME_DECOUPLED__
94                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
95                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, false, sampling_method);
96
97                         if(decoupled) {
98                                 /* cache steps along volume for repeated sampling */
99                                 VolumeSegment volume_segment;
100                                 ShaderData volume_sd;
101
102                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
103                                 kernel_volume_decoupled_record(kg, &state,
104                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
105                                 
106                                 volume_segment.sampling_method = sampling_method;
107
108                                 /* emission */
109                                 if(volume_segment.closure_flag & SD_EMISSION)
110                                         path_radiance_accum_emission(L, throughput, volume_segment.accum_emission, state.bounce);
111
112                                 /* scattering */
113                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
114
115                                 if(volume_segment.closure_flag & SD_SCATTER) {
116                                         bool all = kernel_data.integrator.sample_all_lights_indirect;
117
118                                         /* direct light sampling */
119                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
120                                                 throughput, &state, L, 1.0f, all, &volume_ray, &volume_segment);
121
122                                         /* indirect sample. if we use distance sampling and take just
123                                          * one sample for direct and indirect light, we could share
124                                          * this computation, but makes code a bit complex */
125                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
126                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
127
128                                         result = kernel_volume_decoupled_scatter(kg,
129                                                 &state, &volume_ray, &volume_sd, &throughput,
130                                                 rphase, rscatter, &volume_segment, NULL, true);
131                                 }
132
133                                 if(result != VOLUME_PATH_SCATTERED)
134                                         throughput *= volume_segment.accum_transmittance;
135
136                                 /* free cached steps */
137                                 kernel_volume_decoupled_free(kg, &volume_segment);
138
139                                 if(result == VOLUME_PATH_SCATTERED) {
140                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, L, &ray, 1.0f))
141                                                 continue;
142                                         else
143                                                 break;
144                                 }
145                         }
146                         else
147 #endif
148                         {
149                                 /* integrate along volume segment with distance sampling */
150                                 ShaderData volume_sd;
151                                 VolumeIntegrateResult result = kernel_volume_integrate(
152                                         kg, &state, &volume_sd, &volume_ray, L, &throughput, rng, heterogeneous);
153
154 #ifdef __VOLUME_SCATTER__
155                                 if(result == VOLUME_PATH_SCATTERED) {
156                                         /* direct lighting */
157                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, L, 1.0f);
158
159                                         /* indirect light bounce */
160                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, L, &ray, 1.0f))
161                                                 continue;
162                                         else
163                                                 break;
164                                 }
165 #endif
166                         }
167                 }
168 #endif
169
170                 if(!hit) {
171 #ifdef __BACKGROUND__
172                         /* sample background shader */
173                         float3 L_background = indirect_background(kg, &state, &ray);
174                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
175 #endif
176
177                         break;
178                 }
179
180                 /* setup shading */
181                 ShaderData sd;
182                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
183                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
184                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_INDIRECT);
185 #ifdef __BRANCHED_PATH__
186                 shader_merge_closures(&sd);
187 #endif
188
189                 /* blurring of bsdf after bounces, for rays that have a small likelihood
190                  * of following this particular path (diffuse, rough glossy) */
191                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
192                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
193
194                         if(blur_pdf < 1.0f) {
195                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
196                                 shader_bsdf_blur(kg, &sd, blur_roughness);
197                         }
198                 }
199
200 #ifdef __EMISSION__
201                 /* emission */
202                 if(sd.flag & SD_EMISSION) {
203                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
204                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
205                 }
206 #endif
207
208                 /* path termination. this is a strange place to put the termination, it's
209                  * mainly due to the mixed in MIS that we use. gives too many unneeded
210                  * shader evaluations, only need emission if we are going to terminate */
211                 float probability = path_state_terminate_probability(kg, &state, throughput*num_samples);
212
213                 if(probability == 0.0f) {
214                         break;
215                 }
216                 else if(probability != 1.0f) {
217                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
218
219                         if(terminate >= probability)
220                                 break;
221
222                         throughput /= probability;
223                 }
224
225 #ifdef __AO__
226                 /* ambient occlusion */
227                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
228                         float bsdf_u, bsdf_v;
229                         path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
230
231                         float ao_factor = kernel_data.background.ao_factor;
232                         float3 ao_N;
233                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
234                         float3 ao_D;
235                         float ao_pdf;
236                         float3 ao_alpha = make_float3(0.0f, 0.0f, 0.0f);
237
238                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
239
240                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
241                                 Ray light_ray;
242                                 float3 ao_shadow;
243
244                                 light_ray.P = ray_offset(sd.P, sd.Ng);
245                                 light_ray.D = ao_D;
246                                 light_ray.t = kernel_data.background.ao_distance;
247 #ifdef __OBJECT_MOTION__
248                                 light_ray.time = sd.time;
249 #endif
250                                 light_ray.dP = sd.dP;
251                                 light_ray.dD = differential3_zero();
252
253                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
254                                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state.bounce);
255                         }
256                 }
257 #endif
258
259 #ifdef __SUBSURFACE__
260                 /* bssrdf scatter to a different location on the same object, replacing
261                  * the closures with a diffuse BSDF */
262                 if(sd.flag & SD_BSSRDF) {
263                         float bssrdf_probability;
264                         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, &sd, &bssrdf_probability);
265
266                         /* modify throughput for picking bssrdf or bsdf */
267                         throughput *= bssrdf_probability;
268
269                         /* do bssrdf scatter step if we picked a bssrdf closure */
270                         if(sc) {
271                                 uint lcg_state = lcg_state_init(rng, &state, 0x68bc21eb);
272
273                                 float bssrdf_u, bssrdf_v;
274                                 path_state_rng_2D(kg, rng, &state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
275                                 subsurface_scatter_step(kg, &sd, state.flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
276
277                                 state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
278                         }
279                 }
280 #endif
281
282 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
283                 if(kernel_data.integrator.use_direct_light) {
284                         bool all = kernel_data.integrator.sample_all_lights_indirect;
285                         kernel_branched_path_surface_connect_light(kg, rng, &sd, &state, throughput, 1.0f, L, all);
286                 }
287 #endif
288
289                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, L, &ray))
290                         break;
291         }
292 }
293
294 ccl_device void kernel_path_ao(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
295 {
296         /* todo: solve correlation */
297         float bsdf_u, bsdf_v;
298
299         path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
300
301         float ao_factor = kernel_data.background.ao_factor;
302         float3 ao_N;
303         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
304         float3 ao_D;
305         float ao_pdf;
306         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
307
308         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
309
310         if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
311                 Ray light_ray;
312                 float3 ao_shadow;
313
314                 light_ray.P = ray_offset(sd->P, sd->Ng);
315                 light_ray.D = ao_D;
316                 light_ray.t = kernel_data.background.ao_distance;
317 #ifdef __OBJECT_MOTION__
318                 light_ray.time = sd->time;
319 #endif
320                 light_ray.dP = sd->dP;
321                 light_ray.dD = differential3_zero();
322
323                 if(!shadow_blocked(kg, state, &light_ray, &ao_shadow))
324                         path_radiance_accum_ao(L, throughput, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
325         }
326 }
327
328 ccl_device void kernel_branched_path_ao(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
329 {
330         int num_samples = kernel_data.integrator.ao_samples;
331         float num_samples_inv = 1.0f/num_samples;
332         float ao_factor = kernel_data.background.ao_factor;
333         float3 ao_N;
334         float3 ao_bsdf = shader_bsdf_ao(kg, sd, ao_factor, &ao_N);
335         float3 ao_alpha = shader_bsdf_alpha(kg, sd);
336
337         for(int j = 0; j < num_samples; j++) {
338                 float bsdf_u, bsdf_v;
339                 path_branched_rng_2D(kg, rng, state, j, num_samples, PRNG_BSDF_U, &bsdf_u, &bsdf_v);
340
341                 float3 ao_D;
342                 float ao_pdf;
343
344                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
345
346                 if(dot(sd->Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
347                         Ray light_ray;
348                         float3 ao_shadow;
349
350                         light_ray.P = ray_offset(sd->P, sd->Ng);
351                         light_ray.D = ao_D;
352                         light_ray.t = kernel_data.background.ao_distance;
353 #ifdef __OBJECT_MOTION__
354                         light_ray.time = sd->time;
355 #endif
356                         light_ray.dP = sd->dP;
357                         light_ray.dD = differential3_zero();
358
359                         if(!shadow_blocked(kg, state, &light_ray, &ao_shadow))
360                                 path_radiance_accum_ao(L, throughput*num_samples_inv, ao_alpha, ao_bsdf, ao_shadow, state->bounce);
361                 }
362         }
363 }
364
365 #ifdef __SUBSURFACE__
366 ccl_device bool kernel_path_subsurface_scatter(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, Ray *ray, float3 *throughput)
367 {
368         float bssrdf_probability;
369         ShaderClosure *sc = subsurface_scatter_pick_closure(kg, sd, &bssrdf_probability);
370
371         /* modify throughput for picking bssrdf or bsdf */
372         *throughput *= bssrdf_probability;
373
374         /* do bssrdf scatter step if we picked a bssrdf closure */
375         if(sc) {
376                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
377
378                 ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
379                 float bssrdf_u, bssrdf_v;
380                 path_state_rng_2D(kg, rng, state, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
381                 int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, false);
382
383                 /* compute lighting with the BSDF closure */
384                 for(int hit = 0; hit < num_hits; hit++) {
385                         float3 tp = *throughput;
386                         PathState hit_state = *state;
387                         Ray hit_ray = *ray;
388
389                         hit_state.flag |= PATH_RAY_BSSRDF_ANCESTOR;
390                         hit_state.rng_offset += PRNG_BOUNCE_NUM;
391                         
392                         kernel_path_surface_connect_light(kg, rng, &bssrdf_sd[hit], tp, state, L);
393
394                         if(kernel_path_surface_bounce(kg, rng, &bssrdf_sd[hit], &tp, &hit_state, L, &hit_ray)) {
395 #ifdef __LAMP_MIS__
396                                 hit_state.ray_t = 0.0f;
397 #endif
398
399                                 kernel_path_indirect(kg, rng, hit_ray, tp, state->num_samples, hit_state, L);
400
401                                 /* for render passes, sum and reset indirect light pass variables
402                                  * for the next samples */
403                                 path_radiance_sum_indirect(L);
404                                 path_radiance_reset_indirect(L);
405                         }
406                 }
407                 return true;
408         }
409         return false;
410 }
411 #endif
412
413 ccl_device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
414 {
415         /* initialize */
416         PathRadiance L;
417         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
418         float L_transparent = 0.0f;
419
420         path_radiance_init(&L, kernel_data.film.use_light_pass);
421
422         PathState state;
423         path_state_init(kg, &state, rng, sample);
424
425         /* path iteration */
426         for(;;) {
427                 /* intersect scene */
428                 Intersection isect;
429                 uint visibility = path_state_ray_visibility(kg, &state);
430
431 #ifdef __HAIR__
432                 float difl = 0.0f, extmax = 0.0f;
433                 uint lcg_state = 0;
434
435                 if(kernel_data.bvh.have_curves) {
436                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
437                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
438                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
439                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
440                         }
441
442                         extmax = kernel_data.curve.maximum_width;
443                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
444                 }
445
446                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
447 #else
448                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
449 #endif
450
451 #ifdef __LAMP_MIS__
452                 if(kernel_data.integrator.use_lamp_mis && !(state.flag & PATH_RAY_CAMERA)) {
453                         /* ray starting from previous non-transparent bounce */
454                         Ray light_ray;
455
456                         light_ray.P = ray.P - state.ray_t*ray.D;
457                         state.ray_t += isect.t;
458                         light_ray.D = ray.D;
459                         light_ray.t = state.ray_t;
460                         light_ray.time = ray.time;
461                         light_ray.dD = ray.dD;
462                         light_ray.dP = ray.dP;
463
464                         /* intersect with lamp */
465                         float3 emission;
466
467                         if(indirect_lamp_emission(kg, &state, &light_ray, &emission))
468                                 path_radiance_accum_emission(&L, throughput, emission, state.bounce);
469                 }
470 #endif
471
472 #ifdef __VOLUME__
473                 /* volume attenuation, emission, scatter */
474                 if(state.volume_stack[0].shader != SHADER_NONE) {
475                         Ray volume_ray = ray;
476                         volume_ray.t = (hit)? isect.t: FLT_MAX;
477
478                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
479
480 #ifdef __VOLUME_DECOUPLED__
481                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
482                         bool decoupled = kernel_volume_use_decoupled(kg, heterogeneous, true, sampling_method);
483
484                         if(decoupled) {
485                                 /* cache steps along volume for repeated sampling */
486                                 VolumeSegment volume_segment;
487                                 ShaderData volume_sd;
488
489                                 shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
490                                 kernel_volume_decoupled_record(kg, &state,
491                                         &volume_ray, &volume_sd, &volume_segment, heterogeneous);
492
493                                 volume_segment.sampling_method = sampling_method;
494
495                                 /* emission */
496                                 if(volume_segment.closure_flag & SD_EMISSION)
497                                         path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
498
499                                 /* scattering */
500                                 VolumeIntegrateResult result = VOLUME_PATH_ATTENUATED;
501
502                                 if(volume_segment.closure_flag & SD_SCATTER) {
503                                         bool all = false;
504
505                                         /* direct light sampling */
506                                         kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
507                                                 throughput, &state, &L, 1.0f, all, &volume_ray, &volume_segment);
508
509                                         /* indirect sample. if we use distance sampling and take just
510                                          * one sample for direct and indirect light, we could share
511                                          * this computation, but makes code a bit complex */
512                                         float rphase = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_PHASE);
513                                         float rscatter = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_SCATTER_DISTANCE);
514
515                                         result = kernel_volume_decoupled_scatter(kg,
516                                                 &state, &volume_ray, &volume_sd, &throughput,
517                                                 rphase, rscatter, &volume_segment, NULL, true);
518                                 }
519
520                                 if(result != VOLUME_PATH_SCATTERED)
521                                         throughput *= volume_segment.accum_transmittance;
522
523                                 /* free cached steps */
524                                 kernel_volume_decoupled_free(kg, &volume_segment);
525
526                                 if(result == VOLUME_PATH_SCATTERED) {
527                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray, 1.0f))
528                                                 continue;
529                                         else
530                                                 break;
531                                 }
532                         }
533                         else 
534 #endif
535                         {
536                                 /* integrate along volume segment with distance sampling */
537                                 ShaderData volume_sd;
538                                 VolumeIntegrateResult result = kernel_volume_integrate(
539                                         kg, &state, &volume_sd, &volume_ray, &L, &throughput, rng, heterogeneous);
540
541 #ifdef __VOLUME_SCATTER__
542                                 if(result == VOLUME_PATH_SCATTERED) {
543                                         /* direct lighting */
544                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, &L, 1.0f);
545
546                                         /* indirect light bounce */
547                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &throughput, &state, &L, &ray, 1.0f))
548                                                 continue;
549                                         else
550                                                 break;
551                                 }
552 #endif
553                         }
554                 }
555 #endif
556
557                 if(!hit) {
558                         /* eval background shader if nothing hit */
559                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
560                                 L_transparent += average(throughput);
561
562 #ifdef __PASSES__
563                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
564 #endif
565                                         break;
566                         }
567
568 #ifdef __BACKGROUND__
569                         /* sample background shader */
570                         float3 L_background = indirect_background(kg, &state, &ray);
571                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
572 #endif
573
574                         break;
575                 }
576
577                 /* setup shading */
578                 ShaderData sd;
579                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
580                 float rbsdf = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_BSDF);
581                 shader_eval_surface(kg, &sd, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
582
583                 /* holdout */
584 #ifdef __HOLDOUT__
585                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
586                         if(kernel_data.background.transparent) {
587                                 float3 holdout_weight;
588                                 
589                                 if(sd.flag & SD_HOLDOUT_MASK)
590                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
591                                 else
592                                         holdout_weight = shader_holdout_eval(kg, &sd);
593
594                                 /* any throughput is ok, should all be identical here */
595                                 L_transparent += average(holdout_weight*throughput);
596                         }
597
598                         if(sd.flag & SD_HOLDOUT_MASK)
599                                 break;
600                 }
601 #endif
602
603                 /* holdout mask objects do not write data passes */
604                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
605
606                 /* blurring of bsdf after bounces, for rays that have a small likelihood
607                  * of following this particular path (diffuse, rough glossy) */
608                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
609                         float blur_pdf = kernel_data.integrator.filter_glossy*state.min_ray_pdf;
610
611                         if(blur_pdf < 1.0f) {
612                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
613                                 shader_bsdf_blur(kg, &sd, blur_roughness);
614                         }
615                 }
616
617 #ifdef __EMISSION__
618                 /* emission */
619                 if(sd.flag & SD_EMISSION) {
620                         /* todo: is isect.t wrong here for transparent surfaces? */
621                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
622                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
623                 }
624 #endif
625
626                 /* path termination. this is a strange place to put the termination, it's
627                  * mainly due to the mixed in MIS that we use. gives too many unneeded
628                  * shader evaluations, only need emission if we are going to terminate */
629                 float probability = path_state_terminate_probability(kg, &state, throughput);
630
631                 if(probability == 0.0f) {
632                         break;
633                 }
634                 else if(probability != 1.0f) {
635                         float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
636
637                         if(terminate >= probability)
638                                 break;
639
640                         throughput /= probability;
641                 }
642
643 #ifdef __AO__
644                 /* ambient occlusion */
645                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
646                         kernel_path_ao(kg, &sd, &L, &state, rng, throughput);
647                 }
648 #endif
649
650 #ifdef __SUBSURFACE__
651                 /* bssrdf scatter to a different location on the same object, replacing
652                  * the closures with a diffuse BSDF */
653                 if(sd.flag & SD_BSSRDF) {
654                         if(kernel_path_subsurface_scatter(kg, &sd, &L, &state, rng, &ray, &throughput))
655                                 break;
656                 }
657 #endif
658
659                 /* direct lighting */
660                 kernel_path_surface_connect_light(kg, rng, &sd, throughput, &state, &L);
661
662                 /* compute direct lighting and next bounce */
663                 if(!kernel_path_surface_bounce(kg, rng, &sd, &throughput, &state, &L, &ray))
664                         break;
665         }
666
667         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
668
669         kernel_write_light_passes(kg, buffer, &L, sample);
670
671         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
672 }
673
674 #ifdef __BRANCHED_PATH__
675
676 /* branched path tracing: bounce off surface and integrate indirect light */
677 ccl_device_noinline void kernel_branched_path_surface_indirect_light(KernelGlobals *kg,
678         RNG *rng, ShaderData *sd, float3 throughput, float num_samples_adjust,
679         PathState *state, PathRadiance *L)
680 {
681         for(int i = 0; i< sd->num_closure; i++) {
682                 const ShaderClosure *sc = &sd->closure[i];
683
684                 if(!CLOSURE_IS_BSDF(sc->type))
685                         continue;
686                 /* transparency is not handled here, but in outer loop */
687                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
688                         continue;
689
690                 int num_samples;
691
692                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
693                         num_samples = kernel_data.integrator.diffuse_samples;
694                 else if(CLOSURE_IS_BSDF_BSSRDF(sc->type))
695                         num_samples = 1;
696                 else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
697                         num_samples = kernel_data.integrator.glossy_samples;
698                 else
699                         num_samples = kernel_data.integrator.transmission_samples;
700
701                 num_samples = ceil_to_int(num_samples_adjust*num_samples);
702
703                 float num_samples_inv = num_samples_adjust/num_samples;
704                 RNG bsdf_rng = cmj_hash(*rng, i);
705
706                 for(int j = 0; j < num_samples; j++) {
707                         PathState ps = *state;
708                         float3 tp = throughput;
709                         Ray bsdf_ray;
710
711                         if(!kernel_branched_path_surface_bounce(kg, &bsdf_rng, sd, sc, j, num_samples, &tp, &ps, L, &bsdf_ray))
712                                 continue;
713
714                         kernel_path_indirect(kg, rng, bsdf_ray, tp*num_samples_inv, num_samples, ps, L);
715
716                         /* for render passes, sum and reset indirect light pass variables
717                          * for the next samples */
718                         path_radiance_sum_indirect(L);
719                         path_radiance_reset_indirect(L);
720                 }
721         }
722 }
723
724 #ifdef __SUBSURFACE__
725 ccl_device void kernel_branched_path_subsurface_scatter(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, PathState *state, RNG *rng, float3 throughput)
726 {
727         for(int i = 0; i< sd->num_closure; i++) {
728                 ShaderClosure *sc = &sd->closure[i];
729
730                 if(!CLOSURE_IS_BSSRDF(sc->type))
731                         continue;
732
733                 /* set up random number generator */
734                 uint lcg_state = lcg_state_init(rng, state, 0x68bc21eb);
735                 int num_samples = kernel_data.integrator.subsurface_samples;
736                 float num_samples_inv = 1.0f/num_samples;
737                 RNG bssrdf_rng = cmj_hash(*rng, i);
738
739                 state->flag |= PATH_RAY_BSSRDF_ANCESTOR;
740
741                 /* do subsurface scatter step with copy of shader data, this will
742                  * replace the BSSRDF with a diffuse BSDF closure */
743                 for(int j = 0; j < num_samples; j++) {
744                         ShaderData bssrdf_sd[BSSRDF_MAX_HITS];
745                         float bssrdf_u, bssrdf_v;
746                         path_branched_rng_2D(kg, &bssrdf_rng, state, j, num_samples, PRNG_BSDF_U, &bssrdf_u, &bssrdf_v);
747                         int num_hits = subsurface_scatter_multi_step(kg, sd, bssrdf_sd, state->flag, sc, &lcg_state, bssrdf_u, bssrdf_v, true);
748
749                         /* compute lighting with the BSDF closure */
750                         for(int hit = 0; hit < num_hits; hit++) {
751                                 PathState hit_state = *state;
752
753                                 path_state_branch(&hit_state, j, num_samples);
754
755 #if defined(__EMISSION__) && defined(__BRANCHED_PATH__)
756                                 /* direct light */
757                                 if(kernel_data.integrator.use_direct_light) {
758                                         bool all = kernel_data.integrator.sample_all_lights_direct;
759                                         kernel_branched_path_surface_connect_light(kg, rng,
760                                                 &bssrdf_sd[hit], &hit_state, throughput, num_samples_inv, L, all);
761                                 }
762 #endif
763
764                                 /* indirect light */
765                                 kernel_branched_path_surface_indirect_light(kg, rng,
766                                         &bssrdf_sd[hit], throughput, num_samples_inv,
767                                         &hit_state, L);
768                         }
769                 }
770
771                 state->flag &= ~PATH_RAY_BSSRDF_ANCESTOR;
772         }
773 }
774 #endif
775
776 ccl_device float4 kernel_branched_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, ccl_global float *buffer)
777 {
778         /* initialize */
779         PathRadiance L;
780         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
781         float L_transparent = 0.0f;
782
783         path_radiance_init(&L, kernel_data.film.use_light_pass);
784
785         PathState state;
786         path_state_init(kg, &state, rng, sample);
787
788         for(;;) {
789                 /* intersect scene */
790                 Intersection isect;
791                 uint visibility = path_state_ray_visibility(kg, &state);
792
793 #ifdef __HAIR__
794                 float difl = 0.0f, extmax = 0.0f;
795                 uint lcg_state = 0;
796
797                 if(kernel_data.bvh.have_curves) {
798                         if((kernel_data.cam.resolution == 1) && (state.flag & PATH_RAY_CAMERA)) {       
799                                 float3 pixdiff = ray.dD.dx + ray.dD.dy;
800                                 /*pixdiff = pixdiff - dot(pixdiff, ray.D)*ray.D;*/
801                                 difl = kernel_data.curve.minimum_width * len(pixdiff) * 0.5f;
802                         }
803
804                         extmax = kernel_data.curve.maximum_width;
805                         lcg_state = lcg_state_init(rng, &state, 0x51633e2d);
806                 }
807
808                 bool hit = scene_intersect(kg, &ray, visibility, &isect, &lcg_state, difl, extmax);
809 #else
810                 bool hit = scene_intersect(kg, &ray, visibility, &isect);
811 #endif
812
813 #ifdef __VOLUME__
814                 /* volume attenuation, emission, scatter */
815                 if(state.volume_stack[0].shader != SHADER_NONE) {
816                         Ray volume_ray = ray;
817                         volume_ray.t = (hit)? isect.t: FLT_MAX;
818                         
819                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
820
821 #ifdef __VOLUME_DECOUPLED__
822                         /* decoupled ray marching only supported on CPU */
823
824                         /* cache steps along volume for repeated sampling */
825                         VolumeSegment volume_segment;
826                         ShaderData volume_sd;
827
828                         shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
829                         kernel_volume_decoupled_record(kg, &state,
830                                 &volume_ray, &volume_sd, &volume_segment, heterogeneous);
831
832                         /* direct light sampling */
833                         if(volume_segment.closure_flag & SD_SCATTER) {
834                                 volume_segment.sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
835
836                                 bool all = kernel_data.integrator.sample_all_lights_direct;
837
838                                 kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
839                                         throughput, &state, &L, 1.0f, all, &volume_ray, &volume_segment);
840
841                                 /* indirect light sampling */
842                                 int num_samples = kernel_data.integrator.volume_samples;
843                                 float num_samples_inv = 1.0f/num_samples;
844
845                                 for(int j = 0; j < num_samples; j++) {
846                                         /* workaround to fix correlation bug in T38710, can find better solution
847                                          * in random number generator later, for now this is done here to not impact
848                                          * performance of rendering without volumes */
849                                         RNG tmp_rng = cmj_hash(*rng, state.rng_offset);
850
851                                         PathState ps = state;
852                                         Ray pray = ray;
853                                         float3 tp = throughput;
854
855                                         /* branch RNG state */
856                                         path_state_branch(&ps, j, num_samples);
857
858                                         /* scatter sample. if we use distance sampling and take just one
859                                          * sample for direct and indirect light, we could share this
860                                          * computation, but makes code a bit complex */
861                                         float rphase = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_PHASE);
862                                         float rscatter = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_SCATTER_DISTANCE);
863
864                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
865                                                 &ps, &pray, &volume_sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
866                                                 
867                                         kernel_assert(result == VOLUME_PATH_SCATTERED);
868
869                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray, num_samples_inv)) {
870                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
871
872                                                 /* for render passes, sum and reset indirect light pass variables
873                                                  * for the next samples */
874                                                 path_radiance_sum_indirect(&L);
875                                                 path_radiance_reset_indirect(&L);
876                                         }
877                                 }
878                         }
879
880                         /* emission and transmittance */
881                         if(volume_segment.closure_flag & SD_EMISSION)
882                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
883                         throughput *= volume_segment.accum_transmittance;
884
885                         /* free cached steps */
886                         kernel_volume_decoupled_free(kg, &volume_segment);
887 #else
888                         /* GPU: no decoupled ray marching, scatter probalistically */
889                         int num_samples = kernel_data.integrator.volume_samples;
890                         float num_samples_inv = 1.0f/num_samples;
891
892                         /* todo: we should cache the shader evaluations from stepping
893                          * through the volume, for now we redo them multiple times */
894
895                         for(int j = 0; j < num_samples; j++) {
896                                 PathState ps = state;
897                                 Ray pray = ray;
898                                 ShaderData volume_sd;
899                                 float3 tp = throughput;
900
901                                 /* branch RNG state */
902                                 path_state_branch(&ps, j, num_samples);
903
904                                 VolumeIntegrateResult result = kernel_volume_integrate(
905                                         kg, &ps, &volume_sd, &volume_ray, &L, &tp, rng, heterogeneous);
906                                 
907 #ifdef __VOLUME_SCATTER__
908                                 if(result == VOLUME_PATH_SCATTERED) {
909                                         /* todo: support equiangular, MIS and all light sampling.
910                                          * alternatively get decoupled ray marching working on the GPU */
911                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, &L, num_samples_inv);
912
913                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray, num_samples_inv)) {
914                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
915
916                                                 /* for render passes, sum and reset indirect light pass variables
917                                                  * for the next samples */
918                                                 path_radiance_sum_indirect(&L);
919                                                 path_radiance_reset_indirect(&L);
920                                         }
921                                 }
922 #endif
923                         }
924
925                         /* todo: avoid this calculation using decoupled ray marching */
926                         kernel_volume_shadow(kg, &state, &volume_ray, &throughput);
927 #endif
928                 }
929 #endif
930
931                 if(!hit) {
932                         /* eval background shader if nothing hit */
933                         if(kernel_data.background.transparent) {
934                                 L_transparent += average(throughput);
935
936 #ifdef __PASSES__
937                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
938 #endif
939                                         break;
940                         }
941
942 #ifdef __BACKGROUND__
943                         /* sample background shader */
944                         float3 L_background = indirect_background(kg, &state, &ray);
945                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
946 #endif
947
948                         break;
949                 }
950
951                 /* setup shading */
952                 ShaderData sd;
953                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
954                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
955                 shader_merge_closures(&sd);
956
957                 /* holdout */
958 #ifdef __HOLDOUT__
959                 if(sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) {
960                         if(kernel_data.background.transparent) {
961                                 float3 holdout_weight;
962                                 
963                                 if(sd.flag & SD_HOLDOUT_MASK)
964                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
965                                 else
966                                         holdout_weight = shader_holdout_eval(kg, &sd);
967
968                                 /* any throughput is ok, should all be identical here */
969                                 L_transparent += average(holdout_weight*throughput);
970                         }
971
972                         if(sd.flag & SD_HOLDOUT_MASK)
973                                 break;
974                 }
975 #endif
976
977                 /* holdout mask objects do not write data passes */
978                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
979
980 #ifdef __EMISSION__
981                 /* emission */
982                 if(sd.flag & SD_EMISSION) {
983                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
984                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
985                 }
986 #endif
987
988                 /* transparency termination */
989                 if(state.flag & PATH_RAY_TRANSPARENT) {
990                         /* path termination. this is a strange place to put the termination, it's
991                          * mainly due to the mixed in MIS that we use. gives too many unneeded
992                          * shader evaluations, only need emission if we are going to terminate */
993                         float probability = path_state_terminate_probability(kg, &state, throughput);
994
995                         if(probability == 0.0f) {
996                                 break;
997                         }
998                         else if(probability != 1.0f) {
999                                 float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
1000
1001                                 if(terminate >= probability)
1002                                         break;
1003
1004                                 throughput /= probability;
1005                         }
1006                 }
1007
1008 #ifdef __AO__
1009                 /* ambient occlusion */
1010                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
1011                         kernel_branched_path_ao(kg, &sd, &L, &state, rng, throughput);
1012                 }
1013 #endif
1014
1015 #ifdef __SUBSURFACE__
1016                 /* bssrdf scatter to a different location on the same object */
1017                 if(sd.flag & SD_BSSRDF) {
1018                         kernel_branched_path_subsurface_scatter(kg, &sd, &L, &state, rng, throughput);
1019                 }
1020 #endif
1021
1022                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
1023                         PathState hit_state = state;
1024
1025 #ifdef __EMISSION__
1026                         /* direct light */
1027                         if(kernel_data.integrator.use_direct_light) {
1028                                 bool all = kernel_data.integrator.sample_all_lights_direct;
1029                                 kernel_branched_path_surface_connect_light(kg, rng,
1030                                         &sd, &hit_state, throughput, 1.0f, &L, all);
1031                         }
1032 #endif
1033
1034                         /* indirect light */
1035                         kernel_branched_path_surface_indirect_light(kg, rng,
1036                                 &sd, throughput, 1.0f, &hit_state, &L);
1037
1038                         /* continue in case of transparency */
1039                         throughput *= shader_bsdf_transparency(kg, &sd);
1040
1041                         if(is_zero(throughput))
1042                                 break;
1043                 }
1044
1045                 path_state_next(kg, &state, LABEL_TRANSPARENT);
1046                 ray.P = ray_offset(sd.P, -sd.Ng);
1047                 ray.t -= sd.ray_length; /* clipping works through transparent */
1048
1049 #ifdef __VOLUME__
1050                 /* enter/exit volume */
1051                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
1052 #endif
1053         }
1054
1055         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
1056
1057         kernel_write_light_passes(kg, buffer, &L, sample);
1058
1059         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1060 }
1061
1062 #endif
1063
1064 ccl_device_inline void kernel_path_trace_setup(KernelGlobals *kg, ccl_global uint *rng_state, int sample, int x, int y, RNG *rng, Ray *ray)
1065 {
1066         float filter_u;
1067         float filter_v;
1068
1069         int num_samples = kernel_data.integrator.aa_samples;
1070
1071         path_rng_init(kg, rng_state, sample, num_samples, rng, x, y, &filter_u, &filter_v);
1072
1073         /* sample camera ray */
1074
1075         float lens_u = 0.0f, lens_v = 0.0f;
1076
1077         if(kernel_data.cam.aperturesize > 0.0f)
1078                 path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1079
1080         float time = 0.0f;
1081
1082 #ifdef __CAMERA_MOTION__
1083         if(kernel_data.cam.shuttertime != -1.0f)
1084                 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1085 #endif
1086
1087         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, ray);
1088 }
1089
1090 ccl_device void kernel_path_trace(KernelGlobals *kg,
1091         ccl_global float *buffer, ccl_global uint *rng_state,
1092         int sample, int x, int y, int offset, int stride)
1093 {
1094         /* buffer offset */
1095         int index = offset + x + y*stride;
1096         int pass_stride = kernel_data.film.pass_stride;
1097
1098         rng_state += index;
1099         buffer += index*pass_stride;
1100
1101         /* initialize random numbers and ray */
1102         RNG rng;
1103         Ray ray;
1104
1105         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1106
1107         /* integrate */
1108         float4 L;
1109
1110         if(ray.t != 0.0f)
1111                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
1112         else
1113                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1114
1115         /* accumulate result in output buffer */
1116         kernel_write_pass_float4(buffer, sample, L);
1117
1118         path_rng_end(kg, rng_state, rng);
1119 }
1120
1121 #ifdef __BRANCHED_PATH__
1122 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
1123         ccl_global float *buffer, ccl_global uint *rng_state,
1124         int sample, int x, int y, int offset, int stride)
1125 {
1126         /* buffer offset */
1127         int index = offset + x + y*stride;
1128         int pass_stride = kernel_data.film.pass_stride;
1129
1130         rng_state += index;
1131         buffer += index*pass_stride;
1132
1133         /* initialize random numbers and ray */
1134         RNG rng;
1135         Ray ray;
1136
1137         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1138
1139         /* integrate */
1140         float4 L;
1141
1142         if(ray.t != 0.0f)
1143                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
1144         else
1145                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1146
1147         /* accumulate result in output buffer */
1148         kernel_write_pass_float4(buffer, sample, L);
1149
1150         path_rng_end(kg, rng_state, rng);
1151 }
1152 #endif
1153
1154 CCL_NAMESPACE_END
1155