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