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

depsgraph_query.cc « intern « depsgraph « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 73193747b93693811e8802eb2d701a6f92969349 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * ***** 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) 2013 Blender Foundation.
 * All rights reserved.
 *
 * Original Author: Joshua Leung
 * Contributor(s): None Yet
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/depsgraph/intern/depsgraph_query.cc
 *  \ingroup depsgraph
 *
 * Implementation of Querying and Filtering API's
 */

#include "MEM_guardedalloc.h"

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

#include "BKE_main.h"

#include "DEG_depsgraph_query.h"
} /* extern "C" */

#include "depsgraph_queue.h"
#include "depsnode.h"
#include "depsnode_operation.h"
#include "depsgraph_intern.h"

/* ************************* */
/* Low-Level Graph Traversal */

#if 0
/* Prepare for graph traversal, by tagging nodes, etc. */
static void DEG_graph_traverse_begin(Depsgraph * /*graph*/)
{
	/* go over all nodes, initialising the valence counts */
	// XXX: this will end up being O(|V|), which is bad when we're just updating a few nodes...
}

/* Perform a traversal of graph from given starting node (in execution order) */
// TODO: additional flags for controlling the process?
void DEG_graph_traverse_from_node(Depsgraph *graph, OperationDepsNode *start_node,
                                  DEG_FilterPredicate filter, void *filter_data,
                                  DEG_NodeOperation op, void *operation_data)
{
	DepsgraphQueue *q;

	/* sanity checks */
	if (ELEM(NULL, graph, start_node, op))
		return;

	/* add node as starting node to be evaluated, with value of 0 */
	q = DEG_queue_new();

	start_node->num_links_pending = 0;
	DEG_queue_push(q, start_node, 0.0f);

	/* while we still have nodes in the queue, grab and work on next one */
	do {
		/* grab item at front of queue */
		// XXX: in practice, we may need to wait until one becomes available...
		OperationDepsNode *node = (OperationDepsNode *)DEG_queue_pop(q);

		/* perform operation on node */
		op(graph, node, operation_data);

		/* schedule up operations which depend on this */
		DEPSNODE_RELATIONS_ITER_BEGIN(node->outlinks, rel)
		{
			/* ensure that relationship is not tagged for ignoring (i.e. cyclic, etc.) */
			// TODO: cyclic refs should probably all get clustered towards the end, so that we can just stop on the first one
			if ((rel->flag & DEPSREL_FLAG_CYCLIC) == 0) {
				OperationDepsNode *child_node = (OperationDepsNode *)rel->to;

				/* only visit node if the filtering function agrees */
				if ((filter == NULL) || filter(graph, child_node, filter_data)) {
					/* schedule up node... */
					child_node->num_links_pending--;
					DEG_queue_push(q, child_node, (float)child_node->num_links_pending);
				}
			}
		}
		DEPSNODE_RELATIONS_ITER_END;
	} while (DEG_queue_is_empty(q) == false);

	/* cleanup */
	DEG_queue_free(q);
}
#endif

/* ************************************************************** */
/* Filtering API - Basically, making a copy of the existing graph */

/* Create filtering context */
// TODO: allow passing in a number of criteria?
DepsgraphCopyContext *DEG_filter_init()
{
	DepsgraphCopyContext *dcc = (DepsgraphCopyContext *)MEM_callocN(sizeof(DepsgraphCopyContext), "DepsgraphCopyContext");

	/* init hashes for easy lookups */
	dcc->nodes_hash = BLI_ghash_ptr_new("Depsgraph Filter NodeHash");
	dcc->rels_hash = BLI_ghash_ptr_new("Depsgraph Filter Relationship Hash"); // XXX?

	/* store filtering criteria? */
	// xxx...

	return dcc;
}

/* Cleanup filtering context */
void DEG_filter_cleanup(DepsgraphCopyContext *dcc)
{
	/* sanity check */
	if (dcc == NULL)
		return;

	/* free hashes - contents are weren't copied, so are ok... */
	BLI_ghash_free(dcc->nodes_hash, NULL, NULL);
	BLI_ghash_free(dcc->rels_hash, NULL, NULL);

	/* clear filtering criteria */
	// ...

	/* free dcc itself */
	MEM_freeN(dcc);
}

/* -------------------------------------------------- */

/* Create a copy of provided node */
// FIXME: the handling of sub-nodes and links will need to be subject to filtering options...
// XXX: perhaps this really shouldn't be exposed, as it will just be a sub-step of the evaluation process?
DepsNode *DEG_copy_node(DepsgraphCopyContext *dcc, const DepsNode *src)
{
	/* sanity check */
	if (src == NULL)
		return NULL;

	DepsNodeFactory *factory = DEG_get_node_factory(src->type);
	BLI_assert(factory != NULL);
	DepsNode *dst = factory->copy_node(dcc, src);

	/* add this node-pair to the hash... */
	BLI_ghash_insert(dcc->nodes_hash, (DepsNode *)src, dst);

#if 0 /* XXX TODO */
	/* now, fix up any links in standard "node header" (i.e. DepsNode struct, that all
	 * all others are derived from) that are now corrupt
	 */
	{
		/* relationships to other nodes... */
		// FIXME: how to handle links? We may only have partial set of all nodes still?
		// XXX: the exact details of how to handle this are really part of the querying API...

		// XXX: BUT, for copying subgraphs, we'll need to define an API for doing this stuff anyways
		// (i.e. for resolving and patching over links that exist within subtree...)
		dst->inlinks.clear();
		dst->outlinks.clear();

		/* clear traversal data */
		dst->num_links_pending = 0;
		dst->lasttime = 0;
	}

	/* fix links */
	// XXX...
#endif

	/* return copied node */
	return dst;
}

bool DEG_id_type_tagged(Main *bmain, short idtype)
{
	return bmain->id_tag_update[((unsigned char *)&idtype)[0]] != 0;
}

short DEG_get_eval_flags_for_id(Depsgraph *graph, ID *id)
{
	if (graph == NULL) {
		/* Happens when converting objects to mesh from a python script
		 * after modifying scene graph.
		 *
		 * Currently harmless because it's only called for temporary
		 * objects which are out of the DAG anyway.
		 */
		return 0;
	}

	IDDepsNode *id_node = graph->find_id_node(id);
	if (id_node == NULL) {
		/* TODO(sergey): Does it mean we need to check set scene? */
		return 0;
	}

	return id_node->eval_flags;
}