Blender V5.3
deg_debug_relations_graphviz.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdarg>
12
13#include "BLI_dot_export.hh"
14
15#include "DEG_depsgraph.hh"
17
18#include "intern/depsgraph.hh"
20
25
26#include <sstream>
27
28namespace blender {
29
30/* ****************** */
31/* Graphviz Debugging */
32
33namespace deg {
34
35/* Only one should be enabled, defines whether graphviz nodes
36 * get colored by individual types or classes.
37 */
38#define COLOR_SCHEME_NODE_CLASS 1
39// #define COLOR_SCHEME_NODE_TYPE 2
40
41static const char *deg_debug_graphviz_fontname = "helvetica";
44static const int deg_debug_max_colors = 12;
45#ifdef COLOR_SCHEME_NODE_TYPE
46static const char *deg_debug_colors[] = {
47 "#a6cee3",
48 "#1f78b4",
49 "#b2df8a",
50 "#33a02c",
51 "#fb9a99",
52 "#e31a1c",
53 "#fdbf6f",
54 "#ff7f00",
55 "#cab2d6",
56 "#6a3d9a",
57 "#ffff99",
58 "#b15928",
59 "#ff00ff",
60};
61#endif
62static const char *deg_debug_colors_light[] = {
63 "#8dd3c7",
64 "#ffffb3",
65 "#bebada",
66 "#fb8072",
67 "#80b1d3",
68 "#fdb462",
69 "#b3de69",
70 "#fccde5",
71 "#d9d9d9",
72 "#bc80bd",
73 "#ccebc5",
74 "#ffed6f",
75 "#ff00ff",
76};
77
78#ifdef COLOR_SCHEME_NODE_TYPE
79static const int deg_debug_node_type_color_map[][2] = {
82
83 /* Outer Types */
90 {NodeType::CACHE, 9},
94 {-1, 0},
95};
96#endif
97
99{
100#ifdef COLOR_SCHEME_NODE_CLASS
101 /* Some special types. */
102 switch (node->type) {
103 case NodeType::ID_REF:
104 return 5;
105 case NodeType::OPERATION: {
106 OperationNode *op_node = static_cast<OperationNode *>(const_cast<Node *>(node));
107 if (op_node->is_noop()) {
109 return 7;
110 }
111 return 8;
112 }
113 break;
114 }
115
116 default:
117 break;
118 }
119 /* Do others based on class. */
120 switch (node->get_class()) {
122 return 4;
124 return 1;
125 default:
126 return 9;
127 }
128#endif
129
130#ifdef COLOR_SCHEME_NODE_TYPE
131 const int (*pair)[2];
132 for (pair = deg_debug_node_type_color_map; (*pair)[0] >= 0; pair++) {
133 if ((*pair)[0] == node->type) {
134 return (*pair)[1];
135 }
136 }
137 return -1;
138#endif
139}
140
147
148static void deg_debug_graphviz_legend_color(const char *name,
149 const char *color,
150 std::stringstream &ss)
151{
152
153 ss << "<TR>";
154 ss << "<TD>" << name << "</TD>";
155 ss << "<TD BGCOLOR=\"" << color << "\"></TD>";
156 ss << "</TR>";
157}
158
160{
161 dot_export::Node &legend_node = ctx.digraph.new_node("");
162 legend_node.attributes.set("rank", "sink");
163 legend_node.attributes.set("shape", "none");
164 legend_node.attributes.set("margin", 0);
165
166 std::stringstream ss;
167 ss << "<";
168 ss << R"(<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">)";
169 ss << "<TR><TD COLSPAN=\"2\"><B>Legend</B></TD></TR>";
170
171#ifdef COLOR_SCHEME_NODE_CLASS
172 const char **colors = deg_debug_colors_light;
173 deg_debug_graphviz_legend_color("Operation", colors[4], ss);
174 deg_debug_graphviz_legend_color("Component", colors[1], ss);
175 deg_debug_graphviz_legend_color("ID Node", colors[5], ss);
176 deg_debug_graphviz_legend_color("NOOP", colors[8], ss);
177 deg_debug_graphviz_legend_color("Pinned OP", colors[7], ss);
178#endif
179
180#ifdef COLOR_SCHEME_NODE_TYPE
181 const int (*pair)[2];
182 for (pair = deg_debug_node_type_color_map; (*pair)[0] >= 0; pair++) {
183 DepsNodeFactory *nti = type_get_factory((NodeType)(*pair)[0]);
185 ctx, nti->tname().c_str(), deg_debug_colors_light[(*pair)[1] % deg_debug_max_colors], ss);
186 }
187#endif
188
189 ss << "</TABLE>";
190 ss << ">";
191 legend_node.attributes.set("label", ss.str());
192 legend_node.attributes.set("fontname", deg_debug_graphviz_fontname);
193}
194
196 const Node *node,
197 dot_export::Attributes &dot_attributes)
198{
199 const char *color_default = "black";
200 const char *color_modified = "orangered4";
201 const char *color_update = "dodgerblue3";
202 const char *color = color_default;
203 if (ctx.show_tags) {
204 if (node->get_class() == NodeClass::OPERATION) {
205 OperationNode *op_node = static_cast<OperationNode *>(const_cast<Node *>(node));
206 if (op_node->flag & DEPSOP_FLAG_DIRECTLY_MODIFIED) {
207 color = color_modified;
208 }
209 else if (op_node->flag & DEPSOP_FLAG_NEEDS_UPDATE) {
210 color = color_update;
211 }
212 }
213 }
214 dot_attributes.set("color", color);
215}
216
218 const Node *node,
219 dot_export::Attributes &dot_attributes)
220{
221 float penwidth_default = 1.0f;
222 float penwidth_modified = 4.0f;
223 float penwidth_update = 4.0f;
224 float penwidth = penwidth_default;
225 if (ctx.show_tags) {
226 if (node->get_class() == NodeClass::OPERATION) {
227 OperationNode *op_node = static_cast<OperationNode *>(const_cast<Node *>(node));
228 if (op_node->flag & DEPSOP_FLAG_DIRECTLY_MODIFIED) {
229 penwidth = penwidth_modified;
230 }
231 else if (op_node->flag & DEPSOP_FLAG_NEEDS_UPDATE) {
232 penwidth = penwidth_update;
233 }
234 }
235 }
236 dot_attributes.set("penwidth", penwidth);
237}
238
240 dot_export::Attributes &dot_attributes)
241{
242 const char *defaultcolor = "gainsboro";
243 int color_index = deg_debug_node_color_index(node);
244 const char *fillcolor = color_index < 0 ?
245 defaultcolor :
247 dot_attributes.set("fillcolor", fillcolor);
248}
249
251{
252 const char *color_default = "black";
253 const char *color_cyclic = "red4"; /* The color of crime scene. */
254 const char *color_godmode = "blue4"; /* The color of beautiful sky. */
255 const char *color = color_default;
256 if (rel->flag & RELATION_FLAG_CYCLIC) {
257 color = color_cyclic;
258 }
259 else if (rel->flag & RELATION_FLAG_GODMODE) {
260 color = color_godmode;
261 }
262 edge.attributes.set("color", color);
263}
264
266{
267 const char *style_default = "solid";
268 const char *style_no_flush = "dashed";
269 const char *style_flush_user_only = "dotted";
270 const char *style = style_default;
271 if (rel->flag & RELATION_FLAG_NO_FLUSH) {
272 style = style_no_flush;
273 }
275 style = style_flush_user_only;
276 }
277 edge.attributes.set("style", style);
278}
279
282{
283 const char *shape_default = "normal";
284 const char *shape_no_cow = "box";
285 const char *shape = shape_default;
286 if (rel->from->get_class() == NodeClass::OPERATION &&
288 {
289 OperationNode *op_from = static_cast<OperationNode *>(rel->from);
290 OperationNode *op_to = static_cast<OperationNode *>(rel->to);
291 if (op_from->owner->type == NodeType::COPY_ON_EVAL &&
292 /* The #ID::recalc flag depends on run-time state which is not valid at this point in time.
293 * Pass in all flags although there may be a better way to represent this. */
295 {
296 shape = shape_no_cow;
297 }
298 }
299 edge.attributes.set("arrowhead", shape);
300}
301
303 const Node *node,
304 dot_export::Attributes &dot_attributes)
305{
306 StringRef base_style = "filled"; /* default style */
307 if (ctx.show_tags) {
308 if (node->get_class() == NodeClass::OPERATION) {
309 OperationNode *op_node = static_cast<OperationNode *>(const_cast<Node *>(node));
311 base_style = "striped";
312 }
313 }
314 }
315 switch (node->get_class()) {
317 dot_attributes.set("style", base_style);
318 break;
320 dot_attributes.set("style", base_style);
321 break;
323 dot_attributes.set("style", base_style + ",rounded");
324 break;
325 }
326}
327
329 const Node *node,
330 dot_export::Cluster *parent_cluster)
331{
332 std::string name = node->identifier();
333
334 dot_export::Node &dot_node = ctx.digraph.new_node(name);
335 ctx.nodes_map.add_new(node, &dot_node);
336 dot_node.set_parent_cluster(parent_cluster);
337 dot_node.attributes.set("fontname", deg_debug_graphviz_fontname);
339 dot_node.attributes.set("shape", "box");
340
345}
346
348 DotExportContext &ctx, const Node *node, dot_export::Cluster *parent_cluster)
349{
350 std::string name = node->identifier();
351 dot_export::Cluster &cluster = ctx.digraph.new_cluster(name);
352 cluster.set_parent_cluster(parent_cluster);
353 cluster.attributes.set("fontname", deg_debug_graphviz_fontname);
355 cluster.attributes.set("margin", 16);
360 /* dummy node, so we can add edges between clusters */
361 dot_export::Node &dot_node = ctx.digraph.new_node("");
362 dot_node.attributes.set("shape", "point");
363 dot_node.attributes.set("style", "invis");
364 dot_node.set_parent_cluster(&cluster);
365 ctx.nodes_map.add_new(node, &dot_node);
366 ctx.clusters_map.add_new(node, &cluster);
367 return cluster;
368}
369
370static void deg_debug_graphviz_graph_nodes(DotExportContext &ctx, const Depsgraph *graph);
371static void deg_debug_graphviz_graph_relations(DotExportContext &ctx, const Depsgraph *graph);
372
374 const Node *node,
375 dot_export::Cluster *parent_cluster)
376{
377 switch (node->type) {
378 case NodeType::ID_REF: {
379 const IDNode *id_node = static_cast<const IDNode *>(node);
380 if (id_node->components.is_empty()) {
381 deg_debug_graphviz_node_single(ctx, node, parent_cluster);
382 }
383 else {
385 ctx, node, parent_cluster);
386 for (const ComponentNode *comp : id_node->components.values()) {
387 deg_debug_graphviz_node(ctx, comp, &cluster);
388 }
389 }
390 break;
391 }
399 case NodeType::BONE:
401 case NodeType::CACHE:
413 case NodeType::AUDIO:
416 case NodeType::SCENE:
420 ComponentNode *comp_node = static_cast<ComponentNode *>(const_cast<Node *>(node));
421 if (comp_node->operations.is_empty()) {
422 deg_debug_graphviz_node_single(ctx, node, parent_cluster);
423 }
424 else {
426 ctx, node, parent_cluster);
427 for (Node *op_node : comp_node->operations) {
428 deg_debug_graphviz_node(ctx, op_node, &cluster);
429 }
430 }
431 break;
432 }
436 deg_debug_graphviz_node_single(ctx, node, parent_cluster);
437 break;
439 break;
440 }
441}
442
444{
445 for (Relation *rel : node->inlinks) {
446 float penwidth = 2.0f;
447
448 const Node *head = rel->to; /* same as node */
449 const Node *tail = rel->from;
450 dot_export::Node &dot_tail = *ctx.nodes_map.lookup(tail);
451 dot_export::Node &dot_head = *ctx.nodes_map.lookup(head);
452
453 dot_export::DirectedEdge &edge = ctx.digraph.new_edge(dot_tail, dot_head);
454
455 /* NOTE: without label an id seem necessary to avoid bugs in graphviz/dot. */
456 edge.attributes.set("id", rel->name);
460 edge.attributes.set("penwidth", penwidth);
461
462 /* NOTE: edge from node to our own cluster is not possible and gives graphviz
463 * warning, avoid this here by just linking directly to the invisible
464 * placeholder node. */
465 dot_export::Cluster *tail_cluster = ctx.clusters_map.lookup_default(tail, nullptr);
466 if (tail_cluster != nullptr && tail_cluster->contains(dot_head)) {
467 edge.attributes.set("ltail", tail_cluster->name());
468 }
469 dot_export::Cluster *head_cluster = ctx.clusters_map.lookup_default(head, nullptr);
470 if (head_cluster != nullptr && head_cluster->contains(dot_tail)) {
471 edge.attributes.set("lhead", head_cluster->name());
472 }
473 }
474}
475
477{
478 for (Node *node : graph->id_nodes) {
479 deg_debug_graphviz_node(ctx, node, nullptr);
480 }
481 TimeSourceNode *time_source = graph->find_time_source();
482 if (time_source != nullptr) {
483 deg_debug_graphviz_node(ctx, time_source, nullptr);
484 }
485}
486
488{
489 for (IDNode *id_node : graph->id_nodes) {
490 for (ComponentNode *comp_node : id_node->components.values()) {
491 for (OperationNode *op_node : comp_node->operations) {
493 }
494 }
495 }
496
497 TimeSourceNode *time_source = graph->find_time_source();
498 if (time_source != nullptr) {
499 deg_debug_graphviz_node_relations(ctx, time_source);
500 }
501}
502
503} // namespace deg
504
505std::string DEG_debug_graph_to_dot(const Depsgraph &graph, const StringRef label)
506{
507 const deg::Depsgraph &deg_graph = reinterpret_cast<const deg::Depsgraph &>(graph);
508
510 deg::DotExportContext ctx{false, digraph};
511
513 digraph.attributes.set("compound", "true");
514 digraph.attributes.set("labelloc", "t");
517 digraph.attributes.set("label", label);
518 digraph.attributes.set("splines", "ortho");
519 digraph.attributes.set("overlap", "scalexy");
520
523
525
526 return digraph.to_dot_string();
527}
528
529} // namespace blender
bool is_empty() const
void set(StringRef key, StringRef value)
bool contains(Node &node) const
void set_parent_cluster(Cluster *new_parent)
Definition dot_export.cc:53
std::string to_dot_string() const
DirectedEdge & new_edge(NodePort from, NodePort to)
Definition dot_export.cc:46
Cluster & new_cluster(StringRef label="")
Definition dot_export.cc:30
void set_rankdir(Attr_rankdir rankdir)
Node & new_node(StringRef label)
Definition dot_export.cc:21
void set_parent_cluster(Cluster *cluster)
Definition dot_export.cc:73
static void deg_debug_graphviz_node_color(DotExportContext &ctx, const Node *node, dot_export::Attributes &dot_attributes)
static int deg_debug_node_color_index(const Node *node)
static void deg_debug_graphviz_graph_relations(DotExportContext &ctx, const Depsgraph *graph)
static void deg_debug_graphviz_legend(DotExportContext &ctx)
static void deg_debug_graphviz_relation_color(const Relation *rel, dot_export::DirectedEdge &edge)
static void deg_debug_graphviz_relation_style(const Relation *rel, dot_export::DirectedEdge &edge)
static void deg_debug_graphviz_node_fillcolor(const Node *node, dot_export::Attributes &dot_attributes)
static void deg_debug_graphviz_node_penwidth(DotExportContext &ctx, const Node *node, dot_export::Attributes &dot_attributes)
static float deg_debug_graphviz_node_label_size
static const char * deg_debug_colors_light[]
static dot_export::Cluster & deg_debug_graphviz_node_cluster_create(DotExportContext &ctx, const Node *node, dot_export::Cluster *parent_cluster)
static const int deg_debug_max_colors
static float deg_debug_graphviz_graph_label_size
static void deg_debug_graphviz_relation_arrowhead(const Relation *rel, dot_export::DirectedEdge &edge)
static void deg_debug_graphviz_graph_nodes(DotExportContext &ctx, const Depsgraph *graph)
static void deg_debug_graphviz_node_single(DotExportContext &ctx, const Node *node, dot_export::Cluster *parent_cluster)
DepsNodeFactory * type_get_factory(const NodeType type)
static void deg_debug_graphviz_legend_color(const char *name, const char *color, std::stringstream &ss)
static void deg_debug_graphviz_node(DotExportContext &ctx, const Node *node, dot_export::Cluster *parent_cluster)
static void deg_debug_graphviz_node_relations(DotExportContext &ctx, const Node *node)
static const char * deg_debug_graphviz_fontname
static void deg_debug_graphviz_node_style(DotExportContext &ctx, const Node *node, dot_export::Attributes &dot_attributes)
BMesh Iterators.
std::string DEG_debug_graph_to_dot(const Depsgraph &graph, StringRef label)
@ ID_RECALC_ALL
Definition DNA_ID.h:1190
virtual bool need_tag_cow_before_update(const IDRecalcFlag)
Vector< OperationNode * > operations
TimeSourceNode * find_time_source() const
Definition depsgraph.cc:92
Map< const Node *, dot_export::Cluster * > clusters_map
Map< const Node *, dot_export::Node * > nodes_map
Map< ComponentIDKey, ComponentNode * > components
virtual NodeClass get_class() const
Definition deg_node.cc:302