Blender V5.3
node_fn_integer_math.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Foundation
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string.hh"
6
8
9#include "RNA_enum_types.hh"
10
12#include "UI_resources.hh"
13
15#include "NOD_rna_define.hh"
18
19#include "node_function_util.hh"
20#include "node_shader_util.hh"
21
23
25{
26 b.is_function_node();
27
28 b.add_input<decl::Int>("Value"_ustr).label_fn([](const bNode &node) {
29 switch (node.custom1) {
31 return IFACE_("Base");
32 default:
33 return IFACE_("Value");
34 }
35 });
36
37 b.add_input<decl::Int>("Value"_ustr, "Value_001"_ustr).label_fn([](const bNode &node) {
38 switch (node.custom1) {
40 return IFACE_("Multiplier");
42 return IFACE_("Exponent");
43 default:
44 return IFACE_("Value");
45 }
46 });
47 b.add_input<decl::Int>("Value"_ustr, "Value_002"_ustr).label_fn([](const bNode &node) {
48 switch (node.custom1) {
50 return IFACE_("Addend");
51 default:
52 return IFACE_("Value");
53 }
54 });
55 b.add_output<decl::Int>("Value"_ustr);
56};
57
58static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
59{
60 layout.prop(ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
61}
62
63static void node_update(bNodeTree *ntree, bNode *node)
64{
65 const bool one_input_ops = ELEM(
67 const bool three_input_ops = ELEM(node->custom1, NODE_INTEGER_MATH_MULTIPLY_ADD);
68
69 bNodeSocket *sockA = node->inputs.first();
70 bNodeSocket *sockB = sockA->next;
71 bNodeSocket *sockC = sockB->next;
72
73 bke::node_set_socket_availability(*ntree, *sockB, !one_input_ops);
74 bke::node_set_socket_availability(*ntree, *sockC, three_input_ops);
75}
76
78{
79 bNodeSocket *socket_2 = bke::node_find_socket(node, SOCK_IN, "Value_001"_ustr);
80 BLI_assert(socket_2 != nullptr);
81 int &value_2 = socket_2->default_value_typed<bNodeSocketValueInt>()->value;
82
83 switch (operation) {
92 value_2 = 1;
93 break;
94 }
95
96 default:
97 /* Use the default defined in the node declaration otherwise. */
98 break;
99 }
100}
113
115{
116 if (!params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_INT)) {
117 return;
118 }
119
120 const bool is_integer = params.other_socket().type == SOCK_INT;
121 const int weight = is_integer ? 0 : -1;
122
123 /* Add socket A operations. */
125 item->identifier != nullptr;
126 item++)
127 {
128 if (item->name != nullptr && item->identifier[0] != '\0') {
130 SocketSearchOp{"Value"_ustr, NodeIntegerMathOperation(item->value)},
131 weight);
132 }
133 }
134}
135
136static void node_label(const bNodeTree * /*ntree*/,
137 const bNode *node,
138 char *label,
139 int label_maxncpy)
140{
141 const char *name;
142 bool enum_label = RNA_enum_name(rna_enum_node_integer_math_items, node->custom1, &name);
143 if (!enum_label) {
144 name = CTX_N_(BLT_I18NCONTEXT_ID_NODETREE, "Unknown");
145 }
146 BLI_strncpy(label, CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, name), label_maxncpy);
147}
148
149static const mf::MultiFunction *get_multi_function(const bNode &bnode)
150{
151 switch (NodeIntegerMathOperation(bnode.custom1)) {
153 return &fn::multi_function::registry::lookup("int + int"_ustr);
155 return &fn::multi_function::registry::lookup("int - int"_ustr);
157 return &fn::multi_function::registry::lookup("int * int"_ustr);
159 return &fn::multi_function::registry::lookup("int / int"_ustr);
161 return &fn::multi_function::registry::lookup("floor(int, int)"_ustr);
163 return &fn::multi_function::registry::lookup("divide_ceil(int, int)"_ustr);
165 return &fn::multi_function::registry::lookup("divide_round(int, int)"_ustr);
167 return &fn::multi_function::registry::lookup("int ** int"_ustr);
169 return &fn::multi_function::registry::lookup("int * int + int"_ustr);
171 return &fn::multi_function::registry::lookup("mod_periodic(int, int)"_ustr);
173 return &fn::multi_function::registry::lookup("int % int"_ustr);
175 return &fn::multi_function::registry::lookup("abs(int)"_ustr);
177 return &fn::multi_function::registry::lookup("sign(int)"_ustr);
179 return &fn::multi_function::registry::lookup("min(int, int)"_ustr);
181 return &fn::multi_function::registry::lookup("max(int, int)"_ustr);
183 return &fn::multi_function::registry::lookup("gcd(int, int)"_ustr);
185 return &fn::multi_function::registry::lookup("lcm(int, int)"_ustr);
187 return &fn::multi_function::registry::lookup("-int"_ustr);
188 }
190 return nullptr;
191}
192
194{
195 const mf::MultiFunction *fn = get_multi_function(builder.node());
196 builder.set_matching_fn(fn);
197}
198
199static const char *gpu_shader_get_name(const NodeIntegerMathOperation operation)
200{
201 switch (operation) {
203 return "integer_math_add";
205 return "integer_math_subtract";
207 return "integer_math_multiply";
209 return "integer_math_divide";
211 return "integer_math_multiply_add";
213 return "integer_math_power";
215 return "integer_math_floored_modulo";
217 return "integer_math_absolute";
219 return "integer_math_minimum";
221 return "integer_math_maximum";
223 return "integer_math_gcd";
225 return "integer_math_lcm";
227 return "integer_math_negate";
229 return "integer_math_sign";
231 return "integer_math_divide_floor";
233 return "integer_math_divide_ceil";
235 return "integer_math_divide_round";
237 return "integer_math_modulo";
238 }
239
241 return nullptr;
242}
243
245 bNode *node,
246 bNodeExecData * /*execdata*/,
249{
250 const char *name = gpu_shader_get_name(NodeIntegerMathOperation(node->custom1));
251
252 if (name == nullptr) {
253 return 0;
254 }
255
256 return GPU_stack_link(mat, node, name, in, out);
257}
258
260{
261 using namespace value_elem;
263 switch (op) {
268 IntElem output_elem = params.get_input_elem<IntElem>("Value"_ustr);
269 output_elem.merge(params.get_input_elem<IntElem>("Value_001"_ustr));
270 params.set_output_elem("Value"_ustr, output_elem);
271 break;
272 }
273 default:
274 break;
275 }
276}
277
279{
281 switch (op) {
286 params.set_input_elem("Value"_ustr,
287 params.get_output_elem<value_elem::IntElem>("Value"_ustr));
288 break;
289 }
290 default:
291 break;
292 }
293}
294
296{
298 const UString first_input_id = "Value"_ustr;
299 const UString second_input_id = "Value_001"_ustr;
300 const UString output_id = "Value"_ustr;
301 switch (op) {
303 params.set_input(first_input_id,
304 params.get_output<int>(output_id) - params.get_input<int>(second_input_id));
305 break;
306 }
308 params.set_input(first_input_id,
309 params.get_output<int>(output_id) + params.get_input<int>(second_input_id));
310 break;
311 }
313 params.set_input(first_input_id,
314 math::safe_divide(params.get_output<int>(output_id),
315 params.get_input<int>(second_input_id)));
316 break;
317 }
319 params.set_input(first_input_id,
320 params.get_output<int>(output_id) * params.get_input<int>(second_input_id));
321 break;
322 }
323 default: {
324 break;
325 }
326 }
327}
328
329static void node_rna(StructRNA *srna)
330{
331 PropertyRNA *prop;
332
333 prop = RNA_def_node_enum(srna,
334 "operation",
335 "Operation",
336 "",
342}
343
344static void node_register()
345{
346 static bke::bNodeType ntype;
347
348 common_node_type_base(&ntype, "FunctionNodeIntegerMath"_ustr, FN_NODE_INTEGER_MATH);
349 ntype.ui_name = "Integer Math";
350 ntype.ui_description = "Perform various math operations on the given integer inputs";
351 ntype.enum_name_legacy = "INTEGER_MATH";
353 ntype.declare = node_declare;
354 ntype.labelfunc = node_label;
355 ntype.updatefunc = node_update;
363
365
366 node_rna(ntype.rna_ext.srna);
367}
369
370} // namespace blender::nodes::node_fn_integer_math_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_INTEGER_MATH
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
#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 set_input(const UString identifier, T value)
T get_input_elem(const UString identifier) const
void set_output_elem(const UString identifier, T elem)
void set_input_elem(const UString identifier, T elem)
bool is_integer(bool)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
const MultiFunction & lookup(UString id)
T safe_divide(const T &a, const T &b)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void int_math_input_defaults(bNode &node, const NodeIntegerMathOperation operation)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
static void node_eval_elem(value_elem::ElemEvalParams &params)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_declare(NodeDeclarationBuilder &b)
static const char * gpu_shader_get_name(const NodeIntegerMathOperation operation)
static void node_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void node_eval_inverse(inverse_eval::InverseEvalParams &params)
static void node_update(bNodeTree *ntree, bNode *node)
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)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ NODE_INTEGER_MATH_DIVIDE
@ NODE_INTEGER_MATH_LCM
@ NODE_INTEGER_MATH_DIVIDE_CEIL
@ NODE_INTEGER_MATH_MODULO
@ NODE_INTEGER_MATH_FLOORED_MODULO
@ NODE_INTEGER_MATH_MULTIPLY_ADD
@ NODE_INTEGER_MATH_DIVIDE_ROUND
@ NODE_INTEGER_MATH_GCD
@ NODE_INTEGER_MATH_ADD
@ NODE_INTEGER_MATH_MULTIPLY
@ NODE_INTEGER_MATH_MINIMUM
@ NODE_INTEGER_MATH_ABSOLUTE
@ NODE_INTEGER_MATH_POWER
@ NODE_INTEGER_MATH_NEGATE
@ NODE_INTEGER_MATH_MAXIMUM
@ NODE_INTEGER_MATH_SUBTRACT
@ NODE_INTEGER_MATH_SIGN
@ NODE_INTEGER_MATH_DIVIDE_FLOOR
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
const EnumPropertyItem rna_enum_node_integer_math_items[]
void rna_Node_socket_update(Main *bmain, Scene *scene, PointerRNA *ptr)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeSocketDatatype type
struct bNodeSocket * next
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:434
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:441
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:428
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
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)