HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stageSceneIndex.h
Go to the documentation of this file.
1 //
2 // Copyright 2022 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_IMAGING_USD_IMAGING_STAGE_SCENE_INDEX_H
8 #define PXR_USD_IMAGING_USD_IMAGING_STAGE_SCENE_INDEX_H
9 
10 #include "pxr/pxr.h"
11 
15 
17 
18 #include "pxr/usd/usd/notice.h"
19 #include "pxr/usd/usd/stage.h"
20 
21 #include <mutex>
22 #include <set>
23 
25 
26 #define USDIMAGING_STAGE_SCENE_INDEX_TOKENS \
27  (includeUnloadedPrims) \
28 
29 TF_DECLARE_PUBLIC_TOKENS(UsdImagingStageSceneIndexTokens, USDIMAGING_API,
31 
33  std::shared_ptr<class UsdImagingPrimAdapter>;
34 class UsdImaging_AdapterManager;
35 
37 
38 ///
39 /// A scene index consuming a UsdStage.
40 ///
41 /// This scene index does not perform any transformations such as
42 /// flattening visibility or transforms or aggregate native
43 /// instances. Use UsdImagingCreateSceneIndices to get a chain
44 /// of scene indices to resolve those.
45 ///
47 {
48 public:
49  static UsdImagingStageSceneIndexRefPtr New(
50  HdContainerDataSourceHandle const &inputArgs = nullptr) {
51  return TfCreateRefPtr(new UsdImagingStageSceneIndex(inputArgs));
52  }
53 
56 
57  // ------------------------------------------------------------------------
58  // Scene index API
59 
61  HdSceneIndexPrim GetPrim(const SdfPath & primPath) const override;
62 
64  SdfPathVector GetChildPrimPaths(const SdfPath & primPath) const override;
65 
66  // ------------------------------------------------------------------------
67  // App-facing API
68 
69  // Set the USD stage to pull data from. Note that this will delete all
70  // scene index prims and reset stage global data.
72  void SetStage(UsdStageRefPtr stage);
73 
74  // Set the time, and call PrimsDirtied for any time-varying attributes.
75  //
76  // PrimsDirtied is only called if the time is different from the last call
77  // or forceDirtyingTimeDeps is true.
79  void SetTime(UsdTimeCode time, bool forceDirtyingTimeDeps = false);
80 
81  // Return the current time.
83  UsdTimeCode GetTime() const;
84 
85  // Apply queued stage edits to imaging scene.
86  // If the USD stage is edited while the scene index is pulling from it,
87  // those edits get queued and deferred. Calling ApplyPendingUpdates will
88  // turn resync requests into PrimsAdded/PrimsRemoved, and property changes
89  // into PrimsDirtied.
91  void ApplyPendingUpdates();
92 
93 private:
95  UsdImagingStageSceneIndex(HdContainerDataSourceHandle const &inputArgs);
96 
97  Usd_PrimFlagsPredicate _GetPrimPredicate() const;
98 
99  void _ApplyPendingResyncs();
100  void _ComputeDirtiedEntries(
101  const std::map<SdfPath, TfTokenVector> &pathToUsdProperties,
102  SdfPathVector * primPathsToResync,
103  UsdImagingPropertyInvalidationType invalidationType,
104  HdSceneIndexObserver::DirtiedPrimEntries * dirtiedPrims) const;
105 
106  class _StageGlobals : public UsdImagingDataSourceStageGlobals
107  {
108  public:
109  // Datasource-facing API
110  void FlagAsTimeVarying(
111  const SdfPath & hydraPath,
112  const HdDataSourceLocator & locator) const override;
113 
114  void FlagAsAssetPathDependent(
115  const SdfPath & usdPath) const override;
116 
117  UsdTimeCode GetTime() const override;
118 
119  // Scene index-facing API
120  void SetTime(UsdTimeCode time,
122 
123  void RemoveAssetPathDependentsUnder(const SdfPath &path);
124 
125  void InvalidateAssetPathDependentsUnder(
126  const SdfPath &path,
127  std::vector<SdfPath> *primsToInvalidate,
128  std::map<SdfPath, TfTokenVector> *propertiesToInvalidate) const;
129 
130  void Clear();
131 
132  private:
133  struct _PathHashCompare {
134  static bool equal(const SdfPath &a, const SdfPath &b) {
135  return a == b;
136  }
137  static size_t hash(const SdfPath &p) {
138  return hash_value(p);
139  }
140  };
141  using _VariabilityMap = tbb::concurrent_hash_map<SdfPath,
142  HdDataSourceLocatorSet, _PathHashCompare>;
143  mutable _VariabilityMap _timeVaryingLocators;
144 
145  using _AssetPathDependentsSet = std::set<SdfPath>;
146  mutable _AssetPathDependentsSet _assetPathDependents;
147  mutable std::mutex _assetPathDependentsMutex;
148 
149  UsdTimeCode _time;
150  };
151 
152  const bool _includeUnloadedPrims;
153 
154  UsdStageRefPtr _stage;
155  _StageGlobals _stageGlobals;
156 
157  // Population
158  void _Populate();
159  void _PopulateSubtree(UsdPrim subtreeRoot,
161 
162  // Edit processing
163  void _OnUsdObjectsChanged(UsdNotice::ObjectsChanged const& notice,
164  UsdStageWeakPtr const& sender);
165  TfNotice::Key _objectsChangedNoticeKey;
166 
167  // Keep track of populated paths for use in _ApplyPendingResyncs().
168  SdfPathSet _populatedPaths;
169 
170  // Note: resync paths mean we remove the whole subtree and repopulate.
171  SdfPathVector _usdPrimsToResync;
172  // Property changes get converted into PrimsDirtied messages.
173  std::map<SdfPath, TfTokenVector> _usdPropertiesToUpdate;
174  std::map<SdfPath, TfTokenVector> _usdPropertiesToResync;
175 
176  using _PrimAdapterPair = std::pair<UsdPrim, UsdImagingPrimAdapterSharedPtr>;
177  _PrimAdapterPair _FindResponsibleAncestor(const UsdPrim &prim) const;
178 
179  std::unique_ptr<UsdImaging_AdapterManager> const _adapterManager;
180 };
181 
183 
184 #endif
USDIMAGING_API void SetStage(UsdStageRefPtr stage)
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
#define USDIMAGING_API
Definition: api.h:23
GT_API const UT_StringHolder time
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
USDIMAGING_API void SetTime(UsdTimeCode time, bool forceDirtyingTimeDeps=false)
#define USDIMAGING_STAGE_SCENE_INDEX_TOKENS
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
USDIMAGING_API SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override
UsdStagePtr UsdStageWeakPtr
Definition: common.h:38
TF_DECLARE_PUBLIC_TOKENS(UsdImagingStageSceneIndexTokens, USDIMAGING_API, USDIMAGING_STAGE_SCENE_INDEX_TOKENS)
std::vector< class SdfPath > SdfPathVector
Definition: prim.h:116
Definition: path.h:280
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:199
USDIMAGING_API ~UsdImagingStageSceneIndex()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDIMAGING_API HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
std::shared_ptr< UsdImagingPrimAdapter > UsdImagingPrimAdapterSharedPtr
USDIMAGING_API UsdTimeCode GetTime() const
static UsdImagingStageSceneIndexRefPtr New(HdContainerDataSourceHandle const &inputArgs=nullptr)
USDIMAGING_API void ApplyPendingUpdates()
size_t hash_value(const CH_ChannelRef &ref)
UsdImagingPropertyInvalidationType
Definition: types.h:17
TF_DECLARE_REF_PTRS(UsdImagingStageSceneIndex)