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