HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
networkUtil.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_NETWORK_UTIL_H
8 #define PXR_EXEC_VDF_NETWORK_UTIL_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
16 #include "pxr/exec/vdf/input.h"
17 #include "pxr/exec/vdf/network.h"
18 #include "pxr/exec/vdf/node.h"
19 #include "pxr/exec/vdf/types.h"
20 
21 #include "pxr/base/tf/token.h"
22 
24 
25 class VdfMaskedOutput;
26 class VdfOutput;
27 
28 /// \name Traversal
29 ///
30 /// Used by clients to traverse networks, e.g., to perform invalidation.
31 ///
32 /// @{
33 
34 
35 /// Returns true, if maskedOutput is computed via speculation node.
36 ///
37 /// This does an upward traversal of the network and maybe slow.
38 ///
39 VDF_API
40 bool VdfIsSpeculating(const VdfMaskedOutput &maskedOutput);
41 
42 /// Searches for \p nodeToFind via topological (ie. not sparse) input
43 /// connections starting at \p startNode. Won't traverse over speculation
44 /// nodes.
45 ///
46 /// If \p foundSpecNode is non-null it will be populated when a speculation node
47 /// has been found while searching for \p nodeToFind.
48 ///
49 VDF_API
51  const VdfNode &startNode,
52  const VdfNode &nodeToFind,
53  bool *foundSpecNode = nullptr);
54 
55 /// Returns the output that is the source of the associated input of
56 /// \p output, if any and NULL otherwise. Note that if \p output does not
57 /// have an associated input, NULL is returned.
58 ///
59 VDF_API
61 
62 /// Empty callback node, does nothing.
63 ///
64 VDF_API
65 void VdfEmptyNodeCallback(const VdfNode &);
66 
67 /// Traverses nodes starting at \p startNode and moving along its inputs.
68 /// Calls \p callback on visited nodes.
69 ///
70 /// Nodes are guaranteed to be called at most once, though there is no
71 /// guarantee on the order of traversal.
72 ///
73 template <class Callable>
75  const VdfNode &startNode,
76  Callable &&callback)
77 {
78  // Keep track of the nodes we have already visited.
79  TfBits visited(startNode.GetNetwork().GetNodeCapacity());
80 
81  // Maintain a stack of nodes to traverse.
82  std::vector<const VdfNode *> stack(1, &startNode);
83 
84  // Keep traversing as long as there are entries on the stack.
85  while (!stack.empty()) {
86 
87  // The stack top is the node we are currently looking at.
88  const VdfNode *top = stack.back();
89  stack.pop_back();
90 
91  // Only consider this node, if it hasn't already been visited.
92  const VdfIndex idx = VdfNode::GetIndexFromId(top->GetId());
93  if (visited.IsSet(idx)) {
94  continue;
95  }
96  visited.Set(idx);
97 
98  // Invoke the callback for the node, and push the input dependencies
99  // on the stack, as long as the callback returns true.
100  if (std::forward<Callable>(callback)(*top)) {
101  for (const std::pair<TfToken, VdfInput *> &i :
102  top->GetInputsIterator()) {
103  for (const VdfConnection *c : i.second->GetConnections()) {
104  stack.push_back(&c->GetSourceNode());
105  }
106  }
107  }
108  }
109 }
110 
111 /// @}
112 
114 
115 #endif
116 
117 
const InputMapIterator GetInputsIterator() const
Definition: node.h:177
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
Definition: node.h:52
VDF_API bool VdfIsSpeculating(const VdfMaskedOutput &maskedOutput)
VDF_API bool VdfIsTopologicalSourceNode(const VdfNode &startNode, const VdfNode &nodeToFind, bool *foundSpecNode=nullptr)
#define VDF_API
Definition: api.h:25
Fast bit array that keeps track of the number of bits set and can find the next set in a timely manne...
Definition: bits.h:48
void VdfTraverseTopologicalSourceNodes(const VdfNode &startNode, Callable &&callback)
Definition: networkUtil.h:74
const VdfNetwork & GetNetwork() const
Definition: node.h:138
VdfId GetId() const
Definition: node.h:116
static VdfIndex GetIndexFromId(const VdfId id)
Definition: node.h:123
VDF_API void VdfEmptyNodeCallback(const VdfNode &)
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:31
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
VDF_API const VdfOutput * VdfGetAssociatedSourceOutput(const VdfOutput &output)
GLdouble GLdouble GLdouble top
Definition: glad.h:2817
size_t GetNodeCapacity() const
Definition: network.h:95
uint32_t VdfIndex
The index type for Vdf objects.
Definition: types.h:110