HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
leafNodeIndexer.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_EF_LEAF_NODE_INDEXER_H
8 #define PXR_EXEC_EF_LEAF_NODE_INDEXER_H
9 
10 ///\file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/node.h"
15 #include "pxr/exec/vdf/types.h"
16 
17 #include <tbb/concurrent_queue.h>
18 #include <tbb/concurrent_unordered_map.h>
19 #include <tbb/concurrent_vector.h>
20 
22 
23 class VdfConnection;
24 class VdfMask;
25 class VdfOutput;
26 
27 /// The leaf node indexer tracks leaf nodes added and removed from the network,
28 /// and associates each leaf node with a unique index.
29 ///
30 /// The indexer also maintains a list of the source outputs, each individual
31 /// leaf node is connected to. The size of the index space is relative to the
32 /// number of leaf nodes, rather than all nodes in the network.
33 ///
35 {
36 public:
37  /// Data type of the index.
38  ///
39  using Index = uint32_t;
40 
41  /// Sentinel for an invalid index.
42  ///
43  static constexpr Index InvalidIndex = Index(-1);
44 
45  /// Returns the capacity of the indexer, i.e. the high water mark of
46  /// tracked leaf nodes.
47  ///
48  size_t GetCapacity() const {
49  return _nodes.size();
50  }
51 
52  /// Returns an index for a given leaf \p node. Returns InvalidIndex if no
53  /// such index exists.
54  ///
55  Index GetIndex(const VdfNode &node) const;
56 
57  /// Returns the node for a given \p index. Returns \c nullptr if no such
58  /// node exists.
59  ///
60  const VdfNode *GetNode(Index index) const {
61  return index < _nodes.size() ? _nodes[index].node : nullptr;
62  }
63 
64  /// Returns the output a given leaf node \p index is sourcing data from.
65  /// Returns \c nullptr if no such output exists.
66  ///
68  return index < _nodes.size() ? _nodes[index].output : nullptr;
69  }
70 
71  /// Returns the mask at the output a given leaf node \p index is sourcing
72  /// data from. Returns \c nullptr if no such mask exists.
73  ///
74  const VdfMask *GetSourceMask(Index index) const {
75  return index < _nodes.size() ? _nodes[index].mask : nullptr;
76  }
77 
78  /// Invalidate the entire cache.
79  ///
80  void Invalidate();
81 
82  /// Call this to notify the cache of connections that have been deleted.
83  ///
84  /// \note It is safe to call DidDisconnect() and DidConnect() concurrently.
85  ///
86  void DidDisconnect(const VdfConnection &connection);
87 
88  /// Call this to notify the cache of newly added connections.
89  ///
90  /// \note It is safe to call DidDisconnect() and DidConnect() concurrently.
91  ///
92  void DidConnect(const VdfConnection &connection);
93 
94 private:
95  // The data tracked for each leaf node.
96  struct _LeafNode {
97  const VdfNode *node;
98  const VdfOutput *output;
99  const VdfMask *mask;
100  };
101 
102  // Map from VdfNode index to leaf node index. If a given node does not have
103  // an index, InvalidIndex will be stored at the corresponding location.
104  tbb::concurrent_unordered_map<VdfIndex, Index> _indices;
105 
106  // The tightly packed vector of leaf node data. The vector is indexed with
107  // the leaf node index.
108  tbb::concurrent_vector<_LeafNode> _nodes;
109 
110  // Free list of leaf node data. New indices are assigned by pulling from
111  // this list first.
112  tbb::concurrent_queue<Index> _freeList;
113 };
114 
115 inline
118 {
119  const auto it = _indices.find(VdfNode::GetIndexFromId(node.GetId()));
120  return it != _indices.end() ? it->second : InvalidIndex;
121 }
122 
124 
125 #endif
void DidConnect(const VdfConnection &connection)
void DidDisconnect(const VdfConnection &connection)
const VdfMask * GetSourceMask(Index index) const
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
size_t GetCapacity() const
Definition: node.h:52
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:36
Index GetIndex(const VdfNode &node) const
static constexpr Index InvalidIndex
VdfId GetId() const
Definition: node.h:116
GLint GLuint mask
Definition: glcorearb.h:124
static VdfIndex GetIndexFromId(const VdfId id)
Definition: node.h:123
const VdfOutput * GetSourceOutput(Index index) const
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const VdfNode * GetNode(Index index) const