HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
10 /// \file
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 
25 
26 class VdfConnection;
27 class VdfNode;
28 
29 /// \class VdfSparseInputTraverser
30 ///
31 /// \brief A class used for fast sparse traversals of VdfNetworks in the
32 /// output-to-input direction.
33 ///
34 /// A sparse traversal takes affects masks into account and avoids
35 /// traversing nodes that don't have an affect on the outputs
36 /// requested for the traversal. This is most often useful for
37 /// dependency traversals.
38 ///
39 /// In contrast, VdfIsTopologicalSourceNode() does a full topological
40 /// traversal.
41 ///
43 {
44 public:
45 
46  /// Callback mode for the node callback.
48  {
49  /// Invoke the node callback on all inputs. This is the default.
51 
52  /// Invoke the node callback only on terminal nodes.
54  };
55 
56 
57  /// \name Basic Traversal
58  /// @{
59 
60  /// Callback used when traversing a network.
61  ///
62  /// Called for each node that is visited that affects values of the initial
63  /// masked outputs.
64  ///
65  /// A return value of false halts traversal locally but allows prior
66  /// branches of traversal to continue.
67  ///
68  using NodeCallback = std::function<bool (const VdfNode &)>;
69 
70  /// Traverses the network in the input direction, starting from the
71  /// masked outputs in \p outputs.
72  ///
73  /// Calls \p nodeCallback for each node visited in the sparse
74  /// traversal.
75  ///
76  /// If \p callbackMode is set to CallbackModeTerminalNodes, then the
77  /// \p nodeCallback is only invoked on terminal nodes (i.e. nodes without
78  /// input connections). If it is set to CallbackModeAllNodes (which is the
79  /// default), then the callback is invoked on all nodes that are visited
80  /// by the traverser.
81  ///
82  /// If the callback returns \c false, then traversal halts locally
83  /// but prior branches of traversal continue.
84  ///
85  VDF_API
86  static void Traverse(
87  const VdfMaskedOutputVector &outputs,
88  const NodeCallback &nodeCallback,
89  CallbackMode callbackMode=CallbackModeAllNodes);
90 
91  /// Callback used when traversing a network.
92  ///
93  /// Called for each connection and dependency mask that is visited while
94  /// traversing nodes that affect values of the initial masked outputs.
95  ///
96  using ConnectionCallback = std::function<
97  bool (const VdfConnection &, const VdfMask &)> ;
98 
99  /// Traverses the network in the input direction, starting from the
100  /// masked outputs in \p outputs. The traversal is identical to the one
101  /// provided by Traverse(), except this method calls a connection callback
102  /// instad of a node callback.
103  ///
104  /// Calls \p connectionCallback for each connection visited in the sparse
105  /// traversal.
106  ///
107  /// If the callback returns \c false, then traversal along the supplied
108  /// connection stops, and traversal along sibling connections continues.
109  ///
110  VDF_API
111  static void TraverseWithConnectionCallback(
112  const VdfMaskedOutputVector &outputs,
113  const ConnectionCallback &connectionCallback);
114 
115  /// @}
116 
117 
118  /// \name Traversal with Path Reporting
119  /// @{
120 
121  /// Callback used when traversing a network with path information.
122  ///
123  /// Called for each node that is visited in the sparse traversal.
124  ///
125  /// The path to the visited node from the start is given by \p path and it
126  /// only contains nodes that have an affect on the requested outputs.
127  ///
128  using NodePathCallback = std::function<
129  bool (const VdfNode &node, const VdfObjectPtrVector &path)>;
130 
131  /// Callback used when traversing a network.
132  ///
133  /// Called for each connection and dependency mask that is visited that
134  /// affects values of the initial masked outputs. Note that the currently
135  /// visited connection isn't appended to the path yet.
136  ///
137  using ConnectionPathCallback = std::function<
138  bool (const VdfConnection &, const VdfMask &,
140 
141  /// Traverses the network in the input direction, starting from the
142  /// masked outputs in \p outputs, providing the traversal path to each
143  /// invocation of \p nodePathCallback.
144  ///
145  /// Calls \p nodePathCallback (if specified) for each node visited in the
146  /// sparse traversal. A sparse traversal only visits nodes that have an
147  /// affect on the requested outputs.
148  ///
149  /// Calls \p connectionCallback (if specified) for each node visited in the
150  /// sparse traversal. A sparse traversal only visits nodes that have an
151  /// affect on the requested outputs.
152  ///
153  /// If \p callbackMode is set to CallbackModeTerminalNodes, then
154  /// the \p nodeCallback is only invoked on terminal nodes (i.e.
155  /// nodes without input connections). If it is set to
156  /// CallbackModeAllNodes (which is the default), then the
157  /// callback is invoked on all nodes that are visited by the
158  /// traverser.
159  ///
160  VDF_API
161  static void TraverseWithPath(
162  const VdfMaskedOutputVector &outputs,
163  const NodePathCallback &nodePathCallback,
164  const ConnectionPathCallback &connectionPathCallback,
165  CallbackMode callbackMode=CallbackModeAllNodes);
166 
167  /// @}
168 
169 private:
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.
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 
236 
237 #endif
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
std::function< bool(const VdfConnection &, const VdfMask &, const VdfObjectPtrVector &)> ConnectionPathCallback
Definition: node.h:52
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:36
#define VDF_API
Definition: api.h:25
OutGridT const XformOp bool bool
std::vector< VdfObjectPtr > VdfObjectPtrVector
Definition: object.h:412
CallbackMode
Callback mode for the node callback.
A class used for fast sparse traversals of VdfNetworks in the output-to-input direction.
Invoke the node callback only on terminal nodes.
static VDF_API void Traverse(const VdfMaskedOutputVector &outputs, const NodeCallback &nodeCallback, CallbackMode callbackMode=CallbackModeAllNodes)
static VDF_API void TraverseWithConnectionCallback(const VdfMaskedOutputVector &outputs, const ConnectionCallback &connectionCallback)
std::function< bool(const VdfNode &)> NodeCallback
Invoke the node callback on all inputs. This is the default.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
static VDF_API void TraverseWithPath(const VdfMaskedOutputVector &outputs, const NodePathCallback &nodePathCallback, const ConnectionPathCallback &connectionPathCallback, CallbackMode callbackMode=CallbackModeAllNodes)
std::vector< VdfMaskedOutput > VdfMaskedOutputVector
state
Definition: core.h:2289
std::function< bool(const VdfConnection &, const VdfMask &)> ConnectionCallback
std::function< bool(const VdfNode &node, const VdfObjectPtrVector &path)> NodePathCallback