generativeProceduralResolvingSceneIndex.h
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"
12 #include "pxr/imaging/hdGp/generativeProcedural.h"
14 #include "pxr/imaging/hd/filteringSceneIndex.h"
15 
16 #include <tbb/concurrent_unordered_map.h>
17 #include <mutex>
18 #include <unordered_map>
19 #include <unordered_set>
20 
21 PXR_NAMESPACE_OPEN_SCOPE
22 
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 
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 
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 
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 
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 
248 PXR_NAMESPACE_CLOSE_SCOPE
249 
250 #endif
An abstract base class for a filtering scene index that observes a single input scene index.
void _SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args) override
Implement in order to react directly to system messages sent from downstream.
void _PrimsAdded(const HdSceneIndexBase &sender, const HdSceneIndexObserver::AddedPrimEntries &entries) override
SATISFYING HdSingleInputFilteringSceneIndexBase ///////////////////////.
Small struct representing a 'prim' in the Hydra scene index.
Definition: sceneIndex.h:35
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
HdGpGenerativeProcedural is the base class for procedurals which have full access to an input scene i...
Abstract interface to scene data.
Definition: sceneIndex.h:54
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:58
HDGP_API SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override
Returns the paths of all scene index prims located immediately below primPath.
A hash set with contiguous storage, suitable for use with TfSpan, e.g.
Definition: denseHashSet.h:39
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
HdGpGenerativeProceduralResolvingSceneIndex is a scene index which evaluates prims representing gener...
HDGP_API HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
SATISFYING HdSceneIndexBase ///////////////////////////////////////////.