Cycles: ambient occlusion support, with AO factor and distance, and a render pass.
[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_triangle.h"
22 #include "kernel_object.h"
23 #ifdef __QBVH__
24 #include "kernel_qbvh.h"
25 #else
26 #include "kernel_bvh.h"
27 #endif
28 #include "kernel_accumulate.h"
29 #include "kernel_camera.h"
30 #include "kernel_shader.h"
31 #include "kernel_light.h"
32 #include "kernel_emission.h"
33 #include "kernel_random.h"
34 #include "kernel_passes.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 typedef struct PathState {
39         uint flag;
40         int bounce;
41
42         int diffuse_bounce;
43         int glossy_bounce;
44         int transmission_bounce;
45         int transparent_bounce;
46 } PathState;
47
48 __device_inline void path_state_init(PathState *state)
49 {
50         state->flag = PATH_RAY_CAMERA|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
51         state->bounce = 0;
52         state->diffuse_bounce = 0;
53         state->glossy_bounce = 0;
54         state->transmission_bounce = 0;
55         state->transparent_bounce = 0;
56 }
57
58 __device_inline void path_state_next(KernelGlobals *kg, PathState *state, int label)
59 {
60         /* ray through transparent keeps same flags from previous ray and is
61            not counted as a regular bounce, transparent has separate max */
62         if(label & LABEL_TRANSPARENT) {
63                 state->flag |= PATH_RAY_TRANSPARENT;
64                 state->transparent_bounce++;
65
66                 if(!kernel_data.integrator.transparent_shadows)
67                         state->flag |= PATH_RAY_MIS_SKIP;
68
69                 return;
70         }
71
72         state->bounce++;
73
74         /* reflection/transmission */
75         if(label & LABEL_REFLECT) {
76                 state->flag |= PATH_RAY_REFLECT;
77                 state->flag &= ~(PATH_RAY_TRANSMIT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
78
79                 if(label & LABEL_DIFFUSE)
80                         state->diffuse_bounce++;
81                 else
82                         state->glossy_bounce++;
83         }
84         else {
85                 kernel_assert(label & LABEL_TRANSMIT);
86
87                 state->flag |= PATH_RAY_TRANSMIT;
88                 state->flag &= ~(PATH_RAY_REFLECT|PATH_RAY_CAMERA|PATH_RAY_TRANSPARENT);
89
90                 state->transmission_bounce++;
91         }
92
93         /* diffuse/glossy/singular */
94         if(label & LABEL_DIFFUSE) {
95                 state->flag |= PATH_RAY_DIFFUSE;
96                 state->flag &= ~(PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
97         }
98         else if(label & LABEL_GLOSSY) {
99                 state->flag |= PATH_RAY_GLOSSY;
100                 state->flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP);
101         }
102         else {
103                 kernel_assert(label & LABEL_SINGULAR);
104
105                 state->flag |= PATH_RAY_GLOSSY|PATH_RAY_SINGULAR|PATH_RAY_MIS_SKIP;
106                 state->flag &= ~PATH_RAY_DIFFUSE;
107         }
108 }
109
110 __device_inline uint path_state_ray_visibility(KernelGlobals *kg, PathState *state)
111 {
112         uint flag = state->flag;
113
114         /* for visibility, diffuse/glossy are for reflection only */
115         if(flag & PATH_RAY_TRANSMIT)
116                 flag &= ~(PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY);
117         /* for camera visibility, use render layer flags */
118         if(flag & PATH_RAY_CAMERA)
119                 flag |= kernel_data.integrator.layer_flag;
120
121         return flag;
122 }
123
124 __device_inline float path_state_terminate_probability(KernelGlobals *kg, PathState *state, const float3 throughput)
125 {
126         if(state->flag & PATH_RAY_TRANSPARENT) {
127                 /* transparent rays treated separately */
128                 if(state->transparent_bounce >= kernel_data.integrator.transparent_max_bounce)
129                         return 0.0f;
130                 else if(state->transparent_bounce <= kernel_data.integrator.transparent_min_bounce)
131                         return 1.0f;
132         }
133         else {
134                 /* other rays */
135                 if((state->bounce >= kernel_data.integrator.max_bounce) ||
136                    (state->diffuse_bounce >= kernel_data.integrator.max_diffuse_bounce) ||
137                    (state->glossy_bounce >= kernel_data.integrator.max_glossy_bounce) ||
138                    (state->transmission_bounce >= kernel_data.integrator.max_transmission_bounce))
139                         return 0.0f;
140                 else if(state->bounce <= kernel_data.integrator.min_bounce)
141                         return 1.0f;
142         }
143
144         /* probalistic termination */
145         return average(throughput);
146 }
147
148 __device_inline bool shadow_blocked(KernelGlobals *kg, PathState *state, Ray *ray, float3 *shadow)
149 {
150         if(ray->t == 0.0f)
151                 return false;
152         
153         Intersection isect;
154         bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect);
155
156         *shadow = make_float3(1.0f, 1.0f, 1.0f);
157
158 #ifdef __TRANSPARENT_SHADOWS__
159         if(result && kernel_data.integrator.transparent_shadows) {
160                 /* transparent shadows work in such a way to try to minimize overhead
161                    in cases where we don't need them. after a regular shadow ray is
162                    cast we check if the hit primitive was potentially transparent, and
163                    only in that case start marching. this gives on extra ray cast for
164                    the cases were we do want transparency.
165                    
166                    also note that for this to work correct, multi close sampling must
167                    be used, since we don't pass a random number to shader_eval_surface */
168                 if(shader_transparent_shadow(kg, &isect)) {
169                         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
170                         float3 Pend = ray->P + ray->D*ray->t;
171                         int bounce = state->transparent_bounce;
172
173                         for(;;) {
174                                 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
175                                         return true;
176                                 }
177                                 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
178                                         /* todo: get random number somewhere for probabilistic terminate */
179 #if 0
180                                         float probability = average(throughput);
181                                         float terminate = 0.0f;
182
183                                         if(terminate >= probability)
184                                                 return true;
185
186                                         throughput /= probability;
187 #endif
188                                 }
189
190                                 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect)) {
191                                         *shadow *= throughput;
192                                         return false;
193                                 }
194
195                                 if(!shader_transparent_shadow(kg, &isect))
196                                         return true;
197
198                                 ShaderData sd;
199                                 shader_setup_from_ray(kg, &sd, &isect, ray);
200                                 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW);
201
202                                 throughput *= shader_bsdf_transparency(kg, &sd);
203
204                                 ray->P = ray_offset(sd.P, -sd.Ng);
205                                 if(ray->t != FLT_MAX)
206                                         ray->D = normalize_len(Pend - ray->P, &ray->t);
207
208                                 bounce++;
209                         }
210                 }
211         }
212 #endif
213
214         return result;
215 }
216
217 __device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
218 {
219         /* initialize */
220         PathRadiance L;
221         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
222         float L_transparent = 0.0f;
223
224         path_radiance_init(&L, kernel_data.film.use_light_pass);
225
226         float ray_pdf = 0.0f;
227         PathState state;
228         int rng_offset = PRNG_BASE_NUM;
229
230         path_state_init(&state);
231
232         /* path iteration */
233         for(;; rng_offset += PRNG_BOUNCE_NUM) {
234                 /* intersect scene */
235                 Intersection isect;
236                 uint visibility = path_state_ray_visibility(kg, &state);
237
238                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
239                         /* eval background shader if nothing hit */
240                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
241                                 L_transparent += average(throughput);
242                         }
243 #ifdef __BACKGROUND__
244                         else {
245                                 /* sample background shader */
246                                 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
247                                 path_radiance_accum_background(&L, throughput, L_background, state.bounce);
248                         }
249 #endif
250
251                         break;
252                 }
253
254                 /* setup shading */
255                 ShaderData sd;
256                 shader_setup_from_ray(kg, &sd, &isect, &ray);
257                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
258                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
259
260                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
261
262 #ifdef __HOLDOUT__
263                 if((sd.flag & SD_HOLDOUT) && (state.flag & PATH_RAY_CAMERA)) {
264                         float3 holdout_weight = shader_holdout_eval(kg, &sd);
265
266                         if(kernel_data.background.transparent)
267                                 /* any throughput is ok, should all be identical here */
268                                 L_transparent += average(holdout_weight*throughput);
269                 }
270 #endif
271
272 #ifdef __EMISSION__
273                 /* emission */
274                 if(sd.flag & SD_EMISSION) {
275                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
276                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
277                 }
278 #endif
279
280                 /* path termination. this is a strange place to put the termination, it's
281                    mainly due to the mixed in MIS that we use. gives too many unneeded
282                    shader evaluations, only need emission if we are going to terminate */
283                 float probability = path_state_terminate_probability(kg, &state, throughput);
284                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
285
286                 if(terminate >= probability)
287                         break;
288
289                 throughput /= probability;
290
291
292 #ifdef __AO__
293                 /* ambient occlusion */
294                 if(kernel_data.integrator.use_ambient_occlusion) {
295                         /* todo: solve correlation */
296                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
297                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
298
299                         float3 ao_D;
300                         float ao_pdf;
301
302                         sample_cos_hemisphere(sd.N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
303
304                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
305                                 Ray light_ray;
306                                 float3 ao_shadow;
307
308                                 light_ray.P = ray_offset(sd.P, sd.Ng);
309                                 light_ray.D = ao_D;
310                                 light_ray.t = kernel_data.background.ao_distance;
311
312                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow)) {
313                                         float3 ao_bsdf = shader_bsdf_diffuse(kg, &sd)*kernel_data.background.ao_factor;
314                                         path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
315                                 }
316                         }
317                 }
318 #endif
319
320 #ifdef __EMISSION__
321                 if(kernel_data.integrator.use_direct_light) {
322                         /* sample illumination from lights to find path contribution */
323                         if(sd.flag & SD_BSDF_HAS_EVAL) {
324                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
325                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
326                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
327                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
328
329                                 Ray light_ray;
330                                 BsdfEval L_light;
331
332 #ifdef __MULTI_LIGHT__
333                                 /* index -1 means randomly sample from distribution */
334                                 int i = (kernel_data.integrator.num_distribution)? -1: 0;
335
336                                 for(; i < kernel_data.integrator.num_all_lights; i++) {
337 #else
338                                 const int i = -1;
339 #endif
340                                         if(direct_emission(kg, &sd, i, light_t, light_o, light_u, light_v, &light_ray, &L_light)) {
341                                                 /* trace shadow ray */
342                                                 float3 shadow;
343
344                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
345                                                         /* accumulate */
346                                                         bsdf_eval_mul(&L_light, shadow);
347                                                         path_radiance_accum_light(&L, throughput, &L_light, state.bounce);
348                                                 }
349                                         }
350 #ifdef __MULTI_LIGHT__
351                                 }
352 #endif
353                         }
354                 }
355 #endif
356
357                 /* no BSDF? we can stop here */
358                 if(!(sd.flag & SD_BSDF))
359                         break;
360
361                 /* sample BSDF */
362                 float bsdf_pdf;
363                 BsdfEval bsdf_eval;
364                 float3 bsdf_omega_in;
365                 differential3 bsdf_domega_in;
366                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
367                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
368                 int label;
369
370                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
371                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
372
373                 shader_release(kg, &sd);
374
375                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
376                         break;
377
378                 /* modify throughput */
379                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
380
381                 /* set labels */
382                 if(!(label & LABEL_TRANSPARENT))
383                         ray_pdf = bsdf_pdf;
384
385                 /* update path state */
386                 path_state_next(kg, &state, label);
387
388                 /* setup ray */
389                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
390                 ray.D = bsdf_omega_in;
391                 ray.t = FLT_MAX;
392 #ifdef __RAY_DIFFERENTIALS__
393                 ray.dP = sd.dP;
394                 ray.dD = bsdf_domega_in;
395 #endif
396         }
397
398         float3 L_sum = path_radiance_sum(&L);
399
400         kernel_write_light_passes(kg, buffer, &L, sample);
401
402         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
403 }
404
405 __device void kernel_path_trace(KernelGlobals *kg,
406         __global float *buffer, __global uint *rng_state,
407         int sample, int x, int y, int offset, int stride)
408 {
409         /* buffer offset */
410         int index = offset + x + y*stride;
411         int pass_stride = kernel_data.film.pass_stride;
412
413         rng_state += index;
414         buffer += index*pass_stride;
415
416         /* initialize random numbers */
417         RNG rng;
418
419         float filter_u;
420         float filter_v;
421
422         path_rng_init(kg, rng_state, sample, &rng, x, y, &filter_u, &filter_v);
423
424         /* sample camera ray */
425         Ray ray;
426
427         float lens_u = path_rng(kg, &rng, sample, PRNG_LENS_U);
428         float lens_v = path_rng(kg, &rng, sample, PRNG_LENS_V);
429
430         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, &ray);
431
432         /* integrate */
433         float4 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
434
435         /* accumulate result in output buffer */
436         kernel_write_pass_float4(buffer, sample, L);
437
438         path_rng_end(kg, rng_state, rng);
439 }
440
441 CCL_NAMESPACE_END
442