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 encompasses all prims, in
172  /// strong-to-weak order.
173  PCP_API
174  PcpPrimRange GetPrimRange(PcpRangeType rangeType = PcpRangeTypeAll) const;
175 
176  /// Returns range of iterators that encompasses all prims from the
177  /// site of \p node. \p node must belong to this prim index.
178  PCP_API
179  PcpPrimRange GetPrimRangeForNode(const PcpNodeRef& node) const;
180 
181  /// @}
182 
183  /// \name Lookup
184  /// @{
185 
186  /// Returns the node that brings opinions from \p primSpec into
187  /// this prim index. If no such node exists, returns an invalid PcpNodeRef.
188  PCP_API
189  PcpNodeRef GetNodeProvidingSpec(const SdfPrimSpecHandle& primSpec) const;
190 
191  /// Returns the node that brings opinions from the Sd prim spec at \p layer
192  /// and \p path into this prim index. If no such node exists, returns an
193  /// invalid PcpNodeRef.
194  PCP_API
196  const SdfLayerHandle& layer, const SdfPath& path) const;
197 
198  /// @}
199 
200  /// \name Diagnostics
201  /// @{
202 
203  /// Return the list of errors local to this prim.
205  return _localErrors ? *_localErrors.get() : PcpErrorVector();
206  }
207 
208  /// Prints various statistics about this prim index.
209  PCP_API
210  void PrintStatistics() const;
211 
212  /// Dump the prim index contents to a string.
213  ///
214  /// If \p includeInheritOriginInfo is \c true, output for implied inherit
215  /// nodes will include information about the originating inherit node.
216  /// If \p includeMaps is \c true, output for each node will include the
217  /// mappings to the parent and root node.
218  PCP_API
220  bool includeInheritOriginInfo = true,
221  bool includeMaps = true) const;
222 
223  /// Dump the prim index in dot format to the file named \p filename.
224  /// See Dump(...) for information regarding arguments.
225  PCP_API
226  void DumpToDotGraph(
227  const std::string& filename,
228  bool includeInheritOriginInfo = true,
229  bool includeMaps = false) const;
230 
231  /// @}
232 
233 
234  /// \name Derived computations
235  /// @{
236 
237  /// Compute the prim child names for the given path. \p errors will
238  /// contain any errors encountered while performing this operation.
239  PCP_API
240  void ComputePrimChildNames(TfTokenVector *nameOrder,
241  PcpTokenSet *prohibitedNameSet) const;
242 
243  /// Compute the prim property names for the given path. \p errors will
244  /// contain any errors encountered while performing this operation. The
245  /// \p nameOrder vector must not contain any duplicate entries.
246  PCP_API
247  void ComputePrimPropertyNames(TfTokenVector *nameOrder) const;
248 
249  /// Compose the authored prim variant selections.
250  ///
251  /// These are the variant selections expressed in scene description.
252  /// Note that these selections may not have actually been applied,
253  /// if they are invalid.
254  ///
255  /// \note This result is not cached, but computed each time.
256  PCP_API
258 
259  /// Return the variant selection applied for the named variant set.
260  /// If none was applied, this returns an empty string.
261  /// This can be different from the authored variant selection;
262  /// for example, if the authored selection is invalid.
263  PCP_API
265  const std::string &variantSet) const;
266 
267  /// @}
268 
269 private:
270  friend class PcpPrimIterator;
271  friend struct Pcp_PrimIndexer;
272  friend void Pcp_RescanForSpecs(PcpPrimIndex*, bool usd,
273  bool updateHasSpecs);
274 
275  // The node graph representing the compositional structure of this prim.
276  PcpPrimIndex_GraphRefPtr _graph;
277 
278  // The prim stack. This is just a derived structure representing
279  // a cached strong-to-weak traversal of the graph collecting specs.
280  Pcp_CompressedSdSiteVector _primStack;
281 
282  // List of errors local to this prim, encountered during computation.
283  // NULL if no errors were found (the expected common case).
284  std::unique_ptr<PcpErrorVector> _localErrors;
285 };
286 
287 /// Free function version for generic code and ADL.
288 inline void swap(PcpPrimIndex &l, PcpPrimIndex &r) { l.swap(r); }
289 
290 /// \class PcpPrimIndexOutputs
291 ///
292 /// Outputs of the prim indexing procedure.
293 ///
295 {
296 public:
297  /// Enumerator whose enumerants describe the payload state of this prim
298  /// index. NoPayload if the index has no payload arcs, otherwise whether
299  /// payloads were included or excluded, and if done so by consulting either
300  /// the cache's payload include set, or determined by a payload predicate.
304 
305  /// Prim index describing the composition structure for the associated
306  /// prim.
308 
309  /// List of all errors encountered during indexing.
311 
312  /// Indicates the payload state of this index. See documentation for
313  /// PayloadState enum for more information.
315 
316  /// A list of names of fields that were composed to generate dynamic file
317  /// format arguments for a node in primIndex. These are not necessarily
318  /// fields that had values, but is the list of all fields that a composed
319  /// value was requested for.
321 
322  /// Dependencies on expression variables from composition arcs in this
323  /// prim index.
325 
326  /// Site dependencies from nodes in the prim index that have been culled.
327  std::vector<PcpCulledDependency> culledDependencies;
328 
329  /// Appends the outputs from \p childOutputs to this object, using
330  /// \p arcToParent to connect \p childOutputs' prim index to this object's
331  /// prim index.
332  ///
333  /// Returns the node in this object's prim index corresponding to the root
334  /// node of \p childOutputs' prim index.
335  PcpNodeRef Append(PcpPrimIndexOutputs&& childOutputs,
336  const PcpArc& arcToParent,
338 };
339 
340 /// \class PcpPrimIndexInputs
341 ///
342 /// Inputs for the prim indexing procedure.
343 ///
345 public:
347  : cache(nullptr)
348  , variantFallbacks(nullptr)
349  , includedPayloads(nullptr)
350  , includedPayloadsMutex(nullptr)
351  , parentIndex(nullptr)
352  , cull(true)
353  , usd(false)
354  { }
355 
356  /// Returns true if prim index computations using this parameters object
357  /// would be equivalent to computations using \p params.
358  bool IsEquivalentTo(const PcpPrimIndexInputs& params) const;
359 
360  /// If supplied, the given PcpCache will be used where possible to compute
361  /// needed intermediate results.
363  { cache = cache_; return *this; }
364 
365  /// Ordered list of variant names to use for the "standin" variant set
366  /// if there is no authored opinion in scene description.
368  { variantFallbacks = map; return *this; }
369 
370  /// Set of paths to prims that should have their payloads included
371  /// during composition.
372  using PayloadSet = std::unordered_set<SdfPath, SdfPath::Hash>;
374  { includedPayloads = payloadSet; return *this; }
375 
376  /// Optional mutex for accessing includedPayloads.
377  PcpPrimIndexInputs &IncludedPayloadsMutex(tbb::spin_rw_mutex *mutex)
378  { includedPayloadsMutex = mutex; return *this; }
379 
380  /// Optional predicate evaluated when a not-yet-included payload is
381  /// discovered while indexing. If the predicate returns true, indexing
382  /// includes the payload and sets the includedDiscoveredPayload bit in the
383  /// outputs.
385  std::function<bool (const SdfPath &)> predicate)
386  { includePayloadPredicate = predicate; return *this; }
387 
388  /// Whether subtrees that contribute no opinions should be culled
389  /// from the index.
390  PcpPrimIndexInputs& Cull(bool doCulling = true)
391  { cull = doCulling; return *this; }
392 
393  /// Whether the prim stack should be computed, and
394  /// whether relocates, inherits, permissions, symmetry, or payloads should
395  /// be considered during prim index computation,
396  PcpPrimIndexInputs& USD(bool doUSD = true)
397  { usd = doUSD; return *this; }
398 
399  /// The file format target for scene description layers encountered during
400  /// prim index computation.
402  { fileFormatTarget = target; return *this; }
403 
404 // private:
408  tbb::spin_rw_mutex *includedPayloadsMutex;
409  std::function<bool (const SdfPath &)> includePayloadPredicate;
412  bool cull;
413  bool usd;
414 };
415 
416 /// Compute an index for the given path. \p errors will contain any errors
417 /// encountered while performing this operation.
418 PCP_API
419 void
421  const SdfPath& primPath,
422  const PcpLayerStackPtr& layerStack,
423  const PcpPrimIndexInputs& inputs,
424  PcpPrimIndexOutputs* outputs,
425  ArResolver* pathResolver = NULL);
426 
427 /// Returns true if the 'new' default standin behavior is enabled.
428 PCP_API
429 bool
431 
432 // Sets the prim stack in \p index.
433 void
435 
436 // Returns true if \p index should be recomputed due to changes to
437 // any computed asset paths that were used to find or open layers
438 // when originally composing \p index. This may be due to scene
439 // description changes or external changes to asset resolution that
440 // may affect the computation of those asset paths.
441 bool
443 
445 
446 #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:94
PcpErrorVector allErrors
List of all errors encountered during indexing.
Definition: primIndex.h:310
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:407
PayloadState payloadState
Definition: primIndex.h:314
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:283
PcpExpressionVariablesDependencyData expressionVariablesDependency
Definition: primIndex.h:324
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:307
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:271
const PcpPrimIndex_GraphRefPtr & GetGraph() const
Definition: primIndex.h:115
PcpPrimIndexInputs & VariantFallbacks(const PcpVariantFallbackMap *map)
Definition: primIndex.h:367
PcpPrimIndexInputs & Cull(bool doCulling=true)
Definition: primIndex.h:390
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:327
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:373
PcpPrimIndexInputs & USD(bool doUSD=true)
Definition: primIndex.h:396
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:372
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
Definition: path.h:291
std::function< bool(const SdfPath &)> includePayloadPredicate
Definition: primIndex.h:409
std::vector< PcpErrorBasePtr > PcpErrorVector
Definition: errors.h:78
const PcpVariantFallbackMap * variantFallbacks
Definition: primIndex.h:406
const PcpPrimIndex * parentIndex
Definition: primIndex.h:410
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:377
PCP_API std::string GetSelectionAppliedForVariantSet(const std::string &variantSet) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
PcpCache * cache
Definition: primIndex.h:405
PcpPrimIndexInputs & IncludePayloadPredicate(std::function< bool(const SdfPath &)> predicate)
Definition: primIndex.h:384
PCP_API bool IsUsd() const
tbb::spin_rw_mutex * includedPayloadsMutex
Definition: primIndex.h:408
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:320
#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:411
PcpErrorVector GetLocalErrors() const
Return the list of errors local to this prim.
Definition: primIndex.h:204
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:401
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
PcpPrimIndexInputs & Cache(PcpCache *cache_)
Definition: primIndex.h:362
#define PCP_API
Definition: api.h:40