Loading...
Searching...
No Matches
sparseInputTraverser.h
Go to the documentation of this file.
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_EXEC_VDF_SPARSE_INPUT_TRAVERSER_H
8#define PXR_EXEC_VDF_SPARSE_INPUT_TRAVERSER_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/vdf/api.h"
15#include "pxr/exec/vdf/object.h"
16#include "pxr/exec/vdf/mask.h"
19
20#include "pxr/base/tf/hashmap.h"
21
22#include <functional>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
26class VdfConnection;
27class VdfNode;
28
43{
44public:
45
48 {
51
54 };
55
56
59
68 using NodeCallback = std::function<bool (const VdfNode &)>;
69
85 VDF_API
86 static void Traverse(
87 const VdfMaskedOutputVector &outputs,
88 const NodeCallback &nodeCallback,
90
96 using ConnectionCallback = std::function<
97 bool (const VdfConnection &, const VdfMask &)> ;
98
110 VDF_API
112 const VdfMaskedOutputVector &outputs,
113 const ConnectionCallback &connectionCallback);
114
116
117
120
128 using NodePathCallback = std::function<
129 bool (const VdfNode &node, const VdfObjectPtrVector &path)>;
130
137 using ConnectionPathCallback = std::function<
138 bool (const VdfConnection &, const VdfMask &,
139 const VdfObjectPtrVector &)>;
140
160 VDF_API
161 static void TraverseWithPath(
162 const VdfMaskedOutputVector &outputs,
163 const NodePathCallback &nodePathCallback,
164 const ConnectionPathCallback &connectionPathCallback,
166
168
169private:
170
171 // A type used to represent an input in a priority queue.
172 class _PrioritizedOutput;
173
174 // A map from pool chain index to prioritized output, used to ensure that we
175 // process outputs in their order in the pool chain.
176 //
177 // Note that using a std::map<> gives us the _PrioritizedOutputs sorted by
178 // the pool chain index.
179 //
180 typedef std::map<VdfPoolChainIndex, _PrioritizedOutput,
181 std::greater<VdfPoolChainIndex> > _PrioritizedOutputMap;
182
183 // An individual stack frame in the traversal state.
184 class _StackFrame;
185
186 // Type used to identify the masks that have already been visited for
187 // traversed connections.
188 typedef TfHashMap<const VdfConnection *, VdfMask::Bits, TfHash>
189 _VisitedConnections;
190
191 // This struct embodies the total state of a sparse traversal.
192 struct _TraversalState;
193
194 // Helper to initialize a traversal.
195 static void _InitTraversal(
196 const VdfMaskedOutputVector &outputs,
197 _TraversalState *state,
198 const CallbackMode callbackMode=CallbackModeAllNodes);
199
200 // Helper to traverse an output.
201 static void _TraverseOutput(
202 _TraversalState *state,
203 const _StackFrame &frame,
204 const CallbackMode callbackMode);
205
206 // Adapter that takes a NodeCallback and acts like a NodePathCallback by
207 // ignoring the path.
208 //
209 // XXX:optimization
210 // Using this means we do 2 std function calls for each call to the client
211 // provided callback. That's avoidable if we factor the code differently.
212 //
213 static bool _NodePathCallbackAdapter(
214 const NodeCallback &nodeCallback,
215 const VdfNode &node,
216 const VdfObjectPtrVector &)
217 {
218 return nodeCallback(node);
219 }
220
221 // Adapter that takes a ConnectionCallback and acts like a ConnectionPathCallback
222 // by ignoring the path.
223 //
224 static bool _ConnectionPathCallbackAdapter(
225 const ConnectionCallback &connectionCallback,
226 const VdfConnection &connection,
227 const VdfMask &dependencyMask,
228 const VdfObjectPtrVector &)
229 {
230 return connectionCallback(connection, dependencyMask);
231 }
232
233};
234
235PXR_NAMESPACE_CLOSE_SCOPE
236
237#endif
A class that fully represents a connection between two VdfNodes.
Definition: connection.h:30
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
This is the base class for all nodes in a VdfNetwork.
Definition: node.h:53
Opaque pool chain index type.
A class used for fast sparse traversals of VdfNetworks in the output-to-input direction.
static VDF_API void Traverse(const VdfMaskedOutputVector &outputs, const NodeCallback &nodeCallback, CallbackMode callbackMode=CallbackModeAllNodes)
Traverses the network in the input direction, starting from the masked outputs in outputs.
std::function< bool(const VdfConnection &, const VdfMask &, const VdfObjectPtrVector &)> ConnectionPathCallback
Callback used when traversing a network.
static VDF_API void TraverseWithConnectionCallback(const VdfMaskedOutputVector &outputs, const ConnectionCallback &connectionCallback)
Traverses the network in the input direction, starting from the masked outputs in outputs.
std::function< bool(const VdfNode &node, const VdfObjectPtrVector &path)> NodePathCallback
Callback used when traversing a network with path information.
static VDF_API void TraverseWithPath(const VdfMaskedOutputVector &outputs, const NodePathCallback &nodePathCallback, const ConnectionPathCallback &connectionPathCallback, CallbackMode callbackMode=CallbackModeAllNodes)
Traverses the network in the input direction, starting from the masked outputs in outputs,...
std::function< bool(const VdfNode &)> NodeCallback
Callback used when traversing a network.
std::function< bool(const VdfConnection &, const VdfMask &)> ConnectionCallback
Callback used when traversing a network.
CallbackMode
Callback mode for the node callback.
@ CallbackModeTerminalNodes
Invoke the node callback only on terminal nodes.
@ CallbackModeAllNodes
Invoke the node callback on all inputs. This is the default.
std::vector< VdfObjectPtr > VdfObjectPtrVector
An object vector.
Definition: object.h:412