Blender V5.3
node_operation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_assert.hh"
6#include "BLI_string_ref.hh"
7#include "BLI_vector_set.hh"
8
9#include "DNA_node_types.h"
10
11#include "BKE_node.hh"
12#include "BKE_node_runtime.hh"
13
14#include "GPU_debug.hh"
15
17#include "NOD_eval_log.hh"
18
20#include "COM_bundle_item.hh"
21#include "COM_context.hh"
23#include "COM_node_operation.hh"
24#include "COM_operation.hh"
25#include "COM_result.hh"
26#include "COM_scheduler.hh"
27#include "COM_utilities.hh"
29
30namespace blender::compositor {
31
33{
34 for (const bNodeSocket *output : this->node().output_sockets()) {
36 continue;
37 }
38
40 }
41
42 for (const bNodeSocket *input : this->node().input_sockets()) {
43 if (!is_socket_available(input)) {
44 continue;
45 }
46
47 const InputDescriptor input_descriptor = input_descriptor_from_input_socket(input);
48 declare_input_descriptor(input->identifier, input_descriptor);
49 }
50}
51
53 private:
54 const bNode &node_;
55 const ComputeContext &compute_context_;
57
59
60 public:
62 const ComputeContext &compute_context,
64 : node_(node), compute_context_(compute_context), log_(log)
65 {
66 start_ = nodes::eval_log::Clock::now();
67 }
68
70 {
71 if (!log_) {
72 return;
73 }
74 const nodes::eval_log::TimePoint end = nodes::eval_log::Clock::now();
75 nodes::eval_log::NodeTreeLogger &tree_logger = log_->get_local_tree_logger(compute_context_);
76 tree_logger.node_execution_times.append(*tree_logger.allocator,
77 {node_.identifier, start_, end});
78 }
79};
80
82{
83 const ScopedNodeTimer node_timer{
84 this->node(), this->get_compute_context(), this->context().nodes_evaluation_log()};
85 if (this->context().use_gpu()) {
86 GPU_debug_group_begin(this->node().typeinfo->idname.c_str());
87 }
89 if (this->context().use_gpu()) {
91 }
92}
93
95{
96 for (const bNodeSocket *output : this->node().output_sockets()) {
98 continue;
99 }
100
101 const int reference_count = compute_output_reference_count(*output, schedule);
102 this->get_result(output->identifier).set_reference_count(reference_count);
103 }
104}
105
107{
108 compute_context_ = &compute_context;
109}
110
112{
113 return *compute_context_;
114}
115
117{
119 if (!log) {
120 return;
121 }
122 nodes::eval_log::NodeTreeLogger &tree_logger = log->get_local_tree_logger(
123 this->get_compute_context());
124 tree_logger.node_warnings.append(*tree_logger.allocator,
125 {this->node().identifier, {type, message}});
126}
127
129{
130 nodes::eval_log::NodesEvalLog *log = this->context().nodes_evaluation_log();
131 if (!log) {
132 return;
133 }
134 nodes::eval_log::NodeTreeLogger &tree_logger = log->get_local_tree_logger(*compute_context_);
135
136 if (nodes::gizmos::node_has_gizmo(this->node())) {
137 tree_logger.evaluated_gizmo_nodes.append(*tree_logger.allocator, {node_.identifier});
138 }
139
140 /* Log input values. */
141 for (const bNodeSocket *input_socket : this->node().input_sockets()) {
142 if (!is_socket_available(input_socket)) {
143 continue;
144 }
145
146 const InputDescriptor &input_descriptor = this->get_input_descriptor(input_socket->identifier);
147 if (!input_socket->is_logically_linked() && !input_descriptor.implicit_input.has_value()) {
148 continue;
149 }
150
151 const Result &input = this->get_input(input_socket->identifier);
152 log_result(this->context(), tree_logger, *input_socket, input);
153 }
154
155 /* Log output values. */
156 for (const bNodeSocket *output_socket : this->node().output_sockets()) {
157 if (!is_socket_available(output_socket)) {
158 continue;
159 }
160
161 const Result &result = this->get_result(output_socket->identifier);
162 log_result(this->context(), tree_logger, *output_socket, result);
163 }
164
165 /* Log node preview if they are needed and the node group is active. */
166 const bool node_needs_preview = is_node_preview_needed(this->node());
167 const bool needs_node_previews = flag_is_set(this->context().needed_side_effect_output_types(),
169 if (node_needs_preview && needs_node_previews) {
170 const Result *result = this->get_preview_result();
171 if (result && !result->is_single_value()) {
172 ImBuf *preview = compositor::compute_preview(this->context(), *result);
173 tree_logger.node_image_previews.append(*tree_logger.allocator, {node_.identifier, preview});
174 }
175 }
176}
177
179{
180 return node_;
181}
182
183Result *NodeOperation::get_preview_result()
184{
185 /* Find the first linked output. */
186 for (const bNodeSocket *output : this->node().output_sockets()) {
188 continue;
189 }
190
191 Result &output_result = this->get_result(output->identifier);
192 if (output_result.should_compute()) {
193 return &output_result;
194 }
195 }
196
197 /* No linked outputs, but no inputs either, so nothing to preview. */
198 if (this->node().input_sockets().is_empty()) {
199 return nullptr;
200 }
201
202 /* Find the first allocated input. */
203 for (const bNodeSocket *input : this->node().input_sockets()) {
204 if (!is_socket_available(input)) {
205 continue;
206 }
207
208 Result &input_result = this->get_input(input->identifier);
209 if (input_result.is_allocated()) {
210 return &input_result;
211 }
212 }
213
215 return nullptr;
216}
217
218} // namespace blender::compositor
#define BLI_assert_unreachable()
#define output
virtual nodes::eval_log::NodesEvalLog * nodes_evaluation_log() const
std::optional< ImplicitInputType > implicit_input
NodeOperation(Context &context, const bNode &node)
void add_warning(nodes::NodeWarningType type, std::string message)
void set_compute_context(const ComputeContext &compute_context)
const ComputeContext & get_compute_context() const
void compute_results_reference_counts(const Schedule &schedule)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Operation(Context &context)
Definition operation.cc:22
void populate_result(StringRef identifier, ResultType type)
Definition operation.cc:122
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
InputDescriptor & get_input_descriptor(StringRef identifier)
Definition operation.cc:132
void declare_input_descriptor(StringRef identifier, InputDescriptor descriptor)
Definition operation.cc:127
void set_reference_count(int count)
Definition result.cc:1079
ScopedNodeTimer(const bNode &node, const ComputeContext &compute_context, nodes::eval_log::NodesEvalLog *log)
linear_allocator::ChunkedList< NodeImagePreview, 16 > node_image_previews
linear_allocator::ChunkedList< WarningWithNode > node_warnings
linear_allocator::ChunkedList< NodeExecutionTime, 16 > node_execution_times
linear_allocator::ChunkedList< EvaluatedGizmoNode > evaluated_gizmo_nodes
#define log
int compute_output_reference_count(const bNodeSocket &output, const Schedule &schedule, const VectorSet< const bNode * > *ignored_nodes=nullptr)
Definition scheduler.cc:780
ImBuf * compute_preview(Context &context, const Result &input)
bool is_node_preview_needed(const bNode &node)
Definition utilities.cc:245
void log_result(Context &context, nodes::eval_log::NodeTreeLogger &logger, const bNodeSocket &socket, const Result &result)
ResultType get_node_socket_result_type(const bNodeSocket *socket)
Definition utilities.cc:103
InputDescriptor input_descriptor_from_input_socket(const bNodeSocket *socket)
Definition utilities.cc:195
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
Clock::time_point TimePoint
bool node_has_gizmo(const bNode &node)
void GPU_debug_group_end()
Definition gpu_debug.cc:34
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23