Blender V5.3
multi_function_procedure_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 <memory>
6#include <string>
7
8#include "BLI_assert.hh"
10#include "BLI_cpp_type.hh"
11#include "BLI_generic_span.hh"
12#include "BLI_index_mask.hh"
13#include "BLI_map.hh"
14#include "BLI_math_base.hh"
15#include "BLI_math_euler.hh"
17#include "BLI_string_ref.hh"
18#include "BLI_vector.hh"
19#include "BLI_vector_set.hh"
20
21#include "FN_multi_function.hh"
29
30#include "DNA_node_types.h"
31
32#include "BKE_node.hh"
33#include "BKE_node_runtime.hh"
35
36#include "NOD_multi_function.hh"
37
38#include "COM_context.hh"
39#include "COM_domain.hh"
44#include "COM_result.hh"
45#include "COM_scheduler.hh"
46#include "COM_utilities.hh"
47
48namespace blender::compositor {
49
52 NodeTreeEvaluator &node_tree_evaluator,
53 const bool is_single_value,
54 const ComputeContext &compute_context)
55 : PixelOperation(context, node_tree_evaluator, compute_context, is_single_value),
56 procedure_builder_(procedure_)
57{
58 this->build_procedure();
59 procedure_executor_ = std::make_unique<mf::ProcedureExecutor>(procedure_);
60}
61
63{
64 const Domain domain = is_single_value_ ? Domain(int2(1)) : this->compute_domain();
65 const int64_t size = int64_t(domain.data_size.x) * domain.data_size.y;
66 const IndexMask mask = IndexMask(size);
67 mf::ParamsBuilder parameter_builder{*procedure_executor_, &mask};
68
69 /* For each of the parameters, either add an input or an output depending on its interface type,
70 * allocating the outputs when needed. */
71 for (int i = 0; i < procedure_.params().size(); i++) {
72 if (procedure_.params()[i].type == mf::ParamType::InterfaceType::Input) {
73 const Result &input = get_input(parameter_identifiers_[i]);
74 if (input.is_single_value()) {
75 parameter_builder.add_readonly_single_input(input.single_value());
76 }
77 else {
78 if (is_single_value_) {
79 /* The operation is operating on single values but an image is provided, so add a default
80 * single value as a fallback. */
81 parameter_builder.add_readonly_single_input(
83 }
84 else {
85 parameter_builder.add_readonly_single_input(input.cpu_data());
86 }
87 }
88 }
89 else {
90 Result &output = get_result(parameter_identifiers_[i]);
91 if (is_single_value_) {
92 output.allocate_single_value();
93 parameter_builder.add_uninitialized_single_output(
94 GMutableSpan(output.get_cpp_type(), output.single_value().get(), 1));
95 }
96 else {
97 output.allocate_texture(domain);
98 parameter_builder.add_uninitialized_single_output(output.cpu_data_for_write());
99 }
100 }
101 }
102
103 mf::ContextBuilder context_builder;
104 procedure_executor_->call_auto(mask, parameter_builder, context_builder);
105}
106
107void MultiFunctionProcedureOperation::build_procedure()
108{
110 /* Get the multi-function of the node. */
111 auto &multi_function_builder = *node_multi_functions_.lookup_or_add_cb(node, [&]() {
112 return std::make_unique<nodes::NodeMultiFunctionBuilder>(*node, node->owner_tree());
113 });
114 node->typeinfo->build_multi_function(multi_function_builder);
115 const mf::MultiFunction &multi_function = multi_function_builder.function();
116
117 /* Get the variables of the inputs of the node, creating inputs to the operation/procedure if
118 * needed. */
119 Vector<mf::Variable *> input_variables = this->get_input_variables(*node, multi_function);
120
121 /* Call the node multi-function, getting the variables for its outputs. */
122 Vector<mf::Variable *> output_variables = procedure_builder_.add_call(multi_function,
123 input_variables);
124
125 /* Assign the output variables to the node's respective outputs, creating outputs for the
126 * operation/procedure if needed. */
127 this->assign_output_variables(*node, output_variables);
128 }
129
130 /* Add destructor calls for the variables. */
131 for (const auto &item : output_to_variable_map_.items()) {
132 /* Variables that are used by the outputs should not be destructed. */
133 if (!output_variables_.contains(item.value)) {
134 procedure_builder_.add_destruct(*item.value);
135 }
136 }
137 for (mf::Variable *variable : implicit_variables_) {
138 /* Variables that are used by the outputs should not be destructed. */
139 if (!output_variables_.contains(variable)) {
140 procedure_builder_.add_destruct(*variable);
141 }
142 }
143 for (mf::Variable *variable : implicit_input_to_variable_map_.values()) {
144 procedure_builder_.add_destruct(*variable);
145 }
146
147 mf::ReturnInstruction &return_instruction = procedure_builder_.add_return();
148 procedure_.prepare_for_execution();
149 mf::procedure_optimization::move_destructs_up(procedure_, return_instruction);
150 BLI_assert(procedure_.validate());
151}
152
153Vector<mf::Variable *> MultiFunctionProcedureOperation::get_input_variables(
154 const bNode &node, const mf::MultiFunction &multi_function)
155{
156 int available_inputs_index = 0;
157 Vector<mf::Variable *> input_variables;
158 for (const bNodeSocket *input : node.input_sockets()) {
159 if (!is_socket_available(input)) {
160 continue;
161 }
162
163 const mf::ParamType parameter_type = multi_function.param_type(available_inputs_index);
164 available_inputs_index++;
165
166 if (node_tree_evaluator_.schedule().unneeded_inputs.contains(input)) {
167 input_variables.append(this->get_default_value_variable(parameter_type.data_type()));
168 continue;
169 }
170
171 const bNodeSocket *output = get_output_linked_to_input(*input);
172 if (!output) {
173 const InputDescriptor input_descriptor = input_descriptor_from_input_socket(input);
174 if (!input_descriptor.implicit_input.has_value()) {
175 /* No implicit input, so get a constant variable that holds the socket value. */
176 input_variables.append(this->get_constant_input_variable(*input));
177 }
178 else {
179 input_variables.append(this->get_implicit_input_variable(*input));
180 }
181 }
182 else {
183 /* If the source node is part of the multi-function procedure operation, then the output has
184 * an existing variable for it. */
186 input_variables.append(output_to_variable_map_.lookup(output));
187 }
188 else {
189 /* Otherwise, the source node is not part of the multi-function procedure operation, and a
190 * variable that represents an input to the multi-function procedure operation is used. */
191 input_variables.append(this->get_multi_function_input_variable(*input, *output));
192 }
193 }
194
195 /* Implicitly convert the variable type to the expected parameter type if needed. */
196 input_variables.last() = this->convert_variable(input_variables.last(),
197 parameter_type.data_type());
198 }
199
200 return input_variables;
201}
202
203mf::Variable *MultiFunctionProcedureOperation::get_constant_input_variable(
204 const bNodeSocket &input)
205{
206 const mf::MultiFunction *constant_function = nullptr;
207 switch (input.type) {
208 case SOCK_FLOAT: {
209 const float value = input.default_value_typed<bNodeSocketValueFloat>()->value;
210 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<float>>(value);
211 break;
212 }
213 case SOCK_INT: {
214 const int value = input.default_value_typed<bNodeSocketValueInt>()->value;
215 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<int32_t>>(value);
216 break;
217 }
218 case SOCK_BOOLEAN: {
219 const bool value = input.default_value_typed<bNodeSocketValueBoolean>()->value;
220 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<bool>>(value);
221 break;
222 }
223 case SOCK_VECTOR: {
224 switch (input.default_value_typed<bNodeSocketValueVector>()->dimensions) {
225 case 2: {
226 const float2 value = float2(input.default_value_typed<bNodeSocketValueVector>()->value);
227 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<float2>>(value);
228 break;
229 }
230 case 3: {
231 const float3 value = float3(input.default_value_typed<bNodeSocketValueVector>()->value);
232 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<float3>>(value);
233 break;
234 }
235 case 4: {
236 const float4 value = float4(input.default_value_typed<bNodeSocketValueVector>()->value);
237 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<float4>>(value);
238 break;
239 }
240 default:
242 break;
243 }
244 break;
245 }
246 case SOCK_INT_VECTOR: {
247 switch (input.default_value_typed<bNodeSocketValueIntVector>()->dimensions) {
248 case 2: {
249 const int2 value = int2(input.default_value_typed<bNodeSocketValueIntVector>()->value);
250 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<int2>>(value);
251 break;
252 }
253 case 3: {
254 const int3 value = int3(input.default_value_typed<bNodeSocketValueIntVector>()->value);
255 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<int3>>(value);
256 break;
257 }
258 default:
260 break;
261 }
262 break;
263 }
264 case SOCK_RGBA: {
265 const Color value = Color(input.default_value_typed<bNodeSocketValueRGBA>()->value);
266 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<float4>>(value);
267 break;
268 }
269 case SOCK_MATRIX: {
270 const float4x4 value = float4x4::identity();
271 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<float4x4>>(value);
272 break;
273 }
274 case SOCK_MENU: {
275 const int32_t value = input.default_value_typed<bNodeSocketValueMenu>()->value;
276 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<nodes::MenuValue>>(
277 value);
278 break;
279 }
280 case SOCK_STRING: {
281 const std::string value = input.default_value_typed<bNodeSocketValueString>()->value;
282 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<std::string>>(
283 value);
284 break;
285 }
286 case SOCK_ROTATION: {
287 const bNodeSocketValueRotation *rotation =
288 input.default_value_typed<bNodeSocketValueRotation>();
289 const math::EulerXYZ euler(float3(rotation->value_euler));
290 const math::Quaternion value = math::to_quaternion(euler);
291 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<math::Quaternion>>(
292 value);
293 break;
294 }
295 case SOCK_OBJECT: {
296 Object *value = input.default_value_typed<bNodeSocketValueObject>()->value;
297 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<Object *>>(value);
298 break;
299 }
300 case SOCK_IMAGE: {
301 Image *value = input.default_value_typed<bNodeSocketValueImage>()->value;
302 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<Image *>>(value);
303 break;
304 }
305 case SOCK_FONT: {
306 VFont *value = input.default_value_typed<bNodeSocketValueFont>()->value;
307 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<VFont *>>(value);
308 break;
309 }
310 case SOCK_SCENE: {
311 Scene *value = input.default_value_typed<bNodeSocketValueScene>()->value;
312 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<Scene *>>(value);
313 break;
314 }
315 case SOCK_TEXT_ID: {
316 Text *value = input.default_value_typed<bNodeSocketValueText>()->value;
317 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<Text *>>(value);
318 break;
319 }
320 case SOCK_MASK: {
321 Mask *value = input.default_value_typed<bNodeSocketValueMask>()->value;
322 constant_function = &procedure_.construct_function<mf::CustomMF_Constant<Mask *>>(value);
323 break;
324 }
325 case SOCK_BUNDLE:
326 case SOCK_CLOSURE:
327 /* Not supported in multi-function nodes. */
329 break;
330 default:
332 break;
333 }
334
335 mf::Variable *constant_variable = procedure_builder_.add_call<1>(*constant_function)[0];
336 implicit_variables_.append(constant_variable);
337 return constant_variable;
338}
339
340mf::Variable *MultiFunctionProcedureOperation::get_implicit_input_variable(
341 const bNodeSocket &input)
342{
343 const InputDescriptor input_descriptor = input_descriptor_from_input_socket(&input);
344 const ImplicitInputType implicit_input = input_descriptor.implicit_input.value();
345
346 /* An input was already declared for that implicit input, so no need to declare it again and we
347 * just return its variable. */
348 if (implicit_input_to_variable_map_.contains(implicit_input)) {
349 /* But first we update the domain priority of the input descriptor to be the higher priority of
350 * the existing descriptor and the descriptor of the new input socket. That's because the same
351 * implicit input might be used in inputs inside the multi-function procedure operation which
352 * have different priorities. */
353 InputDescriptor &existing_input_descriptor = this->get_input_descriptor(
355 existing_input_descriptor.domain_priority = math::min(
356 existing_input_descriptor.domain_priority, input_descriptor.domain_priority);
357
358 return implicit_input_to_variable_map_.lookup(implicit_input);
359 }
360
361 const int implicit_input_index = implicit_inputs_to_input_identifiers_map_.size();
362 const std::string input_identifier = "implicit_input" + std::to_string(implicit_input_index);
363 declare_input_descriptor(input_identifier, input_descriptor);
364
365 /* Map the implicit input to the identifier of the operation input that was declared for it. */
366 implicit_inputs_to_input_identifiers_map_.add_new(implicit_input, input_identifier);
367
368 mf::Variable &variable = procedure_builder_.add_input_parameter(
369 mf::DataType::ForSingle(Result::cpp_type(input_descriptor.type)), input_identifier);
370 parameter_identifiers_.append(input_identifier);
371
372 /* Map the implicit input to the variable that was created for it. */
373 implicit_input_to_variable_map_.add(implicit_input, &variable);
374
375 return &variable;
376}
377
378mf::Variable *MultiFunctionProcedureOperation::get_multi_function_input_variable(
379 const bNodeSocket &input_socket, const bNodeSocket &output_socket)
380{
381 /* The output is a single value, so create a constant variable instead of declaring an input for
382 * it, it follows that the result needs to be released since it will no longer be referenced by
383 * the operation.*/
385 if (result.is_single_value()) {
386 const mf::MultiFunction &constant_function =
387 procedure_.construct_function<mf::CustomMF_GenericConstant>(
388 *result.single_value().type(), result.single_value().get(), true);
389 mf::Variable *constant_variable = procedure_builder_.add_call<1>(constant_function)[0];
390 implicit_variables_.append(constant_variable);
391 result.release();
392 return constant_variable;
393 }
394
395 /* An input was already declared for that same output socket, so no need to declare it again and
396 * we just return its variable. */
397 if (output_to_variable_map_.contains(&output_socket)) {
398 /* But first we update the domain priority of the input descriptor to be the higher priority of
399 * the existing descriptor and the descriptor of the new input socket. That's because the same
400 * output might be connected to multiple inputs inside the multi-function procedure operation
401 * which have different priorities. */
402 const std::string input_identifier = outputs_to_declared_inputs_map_.lookup(&output_socket);
403 InputDescriptor &input_descriptor = this->get_input_descriptor(input_identifier);
404 input_descriptor.domain_priority = math::min(
405 input_descriptor.domain_priority,
406 input_descriptor_from_input_socket(&input_socket).domain_priority);
407
408 /* Increment the input's reference count. */
409 inputs_to_reference_counts_map_.lookup(input_identifier)++;
410
411 return output_to_variable_map_.lookup(&output_socket);
412 }
413
414 const int input_index = inputs_to_linked_outputs_map_.size();
415 const std::string input_identifier = "input" + std::to_string(input_index);
416
417 /* Declare the input descriptor for this input and prefer to declare its type to be the same as
418 * the type of the output because doing type conversion in the multi-function procedure is
419 * cheaper. */
420 InputDescriptor input_descriptor = input_descriptor_from_input_socket(&input_socket);
421 input_descriptor.type = result.type();
422 declare_input_descriptor(input_identifier, input_descriptor);
423
424 mf::Variable &variable = procedure_builder_.add_input_parameter(
425 mf::DataType::ForSingle(Result::cpp_type(input_descriptor.type)), input_identifier);
426 parameter_identifiers_.append(input_identifier);
427
428 /* Map the output socket to the variable that was created for it. */
429 output_to_variable_map_.add(&output_socket, &variable);
430
431 /* Map the identifier of the operation input to the output socket it is linked to. */
432 inputs_to_linked_outputs_map_.add_new(input_identifier, &output_socket);
433
434 /* Map the output socket to the identifier of the operation input that was declared for it. */
435 outputs_to_declared_inputs_map_.add_new(&output_socket, input_identifier);
436
437 /* Map the identifier of the operation input to a reference count of 1, this will later be
438 * incremented if that same output was referenced again. */
439 inputs_to_reference_counts_map_.add_new(input_identifier, 1);
440
441 return &variable;
442}
443
444void MultiFunctionProcedureOperation::assign_output_variables(const bNode &node,
445 Vector<mf::Variable *> &variables)
446{
447 const bool should_log_outputs = this->context().nodes_evaluation_log() && is_single_value_;
448
449 /* Only compute previews if they are needed and the node group is active. */
450 const bool node_needs_preview = is_node_preview_needed(node);
451 const bool needs_node_previews = flag_is_set(this->context().needed_side_effect_output_types(),
453 const bNodeSocket *preview_output = nullptr;
454 if (node_needs_preview && needs_node_previews && !is_single_value_) {
455 preview_output = find_preview_output_socket(node);
456 }
457
458 int available_outputs_index = 0;
459 for (const bNodeSocket *output : node.output_sockets()) {
461 continue;
462 }
463
464 mf::Variable *output_variable = variables[available_outputs_index];
465 output_to_variable_map_.add_new(output, output_variable);
466
467 /* If the output is referenced by the schedule outside of the pixel compile unit, then an
468 * output result needs to be populated for it. */
469 const bool is_operation_output = compute_output_reference_count(
470 *output,
473
474 /* If the output is used as the node preview, then an output result needs to be populated for
475 * it, and we additionally keep track of that output to later compute the previews from. */
476 const bool is_preview_output = output == preview_output;
477 if (is_preview_output) {
479 }
480
481 if (should_log_outputs) {
483 }
484
485 if (is_operation_output || is_preview_output || should_log_outputs) {
486 this->populate_operation_result(*output, output_variable);
487 }
488
489 available_outputs_index++;
490 }
491}
492
493void MultiFunctionProcedureOperation::populate_operation_result(const bNodeSocket &output_socket,
494 mf::Variable *variable)
495{
496 const uint output_id = output_sockets_to_output_identifiers_map_.size();
497 const std::string output_identifier = "output" + std::to_string(output_id);
498
499 const ResultType result_type = get_node_socket_result_type(&output_socket);
500 populate_result(output_identifier, result_type);
501
502 /* Map the output socket to the identifier of the newly populated result. */
503 output_sockets_to_output_identifiers_map_.add_new(&output_socket, output_identifier);
504
505 /* Implicitly convert the variable type to the expected result type if needed. */
506 const mf::DataType expected_type = mf::DataType::ForSingle(Result::cpp_type(result_type));
507 mf::Variable *converted_variable = this->convert_variable(variable, expected_type);
508
509 procedure_builder_.add_output_parameter(*converted_variable);
510 output_variables_.add_new(converted_variable);
511 parameter_identifiers_.append(output_identifier);
512}
513
514mf::Variable *MultiFunctionProcedureOperation::convert_variable(mf::Variable *variable,
515 const mf::DataType expected_type)
516{
517 /* Conversion not needed. */
518 const mf::DataType variable_type = variable->data_type();
519 if (variable_type == expected_type) {
520 return variable;
521 }
522
523 const bke::DataTypeConversions &conversion_table = bke::get_implicit_type_conversions();
524 const mf::MultiFunction *function = conversion_table.get_conversion_multi_function(
525 variable_type, expected_type);
526
527 /* Conversion is not possible, return a default variable instead. */
528 if (!function) {
529 return this->get_default_value_variable(expected_type);
530 }
531
532 mf::Variable *converted_variable = procedure_builder_.add_call<1>(*function, {variable})[0];
533 implicit_variables_.append(converted_variable);
534 return converted_variable;
535}
536
537mf::Variable *MultiFunctionProcedureOperation::get_default_value_variable(const mf::DataType type)
538{
539 const mf::MultiFunction &constant_function =
540 procedure_.construct_function<mf::CustomMF_GenericConstant>(
541 type.single_type(), type.single_type().default_value(), false);
542 mf::Variable *constant_variable = procedure_builder_.add_call<1>(constant_function)[0];
543 implicit_variables_.append(constant_variable);
544 return constant_variable;
545}
546
547} // namespace blender::compositor
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define output
const void * default_value() const
GPointer()=default
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
int64_t size() const
Definition BLI_map.hh:976
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool contains(const Key &key) const
void append(const T &value)
virtual nodes::eval_log::NodesEvalLog * nodes_evaluation_log() const
Domain(const int2 &size)
Definition domain.cc:20
MultiFunctionProcedureOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const bool is_single_value, const ComputeContext &compute_context)
Result & get_result_from_output_socket(const bNodeSocket &output)
Result & get_result(StringRef identifier)
Definition operation.cc:40
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
virtual Domain compute_domain()
Definition operation.cc:62
InputDescriptor & get_input_descriptor(StringRef identifier)
Definition operation.cc:132
void declare_input_descriptor(StringRef identifier, InputDescriptor descriptor)
Definition operation.cc:127
Map< ImplicitInputType, std::string > implicit_inputs_to_input_identifiers_map_
VectorSet< const bNodeSocket * > preview_outputs_
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_
PixelOperation(Context &context, NodeTreeEvaluator &node_tree_evaluator, const ComputeContext &compute_context, const bool is_single_value)
Map< const bNodeSocket *, std::string > outputs_to_declared_inputs_map_
const CPPType & get_cpp_type() const
Definition result.cc:482
GPointer single_value() const
Definition result.cc:1234
static const CPPType & cpp_type(const ResultType type)
Definition result.cc:371
const DataTypeConversions & get_implicit_type_conversions()
int compute_output_reference_count(const bNodeSocket &output, const Schedule &schedule, const VectorSet< const bNode * > *ignored_nodes=nullptr)
Definition scheduler.cc:780
const bNodeSocket * find_preview_output_socket(const bNode &node)
Definition utilities.cc:258
bool is_node_preview_needed(const bNode &node)
Definition utilities.cc:245
const bNodeSocket * get_output_linked_to_input(const bNodeSocket &input)
Definition utilities.cc:31
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
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
QuaternionBase< float > Quaternion
QuaternionBase< T > to_quaternion(const AxisAngleBase< T, AngleT > &axis_angle)
EulerXYZBase< float > EulerXYZ
T min(const T &a, const T &b)
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint
Definition BKE_text.h:55
signed int int32_t
Definition BKE_text.h:78
Set< const bNodeSocket * > unneeded_inputs