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  const _APISchemaAdapters &adapters) const;
123 
124  TfToken _GetImagingSubprimType(
125  const _APISchemaAdapters &adapters,
126  const TfToken &subprim) const;
127 
128  HdContainerDataSourceHandle _GetImagingSubprimData(
129  const _APISchemaAdapters &adapters,
130  UsdPrim prim, const TfToken& subprim) const;
131 
132  HdDataSourceLocatorSet _InvalidateImagingSubprim(
133  const _APISchemaAdapters &adapters,
134  TfToken const& subprim, TfTokenVector const& properties) const;
135 
136 
137  class _StageGlobals : public UsdImagingDataSourceStageGlobals
138  {
139  public:
140  // Datasource-facing API
141  void FlagAsTimeVarying(
142  const SdfPath & primPath,
143  const HdDataSourceLocator & locator) const override;
144 
145  UsdTimeCode GetTime() const override;
146 
147  // Scene index-facing API
148  void SetTime(UsdTimeCode time,
150  void Clear();
151 
152  private:
153  struct _PathHashCompare {
154  static bool equal(const SdfPath &a, const SdfPath &b) {
155  return a == b;
156  }
157  static size_t hash(const SdfPath &p) {
158  return hash_value(p);
159  }
160  };
161  using _VariabilityMap = tbb::concurrent_hash_map<SdfPath,
162  HdDataSourceLocatorSet, _PathHashCompare>;
163  mutable _VariabilityMap _timeVaryingLocators;
164 
165  UsdTimeCode _time;
166  };
167 
168  UsdStageRefPtr _stage;
169  _StageGlobals _stageGlobals;
170 
171  // Population
172  void _Populate(UsdPrim subtreeRoot);
173 
174  // Edit processing
175  void _OnUsdObjectsChanged(UsdNotice::ObjectsChanged const& notice,
176  UsdStageWeakPtr const& sender);
177  TfNotice::Key _objectsChangedNoticeKey;
178 
179  // Note: resync paths mean we remove the whole subtree and repopulate.
180  SdfPathVector _usdPrimsToResync;
181  // Property changes get converted into PrimsDirtied messages.
182  std::map<SdfPath, TfTokenVector> _usdPropertiesToUpdate;
183 
184  // Usd Prim Type to Adapter lookup table, concurrent because it could
185  // be potentially filled during concurrent GetPrim calls rather than
186  // just during single-threaded population.
187  using _PrimAdapterMap = tbb::concurrent_unordered_map<
188  TfToken, UsdImagingPrimAdapterSharedPtr, TfHash>;
189 
190  mutable _PrimAdapterMap _primAdapterMap;
191 
192  using _ApiAdapterMap = tbb::concurrent_unordered_map<
193  TfToken, UsdImagingAPISchemaAdapterSharedPtr, TfHash>;
194 
195  mutable _ApiAdapterMap _apiAdapterMap;
196 
197  // Use UsdPrimTypeInfo pointer as key because they are guaranteed to be
198  // cached at least as long as the stage is open.
199  using _AdapterSetMap = tbb::concurrent_unordered_map<
200  const UsdPrimTypeInfo *, _APISchemaAdapters, TfHash>;
201 
202  mutable _AdapterSetMap _adapterSetMap;
203 };
204 
205 PXR_NAMESPACE_CLOSE_SCOPE
206 
207 #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