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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "dependencygraph.h"
#include "datamodel/idatamodel.h"
#include "datamodel/dmelement.h"
#include "mathlib/mathlib.h" // for swap
#include "datamodel/dmattribute.h"
#include "datamodel/dmattributevar.h"
#include "tier1/mempool.h"
#include "tier0/vprof.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Misc helper enums and classes for CDependencyGraph class
//-----------------------------------------------------------------------------
enum TraversalState_t
{
TS_NOT_VISITED,
TS_VISITING,
TS_VISITED,
};
struct COperatorNode
{
COperatorNode( IDmeOperator *pOp = NULL ) :
m_state( TS_NOT_VISITED ),
m_operator( pOp ),
m_bInList( false )
{
}
TraversalState_t m_state;
IDmeOperator *m_operator;
CUtlVector< CAttributeNode * > m_OutputAttributes;
bool m_bInList;
};
class CAttributeNode
{
public:
CAttributeNode( CDmAttribute *attribute = NULL ) :
m_attribute( attribute ),
m_bIsOutputToOperator( false )
{
}
CDmAttribute *m_attribute;
CUtlVector< COperatorNode * > m_InputDependentOperators;
bool m_bIsOutputToOperator;
};
CClassMemoryPool< CAttributeNode > g_AttrNodePool( 1000 );
CClassMemoryPool< COperatorNode > g_OperatorNodePool( 1000 );
bool HashEntryCompareFunc( CAttributeNode *const& lhs, CAttributeNode *const& rhs )
{
return lhs->m_attribute == rhs->m_attribute;
}
uint HashEntryKeyFunc( CAttributeNode *const& keyinfo )
{
uint i = (uint)keyinfo->m_attribute;
return i >> 2; // since memory is allocated on a 4-byte (at least!) boundary
}
//-----------------------------------------------------------------------------
// CDependencyGraph constructor - builds dependency graph from operators
//-----------------------------------------------------------------------------
CDependencyGraph::CDependencyGraph() :
m_attrNodes( 4096, 0, 0, HashEntryCompareFunc, HashEntryKeyFunc )
{
}
void CDependencyGraph::Reset( const CUtlVector< IDmeOperator * > &operators )
{
VPROF_BUDGET( "CDependencyGraph::Reset", VPROF_BUDGETGROUP_TOOLS );
Cleanup();
CUtlVector< CDmAttribute * > attrs; // moved outside the loop to function as a temporary memory pool for performance
int on = operators.Count();
CUtlRBTree< IDmeOperator * > operatorDict( 0, on * 2, DefLessFunc(IDmeOperator *) );
for ( int i = 0; i < on; ++i )
{
operatorDict.Insert( operators[i] );
}
m_opNodes.EnsureCapacity( on );
for ( int oi = 0; oi < on; ++oi )
{
IDmeOperator *pOp = operators[ oi ];
Assert( pOp );
if ( pOp == NULL )
continue;
COperatorNode *pOpNode = g_OperatorNodePool.Alloc();
pOpNode->m_operator = pOp;
attrs.RemoveAll();
pOp->GetInputAttributes( attrs );
int an = attrs.Count();
for ( int ai = 0; ai < an; ++ai )
{
CAttributeNode *pAttrNode = FindAttrNode( attrs[ ai ] );
pAttrNode->m_InputDependentOperators.AddToTail( pOpNode );
}
attrs.RemoveAll();
pOp->GetOutputAttributes( attrs );
an = attrs.Count();
for ( int ai = 0; ai < an; ++ai )
{
CAttributeNode *pAttrNode = FindAttrNode( attrs[ ai ] );
pAttrNode->m_bIsOutputToOperator = true;
pOpNode->m_OutputAttributes.AddToTail( pAttrNode );
#ifdef _DEBUG
// Look for dependent operators, add them if they are not in the array
// FIXME: Should this happen for input attributes too?
CDmElement* pElement = pAttrNode->m_attribute->GetOwner();
IDmeOperator *pOperator = dynamic_cast< IDmeOperator* >( pElement );
if ( pOperator )
{
// Look for the operator in the existing list
if ( operatorDict.Find( pOperator ) == operatorDict.InvalidIndex() )
{
CDmElement *pOp1 = dynamic_cast< CDmElement* >( pOperator );
CDmElement *pOp2 = dynamic_cast< CDmElement* >( pOp );
Warning( "Found dependent operator '%s' referenced by operator '%s' that wasn't in the scene or trackgroups!\n", pOp1->GetName(), pOp2->GetName() );
}
}
#endif
}
m_opNodes.AddToTail( pOpNode );
}
}
//-----------------------------------------------------------------------------
// CDependencyGraph destructor - releases temporary opNodes and attrNodes
//-----------------------------------------------------------------------------
CDependencyGraph::~CDependencyGraph()
{
Cleanup();
}
void CDependencyGraph::Cleanup()
{
VPROF_BUDGET( "CDependencyGraph::Cleanup", VPROF_BUDGETGROUP_TOOLS );
int on = m_opNodes.Count();
for ( int oi = 0; oi < on; ++oi )
{
g_OperatorNodePool.Free( m_opNodes[ oi ] );
}
UtlHashHandle_t h = m_attrNodes.GetFirstHandle();
for ( ; h != m_attrNodes.InvalidHandle(); h = m_attrNodes.GetNextHandle( h ) )
{
g_AttrNodePool.Free( m_attrNodes[ h ] );
}
m_opRoots.RemoveAll();
m_opNodes.RemoveAll();
m_attrNodes.RemoveAll();
m_operators.RemoveAll();
}
//-----------------------------------------------------------------------------
// caches changed operators as roots - typically once per frame, every frame
//-----------------------------------------------------------------------------
void CDependencyGraph::FindRoots()
{
m_opRoots.RemoveAll();
uint oi;
uint on = m_opNodes.Count();
for ( oi = 0; oi < on; ++oi )
{
COperatorNode *pOpNode = m_opNodes[ oi ];
pOpNode->m_bInList = false;
pOpNode->m_state = TS_NOT_VISITED;
IDmeOperator *pOp = pOpNode->m_operator;
if ( !pOp->IsDirty() )
continue;
m_opRoots.AddToTail( pOpNode );
pOpNode->m_bInList = true;
}
// Do we have an attribute which is an input to us which is not an output to some other op?
UtlHashHandle_t h = m_attrNodes.GetFirstHandle();
for ( ; h != m_attrNodes.InvalidHandle(); h = m_attrNodes.GetNextHandle( h ) )
{
CAttributeNode *pAttrNode = m_attrNodes[ h ];
//Msg( "attrib %s %p\n", pAttrNode->m_attribute->GetName(), pAttrNode->m_attribute );
if ( !pAttrNode->m_bIsOutputToOperator &&
pAttrNode->m_attribute->IsFlagSet( FATTRIB_OPERATOR_DIRTY ) )
{
on = pAttrNode->m_InputDependentOperators.Count();
for ( oi = 0; oi < on; ++oi )
{
COperatorNode *pOpNode = pAttrNode->m_InputDependentOperators[ oi ];
if ( !pOpNode->m_bInList )
{
m_opRoots.AddToTail( pOpNode );
pOpNode->m_bInList = true;
}
}
}
pAttrNode->m_attribute->RemoveFlag( FATTRIB_OPERATOR_DIRTY );
}
}
//-----------------------------------------------------------------------------
// returns only the operators that need to be evaluated, sorted by dependencies
//-----------------------------------------------------------------------------
bool CDependencyGraph::CullAndSortOperators()
{
FindRoots();
m_operators.RemoveAll();
bool cycle = GetOperatorOrdering( m_opRoots, m_operators ); // leaves to roots (outputs to inputs)
int on = m_operators.Count();
int oh = on / 2;
for ( int oi = 0; oi < oh; ++oi )
{
V_swap( m_operators[ oi ], m_operators[ on - oi - 1 ] );
}
return cycle;
// return GetOperatorOrdering( m_opLeaves, operators ); // roots to leaves (inputs to outputs)
}
//-----------------------------------------------------------------------------
// GetOperatorOrdering is just a recursive post-order depth-first-search
// since we have two types of nodes, we manually traverse to the opnodes grandchildren, which are opnodes
// (skipping children, which are attrnodes)
// returns true if a cycle found - in this case, an arbitrary link of the cycle will be ignored
//-----------------------------------------------------------------------------
bool CDependencyGraph::GetOperatorOrdering( CUtlVector< COperatorNode * > &pOpNodes, CUtlVector< IDmeOperator * > &operators )
{
bool cycle = false;
uint on = pOpNodes.Count();
for ( uint oi = 0; oi < on; ++oi )
{
COperatorNode *pOpNode = pOpNodes[ oi ];
if ( pOpNode->m_state != TS_NOT_VISITED )
{
if ( pOpNode->m_state == TS_VISITING )
{
cycle = true;
}
continue;
}
pOpNode->m_state = TS_VISITING; // mark as in being visited
// DBG_PrintOperator( pIndent, pOpNode->m_operator );
// leaves to roots (outputs to inputs)
uint an = pOpNode->m_OutputAttributes.Count();
for ( uint ai = 0; ai < an; ++ai )
{
CAttributeNode *pAttrNode = pOpNode->m_OutputAttributes[ ai ];
if ( GetOperatorOrdering( pAttrNode->m_InputDependentOperators, operators ) )
{
cycle = true;
}
}
operators.AddToTail( pOpNode->m_operator );
pOpNode->m_state = TS_VISITED; // mark as done visiting
}
return cycle;
}
//-----------------------------------------------------------------------------
// internal helper method - finds attrNode corresponding to pAttr
//-----------------------------------------------------------------------------
CAttributeNode *CDependencyGraph::FindAttrNode( CDmAttribute *pAttr )
{
VPROF_BUDGET( "CDependencyGraph::FindAttrNode", VPROF_BUDGETGROUP_TOOLS );
Assert( pAttr );
CAttributeNode search( pAttr );
UtlHashHandle_t idx = m_attrNodes.Find( &search );
if ( idx != m_attrNodes.InvalidHandle() )
{
return m_attrNodes.Element( idx );
}
CAttributeNode *pAttrNode = 0;
{
VPROF( "CDependencyGraph::FindAttrNode_Alloc" );
pAttrNode = g_AttrNodePool.Alloc();
pAttrNode->m_attribute = pAttr;
}
{
VPROF( "CDependencyGraph::FindAttrNode_Alloc2" );
m_attrNodes.Insert( pAttrNode );
}
return pAttrNode;
}
//-----------------------------------------------------------------------------
// temporary internal debugging function
//-----------------------------------------------------------------------------
void CDependencyGraph::DBG_PrintOperator( const char *pIndent, IDmeOperator *pOp )
{
CDmElement *pElement = dynamic_cast< CDmElement* >( pOp );
Msg( "%s%s <%s> {\n", pIndent, pElement->GetName(), pElement->GetTypeString() );
}
|