stageSceneIndex.h
1 //
2 // Copyright 2022 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_IMAGING_USD_IMAGING_STAGE_SCENE_INDEX_H
25 #define PXR_USD_IMAGING_USD_IMAGING_STAGE_SCENE_INDEX_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/usdImaging/usdImaging/api.h"
30 #include "pxr/usdImaging/usdImaging/dataSourceStageGlobals.h"
31 
32 #include "pxr/imaging/hd/sceneIndex.h"
33 
34 #include "pxr/usd/usd/notice.h"
35 #include "pxr/usd/usd/stage.h"
36 
37 #include <tbb/concurrent_hash_map.h>
38 #include <tbb/concurrent_unordered_map.h>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
43 using UsdImagingPrimAdapterSharedPtr = std::shared_ptr<UsdImagingPrimAdapter>;
44 
46 using UsdImagingAPISchemaAdapterSharedPtr =
47  std::shared_ptr<UsdImagingAPISchemaAdapter>;
48 
49 
50 class UsdImagingStageSceneIndex;
51 TF_DECLARE_REF_PTRS(UsdImagingStageSceneIndex);
52 
53 class UsdImagingStageSceneIndex : public HdSceneIndexBase
54 {
55 public:
56 
57  static UsdImagingStageSceneIndexRefPtr New() {
58  return TfCreateRefPtr(new UsdImagingStageSceneIndex());
59  }
60 
61  USDIMAGING_API
62  ~UsdImagingStageSceneIndex();
63 
64  // ------------------------------------------------------------------------
65  // Scene index API
66 
67  USDIMAGING_API
68  HdSceneIndexPrim GetPrim(const SdfPath & primPath) const override;
69 
70  USDIMAGING_API
71  SdfPathVector GetChildPrimPaths(const SdfPath & primPath) const override;
72 
73  // ------------------------------------------------------------------------
74  // App-facing API
75 
76  // Set the USD stage to pull data from. Note that this will delete all
77  // scene index prims and reset stage global data.
78  USDIMAGING_API
79  void SetStage(UsdStageRefPtr stage);
80 
81  // Traverse the scene collecting imaging prims, and then call PrimsAdded.
82  USDIMAGING_API
83  void Populate();
84 
85  // Set the time, and call PrimsDirtied for any time-varying attributes.
86  USDIMAGING_API
87  void SetTime(UsdTimeCode time);
88 
89  // Return the current time.
90  USDIMAGING_API
91  UsdTimeCode GetTime() const;
92 
93  // Apply queued stage edits to imaging scene.
94  // If the USD stage is edited while the scene index is pulling from it,
95  // those edits get queued and deferred. Calling ApplyPendingUpdates will
96  // turn resync requests into PrimsAdded/PrimsRemoved, and property changes
97  // into PrimsDirtied.
98  USDIMAGING_API
99  void ApplyPendingUpdates();
100 
101 private:
102  USDIMAGING_API
103  UsdImagingStageSceneIndex();
104 
105  Usd_PrimFlagsConjunction _GetTraversalPredicate() const;
106 
107  using _APISchemaEntry =
108  std::pair<UsdImagingAPISchemaAdapterSharedPtr, TfToken>;
109  using _APISchemaAdapters = TfSmallVector<_APISchemaEntry, 8>;
110 
111  // Adapter delegation.
112 
113  _APISchemaAdapters _AdapterSetLookup(UsdPrim prim) const;
114 
115  UsdImagingAPISchemaAdapterSharedPtr _APIAdapterLookup(
116  const TfToken &adapterKey) const;
117 
118  UsdImagingPrimAdapterSharedPtr _PrimAdapterLookup(
119  const TfToken &adapterKey) const;
120 
121  TfTokenVector _GetImagingSubprims(
122  UsdPrim const& prim, const _APISchemaAdapters &adapters) const;
123 
124  TfToken _GetImagingSubprimType(
125  const _APISchemaAdapters &adapters,
126  UsdPrim const& prim,
127  const TfToken &subprim) const;
128 
129  HdContainerDataSourceHandle _GetImagingSubprimData(
130  const _APISchemaAdapters &adapters,
131  UsdPrim const& prim, const TfToken& subprim) const;
132 
133  HdDataSourceLocatorSet _InvalidateImagingSubprim(
134  const _APISchemaAdapters &adapters,
135  UsdPrim const& prim,
136  TfToken const& subprim, TfTokenVector const& properties) const;
137 
138  void _ApplyPendingResyncs();
139 
140  class _StageGlobals : public UsdImagingDataSourceStageGlobals
141  {
142  public:
143  // Datasource-facing API
144  void FlagAsTimeVarying(
145  const SdfPath & primPath,
146  const HdDataSourceLocator & locator) const override;
147 
148  UsdTimeCode GetTime() const override;
149 
150  // Scene index-facing API
151  void SetTime(UsdTimeCode time,
153  void Clear();
154 
155  private:
156  struct _PathHashCompare {
157  static bool equal(const SdfPath &a, const SdfPath &b) {
158  return a == b;
159  }
160  static size_t hash(const SdfPath &p) {
161  return hash_value(p);
162  }
163  };
164  using _VariabilityMap = tbb::concurrent_hash_map<SdfPath,
165  HdDataSourceLocatorSet, _PathHashCompare>;
166  mutable _VariabilityMap _timeVaryingLocators;
167 
168  UsdTimeCode _time;
169  };
170 
171  UsdStageRefPtr _stage;
172  _StageGlobals _stageGlobals;
173 
174  // Population
175  void _Populate(UsdPrim subtreeRoot);
176 
177  // Edit processing
178  void _OnUsdObjectsChanged(UsdNotice::ObjectsChanged const& notice,
179  UsdStageWeakPtr const& sender);
180  TfNotice::Key _objectsChangedNoticeKey;
181 
182  // Note: resync paths mean we remove the whole subtree and repopulate.
183  SdfPathVector _usdPrimsToResync;
184  // Property changes get converted into PrimsDirtied messages.
185  std::map<SdfPath, TfTokenVector> _usdPropertiesToUpdate;
186 
187  // Usd Prim Type to Adapter lookup table, concurrent because it could
188  // be potentially filled during concurrent GetPrim calls rather than
189  // just during single-threaded population.
190  using _PrimAdapterMap = tbb::concurrent_unordered_map<
191  TfToken, UsdImagingPrimAdapterSharedPtr, TfHash>;
192 
193  mutable _PrimAdapterMap _primAdapterMap;
194 
195  using _ApiAdapterMap = tbb::concurrent_unordered_map<
196  TfToken, UsdImagingAPISchemaAdapterSharedPtr, TfHash>;
197 
198  mutable _ApiAdapterMap _apiAdapterMap;
199 
200  // Use UsdPrimTypeInfo pointer as key because they are guaranteed to be
201  // cached at least as long as the stage is open.
202  using _AdapterSetMap = tbb::concurrent_unordered_map<
203  const UsdPrimTypeInfo *, _APISchemaAdapters, TfHash>;
204 
205  mutable _AdapterSetMap _adapterSetMap;
206 };
207 
208 PXR_NAMESPACE_CLOSE_SCOPE
209 
210 #endif
Handle-object returned by TfNotice::Register().
Definition: notice.h:256
virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const =0
Returns a pair of (prim type, datasource) for the object at primPath.
This is a small-vector class with local storage optimization, the local storage can be specified via ...
Definition: smallVector.h:177
Base class for all API schema adapters.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:447
Small struct representing a 'prim' in the Hydra scene index.
Definition: sceneIndex.h:49
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const =0
Returns the paths of all scene index prims located immediately below primPath.
Represents an object that can identify the location of a data source.
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
This class is used as a context object with global stage information, that gets passed down to dataso...
Base class for all PrimAdapters.
Definition: primAdapter.h:67
Notice sent in response to authored changes that affect UsdObjects.
Definition: notice.h:107
Abstract interface to scene data.
Definition: sceneIndex.h:62
Represents a set of data source locators closed under descendancy.
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:134
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
Class that holds the full type information for a prim.
Definition: primTypeInfo.h:47
std::enable_if< std::is_same< Half, half >::value, size_t >::type hash_value(const Half &h)
Overload hash_value for half.
Definition: half.h:50
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:75