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