HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sceneIndexPluginRegistry.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_PLUGIN_REGISTRY_H
8 #define PXR_IMAGING_HD_SCENE_INDEX_PLUGIN_REGISTRY_H
9 
10 #include "pxr/imaging/hd/api.h"
14 
15 #include "pxr/base/tf/singleton.h"
17 #include "pxr/base/tf/token.h"
18 #include "pxr/base/tf/weakPtr.h"
19 
20 #include "pxr/pxr.h"
21 
22 #include <string>
23 #include <memory>
24 
26 
27 #define HDSCENEINDEXPLUGINREGISTRY_TOKENS \
28  ((rendererDisplayName, "__rendererDisplayName")) \
29  ((allRenderers, ""))
30 
31 TF_DECLARE_PUBLIC_TOKENS(HdSceneIndexPluginRegistryTokens, HD_API,
33 
34 class HdSceneIndexPlugin;
35 
36 ///
37 /// A registry for renderer- and application-specific scene indices.
38 ///
40 {
41 public:
42  ///
43  /// Returns the singleton registry for \c HdSceneIndexPlugin
44  ///
45  HD_API
47 
48  ///
49  /// Entry point for defining an HdSceneIndexPlugin plugin.
50  ///
51  template<typename T, typename... Bases>
52  static void Define();
53 
54  ///
55  /// Given a specific scene index plug-in id, give the plug-in a chance
56  /// to add scene indices to the end of the chain. Return value is the
57  /// last scene index -- or inputScene if the plug-in chooses not to act.
58  /// Input arguments may be nullptr if not relevant to this plug-in.
59  HD_API
60  HdSceneIndexBaseRefPtr AppendSceneIndex(
61  const TfToken &sceneIndexPluginId,
62  const HdSceneIndexBaseRefPtr &inputScene,
63  const HdContainerDataSourceHandle &inputArgs,
64  const std::string &renderInstanceId=std::string());
65 
66  ///
67  /// Append scene indices generated by plug-ins registered (via
68  /// RegisterSceneIndexForRenderer) for this renderer. Return value is the
69  /// last scene index -- or inputScene if no plugins are registered or
70  /// taking action. This also includes plug-ins registered for all
71  /// renderers (via an empty renderer string for the loadWithRenderer JSON
72  /// field) to be added in advance of any registered for the specified
73  /// renderer.
74  ///
75  /// Plugin libraries will only be loaded if they declare they are enabled
76  /// for the provided app name. By default, plugins are auto-loaded for all
77  /// apps, but plugin authors can put an loadWithApps array in their
78  /// plugInfo to narrow down the set of applications they are auto-loaded
79  /// for. Providing an empty app name here (the default) means this will not
80  /// auto-load any application-specific plugin libraries.
81  ///
82  HD_API
83  HdSceneIndexBaseRefPtr AppendSceneIndicesForRenderer(
84  const std::string &rendererDisplayName,
85  const HdSceneIndexBaseRefPtr &inputScene,
86  const std::string &renderInstanceId=std::string(),
87  const std::string &appName=std::string());
88 
90  {
93  };
94 
96 
97  ///
98  /// Register a scene index plugin to be instantiated for a specified
99  /// renderer (or all renderers if \p rendererDisplayName is empty).
100  ///
101  /// \note
102  /// The function name is misleading as this registers the plugin and not
103  /// an instance of the scene index itself.
104  ///
105  /// \p sceneIndexPluginId identifies the associated scene index plugin.
106  /// This should match the name used in the plugInfo.json entry and is
107  /// typically the class name (i.e. CPP type name).
108  /// \p inputArgs is a container data source of arguments that is provided as
109  /// a parameter to the scene index plugin's _AppendSceneIndex method.
110  /// \p insertionPhase is a broad ordering value with lower values indicating
111  /// earlier instantiation (possibly given render plugin-specific meaning
112  /// via enum values).
113  /// \p insertionOrder indicates whether this entry should go at the start
114  /// or end of the specified phase.
115  ///
116  /// \note
117  /// The plugInfo entry for a scene index plugin may have a
118  /// "loadWithRenderer" key that specifies a list of renderer display names
119  /// for which the plugin library should be *loaded*. This is separate from
120  /// the registration of the scene index plugin for a renderer here, which
121  /// is still necessary to have it be instantiated and be a part of the
122  /// scene index graph that is built for that renderer.
123  ///
124  /// \note
125  /// This method is typically invoked via the TF_REGISTRY_FUNCTION macro at
126  /// module load time, like:
127  /// \code
128  /// TF_REGISTRY_FUNCTION(HdSceneIndexPlugin)
129  /// {
130  /// HdSceneIndexPluginRegistry::GetInstance()
131  /// .RegisterSceneIndexForRenderer(...);
132  /// }
133  /// \endcode
134  ///
135  /// \note
136  /// This function may be invoked multiple times for the same
137  /// \p sceneIndexPluginId to have multiple instances of the same
138  /// set of scene indices inserted in _AppendSceneIndex at different points
139  /// in the scene index graph.
140  ///
141  HD_API
143  const std::string &rendererDisplayName,
144  const TfToken &sceneIndexPluginId,
145  const HdContainerDataSourceHandle &inputArgs,
146  InsertionPhase insertionPhase,
147  InsertionOrder insertionOrder);
148 
149 
151  std::function<
152  HdSceneIndexBaseRefPtr(
153  const std::string &renderInstanceId,
154  const HdSceneIndexBaseRefPtr &inputScene,
155  const HdContainerDataSourceHandle &inputArgs)>;
156 
157  ///
158  /// Register a scene index to be instantiated via a \p callback for a
159  /// specified renderer (or all renderers if \p rendererDisplayName is
160  /// empty).
161  ///
162  /// This is most useful for application-specific behavior which wants to
163  /// append and manage scene index instances associated with a specific
164  /// render. To aid in that association, the callback is provided a
165  /// renderInstanceId value typically defined by the application itself.
166  ///
167  /// \p inputArgs is a container data source of arguments that is provided as
168  /// a parameter to the scene index plugin's _AppendSceneIndex method.
169  /// \p insertionPhase is a broad ordering value with lower values indicating
170  /// earlier instantiation (possibly given render plugin-specific meaning
171  /// via enum values).
172  /// \p insertionOrder indicates whether this entry should go at the start
173  /// or end of the specified phase.
174  ///
175  /// \note This method should be invoked *before* render index construction
176  /// when Hydra scene index emulation is enabled.
177  ///
178  /// \note
179  /// Unlike the pattern for registering scene index plugins, it is
180  /// better to invoke this method explicitly in application code rather
181  /// than using a TF_REGISTRY_FUNCTION block.
182  ///
183  HD_API
185  const std::string &rendererDisplayName,
186  SceneIndexAppendCallback callback,
187  const HdContainerDataSourceHandle &inputArgs,
188  InsertionPhase insertionPhase,
189  InsertionOrder insertionOrder);
190 
191  /// Loads plugins for \p rendererDisplayName and \p appName.
192  /// Returns the sceneIndexPluginId's in the order they'll be run.
193  /// This does not include scene indices registered via the callback
194  /// registration method.
195  ///
196  /// This is primarily in service of tests.
197  HD_API
198  std::vector<TfToken> LoadAndGetSceneIndexPluginIds(
199  const std::string& rendererDisplayName,
200  const std::string& appName);
201 
203  {
204  std::string rendererDisplayName;
206  int insertionPhase = -1;
207  };
208 
209  /// Gets metadata about the plugin that inserted the given scene index, if
210  /// available.
211  ///
212  /// This is primarily intended for debugging and diagnostic use.
213  bool
215  const HdSceneIndexBaseRefPtr& sceneIndex,
216  PluginInsertionMetadata& metadata);
217 
218  /// Enum to specify the policy for determining the order of scene index
219  /// plugins. This is primarily in service of testing, and to provide a
220  /// pathway to deprecating the registration API in favor of JSON
221  /// metadata-based ordering.
222  /// The default is based off the environment setting
223  /// HD_SCENE_INDEX_PLUGIN_ORDERING_POLICY_DEFAULT.
224  ///
226  {
227  /// The order of plugins is determined solely by the insertion
228  /// phase/order arguments of the C++ registration API.
230 
231  /// The tags and ordering specified in the JSON metadata are used to
232  /// drive the ordering of scene index plugins.
234 
235  /// A hybrid ordering scheme where we attempt to honor *both* the
236  /// insertion phase/order specified via the C++ registration API and the
237  /// tags and ordering specified in the JSON metadata.
238  Hybrid
239  };
240 
241  /// Sets the policy for determining the order of scene index plugins.
242  /// This is primarily in service of testing.
243  HD_API
244  void
246 
247 protected:
248 
250  const PlugRegistry &plugRegistry, const TfType &pluginType) override;
251 
252 private:
254 
255  // Singleton gets private constructed
257  ~HdSceneIndexPluginRegistry() override;
258 
259  void _LoadPluginsForRenderer(
260  const std::string &rendererDisplayName,
261  const std::string &appName);
262 
263  HdSceneIndexPlugin *_GetSceneIndexPlugin(const TfToken &pluginId);
264 
265  struct _Impl;
266  std::unique_ptr<_Impl> _impl;
267 };
268 
269 template<typename T, typename... Bases>
271 {
273 }
274 
276 
277 #endif // PXR_IMAGING_HD_SCENE_INDEX_PLUGIN_REGISTRY_H
void _CollectAdditionalMetadata(const PlugRegistry &plugRegistry, const TfType &pluginType) override
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
HD_API HdSceneIndexBaseRefPtr AppendSceneIndicesForRenderer(const std::string &rendererDisplayName, const HdSceneIndexBaseRefPtr &inputScene, const std::string &renderInstanceId=std::string(), const std::string &appName=std::string())
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
bool GetPluginInsertionMetadataForSceneIndex(const HdSceneIndexBaseRefPtr &sceneIndex, PluginInsertionMetadata &metadata)
#define HD_API
Definition: api.h:23
std::function< HdSceneIndexBaseRefPtr(const std::string &renderInstanceId, const HdSceneIndexBaseRefPtr &inputScene, const HdContainerDataSourceHandle &inputArgs)> SceneIndexAppendCallback
HD_API HdSceneIndexBaseRefPtr AppendSceneIndex(const TfToken &sceneIndexPluginId, const HdSceneIndexBaseRefPtr &inputScene, const HdContainerDataSourceHandle &inputArgs, const std::string &renderInstanceId=std::string())
static HD_API HdSceneIndexPluginRegistry & GetInstance()
Definition: token.h:70
#define HDSCENEINDEXPLUGINREGISTRY_TOKENS
HD_API void SetPluginOrderingPolicy(PluginOrderingPolicy policy)
HD_API std::vector< TfToken > LoadAndGetSceneIndexPluginIds(const std::string &rendererDisplayName, const std::string &appName)
HD_API void RegisterSceneIndexForRenderer(const std::string &rendererDisplayName, const TfToken &sceneIndexPluginId, const HdContainerDataSourceHandle &inputArgs, InsertionPhase insertionPhase, InsertionOrder insertionOrder)
static void Define()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TF_DECLARE_PUBLIC_TOKENS(HdSceneIndexPluginRegistryTokens, HD_API, HDSCENEINDEXPLUGINREGISTRY_TOKENS)
Definition: type.h:47