generativeProceduralResolvingSceneIndex.h
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 
27 #include "pxr/imaging/hdGp/generativeProcedural.h"
28 #include "pxr/imaging/hd/filteringSceneIndex.h"
30 
31 #include <tbb/concurrent_unordered_map.h>
32 #include <mutex>
33 #include <unordered_map>
34 #include <unordered_set>
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
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 
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 
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 
110 
111 private:
112 
113  static HdGpGenerativeProcedural *_ConstructProcedural(
114  const TfToken &typeName, const SdfPath &proceduralPrimPath);
115 
116 
117  // MEMBER TYPES ///////////////////////////////////////////////////////////
118 
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 
236 PXR_NAMESPACE_CLOSE_SCOPE
237 
238 #endif
An abstract base class for a filtering scene index that observes a single input scene index.
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:49
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HdGpGenerativeProcedural is the base class for procedurals which have full access to an input scene i...
Abstract interface to scene data.
Definition: sceneIndex.h:62
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:75
This is a space efficient container that mimics the TfHashSet API that uses a vector for storage when...
Definition: denseHashSet.h:58
HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
SATISFYING HdSceneIndexBase ///////////////////////////////////////////.
HdGpGenerativeProceduralResolvingSceneIndex is a scene index which evaluates prims representing gener...
SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override
Returns the paths of all scene index prims located immediately below primPath.