Fix use of uninitialized memory in Cycles normal baking.
[blender-staging.git] / intern / cycles / kernel / kernel_bake.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 CCL_NAMESPACE_BEGIN
18
19 #ifdef __BAKING__
20
21 ccl_device_inline void compute_light_pass(KernelGlobals *kg,
22                                           ShaderData *sd,
23                                           PathRadiance *L,
24                                           uint rng_hash,
25                                           int pass_filter,
26                                           int sample)
27 {
28         /* initialize master radiance accumulator */
29         kernel_assert(kernel_data.film.use_light_pass);
30         path_radiance_init(L, kernel_data.film.use_light_pass);
31
32         PathRadiance L_sample;
33         PathState state;
34         Ray ray;
35         float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
36
37         /* emission and indirect shader data memory used by various functions */
38         ShaderData emission_sd, indirect_sd;
39
40         ray.P = sd->P + sd->Ng;
41         ray.D = -sd->Ng;
42         ray.t = FLT_MAX;
43 #ifdef __CAMERA_MOTION__
44         ray.time = 0.5f;
45 #endif
46
47         /* init radiance */
48         path_radiance_init(&L_sample, kernel_data.film.use_light_pass);
49
50         /* init path state */
51         path_state_init(kg, &emission_sd, &state, rng_hash, sample, NULL);
52
53         /* evaluate surface shader */
54         shader_eval_surface(kg, sd, &state, state.flag);
55
56         /* TODO, disable more closures we don't need besides transparent */
57         shader_bsdf_disable_transparency(kg, sd);
58
59 #ifdef __BRANCHED_PATH__
60         if(!kernel_data.integrator.branched) {
61                 /* regular path tracer */
62 #endif
63
64                 /* sample ambient occlusion */
65                 if(pass_filter & BAKE_FILTER_AO) {
66                         kernel_path_ao(kg, sd, &emission_sd, &L_sample, &state, throughput, shader_bsdf_alpha(kg, sd));
67                 }
68
69                 /* sample emission */
70                 if((pass_filter & BAKE_FILTER_EMISSION) && (sd->flag & SD_EMISSION)) {
71                         float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
72                         path_radiance_accum_emission(&L_sample, &state, throughput, emission);
73                 }
74
75                 bool is_sss_sample = false;
76
77 #ifdef __SUBSURFACE__
78                 /* sample subsurface scattering */
79                 if((pass_filter & BAKE_FILTER_SUBSURFACE) && (sd->flag & SD_BSSRDF)) {
80                         /* when mixing BSSRDF and BSDF closures we should skip BSDF lighting if scattering was successful */
81                         SubsurfaceIndirectRays ss_indirect;
82                         kernel_path_subsurface_init_indirect(&ss_indirect);
83                         if(kernel_path_subsurface_scatter(kg,
84                                                           sd,
85                                                           &emission_sd,
86                                                           &L_sample,
87                                                           &state,
88                                                           &ray,
89                                                           &throughput,
90                                                           &ss_indirect))
91                         {
92                                 while(ss_indirect.num_rays) {
93                                         kernel_path_subsurface_setup_indirect(kg,
94                                                                               &ss_indirect,
95                                                                               &state,
96                                                                               &ray,
97                                                                               &L_sample,
98                                                                               &throughput);
99                                         kernel_path_indirect(kg,
100                                                              &indirect_sd,
101                                                              &emission_sd,
102                                                              &ray,
103                                                              throughput,
104                                                              &state,
105                                                              &L_sample);
106                                 }
107                                 is_sss_sample = true;
108                         }
109                 }
110 #endif
111
112                 /* sample light and BSDF */
113                 if(!is_sss_sample && (pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT))) {
114                         kernel_path_surface_connect_light(kg, sd, &emission_sd, throughput, &state, &L_sample);
115
116                         if(kernel_path_surface_bounce(kg, sd, &throughput, &state, &L_sample.state, &ray)) {
117 #ifdef __LAMP_MIS__
118                                 state.ray_t = 0.0f;
119 #endif
120                                 /* compute indirect light */
121                                 kernel_path_indirect(kg, &indirect_sd, &emission_sd, &ray, throughput, &state, &L_sample);
122
123                                 /* sum and reset indirect light pass variables for the next samples */
124                                 path_radiance_sum_indirect(&L_sample);
125                                 path_radiance_reset_indirect(&L_sample);
126                         }
127                 }
128 #ifdef __BRANCHED_PATH__
129         }
130         else {
131                 /* branched path tracer */
132
133                 /* sample ambient occlusion */
134                 if(pass_filter & BAKE_FILTER_AO) {
135                         kernel_branched_path_ao(kg, sd, &emission_sd, &L_sample, &state, throughput);
136                 }
137
138                 /* sample emission */
139                 if((pass_filter & BAKE_FILTER_EMISSION) && (sd->flag & SD_EMISSION)) {
140                         float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
141                         path_radiance_accum_emission(&L_sample, &state, throughput, emission);
142                 }
143
144 #ifdef __SUBSURFACE__
145                 /* sample subsurface scattering */
146                 if((pass_filter & BAKE_FILTER_SUBSURFACE) && (sd->flag & SD_BSSRDF)) {
147                         /* when mixing BSSRDF and BSDF closures we should skip BSDF lighting if scattering was successful */
148                         kernel_branched_path_subsurface_scatter(kg, sd, &indirect_sd,
149                                 &emission_sd, &L_sample, &state, &ray, throughput);
150                 }
151 #endif
152
153                 /* sample light and BSDF */
154                 if(pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT)) {
155 #if defined(__EMISSION__)
156                         /* direct light */
157                         if(kernel_data.integrator.use_direct_light) {
158                                 int all = kernel_data.integrator.sample_all_lights_direct;
159                                 kernel_branched_path_surface_connect_light(kg,
160                                         sd, &emission_sd, &state, throughput, 1.0f, &L_sample, all);
161                         }
162 #endif
163
164                         /* indirect light */
165                         kernel_branched_path_surface_indirect_light(kg,
166                                 sd, &indirect_sd, &emission_sd, throughput, 1.0f, &state, &L_sample);
167                 }
168         }
169 #endif
170
171         /* accumulate into master L */
172         path_radiance_accum_sample(L, &L_sample);
173 }
174
175 ccl_device bool is_aa_pass(ShaderEvalType type)
176 {
177         switch(type) {
178                 case SHADER_EVAL_UV:
179                 case SHADER_EVAL_NORMAL:
180                         return false;
181                 default:
182                         return true;
183         }
184 }
185
186 /* this helps with AA but it's not the real solution as it does not AA the geometry
187  *  but it's better than nothing, thus committed */
188 ccl_device_inline float bake_clamp_mirror_repeat(float u, float max)
189 {
190         /* use mirror repeat (like opengl texture) so that if the barycentric
191          * coordinate goes past the end of the triangle it is not always clamped
192          * to the same value, gives ugly patterns */
193         u /= max;
194         float fu = floorf(u);
195         u = u - fu;
196
197         return ((((int)fu) & 1)? 1.0f - u: u) * max;
198 }
199
200 ccl_device_inline float3 kernel_bake_shader_bsdf(KernelGlobals *kg,
201                                                  ShaderData *sd,
202                                                  const ShaderEvalType type)
203 {
204         switch(type) {
205                 case SHADER_EVAL_DIFFUSE:
206                         return shader_bsdf_diffuse(kg, sd);
207                 case SHADER_EVAL_GLOSSY:
208                         return shader_bsdf_glossy(kg, sd);
209                 case SHADER_EVAL_TRANSMISSION:
210                         return shader_bsdf_transmission(kg, sd);
211 #ifdef __SUBSURFACE__
212                 case SHADER_EVAL_SUBSURFACE:
213                         return shader_bsdf_subsurface(kg, sd);
214 #endif
215                 default:
216                         kernel_assert(!"Unknown bake type passed to BSDF evaluate");
217                         return make_float3(0.0f, 0.0f, 0.0f);
218         }
219 }
220
221 ccl_device float3 kernel_bake_evaluate_direct_indirect(KernelGlobals *kg,
222                                                        ShaderData *sd,
223                                                        PathState *state,
224                                                        float3 direct,
225                                                        float3 indirect,
226                                                        const ShaderEvalType type,
227                                                        const int pass_filter)
228 {
229         float3 color;
230         const bool is_color = (pass_filter & BAKE_FILTER_COLOR) != 0;
231         const bool is_direct = (pass_filter & BAKE_FILTER_DIRECT) != 0;
232         const bool is_indirect = (pass_filter & BAKE_FILTER_INDIRECT) != 0;
233         float3 out = make_float3(0.0f, 0.0f, 0.0f);
234
235         if(is_color) {
236                 if(is_direct || is_indirect) {
237                         /* Leave direct and diffuse channel colored. */
238                         color = make_float3(1.0f, 1.0f, 1.0f);
239                 }
240                 else {
241                         /* surface color of the pass only */
242                         shader_eval_surface(kg, sd, state, 0);
243                         return kernel_bake_shader_bsdf(kg, sd, type);
244                 }
245         }
246         else {
247                 shader_eval_surface(kg, sd, state, 0);
248                 color = kernel_bake_shader_bsdf(kg, sd, type);
249         }
250
251         if(is_direct) {
252                 out += safe_divide_even_color(direct, color);
253         }
254
255         if(is_indirect) {
256                 out += safe_divide_even_color(indirect, color);
257         }
258
259         return out;
260 }
261
262 ccl_device void kernel_bake_evaluate(KernelGlobals *kg, ccl_global uint4 *input, ccl_global float4 *output,
263                                      ShaderEvalType type, int pass_filter, int i, int offset, int sample)
264 {
265         ShaderData sd;
266         PathState state = {0};
267         uint4 in = input[i * 2];
268         uint4 diff = input[i * 2 + 1];
269
270         float3 out = make_float3(0.0f, 0.0f, 0.0f);
271
272         int object = in.x;
273         int prim = in.y;
274
275         if(prim == -1)
276                 return;
277
278         float u = __uint_as_float(in.z);
279         float v = __uint_as_float(in.w);
280
281         float dudx = __uint_as_float(diff.x);
282         float dudy = __uint_as_float(diff.y);
283         float dvdx = __uint_as_float(diff.z);
284         float dvdy = __uint_as_float(diff.w);
285
286         int num_samples = kernel_data.integrator.aa_samples;
287
288         /* random number generator */
289         uint rng_hash = cmj_hash(offset + i, kernel_data.integrator.seed);
290
291         float filter_x, filter_y;
292         if(sample == 0) {
293                 filter_x = filter_y = 0.5f;
294         }
295         else {
296                 path_rng_2D(kg, rng_hash, sample, num_samples, PRNG_FILTER_U, &filter_x, &filter_y);
297         }
298
299         /* subpixel u/v offset */
300         if(sample > 0) {
301                 u = bake_clamp_mirror_repeat(u + dudx*(filter_x - 0.5f) + dudy*(filter_y - 0.5f), 1.0f);
302                 v = bake_clamp_mirror_repeat(v + dvdx*(filter_x - 0.5f) + dvdy*(filter_y - 0.5f), 1.0f - u);
303         }
304
305         /* triangle */
306         int shader;
307         float3 P, Ng;
308
309         triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
310
311         /* light passes */
312         PathRadiance L;
313
314         shader_setup_from_sample(kg, &sd,
315                                  P, Ng, Ng,
316                                  shader, object, prim,
317                                  u, v, 1.0f, 0.5f,
318                                  !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
319                                  LAMP_NONE);
320         sd.I = sd.N;
321
322         /* update differentials */
323         sd.dP.dx = sd.dPdu * dudx + sd.dPdv * dvdx;
324         sd.dP.dy = sd.dPdu * dudy + sd.dPdv * dvdy;
325         sd.du.dx = dudx;
326         sd.du.dy = dudy;
327         sd.dv.dx = dvdx;
328         sd.dv.dy = dvdy;
329
330         /* light passes if we need more than color */
331         if(pass_filter & ~BAKE_FILTER_COLOR)
332                 compute_light_pass(kg, &sd, &L, rng_hash, pass_filter, sample);
333
334         switch(type) {
335                 /* data passes */
336                 case SHADER_EVAL_NORMAL:
337                 {
338                         float3 N = sd.N;
339                         if((sd.flag & SD_HAS_BUMP)) {
340                                 shader_eval_surface(kg, &sd, &state, 0);
341                                 N = shader_bsdf_average_normal(kg, &sd);
342                         }
343
344                         /* encoding: normal = (2 * color) - 1 */
345                         out = N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
346                         break;
347                 }
348                 case SHADER_EVAL_UV:
349                 {
350                         out = primitive_uv(kg, &sd);
351                         break;
352                 }
353                 case SHADER_EVAL_EMISSION:
354                 {
355                         shader_eval_surface(kg, &sd, &state, 0);
356                         out = shader_emissive_eval(kg, &sd);
357                         break;
358                 }
359
360 #ifdef __PASSES__
361                 /* light passes */
362                 case SHADER_EVAL_AO:
363                 {
364                         out = L.ao;
365                         break;
366                 }
367                 case SHADER_EVAL_COMBINED:
368                 {
369                         if((pass_filter & BAKE_FILTER_COMBINED) == BAKE_FILTER_COMBINED) {
370                                 float alpha;
371                                 out = path_radiance_clamp_and_sum(kg, &L, &alpha);
372                                 break;
373                         }
374
375                         if((pass_filter & BAKE_FILTER_DIFFUSE_DIRECT) == BAKE_FILTER_DIFFUSE_DIRECT)
376                                 out += L.direct_diffuse;
377                         if((pass_filter & BAKE_FILTER_DIFFUSE_INDIRECT) == BAKE_FILTER_DIFFUSE_INDIRECT)
378                                 out += L.indirect_diffuse;
379
380                         if((pass_filter & BAKE_FILTER_GLOSSY_DIRECT) == BAKE_FILTER_GLOSSY_DIRECT)
381                                 out += L.direct_glossy;
382                         if((pass_filter & BAKE_FILTER_GLOSSY_INDIRECT) == BAKE_FILTER_GLOSSY_INDIRECT)
383                                 out += L.indirect_glossy;
384
385                         if((pass_filter & BAKE_FILTER_TRANSMISSION_DIRECT) == BAKE_FILTER_TRANSMISSION_DIRECT)
386                                 out += L.direct_transmission;
387                         if((pass_filter & BAKE_FILTER_TRANSMISSION_INDIRECT) == BAKE_FILTER_TRANSMISSION_INDIRECT)
388                                 out += L.indirect_transmission;
389
390                         if((pass_filter & BAKE_FILTER_SUBSURFACE_DIRECT) == BAKE_FILTER_SUBSURFACE_DIRECT)
391                                 out += L.direct_subsurface;
392                         if((pass_filter & BAKE_FILTER_SUBSURFACE_INDIRECT) == BAKE_FILTER_SUBSURFACE_INDIRECT)
393                                 out += L.indirect_subsurface;
394
395                         if((pass_filter & BAKE_FILTER_EMISSION) != 0)
396                                 out += L.emission;
397
398                         break;
399                 }
400                 case SHADER_EVAL_SHADOW:
401                 {
402                         out = make_float3(L.shadow.x, L.shadow.y, L.shadow.z);
403                         break;
404                 }
405                 case SHADER_EVAL_DIFFUSE:
406                 {
407                         out = kernel_bake_evaluate_direct_indirect(kg,
408                                                                    &sd,
409                                                                    &state,
410                                                                    L.direct_diffuse,
411                                                                    L.indirect_diffuse,
412                                                                    type,
413                                                                    pass_filter);
414                         break;
415                 }
416                 case SHADER_EVAL_GLOSSY:
417                 {
418                         out = kernel_bake_evaluate_direct_indirect(kg,
419                                                                    &sd,
420                                                                    &state,
421                                                                    L.direct_glossy,
422                                                                    L.indirect_glossy,
423                                                                    type,
424                                                                    pass_filter);
425                         break;
426                 }
427                 case SHADER_EVAL_TRANSMISSION:
428                 {
429                         out = kernel_bake_evaluate_direct_indirect(kg,
430                                                                    &sd,
431                                                                    &state,
432                                                                    L.direct_transmission,
433                                                                    L.indirect_transmission,
434                                                                    type,
435                                                                    pass_filter);
436                         break;
437                 }
438                 case SHADER_EVAL_SUBSURFACE:
439                 {
440 #ifdef __SUBSURFACE__
441                         out = kernel_bake_evaluate_direct_indirect(kg,
442                                                                    &sd,
443                                                                    &state,
444                                                                    L.direct_subsurface,
445                                                                    L.indirect_subsurface,
446                                                                    type,
447                                                                    pass_filter);
448 #endif
449                         break;
450                 }
451 #endif
452
453                 /* extra */
454                 case SHADER_EVAL_ENVIRONMENT:
455                 {
456                         /* setup ray */
457                         Ray ray;
458
459                         ray.P = make_float3(0.0f, 0.0f, 0.0f);
460                         ray.D = normalize(P);
461                         ray.t = 0.0f;
462 #ifdef __CAMERA_MOTION__
463                         ray.time = 0.5f;
464 #endif
465
466 #ifdef __RAY_DIFFERENTIALS__
467                         ray.dD = differential3_zero();
468                         ray.dP = differential3_zero();
469 #endif
470
471                         /* setup shader data */
472                         shader_setup_from_background(kg, &sd, &ray);
473
474                         /* evaluate */
475                         int flag = 0; /* we can't know which type of BSDF this is for */
476                         out = shader_eval_background(kg, &sd, &state, flag);
477                         break;
478                 }
479                 default:
480                 {
481                         /* no real shader, returning the position of the verts for debugging */
482                         out = normalize(P);
483                         break;
484                 }
485         }
486
487         /* write output */
488         const float output_fac = is_aa_pass(type)? 1.0f/num_samples: 1.0f;
489         const float4 scaled_result = make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
490
491         output[i] = (sample == 0)?  scaled_result: output[i] + scaled_result;
492 }
493
494 #endif  /* __BAKING__ */
495
496 ccl_device void kernel_shader_evaluate(KernelGlobals *kg,
497                                        ccl_global uint4 *input,
498                                        ccl_global float4 *output,
499                                        ccl_global float *output_luma,
500                                        ShaderEvalType type,
501                                        int i,
502                                        int sample)
503 {
504         ShaderData sd;
505         PathState state = {0};
506         uint4 in = input[i];
507         float3 out;
508
509         if(type == SHADER_EVAL_DISPLACE) {
510                 /* setup shader data */
511                 int object = in.x;
512                 int prim = in.y;
513                 float u = __uint_as_float(in.z);
514                 float v = __uint_as_float(in.w);
515
516                 shader_setup_from_displace(kg, &sd, object, prim, u, v);
517
518                 /* evaluate */
519                 float3 P = sd.P;
520                 shader_eval_displacement(kg, &sd, &state);
521                 out = sd.P - P;
522
523                 object_inverse_dir_transform(kg, &sd, &out);
524         }
525         else { // SHADER_EVAL_BACKGROUND
526                 /* setup ray */
527                 Ray ray;
528                 float u = __uint_as_float(in.x);
529                 float v = __uint_as_float(in.y);
530
531                 ray.P = make_float3(0.0f, 0.0f, 0.0f);
532                 ray.D = equirectangular_to_direction(u, v);
533                 ray.t = 0.0f;
534 #ifdef __CAMERA_MOTION__
535                 ray.time = 0.5f;
536 #endif
537
538 #ifdef __RAY_DIFFERENTIALS__
539                 ray.dD = differential3_zero();
540                 ray.dP = differential3_zero();
541 #endif
542
543                 /* setup shader data */
544                 shader_setup_from_background(kg, &sd, &ray);
545
546                 /* evaluate */
547                 int flag = 0; /* we can't know which type of BSDF this is for */
548                 out = shader_eval_background(kg, &sd, &state, flag);
549         }
550         
551         /* write output */
552         if(sample == 0) {
553                 if(output != NULL) {
554                         output[i] = make_float4(out.x, out.y, out.z, 0.0f);
555                 }
556                 if(output_luma != NULL) {
557                         output_luma[i] = average(out);
558                 }
559         }
560         else {
561                 if(output != NULL) {
562                         output[i] += make_float4(out.x, out.y, out.z, 0.0f);
563                 }
564                 if(output_luma != NULL) {
565                         output_luma[i] += average(out);
566                 }
567         }
568 }
569
570 CCL_NAMESPACE_END
571