Blender V5.3
pixel_operation.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
5#include <limits>
6#include <string>
7
8#include "BLI_map.hh"
9#include "BLI_string_ref.hh"
10#include "BLI_vector_set.hh"
11
12#include "DNA_node_types.h"
13
14#include "BKE_node.hh"
15#include "BKE_node_runtime.hh"
17
18#include "NOD_eval_log.hh"
19
21#include "COM_context.hh"
23#include "COM_operation.hh"
25#include "COM_result.hh"
26#include "COM_scheduler.hh"
27#include "COM_utilities.hh"
28
29namespace blender::compositor {
30
32 NodeTreeEvaluator &node_tree_evaluator,
33 const ComputeContext &compute_context,
34 const bool is_single_value)
36 node_tree_evaluator_(node_tree_evaluator),
37 compute_context_(compute_context),
38 is_single_value_(is_single_value)
39{
40}
41
55
57{
59 if (!log) {
60 return;
61 }
62 nodes::eval_log::NodeTreeLogger &tree_logger = log->get_local_tree_logger(compute_context_);
63
64 if (is_single_value_) {
65 for (const bNodeSocket *output_socket : logged_outputs_) {
66 Result &result = this->get_result(
67 this->get_output_identifier_from_output_socket(*output_socket));
68 tree_logger.log_value(output_socket->owner_node(), *output_socket, result.single_value());
69
70 /* Logged results gets as an extra reference in pixel operations as can be seen in the
71 * compute_results_reference_counts method, so release it after logging. */
72 result.release();
73 }
74
75 return;
76 }
77
78 const Domain domain = this->compute_domain();
79
80 /* All inputs and outputs of pixel operations operate in the same domain, so the operation domain
81 * should be logged for all. The exception is inputs that are single values, in which case, their
82 * value is simply logged. */
84 /* Log output values. */
85 for (const bNodeSocket *output_socket : node->output_sockets()) {
86 if (!is_socket_available(output_socket)) {
87 continue;
88 }
89
90 if (!output_socket->is_logically_linked()) {
91 continue;
92 }
93
94 tree_logger.output_socket_values.append(
95 *tree_logger.allocator,
96 {node->identifier,
97 output_socket->index(),
98 get_image_info_log(tree_logger.allocator, domain, this->context().get_precision())});
99 }
100
101 /* Log input values. */
102 for (const bNodeSocket *input_socket : node->input_sockets()) {
103 if (!is_socket_available(input_socket)) {
104 continue;
105 }
106
107 if (node_tree_evaluator_.schedule().unneeded_inputs.contains(input_socket)) {
108 continue;
109 }
110
111 /* The input has an implicit value. Get the input that corresponds to it, if it is a single
112 * value, log that single value, if not, we log the operation domain. */
113 const InputDescriptor input_descriptor = input_descriptor_from_input_socket(input_socket);
114 if (!input_socket->is_logically_linked() && input_descriptor.implicit_input.has_value()) {
115 const std::string &input_identifier = implicit_inputs_to_input_identifiers_map_.lookup(
116 input_descriptor.implicit_input.value());
117 const Result &input = this->get_input(input_identifier);
118 if (input.is_single_value()) {
119 tree_logger.log_value(*node, *input_socket, input.single_value());
120 continue;
121 }
122
123 tree_logger.input_socket_values.append(
124 *tree_logger.allocator,
125 {node->identifier,
126 input_socket->index(),
127 get_image_info_log(tree_logger.allocator, domain, this->context().get_precision())});
128 continue;
129 }
130
131 if (!input_socket->is_logically_linked()) {
132 continue;
133 }
134
135 /* The input is linked to a node that is inside the pixel operation, so skip it since it will
136 * inherit its value from an output that was logged above. */
137 const bNodeSocket &linked_output = *input_socket->logically_linked_sockets()[0];
138 if (node_tree_evaluator_.pixel_compile_unit().contains(&linked_output.owner_node())) {
139 continue;
140 }
141
142 /* Otherwise, it is linked to a node that is outside of the compile unit. If it is a single
143 * value, skip it since it will inherit its value from an output that was logged before, if
144 * not, we log the operation domain. */
145 const Result &input = node_tree_evaluator_.get_result_from_output_socket(linked_output);
146 if (input.is_single_value()) {
147 continue;
148 }
149
150 tree_logger.input_socket_values.append(
151 *tree_logger.allocator,
152 {node->identifier,
153 input_socket->index(),
154 get_image_info_log(tree_logger.allocator, domain, this->context().get_precision())});
155 }
156 }
157
158 for (const bNodeSocket *output : preview_outputs_) {
161 tree_logger.node_image_previews.append(*tree_logger.allocator,
162 {output->owner_node().identifier, preview});
163
164 /* Preview results gets as an extra reference in pixel operations as can be seen in the
165 * compute_results_reference_counts method, so release it after computing preview. */
166 result.release();
167 }
168}
169
175
180
185
190
192{
193 for (const auto item : output_sockets_to_output_identifiers_map_.items()) {
194 const bNodeSocket &output = *item.key;
195 const std::string identifier = item.value;
196
197 /* We ignore inputs that are part of the pixel compile unit, because they are internal and do
198 * not deal with the result directly. */
199 int reference_count = compute_output_reference_count(
201
202 if (preview_outputs_.contains(&output)) {
203 reference_count++;
204 }
205
206 if (logged_outputs_.contains(&output)) {
207 reference_count++;
208 }
209
210 this->get_result(identifier).set_reference_count(reference_count);
211 }
212}
213
214} // namespace blender::compositor
#define output
destruct_ptr< T > construct(Args &&...args)
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool contains(const Key &key) const
virtual nodes::eval_log::NodesEvalLog * nodes_evaluation_log() const
RealizationOptions realization_options
std::optional< ImplicitInputType > implicit_input
Result & get_result_from_output_socket(const bNodeSocket &output)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Operation(Context &context)
Definition operation.cc:22
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
Map< ImplicitInputType, std::string > implicit_inputs_to_input_identifiers_map_
VectorSet< const bNodeSocket * > preview_outputs_
Map< ImplicitInputType, std::string > & get_implicit_inputs_to_input_identifiers_map()
StringRef get_output_identifier_from_output_socket(const bNodeSocket &output_socket)
Map< std::string, const bNodeSocket * > inputs_to_linked_outputs_map_
Map< const bNodeSocket *, std::string > output_sockets_to_output_identifiers_map_
VectorSet< const bNodeSocket * > logged_outputs_
Map< std::string, int > inputs_to_reference_counts_map_
int get_internal_input_reference_count(const StringRef &identifier)
PixelOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context, const bool is_single_value)
Map< std::string, const bNodeSocket * > & get_inputs_to_linked_outputs_map()
void compute_results_reference_counts(const Schedule &schedule)
GPointer single_value() const
Definition result.cc:1234
void set_reference_count(int count)
Definition result.cc:1079
linear_allocator::ChunkedList< SocketValueLog, 16 > input_socket_values
linear_allocator::ChunkedList< NodeImagePreview, 16 > node_image_previews
linear_allocator::ChunkedList< SocketValueLog, 16 > output_socket_values
void log_value(const bNode &node, const bNodeSocket &socket, GPointer value)
Definition eval_log.cc:319
#define log
int compute_output_reference_count(const bNodeSocket &output, const Schedule &schedule, const VectorSet< const bNode * > *ignored_nodes=nullptr)
Definition scheduler.cc:780
StringRefNull to_string(const Interpolation &interpolation)
Definition domain.cc:112
ImBuf * compute_preview(Context &context, const Result &input)
static destruct_ptr< nodes::eval_log::ImageInfoLog > get_image_info_log(LinearAllocator<> *allocator, const Domain &domain, const ResultPrecision &precision)
InputDescriptor input_descriptor_from_input_socket(const bNodeSocket *socket)
Definition utilities.cc:195
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
std::unique_ptr< T, DestructValueAtAddress< T > > destruct_ptr
Set< const bNodeSocket * > unneeded_inputs