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  HDX_API
62  ~HdxTaskController();
63 
65  HdRenderIndex* GetRenderIndex() { return _index; }
66  HdRenderIndex const* GetRenderIndex() const { return _index; }
67 
70  SdfPath const& GetControllerId() const { return _controllerId; }
71 
74 
78  HDX_API
79  HdTaskSharedPtrVector const GetRenderingTasks() const;
80 
83  HDX_API
84  HdTaskSharedPtrVector const GetPickingTasks() const;
85 
88 
90  HDX_API
91  void SetCollection(HdRprimCollection const& collection);
92 
97  HDX_API
98  void SetRenderParams(HdxRenderTaskParams const& params);
99 
104  HDX_API
105  void SetRenderTags(TfTokenVector const& renderTags);
106 
109 
113  HDX_API
114  void SetRenderOutputs(TfTokenVector const& names);
115 
118  HDX_API
119  void SetViewportRenderOutput(TfToken const& name);
120 
124  HDX_API
125  HdRenderBuffer* GetRenderOutput(TfToken const& name);
126 
128  HDX_API
129  void SetRenderOutputSettings(TfToken const& name,
130  HdAovDescriptor const& desc);
131 
133  HDX_API
134  HdAovDescriptor GetRenderOutputSettings(TfToken const& name) const;
135 
141  HDX_API
142  void SetPresentationOutput(TfToken const &api, VtValue const &framebuffer);
143 
146 
150  HDX_API
151  void SetLightingState(GlfSimpleLightingContextPtr const& src);
152 
155 
159  HDX_API
160  void SetRenderBufferSize(const GfVec2i &size);
161 
165  HDX_API
166  void SetFraming(const CameraUtilFraming &framing);
167 
177  HDX_API
178  void SetOverrideWindowPolicy(
179  const std::pair<bool, CameraUtilConformWindowPolicy> &policy);
180 
183  HDX_API
184  void SetCameraPath(SdfPath const& id);
185 
189  HDX_API
190  void SetRenderViewport(GfVec4d const& viewport);
191 
195  HDX_API
196  void SetFreeCameraMatrices(GfMatrix4d const& viewMatrix,
197  GfMatrix4d const& projectionMatrix);
200  HDX_API
201  void SetFreeCameraClipPlanes(std::vector<GfVec4d> const& clipPlanes);
202 
205 
207  HDX_API
208  void SetEnableSelection(bool enable);
209 
211  HDX_API
212  void SetSelectionColor(GfVec4f const& color);
213 
215  HDX_API
216  void SetSelectionLocateColor(GfVec4f const& color);
217 
220  HDX_API
221  void SetSelectionEnableOutline(bool enableOutline);
222 
226  HDX_API
227  void SetSelectionOutlineRadius(unsigned int radius);
228 
231 
233  HDX_API
234  void SetEnableShadows(bool enable);
235 
238  HDX_API
239  void SetShadowParams(HdxShadowTaskParams const& params);
240 
243 
245  HDX_API
246  bool IsConverged() const;
247 
250 
252  HDX_API
253  void SetColorCorrectionParams(HdxColorCorrectionTaskParams const& params);
254 
257 
259  HDX_API
260  void SetBBoxParams(const HdxBoundingBoxTaskParams& params);
261 
264 
268  HDX_API
269  void SetEnablePresentation(bool enabled);
270 
271 private:
275  HdxTaskController(HdxTaskController const&) = delete;
276  HdxTaskController &operator=(HdxTaskController const&) = delete;
277 
278  HdRenderIndex *_index;
279  SdfPath const _controllerId;
280 
281  // Create taskController objects. Since the camera is a parameter
282  // to the tasks, _CreateCamera() should be called first.
283  void _CreateRenderGraph();
284 
285  void _CreateLightingTask();
286  void _CreateShadowTask();
287  SdfPath _CreateSkydomeTask();
288  SdfPath _CreateRenderTask(TfToken const& materialTag);
289  void _CreateOitResolveTask();
290  void _CreateSelectionTask();
291  void _CreateColorizeSelectionTask();
292  void _CreateColorCorrectionTask();
293  void _CreateVisualizeAovTask();
294  void _CreatePickTask();
295  void _CreatePickFromRenderBufferTask();
296  void _CreateBoundingBoxTask();
297  void _CreateAovInputTask();
298  void _CreatePresentTask();
299 
300  void _SetCameraParamForTasks(SdfPath const& id);
301  void _SetCameraFramingForTasks();
302  void _UpdateAovDimensions(GfVec2i const& dimensions);
303 
304  void _SetBlendStateForMaterialTag(TfToken const& materialTag,
305  HdxRenderTaskParams *renderParams) const;
306 
307  // Render graph topology control.
308  bool _ShadowsEnabled() const;
309  bool _SelectionEnabled() const;
310  bool _ColorizeSelectionEnabled() const;
311  bool _ColorCorrectionEnabled() const;
312  bool _VisualizeAovEnabled() const;
313  bool _ColorizeQuantizationEnabled() const;
314  bool _AovsSupported() const;
315  bool _UsingAovs() const;
316 
317  // Helper function for renderbuffer management.
318  SdfPath _GetRenderTaskPath(TfToken const& materialTag) const;
319  SdfPath _GetAovPath(TfToken const& aov) const;
320  SdfPathVector _GetAovEnabledTasks() const;
321 
322  // Helper functions to set up the lighting state for the built-in lights
323  bool _SupportBuiltInLightTypes();
324  void _SetBuiltInLightingState(GlfSimpleLightingContextPtr const& src);
325 
326  // Helper function to get the built-in Camera light type SimpleLight for
327  // Storm, and DistantLight otherwise
328  TfToken _GetCameraLightType();
329 
330  // Helper functions to set the parameters of a light, get a particular light
331  // in the scene, replace and remove Sprims from the scene
332  VtValue _GetDomeLightTexture(GlfSimpleLight const& light);
333  void _SetParameters(SdfPath const& pathName, GlfSimpleLight const& light);
334  void _SetMaterialNetwork(SdfPath const& pathName,
335  GlfSimpleLight const& light);
336  GlfSimpleLight _GetLightAtId(size_t const& pathIdx);
337  void _RemoveLightSprim(size_t const& pathIdx);
338  void _ReplaceLightSprim(size_t const& pathIdx, GlfSimpleLight const& light,
339  SdfPath const& pathName);
340 
341  // A private scene delegate member variable backs the tasks and the free cam
342  // this controller generates. To keep _Delegate simple, the containing class
343  // is responsible for marking things dirty.
344  class _Delegate : public HdSceneDelegate
345  {
346  public:
347  _Delegate(HdRenderIndex *parentIndex,
348  SdfPath const& delegateID)
349  : HdSceneDelegate(parentIndex, delegateID)
350  {}
351  ~_Delegate() override = default;
352 
353  // HdxTaskController set/get interface
354  template <typename T>
355  void SetParameter(SdfPath const& id, TfToken const& key,
356  T const& value) {
357  _valueCacheMap[id][key] = value;
358  }
359  template <typename T>
360  T GetParameter(SdfPath const& id, TfToken const& key) const {
361  VtValue vParams;
362  _ValueCache vCache;
363  TF_VERIFY(
364  TfMapLookup(_valueCacheMap, id, &vCache) &&
365  TfMapLookup(vCache, key, &vParams) &&
366  vParams.IsHolding<T>());
367  return vParams.Get<T>();
368  }
369  bool HasParameter(SdfPath const& id, TfToken const& key) const {
370  _ValueCache vCache;
371  if (TfMapLookup(_valueCacheMap, id, &vCache) &&
372  vCache.count(key) > 0) {
373  return true;
374  }
375  return false;
376  }
377 
378  // HdSceneDelegate interface
379  VtValue Get(SdfPath const& id, TfToken const& key) override;
380  GfMatrix4d GetTransform(SdfPath const& id) override;
381  VtValue GetLightParamValue(SdfPath const& id,
382  TfToken const& paramName) override;
383  VtValue GetMaterialResource(SdfPath const& id) override;
384  bool IsEnabled(TfToken const& option) const override;
386  GetRenderBufferDescriptor(SdfPath const& id) override;
387  TfTokenVector GetTaskRenderTags(SdfPath const& taskId) override;
388 
389 
390  private:
391  using _ValueCache = TfHashMap<TfToken, VtValue, TfToken::HashFunctor>;
392  using _ValueCacheMap = TfHashMap<SdfPath, _ValueCache, SdfPath::Hash>;
393  _ValueCacheMap _valueCacheMap;
394  };
395  _Delegate _delegate;
396  std::unique_ptr<class HdxFreeCameraSceneDelegate> _freeCameraSceneDelegate;
397 
398  // Generated tasks.
399  SdfPath _simpleLightTaskId;
400  SdfPath _shadowTaskId;
401  SdfPathVector _renderTaskIds;
402  SdfPath _aovInputTaskId;
403  SdfPath _oitResolveTaskId;
404  SdfPath _selectionTaskId;
405  SdfPath _colorizeSelectionTaskId;
406  SdfPath _colorCorrectionTaskId;
407  SdfPath _visualizeAovTaskId;
408  SdfPath _pickTaskId;
409  SdfPath _pickFromRenderBufferTaskId;
410  SdfPath _boundingBoxTaskId;
411  SdfPath _presentTaskId;
412 
413  // Current active camera
414  SdfPath _activeCameraId;
415 
416  // Built-in lights
417  SdfPathVector _lightIds;
418 
419  // Generated renderbuffers
420  SdfPathVector _aovBufferIds;
421  TfTokenVector _aovOutputs;
422  TfToken _viewportAov;
423 
424  GfVec2i _renderBufferSize;
425  CameraUtilFraming _framing;
426  std::pair<bool, CameraUtilConformWindowPolicy> _overrideWindowPolicy;
427 
428  GfVec4d _viewport;
429 };
430 
431 PXR_NAMESPACE_CLOSE_SCOPE
432 
433 #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:61
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:63
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:88
#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:63
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