Code cleanup: move rng into path state.
[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                                           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, SHADER_CONTEXT_MAIN);
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, throughput, emission, state.bounce);
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.num_samples,
106                                                              &state,
107                                                              &L_sample);
108                                         kernel_path_subsurface_accum_indirect(&ss_indirect, &L_sample);
109                                 }
110                                 is_sss_sample = true;
111                         }
112                 }
113 #endif
114
115                 /* sample light and BSDF */
116                 if(!is_sss_sample && (pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT))) {
117                         kernel_path_surface_connect_light(kg, sd, &emission_sd, throughput, &state, &L_sample);
118
119                         if(kernel_path_surface_bounce(kg, sd, &throughput, &state, &L_sample, &ray)) {
120 #ifdef __LAMP_MIS__
121                                 state.ray_t = 0.0f;
122 #endif
123                                 /* compute indirect light */
124                                 kernel_path_indirect(kg, &indirect_sd, &emission_sd, &ray, throughput, 1, &state, &L_sample);
125
126                                 /* sum and reset indirect light pass variables for the next samples */
127                                 path_radiance_sum_indirect(&L_sample);
128                                 path_radiance_reset_indirect(&L_sample);
129                         }
130                 }
131 #ifdef __BRANCHED_PATH__
132         }
133         else {
134                 /* branched path tracer */
135
136                 /* sample ambient occlusion */
137                 if(pass_filter & BAKE_FILTER_AO) {
138                         kernel_branched_path_ao(kg, sd, &emission_sd, &L_sample, &state, throughput);
139                 }
140
141                 /* sample emission */
142                 if((pass_filter & BAKE_FILTER_EMISSION) && (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 #ifdef __SUBSURFACE__
148                 /* sample subsurface scattering */
149                 if((pass_filter & BAKE_FILTER_SUBSURFACE) && (sd->flag & SD_BSSRDF)) {
150                         /* when mixing BSSRDF and BSDF closures we should skip BSDF lighting if scattering was successful */
151                         kernel_branched_path_subsurface_scatter(kg, sd, &indirect_sd,
152                                 &emission_sd, &L_sample, &state, &ray, throughput);
153                 }
154 #endif
155
156                 /* sample light and BSDF */
157                 if(pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT)) {
158 #if defined(__EMISSION__)
159                         /* direct light */
160                         if(kernel_data.integrator.use_direct_light) {
161                                 int all = kernel_data.integrator.sample_all_lights_direct;
162                                 kernel_branched_path_surface_connect_light(kg,
163                                         sd, &emission_sd, &state, throughput, 1.0f, &L_sample, all);
164                         }
165 #endif
166
167                         /* indirect light */
168                         kernel_branched_path_surface_indirect_light(kg,
169                                 sd, &indirect_sd, &emission_sd, throughput, 1.0f, &state, &L_sample);
170                 }
171         }
172 #endif
173
174         /* accumulate into master L */
175         path_radiance_accum_sample(L, &L_sample, 1);
176 }
177
178 ccl_device bool is_aa_pass(ShaderEvalType type)
179 {
180         switch(type) {
181                 case SHADER_EVAL_UV:
182                 case SHADER_EVAL_NORMAL:
183                         return false;
184                 default:
185                         return true;
186         }
187 }
188
189 /* this helps with AA but it's not the real solution as it does not AA the geometry
190  *  but it's better than nothing, thus committed */
191 ccl_device_inline float bake_clamp_mirror_repeat(float u, float max)
192 {
193         /* use mirror repeat (like opengl texture) so that if the barycentric
194          * coordinate goes past the end of the triangle it is not always clamped
195          * to the same value, gives ugly patterns */
196         u /= max;
197         float fu = floorf(u);
198         u = u - fu;
199
200         return ((((int)fu) & 1)? 1.0f - u: u) * max;
201 }
202
203 ccl_device_inline float3 kernel_bake_shader_bsdf(KernelGlobals *kg,
204                                                  ShaderData *sd,
205                                                  const ShaderEvalType type)
206 {
207         switch(type) {
208                 case SHADER_EVAL_DIFFUSE:
209                         return shader_bsdf_diffuse(kg, sd);
210                 case SHADER_EVAL_GLOSSY:
211                         return shader_bsdf_glossy(kg, sd);
212                 case SHADER_EVAL_TRANSMISSION:
213                         return shader_bsdf_transmission(kg, sd);
214 #ifdef __SUBSURFACE__
215                 case SHADER_EVAL_SUBSURFACE:
216                         return shader_bsdf_subsurface(kg, sd);
217 #endif
218                 default:
219                         kernel_assert(!"Unknown bake type passed to BSDF evaluate");
220                         return make_float3(0.0f, 0.0f, 0.0f);
221         }
222 }
223
224 ccl_device float3 kernel_bake_evaluate_direct_indirect(KernelGlobals *kg,
225                                                        ShaderData *sd,
226                                                        PathState *state,
227                                                        float3 direct,
228                                                        float3 indirect,
229                                                        const ShaderEvalType type,
230                                                        const int pass_filter)
231 {
232         float3 color;
233         const bool is_color = (pass_filter & BAKE_FILTER_COLOR) != 0;
234         const bool is_direct = (pass_filter & BAKE_FILTER_DIRECT) != 0;
235         const bool is_indirect = (pass_filter & BAKE_FILTER_INDIRECT) != 0;
236         float3 out = make_float3(0.0f, 0.0f, 0.0f);
237
238         if(is_color) {
239                 if(is_direct || is_indirect) {
240                         /* Leave direct and diffuse channel colored. */
241                         color = make_float3(1.0f, 1.0f, 1.0f);
242                 }
243                 else {
244                         /* surface color of the pass only */
245                         shader_eval_surface(kg, sd, state, 0.0f, 0, SHADER_CONTEXT_MAIN);
246                         return kernel_bake_shader_bsdf(kg, sd, type);
247                 }
248         }
249         else {
250                 shader_eval_surface(kg, sd, state, 0.0f, 0, SHADER_CONTEXT_MAIN);
251                 color = kernel_bake_shader_bsdf(kg, sd, type);
252         }
253
254         if(is_direct) {
255                 out += safe_divide_even_color(direct, color);
256         }
257
258         if(is_indirect) {
259                 out += safe_divide_even_color(indirect, color);
260         }
261
262         return out;
263 }
264
265 ccl_device void kernel_bake_evaluate(KernelGlobals *kg, ccl_global uint4 *input, ccl_global float4 *output,
266                                      ShaderEvalType type, int pass_filter, int i, int offset, int sample)
267 {
268         ShaderData sd;
269         PathState state = {0};
270         uint4 in = input[i * 2];
271         uint4 diff = input[i * 2 + 1];
272
273         float3 out = make_float3(0.0f, 0.0f, 0.0f);
274
275         int object = in.x;
276         int prim = in.y;
277
278         if(prim == -1)
279                 return;
280
281         float u = __uint_as_float(in.z);
282         float v = __uint_as_float(in.w);
283
284         float dudx = __uint_as_float(diff.x);
285         float dudy = __uint_as_float(diff.y);
286         float dvdx = __uint_as_float(diff.z);
287         float dvdy = __uint_as_float(diff.w);
288
289         int num_samples = kernel_data.integrator.aa_samples;
290
291         /* random number generator */
292         uint rng_hash = cmj_hash(offset + i, kernel_data.integrator.seed);
293
294         float filter_x, filter_y;
295         if(sample == 0) {
296                 filter_x = filter_y = 0.5f;
297         }
298         else {
299                 path_rng_2D(kg, rng_hash, sample, num_samples, PRNG_FILTER_U, &filter_x, &filter_y);
300         }
301
302         /* subpixel u/v offset */
303         if(sample > 0) {
304                 u = bake_clamp_mirror_repeat(u + dudx*(filter_x - 0.5f) + dudy*(filter_y - 0.5f), 1.0f);
305                 v = bake_clamp_mirror_repeat(v + dvdx*(filter_x - 0.5f) + dvdy*(filter_y - 0.5f), 1.0f - u);
306         }
307
308         /* triangle */
309         int shader;
310         float3 P, Ng;
311
312         triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
313
314         /* light passes */
315         PathRadiance L;
316
317         shader_setup_from_sample(kg, &sd,
318                                  P, Ng, Ng,
319                                  shader, object, prim,
320                                  u, v, 1.0f, 0.5f,
321                                  !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
322                                  LAMP_NONE);
323         sd.I = sd.N;
324
325         /* update differentials */
326         sd.dP.dx = sd.dPdu * dudx + sd.dPdv * dvdx;
327         sd.dP.dy = sd.dPdu * dudy + sd.dPdv * dvdy;
328         sd.du.dx = dudx;
329         sd.du.dy = dudy;
330         sd.dv.dx = dvdx;
331         sd.dv.dy = dvdy;
332
333         /* light passes if we need more than color */
334         if(pass_filter & ~BAKE_FILTER_COLOR)
335                 compute_light_pass(kg, &sd, &L, rng_hash, pass_filter, sample);
336
337         switch(type) {
338                 /* data passes */
339                 case SHADER_EVAL_NORMAL:
340                 {
341                         if((sd.flag & SD_HAS_BUMP)) {
342                                 shader_eval_surface(kg, &sd, &state, 0.f, 0, SHADER_CONTEXT_MAIN);
343                         }
344
345                         /* compression: normal = (2 * color) - 1 */
346                         out = sd.N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
347                         break;
348                 }
349                 case SHADER_EVAL_UV:
350                 {
351                         out = primitive_uv(kg, &sd);
352                         break;
353                 }
354                 case SHADER_EVAL_EMISSION:
355                 {
356                         shader_eval_surface(kg, &sd, &state, 0.f, 0, SHADER_CONTEXT_EMISSION);
357                         out = shader_emissive_eval(kg, &sd);
358                         break;
359                 }
360
361 #ifdef __PASSES__
362                 /* light passes */
363                 case SHADER_EVAL_AO:
364                 {
365                         out = L.ao;
366                         break;
367                 }
368                 case SHADER_EVAL_COMBINED:
369                 {
370                         if((pass_filter & BAKE_FILTER_COMBINED) == BAKE_FILTER_COMBINED) {
371                                 out = path_radiance_clamp_and_sum(kg, &L);
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, SHADER_CONTEXT_MAIN);
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, SHADER_CONTEXT_MAIN);
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, SHADER_CONTEXT_MAIN);
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