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,
114  // optionally return the prim adapter (which will also be
115  // included in wrapped form as part of the ordered main result)
116  UsdImagingPrimAdapterSharedPtr *outputPrimAdapter=nullptr) const;
117 
118  UsdImagingAPISchemaAdapterSharedPtr _APIAdapterLookup(
119  const TfToken &adapterKey) const;
120 
121  UsdImagingPrimAdapterSharedPtr _PrimAdapterLookup(
122  const TfToken &adapterKey) const;
123 
124  TfTokenVector _GetImagingSubprims(
125  UsdPrim const& prim, const _APISchemaAdapters &adapters) const;
126 
127  TfToken _GetImagingSubprimType(
128  const _APISchemaAdapters &adapters,
129  UsdPrim const& prim,
130  const TfToken &subprim) const;
131 
132  HdContainerDataSourceHandle _GetImagingSubprimData(
133  const _APISchemaAdapters &adapters,
134  UsdPrim const& prim, const TfToken& subprim) const;
135 
136  HdDataSourceLocatorSet _InvalidateImagingSubprim(
137  const _APISchemaAdapters &adapters,
138  UsdPrim const& prim,
139  TfToken const& subprim, TfTokenVector const& properties) const;
140 
141  void _ApplyPendingResyncs();
142 
143  class _StageGlobals : public UsdImagingDataSourceStageGlobals
144  {
145  public:
146  // Datasource-facing API
147  void FlagAsTimeVarying(
148  const SdfPath & primPath,
149  const HdDataSourceLocator & locator) const override;
150 
151  UsdTimeCode GetTime() const override;
152 
153  // Scene index-facing API
154  void SetTime(UsdTimeCode time,
156  void Clear();
157 
158  private:
159  struct _PathHashCompare {
160  static bool equal(const SdfPath &a, const SdfPath &b) {
161  return a == b;
162  }
163  static size_t hash(const SdfPath &p) {
164  return hash_value(p);
165  }
166  };
167  using _VariabilityMap = tbb::concurrent_hash_map<SdfPath,
168  HdDataSourceLocatorSet, _PathHashCompare>;
169  mutable _VariabilityMap _timeVaryingLocators;
170 
171  UsdTimeCode _time;
172  };
173 
174  UsdStageRefPtr _stage;
175  _StageGlobals _stageGlobals;
176 
177  // Population
178  void _Populate(UsdPrim subtreeRoot);
179 
180  // Edit processing
181  void _OnUsdObjectsChanged(UsdNotice::ObjectsChanged const& notice,
182  UsdStageWeakPtr const& sender);
183  TfNotice::Key _objectsChangedNoticeKey;
184 
185  // Note: resync paths mean we remove the whole subtree and repopulate.
186  SdfPathVector _usdPrimsToResync;
187  // Property changes get converted into PrimsDirtied messages.
188  std::map<SdfPath, TfTokenVector> _usdPropertiesToUpdate;
189 
190  // Usd Prim Type to Adapter lookup table, concurrent because it could
191  // be potentially filled during concurrent GetPrim calls rather than
192  // just during single-threaded population.
193  using _PrimAdapterMap = tbb::concurrent_unordered_map<
194  TfToken, UsdImagingPrimAdapterSharedPtr, TfHash>;
195 
196  mutable _PrimAdapterMap _primAdapterMap;
197 
198  using _ApiAdapterMap = tbb::concurrent_unordered_map<
199  TfToken, UsdImagingAPISchemaAdapterSharedPtr, TfHash>;
200 
201  mutable _ApiAdapterMap _apiAdapterMap;
202 
203  struct _AdapterSetEntry
204  {
205  // ordered and inclusive of primAdapter
206  _APISchemaAdapters allAdapters;
207 
208  // for identifying prim adapter within same lookup
209  UsdImagingPrimAdapterSharedPtr primAdapter;
210  };
211 
212  // Use UsdPrimTypeInfo pointer as key because they are guaranteed to be
213  // cached at least as long as the stage is open.
214  using _AdapterSetMap = tbb::concurrent_unordered_map<
215  const UsdPrimTypeInfo *, _AdapterSetEntry, TfHash>;
216 
217  mutable _AdapterSetMap _adapterSetMap;
218 
219 
220  using _PrimAdapterPair = std::pair<UsdPrim, UsdImagingPrimAdapterSharedPtr>;
221  _PrimAdapterPair _FindResponsibleAncestor(const UsdPrim &prim);
222 
223 };
224 
225 PXR_NAMESPACE_CLOSE_SCOPE
226 
227 #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.
Base class for all API schema adapters.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:504
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:135
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
size_t hash_value(const half h)
Overload hash_value for half.
Definition: half.h:45
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:75