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