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