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