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 
35 #include "pxr/usdImaging/usdImagingGL/renderParams.h"
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 #include "pxr/imaging/hdx/pickTask.h"
48 
49 #include "pxr/imaging/hgi/hgi.h"
50 
51 #include "pxr/imaging/glf/drawTarget.h"
52 #include "pxr/imaging/glf/simpleLight.h"
53 #include "pxr/imaging/glf/simpleMaterial.h"
54 
55 #include "pxr/imaging/hgi/hgi.h"
56 
57 #include "pxr/usd/sdf/path.h"
58 #include "pxr/usd/usd/timeCode.h"
59 
60 #include "pxr/base/gf/frustum.h"
61 #include "pxr/base/gf/matrix4d.h"
62 #include "pxr/base/gf/vec4d.h"
63 #include "pxr/base/gf/vec4f.h"
64 #include "pxr/base/gf/vec4i.h"
65 
66 #include "pxr/base/vt/dictionary.h"
67 
68 #include "pxr/base/tf/declarePtrs.h"
69 
70 PXR_NAMESPACE_OPEN_SCOPE
71 
72 class UsdPrim;
73 class HdRenderIndex;
74 class HdxTaskController;
75 class UsdImagingDelegate;
76 class UsdImagingGLLegacyEngine;
77 
78 TF_DECLARE_WEAK_AND_REF_PTRS(GlfSimpleLightingContext);
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
123  ~UsdImagingGLEngine();
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 
145  USDIMAGINGGL_API
146  void InvalidateBuffers();
147 
150  USDIMAGINGGL_API
151  bool IsConverged() const;
152 
154 
155  // ---------------------------------------------------------------------
158  // ---------------------------------------------------------------------
159 
161  USDIMAGINGGL_API
162  void SetRootTransform(GfMatrix4d const& xf);
163 
165  USDIMAGINGGL_API
166  void SetRootVisibility(bool isVisible);
167 
169 
170  // ---------------------------------------------------------------------
173  // ---------------------------------------------------------------------
174 
178  USDIMAGINGGL_API
179  void SetRenderViewport(GfVec4d const& viewport);
180 
184  USDIMAGINGGL_API
185  void SetWindowPolicy(CameraUtilConformWindowPolicy policy);
186 
189  USDIMAGINGGL_API
190  void SetCameraPath(SdfPath const& id);
191 
196  USDIMAGINGGL_API
197  void SetCameraState(const GfMatrix4d& viewMatrix,
198  const GfMatrix4d& projectionMatrix);
199 
202  USDIMAGINGGL_API
204 
206 
207  // ---------------------------------------------------------------------
210  // ---------------------------------------------------------------------
211 
214  USDIMAGINGGL_API
216 
218  USDIMAGINGGL_API
219  void SetLightingState(GlfSimpleLightingContextPtr const &src);
220 
225  USDIMAGINGGL_API
226  void SetLightingState(GlfSimpleLightVector const &lights,
227  GlfSimpleMaterial const &material,
228  GfVec4f const &sceneAmbient);
229 
231 
232  // ---------------------------------------------------------------------
235  // ---------------------------------------------------------------------
236 
240  USDIMAGINGGL_API
241  void SetSelected(SdfPathVector const& paths);
242 
245  USDIMAGINGGL_API
246  void ClearSelected();
247 
251  USDIMAGINGGL_API
252  void AddSelected(SdfPath const &path, int instanceIndex);
253 
255  USDIMAGINGGL_API
256  void SetSelectionColor(GfVec4f const& color);
257 
259 
260  // ---------------------------------------------------------------------
263  // ---------------------------------------------------------------------
264 
281  USDIMAGINGGL_API
282  bool TestIntersection(
283  const GfMatrix4d &viewMatrix,
284  const GfMatrix4d &projectionMatrix,
285  const UsdPrim& root,
286  const UsdImagingGLRenderParams &params,
287  GfVec3d *outHitPoint,
288  SdfPath *outHitPrimPath = NULL,
289  SdfPath *outHitInstancerPath = NULL,
290  int *outHitInstanceIndex = NULL,
291  HdInstancerContext *outInstancerContext = NULL);
292 
295  USDIMAGINGGL_API
296  bool DecodeIntersection(
297  unsigned char const primIdColor[4],
298  unsigned char const instanceIdColor[4],
299  SdfPath *outHitPrimPath = NULL,
300  SdfPath *outHitInstancerPath = NULL,
301  int *outHitInstanceIndex = NULL,
302  HdInstancerContext *outInstancerContext = NULL);
303 
305 
306  // ---------------------------------------------------------------------
309  // ---------------------------------------------------------------------
310 
312  USDIMAGINGGL_API
314 
316  USDIMAGINGGL_API
317  static std::string GetRendererDisplayName(TfToken const &id);
318 
320  USDIMAGINGGL_API
322 
325  USDIMAGINGGL_API
326  bool SetRendererPlugin(TfToken const &id);
327 
329 
330  // ---------------------------------------------------------------------
333  // ---------------------------------------------------------------------
334 
336  USDIMAGINGGL_API
338 
340  USDIMAGINGGL_API
341  bool SetRendererAov(TfToken const& id);
342 
344  USDIMAGINGGL_API
345  HgiTextureHandle GetAovTexture(TfToken const& name) const;
346 
348  USDIMAGINGGL_API
349  UsdImagingGLRendererSettingsList GetRendererSettingsList() const;
350 
352  USDIMAGINGGL_API
353  VtValue GetRendererSetting(TfToken const& id) const;
354 
356  USDIMAGINGGL_API
357  void SetRendererSetting(TfToken const& id,
358  VtValue const& value);
359 
361 
362  // ---------------------------------------------------------------------
365  // ---------------------------------------------------------------------
366 
368  USDIMAGINGGL_API
369  bool IsPauseRendererSupported() const;
370 
374  USDIMAGINGGL_API
375  bool PauseRenderer();
376 
380  USDIMAGINGGL_API
381  bool ResumeRenderer();
382 
384  USDIMAGINGGL_API
385  bool IsStopRendererSupported() const;
386 
390  USDIMAGINGGL_API
391  bool StopRenderer();
392 
396  USDIMAGINGGL_API
397  bool RestartRenderer();
398 
400 
401  // ---------------------------------------------------------------------
404  // ---------------------------------------------------------------------
405 
407  USDIMAGINGGL_API
409  TfToken const& id);
410 
412 
414  USDIMAGINGGL_API
415  static bool IsColorCorrectionCapable();
416 
417  // ---------------------------------------------------------------------
420  // ---------------------------------------------------------------------
421 
427  USDIMAGINGGL_API
429 
431 
432  // ---------------------------------------------------------------------
435  // ---------------------------------------------------------------------
436 
439  USDIMAGINGGL_API
440  Hgi* GetHgi();
441 
443 
444 protected:
445 
447  friend class UsdImagingGL_UnitTestGLDrawing;
448 
451  USDIMAGINGGL_API
453 
454  USDIMAGINGGL_API
455  void _Execute(const UsdImagingGLRenderParams &params,
456  HdTaskSharedPtrVector tasks);
457 
458  // These functions factor batch preparation into separate steps so they
459  // can be reused by both the vectorized and non-vectorized API.
460  USDIMAGINGGL_API
461  bool _CanPrepareBatch(const UsdPrim& root,
462  const UsdImagingGLRenderParams& params);
463  USDIMAGINGGL_API
464  void _PreSetTime(const UsdPrim& root,
465  const UsdImagingGLRenderParams& params);
466  USDIMAGINGGL_API
467  void _PostSetTime(const UsdPrim& root,
468  const UsdImagingGLRenderParams& params);
469 
470  // Create a hydra collection given root paths and render params.
471  // Returns true if the collection was updated.
472  USDIMAGINGGL_API
473  static bool _UpdateHydraCollection(HdRprimCollection *collection,
474  SdfPathVector const& roots,
475  UsdImagingGLRenderParams const& params);
476  USDIMAGINGGL_API
477  static HdxRenderTaskParams _MakeHydraUsdImagingGLRenderParams(
478  UsdImagingGLRenderParams const& params);
479  USDIMAGINGGL_API
480  static void _ComputeRenderTags(UsdImagingGLRenderParams const& params,
481  TfTokenVector *renderTags);
482 
483  USDIMAGINGGL_API
484  void _InitializeHgiIfNecessary();
485 
486  USDIMAGINGGL_API
487  void _SetRenderDelegateAndRestoreState(
489 
490  USDIMAGINGGL_API
491  void _SetRenderDelegate(HdPluginRenderDelegateUniqueHandle &&);
492 
493  USDIMAGINGGL_API
494  SdfPath _ComputeControllerPath(const HdPluginRenderDelegateUniqueHandle &);
495 
496  USDIMAGINGGL_API
497  static TfToken _GetDefaultRendererPluginId();
498 
499  USDIMAGINGGL_API
500  UsdImagingDelegate *_GetSceneDelegate() const;
501 
502  USDIMAGINGGL_API
503  HdEngine *_GetHdEngine();
504 
505  USDIMAGINGGL_API
506  HdxTaskController *_GetTaskController() const;
507 
508  USDIMAGINGGL_API
509  bool _IsUsingLegacyImpl() const;
510 
511  USDIMAGINGGL_API
512  HdSelectionSharedPtr _GetSelection() const;
513 
514 protected:
515 
516 // private:
517  // Note that any of the fields below might become private
518  // in the future and subclasses should use the above getters
519  // to access them instead.
520 
521  HgiUniquePtr _hgi;
522  // Similar for HdDriver.
523  HdDriver _hgiDriver;
524 
525 protected:
526  HdPluginRenderDelegateUniqueHandle _renderDelegate;
527  std::unique_ptr<HdRenderIndex> _renderIndex;
528 
529  SdfPath const _sceneDelegateId;
530 
531  std::unique_ptr<HdxTaskController> _taskController;
532 
533  HdxSelectionTrackerSharedPtr _selTracker;
534  HdRprimCollection _renderCollection;
535  HdRprimCollection _intersectCollection;
536 
537  GlfSimpleLightingContextRefPtr _lightingContextForOpenGLState;
538 
539  // Data we want to live across render plugin switches:
540  GfVec4f _selectionColor;
541 
542  SdfPath _rootPath;
543  SdfPathVector _excludedPrimPaths;
544  SdfPathVector _invisedPrimPaths;
545  bool _isPopulated;
546 
547  // An implementation of much of the engine functionality that doesn't
548  // invoke any of the advanced Hydra features. It is kept around for
549  // backwards compatibility and may one day be deprecated. Most of the
550  // time we expect this to be null. When it is not null, none of the other
551  // member variables of this class are used.
552  std::unique_ptr<UsdImagingGLLegacyEngine> _legacyImpl;
553 
554 private:
555  void _DestroyHydraObjects();
556 
557  std::unique_ptr<UsdImagingDelegate> _sceneDelegate;
558  std::unique_ptr<HdEngine> _engine;
559 };
560 
561 
562 PXR_NAMESPACE_CLOSE_SCOPE
563 
564 #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:117
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.
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 VtDictionary GetRenderStats() const
Returns render statistics.
USDIMAGINGGL_API UsdImagingGLRendererSettingsList GetRendererSettingsList() const
Returns the list of renderer settings.
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 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 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:446
A named, semantic collection of objects.
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.
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 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.
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:88
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.
USDIMAGINGGL_API bool TestIntersection(const GfMatrix4d &viewMatrix, const GfMatrix4d &projectionMatrix, const UsdPrim &root, const UsdImagingGLRenderParams &params, GfVec3d *outHitPoint, SdfPath *outHitPrimPath=NULL, SdfPath *outHitInstancerPath=NULL, int *outHitInstanceIndex=NULL, HdInstancerContext *outInstancerContext=NULL)
Finds closest point of intersection with a frustum by rendering.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:174
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.