Code refactor: Split __VOLUME__ defines in Cycles.
[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                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
93
94 #ifdef __VOLUME_DECOUPLED__
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);
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                         int sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
480
481 #ifdef __VOLUME_DECOUPLED__
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);
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 #ifdef __VOLUME_DECOUPLED__
820                         /* decoupled ray marching only supported on CPU */
821                         bool heterogeneous = volume_stack_is_heterogeneous(kg, state.volume_stack);
822
823                         /* cache steps along volume for repeated sampling */
824                         VolumeSegment volume_segment;
825                         ShaderData volume_sd;
826
827                         shader_setup_from_volume(kg, &volume_sd, &volume_ray, state.bounce, state.transparent_bounce);
828                         kernel_volume_decoupled_record(kg, &state,
829                                 &volume_ray, &volume_sd, &volume_segment, heterogeneous);
830
831                         /* direct light sampling */
832                         if(volume_segment.closure_flag & SD_SCATTER) {
833                                 volume_segment.sampling_method = volume_stack_sampling_method(kg, state.volume_stack);
834
835                                 bool all = kernel_data.integrator.sample_all_lights_direct;
836
837                                 kernel_branched_path_volume_connect_light(kg, rng, &volume_sd,
838                                         throughput, &state, &L, 1.0f, all, &volume_ray, &volume_segment);
839
840                                 /* indirect light sampling */
841                                 int num_samples = kernel_data.integrator.volume_samples;
842                                 float num_samples_inv = 1.0f/num_samples;
843
844                                 for(int j = 0; j < num_samples; j++) {
845                                         /* workaround to fix correlation bug in T38710, can find better solution
846                                          * in random number generator later, for now this is done here to not impact
847                                          * performance of rendering without volumes */
848                                         RNG tmp_rng = cmj_hash(*rng, state.rng_offset);
849
850                                         PathState ps = state;
851                                         Ray pray = ray;
852                                         float3 tp = throughput;
853
854                                         /* branch RNG state */
855                                         path_state_branch(&ps, j, num_samples);
856
857                                         /* scatter sample. if we use distance sampling and take just one
858                                          * sample for direct and indirect light, we could share this
859                                          * computation, but makes code a bit complex */
860                                         float rphase = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_PHASE);
861                                         float rscatter = path_state_rng_1D_for_decision(kg, &tmp_rng, &ps, PRNG_SCATTER_DISTANCE);
862
863                                         VolumeIntegrateResult result = kernel_volume_decoupled_scatter(kg,
864                                                 &ps, &pray, &volume_sd, &tp, rphase, rscatter, &volume_segment, NULL, false);
865                                                 
866                                         kernel_assert(result == VOLUME_PATH_SCATTERED);
867
868                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray, num_samples_inv)) {
869                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
870
871                                                 /* for render passes, sum and reset indirect light pass variables
872                                                  * for the next samples */
873                                                 path_radiance_sum_indirect(&L);
874                                                 path_radiance_reset_indirect(&L);
875                                         }
876                                 }
877                         }
878
879                         /* emission and transmittance */
880                         if(volume_segment.closure_flag & SD_EMISSION)
881                                 path_radiance_accum_emission(&L, throughput, volume_segment.accum_emission, state.bounce);
882                         throughput *= volume_segment.accum_transmittance;
883
884                         /* free cached steps */
885                         kernel_volume_decoupled_free(kg, &volume_segment);
886 #else
887                         /* GPU: no decoupled ray marching, scatter probalistically */
888                         int num_samples = kernel_data.integrator.volume_samples;
889                         float num_samples_inv = 1.0f/num_samples;
890
891                         /* todo: we should cache the shader evaluations from stepping
892                          * through the volume, for now we redo them multiple times */
893
894                         for(int j = 0; j < num_samples; j++) {
895                                 PathState ps = state;
896                                 Ray pray = ray;
897                                 ShaderData volume_sd;
898                                 float3 tp = throughput;
899
900                                 /* branch RNG state */
901                                 path_state_branch(&ps, j, num_samples);
902
903                                 VolumeIntegrateResult result = kernel_volume_integrate(
904                                         kg, &ps, &volume_sd, &volume_ray, &L, &tp, rng);
905                                 
906 #ifdef __VOLUME_SCATTER__
907                                 if(result == VOLUME_PATH_SCATTERED) {
908                                         /* todo: support equiangular, MIS and all light sampling.
909                                          * alternatively get decoupled ray marching working on the GPU */
910                                         kernel_path_volume_connect_light(kg, rng, &volume_sd, throughput, &state, &L, num_samples_inv);
911
912                                         if(kernel_path_volume_bounce(kg, rng, &volume_sd, &tp, &ps, &L, &pray, num_samples_inv)) {
913                                                 kernel_path_indirect(kg, rng, pray, tp*num_samples_inv, num_samples, ps, &L);
914
915                                                 /* for render passes, sum and reset indirect light pass variables
916                                                  * for the next samples */
917                                                 path_radiance_sum_indirect(&L);
918                                                 path_radiance_reset_indirect(&L);
919                                         }
920                                 }
921 #endif
922                         }
923
924                         /* todo: avoid this calculation using decoupled ray marching */
925                         kernel_volume_shadow(kg, &state, &volume_ray, &throughput);
926 #endif
927                 }
928 #endif
929
930                 if(!hit) {
931                         /* eval background shader if nothing hit */
932                         if(kernel_data.background.transparent) {
933                                 L_transparent += average(throughput);
934
935 #ifdef __PASSES__
936                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
937 #endif
938                                         break;
939                         }
940
941 #ifdef __BACKGROUND__
942                         /* sample background shader */
943                         float3 L_background = indirect_background(kg, &state, &ray);
944                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
945 #endif
946
947                         break;
948                 }
949
950                 /* setup shading */
951                 ShaderData sd;
952                 shader_setup_from_ray(kg, &sd, &isect, &ray, state.bounce, state.transparent_bounce);
953                 shader_eval_surface(kg, &sd, 0.0f, state.flag, SHADER_CONTEXT_MAIN);
954                 shader_merge_closures(&sd);
955
956                 /* holdout */
957 #ifdef __HOLDOUT__
958                 if(sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) {
959                         if(kernel_data.background.transparent) {
960                                 float3 holdout_weight;
961                                 
962                                 if(sd.flag & SD_HOLDOUT_MASK)
963                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
964                                 else
965                                         holdout_weight = shader_holdout_eval(kg, &sd);
966
967                                 /* any throughput is ok, should all be identical here */
968                                 L_transparent += average(holdout_weight*throughput);
969                         }
970
971                         if(sd.flag & SD_HOLDOUT_MASK)
972                                 break;
973                 }
974 #endif
975
976                 /* holdout mask objects do not write data passes */
977                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, &state, throughput);
978
979 #ifdef __EMISSION__
980                 /* emission */
981                 if(sd.flag & SD_EMISSION) {
982                         float3 emission = indirect_primitive_emission(kg, &sd, isect.t, state.flag, state.ray_pdf);
983                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
984                 }
985 #endif
986
987                 /* transparency termination */
988                 if(state.flag & PATH_RAY_TRANSPARENT) {
989                         /* path termination. this is a strange place to put the termination, it's
990                          * mainly due to the mixed in MIS that we use. gives too many unneeded
991                          * shader evaluations, only need emission if we are going to terminate */
992                         float probability = path_state_terminate_probability(kg, &state, throughput);
993
994                         if(probability == 0.0f) {
995                                 break;
996                         }
997                         else if(probability != 1.0f) {
998                                 float terminate = path_state_rng_1D_for_decision(kg, rng, &state, PRNG_TERMINATE);
999
1000                                 if(terminate >= probability)
1001                                         break;
1002
1003                                 throughput /= probability;
1004                         }
1005                 }
1006
1007 #ifdef __AO__
1008                 /* ambient occlusion */
1009                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
1010                         kernel_branched_path_ao(kg, &sd, &L, &state, rng, throughput);
1011                 }
1012 #endif
1013
1014 #ifdef __SUBSURFACE__
1015                 /* bssrdf scatter to a different location on the same object */
1016                 if(sd.flag & SD_BSSRDF) {
1017                         kernel_branched_path_subsurface_scatter(kg, &sd, &L, &state, rng, throughput);
1018                 }
1019 #endif
1020
1021                 if(!(sd.flag & SD_HAS_ONLY_VOLUME)) {
1022                         PathState hit_state = state;
1023
1024 #ifdef __EMISSION__
1025                         /* direct light */
1026                         if(kernel_data.integrator.use_direct_light) {
1027                                 bool all = kernel_data.integrator.sample_all_lights_direct;
1028                                 kernel_branched_path_surface_connect_light(kg, rng,
1029                                         &sd, &hit_state, throughput, 1.0f, &L, all);
1030                         }
1031 #endif
1032
1033                         /* indirect light */
1034                         kernel_branched_path_surface_indirect_light(kg, rng,
1035                                 &sd, throughput, 1.0f, &hit_state, &L);
1036
1037                         /* continue in case of transparency */
1038                         throughput *= shader_bsdf_transparency(kg, &sd);
1039
1040                         if(is_zero(throughput))
1041                                 break;
1042                 }
1043
1044                 path_state_next(kg, &state, LABEL_TRANSPARENT);
1045                 ray.P = ray_offset(sd.P, -sd.Ng);
1046                 ray.t -= sd.ray_length; /* clipping works through transparent */
1047
1048 #ifdef __VOLUME__
1049                 /* enter/exit volume */
1050                 kernel_volume_stack_enter_exit(kg, &sd, state.volume_stack);
1051 #endif
1052         }
1053
1054         float3 L_sum = path_radiance_clamp_and_sum(kg, &L);
1055
1056         kernel_write_light_passes(kg, buffer, &L, sample);
1057
1058         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
1059 }
1060
1061 #endif
1062
1063 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)
1064 {
1065         float filter_u;
1066         float filter_v;
1067
1068         int num_samples = kernel_data.integrator.aa_samples;
1069
1070         path_rng_init(kg, rng_state, sample, num_samples, rng, x, y, &filter_u, &filter_v);
1071
1072         /* sample camera ray */
1073
1074         float lens_u = 0.0f, lens_v = 0.0f;
1075
1076         if(kernel_data.cam.aperturesize > 0.0f)
1077                 path_rng_2D(kg, rng, sample, num_samples, PRNG_LENS_U, &lens_u, &lens_v);
1078
1079         float time = 0.0f;
1080
1081 #ifdef __CAMERA_MOTION__
1082         if(kernel_data.cam.shuttertime != -1.0f)
1083                 time = path_rng_1D(kg, rng, sample, num_samples, PRNG_TIME);
1084 #endif
1085
1086         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, ray);
1087 }
1088
1089 ccl_device void kernel_path_trace(KernelGlobals *kg,
1090         ccl_global float *buffer, ccl_global uint *rng_state,
1091         int sample, int x, int y, int offset, int stride)
1092 {
1093         /* buffer offset */
1094         int index = offset + x + y*stride;
1095         int pass_stride = kernel_data.film.pass_stride;
1096
1097         rng_state += index;
1098         buffer += index*pass_stride;
1099
1100         /* initialize random numbers and ray */
1101         RNG rng;
1102         Ray ray;
1103
1104         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1105
1106         /* integrate */
1107         float4 L;
1108
1109         if(ray.t != 0.0f)
1110                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
1111         else
1112                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1113
1114         /* accumulate result in output buffer */
1115         kernel_write_pass_float4(buffer, sample, L);
1116
1117         path_rng_end(kg, rng_state, rng);
1118 }
1119
1120 #ifdef __BRANCHED_PATH__
1121 ccl_device void kernel_branched_path_trace(KernelGlobals *kg,
1122         ccl_global float *buffer, ccl_global uint *rng_state,
1123         int sample, int x, int y, int offset, int stride)
1124 {
1125         /* buffer offset */
1126         int index = offset + x + y*stride;
1127         int pass_stride = kernel_data.film.pass_stride;
1128
1129         rng_state += index;
1130         buffer += index*pass_stride;
1131
1132         /* initialize random numbers and ray */
1133         RNG rng;
1134         Ray ray;
1135
1136         kernel_path_trace_setup(kg, rng_state, sample, x, y, &rng, &ray);
1137
1138         /* integrate */
1139         float4 L;
1140
1141         if(ray.t != 0.0f)
1142                 L = kernel_branched_path_integrate(kg, &rng, sample, ray, buffer);
1143         else
1144                 L = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
1145
1146         /* accumulate result in output buffer */
1147         kernel_write_pass_float4(buffer, sample, L);
1148
1149         path_rng_end(kg, rng_state, rng);
1150 }
1151 #endif
1152
1153 CCL_NAMESPACE_END
1154