Blender  V2.93
node_shader_bsdf_principled.c
Go to the documentation of this file.
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 
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,
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 
70  {SOCK_SHADER, N_("BSDF")},
71  {-1, ""},
72 };
73 
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 
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",
110  in[22].link,
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  uint flag = GPU_MATFLAG_GLOSSY;
138  if (use_diffuse) {
139  flag |= GPU_MATFLAG_DIFFUSE;
140  }
141  if (use_refract) {
142  flag |= GPU_MATFLAG_REFRACT;
143  }
144  if (use_subsurf) {
145  flag |= GPU_MATFLAG_SSS;
146  }
147 
148  float f_use_diffuse = use_diffuse ? 1.0f : 0.0f;
149  float f_use_clearcoat = use_clear ? 1.0f : 0.0f;
150  float f_use_refraction = use_refract ? 1.0f : 0.0f;
151  float use_multi_scatter = (node->custom1 == SHD_GLOSSY_MULTI_GGX) ? 1.0f : 0.0f;
152 
153  GPU_material_flag_set(mat, flag);
154 
155  return GPU_stack_link(mat,
156  node,
157  "node_bsdf_principled",
158  in,
159  out,
160  GPU_constant(&f_use_diffuse),
161  GPU_constant(&f_use_clearcoat),
162  GPU_constant(&f_use_refraction),
163  GPU_constant(&use_multi_scatter),
164  GPU_constant(&node->ssr_id),
165  GPU_constant(&node->sss_id),
166  sss_scale);
167 }
168 
170 {
171  bNodeSocket *sock;
172  int distribution = node->custom1;
173 
174  for (sock = node->inputs.first; sock; sock = sock->next) {
175  if (STREQ(sock->name, "Transmission Roughness")) {
176  if (distribution == SHD_GLOSSY_GGX) {
177  sock->flag &= ~SOCK_UNAVAIL;
178  }
179  else {
180  sock->flag |= SOCK_UNAVAIL;
181  }
182  }
183  }
184 }
185 
186 /* node type definition */
188 {
189  static bNodeType ntype;
190 
191  sh_node_type_base(&ntype, SH_NODE_BSDF_PRINCIPLED, "Principled BSDF", NODE_CLASS_SHADER, 0);
195  node_type_storage(&ntype, "", NULL, NULL);
198 
199  nodeRegisterType(&ntype);
200 }
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4645
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_update(struct bNodeType *ntype, void(*updatefunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4623
#define SH_NODE_BSDF_PRINCIPLED
Definition: BKE_node.h:1058
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define NODE_CLASS_SHADER
Definition: BKE_node.h:358
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_LARGE
Definition: BKE_node.h:373
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define STREQ(a, b)
#define N_(msgid)
@ SHD_SUBSURFACE_BURLEY
@ SOCK_HIDE_VALUE
@ SOCK_COMPACT
@ SOCK_UNAVAIL
#define SHD_GLOSSY_GGX
@ SOCK_VECTOR
@ SOCK_SHADER
@ SOCK_FLOAT
@ SOCK_RGBA
#define SHD_GLOSSY_MULTI_GGX
GPUNodeLink * GPU_builtin(eGPUBuiltin builtin)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, CustomDataType type, const char *name)
@ GPU_MATFLAG_GLOSSY
Definition: GPU_material.h:110
@ GPU_MATFLAG_REFRACT
Definition: GPU_material.h:111
@ GPU_MATFLAG_SSS
Definition: GPU_material.h:112
@ GPU_MATFLAG_DIFFUSE
Definition: GPU_material.h:109
GPUNodeLink * GPU_constant(const float *num)
@ GPU_WORLD_NORMAL
Definition: GPU_material.h:105
@ GPU_OBJECT_MATRIX
Definition: GPU_material.h:88
void GPU_material_flag_set(GPUMaterial *mat, eGPUMatFlag flag)
Definition: gpu_material.c:641
void GPU_material_sss_profile_create(GPUMaterial *material, float radii[3], const short *falloff_type, const float *sharpness)
Definition: gpu_material.c:502
bool GPU_link(GPUMaterial *mat, const char *name,...)
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ PROP_NONE
Definition: RNA_types.h:113
@ PROP_FACTOR
Definition: RNA_types.h:131
OperationNode * node
bNodeTree * ntree
#define socket_not_one(sock)
#define socket_not_zero(sock)
static bNodeSocketTemplate sh_node_bsdf_principled_out[]
static void node_shader_init_principled(bNodeTree *UNUSED(ntree), bNode *node)
static int node_shader_gpu_bsdf_principled(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_update_principled(bNodeTree *UNUSED(ntree), bNode *node)
void register_node_type_sh_bsdf_principled(void)
static bNodeSocketTemplate sh_node_bsdf_principled_in[]
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
struct GPUNodeLink * link
Definition: GPU_material.h:119
Compact definition of a node socket.
Definition: BKE_node.h:95
char name[64]
struct bNodeSocket * next
void * default_value
Defines a node type.
Definition: BKE_node.h:221