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