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