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