taskController.h
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"
30 #include "pxr/imaging/hdx/selectionTracker.h"
31 #include "pxr/imaging/hdx/renderSetupTask.h"
32 #include "pxr/imaging/hdx/shadowTask.h"
33 #include "pxr/imaging/hdx/colorCorrectionTask.h"
34 #include "pxr/imaging/hdx/boundingBoxTask.h"
35 
36 #include "pxr/imaging/hd/aov.h"
37 #include "pxr/imaging/hd/renderIndex.h"
38 #include "pxr/imaging/hd/sceneDelegate.h"
39 #include "pxr/imaging/hd/task.h"
40 
41 #include "pxr/imaging/cameraUtil/framing.h"
43 #include "pxr/usd/sdf/path.h"
44 
45 #include "pxr/base/gf/bbox3d.h"
46 #include "pxr/base/gf/matrix4d.h"
47 
48 PXR_NAMESPACE_OPEN_SCOPE
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
63  ~HdxTaskController();
64 
66  HdRenderIndex* GetRenderIndex() { return _index; }
67  HdRenderIndex const* GetRenderIndex() const { return _index; }
68 
71  SdfPath const& GetControllerId() const { return _controllerId; }
72 
75 
79  HDX_API
80  HdTaskSharedPtrVector const GetRenderingTasks() const;
81 
84  HDX_API
85  HdTaskSharedPtrVector const GetPickingTasks() const;
86 
89 
91  HDX_API
92  void SetCollection(HdRprimCollection const& collection);
93 
98  HDX_API
99  void SetRenderParams(HdxRenderTaskParams const& params);
100 
105  HDX_API
106  void SetRenderTags(TfTokenVector const& renderTags);
107 
110 
114  HDX_API
115  void SetRenderOutputs(TfTokenVector const& names);
116 
119  HDX_API
120  void SetViewportRenderOutput(TfToken const& name);
121 
125  HDX_API
126  HdRenderBuffer* GetRenderOutput(TfToken const& name);
127 
129  HDX_API
130  void SetRenderOutputSettings(TfToken const& name,
131  HdAovDescriptor const& desc);
132 
134  HDX_API
135  HdAovDescriptor GetRenderOutputSettings(TfToken const& name) const;
136 
142  HDX_API
143  void SetPresentationOutput(TfToken const &api, VtValue const &framebuffer);
144 
147 
151  HDX_API
152  void SetLightingState(GlfSimpleLightingContextPtr const& src);
153 
156 
160  HDX_API
161  void SetRenderBufferSize(const GfVec2i &size);
162 
166  HDX_API
167  void SetFraming(const CameraUtilFraming &framing);
168 
178  HDX_API
179  void SetOverrideWindowPolicy(
180  const std::pair<bool, CameraUtilConformWindowPolicy> &policy);
181 
184  HDX_API
185  void SetCameraPath(SdfPath const& id);
186 
190  HDX_API
191  void SetRenderViewport(GfVec4d const& viewport);
192 
196  HDX_API
197  void SetFreeCameraMatrices(GfMatrix4d const& viewMatrix,
198  GfMatrix4d const& projectionMatrix);
201  HDX_API
202  void SetFreeCameraClipPlanes(std::vector<GfVec4d> const& clipPlanes);
203 
206 
208  HDX_API
209  void SetEnableSelection(bool enable);
210 
212  HDX_API
213  void SetSelectionColor(GfVec4f const& color);
214 
216  HDX_API
217  void SetSelectionLocateColor(GfVec4f const& color);
218 
221  HDX_API
222  void SetSelectionEnableOutline(bool enableOutline);
223 
227  HDX_API
228  void SetSelectionOutlineRadius(unsigned int radius);
229 
232 
234  HDX_API
235  void SetEnableShadows(bool enable);
236 
239  HDX_API
240  void SetShadowParams(HdxShadowTaskParams const& params);
241 
244 
246  HDX_API
247  bool IsConverged() const;
248 
251 
253  HDX_API
254  void SetColorCorrectionParams(HdxColorCorrectionTaskParams const& params);
255 
258 
260  HDX_API
261  void SetBBoxParams(const HdxBoundingBoxTaskParams& params);
262 
265 
269  HDX_API
270  void SetEnablePresentation(bool enabled);
271 
272 private:
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:
393  using _ValueCache = TfHashMap<TfToken, VtValue, TfToken::HashFunctor>;
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 
433 PXR_NAMESPACE_CLOSE_SCOPE
434 
435 #endif // PXR_IMAGING_HDX_TASK_CONTROLLER_H
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:120
Basic type for a vector of 2 int components.
Definition: vec2i.h:60
Describes the allocation structure of a render buffer bprim.
Definition: aov.h:84
Basic type for a vector of 4 double components.
Definition: vec4d.h:62
ColorCorrectionTask parameters.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
RenderTask parameters (renderpass state).
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:87
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
bool TfMapLookup(Container const &map, Key const &key, Result *valuePtr)
Checks if an item exists in a map or a TfHashMap.
Definition: stl.h:88
A named, semantic collection of objects.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
T const & Get() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1109
Basic type for a vector of 4 float components.
Definition: vec4f.h:62
A render buffer is a handle to a data resource that can be rendered into, such as a 2d image for a dr...
Definition: renderBuffer.h:49
A bundle of state describing an AOV ("Arbitrary Output Variable") display channel.
Definition: aov.h:46
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1053
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
BoundingBoxTask parameters.
Framing information.
Definition: framing.h:79