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