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