Blender V5.3
group_node_operation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <memory>
6
7#include "BLI_assert.hh"
8#include "BLI_vector.hh"
9
10#include "BLT_translation.hh"
11
12#include "DNA_node_types.h"
13#include "DNA_userdef_types.h"
14
17#include "BKE_node.hh"
18#include "BKE_node_runtime.hh"
19
21
24#include "COM_node_operation.hh"
25#include "COM_result.hh"
26#include "COM_utilities.hh"
27
28namespace blender::compositor {
29
30/* A node operation representing a group node. This is a thin wrapper around a NodeGroupOperation
31 * mapping its own inputs to the inputs of the node group operation and sharing its results with
32 * the results of the node group operation. */
34 public:
36 {
37 for (const bNodeSocket *input : node.input_sockets()) {
38 if (!is_socket_available(input)) {
39 continue;
40 }
41
42 InputDescriptor &descriptor = this->get_input_descriptor(input->identifier);
43 /* The structure type of the inputs of Group nodes are inferred, so we need to make sure this
44 * is not wrongly expecting single values. */
45 descriptor.expects_single_value = false;
46 /* Groups nodes should not force realization since it is defined by the user, and there is
47 * currently no way for the user to define that through the UI. */
48 descriptor.realization_mode = InputRealizationMode::None;
49 }
50 }
51
52 void execute() override
53 {
54 const bNodeTree *node_group = this->get_node_group();
55 const bNodeTree *original_node_group = DEG_get_original(node_group);
56 if (!original_node_group || ID_MISSING(original_node_group)) {
58 return;
59 }
60
61 if (this->get_compute_context().parents_num() >= U.nodes_stack_limit) {
63 TIP_("Stack limit reached. Group node is ignored."));
65 return;
66 }
67
68 const bke::GroupNodeComputeContext &compute_context =
69 this->context().compute_context_cache().for_group_node(
70 &this->get_compute_context(), this->node().identifier, &this->node().owner_tree());
71 NodeGroupOperation operation(this->context(), *node_group, compute_context);
72
73 this->set_reference_counts(operation);
74 Vector<std::unique_ptr<Result>> temporary_inputs = this->map_inputs(operation);
75 operation.evaluate();
76 this->write_outputs(operation);
77 }
78
79 /* Sets the reference counts of the node group operation according to the needed status of the
80 * outputs of the group node. */
82 {
83 const bNodeTree *node_group = this->get_node_group();
84 node_group->ensure_interface_cache();
85 for (const bNodeTreeInterfaceSocket *output_socket : node_group->interface_outputs()) {
86 Result &node_group_result = operation.get_result(output_socket->identifier);
87 Result &group_node_result = this->get_result(output_socket->identifier);
88 node_group_result.set_reference_count(group_node_result.should_compute() ? 1 : 0);
89 }
90 }
91
92 /* Maps the input results of the node group operation to this group node's inputs through
93 * temporary results that share the data of the this group's inputs. */
95 {
96 const bNodeTree *node_group = this->get_node_group();
97 Vector<std::unique_ptr<Result>> temporary_inputs;
98 node_group->ensure_interface_cache();
99 for (const bNodeTreeInterfaceSocket *input_socket : node_group->interface_inputs()) {
100 const Result &input_result = this->get_input(input_socket->identifier);
101 std::unique_ptr<Result> temporary_input = std::make_unique<Result>(
102 this->context().create_result(input_result.type(), input_result.precision()));
103 temporary_input->share_data(input_result);
104 temporary_inputs.append(std::move(temporary_input));
105 operation.map_input_to_result(input_socket->identifier, temporary_inputs.last().get());
106 }
107 return temporary_inputs;
108 }
109
110 /* Writes the output results of the node group operation to this group node operation by sharing
111 * its data and freeing the results. */
112 void write_outputs(Operation &operation)
113 {
114 const bNodeTree *node_group = this->get_node_group();
115 node_group->ensure_interface_cache();
116 for (const bNodeTreeInterfaceSocket *output_socket : node_group->interface_outputs()) {
117 Result &node_group_result = operation.get_result(output_socket->identifier);
118 Result &group_node_result = this->get_result(output_socket->identifier);
119 if (group_node_result.should_compute()) {
120 group_node_result.share_data(node_group_result);
121 node_group_result.release();
122 }
123 }
124 }
125
127 {
128 BLI_assert(this->node().is_group());
129 return reinterpret_cast<const bNodeTree *>(this->node().id);
130 }
131};
132
134{
135 return new GroupNodeOperation(context, node);
136}
137
138} // namespace blender::compositor
#define TIP_(msgid)
#define ID_MISSING(_id)
Definition DNA_ID.h:695
void set_reference_counts(Operation &operation)
Vector< std::unique_ptr< Result > > map_inputs(Operation &operation)
GroupNodeOperation(Context &context, const bNode &node)
NodeOperation(Context &context, const bNode &node)
void add_warning(nodes::NodeWarningType type, std::string message)
const ComputeContext & get_compute_context() const
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
void map_input_to_result(StringRef identifier, Result *result)
Definition operation.cc:45
void share_data(const Result &source)
Definition result.cc:996
bool should_compute() const
Definition result.cc:1124
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
void set_reference_count(int count)
Definition result.cc:1079
NodeOperation * get_group_node_operation(Context &context, const bNode &node)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
T * DEG_get_original(T *id)