2b305e5488d487469546ba2dc8b697d28367bc3a
[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_combined, 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, 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_combined || 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_combined || is_sss_sample) && (sd->flag & SD_BSSRDF)) {
66                         /* when mixing BSSRDF and BSDF closures we should skip BSDF lighting if scattering was successful */
67                         if(kernel_path_subsurface_scatter(kg, sd, &L_sample, &state, &rng, &ray, &throughput))
68                                 is_sss_sample = true;
69                 }
70 #endif
71
72                 /* sample light and BSDF */
73                 if((!is_sss_sample) && (!is_ao)) {
74
75                         if(sd->flag & SD_EMISSION) {
76                                 float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
77                                 path_radiance_accum_emission(&L_sample, throughput, emission, state.bounce);
78                         }
79
80                         kernel_path_surface_connect_light(kg, &rng, sd, throughput, &state, &L_sample);
81
82                         if(kernel_path_surface_bounce(kg, &rng, sd, &throughput, &state, &L_sample, &ray)) {
83 #ifdef __LAMP_MIS__
84                                 state.ray_t = 0.0f;
85 #endif
86                                 /* compute indirect light */
87                                 kernel_path_indirect(kg, &rng, ray, throughput, 1, state, &L_sample);
88
89                                 /* sum and reset indirect light pass variables for the next samples */
90                                 path_radiance_sum_indirect(&L_sample);
91                                 path_radiance_reset_indirect(&L_sample);
92                         }
93                 }
94 #ifdef __BRANCHED_PATH__
95         }
96         else {
97                 /* branched path tracer */
98
99                 /* sample ambient occlusion */
100                 if(is_combined || is_ao) {
101                         kernel_branched_path_ao(kg, sd, &L_sample, &state, &rng, throughput);
102                 }
103
104 #ifdef __SUBSURFACE__
105                 /* sample subsurface scattering */
106                 if((is_combined || is_sss_sample) && (sd->flag & SD_BSSRDF)) {
107                         /* when mixing BSSRDF and BSDF closures we should skip BSDF lighting if scattering was successful */
108                         kernel_branched_path_subsurface_scatter(kg, sd, &L_sample, &state, &rng, &ray, throughput);
109                 }
110 #endif
111
112                 /* sample light and BSDF */
113                 if((!is_sss_sample) && (!is_ao)) {
114
115                         if(sd->flag & SD_EMISSION) {
116                                 float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
117                                 path_radiance_accum_emission(&L_sample, throughput, emission, state.bounce);
118                         }
119
120 #if defined(__EMISSION__)
121                         /* direct light */
122                         if(kernel_data.integrator.use_direct_light) {
123                                 bool all = kernel_data.integrator.sample_all_lights_direct;
124                                 kernel_branched_path_surface_connect_light(kg, &rng,
125                                         sd, &state, throughput, 1.0f, &L_sample, all);
126                         }
127 #endif
128
129                         /* indirect light */
130                         kernel_branched_path_surface_indirect_light(kg, &rng,
131                                 sd, throughput, 1.0f, &state, &L_sample);
132                 }
133         }
134 #endif
135
136         /* accumulate into master L */
137         path_radiance_accum_sample(L, &L_sample, 1);
138 }
139
140 ccl_device bool is_aa_pass(ShaderEvalType type)
141 {
142         switch(type) {
143                 case SHADER_EVAL_UV:
144                 case SHADER_EVAL_NORMAL:
145                         return false;
146                 default:
147                         return true;
148         }
149 }
150
151 ccl_device bool is_light_pass(ShaderEvalType type)
152 {
153         switch (type) {
154                 case SHADER_EVAL_AO:
155                 case SHADER_EVAL_COMBINED:
156                 case SHADER_EVAL_SHADOW:
157                 case SHADER_EVAL_DIFFUSE_DIRECT:
158                 case SHADER_EVAL_GLOSSY_DIRECT:
159                 case SHADER_EVAL_TRANSMISSION_DIRECT:
160                 case SHADER_EVAL_SUBSURFACE_DIRECT:
161                 case SHADER_EVAL_DIFFUSE_INDIRECT:
162                 case SHADER_EVAL_GLOSSY_INDIRECT:
163                 case SHADER_EVAL_TRANSMISSION_INDIRECT:
164                 case SHADER_EVAL_SUBSURFACE_INDIRECT:
165                         return true;
166                 default:
167                         return false;
168         }
169 }
170
171 /* this helps with AA but it's not the real solution as it does not AA the geometry
172  *  but it's better than nothing, thus committed */
173 ccl_device_inline float bake_clamp_mirror_repeat(float u)
174 {
175         /* use mirror repeat (like opengl texture) so that if the barycentric
176          * coordinate goes past the end of the triangle it is not always clamped
177          * to the same value, gives ugly patterns */
178         float fu = floorf(u);
179         u = u - fu;
180
181         return (((int)fu) & 1)? 1.0f - u: u;
182 }
183
184 ccl_device void kernel_bake_evaluate(KernelGlobals *kg, ccl_global uint4 *input, ccl_global float4 *output,
185                                      ShaderEvalType type, int i, int offset, int sample)
186 {
187         ShaderData sd;
188         uint4 in = input[i * 2];
189         uint4 diff = input[i * 2 + 1];
190
191         float3 out;
192
193         int object = in.x;
194         int prim = in.y;
195
196         if(prim == -1)
197                 return;
198
199         float u = __uint_as_float(in.z);
200         float v = __uint_as_float(in.w);
201
202         float dudx = __uint_as_float(diff.x);
203         float dudy = __uint_as_float(diff.y);
204         float dvdx = __uint_as_float(diff.z);
205         float dvdy = __uint_as_float(diff.w);
206
207         int num_samples = kernel_data.integrator.aa_samples;
208
209         /* random number generator */
210         RNG rng = cmj_hash(offset + i, kernel_data.integrator.seed);
211
212 #ifdef USE_BAKE_JITTER
213         float filter_x, filter_y;
214         if(sample == 0) {
215                 filter_x = filter_y = 0.5f;
216         }
217         else {
218                 path_rng_2D(kg, &rng, sample, num_samples, PRNG_FILTER_U, &filter_x, &filter_y);
219         }
220
221         /* subpixel u/v offset */
222         if(sample > 0) {
223                 u = bake_clamp_mirror_repeat(u + dudx*(filter_x - 0.5f) + dudy*(filter_y - 0.5f));
224                 v = bake_clamp_mirror_repeat(v + dvdx*(filter_x - 0.5f) + dvdy*(filter_y - 0.5f));
225         }
226 #endif
227
228         /* triangle */
229         int shader;
230         float3 P, Ng;
231
232         triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
233
234         /* dummy initilizations copied from SHADER_EVAL_DISPLACE */
235         float3 I = Ng;
236         float t = 0.0f;
237         float time = TIME_INVALID;
238         int bounce = 0;
239         int transparent_bounce = 0;
240
241         /* light passes */
242         PathRadiance L;
243
244         shader_setup_from_sample(kg, &sd, P, Ng, I, shader, object, prim, u, v, t, time, bounce, transparent_bounce);
245         sd.I = sd.N;
246
247         /* update differentials */
248         sd.dP.dx = sd.dPdu * dudx + sd.dPdv * dvdx;
249         sd.dP.dy = sd.dPdu * dudy + sd.dPdv * dvdy;
250         sd.du.dx = dudx;
251         sd.du.dy = dudy;
252         sd.dv.dx = dvdx;
253         sd.dv.dy = dvdy;
254
255         /* light passes */
256         if(is_light_pass(type)) {
257                 compute_light_pass(kg, &sd, &L, rng,
258                                    (type == SHADER_EVAL_COMBINED),
259                                    (type == SHADER_EVAL_AO),
260                                    (type == SHADER_EVAL_SUBSURFACE_DIRECT ||
261                                     type == SHADER_EVAL_SUBSURFACE_INDIRECT),
262                                    sample);
263         }
264
265         switch (type) {
266                 /* data passes */
267                 case SHADER_EVAL_NORMAL:
268                 {
269                         if((sd.flag & SD_HAS_BUMP)) {
270                                 shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
271                         }
272
273                         /* compression: normal = (2 * color) - 1 */
274                         out = sd.N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
275                         break;
276                 }
277                 case SHADER_EVAL_UV:
278                 {
279                         out = primitive_uv(kg, &sd);
280                         break;
281                 }
282                 case SHADER_EVAL_DIFFUSE_COLOR:
283                 {
284                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
285                         out = shader_bsdf_diffuse(kg, &sd);
286                         break;
287                 }
288                 case SHADER_EVAL_GLOSSY_COLOR:
289                 {
290                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
291                         out = shader_bsdf_glossy(kg, &sd);
292                         break;
293                 }
294                 case SHADER_EVAL_TRANSMISSION_COLOR:
295                 {
296                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
297                         out = shader_bsdf_transmission(kg, &sd);
298                         break;
299                 }
300                 case SHADER_EVAL_SUBSURFACE_COLOR:
301                 {
302 #ifdef __SUBSURFACE__
303                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
304                         out = shader_bsdf_subsurface(kg, &sd);
305 #endif
306                         break;
307                 }
308                 case SHADER_EVAL_EMISSION:
309                 {
310                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_EMISSION);
311                         out = shader_emissive_eval(kg, &sd);
312                         break;
313                 }
314
315 #ifdef __PASSES__
316                 /* light passes */
317                 case SHADER_EVAL_AO:
318                 {
319                         out = L.ao;
320                         break;
321                 }
322                 case SHADER_EVAL_COMBINED:
323                 {
324                         out = path_radiance_clamp_and_sum(kg, &L);
325                         break;
326                 }
327                 case SHADER_EVAL_SHADOW:
328                 {
329                         out = make_float3(L.shadow.x, L.shadow.y, L.shadow.z);
330                         break;
331                 }
332                 case SHADER_EVAL_DIFFUSE_DIRECT:
333                 {
334                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
335                         out = safe_divide_color(L.direct_diffuse, shader_bsdf_diffuse(kg, &sd));
336                         break;
337                 }
338                 case SHADER_EVAL_GLOSSY_DIRECT:
339                 {
340                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
341                         out = safe_divide_color(L.direct_glossy, shader_bsdf_glossy(kg, &sd));
342                         break;
343                 }
344                 case SHADER_EVAL_TRANSMISSION_DIRECT:
345                 {
346                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
347                         out = safe_divide_color(L.direct_transmission, shader_bsdf_transmission(kg, &sd));
348                         break;
349                 }
350                 case SHADER_EVAL_SUBSURFACE_DIRECT:
351                 {
352 #ifdef __SUBSURFACE__
353                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
354                         out = safe_divide_color(L.direct_subsurface, shader_bsdf_subsurface(kg, &sd));
355 #endif
356                         break;
357                 }
358                 case SHADER_EVAL_DIFFUSE_INDIRECT:
359                 {
360                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
361                         out = safe_divide_color(L.indirect_diffuse, shader_bsdf_diffuse(kg, &sd));
362                         break;
363                 }
364                 case SHADER_EVAL_GLOSSY_INDIRECT:
365                 {
366                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
367                         out = safe_divide_color(L.indirect_glossy, shader_bsdf_glossy(kg, &sd));
368                         break;
369                 }
370                 case SHADER_EVAL_TRANSMISSION_INDIRECT:
371                 {
372                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
373                         out = safe_divide_color(L.indirect_transmission, shader_bsdf_transmission(kg, &sd));
374                         break;
375                 }
376                 case SHADER_EVAL_SUBSURFACE_INDIRECT:
377                 {
378 #ifdef __SUBSURFACE__
379                         shader_eval_surface(kg, &sd, 0.f, 0, SHADER_CONTEXT_MAIN);
380                         out = safe_divide_color(L.indirect_subsurface, shader_bsdf_subsurface(kg, &sd));
381 #endif
382                         break;
383                 }
384 #endif
385
386                 /* extra */
387                 case SHADER_EVAL_ENVIRONMENT:
388                 {
389                         /* setup ray */
390                         Ray ray;
391
392                         ray.P = make_float3(0.0f, 0.0f, 0.0f);
393                         ray.D = normalize(P);
394                         ray.t = 0.0f;
395 #ifdef __CAMERA_MOTION__
396                         ray.time = 0.5f;
397 #endif
398
399 #ifdef __RAY_DIFFERENTIALS__
400                         ray.dD = differential3_zero();
401                         ray.dP = differential3_zero();
402 #endif
403
404                         /* setup shader data */
405                         shader_setup_from_background(kg, &sd, &ray, 0, 0);
406
407                         /* evaluate */
408                         int flag = 0; /* we can't know which type of BSDF this is for */
409                         out = shader_eval_background(kg, &sd, flag, SHADER_CONTEXT_MAIN);
410                         break;
411                 }
412                 default:
413                 {
414                         /* no real shader, returning the position of the verts for debugging */
415                         out = normalize(P);
416                         break;
417                 }
418         }
419
420         /* write output */
421         float output_fac = is_aa_pass(type)? 1.0f/num_samples: 1.0f;
422
423         if(sample == 0)
424                 output[i] = make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
425         else
426                 output[i] += make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
427 }
428
429 ccl_device void kernel_shader_evaluate(KernelGlobals *kg, ccl_global uint4 *input, ccl_global float4 *output, ShaderEvalType type, int i, int sample)
430 {
431         ShaderData sd;
432         uint4 in = input[i];
433         float3 out;
434
435         if(type == SHADER_EVAL_DISPLACE) {
436                 /* setup shader data */
437                 int object = in.x;
438                 int prim = in.y;
439                 float u = __uint_as_float(in.z);
440                 float v = __uint_as_float(in.w);
441
442                 shader_setup_from_displace(kg, &sd, object, prim, u, v);
443
444                 /* evaluate */
445                 float3 P = sd.P;
446                 shader_eval_displacement(kg, &sd, SHADER_CONTEXT_MAIN);
447                 out = sd.P - P;
448         }
449         else { // SHADER_EVAL_BACKGROUND
450                 /* setup ray */
451                 Ray ray;
452                 float u = __uint_as_float(in.x);
453                 float v = __uint_as_float(in.y);
454
455                 ray.P = make_float3(0.0f, 0.0f, 0.0f);
456                 ray.D = equirectangular_to_direction(u, v);
457                 ray.t = 0.0f;
458 #ifdef __CAMERA_MOTION__
459                 ray.time = 0.5f;
460 #endif
461
462 #ifdef __RAY_DIFFERENTIALS__
463                 ray.dD = differential3_zero();
464                 ray.dP = differential3_zero();
465 #endif
466
467                 /* setup shader data */
468                 shader_setup_from_background(kg, &sd, &ray, 0, 0);
469
470                 /* evaluate */
471                 int flag = 0; /* we can't know which type of BSDF this is for */
472                 out = shader_eval_background(kg, &sd, flag, SHADER_CONTEXT_MAIN);
473         }
474         
475         /* write output */
476         if(sample == 0)
477                 output[i] = make_float4(out.x, out.y, out.z, 0.0f);
478         else
479                 output[i] += make_float4(out.x, out.y, out.z, 0.0f);
480 }
481
482 CCL_NAMESPACE_END
483