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