HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparseVectorizedOutputTraverser.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_VECTORIZED_OUTPUT_TRAVERSER_H
8 #define PXR_EXEC_VDF_SPARSE_VECTORIZED_OUTPUT_TRAVERSER_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
15 #include "pxr/exec/vdf/mask.h"
21 
22 #include <tbb/concurrent_unordered_map.h>
23 
24 #include <functional>
25 #include <map>
26 #include <vector>
27 
29 
30 class VdfNode;
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 ///
34 /// \class VdfSparseVectorizedOutputTraverser
35 ///
36 /// Traverses a VdfNetwork in the input-to-output direction, while treating
37 /// each output in the traversal request as a separate traversal.
38 ///
40 {
41 public:
42 
43  /// The callback invoked for all all terminal nodes. The integer parameter
44  /// indicates which entry in the traversal request lead to the given node.
45  ///
46  using NodeCallback = std::function<bool (const VdfNode &, size_t)>;
47 
48  /// Starts a traversal with the given \p outputs request and node
49  /// \p callback. The callback will be invoked for each terminal node
50  /// visited. Note that \p callback MUST be thread-safe, since it may be
51  /// invoked concurrently!
52  ///
53  VDF_API
54  void Traverse(
55  const VdfMaskedOutputVector &outputs,
56  const NodeCallback &callback);
57 
58  /// Invalidate the internal traversal cache.
59  ///
60  VDF_API
61  void Invalidate();
62 
63 private:
64 
65  // A cached dependency on a pool output.
66  struct _PoolDependency {
67  VdfPoolChainIndex poolChainIndex;
68  VdfMaskedOutput maskedOutput;
69  };
70 
71  // An entry with cached dependencies.
72  struct _Dependencies {
76  };
77 
78  // A pair of output pointer and mask pointer used for building the
79  // traversal stack (and queue.) The pointer-to-mask is used in order to
80  // avoid expensive traffic on the mask ref count.
81  struct _OutputAndMask {
82  VdfOutput *output;
83  const VdfMask *mask;
84  };
85 
86  // A map with entries of outputs that have been visited, and with which
87  // mask these outputs have been visited with.
88  using _VisitedOutputs =
90 
91  // The type of output stack used to guide the traversal.
92  using _OutputStack = std::vector<_OutputAndMask>;
93 
94  // The type of queue used to guide the traversal along the pool.
95  using _PoolQueue = std::map<const VdfPoolChainIndex, _OutputAndMask>;
96 
97  // Returns true if the output should be visited and false if it already has
98  // been visited with the given mask.
99  bool _Visit(
100  const _OutputAndMask &outputAndMask,
101  _VisitedOutputs *visitedOutputs);
102 
103  // Start a new traversal at the given output.
104  void _Traverse(
105  size_t index,
106  const VdfMaskedOutput &maskedOutput,
107  const NodeCallback &callback);
108 
109  // Visits a single output.
110  void _TraverseOutput(
111  size_t index,
112  const _OutputAndMask &outputAndMask,
113  const NodeCallback &callback,
114  _OutputStack *stack,
115  _PoolQueue *queue);
116 
117  // Queue a pool output.
118  void _QueuePoolOutput(
119  const VdfPoolChainIndex &poolChainIndex,
120  const _OutputAndMask &outputAndMask,
121  _PoolQueue *queue);
122 
123  // Take a shortcut through the pool, if possible.
124  bool _TakePoolShortcut(
125  const _OutputAndMask &outputAndMask,
126  _PoolQueue *queue);
127 
128  // Retrieves the dependencies for a single output, if cached, or computes
129  // dependencies of uncached.
130  const _Dependencies &_GetDependencies(
131  const _OutputAndMask &outputAndMask);
132 
133  // Computes the dependencies for a single output.
134  void _ComputeDependencies(
135  const _OutputAndMask &outputAndMask,
136  _Dependencies *dependencies);
137 
138  // The cached dependencies.
139  using _DependencyMap = tbb::concurrent_unordered_map<
140  VdfMaskedOutput, _Dependencies, VdfMaskedOutput::Hash>;
141  _DependencyMap _dependencyMap;
142 
143  // The memoized mask operations.
145 };
146 
148 
149 #endif
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
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
VDF_API void Traverse(const VdfMaskedOutputVector &outputs, const NodeCallback &callback)
std::function< bool(const VdfNode &, size_t)> NodeCallback
GLint GLuint mask
Definition: glcorearb.h:124
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the queue
Definition: thread.h:632
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:31
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::vector< VdfMaskedOutput > VdfMaskedOutputVector