Welcome to mirror list, hosted at ThFree Co, Russian Federation.

deg_builder_cycle.cc « builder « intern « depsgraph « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b37aaa12ffa639d0e557af44a29311d3738dd34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2015 Blender Foundation.
 * All rights reserved.
 *
 * Contributor(s): Sergey Sharybin
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/depsgraph/intern/builder/deg_builder_cycle.cc
 *  \ingroup depsgraph
 */

#include "intern/builder/deg_builder_cycle.h"

// TOO(sergey): Use some wrappers over those?
#include <cstdio>
#include <cstdlib>
#include <stack>

extern "C" {
#include "BLI_utildefines.h"
}

#include "util/deg_util_foreach.h"

#include "intern/nodes/deg_node.h"
#include "intern/nodes/deg_node_component.h"
#include "intern/nodes/deg_node_operation.h"

#include "intern/depsgraph.h"

namespace DEG {

struct StackEntry {
	OperationDepsNode *node;
	StackEntry *from;
	DepsRelation *via_relation;
};

void deg_graph_detect_cycles(Depsgraph *graph)
{
	enum {
		/* Not is not visited at all during traversal. */
		NODE_NOT_VISITED = 0,
		/* Node has been visited during traversal and not in current stack. */
		NODE_VISITED = 1,
		/* Node has been visited during traversal and is in current stack. */
		NODE_IN_STACK = 2,
	};

	std::stack<StackEntry> traversal_stack;
	foreach (OperationDepsNode *node, graph->operations) {
		bool has_inlinks = false;
		foreach (DepsRelation *rel, node->inlinks) {
			if (rel->from->type == DEPSNODE_TYPE_OPERATION) {
				has_inlinks = true;
			}
		}
		if (has_inlinks == false) {
			StackEntry entry;
			entry.node = node;
			entry.from = NULL;
			entry.via_relation = NULL;
			traversal_stack.push(entry);
			node->tag = NODE_IN_STACK;
		}
		else {
			node->tag = NODE_NOT_VISITED;
		}
		node->done = 0;
	}

	while (!traversal_stack.empty()) {
		StackEntry& entry = traversal_stack.top();
		OperationDepsNode *node = entry.node;
		bool all_child_traversed = true;
		for (int i = node->done; i < node->outlinks.size(); ++i) {
			DepsRelation *rel = node->outlinks[i];
			if (rel->to->type == DEPSNODE_TYPE_OPERATION) {
				OperationDepsNode *to = (OperationDepsNode *)rel->to;
				if (to->tag == NODE_IN_STACK) {
					printf("Dependency cycle detected:\n");
					printf("  '%s' depends on '%s' through '%s'\n",
					       to->full_identifier().c_str(),
					       node->full_identifier().c_str(),
					       rel->name);

					StackEntry *current = &entry;
					while (current->node != to) {
						BLI_assert(current != NULL);
						printf("  '%s' depends on '%s' through '%s'\n",
						       current->node->full_identifier().c_str(),
						       current->from->node->full_identifier().c_str(),
						       current->via_relation->name);
						current = current->from;
					}
					/* TODO(sergey): So called russian roulette cycle solver. */
					rel->flag |= DEPSREL_FLAG_CYCLIC;
				}
				else if (to->tag == NODE_NOT_VISITED) {
					StackEntry new_entry;
					new_entry.node = to;
					new_entry.from = &entry;
					new_entry.via_relation = rel;
					traversal_stack.push(new_entry);
					to->tag = NODE_IN_STACK;
					all_child_traversed = false;
					node->done = i;
					break;
				}
			}
		}
		if (all_child_traversed) {
			node->tag = NODE_VISITED;
			traversal_stack.pop();
		}
	}
}

}  // namespace DEG