All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
stageSceneIndex.h
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
12#include "pxr/usdImaging/usdImaging/api.h"
13#include "pxr/usdImaging/usdImaging/dataSourceStageGlobals.h"
14#include "pxr/usdImaging/usdImaging/types.h"
15
16#include "pxr/imaging/hd/sceneIndex.h"
17
18#include "pxr/usd/usd/notice.h"
19#include "pxr/usd/usd/stage.h"
20
21#include <mutex>
22#include <set>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
26#define USDIMAGING_STAGE_SCENE_INDEX_TOKENS \
27 (includeUnloadedPrims) \
28
29TF_DECLARE_PUBLIC_TOKENS(UsdImagingStageSceneIndexTokens, USDIMAGING_API,
30 USDIMAGING_STAGE_SCENE_INDEX_TOKENS);
31
32using UsdImagingPrimAdapterSharedPtr =
33 std::shared_ptr<class UsdImagingPrimAdapter>;
34class UsdImaging_AdapterManager;
35
36TF_DECLARE_REF_PTRS(UsdImagingStageSceneIndex);
37
38class UsdImagingStageSceneIndex : public HdSceneIndexBase
39{
40public:
41 static UsdImagingStageSceneIndexRefPtr New(
42 HdContainerDataSourceHandle const &inputArgs = nullptr) {
43 return TfCreateRefPtr(new UsdImagingStageSceneIndex(inputArgs));
44 }
45
46 USDIMAGING_API
47 ~UsdImagingStageSceneIndex();
48
49 // ------------------------------------------------------------------------
50 // Scene index API
51
52 USDIMAGING_API
53 HdSceneIndexPrim GetPrim(const SdfPath & primPath) const override;
54
55 USDIMAGING_API
56 SdfPathVector GetChildPrimPaths(const SdfPath & primPath) const override;
57
58 // ------------------------------------------------------------------------
59 // App-facing API
60
61 // Set the USD stage to pull data from. Note that this will delete all
62 // scene index prims and reset stage global data.
63 USDIMAGING_API
64 void SetStage(UsdStageRefPtr stage);
65
66 // Set the time, and call PrimsDirtied for any time-varying attributes.
67 //
68 // PrimsDirtied is only called if the time is different from the last call
69 // or forceDirtyingTimeDeps is true.
70 USDIMAGING_API
71 void SetTime(UsdTimeCode time, bool forceDirtyingTimeDeps = false);
72
73 // Return the current time.
74 USDIMAGING_API
75 UsdTimeCode GetTime() const;
76
77 // Apply queued stage edits to imaging scene.
78 // If the USD stage is edited while the scene index is pulling from it,
79 // those edits get queued and deferred. Calling ApplyPendingUpdates will
80 // turn resync requests into PrimsAdded/PrimsRemoved, and property changes
81 // into PrimsDirtied.
82 USDIMAGING_API
83 void ApplyPendingUpdates();
84
85private:
86 USDIMAGING_API
87 UsdImagingStageSceneIndex(HdContainerDataSourceHandle const &inputArgs);
88
89 Usd_PrimFlagsPredicate _GetPrimPredicate() const;
90
91 void _ApplyPendingResyncs();
92 void _ComputeDirtiedEntries(
93 const std::map<SdfPath, TfTokenVector> &pathToUsdProperties,
94 SdfPathVector * primPathsToResync,
95 UsdImagingPropertyInvalidationType invalidationType,
96 HdSceneIndexObserver::DirtiedPrimEntries * dirtiedPrims) const;
97
98 class _StageGlobals : public UsdImagingDataSourceStageGlobals
99 {
100 public:
101 // Datasource-facing API
102 void FlagAsTimeVarying(
103 const SdfPath & hydraPath,
104 const HdDataSourceLocator & locator) const override;
105
106 void FlagAsAssetPathDependent(
107 const SdfPath & usdPath) const override;
108
109 UsdTimeCode GetTime() const override;
110
111 // Scene index-facing API
112 void SetTime(UsdTimeCode time,
114
115 void RemoveAssetPathDependentsUnder(const SdfPath &path);
116
117 void InvalidateAssetPathDependentsUnder(
118 const SdfPath &path,
119 std::vector<SdfPath> *primsToInvalidate,
120 std::map<SdfPath, TfTokenVector> *propertiesToInvalidate) const;
121
122 void Clear();
123
124 private:
125 struct _PathHashCompare {
126 static bool equal(const SdfPath &a, const SdfPath &b) {
127 return a == b;
128 }
129 static size_t hash(const SdfPath &p) {
130 return hash_value(p);
131 }
132 };
133 using _VariabilityMap = tbb::concurrent_hash_map<SdfPath,
134 HdDataSourceLocatorSet, _PathHashCompare>;
135 mutable _VariabilityMap _timeVaryingLocators;
136
137 using _AssetPathDependentsSet = std::set<SdfPath>;
138 mutable _AssetPathDependentsSet _assetPathDependents;
139 mutable std::mutex _assetPathDependentsMutex;
140
141 UsdTimeCode _time;
142 };
143
144 const bool _includeUnloadedPrims;
145
146 UsdStageRefPtr _stage;
147 _StageGlobals _stageGlobals;
148
149 // Population
150 void _Populate();
151 void _PopulateSubtree(UsdPrim subtreeRoot);
152
153 // Edit processing
154 void _OnUsdObjectsChanged(UsdNotice::ObjectsChanged const& notice,
155 UsdStageWeakPtr const& sender);
156 TfNotice::Key _objectsChangedNoticeKey;
157
158 // Note: resync paths mean we remove the whole subtree and repopulate.
159 SdfPathVector _usdPrimsToResync;
160 // Property changes get converted into PrimsDirtied messages.
161 std::map<SdfPath, TfTokenVector> _usdPropertiesToUpdate;
162 std::map<SdfPath, TfTokenVector> _usdPropertiesToResync;
163
164 using _PrimAdapterPair = std::pair<UsdPrim, UsdImagingPrimAdapterSharedPtr>;
165 _PrimAdapterPair _FindResponsibleAncestor(const UsdPrim &prim) const;
166
167 std::unique_ptr<UsdImaging_AdapterManager> const _adapterManager;
168};
169
170PXR_NAMESPACE_CLOSE_SCOPE
171
172#endif
Represents an object that can identify the location of a data source.
Represents a set of data source locators closed under descendancy.
Abstract interface to scene data.
Definition: sceneIndex.h:48
virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const =0
Returns the paths of all scene index prims located immediately below primPath.
virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const =0
Returns a pair of (prim type, datasource) for the object at primPath.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Handle-object returned by TfNotice::Register().
Definition: notice.h:239
This class is used as a context object with global stage information, that gets passed down to dataso...
Notice sent in response to authored changes that affect UsdObjects.
Definition: notice.h:111
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:67
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:58
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:81
Small struct representing a 'prim' in the Hydra scene index.
Definition: sceneIndex.h:35
size_t hash_value(const TfToken &x)
Overload hash_value for TfToken.
Definition: token.h:437