/* SPDX-FileCopyrightText: 2024 Blender Authors * * SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "NOD_geometry_nodes_execute.hh" #include "NOD_menu_value.hh" #include "NOD_multi_function.hh" #include "NOD_node_declaration.hh" #include "NOD_node_in_compute_context.hh" #include "NOD_socket_usage_inference.hh" #include "DNA_anim_types.h" #include "DNA_material_types.h" #include "DNA_node_types.h" #include "BKE_compute_context_cache.hh" #include "BKE_compute_contexts.hh" #include "BKE_node_legacy_types.hh" #include "BKE_node_runtime.hh" #include "BKE_type_conversions.hh" #include "ANIM_action.hh" #include "ANIM_action_iterators.hh" #include "BLI_listbase.h" #include "BLI_stack.hh" namespace blender::nodes::socket_usage_inference { /** Utility class to simplify passing global state into all the functions during inferencing. */ struct SocketUsageInferencer { private: friend InputSocketUsageParams; ResourceScope &scope_; bke::ComputeContextCache &compute_context_cache_; /** Inferences the socket values if possible. */ SocketValueInferencer value_inferencer_; /** Root node tree. */ const bNodeTree &root_tree_; /** * Stack of tasks that allows depth-first (partial) evaluation of the tree. */ Stack usage_tasks_; /** * If the usage of a socket is known, it is added to this map. Sockets not in this map are not * known yet. */ Map all_socket_usages_; /** * Treat top-level nodes as if they are never muted for usage-inferencing. This is used when * computing the socket usage that is displayed in the node editor (through grayed out or hidden * sockets). Which inputs/outputs of a node is visible should never depend on whether it is muted * or not. */ bool ignore_top_level_node_muting_ = false; public: SocketUsageInferencer(const bNodeTree &tree, const std::optional> tree_input_values, ResourceScope &scope, bke::ComputeContextCache &compute_context_cache, const std::optional> top_level_ignored_inputs = std::nullopt, const bool ignore_top_level_node_muting = false) : scope_(scope), compute_context_cache_(compute_context_cache), value_inferencer_( tree, scope_, compute_context_cache_, tree_input_values, top_level_ignored_inputs), root_tree_(tree), ignore_top_level_node_muting_(ignore_top_level_node_muting) { root_tree_.ensure_topology_cache(); root_tree_.ensure_interface_cache(); } void mark_top_level_node_outputs_as_used() { for (const bNode *node : root_tree_.all_nodes()) { if (node->is_group_input()) { /* Can skip these sockets, because they don't affect usage anyway, and there may be a lot * of them. See #144756. */ continue; } for (const bNodeSocket *socket : node->output_sockets()) { all_socket_usages_.add_new({nullptr, socket}, true); } } } bool is_group_input_used(const int input_i) { for (const bNode *node : root_tree_.group_input_nodes()) { const SocketInContext socket{nullptr, &node->output_socket(input_i)}; if (this->is_socket_used(socket)) { return true; } } return false; } bool is_socket_used(const SocketInContext &socket) { const std::optional is_used = all_socket_usages_.lookup_try(socket); if (is_used.has_value()) { return *is_used; } if (socket->is_output() && !socket->is_directly_linked()) { /* In this case we can return early because the socket can't be used if it's not linked. */ return false; } if (socket->owner_tree().has_available_link_cycle()) { return false; } BLI_assert(usage_tasks_.is_empty()); usage_tasks_.push(socket); while (!usage_tasks_.is_empty()) { const SocketInContext &socket = usage_tasks_.peek(); this->usage_task(socket); if (&socket == &usage_tasks_.peek()) { /* The task is finished if it hasn't added any new task it depends on. */ usage_tasks_.pop(); } } return all_socket_usages_.lookup(socket); } InferenceValue get_socket_value(const SocketInContext &socket) { return value_inferencer_.get_socket_value(socket); } private: void usage_task(const SocketInContext &socket) { if (all_socket_usages_.contains(socket)) { return; } const bNode &node = socket->owner_node(); if (!socket->is_available()) { all_socket_usages_.add_new(socket, false); return; } if (node.is_undefined() && !node.is_custom_group()) { all_socket_usages_.add_new(socket, false); return; } if (socket->is_input()) { this->usage_task__input(socket); } else { this->usage_task__output(socket); } } void usage_task__input(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); if (node->is_muted()) { const bool is_top_level = socket.context == nullptr; if (!this->ignore_top_level_node_muting_ || !is_top_level) { this->usage_task__input__muted_node(socket); return; } } switch (node->type_legacy) { case NODE_GROUP: case NODE_CUSTOM_GROUP: { this->usage_task__input__group_node(socket); break; } case NODE_GROUP_OUTPUT: { this->usage_task__input__group_output_node(socket); break; } case GEO_NODE_SWITCH: { this->usage_task__input__generic_switch( socket, switch_node_inference_utils::is_socket_selected__switch); break; } case GEO_NODE_INDEX_SWITCH: { this->usage_task__input__generic_switch( socket, switch_node_inference_utils::is_socket_selected__index_switch); break; } case GEO_NODE_MENU_SWITCH: { if (socket->index() == 0) { this->usage_task__input__fallback(socket); } else { this->usage_task__input__generic_switch( socket, switch_node_inference_utils::is_socket_selected__menu_switch); } break; } case SH_NODE_MIX: { this->usage_task__input__generic_switch( socket, switch_node_inference_utils::is_socket_selected__mix_node); break; } case SH_NODE_MIX_SHADER: { this->usage_task__input__generic_switch( socket, switch_node_inference_utils::is_socket_selected__shader_mix_node); break; } case GEO_NODE_SIMULATION_INPUT: { this->usage_task__input__simulation_input_node(socket); break; } case GEO_NODE_REPEAT_INPUT: { this->usage_task__input__repeat_input_node(socket); break; } case GEO_NODE_FOREACH_GEOMETRY_ELEMENT_INPUT: { this->usage_task__input__foreach_element_input_node(socket); break; } case GEO_NODE_FOREACH_GEOMETRY_ELEMENT_OUTPUT: { this->usage_task__input__foreach_element_output_node(socket); break; } case GEO_NODE_CAPTURE_ATTRIBUTE: { this->usage_task__input__capture_attribute_node(socket); break; } case SH_NODE_OUTPUT_AOV: case SH_NODE_OUTPUT_LIGHT: case SH_NODE_OUTPUT_WORLD: case SH_NODE_OUTPUT_LINESTYLE: case SH_NODE_OUTPUT_MATERIAL: case CMP_NODE_OUTPUT_FILE: case TEX_NODE_OUTPUT: { this->usage_task__input__output_node(socket); break; } default: { this->usage_task__input__fallback(socket); break; } } } void usage_task__input__output_node(const SocketInContext &socket) { all_socket_usages_.add_new(socket, true); } /** * Assumes that the first input is a condition that selects one of the remaining inputs which is * then output. If necessary, this can trigger a value task for the condition socket. */ void usage_task__input__generic_switch( const SocketInContext &socket, const FunctionRef is_selected_socket) { const NodeInContext node = socket.owner_node(); BLI_assert(node->input_sockets().size() >= 1); BLI_assert(node->output_sockets().size() >= 1); if (socket->type == SOCK_CUSTOM && STREQ(socket->idname, "NodeSocketVirtual")) { all_socket_usages_.add_new(socket, false); return; } const SocketInContext output_socket{socket.context, get_first_available_bsocket(node->output_sockets())}; const std::optional output_is_used = all_socket_usages_.lookup_try(output_socket); if (!output_is_used.has_value()) { this->push_usage_task(output_socket); return; } if (!*output_is_used) { all_socket_usages_.add_new(socket, false); return; } const SocketInContext condition_socket{socket.context, get_first_available_bsocket(node->input_sockets())}; if (socket == condition_socket) { all_socket_usages_.add_new(socket, true); return; } const InferenceValue condition_value = this->get_socket_value(condition_socket); if (condition_value.is_unknown()) { /* The exact condition value is unknown, so any input may be used. */ all_socket_usages_.add_new(socket, true); return; } const bool is_used = is_selected_socket(socket, condition_value); all_socket_usages_.add_new(socket, is_used); } void usage_task__input__group_node(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); const bNodeTree *group = reinterpret_cast(node->id); if (!group || ID_MISSING(&group->id)) { all_socket_usages_.add_new(socket, false); return; } group->ensure_topology_cache(); if (group->has_available_link_cycle()) { all_socket_usages_.add_new(socket, false); return; } /* The group node input is used if any of the matching group inputs within the group is * used. */ const ComputeContext &group_context = compute_context_cache_.for_group_node( socket.context, node->identifier, &node->owner_tree()); Vector dependent_sockets; for (const bNode *group_input_node : group->group_input_nodes()) { const bNodeSocket &group_input_socket = group_input_node->output_socket(socket->index()); if (group_input_socket.is_directly_linked()) { /* Skip unlinked group inputs to avoid further unnecessary processing of them further down * the line. */ dependent_sockets.append(&group_input_socket); } } this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, &group_context); } void usage_task__input__group_output_node(const SocketInContext &socket) { const int output_i = socket->index(); if (socket.context == nullptr) { /* This is a final output which is always used. */ all_socket_usages_.add_new(socket, true); return; } /* The group output node is used if the matching output of the parent group node is used. */ const bke::GroupNodeComputeContext &group_context = *static_cast(socket.context); const bNodeSocket &group_node_output = group_context.node()->output_socket(output_i); this->usage_task__with_dependent_sockets( socket, {&group_node_output}, {}, group_context.parent()); } void usage_task__output(const SocketInContext &socket) { /* An output socket is used if any of the sockets it is connected to is used. */ Vector dependent_sockets; for (const bNodeLink *link : socket->directly_linked_links()) { if (link->is_used()) { dependent_sockets.append(link->tosock); } } this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context); } void usage_task__input__simulation_input_node(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); const bNodeTree &tree = socket->owner_tree(); const NodeGeometrySimulationInput &storage = *static_cast( node->storage); const bNode *sim_output_node = tree.node_by_id(storage.output_node_id); if (!sim_output_node) { all_socket_usages_.add_new(socket, false); return; } /* Simulation inputs are also used when any of the simulation outputs are used. */ Vector dependent_sockets; dependent_sockets.extend(node->output_sockets()); dependent_sockets.extend(sim_output_node->output_sockets()); this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context); } void usage_task__input__repeat_input_node(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); const bNodeTree &tree = socket->owner_tree(); const NodeGeometryRepeatInput &storage = *static_cast( node->storage); const bNode *repeat_output_node = tree.node_by_id(storage.output_node_id); if (!repeat_output_node) { all_socket_usages_.add_new(socket, false); return; } /* Assume that all repeat inputs are used when any of the outputs are used. This check could * become more precise in the future if necessary. */ Vector dependent_sockets; dependent_sockets.extend(node->output_sockets()); dependent_sockets.extend(repeat_output_node->output_sockets()); this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context); } void usage_task__input__foreach_element_output_node(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); this->usage_task__with_dependent_sockets( socket, {node->output_by_identifier(socket->identifier)}, {}, socket.context); } void usage_task__input__capture_attribute_node(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); this->usage_task__with_dependent_sockets( socket, {&node->output_socket(socket->index())}, {}, socket.context); } void usage_task__input__fallback(const SocketInContext &socket) { const SocketDeclaration *socket_decl = socket->runtime->declaration; if (!socket_decl) { all_socket_usages_.add_new(socket, true); return; } if (!socket_decl->usage_inference_fn) { this->usage_task__with_dependent_sockets( socket, socket->owner_node().output_sockets(), {}, socket.context); return; } InputSocketUsageParams params{ *this, socket.context, socket->owner_tree(), socket->owner_node(), *socket}; const std::optional is_used = (*socket_decl->usage_inference_fn)(params); if (!is_used.has_value()) { /* Some value was requested, come back later when that value is available. */ return; } all_socket_usages_.add_new(socket, *is_used); } void usage_task__input__foreach_element_input_node(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); const bNodeTree &tree = socket->owner_tree(); const NodeGeometryForeachGeometryElementInput &storage = *static_cast(node->storage); const bNode *foreach_output_node = tree.node_by_id(storage.output_node_id); if (!foreach_output_node) { all_socket_usages_.add_new(socket, false); return; } Vector dependent_sockets; if (StringRef(socket->identifier).startswith("Input_")) { dependent_sockets.append(node->output_by_identifier(socket->identifier)); } else { /* The geometry and selection inputs are used whenever any of the zone outputs is used. */ dependent_sockets.extend(node->output_sockets()); dependent_sockets.extend(foreach_output_node->output_sockets()); } this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context); } void usage_task__input__muted_node(const SocketInContext &socket) { const NodeInContext node = socket.owner_node(); Vector dependent_sockets; for (const bNodeLink &internal_link : node->internal_links()) { if (internal_link.fromsock != socket.socket) { continue; } dependent_sockets.append(internal_link.tosock); } this->usage_task__with_dependent_sockets(socket, dependent_sockets, {}, socket.context); } /** * Utility that handles simple cases where a socket is used if any of its dependent sockets is * used. */ void usage_task__with_dependent_sockets(const SocketInContext &socket, const Span dependent_outputs, const Span condition_inputs, const ComputeContext *dependent_socket_context) { /* Check if any of the dependent outputs are used. */ SocketInContext next_unknown_output; bool any_output_used = false; for (const bNodeSocket *dependent_socket_ptr : dependent_outputs) { const SocketInContext dependent_socket{dependent_socket_context, dependent_socket_ptr}; const std::optional is_used = all_socket_usages_.lookup_try(dependent_socket); if (!is_used.has_value() && !next_unknown_output) { next_unknown_output = dependent_socket; continue; } if (is_used.value_or(false)) { any_output_used = true; break; } } if (next_unknown_output) { /* Create a task that checks if the next dependent socket is used. Intentionally only create * a task for the very next one and not for all, because that could potentially trigger a lot * of unnecessary evaluations. */ this->push_usage_task(next_unknown_output); return; } if (!any_output_used) { all_socket_usages_.add_new(socket, false); return; } bool all_condition_inputs_true = true; for (const bNodeSocket *condition_input_ptr : condition_inputs) { const SocketInContext condition_input{dependent_socket_context, condition_input_ptr}; const InferenceValue condition_value = this->get_socket_value(condition_input); if (!condition_value.is_primitive_value()) { /* The condition is not known, so it may be true. */ continue; } BLI_assert(condition_input_ptr->type == SOCK_BOOLEAN); if (!condition_value.get_primitive()) { all_condition_inputs_true = false; break; } } all_socket_usages_.add_new(socket, all_condition_inputs_true); } void push_usage_task(const SocketInContext &socket) { usage_tasks_.push(socket); } static const bNodeSocket *get_first_available_bsocket(const Span sockets) { for (const bNodeSocket *socket : sockets) { if (socket->is_available()) { return socket; } } return nullptr; } }; static bool input_may_affect_visibility(const bNodeTreeInterfaceSocket &socket) { return socket.socket_type == StringRef("NodeSocketMenu"); } static bool input_may_affect_visibility(const bNodeSocket &socket) { return socket.type == SOCK_MENU; } Array infer_all_input_sockets_usage(const bNodeTree &tree) { tree.ensure_topology_cache(); const Span all_input_sockets = tree.all_input_sockets(); Array all_usages(all_input_sockets.size()); ResourceScope scope; bke::ComputeContextCache compute_context_cache; const bool ignore_top_level_node_muting = true; { /* Find actual socket usages. */ SocketUsageInferencer inferencer{tree, std::nullopt, scope, compute_context_cache, std::nullopt, ignore_top_level_node_muting}; inferencer.mark_top_level_node_outputs_as_used(); for (const int i : all_input_sockets.index_range()) { const bNodeSocket &socket = *all_input_sockets[i]; all_usages[i].is_used = inferencer.is_socket_used({nullptr, &socket}); } } /* Find input sockets that should be hidden. */ Array only_controllers_used(all_input_sockets.size(), NoInitialization{}); Array all_ignored_inputs(all_input_sockets.size(), true); threading::parallel_for(all_input_sockets.index_range(), 1024, [&](const IndexRange range) { for (const int i : range) { const bNodeSocket &socket = *all_input_sockets[i]; only_controllers_used[i] = !input_may_affect_visibility(socket); } }); SocketUsageInferencer inferencer_all_unknown{tree, std::nullopt, scope, compute_context_cache, all_ignored_inputs, ignore_top_level_node_muting}; SocketUsageInferencer inferencer_only_controllers{tree, std::nullopt, scope, compute_context_cache, only_controllers_used, ignore_top_level_node_muting}; inferencer_all_unknown.mark_top_level_node_outputs_as_used(); inferencer_only_controllers.mark_top_level_node_outputs_as_used(); for (const int i : all_input_sockets.index_range()) { if (all_usages[i].is_used) { /* Used inputs are always visible. */ continue; } const SocketInContext socket{nullptr, all_input_sockets[i]}; if (inferencer_only_controllers.is_socket_used(socket)) { /* The input should be visible if it's used if only visibility-controlling inputs are * considered. */ continue; } if (!inferencer_all_unknown.is_socket_used(socket)) { /* The input should be visible if it's never used, regardless of any inputs. Its usage does * not depend on any visibility-controlling input. */ continue; } all_usages[i].is_visible = false; } return all_usages; } void infer_group_interface_inputs_usage(const bNodeTree &group, const Span group_input_values, const MutableSpan r_input_usages) { SocketUsage default_usage; default_usage.is_used = false; default_usage.is_visible = true; r_input_usages.fill(default_usage); ResourceScope scope; bke::ComputeContextCache compute_context_cache; { /* Detect actually used inputs. */ SocketUsageInferencer inferencer{group, group_input_values, scope, compute_context_cache}; for (const bNode *node : group.group_input_nodes()) { for (const int i : group.interface_inputs().index_range()) { const bNodeSocket &socket = node->output_socket(i); r_input_usages[i].is_used |= inferencer.is_socket_used({nullptr, &socket}); } } } if (std::all_of(r_input_usages.begin(), r_input_usages.end(), [](const SocketUsage &usage) { return usage.is_used; })) { /* If all inputs are used, there is no need to infer visibility because all inputs should be * visible. */ return; } bool visibility_controlling_input_exists = false; Array inputs_all_unknown(group_input_values.size(), InferenceValue::Unknown()); Array inputs_only_controllers = group_input_values; for (const int i : group.interface_inputs().index_range()) { const bNodeTreeInterfaceSocket &io_socket = *group.interface_inputs()[i]; if (input_may_affect_visibility(io_socket)) { visibility_controlling_input_exists = true; } else { inputs_only_controllers[i] = InferenceValue::Unknown(); } } if (!visibility_controlling_input_exists) { /* If there is no visibility controller inputs, all inputs are always visible. */ return; } SocketUsageInferencer inferencer_all_unknown{ group, inputs_all_unknown, scope, compute_context_cache}; SocketUsageInferencer inferencer_only_controllers{ group, inputs_only_controllers, scope, compute_context_cache}; for (const int i : group.interface_inputs().index_range()) { if (r_input_usages[i].is_used) { /* Used inputs are always visible. */ continue; } if (inferencer_only_controllers.is_group_input_used(i)) { /* The input should be visible if it's used if only visibility-controlling inputs are * considered. */ continue; } if (!inferencer_all_unknown.is_group_input_used(i)) { /* The input should be visible if it's never used, regardless of any inputs. Its usage does * not depend on any visibility-controlling input. */ continue; } r_input_usages[i].is_visible = false; } } void infer_group_interface_inputs_usage(const bNodeTree &group, Span input_sockets, MutableSpan r_input_usages) { BLI_assert(group.interface_inputs().size() == input_sockets.size()); AlignedBuffer<1024, 8> allocator_buffer; ResourceScope scope; scope.allocator().provide_buffer(allocator_buffer); Array input_values(input_sockets.size(), InferenceValue::Unknown()); for (const int i : input_sockets.index_range()) { const bNodeSocket &socket = *input_sockets[i]; if (socket.is_directly_linked()) { continue; } const bke::bNodeSocketType &stype = *socket.typeinfo; const CPPType *base_type = stype.base_cpp_type; if (base_type == nullptr) { continue; } void *value = scope.allocate_owned(*base_type); stype.get_base_cpp_value(socket.default_value, value); input_values[i] = InferenceValue::from_primitive(value); } infer_group_interface_inputs_usage(group, input_values, r_input_usages); } void infer_group_interface_inputs_usage(const bNodeTree &group, const IDProperty *properties, MutableSpan r_input_usages) { ResourceScope scope; const Vector group_input_values = nodes::get_geometry_nodes_input_inference_values(group, properties, scope); nodes::socket_usage_inference::infer_group_interface_inputs_usage( group, group_input_values, r_input_usages); } InputSocketUsageParams::InputSocketUsageParams(SocketUsageInferencer &inferencer, const ComputeContext *compute_context, const bNodeTree &tree, const bNode &node, const bNodeSocket &socket) : inferencer_(inferencer), compute_context_(compute_context), tree(tree), node(node), socket(socket) { } InferenceValue InputSocketUsageParams::get_input(const StringRef identifier) const { const SocketInContext input_socket{compute_context_, this->node.input_by_identifier(identifier)}; return inferencer_.get_socket_value(input_socket); } std::optional InputSocketUsageParams::any_output_is_used() const { const bNodeSocket *first_missing = nullptr; for (const bNodeSocket *output_socket : this->node.output_sockets()) { if (const std::optional is_used = inferencer_.all_socket_usages_.lookup_try( {compute_context_, output_socket})) { if (*is_used) { return true; } } else { first_missing = output_socket; } } if (first_missing) { inferencer_.push_usage_task({compute_context_, first_missing}); return std::nullopt; } return false; } bool InputSocketUsageParams::menu_input_may_be(const StringRef identifier, const int enum_value) const { BLI_assert(this->node.input_by_identifier(identifier)->type == SOCK_MENU); const InferenceValue value = this->get_input(identifier); if (!value.is_primitive_value()) { /* The value is unknown, so it may be the requested enum value. */ return true; } return value.get_primitive().value == enum_value; } } // namespace blender::nodes::socket_usage_inference