All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
76 TF_DECLARE_WEAK_AND_REF_PTRS(GlfSimpleLightingContext);
77 
83 {
84 public:
85 
86  // ---------------------------------------------------------------------
89  // ---------------------------------------------------------------------
90 
92  USDIMAGINGGL_API
93  static bool IsHydraEnabled();
94 
96 
97  // ---------------------------------------------------------------------
100  // ---------------------------------------------------------------------
101 
105  USDIMAGINGGL_API
106  UsdImagingGLEngine(const HdDriver& driver = HdDriver());
107 
108  USDIMAGINGGL_API
109  UsdImagingGLEngine(const SdfPath& rootPath,
110  const SdfPathVector& excludedPaths,
111  const SdfPathVector& invisedPaths=SdfPathVector(),
112  const SdfPath& sceneDelegateID =
114  const HdDriver& driver = HdDriver());
115 
116  // Disallow copies
117  UsdImagingGLEngine(const UsdImagingGLEngine&) = delete;
118  UsdImagingGLEngine& operator=(const UsdImagingGLEngine&) = delete;
119 
120  USDIMAGINGGL_API
121  ~UsdImagingGLEngine();
122 
124 
125  // ---------------------------------------------------------------------
128  // ---------------------------------------------------------------------
129 
131  USDIMAGINGGL_API
132  void PrepareBatch(const UsdPrim& root,
133  const UsdImagingGLRenderParams& params);
134  USDIMAGINGGL_API
135  void RenderBatch(const SdfPathVector& paths,
136  const UsdImagingGLRenderParams& params);
137 
139  USDIMAGINGGL_API
140  void Render(const UsdPrim& root,
141  const UsdImagingGLRenderParams &params);
142 
145  USDIMAGINGGL_API
146  bool IsConverged() const;
147 
149 
150  // ---------------------------------------------------------------------
153  // ---------------------------------------------------------------------
154 
156  USDIMAGINGGL_API
157  void SetRootTransform(GfMatrix4d const& xf);
158 
160  USDIMAGINGGL_API
161  void SetRootVisibility(bool isVisible);
162 
164 
165  // ---------------------------------------------------------------------
168  // ---------------------------------------------------------------------
169 
172  USDIMAGINGGL_API
173  void SetCameraPath(SdfPath const& id);
174 
178  USDIMAGINGGL_API
179  void SetFraming(CameraUtilFraming const& framing);
180 
190  USDIMAGINGGL_API
192  const std::pair<bool, CameraUtilConformWindowPolicy> &policy);
193 
197  USDIMAGINGGL_API
198  void SetRenderBufferSize(GfVec2i const& size);
199 
205  USDIMAGINGGL_API
206  void SetRenderViewport(GfVec4d const& viewport);
207 
211  USDIMAGINGGL_API
212  void SetWindowPolicy(CameraUtilConformWindowPolicy policy);
213 
218  USDIMAGINGGL_API
219  void SetCameraState(const GfMatrix4d& viewMatrix,
220  const GfMatrix4d& projectionMatrix);
221 
224  USDIMAGINGGL_API
226 
228 
229  // ---------------------------------------------------------------------
232  // ---------------------------------------------------------------------
233 
236  USDIMAGINGGL_API
238 
240  USDIMAGINGGL_API
241  void SetLightingState(GlfSimpleLightingContextPtr const &src);
242 
247  USDIMAGINGGL_API
248  void SetLightingState(GlfSimpleLightVector const &lights,
249  GlfSimpleMaterial const &material,
250  GfVec4f const &sceneAmbient);
251 
253 
254  // ---------------------------------------------------------------------
257  // ---------------------------------------------------------------------
258 
262  USDIMAGINGGL_API
263  void SetSelected(SdfPathVector const& paths);
264 
267  USDIMAGINGGL_API
268  void ClearSelected();
269 
273  USDIMAGINGGL_API
274  void AddSelected(SdfPath const &path, int instanceIndex);
275 
277  USDIMAGINGGL_API
278  void SetSelectionColor(GfVec4f const& color);
279 
281 
282  // ---------------------------------------------------------------------
285  // ---------------------------------------------------------------------
286 
304  USDIMAGINGGL_API
305  bool TestIntersection(
306  const GfMatrix4d &viewMatrix,
307  const GfMatrix4d &projectionMatrix,
308  const UsdPrim& root,
309  const UsdImagingGLRenderParams &params,
310  GfVec3d *outHitPoint,
311  GfVec3d *outHitNormal,
312  SdfPath *outHitPrimPath = NULL,
313  SdfPath *outHitInstancerPath = NULL,
314  int *outHitInstanceIndex = NULL,
315  HdInstancerContext *outInstancerContext = NULL);
316 
319  USDIMAGINGGL_API
320  bool DecodeIntersection(
321  unsigned char const primIdColor[4],
322  unsigned char const instanceIdColor[4],
323  SdfPath *outHitPrimPath = NULL,
324  SdfPath *outHitInstancerPath = NULL,
325  int *outHitInstanceIndex = NULL,
326  HdInstancerContext *outInstancerContext = NULL);
327 
329 
330  // ---------------------------------------------------------------------
333  // ---------------------------------------------------------------------
334 
336  USDIMAGINGGL_API
338 
340  USDIMAGINGGL_API
341  static std::string GetRendererDisplayName(TfToken const &id);
342 
344  USDIMAGINGGL_API
346 
349  USDIMAGINGGL_API
350  bool SetRendererPlugin(TfToken const &id);
351 
353 
354  // ---------------------------------------------------------------------
357  // ---------------------------------------------------------------------
358 
360  USDIMAGINGGL_API
362 
364  USDIMAGINGGL_API
365  bool SetRendererAov(TfToken const& id);
366 
368  USDIMAGINGGL_API
369  HgiTextureHandle GetAovTexture(TfToken const& name) const;
370 
372  USDIMAGINGGL_API
373  UsdImagingGLRendererSettingsList GetRendererSettingsList() const;
374 
376  USDIMAGINGGL_API
377  VtValue GetRendererSetting(TfToken const& id) const;
378 
380  USDIMAGINGGL_API
381  void SetRendererSetting(TfToken const& id,
382  VtValue const& value);
383 
387  USDIMAGINGGL_API
388  void SetEnablePresentation(bool enabled);
389 
395  USDIMAGINGGL_API
396  void SetPresentationOutput(TfToken const &api, VtValue const &framebuffer);
397 
399 
400  // ---------------------------------------------------------------------
403  // ---------------------------------------------------------------------
404 
408  USDIMAGINGGL_API
409  HdCommandDescriptors GetRendererCommandDescriptors() const;
410 
416  USDIMAGINGGL_API
418  const TfToken &command,
419  const HdCommandArgs &args = HdCommandArgs()) const;
420 
421  // ---------------------------------------------------------------------
424  // ---------------------------------------------------------------------
425 
427  USDIMAGINGGL_API
428  bool IsPauseRendererSupported() const;
429 
433  USDIMAGINGGL_API
434  bool PauseRenderer();
435 
439  USDIMAGINGGL_API
440  bool ResumeRenderer();
441 
443  USDIMAGINGGL_API
444  bool IsStopRendererSupported() const;
445 
449  USDIMAGINGGL_API
450  bool StopRenderer();
451 
455  USDIMAGINGGL_API
456  bool RestartRenderer();
457 
459 
460  // ---------------------------------------------------------------------
463  // ---------------------------------------------------------------------
464 
466  USDIMAGINGGL_API
468  TfToken const& id);
469 
471 
473  USDIMAGINGGL_API
474  static bool IsColorCorrectionCapable();
475 
476  // ---------------------------------------------------------------------
479  // ---------------------------------------------------------------------
480 
486  USDIMAGINGGL_API
488 
490 
491  // ---------------------------------------------------------------------
494  // ---------------------------------------------------------------------
495 
498  USDIMAGINGGL_API
499  Hgi* GetHgi();
500 
502 
503 protected:
504 
506  friend class UsdImagingGL_UnitTestGLDrawing;
507 
510  USDIMAGINGGL_API
512 
513  USDIMAGINGGL_API
514  void _Execute(const UsdImagingGLRenderParams &params,
515  HdTaskSharedPtrVector tasks);
516 
517  USDIMAGINGGL_API
518  bool _CanPrepare(const UsdPrim& root);
519  USDIMAGINGGL_API
520  void _PreSetTime(const UsdImagingGLRenderParams& params);
521  USDIMAGINGGL_API
522  void _PostSetTime(const UsdImagingGLRenderParams& params);
523 
524  USDIMAGINGGL_API
525  void _PrepareRender(const UsdImagingGLRenderParams& params);
526 
527  // Create a hydra collection given root paths and render params.
528  // Returns true if the collection was updated.
529  USDIMAGINGGL_API
530  static bool _UpdateHydraCollection(HdRprimCollection *collection,
531  SdfPathVector const& roots,
532  UsdImagingGLRenderParams const& params);
533  USDIMAGINGGL_API
534  static HdxRenderTaskParams _MakeHydraUsdImagingGLRenderParams(
535  UsdImagingGLRenderParams const& params);
536  USDIMAGINGGL_API
537  static void _ComputeRenderTags(UsdImagingGLRenderParams const& params,
538  TfTokenVector *renderTags);
539 
540  USDIMAGINGGL_API
541  void _InitializeHgiIfNecessary();
542 
543  USDIMAGINGGL_API
544  void _SetRenderDelegateAndRestoreState(
546 
547  USDIMAGINGGL_API
548  void _SetRenderDelegate(HdPluginRenderDelegateUniqueHandle &&);
549 
550  USDIMAGINGGL_API
551  SdfPath _ComputeControllerPath(const HdPluginRenderDelegateUniqueHandle &);
552 
553  USDIMAGINGGL_API
554  static TfToken _GetDefaultRendererPluginId();
555 
556  USDIMAGINGGL_API
557  UsdImagingDelegate *_GetSceneDelegate() const;
558 
559  USDIMAGINGGL_API
560  HdEngine *_GetHdEngine();
561 
562  USDIMAGINGGL_API
563  HdxTaskController *_GetTaskController() const;
564 
565  USDIMAGINGGL_API
566  bool _IsUsingLegacyImpl() const;
567 
568  USDIMAGINGGL_API
569  HdSelectionSharedPtr _GetSelection() const;
570 
571 protected:
572 
573 // private:
574  // Note that any of the fields below might become private
575  // in the future and subclasses should use the above getters
576  // to access them instead.
577 
578  HgiUniquePtr _hgi;
579  // Similar for HdDriver.
580  HdDriver _hgiDriver;
581 
582  VtValue _userFramebuffer;
583 
584 protected:
585  HdPluginRenderDelegateUniqueHandle _renderDelegate;
586  std::unique_ptr<HdRenderIndex> _renderIndex;
587 
588  SdfPath const _sceneDelegateId;
589 
590  std::unique_ptr<HdxTaskController> _taskController;
591 
592  HdxSelectionTrackerSharedPtr _selTracker;
593  HdRprimCollection _renderCollection;
594  HdRprimCollection _intersectCollection;
595 
596  GlfSimpleLightingContextRefPtr _lightingContextForOpenGLState;
597 
598  // Data we want to live across render plugin switches:
599  GfVec4f _selectionColor;
600 
601  SdfPath _rootPath;
602  SdfPathVector _excludedPrimPaths;
603  SdfPathVector _invisedPrimPaths;
604  bool _isPopulated;
605 
606  // An implementation of much of the engine functionality that doesn't
607  // invoke any of the advanced Hydra features. It is kept around for
608  // backwards compatibility and may one day be deprecated. Most of the
609  // time we expect this to be null. When it is not null, none of the other
610  // member variables of this class are used.
611  std::unique_ptr<UsdImagingGLLegacyEngine> _legacyImpl;
612 
613 private:
614  void _DestroyHydraObjects();
615 
616  std::unique_ptr<UsdImagingDelegate> _sceneDelegate;
617  std::unique_ptr<HdEngine> _engine;
618 };
619 
620 
621 PXR_NAMESPACE_CLOSE_SCOPE
622 
623 #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 void SetColorCorrectionSettings(TfToken const &id)
Set id to one of the HdxColorCorrectionTokens.
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:116
USDIMAGINGGL_API HgiTextureHandle GetAovTexture(TfToken const &name) const
Returns an AOV texture handle for the given token.
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&#39;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 bool IsPauseRendererSupported() const
Query the renderer as to whether it supports pausing and resuming.
USDIMAGINGGL_API HdRenderIndex * _GetRenderIndex() const
Returns the render index of the engine, if any.
A map with string keys and VtValue values.
Definition: dictionary.h:63
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 VtDictionary GetRenderStats() const
Returns render statistics.
USDIMAGINGGL_API UsdImagingGLRendererSettingsList GetRendererSettingsList() const
Returns the list of renderer settings.
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 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)...
USDIMAGINGGL_API HdCommandDescriptors GetRendererCommandDescriptors() const
Return command deescriptors for commands supported by the active render delegate. ...
USDIMAGINGGL_API bool IsConverged() const
Returns true if the resulting image is fully converged.
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:132
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:288
USDIMAGINGGL_API void SetLightingStateFromOpenGL()
Helper function to extract lighting state from opengl and then call SetLights.
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 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:106
USDIMAGINGGL_API void SetCameraStateFromOpenGL()
Helper function to extract camera and viewport state from opengl and then call SetCameraState and Set...
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...
USDIMAGINGGL_API TfTokenVector GetRendererAovs() const
Return the vector of available renderer AOV settings.
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
USDIMAGINGGL_API bool PauseRenderer()
Pause the renderer.
USDIMAGINGGL_API TfToken GetCurrentRendererId() const
Return the id of the currently used renderer plugin.
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.
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:82
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.
USDIMAGINGGL_API bool InvokeRendererCommand(const TfToken &command, const HdCommandArgs &args=HdCommandArgs()) const
Invokes command on the active render delegate.
USDIMAGINGGL_API bool IsStopRendererSupported() const
Query the renderer as to whether it supports stopping and restarting.
The primary translation layer between the Hydra (Hd) core and the Usd scene graph.
Definition: delegate.h:87
USDIMAGINGGL_API VtValue GetRendererSetting(TfToken const &id) const
Gets a renderer setting&#39;s current value.
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:168
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&#39;d get from an ID render).
USDIMAGINGGL_API bool SetRendererPlugin(TfToken const &id)
Set the current render-graph delegate to id.
Framing information.
Definition: framing.h:79