All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
instanceCache.h
1 //
2 // Copyright 2016 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 USD_INSTANCE_CACHE_H
25 #define USD_INSTANCE_CACHE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/instanceKey.h"
29 
30 #include "pxr/usd/sdf/path.h"
31 #include "pxr/base/tf/hashmap.h"
32 
33 #include <tbb/mutex.h>
34 #include <map>
35 #include <unordered_map>
36 #include <vector>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 
46 class Usd_InstanceChanges
47 {
48 public:
49  void AppendChanges(const Usd_InstanceChanges& c)
50  {
51  newMasterPrims.insert(
52  newMasterPrims.end(),
53  c.newMasterPrims.begin(),
54  c.newMasterPrims.end());
55 
56  newMasterPrimIndexes.insert(
57  newMasterPrimIndexes.end(),
58  c.newMasterPrimIndexes.begin(),
59  c.newMasterPrimIndexes.end());
60 
61  changedMasterPrims.insert(
62  changedMasterPrims.end(),
63  c.changedMasterPrims.begin(),
64  c.changedMasterPrims.end());
65 
66  changedMasterPrimIndexes.insert(
67  changedMasterPrimIndexes.end(),
68  c.changedMasterPrimIndexes.begin(),
69  c.changedMasterPrimIndexes.end());
70 
71  deadMasterPrims.insert(
72  deadMasterPrims.end(),
73  c.deadMasterPrims.begin(), c.deadMasterPrims.end());
74  }
75 
78  std::vector<SdfPath> newMasterPrims;
79  std::vector<SdfPath> newMasterPrimIndexes;
80 
83  std::vector<SdfPath> changedMasterPrims;
84  std::vector<SdfPath> changedMasterPrimIndexes;
85 
87  std::vector<SdfPath> deadMasterPrims;
88 };
89 
123 class Usd_InstanceCache
124 {
125  Usd_InstanceCache(Usd_InstanceCache const &) = delete;
126  Usd_InstanceCache &operator=(Usd_InstanceCache const &) = delete;
127 public:
128  Usd_InstanceCache();
129 
140  bool RegisterInstancePrimIndex(const PcpPrimIndex& index,
141  const UsdStagePopulationMask *mask,
142  const UsdStageLoadRules &loadRules);
143 
147  void UnregisterInstancePrimIndexesUnder(const SdfPath& primIndexPath);
148 
152  void ProcessChanges(Usd_InstanceChanges* changes);
153 
156  static bool IsPathInMaster(const SdfPath& path);
157 
160  std::vector<SdfPath> GetAllMasters() const;
161 
164  size_t GetNumMasters() const;
165 
173  SdfPath GetMasterUsingPrimIndexPath(const SdfPath& primIndexPath) const;
174 
187  std::vector<SdfPath>
188  GetPrimsInMastersUsingPrimIndexPath(const SdfPath& primIndexPath) const;
189 
192  bool MasterUsesPrimIndexPath(const SdfPath& primIndexPath) const;
193 
198  SdfPath
199  GetMasterForInstanceablePrimIndexPath(const SdfPath& primIndexPath) const;
200 
204  bool IsPathDescendantToAnInstance(const SdfPath& primPath) const;
205 
208  SdfPath GetMostAncestralInstancePath(const SdfPath &primPath) const;
209 
213  SdfPath GetPathInMasterForInstancePath(const SdfPath& primPath) const;
214 
215 private:
216  typedef std::vector<SdfPath> _PrimIndexPaths;
217 
218  void _CreateOrUpdateMasterForInstances(
219  const Usd_InstanceKey& instanceKey,
220  _PrimIndexPaths* primIndexPaths,
221  Usd_InstanceChanges* changes,
222  std::unordered_map<SdfPath, SdfPath, SdfPath::Hash> const &
223  masterToOldSourceIndexPath);
224 
225  void _RemoveInstances(
226  const Usd_InstanceKey& instanceKey,
227  const _PrimIndexPaths& primIndexPaths,
228  Usd_InstanceChanges* changes,
229  std::unordered_map<SdfPath, SdfPath, SdfPath::Hash> *
230  masterToOldSourceIndexPath);
231 
232  void _RemoveMasterIfNoInstances(
233  const Usd_InstanceKey& instanceKey,
234  Usd_InstanceChanges* changes);
235 
236  bool _MasterUsesPrimIndexPath(
237  const SdfPath& primIndexPath,
238  std::vector<SdfPath>* masterPaths = nullptr) const;
239 
240  SdfPath _GetNextMasterPath(const Usd_InstanceKey& key);
241 
242 private:
243  tbb::spin_mutex _mutex;
244 
245  // Mapping from instance key <-> master prim path.
246  // This stores the path of the master prim that should be used
247  // for all instanceable prim indexes with the given instance key.
248  typedef TfHashMap<Usd_InstanceKey, SdfPath, boost::hash<Usd_InstanceKey> >
249  _InstanceKeyToMasterMap;
250  typedef TfHashMap<SdfPath, Usd_InstanceKey, SdfPath::Hash>
251  _MasterToInstanceKeyMap;
252  _InstanceKeyToMasterMap _instanceKeyToMasterMap;
253  _MasterToInstanceKeyMap _masterToInstanceKeyMap;
254 
255  // Mapping from instance prim index path <-> master prim path.
256  // This map stores which prim index serves as the source index
257  // for a given master prim.
258  typedef std::map<SdfPath, SdfPath> _SourcePrimIndexToMasterMap;
259  typedef std::map<SdfPath, SdfPath> _MasterToSourcePrimIndexMap;
260  _SourcePrimIndexToMasterMap _sourcePrimIndexToMasterMap;
261  _MasterToSourcePrimIndexMap _masterToSourcePrimIndexMap;
262 
263  // Mapping from master prim path <-> list of instanceable prim indexes
264  // This map stores which instanceable prim indexes have been assigned to
265  // a master prim.
266  typedef std::map<SdfPath, _PrimIndexPaths> _MasterToPrimIndexesMap;
267  typedef std::map<SdfPath, SdfPath> _PrimIndexToMasterMap;
268  _MasterToPrimIndexesMap _masterToPrimIndexesMap;
269  _PrimIndexToMasterMap _primIndexToMasterMap;
270 
271  // Map from instance key -> list of prim index paths
272  // These maps contain lists of pending changes and are the only containers
273  // that should be modified during registration and unregistration.
274  typedef TfHashMap<
275  Usd_InstanceKey, _PrimIndexPaths, boost::hash<Usd_InstanceKey> >
276  _InstanceKeyToPrimIndexesMap;
277  _InstanceKeyToPrimIndexesMap _pendingAddedPrimIndexes;
278  _InstanceKeyToPrimIndexesMap _pendingRemovedPrimIndexes;
279 
280  // Index of last master prim created. Used to create
281  // master prim names.
282  size_t _lastMasterIndex;
283 };
284 
285 
286 PXR_NAMESPACE_CLOSE_SCOPE
287 
288 #endif // USD_INSTANCE_CACHE_H
PcpPrimIndex is an index of the all sites of scene description that contribute opinions to a specific...
Definition: primIndex.h:77
This class represents rules that govern payload inclusion on UsdStages.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
This class represents a mask that may be applied to a UsdStage to limit the set of UsdPrim s it popul...