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