Blender V5.3
socket_usage_inference.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
8
9#include <optional>
10#include <regex>
11
13#include "NOD_menu_value.hh"
14#include "NOD_multi_function.hh"
18
19#include "DNA_anim_types.h"
20#include "DNA_material_types.h"
21#include "DNA_node_types.h"
22
26#include "BKE_node_runtime.hh"
28
29#include "ANIM_action.hh"
31
32#include "BLI_listbase.hh"
33#include "BLI_stack.hh"
34
36
39 private:
40 friend SocketUsageParams;
41
42 bke::ComputeContextCache &compute_context_cache_;
43
45 SocketValueInferencer &value_inferencer_;
46
48 const bNodeTree &root_tree_;
49
53 Stack<SocketInContext> usage_tasks_;
54
59 Map<SocketInContext, bool> all_socket_usages_;
60
64 Stack<SocketInContext> disabled_output_tasks_;
65
69 Map<SocketInContext, bool> all_socket_disable_states_;
70
77 bool ignore_top_level_node_muting_ = false;
78
79 public:
81
83 SocketValueInferencer &value_inferencer,
84 bke::ComputeContextCache &compute_context_cache,
85 const bool ignore_top_level_node_muting)
86 : compute_context_cache_(compute_context_cache),
87 value_inferencer_(value_inferencer),
88 root_tree_(tree),
89 ignore_top_level_node_muting_(ignore_top_level_node_muting)
90 {
91 root_tree_.ensure_topology_cache();
92 root_tree_.ensure_interface_cache();
93 }
94
96 {
97 for (const bNode *node : root_tree_.all_nodes()) {
98 if (node->is_group_input()) {
99 /* Can skip these sockets, because they don't affect usage anyway, and there may be a lot
100 * of them. See #144756. */
101 continue;
102 }
103 for (const bNodeSocket *socket : node->output_sockets()) {
104 all_socket_usages_.add_new({nullptr, socket}, true);
105 }
106 }
107 }
108
109 bool is_group_input_used(const int input_i)
110 {
111 for (const bNode *node : root_tree_.group_input_nodes()) {
112 const bNodeSocket &socket = node->output_socket(input_i);
113 if (!socket.is_directly_linked()) {
114 continue;
115 }
116 const SocketInContext socket_ctx{nullptr, &socket};
117 if (this->is_socket_used(socket_ctx)) {
118 return true;
119 }
120 }
121 return false;
122 }
123
124 bool is_socket_used(const SocketInContext &socket)
125 {
126 const std::optional<bool> is_used = all_socket_usages_.lookup_try(socket);
127 if (is_used.has_value()) {
128 return *is_used;
129 }
130 if (socket->is_output() && !socket->is_directly_linked()) {
131 /* In this case we can return early because the socket can't be used if it's not linked. */
132 return false;
133 }
134 if (socket->owner_tree().has_available_link_cycle()) {
135 return false;
136 }
137
138 BLI_assert(usage_tasks_.is_empty());
139 usage_tasks_.push(socket);
140
141 while (!usage_tasks_.is_empty()) {
142 const SocketInContext &socket = usage_tasks_.peek();
143 this->usage_task(socket);
144 if (&socket == &usage_tasks_.peek()) {
145 /* The task is finished if it hasn't added any new task it depends on. */
146 usage_tasks_.pop();
147 }
148 }
149
150 return all_socket_usages_.lookup(socket);
151 }
152
154 {
155 return value_inferencer_.get_socket_value(socket);
156 }
157
158 bool is_disabled_group_output(const int output_i)
159 {
160 const bNode *group_output_node = root_tree_.group_output_node();
161 if (!group_output_node) {
162 return true;
163 }
164 const SocketInContext socket{nullptr, &group_output_node->input_socket(output_i)};
165 return this->is_disabled_output(socket);
166 }
167
169 {
170 const std::optional<bool> is_disabled = all_socket_disable_states_.lookup_try(socket);
171 if (is_disabled.has_value()) {
172 return *is_disabled;
173 }
174 if (socket->owner_tree().has_available_link_cycle()) {
175 return true;
176 }
177 BLI_assert(disabled_output_tasks_.is_empty());
178 disabled_output_tasks_.push(socket);
179
180 while (!disabled_output_tasks_.is_empty()) {
181 const SocketInContext &socket = disabled_output_tasks_.peek();
182 this->disabled_output_task(socket);
183 if (&socket == &disabled_output_tasks_.peek()) {
184 /* The task is finished if it hasn't added any new task it depends on. */
185 disabled_output_tasks_.pop();
186 }
187 }
188 return all_socket_disable_states_.lookup(socket);
189 }
190
191 private:
192 void usage_task(const SocketInContext &socket)
193 {
194 if (all_socket_usages_.contains(socket)) {
195 return;
196 }
197 const bNode &node = socket->owner_node();
198 if (!socket->is_available()) {
199 all_socket_usages_.add_new(socket, false);
200 return;
201 }
202 if (node.is_undefined() && !node.is_custom_group()) {
203 all_socket_usages_.add_new(socket, false);
204 return;
205 }
206 if (socket->is_input()) {
207 this->usage_task__input(socket);
208 }
209 else {
210 this->usage_task__output(socket);
211 }
212 }
213
214 void usage_task__input(const SocketInContext &socket)
215 {
216 const NodeInContext node = socket.owner_node();
217
218 if (node->is_muted()) {
219 const bool is_top_level = socket.context == nullptr;
220 if (!this->ignore_top_level_node_muting_ || !is_top_level) {
221 this->usage_task__input__muted_node(socket);
222 return;
223 }
224 }
225
226 switch (node->type_legacy) {
227 case NODE_GROUP:
228 case NODE_CUSTOM_GROUP: {
229 this->usage_task__input__group_node(socket);
230 break;
231 }
232 case NODE_GROUP_OUTPUT: {
233 this->usage_task__input__group_output_node(socket);
234 break;
235 }
236 case GEO_NODE_SWITCH: {
237 this->usage_task__input__generic_switch(
239 break;
240 }
242 this->usage_task__input__generic_switch(
244 break;
245 }
247 if (socket->index() == 0) {
248 this->usage_task__input__fallback(socket);
249 }
250 else {
251 this->usage_task__input__generic_switch(
253 }
254 break;
255 }
256 case SH_NODE_MIX: {
257 this->usage_task__input__generic_switch(
259 break;
260 }
261 case SH_NODE_MIX_SHADER: {
262 this->usage_task__input__generic_switch(
264 break;
265 }
267 this->usage_task__input__simulation_input_node(socket);
268 break;
269 }
271 this->usage_task__input__repeat_input_node(socket);
272 break;
273 }
275 this->usage_task__input__foreach_element_input_node(socket);
276 break;
277 }
279 this->usage_task__input__foreach_element_output_node(socket);
280 break;
281 }
283 this->usage_task__input__capture_attribute_node(socket);
284 break;
285 }
286 case GEO_NODE_WARNING: {
287 this->usage_task__input__warning_node(socket);
288 break;
289 }
296 case TEX_NODE_OUTPUT: {
297 this->usage_task__input__output_node(socket);
298 break;
299 }
300 default: {
301 if (node->is_type("NodeEnableOutput"_ustr)) {
302 this->usage_task__input__enable_output(socket);
303 break;
304 }
305 this->usage_task__input__fallback(socket);
306 break;
307 }
308 }
309 }
310
311 void usage_task__input__output_node(const SocketInContext &socket)
312 {
313 all_socket_usages_.add_new(socket, true);
314 }
315
320 void usage_task__input__generic_switch(
321 const SocketInContext &socket,
322 const FunctionRef<bool(const SocketInContext &socket, const InferenceValue &condition)>
323 is_selected_socket)
324 {
325 const NodeInContext node = socket.owner_node();
326 BLI_assert(node->input_sockets().size() >= 1);
327 BLI_assert(node->output_sockets().size() >= 1);
328
329 if (socket->type == SOCK_CUSTOM && STREQ(socket->idname, "NodeSocketVirtual")) {
330 all_socket_usages_.add_new(socket, false);
331 return;
332 }
333 const SocketInContext output_socket{socket.context,
334 get_first_available_bsocket(node->output_sockets())};
335 const std::optional<bool> output_is_used = all_socket_usages_.lookup_try(output_socket);
336 if (!output_is_used.has_value()) {
337 this->push_usage_task(output_socket);
338 return;
339 }
340 if (!*output_is_used) {
341 all_socket_usages_.add_new(socket, false);
342 return;
343 }
344 const SocketInContext condition_socket{socket.context,
345 get_first_available_bsocket(node->input_sockets())};
346 if (socket == condition_socket) {
347 all_socket_usages_.add_new(socket, true);
348 return;
349 }
350 const InferenceValue condition_value = this->get_socket_value(condition_socket);
351 if (condition_value.is_unknown()) {
352 /* The exact condition value is unknown, so any input may be used. */
353 all_socket_usages_.add_new(socket, true);
354 return;
355 }
356 const bool is_used = is_selected_socket(socket, condition_value);
357 all_socket_usages_.add_new(socket, is_used);
358 }
359
360 void usage_task__input__group_node(const SocketInContext &socket)
361 {
362 const NodeInContext node = socket.owner_node();
363 const bNodeTree *group = reinterpret_cast<const bNodeTree *>(node->id);
364 if (!group || ID_MISSING(&group->id)) {
365 all_socket_usages_.add_new(socket, false);
366 return;
367 }
368 group->ensure_topology_cache();
369 if (group->has_available_link_cycle()) {
370 all_socket_usages_.add_new(socket, false);
371 return;
372 }
373
374 /* The group node input is used if any of the matching group inputs within the group is
375 * used. */
376 const ComputeContext &group_context = compute_context_cache_.for_group_node(
377 socket.context, node->identifier, &node->owner_tree());
378 Vector<const bNodeSocket *> dependent_sockets;
379 for (const bNode *group_input_node : group->group_input_nodes()) {
380 const bNodeSocket &group_input_socket = group_input_node->output_socket(socket->index());
381 if (group_input_socket.is_directly_linked()) {
382 /* Skip unlinked group inputs to avoid further unnecessary processing of them further down
383 * the line. */
384 dependent_sockets.append(&group_input_socket);
385 }
386 }
387 this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, &group_context);
388 }
389
390 void usage_task__input__group_output_node(const SocketInContext &socket)
391 {
392 const int output_i = socket->index();
393 if (socket.context == nullptr) {
394 /* This is a final output which is always used. */
395 all_socket_usages_.add_new(socket, true);
396 return;
397 }
398 /* The group output node is used if the matching output of the parent group node is used. */
399 const bke::GroupNodeComputeContext &group_context =
400 *static_cast<const bke::GroupNodeComputeContext *>(socket.context);
401 const bNodeSocket &group_node_output = group_context.node()->output_socket(output_i);
402 this->usage_task__with_dependent_sockets(
403 socket, {&group_node_output}, {}, group_context.parent());
404 }
405
406 void usage_task__output(const SocketInContext &socket)
407 {
408 /* An output socket is used if any of the sockets it is connected to is used. */
409 Vector<const bNodeSocket *> dependent_sockets;
410 for (const bNodeLink *link : socket->directly_linked_links()) {
411 if (link->is_used()) {
412 dependent_sockets.append(link->tosock);
413 }
414 }
415 this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context);
416 }
417
418 void usage_task__input__simulation_input_node(const SocketInContext &socket)
419 {
420 const NodeInContext node = socket.owner_node();
421 const bNodeTree &tree = socket->owner_tree();
422
423 const NodeGeometrySimulationInput &storage = *static_cast<const NodeGeometrySimulationInput *>(
424 node->storage);
425 const bNode *sim_output_node = tree.node_by_id(storage.output_node_id);
426 if (!sim_output_node) {
427 all_socket_usages_.add_new(socket, false);
428 return;
429 }
430 /* Simulation inputs are also used when any of the simulation outputs are used. */
431 Vector<const bNodeSocket *, 16> dependent_sockets;
432 dependent_sockets.extend(node->output_sockets());
433 dependent_sockets.extend(sim_output_node->output_sockets());
434 this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context);
435 }
436
437 void usage_task__input__repeat_input_node(const SocketInContext &socket)
438 {
439 const NodeInContext node = socket.owner_node();
440 const bNodeTree &tree = socket->owner_tree();
441
442 const NodeGeometryRepeatInput &storage = *static_cast<const NodeGeometryRepeatInput *>(
443 node->storage);
444 const bNode *repeat_output_node = tree.node_by_id(storage.output_node_id);
445 if (!repeat_output_node) {
446 all_socket_usages_.add_new(socket, false);
447 return;
448 }
449 /* Assume that all repeat inputs are used when any of the outputs are used. This check could
450 * become more precise in the future if necessary. */
451 Vector<const bNodeSocket *, 16> dependent_sockets;
452 dependent_sockets.extend(node->output_sockets());
453 dependent_sockets.extend(repeat_output_node->output_sockets());
454 this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context);
455 }
456
457 void usage_task__input__foreach_element_output_node(const SocketInContext &socket)
458 {
459 const NodeInContext node = socket.owner_node();
460 this->usage_task__with_dependent_sockets(
461 socket, {node->output_by_identifier(socket->identifier_ustr())}, {}, socket.context);
462 }
463
464 void usage_task__input__capture_attribute_node(const SocketInContext &socket)
465 {
466 const NodeInContext node = socket.owner_node();
467 this->usage_task__with_dependent_sockets(
468 socket, {&node->output_socket(socket->index())}, {}, socket.context);
469 }
470
471 void usage_task__input__warning_node(const SocketInContext &socket)
472 {
473 const NodeInContext node = socket.owner_node();
474 const SocketInContext show_input_socket = node.input_socket(0);
475 const SocketInContext message_input_socket = node.input_socket(1);
476 const SocketInContext show_output_socket = node.output_socket(0);
477 if (show_output_socket->is_directly_linked()) {
478 if (socket == show_input_socket) {
479 this->usage_task__with_dependent_sockets(
480 show_input_socket, {&*show_output_socket}, {}, socket.context);
481 }
482 if (socket == message_input_socket) {
483 this->usage_task__with_dependent_sockets(
484 message_input_socket, {&*show_output_socket}, {&*show_input_socket}, socket.context);
485 }
486 return;
487 }
488 const ComputeContext *context = node.context;
489 const bool is_top_level = context == nullptr;
490 if (is_top_level) {
491 if (socket == show_input_socket) {
492 /* At the top level, the show input is always used. */
493 all_socket_usages_.add_new(socket, true);
494 }
495 if (socket == message_input_socket) {
496 /* The message input is used if the show input is true. */
497 const InferenceValue show_inference_value = this->get_socket_value(show_input_socket);
498 const bool show_value = show_inference_value.get_if_primitive<bool>().value_or(true);
499 all_socket_usages_.add_new(socket, show_value);
500 }
501 return;
502 }
503 const bool is_in_zone = !dynamic_cast<const bke::GroupNodeComputeContext *>(context);
504 if (is_in_zone) {
505 /* Warning nodes where the output is not linked must not be in a zone. */
506 all_socket_usages_.add_new(socket, false);
507 return;
508 }
509 const bNode *output_node = node->owner_tree().group_output_node();
510 if (!output_node) {
511 all_socket_usages_.add_new(socket, false);
512 return;
513 }
514 const Span<const bNodeSocket *> group_output_sockets = output_node->input_sockets().drop_back(
515 1);
516 /* The warning is used if any of the group outputs is used. */
517 if (socket == show_input_socket) {
518 this->usage_task__with_dependent_sockets(
519 show_input_socket, group_output_sockets, {}, context);
520 }
521 if (socket == message_input_socket) {
522 this->usage_task__with_dependent_sockets(
523 message_input_socket, group_output_sockets, {&*show_input_socket}, context);
524 }
525 }
526
527 void usage_task__input__enable_output(const SocketInContext &socket)
528 {
529 const NodeInContext node = socket.owner_node();
530 const SocketInContext enable_socket = node.input_socket(0);
531 const SocketInContext output_socket = node.output_socket(0);
532 if (socket == enable_socket) {
533 this->usage_task__with_dependent_sockets(socket, {&*output_socket}, {}, socket.context);
534 }
535 else {
536 this->usage_task__with_dependent_sockets(
537 socket, {&*output_socket}, {&*enable_socket}, socket.context);
538 }
539 }
540
541 void usage_task__input__fallback(const SocketInContext &socket)
542 {
543 const SocketDeclaration *socket_decl = socket->runtime->declaration;
544 if (!socket_decl) {
545 all_socket_usages_.add_new(socket, true);
546 return;
547 }
548 if (!socket_decl->usage_inference_fn) {
549 this->usage_task__with_dependent_sockets(
550 socket, socket->owner_node().output_sockets(), {}, socket.context);
551 return;
552 }
553 SocketUsageParams params{
554 *owner_, socket.context, socket->owner_tree(), socket->owner_node(), *socket};
555 const std::optional<bool> is_used = (*socket_decl->usage_inference_fn)(params);
556 if (!is_used.has_value()) {
557 /* Some value was requested, come back later when that value is available. */
558 return;
559 }
560 all_socket_usages_.add_new(socket, *is_used);
561 }
562
563 void usage_task__input__foreach_element_input_node(const SocketInContext &socket)
564 {
565 const NodeInContext node = socket.owner_node();
566 const bNodeTree &tree = socket->owner_tree();
567
568 const NodeGeometryForeachGeometryElementInput &storage =
569 *static_cast<const NodeGeometryForeachGeometryElementInput *>(node->storage);
570 const bNode *foreach_output_node = tree.node_by_id(storage.output_node_id);
571 if (!foreach_output_node) {
572 all_socket_usages_.add_new(socket, false);
573 return;
574 }
575 Vector<const bNodeSocket *, 16> dependent_sockets;
576 if (StringRef(socket->identifier).startswith("Input_")) {
577 dependent_sockets.append(node->output_by_identifier(socket->identifier_ustr()));
578 }
579 else {
580 /* The geometry and selection inputs are used whenever any of the zone outputs is used. */
581 dependent_sockets.extend(node->output_sockets());
582 dependent_sockets.extend(foreach_output_node->output_sockets());
583 }
584 this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context);
585 }
586
587 void usage_task__input__muted_node(const SocketInContext &socket)
588 {
589 const NodeInContext node = socket.owner_node();
590 Vector<const bNodeSocket *> dependent_sockets;
591 for (const bNodeInternalLink &internal_link : node->internal_links()) {
592 if (internal_link.in != socket.socket) {
593 continue;
594 }
595 dependent_sockets.append(internal_link.out);
596 }
597 this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context);
598 }
599
604 void usage_task__with_dependent_sockets(const SocketInContext &socket,
605 const Span<const bNodeSocket *> dependent_sockets,
606 const Span<const bNodeSocket *> condition_inputs,
607 const ComputeContext *dependent_socket_context)
608 {
609 /* Check if any of the dependent outputs are used. */
610 SocketInContext next_unknown_socket;
611 bool any_output_used = false;
612 for (const bNodeSocket *dependent_socket_ptr : dependent_sockets) {
613 const SocketInContext dependent_socket{dependent_socket_context, dependent_socket_ptr};
614 const std::optional<bool> is_used = all_socket_usages_.lookup_try(dependent_socket);
615 if (!is_used.has_value()) {
616 if (dependent_socket_ptr->is_output() && !dependent_socket_ptr->is_directly_linked()) {
617 continue;
618 }
619 if (!next_unknown_socket) {
620 next_unknown_socket = dependent_socket;
621 continue;
622 }
623 }
624 if (is_used.value_or(false)) {
625 any_output_used = true;
626 break;
627 }
628 }
629 if (next_unknown_socket) {
630 /* Create a task that checks if the next dependent socket is used. Intentionally only create
631 * a task for the very next one and not for all, because that could potentially trigger a lot
632 * of unnecessary evaluations. */
633 this->push_usage_task(next_unknown_socket);
634 return;
635 }
636 if (!any_output_used) {
637 all_socket_usages_.add_new(socket, false);
638 return;
639 }
640 bool all_condition_inputs_true = true;
641 for (const bNodeSocket *condition_input_ptr : condition_inputs) {
642 const SocketInContext condition_input{dependent_socket_context, condition_input_ptr};
643 const InferenceValue condition_value = this->get_socket_value(condition_input);
644 if (!condition_value.is_primitive_value()) {
645 /* The condition is not known, so it may be true. */
646 continue;
647 }
648 BLI_assert(condition_input_ptr->type == SOCK_BOOLEAN);
649 if (!condition_value.get_primitive<bool>()) {
650 all_condition_inputs_true = false;
651 break;
652 }
653 }
654 all_socket_usages_.add_new(socket, all_condition_inputs_true);
655 }
656
657 void push_usage_task(const SocketInContext &socket)
658 {
659 usage_tasks_.push(socket);
660 }
661
662 void disabled_output_task(const SocketInContext &socket)
663 {
664 if (all_socket_disable_states_.contains(socket)) {
665 return;
666 }
667 const bNode &node = socket->owner_node();
668 if (!socket->is_available()) {
669 all_socket_disable_states_.add_new(socket, true);
670 return;
671 }
672 if (node.is_undefined() && !node.is_custom_group()) {
673 all_socket_disable_states_.add_new(socket, true);
674 return;
675 }
676 if (socket->is_input()) {
677 this->disabled_output_task__input(socket);
678 }
679 else {
680 this->disabled_output_task__output(socket);
681 }
682 }
683
684 void disabled_output_task__input(const SocketInContext &socket)
685 {
686 const Span<const bNodeLink *> links = socket->directly_linked_links();
687 const bNodeLink *single_link = links.size() == 1 && links[0]->is_used() ? links[0] : nullptr;
688 if (links.size() != 1 || !links[0]->is_used()) {
689 /* The socket is not linked, thus it is not disabled. */
690 all_socket_disable_states_.add_new(socket, false);
691 return;
692 }
693 const SocketInContext origin_socket{socket.context, single_link->fromsock};
694 this->disabled_output_task__with_origin_socket(socket, origin_socket);
695 }
696
697 void disabled_output_task__output(const SocketInContext &socket)
698 {
699 const NodeInContext node = socket.owner_node();
700 if (node->is_muted()) {
701 const bool is_top_level = socket.context == nullptr;
702 if (!this->ignore_top_level_node_muting_ || !is_top_level) {
703 this->disabled_output_task__output__muted_node(socket);
704 return;
705 }
706 }
707
708 switch (node->type_legacy) {
709 case NODE_GROUP:
710 case NODE_CUSTOM_GROUP: {
711 this->disabled_output_task__output__group_node(socket);
712 break;
713 }
714 case NODE_REROUTE: {
715 this->disabled_output_task__with_origin_socket(socket, node.input_socket(0));
716 break;
717 }
718 case GEO_NODE_SWITCH: {
719 this->disabled_output_task__output__generic_switch(
721 break;
722 }
724 this->disabled_output_task__output__generic_switch(
726 break;
727 }
729 this->disabled_output_task__output__generic_switch(
731 break;
732 }
733 default: {
734 if (node->is_type("NodeEnableOutput"_ustr)) {
735 this->disabled_output_task__output__enable_output_node(socket);
736 break;
737 }
738
739 const SocketDeclaration *socket_declaration = socket->runtime->declaration;
740 if (socket_declaration && socket_declaration->usage_inference_fn) {
741 SocketUsageParams params{
742 *owner_, socket.context, socket->owner_tree(), socket->owner_node(), *socket};
743 const std::optional<bool> is_used = (*socket_declaration->usage_inference_fn)(params);
744 if (!is_used.has_value()) {
745 /* Some value was requested, come back later when that value is available. */
746 return;
747 }
748 if (!*is_used) {
749 all_socket_disable_states_.add_new(socket, true);
750 break;
751 }
752 }
753
754 /* By default, all output sockets are enabled unless they are explicitly disabled by some
755 * rule above. */
756 all_socket_disable_states_.add_new(socket, false);
757 break;
758 }
759 }
760 }
761
762 void disabled_output_task__output__muted_node(const SocketInContext &socket)
763 {
764 const NodeInContext node = socket.owner_node();
765 for (const bNodeInternalLink &internal_link : node->internal_links()) {
766 if (internal_link.out != socket.socket) {
767 continue;
768 }
769 this->disabled_output_task__with_origin_socket(socket, {socket.context, internal_link.in});
770 return;
771 }
772 all_socket_disable_states_.add_new(socket, false);
773 }
774
775 void disabled_output_task__output__group_node(const SocketInContext &socket)
776 {
777 const NodeInContext node = socket.owner_node();
778 const bNodeTree *group = reinterpret_cast<const bNodeTree *>(node->id);
779 if (!group || ID_MISSING(&group->id)) {
780 all_socket_disable_states_.add_new(socket, false);
781 return;
782 }
783 group->ensure_topology_cache();
784 if (group->has_available_link_cycle()) {
785 all_socket_disable_states_.add_new(socket, false);
786 return;
787 }
788 const bNode *group_output_node = group->group_output_node();
789 if (!group_output_node) {
790 all_socket_disable_states_.add_new(socket, false);
791 return;
792 }
793 const ComputeContext &group_context = compute_context_cache_.for_group_node(
794 socket.context, node->identifier, &node->owner_tree());
795 const SocketInContext origin_socket{&group_context,
796 &group_output_node->input_socket(socket->index())};
797 this->disabled_output_task__with_origin_socket(socket, origin_socket);
798 }
799
800 void disabled_output_task__output__generic_switch(
801 const SocketInContext &socket,
802 const FunctionRef<bool(const SocketInContext &socket, const InferenceValue &condition)>
803 is_selected_socket)
804 {
805 const NodeInContext node = socket.owner_node();
806 BLI_assert(node->input_sockets().size() >= 1);
807 BLI_assert(node->output_sockets().size() >= 1);
808
809 if (socket->type == SOCK_CUSTOM && STREQ(socket->idname, "NodeSocketVirtual")) {
810 all_socket_disable_states_.add_new(socket, false);
811 return;
812 }
813 const SocketInContext condition_socket{socket.context,
814 get_first_available_bsocket(node->input_sockets())};
815 const InferenceValue condition_value = this->get_socket_value(condition_socket);
816 if (condition_value.is_unknown()) {
817 all_socket_disable_states_.add_new(socket, false);
818 return;
819 }
820 for (const bNodeSocket *input_socket : node->input_sockets()) {
821 if (!input_socket->is_available()) {
822 continue;
823 }
824 if (input_socket->type == SOCK_CUSTOM && STREQ(input_socket->idname, "NodeSocketVirtual")) {
825 continue;
826 }
827 if (input_socket == condition_socket.socket) {
828 continue;
829 }
830 const SocketInContext origin_socket{socket.context, input_socket};
831 const bool is_selected = is_selected_socket(origin_socket, condition_value);
832 if (is_selected) {
833 this->disabled_output_task__with_origin_socket(socket, origin_socket);
834 return;
835 }
836 }
837 all_socket_disable_states_.add_new(socket, false);
838 }
839
840 void disabled_output_task__output__enable_output_node(const SocketInContext &socket)
841 {
842 const NodeInContext node = socket.owner_node();
843 const SocketInContext enable_socket = node.input_socket(0);
844 const InferenceValue enable_value = this->get_socket_value(enable_socket);
845 const std::optional<bool> is_enabled_opt = enable_value.get_if_primitive<bool>();
846 const bool is_enabled = is_enabled_opt.value_or(true);
847 all_socket_disable_states_.add_new(socket, !is_enabled);
848 }
849
850 void disabled_output_task__with_origin_socket(const SocketInContext &socket,
851 const SocketInContext &origin_socket)
852 {
853 const std::optional<bool> is_disabled = all_socket_disable_states_.lookup_try(origin_socket);
854 if (is_disabled.has_value()) {
855 all_socket_disable_states_.add_new(socket, *is_disabled);
856 return;
857 }
858 this->push_disabled_output_task(origin_socket);
859 }
860
861 void push_disabled_output_task(const SocketInContext &socket)
862 {
863 disabled_output_tasks_.push(socket);
864 }
865
866 static const bNodeSocket *get_first_available_bsocket(const Span<const bNodeSocket *> sockets)
867 {
868 for (const bNodeSocket *socket : sockets) {
869 if (socket->is_available()) {
870 return socket;
871 }
872 }
873 return nullptr;
874 }
875};
876
878 ResourceScope &scope,
879 SocketValueInferencer &value_inferencer,
880 bke::ComputeContextCache &compute_context_cache,
881 const bool ignore_top_level_node_muting)
882 : impl_(scope.construct<SocketUsageInferencerImpl>(
883 tree, value_inferencer, compute_context_cache, ignore_top_level_node_muting))
884{
885 impl_.owner_ = this;
886}
887
889{
890 return socket.socket_type == StringRef("NodeSocketMenu");
891}
892
893static bool input_may_affect_visibility(const bNodeSocket &socket)
894{
895 return socket.type == SOCK_MENU;
896}
897
899{
900 tree.ensure_topology_cache();
901 const Span<const bNodeSocket *> all_input_sockets = tree.all_input_sockets();
902 const Span<const bNodeSocket *> all_output_sockets = tree.all_output_sockets();
903 Array<SocketUsage> all_usages(tree.all_sockets().size());
904
905 if (tree.has_available_link_cycle()) {
906 return all_usages;
907 }
908
909 ResourceScope scope;
910 bke::ComputeContextCache compute_context_cache;
911
912 const bool ignore_top_level_node_muting = true;
913
914 {
915 /* Find actual socket usages. */
916 SocketValueInferencer value_inferencer{tree, scope, compute_context_cache};
917 SocketUsageInferencer usage_inferencer{
918 tree, scope, value_inferencer, compute_context_cache, ignore_top_level_node_muting};
919 usage_inferencer.mark_top_level_node_outputs_as_used();
920 for (const bNodeSocket *socket : all_input_sockets) {
921 all_usages[socket->index_in_tree()].is_used = usage_inferencer.is_socket_used(
922 {nullptr, socket});
923 }
924 }
925
926 /* Find input sockets that should be hidden. */
927 Array<bool> only_controllers_used(all_input_sockets.size(), NoInitialization{});
928 Array<bool> all_ignored_inputs(all_input_sockets.size(), true);
929 threading::parallel_for(all_input_sockets.index_range(), 1024, [&](const IndexRange range) {
930 for (const int i : range) {
931 const bNodeSocket &socket = *all_input_sockets[i];
932 only_controllers_used[i] = !input_may_affect_visibility(socket);
933 }
934 });
935 SocketValueInferencer value_inferencer_all_unknown{
936 tree, scope, compute_context_cache, nullptr, all_ignored_inputs};
937 SocketUsageInferencer usage_inferencer_all_unknown{tree,
938 scope,
939 value_inferencer_all_unknown,
940 compute_context_cache,
941 ignore_top_level_node_muting};
942 SocketValueInferencer value_inferencer_only_controllers{
943 tree, scope, compute_context_cache, nullptr, only_controllers_used};
944 SocketUsageInferencer usage_inferencer_only_controllers{tree,
945 scope,
946 value_inferencer_only_controllers,
947 compute_context_cache,
948 ignore_top_level_node_muting};
949 usage_inferencer_all_unknown.mark_top_level_node_outputs_as_used();
950 usage_inferencer_only_controllers.mark_top_level_node_outputs_as_used();
951 for (const bNodeSocket *socket : all_input_sockets) {
952 SocketUsage &usage = all_usages[socket->index_in_tree()];
953 if (usage.is_used) {
954 /* Used inputs are always visible. */
955 continue;
956 }
957 const SocketInContext socket_ctx{nullptr, socket};
958 if (usage_inferencer_only_controllers.is_socket_used(socket_ctx)) {
959 /* The input should be visible if it's used if only visibility-controlling inputs are
960 * considered. */
961 continue;
962 }
963 if (!usage_inferencer_all_unknown.is_socket_used(socket_ctx)) {
964 /* The input should be visible if it's never used, regardless of any inputs. Its usage does
965 * not depend on any visibility-controlling input. */
966 continue;
967 }
968 usage.is_visible = false;
969 }
970 for (const bNodeSocket *socket : all_output_sockets) {
971 const bNode &node = socket->owner_node();
972 if (node.is_group_input()) {
973 continue;
974 }
975 const SocketInContext socket_ctx{nullptr, socket};
976 if (usage_inferencer_only_controllers.is_disabled_output(socket_ctx)) {
977 SocketUsage &usage = all_usages[socket->index_in_tree()];
978 usage.is_visible = false;
979 }
980 }
981
982 return all_usages;
983}
984
986 const Span<InferenceValue> group_input_values,
987 const MutableSpan<SocketUsage> r_input_usages,
988 const std::optional<MutableSpan<SocketUsage>> r_output_usages)
989{
990 SocketUsage default_usage;
991 default_usage.is_used = false;
992 default_usage.is_visible = true;
993 r_input_usages.fill(default_usage);
994 if (r_output_usages) {
995 r_output_usages->fill({true, true});
996 }
997
998 ResourceScope scope;
999 bke::ComputeContextCache compute_context_cache;
1000
1001 {
1002 /* Detect actually used inputs. */
1003 const auto get_input_value = [&](const int group_input_i) {
1004 return group_input_values[group_input_i];
1005 };
1006 SocketValueInferencer value_inferencer{group, scope, compute_context_cache, get_input_value};
1007 SocketUsageInferencer usage_inferencer{group, scope, value_inferencer, compute_context_cache};
1008 for (const int i : group.interface_inputs().index_range()) {
1009 r_input_usages[i].is_used |= usage_inferencer.is_group_input_used(i);
1010 }
1011 }
1012 bool visibility_controlling_input_exists = false;
1013 for (const int i : group.interface_inputs().index_range()) {
1014 const bNodeTreeInterfaceSocket &io_socket = *group.interface_inputs()[i];
1015 if (input_may_affect_visibility(io_socket)) {
1016 visibility_controlling_input_exists = true;
1017 }
1018 }
1019 if (!visibility_controlling_input_exists) {
1020 /* If there is no visibility controller inputs, all inputs are always visible. */
1021 return;
1022 }
1023 SocketValueInferencer value_inferencer_all_unknown{group, scope, compute_context_cache};
1024 SocketUsageInferencer usage_inferencer_all_unknown{
1025 group, scope, value_inferencer_all_unknown, compute_context_cache};
1026 const auto get_only_controllers_input_value = [&](const int group_input_i) {
1027 const bNodeTreeInterfaceSocket &io_socket = *group.interface_inputs()[group_input_i];
1028 if (input_may_affect_visibility(io_socket)) {
1029 return group_input_values[group_input_i];
1030 }
1031 return InferenceValue::Unknown();
1032 };
1033 SocketValueInferencer value_inferencer_only_controllers{
1034 group, scope, compute_context_cache, get_only_controllers_input_value};
1035 SocketUsageInferencer usage_inferencer_only_controllers{
1036 group, scope, value_inferencer_only_controllers, compute_context_cache};
1037 for (const int i : group.interface_inputs().index_range()) {
1038 if (r_input_usages[i].is_used) {
1039 /* Used inputs are always visible. */
1040 continue;
1041 }
1042 if (usage_inferencer_only_controllers.is_group_input_used(i)) {
1043 /* The input should be visible if it's used if only visibility-controlling inputs are
1044 * considered. */
1045 continue;
1046 }
1047 if (!usage_inferencer_all_unknown.is_group_input_used(i)) {
1048 /* The input should be visible if it's never used, regardless of any inputs. Its usage does
1049 * not depend on any visibility-controlling input. */
1050 continue;
1051 }
1052 r_input_usages[i].is_visible = false;
1053 }
1054 if (r_output_usages) {
1055 for (const int i : group.interface_outputs().index_range()) {
1056 if (usage_inferencer_only_controllers.is_disabled_group_output(i)) {
1057 SocketUsage &usage = (*r_output_usages)[i];
1058 usage.is_used = false;
1059 usage.is_visible = false;
1060 }
1061 }
1062 }
1063}
1064
1066 Span<const bNodeSocket *> input_sockets,
1067 MutableSpan<SocketUsage> r_input_usages)
1068{
1069 BLI_assert(group.interface_inputs().size() == input_sockets.size());
1070
1071 AlignedBuffer<1024, 8> allocator_buffer;
1072 ResourceScope scope(allocator_buffer);
1073
1074 Array<InferenceValue> input_values(input_sockets.size(), InferenceValue::Unknown());
1075 for (const int i : input_sockets.index_range()) {
1076 const bNodeSocket &socket = *input_sockets[i];
1077 if (socket.is_directly_linked()) {
1078 continue;
1079 }
1080
1081 const bke::bNodeSocketType &stype = *socket.typeinfo;
1082 const CPPType *base_type = stype.base_cpp_type;
1083 if (base_type == nullptr) {
1084 continue;
1085 }
1086 void *value = scope.allocate_owned(*base_type);
1087 stype.get_base_cpp_value(socket.default_value, value);
1088 input_values[i] = InferenceValue::from_primitive(value);
1089 }
1090
1091 infer_group_interface_usage(group, input_values, r_input_usages, {}); // TODO
1092}
1093
1095 const PointerRNA &properties_ptr,
1096 MutableSpan<SocketUsage> r_input_usages,
1097 std::optional<MutableSpan<SocketUsage>> r_output_usages)
1098{
1099 ResourceScope scope;
1100 const Vector<InferenceValue> group_input_values =
1101 nodes::get_geometry_nodes_input_inference_values(group, properties_ptr, scope);
1103 group, group_input_values, r_input_usages, r_output_usages);
1104}
1105
1107 const ComputeContext *compute_context,
1108 const bNodeTree &tree,
1109 const bNode &node,
1110 const bNodeSocket &socket)
1111 : inferencer_(inferencer),
1112 compute_context_(compute_context),
1113 tree(tree),
1114 node(node),
1115 socket(socket)
1116{
1117}
1118
1120{
1121 const SocketInContext input_socket{compute_context_, this->node.input_by_identifier(identifier)};
1122 return inferencer_.impl_.get_socket_value(input_socket);
1123}
1124
1125std::optional<bool> SocketUsageParams::any_output_is_used() const
1126{
1127 const bNodeSocket *first_missing = nullptr;
1128 for (const bNodeSocket *output_socket : this->node.output_sockets()) {
1129 if (const std::optional<bool> is_used = inferencer_.impl_.all_socket_usages_.lookup_try(
1130 {compute_context_, output_socket}))
1131 {
1132 if (*is_used) {
1133 return true;
1134 }
1135 }
1136 else {
1137 first_missing = output_socket;
1138 }
1139 }
1140 if (first_missing) {
1141 inferencer_.impl_.push_usage_task({compute_context_, first_missing});
1142 return std::nullopt;
1143 }
1144 return false;
1145}
1146
1147bool SocketUsageParams::menu_input_may_be(const UString identifier, const int enum_value) const
1148{
1149 BLI_assert(this->node.input_by_identifier(identifier)->type == SOCK_MENU);
1150 const InferenceValue value = this->get_input(identifier);
1151 if (!value.is_primitive_value()) {
1152 /* The value is unknown, so it may be the requested enum value. */
1153 return true;
1154 }
1155 return value.get_primitive<MenuValue>().value == enum_value;
1156}
1157
1158bool SocketUsageParams::bool_input_may_be(const UString identifier, const bool bool_value) const
1159{
1160 BLI_assert(this->node.input_by_identifier(identifier)->type == SOCK_BOOLEAN);
1161 const InferenceValue value = this->get_input(identifier);
1162 if (!value.is_primitive_value()) {
1163 /* The value is unknown, so it may be the requested enum value. */
1164 return true;
1165 }
1166 return value.get_primitive<bool>() == bool_value;
1167}
1168
1170{
1171 impl_.mark_top_level_node_outputs_as_used();
1172}
1173
1175{
1176 return impl_.is_group_input_used(input_i);
1177}
1178
1180{
1181 return impl_.is_socket_used(socket);
1182}
1183
1185{
1186 return impl_.is_disabled_group_output(output_i);
1187}
1188
1190{
1191 return impl_.is_disabled_output(socket);
1192}
1193
1194} // namespace blender::nodes::socket_usage_inference
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
#define NODE_REROUTE
Definition BKE_node.hh:867
#define NODE_CUSTOM_GROUP
Definition BKE_node.hh:870
#define NODE_GROUP_OUTPUT
Definition BKE_node.hh:869
#define NODE_GROUP
Definition BKE_node.hh:865
#define SH_NODE_MIX_SHADER
#define SH_NODE_OUTPUT_WORLD
#define GEO_NODE_MENU_SWITCH
#define GEO_NODE_FOREACH_GEOMETRY_ELEMENT_INPUT
#define GEO_NODE_FOREACH_GEOMETRY_ELEMENT_OUTPUT
#define GEO_NODE_SWITCH
#define SH_NODE_OUTPUT_MATERIAL
#define CMP_NODE_OUTPUT_FILE
#define SH_NODE_MIX
#define GEO_NODE_CAPTURE_ATTRIBUTE
#define TEX_NODE_OUTPUT
#define GEO_NODE_WARNING
#define GEO_NODE_REPEAT_INPUT
#define GEO_NODE_SIMULATION_INPUT
#define SH_NODE_OUTPUT_LIGHT
#define SH_NODE_OUTPUT_AOV
#define SH_NODE_OUTPUT_LINESTYLE
#define GEO_NODE_INDEX_SWITCH
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define BLI_assert(a)
#define STREQ(a, b)
constexpr int64_t size() const
Definition BLI_span.hh:256
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
void * allocate_owned(const CPPType &type)
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr StringRef()
static InferenceValue from_primitive(const void *value)
SocketUsageInferencerImpl(const bNodeTree &tree, SocketValueInferencer &value_inferencer, bke::ComputeContextCache &compute_context_cache, const bool ignore_top_level_node_muting)
SocketUsageInferencer(const bNodeTree &tree, ResourceScope &scope, SocketValueInferencer &value_inferencer, bke::ComputeContextCache &compute_context_cache, bool ignore_top_level_node_muting=false)
SocketUsageParams(SocketUsageInferencer &inferencer, const ComputeContext *compute_context, const bNodeTree &tree, const bNode &node, const bNodeSocket &socket)
bool bool_input_may_be(UString identifier, bool bool_value) const
bool menu_input_may_be(UString identifier, int enum_value) const
#define storage(slot, qualifiers)
#define condition(cond)
bool is_enabled(const Scene &scene, ExecutionMode mode)
int context(const bContext *C, const char *member, bContextDataResult *result)
Array< SocketUsage > infer_all_sockets_usage(const bNodeTree &tree)
static bool input_may_affect_visibility(const bNodeTreeInterfaceSocket &socket)
void infer_group_interface_inputs_usage(const bNodeTree &group, Span< const bNodeSocket * > input_sockets, MutableSpan< SocketUsage > r_input_usages)
void infer_group_interface_usage(const bNodeTree &group, const Span< InferenceValue > group_input_values, const MutableSpan< SocketUsage > r_input_usages, const std::optional< MutableSpan< SocketUsage > > r_output_usages)
bool is_socket_selected__shader_mix_node(const SocketInContext &socket, const InferenceValue &condition)
bool is_socket_selected__mix_node(const SocketInContext &socket, const InferenceValue &condition)
bool is_socket_selected__menu_switch(const SocketInContext &socket, const InferenceValue &condition)
bool is_socket_selected__index_switch(const SocketInContext &socket, const InferenceValue &condition)
bool is_socket_selected__switch(const SocketInContext &socket, const InferenceValue &condition)
Vector< InferenceValue > get_geometry_nodes_input_inference_values(const bNodeTree &btree, const PointerRNA &properties_ptr, ResourceScope &scope)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bke::bNodeSocketType * typeinfo
eNodeSocketDatatype type
Defines a socket type.
Definition BKE_node.hh:163
const CPPType * base_cpp_type
Definition BKE_node.hh:210
SocketGetCPPValueFunction get_base_cpp_value
Definition BKE_node.hh:212