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 private:
112 
113  static HdGpGenerativeProcedural *_ConstructProcedural(
114  const TfToken &typeName, const SdfPath &proceduralPrimPath);
115 
116 
117  // MEMBER TYPES ///////////////////////////////////////////////////////////
118 
119  using _DensePathSet = TfDenseHashSet<SdfPath, TfHash>;
120 
121  static void _CombinePathArrays(const _DensePathSet &s, SdfPathVector *v);
122 
123  struct _ProcEntry
124  {
125  enum State : unsigned char {
126  StateUncooked = 0,
127  StateDependenciesCooking,
128  StateDependenciesCooked,
129  StateCooking,
130  StateCooked,
131  };
132 
133  using _PathSetMap =
135 
136  std::atomic<State> state;
137  TfToken typeName;
138  std::shared_ptr<HdGpGenerativeProcedural> proc;
141  _PathSetMap childHierarchy;
142  std::mutex cookMutex;
143 
144 
145  _ProcEntry()
146  : state(StateUncooked)
147  {}
148 
149  _ProcEntry(const _ProcEntry &rhs)
150  {
151  state.store(rhs.state.load());
152  proc = rhs.proc;
153  typeName = rhs.typeName;
154  childTypes = rhs.childTypes;
155  dependencies = rhs.dependencies;
156  childHierarchy = rhs.childHierarchy;
157  }
158  };
159 
160  struct _GeneratedPrimEntry
161  {
162  _GeneratedPrimEntry()
163  : responsibleProc(nullptr)
164  {}
165 
166  _GeneratedPrimEntry(_ProcEntry * p)
167  : responsibleProc(p)
168  {}
169 
170  _GeneratedPrimEntry(const _GeneratedPrimEntry &rhs)
171  {
172  responsibleProc.store(rhs.responsibleProc.load());
173  }
174  std::atomic<_ProcEntry *> responsibleProc;
175  };
176 
177  using _GeneratedPrimsMap = tbb::concurrent_unordered_map<
178  SdfPath, _GeneratedPrimEntry, SdfPath::Hash>;
179 
180  using _ProcEntryMap =
181  std::unordered_map<SdfPath, _ProcEntry, TfHash>;
182 
183  using _PathSet = std::unordered_set<SdfPath, TfHash>;
184 
185  using _DependencyMap =
186  std::unordered_map<SdfPath, _PathSet, SdfPath::Hash>;
187 
188  struct _Notices
189  {
193  };
194 
195  // MEMBER FUNCTIONS ///////////////////////////////////////////////////////
196 
197  _ProcEntry * _UpdateProceduralDependencies(
198  const SdfPath &proceduralPrimPath) const;
199 
200  _ProcEntry * _UpdateProcedural(
201  const SdfPath &proceduralPrimPath,
202  bool forceUpdate,
203  _Notices *outputNotices,
205  *dirtiedDependencies = nullptr
206  ) const;
207 
208  void _RemoveProcedural(
209  const SdfPath &proceduralPrimPath,
210  _Notices *outputNotices=nullptr) const;
211 
212  // XXX Does thread-unsafe deletion.
213  // Removes deleted entries from _generatedPrims.
214  // This is private for now but intended for future use by a discussed formal
215  // method on HdSceneIndexBase itself.
216  void _GarbageCollect();
217 
218  // MEMBER VARIABLES ///////////////////////////////////////////////////////
219  // procedural prim path -> entry
220  mutable _ProcEntryMap _procedurals;
221 
222  // reverse mapping of dependency -> dependent roots
223  mutable _DependencyMap _dependencies;
224 
225  mutable _GeneratedPrimsMap _generatedPrims;
226 
227  // no shared mutex, shared/unique lock is the same
228  using _MapMutex = std::mutex;
229  using _MapLock = std::lock_guard<_MapMutex>;
230  mutable _MapMutex _dependenciesMutex;
231  mutable _MapMutex _proceduralsMutex;
232 
233  TfToken _targetPrimTypeName;
234 };
235 
237 
238 #endif
HdGpGenerativeProceduralResolvingSceneIndex(const HdSceneIndexBaseRefPtr &inputScene)
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1198
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:291
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:212
HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
SATISFYING HdSceneIndexBase ///////////////////////////////////////////.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
void _PrimsDirtied(const HdSceneIndexBase &sender, const HdSceneIndexObserver::DirtiedPrimEntries &entries) override
void _PrimsAdded(const HdSceneIndexBase &sender, const HdSceneIndexObserver::AddedPrimEntries &entries) override
SATISFYING HdSingleInputFilteringSceneIndexBase ///////////////////////.