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