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