Blender V5.3
node_fn_bit_math.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string.hh"
6#include "BLI_string_utf8.hh"
7
8#include "RNA_enum_types.hh"
9
11#include "UI_resources.hh"
12
13#include "NOD_rna_define.hh"
15
17
18#include "node_function_util.hh"
19#include "node_shader_util.hh"
20
21namespace blender {
22
23static_assert(-1 == ~0, "Two's complement must be used for bitwise operations.");
24
26
28 And = 0,
29 Or = 1,
30 Xor = 2,
31 Not = 3,
32 Shift = 4,
33 Rotate = 5,
34};
35
36const std::array<EnumPropertyItem, 7> bit_math_operation_items = {{
38 "AND",
39 0,
40 "And",
41 "Returns a value where the bits of A and B are both set"},
43 "OR",
44 0,
45 "Or",
46 "Returns a value where the bits of either A or B are set"},
48 "XOR",
49 0,
50 "Exclusive Or",
51 "Returns a value where only one bit from A and B is set"},
53 "NOT",
54 0,
55 "Not",
56 "Returns the opposite bit value of A, in decimal it is equivalent of A = -A - 1"},
58 "SHIFT",
59 0,
60 "Shift",
61 "Shifts the bit values of A by the specified Shift amount. Positive values shift left, "
62 "negative values shift right."},
64 "ROTATE",
65 0,
66 "Rotate",
67 "Rotates the bit values of A by the specified Shift amount. Positive values rotate left, "
68 "negative values rotate right."},
69 {0, nullptr, 0, nullptr, nullptr},
70}};
71
72constexpr static int32_t max_shift = sizeof(int32_t) * CHAR_BIT - 1;
73constexpr static int32_t min_shift = -max_shift;
74
76{
77 b.is_function_node();
78 b.add_input<decl::Int>("A"_ustr);
79 auto &b_socket = b.add_input<decl::Int>("B"_ustr);
80 auto &shift = b.add_input<decl::Int>("Shift"_ustr).min(min_shift).max(max_shift);
81 b.add_output<decl::Int>("Value"_ustr);
82
83 if (const bNode *node = b.node_or_null()) {
84 const BitMathOperation operation = BitMathOperation(node->custom1);
85 b_socket.available(!ELEM(
87 shift.available(ELEM(operation, BitMathOperation::Shift, BitMathOperation::Rotate));
88 }
89};
90
91static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
92{
93 layout.prop(ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
94}
95
97 public:
101 {
102 bNode &node = params.add_node("FunctionNodeBitMath"_ustr);
103 node.custom1 = int16_t(operation);
105 }
106};
107
109{
110 if (!params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_INT)) {
111 return;
112 }
113
114 const bool is_integer = params.other_socket().type == SOCK_INT;
115 const int weight = is_integer ? 0 : -1;
116
117 const UString socket_name = (params.in_out() == SOCK_OUT) ? "Value"_ustr : "A"_ustr;
118
119 for (const auto &item : bit_math_operation_items) {
120 if (item.name != nullptr && item.identifier[0] != '\0') {
121 params.add_item(
122 IFACE_(item.name), SocketSearchOp{socket_name, BitMathOperation(item.value)}, weight);
123 }
124 }
125}
126
127static void node_label(const bNodeTree * /*ntree*/,
128 const bNode *node,
129 char *label,
130 int label_maxncpy)
131{
132 const char *operation_name = IFACE_("Unknown");
133 /* NOTE: This assumes that the matching RNA enum property also uses the default i18n context, and
134 * needs to be kept manually in sync. */
136 bit_math_operation_items.data(), node->custom1, BLT_I18NCONTEXT_DEFAULT, &operation_name);
137 BLI_snprintf_utf8(label, label_maxncpy, IFACE_("Bitwise %s"), operation_name);
138}
139
140static const mf::MultiFunction *get_multi_function(const bNode &bnode)
141{
142 const BitMathOperation operation = BitMathOperation(bnode.custom1);
143 switch (operation) {
145 return &fn::multi_function::registry::lookup("int & int"_ustr);
147 return &fn::multi_function::registry::lookup("int | int"_ustr);
149 return &fn::multi_function::registry::lookup("int ^ int"_ustr);
151 return &fn::multi_function::registry::lookup("~int"_ustr);
153 return &fn::multi_function::registry::lookup("shift(int, int)"_ustr);
155 return &fn::multi_function::registry::lookup("rotate(int, int)"_ustr);
156 }
158 return nullptr;
159}
160
162{
163 const mf::MultiFunction *fn = get_multi_function(builder.node());
164 builder.set_matching_fn(fn);
165}
166
167static const char *gpu_shader_get_name(const BitMathOperation operation)
168{
169 switch (operation) {
171 return "bit_math_and";
173 return "bit_math_or";
175 return "bit_math_xor";
177 return "bit_math_not";
179 return "bit_math_shift";
181 return "bit_math_rotate";
182 }
183
185 return nullptr;
186}
187
189 bNode *node,
190 bNodeExecData * /*execdata*/,
193{
194 const BitMathOperation operation = BitMathOperation(node->custom1);
195 const char *name = gpu_shader_get_name(operation);
196
197 if (name == nullptr) {
198 return 0;
199 }
200
201 /* Avoid GPU_stack_link here because it creates uniforms for unavailable sockets. */
202 switch (operation) {
206 return GPU_link(mat,
207 name,
210 &GPU_node_get_output(*node, out, "Value").link);
212 return GPU_link(mat,
213 name,
215 &GPU_node_get_output(*node, out, "Value").link);
218 return GPU_link(mat,
219 name,
221 GPU_node_get_input_link(*node, in, "Shift"),
222 &GPU_node_get_output(*node, out, "Value").link);
223 }
224
226 return 0;
227}
228
229static void node_rna(StructRNA *srna)
230{
231 PropertyRNA *prop = RNA_def_node_enum(srna,
232 "operation",
233 "Operation",
234 "",
239}
240
241static void node_register()
242{
243 static bke::bNodeType ntype;
244
245 fn_cmp_node_type_base(&ntype, "FunctionNodeBitMath"_ustr);
246 ntype.ui_name = "Bit Math";
248 ntype.declare = node_declare;
249 ntype.labelfunc = node_label;
254 ntype.ui_description = "Perform bitwise operations on 32-bit integers";
255
257 node_rna(ntype.rna_ext.srna);
258}
260
261} // namespace nodes::node_fn_bit_math_cc
262
263} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define BLT_I18NCONTEXT_DEFAULT
#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)
bool is_integer(bool)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
const MultiFunction & lookup(UString id)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
const std::array< EnumPropertyItem, 7 > bit_math_operation_items
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static const char * gpu_shader_get_name(const BitMathOperation operation)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
static void node_rna(StructRNA *srna)
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)
BMesh Iterators.
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
GPUNodeLink * GPU_node_get_input_link(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeStack & GPU_node_get_output(const bNode &node, GPUNodeStack outputs[], StringRef identifier)
void rna_Node_socket_update(Main *bmain, Scene *scene, PointerRNA *ptr)
bool RNA_enum_name_gettexted(const EnumPropertyItem *item, int value, const char *translation_context, const char **r_name)
signed int int32_t
Definition BKE_text.h:78
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
signed short int16_t
Definition BKE_text.h:77
#define min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
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
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
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
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)