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 /* Fresnel Node */
20
svm_node_fresnel(ShaderData * sd,float * stack,uint ior_offset,uint ior_value,uint node)21 ccl_device void svm_node_fresnel(
22 ShaderData *sd, float *stack, uint ior_offset, uint ior_value, uint node)
23 {
24 uint normal_offset, out_offset;
25 svm_unpack_node_uchar2(node, &normal_offset, &out_offset);
26 float eta = (stack_valid(ior_offset)) ? stack_load_float(stack, ior_offset) :
27 __uint_as_float(ior_value);
28 float3 normal_in = stack_valid(normal_offset) ? stack_load_float3(stack, normal_offset) : sd->N;
29
30 eta = fmaxf(eta, 1e-5f);
31 eta = (sd->flag & SD_BACKFACING) ? 1.0f / eta : eta;
32
33 float f = fresnel_dielectric_cos(dot(sd->I, normal_in), eta);
34
35 stack_store_float(stack, out_offset, f);
36 }
37
38 /* Layer Weight Node */
39
svm_node_layer_weight(ShaderData * sd,float * stack,uint4 node)40 ccl_device void svm_node_layer_weight(ShaderData *sd, float *stack, uint4 node)
41 {
42 uint blend_offset = node.y;
43 uint blend_value = node.z;
44
45 uint type, normal_offset, out_offset;
46 svm_unpack_node_uchar3(node.w, &type, &normal_offset, &out_offset);
47
48 float blend = (stack_valid(blend_offset)) ? stack_load_float(stack, blend_offset) :
49 __uint_as_float(blend_value);
50 float3 normal_in = (stack_valid(normal_offset)) ? stack_load_float3(stack, normal_offset) :
51 sd->N;
52
53 float f;
54
55 if (type == NODE_LAYER_WEIGHT_FRESNEL) {
56 float eta = fmaxf(1.0f - blend, 1e-5f);
57 eta = (sd->flag & SD_BACKFACING) ? eta : 1.0f / eta;
58
59 f = fresnel_dielectric_cos(dot(sd->I, normal_in), eta);
60 }
61 else {
62 f = fabsf(dot(sd->I, normal_in));
63
64 if (blend != 0.5f) {
65 blend = clamp(blend, 0.0f, 1.0f - 1e-5f);
66 blend = (blend < 0.5f) ? 2.0f * blend : 0.5f / (1.0f - blend);
67
68 f = powf(f, blend);
69 }
70
71 f = 1.0f - f;
72 }
73
74 stack_store_float(stack, out_offset, f);
75 }
76
77 CCL_NAMESPACE_END
78