Blender V5.3
node_tree_structure_type_inferencing.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
8
9#include "BLI_array_utils.hh"
10#include "BLI_bit_span_ops.hh"
11#include "BLI_bit_vector.hh"
12#include "BLI_enum_flags.hh"
13#include "BLI_stack.hh"
14
15#include "BKE_node.hh"
17#include "BKE_node_runtime.hh"
19
21#include "DNA_node_types.h"
22
24#include "NOD_socket.hh"
25
27
28using nodes::StructureType;
29namespace rl = nodes::reference_lifetimes;
30
31static StructureType ensure_supported_structure_type(const eNodeSocketDatatype socket_type,
32 const StructureType structure_type)
33{
34 if (structure_type == StructureType::Field && !nodes::socket_type_supports_fields(socket_type)) {
35 return StructureType::Single;
36 }
37 if (structure_type == StructureType::Grid && !nodes::socket_type_supports_grids(socket_type)) {
38 return StructureType::Single;
39 }
40 return structure_type;
41}
42
43static StructureType ensure_supported_structure_type(const bNodeSocket &socket,
44 const StructureType structure_type)
45{
46 return ensure_supported_structure_type(eNodeSocketDatatype(socket.type), structure_type);
47}
48
49static StructureType ensure_supported_structure_type(const bNodeTreeInterfaceSocket &io_socket,
50 const StructureType structure_type)
51{
52 const bke::bNodeSocketType *socket_type = io_socket.socket_typeinfo();
53 if (!socket_type) {
54 return structure_type;
55 }
56 return ensure_supported_structure_type(eNodeSocketDatatype(socket_type->type), structure_type);
57}
58
60{
61 if (node.is_group()) {
62 if (const bNodeTree *group = id_cast<const bNodeTree *>(node.id)) {
63 if (!ID_MISSING(group) && group->runtime->structure_type_interface) {
64 return *group->runtime->structure_type_interface;
65 }
66 }
67 }
68
69 const Span<const bNodeSocket *> input_sockets = node.input_sockets();
70 const Span<const bNodeSocket *> output_sockets = node.output_sockets();
71
73 node_interface.inputs.reinitialize(input_sockets.size());
74 node_interface.outputs.reinitialize(output_sockets.size());
75
76 if (node.is_undefined() || !node.declaration() || node.declaration()->skip_updating_sockets) {
77 node_interface.inputs.fill(StructureType::Dynamic);
78 node_interface.outputs.fill(
80 return node_interface;
81 }
82 if (node.is_reroute()) {
83 node_interface.inputs.first() = StructureType::Dynamic;
84 node_interface.outputs.first() = {StructureType::Dynamic, {0}};
85 return node_interface;
86 }
87
88 for (const int i : input_sockets.index_range()) {
89 const nodes::SocketDeclaration &decl = *input_sockets[i]->runtime->declaration;
90 node_interface.inputs[i] = decl.structure_type;
91 }
92
93 for (const int output : output_sockets.index_range()) {
94 const nodes::SocketDeclaration &decl = *output_sockets[output]->runtime->declaration;
96 dependency.type = decl.structure_type;
97 if (dependency.type != StructureType::Dynamic) {
98 continue;
99 }
100 std::visit(
101 [&]<typename T>(const T &value) {
102 if constexpr (std::is_same_v<T, nodes::OutputStructureTypeDependency::None>) {
103 /* Nothing to do. */
104 }
105 else if constexpr (std::is_same_v<T, nodes::OutputStructureTypeDependency::All>) {
106 dependency.linked_inputs.reinitialize(input_sockets.size());
108 }
109 else if constexpr (std::is_same_v<T, nodes::OutputStructureTypeDependency::Partial>) {
110 dependency.linked_inputs = value.linked_inputs.as_span();
111 }
112 else {
114 }
115 },
116 decl.structure_type_output_dependency.variant);
117 }
118
119 return node_interface;
120}
121
123{
124 const Span<const bNode *> nodes = tree.all_nodes();
126 for (const int i : nodes.index_range()) {
127 interfaces[i] = calc_node_interface(*nodes[i]);
128 }
129 return interfaces;
130}
131
132enum class DataRequirement : int8_t { None, Field, Single, Grid, List, Invalid };
133
135{
136 if (a == b) {
137 return a;
138 }
139 if (a == DataRequirement::None) {
140 return b;
141 }
142 if (b == DataRequirement::None) {
143 return a;
144 }
147 {
148 /* Single beats field, because fields can accept single values too. */
150 }
152}
153
154static StructureType data_requirement_to_auto_structure_type(const DataRequirement requirement)
155{
156 switch (requirement) {
158 return StructureType::Dynamic;
160 return StructureType::Field;
162 return StructureType::Single;
164 return StructureType::Grid;
166 return StructureType::List;
168 return StructureType::Dynamic;
169 }
171 return StructureType::Dynamic;
172}
173
175 bits::MutableBoundedBitSpan is_auto_structure_type)
176{
177 /* Handle group inputs. */
178 for (const int i : tree.interface_inputs().index_range()) {
179 const bNodeTreeInterfaceSocket &io_socket = *tree.interface_inputs()[i];
181 continue;
182 }
183 for (const bNode *node : tree.group_input_nodes()) {
184 const bNodeSocket &socket = node->output_socket(i);
185 is_auto_structure_type[socket.index_in_tree()].set();
186 }
187 }
188
189 /* Handle group outputs. */
190 if (const bNode *group_output_node = tree.group_output_node()) {
191 is_auto_structure_type.slice(group_output_node->input_socket_indices_in_tree().drop_back(1))
192 .set_all();
193 }
194
195 /* Handle closure inputs and outputs. */
197 for (const bNode *closure_input_node : tree.nodes_by_type("NodeClosureInput"_ustr)) {
198 const auto *closure_output_node = closure_zone_type->get_corresponding_output(
199 tree, *closure_input_node);
200 if (!closure_output_node) {
201 continue;
202 }
203 const auto &storage = *static_cast<const NodeClosureOutput *>(closure_output_node->storage);
204 for (const int i : IndexRange(storage.input_items.items_num)) {
205 const NodeClosureInputItem &item = storage.input_items.items[i];
207 const bNodeSocket &socket = closure_input_node->output_socket(i);
208 is_auto_structure_type[socket.index_in_tree()].set();
209 }
210 }
211 for (const int i : IndexRange(storage.output_items.items_num)) {
212 const NodeClosureOutputItem &item = storage.output_items.items[i];
214 const bNodeSocket &socket = closure_output_node->input_socket(i);
215 is_auto_structure_type[socket.index_in_tree()].set();
216 }
217 }
218 }
219
220 /* Handle Evaluate Closure nodes. */
221 for (const bNode *evaluate_closure_node : tree.nodes_by_type("NodeEvaluateClosure"_ustr)) {
222 auto &storage = *static_cast<NodeEvaluateClosure *>(evaluate_closure_node->storage);
223 for (const int i : IndexRange(storage.input_items.items_num)) {
224 const NodeEvaluateClosureInputItem &item = storage.input_items.items[i];
226 const bNodeSocket &socket = evaluate_closure_node->input_socket(i + 1);
227 is_auto_structure_type[socket.index_in_tree()].set();
228 }
229 }
230 for (const int i : IndexRange(storage.output_items.items_num)) {
231 const NodeEvaluateClosureOutputItem &item = storage.output_items.items[i];
233 const bNodeSocket &socket = evaluate_closure_node->output_socket(i);
234 is_auto_structure_type[socket.index_in_tree()].set();
235 }
236 }
237 }
238
239 /* Handle Combine Bundle nodes. */
240 for (const bNode *node : tree.nodes_by_type("NodeCombineBundle"_ustr)) {
241 auto &storage = *static_cast<NodeCombineBundle *>(node->storage);
242 for (const int i : IndexRange(storage.items_num)) {
243 const NodeCombineBundleItem &item = storage.items[i];
245 const bNodeSocket &socket = node->input_socket(i);
246 is_auto_structure_type[socket.index_in_tree()].set();
247 }
248 }
249 }
250
251 /* Handle Separate Bundle nodes. */
252 for (const bNode *node : tree.nodes_by_type("NodeSeparateBundle"_ustr)) {
253 auto &storage = *static_cast<NodeSeparateBundle *>(node->storage);
254 for (const int i : IndexRange(storage.items_num)) {
255 const NodeSeparateBundleItem &item = storage.items[i];
257 const bNodeSocket &socket = node->output_socket(i);
258 is_auto_structure_type[socket.index_in_tree()].set();
259 }
260 }
261 }
262
263 /* Handle Store Bundle Item nodes. */
264 for (const bNode *node : tree.nodes_by_type("NodeStoreBundleItem"_ustr)) {
265 auto &storage = *static_cast<NodeStoreBundleItem *>(node->storage);
266 if (storage.structure_type == NodeSocketInterfaceStructureType::Auto) {
267 const bNodeSocket &socket = *node->input_by_identifier("Item"_ustr);
268 is_auto_structure_type[socket.index_in_tree()].set();
269 }
270 }
271
272 /* Handle Get Bundle Item nodes. */
273 for (const bNode *node : tree.nodes_by_type("NodeGetBundleItem"_ustr)) {
274 auto &storage = *static_cast<NodeGetBundleItem *>(node->storage);
275 if (storage.structure_type == NodeSocketInterfaceStructureType::Auto) {
276 const bNodeSocket &socket = *node->output_by_identifier("Item"_ustr);
277 is_auto_structure_type[socket.index_in_tree()].set();
278 }
279 }
280}
281
282static void init_input_requirements(const bNodeTree &tree,
283 const bits::BoundedBitSpan is_auto_structure_type,
284 MutableSpan<DataRequirement> input_requirements)
285{
286 for (const bNode *node : tree.all_nodes()) {
287 for (const bNodeSocket *socket : node->input_sockets()) {
288 DataRequirement &requirement = input_requirements[socket->index_in_all_inputs()];
289 if (is_auto_structure_type[socket->index_in_tree()]) {
290 requirement = DataRequirement::None;
291 continue;
292 }
293 const nodes::SocketDeclaration *declaration = socket->runtime->declaration;
294 if (!declaration) {
295 requirement = DataRequirement::None;
296 continue;
297 }
298 switch (declaration->structure_type) {
299 case StructureType::Dynamic: {
300 requirement = DataRequirement::None;
301 break;
302 }
303 case StructureType::Single: {
304 requirement = DataRequirement::Single;
305 break;
306 }
307 case StructureType::Grid: {
308 requirement = DataRequirement::Grid;
309 break;
310 }
311 case StructureType::Field: {
312 requirement = DataRequirement::Field;
313 break;
314 }
315 case StructureType::List: {
316 requirement = DataRequirement::List;
317 break;
318 }
319 }
320 }
321 }
322}
323
325 const bNodeSocket &output_socket, const Span<DataRequirement> input_requirements)
326{
328 if (!output_socket.is_available()) {
329 return requirement;
330 }
331 for (const bNodeSocket *socket : output_socket.directly_linked_sockets()) {
332 if (!socket->is_available()) {
333 continue;
334 }
335 requirement = merge(requirement, input_requirements[socket->index_in_all_inputs()]);
336 }
337 return requirement;
338}
339
341 const Span<DataRequirement> input_requirements,
342 nodes::StructureTypeInterface &derived_interface)
343{
344 /* Merge usages from all group input nodes. */
345 Array<DataRequirement> interface_requirements(tree.interface_inputs().size(),
347 for (const bNode *node : tree.group_input_nodes()) {
348 const Span<const bNodeSocket *> output_sockets = node->output_sockets();
349 for (const int i : output_sockets.index_range().drop_back(1)) {
350 const bNodeSocket &output = *output_sockets[i];
351 interface_requirements[i] = merge(
352 interface_requirements[i], calc_output_socket_requirement(output, input_requirements));
353 }
354 }
355
356 /* Build derived interface structure types from group input nodes. */
357 for (const int i : tree.interface_inputs().index_range()) {
358 const bNodeTreeInterfaceSocket &io_socket = *tree.interface_inputs()[i];
360 derived_interface.inputs[i] = ensure_supported_structure_type(
361 io_socket, StructureType(io_socket.structure_type));
362 continue;
363 }
364
365 const DataRequirement requirement = interface_requirements[i];
366 derived_interface.inputs[i] = ensure_supported_structure_type(
367 io_socket, data_requirement_to_auto_structure_type(requirement));
368 }
369}
370
372 const Span<DataRequirement> input_requirements,
373 const bits::BoundedBitSpan is_auto_structure_type,
374 MutableSpan<StructureType> structure_types)
375{
376 const Span<const bNodeSocket *> all_sockets = tree.all_sockets();
377 bits::foreach_1_index(is_auto_structure_type, [&](const int i) {
378 const bNodeSocket &socket = *all_sockets[i];
379 if (socket.is_input()) {
380 return;
381 }
382 const bNode &node = socket.owner_node();
383 if (node.is_group_input()) {
384 /* Group input nodes have special handling in #store_group_input_structure_types because
385 * corresponding sockets on all group input nodes should have the same structure type. */
386 return;
387 }
388
389 const DataRequirement requirement = calc_output_socket_requirement(socket, input_requirements);
390 structure_types[socket.index_in_tree()] = ensure_supported_structure_type(
391 socket, data_requirement_to_auto_structure_type(requirement));
392 });
393}
394
395enum class ZoneInOutChange {
396 None = 0,
397 In = (1 << 1),
398 Out = (1 << 2),
399};
401
403 const bNode &input_node,
404 const bNode &output_node,
405 MutableSpan<DataRequirement> input_requirements)
406{
408 for (const int i : output_node.output_sockets().index_range()) {
409 /* First input node output is Delta Time which does not appear in the output node outputs. */
410 const bNodeSocket &input_of_input_node = input_node.input_socket(i);
411 const bNodeSocket &output_of_output_node = output_node.output_socket(i);
412 const bNodeSocket &input_of_output_node = output_node.input_socket(i + 1);
413 const DataRequirement new_value = merge(
414 input_requirements[input_of_input_node.index_in_all_inputs()],
415 calc_output_socket_requirement(output_of_output_node, input_requirements));
416 if (input_requirements[input_of_input_node.index_in_all_inputs()] != new_value) {
417 input_requirements[input_of_input_node.index_in_all_inputs()] = new_value;
418 change |= ZoneInOutChange::In;
419 }
420 if (input_requirements[input_of_output_node.index_in_all_inputs()] != new_value) {
421 input_requirements[input_of_output_node.index_in_all_inputs()] = new_value;
422 change |= ZoneInOutChange::Out;
423 }
424 }
425 return change;
426}
427
429 const bNode &input_node,
430 const bNode &output_node,
431 MutableSpan<DataRequirement> input_requirements)
432{
434 for (const int i : output_node.output_sockets().index_range()) {
435 const bNodeSocket &input_of_input_node = input_node.input_socket(i + 1);
436 const bNodeSocket &output_of_output_node = output_node.output_socket(i);
437 const bNodeSocket &input_of_output_node = output_node.input_socket(i);
438 const DataRequirement new_value = merge(
439 input_requirements[input_of_input_node.index_in_all_inputs()],
440 calc_output_socket_requirement(output_of_output_node, input_requirements));
441 if (input_requirements[input_of_input_node.index_in_all_inputs()] != new_value) {
442 input_requirements[input_of_input_node.index_in_all_inputs()] = new_value;
443 change |= ZoneInOutChange::In;
444 }
445 if (input_requirements[input_of_output_node.index_in_all_inputs()] != new_value) {
446 input_requirements[input_of_output_node.index_in_all_inputs()] = new_value;
447 change |= ZoneInOutChange::Out;
448 }
449 }
450 return change;
451}
452
454 const bNode &node,
455 MutableSpan<DataRequirement> input_requirements)
456{
457 /* Sync field state between zone nodes and schedule another pass if necessary. */
458 switch (node.type_legacy) {
460 const auto &data = *static_cast<const NodeGeometrySimulationInput *>(node.storage);
461 if (const bNode *output_node = tree.node_by_id(data.output_node_id)) {
463 node, *output_node, input_requirements);
464 if (flag_is_set(change, ZoneInOutChange::Out)) {
465 return true;
466 }
467 }
468 return false;
469 }
471 for (const bNode *input_node : tree.nodes_by_type("GeometryNodeSimulationInput"_ustr)) {
472 const auto &data = *static_cast<const NodeGeometrySimulationInput *>(input_node->storage);
473 if (node.identifier == data.output_node_id) {
475 *input_node, node, input_requirements);
476 if (flag_is_set(change, ZoneInOutChange::In)) {
477 return true;
478 }
479 }
480 }
481 return false;
482 }
484 const auto &data = *static_cast<const NodeGeometryRepeatInput *>(node.storage);
485 if (const bNode *output_node = tree.node_by_id(data.output_node_id)) {
487 node, *output_node, input_requirements);
488 if (flag_is_set(change, ZoneInOutChange::Out)) {
489 return true;
490 }
491 }
492 return false;
493 }
495 for (const bNode *input_node : tree.nodes_by_type("GeometryNodeRepeatInput"_ustr)) {
496 const auto &data = *static_cast<const NodeGeometryRepeatInput *>(input_node->storage);
497 if (node.identifier == data.output_node_id) {
499 *input_node, node, input_requirements);
500 if (flag_is_set(change, ZoneInOutChange::In)) {
501 return true;
502 }
503 }
504 }
505 return false;
506 }
507 }
508
509 return false;
510}
511
512static void propagate_right_to_left(const bNodeTree &tree,
513 const Span<nodes::StructureTypeInterface> node_interfaces,
514 MutableSpan<DataRequirement> input_requirements)
515{
516 while (true) {
517 bool need_update = false;
518
519 for (const bNode *node : tree.toposort_right_to_left()) {
520 const Span<const bNodeSocket *> input_sockets = node->input_sockets();
521 const Span<const bNodeSocket *> output_sockets = node->output_sockets();
522 const nodes::StructureTypeInterface &node_interface = node_interfaces[node->index()];
523
524 for (const int output : node_interface.outputs.index_range()) {
525 const bNodeSocket &output_socket = *output_sockets[output];
526 DataRequirement output_requirement = DataRequirement::None;
527 for (const bNodeSocket *socket : output_socket.directly_linked_sockets()) {
528 if (!socket->is_available()) {
529 continue;
530 }
531 output_requirement = merge(output_requirement,
532 input_requirements[socket->index_in_all_inputs()]);
533 }
534
535 switch (output_requirement) {
538 break;
539 }
541 /* If the output is a single, all inputs must be singles. */
542 for (const int input : node_interface.outputs[output].linked_inputs) {
543 const bNodeSocket &input_socket = *input_sockets[input];
544 input_requirements[input_socket.index_in_all_inputs()] = DataRequirement::Single;
545 }
546 break;
547 }
551 /* When a data requirement could be provided by multiple node inputs (i.e. only a
552 * single node input involved in a math operation has to be a volume grid for the
553 * output to be a grid), it's better to not propagate the data requirement than
554 * incorrectly saying that all of the inputs have it. */
555 Vector<int, 8> inputs_with_links;
556 for (const int input : node_interface.outputs[output].linked_inputs) {
557 const bNodeSocket &input_socket = *input_sockets[input];
558 if (input_requirements[input_socket.index_in_all_inputs()] ==
560 {
561 /* Inputs which require a single value can't get a different requirement. */
562 continue;
563 }
564 if (input_socket.is_directly_linked()) {
565 inputs_with_links.append(input_socket.index_in_all_inputs());
566 }
567 }
568 if (inputs_with_links.size() == 1) {
569 input_requirements[inputs_with_links.first()] = output_requirement;
570 }
571 else {
572 for (const int input : inputs_with_links) {
573 input_requirements[input] = DataRequirement::None;
574 }
575 }
576 break;
577 }
578 }
579 }
580
581 /* Find reverse dependencies and resolve conflicts, which may require another pass. */
582 if (propagate_zone_data_requirements(tree, *node, input_requirements)) {
583 need_update = true;
584 }
585 }
586
587 if (!need_update) {
588 break;
589 }
590 }
591}
592
593static StructureType left_to_right_merge(const StructureType a, const StructureType b)
594{
595 if (a == b) {
596 return a;
597 }
598 if (a == StructureType::Dynamic || b == StructureType::Dynamic) {
599 return StructureType::Dynamic;
600 }
601 if ((a == StructureType::Field && b == StructureType::Grid) ||
602 (a == StructureType::Grid && b == StructureType::Field))
603 {
604 return StructureType::Grid;
605 }
606 if ((a == StructureType::Single && b == StructureType::Field) ||
607 (a == StructureType::Field && b == StructureType::Single))
608 {
609 return StructureType::Field;
610 }
611 if ((a == StructureType::Single && b == StructureType::Grid) ||
612 (a == StructureType::Grid && b == StructureType::Single))
613 {
614 return StructureType::Grid;
615 }
616 if ((a == StructureType::Single && b == StructureType::List) ||
617 (a == StructureType::List && b == StructureType::Single))
618 {
619 return StructureType::List;
620 }
621 if ((a == StructureType::Field && b == StructureType::List) ||
622 (a == StructureType::List && b == StructureType::Field))
623 {
624 return StructureType::List;
625 }
626 /* Invalid combination. */
627 return a;
628}
629
631 const bNode &output_node,
632 MutableSpan<StructureType> structure_types)
633{
635 for (const int i : output_node.output_sockets().index_range()) {
636 /* First input node output is Delta Time which does not appear in the output node outputs. */
637 const bNodeSocket &input = input_node.output_socket(i + 1);
638 const bNodeSocket &output = output_node.output_socket(i);
639 const StructureType new_value = left_to_right_merge(structure_types[input.index_in_tree()],
640 structure_types[output.index_in_tree()]);
641 const StructureType new_input_value = ensure_supported_structure_type(input, new_value);
642 const StructureType new_output_value = ensure_supported_structure_type(output, new_value);
643 if (structure_types[input.index_in_tree()] != new_input_value) {
644 structure_types[input.index_in_tree()] = new_input_value;
645 change |= ZoneInOutChange::In;
646 }
647 if (structure_types[output.index_in_tree()] != new_output_value) {
648 structure_types[output.index_in_tree()] = new_output_value;
649 change |= ZoneInOutChange::Out;
650 }
651 }
652 return change;
653}
654
656 const bNode &output_node,
657 MutableSpan<StructureType> structure_types)
658{
660 for (const int i : output_node.output_sockets().index_range()) {
661 const bNodeSocket &input_of_input_node = input_node.output_socket(i + 1);
662 const bNodeSocket &output_of_output_node = output_node.output_socket(i);
663 const StructureType new_value = left_to_right_merge(
664 structure_types[input_of_input_node.index_in_tree()],
665 structure_types[output_of_output_node.index_in_tree()]);
666 const StructureType new_input_value = ensure_supported_structure_type(input_of_input_node,
667 new_value);
668 const StructureType new_output_value = ensure_supported_structure_type(output_of_output_node,
669 new_value);
670 if (structure_types[input_of_input_node.index_in_tree()] != new_input_value) {
671 structure_types[input_of_input_node.index_in_tree()] = new_input_value;
672 change |= ZoneInOutChange::In;
673 }
674 if (structure_types[output_of_output_node.index_in_tree()] != new_output_value) {
675 structure_types[output_of_output_node.index_in_tree()] = new_output_value;
676 change |= ZoneInOutChange::Out;
677 }
678 }
679 return change;
680}
681
682static bool propagate_zone_status(const bNodeTree &tree,
683 const bNode &node,
684 MutableSpan<StructureType> structure_types)
685{
686 /* Sync field state between zone nodes and schedule another pass if necessary. */
687 switch (node.type_legacy) {
689 const auto &data = *static_cast<const NodeGeometrySimulationInput *>(node.storage);
690 if (const bNode *output_node = tree.node_by_id(data.output_node_id)) {
692 node, *output_node, structure_types);
693 if (flag_is_set(change, ZoneInOutChange::Out)) {
694 return true;
695 }
696 }
697 return false;
698 }
700 for (const bNode *input_node : tree.nodes_by_type("GeometryNodeSimulationInput"_ustr)) {
701 const auto &data = *static_cast<const NodeGeometrySimulationInput *>(input_node->storage);
702 if (node.identifier == data.output_node_id) {
704 *input_node, node, structure_types);
705 if (flag_is_set(change, ZoneInOutChange::In)) {
706 return true;
707 }
708 }
709 }
710 return false;
711 }
713 const auto &data = *static_cast<const NodeGeometryRepeatInput *>(node.storage);
714 if (const bNode *output_node = tree.node_by_id(data.output_node_id)) {
716 node, *output_node, structure_types);
717 if (flag_is_set(change, ZoneInOutChange::Out)) {
718 return true;
719 }
720 }
721 return false;
722 }
724 for (const bNode *input_node : tree.nodes_by_type("GeometryNodeRepeatInput"_ustr)) {
725 const auto &data = *static_cast<const NodeGeometryRepeatInput *>(input_node->storage);
726 if (node.identifier == data.output_node_id) {
728 *input_node, node, structure_types);
729 if (flag_is_set(change, ZoneInOutChange::In)) {
730 return true;
731 }
732 }
733 }
734 return false;
735 }
736 }
737
738 return false;
739}
740
742 const nodes::SocketDeclaration &declaration)
743{
745 return StructureType::Field;
746 }
747 return StructureType::Single;
748}
749
750static void propagate_left_to_right(const bNodeTree &tree,
751 const Span<nodes::StructureTypeInterface> node_interfaces,
752 const Span<StructureType> group_input_structure_types,
753 const bits::BoundedBitSpan is_auto_structure_type,
754 MutableSpan<StructureType> structure_types)
755{
756 for (const bNodeSocket *input : tree.all_input_sockets()) {
757 if (input->owner_node().is_undefined()) {
758 continue;
759 }
760 if (!input->is_directly_linked()) {
761 if (const nodes::SocketDeclaration *declaration = input->runtime->declaration) {
762 structure_types[input->index_in_tree()] = ensure_supported_structure_type(
763 *input, get_unconnected_input_structure_type(*declaration));
764 }
765 }
766 }
767
768 /* Outputs of these nodes have dynamic structure type but should start out as single values. */
769 for (const UString idname : {"GeometryNodeRepeatInput"_ustr,
770 "GeometryNodeRepeatOutput"_ustr,
771 "GeometryNodeSimulationInput"_ustr,
772 "GeometryNodeSimulationOutput"_ustr})
773 {
774 for (const bNode *node : tree.nodes_by_type(idname)) {
775 for (const bNodeSocket *socket : node->output_sockets()) {
776 structure_types[socket->index_in_tree()] = StructureType::Single;
777 }
778 }
779 }
780
781 while (true) {
782 bool need_update = false;
783 for (const bNode *node : tree.toposort_left_to_right()) {
784 if (node->is_undefined()) {
785 continue;
786 }
787 const Span<const bNodeSocket *> input_sockets = node->input_sockets();
788 const Span<const bNodeSocket *> output_sockets = node->output_sockets();
789 if (node->is_group_input()) {
790 for (const int i : output_sockets.index_range().drop_back(1)) {
791 structure_types[output_sockets[i]->index_in_tree()] = ensure_supported_structure_type(
792 *output_sockets[i], group_input_structure_types[i]);
793 }
794 continue;
795 }
796
797 for (const bNodeSocket *input : input_sockets) {
798 if (!input->is_available()) {
799 continue;
800 }
801
802 std::optional<StructureType> input_type;
803 for (const bNodeLink *link : input->directly_linked_links()) {
804 if (!link->is_used()) {
805 continue;
806 }
807 const StructureType new_type = structure_types[link->fromsock->index_in_tree()];
808 if (input_type) {
809 input_type = left_to_right_merge(*input_type, new_type);
810 }
811 else {
812 input_type = new_type;
813 }
814 }
815 if (input_type) {
816 structure_types[input->index_in_tree()] = ensure_supported_structure_type(*input,
817 *input_type);
818 }
819 }
820
821 const nodes::StructureTypeInterface &node_interface = node_interfaces[node->index()];
822
823 for (const int output_index : node_interface.outputs.index_range()) {
824 const bNodeSocket &output = *output_sockets[output_index];
825 if (!output.is_available() || !output.runtime->declaration) {
826 continue;
827 }
828 if (is_auto_structure_type[output.index_in_tree()]) {
829 /* Has been initialized in #store_auto_output_structure_types. */
830 continue;
831 }
832 const nodes::SocketDeclaration &declaration = *output.runtime->declaration;
833
834 std::optional<StructureType> output_type;
835 for (const int input_index : node_interface.outputs[output_index].linked_inputs) {
836 const bNodeSocket &input = node->input_socket(input_index);
837 if (!input.is_available()) {
838 continue;
839 }
840 const StructureType new_type = structure_types[input.index_in_tree()];
841 if (output_type) {
842 output_type = left_to_right_merge(*output_type, new_type);
843 }
844 else {
845 output_type = new_type;
846 }
847 }
848 structure_types[output.index_in_tree()] = ensure_supported_structure_type(
849 output, output_type.value_or(declaration.structure_type));
850 }
851
852 if (propagate_zone_status(tree, *node, structure_types)) {
853 need_update = true;
854 }
855 }
856
857 if (!need_update) {
858 break;
859 }
860 }
861}
862
864 const bNodeSocket &group_output, const Span<nodes::StructureTypeInterface> interface_by_node)
865{
866 /* Use a Set instead of an array indexed by socket because we may only look at a few sockets. */
867 Set<const bNodeSocket *> handled_sockets;
868 Stack<const bNodeSocket *> sockets_to_check;
869
870 handled_sockets.add(&group_output);
871 sockets_to_check.push(&group_output);
872
873 Vector<int> group_inputs;
874
875 while (!sockets_to_check.is_empty()) {
876 const bNodeSocket *input_socket = sockets_to_check.pop();
877 if (!input_socket->is_directly_linked()) {
878 continue;
879 }
880
881 for (const bNodeSocket *origin_socket : input_socket->directly_linked_sockets()) {
882 const bNode &origin_node = origin_socket->owner_node();
883 if (origin_node.is_group_input()) {
884 group_inputs.append_non_duplicates(origin_socket->index());
885 continue;
886 }
887
888 const nodes::StructureTypeInterface &node_interface = interface_by_node[origin_node.index()];
889 for (const int input_index : node_interface.outputs[origin_socket->index()].linked_inputs) {
890 const bNodeSocket &input = origin_node.input_socket(input_index);
891 if (!input.is_available()) {
892 continue;
893 }
894 if (handled_sockets.add(&input)) {
895 sockets_to_check.push(&input);
896 }
897 }
898 }
899 }
900
901 return group_inputs;
902}
903
905 const bNodeTree &tree,
906 const Span<nodes::StructureTypeInterface> interface_by_node,
907 const Span<StructureType> structure_types,
909{
910 const bNode *group_output_node = tree.group_output_node();
911 if (!group_output_node) {
913 output.type = StructureType::Dynamic;
914 }
915 return;
916 }
917
918 const Span<const bNodeTreeInterfaceSocket *> interface_outputs = tree.interface_outputs();
919 const Span<const bNodeSocket *> sockets = group_output_node->input_sockets().drop_back(1);
920 for (const int i : sockets.index_range()) {
921 if (interface_outputs[i]->structure_type != NodeSocketInterfaceStructureType::Auto) {
922 interface.outputs[i] = {
923 ensure_supported_structure_type(*interface_outputs[i],
924 StructureType(interface_outputs[i]->structure_type)),
925 {}};
926 continue;
927 }
928 /* Update derived interface output structure types from output node socket usages. */
929 interface.outputs[i].type = ensure_supported_structure_type(
930 *interface_outputs[i], structure_types[sockets[i]->index_in_tree()]);
931 if (interface.outputs[i].type == StructureType::Dynamic) {
932 const Vector<int> linked_inputs = find_dynamic_output_linked_inputs(*sockets[i],
933 interface_by_node);
934 interface.outputs[i] = {StructureType::Dynamic, linked_inputs.as_span()};
935 }
936 }
937}
938
943
945{
946 tree.ensure_topology_cache();
947 tree.ensure_interface_cache();
948
950 result.socket_structure_types = Array<StructureType>(tree.all_sockets().size(),
951 StructureType::Dynamic);
952
953 result.group_interface.inputs.reinitialize(tree.interface_inputs().size());
954 result.group_interface.outputs.reinitialize(tree.interface_outputs().size());
955 if (tree.has_available_link_cycle()) {
956 result.group_interface.inputs.fill(StructureType::Dynamic);
957 result.group_interface.outputs.fill({StructureType::Dynamic, {}});
958 return result;
959 }
960
962 bits::BitVector<> is_auto_structure_type(tree.all_sockets().size(), false);
963
964 Array<DataRequirement> data_requirements(tree.all_input_sockets().size());
965
966 find_auto_structure_type_sockets(tree, is_auto_structure_type);
967 init_input_requirements(tree, is_auto_structure_type, data_requirements);
968 propagate_right_to_left(tree, node_interfaces, data_requirements);
969 store_group_input_structure_types(tree, data_requirements, result.group_interface);
971 tree, data_requirements, is_auto_structure_type, result.socket_structure_types);
973 node_interfaces,
974 result.group_interface.inputs,
975 is_auto_structure_type,
976 result.socket_structure_types);
978 tree, node_interfaces, result.socket_structure_types, result.group_interface);
979
980 return result;
981}
982
984{
986 for (const int i : tree.all_sockets().index_range()) {
987 const bNodeSocket &socket = *tree.all_sockets()[i];
988 socket.runtime->inferred_structure_type = result.socket_structure_types[i];
989 }
991 *tree.runtime->structure_type_interface == result.group_interface)
992 {
993 return false;
994 }
995 tree.runtime->structure_type_interface = std::make_unique<nodes::StructureTypeInterface>(
996 std::move(result.group_interface));
997 return true;
998}
999
1000} // namespace blender::bke::node_structure_type_inferencing
#define GEO_NODE_SIMULATION_OUTPUT
#define NODE_CLOSURE_OUTPUT
#define GEO_NODE_REPEAT_OUTPUT
#define GEO_NODE_REPEAT_INPUT
#define GEO_NODE_SIMULATION_INPUT
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define BLI_assert_unreachable()
#define BLI_assert_unreachable_static_t(T)
#define output
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr IndexRange drop_back(int64_t n) const
constexpr IndexRange()=default
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
bool is_empty() const
Definition BLI_stack.hh:310
void push(const T &value)
Definition BLI_stack.hh:215
int64_t size() const
void append(const T &value)
void append_non_duplicates(const T &value)
Span< T > as_span() const
const T & first() const
MutableBitSpan slice(const IndexRange range) const
nodes::StructureType inferred_structure_type
std::unique_ptr< nodes::StructureTypeInterface > structure_type_interface
const bNode * get_corresponding_output(const bNodeTree &tree, const bNode &input_bnode) const
#define storage(slot, qualifiers)
#define T
void fill_index_range(MutableSpan< T > span, const T start=0)
void foreach_1_index(const BitSpanT &data, Fn &&fn)
static void store_group_input_structure_types(const bNodeTree &tree, const Span< DataRequirement > input_requirements, nodes::StructureTypeInterface &derived_interface)
static void store_auto_output_structure_types(const bNodeTree &tree, const Span< DataRequirement > input_requirements, const bits::BoundedBitSpan is_auto_structure_type, MutableSpan< StructureType > structure_types)
static void init_input_requirements(const bNodeTree &tree, const bits::BoundedBitSpan is_auto_structure_type, MutableSpan< DataRequirement > input_requirements)
static void propagate_right_to_left(const bNodeTree &tree, const Span< nodes::StructureTypeInterface > node_interfaces, MutableSpan< DataRequirement > input_requirements)
static DataRequirement merge(const DataRequirement a, const DataRequirement b)
static StructureType data_requirement_to_auto_structure_type(const DataRequirement requirement)
static StructureType left_to_right_merge(const StructureType a, const StructureType b)
static ZoneInOutChange simulation_zone_requirements_propagate(const bNode &input_node, const bNode &output_node, MutableSpan< DataRequirement > input_requirements)
static bool propagate_zone_status(const bNodeTree &tree, const bNode &node, MutableSpan< StructureType > structure_types)
static ZoneInOutChange simulation_zone_status_propagate(const bNode &input_node, const bNode &output_node, MutableSpan< StructureType > structure_types)
static StructureTypeInferenceResult calc_structure_type_interface(const bNodeTree &tree)
static ZoneInOutChange repeat_zone_requirements_propagate(const bNode &input_node, const bNode &output_node, MutableSpan< DataRequirement > input_requirements)
static StructureType ensure_supported_structure_type(const eNodeSocketDatatype socket_type, const StructureType structure_type)
static DataRequirement calc_output_socket_requirement(const bNodeSocket &output_socket, const Span< DataRequirement > input_requirements)
static Vector< int > find_dynamic_output_linked_inputs(const bNodeSocket &group_output, const Span< nodes::StructureTypeInterface > interface_by_node)
static nodes::StructureTypeInterface calc_node_interface(const bNode &node)
static void propagate_left_to_right(const bNodeTree &tree, const Span< nodes::StructureTypeInterface > node_interfaces, const Span< StructureType > group_input_structure_types, const bits::BoundedBitSpan is_auto_structure_type, MutableSpan< StructureType > structure_types)
static bool propagate_zone_data_requirements(const bNodeTree &tree, const bNode &node, MutableSpan< DataRequirement > input_requirements)
static ZoneInOutChange repeat_zone_status_propagate(const bNode &input_node, const bNode &output_node, MutableSpan< StructureType > structure_types)
static StructureType get_unconnected_input_structure_type(const nodes::SocketDeclaration &declaration)
static void store_group_output_structure_types(const bNodeTree &tree, const Span< nodes::StructureTypeInterface > interface_by_node, const Span< StructureType > structure_types, nodes::StructureTypeInterface &interface)
static Array< nodes::StructureTypeInterface > calc_node_interfaces(const bNodeTree &tree)
static void find_auto_structure_type_sockets(const bNodeTree &tree, bits::MutableBoundedBitSpan is_auto_structure_type)
const bNodeZoneType * zone_type_by_node_type(const int node_type)
bool socket_type_supports_fields(const eNodeSocketDatatype socket_type)
bool default_input_type_is_field(const NodeDefaultInputType input_type)
bool socket_type_supports_grids(const eNodeSocketDatatype socket_type)
BMesh const char void * data
signed char int8_t
Definition BKE_text.h:76
@ Invalid
Definition types.hh:42
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
eNodeSocketDatatype type
bke::bNodeSocketRuntime * runtime
NodeSocketInterfaceStructureType structure_type
bke::bNodeTreeRuntime * runtime
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198