2 * Copyright 2011-2013 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 ccl_device void compute_light_pass(KernelGlobals *kg, ShaderData *sd, PathRadiance *L, RNG rng,
22 int pass_filter, int sample)
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);
28 PathRadiance L_sample;
31 float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
33 ray.P = sd->P + sd->Ng;
36 #ifdef __CAMERA_MOTION__
37 ray.time = TIME_INVALID;
41 path_radiance_init(&L_sample, kernel_data.film.use_light_pass);
44 path_state_init(kg, &state, &rng, sample, NULL);
46 /* evaluate surface shader */
47 float rbsdf = path_state_rng_1D(kg, &rng, &state, PRNG_BSDF);
48 shader_eval_surface(kg, sd, &state, rbsdf, state.flag, SHADER_CONTEXT_MAIN);
50 /* TODO, disable the closures we won't need */
52 #ifdef __BRANCHED_PATH__
53 if(!kernel_data.integrator.branched) {
54 /* regular path tracer */
57 /* sample ambient occlusion */
58 if(pass_filter & BAKE_FILTER_AO) {
59 kernel_path_ao(kg, sd, &L_sample, &state, &rng, throughput);
63 if((pass_filter & BAKE_FILTER_EMISSION) && (sd->flag & SD_EMISSION)) {
64 float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
65 path_radiance_accum_emission(&L_sample, throughput, emission, state.bounce);
68 bool is_sss_sample = false;
71 /* sample subsurface scattering */
72 if((pass_filter & BAKE_FILTER_SUBSURFACE) && (sd->flag & SD_BSSRDF)) {
73 /* when mixing BSSRDF and BSDF closures we should skip BSDF lighting if scattering was successful */
74 SubsurfaceIndirectRays ss_indirect;
75 kernel_path_subsurface_init_indirect(&ss_indirect);
76 if(kernel_path_subsurface_scatter(kg,
85 while(ss_indirect.num_rays) {
86 kernel_path_subsurface_setup_indirect(kg,
92 kernel_path_indirect(kg,
99 kernel_path_subsurface_accum_indirect(&ss_indirect, &L_sample);
101 is_sss_sample = true;
106 /* sample light and BSDF */
107 if(!is_sss_sample && (pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT))) {
108 kernel_path_surface_connect_light(kg, &rng, sd, throughput, &state, &L_sample);
110 if(kernel_path_surface_bounce(kg, &rng, sd, &throughput, &state, &L_sample, &ray)) {
114 /* compute indirect light */
115 kernel_path_indirect(kg, &rng, &ray, throughput, 1, &state, &L_sample);
117 /* sum and reset indirect light pass variables for the next samples */
118 path_radiance_sum_indirect(&L_sample);
119 path_radiance_reset_indirect(&L_sample);
122 #ifdef __BRANCHED_PATH__
125 /* branched path tracer */
127 /* sample ambient occlusion */
128 if(pass_filter & BAKE_FILTER_AO) {
129 kernel_branched_path_ao(kg, sd, &L_sample, &state, &rng, throughput);
132 /* sample emission */
133 if((pass_filter & BAKE_FILTER_EMISSION) && (sd->flag & SD_EMISSION)) {
134 float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
135 path_radiance_accum_emission(&L_sample, throughput, emission, state.bounce);
138 #ifdef __SUBSURFACE__
139 /* sample subsurface scattering */
140 if((pass_filter & BAKE_FILTER_SUBSURFACE) && (sd->flag & SD_BSSRDF)) {
141 /* when mixing BSSRDF and BSDF closures we should skip BSDF lighting if scattering was successful */
142 kernel_branched_path_subsurface_scatter(kg, sd, &L_sample, &state, &rng, &ray, throughput);
146 /* sample light and BSDF */
147 if(pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT)) {
148 #if defined(__EMISSION__)
150 if(kernel_data.integrator.use_direct_light) {
151 int all = kernel_data.integrator.sample_all_lights_direct;
152 kernel_branched_path_surface_connect_light(kg, &rng,
153 sd, &state, throughput, 1.0f, &L_sample, all);
158 kernel_branched_path_surface_indirect_light(kg, &rng,
159 sd, throughput, 1.0f, &state, &L_sample);
164 /* accumulate into master L */
165 path_radiance_accum_sample(L, &L_sample, 1);
168 ccl_device bool is_aa_pass(ShaderEvalType type)
172 case SHADER_EVAL_NORMAL:
179 /* this helps with AA but it's not the real solution as it does not AA the geometry
180 * but it's better than nothing, thus committed */
181 ccl_device_inline float bake_clamp_mirror_repeat(float u, float max)
183 /* use mirror repeat (like opengl texture) so that if the barycentric
184 * coordinate goes past the end of the triangle it is not always clamped
185 * to the same value, gives ugly patterns */
187 float fu = floorf(u);
190 return ((((int)fu) & 1)? 1.0f - u: u) * max;
193 ccl_device_inline float3 kernel_bake_shader_bsdf(KernelGlobals *kg,
195 const ShaderEvalType type)
198 case SHADER_EVAL_DIFFUSE:
199 return shader_bsdf_diffuse(kg, sd);
200 case SHADER_EVAL_GLOSSY:
201 return shader_bsdf_glossy(kg, sd);
202 case SHADER_EVAL_TRANSMISSION:
203 return shader_bsdf_transmission(kg, sd);
204 #ifdef __SUBSURFACE__
205 case SHADER_EVAL_SUBSURFACE:
206 return shader_bsdf_subsurface(kg, sd);
209 kernel_assert(!"Unknown bake type passed to BSDF evaluate");
210 return make_float3(0.0f, 0.0f, 0.0f);
214 ccl_device float3 kernel_bake_evaluate_direct_indirect(KernelGlobals *kg,
219 const ShaderEvalType type,
220 const int pass_filter)
223 const bool is_color = (pass_filter & BAKE_FILTER_COLOR) != 0;
224 const bool is_direct = (pass_filter & BAKE_FILTER_DIRECT) != 0;
225 const bool is_indirect = (pass_filter & BAKE_FILTER_INDIRECT) != 0;
226 float3 out = make_float3(0.0f, 0.0f, 0.0f);
229 if(is_direct || is_indirect) {
230 /* Leave direct and diffuse channel colored. */
231 color = make_float3(1.0f, 1.0f, 1.0f);
234 /* surface color of the pass only */
235 shader_eval_surface(kg, sd, state, 0.0f, 0, SHADER_CONTEXT_MAIN);
236 return kernel_bake_shader_bsdf(kg, sd, type);
240 shader_eval_surface(kg, sd, state, 0.0f, 0, SHADER_CONTEXT_MAIN);
241 color = kernel_bake_shader_bsdf(kg, sd, type);
245 out += safe_divide_color(direct, color);
249 out += safe_divide_color(indirect, color);
255 ccl_device void kernel_bake_evaluate(KernelGlobals *kg, ccl_global uint4 *input, ccl_global float4 *output,
256 ShaderEvalType type, int pass_filter, int i, int offset, int sample)
259 PathState state = {0};
260 uint4 in = input[i * 2];
261 uint4 diff = input[i * 2 + 1];
263 float3 out = make_float3(0.0f, 0.0f, 0.0f);
271 float u = __uint_as_float(in.z);
272 float v = __uint_as_float(in.w);
274 float dudx = __uint_as_float(diff.x);
275 float dudy = __uint_as_float(diff.y);
276 float dvdx = __uint_as_float(diff.z);
277 float dvdy = __uint_as_float(diff.w);
279 int num_samples = kernel_data.integrator.aa_samples;
281 /* random number generator */
282 RNG rng = cmj_hash(offset + i, kernel_data.integrator.seed);
284 float filter_x, filter_y;
286 filter_x = filter_y = 0.5f;
289 path_rng_2D(kg, &rng, sample, num_samples, PRNG_FILTER_U, &filter_x, &filter_y);
292 /* subpixel u/v offset */
294 u = bake_clamp_mirror_repeat(u + dudx*(filter_x - 0.5f) + dudy*(filter_y - 0.5f), 1.0f);
295 v = bake_clamp_mirror_repeat(v + dvdx*(filter_x - 0.5f) + dvdy*(filter_y - 0.5f), 1.0f - u);
302 triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
304 /* dummy initilizations copied from SHADER_EVAL_DISPLACE */
307 float time = TIME_INVALID;
312 shader_setup_from_sample(kg, &sd, P, Ng, I, shader, object, prim, u, v, t, time);
315 /* update differentials */
316 sd.dP.dx = sd.dPdu * dudx + sd.dPdv * dvdx;
317 sd.dP.dy = sd.dPdu * dudy + sd.dPdv * dvdy;
323 /* light passes if we need more than color */
324 if(pass_filter & ~BAKE_FILTER_COLOR)
325 compute_light_pass(kg, &sd, &L, rng, pass_filter, sample);
329 case SHADER_EVAL_NORMAL:
331 if((sd.flag & SD_HAS_BUMP)) {
332 shader_eval_surface(kg, &sd, &state, 0.f, 0, SHADER_CONTEXT_MAIN);
335 /* compression: normal = (2 * color) - 1 */
336 out = sd.N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
341 out = primitive_uv(kg, &sd);
344 case SHADER_EVAL_EMISSION:
346 shader_eval_surface(kg, &sd, &state, 0.f, 0, SHADER_CONTEXT_EMISSION);
347 out = shader_emissive_eval(kg, &sd);
358 case SHADER_EVAL_COMBINED:
360 if((pass_filter & BAKE_FILTER_COMBINED) == BAKE_FILTER_COMBINED) {
361 out = path_radiance_clamp_and_sum(kg, &L);
365 if((pass_filter & BAKE_FILTER_DIFFUSE_DIRECT) == BAKE_FILTER_DIFFUSE_DIRECT)
366 out += L.direct_diffuse;
367 if((pass_filter & BAKE_FILTER_DIFFUSE_INDIRECT) == BAKE_FILTER_DIFFUSE_INDIRECT)
368 out += L.indirect_diffuse;
370 if((pass_filter & BAKE_FILTER_GLOSSY_DIRECT) == BAKE_FILTER_GLOSSY_DIRECT)
371 out += L.direct_glossy;
372 if((pass_filter & BAKE_FILTER_GLOSSY_INDIRECT) == BAKE_FILTER_GLOSSY_INDIRECT)
373 out += L.indirect_glossy;
375 if((pass_filter & BAKE_FILTER_TRANSMISSION_DIRECT) == BAKE_FILTER_TRANSMISSION_DIRECT)
376 out += L.direct_transmission;
377 if((pass_filter & BAKE_FILTER_TRANSMISSION_INDIRECT) == BAKE_FILTER_TRANSMISSION_INDIRECT)
378 out += L.indirect_transmission;
380 if((pass_filter & BAKE_FILTER_SUBSURFACE_DIRECT) == BAKE_FILTER_SUBSURFACE_DIRECT)
381 out += L.direct_subsurface;
382 if((pass_filter & BAKE_FILTER_SUBSURFACE_INDIRECT) == BAKE_FILTER_SUBSURFACE_INDIRECT)
383 out += L.indirect_subsurface;
385 if((pass_filter & BAKE_FILTER_EMISSION) != 0)
390 case SHADER_EVAL_SHADOW:
392 out = make_float3(L.shadow.x, L.shadow.y, L.shadow.z);
395 case SHADER_EVAL_DIFFUSE:
397 out = kernel_bake_evaluate_direct_indirect(kg,
406 case SHADER_EVAL_GLOSSY:
408 out = kernel_bake_evaluate_direct_indirect(kg,
417 case SHADER_EVAL_TRANSMISSION:
419 out = kernel_bake_evaluate_direct_indirect(kg,
422 L.direct_transmission,
423 L.indirect_transmission,
428 case SHADER_EVAL_SUBSURFACE:
430 #ifdef __SUBSURFACE__
431 out = kernel_bake_evaluate_direct_indirect(kg,
435 L.indirect_subsurface,
444 case SHADER_EVAL_ENVIRONMENT:
449 ray.P = make_float3(0.0f, 0.0f, 0.0f);
450 ray.D = normalize(P);
452 #ifdef __CAMERA_MOTION__
456 #ifdef __RAY_DIFFERENTIALS__
457 ray.dD = differential3_zero();
458 ray.dP = differential3_zero();
461 /* setup shader data */
462 shader_setup_from_background(kg, &sd, &ray);
465 int flag = 0; /* we can't know which type of BSDF this is for */
466 out = shader_eval_background(kg, &sd, &state, flag, SHADER_CONTEXT_MAIN);
471 /* no real shader, returning the position of the verts for debugging */
478 float output_fac = is_aa_pass(type)? 1.0f/num_samples: 1.0f;
481 output[i] = make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
483 output[i] += make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
486 #endif /* __BAKING__ */
488 ccl_device void kernel_shader_evaluate(KernelGlobals *kg,
489 ccl_global uint4 *input,
490 ccl_global float4 *output,
491 ccl_global float *output_luma,
497 PathState state = {0};
501 if(type == SHADER_EVAL_DISPLACE) {
502 /* setup shader data */
505 float u = __uint_as_float(in.z);
506 float v = __uint_as_float(in.w);
508 shader_setup_from_displace(kg, &sd, object, prim, u, v);
512 shader_eval_displacement(kg, &sd, &state, SHADER_CONTEXT_MAIN);
515 else { // SHADER_EVAL_BACKGROUND
518 float u = __uint_as_float(in.x);
519 float v = __uint_as_float(in.y);
521 ray.P = make_float3(0.0f, 0.0f, 0.0f);
522 ray.D = equirectangular_to_direction(u, v);
524 #ifdef __CAMERA_MOTION__
528 #ifdef __RAY_DIFFERENTIALS__
529 ray.dD = differential3_zero();
530 ray.dP = differential3_zero();
533 /* setup shader data */
534 shader_setup_from_background(kg, &sd, &ray);
537 int flag = 0; /* we can't know which type of BSDF this is for */
538 out = shader_eval_background(kg, &sd, &state, flag, SHADER_CONTEXT_MAIN);
544 output[i] = make_float4(out.x, out.y, out.z, 0.0f);
546 if(output_luma != NULL) {
547 output_luma[i] = average(out);
552 output[i] += make_float4(out.x, out.y, out.z, 0.0f);
554 if(output_luma != NULL) {
555 output_luma[i] += average(out);