HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sceneIndex.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 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_IMAGING_HD_SCENE_INDEX_H
25 #define PXR_IMAGING_HD_SCENE_INDEX_H
26 
27 #include "pxr/pxr.h"
28 
29 #include <set>
30 #include <unordered_map>
31 
33 #include "pxr/base/tf/singleton.h"
34 
35 #include "pxr/usd/sdf/path.h"
36 
37 #include "pxr/imaging/hd/api.h"
41 
43 
44 
45 ///
46 /// Small struct representing a 'prim' in the Hydra scene index. A prim is
47 /// represented by a container data source which contains a tree of properties.
48 ///
50 {
52  HdContainerDataSourceHandle dataSource;
53 };
54 
55 ///
56 /// \class HdSceneIndexBase
57 ///
58 /// Abstract interface to scene data. This class can be queried for scene
59 /// data directly, and it can also register observers to be notified about
60 /// scene changes (see HdSceneIndexObserver).
61 ///
62 class HdSceneIndexBase : public TfRefBase, public TfWeakBase
63 {
64 public:
65 
66  HD_API
67  ~HdSceneIndexBase() override;
68 
69  // ------------------------------------------------------------------------
70  // Scene Observer API
71  // ------------------------------------------------------------------------
72 
73  /// Adds an observer to this scene index. The given observer will be sent
74  /// notices for prims added, removed, or dirtied after it is added as an
75  /// observer. It will not be sent notices for prims already in the scene
76  /// index; the calling code is responsible for updating observer state
77  /// if the scene index has already been populated. This function is not
78  /// threadsafe.
79  HD_API
80  void AddObserver(const HdSceneIndexObserverPtr &observer);
81 
82  /// Removes an observer from this scene index; the given observer will no
83  /// longer be forwarded notices. Note that the observer won't get any
84  /// notices as a result of being detached from this scene index. If
85  /// \p observer is not registered on this scene index, this call does
86  /// nothing. This function is not threadsafe.
87  HD_API
88  void RemoveObserver(const HdSceneIndexObserverPtr &observer);
89 
90  // ------------------------------------------------------------------------
91  // Scene Data API
92  // ------------------------------------------------------------------------
93 
94  /// Returns a pair of (prim type, datasource) for the object at
95  /// \p primPath. If no such object exists, the type will be the empty
96  /// token and the datasource will be null. This function is expected to
97  /// be threadsafe.
98  virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const = 0;
99 
100  /// Returns the paths of all scene index prims located immediately below
101  /// \p primPath. This function can be used to traverse
102  /// the scene by recursing from \p SdfPath::AbsoluteRootPath(); such a
103  /// traversal is expected to give the same set of prims as the
104  /// flattening of the scene index's \p PrimsAdded and \p PrimsRemoved
105  /// messages. This function is expected to be threadsafe.
106  virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const = 0;
107 
108  /// A convenience function: look up the object at \p primPath, and if
109  /// successful return the datasource at \p locator within that prim. This
110  /// is equivalent to calling \p GetPrim(primPath), and then calling
111  /// \p HdContainerDataSource::Get(prim.dataSource, locator).
112  HdDataSourceBaseHandle GetDataSource(
113  const SdfPath &primPath,
114  const HdDataSourceLocator &locator) const
115  {
117  GetPrim(primPath).dataSource, locator);
118  }
119 
120 protected:
121 
122  /// Notify attached observers of prims added to the scene. The set of
123  /// scene prims compiled from added/removed notices should match the set
124  /// from a traversal based on \p GetChildPrimNames. Each prim has a path
125  /// and type. It's possible for \p PrimsAdded to be called for prims that
126  /// already exist; in that case, observers should be sure to update the
127  /// prim type, in case it changed, and resync the prim. This function is
128  /// not threadsafe; some observers expect it to be called from a single
129  /// thread.
130  HD_API
131  void _SendPrimsAdded(
133 
134  /// Notify attached observers of prims removed from the scene. Note that
135  /// this message is considered hierarchical: if \p /Path is removed,
136  /// \p /Path/child is considered removed as well. This function is not
137  /// threadsafe; some observers expect it to be called from a single thread.
138  HD_API
139  void _SendPrimsRemoved(
141 
142  /// Notify attached observers of datasource invalidations from the scene.
143  /// This message is not considered hierarchical on \p primPath; if
144  /// \p /Path is dirtied, \p /Path/child is not necessarily dirtied.
145  /// However, locators are considered hierarchical: if \p primvars is
146  /// dirtied on a prim, \p primvars/color is considered dirtied as well.
147  /// This function is not threadsafe; some observers expect it to be called
148  /// from a single thread.
149  HD_API
150  void _SendPrimsDirtied(
152 
153  /// Returns whether the scene index has any registered observers; this
154  /// information can be used to skip work preparing notices when there are
155  /// no observers.
156  HD_API
157  bool _IsObserved() const;
158 
159 private:
160 
161  using _ObserverSet = std::set<HdSceneIndexObserverPtr>;
162  _ObserverSet _observers;
163 };
164 
165 
166 ///
167 /// \class HdSceneIndexNameRegistry
168 ///
169 /// A registry containing named instances of Hydra indexes. Scene Indexes
170 /// are not automatically registered here, and must be manually added
171 /// (generally by the application).
172 ///
174  : public TfSingleton<HdSceneIndexNameRegistry>
175 {
177 
178  HdSceneIndexNameRegistry() = default;
179 
180 public:
181 
182  /// Returns the singleton-instance of this registry.
183  ///
186  }
187 
188  /// Registers an \p instance of a scene index with a given \p name.
189  ///
190  HD_API
192  const std::string &name, HdSceneIndexBasePtr instance);
193 
194  /// Returns the names of all registered scene indexes.
195  ///
196  HD_API
197  std::vector<std::string> GetRegisteredNames();
198 
199  /// Returns the scene index that was registered with the given \p name.
200  ///
201  HD_API
202  HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name);
203 
204 private:
205 
206  using _NamedInstanceMap =
207  std::unordered_map<std::string, HdSceneIndexBasePtr>;
208 
209  _NamedInstanceMap _namedInstances;
210 };
211 
213 
214 #endif // PXR_IMAGING_HD_SCENE_INDEX_H
static T & GetInstance()
Definition: singleton.h:137
HD_API void _SendPrimsRemoved(const HdSceneIndexObserver::RemovedPrimEntries &entries)
HD_API void RegisterNamedSceneIndex(const std::string &name, HdSceneIndexBasePtr instance)
TfToken primType
Definition: sceneIndex.h:51
#define HD_API
Definition: api.h:40
HD_API void _SendPrimsDirtied(const HdSceneIndexObserver::DirtiedPrimEntries &entries)
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
HD_API ~HdSceneIndexBase() override
virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const =0
Definition: token.h:87
HD_API void RemoveObserver(const HdSceneIndexObserverPtr &observer)
HdContainerDataSourceHandle dataSource
Definition: sceneIndex.h:52
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
HD_API void AddObserver(const HdSceneIndexObserverPtr &observer)
HdDataSourceBaseHandle GetDataSource(const SdfPath &primPath, const HdDataSourceLocator &locator) const
Definition: sceneIndex.h:112
Definition: path.h:290
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:211
HD_API void _SendPrimsAdded(const HdSceneIndexObserver::AddedPrimEntries &entries)
virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const =0
HD_API HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1394
static HdSceneIndexNameRegistry & GetInstance()
Definition: sceneIndex.h:184
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HD_API bool _IsObserved() const
HD_API std::vector< std::string > GetRegisteredNames()