Blender V5.3
lazy_function_graph.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_dot_export.hh"
10
12
13#include <sstream>
14
16
18{
19 name_ = allocator_.copy_string(name);
20 graph_input_node_ = allocator_.construct<InterfaceNode>().release();
21 graph_output_node_ = allocator_.construct<InterfaceNode>().release();
22 nodes_.append(graph_input_node_);
23 nodes_.append(graph_output_node_);
24}
25
27{
28 for (FunctionNode *node : this->function_nodes()) {
29 for (InputSocket *socket : node->inputs_) {
30 std::destroy_at(socket);
31 }
32 for (OutputSocket *socket : node->outputs_) {
33 std::destroy_at(socket);
34 }
35 std::destroy_at(node);
36 }
37 for (const InterfaceNode *node : {graph_input_node_, graph_output_node_}) {
38 for (InputSocket *socket : node->inputs_) {
39 std::destroy_at(socket);
40 }
41 for (OutputSocket *socket : node->outputs_) {
42 std::destroy_at(socket);
43 }
44 std::destroy_at(node);
45 }
46}
47
49{
50 const Span<Input> inputs = fn.inputs();
51 const Span<Output> outputs = fn.outputs();
52
53 FunctionNode &node = *allocator_.construct<FunctionNode>().release();
54 node.fn_ = &fn;
55 node.inputs_ = allocator_.construct_elements_and_pointer_array<InputSocket>(inputs.size());
56 node.outputs_ = allocator_.construct_elements_and_pointer_array<OutputSocket>(outputs.size());
57
58 for (const int i : inputs.index_range()) {
59 InputSocket &socket = *node.inputs_[i];
60 socket.index_in_node_ = i;
61 socket.is_input_ = true;
62 socket.node_ = &node;
63 socket.type_ = inputs[i].type;
64 }
65 for (const int i : outputs.index_range()) {
66 OutputSocket &socket = *node.outputs_[i];
67 socket.index_in_node_ = i;
68 socket.is_input_ = false;
69 socket.node_ = &node;
70 socket.type_ = outputs[i].type;
71 }
72
73 nodes_.append(&node);
74 return node;
75}
76
78{
79 GraphInputSocket &socket = *allocator_.construct<GraphInputSocket>().release();
80 socket.is_input_ = false;
81 socket.node_ = graph_input_node_;
82 socket.type_ = &type;
83 socket.index_in_node_ = graph_inputs_.append_and_get_index(&socket);
84 graph_input_node_->outputs_ = graph_inputs_;
85
86 graph_input_node_->socket_names_.append(std::move(name));
87 return socket;
88}
89
91{
92 GraphOutputSocket &socket = *allocator_.construct<GraphOutputSocket>().release();
93 socket.is_input_ = true;
94 socket.node_ = graph_output_node_;
95 socket.type_ = &type;
96 socket.index_in_node_ = graph_outputs_.append_and_get_index(&socket);
97 graph_output_node_->inputs_ = graph_outputs_;
98
99 graph_output_node_->socket_names_.append(std::move(name));
100 return socket;
101}
102
104{
105 BLI_assert(to.origin_ == nullptr);
106 BLI_assert(from.type_ == to.type_);
107 to.origin_ = &from;
108 from.targets_.append(&to);
109}
110
112{
113 if (socket.origin_ != nullptr) {
114 socket.origin_->targets_.remove_first_occurrence_and_reorder(&socket);
115 socket.origin_ = nullptr;
116 }
117}
118
120{
121 for (const int i : nodes_.index_range()) {
122 nodes_[i]->index_in_graph_ = i;
123 }
124}
125
127{
128 int socket_counter = 0;
129 for (const int i : nodes_.index_range()) {
130 for (InputSocket *socket : nodes_[i]->inputs()) {
131 socket->index_in_graph_ = socket_counter++;
132 }
133 for (OutputSocket *socket : nodes_[i]->outputs()) {
134 socket->index_in_graph_ = socket_counter++;
135 }
136 }
137 socket_num_ = socket_counter;
138}
139
141{
142 for (const int i : nodes_.index_range()) {
143 if (nodes_[i]->index_in_graph_ != i) {
144 return false;
145 }
146 }
147 return true;
148}
149
150std::string Socket::name() const
151{
152 if (node_->is_function()) {
153 const FunctionNode &fn_node = static_cast<const FunctionNode &>(*node_);
154 const LazyFunction &fn = fn_node.function();
155 if (is_input_) {
156 return fn.input_name(index_in_node_);
157 }
158 return fn.output_name(index_in_node_);
159 }
160 const InterfaceNode &interface_node = *static_cast<const InterfaceNode *>(node_);
161 return interface_node.socket_names_[index_in_node_];
162}
163
164std::string Socket::detailed_name() const
165{
166 std::stringstream ss;
167 ss << node_->name() << ":" << (is_input_ ? "IN" : "OUT") << ":" << index_in_node_ << ":"
168 << this->name();
169 return ss.str();
170}
171
172std::string Node::name() const
173{
174 if (this->is_function()) {
175 return fn_->name();
176 }
177 return "Interface";
178}
179
180std::string Graph::ToDotOptions::socket_name(const Socket &socket) const
181{
182 return socket.name();
183}
184
185std::optional<std::string> Graph::ToDotOptions::socket_font_color(const Socket & /*socket*/) const
186{
187 return std::nullopt;
188}
189
191 const InputSocket & /*to*/,
192 dot_export::DirectedEdge & /*dot_edge*/) const
193{
194}
195
196std::string Graph::to_dot(const ToDotOptions &options) const
197{
200
202
203 for (const Node *node : nodes_) {
204 dot_export::Node &dot_node = digraph.new_node("");
205 if (node->is_interface()) {
206 dot_node.set_background_color("lightblue");
207 }
208 else {
209 dot_node.set_background_color("white");
210 }
211
212 dot_export::NodeWithSockets dot_node_with_sockets;
213 dot_node_with_sockets.node_name = node->name();
214 for (const InputSocket *socket : node->inputs()) {
215 dot_export::NodeWithSockets::Input &dot_input = dot_node_with_sockets.add_input(
216 options.socket_name(*socket));
217 dot_input.fontcolor = options.socket_font_color(*socket);
218 }
219 for (const OutputSocket *socket : node->outputs()) {
220 dot_export::NodeWithSockets::Output &dot_output = dot_node_with_sockets.add_output(
221 options.socket_name(*socket));
222 dot_output.fontcolor = options.socket_font_color(*socket);
223 }
224
225 dot_nodes.add_new(node, dot_export::NodeWithSocketsRef(dot_node, dot_node_with_sockets));
226 }
227
228 for (const Node *node : nodes_) {
229 for (const InputSocket *socket : node->inputs()) {
230 const dot_export::NodeWithSocketsRef &to_dot_node = dot_nodes.lookup(&socket->node());
231 const dot_export::NodePort to_dot_port = to_dot_node.input(socket->index());
232
233 if (const OutputSocket *origin = socket->origin()) {
234 dot_export::NodeWithSocketsRef &from_dot_node = dot_nodes.lookup(&origin->node());
235 dot_export::DirectedEdge &dot_edge = digraph.new_edge(
236 from_dot_node.output(origin->index()), to_dot_port);
237 options.add_edge_attributes(*origin, *socket, dot_edge);
238 }
239 else if (const void *default_value = socket->default_value()) {
240 const CPPType &type = socket->type();
241 std::string value_string;
242 if (type.is_printable()) {
243 value_string = type.to_string(default_value);
244 }
245 else {
246 value_string = type.name();
247 }
248 dot_export::Node &default_value_dot_node = digraph.new_node(value_string);
249 default_value_dot_node.set_shape(dot_export::Attr_shape::Ellipse);
250 default_value_dot_node.attributes.set("color", "#00000055");
251 digraph.new_edge(default_value_dot_node, to_dot_port);
252 }
253 }
254 }
255
256 return digraph.to_dot_string();
257}
258
259} // namespace blender::fn::lazy_function
bool is_printable() const
std::string to_string(const void *value) const
Definition cpp_type.cc:15
StringRefNull name() const
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void set(StringRef key, StringRef value)
std::string to_dot_string() const
DirectedEdge & new_edge(NodePort from, NodePort to)
Definition dot_export.cc:46
void set_rankdir(Attr_rankdir rankdir)
Node & new_node(StringRef label)
Definition dot_export.cc:21
void set_shape(Attr_shape shape)
void set_background_color(StringRef name)
virtual std::string socket_name(const Socket &socket) const
virtual void add_edge_attributes(const OutputSocket &from, const InputSocket &to, dot_export::DirectedEdge &dot_edge) const
virtual std::optional< std::string > socket_font_color(const Socket &socket) const
Span< const FunctionNode * > function_nodes() const
FunctionNode & add_function(const LazyFunction &fn)
void add_link(OutputSocket &from, InputSocket &to)
GraphOutputSocket & add_output(const CPPType &type, std::string name="")
void clear_origin(InputSocket &socket)
std::string to_dot(const ToDotOptions &options={}) const
GraphInputSocket & add_input(const CPPType &type, std::string name="")
CCL_NAMESPACE_BEGIN struct Options options
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Input & add_input(std::string name)
Output & add_output(std::string name)