Blender V5.3
node_shader_attribute.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10
11#include "node_util.hh"
12
14#include "UI_resources.hh"
15
16#include "BLI_ghash.hh"
17
18#include "RNA_access.hh"
19
20namespace blender {
21
23
25{
26 const bNodeTree *ntree = b.tree_or_null();
27 const bool is_gpu_internal = ntree && (ntree->flag & NTREE_IS_GPU_SHADER_INTERNAL);
28
29 b.add_input<decl::Int>("LightIndex"_ustr).available(is_gpu_internal);
30
31 b.add_output<decl::Color>("Color"_ustr);
32 b.add_output<decl::Vector>("Vector"_ustr);
33 b.add_output<decl::Float>("Factor"_ustr, "Fac"_ustr);
34 b.add_output<decl::Float>("Alpha"_ustr);
35}
36
37static void node_shader_buts_attribute(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
38{
39 layout.prop(ptr, "attribute_type", UI_ITEM_NONE, "", ICON_NONE);
40 layout.prop(ptr,
41 RNA_struct_find_property(ptr, "attribute_name"),
42 -1,
43 0,
45 "",
46 ICON_NONE,
47 IFACE_("Name"));
48}
49
51{
52 NodeShaderAttribute *attr = MEM_new<NodeShaderAttribute>("NodeShaderAttribute");
53 node->storage = attr;
54}
55
57 bNode *node,
58 bNodeExecData * /*execdata*/,
61{
62 NodeShaderAttribute *attr = static_cast<NodeShaderAttribute *>(node->storage);
63 float attr_hash = 0.0f;
64
65 GPUNodeLink *cd_attr;
66
67 switch (attr->type) {
69 if (!in[0].link) {
70 /* Error: Attribute node is not linked to a light accumulation node. */
71 return false;
72 }
73 else if (STREQ(attr->name, "is_sun")) {
74 GPU_link(mat, "node_attribute_light_is_sun", in[0].link, GPU_kernel_globals(), &cd_attr);
75 }
76 else if (STREQ(attr->name, "is_point")) {
77 GPU_link(mat, "node_attribute_light_is_point", in[0].link, GPU_kernel_globals(), &cd_attr);
78 }
79 else if (STREQ(attr->name, "is_spot")) {
80 GPU_link(mat, "node_attribute_light_is_spot", in[0].link, GPU_kernel_globals(), &cd_attr);
81 }
82 else if (STREQ(attr->name, "is_area")) {
83 GPU_link(mat, "node_attribute_light_is_area", in[0].link, GPU_kernel_globals(), &cd_attr);
84 }
85 else if (STREQ(attr->name, "cutoff_distance")) {
86 GPU_link(mat,
87 "node_attribute_light_cutoff_distance",
88 in[0].link,
90 &cd_attr);
91 }
92 else {
94 const bool use_dupli = false;
95 /* Mimic gpu_node_graph_add_uniform_attribute */
96 uint hash_code = BLI_ghashutil_strhash_p(attr->name) << 1 | (use_dupli ? 0 : 1);
97
98 attr_hash = *reinterpret_cast<float *>(&hash_code);
99 GPU_link(mat,
100 "node_attribute_light",
101 in[0].link,
102 GPU_uniform(&attr_hash),
104 &cd_attr);
105 }
106 break;
107 }
109 cd_attr = GPU_attribute(mat, CD_AUTO_FROM_NAME, attr->name);
110
111 if (STREQ(attr->name, "color")) {
112 GPU_link(mat, "node_attribute_color", GPU_kernel_globals(), cd_attr, &cd_attr);
113 }
114 else if (STREQ(attr->name, "temperature")) {
115 GPU_link(mat, "node_attribute_temperature", GPU_kernel_globals(), cd_attr, &cd_attr);
116 }
117 break;
118 }
120 cd_attr = GPU_layer_attribute(mat, attr->name);
121 break;
122 }
125 cd_attr = GPU_uniform_attribute(mat,
126 attr->name,
128 reinterpret_cast<uint32_t *>(&attr_hash));
129
130 GPU_link(mat,
131 "node_attribute_uniform",
132 cd_attr,
133 GPU_constant(&attr_hash),
136 &cd_attr);
137 break;
138 }
139 }
140
141 GPU_link(mat, "node_attribute", cd_attr, &out[0].link, &out[1].link, &out[2].link, &out[3].link);
142
143 if (attr->type == SHD_ATTRIBUTE_GEOMETRY) {
144 for (const auto [i, sock] : node->outputs.enumerate()) {
146 }
147 }
148
149 return 1;
150}
151
153#ifdef WITH_MATERIALX
154{
155 /* TODO: some outputs expected be implemented within the next iteration
156 * (see node-definition `<geompropvalue>`). */
157 return get_output_default(socket_out_->identifier, NodeItem::Type::Any);
158}
159#endif
161
162} // namespace nodes::node_shader_attribute_cc
163
164/* node type definition */
166{
167 namespace file_ns = nodes::node_shader_attribute_cc;
168
169 static bke::bNodeType ntype;
170
171 sh_node_type_base(&ntype, "ShaderNodeAttribute"_ustr, SH_NODE_ATTRIBUTE);
172 ntype.ui_name = "Attribute";
173 ntype.ui_description = "Retrieve attributes attached to objects or geometry";
174 ntype.enum_name_legacy = "ATTRIBUTE";
175 ntype.nclass = NODE_CLASS_INPUT;
176 ntype.declare = file_ns::node_declare;
177 ntype.draw_buttons = file_ns::node_shader_buts_attribute;
178 ntype.initfunc = file_ns::node_shader_init_attribute;
180 ntype, "NodeShaderAttribute", node_free_standard_storage, node_copy_standard_storage);
182 ntype.gpu_fn = file_ns::node_shader_gpu_attribute;
183 ntype.materialx_fn = file_ns::node_shader_materialx;
184
186}
187
188} // namespace blender
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define SH_NODE_ATTRIBUTE
#define IFACE_(msgid)
#define UI_ITEM_NONE
#define STREQ(a, b)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static int node_shader_gpu_attribute(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_init_attribute(bNodeTree *, bNode *node)
static void node_shader_buts_attribute(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
BMesh Iterators.
GPUNodeLink * GPU_uniform_attribute(GPUMaterial *mat, const char *name, bool use_dupli, uint32_t *r_hash)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
unsigned int uint
Definition BKE_text.h:55
@ SHD_ATTRIBUTE_INSTANCER
@ SHD_ATTRIBUTE_LIGHT
@ SHD_ATTRIBUTE_VIEW_LAYER
@ SHD_ATTRIBUTE_GEOMETRY
@ SHD_ATTRIBUTE_OBJECT
void register_node_type_sh_attribute()
@ GPU_MATFLAG_LIGHT_ATTRIBUTE
GPUNodeLink * GPU_layer_attribute(GPUMaterial *mat, const char *name)
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GPUNodeLink * GPU_uniform(const float *num)
unsigned int uint32_t
Definition BKE_text.h:81
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
GPUNodeLink * GPU_shading_data()
@ NTREE_IS_GPU_SHADER_INTERNAL
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
GPUNodeLink * GPU_kernel_globals()
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void search_link_ops_for_shader_material_lighting_node(nodes::GatherLinkSearchOpParams &params)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
eNodeTree_Flag flag
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)