Blender V5.3
node_fn_hash_value.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_hash_c.hh"
7#include "BLI_noise.hh"
8
9#include "NOD_rna_define.hh"
11
12#include "RNA_enum_types.hh"
13
14#include "node_function_util.hh"
15#include "node_shader_util.hh"
16
18#include "UI_resources.hh"
19
21
23{
24 b.is_function_node();
25
26 const bNode *node = b.node_or_null();
27 if (node) {
28 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
29 b.add_input(data_type, "Value"_ustr);
30 }
31 b.add_input<decl::Int>("Seed"_ustr, "Seed"_ustr);
32 b.add_output<decl::Int>("Hash"_ustr);
33}
34
35static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
36{
37 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
38}
39
40static void node_init(bNodeTree * /*tree*/, bNode *node)
41{
42 node->custom1 = SOCK_INT;
43}
44
45static const mf::MultiFunction *get_multi_function(const bNode &bnode)
46{
47 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(bnode.custom1);
48
49 static auto exec_preset = mf::build::exec_presets::AllSpanOrSingle();
50
51 static auto fn_hash_float = mf::build::SI2_SO<float, int, int>(
52 "Hash Float",
53 [](float a, int seed) { return noise::hash(noise::hash_float(a), seed); },
54 exec_preset);
55 static auto fn_hash_vector = mf::build::SI2_SO<float3, int, int>(
56 "Hash Vector",
57 [](float3 a, int seed) { return noise::hash(noise::hash_float(a), seed); },
58 exec_preset);
59 static auto fn_hash_color = mf::build::SI2_SO<ColorGeometry4f, int, int>(
60 "Hash Color",
61 [](ColorGeometry4f a, int seed) { return noise::hash(noise::hash_float(float4(a)), seed); },
62 exec_preset);
63 static auto fn_hash_int = mf::build::SI2_SO<int, int, int>(
64 "Hash Integer",
65 [](int a, int seed) { return noise::hash(noise::hash(a), seed); },
66 exec_preset);
67 static auto fn_hash_string = mf::build::SI2_SO<std::string, int, int>(
68 "Hash String",
69 [](std::string a, int seed) { return noise::hash(BLI_hash_string(a.c_str()), seed); },
70 exec_preset);
71 static auto fn_hash_rotation = mf::build::SI2_SO<math::Quaternion, int, int>(
72 "Hash Rotation",
73 [](math::Quaternion a, int seed) { return noise::hash(noise::hash_float(float4(a)), seed); },
74 exec_preset);
75 static auto fn_hash_matrix = mf::build::SI2_SO<float4x4, int, int>(
76 "Hash Matrix",
77 [](float4x4 a, int seed) { return noise::hash(noise::hash_float(a), seed); },
78 exec_preset);
79
80 switch (socket_type) {
81 case SOCK_MATRIX:
82 return &fn_hash_matrix;
83 case SOCK_ROTATION:
84 return &fn_hash_rotation;
85 case SOCK_STRING:
86 return &fn_hash_string;
87 case SOCK_FLOAT:
88 return &fn_hash_float;
89 case SOCK_VECTOR:
90 return &fn_hash_vector;
91 case SOCK_RGBA:
92 return &fn_hash_color;
93 case SOCK_INT:
94 return &fn_hash_int;
95 default:
97 return nullptr;
98 }
99}
100
102 public:
106 {
107 bNode &node = params.add_node("FunctionNodeHashValue"_ustr);
108 node.custom1 = socket_type;
110 }
111};
112
114{
115 const mf::MultiFunction *fn = get_multi_function(builder.node());
116 builder.set_matching_fn(fn);
117}
118
120{
121 eNodeSocketDatatype socket_type = params.other_socket().type;
122 if (!ELEM(socket_type,
125 SOCK_INT,
130 SOCK_RGBA))
131 {
132 return;
133 }
134
135 if (params.in_out() == SOCK_IN) {
136 if (socket_type == SOCK_BOOLEAN) {
137 socket_type = SOCK_INT;
138 }
139 params.add_item(IFACE_("Value"), SocketSearchOp{"Value"_ustr, socket_type});
140 params.add_item(IFACE_("Seed"), SocketSearchOp{"Seed"_ustr, SOCK_INT});
141 }
142 else {
143 if (!ELEM(socket_type, SOCK_STRING)) {
144 const int weight = ELEM(params.other_socket().type, SOCK_INT) ? 0 : -1;
145 params.add_item(IFACE_("Hash"), SocketSearchOp{"Hash"_ustr, SOCK_INT}, weight);
146 }
147 }
148}
149
150static const char *gpu_shader_get_name(const eNodeSocketDatatype data_type)
151{
152 switch (data_type) {
153 case SOCK_FLOAT:
154 return "hash_value_float";
155 case SOCK_VECTOR:
156 return "hash_value_vector";
157 case SOCK_RGBA:
158 return "hash_value_color";
159 case SOCK_INT:
160 return "hash_value_int";
161 case SOCK_ROTATION:
162 return "hash_value_rotation";
163 case SOCK_MATRIX:
164 return "hash_value_matrix";
165 case SOCK_STRING:
166 /* Strings are single-value, so they are executed via the multi-function path. */
167 return nullptr;
168 default:
169 break;
170 }
171
173 return nullptr;
174}
175
177 bNode *node,
178 bNodeExecData * /*execdata*/,
181{
182 const char *name = gpu_shader_get_name(eNodeSocketDatatype(node->custom1));
183
184 if (name == nullptr) {
185 return 0;
186 }
187
188 return GPU_stack_link(mat, node, name, in, out);
189}
190
191static void node_rna(StructRNA *srna)
192{
194 srna,
195 "data_type",
196 "Data Type",
197 "",
200 SOCK_INT,
201 [](bContext * /*C*/, PointerRNA * /*ptr*/, PropertyRNA * /*prop*/, bool *r_free) {
202 *r_free = true;
204 [](const EnumPropertyItem &item) -> bool {
205 return ELEM(item.value,
207 SOCK_INT,
212 SOCK_RGBA);
213 });
214 });
215}
216
217static void node_register()
218{
219 static bke::bNodeType ntype;
220 fn_cmp_node_type_base(&ntype, "FunctionNodeHashValue"_ustr, FN_NODE_HASH_VALUE);
221 ntype.ui_name = "Hash Value";
222 ntype.ui_description = "Generate a randomized integer using the given input value as a seed";
223 ntype.enum_name_legacy = "HASH_VALUE";
225 ntype.declare = node_declare;
226 ntype.initfunc = node_init;
232
233 node_rna(ntype.rna_ext.srna);
234}
236
237} // namespace blender::nodes::node_fn_hash_value_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_HASH_VALUE
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define ELEM(...)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void set_matching_fn(const mf::MultiFunction *fn)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
QuaternionBase< float > Quaternion
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static const char * gpu_shader_get_name(const eNodeSocketDatatype data_type)
static void node_rna(StructRNA *srna)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
uint32_t hash_float(float kx)
Definition noise.cc:200
uint32_t hash(uint32_t kx)
Definition noise.cc:89
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash_c.hh:69
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
VecBase< float, 3 > float3
eNodeSocketDatatype type
Defines a node type.
Definition BKE_node.hh:265
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
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
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)