Blender V5.3
node_fn_compare.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cmath>
6
7#include "BLI_listbase.hh"
9#include "BLI_string_utf8.hh"
10
11#include "BLT_translation.hh"
12
14#include "UI_resources.hh"
15
17
18#include "RNA_enum_types.hh"
19
21
22#include "node_function_util.hh"
23#include "node_shader_util.hh"
24
25#include "NOD_rna_define.hh"
27
29#include "DNA_image_types.h"
30#include "DNA_material_types.h"
31#include "DNA_object_types.h"
32#include "DNA_sound_types.h"
33#include "DNA_vfont_types.h"
34
36
38
39static bool is_supported_data_block_type(const bNodeTree *ntree,
40 const eNodeSocketDatatype data_type)
41{
42 if (!ELEM(data_type,
49 {
50 return false;
51 }
52 return bke::node_tree_type_supports_socket_type_static(ntree->type, data_type);
53}
54
56{
57 b.is_function_node();
58
59 const bNode *node = b.node_or_null();
60 const bNodeTree *ntree = b.tree_or_null();
61 if (node != nullptr) {
62 const NodeFunctionCompare &storage = node_storage(*node);
63 const NodeCompareOperation operation = NodeCompareOperation(storage.operation);
64 const eNodeSocketDatatype data_type = storage.data_type;
65 const NodeCompareMode mode = NodeCompareMode(storage.mode);
66
67 const bool type_is_float = ELEM(data_type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA);
68 const bool is_vector = data_type == SOCK_VECTOR;
69 const bool is_data_block = is_supported_data_block_type(ntree, data_type);
70
71 auto &a_input =
72 b.add_input(data_type, "A"_ustr).translation_context(BLT_I18NCONTEXT_ID_NODETREE);
73 auto &b_input =
74 b.add_input(data_type, "B"_ustr).translation_context(BLT_I18NCONTEXT_ID_NODETREE);
75
76 if (data_type == SOCK_STRING || is_data_block) {
77 a_input.optional_label();
78 b_input.optional_label();
79 }
80
81 if (is_vector && mode == NODE_COMPARE_MODE_DOT_PRODUCT) {
82 b.add_input<decl::Float>("C"_ustr).default_value(0.9f);
83 }
84
85 if (is_vector && mode == NODE_COMPARE_MODE_DIRECTION) {
86 b.add_input<decl::Float>("Angle"_ustr).default_value(0.0872665f).subtype(PROP_ANGLE);
87 }
88
89 if (type_is_float && ELEM(operation, NODE_COMPARE_EQUAL, NODE_COMPARE_NOT_EQUAL)) {
90 b.add_input<decl::Float>("Epsilon"_ustr).default_value(0.001);
91 }
92 }
93
94 b.add_output<decl::Bool>("Result"_ustr);
95}
96
97static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
98{
99 const NodeFunctionCompare &data = node_storage(*static_cast<const bNode *>(ptr->data));
100 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
101 if (data.data_type == SOCK_VECTOR) {
102 layout.prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
103 }
104 layout.prop(ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
105}
106
107static void node_init(bNodeTree * /*tree*/, bNode *node)
108{
109 NodeFunctionCompare *data = MEM_new<NodeFunctionCompare>(__func__);
110 data->operation = NODE_COMPARE_GREATER_THAN;
111 data->data_type = SOCK_FLOAT;
113 node->storage = data;
114}
115
117 public:
123 {
124 bNode &node = params.add_node("FunctionNodeCompare"_ustr);
125 node_storage(node).data_type = data_type;
126 node_storage(node).operation = operation;
127 node_storage(node).mode = mode;
129 }
130};
131
132static std::optional<eNodeSocketDatatype> get_compare_type_for_operation(
133 const bNodeTree &ntree, const eNodeSocketDatatype type, const NodeCompareOperation operation)
134{
135 switch (type) {
136 case SOCK_BOOLEAN:
138 return SOCK_RGBA;
139 }
140 return SOCK_INT;
141 case SOCK_INT:
142 case SOCK_FLOAT:
143 case SOCK_VECTOR:
145 return SOCK_RGBA;
146 }
147 return type;
148 case SOCK_RGBA:
149 if (!ELEM(operation,
154 {
155 return SOCK_VECTOR;
156 }
157 return type;
158 case SOCK_STRING:
160 return std::nullopt;
161 }
162 return type;
163 default:
164 if (is_supported_data_block_type(&ntree, type)) {
166 return std::nullopt;
167 }
168 return type;
169 }
170 return std::nullopt;
171 }
172}
173
175{
176 switch (value) {
178 return SOCK_FLOAT;
180 return SOCK_FLOAT;
182 return SOCK_FLOAT;
184 return SOCK_FLOAT;
186 return SOCK_INT;
188 return SOCK_INT;
190 return SOCK_RGBA;
192 return SOCK_RGBA;
193 }
195 return SOCK_RGBA;
196}
197
199{
200 const bNodeTree &ntree = params.node_tree();
201 const bool is_input = params.in_out() == SOCK_IN;
202 const eNodeSocketDatatype type = params.other_socket().type;
203 const bool can_link_to_result = !is_input && ntree.typeinfo->validate_link(SOCK_BOOLEAN, type);
205 !can_link_to_result && !is_supported_data_block_type(&ntree, type))
206 {
207 return;
208 }
209
210 if (!is_input) {
211 if (can_link_to_result) {
213 item->identifier != nullptr;
214 item++)
215 {
216 if (item->name == nullptr || item->identifier[0] == '\0') {
217 continue;
218 }
219 const NodeCompareOperation operation = NodeCompareOperation(item->value);
220 const eNodeSocketDatatype type = op_type_to_socket_type(operation);
221 params.add_item(IFACE_(item->name), SocketSearchOp{"Result"_ustr, type, operation});
222 }
223 }
224 return;
225 }
226
228 item->identifier != nullptr;
229 item++)
230 {
231 if (item->name != nullptr && item->identifier[0] != '\0') {
232 const NodeCompareOperation operation = NodeCompareOperation(item->value);
233 if (const std::optional<eNodeSocketDatatype> fixed_type = get_compare_type_for_operation(
234 ntree, type, operation))
235 {
236 params.add_item(IFACE_(item->name), SocketSearchOp{"A"_ustr, *fixed_type, operation});
237 }
238 }
239 }
240
241 if (params.in_out() != SOCK_IN) {
242 return;
243 }
244
245 if (type == SOCK_STRING || is_supported_data_block_type(&ntree, type)) {
246 return;
247 }
248
249 params.add_item(
250 IFACE_("Angle"),
253}
254
255static void node_label(const bNodeTree * /*tree*/,
256 const bNode *node,
257 char *label,
258 int label_maxncpy)
259{
261 const char *name;
262 bool enum_label = RNA_enum_name(rna_enum_node_compare_operation_items, data->operation, &name);
263 if (!enum_label) {
264 name = N_("Unknown");
265 }
266 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
267}
268
270{
271 return (a.x + a.y + a.z) / 3.0f;
272}
273
274template<typename Fn>
275static auto to_static_data_block_type(const eNodeSocketDatatype socket_type, Fn &&fn)
276{
277 switch (socket_type) {
278 case SOCK_OBJECT:
279 return fn.template operator()<Object>();
280 case SOCK_IMAGE:
281 return fn.template operator()<Image>();
282 case SOCK_COLLECTION:
283 return fn.template operator()<Collection>();
284 case SOCK_MATERIAL:
285 return fn.template operator()<Material>();
286 case SOCK_FONT:
287 return fn.template operator()<VFont>();
288 case SOCK_SOUND:
289 return fn.template operator()<bSound>();
290 default:
292 return fn.template operator()<Object>();
293 }
294}
295
296static bool data_blocks_are_equal(const ID *a, const ID *b)
297{
298 return DEG_get_original(a) == DEG_get_original(b);
299}
300
301static const mf::MultiFunction *get_multi_function(const bNode &node)
302{
304 const eNodeSocketDatatype data_type = data->data_type;
305 const bNodeTree &ntree = node.owner_tree();
306
307 static auto exec_preset_all = mf::build::exec_presets::AllSpanOrSingle();
308 static auto exec_preset_first_two = mf::build::exec_presets::SomeSpanOrSingle<0, 1>();
309
310 switch (data_type) {
311 case SOCK_FLOAT:
312 switch (data->operation) {
314 static auto fn = mf::build::SI2_SO<float, float, bool>(
315 "Less Than", [](float a, float b) { return a < b; }, exec_preset_all);
316 return &fn;
317 }
319 static auto fn = mf::build::SI2_SO<float, float, bool>(
320 "Less Equal", [](float a, float b) { return a <= b; }, exec_preset_all);
321 return &fn;
322 }
324 static auto fn = mf::build::SI2_SO<float, float, bool>(
325 "Greater Than", [](float a, float b) { return a > b; }, exec_preset_all);
326 return &fn;
327 }
329 static auto fn = mf::build::SI2_SO<float, float, bool>(
330 "Greater Equal", [](float a, float b) { return a >= b; }, exec_preset_all);
331 return &fn;
332 }
333 case NODE_COMPARE_EQUAL: {
334 static auto fn = mf::build::SI3_SO<float, float, float, bool>(
335 "Equal",
336 [](float a, float b, float epsilon) { return std::abs(a - b) <= epsilon; },
337 exec_preset_first_two);
338 return &fn;
339 }
341 static auto fn = mf::build::SI3_SO<float, float, float, bool>(
342 "Not Equal",
343 [](float a, float b, float epsilon) { return std::abs(a - b) > epsilon; },
344 exec_preset_first_two);
345 return &fn;
346 }
349 break;
350 }
351 break;
352 case SOCK_INT:
353 switch (data->operation) {
355 static auto fn = mf::build::SI2_SO<int, int, bool>(
356 "Less Than", [](int a, int b) { return a < b; }, exec_preset_all);
357 return &fn;
358 }
360 static auto fn = mf::build::SI2_SO<int, int, bool>(
361 "Less Equal", [](int a, int b) { return a <= b; }, exec_preset_all);
362 return &fn;
363 }
365 static auto fn = mf::build::SI2_SO<int, int, bool>(
366 "Greater Than", [](int a, int b) { return a > b; }, exec_preset_all);
367 return &fn;
368 }
370 static auto fn = mf::build::SI2_SO<int, int, bool>(
371 "Greater Equal", [](int a, int b) { return a >= b; }, exec_preset_all);
372 return &fn;
373 }
374 case NODE_COMPARE_EQUAL: {
375 static auto fn = mf::build::SI2_SO<int, int, bool>(
376 "Equal", [](int a, int b) { return a == b; }, exec_preset_all);
377 return &fn;
378 }
380 static auto fn = mf::build::SI2_SO<int, int, bool>(
381 "Not Equal", [](int a, int b) { return a != b; }, exec_preset_all);
382 return &fn;
383 }
386 break;
387 }
388 break;
389 case SOCK_VECTOR:
390 switch (data->operation) {
392 switch (data->mode) {
394 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
395 "Less Than - Average",
396 [](float3 a, float3 b) { return component_average(a) < component_average(b); },
397 exec_preset_all);
398 return &fn;
399 }
401 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
402 "Less Than - Dot Product",
403 [](float3 a, float3 b, float comp) { return math::dot(a, b) < comp; },
404 exec_preset_first_two);
405 return &fn;
406 }
408 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
409 "Less Than - Direction",
410 [](float3 a, float3 b, float angle) { return angle_v3v3(a, b) < angle; },
411 exec_preset_first_two);
412 return &fn;
413 }
415 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
416 "Less Than - Element-wise",
417 [](float3 a, float3 b) { return a.x < b.x && a.y < b.y && a.z < b.z; },
418 exec_preset_all);
419 return &fn;
420 }
422 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
423 "Less Than - Length",
424 [](float3 a, float3 b) { return math::length(a) < math::length(b); },
425 exec_preset_all);
426 return &fn;
427 }
428 }
429 break;
431 switch (data->mode) {
433 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
434 "Less Equal - Average",
435 [](float3 a, float3 b) { return component_average(a) <= component_average(b); },
436 exec_preset_all);
437 return &fn;
438 }
440 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
441 "Less Equal - Dot Product",
442 [](float3 a, float3 b, float comp) { return math::dot(a, b) <= comp; },
443 exec_preset_first_two);
444 return &fn;
445 }
447 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
448 "Less Equal - Direction",
449 [](float3 a, float3 b, float angle) { return angle_v3v3(a, b) <= angle; },
450 exec_preset_first_two);
451 return &fn;
452 }
454 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
455 "Less Equal - Element-wise",
456 [](float3 a, float3 b) { return a.x <= b.x && a.y <= b.y && a.z <= b.z; },
457 exec_preset_all);
458 return &fn;
459 }
461 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
462 "Less Equal - Length",
463 [](float3 a, float3 b) { return math::length(a) <= math::length(b); },
464 exec_preset_all);
465 return &fn;
466 }
467 }
468 break;
470 switch (data->mode) {
472 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
473 "Greater Than - Average",
474 [](float3 a, float3 b) { return component_average(a) > component_average(b); },
475 exec_preset_all);
476 return &fn;
477 }
479 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
480 "Greater Than - Dot Product",
481 [](float3 a, float3 b, float comp) { return math::dot(a, b) > comp; },
482 exec_preset_first_two);
483 return &fn;
484 }
486 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
487 "Greater Than - Direction",
488 [](float3 a, float3 b, float angle) { return angle_v3v3(a, b) > angle; },
489 exec_preset_first_two);
490 return &fn;
491 }
493 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
494 "Greater Than - Element-wise",
495 [](float3 a, float3 b) { return a.x > b.x && a.y > b.y && a.z > b.z; },
496 exec_preset_all);
497 return &fn;
498 }
500 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
501 "Greater Than - Length",
502 [](float3 a, float3 b) { return math::length(a) > math::length(b); },
503 exec_preset_all);
504 return &fn;
505 }
506 }
507 break;
509 switch (data->mode) {
511 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
512 "Greater Equal - Average",
513 [](float3 a, float3 b) { return component_average(a) >= component_average(b); },
514 exec_preset_all);
515 return &fn;
516 }
518 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
519 "Greater Equal - Dot Product",
520 [](float3 a, float3 b, float comp) { return math::dot(a, b) >= comp; },
521 exec_preset_first_two);
522 return &fn;
523 }
525 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
526 "Greater Equal - Direction",
527 [](float3 a, float3 b, float angle) { return angle_v3v3(a, b) >= angle; },
528 exec_preset_first_two);
529 return &fn;
530 }
532 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
533 "Greater Equal - Element-wise",
534 [](float3 a, float3 b) { return a.x >= b.x && a.y >= b.y && a.z >= b.z; },
535 exec_preset_all);
536 return &fn;
537 }
539 static auto fn = mf::build::SI2_SO<float3, float3, bool>(
540 "Greater Equal - Length",
541 [](float3 a, float3 b) { return math::length(a) >= math::length(b); },
542 exec_preset_all);
543 return &fn;
544 }
545 }
546 break;
548 switch (data->mode) {
550 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
551 "Equal - Average",
552 [](float3 a, float3 b, float epsilon) {
553 return abs(component_average(a) - component_average(b)) <= epsilon;
554 },
555 exec_preset_first_two);
556 return &fn;
557 }
559 static auto fn = mf::build::SI4_SO<float3, float3, float, float, bool>(
560 "Equal - Dot Product",
561 [](float3 a, float3 b, float comp, float epsilon) {
562 return abs(math::dot(a, b) - comp) <= epsilon;
563 },
564 exec_preset_first_two);
565 return &fn;
566 }
568 static auto fn = mf::build::SI4_SO<float3, float3, float, float, bool>(
569 "Equal - Direction",
570 [](float3 a, float3 b, float angle, float epsilon) {
571 return abs(angle_v3v3(a, b) - angle) <= epsilon;
572 },
573 exec_preset_first_two);
574 return &fn;
575 }
577 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
578 "Equal - Element-wise",
579 [](float3 a, float3 b, float epsilon) {
580 return abs(a.x - b.x) <= epsilon && abs(a.y - b.y) <= epsilon &&
581 abs(a.z - b.z) <= epsilon;
582 },
583 exec_preset_first_two);
584 return &fn;
585 }
587 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
588 "Equal - Length",
589 [](float3 a, float3 b, float epsilon) {
590 return abs(math::length(a) - math::length(b)) <= epsilon;
591 },
592 exec_preset_first_two);
593 return &fn;
594 }
595 }
596 break;
598 switch (data->mode) {
600 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
601 "Not Equal - Average",
602 [](float3 a, float3 b, float epsilon) {
603 return abs(component_average(a) - component_average(b)) > epsilon;
604 },
605 exec_preset_first_two);
606 return &fn;
607 }
609 static auto fn = mf::build::SI4_SO<float3, float3, float, float, bool>(
610 "Not Equal - Dot Product",
611 [](float3 a, float3 b, float comp, float epsilon) {
612 return abs(math::dot(a, b) - comp) > epsilon;
613 },
614 exec_preset_first_two);
615 return &fn;
616 }
618 static auto fn = mf::build::SI4_SO<float3, float3, float, float, bool>(
619 "Not Equal - Direction",
620 [](float3 a, float3 b, float angle, float epsilon) {
621 return abs(angle_v3v3(a, b) - angle) > epsilon;
622 },
623 exec_preset_first_two);
624 return &fn;
625 }
627 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
628 "Not Equal - Element-wise",
629 [](float3 a, float3 b, float epsilon) {
630 return abs(a.x - b.x) > epsilon || abs(a.y - b.y) > epsilon ||
631 abs(a.z - b.z) > epsilon;
632 },
633 exec_preset_first_two);
634 return &fn;
635 }
637 static auto fn = mf::build::SI3_SO<float3, float3, float, bool>(
638 "Not Equal - Length",
639 [](float3 a, float3 b, float epsilon) {
640 return abs(math::length(a) - math::length(b)) > epsilon;
641 },
642 exec_preset_first_two);
643 return &fn;
644 }
645 }
646 break;
649 break;
650 }
651 break;
652 case SOCK_RGBA:
653 switch (data->operation) {
654 case NODE_COMPARE_EQUAL: {
655 static auto fn = mf::build::SI3_SO<ColorGeometry4f, ColorGeometry4f, float, bool>(
656 "Equal",
657 [](ColorGeometry4f a, ColorGeometry4f b, float epsilon) {
658 return abs(a.r - b.r) <= epsilon && abs(a.g - b.g) <= epsilon &&
659 abs(a.b - b.b) <= epsilon;
660 },
661 exec_preset_first_two);
662 return &fn;
663 }
665 static auto fn = mf::build::SI3_SO<ColorGeometry4f, ColorGeometry4f, float, bool>(
666 "Not Equal",
667 [](ColorGeometry4f a, ColorGeometry4f b, float epsilon) {
668 return abs(a.r - b.r) > epsilon || abs(a.g - b.g) > epsilon ||
669 abs(a.b - b.b) > epsilon;
670 },
671 exec_preset_first_two);
672 return &fn;
673 }
675 static auto fn = mf::build::SI2_SO<ColorGeometry4f, ColorGeometry4f, bool>(
676 "Brighter",
679 },
680 exec_preset_all);
681 return &fn;
682 }
684 static auto fn = mf::build::SI2_SO<ColorGeometry4f, ColorGeometry4f, bool>(
685 "Darker",
688 },
689 exec_preset_all);
690 return &fn;
691 }
696 break;
697 }
698 break;
699 case SOCK_STRING:
700 switch (data->operation) {
701 case NODE_COMPARE_EQUAL: {
702 static auto fn = mf::build::SI2_SO<std::string, std::string, bool>(
703 "Equal", [](std::string a, std::string b) { return a == b; });
704 return &fn;
705 }
707 static auto fn = mf::build::SI2_SO<std::string, std::string, bool>(
708 "Not Equal", [](std::string a, std::string b) { return a != b; });
709 return &fn;
710 }
717 break;
718 }
719 break;
720 default: {
721 if (is_supported_data_block_type(&ntree, data_type)) {
723 data_type, [&]<typename T>() -> const mf::MultiFunction * {
724 switch (data->operation) {
725 case NODE_COMPARE_EQUAL: {
726 static auto fn = mf::build::SI2_SO<T *, T *, bool>(
727 "Equal",
728 [](const T *a, const T *b) {
729 return data_blocks_are_equal(id_cast<const ID *>(a),
730 id_cast<const ID *>(b));
731 },
732 mf::build::exec_presets::Simple{});
733 return &fn;
734 }
736 static auto fn = mf::build::SI2_SO<T *, T *, bool>(
737 "Not Equal",
738 [](const T *a, const T *b) {
739 return !data_blocks_are_equal(id_cast<const ID *>(a),
740 id_cast<const ID *>(b));
741 },
742 mf::build::exec_presets::Simple{});
743 return &fn;
744 }
745 default: {
746 return nullptr;
747 }
748 }
749 });
750 }
751 }
752 }
753 return nullptr;
754}
755
757{
758 const mf::MultiFunction *fn = get_multi_function(builder.node());
759 builder.set_matching_fn(fn);
760}
761
762static const char *gpu_shader_get_name(const eNodeSocketDatatype data_type,
763 const NodeCompareOperation operation,
764 const NodeCompareMode mode)
765{
766 switch (data_type) {
767 case SOCK_FLOAT:
768 switch (operation) {
770 return "compare_float_less_than";
772 return "compare_float_less_equal";
774 return "compare_float_greater_than";
776 return "compare_float_greater_equal";
778 return "compare_float_equal";
780 return "compare_float_not_equal";
783 break;
784 }
785 break;
786 case SOCK_INT:
787 switch (operation) {
789 return "compare_int_less_than";
791 return "compare_int_less_equal";
793 return "compare_int_greater_than";
795 return "compare_int_greater_equal";
797 return "compare_int_equal";
799 return "compare_int_not_equal";
802 break;
803 }
804 break;
805 case SOCK_VECTOR:
806 switch (operation) {
808 switch (mode) {
810 return "compare_vector_average_less_than";
812 return "compare_vector_dot_less_than";
814 return "compare_vector_direction_less_than";
816 return "compare_vector_element_less_than";
818 return "compare_vector_length_less_than";
819 }
820 break;
822 switch (mode) {
824 return "compare_vector_average_less_equal";
826 return "compare_vector_dot_less_equal";
828 return "compare_vector_direction_less_equal";
830 return "compare_vector_element_less_equal";
832 return "compare_vector_length_less_equal";
833 }
834 break;
836 switch (mode) {
838 return "compare_vector_average_greater_than";
840 return "compare_vector_dot_greater_than";
842 return "compare_vector_direction_greater_than";
844 return "compare_vector_element_greater_than";
846 return "compare_vector_length_greater_than";
847 }
848 break;
850 switch (mode) {
852 return "compare_vector_average_greater_equal";
854 return "compare_vector_dot_greater_equal";
856 return "compare_vector_direction_greater_equal";
858 return "compare_vector_element_greater_equal";
860 return "compare_vector_length_greater_equal";
861 }
862 break;
864 switch (mode) {
866 return "compare_vector_average_equal";
868 return "compare_vector_dot_equal";
870 return "compare_vector_direction_equal";
872 return "compare_vector_element_equal";
874 return "compare_vector_length_equal";
875 }
876 break;
878 switch (mode) {
880 return "compare_vector_average_not_equal";
882 return "compare_vector_dot_not_equal";
884 return "compare_vector_direction_not_equal";
886 return "compare_vector_element_not_equal";
888 return "compare_vector_length_not_equal";
889 }
890 break;
893 break;
894 }
895 break;
896 case SOCK_RGBA:
897 switch (operation) {
899 return "compare_color_equal";
901 return "compare_color_not_equal";
903 return "compare_color_brighter";
905 return "compare_color_darker";
910 break;
911 }
912 break;
913 default:
914 break;
915 }
916
918 return nullptr;
919}
920
922 bNode *node,
923 bNodeExecData * /*execdata*/,
926{
927 const NodeFunctionCompare &storage = node_storage(*node);
928 const eNodeSocketDatatype data_type = eNodeSocketDatatype(storage.data_type);
929 const NodeCompareOperation operation = NodeCompareOperation(storage.operation);
930 const NodeCompareMode mode = NodeCompareMode(storage.mode);
931
932 const char *name = gpu_shader_get_name(data_type, operation, mode);
933
934 if (name == nullptr) {
935 return 0;
936 }
937
939 float luminance_coefficients[3];
941 return GPU_stack_link(mat, node, name, in, out, GPU_constant(luminance_coefficients));
942 }
943
944 return GPU_stack_link(mat, node, name, in, out);
945}
946
947static void data_type_update(Main *bmain, Scene *scene, PointerRNA *ptr)
948{
949 bNode *node = static_cast<bNode *>(ptr->data);
950 NodeFunctionCompare *node_storage = static_cast<NodeFunctionCompare *>(node->storage);
951 const bNodeTree *ntree = reinterpret_cast<const bNodeTree *>(ptr->owner_id);
952
953 if (node_storage->data_type == SOCK_RGBA && !ELEM(node_storage->operation,
958 {
959 node_storage->operation = NODE_COMPARE_EQUAL;
960 }
961 else if ((node_storage->data_type == SOCK_STRING ||
962 is_supported_data_block_type(ntree, node_storage->data_type)) &&
964 {
965 node_storage->operation = NODE_COMPARE_EQUAL;
966 }
967 else if (node_storage->data_type != SOCK_RGBA &&
969 {
970 node_storage->operation = NODE_COMPARE_EQUAL;
971 }
972
973 rna_Node_socket_update(bmain, scene, ptr);
974}
975
976static void node_rna(StructRNA *srna)
977{
978 static const EnumPropertyItem mode_items[] = {
980 "ELEMENT",
981 0,
982 "Element-Wise",
983 "Compare each element of the input vectors"},
984 {NODE_COMPARE_MODE_LENGTH, "LENGTH", 0, "Length", "Compare the length of the input vectors"},
986 "AVERAGE",
987 0,
988 "Average",
989 "Compare the average of the input vectors elements"},
991 "DOT_PRODUCT",
992 0,
993 "Dot Product",
994 "Compare the dot products of the input vectors"},
996 "DIRECTION",
997 0,
998 "Direction",
999 "Compare the direction of the input vectors"},
1000 {0, nullptr, 0, nullptr, nullptr},
1001 };
1002
1003 PropertyRNA *prop;
1004
1006 srna,
1007 "operation",
1008 "Operation",
1009 "",
1011 NOD_storage_enum_accessors(operation),
1013 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
1014 *r_free = true;
1015 bNode *node = static_cast<bNode *>(ptr->data);
1016 NodeFunctionCompare *data = static_cast<NodeFunctionCompare *>(node->storage);
1017 const bNodeTree *ntree = reinterpret_cast<const bNodeTree *>(ptr->owner_id);
1018
1019 if (ELEM(data->data_type, SOCK_FLOAT, SOCK_INT, SOCK_VECTOR)) {
1020 return enum_items_filter(
1023 });
1024 }
1025 if (data->data_type == SOCK_STRING) {
1026 return enum_items_filter(
1029 });
1030 }
1031 if (data->data_type == SOCK_RGBA) {
1033 [](const EnumPropertyItem &item) {
1034 return ELEM(item.value,
1039 });
1040 }
1041 if (is_supported_data_block_type(ntree, data->data_type)) {
1042 return enum_items_filter(
1045 });
1046 }
1048 [](const EnumPropertyItem & /*item*/) { return false; });
1049 });
1050
1051 prop = RNA_def_node_enum(
1052 srna,
1053 "data_type",
1054 "Input Type",
1055 "",
1057 NOD_storage_enum_accessors(data_type),
1058 std::nullopt,
1059 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
1060 *r_free = true;
1061 const bNodeTree *ntree = reinterpret_cast<const bNodeTree *>(ptr->owner_id);
1062 return enum_items_filter(
1066 });
1067 });
1069
1070 RNA_def_node_enum(srna,
1071 "mode",
1072 "Mode",
1073 "",
1074 mode_items,
1077}
1078
1079static void node_register()
1080{
1081 static bke::bNodeType ntype;
1082 fn_cmp_node_type_base(&ntype, "FunctionNodeCompare"_ustr, FN_NODE_COMPARE);
1083 ntype.ui_name = "Compare";
1084 ntype.ui_description = "Perform a comparison operation on the two given inputs";
1085 ntype.enum_name_legacy = "COMPARE";
1087 ntype.declare = node_declare;
1088 ntype.labelfunc = node_label;
1089 ntype.initfunc = node_init;
1091 ntype, "NodeFunctionCompare", node_free_standard_storage, node_copy_standard_storage);
1092 ntype.gpu_fn = node_gpu_material;
1094 ntype.draw_buttons = node_layout;
1097
1098 node_rna(ntype.rna_ext.srna);
1099}
1101
1102} // namespace blender::nodes::node_fn_compare_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define FN_NODE_COMPARE
#define BLT_I18NCONTEXT_ID_NODETREE
#define IFACE_(msgid)
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
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)
#define storage(slot, qualifiers)
#define abs
#define T
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
bool node_tree_type_supports_socket_type_static(const int ntree_type, const eNodeSocketDatatype socket_type)
Definition node.cc:6295
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
static const mf::MultiFunction * get_multi_function(const bNode &node)
static void node_init(bNodeTree *, bNode *node)
static auto to_static_data_block_type(const eNodeSocketDatatype socket_type, Fn &&fn)
static eNodeSocketDatatype op_type_to_socket_type(const NodeCompareOperation value)
static bool data_blocks_are_equal(const ID *a, const ID *b)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_rna(StructRNA *srna)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static std::optional< eNodeSocketDatatype > get_compare_type_for_operation(const bNodeTree &ntree, const eNodeSocketDatatype type, const NodeCompareOperation operation)
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void data_type_update(Main *bmain, Scene *scene, PointerRNA *ptr)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static float component_average(float3 a)
static bool is_supported_data_block_type(const bNodeTree *ntree, const eNodeSocketDatatype data_type)
static const char * gpu_shader_get_name(const eNodeSocketDatatype data_type, const NodeCompareOperation operation, const NodeCompareMode mode)
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)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
const EnumPropertyItem rna_enum_node_compare_operation_items[]
float angle_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
@ NODE_COMPARE_MODE_AVERAGE
@ NODE_COMPARE_MODE_DIRECTION
@ NODE_COMPARE_MODE_ELEMENT
@ NODE_COMPARE_MODE_DOT_PRODUCT
@ NODE_COMPARE_MODE_LENGTH
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GPUNodeLink * GPU_constant(const float *num)
BMesh const char void * data
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
@ NODE_COMPARE_COLOR_BRIGHTER
@ NODE_COMPARE_NOT_EQUAL
@ NODE_COMPARE_LESS_EQUAL
@ NODE_COMPARE_COLOR_DARKER
@ NODE_COMPARE_GREATER_EQUAL
@ NODE_COMPARE_GREATER_THAN
@ NODE_COMPARE_LESS_THAN
void rna_Node_socket_update(Main *bmain, Scene *scene, PointerRNA *ptr)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
T * DEG_get_original(T *id)
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
eNodeSocketDatatype data_type
NodeCompareOperation operation
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
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)
#define N_(msgid)