style cleanup: block comments
[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                         return 0.0f;
141                 else if(state->bounce <= kernel_data.integrator.min_bounce)
142                         return 1.0f;
143         }
144
145         /* probalistic termination */
146         return average(throughput);
147 }
148
149 __device_inline bool shadow_blocked(KernelGlobals *kg, PathState *state, Ray *ray, float3 *shadow)
150 {
151         *shadow = make_float3(1.0f, 1.0f, 1.0f);
152
153         if(ray->t == 0.0f)
154                 return false;
155         
156         Intersection isect;
157         bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, &isect);
158
159 #ifdef __TRANSPARENT_SHADOWS__
160         if(result && kernel_data.integrator.transparent_shadows) {
161                 /* transparent shadows work in such a way to try to minimize overhead
162                  * in cases where we don't need them. after a regular shadow ray is
163                  * cast we check if the hit primitive was potentially transparent, and
164                  * only in that case start marching. this gives on extra ray cast for
165                  * the cases were we do want transparency.
166                  *
167                  * also note that for this to work correct, multi close sampling must
168                  * be used, since we don't pass a random number to shader_eval_surface */
169                 if(shader_transparent_shadow(kg, &isect)) {
170                         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
171                         float3 Pend = ray->P + ray->D*ray->t;
172                         int bounce = state->transparent_bounce;
173
174                         for(;;) {
175                                 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
176                                         return true;
177                                 }
178                                 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
179                                         /* todo: get random number somewhere for probabilistic terminate */
180 #if 0
181                                         float probability = average(throughput);
182                                         float terminate = 0.0f;
183
184                                         if(terminate >= probability)
185                                                 return true;
186
187                                         throughput /= probability;
188 #endif
189                                 }
190
191                                 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, &isect)) {
192                                         *shadow *= throughput;
193                                         return false;
194                                 }
195
196                                 if(!shader_transparent_shadow(kg, &isect))
197                                         return true;
198
199                                 ShaderData sd;
200                                 shader_setup_from_ray(kg, &sd, &isect, ray);
201                                 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW);
202
203                                 throughput *= shader_bsdf_transparency(kg, &sd);
204
205                                 ray->P = ray_offset(sd.P, -sd.Ng);
206                                 if(ray->t != FLT_MAX)
207                                         ray->D = normalize_len(Pend - ray->P, &ray->t);
208
209                                 bounce++;
210                         }
211                 }
212         }
213 #endif
214
215         return result;
216 }
217
218 __device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
219 {
220         /* initialize */
221         PathRadiance L;
222         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
223         float L_transparent = 0.0f;
224
225         path_radiance_init(&L, kernel_data.film.use_light_pass);
226
227         float min_ray_pdf = FLT_MAX;
228         float ray_pdf = 0.0f;
229         PathState state;
230         int rng_offset = PRNG_BASE_NUM;
231
232         path_state_init(&state);
233
234         /* path iteration */
235         for(;; rng_offset += PRNG_BOUNCE_NUM) {
236                 /* intersect scene */
237                 Intersection isect;
238                 uint visibility = path_state_ray_visibility(kg, &state);
239
240                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
241                         /* eval background shader if nothing hit */
242                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
243                                 L_transparent += average(throughput);
244
245 #ifdef __PASSES__
246                                 if(!(kernel_data.film.pass_flag & PASS_BACKGROUND))
247 #endif
248                                         break;
249                         }
250
251 #ifdef __BACKGROUND__
252                         /* sample background shader */
253                         float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
254                         path_radiance_accum_background(&L, throughput, L_background, state.bounce);
255 #endif
256
257                         break;
258                 }
259
260                 /* setup shading */
261                 ShaderData sd;
262                 shader_setup_from_ray(kg, &sd, &isect, &ray);
263                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
264                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
265
266                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
267
268                 /* blurring of bsdf after bounces, for rays that have a small likelihood
269                  * of following this particular path (diffuse, rough glossy) */
270                 if(kernel_data.integrator.filter_glossy != FLT_MAX) {
271                         float blur_pdf = kernel_data.integrator.filter_glossy*min_ray_pdf;
272
273                         if(blur_pdf < 1.0f) {
274                                 float blur_roughness = sqrtf(1.0f - blur_pdf)*0.5f;
275                                 shader_bsdf_blur(kg, &sd, blur_roughness);
276                         }
277                 }
278
279                 /* holdout */
280 #ifdef __HOLDOUT__
281                 if((sd.flag & (SD_HOLDOUT|SD_HOLDOUT_MASK)) && (state.flag & PATH_RAY_CAMERA)) {
282                         if(kernel_data.background.transparent) {
283                                 float3 holdout_weight;
284                                 
285                                 if(sd.flag & SD_HOLDOUT_MASK)
286                                         holdout_weight = make_float3(1.0f, 1.0f, 1.0f);
287                                 else
288                                         shader_holdout_eval(kg, &sd);
289
290                                 /* any throughput is ok, should all be identical here */
291                                 L_transparent += average(holdout_weight*throughput);
292                         }
293
294                         if(sd.flag & SD_HOLDOUT_MASK)
295                                 break;
296                 }
297 #endif
298
299 #ifdef __EMISSION__
300                 /* emission */
301                 if(sd.flag & SD_EMISSION) {
302                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
303                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
304                 }
305 #endif
306
307                 /* path termination. this is a strange place to put the termination, it's
308                  * mainly due to the mixed in MIS that we use. gives too many unneeded
309                  * shader evaluations, only need emission if we are going to terminate */
310                 float probability = path_state_terminate_probability(kg, &state, throughput);
311                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
312
313                 if(terminate >= probability)
314                         break;
315
316                 throughput /= probability;
317
318 #ifdef __AO__
319                 /* ambient occlusion */
320                 if(kernel_data.integrator.use_ambient_occlusion) {
321                         /* todo: solve correlation */
322                         float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
323                         float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
324
325                         float3 ao_D;
326                         float ao_pdf;
327
328                         sample_cos_hemisphere(sd.N, bsdf_u, bsdf_v, &ao_D, &ao_pdf);
329
330                         if(dot(sd.Ng, ao_D) > 0.0f && ao_pdf != 0.0f) {
331                                 Ray light_ray;
332                                 float3 ao_shadow;
333
334                                 light_ray.P = ray_offset(sd.P, sd.Ng);
335                                 light_ray.D = ao_D;
336                                 light_ray.t = kernel_data.background.ao_distance;
337 #ifdef __MOTION__
338                                 light_ray.time = sd.time;
339 #endif
340
341                                 if(!shadow_blocked(kg, &state, &light_ray, &ao_shadow)) {
342                                         float3 ao_bsdf = shader_bsdf_diffuse(kg, &sd)*kernel_data.background.ao_factor;
343                                         path_radiance_accum_ao(&L, throughput, ao_bsdf, ao_shadow, state.bounce);
344                                 }
345                         }
346                 }
347 #endif
348
349 #ifdef __EMISSION__
350                 if(kernel_data.integrator.use_direct_light) {
351                         /* sample illumination from lights to find path contribution */
352                         if(sd.flag & SD_BSDF_HAS_EVAL) {
353                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
354                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
355                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
356                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
357
358                                 Ray light_ray;
359                                 BsdfEval L_light;
360                                 bool is_lamp;
361
362 #ifdef __MOTION__
363                                 light_ray.time = sd.time;
364 #endif
365
366 #ifdef __MULTI_LIGHT__
367                                 /* index -1 means randomly sample from distribution */
368                                 int i = (kernel_data.integrator.num_all_lights)? 0: -1;
369
370                                 for(; i < kernel_data.integrator.num_all_lights; i++) {
371 #else
372                                 const int i = -1;
373 #endif
374                                         if(direct_emission(kg, &sd, i, light_t, light_o, light_u, light_v, &light_ray, &L_light, &is_lamp)) {
375                                                 /* trace shadow ray */
376                                                 float3 shadow;
377
378                                                 if(!shadow_blocked(kg, &state, &light_ray, &shadow)) {
379                                                         /* accumulate */
380                                                         path_radiance_accum_light(&L, throughput, &L_light, shadow, state.bounce, is_lamp);
381                                                 }
382                                         }
383 #ifdef __MULTI_LIGHT__
384                                 }
385 #endif
386                         }
387                 }
388 #endif
389
390                 /* no BSDF? we can stop here */
391                 if(!(sd.flag & SD_BSDF))
392                         break;
393
394                 /* sample BSDF */
395                 float bsdf_pdf;
396                 BsdfEval bsdf_eval;
397                 float3 bsdf_omega_in;
398                 differential3 bsdf_domega_in;
399                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
400                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
401                 int label;
402
403                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
404                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
405
406                 shader_release(kg, &sd);
407
408                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
409                         break;
410
411                 /* modify throughput */
412                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
413
414                 /* set labels */
415                 if(!(label & LABEL_TRANSPARENT)) {
416                         ray_pdf = bsdf_pdf;
417                         min_ray_pdf = fminf(bsdf_pdf, min_ray_pdf);
418                 }
419
420                 /* update path state */
421                 path_state_next(kg, &state, label);
422
423                 /* setup ray */
424                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
425                 ray.D = bsdf_omega_in;
426                 ray.t = FLT_MAX;
427 #ifdef __RAY_DIFFERENTIALS__
428                 ray.dP = sd.dP;
429                 ray.dD = bsdf_domega_in;
430 #endif
431         }
432
433         float3 L_sum = path_radiance_sum(kg, &L);
434
435 #ifdef __CLAMP_SAMPLE__
436         path_radiance_clamp(&L, &L_sum, kernel_data.integrator.sample_clamp);
437 #endif
438
439         kernel_write_light_passes(kg, buffer, &L, sample);
440
441         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
442 }
443
444 __device void kernel_path_trace(KernelGlobals *kg,
445         __global float *buffer, __global uint *rng_state,
446         int sample, int x, int y, int offset, int stride)
447 {
448         /* buffer offset */
449         int index = offset + x + y*stride;
450         int pass_stride = kernel_data.film.pass_stride;
451
452         rng_state += index;
453         buffer += index*pass_stride;
454
455         /* initialize random numbers */
456         RNG rng;
457
458         float filter_u;
459         float filter_v;
460
461         path_rng_init(kg, rng_state, sample, &rng, x, y, &filter_u, &filter_v);
462
463         /* sample camera ray */
464         Ray ray;
465
466         float lens_u = path_rng(kg, &rng, sample, PRNG_LENS_U);
467         float lens_v = path_rng(kg, &rng, sample, PRNG_LENS_V);
468
469 #ifdef __MOTION__
470         float time = path_rng(kg, &rng, sample, PRNG_TIME);
471 #else
472         float time = 0.0f;
473 #endif
474
475         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, time, &ray);
476
477         /* integrate */
478         float4 L;
479
480         if (ray.t != 0.f)
481                 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
482         else
483                 L = make_float4(0.f, 0.f, 0.f, 0.f);
484
485         /* accumulate result in output buffer */
486         kernel_write_pass_float4(buffer, sample, L);
487
488         path_rng_end(kg, rng_state, rng);
489 }
490
491 CCL_NAMESPACE_END
492