Blender V5.3
shader_nodes_inline.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 <fmt/format.h>
10#include <variant>
11
13#include "BKE_lib_id.hh"
16
17#include "BLI_listbase.hh"
18#include "BLI_math_vector_c.hh"
19#include "BLI_stack.hh"
20#include "BLI_string.hh"
21#include "BLI_string_utf8.hh"
22
23#include "NOD_menu_value.hh"
24#include "NOD_multi_function.hh"
29
30namespace blender::nodes {
31namespace {
32
33struct SocketValue;
34struct BundleSocketValue;
35using BundleSocketValuePtr = std::shared_ptr<BundleSocketValue>;
36
37struct FallbackValue {};
38
40struct DanglingValue {};
41
42struct NodeAndSocket {
43 bNode *node = nullptr;
44 bNodeSocket *socket = nullptr;
45};
46
47struct PrimitiveSocketValue {
48 std::variant<int, float, bool, ColorGeometry4f, float3, MenuValue, std::string> value;
49
50 const void *buffer() const
51 {
52 return std::visit([](auto &&value) -> const void * { return &value; }, value);
53 }
54
55 void *buffer()
56 {
57 return const_cast<void *>(const_cast<const PrimitiveSocketValue *>(this)->buffer());
58 }
59
60 static PrimitiveSocketValue from_value(const GPointer value)
61 {
62 const CPPType &type = *value.type();
63 if (type.is<int>()) {
64 return {*static_cast<const int *>(value.get())};
65 }
66 if (type.is<float>()) {
67 return {*static_cast<const float *>(value.get())};
68 }
69 if (type.is<bool>()) {
70 return {*static_cast<const bool *>(value.get())};
71 }
72 if (type.is<ColorGeometry4f>()) {
73 return {*static_cast<const ColorGeometry4f *>(value.get())};
74 }
75 if (type.is<float3>()) {
76 return {*static_cast<const float3 *>(value.get())};
77 }
78 if (type.is<MenuValue>()) {
79 return {*static_cast<const MenuValue *>(value.get())};
80 }
81 if (type.is<std::string>()) {
82 return {*static_cast<const std::string *>(value.get())};
83 }
85 return {};
86 }
87};
88
90struct LinkedSocketValue {
91 bNode *node = nullptr;
92 bNodeSocket *socket = nullptr;
93};
94
96struct InputSocketValue {
97 const bNodeSocket *socket = nullptr;
98};
99
100struct ClosureZoneValue {
101 const bke::bNodeTreeZone *zone = nullptr;
102 const ComputeContext *closure_creation_context = nullptr;
103};
104
105struct MultiInputValue {
106 Vector<SocketValue, 0> values;
107};
108
109struct SocketValue {
115 std::variant<FallbackValue,
116 DanglingValue,
117 LinkedSocketValue,
118 InputSocketValue,
119 PrimitiveSocketValue,
120 ClosureZoneValue,
121 BundleSocketValuePtr,
122 MultiInputValue>
123 value;
124
126 std::optional<PrimitiveSocketValue> to_primitive(const bke::bNodeSocketType &type) const
127 {
128 if (const auto *primitive_value = std::get_if<PrimitiveSocketValue>(&this->value)) {
129 return *primitive_value;
130 }
131 if (const auto *input_socket_value = std::get_if<InputSocketValue>(&this->value)) {
132 const bNodeSocket &socket = *input_socket_value->socket;
133 BLI_assert(socket.type == type.type);
134 if (!socket.runtime->declaration) {
135 return std::nullopt;
136 }
137 if (socket.runtime->declaration->default_input_type != NODE_DEFAULT_INPUT_VALUE) {
138 return std::nullopt;
139 }
140 switch (socket.typeinfo->type) {
141 case SOCK_FLOAT:
142 return PrimitiveSocketValue{socket.default_value_typed<bNodeSocketValueFloat>()->value};
143 case SOCK_INT:
144 return PrimitiveSocketValue{socket.default_value_typed<bNodeSocketValueInt>()->value};
145 case SOCK_BOOLEAN:
146 return PrimitiveSocketValue{
147 bool(socket.default_value_typed<bNodeSocketValueBoolean>()->value)};
148 case SOCK_VECTOR:
149 return PrimitiveSocketValue{
150 float3(socket.default_value_typed<bNodeSocketValueVector>()->value)};
151 case SOCK_RGBA:
152 return PrimitiveSocketValue{
153 ColorGeometry4f(socket.default_value_typed<bNodeSocketValueRGBA>()->value)};
154 case SOCK_MENU:
155 return PrimitiveSocketValue{
156 MenuValue(socket.default_value_typed<bNodeSocketValueMenu>()->value)};
157 case SOCK_STRING:
158 return PrimitiveSocketValue{
159 std::string(socket.default_value_typed<bNodeSocketValueString>()->value)};
160 default:
161 return std::nullopt;
162 }
163 }
164 if (std::get_if<FallbackValue>(&this->value)) {
165 switch (type.type) {
166 case SOCK_INT:
167 case SOCK_BOOLEAN:
168 case SOCK_VECTOR:
169 case SOCK_RGBA:
170 case SOCK_FLOAT:
171 case SOCK_STRING:
172 case SOCK_MENU:
173 return PrimitiveSocketValue::from_value(
174 {type.base_cpp_type, type.base_cpp_type->default_value()});
175 default:
176 return std::nullopt;
177 }
178 }
179 return std::nullopt;
180 }
181};
182
183struct BundleSocketValue {
184 struct Item {
185 std::string key;
186 SocketValue value;
187 const bke::bNodeSocketType *socket_type = nullptr;
188 };
189
190 Vector<Item> items;
191};
192
193struct PreservedZone {
194 bNode *input_node = nullptr;
195 bNode *output_node = nullptr;
196};
197
203constexpr int preserved_repeat_zone_iteration = -1;
204
205class ShaderNodesInliner {
206 private:
208 ResourceScope scope_;
210 const bNodeTree &src_tree_;
212 bNodeTree &dst_tree_;
214 InlineShaderNodeTreeParams &params_;
216 bke::ComputeContextCache compute_context_cache_;
221 Map<const ComputeContext *, const ComputeContext *> parent_zone_contexts_;
223 Map<SocketInContext, SocketValue> value_by_socket_;
228 Map<NodeInContext, PreservedZone> copied_zone_by_zone_output_node_;
230 Stack<SocketInContext> scheduled_sockets_stack_;
232 const bke::DataTypeConversions &data_type_conversions_;
234 int dst_node_counter_ = 0;
235
236 struct FailedToInlineRepeatZone {
237 const ComputeContext *parent_ctx = nullptr;
238 int output_node_id = 0;
239
240 friend bool operator==(const FailedToInlineRepeatZone &a,
241 const FailedToInlineRepeatZone &b) = default;
242
243 uint64_t hash() const
244 {
245 return get_default_hash(this->parent_ctx, this->output_node_id);
246 }
247 };
248
265 Set<FailedToInlineRepeatZone> repeat_zones_to_force_inline_;
266
271 Set<const ComputeContext *> handled_side_effect_contexts_;
272
273 public:
274 ShaderNodesInliner(const bNodeTree &src_tree,
275 bNodeTree &dst_tree,
276 InlineShaderNodeTreeParams &params)
277 : src_tree_(src_tree),
278 dst_tree_(dst_tree),
279 params_(params),
280 data_type_conversions_(bke::get_implicit_type_conversions())
281 {
282 }
283
284 bool do_inline()
285 {
286 src_tree_.ensure_topology_cache();
287 if (src_tree_.has_available_link_cycle()) {
288 return false;
289 }
290
291 const Vector<SocketInContext> final_output_sockets = this->find_final_output_sockets();
292
293 /* Evaluation starts at the final output sockets which will request the evaluation of whether
294 * sockets are linked to them. */
295 for (const SocketInContext &socket : final_output_sockets) {
296 this->schedule_socket(socket);
297 }
298
299 this->schedule_side_effect_nodes_in_context(src_tree_, nullptr, nullptr);
300 /* Final outputs may be in nested groups, so schedule side effect nodes for these too. */
301 for (const SocketInContext &socket : final_output_sockets) {
302 this->schedule_side_effect_nodes_in_context(socket->owner_tree(), nullptr, socket.context);
303 }
304
305 /* Evaluate until all scheduled sockets have a value. While evaluating a single socket, it may
306 * either end up having a value, or request more other sockets that need to be evaluated first.
307 *
308 * This uses an explicit stack instead of recursion to avoid stack overflows which can easily
309 * happen when there are long chains of nodes (or e.g. repeat zones with many iterations). */
310 while (!scheduled_sockets_stack_.is_empty()) {
311 const SocketInContext socket = scheduled_sockets_stack_.peek();
312 const int old_stack_size = scheduled_sockets_stack_.size();
313
314 this->handle_socket(socket);
315
316 if (scheduled_sockets_stack_.size() == old_stack_size) {
317 /* No additional dependencies were pushed, so this socket is fully handled and can be
318 * popped from the stack. */
319 BLI_assert(socket == scheduled_sockets_stack_.peek());
320 scheduled_sockets_stack_.pop();
321 }
322 }
323
324 /* Create actual output nodes. */
325 Map<NodeInContext, bNode *> final_output_nodes;
326 for (const SocketInContext &socket : final_output_sockets) {
327 const NodeInContext src_node = socket.owner_node();
328 bNode *copied_node = final_output_nodes.lookup_or_add_cb(src_node, [&]() {
329 Map<const bNodeSocket *, bNodeSocket *> socket_map;
330 bNode *copied_node = bke::node_copy_with_mapping(&dst_tree_,
331 *src_node.node,
332 this->node_copy_flag(),
333 std::nullopt,
334 this->get_next_node_identifier(),
335 socket_map);
336 copied_node->parent = nullptr;
337 return copied_node;
338 });
339 bNodeSocket *copied_socket = static_cast<bNodeSocket *>(
340 BLI_findlink(&copied_node->inputs, socket.socket->index()));
341 this->set_input_socket_value(
342 src_node, *copied_node, *copied_socket, value_by_socket_.lookup(socket));
343 }
344
345 this->restore_zones_in_output_tree();
346 this->position_nodes_in_output_tree();
347 return true;
348 }
349
350 Vector<SocketInContext> find_final_output_sockets()
351 {
352 Vector<TreeInContext> trees;
353 this->find_trees_potentially_containing_shader_outputs_recursive(nullptr, src_tree_, trees);
354
355 auto get_engine_target = [](const bNode *output_node) {
356 if (STR_ELEM(output_node->idname,
357 "ShaderNodeOutputMaterial",
358 "ShaderNodeOutputLight",
359 "ShaderNodeOutputWorld"))
360 {
361 return NodeShaderOutputTarget(output_node->custom1);
362 }
363 return SHD_OUTPUT_ALL;
364 };
365
366 Vector<SocketInContext> output_sockets;
367 auto add_output_type = [&](const UString output_type) {
368 for (const TreeInContext &tree : trees) {
369 const bke::bNodeTreeZones &zones = *tree->zones();
370 for (const bNode *node : tree->nodes_by_type(output_type)) {
371 if (!ELEM(get_engine_target(node), SHD_OUTPUT_ALL, params_.target_engine_)) {
372 continue;
373 }
374 const bke::bNodeTreeZone *zone = zones.get_zone_by_node(node->identifier);
375 if (zone) {
376 this->report_error({tree.context, node},
377 TIP_("Output node must not be in zone"),
379 continue;
380 }
381 for (const bNodeSocket *socket : node->input_sockets()) {
382 output_sockets.append({tree.context, socket});
383 }
384 }
385 }
386 };
387
388 /* owner_id can be null for DefaultSurfaceNodeTree. */
389 ID_Type tree_type = src_tree_.owner_id ? src_tree_.owner_id->id_type() : ID_MA;
390
391 switch (tree_type) {
392 case ID_MA:
393 add_output_type("ShaderNodeOutputMaterial"_ustr);
394 add_output_type("ShaderNodeOutputLight"_ustr);
395 add_output_type("ShaderNodeOutputAOV"_ustr);
396 break;
397 case ID_WO:
398 add_output_type("ShaderNodeOutputWorld"_ustr);
399 add_output_type("ShaderNodeOutputAOV"_ustr);
400 break;
401 case ID_LA:
402 add_output_type("ShaderNodeOutputLight"_ustr);
403 break;
404 case ID_LS:
405 add_output_type("ShaderNodeOutputLineStyle"_ustr);
406 break;
407 default:
409 }
410
411 return output_sockets;
412 }
413
414 void find_trees_potentially_containing_shader_outputs_recursive(const ComputeContext *context,
415 const bNodeTree &tree,
416 Vector<TreeInContext> &r_trees)
417 {
418 const bke::bNodeTreeZones *zones = tree.zones();
419 if (!zones) {
420 return;
421 }
422 if (tree.has_available_link_cycle()) {
423 return;
424 }
425 r_trees.append({context, &tree});
426 for (const bNode *group_node : tree.group_nodes()) {
427 if (group_node->is_muted()) {
428 continue;
429 }
430 const bNodeTree *group = id_cast<const bNodeTree *>(group_node->id);
431 if (!group || ID_MISSING(&group->id)) {
432 continue;
433 }
434 group->ensure_topology_cache();
435 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(group_node->identifier);
436 if (zone) {
437 /* Node groups in zones are ignored. */
438 continue;
439 }
440 const ComputeContext &group_context = compute_context_cache_.for_group_node(
441 context, group_node->identifier, &tree);
442 this->find_trees_potentially_containing_shader_outputs_recursive(
443 &group_context, *group, r_trees);
444 }
445 }
446
447 void handle_socket(const SocketInContext &socket)
448 {
449 if (!socket->is_available()) {
450 return;
451 }
452 if (value_by_socket_.contains(socket)) {
453 /* The socket already has a value, so there is nothing to do. */
454 return;
455 }
456 if (socket->is_input()) {
457 this->handle_input_socket(socket);
458 }
459 else {
460 this->handle_output_socket(socket);
461 }
462 }
463
464 bool should_use_link(const bNodeLink &link) const
465 {
466 return link.is_used() && (link.flag & NODE_LINK_VALID) &&
467 !this->socket_is_ignored(*link.fromsock);
468 }
469
470 void handle_input_socket(const SocketInContext &socket)
471 {
472 if (socket->is_multi_input()) {
473 this->handle_multi_input_socket(socket);
474 return;
475 }
476
477 const bNodeLink *used_link = nullptr;
478 for (const bNodeLink *link : socket->directly_linked_links()) {
479 if (!this->should_use_link(*link) || link->fromnode == nullptr ||
480 link->fromnode->is_undefined())
481 {
482 continue;
483 }
484 used_link = link;
485 }
486 if (!used_link) {
487 /* If there is no link on the input, use the value of the socket directly. */
488 if (this->input_socket_may_have_dangling_value(socket)) {
489 this->store_socket_value(socket, {DanglingValue{}});
490 }
491 else {
492 this->store_socket_value(socket, {InputSocketValue{socket.socket}});
493 }
494 return;
495 }
496
497 const ComputeContext *from_context = this->get_link_source_context(*used_link, socket);
498 const SocketInContext origin_socket = {from_context, used_link->fromsock};
499 if (const auto *value = value_by_socket_.lookup_ptr(origin_socket)) {
500 if (std::holds_alternative<DanglingValue>(value->value)) {
501 if (this->input_socket_may_have_dangling_value(socket)) {
502 this->store_socket_value(socket, {DanglingValue{}});
503 }
504 else {
505 /* If the input value is dangling, use the value of the socket itself. */
506 this->store_socket_value(socket, {InputSocketValue{socket.socket}});
507 }
508 return;
509 }
510 /* If the socket linked to the input has a value already, copy that value to the current
511 * socket, potentially with an implicit conversion. */
512 this->store_socket_value(socket,
513 this->handle_implicit_conversion(*value,
514 *used_link->fromsock->typeinfo,
515 *used_link->tosock->typeinfo));
516 return;
517 }
518 /* If the origin socket does not have a value yet, only schedule it for evaluation for now. */
519 this->schedule_socket(origin_socket);
520 }
521
522 void handle_multi_input_socket(const SocketInContext &socket)
523 {
524 bool all_links_ready = true;
525 Vector<SocketValue, 0> values;
526 for (const bNodeLink *link : socket->directly_linked_links()) {
527 if (!this->should_use_link(*link)) {
528 continue;
529 }
530 const ComputeContext *from_context = this->get_link_source_context(*link, socket);
531 const SocketInContext origin_socket = {from_context, link->fromsock};
532 const SocketValue *value = value_by_socket_.lookup_ptr(origin_socket);
533 if (!value) {
534 this->schedule_socket(origin_socket);
535 all_links_ready = false;
536 continue;
537 }
538 values.append(*value);
539 }
540 if (!all_links_ready) {
541 return;
542 }
543 this->store_socket_value(socket, {MultiInputValue{std::move(values)}});
544 }
545
551 bool input_socket_may_have_dangling_value(const SocketInContext &socket)
552 {
553 BLI_assert(socket->is_input());
554 const NodeInContext node = socket.owner_node();
555 return node->is_reroute() || node->is_muted();
556 }
557
558 const ComputeContext *get_link_source_context(const bNodeLink &link,
559 const SocketInContext &to_socket)
560 {
561 const bNodeTree &tree = to_socket->owner_tree();
562 const bke::bNodeTreeZones *zones = tree.zones();
563 if (!zones) {
564 return nullptr;
565 }
566 const bke::bNodeTreeZone *to_zone = zones->get_zone_by_socket(*to_socket);
567 const bke::bNodeTreeZone *from_zone = zones->get_zone_by_socket(*link.fromsock);
568 const ComputeContext *context = to_socket.context;
569 for (const bke::bNodeTreeZone *zone = to_zone; zone != from_zone; zone = zone->parent_zone) {
570 context = parent_zone_contexts_.lookup(context);
571 }
572 return context;
573 }
574
575 void handle_output_socket(const SocketInContext &socket)
576 {
577 const NodeInContext node = socket.owner_node();
578 if (node->is_reroute()) {
579 this->handle_output_socket__reroute(socket);
580 return;
581 }
582 if (node->is_muted()) {
583 if (!this->handle_output_socket__internal_links(socket)) {
584 /* The output socket does not have a corresponding input, so the value is ignored. */
585 this->store_socket_value_dangling(socket);
586 }
587 return;
588 }
589 if (node->is_group()) {
590 this->handle_output_socket__group(socket);
591 return;
592 }
593 if (node->is_group_input()) {
594 this->handle_output_socket__group_input(socket);
595 return;
596 }
597 if (node->is_type("GeometryNodeRepeatOutput"_ustr)) {
598 if (this->should_preserve_repeat_zone_node(node.context, *node)) {
599 this->handle_output_socket__preserved_repeat_output(socket);
600 return;
601 }
602 this->handle_output_socket__repeat_output(socket);
603 return;
604 }
605 if (node->is_type("GeometryNodeRepeatInput"_ustr)) {
606 const auto *context = dynamic_cast<const bke::RepeatZoneComputeContext *>(socket.context);
607 if (!context) {
608 /* This socket is expected to be in the repeat zone, so it should have a context. */
609 this->store_socket_value_fallback(socket);
610 return;
611 }
612 if (context->iteration() == preserved_repeat_zone_iteration) {
613 this->handle_output_socket__preserved_repeat_input(socket);
614 return;
615 }
616 this->handle_output_socket__repeat_input(socket);
617 return;
618 }
619 if (node->is_type("NodeClosureOutput"_ustr)) {
620 this->handle_output_socket__closure_output(socket);
621 return;
622 }
623 if (node->is_type("NodeClosureInput"_ustr)) {
624 this->handle_output_socket__closure_input(socket);
625 return;
626 }
627 if (node->is_type("NodeEvaluateClosure"_ustr)) {
628 this->handle_output_socket__evaluate_closure(socket);
629 return;
630 }
631 if (node->is_type("NodeCombineBundle"_ustr)) {
632 this->handle_output_socket__combine_bundle(socket);
633 return;
634 }
635 if (node->is_type("NodeSeparateBundle"_ustr)) {
636 this->handle_output_socket__separate_bundle(socket);
637 return;
638 }
639 if (node->is_type("GeometryNodeMenuSwitch"_ustr)) {
640 this->handle_output_socket__menu_switch(socket);
641 return;
642 }
643 if (node->is_type("GeometryNodeIndexSwitch"_ustr)) {
644 this->handle_output_socket__index_switch(socket);
645 return;
646 }
647 if (node->is_type("GeometryNodeSwitch"_ustr)) {
648 this->handle_output_socket__switch(socket);
649 return;
650 }
651 if (node->is_type("FunctionNodeInputMenu"_ustr)) {
652 this->handle_output_socket__input_menu(socket);
653 return;
654 }
655 if (node->is_type("NodeJoinBundle"_ustr)) {
656 this->handle_output_socket__join_bundle(socket);
657 return;
658 }
659 if (node->is_type("NodeImplicitConversion"_ustr)) {
660 this->handle_output_socket__implicit_conversion(socket);
661 return;
662 }
663 if (node->is_type("GeometryNodeWarning"_ustr)) {
664 this->handle_output_socket__warning(socket);
665 return;
666 }
667 if (node->is_type("NodeEnableOutput"_ustr)) {
668 this->handle_output_socket__enable_output(socket);
669 return;
670 }
671 this->handle_output_socket__eval(socket);
672 }
673
674 void handle_output_socket__reroute(const SocketInContext &socket)
675 {
676 const NodeInContext node = socket.owner_node();
677 const SocketInContext input_socket = node.input_socket(0);
678 this->forward_value_or_schedule(socket, input_socket);
679 }
680
681 /* Returns whether the socket was handled. */
682 [[nodiscard]] bool handle_output_socket__internal_links(const SocketInContext &socket)
683 {
684 const NodeInContext node = socket.owner_node();
685 for (const bNodeInternalLink &internal_link : node->internal_links()) {
686 if (internal_link.out == socket.socket) {
687 const SocketInContext src_socket = {socket.context, internal_link.in};
688 if (src_socket->is_multi_input()) {
689 const bNodeLink *src_link = nullptr;
690 for (const bNodeLink *link : src_socket->directly_linked_links()) {
691 if (this->should_use_link(*link)) {
692 src_link = link;
693 break;
694 }
695 }
696 if (!src_link) {
697 return false;
698 }
699 const ComputeContext *from_context = this->get_link_source_context(*src_link,
700 src_socket);
701 const SocketInContext origin_socket = {from_context, src_link->fromsock};
702 this->forward_value_or_schedule(socket, origin_socket);
703 return true;
704 }
705 if (const SocketValue *value = value_by_socket_.lookup_ptr(src_socket)) {
706 /* Pass the value of the internally linked input socket, with an implicit conversion if
707 * necessary. */
708 this->store_socket_value(socket,
709 this->handle_implicit_conversion(*value,
710 *internal_link.in->typeinfo,
711 *internal_link.out->typeinfo));
712 return true;
713 }
714 this->schedule_socket(src_socket);
715 return true;
716 }
717 }
718 return false;
719 }
720
721 void handle_output_socket__group(const SocketInContext &socket)
722 {
723 const NodeInContext node = socket.owner_node();
724 const bNodeTree *group = reinterpret_cast<const bNodeTree *>(node->id);
725 if (!group || ID_MISSING(&group->id)) {
726 this->store_socket_value_fallback(socket);
727 return;
728 }
729 if (socket.context && socket.context->parents_num() >= U.nodes_stack_limit) {
730 this->store_socket_value_fallback(socket);
731 this->report_error(node,
732 TIP_("Nodes stack limit reached (too many levels of nested nodes)"),
734 return;
735 }
736 group->ensure_interface_cache();
737 group->ensure_topology_cache();
738 const bNode *group_output_node = group->group_output_node();
739 if (!group_output_node) {
740 this->store_socket_value_fallback(socket);
741 return;
742 }
743 /* Get the value of an output of a group node by evaluating the corresponding output of the
744 * node group. Since this socket is in a different tree, the compute context is different. */
745 const ComputeContext &group_compute_context = compute_context_cache_.for_group_node(
746 socket.context, node->identifier, &node->owner_tree());
747 const SocketInContext group_output_socket_ctx = {
748 &group_compute_context, &group_output_node->input_socket(socket->index())};
749 this->schedule_side_effect_nodes_in_context(*group, nullptr, &group_compute_context);
750 this->forward_value_or_schedule(socket, group_output_socket_ctx);
751 }
752
753 void handle_output_socket__group_input(const SocketInContext &socket)
754 {
755 if (const auto *group_node_compute_context =
756 dynamic_cast<const bke::GroupNodeComputeContext *>(socket.context))
757 {
758 /* Get the value of a group input from the corresponding input socket of the parent group
759 * node. */
760 const ComputeContext *parent_compute_context = group_node_compute_context->parent();
761 const bNode *group_node = group_node_compute_context->node();
762 BLI_assert(group_node);
763 const bNodeSocket &group_node_input = group_node->input_socket(socket->index());
764 const SocketInContext group_input_socket_ctx = {parent_compute_context, &group_node_input};
765 this->forward_value_or_schedule(socket, group_input_socket_ctx);
766 return;
767 }
768 this->store_socket_value_fallback(socket);
769 }
770
778 bool should_preserve_repeat_zone_node(const ComputeContext *outer_ctx,
779 const bNode &repeat_zone_node) const
780 {
781 BLI_assert(repeat_zone_node.is_type("GeometryNodeRepeatOutput"_ustr) ||
782 repeat_zone_node.is_type("GeometryNodeRepeatInput"_ustr));
783 if (!params_.allow_preserving_repeat_zones) {
784 return false;
785 }
786 const bNodeTree &tree = repeat_zone_node.owner_tree();
787 const bke::bNodeTreeZones *zones = tree.zones();
788 if (!zones) {
789 return false;
790 }
791 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(repeat_zone_node.identifier);
792 if (!zone) {
793 return false;
794 }
795 const bNode *repeat_zone_input_node = zone->input_node();
796 const bNode *repeat_zone_output_node = zone->output_node();
797 if (!repeat_zone_input_node || !repeat_zone_output_node) {
798 return false;
799 }
800 if (repeat_zones_to_force_inline_.contains({outer_ctx, *zone->output_node_id})) {
801 return false;
802 }
803 const auto &storage = *static_cast<const NodeGeometryRepeatOutput *>(
804 repeat_zone_output_node->storage);
805 for (const int i : IndexRange(storage.items_num)) {
806 const NodeRepeatItem &item = storage.items[i];
807 if (!ELEM(item.socket_type, SOCK_INT, SOCK_FLOAT, SOCK_BOOLEAN, SOCK_RGBA, SOCK_VECTOR)) {
808 /* Repeat zones with more special types have to be inlined. */
809 return false;
810 }
811 }
812 return true;
813 }
814
815 bool is_inliner_evaluation_node(const bNode &node) const
816 {
817 const UString idname = node.typeinfo->idname;
818 return ELEM(idname,
819 "FunctionNodeFindInString"_ustr,
820 "FunctionNodeFormatString"_ustr,
821 "FunctionNodeInputSpecialCharacters"_ustr,
822 "FunctionNodeInputString"_ustr,
823 "FunctionNodeMatchString"_ustr,
824 "FunctionNodeReplaceString"_ustr,
825 "FunctionNodeReverseString"_ustr,
826 "FunctionNodeSetStringCase"_ustr,
827 "FunctionNodeSliceString"_ustr,
828 "FunctionNodeStringLength"_ustr,
829 "FunctionNodeStringToValue"_ustr,
830 "FunctionNodeTrimString"_ustr,
831 "FunctionNodeValueToString"_ustr);
832 }
833
834 void handle_output_socket__repeat_output(const SocketInContext &socket)
835 {
836 const bNode &repeat_output_node = socket->owner_node();
837 const bNodeTree &tree = socket->owner_tree();
838
839 const bke::bNodeTreeZones *zones = tree.zones();
840 if (!zones) {
841 this->store_socket_value_fallback(socket);
842 return;
843 }
844 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(repeat_output_node.identifier);
845 if (!zone) {
846 this->store_socket_value_fallback(socket);
847 return;
848 }
849 const NodeInContext repeat_input_node = {socket.context, zone->input_node()};
850 const SocketInContext iterations_input = repeat_input_node.input_socket(0);
851 const SocketValue *iterations_socket_value = value_by_socket_.lookup_ptr(iterations_input);
852 if (!iterations_socket_value) {
853 /* The number of iterations is not known yet, so only schedule that socket for now. */
854 this->schedule_socket(iterations_input);
855 return;
856 }
857 const std::optional<PrimitiveSocketValue> iterations_value_opt =
858 iterations_socket_value->to_primitive(*iterations_input->typeinfo);
859 if (!iterations_value_opt) {
860 this->add_dynamic_repeat_zone_iterations_error(repeat_input_node);
861 }
862 const int iterations = iterations_value_opt.has_value() ?
863 std::get<int>(iterations_value_opt->value) :
864 0;
865 if (iterations <= 0) {
866 /* If the number of iterations is zero, the values are copied directly from the repeat input
867 * node. */
868 const SocketInContext origin_socket = repeat_input_node.input_socket(1 + socket->index());
869 this->forward_value_or_schedule(socket, origin_socket);
870 return;
871 }
872 /* Otherwise, the value is copied from the output of the last iteration. */
873 const ComputeContext &last_iteration_context = compute_context_cache_.for_repeat_zone(
874 socket.context, repeat_output_node, iterations - 1);
875 parent_zone_contexts_.add(&last_iteration_context, socket.context);
876 this->schedule_side_effect_nodes_in_context(tree, zone, &last_iteration_context);
877 const SocketInContext origin_socket = {&last_iteration_context,
878 &repeat_output_node.input_socket(socket->index())};
879 this->forward_value_or_schedule(socket, origin_socket);
880 }
881
882 void handle_output_socket__preserved_repeat_output(const SocketInContext &socket)
883 {
884 const bNodeTree &tree = socket->owner_tree();
885 const NodeInContext repeat_output_node = socket.owner_node();
886 const bke::bNodeTreeZones &zones = *tree.zones();
887 const bke::bNodeTreeZone &zone = *zones.get_zone_by_node(repeat_output_node->identifier);
888 const bNode &repeat_input_node = *zone.input_node();
889
890 const ComputeContext *out_context = socket.context;
891 const ComputeContext &in_context = compute_context_cache_.for_repeat_zone(
892 socket.context, repeat_output_node->identifier, preserved_repeat_zone_iteration);
893 parent_zone_contexts_.add(&in_context, out_context);
894
895 /* Scheduling side effect nodes last will make sure that they are evaluated before other nodes
896 * in the repeat zone. This is important for back-tracking the decision to preserve the repeat
897 * zone in case e.g. the warning node is used. Otherwise, the side effect nodes may only be
898 * evaluated when the repeat zone is completely done, at which point it is too late to
899 * back-track. */
901 [&]() { this->schedule_side_effect_nodes_in_context(tree, &zone, &in_context); });
902
903 const EnsureInputsResult ensured_inputs = this->ensure_node_inputs(
904 {&in_context, &socket->owner_node()});
905 if (ensured_inputs.has_missing_inputs) {
906 /* The node can only be evaluated if all inputs values are known. */
907 return;
908 }
909 const NodeInContext node = socket.owner_node();
910 bNode &copied_node = this->handle_output_socket__eval_copy_node(
911 *node, &in_context, out_context);
912 PreservedZone &preserved_zone = copied_zone_by_zone_output_node_.lookup_or_add_default(
913 repeat_output_node);
914 preserved_zone.output_node = &copied_node;
915 /* Ensure that the repeat input node is created as well. */
916 this->schedule_socket({&in_context, &repeat_input_node.output_socket(0)});
917 }
918
919 void handle_output_socket__preserved_repeat_input(const SocketInContext &socket)
920 {
921 const ComputeContext &out_context = *socket.context;
922 const ComputeContext *in_context = out_context.parent();
923 BLI_assert(dynamic_cast<const bke::RepeatZoneComputeContext &>(out_context).iteration() ==
924 preserved_repeat_zone_iteration);
925
926 const EnsureInputsResult ensured_inputs = this->ensure_node_inputs(
927 {in_context, &socket->owner_node()});
928 if (ensured_inputs.has_missing_inputs) {
929 /* The node can only be evaluated if all inputs values are known. */
930 return;
931 }
932 const bNodeTree &tree = socket->owner_tree();
933 const NodeInContext node = socket.owner_node();
934 bNode &copied_node = this->handle_output_socket__eval_copy_node(
935 *node, in_context, &out_context);
936 const auto &storage = *static_cast<const NodeGeometryRepeatInput *>(node->storage);
937 const NodeInContext repeat_output_node{in_context, tree.node_by_id(storage.output_node_id)};
938 PreservedZone &preserved_zone = copied_zone_by_zone_output_node_.lookup_or_add_default(
939 repeat_output_node);
940 preserved_zone.input_node = &copied_node;
941 }
942
943 void add_dynamic_repeat_zone_iterations_error(const NodeInContext &repeat_input_node)
944 {
945 this->report_error(repeat_input_node,
946 TIP_("Iterations input has to be a constant value"),
948 }
949
950 void handle_output_socket__repeat_input(const SocketInContext &socket)
951 {
952 const bNodeTree &tree = socket->owner_tree();
953 const bNode &repeat_input_node = socket->owner_node();
954 const auto *repeat_zone_context = dynamic_cast<const bke::RepeatZoneComputeContext *>(
955 socket.context);
956 if (!repeat_zone_context) {
957 this->store_socket_value_fallback(socket);
958 return;
959 }
960 /* The index of the current iteration comes from the context. */
961 const int iteration = repeat_zone_context->iteration();
962
963 if (socket->index() == 0) {
964 /* The first output is the current iteration index. */
965 this->store_socket_value_primitive(socket, iteration);
966 return;
967 }
968
969 if (iteration == 0) {
970 /* In the first iteration, the values are copied from the corresponding input socket. */
971 const SocketInContext origin_socket = {repeat_zone_context->parent(),
972 &repeat_input_node.input_socket(socket->index())};
973 this->forward_value_or_schedule(socket, origin_socket);
974 return;
975 }
976 /* For later iterations, the values are copied from the corresponding output of the previous
977 * iteration. */
978 const bke::bNodeTreeZones &zones = *tree.zones();
979 const bke::bNodeTreeZone &zone = *zones.get_zone_by_node(repeat_input_node.identifier);
980 const bNode &repeat_output_node = *repeat_input_node.owner_tree().node_by_id(
981 repeat_zone_context->output_node_id());
982 const int previous_iteration = iteration - 1;
983 const ComputeContext &previous_iteration_context = compute_context_cache_.for_repeat_zone(
984 repeat_zone_context->parent(), repeat_output_node, previous_iteration);
985 parent_zone_contexts_.add(&previous_iteration_context, repeat_zone_context->parent());
986 this->schedule_side_effect_nodes_in_context(tree, &zone, &previous_iteration_context);
987 const SocketInContext origin_socket = {&previous_iteration_context,
988 &repeat_output_node.input_socket(socket->index() - 1)};
989 this->forward_value_or_schedule(socket, origin_socket);
990 }
991
992 void handle_output_socket__closure_output(const SocketInContext &socket)
993 {
994 const bNode &node = socket->owner_node();
995 const bke::bNodeTreeZones *zones = node.owner_tree().zones();
996 if (!zones) {
997 this->store_socket_value_fallback(socket);
998 return;
999 }
1000 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(node.identifier);
1001 if (!zone) {
1002 this->store_socket_value_fallback(socket);
1003 return;
1004 }
1005 /* Just store a reference to the closure. */
1006 this->store_socket_value(socket, {ClosureZoneValue{zone, socket.context}});
1007 }
1008
1009 void handle_output_socket__evaluate_closure(const SocketInContext &socket)
1010 {
1011 const NodeInContext evaluate_closure_node = socket.owner_node();
1012 const SocketInContext closure_input_socket = evaluate_closure_node.input_socket(0);
1013 const SocketValue *closure_input_value = value_by_socket_.lookup_ptr(closure_input_socket);
1014 if (!closure_input_value) {
1015 /* The closure to evaluate is not known yet, so schedule the closure input before it can be
1016 * evaluated. */
1017 this->schedule_socket(closure_input_socket);
1018 return;
1019 }
1020 const ClosureZoneValue *closure_zone_value = std::get_if<ClosureZoneValue>(
1021 &closure_input_value->value);
1022 if (!closure_zone_value) {
1023 /* If the closure is null, the node behaves as if it is muted. */
1024 if (!this->handle_output_socket__internal_links(socket)) {
1025 this->store_socket_value_fallback(socket);
1026 }
1027 return;
1028 }
1029 if (socket.context && socket.context->parents_num() >= U.nodes_stack_limit) {
1030 this->store_socket_value_fallback(socket);
1031 this->report_error(evaluate_closure_node,
1032 TIP_("Nodes stack limit reached (too many levels of nested nodes)"),
1034 return;
1035 }
1036
1037 const auto *evaluate_closure_storage = static_cast<const NodeEvaluateClosure *>(
1038 evaluate_closure_node->storage);
1039 const bNode &closure_output_node = *closure_zone_value->zone->output_node();
1040 const auto &closure_storage = *static_cast<const NodeClosureOutput *>(
1041 closure_output_node.storage);
1042 const StringRef key = evaluate_closure_storage->output_items.items[socket->index()].name;
1043
1044 const bNodeTree &closure_tree = closure_output_node.owner_tree();
1045 const ClosureSourceLocation closure_source_location{
1046 &closure_tree,
1047 closure_output_node.identifier,
1048 closure_zone_value->closure_creation_context ?
1049 closure_zone_value->closure_creation_context->hash() :
1050 ComputeContextHash{},
1051 closure_zone_value->closure_creation_context};
1052 const bke::EvaluateClosureComputeContext &closure_eval_context =
1053 compute_context_cache_.for_evaluate_closure(socket.context,
1054 evaluate_closure_node->identifier,
1055 &socket->owner_tree(),
1056 closure_source_location);
1057 parent_zone_contexts_.add(&closure_eval_context, closure_zone_value->closure_creation_context);
1058 this->schedule_side_effect_nodes_in_context(
1059 closure_tree, closure_zone_value->zone, &closure_eval_context);
1060
1061 for (const int i : IndexRange(closure_storage.output_items.items_num)) {
1062 const NodeClosureOutputItem &item = closure_storage.output_items.items[i];
1063 if (key != item.name) {
1064 continue;
1065 }
1066 /* Get the value of the output by evaluating the corresponding output in the closure zone. */
1067 const SocketInContext origin_socket = {&closure_eval_context,
1068 &closure_output_node.input_socket(i)};
1069 this->forward_value_or_schedule(socket, origin_socket);
1070 return;
1071 }
1072 this->store_socket_value_fallback(socket);
1073 }
1074
1075 void handle_output_socket__closure_input(const SocketInContext &socket)
1076 {
1077 const bNode &closure_input_node = socket->owner_node();
1078 const auto *closure_eval_context = dynamic_cast<const bke::EvaluateClosureComputeContext *>(
1079 socket.context);
1080 if (!closure_eval_context) {
1081 this->store_socket_value_fallback(socket);
1082 return;
1083 }
1084 const bNode &closure_output_node = *closure_input_node.owner_tree().node_by_id(
1085 closure_eval_context->closure_source_location()->closure_output_node_id);
1086 const NodeInContext closure_eval_node = {closure_eval_context->parent(),
1087 closure_eval_context->node()};
1088
1089 const auto &closure_storage = *static_cast<const NodeClosureOutput *>(
1090 closure_output_node.storage);
1091 const auto &eval_closure_storage = *static_cast<const NodeEvaluateClosure *>(
1092 closure_eval_node->storage);
1093
1094 const StringRef key = closure_storage.input_items.items[socket->index()].name;
1095 for (const int i : IndexRange(eval_closure_storage.input_items.items_num)) {
1096 const NodeEvaluateClosureInputItem &item = eval_closure_storage.input_items.items[i];
1097 if (key != item.name) {
1098 continue;
1099 }
1100 /* The input of a closure zone gets its value from the corresponding input of the Evaluate
1101 * Closure node that evaluates it. */
1102 const SocketInContext origin_socket = closure_eval_node.input_socket(i + 1);
1103 this->forward_value_or_schedule(socket, origin_socket);
1104 return;
1105 }
1106 this->store_socket_value_fallback(socket);
1107 }
1108
1109 void handle_output_socket__combine_bundle(const SocketInContext &socket)
1110 {
1111 const NodeInContext node = socket.owner_node();
1112 const auto &storage = *static_cast<const NodeCombineBundle *>(node->storage);
1113
1114 bool all_inputs_available = true;
1115 for (const bNodeSocket *input_socket : node->input_sockets()) {
1116 const SocketInContext input_socket_ctx = {socket.context, input_socket};
1117 if (!value_by_socket_.lookup_ptr(input_socket_ctx)) {
1118 this->schedule_socket(input_socket_ctx);
1119 all_inputs_available = false;
1120 }
1121 }
1122 if (!all_inputs_available) {
1123 /* Can't create the bundle yet. Wait until all inputs are available. */
1124 return;
1125 }
1126 /* Build the actual bundle socket value from the input values. */
1127 auto bundle_value = std::make_shared<BundleSocketValue>();
1128 for (const int i : IndexRange(storage.items_num)) {
1129 const SocketInContext input_socket = node.input_socket(i);
1130 const NodeCombineBundleItem &item = storage.items[i];
1131 const StringRef key = item.name;
1132 const auto &socket_value = value_by_socket_.lookup(input_socket);
1133 bundle_value->items.append({key, socket_value, input_socket->typeinfo});
1134 }
1135 this->store_socket_value(socket, {bundle_value});
1136 }
1137
1138 void handle_output_socket__separate_bundle(const SocketInContext &socket)
1139 {
1140 const NodeInContext node = socket.owner_node();
1141 const auto &storage = *static_cast<const NodeSeparateBundle *>(node->storage);
1142
1143 const SocketInContext input_socket = node.input_socket(0);
1144 const SocketValue *socket_value = value_by_socket_.lookup_ptr(input_socket);
1145 if (!socket_value) {
1146 /* The input bundle is not known yet, so schedule it for now. */
1147 this->schedule_socket(input_socket);
1148 return;
1149 }
1150 const auto *bundle_value_ptr = std::get_if<BundleSocketValuePtr>(&socket_value->value);
1151 if (!bundle_value_ptr) {
1152 /* The bundle is empty. Use the fallback value. */
1153 this->store_socket_value_fallback(socket);
1154 return;
1155 }
1156 const BundleSocketValue &bundle_value = **bundle_value_ptr;
1157
1158 const StringRef key = storage.items[socket->index()].name;
1159 for (const BundleSocketValue::Item &item : bundle_value.items) {
1160 if (key != item.key) {
1161 continue;
1162 }
1163 /* Extract the value from the bundle. */
1164 const SocketValue converted_value = this->handle_implicit_conversion(
1165 item.value, *item.socket_type, *socket->typeinfo);
1166 this->store_socket_value(socket, converted_value);
1167 return;
1168 }
1169 /* The bundle does not contain the requested key, so use the fallback value. */
1170 this->store_socket_value_fallback(socket);
1171 }
1172
1173 void handle_output_socket__join_bundle(const SocketInContext &socket)
1174 {
1175 const NodeInContext node = socket.owner_node();
1176 const SocketInContext input_socket = node.input_socket(0);
1177 const SocketValue *socket_value = value_by_socket_.lookup_ptr(input_socket);
1178 if (!socket_value) {
1179 /* The input bundles are not known yet, so schedule them for now. */
1180 this->schedule_socket(input_socket);
1181 return;
1182 }
1183 const auto &multi_input_value = *std::get_if<MultiInputValue>(&socket_value->value);
1184 if (multi_input_value.values.is_empty()) {
1185 /* The input is empty, so use the fallback value. */
1186 this->store_socket_value_fallback(socket);
1187 return;
1188 }
1189
1190 Set<StringRef> existing_keys;
1191 auto joined_bundle = std::make_shared<BundleSocketValue>();
1192 for (const SocketValue &value : multi_input_value.values) {
1193 const auto *bundle_value = std::get_if<BundleSocketValuePtr>(&value.value);
1194 if (!bundle_value || !*bundle_value) {
1195 /* Ignore invalid values. */
1196 continue;
1197 }
1198 for (const BundleSocketValue::Item &item : (*bundle_value)->items) {
1199 if (existing_keys.add(item.key)) {
1200 joined_bundle->items.append(item);
1201 }
1202 }
1203 }
1204 this->store_socket_value(socket, {BundleSocketValuePtr{joined_bundle}});
1205 }
1206
1207 void handle_output_socket__menu_switch(const SocketInContext &socket)
1208 {
1209 const NodeInContext node = socket.owner_node();
1210 const auto &storage = *static_cast<const NodeMenuSwitch *>(node->storage);
1211
1212 const SocketInContext menu_input = node.input_socket(0);
1213 const SocketValue *menu_socket_value = value_by_socket_.lookup_ptr(menu_input);
1214 if (!menu_socket_value) {
1215 /* The menu value is not known yet, so schedule it for now. */
1216 this->schedule_socket(menu_input);
1217 return;
1218 }
1219
1220 const std::optional<PrimitiveSocketValue> menu_value_opt = menu_socket_value->to_primitive(
1221 *menu_input->typeinfo);
1222 if (!menu_value_opt) {
1223 /* This limitation may be lifted in the future. Menu Switch nodes could be supported natively
1224 * by render engines or we convert them to a bunch of mix nodes. */
1225 this->store_socket_value_fallback(socket);
1226 this->report_required_constant_input_or_backtrack(
1227 node, TIP_("Menu value has to be a constant value"));
1228 return;
1229 }
1230 const MenuValue menu_value = std::get<MenuValue>(menu_value_opt->value);
1231 /* Find the selected item index. */
1232 std::optional<int> selected_index;
1233 for (const int item_i : IndexRange(storage.enum_definition.items_num)) {
1234 const NodeEnumItem &item = storage.enum_definition.items_array[item_i];
1235 if (MenuValue(item.identifier) == menu_value) {
1236 selected_index = item_i;
1237 break;
1238 }
1239 }
1240 if (!selected_index.has_value()) {
1241 /* The input value does not exist in the menu. */
1242 this->store_socket_value_fallback(socket);
1243 return;
1244 }
1245 if (socket->index() == 0) {
1246 /* Handle forwarding the selected value. */
1247 this->forward_value_or_schedule(socket, node.input_socket(*selected_index + 1));
1248 return;
1249 }
1250 /* Set the value of the mask output. */
1251 const bool is_selected = selected_index == socket->index() - 1;
1252 this->store_socket_value_primitive(socket, is_selected);
1253 }
1254
1255 struct MixNodeInfo {
1256 bNode *node = nullptr;
1257 bNodeSocket *factor_in = nullptr;
1258 bNodeSocket *a_in = nullptr;
1259 bNodeSocket *b_in = nullptr;
1260 bNodeSocket *result_out = nullptr;
1261 };
1262
1263 MixNodeInfo create_mix_node(const eNodeSocketDatatype socket_type)
1264 {
1265 MixNodeInfo mix;
1266 switch (socket_type) {
1267 case SOCK_FLOAT:
1268 case SOCK_VECTOR:
1269 case SOCK_RGBA: {
1270 /* The ShaderNodeMix node uses different socket identifiers based on the data type, so find
1271 * the correct socket based on the name instead. */
1272 auto find_socket_by_name =
1273 [](bNode &node, const eNodeSocketInOut in_out, const StringRef name) -> bNodeSocket * {
1274 for (bNodeSocket &socket : in_out == SOCK_IN ? node.inputs : node.outputs) {
1275 if (socket.is_available()) {
1276 if (socket.name == name) {
1277 return &socket;
1278 }
1279 }
1280 }
1282 return nullptr;
1283 };
1284
1285 mix.node = this->add_node("ShaderNodeMix"_ustr);
1286 auto &mix_storage = *static_cast<NodeShaderMix *>(mix.node->storage);
1287 mix_storage.data_type = socket_type;
1288 mix_storage.clamp_result = false;
1289 /* Use clamping of the mix node to avoid the need for a separate clamp node. */
1290 mix_storage.clamp_factor = true;
1291 /* This makes the right sockets for the given data type available. */
1292 mix.node->typeinfo->updatefunc(&dst_tree_, mix.node);
1293 mix.factor_in = find_socket_by_name(*mix.node, SOCK_IN, "Factor");
1294 mix.a_in = find_socket_by_name(*mix.node, SOCK_IN, "A");
1295 mix.b_in = find_socket_by_name(*mix.node, SOCK_IN, "B");
1296 mix.result_out = find_socket_by_name(*mix.node, SOCK_OUT, "Result");
1297 break;
1298 }
1299 case SOCK_SHADER: {
1300 mix.node = this->add_node("ShaderNodeMixShader"_ustr);
1301 mix.factor_in = mix.node->inputs.first();
1302 mix.a_in = mix.factor_in->next;
1303 mix.b_in = mix.a_in->next;
1304 mix.result_out = mix.node->outputs.first();
1305 break;
1306 }
1307 default: {
1309 }
1310 }
1311 return mix;
1312 }
1313
1318 std::optional<eNodeSocketDatatype> get_internal_mix_socket_type(
1319 const eNodeSocketDatatype socket_type) const
1320 {
1321 switch (socket_type) {
1322 case SOCK_FLOAT:
1323 case SOCK_INT:
1324 case SOCK_BOOLEAN:
1325 return SOCK_FLOAT;
1326 case SOCK_VECTOR:
1327 return SOCK_VECTOR;
1328 case SOCK_RGBA:
1329 return SOCK_RGBA;
1330 case SOCK_SHADER:
1331 return SOCK_SHADER;
1332 default:
1333 return std::nullopt;
1334 }
1335 }
1336
1337 void handle_output_socket__index_switch(const SocketInContext &socket)
1338 {
1339 const NodeInContext node = socket.owner_node();
1340 const auto &storage = *static_cast<const NodeIndexSwitch *>(node->storage);
1341
1342 if (storage.items_num == 0) {
1343 this->store_socket_value_fallback(socket);
1344 return;
1345 }
1346
1347 const SocketInContext index_input = node.input_socket(0);
1348 const SocketValue *index_input_value = value_by_socket_.lookup_ptr(index_input);
1349 if (!index_input_value) {
1350 /* The index is not known yet, so schedule it for now. */
1351 this->schedule_socket(index_input);
1352 return;
1353 }
1354
1355 if (const std::optional<PrimitiveSocketValue> primitive_index_value_opt =
1356 index_input_value->to_primitive(*index_input->typeinfo))
1357 {
1358 const int index = std::get<int>(primitive_index_value_opt->value);
1359 if (index < 0 || index >= storage.items_num) {
1360 this->store_socket_value_fallback(socket);
1361 this->report_error(node,
1362 fmt::format("{}: {}", TIP_("Index out of range"), index),
1364 return;
1365 }
1366 this->forward_value_or_schedule(socket, node.input_socket(index + 1));
1367 return;
1368 }
1369
1370 /* If the index is not a constant value, we imitate the index switch node using a chain of
1371 * mix nodes. This allows renderers using the Index Switch node with rendering backends which
1372 * don't support it natively. */
1373 const std::optional<eNodeSocketDatatype> internal_mix_type =
1374 this->get_internal_mix_socket_type(storage.data_type);
1375 if (!internal_mix_type) {
1376 this->report_required_constant_input_or_backtrack(
1377 node, TIP_("Index must be a constant value for this data type"));
1378 this->store_socket_value_fallback(socket);
1379 return;
1380 }
1381
1382 const EnsureInputsResult ensured_inputs = this->ensure_node_inputs(node);
1383 if (ensured_inputs.has_missing_inputs) {
1384 /* Wait until all inputs values are available. */
1385 return;
1386 }
1387
1388 /* Use a truncate node to turn the input value into an int. */
1389 bNode &truncate_math_node = *this->add_node("ShaderNodeMath"_ustr);
1390 truncate_math_node.custom1 = NODE_MATH_TRUNC;
1391 bNodeSocket &truncate_input = *truncate_math_node.inputs.first();
1392 bNodeSocket &truncate_output = *truncate_math_node.outputs.first();
1393 this->set_input_socket_value(node, truncate_math_node, truncate_input, *index_input_value);
1394
1395 bNode *prev_mix = nullptr;
1396 bNodeSocket *prev_mix_result = nullptr;
1397 for (const int i : IndexRange(storage.items_num + 1)) {
1398 /* Use a math node to turn the index into a factor for the mix node. */
1399 const int index_to_factor_offset = 1 - i;
1400
1401 bNode *factor_node = nullptr;
1402 bNodeSocket *factor_out = nullptr;
1403 if (index_to_factor_offset == 0) {
1404 /* No need to add an extra math node here that just computes +0. */
1405 factor_node = &truncate_math_node;
1406 factor_out = &truncate_output;
1407 }
1408 else {
1409 bNode &add_math_node = *this->add_node("ShaderNodeMath"_ustr);
1410 add_math_node.custom1 = NODE_MATH_ADD;
1411 bNodeSocket &add_in_1 = *add_math_node.inputs.first();
1412 bNodeSocket &add_in_2 = *add_in_1.next;
1414 dst_tree_, truncate_math_node, truncate_output, add_math_node, add_in_1);
1415 static_cast<bNodeSocketValueFloat *>(add_in_2.default_value)->value =
1416 index_to_factor_offset;
1417 factor_node = &add_math_node;
1418 factor_out = add_math_node.outputs.first();
1419 }
1420
1421 const MixNodeInfo mix = this->create_mix_node(*internal_mix_type);
1422 bke::node_add_link(dst_tree_, *factor_node, *factor_out, *mix.node, *mix.factor_in);
1423 if (i == 0) {
1424 this->set_input_socket_value(node, *mix.node, *mix.a_in, {FallbackValue{}});
1425 }
1426 else {
1427 bke::node_add_link(dst_tree_, *prev_mix, *prev_mix_result, *mix.node, *mix.a_in);
1428 }
1429 if (i < storage.items_num) {
1430 const SocketInContext input_socket = node.input_socket(i + 1);
1431 const SocketValue &input_value = value_by_socket_.lookup(input_socket);
1432 const SocketValue converted_value = this->handle_implicit_conversion(
1433 input_value, *input_socket->typeinfo, *mix.b_in->typeinfo);
1434 this->set_input_socket_value(node, *mix.node, *mix.b_in, converted_value);
1435 }
1436 else {
1437 this->set_input_socket_value(node, *mix.node, *mix.b_in, {FallbackValue{}});
1438 }
1439
1440 prev_mix = mix.node;
1441 prev_mix_result = mix.result_out;
1442 }
1443
1444 this->store_socket_value(socket, {LinkedSocketValue{prev_mix, prev_mix_result}});
1445 }
1446
1447 void handle_output_socket__switch(const SocketInContext &socket)
1448 {
1449 const NodeInContext node = socket.owner_node();
1450 const auto &storage = *static_cast<const NodeSwitch *>(node->storage);
1451
1452 const SocketInContext switch_input = node.input_socket(0);
1453 const SocketValue *switch_input_value = value_by_socket_.lookup_ptr(switch_input);
1454 if (!switch_input_value) {
1455 /* The switch condition is not known yet, so schedule it for now. */
1456 this->schedule_socket(switch_input);
1457 return;
1458 }
1459
1460 if (const std::optional<PrimitiveSocketValue> primitive_switch_value_opt =
1461 switch_input_value->to_primitive(*switch_input->typeinfo))
1462 {
1463 const bool condition = std::get<bool>(primitive_switch_value_opt->value);
1464 this->forward_value_or_schedule(socket, node.input_socket(condition ? 2 : 1));
1465 return;
1466 }
1467
1468 /* If the switch condition is not a constant value, imitate the Switch node using a mix node.
1469 * This allows using the Switch node with rendering backends which don't support it natively.
1470 */
1471 const std::optional<eNodeSocketDatatype> internal_mix_type =
1472 this->get_internal_mix_socket_type(storage.input_type);
1473 if (!internal_mix_type) {
1474 this->report_required_constant_input_or_backtrack(
1475 node, TIP_("Switch must be a constant value for this data type"));
1476 this->store_socket_value_fallback(socket);
1477 return;
1478 }
1479
1480 const EnsureInputsResult ensured_inputs = this->ensure_node_inputs(node);
1481 if (ensured_inputs.has_missing_inputs) {
1482 /* Wait until all inputs values are available. */
1483 return;
1484 }
1485
1486 const MixNodeInfo mix = this->create_mix_node(*internal_mix_type);
1487 this->set_input_socket_value(node,
1488 *mix.node,
1489 *mix.factor_in,
1490 this->handle_implicit_conversion(*switch_input_value,
1491 *switch_input->typeinfo,
1492 *mix.factor_in->typeinfo));
1493
1494 const SocketInContext false_input = node.input_socket(1);
1495 const SocketInContext true_input = node.input_socket(2);
1496 const SocketValue &false_value = value_by_socket_.lookup(false_input);
1497 const SocketValue &true_value = value_by_socket_.lookup(true_input);
1498 this->set_input_socket_value(node,
1499 *mix.node,
1500 *mix.a_in,
1501 this->handle_implicit_conversion(
1502 false_value, *false_input->typeinfo, *mix.a_in->typeinfo));
1503 this->set_input_socket_value(
1504 node,
1505 *mix.node,
1506 *mix.b_in,
1507 this->handle_implicit_conversion(true_value, *true_input->typeinfo, *mix.b_in->typeinfo));
1508
1509 this->store_socket_value(socket, {LinkedSocketValue{mix.node, mix.result_out}});
1510 }
1511
1512 void handle_output_socket__input_menu(const SocketInContext &socket)
1513 {
1514 const NodeInContext node = socket.owner_node();
1515 const auto &storage = *static_cast<const NodeInputMenu *>(node->storage);
1516 SocketInContext output_socket = node.output_socket(0);
1517 this->store_socket_value(output_socket,
1518 {PrimitiveSocketValue::from_value(
1519 {output_socket->typeinfo->base_cpp_type, &storage.value})});
1520 }
1521
1522 void handle_output_socket__implicit_conversion(const SocketInContext &socket)
1523 {
1524 const NodeInContext node = socket.owner_node();
1525
1526 const SocketInContext input_socket = node.input_socket(0);
1527 const SocketValue *socket_value = value_by_socket_.lookup_ptr(input_socket);
1528 if (!socket_value) {
1529 /* The input bundle is not known yet, so schedule it for now. */
1530 this->schedule_socket(input_socket);
1531 return;
1532 }
1533 const SocketValue converted_value = this->handle_implicit_conversion(
1534 *socket_value, *socket->typeinfo, *socket->typeinfo);
1535 this->store_socket_value(socket, converted_value);
1536 }
1537
1538 void handle_output_socket__warning(const SocketInContext &socket)
1539 {
1540 const NodeInContext node = socket.owner_node();
1541
1542 const SocketInContext show_input_socket = node.input_socket(0);
1543 const SocketValue *show_value = value_by_socket_.lookup_ptr(show_input_socket);
1544 if (!show_value) {
1545 /* Wait until the value is available. */
1546 this->schedule_socket(show_input_socket);
1547 return;
1548 }
1549 const std::optional<PrimitiveSocketValue> primitive_show = show_value->to_primitive(
1550 *show_input_socket->typeinfo);
1551 if (!primitive_show) {
1552 this->report_error(
1553 node, TIP_("Warnings only work when inputs are constant values"), NodeWarningType::Info);
1554 this->store_socket_value_fallback(socket);
1555 return;
1556 }
1557 const bool show = std::get<bool>(primitive_show->value);
1558 if (show) {
1559 const SocketInContext message_socket = node.input_socket(1);
1560 const SocketValue *message_value = value_by_socket_.lookup_ptr(message_socket);
1561 if (!message_value) {
1562 /* Wait until the value is available. */
1563 this->schedule_socket(message_socket);
1564 return;
1565 }
1566 if (const std::optional<PrimitiveSocketValue> primitive_message =
1567 message_value->to_primitive(*message_socket->typeinfo))
1568 {
1569 const NodeWarningType type = NodeWarningType(node->custom1);
1570 const StringRef message = std::get<std::string>(primitive_message->value);
1571 this->report_error(node, message, type);
1572 }
1573 else {
1574 this->report_error(node,
1575 TIP_("Warnings only work when inputs are constant values"),
1577 }
1578 }
1579 this->forward_value_or_schedule(socket, show_input_socket);
1580 }
1581
1582 void handle_output_socket__enable_output(const SocketInContext &socket)
1583 {
1584 const NodeInContext node = socket.owner_node();
1585 const SocketInContext enable_socket = node.input_socket(0);
1586 const SocketInContext value_input_socket = node.input_socket(1);
1587
1588 const SocketValue *enable_value = value_by_socket_.lookup_ptr(enable_socket);
1589 if (!enable_value) {
1590 /* Wait until the value is available. */
1591 this->schedule_socket(enable_socket);
1592 return;
1593 }
1594 const std::optional<PrimitiveSocketValue> enable_primitive = enable_value->to_primitive(
1595 *enable_socket->typeinfo);
1596 if (enable_primitive.has_value()) {
1597 const bool enable = std::get<bool>(enable_primitive->value);
1598 if (enable) {
1599 this->forward_value_or_schedule(socket, value_input_socket);
1600 return;
1601 }
1602 this->store_socket_value_fallback(socket);
1603 return;
1604 }
1605 const std::optional<eNodeSocketDatatype> internal_mix_type =
1606 this->get_internal_mix_socket_type(value_input_socket->type);
1607 if (!internal_mix_type) {
1608 this->report_required_constant_input_or_backtrack(
1609 node, TIP_("Enable input must be a constant value for this data type"));
1610 this->store_socket_value_fallback(socket);
1611 return;
1612 }
1613 const SocketValue *value = value_by_socket_.lookup_ptr(value_input_socket);
1614 if (!value) {
1615 /* Wait until the value is available. */
1616 this->schedule_socket(value_input_socket);
1617 return;
1618 }
1619 const MixNodeInfo mix = this->create_mix_node(*internal_mix_type);
1620 this->set_input_socket_value(node,
1621 *mix.node,
1622 *mix.factor_in,
1623 this->handle_implicit_conversion(*enable_value,
1624 *enable_socket->typeinfo,
1625 *mix.factor_in->typeinfo));
1626 this->set_input_socket_value(node, *mix.node, *mix.a_in, {FallbackValue{}});
1627 this->set_input_socket_value(node,
1628 *mix.node,
1629 *mix.b_in,
1630 this->handle_implicit_conversion(
1631 *value, *value_input_socket->typeinfo, *mix.b_in->typeinfo));
1632 this->store_socket_value(socket, {LinkedSocketValue{mix.node, mix.result_out}});
1633 }
1634
1640 void schedule_side_effect_nodes_in_context(const bNodeTree &tree,
1641 const bke::bNodeTreeZone *zone,
1642 const ComputeContext *context)
1643 {
1644 if (!handled_side_effect_contexts_.add(context)) {
1645 return;
1646 }
1647 const bke::bNodeTreeZones *zones = tree.zones();
1648 if (!zones) {
1649 return;
1650 }
1651 /* Schedule warning nodes in this compute context. */
1652 for (const bNode *warning_node : tree.nodes_by_type("GeometryNodeWarning"_ustr)) {
1653 if (warning_node->is_muted()) {
1654 continue;
1655 }
1656 const bke::bNodeTreeZone *warning_zone = zones->get_zone_by_node(warning_node->identifier);
1657 if (warning_zone == zone) {
1658 SocketInContext warning_output_socket{context, &warning_node->output_socket(0)};
1659 if (!warning_output_socket->is_directly_linked()) {
1660 /* Evaluate the warning node in this context even if it's not otherwise used. */
1661 this->schedule_socket(warning_output_socket);
1662 }
1663 }
1664 }
1665 }
1666
1671 void handle_output_socket__eval(const SocketInContext &socket)
1672 {
1673 const NodeInContext node = socket.owner_node();
1674 const EnsureInputsResult ensured_inputs = this->ensure_node_inputs(node);
1675 if (ensured_inputs.has_missing_inputs) {
1676 /* The node can only be evaluated if all inputs values are known. */
1677 return;
1678 }
1679 const bke::bNodeType &node_type = *node->typeinfo;
1680 if (node_type.build_multi_function && ensured_inputs.all_inputs_primitive) {
1681 /* Do constant folding. */
1682 this->handle_output_socket__eval_multi_function(node);
1683 return;
1684 }
1685 /* Check if this node is supported by the renderer. If not, either try inlining it harder to
1686 * eliminate the node, or report an error and skip it. */
1687 if (this->is_inliner_evaluation_node(*node)) {
1688 this->report_required_constant_input_or_backtrack(
1689 node, TIP_("Node requires constant input values"));
1690 this->store_socket_value_fallback(socket);
1691 return;
1692 }
1693 if (this->try_short_circuit(node)) {
1694 return;
1695 }
1696 /* The node can't be constant-folded. So copy it to the destination tree instead. */
1697 this->handle_output_socket__eval_copy_node(*node, node.context, node.context);
1698 }
1699
1700 [[nodiscard]] bool try_short_circuit(const NodeInContext &node)
1701 {
1702 if (node->is_type("FunctionNodeBooleanMath"_ustr)) {
1703 return this->try_short_circuit__boolean_math(node);
1704 }
1705 if (node->is_type("ShaderNodeMath"_ustr)) {
1706 return this->try_short_circuit__float_math(node);
1707 }
1708 if (node->is_type("FunctionNodeIntegerMath"_ustr)) {
1709 return this->try_short_circuit__integer_math(node);
1710 }
1711 if (node->is_type("ShaderNodeVectorMath"_ustr)) {
1712 return this->try_short_circuit__vector_math(node);
1713 }
1714 if (node->is_type("ShaderNodeMixShader"_ustr)) {
1715 return this->try_short_circuit__mix_shader(node);
1716 }
1717 return false;
1718 }
1719
1720 [[nodiscard]] bool try_short_circuit__boolean_math(const NodeInContext &node)
1721 {
1722 const auto op = NodeBooleanMathOperation(node->custom1);
1723 const SocketInContext output_socket = node.output_socket(0);
1724
1725 switch (op) {
1726 case NODE_BOOLEAN_MATH_AND: {
1727 const std::optional<bool> a = this->try_get_input<bool>(node, "Boolean"_ustr);
1728 const std::optional<bool> b = this->try_get_input<bool>(node, "Boolean_001"_ustr);
1729 if (a == false || b == false) {
1730 this->store_socket_value_primitive(output_socket, false);
1731 return true;
1732 }
1733 if (a == true) {
1734 this->store_socket_value(output_socket, this->get_input_value(node, "Boolean_001"_ustr));
1735 return true;
1736 }
1737 if (b == true) {
1738 this->store_socket_value(output_socket, this->get_input_value(node, "Boolean"_ustr));
1739 return true;
1740 }
1741 break;
1742 }
1743 case NODE_BOOLEAN_MATH_OR: {
1744 const std::optional<bool> a = this->try_get_input<bool>(node, "Boolean"_ustr);
1745 const std::optional<bool> b = this->try_get_input<bool>(node, "Boolean_001"_ustr);
1746 if (a == true || b == true) {
1747 this->store_socket_value_primitive(output_socket, true);
1748 return true;
1749 }
1750 if (a == false) {
1751 this->store_socket_value(output_socket, this->get_input_value(node, "Boolean_001"_ustr));
1752 return true;
1753 }
1754 if (b == false) {
1755 this->store_socket_value(output_socket, this->get_input_value(node, "Boolean"_ustr));
1756 return true;
1757 }
1758 break;
1759 }
1761 const std::optional<bool> a = this->try_get_input<bool>(node, "Boolean"_ustr);
1762 const std::optional<bool> b = this->try_get_input<bool>(node, "Boolean_001"_ustr);
1763 if (a == false || b == false) {
1764 this->store_socket_value_primitive(output_socket, true);
1765 return true;
1766 }
1767 if (a == true) {
1768 this->store_socket_value(
1769 output_socket,
1770 this->invert_bool_value(node, this->get_input_value(node, "Boolean_001"_ustr)));
1771 return true;
1772 }
1773 if (b == true) {
1774 this->store_socket_value(
1775 output_socket,
1776 this->invert_bool_value(node, this->get_input_value(node, "Boolean"_ustr)));
1777 return true;
1778 }
1779 break;
1780 }
1781 case NODE_BOOLEAN_MATH_NOR: {
1782 const std::optional<bool> a = this->try_get_input<bool>(node, "Boolean"_ustr);
1783 const std::optional<bool> b = this->try_get_input<bool>(node, "Boolean_001"_ustr);
1784 if (a == true || b == true) {
1785 this->store_socket_value_primitive(output_socket, false);
1786 return true;
1787 }
1788 if (a == false) {
1789 this->store_socket_value(
1790 output_socket,
1791 this->invert_bool_value(node, this->get_input_value(node, "Boolean_001"_ustr)));
1792 return true;
1793 }
1794 if (b == false) {
1795 this->store_socket_value(
1796 output_socket,
1797 this->invert_bool_value(node, this->get_input_value(node, "Boolean"_ustr)));
1798 return true;
1799 }
1800 break;
1801 }
1803 const std::optional<bool> a = this->try_get_input<bool>(node, "Boolean"_ustr);
1804 const std::optional<bool> b = this->try_get_input<bool>(node, "Boolean_001"_ustr);
1805 if (a == false || b == true) {
1806 this->store_socket_value_primitive(output_socket, true);
1807 return true;
1808 }
1809 if (a == true) {
1810 this->store_socket_value(output_socket, this->get_input_value(node, "Boolean_001"_ustr));
1811 return true;
1812 }
1813 if (b == false) {
1814 this->store_socket_value(
1815 output_socket,
1816 this->invert_bool_value(node, this->get_input_value(node, "Boolean"_ustr)));
1817 return true;
1818 }
1819 break;
1820 }
1822 const std::optional<bool> a = this->try_get_input<bool>(node, "Boolean"_ustr);
1823 const std::optional<bool> b = this->try_get_input<bool>(node, "Boolean_001"_ustr);
1824 if (a == false || b == true) {
1825 this->store_socket_value_primitive(output_socket, false);
1826 return true;
1827 }
1828 if (a == true) {
1829 this->store_socket_value(
1830 output_socket,
1831 this->invert_bool_value(node, this->get_input_value(node, "Boolean_001"_ustr)));
1832 return true;
1833 }
1834 if (b == false) {
1835 this->store_socket_value(output_socket, this->get_input_value(node, "Boolean"_ustr));
1836 return true;
1837 }
1838 break;
1839 }
1843 break;
1844 }
1845
1846 return false;
1847 }
1848
1849 SocketValue invert_bool_value(const NodeInContext &node, const SocketValue &value)
1850 {
1851 static bke::bNodeSocketType &bool_type = *bke::node_socket_type_find_static(SOCK_BOOLEAN);
1852 if (const std::optional<PrimitiveSocketValue> primitive = value.to_primitive(bool_type)) {
1853 return {PrimitiveSocketValue{!std::get<bool>(primitive->value)}};
1854 }
1855 bNode *not_node = this->add_node("FunctionNodeBooleanMath"_ustr);
1856 not_node->custom1 = NODE_BOOLEAN_MATH_NOT;
1857 this->set_input_socket_value(node, *not_node, *not_node->inputs.first(), value);
1858 return {LinkedSocketValue{not_node, not_node->outputs.first()}};
1859 }
1860
1861 [[nodiscard]] bool try_short_circuit__float_math(const NodeInContext &node)
1862 {
1863 const SocketInContext output_socket = node.output_socket(0);
1864 const auto op = NodeMathOperation(node->custom1);
1865
1866 switch (op) {
1867 case NODE_MATH_MULTIPLY:
1868 case NODE_MATH_DIVIDE: {
1869 const std::optional<float> a = this->try_get_input<float>(node, "Value"_ustr);
1870 const std::optional<float> b = this->try_get_input<float>(node, "Value_001"_ustr);
1871 if (a == 0.0f || b == 0.0f) {
1872 this->store_socket_value_primitive(output_socket, 0.0f);
1873 return true;
1874 }
1875 break;
1876 }
1877 default:
1878 break;
1879 }
1880
1881 return false;
1882 }
1883
1884 [[nodiscard]] bool try_short_circuit__integer_math(const NodeInContext &node)
1885 {
1886 const SocketInContext output_socket = node.output_socket(0);
1887 const auto op = NodeIntegerMathOperation(node->custom1);
1888
1889 switch (op) {
1892 const std::optional<int> a = this->try_get_input<int>(node, "Value"_ustr);
1893 const std::optional<int> b = this->try_get_input<int>(node, "Value_001"_ustr);
1894 if (a == 0 || b == 0) {
1895 this->store_socket_value_primitive(output_socket, 0);
1896 return true;
1897 }
1898 break;
1899 }
1900 default:
1901 break;
1902 }
1903
1904 return false;
1905 }
1906
1907 [[nodiscard]] bool try_short_circuit__vector_math(const NodeInContext &node)
1908 {
1909 const auto op = NodeVectorMathOperation(node->custom1);
1910
1911 auto output_socket = [&](const UString identifier) -> SocketInContext {
1912 return {node.context, node->output_by_identifier(identifier)};
1913 };
1914
1915 switch (op) {
1919 const std::optional<float3> a = this->try_get_input<float3>(node, "Vector"_ustr);
1920 const std::optional<float3> b = this->try_get_input<float3>(node, "Vector_001"_ustr);
1921 if (a == float3(0.0f) || b == float3(0.0f)) {
1922 this->store_socket_value_primitive(output_socket("Vector"_ustr), float3(0.0f));
1923 return true;
1924 }
1925 break;
1926 }
1928 const std::optional<float3> a = this->try_get_input<float3>(node, "Vector"_ustr);
1929 const std::optional<float3> b = this->try_get_input<float3>(node, "Vector_001"_ustr);
1930 if (a == float3(0.0f) || b == float3(0.0f)) {
1931 this->store_socket_value_primitive(output_socket("Value"_ustr), 0.0f);
1932 return true;
1933 }
1934 break;
1935 }
1937 const std::optional<float3> a = this->try_get_input<float3>(node, "Vector"_ustr);
1938 const std::optional<float> scale = this->try_get_input<float>(node, "Scale"_ustr);
1939 if (a == float3(0.0f) || scale == 0.0f) {
1940 this->store_socket_value_primitive(output_socket("Vector"_ustr), float3(0.0f));
1941 return true;
1942 }
1943 break;
1944 }
1945 default:
1946 break;
1947 }
1948
1949 return false;
1950 }
1951
1952 [[nodiscard]] bool try_short_circuit__mix_shader(const NodeInContext &node)
1953 {
1954 const SocketInContext output_socket = node.output_socket(0);
1955 const std::optional<float> factor = this->try_get_input<float>(node, "Fac"_ustr);
1956 if (factor == 0.0f) {
1957 this->store_socket_value(output_socket, this->get_input_value(node, "Shader"_ustr));
1958 return true;
1959 }
1960 if (factor == 1.0f) {
1961 this->store_socket_value(output_socket, this->get_input_value(node, "Shader_001"_ustr));
1962 return true;
1963 }
1964 return false;
1965 }
1966
1967 template<typename T>
1968 std::optional<T> try_get_input(const NodeInContext &node, const bNodeSocket &socket)
1969 {
1970 const SocketValue &value = this->get_input_value(node, socket);
1971 if (const std::optional<PrimitiveSocketValue> primitive = value.to_primitive(*socket.typeinfo))
1972 {
1973 return std::get<T>(primitive->value);
1974 }
1975 return std::nullopt;
1976 }
1977
1978 template<typename T>
1979 std::optional<T> try_get_input(const NodeInContext &node, const UString identifier)
1980 {
1981 return this->try_get_input<T>(node, *node->input_by_identifier(identifier));
1982 }
1983
1984 SocketValue get_input_value(const NodeInContext &node, const bNodeSocket &socket)
1985 {
1986 return value_by_socket_.lookup({node.context, &socket});
1987 }
1988
1989 SocketValue get_input_value(const NodeInContext &node, const UString identifier)
1990 {
1991 return this->get_input_value(node, *node->input_by_identifier(identifier));
1992 }
1993
2002 void report_required_constant_input_or_backtrack(const NodeInContext node,
2003 const StringRef message)
2004 {
2005 /* Before reporting an error, attempt to inline a surrounding repeat zone. */
2006 for (const ComputeContext *ctx = node.context; ctx; ctx = ctx->parent()) {
2007 if (const auto *repeat_zone_ctx = dynamic_cast<const bke::RepeatZoneComputeContext *>(ctx)) {
2008 if (repeat_zone_ctx->iteration() == preserved_repeat_zone_iteration) {
2009 repeat_zones_to_force_inline_.add(
2010 {repeat_zone_ctx->parent(), repeat_zone_ctx->output_node_id()});
2011 return;
2012 }
2013 }
2014 }
2015 this->report_error(node, message, NodeWarningType::Error);
2016 }
2017
2018 struct EnsureInputsResult {
2019 bool has_missing_inputs = false;
2020 bool all_inputs_primitive = false;
2021 };
2022
2023 EnsureInputsResult ensure_node_inputs(const NodeInContext &node)
2024 {
2025 EnsureInputsResult result;
2026 result.has_missing_inputs = false;
2027 result.all_inputs_primitive = true;
2028 for (const bNodeSocket *input_socket : node->input_sockets()) {
2029 if (this->socket_is_ignored(*input_socket)) {
2030 continue;
2031 }
2032 const SocketInContext input_socket_ctx = {node.context, input_socket};
2033 const SocketValue *value = value_by_socket_.lookup_ptr(input_socket_ctx);
2034 if (!value) {
2035 this->schedule_socket(input_socket_ctx);
2036 result.has_missing_inputs = true;
2037 continue;
2038 }
2039 if (!value->to_primitive(*input_socket->typeinfo)) {
2040 result.all_inputs_primitive = false;
2041 }
2042 }
2043 return result;
2044 }
2045
2046 void handle_output_socket__eval_multi_function(const NodeInContext &node)
2047 {
2048 NodeMultiFunctionBuilder builder{*node.node, node->owner_tree()};
2049 node->typeinfo->build_multi_function(builder);
2050 const mf::MultiFunction &fn = builder.function();
2051 mf::ContextBuilder context;
2052 ShaderNodesMultiFunctionUserData user_data;
2053 context.user_data(&user_data);
2054 IndexMask mask(1);
2055 mf::ParamsBuilder params{fn, &mask};
2056
2057 /* Prepare inputs to the multi-function evaluation. */
2058 for (const bNodeSocket *input_socket : node->input_sockets()) {
2059 if (this->socket_is_ignored(*input_socket)) {
2060 continue;
2061 }
2062 const SocketInContext input_socket_ctx = {node.context, input_socket};
2063 const PrimitiveSocketValue value =
2064 *value_by_socket_.lookup(input_socket_ctx).to_primitive(*input_socket->typeinfo);
2065 params.add_readonly_single_input(
2066 GVArray::from_single(*input_socket->typeinfo->base_cpp_type, 1, value.buffer()));
2067 }
2068
2069 /* Prepare output buffers. */
2070 Vector<void *> output_values;
2071 for (const bNodeSocket *output_socket : node->output_sockets()) {
2072 if (this->socket_is_ignored(*output_socket)) {
2073 continue;
2074 }
2075 void *value = scope_.allocate_owned(*output_socket->typeinfo->base_cpp_type);
2076 output_values.append(value);
2077 params.add_uninitialized_single_output(
2078 GMutableSpan(output_socket->typeinfo->base_cpp_type, value, 1));
2079 }
2080
2081 fn.call(mask, params, context);
2082
2083 /* Store constant-folded values for the output sockets. */
2084 int current_output_i = 0;
2085 for (const bNodeSocket *output_socket : node->output_sockets()) {
2086 if (this->socket_is_ignored(*output_socket)) {
2087 continue;
2088 }
2089 const void *value = output_values[current_output_i++];
2090 this->store_socket_value(
2091 {node.context, output_socket},
2092 {PrimitiveSocketValue::from_value({output_socket->typeinfo->base_cpp_type, value})});
2093 }
2094
2095 for (const NodeWarning &warning : user_data.warnings) {
2096 this->report_error(node, warning.message, warning.type);
2097 }
2098 }
2099
2100 bNode &handle_output_socket__eval_copy_node(const bNode &node,
2101 const ComputeContext *in_context,
2102 const ComputeContext *out_context)
2103 {
2104 Map<const bNodeSocket *, bNodeSocket *> socket_map;
2105 /* We generate our own identifier and name here to get unique values without having to scan all
2106 * already existing nodes. */
2107 const int identifier = this->get_next_node_identifier();
2108 const std::string unique_name = fmt::format("{}_{}", identifier, node.name);
2109 bNode &copied_node = *bke::node_copy_with_mapping(
2110 &dst_tree_,
2111 node,
2112 this->node_copy_flag(),
2113 unique_name.size() < sizeof(bNode::name) ? std::make_optional<StringRefNull>(unique_name) :
2114 std::nullopt,
2115 identifier,
2116 socket_map);
2117
2118 /* Clear the parent frame pointer, because it does not exist in the destination tree. */
2119 copied_node.parent = nullptr;
2120
2121 /* Setup input sockets for the copied node. */
2122 for (const bNodeSocket *src_input_socket : node.input_sockets()) {
2123 if (this->socket_is_ignored(*src_input_socket)) {
2124 continue;
2125 }
2126 bNodeSocket &dst_input_socket = *socket_map.lookup(src_input_socket);
2127 const SocketInContext input_socket_ctx = {in_context, src_input_socket};
2128 const SocketValue &value = value_by_socket_.lookup(input_socket_ctx);
2129 this->set_input_socket_value({in_context, &node}, copied_node, dst_input_socket, value);
2130 }
2131 for (const bNodeSocket *src_output_socket : node.output_sockets()) {
2132 if (this->socket_is_ignored(*src_output_socket)) {
2133 continue;
2134 }
2135 bNodeSocket &dst_output_socket = *socket_map.lookup(src_output_socket);
2136 const SocketInContext output_socket_ctx = {out_context, src_output_socket};
2137 this->store_socket_value(output_socket_ctx,
2138 {LinkedSocketValue{&copied_node, &dst_output_socket}});
2139 }
2140 return copied_node;
2141 }
2142
2144 SocketValue handle_implicit_conversion(const SocketValue &src_value,
2145 const bke::bNodeSocketType &from_socket_type,
2146 const bke::bNodeSocketType &to_socket_type)
2147 {
2148 if (from_socket_type.type == to_socket_type.type) {
2149 return src_value;
2150 }
2151 if (std::get_if<LinkedSocketValue>(&src_value.value)) {
2152 if (to_socket_type.type == SOCK_BOOLEAN) {
2153 if (ELEM(from_socket_type.type, SOCK_FLOAT, SOCK_INT, SOCK_RGBA)) {
2154 /* The same to-bool conversion happens to work for integer and color sockets too. */
2155 return {this->insert_float_to_bool_conversion(src_value)};
2156 }
2157 if (from_socket_type.type == SOCK_VECTOR) {
2158 return {this->insert_vector_to_bool_conversion(src_value)};
2159 }
2160 }
2161 return src_value;
2162 }
2163 if (std::get_if<DanglingValue>(&src_value.value)) {
2164 return src_value;
2165 }
2166 const std::optional<PrimitiveSocketValue> src_primitive_value = src_value.to_primitive(
2167 from_socket_type);
2168 if (src_primitive_value && to_socket_type.base_cpp_type) {
2169 if (data_type_conversions_.is_convertible(*from_socket_type.base_cpp_type,
2170 *to_socket_type.base_cpp_type))
2171 {
2172 const void *src_buffer = src_primitive_value->buffer();
2173 BUFFER_FOR_CPP_TYPE_VALUE(*to_socket_type.base_cpp_type, dst_buffer);
2174 data_type_conversions_.convert_to_uninitialized(*from_socket_type.base_cpp_type,
2175 *to_socket_type.base_cpp_type,
2176 src_buffer,
2177 dst_buffer);
2178 return {
2179 PrimitiveSocketValue::from_value(GPointer{to_socket_type.base_cpp_type, dst_buffer})};
2180 }
2181 }
2182 if (src_primitive_value && to_socket_type.type == SOCK_SHADER) {
2183 const CPPType &from_cpp_type = *from_socket_type.base_cpp_type;
2184 const CPPType &to_cpp_type = CPPType::get<ColorGeometry4f>();
2185 if (from_cpp_type == to_cpp_type ||
2186 data_type_conversions_.is_convertible(from_cpp_type, to_cpp_type))
2187 {
2188 /* Insert a Color node when converting a primitive value to a shader. */
2189 bNode *color_node = this->add_node("ShaderNodeRGB"_ustr);
2190 const void *src_buffer = src_primitive_value->buffer();
2191 ColorGeometry4f color;
2192 data_type_conversions_.convert_to_uninitialized(
2193 from_cpp_type, to_cpp_type, src_buffer, &color);
2194 bNodeSocket *output_socket = color_node->outputs.first();
2195 auto *socket_storage = static_cast<bNodeSocketValueRGBA *>(output_socket->default_value);
2196 copy_v3_v3(socket_storage->value, color);
2197 socket_storage->value[3] = 1.0f;
2198 return {LinkedSocketValue{color_node, output_socket}};
2199 }
2200 }
2201
2202 return SocketValue{FallbackValue{}};
2203 }
2204
2210 LinkedSocketValue insert_float_to_bool_conversion(const SocketValue &src_value)
2211 {
2212 bNode &math_node = *this->add_node("ShaderNodeMath"_ustr);
2213 math_node.custom1 = NODE_MATH_GREATER_THAN;
2214 bNodeSocket &value_in = *math_node.inputs.first();
2215 bNodeSocket &threshold_in = *value_in.next;
2216 bNodeSocket &value_out = *math_node.outputs.first();
2217 this->set_input_socket_value({}, math_node, value_in, src_value);
2218 static_cast<bNodeSocketValueFloat *>(threshold_in.default_value)->value = 0.0f;
2219 return {&math_node, &value_out};
2220 }
2221
2223 LinkedSocketValue insert_vector_to_bool_conversion(const SocketValue &src_value)
2224 {
2225 bNode &length_node = *this->add_node("ShaderNodeVectorMath"_ustr);
2226 length_node.custom1 = NODE_VECTOR_MATH_LENGTH;
2227 length_node.typeinfo->updatefunc(&dst_tree_, &length_node);
2228 bNodeSocket &vector_in = *length_node.inputs.first();
2229 bNodeSocket &length_out = *bke::node_find_socket(length_node, SOCK_OUT, "Value"_ustr);
2230 this->set_input_socket_value({}, length_node, vector_in, src_value);
2231 return this->insert_float_to_bool_conversion({LinkedSocketValue{&length_node, &length_out}});
2232 }
2233
2234 void set_input_socket_value(const NodeInContext &original_node,
2235 bNode &dst_node,
2236 bNodeSocket &dst_socket,
2237 const SocketValue &value)
2238 {
2239 BLI_assert(dst_socket.is_input());
2240 if (dst_socket.flag & SOCK_HIDE_VALUE) {
2241 if (const auto *input_socket_value = std::get_if<InputSocketValue>(&value.value)) {
2242 if (input_socket_value->socket->flag & SOCK_HIDE_VALUE) {
2243 /* Don't add a value or link of the source and destination sockets don't have a value. */
2244 return;
2245 }
2246 }
2247 }
2248 if (const std::optional<PrimitiveSocketValue> primitive_value = value.to_primitive(
2249 *dst_socket.typeinfo))
2250 {
2251 if (dst_socket.flag & SOCK_HIDE_VALUE) {
2252 /* Can't store the primitive value directly on the socket. So create a new input node and
2253 * link it instead. */
2254 const NodeAndSocket node_and_socket = this->try_primitive_value_to_output_socket(
2255 *primitive_value);
2256 if (!node_and_socket.socket) {
2257 /* There is no input node for this value type, so store it on the socket after all. */
2258 this->set_primitive_value_on_socket(dst_socket, *primitive_value);
2259 }
2260 else if (dst_tree_.typeinfo->validate_link(node_and_socket.socket->typeinfo->type,
2261 dst_socket.typeinfo->type))
2262 {
2263 bke::node_add_link(
2264 dst_tree_, *node_and_socket.node, *node_and_socket.socket, dst_node, dst_socket);
2265 }
2266 }
2267 else {
2268 this->set_primitive_value_on_socket(dst_socket, *primitive_value);
2269 }
2270 return;
2271 }
2272 if (!params_.allow_preserving_repeat_zones) {
2273 const bool is_iterations_input = dst_node.inputs.first_ == &dst_socket &&
2274 dst_node.is_type("GeometryNodeRepeatInput"_ustr);
2275 if (is_iterations_input) {
2276 this->add_dynamic_repeat_zone_iterations_error(original_node);
2277 this->set_primitive_value_on_socket(dst_socket, PrimitiveSocketValue{0});
2278 return;
2279 }
2280 }
2281 if (std::get_if<InputSocketValue>(&value.value)) {
2282 /* Cases were the input has a primitive value are handled above. */
2283 return;
2284 }
2285 if (std::get_if<FallbackValue>(&value.value)) {
2286 /* Cases were the input has a primitive fallback value are handled above. */
2287 return;
2288 }
2289 if (std::get_if<DanglingValue>(&value.value)) {
2290 /* Input sockets should never have a dangling value, because they are replaced by the socket
2291 * value in #handle_input_socket. */
2293 return;
2294 }
2295 if (std::get_if<BundleSocketValuePtr>(&value.value)) {
2296 /* This type can't be assigned to a socket. The bundle has to be separated first. */
2298 return;
2299 }
2300 if (std::get_if<ClosureZoneValue>(&value.value)) {
2301 /* This type can't be assigned to a socket. One has to evaluate a closure. */
2303 return;
2304 }
2305 if (std::get_if<MultiInputValue>(&value.value)) {
2306 /* This type can't be assigned to a socket. */
2308 return;
2309 }
2310 if (const auto *src_socket_value = std::get_if<LinkedSocketValue>(&value.value)) {
2311 if (dst_tree_.typeinfo->validate_link(src_socket_value->socket->typeinfo->type,
2312 dst_socket.typeinfo->type))
2313 {
2314 bke::node_add_link(
2315 dst_tree_, *src_socket_value->node, *src_socket_value->socket, dst_node, dst_socket);
2316 }
2317 return;
2318 }
2320 }
2321
2322 NodeAndSocket try_primitive_value_to_output_socket(const PrimitiveSocketValue &value)
2323 {
2324 if (const float *value_float = std::get_if<float>(&value.value)) {
2325 bNode *node = this->add_node("ShaderNodeValue"_ustr);
2326 bNodeSocket *socket = node->outputs.first();
2327 socket->default_value_typed<bNodeSocketValueFloat>()->value = *value_float;
2328 return {node, socket};
2329 }
2330 if (const int *value_int = std::get_if<int>(&value.value)) {
2331 bNode *node = this->add_node("FunctionNodeInputInt"_ustr);
2332 auto &storage = *static_cast<NodeInputInt *>(node->storage);
2333 storage.integer = *value_int;
2334 return {node, node->outputs.first()};
2335 }
2336 if (const bool *value_bool = std::get_if<bool>(&value.value)) {
2337 bNode *node = this->add_node("FunctionNodeInputBool"_ustr);
2338 auto &storage = *static_cast<NodeInputBool *>(node->storage);
2339 storage.boolean = int(*value_bool);
2340 return {node, node->outputs.first()};
2341 }
2342 if (const float3 *value_float3 = std::get_if<float3>(&value.value)) {
2343 bNode *node = this->add_node("FunctionNodeInputVector"_ustr);
2344 auto &storage = *static_cast<NodeInputVector *>(node->storage);
2345 copy_v3_v3(storage.vector, *value_float3);
2346 storage.dimensions = 3;
2347 return {node, node->outputs.first()};
2348 }
2349 if (const ColorGeometry4f *value_color = std::get_if<ColorGeometry4f>(&value.value)) {
2350 bNode *node = this->add_node("ShaderNodeRGB"_ustr);
2351 bNodeSocket *output_socket = node->outputs.first();
2352 auto *socket_storage = static_cast<bNodeSocketValueRGBA *>(output_socket->default_value);
2353 copy_v3_v3(socket_storage->value, *value_color);
2354 socket_storage->value[3] = 1.0f;
2355 return {node, output_socket};
2356 }
2357 if (std::get_if<std::string>(&value.value) || std::get_if<MenuValue>(&value.value)) {
2358 /* There is no node that outputs these values in the destination tree. */
2359 return {};
2360 }
2362 return {};
2363 }
2364
2365 bNode *add_node(const UString idname)
2366 {
2367 return bke::node_add_node(nullptr, dst_tree_, idname, this->get_next_node_identifier());
2368 }
2369
2370 int get_next_node_identifier()
2371 {
2372 return ++dst_node_counter_;
2373 }
2374
2375 void set_primitive_value_on_socket(bNodeSocket &socket, const PrimitiveSocketValue &value)
2376 {
2377 switch (socket.type) {
2378 case SOCK_FLOAT: {
2379 socket.default_value_typed<bNodeSocketValueFloat>()->value = std::get<float>(value.value);
2380 break;
2381 }
2382 case SOCK_INT: {
2383 socket.default_value_typed<bNodeSocketValueInt>()->value = std::get<int>(value.value);
2384 break;
2385 }
2386 case SOCK_BOOLEAN: {
2387 socket.default_value_typed<bNodeSocketValueBoolean>()->value = std::get<bool>(value.value);
2388 break;
2389 }
2390 case SOCK_VECTOR: {
2391 copy_v3_v3(socket.default_value_typed<bNodeSocketValueVector>()->value,
2392 std::get<float3>(value.value));
2393 break;
2394 }
2395 case SOCK_RGBA: {
2396 copy_v4_v4(socket.default_value_typed<bNodeSocketValueRGBA>()->value,
2397 std::get<ColorGeometry4f>(value.value));
2398 break;
2399 }
2400 case SOCK_MENU: {
2401 socket.default_value_typed<bNodeSocketValueMenu>()->value =
2402 std::get<nodes::MenuValue>(value.value).value;
2403 break;
2404 }
2405 case SOCK_STRING: {
2406 STRNCPY_UTF8(socket.default_value_typed<bNodeSocketValueString>()->value,
2407 std::get<std::string>(value.value).c_str());
2408 break;
2409 }
2410 default: {
2412 break;
2413 }
2414 }
2415 }
2416
2417 void restore_zones_in_output_tree()
2418 {
2419 for (const PreservedZone &copied_zone : copied_zone_by_zone_output_node_.values()) {
2420 if (!copied_zone.input_node || !copied_zone.output_node) {
2421 continue;
2422 }
2423 const bke::bNodeZoneType *zone_type = bke::zone_type_by_node_type(
2424 copied_zone.input_node->type_legacy);
2425 if (!zone_type) {
2426 continue;
2427 }
2428 int &output_id = zone_type->get_corresponding_output_id(*copied_zone.input_node);
2429 output_id = copied_zone.output_node->identifier;
2430 }
2431 }
2432
2433 void position_nodes_in_output_tree()
2434 {
2435 bNodeTree &tree = dst_tree_;
2436 tree.ensure_topology_cache();
2437
2438 Map<int, int> num_by_depth;
2439 Map<bNode *, int> depth_by_node;
2440
2441 /* Simple algorithm that does a very rough layout of the generated tree. This does not produce
2442 * great results generally, but is usually good enough when debugging smaller node trees. */
2443 for (bNode *node : tree.toposort_right_to_left()) {
2444 int depth = 0;
2445 for (bNodeSocket *socket : node->output_sockets()) {
2446 for (bNodeSocket *target : socket->directly_linked_sockets()) {
2447 depth = std::max(depth, depth_by_node.lookup(&target->owner_node()) + 1);
2448 }
2449 }
2450 depth_by_node.add_new(node, depth);
2451 const int index_at_depth = num_by_depth.lookup_or_add(depth, 0)++;
2452 node->location[0] = 200 - depth * 200;
2453 node->location[1] = -index_at_depth * 300;
2454 }
2455 }
2456
2461 void forward_value_or_schedule(const SocketInContext &socket, const SocketInContext &origin)
2462 {
2463 if (const SocketValue *value = value_by_socket_.lookup_ptr(origin)) {
2464 if (socket->type == origin->type) {
2465 this->store_socket_value(socket, *value);
2466 return;
2467 }
2468 this->store_socket_value(
2469 socket, this->handle_implicit_conversion(*value, *origin->typeinfo, *socket->typeinfo));
2470 return;
2471 }
2472 this->schedule_socket(origin);
2473 }
2474
2475 template<typename T> void store_socket_value_primitive(const SocketInContext &socket, T value)
2476 {
2477 value_by_socket_.add_new(socket, {PrimitiveSocketValue{value}});
2478 }
2479
2480 void store_socket_value(const SocketInContext &socket, SocketValue value)
2481 {
2482 value_by_socket_.add_new(socket, std::move(value));
2483 }
2484
2485 void store_socket_value_fallback(const SocketInContext &socket)
2486 {
2487 value_by_socket_.add_new(socket, {FallbackValue{}});
2488 }
2489
2490 void store_socket_value_dangling(const SocketInContext &socket)
2491 {
2492 value_by_socket_.add_new(socket, {DanglingValue{}});
2493 }
2494
2495 void schedule_socket(const SocketInContext &socket)
2496 {
2497 scheduled_sockets_stack_.push(socket);
2498 }
2499
2500 int node_copy_flag() const
2501 {
2502 const bool use_refcounting = !(dst_tree_.id.tag & ID_TAG_NO_MAIN);
2503 return use_refcounting ? 0 : LIB_ID_CREATE_NO_USER_REFCOUNT;
2504 }
2505
2506 bool socket_is_ignored(const bNodeSocket &socket) const
2507 {
2508 return !socket.is_available() || socket.typeinfo->idname == "NodeSocketVirtual"_ustr;
2509 }
2510
2511 void report_error(const NodeInContext &node, const StringRef message, const NodeWarningType type)
2512 {
2513 Vector<NodeInContext> nodes;
2514 nodes.append(node);
2515 for (const ComputeContext *context = node.context; context; context = context->parent()) {
2516 if (!warning_is_propagated(nodes.last()->warning_propagation, type)) {
2517 break;
2518 }
2519 if (const auto *group_context = dynamic_cast<const bke::GroupNodeComputeContext *>(context))
2520 {
2521 nodes.append({context->parent(), group_context->node()});
2522 }
2523 }
2524 for (const NodeInContext &node : nodes) {
2525 params_.r_error_messages.append({&*node, message, type});
2526 }
2527 }
2528};
2529
2530} // namespace
2531
2533 bNodeTree &dst_tree,
2535{
2536 ShaderNodesInliner inliner(src_tree, dst_tree, params);
2537
2538 if (inliner.do_inline()) {
2539 /* Update deprecated bNodeSocket.link pointers because some code still depends on it. */
2540 for (bNode &node : dst_tree.nodes) {
2541 for (bNodeSocket &sock : node.inputs) {
2542 sock.link = nullptr;
2543 }
2544 for (bNodeSocket &sock : node.outputs) {
2545 sock.link = nullptr;
2546 }
2547 }
2548 for (bNodeLink &link : dst_tree.links) {
2549 link.tosock->link = &link;
2551 link.tosock->typeinfo->type));
2552 link.flag |= NODE_LINK_VALID;
2553 }
2554 return true;
2555 }
2556
2557 return false;
2558}
2559
2560} // namespace blender::nodes
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define BLI_SCOPED_DEFER(function_to_defer)
#define STR_ELEM(...)
#define STRNCPY_UTF8(dst, src)
#define TIP_(msgid)
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define ELEM(...)
constexpr IndexRange()=default
#define storage(slot, qualifiers)
#define index(i)
#define condition(cond)
float3x3 scale(float3x3 mat, float2 scale)
@ NODE_BOOLEAN_MATH_IMPLY
@ NODE_BOOLEAN_MATH_AND
@ NODE_BOOLEAN_MATH_NAND
@ NODE_BOOLEAN_MATH_XOR
@ NODE_BOOLEAN_MATH_NOT
@ NODE_BOOLEAN_MATH_OR
@ NODE_BOOLEAN_MATH_NIMPLY
@ NODE_BOOLEAN_MATH_XNOR
@ NODE_BOOLEAN_MATH_NOR
@ NODE_MATH_DIVIDE
@ NODE_MATH_GREATER_THAN
@ NODE_MATH_MULTIPLY
@ NODE_INTEGER_MATH_MULTIPLY
@ NODE_INTEGER_MATH_DIVIDE
@ NODE_VECTOR_MATH_LENGTH
@ NODE_VECTOR_MATH_CROSS_PRODUCT
@ NODE_VECTOR_MATH_DOT_PRODUCT
@ NODE_VECTOR_MATH_DIVIDE
@ NODE_VECTOR_MATH_MULTIPLY
@ NODE_VECTOR_MATH_SCALE
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
ccl_device_inline void copy_v3_v3(ccl_private float *r, const float3 val)
ccl_device_inline void copy_v4_v4(ccl_private float *r, const float4 val)
#define T
const DataTypeConversions & get_implicit_type_conversions()
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNode * node_copy_with_mapping(bNodeTree *dst_tree, const bNode &node_src, int flag, std::optional< StringRefNull > dst_unique_name, std::optional< int > dst_unique_identifier, Map< const bNodeSocket *, bNodeSocket * > &new_socket_map, bool allow_duplicate_names=false)
Definition node.cc:4327
int context(const bContext *C, const char *member, bContextDataResult *result)
GreasePencil * from_context(bContext &C)
bNode * add_node(const bContext &C, const UString idname, const float2 &location)
Definition node_add.cc:87
bool inline_shader_node_tree(const bNodeTree &src_tree, bNodeTree &dst_tree, InlineShaderNodeTreeParams &params)
bool operator==(const BundleSignature &a, const BundleSignature &b)
bool warning_is_propagated(const NodeWarningPropagation propagation, const NodeWarningType warning_type)
Definition warning.cc:74
void report_error(const StringRefNull error)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
#define hash
Definition noise_c.cc:156
#define mix
bke::bNodeSocketType * typeinfo
struct bNodeLink * link
ListBaseT< bNodeLink > links
bke::bNodeTreeType * typeinfo
ListBaseT< bNode > nodes
eNodeSocketDatatype type
Definition BKE_node.hh:198
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566