HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
generativeProceduralResolvingSceneIndex.h
Go to the documentation of this file.
1 //
2 // Copyright 2022 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_IMAGING_HD_GP_GENERATIVE_PROCEDURAL_RESOLVING_SCENE_INDEX_H
8 #define PXR_IMAGING_HD_GP_GENERATIVE_PROCEDURAL_RESOLVING_SCENE_INDEX_H
9 
13 
14 #include <tbb/concurrent_unordered_map.h>
15 #include <mutex>
16 #include <unordered_map>
17 #include <unordered_set>
18 
20 
21 /// \class HdGpGenerativeProceduralResolvingSceneIndex
22 ///
23 /// HdGpGenerativeProceduralResolvingSceneIndex is a scene index which
24 /// evaluates prims representing generative procedurals within its incoming
25 /// scene and outputs their resulting prims its own observers.
26 ///
27 /// The hydra prim type used to identify generative procedurals can be
28 /// configured per instance of this scene index to allow for a pipeline to
29 /// stage when certain procedural prims are resolved within the chain of scene
30 /// indicies. By default that type is "generativeProcedural".
31 ///
32 /// This scene index also re-types (to its observers) any procedural prim it
33 /// acts upon to be of type "resolvedGenerativeProcedural" to avoid potentially
34 /// evaluating a single procedural multiple times.
35 ///
36 /// In its current form, it does NOT recursively resolve any procedural prims
37 /// which are the result of the procedural prims for which it is itself
38 /// evaluting. Additionally, all procedural prims evaluated here see the same
39 /// input scene -- and not the results of other procedurals resolved by the
40 /// same scene index instance.
41 ///
44 
47 {
48 public:
49 
50  static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
51  const HdSceneIndexBaseRefPtr &inputScene) {
52  return TfCreateRefPtr(
54  }
55 
56  static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
57  const HdSceneIndexBaseRefPtr &inputScene,
58  const TfToken &targetPrimTypeName) {
59  return TfCreateRefPtr(
61  inputScene, targetPrimTypeName));
62  }
63 
64  /// SATISFYING HdSceneIndexBase ///////////////////////////////////////////
65 
66  HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
67  SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
68 
69 protected:
70 
72  const HdSceneIndexBaseRefPtr &inputScene);
73 
75  const HdSceneIndexBaseRefPtr &inputScene,
76  const TfToken &targetPrimTypeName);
77 
78  /// SATISFYING HdSingleInputFilteringSceneIndexBase ///////////////////////
79 
80  void _PrimsAdded(
81  const HdSceneIndexBase &sender,
82  const HdSceneIndexObserver::AddedPrimEntries &entries) override;
83 
84  void _PrimsRemoved(
85  const HdSceneIndexBase &sender,
86  const HdSceneIndexObserver::RemovedPrimEntries &entries) override;
87 
88  void _PrimsDirtied(
89  const HdSceneIndexBase &sender,
90  const HdSceneIndexObserver::DirtiedPrimEntries &entries) override;
91 
92  ///////////////////////////////////////////////////////////////////////////
93 
94  void _SystemMessage(
95  const TfToken &messageType,
96  const HdDataSourceBaseHandle &args) override;
97 
98 private:
99 
100  static HdGpGenerativeProcedural *_ConstructProcedural(
101  const TfToken &typeName, const SdfPath &proceduralPrimPath);
102 
103 
104  // MEMBER TYPES ///////////////////////////////////////////////////////////
105 
106  using _DensePathSet = TfDenseHashSet<SdfPath, TfHash>;
107 
108  static void _CombinePathArrays(const _DensePathSet &s, SdfPathVector *v);
109 
110  class _ProcEntry : public TfWeakBase
111  {
112  public:
113  enum State : unsigned char {
114  StateUncooked = 0,
115  StateDependenciesCooking,
116  StateDependenciesCooked,
117  StateCooking,
118  StateCooked,
119  };
120 
121  using _PathSetMap =
123 
124  std::atomic<State> state;
125  TfToken typeName;
126  std::shared_ptr<HdGpGenerativeProcedural> proc;
129  _PathSetMap childHierarchy;
130  std::mutex cookMutex;
131 
132 
133  _ProcEntry()
134  : state(StateUncooked)
135  {}
136 
137  _ProcEntry(const _ProcEntry &rhs)
138  {
139  state.store(rhs.state.load());
140  proc = rhs.proc;
141  typeName = rhs.typeName;
142  childTypes = rhs.childTypes;
143  dependencies = rhs.dependencies;
144  childHierarchy = rhs.childHierarchy;
145  }
146  };
147 
148  TF_DECLARE_WEAK_PTRS(_ProcEntry);
149 
150  struct _GeneratedPrimEntry
151  {
152  _GeneratedPrimEntry()
153  : responsibleProc(nullptr)
154  {}
155 
156  _GeneratedPrimEntry(_ProcEntry * p)
157  : responsibleProc(p)
158  {}
159 
160  _GeneratedPrimEntry(const _GeneratedPrimEntry &rhs)
161  {
162  responsibleProc.store(rhs.responsibleProc.load());
163  }
164  std::atomic<_ProcEntry *> responsibleProc;
165  };
166 
167  using _GeneratedPrimsMap = tbb::concurrent_unordered_map<
168  SdfPath, _GeneratedPrimEntry, SdfPath::Hash>;
169 
170  using _ProcEntryMap =
171  std::unordered_map<SdfPath, _ProcEntry, TfHash>;
172 
173  using _WeakProcEntryMap =
174  tbb::concurrent_unordered_map<SdfPath, _ProcEntryPtr, TfHash>;
175 
176  using _PathSet = std::unordered_set<SdfPath, TfHash>;
177 
178  using _DependencyMap =
179  std::unordered_map<SdfPath, _PathSet, SdfPath::Hash>;
180 
181  struct _Notices
182  {
186  };
187 
188  // MEMBER FUNCTIONS ///////////////////////////////////////////////////////
189 
190  _ProcEntry * _UpdateProceduralDependencies(
191  const SdfPath &proceduralPrimPath,
192  _Notices* outputNotices) const;
193 
194  _ProcEntry * _UpdateProcedural(
195  const SdfPath &proceduralPrimPath,
196  bool forceUpdate,
197  _Notices *outputNotices,
199  *dirtiedDependencies = nullptr
200  ) const;
201 
202 
203  void _UpdateProceduralResult(
204  _ProcEntry *procEntry,
205  const SdfPath &proceduralPrimPath,
206  const HdGpGenerativeProcedural::ChildPrimTypeMap &newChildTypes,
207  _Notices *outputNotices) const;
208 
209 
210  void _RemoveProcedural(
211  const SdfPath &proceduralPrimPath,
212  _Notices *outputNotices=nullptr) const;
213 
214  // XXX Does thread-unsafe deletion.
215  // Removes deleted entries from _generatedPrims.
216  // This is private for now but intended for future use by a discussed formal
217  // method on HdSceneIndexBase itself.
218  void _GarbageCollect();
219 
220  // MEMBER VARIABLES ///////////////////////////////////////////////////////
221  // procedural prim path -> entry
222  mutable _ProcEntryMap _procedurals;
223 
224  mutable _WeakProcEntryMap _activeSyncProcedurals;
225 
226  // reverse mapping of dependency -> dependent roots
227  mutable _DependencyMap _dependencies;
228 
229  mutable _GeneratedPrimsMap _generatedPrims;
230 
231  // no shared mutex, shared/unique lock is the same
232  using _MapMutex = std::mutex;
233  using _MapLock = std::lock_guard<_MapMutex>;
234  mutable _MapMutex _dependenciesMutex;
235  mutable _MapMutex _proceduralsMutex;
236 
237  TfToken _targetPrimTypeName;
238 
239  bool _attemptAsync;
240 };
241 
243 
244 #endif
HdGpGenerativeProceduralResolvingSceneIndex(const HdSceneIndexBaseRefPtr &inputScene)
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
const GLdouble * v
Definition: glcorearb.h:837
GLdouble s
Definition: glad.h:3009
TF_DECLARE_REF_PTRS(HdGpGenerativeProceduralResolvingSceneIndex)
static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(const HdSceneIndexBaseRefPtr &inputScene, const TfToken &targetPrimTypeName)
Definition: token.h:70
SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override
std::vector< class SdfPath > SdfPathVector
static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(const HdSceneIndexBaseRefPtr &inputScene)
void _PrimsRemoved(const HdSceneIndexBase &sender, const HdSceneIndexObserver::RemovedPrimEntries &entries) override
Definition: path.h:273
HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
SATISFYING HdSceneIndexBase ///////////////////////////////////////////.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Processor proc(inIter, Adapter::tree(outGrid), op, merge)
void _PrimsDirtied(const HdSceneIndexBase &sender, const HdSceneIndexObserver::DirtiedPrimEntries &entries) override
**If you just want to fire and args
Definition: thread.h:618
void _SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args) override
state
Definition: core.h:2289
void _PrimsAdded(const HdSceneIndexBase &sender, const HdSceneIndexObserver::AddedPrimEntries &entries) override
SATISFYING HdSingleInputFilteringSceneIndexBase ///////////////////////.