afb631528309aa036ba9d4c44e5e80dc632bcc47
[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_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 /* this helps with AA but it's not the real solution as it does not AA the geometry
176  *  but it's better than nothing, thus committed */
177 ccl_device_inline float bake_clamp_mirror_repeat(float u, float max)
178 {
179         /* use mirror repeat (like opengl texture) so that if the barycentric
180          * coordinate goes past the end of the triangle it is not always clamped
181          * to the same value, gives ugly patterns */
182         u /= max;
183         float fu = floorf(u);
184         u = u - fu;
185
186         return ((((int)fu) & 1)? 1.0f - u: u) * max;
187 }
188
189 ccl_device_inline float3 kernel_bake_shader_bsdf(KernelGlobals *kg,
190                                                  ShaderData *sd,
191                                                  const ShaderEvalType type)
192 {
193         switch(type) {
194                 case SHADER_EVAL_DIFFUSE:
195                         return shader_bsdf_diffuse(kg, sd);
196                 case SHADER_EVAL_GLOSSY:
197                         return shader_bsdf_glossy(kg, sd);
198                 case SHADER_EVAL_TRANSMISSION:
199                         return shader_bsdf_transmission(kg, sd);
200 #ifdef __SUBSURFACE__
201                 case SHADER_EVAL_SUBSURFACE:
202                         return shader_bsdf_subsurface(kg, sd);
203 #endif
204                 default:
205                         kernel_assert(!"Unknown bake type passed to BSDF evaluate");
206                         return make_float3(0.0f, 0.0f, 0.0f);
207         }
208 }
209
210 ccl_device float3 kernel_bake_evaluate_direct_indirect(KernelGlobals *kg,
211                                                        ShaderData *sd,
212                                                        PathState *state,
213                                                        float3 direct,
214                                                        float3 indirect,
215                                                        const ShaderEvalType type,
216                                                        const int pass_filter)
217 {
218         float3 color;
219         const bool is_color = (pass_filter & BAKE_FILTER_COLOR) != 0;
220         const bool is_direct = (pass_filter & BAKE_FILTER_DIRECT) != 0;
221         const bool is_indirect = (pass_filter & BAKE_FILTER_INDIRECT) != 0;
222         float3 out = make_float3(0.0f, 0.0f, 0.0f);
223
224         if(is_color) {
225                 if(is_direct || is_indirect) {
226                         /* Leave direct and diffuse channel colored. */
227                         color = make_float3(1.0f, 1.0f, 1.0f);
228                 }
229                 else {
230                         /* surface color of the pass only */
231                         shader_eval_surface(kg, sd, state, 0);
232                         return kernel_bake_shader_bsdf(kg, sd, type);
233                 }
234         }
235         else {
236                 shader_eval_surface(kg, sd, state, 0);
237                 color = kernel_bake_shader_bsdf(kg, sd, type);
238         }
239
240         if(is_direct) {
241                 out += safe_divide_even_color(direct, color);
242         }
243
244         if(is_indirect) {
245                 out += safe_divide_even_color(indirect, color);
246         }
247
248         return out;
249 }
250
251 ccl_device void kernel_bake_evaluate(KernelGlobals *kg, ccl_global uint4 *input, ccl_global float4 *output,
252                                      ShaderEvalType type, int pass_filter, int i, int offset, int sample)
253 {
254         ShaderData sd;
255         PathState state = {0};
256         uint4 in = input[i * 2];
257         uint4 diff = input[i * 2 + 1];
258
259         float3 out = make_float3(0.0f, 0.0f, 0.0f);
260
261         int object = in.x;
262         int prim = in.y;
263
264         if(prim == -1)
265                 return;
266
267         float u = __uint_as_float(in.z);
268         float v = __uint_as_float(in.w);
269
270         float dudx = __uint_as_float(diff.x);
271         float dudy = __uint_as_float(diff.y);
272         float dvdx = __uint_as_float(diff.z);
273         float dvdy = __uint_as_float(diff.w);
274
275         int num_samples = kernel_data.integrator.aa_samples;
276
277         /* random number generator */
278         uint rng_hash = cmj_hash(offset + i, kernel_data.integrator.seed);
279
280         float filter_x, filter_y;
281         if(sample == 0) {
282                 filter_x = filter_y = 0.5f;
283         }
284         else {
285                 path_rng_2D(kg, rng_hash, sample, num_samples, PRNG_FILTER_U, &filter_x, &filter_y);
286         }
287
288         /* subpixel u/v offset */
289         if(sample > 0) {
290                 u = bake_clamp_mirror_repeat(u + dudx*(filter_x - 0.5f) + dudy*(filter_y - 0.5f), 1.0f);
291                 v = bake_clamp_mirror_repeat(v + dvdx*(filter_x - 0.5f) + dvdy*(filter_y - 0.5f), 1.0f - u);
292         }
293
294         /* triangle */
295         int shader;
296         float3 P, Ng;
297
298         triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
299
300         /* light passes */
301         PathRadiance L;
302
303         shader_setup_from_sample(kg, &sd,
304                                  P, Ng, Ng,
305                                  shader, object, prim,
306                                  u, v, 1.0f, 0.5f,
307                                  !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
308                                  LAMP_NONE);
309         sd.I = sd.N;
310
311         /* update differentials */
312         sd.dP.dx = sd.dPdu * dudx + sd.dPdv * dvdx;
313         sd.dP.dy = sd.dPdu * dudy + sd.dPdv * dvdy;
314         sd.du.dx = dudx;
315         sd.du.dy = dudy;
316         sd.dv.dx = dvdx;
317         sd.dv.dy = dvdy;
318
319         /* set RNG state for shaders that use sampling */
320         state.rng_hash = rng_hash;
321         state.rng_offset = 0;
322         state.sample = sample;
323         state.num_samples = num_samples;
324         state.min_ray_pdf = FLT_MAX;
325
326         /* light passes if we need more than color */
327         if(pass_filter & ~BAKE_FILTER_COLOR)
328                 compute_light_pass(kg, &sd, &L, rng_hash, pass_filter, sample);
329
330         switch(type) {
331                 /* data passes */
332                 case SHADER_EVAL_NORMAL:
333                 case SHADER_EVAL_ROUGHNESS:
334                 case SHADER_EVAL_EMISSION:
335                 {
336                         if(type != SHADER_EVAL_NORMAL || (sd.flag & SD_HAS_BUMP)) {
337                                 int path_flag = (type == SHADER_EVAL_EMISSION) ? PATH_RAY_EMISSION : 0;
338                                 shader_eval_surface(kg, &sd, &state, path_flag);
339                         }
340
341                         if(type == SHADER_EVAL_NORMAL) {
342                                 float3 N = sd.N;
343                                 if(sd.flag & SD_HAS_BUMP) {
344                                         N = shader_bsdf_average_normal(kg, &sd);
345                                 }
346
347                                 /* encoding: normal = (2 * color) - 1 */
348                                 out = N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
349                         }
350                         else if(type == SHADER_EVAL_ROUGHNESS) {
351                                 float roughness = shader_bsdf_average_roughness(&sd);
352                                 out = make_float3(roughness, roughness, roughness);
353                         }
354                         else {
355                                 out = shader_emissive_eval(kg, &sd);
356                         }
357                         break;
358                 }
359                 case SHADER_EVAL_UV:
360                 {
361                         out = primitive_uv(kg, &sd);
362                         break;
363                 }
364 #ifdef __PASSES__
365                 /* light passes */
366                 case SHADER_EVAL_AO:
367                 {
368                         out = L.ao;
369                         break;
370                 }
371                 case SHADER_EVAL_COMBINED:
372                 {
373                         if((pass_filter & BAKE_FILTER_COMBINED) == BAKE_FILTER_COMBINED) {
374                                 float alpha;
375                                 out = path_radiance_clamp_and_sum(kg, &L, &alpha);
376                                 break;
377                         }
378
379                         if((pass_filter & BAKE_FILTER_DIFFUSE_DIRECT) == BAKE_FILTER_DIFFUSE_DIRECT)
380                                 out += L.direct_diffuse;
381                         if((pass_filter & BAKE_FILTER_DIFFUSE_INDIRECT) == BAKE_FILTER_DIFFUSE_INDIRECT)
382                                 out += L.indirect_diffuse;
383
384                         if((pass_filter & BAKE_FILTER_GLOSSY_DIRECT) == BAKE_FILTER_GLOSSY_DIRECT)
385                                 out += L.direct_glossy;
386                         if((pass_filter & BAKE_FILTER_GLOSSY_INDIRECT) == BAKE_FILTER_GLOSSY_INDIRECT)
387                                 out += L.indirect_glossy;
388
389                         if((pass_filter & BAKE_FILTER_TRANSMISSION_DIRECT) == BAKE_FILTER_TRANSMISSION_DIRECT)
390                                 out += L.direct_transmission;
391                         if((pass_filter & BAKE_FILTER_TRANSMISSION_INDIRECT) == BAKE_FILTER_TRANSMISSION_INDIRECT)
392                                 out += L.indirect_transmission;
393
394                         if((pass_filter & BAKE_FILTER_SUBSURFACE_DIRECT) == BAKE_FILTER_SUBSURFACE_DIRECT)
395                                 out += L.direct_subsurface;
396                         if((pass_filter & BAKE_FILTER_SUBSURFACE_INDIRECT) == BAKE_FILTER_SUBSURFACE_INDIRECT)
397                                 out += L.indirect_subsurface;
398
399                         if((pass_filter & BAKE_FILTER_EMISSION) != 0)
400                                 out += L.emission;
401
402                         break;
403                 }
404                 case SHADER_EVAL_SHADOW:
405                 {
406                         out = make_float3(L.shadow.x, L.shadow.y, L.shadow.z);
407                         break;
408                 }
409                 case SHADER_EVAL_DIFFUSE:
410                 {
411                         out = kernel_bake_evaluate_direct_indirect(kg,
412                                                                    &sd,
413                                                                    &state,
414                                                                    L.direct_diffuse,
415                                                                    L.indirect_diffuse,
416                                                                    type,
417                                                                    pass_filter);
418                         break;
419                 }
420                 case SHADER_EVAL_GLOSSY:
421                 {
422                         out = kernel_bake_evaluate_direct_indirect(kg,
423                                                                    &sd,
424                                                                    &state,
425                                                                    L.direct_glossy,
426                                                                    L.indirect_glossy,
427                                                                    type,
428                                                                    pass_filter);
429                         break;
430                 }
431                 case SHADER_EVAL_TRANSMISSION:
432                 {
433                         out = kernel_bake_evaluate_direct_indirect(kg,
434                                                                    &sd,
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                                                                    &state,
448                                                                    L.direct_subsurface,
449                                                                    L.indirect_subsurface,
450                                                                    type,
451                                                                    pass_filter);
452 #endif
453                         break;
454                 }
455 #endif
456
457                 /* extra */
458                 case SHADER_EVAL_ENVIRONMENT:
459                 {
460                         /* setup ray */
461                         Ray ray;
462
463                         ray.P = make_float3(0.0f, 0.0f, 0.0f);
464                         ray.D = normalize(P);
465                         ray.t = 0.0f;
466 #ifdef __CAMERA_MOTION__
467                         ray.time = 0.5f;
468 #endif
469
470 #ifdef __RAY_DIFFERENTIALS__
471                         ray.dD = differential3_zero();
472                         ray.dP = differential3_zero();
473 #endif
474
475                         /* setup shader data */
476                         shader_setup_from_background(kg, &sd, &ray);
477
478                         /* evaluate */
479                         int flag = 0; /* we can't know which type of BSDF this is for */
480                         out = shader_eval_background(kg, &sd, &state, flag);
481                         break;
482                 }
483                 default:
484                 {
485                         /* no real shader, returning the position of the verts for debugging */
486                         out = normalize(P);
487                         break;
488                 }
489         }
490
491         /* write output */
492         const float output_fac = 1.0f/num_samples;
493         const float4 scaled_result = make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
494
495         output[i] = (sample == 0)? scaled_result: output[i] + scaled_result;
496 }
497
498 #endif  /* __BAKING__ */
499
500 ccl_device void kernel_displace_evaluate(KernelGlobals *kg,
501                                          ccl_global uint4 *input,
502                                          ccl_global float4 *output,
503                                          int i)
504 {
505         ShaderData sd;
506         PathState state = {0};
507         uint4 in = input[i];
508
509         /* setup shader data */
510         int object = in.x;
511         int prim = in.y;
512         float u = __uint_as_float(in.z);
513         float v = __uint_as_float(in.w);
514
515         shader_setup_from_displace(kg, &sd, object, prim, u, v);
516
517         /* evaluate */
518         float3 P = sd.P;
519         shader_eval_displacement(kg, &sd, &state);
520         float3 D = sd.P - P;
521
522         object_inverse_dir_transform(kg, &sd, &D);
523
524         /* write output */
525         output[i] += make_float4(D.x, D.y, D.z, 0.0f);
526 }
527
528 ccl_device void kernel_background_evaluate(KernelGlobals *kg,
529                                            ccl_global uint4 *input,
530                                            ccl_global float4 *output,
531                                            int i)
532 {
533         ShaderData sd;
534         PathState state = {0};
535         uint4 in = input[i];
536
537         /* setup ray */
538         Ray ray;
539         float u = __uint_as_float(in.x);
540         float v = __uint_as_float(in.y);
541
542         ray.P = make_float3(0.0f, 0.0f, 0.0f);
543         ray.D = equirectangular_to_direction(u, v);
544         ray.t = 0.0f;
545 #ifdef __CAMERA_MOTION__
546         ray.time = 0.5f;
547 #endif
548
549 #ifdef __RAY_DIFFERENTIALS__
550         ray.dD = differential3_zero();
551         ray.dP = differential3_zero();
552 #endif
553
554         /* setup shader data */
555         shader_setup_from_background(kg, &sd, &ray);
556
557         /* evaluate */
558         int flag = 0; /* we can't know which type of BSDF this is for */
559         float3 color = shader_eval_background(kg, &sd, &state, flag);
560
561         /* write output */
562         output[i] += make_float4(color.x, color.y, color.z, 0.0f);
563 }
564
565 CCL_NAMESPACE_END