Cycles: Replace __MAX_CLOSURE__ build option with runtime integrator variable
[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, kernel_data.integrator.max_closures);
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, kernel_data.integrator.max_closures);
232                         return kernel_bake_shader_bsdf(kg, sd, type);
233                 }
234         }
235         else {
236                 shader_eval_surface(kg, sd, state, 0, kernel_data.integrator.max_closures);
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                 {
334                         float3 N = sd.N;
335                         if((sd.flag & SD_HAS_BUMP)) {
336                                 shader_eval_surface(kg, &sd, &state, 0, kernel_data.integrator.max_closures);
337                                 N = shader_bsdf_average_normal(kg, &sd);
338                         }
339
340                         /* encoding: normal = (2 * color) - 1 */
341                         out = N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
342                         break;
343                 }
344                 case SHADER_EVAL_UV:
345                 {
346                         out = primitive_uv(kg, &sd);
347                         break;
348                 }
349                 case SHADER_EVAL_EMISSION:
350                 {
351                         shader_eval_surface(kg, &sd, &state, 0, 0);
352                         out = shader_emissive_eval(kg, &sd);
353                         break;
354                 }
355
356 #ifdef __PASSES__
357                 /* light passes */
358                 case SHADER_EVAL_AO:
359                 {
360                         out = L.ao;
361                         break;
362                 }
363                 case SHADER_EVAL_COMBINED:
364                 {
365                         if((pass_filter & BAKE_FILTER_COMBINED) == BAKE_FILTER_COMBINED) {
366                                 float alpha;
367                                 out = path_radiance_clamp_and_sum(kg, &L, &alpha);
368                                 break;
369                         }
370
371                         if((pass_filter & BAKE_FILTER_DIFFUSE_DIRECT) == BAKE_FILTER_DIFFUSE_DIRECT)
372                                 out += L.direct_diffuse;
373                         if((pass_filter & BAKE_FILTER_DIFFUSE_INDIRECT) == BAKE_FILTER_DIFFUSE_INDIRECT)
374                                 out += L.indirect_diffuse;
375
376                         if((pass_filter & BAKE_FILTER_GLOSSY_DIRECT) == BAKE_FILTER_GLOSSY_DIRECT)
377                                 out += L.direct_glossy;
378                         if((pass_filter & BAKE_FILTER_GLOSSY_INDIRECT) == BAKE_FILTER_GLOSSY_INDIRECT)
379                                 out += L.indirect_glossy;
380
381                         if((pass_filter & BAKE_FILTER_TRANSMISSION_DIRECT) == BAKE_FILTER_TRANSMISSION_DIRECT)
382                                 out += L.direct_transmission;
383                         if((pass_filter & BAKE_FILTER_TRANSMISSION_INDIRECT) == BAKE_FILTER_TRANSMISSION_INDIRECT)
384                                 out += L.indirect_transmission;
385
386                         if((pass_filter & BAKE_FILTER_SUBSURFACE_DIRECT) == BAKE_FILTER_SUBSURFACE_DIRECT)
387                                 out += L.direct_subsurface;
388                         if((pass_filter & BAKE_FILTER_SUBSURFACE_INDIRECT) == BAKE_FILTER_SUBSURFACE_INDIRECT)
389                                 out += L.indirect_subsurface;
390
391                         if((pass_filter & BAKE_FILTER_EMISSION) != 0)
392                                 out += L.emission;
393
394                         break;
395                 }
396                 case SHADER_EVAL_SHADOW:
397                 {
398                         out = make_float3(L.shadow.x, L.shadow.y, L.shadow.z);
399                         break;
400                 }
401                 case SHADER_EVAL_DIFFUSE:
402                 {
403                         out = kernel_bake_evaluate_direct_indirect(kg,
404                                                                    &sd,
405                                                                    &state,
406                                                                    L.direct_diffuse,
407                                                                    L.indirect_diffuse,
408                                                                    type,
409                                                                    pass_filter);
410                         break;
411                 }
412                 case SHADER_EVAL_GLOSSY:
413                 {
414                         out = kernel_bake_evaluate_direct_indirect(kg,
415                                                                    &sd,
416                                                                    &state,
417                                                                    L.direct_glossy,
418                                                                    L.indirect_glossy,
419                                                                    type,
420                                                                    pass_filter);
421                         break;
422                 }
423                 case SHADER_EVAL_TRANSMISSION:
424                 {
425                         out = kernel_bake_evaluate_direct_indirect(kg,
426                                                                    &sd,
427                                                                    &state,
428                                                                    L.direct_transmission,
429                                                                    L.indirect_transmission,
430                                                                    type,
431                                                                    pass_filter);
432                         break;
433                 }
434                 case SHADER_EVAL_SUBSURFACE:
435                 {
436 #ifdef __SUBSURFACE__
437                         out = kernel_bake_evaluate_direct_indirect(kg,
438                                                                    &sd,
439                                                                    &state,
440                                                                    L.direct_subsurface,
441                                                                    L.indirect_subsurface,
442                                                                    type,
443                                                                    pass_filter);
444 #endif
445                         break;
446                 }
447 #endif
448
449                 /* extra */
450                 case SHADER_EVAL_ENVIRONMENT:
451                 {
452                         /* setup ray */
453                         Ray ray;
454
455                         ray.P = make_float3(0.0f, 0.0f, 0.0f);
456                         ray.D = normalize(P);
457                         ray.t = 0.0f;
458 #ifdef __CAMERA_MOTION__
459                         ray.time = 0.5f;
460 #endif
461
462 #ifdef __RAY_DIFFERENTIALS__
463                         ray.dD = differential3_zero();
464                         ray.dP = differential3_zero();
465 #endif
466
467                         /* setup shader data */
468                         shader_setup_from_background(kg, &sd, &ray);
469
470                         /* evaluate */
471                         int flag = 0; /* we can't know which type of BSDF this is for */
472                         out = shader_eval_background(kg, &sd, &state, flag);
473                         break;
474                 }
475                 default:
476                 {
477                         /* no real shader, returning the position of the verts for debugging */
478                         out = normalize(P);
479                         break;
480                 }
481         }
482
483         /* write output */
484         const float output_fac = 1.0f/num_samples;
485         const float4 scaled_result = make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
486
487         output[i] = (sample == 0)? scaled_result: output[i] + scaled_result;
488 }
489
490 #endif  /* __BAKING__ */
491
492 ccl_device void kernel_displace_evaluate(KernelGlobals *kg,
493                                          ccl_global uint4 *input,
494                                          ccl_global float4 *output,
495                                          int i)
496 {
497         ShaderData sd;
498         PathState state = {0};
499         uint4 in = input[i];
500
501         /* setup shader data */
502         int object = in.x;
503         int prim = in.y;
504         float u = __uint_as_float(in.z);
505         float v = __uint_as_float(in.w);
506
507         shader_setup_from_displace(kg, &sd, object, prim, u, v);
508
509         /* evaluate */
510         float3 P = sd.P;
511         shader_eval_displacement(kg, &sd, &state);
512         float3 D = sd.P - P;
513
514         object_inverse_dir_transform(kg, &sd, &D);
515
516         /* write output */
517         output[i] += make_float4(D.x, D.y, D.z, 0.0f);
518 }
519
520 ccl_device void kernel_background_evaluate(KernelGlobals *kg,
521                                            ccl_global uint4 *input,
522                                            ccl_global float4 *output,
523                                            int i)
524 {
525         ShaderData sd;
526         PathState state = {0};
527         uint4 in = input[i];
528
529         /* setup ray */
530         Ray ray;
531         float u = __uint_as_float(in.x);
532         float v = __uint_as_float(in.y);
533
534         ray.P = make_float3(0.0f, 0.0f, 0.0f);
535         ray.D = equirectangular_to_direction(u, v);
536         ray.t = 0.0f;
537 #ifdef __CAMERA_MOTION__
538         ray.time = 0.5f;
539 #endif
540
541 #ifdef __RAY_DIFFERENTIALS__
542         ray.dD = differential3_zero();
543         ray.dP = differential3_zero();
544 #endif
545
546         /* setup shader data */
547         shader_setup_from_background(kg, &sd, &ray);
548
549         /* evaluate */
550         int flag = 0; /* we can't know which type of BSDF this is for */
551         float3 color = shader_eval_background(kg, &sd, &state, flag);
552
553         /* write output */
554         output[i] += make_float4(color.x, color.y, color.z, 0.0f);
555 }
556
557 CCL_NAMESPACE_END
558