svn merge ^/trunk/blender -r43685:43693
[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, Intersection *isect, BsdfEval *L_light)
149 {
150         if(ray->t == 0.0f)
151                 return false;
152         
153         bool result = scene_intersect(kg, ray, PATH_RAY_SHADOW_OPAQUE, isect);
154
155 #ifdef __TRANSPARENT_SHADOWS__
156         if(result && kernel_data.integrator.transparent_shadows) {
157                 /* transparent shadows work in such a way to try to minimize overhead
158                    in cases where we don't need them. after a regular shadow ray is
159                    cast we check if the hit primitive was potentially transparent, and
160                    only in that case start marching. this gives on extra ray cast for
161                    the cases were we do want transparency.
162                    
163                    also note that for this to work correct, multi close sampling must
164                    be used, since we don't pass a random number to shader_eval_surface */
165                 if(shader_transparent_shadow(kg, isect)) {
166                         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
167                         float3 Pend = ray->P + ray->D*ray->t;
168                         int bounce = state->transparent_bounce;
169
170                         for(;;) {
171                                 if(bounce >= kernel_data.integrator.transparent_max_bounce) {
172                                         return true;
173                                 }
174                                 else if(bounce >= kernel_data.integrator.transparent_min_bounce) {
175                                         /* todo: get random number somewhere for probabilistic terminate */
176 #if 0
177                                         float probability = average(throughput);
178                                         float terminate = 0.0f;
179
180                                         if(terminate >= probability)
181                                                 return true;
182
183                                         throughput /= probability;
184 #endif
185                                 }
186
187                                 if(!scene_intersect(kg, ray, PATH_RAY_SHADOW_TRANSPARENT, isect)) {
188                                         bsdf_eval_mul(L_light, throughput);
189                                         return false;
190                                 }
191
192                                 if(!shader_transparent_shadow(kg, isect))
193                                         return true;
194
195                                 ShaderData sd;
196                                 shader_setup_from_ray(kg, &sd, isect, ray);
197                                 shader_eval_surface(kg, &sd, 0.0f, PATH_RAY_SHADOW);
198
199                                 throughput *= shader_bsdf_transparency(kg, &sd);
200
201                                 ray->P = ray_offset(sd.P, -sd.Ng);
202                                 if(ray->t != FLT_MAX)
203                                         ray->D = normalize_len(Pend - ray->P, &ray->t);
204
205                                 bounce++;
206                         }
207                 }
208         }
209 #endif
210
211         return result;
212 }
213
214 __device float4 kernel_path_integrate(KernelGlobals *kg, RNG *rng, int sample, Ray ray, __global float *buffer)
215 {
216         /* initialize */
217         PathRadiance L;
218         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
219         float L_transparent = 0.0f;
220
221         path_radiance_init(&L, kernel_data.film.use_light_pass);
222
223 #ifdef __EMISSION__
224         float ray_pdf = 0.0f;
225 #endif
226         PathState state;
227         int rng_offset = PRNG_BASE_NUM;
228
229         path_state_init(&state);
230
231         /* path iteration */
232         for(;; rng_offset += PRNG_BOUNCE_NUM) {
233                 /* intersect scene */
234                 Intersection isect;
235                 uint visibility = path_state_ray_visibility(kg, &state);
236
237                 if(!scene_intersect(kg, &ray, visibility, &isect)) {
238                         /* eval background shader if nothing hit */
239                         if(kernel_data.background.transparent && (state.flag & PATH_RAY_CAMERA)) {
240                                 L_transparent += average(throughput);
241                         }
242                         else {
243                                 /* sample background shader */
244                                 float3 L_background = indirect_background(kg, &ray, state.flag, ray_pdf);
245                                 path_radiance_accum_background(&L, throughput, L_background, state.bounce);
246                         }
247
248                         break;
249                 }
250
251                 /* setup shading */
252                 ShaderData sd;
253                 shader_setup_from_ray(kg, &sd, &isect, &ray);
254                 float rbsdf = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF);
255                 shader_eval_surface(kg, &sd, rbsdf, state.flag);
256
257                 kernel_write_data_passes(kg, buffer, &L, &sd, sample, state.flag, throughput);
258
259 #ifdef __HOLDOUT__
260                 if((sd.flag & SD_HOLDOUT) && (state.flag & PATH_RAY_CAMERA)) {
261                         float3 holdout_weight = shader_holdout_eval(kg, &sd);
262
263                         if(kernel_data.background.transparent)
264                                 /* any throughput is ok, should all be identical here */
265                                 L_transparent += average(holdout_weight*throughput);
266                 }
267 #endif
268
269 #ifdef __EMISSION__
270                 /* emission */
271                 if(sd.flag & SD_EMISSION) {
272                         float3 emission = indirect_emission(kg, &sd, isect.t, state.flag, ray_pdf);
273                         path_radiance_accum_emission(&L, throughput, emission, state.bounce);
274                 }
275 #endif
276
277                 /* path termination. this is a strange place to put the termination, it's
278                    mainly due to the mixed in MIS that we use. gives too many unneeded
279                    shader evaluations, only need emission if we are going to terminate */
280                 float probability = path_state_terminate_probability(kg, &state, throughput);
281                 float terminate = path_rng(kg, rng, sample, rng_offset + PRNG_TERMINATE);
282
283                 if(terminate >= probability)
284                         break;
285
286                 throughput /= probability;
287
288 #ifdef __EMISSION__
289                 if(kernel_data.integrator.use_direct_light) {
290                         /* sample illumination from lights to find path contribution */
291                         if(sd.flag & SD_BSDF_HAS_EVAL) {
292                                 float light_t = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT);
293                                 float light_o = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_F);
294                                 float light_u = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_U);
295                                 float light_v = path_rng(kg, rng, sample, rng_offset + PRNG_LIGHT_V);
296
297                                 Ray light_ray;
298                                 BsdfEval L_light;
299
300 #ifdef __MULTI_LIGHT__
301                                 /* index -1 means randomly sample from distribution */
302                                 int i = (kernel_data.integrator.num_distribution)? -1: 0;
303
304                                 for(; i < kernel_data.integrator.num_all_lights; i++) {
305 #else
306                                 const int i = -1;
307 #endif
308                                         if(direct_emission(kg, &sd, i, light_t, light_o, light_u, light_v, &light_ray, &L_light)) {
309                                                 /* trace shadow ray */
310                                                 if(!shadow_blocked(kg, &state, &light_ray, &isect, &L_light))
311                                                         path_radiance_accum_light(&L, throughput, &L_light, state.bounce);
312                                         }
313 #ifdef __MULTI_LIGHT__
314                                 }
315 #endif
316                         }
317                 }
318 #endif
319
320                 /* no BSDF? we can stop here */
321                 if(!(sd.flag & SD_BSDF))
322                         break;
323
324                 /* sample BSDF */
325                 float bsdf_pdf;
326                 BsdfEval bsdf_eval;
327                 float3 bsdf_omega_in;
328                 differential3 bsdf_domega_in;
329                 float bsdf_u = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_U);
330                 float bsdf_v = path_rng(kg, rng, sample, rng_offset + PRNG_BSDF_V);
331                 int label;
332
333                 label = shader_bsdf_sample(kg, &sd, bsdf_u, bsdf_v, &bsdf_eval,
334                         &bsdf_omega_in, &bsdf_domega_in, &bsdf_pdf);
335
336                 shader_release(kg, &sd);
337
338                 if(bsdf_pdf == 0.0f || bsdf_eval_is_zero(&bsdf_eval))
339                         break;
340
341                 /* modify throughput */
342                 path_radiance_bsdf_bounce(&L, &throughput, &bsdf_eval, bsdf_pdf, state.bounce, label);
343
344                 /* set labels */
345 #if defined(__EMISSION__) || defined(__BACKGROUND__)
346                 ray_pdf = bsdf_pdf;
347 #endif
348
349                 /* update path state */
350                 path_state_next(kg, &state, label);
351
352                 /* setup ray */
353                 ray.P = ray_offset(sd.P, (label & LABEL_TRANSMIT)? -sd.Ng: sd.Ng);
354                 ray.D = bsdf_omega_in;
355                 ray.t = FLT_MAX;
356 #ifdef __RAY_DIFFERENTIALS__
357                 ray.dP = sd.dP;
358                 ray.dD = bsdf_domega_in;
359 #endif
360         }
361
362         float3 L_sum = path_radiance_sum(&L);
363
364         kernel_write_light_passes(kg, buffer, &L, sample);
365
366         return make_float4(L_sum.x, L_sum.y, L_sum.z, 1.0f - L_transparent);
367 }
368
369 __device void kernel_path_trace(KernelGlobals *kg,
370         __global float *buffer, __global uint *rng_state,
371         int sample, int x, int y, int offset, int stride)
372 {
373         /* buffer offset */
374         int index = offset + x + y*stride;
375         int pass_stride = kernel_data.film.pass_stride;
376
377         rng_state += index;
378         buffer += index*pass_stride;
379
380         kernel_clear_passes(buffer, sample, pass_stride);
381
382         /* initialize random numbers */
383         RNG rng;
384
385         float filter_u;
386         float filter_v;
387
388         path_rng_init(kg, rng_state, sample, &rng, x, y, &filter_u, &filter_v);
389
390         /* sample camera ray */
391         Ray ray;
392
393         float lens_u = path_rng(kg, &rng, sample, PRNG_LENS_U);
394         float lens_v = path_rng(kg, &rng, sample, PRNG_LENS_V);
395
396         camera_sample(kg, x, y, filter_u, filter_v, lens_u, lens_v, &ray);
397
398         /* integrate */
399         float4 L = kernel_path_integrate(kg, &rng, sample, ray, buffer);
400
401         /* accumulate result in output buffer */
402         kernel_write_pass_float4(buffer, sample, L);
403
404         path_rng_end(kg, rng_state, rng);
405 }
406
407 CCL_NAMESPACE_END
408