HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primIndex.h
Go to the documentation of this file.
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 PXR_USD_PCP_PRIM_INDEX_H
25 #define PXR_USD_PCP_PRIM_INDEX_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/pcp/api.h"
30 #include "pxr/usd/pcp/dependency.h"
33 #include "pxr/usd/pcp/errors.h"
34 #include "pxr/usd/pcp/iterator.h"
35 #include "pxr/usd/pcp/node.h"
36 #include "pxr/usd/pcp/types.h"
38 #include "pxr/usd/sdf/site.h"
40 #include "pxr/base/tf/hashmap.h"
41 #include "pxr/base/tf/hashset.h"
42 
43 #include <tbb/spin_rw_mutex.h>
44 
45 #include <functional>
46 #include <map>
47 #include <memory>
48 #include <unordered_set>
49 
51 
54 
56 TF_DECLARE_WEAK_AND_REF_PTRS(PcpPrimIndex_Graph);
57 
58 class ArResolver;
59 class PcpCache;
60 class PcpPrimIndex;
61 class PcpPrimIndexInputs;
63 class SdfPath;
64 
65 /// \class PcpPrimIndex
66 ///
67 /// PcpPrimIndex is an index of the all sites of scene description that
68 /// contribute opinions to a specific prim, under composition
69 /// semantics.
70 ///
71 /// PcpComputePrimIndex() builds an index ("indexes") the given prim site.
72 /// At any site there may be scene description values expressing arcs
73 /// that represent instructions to pull in further scene description.
74 /// PcpComputePrimIndex() recursively follows these arcs, building and
75 /// ordering the results.
76 ///
78 {
79 public:
80  /// Default construct an empty, invalid prim index.
81  PCP_API
82  PcpPrimIndex();
83 
84  /// Copy-construct a prim index.
85  PCP_API
86  PcpPrimIndex(const PcpPrimIndex& rhs);
87 
88  /// Move-construction
89  PcpPrimIndex(PcpPrimIndex &&rhs) noexcept = default;
90 
91  /// Assignment.
92  PcpPrimIndex &operator=(const PcpPrimIndex &rhs) {
93  PcpPrimIndex(rhs).Swap(*this);
94  return *this;
95  }
96 
97  // Move-assignment.
98  PcpPrimIndex &operator=(PcpPrimIndex &&rhs) noexcept = default;
99 
100  /// Swap the contents of this prim index with \p index.
101  PCP_API
102  void Swap(PcpPrimIndex& rhs);
103 
104  /// Same as Swap(), but standard name.
105  inline void swap(PcpPrimIndex &rhs) { Swap(rhs); }
106 
107  /// Return true if this index is valid.
108  /// A default-constructed index is invalid.
109  bool IsValid() const { return bool(_graph); }
110 
111  void SetGraph(const PcpPrimIndex_GraphRefPtr& graph) {
112  _graph = graph;
113  }
114 
115  const PcpPrimIndex_GraphRefPtr &GetGraph() const {
116  return _graph;
117  }
118 
119  /// Returns the root node of the prim index graph.
120  PCP_API
121  PcpNodeRef GetRootNode() const;
122 
123  /// Returns the path of the prim whose opinions are represented by this
124  /// prim index.
125  PCP_API
126  const SdfPath& GetPath() const;
127 
128  /// Returns true if this prim index contains any scene description
129  /// opinions.
130  PCP_API
131  bool HasSpecs() const;
132 
133  /// Returns true if the prim has any authored payload arcs.
134  /// The payload contents are only resolved and included
135  /// if this prim's path is in the payload inclusion set
136  /// provided in PcpPrimIndexInputs.
137  PCP_API
138  bool HasAnyPayloads() const;
139 
140  /// Returns true if this prim index was composed in USD mode.
141  /// \see PcpCache::IsUsd().
142  PCP_API
143  bool IsUsd() const;
144 
145  /// Returns true if this prim index is instanceable.
146  /// Instanceable prim indexes with the same instance key are
147  /// guaranteed to have the same set of opinions, but may not have
148  /// local opinions about name children.
149  /// \see PcpInstanceKey
150  PCP_API
151  bool IsInstanceable() const;
152 
153  /// \name Iteration
154  /// @{
155 
156  /// Returns range of iterators that encompass all children of the root node
157  /// with the given arc type as well as their descendants, in
158  /// strong-to-weak order.
159  ///
160  /// By default, this returns a range encompassing the entire index.
161  PCP_API
162  PcpNodeRange GetNodeRange(PcpRangeType rangeType = PcpRangeTypeAll) const;
163 
164  /// Returns the node iterator that points to the given \p node if the
165  /// node is in the prim index graph.
166  /// Returns the end of the node range if the node is not contained in this
167  /// prim index.
168  PCP_API
170 
171  /// Returns range of iterators that encompass the given \p node and all of
172  /// its descendants in strong-to-weak order.
173  PCP_API
174  PcpNodeRange GetNodeSubtreeRange(const PcpNodeRef &node) const;
175 
176  /// Returns range of iterators that encompasses all prims, in
177  /// strong-to-weak order.
178  PCP_API
179  PcpPrimRange GetPrimRange(PcpRangeType rangeType = PcpRangeTypeAll) const;
180 
181  /// Returns range of iterators that encompasses all prims from the
182  /// site of \p node. \p node must belong to this prim index.
183  PCP_API
184  PcpPrimRange GetPrimRangeForNode(const PcpNodeRef& node) const;
185 
186  /// @}
187 
188  /// \name Lookup
189  /// @{
190 
191  /// Returns the node that brings opinions from \p primSpec into
192  /// this prim index. If no such node exists, returns an invalid PcpNodeRef.
193  PCP_API
194  PcpNodeRef GetNodeProvidingSpec(const SdfPrimSpecHandle& primSpec) const;
195 
196  /// Returns the node that brings opinions from the Sd prim spec at \p layer
197  /// and \p path into this prim index. If no such node exists, returns an
198  /// invalid PcpNodeRef.
199  PCP_API
201  const SdfLayerHandle& layer, const SdfPath& path) const;
202 
203  /// @}
204 
205  /// \name Diagnostics
206  /// @{
207 
208  /// Return the list of errors local to this prim.
210  return _localErrors ? *_localErrors.get() : PcpErrorVector();
211  }
212 
213  /// Prints various statistics about this prim index.
214  PCP_API
215  void PrintStatistics() const;
216 
217  /// Dump the prim index contents to a string.
218  ///
219  /// If \p includeInheritOriginInfo is \c true, output for implied inherit
220  /// nodes will include information about the originating inherit node.
221  /// If \p includeMaps is \c true, output for each node will include the
222  /// mappings to the parent and root node.
223  PCP_API
225  bool includeInheritOriginInfo = true,
226  bool includeMaps = true) const;
227 
228  /// Dump the prim index in dot format to the file named \p filename.
229  /// See Dump(...) for information regarding arguments.
230  PCP_API
231  void DumpToDotGraph(
232  const std::string& filename,
233  bool includeInheritOriginInfo = true,
234  bool includeMaps = false) const;
235 
236  /// @}
237 
238 
239  /// \name Derived computations
240  /// @{
241 
242  /// Compute the prim child names for the given path. \p errors will
243  /// contain any errors encountered while performing this operation.
244  PCP_API
245  void ComputePrimChildNames(TfTokenVector *nameOrder,
246  PcpTokenSet *prohibitedNameSet) const;
247 
248  /// Compute the prim property names for the given path. \p errors will
249  /// contain any errors encountered while performing this operation. The
250  /// \p nameOrder vector must not contain any duplicate entries.
251  PCP_API
252  void ComputePrimPropertyNames(TfTokenVector *nameOrder) const;
253 
254  /// Compose the authored prim variant selections.
255  ///
256  /// These are the variant selections expressed in scene description.
257  /// Note that these selections may not have actually been applied,
258  /// if they are invalid.
259  ///
260  /// \note This result is not cached, but computed each time.
261  PCP_API
263 
264  /// Return the variant selection applied for the named variant set.
265  /// If none was applied, this returns an empty string.
266  /// This can be different from the authored variant selection;
267  /// for example, if the authored selection is invalid.
268  PCP_API
270  const std::string &variantSet) const;
271 
272  /// @}
273 
274 private:
275  friend class PcpPrimIterator;
276  friend struct Pcp_PrimIndexer;
277  friend void Pcp_RescanForSpecs(PcpPrimIndex*, bool usd,
278  bool updateHasSpecs);
279 
280  // The node graph representing the compositional structure of this prim.
281  PcpPrimIndex_GraphRefPtr _graph;
282 
283  // The prim stack. This is just a derived structure representing
284  // a cached strong-to-weak traversal of the graph collecting specs.
285  Pcp_CompressedSdSiteVector _primStack;
286 
287  // List of errors local to this prim, encountered during computation.
288  // NULL if no errors were found (the expected common case).
289  std::unique_ptr<PcpErrorVector> _localErrors;
290 };
291 
292 /// Free function version for generic code and ADL.
293 inline void swap(PcpPrimIndex &l, PcpPrimIndex &r) { l.swap(r); }
294 
295 /// \class PcpPrimIndexOutputs
296 ///
297 /// Outputs of the prim indexing procedure.
298 ///
300 {
301 public:
302  /// Enumerator whose enumerants describe the payload state of this prim
303  /// index. NoPayload if the index has no payload arcs, otherwise whether
304  /// payloads were included or excluded, and if done so by consulting either
305  /// the cache's payload include set, or determined by a payload predicate.
309 
310  /// Prim index describing the composition structure for the associated
311  /// prim.
313 
314  /// List of all errors encountered during indexing.
316 
317  /// Indicates the payload state of this index. See documentation for
318  /// PayloadState enum for more information.
320 
321  /// A list of names of fields that were composed to generate dynamic file
322  /// format arguments for a node in primIndex. These are not necessarily
323  /// fields that had values, but is the list of all fields that a composed
324  /// value was requested for.
326 
327  /// Dependencies on expression variables from composition arcs in this
328  /// prim index.
330 
331  /// Site dependencies from nodes in the prim index that have been culled.
332  std::vector<PcpCulledDependency> culledDependencies;
333 
334  /// Appends the outputs from \p childOutputs to this object, using
335  /// \p arcToParent to connect \p childOutputs' prim index to this object's
336  /// prim index.
337  ///
338  /// Returns the node in this object's prim index corresponding to the root
339  /// node of \p childOutputs' prim index.
340  PcpNodeRef Append(PcpPrimIndexOutputs&& childOutputs,
341  const PcpArc& arcToParent,
343 };
344 
345 /// \class PcpPrimIndexInputs
346 ///
347 /// Inputs for the prim indexing procedure.
348 ///
350 public:
352  : cache(nullptr)
353  , variantFallbacks(nullptr)
354  , includedPayloads(nullptr)
355  , includedPayloadsMutex(nullptr)
356  , parentIndex(nullptr)
357  , cull(true)
358  , usd(false)
359  { }
360 
361  /// Returns true if prim index computations using this parameters object
362  /// would be equivalent to computations using \p params.
363  bool IsEquivalentTo(const PcpPrimIndexInputs& params) const;
364 
365  /// If supplied, the given PcpCache will be used where possible to compute
366  /// needed intermediate results.
368  { cache = cache_; return *this; }
369 
370  /// Ordered list of variant names to use for the "standin" variant set
371  /// if there is no authored opinion in scene description.
373  { variantFallbacks = map; return *this; }
374 
375  /// Set of paths to prims that should have their payloads included
376  /// during composition.
377  using PayloadSet = std::unordered_set<SdfPath, SdfPath::Hash>;
379  { includedPayloads = payloadSet; return *this; }
380 
381  /// Optional mutex for accessing includedPayloads.
382  PcpPrimIndexInputs &IncludedPayloadsMutex(tbb::spin_rw_mutex *mutex)
383  { includedPayloadsMutex = mutex; return *this; }
384 
385  /// Optional predicate evaluated when a not-yet-included payload is
386  /// discovered while indexing. If the predicate returns true, indexing
387  /// includes the payload and sets the includedDiscoveredPayload bit in the
388  /// outputs.
390  std::function<bool (const SdfPath &)> predicate)
391  { includePayloadPredicate = predicate; return *this; }
392 
393  /// Whether subtrees that contribute no opinions should be culled
394  /// from the index.
395  PcpPrimIndexInputs& Cull(bool doCulling = true)
396  { cull = doCulling; return *this; }
397 
398  /// Whether the prim stack should be computed, and
399  /// whether relocates, inherits, permissions, symmetry, or payloads should
400  /// be considered during prim index computation,
401  PcpPrimIndexInputs& USD(bool doUSD = true)
402  { usd = doUSD; return *this; }
403 
404  /// The file format target for scene description layers encountered during
405  /// prim index computation.
407  { fileFormatTarget = target; return *this; }
408 
409 // private:
413  tbb::spin_rw_mutex *includedPayloadsMutex;
414  std::function<bool (const SdfPath &)> includePayloadPredicate;
417  bool cull;
418  bool usd;
419 };
420 
421 /// Compute an index for the given path. \p errors will contain any errors
422 /// encountered while performing this operation.
423 PCP_API
424 void
426  const SdfPath& primPath,
427  const PcpLayerStackPtr& layerStack,
428  const PcpPrimIndexInputs& inputs,
429  PcpPrimIndexOutputs* outputs,
430  ArResolver* pathResolver = NULL);
431 
432 /// Returns true if the 'new' default standin behavior is enabled.
433 PCP_API
434 bool
436 
437 // Sets the prim stack in \p index.
438 void
440 
441 // Returns true if \p index should be recomputed due to changes to
442 // any computed asset paths that were used to find or open layers
443 // when originally composing \p index. This may be due to scene
444 // description changes or external changes to asset resolution that
445 // may affect the computation of those asset paths.
446 bool
448 
450 
451 #endif // PXR_USD_PCP_PRIM_INDEX_H
void swap(ArAssetInfo &lhs, ArAssetInfo &rhs)
Definition: assetInfo.h:74
Definition: arc.h:44
bool IsValid() const
Definition: primIndex.h:109
Definition: layer.h:97
PcpErrorVector allErrors
List of all errors encountered during indexing.
Definition: primIndex.h:315
GT_API const UT_StringHolder filename
PCP_API void Swap(PcpPrimIndex &rhs)
Swap the contents of this prim index with index.
PCP_API PcpPrimIndex()
Default construct an empty, invalid prim index.
PcpRangeType
Definition: types.h:61
const PayloadSet * includedPayloads
Definition: primIndex.h:412
PayloadState payloadState
Definition: primIndex.h:319
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
PCP_API void ComputePrimChildNames(TfTokenVector *nameOrder, PcpTokenSet *prohibitedNameSet) const
PCP_API PcpPrimRange GetPrimRange(PcpRangeType rangeType=PcpRangeTypeAll) const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
bool IsEquivalentTo(const PcpPrimIndexInputs &params) const
void swap(PcpPrimIndex &rhs)
Same as Swap(), but standard name.
Definition: primIndex.h:105
PCP_API void PcpComputePrimIndex(const SdfPath &primPath, const PcpLayerStackPtr &layerStack, const PcpPrimIndexInputs &inputs, PcpPrimIndexOutputs *outputs, ArResolver *pathResolver=NULL)
PcpPrimIndex & operator=(const PcpPrimIndex &rhs)
Assignment.
Definition: primIndex.h:92
PCP_API SdfVariantSelectionMap ComposeAuthoredVariantSelections() const
PCP_API PcpNodeRef GetRootNode() const
Returns the root node of the prim index graph.
Definition: cache.h:93
std::map< std::string, std::string > SdfVariantSelectionMap
A map of reference variant set names to variants in those sets.
Definition: types.h:275
PcpExpressionVariablesDependencyData expressionVariablesDependency
Definition: primIndex.h:329
void Pcp_RescanForSpecs(PcpPrimIndex *index, bool usd)
GLenum const GLfloat * params
Definition: glcorearb.h:105
std::shared_ptr< PcpErrorBase > PcpErrorBasePtr
Definition: errors.h:76
PcpPrimIndex primIndex
Definition: primIndex.h:312
bool Pcp_NeedToRecomputeDueToAssetPathChange(const PcpPrimIndex &index)
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
PCP_API const SdfPath & GetPath() const
PCP_API PcpNodeRef GetNodeProvidingSpec(const SdfPrimSpecHandle &primSpec) const
< returns > If no error
Definition: snippets.dox:2
friend struct Pcp_PrimIndexer
Definition: primIndex.h:276
const PcpPrimIndex_GraphRefPtr & GetGraph() const
Definition: primIndex.h:115
PcpPrimIndexInputs & VariantFallbacks(const PcpVariantFallbackMap *map)
Definition: primIndex.h:372
PcpPrimIndexInputs & Cull(bool doCulling=true)
Definition: primIndex.h:395
PCP_API std::string DumpToString(bool includeInheritOriginInfo=true, bool includeMaps=true) const
PCP_API PcpPrimRange GetPrimRangeForNode(const PcpNodeRef &node) const
PCP_API bool HasSpecs() const
std::vector< PcpCulledDependency > culledDependencies
Site dependencies from nodes in the prim index that have been culled.
Definition: primIndex.h:332
friend void Pcp_RescanForSpecs(PcpPrimIndex *, bool usd, bool updateHasSpecs)
PCP_API bool HasAnyPayloads() const
TF_DECLARE_REF_PTRS(PcpLayerStack)
PcpPrimIndexInputs & IncludedPayloads(const PayloadSet *payloadSet)
Definition: primIndex.h:378
PcpPrimIndexInputs & USD(bool doUSD=true)
Definition: primIndex.h:401
void SetGraph(const PcpPrimIndex_GraphRefPtr &graph)
Definition: primIndex.h:111
PCP_API void DumpToDotGraph(const std::string &filename, bool includeInheritOriginInfo=true, bool includeMaps=false) const
PCP_API PcpNodeIterator GetNodeIteratorAtNode(const PcpNodeRef &node) const
GLenum target
Definition: glcorearb.h:1667
std::unordered_set< SdfPath, SdfPath::Hash > PayloadSet
Definition: primIndex.h:377
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
Definition: path.h:290
std::function< bool(const SdfPath &)> includePayloadPredicate
Definition: primIndex.h:414
std::vector< PcpErrorBasePtr > PcpErrorVector
Definition: errors.h:78
const PcpVariantFallbackMap * variantFallbacks
Definition: primIndex.h:411
const PcpPrimIndex * parentIndex
Definition: primIndex.h:415
std::vector< Pcp_CompressedSdSite > Pcp_CompressedSdSiteVector
Definition: types.h:189
PcpPrimIndexInputs & IncludedPayloadsMutex(tbb::spin_rw_mutex *mutex)
Optional mutex for accessing includedPayloads.
Definition: primIndex.h:382
PCP_API std::string GetSelectionAppliedForVariantSet(const std::string &variantSet) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
PcpCache * cache
Definition: primIndex.h:410
PcpPrimIndexInputs & IncludePayloadPredicate(std::function< bool(const SdfPath &)> predicate)
Definition: primIndex.h:389
PCP_API bool IsUsd() const
tbb::spin_rw_mutex * includedPayloadsMutex
Definition: primIndex.h:413
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfLayer)
GLuint index
Definition: glcorearb.h:786
PcpNodeRef Append(PcpPrimIndexOutputs &&childOutputs, const PcpArc &arcToParent, PcpErrorBasePtr *error)
PCP_API bool IsInstanceable() const
PcpDynamicFileFormatDependencyData dynamicFileFormatDependency
Definition: primIndex.h:325
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
PCP_API PcpNodeRange GetNodeRange(PcpRangeType rangeType=PcpRangeTypeAll) const
TF_DECLARE_WEAK_AND_REF_PTRS(PcpPrimIndex_Graph)
std::string fileFormatTarget
Definition: primIndex.h:416
PcpErrorVector GetLocalErrors() const
Return the list of errors local to this prim.
Definition: primIndex.h:209
GLboolean r
Definition: glcorearb.h:1222
PCP_API void PrintStatistics() const
Prints various statistics about this prim index.
PCP_API void ComputePrimPropertyNames(TfTokenVector *nameOrder) const
PcpPrimIndexInputs & FileFormatTarget(const std::string &target)
Definition: primIndex.h:406
PCP_API bool PcpIsNewDefaultStandinBehaviorEnabled()
Returns true if the 'new' default standin behavior is enabled.
std::map< std::string, std::vector< std::string > > PcpVariantFallbackMap
Definition: types.h:206
PCP_API PcpNodeRange GetNodeSubtreeRange(const PcpNodeRef &node) const
PcpPrimIndexInputs & Cache(PcpCache *cache_)
Definition: primIndex.h:367
#define PCP_API
Definition: api.h:40