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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_SCENE_INDEX_H
8 #define PXR_IMAGING_HD_SCENE_INDEX_H
9 
10 #include "pxr/pxr.h"
11 
12 #include <set>
13 #include <unordered_map>
14 
16 #include "pxr/base/tf/singleton.h"
18 
19 
20 #include "pxr/usd/sdf/path.h"
21 
22 #include "pxr/imaging/hd/api.h"
26 
28 
30 
31 ///
32 /// Small struct representing a 'prim' in the Hydra scene index. A prim is
33 /// represented by a container data source which contains a tree of properties.
34 ///
36 {
38  HdContainerDataSourceHandle dataSource;
39 
40  /// Does this prim returned by \ref HdSceneIndex::GetPrim exist in the
41  /// scene index?
42  bool IsDefined() const { return bool(dataSource); }
43  /// Same as IsDefined.
44  operator bool() const { return IsDefined(); }
45 };
46 
47 ///
48 /// \class HdSceneIndexBase
49 ///
50 /// Abstract interface to scene data. This class can be queried for scene
51 /// data directly, and it can also register observers to be notified about
52 /// scene changes (see HdSceneIndexObserver).
53 ///
54 class HdSceneIndexBase : public TfRefBase, public TfWeakBase
55 {
56 public:
57  HD_API
59 
60  HD_API
61  ~HdSceneIndexBase() override;
62 
63  // ------------------------------------------------------------------------
64  // Scene Observer API
65  // ------------------------------------------------------------------------
66 
67  /// Adds an observer to this scene index. The given observer will be sent
68  /// notices for prims added, removed, or dirtied after it is added as an
69  /// observer. It will not be sent notices for prims already in the scene
70  /// index; the calling code is responsible for updating observer state
71  /// if the scene index has already been populated. This function is not
72  /// threadsafe.
73  HD_API
74  void AddObserver(const HdSceneIndexObserverPtr &observer);
75 
76  /// Removes an observer from this scene index; the given observer will no
77  /// longer be forwarded notices. Note that the observer won't get any
78  /// notices as a result of being detached from this scene index. If
79  /// \p observer is not registered on this scene index, this call does
80  /// nothing. This function is not threadsafe.
81  HD_API
82  void RemoveObserver(const HdSceneIndexObserverPtr &observer);
83 
84  // ------------------------------------------------------------------------
85  // Scene Data API
86  // ------------------------------------------------------------------------
87 
88  /// Returns a pair of (prim type, datasource). A prim exists at
89  /// \a primPath if and only if datasource is a non-null pointer.
90  /// In particular, we consider the prim to exist even if the prim type
91  /// or the container that datasource points to is empty.
92  ///
93  /// Note that we require \ref GetChildPrimPaths to be consistent with this
94  /// notion of prim existence. That is, unless \a primPath is the absolute
95  /// root path, the prim at \p primPath exists if and only if \p primPath is
96  /// contained in \ref GetChildPrimPaths of the parent path.
97  ///
98  /// This function is expected to be threadsafe.
99  virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const = 0;
100 
101  /// Returns the paths of all scene index prims located immediately below
102  /// \a primPath. This function can be used to traverse
103  /// the scene by recursing from \ref SdfPath::AbsoluteRootPath.
104  /// The traveral is expected to give exactly the set of paths where
105  /// prim exists as defined in \ref GetPrim. The traversal is also expected
106  /// to give the same set of prims as the flattening of the scene index's
107  /// \p PrimsAdded and \p PrimsRemoved messages.
108  ///
109  /// This function is expected to be threadsafe.
110  virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const = 0;
111 
112  /// A convenience function: look up the object at \a primPath, and if
113  /// successful return the datasource at \a locator within that prim. This
114  /// is equivalent to calling \p GetPrim(primPath), and then calling
115  /// \p HdContainerDataSource::Get(prim.dataSource, locator).
116  HdDataSourceBaseHandle GetDataSource(
117  const SdfPath &primPath,
118  const HdDataSourceLocator &locator) const
119  {
121  GetPrim(primPath).dataSource, locator);
122  }
123 
124  // ------------------------------------------------------------------------
125  // System-wide API
126  // ------------------------------------------------------------------------
127 
128  /// Sends a message with optional arguments to this and any upstream input
129  /// scene indices. Scene indices may implement _SystemMessage to provide
130  /// custom handling. See systemMessages.h for common message definitions.
131  HD_API
132  void SystemMessage(
133  const TfToken &messageType,
134  const HdDataSourceBaseHandle &args);
135 
136  // ------------------------------------------------------------------------
137  // User Interface Utilities
138  // ------------------------------------------------------------------------
139 
140  /// Returns a value previously set by SetDisplayName. If no value (or an
141  /// empty string) was last set, this returns a symbol-demangled version of
142  /// the class type itself. This is in service of user interfaces with views
143  /// of scene index chains or graphs.
144  HD_API
145  std::string GetDisplayName() const;
146 
147  /// Allows for scene index instances to be identified in a more contextually
148  /// relevant way. This is in service of user interfaces with views of scene
149  /// index chains or graphs.
150  HD_API
151  void SetDisplayName(const std::string &n);
152 
153  /// Adds a specified tag token to a scene index instance. This is in service
154  /// of user interfaces which want to filter views of a scene index chain
155  /// or graph.
156  HD_API
157  void AddTag(const TfToken &tag);
158 
159  /// Removes a specified tag token to a scene index instance.
160  /// This is in service of user interfaces which want to filter views of a
161  /// scene index chain or graph.
162  HD_API
163  void RemoveTag(const TfToken &tag);
164 
165  /// Returns true if a specified tag token has been added to a scene index
166  /// instance. This is in service of user interfaces which want to filter
167  /// views of a scene index chain or graph.
168  HD_API
169  bool HasTag(const TfToken &tag) const;
170 
171  /// Returns all tag tokens currently added to a scene index instance. This
172  /// is in service of user interfaces which want to filter views of a scene
173  /// index chain or graph.
174  HD_API
175  TfTokenVector GetTags() const;
176 
177 protected:
178 
179  /// Notify attached observers of prims added to the scene. The set of
180  /// scene prims compiled from added/removed notices should match the set
181  /// from a traversal based on \p GetChildPrimPaths. Each prim has a path
182  /// and type. It's possible for \p PrimsAdded to be called for prims that
183  /// already exist; in that case, observers should be sure to update the
184  /// prim type, in case it changed, and resync the prim. This function is
185  /// not threadsafe; some observers expect it to be called from a single
186  /// thread.
187  HD_API
188  void _SendPrimsAdded(
190 
191  /// Notify attached observers of prims removed from the scene. Note that
192  /// this message is considered hierarchical: if \p /Path is removed,
193  /// \p /Path/child is considered removed as well. This function is not
194  /// threadsafe; some observers expect it to be called from a single thread.
195  HD_API
196  void _SendPrimsRemoved(
198 
199  /// Notify attached observers of datasource invalidations from the scene.
200  /// This message is not considered hierarchical on \p primPath; if
201  /// \p /Path is dirtied, \p /Path/child is not necessarily dirtied.
202  /// However, locators are considered hierarchical: if \p primvars is
203  /// dirtied on a prim, \p primvars/color is considered dirtied as well.
204  /// This function is not threadsafe; some observers expect it to be called
205  /// from a single thread.
206  HD_API
207  void _SendPrimsDirtied(
209 
210 
211  /// Notify attached observers of prims (and their descendents) which have
212  /// been renamed or reparented.
213  /// This function is not threadsafe; some observers expect it to be called
214  /// from a single thread.
215  HD_API
216  void _SendPrimsRenamed(
218 
219 
220  /// Returns whether the scene index has any registered observers; this
221  /// information can be used to skip work preparing notices when there are
222  /// no observers.
223  HD_API
224  bool _IsObserved() const;
225 
226  /// Implement in order to react directly to system messages sent from
227  /// downstream.
228  HD_API
229  virtual void _SystemMessage(
230  const TfToken &messageType,
231  const HdDataSourceBaseHandle &args);
232 
233 private:
234  void _RemoveExpiredObservers();
235 
236  // Scoped (RAII) helper to manage tracking recursion depth,
237  // and to remove expired observers after completing delivery.
238  struct _NotifyScope;
239 
240  // Registered observers, in order of registration.
241  using _Observers = std::vector<HdSceneIndexObserverPtr>;
242  _Observers _observers;
243 
244  // Count of in-flight observer notifications
245  int _notifyDepth;
246 
247  // Flag hinting that expired observers may exist.
248  bool _shouldRemoveExpiredObservers;
249 
250  // User-visible label for this scene index
251  std::string _displayName;
252 
253  // Tags used to categorize this scene index
255  _TagSet _tags;
256 };
257 
258 
259 ///
260 /// \class HdSceneIndexNameRegistry
261 ///
262 /// A registry containing named instances of Hydra indexes. Scene Indexes
263 /// are not automatically registered here, and must be manually added
264 /// (generally by the application).
265 ///
267  : public TfSingleton<HdSceneIndexNameRegistry>
268 {
270 
271  HdSceneIndexNameRegistry() = default;
272 
273 public:
274 
275  /// Returns the singleton-instance of this registry.
276  ///
277  HD_API
280  }
281 
282  /// Registers an \p instance of a scene index with a given \p name.
283  ///
284  HD_API
286  const std::string &name, HdSceneIndexBasePtr instance);
287 
288  /// Returns the names of all registered scene indexes.
289  ///
290  HD_API
291  std::vector<std::string> GetRegisteredNames();
292 
293  /// Returns the scene index that was registered with the given \p name.
294  ///
295  HD_API
296  HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name);
297 
298 private:
299 
300  using _NamedInstanceMap =
301  std::unordered_map<std::string, HdSceneIndexBasePtr>;
302 
303  _NamedInstanceMap _namedInstances;
304 };
305 
307 
308 #endif // PXR_IMAGING_HD_SCENE_INDEX_H
HD_API void SetDisplayName(const std::string &n)
static T & GetInstance()
Definition: singleton.h:122
HD_API void _SendPrimsRemoved(const HdSceneIndexObserver::RemovedPrimEntries &entries)
bool IsDefined() const
Definition: sceneIndex.h:42
HD_API void RegisterNamedSceneIndex(const std::string &name, HdSceneIndexBasePtr instance)
HD_API void SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
TfToken primType
Definition: sceneIndex.h:37
#define HD_API
Definition: api.h:23
static HD_API HdSceneIndexNameRegistry & GetInstance()
Definition: sceneIndex.h:278
HD_API void _SendPrimsRenamed(const HdSceneIndexObserver::RenamedPrimEntries &entries)
HD_API void _SendPrimsDirtied(const HdSceneIndexObserver::DirtiedPrimEntries &entries)
virtual HD_API void _SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args)
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
OutGridT const XformOp bool bool
HD_API ~HdSceneIndexBase() override
virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const =0
HD_API std::string GetDisplayName() const
GLdouble n
Definition: glcorearb.h:2008
Definition: token.h:70
HD_API void RemoveObserver(const HdSceneIndexObserverPtr &observer)
std::vector< class SdfPath > SdfPathVector
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_AND_REF_PTRS(HdSceneIndexBase)
HdContainerDataSourceHandle dataSource
Definition: sceneIndex.h:38
HD_API void AddObserver(const HdSceneIndexObserverPtr &observer)
HD_API TfTokenVector GetTags() const
HD_API HdSceneIndexBase()
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
HdDataSourceBaseHandle GetDataSource(const SdfPath &primPath, const HdDataSourceLocator &locator) const
Definition: sceneIndex.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:280
HD_API void AddTag(const TfToken &tag)
HD_API void _SendPrimsAdded(const HdSceneIndexObserver::AddedPrimEntries &entries)
HD_API void RemoveTag(const TfToken &tag)
virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const =0
HD_API HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
**If you just want to fire and args
Definition: thread.h:618
HD_API bool _IsObserved() const
HD_API bool HasTag(const TfToken &tag) const
HD_API std::vector< std::string > GetRegisteredNames()