HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
taskController.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 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_HDX_TASK_CONTROLLER_H
8 #define PXR_IMAGING_HDX_TASK_CONTROLLER_H
9 
10 #include "pxr/pxr.h"
11 
12 #include "pxr/imaging/hdx/api.h"
18 
19 #include "pxr/imaging/hd/aov.h"
22 #include "pxr/imaging/hd/task.h"
23 
26 #include "pxr/usd/sdf/path.h"
27 
28 #include "pxr/base/gf/bbox3d.h"
29 #include "pxr/base/gf/matrix4d.h"
30 
32 
33 // XXX: This API is transitional. At the least, render/picking/selection
34 // APIs should be decoupled.
35 
36 class HdRenderBuffer;
37 
38 class HdxTaskController final
39 {
40 public:
41  HDX_API
42  HdxTaskController(HdRenderIndex *renderIndex,
43  SdfPath const& controllerId,
44  bool gpuEnabled = true);
45  HDX_API
47 
48  /// Return the render index this controller is bound to.
49  HdRenderIndex* GetRenderIndex() { return _index; }
50  HdRenderIndex const* GetRenderIndex() const { return _index; }
51 
52  /// Return the controller's scene-graph id (prefixed to any
53  /// scene graph objects it creates).
54  SdfPath const& GetControllerId() const { return _controllerId; }
55 
56  /// -------------------------------------------------------
57  /// Execution API
58 
59  /// Obtain paths to the tasks managed by the task controller,
60  /// for image generation. The tasks returned will be different
61  /// based on current renderer state.
62  HDX_API
64 
65  /// Obtain paths to tasks managed by the task controller,
66  /// for picking.
67  HDX_API
69 
70  /// \deprecated Use GetRenderingTaskPaths
71  ///
72  /// Deprecated in preparation of changing to an
73  /// HdxTaskControllerSceneIndex.
74  ///
75  HDX_API
77 
78  /// \deprecated Use GetPickingTaskPaths
79  ///
80  /// Deprecated in preparation of changing to an
81  /// HdxTaskControllerSceneIndex.
82  ///
83  HDX_API
85 
86  /// -------------------------------------------------------
87  /// Rendering API
88 
89  /// Set the collection to be rendered.
90  HDX_API
91  void SetCollection(HdRprimCollection const& collection);
92 
93  /// Set the render params. Note: params.viewport will
94  /// be overwritten, since it comes from SetRenderViewport.
95  /// XXX: For GL renders, HdxTaskController relies on the caller to
96  /// correctly set GL_SAMPLE_ALPHA_TO_COVERAGE.
97  HDX_API
99 
100  /// Set the "view" opinion of the scenes render tags.
101  /// The opinion is the base opinion for the entire scene.
102  /// Individual tasks (such as the shadow task) may
103  /// have a stronger opinion and override this opinion
104  HDX_API
105  void SetRenderTags(TfTokenVector const& renderTags);
106 
107  /// -------------------------------------------------------
108  /// AOV API
109 
110  /// Set the list of outputs to be rendered. If outputs.size() == 1,
111  /// this will send that output to the viewport via a colorizer task.
112  /// Note: names should come from HdAovTokens.
113  HDX_API
114  void SetRenderOutputs(TfTokenVector const& names);
115 
116  /// Set which output should be rendered to the viewport. The empty token
117  /// disables viewport rendering.
118  HDX_API
119  void SetViewportRenderOutput(TfToken const& name);
120 
121  /// Get the buffer for a rendered output. Note: the caller should call
122  /// Resolve(), as HdxTaskController doesn't guarantee the buffer will
123  /// be resolved.
124  HDX_API
126 
127  /// Set custom parameters for an AOV.
128  HDX_API
130  HdAovDescriptor const& desc);
131 
132  /// Get parameters for an AOV.
133  HDX_API
135 
136  /// The destination API (e.g., OpenGL, see hgiInterop for details) and
137  /// framebuffer that the AOVs are presented into. The framebuffer
138  /// is a VtValue that encoding a framebuffer in a destination API
139  /// specific way.
140  /// E.g., a uint32_t (aka GLuint) for framebuffer object for OpenGL.
141  HDX_API
142  void SetPresentationOutput(TfToken const &api, VtValue const &framebuffer);
143 
144  /// -------------------------------------------------------
145  /// Lighting API
146 
147  /// Set the lighting state for the scene. HdxTaskController maintains
148  /// a set of light sprims with data set from the lights in "src".
149  /// @param src Lighting state to implement.
150  HDX_API
151  void SetLightingState(GlfSimpleLightingContextPtr const& src);
152 
153  /// -------------------------------------------------------
154  /// Camera and Framing API
155 
156  /// Set the size of the render buffers backing the AOVs.
157  /// GUI applications should set this to the size of the window.
158  ///
159  HDX_API
160  void SetRenderBufferSize(const GfVec2i &size);
161 
162  /// Determines how the filmback of the camera is mapped into
163  /// the pixels of the render buffer and what pixels of the render
164  /// buffer will be rendered into.
165  HDX_API
166  void SetFraming(const CameraUtilFraming &framing);
167 
168  /// Specifies whether to force a window policy when conforming
169  /// the frustum of the camera to match the display window of
170  /// the camera framing.
171  HDX_API
173  const std::optional<CameraUtilConformWindowPolicy> &policy);
174 
175  /// -- Scene camera --
176  /// Set the camera param on tasks to a USD camera path.
177  HDX_API
178  void SetCameraPath(SdfPath const& id);
179 
180  /// Set the viewport param on tasks.
181  ///
182  /// \deprecated Use SetFraming and SetRenderBufferSize instead.
183  HDX_API
184  void SetRenderViewport(GfVec4d const& viewport);
185 
186  /// -- Free camera --
187  /// Set the view and projection matrices for the free camera.
188  /// Note: The projection matrix must be pre-adjusted for the window policy.
189  HDX_API
190  void SetFreeCameraMatrices(GfMatrix4d const& viewMatrix,
191  GfMatrix4d const& projectionMatrix);
192  /// Set the free camera clip planes.
193  /// (Note: Scene cameras use clipping planes authored on the camera prim)
194  HDX_API
195  void SetFreeCameraClipPlanes(std::vector<GfVec4d> const& clipPlanes);
196 
197  /// Get the free camera's Hydra prim path
198  HDX_API
200 
201  /// -------------------------------------------------------
202  /// Selection API
203 
204  /// Turns the selection task on or off.
205  HDX_API
206  void SetEnableSelection(bool enable);
207 
208  /// Set the selection color.
209  HDX_API
210  void SetSelectionColor(GfVec4f const& color);
211 
212  /// Set the selection locate (over) color.
213  HDX_API
215 
216  /// Set if the selection highlight should be rendered as an outline around
217  /// the selected objects or as a solid color overlaid on top of them.
218  HDX_API
219  void SetSelectionEnableOutline(bool enableOutline);
220 
221  /// Set the selection outline radius (thickness) in pixels. This is only
222  /// relevant if the highlight is meant to be rendered as an outline (if
223  /// SetSelectionRenderOutline(true) is called).
224  HDX_API
225  void SetSelectionOutlineRadius(unsigned int radius);
226 
227  /// -------------------------------------------------------
228  /// Shadow API
229 
230  /// Turns the shadow task on or off.
231  HDX_API
232  void SetEnableShadows(bool enable);
233 
234  /// Set the shadow params. Note: params.camera will
235  /// be overwritten, since it comes from SetCameraPath/SetCameraState.
236  HDX_API
238 
239  /// -------------------------------------------------------
240  /// Progressive Image Generation
241 
242  /// Return whether the image has converged.
243  ///
244  /// \deprecated
245  ///
246  /// Instead, checks whether HdxTask::IsConverged for a
247  /// task in the render index for each path in GetRenderingTaskPaths().
248  ///
249  /// Deprecated in preparation of changing to an
250  /// HdxTaskControllerSceneIndex.
251  ///
252  HDX_API
253  bool IsConverged() const;
254 
255  /// -------------------------------------------------------
256  /// Color Correction API
257 
258  /// Configure color correction by settings params.
259  HDX_API
261 
262  /// -------------------------------------------------------
263  /// Bounding Box API
264 
265  /// Set the bounding box params.
266  HDX_API
268 
269  /// -------------------------------------------------------
270  /// Present API
271 
272  /// Enable / disable presenting the render to bound framebuffer.
273  /// An application may choose to manage the AOVs that are rendered into
274  /// itself and skip the task controller's presentation.
275  HDX_API
276  void SetEnablePresentation(bool enabled);
277 
278 private:
279  ///
280  /// This class is not intended to be copied.
281  ///
282  HdxTaskController(HdxTaskController const&) = delete;
283  HdxTaskController &operator=(HdxTaskController const&) = delete;
284 
285  HdRenderIndex *_index;
286  SdfPath const _controllerId;
287  bool _gpuEnabled;
288 
289  // Create taskController objects. Since the camera is a parameter
290  // to the tasks, _CreateCamera() should be called first.
291  void _CreateRenderGraph();
292 
293  void _CreateLightingTask();
294  void _CreateShadowTask();
295  SdfPath _CreateSkydomeTask();
296  SdfPath _CreateRenderTask(TfToken const& materialTag);
297  void _CreateOitResolveTask();
298  void _CreateSelectionTask();
299  void _CreateColorizeSelectionTask();
300  void _CreateColorCorrectionTask();
301  void _CreateVisualizeAovTask();
302  void _CreatePickTask();
303  void _CreatePickFromRenderBufferTask();
304  void _CreateBoundingBoxTask();
305  void _CreateAovInputTask();
306  void _CreatePresentTask();
307 
308  void _SetCameraParamForTasks(SdfPath const& id);
309  void _SetCameraFramingForTasks();
310  void _UpdateAovDimensions(GfVec2i const& dimensions);
311 
312  void _SetBlendStateForMaterialTag(TfToken const& materialTag,
313  HdxRenderTaskParams *renderParams) const;
314 
315  // Render graph topology control.
316  bool _ShadowsEnabled() const;
317  bool _SelectionEnabled() const;
318  bool _ColorizeSelectionEnabled() const;
319  bool _ColorCorrectionEnabled() const;
320  bool _VisualizeAovEnabled() const;
321  bool _ColorizeQuantizationEnabled() const;
322  bool _AovsSupported() const;
323  bool _UsingAovs() const;
324 
325  // Helper function for renderbuffer management.
326  SdfPath _GetRenderTaskPath(TfToken const& materialTag) const;
327  SdfPath _GetAovPath(TfToken const& aov) const;
328  SdfPathVector _GetAovEnabledTasks() const;
329 
330  // Helper functions to set up the lighting state for the built-in lights
331  bool _SupportBuiltInLightTypes();
332  void _SetBuiltInLightingState(GlfSimpleLightingContextPtr const& src);
333 
334  // Helper function to get the built-in Camera light type SimpleLight for
335  // Storm, and DistantLight otherwise
336  TfToken _GetCameraLightType();
337 
338  // Helper functions to set the parameters of a light, get a particular light
339  // in the scene, replace and remove Sprims from the scene
340  VtValue _GetDomeLightTexture(GlfSimpleLight const& light);
341  void _SetParameters(SdfPath const& pathName, GlfSimpleLight const& light);
342  void _SetMaterialNetwork(SdfPath const& pathName,
343  GlfSimpleLight const& light);
344  GlfSimpleLight _GetLightAtId(size_t const& pathIdx);
345  void _RemoveLightSprim(size_t const& pathIdx);
346  void _ReplaceLightSprim(size_t const& pathIdx, GlfSimpleLight const& light,
347  SdfPath const& pathName);
348 
349  // A private scene delegate member variable backs the tasks and the free cam
350  // this controller generates. To keep _Delegate simple, the containing class
351  // is responsible for marking things dirty.
352  class _Delegate : public HdSceneDelegate
353  {
354  public:
355  _Delegate(HdRenderIndex *parentIndex,
356  SdfPath const& delegateID)
357  : HdSceneDelegate(parentIndex, delegateID)
358  {}
359  ~_Delegate() override = default;
360 
361  // HdxTaskController set/get interface
362  template <typename T>
363  void SetParameter(SdfPath const& id, TfToken const& key,
364  T const& value) {
365  _valueCacheMap[id][key] = value;
366  }
367  template <typename T>
368  T GetParameter(SdfPath const& id, TfToken const& key) const {
369  VtValue vParams;
370  _ValueCache vCache;
371  TF_VERIFY(
372  TfMapLookup(_valueCacheMap, id, &vCache) &&
373  TfMapLookup(vCache, key, &vParams) &&
374  vParams.IsHolding<T>());
375  return vParams.Get<T>();
376  }
377  bool HasParameter(SdfPath const& id, TfToken const& key) const {
378  _ValueCache vCache;
379  if (TfMapLookup(_valueCacheMap, id, &vCache) &&
380  vCache.count(key) > 0) {
381  return true;
382  }
383  return false;
384  }
385 
386  // HdSceneDelegate interface
387  VtValue Get(SdfPath const& id, TfToken const& key) override;
388  GfMatrix4d GetTransform(SdfPath const& id) override;
389  VtValue GetLightParamValue(SdfPath const& id,
390  TfToken const& paramName) override;
391  VtValue GetMaterialResource(SdfPath const& id) override;
392  bool IsEnabled(TfToken const& option) const override;
394  GetRenderBufferDescriptor(SdfPath const& id) override;
395  TfTokenVector GetTaskRenderTags(SdfPath const& taskId) override;
396 
397 
398  private:
400  using _ValueCacheMap = TfHashMap<SdfPath, _ValueCache, SdfPath::Hash>;
401  _ValueCacheMap _valueCacheMap;
402  };
403  _Delegate _delegate;
404  std::unique_ptr<class HdxFreeCameraSceneDelegate> _freeCameraSceneDelegate;
405 
406  // Generated tasks.
407  SdfPath _simpleLightTaskId;
408  SdfPath _shadowTaskId;
409  SdfPathVector _renderTaskIds;
410  SdfPath _aovInputTaskId;
411  SdfPath _oitResolveTaskId;
412  SdfPath _selectionTaskId;
413  SdfPath _colorizeSelectionTaskId;
414  SdfPath _colorCorrectionTaskId;
415  SdfPath _visualizeAovTaskId;
416  SdfPath _pickTaskId;
417  SdfPath _pickFromRenderBufferTaskId;
418  SdfPath _boundingBoxTaskId;
419  SdfPath _presentTaskId;
420 
421  // Current active camera
422  SdfPath _activeCameraId;
423 
424  // Built-in lights
425  SdfPathVector _lightIds;
426 
427  // Generated renderbuffers
428  SdfPathVector _aovBufferIds;
429  TfTokenVector _aovOutputs;
430  TfToken _viewportAov;
431 
432  GfVec2i _renderBufferSize;
433  CameraUtilFraming _framing;
434  std::optional<CameraUtilConformWindowPolicy> _overrideWindowPolicy;
435 
436  GfVec4d _viewport;
437 };
438 
440 
441 #endif // PXR_IMAGING_HDX_TASK_CONTROLLER_H
HDX_API HdTaskSharedPtrVector GetRenderingTasks() const
HDX_API void SetOverrideWindowPolicy(const std::optional< CameraUtilConformWindowPolicy > &policy)
HDX_API ~HdxTaskController()
HDX_API void SetRenderParams(HdxRenderTaskParams const &params)
HDX_API HdAovDescriptor GetRenderOutputSettings(TfToken const &name) const
Get parameters for an AOV.
Definition: vec2i.h:43
HDX_API void SetBBoxParams(const HdxBoundingBoxTaskParams &params)
Set the bounding box params.
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Definition: vec4d.h:45
HDX_API SdfPathVector GetPickingTaskPaths() const
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glcorearb.h:2539
#define HDX_API
Definition: api.h:23
HDX_API void SetRenderOutputs(TfTokenVector const &names)
HDX_API void SetRenderBufferSize(const GfVec2i &size)
std::vector< HdTaskSharedPtr > HdTaskSharedPtrVector
Definition: renderIndex.h:58
HDX_API HdTaskSharedPtrVector GetPickingTasks() const
HDX_API HdRenderBuffer * GetRenderOutput(TfToken const &name)
GLenum const GLfloat * params
Definition: glcorearb.h:105
HdRenderIndex * GetRenderIndex()
Return the render index this controller is bound to.
HDX_API void SetViewportRenderOutput(TfToken const &name)
HdRenderIndex const * GetRenderIndex() const
SdfPath const & GetControllerId() const
bool TfMapLookup(Container const &map, Key const &key, Result *valuePtr)
Definition: stl.h:69
HDX_API void SetFraming(const CameraUtilFraming &framing)
HDX_API void SetLightingState(GlfSimpleLightingContextPtr const &src)
T const & Get() const &
Definition: value.h:1063
HDX_API void SetCameraPath(SdfPath const &id)
Definition: token.h:70
GLuint framebuffer
Definition: glcorearb.h:1287
std::vector< class SdfPath > SdfPathVector
HDX_API HdxTaskController(HdRenderIndex *renderIndex, SdfPath const &controllerId, bool gpuEnabled=true)
HDX_API void SetSelectionColor(GfVec4f const &color)
Set the selection color.
GLuint id
Definition: glcorearb.h:655
HDX_API void SetFreeCameraClipPlanes(std::vector< GfVec4d > const &clipPlanes)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:280
HDX_API bool IsConverged() const
HDX_API void SetShadowParams(HdxShadowTaskParams const &params)
HDX_API void SetSelectionEnableOutline(bool enableOutline)
Definition: vec4f.h:45
HDX_API void SetRenderOutputSettings(TfToken const &name, HdAovDescriptor const &desc)
Set custom parameters for an AOV.
HDX_API SdfPathVector GetRenderingTaskPaths() const
GLsizeiptr size
Definition: glcorearb.h:664
HDX_API void SetColorCorrectionParams(HdxColorCorrectionTaskParams const &params)
Configure color correction by settings params.
HDX_API void SetEnablePresentation(bool enabled)
GLuint color
Definition: glcorearb.h:1261
bool IsHolding() const
Definition: value.h:1002
HDX_API void SetRenderViewport(GfVec4d const &viewport)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HDX_API void SetRenderTags(TfTokenVector const &renderTags)
HDX_API SdfPath GetFreeCameraPath()
Get the free camera's Hydra prim path.
HDX_API void SetPresentationOutput(TfToken const &api, VtValue const &framebuffer)
HDX_API void SetSelectionOutlineRadius(unsigned int radius)
HDX_API void SetEnableSelection(bool enable)
Turns the selection task on or off.
Definition: value.h:89
HDX_API void SetCollection(HdRprimCollection const &collection)
Set the collection to be rendered.
HDX_API void SetFreeCameraMatrices(GfMatrix4d const &viewMatrix, GfMatrix4d const &projectionMatrix)
HDX_API void SetEnableShadows(bool enable)
Turns the shadow task on or off.
GLenum src
Definition: glcorearb.h:1793
HDX_API void SetSelectionLocateColor(GfVec4f const &color)
Set the selection locate (over) color.