engine.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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 
26 
27 #ifndef PXR_USD_IMAGING_USD_IMAGING_GL_ENGINE_H
28 #define PXR_USD_IMAGING_USD_IMAGING_GL_ENGINE_H
29 
30 #include "pxr/pxr.h"
31 #include "pxr/usdImaging/usdImagingGL/api.h"
32 #include "pxr/usdImaging/usdImagingGL/version.h"
33 #include "pxr/usdImaging/usdImaging/version.h"
34 
36 #include "pxr/usdImaging/usdImagingGL/rendererSettings.h"
37 
38 #include "pxr/imaging/cameraUtil/conformWindow.h"
39 
40 #include "pxr/imaging/hd/driver.h"
41 #include "pxr/imaging/hd/engine.h"
42 #include "pxr/imaging/hd/rprimCollection.h"
43 #include "pxr/imaging/hd/pluginRenderDelegateUniqueHandle.h"
44 
45 #include "pxr/imaging/hdx/selectionTracker.h"
46 #include "pxr/imaging/hdx/renderSetupTask.h"
47 
48 #include "pxr/imaging/hgi/hgi.h"
49 
52 
53 #include "pxr/imaging/hgi/hgi.h"
54 
55 #include "pxr/usd/sdf/path.h"
56 #include "pxr/usd/usd/timeCode.h"
57 
58 #include "pxr/base/gf/frustum.h"
59 #include "pxr/base/gf/matrix4d.h"
60 #include "pxr/base/gf/vec4d.h"
61 #include "pxr/base/gf/vec4f.h"
62 #include "pxr/base/gf/vec4i.h"
63 
64 #include "pxr/base/vt/dictionary.h"
65 
67 
68 PXR_NAMESPACE_OPEN_SCOPE
69 
70 class UsdPrim;
71 class HdRenderIndex;
72 class HdxTaskController;
73 class UsdImagingDelegate;
74 class UsdImagingGLLegacyEngine;
75 class UsdImagingStageSceneIndex;
76 
77 TF_DECLARE_WEAK_AND_REF_PTRS(GlfSimpleLightingContext);
78 TF_DECLARE_WEAK_AND_REF_PTRS(UsdImagingStageSceneIndex);
79 
85 {
86 public:
87 
88  // ---------------------------------------------------------------------
91  // ---------------------------------------------------------------------
92 
94  USDIMAGINGGL_API
95  static bool IsHydraEnabled();
96 
98 
99  // ---------------------------------------------------------------------
102  // ---------------------------------------------------------------------
103 
107  USDIMAGINGGL_API
108  UsdImagingGLEngine(const HdDriver& driver = HdDriver());
109 
110  USDIMAGINGGL_API
111  UsdImagingGLEngine(const SdfPath& rootPath,
112  const SdfPathVector& excludedPaths,
113  const SdfPathVector& invisedPaths=SdfPathVector(),
114  const SdfPath& sceneDelegateID =
116  const HdDriver& driver = HdDriver());
117 
118  // Disallow copies
119  UsdImagingGLEngine(const UsdImagingGLEngine&) = delete;
120  UsdImagingGLEngine& operator=(const UsdImagingGLEngine&) = delete;
121 
122  USDIMAGINGGL_API
124 
126 
127  // ---------------------------------------------------------------------
130  // ---------------------------------------------------------------------
131 
133  USDIMAGINGGL_API
134  void PrepareBatch(const UsdPrim& root,
135  const UsdImagingGLRenderParams& params);
136  USDIMAGINGGL_API
137  void RenderBatch(const SdfPathVector& paths,
138  const UsdImagingGLRenderParams& params);
139 
141  USDIMAGINGGL_API
142  void Render(const UsdPrim& root,
143  const UsdImagingGLRenderParams &params);
144 
147  USDIMAGINGGL_API
148  bool IsConverged() const;
149 
151 
152  // ---------------------------------------------------------------------
155  // ---------------------------------------------------------------------
156 
158  USDIMAGINGGL_API
159  void SetRootTransform(GfMatrix4d const& xf);
160 
162  USDIMAGINGGL_API
163  void SetRootVisibility(bool isVisible);
164 
166 
167  // ---------------------------------------------------------------------
170  // ---------------------------------------------------------------------
171 
174  USDIMAGINGGL_API
175  void SetCameraPath(SdfPath const& id);
176 
180  USDIMAGINGGL_API
181  void SetFraming(CameraUtilFraming const& framing);
182 
192  USDIMAGINGGL_API
194  const std::pair<bool, CameraUtilConformWindowPolicy> &policy);
195 
199  USDIMAGINGGL_API
200  void SetRenderBufferSize(GfVec2i const& size);
201 
207  USDIMAGINGGL_API
208  void SetRenderViewport(GfVec4d const& viewport);
209 
213  USDIMAGINGGL_API
214  void SetWindowPolicy(CameraUtilConformWindowPolicy policy);
215 
220  USDIMAGINGGL_API
221  void SetCameraState(const GfMatrix4d& viewMatrix,
222  const GfMatrix4d& projectionMatrix);
223 
225 
226  // ---------------------------------------------------------------------
229  // ---------------------------------------------------------------------
230 
232  USDIMAGINGGL_API
233  void SetLightingState(GlfSimpleLightingContextPtr const &src);
234 
239  USDIMAGINGGL_API
240  void SetLightingState(GlfSimpleLightVector const &lights,
241  GlfSimpleMaterial const &material,
242  GfVec4f const &sceneAmbient);
243 
245 
246  // ---------------------------------------------------------------------
249  // ---------------------------------------------------------------------
250 
254  USDIMAGINGGL_API
255  void SetSelected(SdfPathVector const& paths);
256 
259  USDIMAGINGGL_API
260  void ClearSelected();
261 
265  USDIMAGINGGL_API
266  void AddSelected(SdfPath const &path, int instanceIndex);
267 
269  USDIMAGINGGL_API
270  void SetSelectionColor(GfVec4f const& color);
271 
273 
274  // ---------------------------------------------------------------------
277  // ---------------------------------------------------------------------
278 
296  USDIMAGINGGL_API
297  bool TestIntersection(
298  const GfMatrix4d &viewMatrix,
299  const GfMatrix4d &projectionMatrix,
300  const UsdPrim& root,
301  const UsdImagingGLRenderParams &params,
302  GfVec3d *outHitPoint,
303  GfVec3d *outHitNormal,
304  SdfPath *outHitPrimPath = NULL,
305  SdfPath *outHitInstancerPath = NULL,
306  int *outHitInstanceIndex = NULL,
307  HdInstancerContext *outInstancerContext = NULL);
308 
311  USDIMAGINGGL_API
312  bool DecodeIntersection(
313  unsigned char const primIdColor[4],
314  unsigned char const instanceIdColor[4],
315  SdfPath *outHitPrimPath = NULL,
316  SdfPath *outHitInstancerPath = NULL,
317  int *outHitInstanceIndex = NULL,
318  HdInstancerContext *outInstancerContext = NULL);
319 
321 
322  // ---------------------------------------------------------------------
325  // ---------------------------------------------------------------------
326 
328  USDIMAGINGGL_API
330 
332  USDIMAGINGGL_API
333  static std::string GetRendererDisplayName(TfToken const &id);
334 
336  USDIMAGINGGL_API
338 
341  USDIMAGINGGL_API
342  bool SetRendererPlugin(TfToken const &id);
343 
345 
346  // ---------------------------------------------------------------------
349  // ---------------------------------------------------------------------
350 
352  USDIMAGINGGL_API
354 
356  USDIMAGINGGL_API
357  bool SetRendererAov(TfToken const& id);
358 
360  USDIMAGINGGL_API
361  HgiTextureHandle GetAovTexture(TfToken const& name) const;
362 
364  USDIMAGINGGL_API
365  HdRenderBuffer* GetAovRenderBuffer(TfToken const& name) const;
366 
368  USDIMAGINGGL_API
369  UsdImagingGLRendererSettingsList GetRendererSettingsList() const;
370 
372  USDIMAGINGGL_API
373  VtValue GetRendererSetting(TfToken const& id) const;
374 
376  USDIMAGINGGL_API
377  void SetRendererSetting(TfToken const& id,
378  VtValue const& value);
379 
383  USDIMAGINGGL_API
384  void SetEnablePresentation(bool enabled);
385 
391  USDIMAGINGGL_API
392  void SetPresentationOutput(TfToken const &api, VtValue const &framebuffer);
393 
395 
396  // ---------------------------------------------------------------------
399  // ---------------------------------------------------------------------
400 
404  USDIMAGINGGL_API
405  HdCommandDescriptors GetRendererCommandDescriptors() const;
406 
412  USDIMAGINGGL_API
414  const TfToken &command,
415  const HdCommandArgs &args = HdCommandArgs()) const;
416 
417  // ---------------------------------------------------------------------
420  // ---------------------------------------------------------------------
421 
423  USDIMAGINGGL_API
424  bool IsPauseRendererSupported() const;
425 
429  USDIMAGINGGL_API
430  bool PauseRenderer();
431 
435  USDIMAGINGGL_API
436  bool ResumeRenderer();
437 
439  USDIMAGINGGL_API
440  bool IsStopRendererSupported() const;
441 
445  USDIMAGINGGL_API
446  bool StopRenderer();
447 
451  USDIMAGINGGL_API
452  bool RestartRenderer();
453 
455 
456  // ---------------------------------------------------------------------
459  // ---------------------------------------------------------------------
460 
475  USDIMAGINGGL_API
477  TfToken const& ccType,
478  TfToken const& ocioDisplay = {},
479  TfToken const& ocioView = {},
480  TfToken const& ocioColorSpace = {},
481  TfToken const& ocioLook = {});
482 
484 
486  USDIMAGINGGL_API
487  static bool IsColorCorrectionCapable();
488 
489  // ---------------------------------------------------------------------
492  // ---------------------------------------------------------------------
493 
499  USDIMAGINGGL_API
501 
503 
504  // ---------------------------------------------------------------------
507  // ---------------------------------------------------------------------
508 
511  USDIMAGINGGL_API
512  Hgi* GetHgi();
513 
515 
516 protected:
517 
519  friend class UsdImagingGL_UnitTestGLDrawing;
520 
523  USDIMAGINGGL_API
525 
526  USDIMAGINGGL_API
527  void _Execute(const UsdImagingGLRenderParams &params,
528  HdTaskSharedPtrVector tasks);
529 
530  USDIMAGINGGL_API
531  bool _CanPrepare(const UsdPrim& root);
532  USDIMAGINGGL_API
533  void _PreSetTime(const UsdImagingGLRenderParams& params);
534  USDIMAGINGGL_API
535  void _PostSetTime(const UsdImagingGLRenderParams& params);
536 
537  USDIMAGINGGL_API
538  void _PrepareRender(const UsdImagingGLRenderParams& params);
539 
540  // Create a hydra collection given root paths and render params.
541  // Returns true if the collection was updated.
542  USDIMAGINGGL_API
543  static bool _UpdateHydraCollection(HdRprimCollection *collection,
544  SdfPathVector const& roots,
545  UsdImagingGLRenderParams const& params);
546  USDIMAGINGGL_API
547  static HdxRenderTaskParams _MakeHydraUsdImagingGLRenderParams(
548  UsdImagingGLRenderParams const& params);
549  USDIMAGINGGL_API
550  static void _ComputeRenderTags(UsdImagingGLRenderParams const& params,
551  TfTokenVector *renderTags);
552 
553  USDIMAGINGGL_API
554  void _InitializeHgiIfNecessary();
555 
556  USDIMAGINGGL_API
557  void _SetRenderDelegateAndRestoreState(
559 
560  USDIMAGINGGL_API
561  void _SetRenderDelegate(HdPluginRenderDelegateUniqueHandle &&);
562 
563  USDIMAGINGGL_API
564  SdfPath _ComputeControllerPath(const HdPluginRenderDelegateUniqueHandle &);
565 
566  USDIMAGINGGL_API
567  static TfToken _GetDefaultRendererPluginId();
568 
574  USDIMAGINGGL_API
576 
577  USDIMAGINGGL_API
578  HdEngine *_GetHdEngine();
579 
580  USDIMAGINGGL_API
581  HdxTaskController *_GetTaskController() const;
582 
583  USDIMAGINGGL_API
584  bool _IsUsingLegacyImpl() const;
585 
586  USDIMAGINGGL_API
587  HdSelectionSharedPtr _GetSelection() const;
588 
589 protected:
590 
591  // Note that any of the fields below might become private
592  // in the future and subclasses should use the above getters
593  // to access them instead.
594 
595  HgiUniquePtr _hgi;
596  // Similar for HdDriver.
597  HdDriver _hgiDriver;
598 
599  VtValue _userFramebuffer;
600 
601 protected:
602  HdPluginRenderDelegateUniqueHandle _renderDelegate;
603  std::unique_ptr<HdRenderIndex> _renderIndex;
604 
605  SdfPath const _sceneDelegateId;
606 
607  std::unique_ptr<HdxTaskController> _taskController;
608 
609  HdxSelectionTrackerSharedPtr _selTracker;
610  HdRprimCollection _renderCollection;
611  HdRprimCollection _intersectCollection;
612 
613  GlfSimpleLightingContextRefPtr _lightingContextForOpenGLState;
614 
615  // Data we want to live across render plugin switches:
616  GfVec4f _selectionColor;
617 
618  SdfPath _rootPath;
619  SdfPathVector _excludedPrimPaths;
620  SdfPathVector _invisedPrimPaths;
621  bool _isPopulated;
622 
623  // An implementation of much of the engine functionality that doesn't
624  // invoke any of the advanced Hydra features. It is kept around for
625  // backwards compatibility, but it's deprecated and scheduled for deletion.
626  // When we use the legacy code, this pointer is non-null and most of the
627  // rest of this class isn't used; when we use hydra, this pointer is null.
628  std::unique_ptr<UsdImagingGLLegacyEngine> _legacyImpl;
629 
630 private:
631  void _DestroyHydraObjects();
632 
633  // Note that we'll only ever use one of _sceneIndex/_sceneDelegate
634  // at a time...
635  UsdImagingStageSceneIndexRefPtr _sceneIndex;
636  std::unique_ptr<UsdImagingDelegate> _sceneDelegate;
637 
638  std::unique_ptr<HdEngine> _engine;
639 };
640 
641 
642 PXR_NAMESPACE_CLOSE_SCOPE
643 
644 #endif // PXR_USD_IMAGING_USD_IMAGING_GL_ENGINE_H
static USDIMAGINGGL_API bool IsHydraEnabled()
Returns true if Hydra is enabled for GL drawing.
USDIMAGINGGL_API HdCommandDescriptors GetRendererCommandDescriptors() const
Return command deescriptors for commands supported by the active render delegate.
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:120
static USDIMAGINGGL_API bool IsColorCorrectionCapable()
Returns true if the platform is color correction capable.
A (movable) handle for a render delegate that was created using a a plugin.
USDIMAGINGGL_API void SetRendererSetting(TfToken const &id, VtValue const &value)
Sets a renderer setting's value.
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
Standard pointer typedefs.
USDIMAGINGGL_API void SetCameraPath(SdfPath const &id)
Scene camera API Set the scene camera path to use for rendering.
USDIMAGINGGL_API TfTokenVector GetRendererAovs() const
Return the vector of available renderer AOV settings.
USDIMAGINGGL_API UsdImagingDelegate * _GetSceneDelegate() const
Get a direct pointer to the scene delegate.
USDIMAGINGGL_API UsdImagingGLRendererSettingsList GetRendererSettingsList() const
Returns the list of renderer settings.
USDIMAGINGGL_API VtValue GetRendererSetting(TfToken const &id) const
Gets a renderer setting's current value.
A map with string keys and VtValue values.
Definition: dictionary.h:63
USDIMAGINGGL_API TfToken GetCurrentRendererId() const
Return the id of the currently used renderer plugin.
static USDIMAGINGGL_API TfTokenVector GetRendererPlugins()
Return the vector of available render-graph delegate plugins.
USDIMAGINGGL_API Hgi * GetHgi()
Returns the HGI interface.
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
USDIMAGINGGL_API bool RestartRenderer()
Restart the renderer.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
USDIMAGINGGL_API void SetPresentationOutput(TfToken const &api, VtValue const &framebuffer)
The destination API (e.g., OpenGL, see hgiInterop for details) and framebuffer that the AOVs are pres...
USDIMAGINGGL_API HdRenderIndex * _GetRenderIndex() const
Returns the render index of the engine, if any.
USDIMAGINGGL_API void SetFraming(CameraUtilFraming const &framing)
Determines how the filmback of the camera is mapped into the pixels of the render buffer and what pix...
USDIMAGINGGL_API bool StopRenderer()
Stop the renderer.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
USDIMAGINGGL_API void SetRenderViewport(GfVec4d const &viewport)
Set the viewport to use for rendering as (x,y,w,h), where (x,y) represents the lower left corner of t...
USDIMAGINGGL_API void SetWindowPolicy(CameraUtilConformWindowPolicy policy)
Set the window policy to use.
HdDriver represents a device object, commonly a render device, that is owned by the application and p...
Definition: driver.h:40
RenderTask parameters (renderpass state).
USDIMAGINGGL_API void SetRenderBufferSize(GfVec2i const &size)
Set the size of the render buffers baking the AOVs.
USDIMAGINGGL_API void SetSelected(SdfPathVector const &paths)
Sets (replaces) the list of prim paths that should be included in selection highlighting.
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
The application-facing entry point top-level entry point for accessing Hydra.
Definition: engine.h:48
USDIMAGINGGL_API bool IsConverged() const
Returns true if the resulting image is fully converged.
USDIMAGINGGL_API void SetCameraState(const GfMatrix4d &viewMatrix, const GfMatrix4d &projectionMatrix)
Free camera API Set camera framing state directly (without pointing to a camera on the USD stage).
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:132
USDIMAGINGGL_API HgiTextureHandle GetAovTexture(TfToken const &name) const
Returns an AOV texture handle for the given token.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A named, semantic collection of objects.
USDIMAGINGGL_API bool TestIntersection(const GfMatrix4d &viewMatrix, const GfMatrix4d &projectionMatrix, const UsdPrim &root, const UsdImagingGLRenderParams &params, GfVec3d *outHitPoint, GfVec3d *outHitNormal, SdfPath *outHitPrimPath=NULL, SdfPath *outHitInstancerPath=NULL, int *outHitInstanceIndex=NULL, HdInstancerContext *outInstancerContext=NULL)
Finds closest point of intersection with a frustum by rendering.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
USDIMAGINGGL_API VtDictionary GetRenderStats() const
Returns render statistics.
USDIMAGINGGL_API void PrepareBatch(const UsdPrim &root, const UsdImagingGLRenderParams &params)
Support for batched drawing.
USDIMAGINGGL_API bool ResumeRenderer()
Resume the renderer.
USDIMAGINGGL_API void SetOverrideWindowPolicy(const std::pair< bool, CameraUtilConformWindowPolicy > &policy)
Specifies whether to force a window policy when conforming the frustum of the camera to match the dis...
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
USDIMAGINGGL_API bool InvokeRendererCommand(const TfToken &command, const HdCommandArgs &args=HdCommandArgs()) const
Invokes command on the active render delegate.
USDIMAGINGGL_API UsdImagingGLEngine(const HdDriver &driver=HdDriver())
A HdDriver, containing the Hgi of your choice, can be optionally passed in during construction.
Hydra Graphics Interface.
Definition: hgi.h:108
Used as an arguments class for various methods in UsdImagingGLEngine.
Definition: renderParams.h:72
USDIMAGINGGL_API void AddSelected(SdfPath const &path, int instanceIndex)
Add a path with instanceIndex to the list of prim paths that should be included in selection highligh...
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
USDIMAGINGGL_API bool PauseRenderer()
Pause the renderer.
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
USDIMAGINGGL_API void SetSelectionColor(GfVec4f const &color)
Sets the selection highlighting color.
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
USDIMAGINGGL_API bool IsStopRendererSupported() const
Query the renderer as to whether it supports stopping and restarting.
USDIMAGINGGL_API void SetColorCorrectionSettings(TfToken const &ccType, TfToken const &ocioDisplay={}, TfToken const &ocioView={}, TfToken const &ocioColorSpace={}, TfToken const &ocioLook={})
Set ccType to one of the HdxColorCorrectionTokens: {disabled, sRGB, openColorIO}.
USDIMAGINGGL_API void ClearSelected()
Clear the list of prim paths that should be included in selection highlighting.
USDIMAGINGGL_API void SetRootVisibility(bool isVisible)
Sets the root visibility.
static USDIMAGINGGL_API std::string GetRendererDisplayName(TfToken const &id)
Return the user-friendly description of a renderer plugin.
USDIMAGINGGL_API void SetEnablePresentation(bool enabled)
Enable / disable presenting the render to bound framebuffer.
USDIMAGINGGL_API bool SetRendererAov(TfToken const &id)
Set the current renderer AOV to id.
The UsdImagingGLEngine is the main entry point API for rendering USD scenes.
Definition: engine.h:84
USDIMAGINGGL_API void SetLightingState(GlfSimpleLightingContextPtr const &src)
Copy lighting state from another lighting context.
USDIMAGINGGL_API void Render(const UsdPrim &root, const UsdImagingGLRenderParams &params)
Entry point for kicking off a render.
The primary translation layer between the Hydra (Hd) core and the Usd scene graph.
Definition: delegate.h:87
USDIMAGINGGL_API void SetRootTransform(GfMatrix4d const &xf)
Sets the root transform.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
USDIMAGINGGL_API bool IsPauseRendererSupported() const
Query the renderer as to whether it supports pausing and resuming.
USDIMAGINGGL_API bool DecodeIntersection(unsigned char const primIdColor[4], unsigned char const instanceIdColor[4], SdfPath *outHitPrimPath=NULL, SdfPath *outHitInstancerPath=NULL, int *outHitInstanceIndex=NULL, HdInstancerContext *outInstancerContext=NULL)
Decodes a pick result given hydra prim ID/instance ID (like you'd get from an ID render).
USDIMAGINGGL_API bool SetRendererPlugin(TfToken const &id)
Set the current render-graph delegate to id.
USDIMAGINGGL_API HdRenderBuffer * GetAovRenderBuffer(TfToken const &name) const
Returns the AOV render buffer for the given token.
Framing information.
Definition: framing.h:79