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 <unordered_map>
33 #include <unordered_set>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
60 
63 {
64 public:
65 
66  static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
67  const HdSceneIndexBaseRefPtr &inputScene) {
68  return TfCreateRefPtr(
70  }
71 
72  static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
73  const HdSceneIndexBaseRefPtr &inputScene,
74  const TfToken &targetPrimTypeName) {
75  return TfCreateRefPtr(
77  inputScene, targetPrimTypeName));
78  }
79 
81 
82  HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
83  SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
84 
85 protected:
86 
88  const HdSceneIndexBaseRefPtr &inputScene);
89 
91  const HdSceneIndexBaseRefPtr &inputScene,
92  const TfToken &targetPrimTypeName);
93 
95 
96  void _PrimsAdded(
97  const HdSceneIndexBase &sender,
98  const HdSceneIndexObserver::AddedPrimEntries &entries) override;
99 
100  void _PrimsRemoved(
101  const HdSceneIndexBase &sender,
102  const HdSceneIndexObserver::RemovedPrimEntries &entries) override;
103 
104  void _PrimsDirtied(
105  const HdSceneIndexBase &sender,
106  const HdSceneIndexObserver::DirtiedPrimEntries &entries) override;
107 
109 
110 private:
111 
112  static HdGpGenerativeProcedural *_ConstructProcedural(
113  const TfToken &typeName, const SdfPath &proceduralPrimPath);
114 
115 
116  // MEMBER TYPES ///////////////////////////////////////////////////////////
117 
118  struct _ProcEntry
119  {
120  enum State : unsigned char {
121  StateUncooked = 0,
122  StateDependenciesCooking,
123  StateDependenciesCooked,
124  StateCooking,
125  StateCooked,
126  };
127 
128  using _PathSetMap =
130 
131  std::atomic<State> state;
132  TfToken typeName;
133  std::shared_ptr<HdGpGenerativeProcedural> proc;
136  _PathSetMap childHierarchy;
137  std::mutex cookMutex;
138 
139 
140  _ProcEntry()
141  : state(StateUncooked)
142  {}
143 
144  _ProcEntry(const _ProcEntry &rhs)
145  {
146  state.store(rhs.state.load());
147  proc = rhs.proc;
148  typeName = rhs.typeName;
149  childTypes = rhs.childTypes;
150  dependencies = rhs.dependencies;
151  childHierarchy = rhs.childHierarchy;
152  }
153  };
154 
155  struct _GeneratedPrimEntry
156  {
157  _GeneratedPrimEntry()
158  : responsibleProc(nullptr)
159  {}
160 
161  _GeneratedPrimEntry(_ProcEntry * p)
162  : responsibleProc(p)
163  {}
164 
165  _GeneratedPrimEntry(const _GeneratedPrimEntry &rhs)
166  {
167  responsibleProc.store(rhs.responsibleProc.load());
168  }
169  std::atomic<_ProcEntry *> responsibleProc;
170  };
171 
172  using _GeneratedPrimsMap = tbb::concurrent_unordered_map<
173  SdfPath, _GeneratedPrimEntry, SdfPath::Hash>;
174 
175  using _ProcEntryMap =
176  std::unordered_map<SdfPath, _ProcEntry, TfHash>;
177 
178  using _PathSet = std::unordered_set<SdfPath, TfHash>;
179 
180  using _DependencyMap =
181  std::unordered_map<SdfPath, _PathSet, SdfPath::Hash>;
182 
183  struct _Notices
184  {
188  };
189 
190  // MEMBER FUNCTIONS ///////////////////////////////////////////////////////
191 
192  _ProcEntry * _UpdateProceduralDependencies(
193  const SdfPath &proceduralPrimPath) const;
194 
195  _ProcEntry * _UpdateProcedural(
196  const SdfPath &proceduralPrimPath,
197  bool forceUpdate,
198  _Notices *outputNotices,
200  *dirtiedDependencies = nullptr
201  ) const;
202 
203  void _RemoveProcedural(
204  const SdfPath &proceduralPrimPath,
205  _Notices *outputNotices=nullptr) const;
206 
207  // XXX Does thread-unsafe deletion.
208  // Removes deleted entries from _generatedPrims.
209  // This is private for now but intended for future use by a discussed formal
210  // method on HdSceneIndexBase itself.
211  void _GarbageCollect();
212 
213  // MEMBER VARIABLES ///////////////////////////////////////////////////////
214  // procedural prim path -> entry
215  mutable _ProcEntryMap _procedurals;
216 
217  // reverse mapping of dependency -> dependent roots
218  mutable _DependencyMap _dependencies;
219 
220  mutable _GeneratedPrimsMap _generatedPrims;
221 
222  // no shared mutex, shared/unique lock is the same
223  using _MapMutex = std::mutex;
224  using _MapLock = std::lock_guard<_MapMutex>;
225  mutable _MapMutex _dependenciesMutex;
226  mutable _MapMutex _proceduralsMutex;
227 
228  TfToken _targetPrimTypeName;
229 };
230 
231 PXR_NAMESPACE_CLOSE_SCOPE
232 
233 #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 ///////////////////////.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:447
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
This is a space efficient container that mimics the TfHashMap API that uses a vector for storage when...
Definition: denseHashMap.h:59
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
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.