1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2005 Blender Foundation.
17 * All rights reserved.
18 */
19
20 #include "../node_shader_util.h"
21
22 /* **************** OUTPUT ******************** */
23
24 static bNodeSocketTemplate sh_node_bsdf_principled_in[] = {
25 {SOCK_RGBA, N_("Base Color"), 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
26 {SOCK_FLOAT, N_("Subsurface"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
27 {SOCK_VECTOR,
28 N_("Subsurface Radius"),
29 1.0f,
30 0.2f,
31 0.1f,
32 0.0f,
33 0.0f,
34 100.0f,
35 PROP_NONE,
36 SOCK_COMPACT},
37 {SOCK_RGBA, N_("Subsurface Color"), 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
38 {SOCK_FLOAT, N_("Metallic"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
39 {SOCK_FLOAT, N_("Specular"), 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
40 {SOCK_FLOAT, N_("Specular Tint"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
41 {SOCK_FLOAT, N_("Roughness"), 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
42 {SOCK_FLOAT, N_("Anisotropic"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
43 {SOCK_FLOAT, N_("Anisotropic Rotation"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
44 {SOCK_FLOAT, N_("Sheen"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
45 {SOCK_FLOAT, N_("Sheen Tint"), 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
46 {SOCK_FLOAT, N_("Clearcoat"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
47 {SOCK_FLOAT, N_("Clearcoat Roughness"), 0.03f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
48 {SOCK_FLOAT, N_("IOR"), 1.45f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
49 {SOCK_FLOAT, N_("Transmission"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
50 {SOCK_FLOAT, N_("Transmission Roughness"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
51 {SOCK_RGBA, N_("Emission"), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
52 {SOCK_FLOAT, N_("Emission Strength"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000000.0f},
53 {SOCK_FLOAT, N_("Alpha"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
54 {SOCK_VECTOR, N_("Normal"), 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
55 {SOCK_VECTOR,
56 N_("Clearcoat Normal"),
57 0.0f,
58 0.0f,
59 0.0f,
60 1.0f,
61 -1.0f,
62 1.0f,
63 PROP_NONE,
64 SOCK_HIDE_VALUE},
65 {SOCK_VECTOR, N_("Tangent"), 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
66 {-1, ""},
67 };
68
69 static bNodeSocketTemplate sh_node_bsdf_principled_out[] = {
70 {SOCK_SHADER, N_("BSDF")},
71 {-1, ""},
72 };
73
node_shader_init_principled(bNodeTree * UNUSED (ntree),bNode * node)74 static void node_shader_init_principled(bNodeTree *UNUSED(ntree), bNode *node)
75 {
76 node->custom1 = SHD_GLOSSY_GGX;
77 node->custom2 = SHD_SUBSURFACE_BURLEY;
78 }
79
80 #define socket_not_zero(sock) (in[sock].link || (clamp_f(in[sock].vec[0], 0.0f, 1.0f) > 1e-5f))
81 #define socket_not_one(sock) \
82 (in[sock].link || (clamp_f(in[sock].vec[0], 0.0f, 1.0f) < 1.0f - 1e-5f))
83
node_shader_gpu_bsdf_principled(GPUMaterial * mat,bNode * node,bNodeExecData * UNUSED (execdata),GPUNodeStack * in,GPUNodeStack * out)84 static int node_shader_gpu_bsdf_principled(GPUMaterial *mat,
85 bNode *node,
86 bNodeExecData *UNUSED(execdata),
87 GPUNodeStack *in,
88 GPUNodeStack *out)
89 {
90 GPUNodeLink *sss_scale;
91
92 /* Normals */
93 if (!in[20].link) {
94 GPU_link(mat, "world_normals_get", &in[20].link);
95 }
96
97 /* Clearcoat Normals */
98 if (!in[21].link) {
99 GPU_link(mat, "world_normals_get", &in[21].link);
100 }
101
102 #if 0 /* Not used at the moment. */
103 /* Tangents */
104 if (!in[22].link) {
105 GPUNodeLink *orco = GPU_attribute(CD_ORCO, "");
106 GPU_link(mat, "tangent_orco_z", orco, &in[22].link);
107 GPU_link(mat,
108 "node_tangent",
109 GPU_builtin(GPU_WORLD_NORMAL),
110 in[22].link,
111 GPU_builtin(GPU_OBJECT_MATRIX),
112 &in[22].link);
113 }
114 #endif
115
116 bool use_diffuse = socket_not_one(4) && socket_not_one(15);
117 bool use_subsurf = socket_not_zero(1) && use_diffuse && node->sss_id > 0;
118 bool use_refract = socket_not_one(4) && socket_not_zero(15);
119 bool use_clear = socket_not_zero(12);
120
121 /* SSS Profile */
122 if (use_subsurf) {
123 static short profile = SHD_SUBSURFACE_BURLEY;
124 bNodeSocket *socket = BLI_findlink(&node->original->inputs, 2);
125 bNodeSocketValueRGBA *socket_data = socket->default_value;
126 /* For some reason it seems that the socket value is in ARGB format. */
127 GPU_material_sss_profile_create(mat, &socket_data->value[1], &profile, NULL);
128 }
129
130 if (in[2].link) {
131 sss_scale = in[2].link;
132 }
133 else {
134 GPU_link(mat, "set_rgb_one", &sss_scale);
135 }
136
137 /* Due to the manual effort done per config, we only optimize the most common permutations. */
138 char *node_name;
139 uint flag = 0;
140 if (!use_subsurf && use_diffuse && !use_refract && !use_clear) {
141 static char name[] = "node_bsdf_principled_dielectric";
142 node_name = name;
143 flag = GPU_MATFLAG_DIFFUSE | GPU_MATFLAG_GLOSSY;
144 }
145 else if (!use_subsurf && !use_diffuse && !use_refract && !use_clear) {
146 static char name[] = "node_bsdf_principled_metallic";
147 node_name = name;
148 flag = GPU_MATFLAG_GLOSSY;
149 }
150 else if (!use_subsurf && !use_diffuse && !use_refract && use_clear) {
151 static char name[] = "node_bsdf_principled_clearcoat";
152 node_name = name;
153 flag = GPU_MATFLAG_GLOSSY;
154 }
155 else if (use_subsurf && use_diffuse && !use_refract && !use_clear) {
156 static char name[] = "node_bsdf_principled_subsurface";
157 node_name = name;
158 flag = GPU_MATFLAG_DIFFUSE | GPU_MATFLAG_GLOSSY;
159 }
160 else if (!use_subsurf && !use_diffuse && use_refract && !use_clear && !socket_not_zero(4)) {
161 static char name[] = "node_bsdf_principled_glass";
162 node_name = name;
163 flag = GPU_MATFLAG_GLOSSY | GPU_MATFLAG_REFRACT;
164 }
165 else {
166 static char name[] = "node_bsdf_principled";
167 node_name = name;
168 flag = GPU_MATFLAG_DIFFUSE | GPU_MATFLAG_GLOSSY | GPU_MATFLAG_REFRACT;
169 }
170
171 if (use_subsurf) {
172 flag |= GPU_MATFLAG_SSS;
173 }
174
175 float use_multi_scatter = (node->custom1 == SHD_GLOSSY_MULTI_GGX) ? 1.0f : 0.0f;
176
177 GPU_material_flag_set(mat, flag);
178
179 return GPU_stack_link(mat,
180 node,
181 node_name,
182 in,
183 out,
184 GPU_builtin(GPU_VIEW_POSITION),
185 GPU_constant(&use_multi_scatter),
186 GPU_constant(&node->ssr_id),
187 GPU_constant(&node->sss_id),
188 sss_scale);
189 }
190
node_shader_update_principled(bNodeTree * UNUSED (ntree),bNode * node)191 static void node_shader_update_principled(bNodeTree *UNUSED(ntree), bNode *node)
192 {
193 bNodeSocket *sock;
194 int distribution = node->custom1;
195
196 for (sock = node->inputs.first; sock; sock = sock->next) {
197 if (STREQ(sock->name, "Transmission Roughness")) {
198 if (distribution == SHD_GLOSSY_GGX) {
199 sock->flag &= ~SOCK_UNAVAIL;
200 }
201 else {
202 sock->flag |= SOCK_UNAVAIL;
203 }
204 }
205 }
206 }
207
208 /* node type definition */
register_node_type_sh_bsdf_principled(void)209 void register_node_type_sh_bsdf_principled(void)
210 {
211 static bNodeType ntype;
212
213 sh_node_type_base(&ntype, SH_NODE_BSDF_PRINCIPLED, "Principled BSDF", NODE_CLASS_SHADER, 0);
214 node_type_socket_templates(&ntype, sh_node_bsdf_principled_in, sh_node_bsdf_principled_out);
215 node_type_size_preset(&ntype, NODE_SIZE_LARGE);
216 node_type_init(&ntype, node_shader_init_principled);
217 node_type_storage(&ntype, "", NULL, NULL);
218 node_type_gpu(&ntype, node_shader_gpu_bsdf_principled);
219 node_type_update(&ntype, node_shader_update_principled);
220
221 nodeRegisterType(&ntype);
222 }
223