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  /// -------------------------------------------------------
198  /// Selection API
199 
200  /// Turns the selection task on or off.
201  HDX_API
202  void SetEnableSelection(bool enable);
203 
204  /// Set the selection color.
205  HDX_API
206  void SetSelectionColor(GfVec4f const& color);
207 
208  /// Set the selection locate (over) color.
209  HDX_API
211 
212  /// Set if the selection highlight should be rendered as an outline around
213  /// the selected objects or as a solid color overlaid on top of them.
214  HDX_API
215  void SetSelectionEnableOutline(bool enableOutline);
216 
217  /// Set the selection outline radius (thickness) in pixels. This is only
218  /// relevant if the highlight is meant to be rendered as an outline (if
219  /// SetSelectionRenderOutline(true) is called).
220  HDX_API
221  void SetSelectionOutlineRadius(unsigned int radius);
222 
223  /// -------------------------------------------------------
224  /// Shadow API
225 
226  /// Turns the shadow task on or off.
227  HDX_API
228  void SetEnableShadows(bool enable);
229 
230  /// Set the shadow params. Note: params.camera will
231  /// be overwritten, since it comes from SetCameraPath/SetCameraState.
232  HDX_API
234 
235  /// -------------------------------------------------------
236  /// Progressive Image Generation
237 
238  /// Return whether the image has converged.
239  ///
240  /// \deprecated
241  ///
242  /// Instead, checks whether HdxTask::IsConverged for a
243  /// task in the render index for each path in GetRenderingTaskPaths().
244  ///
245  /// Deprecated in preparation of changing to an
246  /// HdxTaskControllerSceneIndex.
247  ///
248  HDX_API
249  bool IsConverged() const;
250 
251  /// -------------------------------------------------------
252  /// Color Correction API
253 
254  /// Configure color correction by settings params.
255  HDX_API
257 
258  /// -------------------------------------------------------
259  /// Bounding Box API
260 
261  /// Set the bounding box params.
262  HDX_API
264 
265  /// -------------------------------------------------------
266  /// Present API
267 
268  /// Enable / disable presenting the render to bound framebuffer.
269  /// An application may choose to manage the AOVs that are rendered into
270  /// itself and skip the task controller's presentation.
271  HDX_API
272  void SetEnablePresentation(bool enabled);
273 
274 private:
275  ///
276  /// This class is not intended to be copied.
277  ///
278  HdxTaskController(HdxTaskController const&) = delete;
279  HdxTaskController &operator=(HdxTaskController const&) = delete;
280 
281  HdRenderIndex *_index;
282  SdfPath const _controllerId;
283  bool _gpuEnabled;
284 
285  // Create taskController objects. Since the camera is a parameter
286  // to the tasks, _CreateCamera() should be called first.
287  void _CreateRenderGraph();
288 
289  void _CreateLightingTask();
290  void _CreateShadowTask();
291  SdfPath _CreateSkydomeTask();
292  SdfPath _CreateRenderTask(TfToken const& materialTag);
293  void _CreateOitResolveTask();
294  void _CreateSelectionTask();
295  void _CreateColorizeSelectionTask();
296  void _CreateColorCorrectionTask();
297  void _CreateVisualizeAovTask();
298  void _CreatePickTask();
299  void _CreatePickFromRenderBufferTask();
300  void _CreateBoundingBoxTask();
301  void _CreateAovInputTask();
302  void _CreatePresentTask();
303 
304  void _SetCameraParamForTasks(SdfPath const& id);
305  void _SetCameraFramingForTasks();
306  void _UpdateAovDimensions(GfVec2i const& dimensions);
307 
308  void _SetBlendStateForMaterialTag(TfToken const& materialTag,
309  HdxRenderTaskParams *renderParams) const;
310 
311  // Render graph topology control.
312  bool _ShadowsEnabled() const;
313  bool _SelectionEnabled() const;
314  bool _ColorizeSelectionEnabled() const;
315  bool _ColorCorrectionEnabled() const;
316  bool _VisualizeAovEnabled() const;
317  bool _ColorizeQuantizationEnabled() const;
318  bool _AovsSupported() const;
319  bool _UsingAovs() const;
320 
321  // Helper function for renderbuffer management.
322  SdfPath _GetRenderTaskPath(TfToken const& materialTag) const;
323  SdfPath _GetAovPath(TfToken const& aov) const;
324  SdfPathVector _GetAovEnabledTasks() const;
325 
326  // Helper functions to set up the lighting state for the built-in lights
327  bool _SupportBuiltInLightTypes();
328  void _SetBuiltInLightingState(GlfSimpleLightingContextPtr const& src);
329 
330  // Helper function to get the built-in Camera light type SimpleLight for
331  // Storm, and DistantLight otherwise
332  TfToken _GetCameraLightType();
333 
334  // Helper functions to set the parameters of a light, get a particular light
335  // in the scene, replace and remove Sprims from the scene
336  VtValue _GetDomeLightTexture(GlfSimpleLight const& light);
337  void _SetParameters(SdfPath const& pathName, GlfSimpleLight const& light);
338  void _SetMaterialNetwork(SdfPath const& pathName,
339  GlfSimpleLight const& light);
340  GlfSimpleLight _GetLightAtId(size_t const& pathIdx);
341  void _RemoveLightSprim(size_t const& pathIdx);
342  void _ReplaceLightSprim(size_t const& pathIdx, GlfSimpleLight const& light,
343  SdfPath const& pathName);
344 
345  // A private scene delegate member variable backs the tasks and the free cam
346  // this controller generates. To keep _Delegate simple, the containing class
347  // is responsible for marking things dirty.
348  class _Delegate : public HdSceneDelegate
349  {
350  public:
351  _Delegate(HdRenderIndex *parentIndex,
352  SdfPath const& delegateID)
353  : HdSceneDelegate(parentIndex, delegateID)
354  {}
355  ~_Delegate() override = default;
356 
357  // HdxTaskController set/get interface
358  template <typename T>
359  void SetParameter(SdfPath const& id, TfToken const& key,
360  T const& value) {
361  _valueCacheMap[id][key] = value;
362  }
363  template <typename T>
364  T GetParameter(SdfPath const& id, TfToken const& key) const {
365  VtValue vParams;
366  _ValueCache vCache;
367  TF_VERIFY(
368  TfMapLookup(_valueCacheMap, id, &vCache) &&
369  TfMapLookup(vCache, key, &vParams) &&
370  vParams.IsHolding<T>());
371  return vParams.Get<T>();
372  }
373  bool HasParameter(SdfPath const& id, TfToken const& key) const {
374  _ValueCache vCache;
375  if (TfMapLookup(_valueCacheMap, id, &vCache) &&
376  vCache.count(key) > 0) {
377  return true;
378  }
379  return false;
380  }
381 
382  // HdSceneDelegate interface
383  VtValue Get(SdfPath const& id, TfToken const& key) override;
384  GfMatrix4d GetTransform(SdfPath const& id) override;
385  VtValue GetLightParamValue(SdfPath const& id,
386  TfToken const& paramName) override;
387  VtValue GetMaterialResource(SdfPath const& id) override;
388  bool IsEnabled(TfToken const& option) const override;
390  GetRenderBufferDescriptor(SdfPath const& id) override;
391  TfTokenVector GetTaskRenderTags(SdfPath const& taskId) override;
392 
393 
394  private:
396  using _ValueCacheMap = TfHashMap<SdfPath, _ValueCache, SdfPath::Hash>;
397  _ValueCacheMap _valueCacheMap;
398  };
399  _Delegate _delegate;
400  std::unique_ptr<class HdxFreeCameraSceneDelegate> _freeCameraSceneDelegate;
401 
402  // Generated tasks.
403  SdfPath _simpleLightTaskId;
404  SdfPath _shadowTaskId;
405  SdfPathVector _renderTaskIds;
406  SdfPath _aovInputTaskId;
407  SdfPath _oitResolveTaskId;
408  SdfPath _selectionTaskId;
409  SdfPath _colorizeSelectionTaskId;
410  SdfPath _colorCorrectionTaskId;
411  SdfPath _visualizeAovTaskId;
412  SdfPath _pickTaskId;
413  SdfPath _pickFromRenderBufferTaskId;
414  SdfPath _boundingBoxTaskId;
415  SdfPath _presentTaskId;
416 
417  // Current active camera
418  SdfPath _activeCameraId;
419 
420  // Built-in lights
421  SdfPathVector _lightIds;
422 
423  // Generated renderbuffers
424  SdfPathVector _aovBufferIds;
425  TfTokenVector _aovOutputs;
426  TfToken _viewportAov;
427 
428  GfVec2i _renderBufferSize;
429  CameraUtilFraming _framing;
430  std::optional<CameraUtilConformWindowPolicy> _overrideWindowPolicy;
431 
432  GfVec4d _viewport;
433 };
434 
436 
437 #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)
hboost::math::policies::policy< hboost::math::policies::domain_error< hboost::math::policies::ignore_error >, hboost::math::policies::pole_error< hboost::math::policies::ignore_error >, hboost::math::policies::overflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::underflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::denorm_error< hboost::math::policies::ignore_error >, hboost::math::policies::rounding_error< hboost::math::policies::ignore_error >, hboost::math::policies::evaluation_error< hboost::math::policies::ignore_error >, hboost::math::policies::indeterminate_result_error< hboost::math::policies::ignore_error > > policy
Definition: SYS_MathCbrt.h:35
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.
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:1121
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:273
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.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HDX_API void SetEnablePresentation(bool enabled)
GLuint color
Definition: glcorearb.h:1261
bool IsHolding() const
Definition: value.h:1064
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 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:146
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.