HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
renderDelegate.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_RENDER_DELEGATE_H
8 #define PXR_IMAGING_HD_RENDER_DELEGATE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
12 #include "pxr/imaging/hd/aov.h"
14 #include "pxr/imaging/hd/command.h"
16 #include "pxr/base/vt/dictionary.h"
17 #include "pxr/base/tf/token.h"
18 
19 #include <memory>
20 
22 
23 class SdfPath;
24 class HdRprim;
25 class HdSprim;
26 class HdBprim;
27 class HdSceneDelegate;
28 class HdRenderIndex;
29 class HdRenderPass;
30 class HdInstancer;
31 class HdDriver;
32 
34 
35 using HdRenderPassSharedPtr = std::shared_ptr<class HdRenderPass>;
36 using HdRenderPassStateSharedPtr = std::shared_ptr<class HdRenderPassState>;
37 using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
38 using HdDriverVector = std::vector<HdDriver*>;
39 
40 ///
41 /// The HdRenderParam is an opaque (to core Hydra) handle, to an object
42 /// that is obtained from the render delegate and passed to each prim
43 /// during Sync processing.
44 ///
46 {
47 public:
49  HD_API
50  virtual ~HdRenderParam();
51 
52  /// Set a custom value in the render param's implementation.
53  /// The \p key identifies the value, while \p value holds the data to set.
54  /// Return true if the value was successfully set, false if the operation is
55  /// not supported for the specified key or value.
56  /// Since this method can be called by client code, it must be thread-safe.
57  HD_API
58  virtual bool SetArbitraryValue(const TfToken& key, const VtValue& value);
59 
60  /// Retrieve a custom value identified by \p key from the render param's
61  /// implementation. The value could have been set via SetArbitraryValue() or
62  /// provided internally by the render param.
63  /// Return an empty VtValue if no value is associated with the key.
64  /// This can either be used to retrieve arbitrary values by the
65  /// render delegate or by Hydra client code.
66  /// Since this method can be called by client code, it must be thread-safe.
67  HD_API
68  virtual VtValue GetArbitraryValue(const TfToken& key) const;
69 
70  /// Check whether a valid custom value exists for the specified \p key
71  /// in the render param's implementation. Returns true if the value
72  /// exists, false otherwise.
73  /// Since this method can be called by client code, it must be thread-safe.
74  HD_API
75  virtual bool HasArbitraryValue(const TfToken& key) const;
76 
77 private:
78  // Hydra will not attempt to copy the class.
79  HdRenderParam(const HdRenderParam &) = delete;
80  HdRenderParam &operator =(const HdRenderParam &) = delete;
81 };
82 
84 
85 ///
86 /// HdRenderSettingDescriptor represents a render setting that a render delegate
87 /// wants to export (e.g. to UI).
88 ///
90 {
91  // A human readable name.
92  std::string name;
93  // The key for HdRenderDelegate::SetRenderSetting/GetRenderSetting.
95  // The default value.
97 };
98 
99 typedef std::vector<HdRenderSettingDescriptor> HdRenderSettingDescriptorList;
100 
101 /// \class HdRenderDelegate
102 ///
104 {
105 public:
106  HD_API
107  virtual ~HdRenderDelegate();
108 
109  ///
110  /// Set list of driver objects, such as a rendering context / devices.
111  /// This is automatically called from HdRenderIndex when a HdDriver is
112  /// provided during its construction. Default implementation does nothing.
113  ///
114  HD_API
115  virtual void SetDrivers(HdDriverVector const& drivers);
116 
117  ///
118  /// Returns a list of typeId's of all supported Rprims by this render
119  /// delegate.
120  ///
121  virtual const TfTokenVector &GetSupportedRprimTypes() const = 0;
122 
123  ///
124  /// Returns a list of typeId's of all supported Sprims by this render
125  /// delegate.
126  ///
127  virtual const TfTokenVector &GetSupportedSprimTypes() const = 0;
128 
129 
130  ///
131  /// Returns a list of typeId's of all supported Bprims by this render
132  /// delegate.
133  ///
134  virtual const TfTokenVector &GetSupportedBprimTypes() const = 0;
135 
136  ///
137  /// Returns an opaque handle to a render param, that in turn is
138  /// passed to each prim created by the render delegate during sync
139  /// processing. This avoids the need to store a global state pointer
140  /// in each prim.
141  ///
142  /// The typical lifetime of the renderParam would match that of the
143  /// RenderDelegate, however the minimal lifetime is that of the Sync
144  /// processing. The param maybe queried multiple times during sync.
145  ///
146  /// A render delegate may return null for the param.
147  ///
148  HD_API
149  virtual HdRenderParam *GetRenderParam() const;
150 
151  ///
152  /// Returns a shared ptr to the resource registry of the current render
153  /// delegate.
154  ///
156 
157  ///
158  /// Set a custom render setting on this render delegate.
159  ///
160  HD_API
161  virtual void SetRenderSetting(TfToken const& key, VtValue const& value);
162 
163  ///
164  /// Get the current value for a render setting.
165  ///
166  HD_API
167  virtual VtValue GetRenderSetting(TfToken const& key) const;
168 
169  ///
170  /// Get the current value for a render setting, taking a desired type
171  /// and a fallback value in case of type mismatch.
172  ///
173  template<typename T>
174  T GetRenderSetting(TfToken const& key, T const& defValue) const {
175  return GetRenderSetting(key).Cast<T>().GetWithDefault(defValue);
176  }
177 
178  ///
179  /// Get the backend-exported render setting descriptors.
180  ///
181  HD_API
183 
184  ///
185  /// Get the current version of the render settings dictionary.
186  ///
187  HD_API
188  virtual unsigned int GetRenderSettingsVersion() const;
189 
190  ///
191  /// Returns an open-format dictionary of render statistics
192  ///
193  HD_API
194  virtual VtDictionary GetRenderStats() const;
195 
196  ///
197  /// Gives capabilities of render delegate as data source
198  /// (conforming to HdRenderCapabilitiesSchema).
199  ///
200  HD_API
201  virtual HdContainerDataSourceHandle GetCapabilities() const;
202 
203  ////////////////////////////////////////////////////////////////////////////
204  ///
205  /// Control of background rendering threads.
206  ///
207  ////////////////////////////////////////////////////////////////////////////
208 
209  ///
210  /// Advertise whether this delegate supports pausing and resuming of
211  /// background render threads. Default implementation returns false.
212  ///
213  HD_API
214  virtual bool IsPauseSupported() const;
215 
216  ///
217  /// Query the delegate's pause state. Returns true if the background
218  /// rendering threads are currently paused.
219  ///
220  HD_API
221  virtual bool IsPaused() const;
222 
223  ///
224  /// Pause all of this delegate's background rendering threads. Default
225  /// implementation does nothing.
226  ///
227  /// Returns \c true if successful.
228  ///
229  HD_API
230  virtual bool Pause();
231 
232  ///
233  /// Resume all of this delegate's background rendering threads previously
234  /// paused by a call to Pause. Default implementation does nothing.
235  ///
236  /// Returns \c true if successful.
237  ///
238  HD_API
239  virtual bool Resume();
240 
241  ///
242  /// Advertise whether this delegate supports stopping and restarting of
243  /// background render threads. Default implementation returns false.
244  ///
245  HD_API
246  virtual bool IsStopSupported() const;
247 
248  ///
249  /// Query the delegate's stop state. Returns true if the background
250  /// rendering threads are not currently active.
251  ///
252  HD_API
253  virtual bool IsStopped() const;
254 
255  ///
256  /// Stop all of this delegate's background rendering threads; if blocking
257  /// is true, the function waits until they exit.
258  /// Default implementation does nothing.
259  ///
260  /// Returns \c true if successfully stopped.
261  ///
262  HD_API
263  virtual bool Stop(bool blocking = true);
264 
265  ///
266  /// Restart all of this delegate's background rendering threads previously
267  /// stopped by a call to Stop. Default implementation does nothing.
268  ///
269  /// Returns \c true if successful.
270  ///
271  HD_API
272  virtual bool Restart();
273 
274  ////////////////////////////////////////////////////////////////////////////
275  ///
276  /// Renderpass Factory
277  ///
278  ////////////////////////////////////////////////////////////////////////////
279 
280  ///
281  /// Request to create a new renderpass.
282  /// \param index the render index to bind to the new renderpass.
283  /// \param collection the rprim collection to bind to the new renderpass.
284  /// \return A shared pointer to the new renderpass or empty on error.
285  ///
287  HdRprimCollection const& collection) = 0;
288 
289  ///
290  /// Request to create a new renderpass state.
291  /// The default implementation creates an HdRenderPassState instance,
292  /// but derived render delegates may instantiate their own state type.
293  /// \param shader The render pass shader to use. If null, a new
294  /// HdRenderPassShared will be created.
295  /// \return A shared pointer to the new renderpass state.
296  ///
297  HD_API
299 
300  ////////////////////////////////////////////////////////////////////////////
301  ///
302  /// Instancer Factory
303  ///
304  ////////////////////////////////////////////////////////////////////////////
305 
306  ///
307  /// Request to create a new instancer.
308  /// \param id The unique identifier of this instancer.
309  /// \return A pointer to the new instancer or nullptr on error.
310  ///
311  virtual HdInstancer *CreateInstancer(HdSceneDelegate *delegate,
312  SdfPath const& id) = 0;
313 
314  virtual void DestroyInstancer(HdInstancer *instancer) = 0;
315 
316  ////////////////////////////////////////////////////////////////////////////
317  ///
318  /// Prim Factories
319  ///
320  ////////////////////////////////////////////////////////////////////////////
321 
322 
323  ///
324  /// Request to Allocate and Construct a new Rprim.
325  /// \param typeId the type identifier of the prim to allocate
326  /// \param rprimId a unique identifier for the prim
327  /// \return A pointer to the new prim or nullptr on error.
328  ///
329  virtual HdRprim *CreateRprim(TfToken const& typeId,
330  SdfPath const& rprimId) = 0;
331 
332  ///
333  /// Request to Destruct and deallocate the prim.
334  ///
335  virtual void DestroyRprim(HdRprim *rPrim) = 0;
336 
337  ///
338  /// Request to Allocate and Construct a new Sprim.
339  /// \param typeId the type identifier of the prim to allocate
340  /// \param sprimId a unique identifier for the prim
341  /// \return A pointer to the new prim or nullptr on error.
342  ///
343  virtual HdSprim *CreateSprim(TfToken const& typeId,
344  SdfPath const& sprimId) = 0;
345 
346  ///
347  /// Request to Allocate and Construct an Sprim to use as a standin, if there
348  /// if an error with another another Sprim of the same type. For example,
349  /// if another prim references a non-exisiting Sprim, the fallback could
350  /// be used.
351  ///
352  /// \param typeId the type identifier of the prim to allocate
353  /// \return A pointer to the new prim or nullptr on error.
354  ///
355  virtual HdSprim *CreateFallbackSprim(TfToken const& typeId) = 0;
356 
357  ///
358  /// Request to Destruct and deallocate the prim.
359  ///
360  virtual void DestroySprim(HdSprim *sprim) = 0;
361 
362  ///
363  /// Request to Allocate and Construct a new Bprim.
364  /// \param typeId the type identifier of the prim to allocate
365  /// \param sprimId a unique identifier for the prim
366  /// \return A pointer to the new prim or nullptr on error.
367  ///
368  virtual HdBprim *CreateBprim(TfToken const& typeId,
369  SdfPath const& bprimId) = 0;
370 
371 
372  ///
373  /// Request to Allocate and Construct a Bprim to use as a standin, if there
374  /// if an error with another another Bprim of the same type. For example,
375  /// if another prim references a non-exisiting Bprim, the fallback could
376  /// be used.
377  ///
378  /// \param typeId the type identifier of the prim to allocate
379  /// \return A pointer to the new prim or nullptr on error.
380  ///
381  virtual HdBprim *CreateFallbackBprim(TfToken const& typeId) = 0;
382 
383  ///
384  /// Request to Destruct and deallocate the prim.
385  ///
386  virtual void DestroyBprim(HdBprim *bprim) = 0;
387 
388  ////////////////////////////////////////////////////////////////////////////
389  ///
390  /// Sync, Execute & Dispatch Hooks
391  ///
392  ////////////////////////////////////////////////////////////////////////////
393 
394  ///
395  /// Notification point from the Engine to the delegate.
396  /// This notification occurs after all Sync's have completed and
397  /// before task execution.
398  ///
399  /// This notification gives the Render Delegate a chance to
400  /// update and move memory that the render may need.
401  ///
402  /// For example, the render delegate might fill primvar buffers or texture
403  /// memory.
404  ///
405  virtual void CommitResources(HdChangeTracker *tracker) = 0;
406 
407  ////////////////////////////////////////////////////////////////////////////
408  ///
409  /// Materials
410  ///
411  ////////////////////////////////////////////////////////////////////////////
412 
413  ///
414  /// Returns a token that indicates material bindings should be used,
415  /// based on the indicated purpose. The default purpose is
416  /// HdTokens->preview.
417  ///
418  HD_API
419  virtual TfToken GetMaterialBindingPurpose() const;
420 
421 
422  /// \deprecated use GetMaterialRenderContexts()
423  HD_API
424  virtual TfToken GetMaterialNetworkSelector() const;
425 
426  ///
427  /// Returns a list, in descending order of preference, that can be used to
428  /// select among multiple material network implementations. The default
429  /// list contains an empty token.
430  ///
431  HD_API
433 
434  /// Returns a list of namespace prefixes for render settings attributes
435  /// relevant to a render delegate. This list is used to gather just the
436  /// relevant attributes from render settings scene description. The default
437  /// is an empty list, which will gather all custom (non-schema) attributes.
438  ///
439  HD_API
441 
442  ///
443  /// Return true to indicate that the render delegate wants rprim primvars
444  /// to be filtered by the scene delegate to reduce the amount of primvars
445  /// that are send to the render delegate. For example the scene delegate
446  /// may check the bound material primvar requirements and send only those
447  /// to the render delegate. Return false to not apply primvar filtering in
448  /// the scene delegate. Defaults to false.
449  ///
450  HD_API
451  virtual bool IsPrimvarFilteringNeeded() const;
452 
453  ///
454  /// Returns the ordered list of shader source types that the render delegate
455  /// supports.
456  ///
457  HD_API
458  virtual TfTokenVector GetShaderSourceTypes() const;
459 
460  ////////////////////////////////////////////////////////////////////////////
461  ///
462  /// AOVs
463  ///
464  ////////////////////////////////////////////////////////////////////////////
465 
466  ///
467  /// Returns a default AOV descriptor for the given named AOV, specifying
468  /// things like preferred format.
469  ///
470  HD_API
471  virtual HdAovDescriptor GetDefaultAovDescriptor(TfToken const& name) const;
472 
473  ////////////////////////////////////////////////////////////////////////////
474  ///
475  /// Commands API
476  ///
477  ////////////////////////////////////////////////////////////////////////////
478 
479  ///
480  /// Get the descriptors for the commands supported by this render delegate.
481  ///
482  HD_API
484 
485  ///
486  /// Invokes the command described by the token \p command with optional
487  /// \p args.
488  ///
489  /// If the command succeeds, returns \c true, otherwise returns \c false.
490  /// A command will generally fail if it is not among those returned by
491  /// GetCommandDescriptors().
492  ///
493  HD_API
494  virtual bool InvokeCommand(
495  const TfToken &command,
496  const HdCommandArgs &args = HdCommandArgs());
497 
498  ///
499  /// Populated when instantiated via the HdRendererPluginRegistry
500  HD_API
501  const std::string &GetRendererDisplayName() {
502  return _displayName;
503  }
504 
505  ////////////////////////////////////////////////////////////////////////////
506  ///
507  /// Hydra 2.0 API
508  ///
509  /// \note The following methods aid in migrating existing 1.0 based
510  /// render delegates to the Hydra 2.0 API.
511  ///
512  ////////////////////////////////////////////////////////////////////////////
513 
514  /// Called after the scene index graph is created during render index
515  /// construction, providing a hook point for the render delegate to
516  /// register an observer of the terminal scene index.
517  ///
518  /// \note Render delegates should not assume that the scene index is fully
519  /// populated at this point.
520  ///
521  HD_API
522  virtual void SetTerminalSceneIndex(
523  const HdSceneIndexBaseRefPtr &terminalSceneIndex);
524 
525  /// Called at the beginning of HdRenderIndex::SyncAll, before render index
526  /// prim sync, to provide the render delegate an opportunity to directly
527  /// process change notices from observing the terminal scene index,
528  /// rather than using the Hydra 1.0 Sync algorithm.
529  ///
530  HD_API
531  virtual void Update();
532 
533  /// Whether or not multithreaded sync is enabled for the
534  /// specified prim type.
535  HD_API
536  virtual bool IsParallelSyncEnabled(const TfToken &primType) const;
537 
538 protected:
539  /// This class must be derived from.
540  HD_API
542  /// Allow derived classes to pass construction-time render settings.
543  HD_API
544  HdRenderDelegate(HdRenderSettingsMap const& settingsMap);
545 
546  ///
547  /// This class is not intended to be copied.
548  ///
549  HdRenderDelegate(const HdRenderDelegate &) = delete;
550  HdRenderDelegate &operator=(const HdRenderDelegate &) = delete;
551 
552  HD_API
554  HdRenderSettingDescriptorList const& defaultSettings);
555 
556  /// Render settings state.
558  unsigned int _settingsVersion;
559 
560 private:
561 
562  friend class HdRendererPlugin;
563  ///
564  /// Populated when instantiated via the HdRendererPluginRegistry and
565  /// currently used to associate a renderer delegate instance with related
566  /// code and resources.
567  void _SetRendererDisplayName(const std::string &displayName) {
568  _displayName = displayName;
569  }
570  std::string _displayName;
571 
572 };
573 
575 
576 #endif //PXR_IMAGING_HD_RENDER_DELEGATE_H
virtual HD_API void SetRenderSetting(TfToken const &key, VtValue const &value)
virtual void DestroyRprim(HdRprim *rPrim)=0
virtual const TfTokenVector & GetSupportedSprimTypes() const =0
virtual void DestroySprim(HdSprim *sprim)=0
virtual HD_API TfTokenVector GetMaterialRenderContexts() const
std::shared_ptr< class HdRenderPassState > HdRenderPassStateSharedPtr
Definition: engine.h:25
HD_API const std::string & GetRendererDisplayName()
Populated when instantiated via the HdRendererPluginRegistry.
virtual HdResourceRegistrySharedPtr GetResourceRegistry() const =0
virtual HdSprim * CreateSprim(TfToken const &typeId, SdfPath const &sprimId)=0
virtual HD_API ~HdRenderDelegate()
virtual HD_API VtValue GetRenderSetting(TfToken const &key) const
std::shared_ptr< class HdRenderPass > HdRenderPassSharedPtr
Definition: engine.h:24
std::shared_ptr< class HdResourceRegistry > HdResourceRegistrySharedPtr
virtual HD_API void Update()
std::vector< HdRenderSettingDescriptor > HdRenderSettingDescriptorList
virtual HdBprim * CreateFallbackBprim(TfToken const &typeId)=0
virtual HD_API bool Resume()
GLsizei const GLfloat * value
Definition: glcorearb.h:824
virtual HD_API void SetDrivers(HdDriverVector const &drivers)
virtual HD_API bool IsPaused() const
virtual HdBprim * CreateBprim(TfToken const &typeId, SdfPath const &bprimId)=0
TF_DECLARE_REF_PTRS(HdSceneIndexBase)
#define HD_API
Definition: api.h:23
virtual HD_API bool IsStopSupported() const
virtual HD_API bool InvokeCommand(const TfToken &command, const HdCommandArgs &args=HdCommandArgs())
virtual HdRenderPassSharedPtr CreateRenderPass(HdRenderIndex *index, HdRprimCollection const &collection)=0
virtual HD_API HdRenderSettingDescriptorList GetRenderSettingDescriptors() const
HdRenderSettingsMap _settingsMap
Render settings state.
virtual const TfTokenVector & GetSupportedRprimTypes() const =0
virtual HD_API TfToken GetMaterialNetworkSelector() const
virtual HD_API bool IsPrimvarFilteringNeeded() const
unsigned int _settingsVersion
virtual HD_API TfTokenVector GetRenderSettingsNamespaces() const
Definition: token.h:70
std::vector< HdDriver * > HdDriverVector
Definition: rprim.h:37
virtual HD_API bool Stop(bool blocking=true)
std::vector< HdCommandDescriptor > HdCommandDescriptors
Definition: command.h:91
HdRenderDelegate & operator=(const HdRenderDelegate &)=delete
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual HD_API HdCommandDescriptors GetCommandDescriptors() const
Definition: path.h:273
virtual HdRprim * CreateRprim(TfToken const &typeId, SdfPath const &rprimId)=0
virtual void DestroyBprim(HdBprim *bprim)=0
virtual HD_API HdAovDescriptor GetDefaultAovDescriptor(TfToken const &name) const
virtual HD_API ~HdRenderParam()
virtual HD_API bool IsStopped() const
virtual HD_API TfTokenVector GetShaderSourceTypes() const
virtual HD_API bool HasArbitraryValue(const TfToken &key) const
virtual HD_API void SetTerminalSceneIndex(const HdSceneIndexBaseRefPtr &terminalSceneIndex)
virtual HD_API HdRenderPassStateSharedPtr CreateRenderPassState() const
Definition: sprim.h:34
virtual HdSprim * CreateFallbackSprim(TfToken const &typeId)=0
virtual HD_API bool Pause()
virtual HD_API bool Restart()
HD_API void _PopulateDefaultSettings(HdRenderSettingDescriptorList const &defaultSettings)
virtual void DestroyInstancer(HdInstancer *instancer)=0
virtual HdInstancer * CreateInstancer(HdSceneDelegate *delegate, SdfPath const &id)=0
virtual void CommitResources(HdChangeTracker *tracker)=0
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
VtDictionary HdCommandArgs
Definition: command.h:54
T GetRenderSetting(TfToken const &key, T const &defValue) const
static VtValue Cast(VtValue const &val)
Definition: value.h:1189
GLuint index
Definition: glcorearb.h:786
HD_API HdRenderDelegate()
This class must be derived from.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: bprim.h:39
virtual const TfTokenVector & GetSupportedBprimTypes() const =0
TfHashMap< TfToken, VtValue, TfToken::HashFunctor > HdRenderSettingsMap
**If you just want to fire and args
Definition: thread.h:618
virtual HD_API TfToken GetMaterialBindingPurpose() const
virtual HD_API bool IsPauseSupported() const
virtual HD_API VtValue GetArbitraryValue(const TfToken &key) const
virtual HD_API VtDictionary GetRenderStats() const
virtual HD_API HdRenderParam * GetRenderParam() const
virtual HD_API unsigned int GetRenderSettingsVersion() const
virtual HD_API bool SetArbitraryValue(const TfToken &key, const VtValue &value)
Definition: value.h:146
virtual HD_API bool IsParallelSyncEnabled(const TfToken &primType) const
virtual HD_API HdContainerDataSourceHandle GetCapabilities() const