Fix #33188: AO pass wrong after recent change.
[blender.git] / intern / cycles / kernel / kernel_path.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "kernel_differential.h"
20 #include "kernel_montecarlo.h"
21 #include "kernel_projection.h"
22 #include "kernel_object.h"
23 #include "kernel_triangle.h"
24 #ifdef __QBVH__
25 #include "kernel_qbvh.h"
26 #else
27 #include "kernel_bvh.h"
28 #endif
29 #include "kernel_accumulate.h"
30 #include "kernel_camera.h"
31 #include "kernel_shader.h"
32 #include "kernel_light.h"
33 #include "kernel_emission.h"
34 #include "kernel_random.h"
35 #include "kernel_passes.h"
36
37 CCL_NAMESPACE_BEGIN
38
39 typedef struct PathState {
40         uint flag;
41         int bounce;
42
43         int diffuse_bounce;
44         int glossy_bounce;
45         int transmission_bounce;
46         int transparent_bounce;
47 } PathState;
48
49 __device_inline void path_state_init(PathState *state)
50 {
51         state->flag = PATH_RAY_CAMERA|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
52         state->bounce = 0;
53         state->diffuse_bounce = 0;
54         state->glossy_bounce = 0;
55         state->transmission_bounce = 0;
56         state->transparent_bounce = 0;
57 }
58
59 __device_inline void path_state_next(KernelGlobals *kg, PathState *state, int label)
60 {
61         /* ray through transparent keeps same flags from previous ray and is
62          * not counted as a regular bounce, transparent has separate max */
63         if(label & LABEL_TRANSPARENT) {
64                 state->flag |= PATH_RAY_TRANSPARENT;
65                 state->transparent_bounce++;
66
67                 if(!kernel_data.integrator.transparent_shadows)
68                         state->flag |= PATH_RAY_MIS_SKIP;
69
70                 return;
71         }
72
73         state->bounce++;
74
75         /* reflection/transmission */
76         if(label & LABEL_REFLECT) {
77                 state->flag |= PATH_RAY_REFLECT;
78                 state->flag &= ~(PATH_RAY_TRANSMIT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
79
80                 if(label & LABEL_DIFFUSE)
81                         state->diffuse_bounce++;
82                 else
83                         state->glossy_bounce++;
84         }
85         else {
86                 kernel_assert(label & LABEL_TRANSMIT);
87
88                 state->flag |= PATH_RAY_TRANSMIT;
89                 state->flag &= ~(PATH_RAY_REFLECT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
90
91                 state->transmission_bounce++;
92         }
93
94         /* diffuse/glossy/singular */
95         if(label & LABEL_DIFFUSE) {
96                 state->flag |= PATH_RAY_DIFFUSE;
97                 state->flag &= ~(PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
98         }
99         else if(label & LABEL_GLOSSY) {
100                 state->flag |= PATH_RAY_GLOSSY;
101                 state->flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
102         }
103         else {
104                 kernel_assert(label & LABEL_SINGULAR);
105
106                 state->flag |= PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
107                 state->flag &= ~PATH_RAY_DIFFUSE;
108         }
109 }
110
111 __device_inline uint path_state_ray_visibility(KernelGlobals *kg, PathState *state)
112 {
113         uint flag = state->flag;
114
115         /* for visibility, diffuse/glossy are for reflection only */
116         if(flag & PATH_RAY_TRANSMIT)
117                 flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY);
118         /* for camera visibility, use render layer flags */
119         if(flag & PATH_RAY_CAMERA)
120                 flag |= kernel_data.integrator.layer_flag;
121
122         return flag;
123 }
124
125 __device_inline float path_state_terminate_probability(KernelGlobals *kg, PathState *state, const float3 throughput)
126 {
127         if(state->flag & PATH_RAY_TRANSPARENT) {
128                 /* transparent rays treated separately */
129                 if(state->transparent_bounce >= kernel_data.integrator.transparent_max_bounce)
130                         return 0.0f;
131                 else if(state->transparent_bounce <= kernel_data.integrator.transparent_min_bounce)
132                         return 1.0f;
133         }
134         else {
135                 /* other rays */
136                 if((state->bounce >= kernel_data.integrator.max_bounce) ||
137                    (state->diffuse_bounce >= kernel_data.integrator.max_diffuse_bounce) ||
138                    (state->glossy_bounce >= kernel_data.integrator.max_glossy_bounce) ||
139                    (state->transmission_bounce >= kernel_data.integrator.max_transmission_bounce))
140                 {
141                         return 0.0f;
142                 }
143                 else if(state->bounce <= kernel_data.integrator.min_bounce) {
144                         return 1.0f;
145                 }
146         }
147
148         /* probalistic termination */
149         return average(throughput);
150 }
151
152 __device_inline bool shadow_blocked(KernelGlobals *kg, PathState *state, Ray *ray, float3 *shadow)
153 {
154         *shadow = make_float3(1.0f, 1.0f, 1.0f);
155
156         if(ray->t == 0.0f)
157                 return false;
158         
159         Intersection isect;
160         bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect);
161
162 #ifdef __TRANSPARENT_SHADOWS__
163         if(result && kernel_data.integrator.transparent_shadows) {
164                 /* transparent shadows work in such a way to try to minimize overhead
165                  * in cases where we don't need them. after a regular shadow ray is
166                  * cast we check if the hit primitive was potentially transparent, and
167                  * only in that case start marching. this gives on extra ray cast for
168                  * the cases were we do want transparency.
169                  *
170                  * also note that for this to work correct, multi close sampling must
171                  * be used, since we don't pass a random number to shader_eval_surface */
172                 if(shader_transparent_shadow(kg, &isect)) {
173                         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
174                         float3 Pend = ray->P + ray->D*ray->t;
175                         int bounce = state->transparent_bounce;
176
177                         for(;;) {
178                                 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
179                                         return true;
180                                 }
181                                 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
182                                         /* todo: get random number somewhere for probabilistic terminate */
183 #if 0
184                                         float probability = average(throughput);
185                                         float terminate = 0.0f;
186
187                                         if(terminate >= probability)
188                                                 return true;
189
190                                         throughput /= probability;
191 #endif
192                                 }
193
194                                 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect)) {
195                                         *shadow *= throughput;
196                                         return false;
197                                 }
198
199                                 if(!shader_transparent_shadow(kg, &isect))
200                                         return true;
201
202                                 ShaderData sd;
203                                 shader_setup_from_ray(kg, &sd, &isect, ray);
204                                 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW);
205
206                                 throughput *= shader_bsdf_transparency(kg, &sd);
207
208                                 ray->P = ray_offset(sd.P, -sd.Ng);
209                                 if(ray->t != FLT_MAX)
210                                         ray->D = normalize_len(Pend - ray->P, &ray->t);
211
212                                 shader_release(kg, &sd);
213
214                                 bounce++;
215                         }
216                 }
217         }
218 #endif
219
220         return result;
221 }
222
223 __device float4 kernel_path_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
224 {
225         /* initialize */
226         PathRadiance L;
227         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
228         float L_transparent = 0.0f;
229
230         path_radiance_init(&L, kernel_data.film.use_light_pass);
231
232         float min_ray_pdf = FLT_MAX;
233         float ray_pdf = 0.0f;
234         PathState state;
235         int rng_offset = PRNG_BASE_NUM;
236
237         path_state_init(&state);
238
239         /* path iteration */
240         for(;; rng_offset += PRNG_BOUNCE_NUM) {
241                 /* intersect scene */
242                 Intersection isect;
243                 uint visibility = path_state_ray_visibility(kg, &state);
244
245                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
246                         /* eval background shader if nothing hit */
247                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
248                                 L_transparent += average(throughput);
249
250 #ifdef __PASSES__
251                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
252 #endif
253                                         break;
254                         }
255
256 #ifdef __BACKGROUND__
257                         /* sample background shader */
258                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
259                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
260 #endif
261
262                         break;
263                 }
264
265                 /* setup shading */
266                 ShaderData sd;
267                 shader_setup_from_ray(kg, &sd, &isect, &ray);
268                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
269                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
270
271                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
272
273                 /* blurring of bsdf after bounces, for rays that have a small likelihood
274                  * of following this particular path (diffuse, rough glossy) */
275                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
276                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
277
278                         if(blur_pdf < 1.0f) {
279                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
280                                 shader_bsdf_blur(kg, &sd, blur_roughness);
281                         }
282                 }
283
284                 /* holdout */
285 #ifdef __HOLDOUT__
286                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
287                         if(kernel_data.background.transparent) {
288                                 float3 holdout_weight;
289                                 
290                                 if(sd.flag & SD_HOLDOUT_MASK)
291                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
292                                 else
293                                         holdout_weight = shader_holdout_eval(kg, &sd);
294
295                                 /* any throughput is ok, should all be identical here */
296                                 L_transparent += average(holdout_weight*throughput);
297                         }
298
299                         if(sd.flag & SD_HOLDOUT_MASK) {
300                                 shader_release(kg, &sd);
301                                 break;
302                         }
303                 }
304 #endif
305
306 #ifdef __EMISSION__
307                 /* emission */
308                 if(sd.flag & SD_EMISSION) {
309                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
310                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
311                 }
312 #endif
313
314                 /* path termination. this is a strange place to put the termination, it's
315                  * mainly due to the mixed in MIS that we use. gives too many unneeded
316                  * shader evaluations, only need emission if we are going to terminate */
317                 float probability = path_state_terminate_probability(kg, &state, throughput);
318                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
319
320                 if(terminate >= probability) {
321                         shader_release(kg, &sd);
322                         break;
323                 }
324
325                 throughput /= probability;
326
327 #ifdef __AO__
328                 /* ambient occlusion */
329                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
330                         /* todo: solve correlation */
331                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
332                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
333
334                         float ao_factor = kernel_data.background.ao_factor;
335                         float3 ao_N;
336                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
337                         float3 ao_D;
338                         float ao_pdf;
339
340                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
341
342                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
343                                 Ray light_ray;
344                                 float3 ao_shadow;
345
346                                 light_ray.P = ray_offset(sd.P, sd.Ng);
347                                 light_ray.D = ao_D;
348                                 light_ray.t = kernel_data.background.ao_distance;
349 #ifdef __OBJECT_MOTION__
350                                 light_ray.time = sd.time;
351 #endif
352
353                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
354                                         path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
355                         }
356                 }
357 #endif
358
359 #ifdef __EMISSION__
360                 if(kernel_data.integrator.use_direct_light) {
361                         /* sample illumination from lights to find path contribution */
362                         if(sd.flag & SD_BSDF_HAS_EVAL) {
363                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
364                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
365                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
366                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
367
368                                 Ray light_ray;
369                                 BsdfEval L_light;
370                                 bool is_lamp;
371
372 #ifdef __OBJECT_MOTION__
373                                 light_ray.time = sd.time;
374 #endif
375
376                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
377                                         /* trace shadow ray */
378                                         float3 shadow;
379
380                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
381                                                 /* accumulate */
382                                                 path_radiance_accum_light(&L, throughput, &L_light, shadow, state.bounce, is_lamp);
383                                         }
384                                 }
385                         }
386                 }
387 #endif
388
389                 /* no BSDF? we can stop here */
390                 if(!(sd.flag & SD_BSDF)) {
391                         shader_release(kg, &sd);
392                         break;
393                 }
394
395                 /* sample BSDF */
396                 float bsdf_pdf;
397                 BsdfEval bsdf_eval;
398                 float3 bsdf_omega_in;
399                 differential3 bsdf_domega_in;
400                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
401                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
402                 int label;
403
404                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
405                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
406
407                 shader_release(kg, &sd);
408
409                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
410                         break;
411
412                 /* modify throughput */
413                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
414
415                 /* set labels */
416                 if(!(label & LABEL_TRANSPARENT)) {
417                         ray_pdf = bsdf_pdf;
418                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
419                 }
420
421                 /* update path state */
422                 path_state_next(kg, &state, label);
423
424                 /* setup ray */
425                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
426                 ray.D = bsdf_omega_in;
427                 ray.t = FLT_MAX;
428 #ifdef __RAY_DIFFERENTIALS__
429                 ray.dP = sd.dP;
430                 ray.dD = bsdf_domega_in;
431 #endif
432         }
433
434         float3 L_sum = path_radiance_sum(kg, &L);
435
436 #ifdef __CLAMP_SAMPLE__
437         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
438 #endif
439
440         kernel_write_light_passes(kg, buffer, &L, sample);
441
442         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
443 }
444
445 #ifdef __NON_PROGRESSIVE__
446
447 __device void kernel_path_indirect(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer,
448         float3 throughput, float min_ray_pdf, float ray_pdf, PathState state, int rng_offset, PathRadiance *L)
449 {
450         /* path iteration */
451         for(;; rng_offset += PRNG_BOUNCE_NUM) {
452                 /* intersect scene */
453                 Intersection isect;
454                 uint visibility = path_state_ray_visibility(kg, &state);
455
456                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
457 #ifdef __BACKGROUND__
458                         /* sample background shader */
459                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
460                         path_radiance_accum_background(L, throughput, L_background, state.bounce);
461 #endif
462
463                         break;
464                 }
465
466                 /* setup shading */
467                 ShaderData sd;
468                 shader_setup_from_ray(kg, &sd, &isect, &ray);
469                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
470                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
471                 shader_merge_closures(kg, &sd);
472
473                 /* blurring of bsdf after bounces, for rays that have a small likelihood
474                  * of following this particular path (diffuse, rough glossy) */
475                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
476                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
477
478                         if(blur_pdf < 1.0f) {
479                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
480                                 shader_bsdf_blur(kg, &sd, blur_roughness);
481                         }
482                 }
483
484 #ifdef __EMISSION__
485                 /* emission */
486                 if(sd.flag & SD_EMISSION) {
487                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
488                         path_radiance_accum_emission(L, throughput, emission, state.bounce);
489                 }
490 #endif
491
492                 /* path termination. this is a strange place to put the termination, it's
493                  * mainly due to the mixed in MIS that we use. gives too many unneeded
494                  * shader evaluations, only need emission if we are going to terminate */
495                 float probability = path_state_terminate_probability(kg, &state, throughput);
496                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
497
498                 if(terminate >= probability) {
499                         shader_release(kg, &sd);
500                         break;
501                 }
502
503                 throughput /= probability;
504
505 #ifdef __AO__
506                 /* ambient occlusion */
507                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
508                         /* todo: solve correlation */
509                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
510                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
511
512                         float ao_factor = kernel_data.background.ao_factor;
513                         float3 ao_N;
514                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
515                         float3 ao_D;
516                         float ao_pdf;
517
518                         sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
519
520                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
521                                 Ray light_ray;
522                                 float3 ao_shadow;
523
524                                 light_ray.P = ray_offset(sd.P, sd.Ng);
525                                 light_ray.D = ao_D;
526                                 light_ray.t = kernel_data.background.ao_distance;
527 #ifdef __OBJECT_MOTION__
528                                 light_ray.time = sd.time;
529 #endif
530
531                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
532                                         path_radiance_accum_ao(L, throughput, ao_bsdf, ao_shadow, state.bounce);
533                         }
534                 }
535 #endif
536
537 #ifdef __EMISSION__
538                 if(kernel_data.integrator.use_direct_light) {
539                         /* sample illumination from lights to find path contribution */
540                         if(sd.flag & SD_BSDF_HAS_EVAL) {
541                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
542                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
543                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
544                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
545
546                                 Ray light_ray;
547                                 BsdfEval L_light;
548                                 bool is_lamp;
549
550 #ifdef __OBJECT_MOTION__
551                                 light_ray.time = sd.time;
552 #endif
553
554                                 /* sample random light */
555                                 if(direct_emission(kg, &sd, -1, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
556                                         /* trace shadow ray */
557                                         float3 shadow;
558
559                                         if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
560                                                 /* accumulate */
561                                                 path_radiance_accum_light(L, throughput, &L_light, shadow, state.bounce, is_lamp);
562                                         }
563                                 }
564                         }
565                 }
566 #endif
567
568                 /* no BSDF? we can stop here */
569                 if(!(sd.flag & SD_BSDF)) {
570                         shader_release(kg, &sd);
571                         break;
572                 }
573
574                 /* sample BSDF */
575                 float bsdf_pdf;
576                 BsdfEval bsdf_eval;
577                 float3 bsdf_omega_in;
578                 differential3 bsdf_domega_in;
579                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
580                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
581                 int label;
582
583                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
584                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
585
586                 shader_release(kg, &sd);
587
588                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
589                         break;
590
591                 /* modify throughput */
592                 path_radiance_bsdf_bounce(L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
593
594                 /* set labels */
595                 if(!(label & LABEL_TRANSPARENT)) {
596                         ray_pdf = bsdf_pdf;
597                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
598                 }
599
600                 /* update path state */
601                 path_state_next(kg, &state, label);
602
603                 /* setup ray */
604                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
605                 ray.D = bsdf_omega_in;
606                 ray.t = FLT_MAX;
607 #ifdef __RAY_DIFFERENTIALS__
608                 ray.dP = sd.dP;
609                 ray.dD = bsdf_domega_in;
610 #endif
611         }
612 }
613
614 __device float4 kernel_path_non_progressive(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
615 {
616         /* initialize */
617         PathRadiance L;
618         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
619         float L_transparent = 0.0f;
620
621         path_radiance_init(&L, kernel_data.film.use_light_pass);
622
623         float ray_pdf = 0.0f;
624         PathState state;
625         int rng_offset = PRNG_BASE_NUM;
626
627         path_state_init(&state);
628
629         for(;; rng_offset += PRNG_BOUNCE_NUM) {
630                 /* intersect scene */
631                 Intersection isect;
632                 uint visibility = path_state_ray_visibility(kg, &state);
633
634                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
635                         /* eval background shader if nothing hit */
636                         if(kernel_data.background.transparent) {
637                                 L_transparent += average(throughput);
638
639 #ifdef __PASSES__
640                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
641 #endif
642                                         break;
643                         }
644
645 #ifdef __BACKGROUND__
646                         /* sample background shader */
647                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
648                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
649 #endif
650
651                         break;
652                 }
653
654                 /* setup shading */
655                 ShaderData sd;
656                 shader_setup_from_ray(kg, &sd, &isect, &ray);
657                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
658                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
659                 shader_merge_closures(kg, &sd);
660
661                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
662
663                 /* holdout */
664 #ifdef __HOLDOUT__
665                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK))) {
666                         if(kernel_data.background.transparent) {
667                                 float3 holdout_weight;
668                                 
669                                 if(sd.flag & SD_HOLDOUT_MASK)
670                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
671                                 else
672                                         holdout_weight = shader_holdout_eval(kg, &sd);
673
674                                 /* any throughput is ok, should all be identical here */
675                                 L_transparent += average(holdout_weight*throughput);
676                         }
677
678                         if(sd.flag & SD_HOLDOUT_MASK) {
679                                 shader_release(kg, &sd);
680                                 break;
681                         }
682                 }
683 #endif
684
685 #ifdef __EMISSION__
686                 /* emission */
687                 if(sd.flag & SD_EMISSION) {
688                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
689                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
690                 }
691 #endif
692
693                 /* transparency termination */
694                 if(state.flag & PATH_RAY_TRANSPARENT) {
695                         /* path termination. this is a strange place to put the termination, it's
696                          * mainly due to the mixed in MIS that we use. gives too many unneeded
697                          * shader evaluations, only need emission if we are going to terminate */
698                         float probability = path_state_terminate_probability(kg, &state, throughput);
699                         float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
700
701                         if(terminate >= probability) {
702                                 shader_release(kg, &sd);
703                                 break;
704                         }
705
706                         throughput /= probability;
707                 }
708
709 #ifdef __AO__
710                 /* ambient occlusion */
711                 if(kernel_data.integrator.use_ambient_occlusion || (sd.flag & SD_AO)) {
712                         int num_samples = kernel_data.integrator.ao_samples;
713                         float num_samples_inv = 1.0f/num_samples;
714                         float ao_factor = kernel_data.background.ao_factor;
715                         float3 ao_N;
716                         float3 ao_bsdf = shader_bsdf_ao(kg, &sd, ao_factor, &ao_N);
717
718                         for(int j = 0; j < num_samples; j++) {
719                                 /* todo: solve correlation */
720                                 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
721                                 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
722
723                                 float3 ao_D;
724                                 float ao_pdf;
725
726                                 sample_cos_hemisphere(ao_N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
727
728                                 if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
729                                         Ray light_ray;
730                                         float3 ao_shadow;
731
732                                         light_ray.P = ray_offset(sd.P, sd.Ng);
733                                         light_ray.D = ao_D;
734                                         light_ray.t = kernel_data.background.ao_distance;
735 #ifdef __OBJECT_MOTION__
736                                         light_ray.time = sd.time;
737 #endif
738
739                                         if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow))
740                                                 path_radiance_accum_ao(&L, throughput*num_samples_inv, ao_bsdf, ao_shadow, state.bounce);
741                                 }
742                         }
743                 }
744 #endif
745
746 #ifdef __EMISSION__
747                 /* sample illumination from lights to find path contribution */
748                 if(sd.flag & SD_BSDF_HAS_EVAL) {
749                         Ray light_ray;
750                         BsdfEval L_light;
751                         bool is_lamp;
752
753 #ifdef __OBJECT_MOTION__
754                         light_ray.time = sd.time;
755 #endif
756
757                         /* lamp sampling */
758                         for(int i = 0; i < kernel_data.integrator.num_all_lights; i++) {
759                                 int num_samples = light_select_num_samples(kg, i);
760                                 float num_samples_inv = 1.0f/(num_samples*kernel_data.integrator.num_all_lights);
761
762                                 if(kernel_data.integrator.pdf_triangles != 0.0f)
763                                         num_samples_inv *= 0.5f;
764
765                                 for(int j = 0; j < num_samples; j++) {
766                                         float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
767                                         float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
768
769                                         if(direct_emission(kg, &sd, i, 0.0f, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
770                                                 /* trace shadow ray */
771                                                 float3 shadow;
772
773                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
774                                                         /* accumulate */
775                                                         path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
776                                                 }
777                                         }
778                                 }
779                         }
780
781                         /* mesh light sampling */
782                         if(kernel_data.integrator.pdf_triangles != 0.0f) {
783                                 int num_samples = kernel_data.integrator.mesh_light_samples;
784                                 float num_samples_inv = 1.0f/num_samples;
785
786                                 if(kernel_data.integrator.num_all_lights)
787                                         num_samples_inv *= 0.5f;
788
789                                 for(int j = 0; j < num_samples; j++) {
790                                         float light_t = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT);
791                                         float light_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_U);
792                                         float light_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_LIGHT_V);
793
794                                         /* only sample triangle lights */
795                                         if(kernel_data.integrator.num_all_lights)
796                                                 light_t = 0.5f*light_t;
797
798                                         if(direct_emission(kg, &sd, -1, light_t, 0.0f, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
799                                                 /* trace shadow ray */
800                                                 float3 shadow;
801
802                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
803                                                         /* accumulate */
804                                                         path_radiance_accum_light(&L, throughput*num_samples_inv, &L_light, shadow, state.bounce, is_lamp);
805                                                 }
806                                         }
807                                 }
808                         }
809                 }
810 #endif
811
812                 for(int i = 0; i< sd.num_closure; i++) {
813                         const ShaderClosure *sc = &sd.closure[i];
814
815                         if(!CLOSURE_IS_BSDF(sc->type))
816                                 continue;
817                         /* transparency is not handled here, but in outer loop */
818                         if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID)
819                                 continue;
820
821                         int num_samples;
822
823                         if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
824                                 num_samples = kernel_data.integrator.diffuse_samples;
825                         else if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
826                                 num_samples = kernel_data.integrator.glossy_samples;
827                         else
828                                 num_samples = kernel_data.integrator.transmission_samples;
829
830                         float num_samples_inv = 1.0f/num_samples;
831
832                         for(int j = 0; j < num_samples; j++) {
833                                 /* sample BSDF */
834                                 float bsdf_pdf;
835                                 BsdfEval bsdf_eval;
836                                 float3 bsdf_omega_in;
837                                 differential3 bsdf_domega_in;
838                                 float bsdf_u = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_U);
839                                 float bsdf_v = path_rng(kg, rng, sample*num_samples + j, rng_offset + PRNG_BSDF_V);
840                                 int label;
841
842                                 label = shader_bsdf_sample_closure(kg, &sd, sc, bsdf_u, bsdf_v, &bsdf_eval,
843                                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
844
845                                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
846                                         continue;
847
848                                 /* modify throughput */
849                                 float3 tp = throughput;
850                                 path_radiance_bsdf_bounce(&L, &tp, &bsdf_eval, bsdf_pdf, state.bounce, label);
851
852                                 /* set labels */
853                                 float min_ray_pdf = FLT_MAX;
854
855                                 if(!(label & LABEL_TRANSPARENT))
856                                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
857
858                                 /* modify path state */
859                                 PathState ps = state;
860                                 path_state_next(kg, &ps, label);
861
862                                 /* setup ray */
863                                 Ray bsdf_ray;
864
865                                 bsdf_ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
866                                 bsdf_ray.D = bsdf_omega_in;
867                                 bsdf_ray.t = FLT_MAX;
868 #ifdef __RAY_DIFFERENTIALS__
869                                 bsdf_ray.dP = sd.dP;
870                                 bsdf_ray.dD = bsdf_domega_in;
871 #endif
872 #ifdef __OBJECT_MOTION__
873                                 bsdf_ray.time = sd.time;
874 #endif
875
876                                 kernel_path_indirect(kg, rng, sample*num_samples, bsdf_ray, buffer,
877                                         tp*num_samples_inv, min_ray_pdf, bsdf_pdf, ps, rng_offset+PRNG_BOUNCE_NUM, &L);
878                         }
879                 }
880
881                 /* continue in case of transparency */
882                 throughput *= shader_bsdf_transparency(kg, &sd);
883                 shader_release(kg, &sd);
884
885                 if(is_zero(throughput))
886                         break;
887
888                 path_state_next(kg, &state, LABEL_TRANSPARENT);
889                 ray.P = ray_offset(sd.P, -sd.Ng);
890         }
891
892         float3 L_sum = path_radiance_sum(kg, &L);
893
894 #ifdef __CLAMP_SAMPLE__
895         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
896 #endif
897
898         kernel_write_light_passes(kg, buffer, &L, sample);
899
900         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
901 }
902
903 #endif
904
905 __device void kernel_path_trace(KernelGlobals *kg,
906         __global float *buffer, __global uint *rng_state,
907         int sample, int x, int y, int offset, int stride)
908 {
909         /* buffer offset */
910         int index = offset + x + y*stride;
911         int pass_stride = kernel_data.film.pass_stride;
912
913         rng_state += index;
914         buffer += index*pass_stride;
915
916         /* initialize random numbers */
917         RNG rng;
918
919         float filter_u;
920         float filter_v;
921
922         path_rng_init(kg, rng_state, sample, &rng, x, y, &filter_u, &filter_v);
923
924         /* sample camera ray */
925         Ray ray;
926
927         float lens_u = path_rng(kg, &rng, sample, PRNG_LENS_U);
928         float lens_v = path_rng(kg, &rng, sample, PRNG_LENS_V);
929
930 #ifdef __CAMERA_MOTION__
931         float time = path_rng(kg, &rng, sample, PRNG_TIME);
932 #else
933         float time = 0.0f;
934 #endif
935
936         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, &ray);
937
938         /* integrate */
939         float4 L;
940
941         if (ray.t != 0.0f) {
942 #ifdef __NON_PROGRESSIVE__
943                 if(kernel_data.integrator.progressive)
944 #endif
945                         L = kernel_path_progressive(kg, &rng, sample, ray, buffer);
946 #ifdef __NON_PROGRESSIVE__
947                 else
948                         L = kernel_path_non_progressive(kg, &rng, sample, ray, buffer);
949 #endif
950         }
951         else
952                 L = make_float4(0.f, 0.f, 0.f, 0.f);
953
954         /* accumulate result in output buffer */
955         kernel_write_pass_float4(buffer, sample, L);
956
957         path_rng_end(kg, rng_state, rng);
958 }
959
960 CCL_NAMESPACE_END
961