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