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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_GP_GENERATIVE_PROCEDURAL_RESOLVING_SCENE_INDEX_H
25 #define PXR_IMAGING_HD_GP_GENERATIVE_PROCEDURAL_RESOLVING_SCENE_INDEX_H
26 
30 
31 #include <tbb/concurrent_unordered_map.h>
32 #include <mutex>
33 #include <unordered_map>
34 #include <unordered_set>
35 
37 
38 /// \class HdGpGenerativeProceduralResolvingSceneIndex
39 ///
40 /// HdGpGenerativeProceduralResolvingSceneIndex is a scene index which
41 /// evaluates prims representing generative procedurals within its incoming
42 /// scene and outputs their resulting prims its own observers.
43 ///
44 /// The hydra prim type used to identify generative procedurals can be
45 /// configured per instance of this scene index to allow for a pipeline to
46 /// stage when certain procedural prims are resolved within the chain of scene
47 /// indicies. By default that type is "generativeProcedural".
48 ///
49 /// This scene index also re-types (to its observers) any procedural prim it
50 /// acts upon to be of type "resolvedGenerativeProcedural" to avoid potentially
51 /// evaluating a single procedural multiple times.
52 ///
53 /// In its current form, it does NOT recursively resolve any procedural prims
54 /// which are the result of the procedural prims for which it is itself
55 /// evaluting. Additionally, all procedural prims evaluated here see the same
56 /// input scene -- and not the results of other procedurals resolved by the
57 /// same scene index instance.
58 ///
61 
64 {
65 public:
66 
67  static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
68  const HdSceneIndexBaseRefPtr &inputScene) {
69  return TfCreateRefPtr(
71  }
72 
73  static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
74  const HdSceneIndexBaseRefPtr &inputScene,
75  const TfToken &targetPrimTypeName) {
76  return TfCreateRefPtr(
78  inputScene, targetPrimTypeName));
79  }
80 
81  /// SATISFYING HdSceneIndexBase ///////////////////////////////////////////
82 
83  HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
84  SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
85 
86 protected:
87 
89  const HdSceneIndexBaseRefPtr &inputScene);
90 
92  const HdSceneIndexBaseRefPtr &inputScene,
93  const TfToken &targetPrimTypeName);
94 
95  /// SATISFYING HdSingleInputFilteringSceneIndexBase ///////////////////////
96 
97  void _PrimsAdded(
98  const HdSceneIndexBase &sender,
99  const HdSceneIndexObserver::AddedPrimEntries &entries) override;
100 
101  void _PrimsRemoved(
102  const HdSceneIndexBase &sender,
103  const HdSceneIndexObserver::RemovedPrimEntries &entries) override;
104 
105  void _PrimsDirtied(
106  const HdSceneIndexBase &sender,
107  const HdSceneIndexObserver::DirtiedPrimEntries &entries) override;
108 
109  ///////////////////////////////////////////////////////////////////////////
110 
111  void _SystemMessage(
112  const TfToken &messageType,
113  const HdDataSourceBaseHandle &args) override;
114 
115 private:
116 
117  static HdGpGenerativeProcedural *_ConstructProcedural(
118  const TfToken &typeName, const SdfPath &proceduralPrimPath);
119 
120 
121  // MEMBER TYPES ///////////////////////////////////////////////////////////
122 
123  using _DensePathSet = TfDenseHashSet<SdfPath, TfHash>;
124 
125  static void _CombinePathArrays(const _DensePathSet &s, SdfPathVector *v);
126 
127  struct _ProcEntry : public TfWeakBase
128  {
129  enum State : unsigned char {
130  StateUncooked = 0,
131  StateDependenciesCooking,
132  StateDependenciesCooked,
133  StateCooking,
134  StateCooked,
135  };
136 
137  using _PathSetMap =
139 
140  std::atomic<State> state;
141  TfToken typeName;
142  std::shared_ptr<HdGpGenerativeProcedural> proc;
145  _PathSetMap childHierarchy;
146  std::mutex cookMutex;
147 
148 
149  _ProcEntry()
150  : state(StateUncooked)
151  {}
152 
153  _ProcEntry(const _ProcEntry &rhs)
154  {
155  state.store(rhs.state.load());
156  proc = rhs.proc;
157  typeName = rhs.typeName;
158  childTypes = rhs.childTypes;
159  dependencies = rhs.dependencies;
160  childHierarchy = rhs.childHierarchy;
161  }
162  };
163 
164  TF_DECLARE_WEAK_PTRS(_ProcEntry);
165 
166  struct _GeneratedPrimEntry
167  {
168  _GeneratedPrimEntry()
169  : responsibleProc(nullptr)
170  {}
171 
172  _GeneratedPrimEntry(_ProcEntry * p)
173  : responsibleProc(p)
174  {}
175 
176  _GeneratedPrimEntry(const _GeneratedPrimEntry &rhs)
177  {
178  responsibleProc.store(rhs.responsibleProc.load());
179  }
180  std::atomic<_ProcEntry *> responsibleProc;
181  };
182 
183  using _GeneratedPrimsMap = tbb::concurrent_unordered_map<
184  SdfPath, _GeneratedPrimEntry, SdfPath::Hash>;
185 
186  using _ProcEntryMap =
187  std::unordered_map<SdfPath, _ProcEntry, TfHash>;
188 
189  using _WeakProcEntryMap =
190  tbb::concurrent_unordered_map<SdfPath, _ProcEntryPtr, TfHash>;
191 
192  using _PathSet = std::unordered_set<SdfPath, TfHash>;
193 
194  using _DependencyMap =
195  std::unordered_map<SdfPath, _PathSet, SdfPath::Hash>;
196 
197  struct _Notices
198  {
202  };
203 
204  // MEMBER FUNCTIONS ///////////////////////////////////////////////////////
205 
206  _ProcEntry * _UpdateProceduralDependencies(
207  const SdfPath &proceduralPrimPath) const;
208 
209  _ProcEntry * _UpdateProcedural(
210  const SdfPath &proceduralPrimPath,
211  bool forceUpdate,
212  _Notices *outputNotices,
214  *dirtiedDependencies = nullptr
215  ) const;
216 
217 
218  void _UpdateProceduralResult(
219  _ProcEntry *procEntry,
220  const SdfPath &proceduralPrimPath,
221  const HdGpGenerativeProcedural::ChildPrimTypeMap &newChildTypes,
222  _Notices *outputNotices) const;
223 
224 
225  void _RemoveProcedural(
226  const SdfPath &proceduralPrimPath,
227  _Notices *outputNotices=nullptr) const;
228 
229  // XXX Does thread-unsafe deletion.
230  // Removes deleted entries from _generatedPrims.
231  // This is private for now but intended for future use by a discussed formal
232  // method on HdSceneIndexBase itself.
233  void _GarbageCollect();
234 
235  // MEMBER VARIABLES ///////////////////////////////////////////////////////
236  // procedural prim path -> entry
237  mutable _ProcEntryMap _procedurals;
238 
239  mutable _WeakProcEntryMap _activeSyncProcedurals;
240 
241  // reverse mapping of dependency -> dependent roots
242  mutable _DependencyMap _dependencies;
243 
244  mutable _GeneratedPrimsMap _generatedPrims;
245 
246  // no shared mutex, shared/unique lock is the same
247  using _MapMutex = std::mutex;
248  using _MapLock = std::lock_guard<_MapMutex>;
249  mutable _MapMutex _dependenciesMutex;
250  mutable _MapMutex _proceduralsMutex;
251 
252  TfToken _targetPrimTypeName;
253 
254  bool _attemptAsync;
255 };
256 
258 
259 #endif
HdGpGenerativeProceduralResolvingSceneIndex(const HdSceneIndexBaseRefPtr &inputScene)
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1223
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:87
SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override
static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(const HdSceneIndexBaseRefPtr &inputScene)
void _PrimsRemoved(const HdSceneIndexBase &sender, const HdSceneIndexObserver::RemovedPrimEntries &entries) override
Definition: path.h:290
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:211
HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
SATISFYING HdSceneIndexBase ///////////////////////////////////////////.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
void _PrimsDirtied(const HdSceneIndexBase &sender, const HdSceneIndexObserver::DirtiedPrimEntries &entries) override
**If you just want to fire and args
Definition: thread.h:609
void _SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args) override
void _PrimsAdded(const HdSceneIndexBase &sender, const HdSceneIndexObserver::AddedPrimEntries &entries) override
SATISFYING HdSingleInputFilteringSceneIndexBase ///////////////////////.