Blender V5.3
repeat_zone_operation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "DNA_node_types.h"
6
9#include "BKE_node.hh"
10#include "BKE_node_runtime.hh"
12
15#include "COM_result.hh"
16#include "COM_scheduler.hh"
17#include "COM_utilities.hh"
19
20namespace blender::compositor {
21
23{
24 const int iterations_count = this->get_input("Iterations").get_single_value_default<int>();
25 if (iterations_count <= 0) {
26 /* Pass the inputs to the outputs directly. */
27 for (const bNodeSocket *output : this->zone().output_node()->output_sockets()) {
29 continue;
30 }
31
32 Result &result = this->get_result(output->identifier);
33 if (result.should_compute()) {
34 const Result &input = this->get_input(output->identifier);
35 result.share_data(input);
36 }
37 }
38 return;
39 }
40
41 std::unique_ptr<ZoneTreeOperation> last_operation;
42 const IndexRange iterations_range = IndexRange(iterations_count);
43 for (const int64_t i : iterations_range) {
44 const bke::RepeatZoneComputeContext &compute_context =
46 &compute_context_, *this->zone().output_node(), i);
47 ZoneTreeOperation *zone_tree_operation = new ZoneTreeOperation(
48 this->context(), this->zone(), compute_context);
49
50 Result iteration_input = this->context().create_result(ResultType::Int);
51 iteration_input.allocate_single_value();
52 iteration_input.set_single_value(int(i));
53 zone_tree_operation->map_input_to_result("Iteration", &iteration_input);
54
55 if (i == iterations_range.last()) {
56 this->set_reference_counts(*zone_tree_operation);
57 }
58 Vector<std::unique_ptr<Result>> inputs = this->map_inputs(*zone_tree_operation,
59 last_operation.get());
60 Vector<std::unique_ptr<Result>> border_inputs = this->map_external_inputs(
61 *zone_tree_operation);
62 zone_tree_operation->evaluate();
63
64 last_operation.reset(zone_tree_operation);
65
66 if (this->context().is_canceled()) {
67 this->cancel_evaluation(*last_operation);
68 return;
69 }
70 }
71
72 this->write_outputs(*last_operation);
73}
74
75void RepeatZoneOperation::set_reference_counts(ZoneTreeOperation &zone_tree_operation)
76{
77 for (const bNodeSocket *output : this->zone().output_node()->output_sockets()) {
79 continue;
80 }
81
82 Result &zone_result = zone_tree_operation.get_result(output->identifier);
83 Result &repeat_zone_result = this->get_result(output->identifier);
84 zone_result.set_reference_count(repeat_zone_result.should_compute() ? 1 : 0);
85 }
86}
87
88Vector<std::unique_ptr<Result>> RepeatZoneOperation::map_inputs(
89 ZoneTreeOperation &zone_tree_operation, ZoneTreeOperation *last_zone_tree_operation)
90{
91 Vector<std::unique_ptr<Result>> temporary_inputs;
92 /* If a last operation exist, map inputs to the outputs of the last iteration instead. */
93 if (last_zone_tree_operation) {
94 for (const bNodeSocket *output : this->zone().output_node()->output_sockets()) {
96 continue;
97 }
98
99 Result &last_result = last_zone_tree_operation->get_result(output->identifier);
100 std::unique_ptr<Result> temporary_input = std::make_unique<Result>(
101 this->context().create_result(last_result.type(), last_result.precision()));
102 temporary_input->share_data(last_result);
103 temporary_inputs.append(std::move(temporary_input));
104 zone_tree_operation.map_input_to_result(output->identifier, temporary_inputs.last().get());
105 }
106 return temporary_inputs;
107 }
108
109 /* Otherwise, map the initial inputs of the operation. */
110 for (const bNodeSocket *output : this->zone().output_node()->output_sockets()) {
112 continue;
113 }
114
115 const Result &input_result = this->get_input(output->identifier);
116 std::unique_ptr<Result> temporary_input = std::make_unique<Result>(
117 this->context().create_result(input_result.type(), input_result.precision()));
118 temporary_input->share_data(input_result);
119 temporary_inputs.append(std::move(temporary_input));
120 zone_tree_operation.map_input_to_result(output->identifier, temporary_inputs.last().get());
121 }
122 return temporary_inputs;
123}
124
125Vector<std::unique_ptr<Result>> RepeatZoneOperation::map_external_inputs(
126 ZoneTreeOperation &zone_tree_operation)
127{
128 Vector<std::unique_ptr<Result>> temporary_inputs;
129 Set<const bNodeSocket *> outputs_already_considered;
130 for (const bNodeLink *link : this->zone().border_links) {
131 const bNodeSocket *output = get_output_linked_to_input(*link->tosock);
132 if (!output) {
133 continue;
134 }
135
136 if (outputs_already_considered.contains(output)) {
137 continue;
138 }
139 outputs_already_considered.add_new(output);
140
141 const std::string input_identifier = ZoneTreeOperation::get_external_input_identifier(*output);
142 const Result &input_result = this->get_input(input_identifier);
143 std::unique_ptr<Result> temporary_input = std::make_unique<Result>(
144 this->context().create_result(input_result.type(), input_result.precision()));
145 temporary_input->share_data(input_result);
146 temporary_inputs.append(std::move(temporary_input));
147 zone_tree_operation.map_input_to_result(input_identifier, temporary_inputs.last().get());
148 }
149 return temporary_inputs;
150}
151
152void RepeatZoneOperation::write_outputs(ZoneTreeOperation &zone_tree_operation)
153{
154 for (const bNodeSocket *output : this->zone().output_node()->output_sockets()) {
156 continue;
157 }
158
159 Result &zone_tree_result = zone_tree_operation.get_result(output->identifier);
160 Result &repeat_zone_result = this->get_result(output->identifier);
161 if (repeat_zone_result.should_compute()) {
162 repeat_zone_result.share_data(zone_tree_result);
163 zone_tree_result.release();
164 }
165 }
166}
167
168void RepeatZoneOperation::cancel_evaluation(ZoneTreeOperation &last_zone_tree_operation)
169{
171
172 for (const bNodeSocket *output : this->zone().output_node()->output_sockets()) {
174 continue;
175 }
176
177 Result &zone_tree_result = last_zone_tree_operation.get_result(output->identifier);
178 if (zone_tree_result.should_compute()) {
179 zone_tree_result.release();
180 }
181 }
182}
183
184} // namespace blender::compositor
#define output
constexpr IndexRange()=default
const RepeatZoneComputeContext & for_repeat_zone(const ComputeContext *parent, int32_t output_node_id, int iteration)
Result create_result(ResultType type, ResultPrecision precision)
bke::ComputeContextCache & compute_context_cache() 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 set_single_value(const T &value)
const bke::bNodeTreeZone & zone() const
static std::string get_external_input_identifier(const bNodeSocket &output)
ZoneTreeOperation(Context &context, const bke::bNodeTreeZone &zone, const ComputeContext &compute_context)
const bNodeSocket * get_output_linked_to_input(const bNodeSocket &input)
Definition utilities.cc:31
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
__int64 int64_t
Definition BKE_text.h:90