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