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 USDIMAGINGGL_ENGINE_H
28 #define USDIMAGINGGL_ENGINE_H
29 
30 #include "pxr/pxr.h"
31 #include "pxr/usdImaging/usdImagingGL/api.h"
32 #include "pxr/usdImaging/usdImaging/version.h"
33 
34 #include "pxr/usdImaging/usdImagingGL/renderParams.h"
35 #include "pxr/usdImaging/usdImagingGL/rendererSettings.h"
36 
37 #include "pxr/imaging/cameraUtil/conformWindow.h"
38 
39 #include "pxr/imaging/hd/engine.h"
40 #include "pxr/imaging/hd/rprimCollection.h"
41 
42 #include "pxr/imaging/hdx/compositor.h"
43 #include "pxr/imaging/hdx/selectionTracker.h"
44 #include "pxr/imaging/hdx/renderSetupTask.h"
45 #include "pxr/imaging/hdx/pickTask.h"
46 
47 #include "pxr/imaging/glf/drawTarget.h"
48 #include "pxr/imaging/glf/simpleLight.h"
49 #include "pxr/imaging/glf/simpleMaterial.h"
50 
51 #include "pxr/usd/sdf/path.h"
52 #include "pxr/usd/usd/timeCode.h"
53 
54 #include "pxr/base/gf/frustum.h"
55 #include "pxr/base/gf/matrix4d.h"
56 #include "pxr/base/gf/vec4d.h"
57 #include "pxr/base/gf/vec4f.h"
58 #include "pxr/base/gf/vec4i.h"
59 
60 #include "pxr/base/vt/dictionary.h"
61 
62 #include "pxr/base/tf/declarePtrs.h"
63 
64 PXR_NAMESPACE_OPEN_SCOPE
65 
66 class UsdPrim;
67 class HdRenderIndex;
68 class HdRendererPlugin;
69 class HdxTaskController;
70 class UsdImagingDelegate;
71 class UsdImagingGLLegacyEngine;
72 
73 typedef boost::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
75 TF_DECLARE_WEAK_AND_REF_PTRS(GlfSimpleLightingContext);
76 
82 {
83 public:
84 
85  // ---------------------------------------------------------------------
88  // ---------------------------------------------------------------------
89 
91  USDIMAGINGGL_API
92  static bool IsHydraEnabled();
93 
95 
96  // ---------------------------------------------------------------------
99  // ---------------------------------------------------------------------
100  USDIMAGINGGL_API
102 
103  USDIMAGINGGL_API
104  UsdImagingGLEngine(const SdfPath& rootPath,
105  const SdfPathVector& excludedPaths,
106  const SdfPathVector& invisedPaths=SdfPathVector(),
107  const SdfPath& delegateID = SdfPath::AbsoluteRootPath());
108 
109  // Disallow copies
110  UsdImagingGLEngine(const UsdImagingGLEngine&) = delete;
111  UsdImagingGLEngine& operator=(const UsdImagingGLEngine&) = delete;
112 
113  USDIMAGINGGL_API
114  ~UsdImagingGLEngine();
115 
117 
118  // ---------------------------------------------------------------------
121  // ---------------------------------------------------------------------
122 
124  USDIMAGINGGL_API
125  void PrepareBatch(const UsdPrim& root,
126  const UsdImagingGLRenderParams& params);
127  USDIMAGINGGL_API
128  void RenderBatch(const SdfPathVector& paths,
129  const UsdImagingGLRenderParams& params);
130 
132  USDIMAGINGGL_API
133  void Render(const UsdPrim& root,
134  const UsdImagingGLRenderParams &params);
135 
136  USDIMAGINGGL_API
137  void InvalidateBuffers();
138 
141  USDIMAGINGGL_API
142  bool IsConverged() const;
143 
145 
146  // ---------------------------------------------------------------------
149  // ---------------------------------------------------------------------
150 
152  USDIMAGINGGL_API
153  void SetRootTransform(GfMatrix4d const& xf);
154 
156  USDIMAGINGGL_API
157  void SetRootVisibility(bool isVisible);
158 
160 
161  // ---------------------------------------------------------------------
164  // ---------------------------------------------------------------------
165 
169  USDIMAGINGGL_API
170  void SetRenderViewport(GfVec4d const& viewport);
171 
175  USDIMAGINGGL_API
176  void SetWindowPolicy(CameraUtilConformWindowPolicy policy);
177 
180  USDIMAGINGGL_API
181  void SetCameraPath(SdfPath const& id);
182 
187  USDIMAGINGGL_API
188  void SetCameraState(const GfMatrix4d& viewMatrix,
189  const GfMatrix4d& projectionMatrix);
190 
193  USDIMAGINGGL_API
195 
197 
198  // ---------------------------------------------------------------------
201  // ---------------------------------------------------------------------
202 
205  USDIMAGINGGL_API
207 
209  USDIMAGINGGL_API
210  void SetLightingState(GlfSimpleLightingContextPtr const &src);
211 
216  USDIMAGINGGL_API
217  void SetLightingState(GlfSimpleLightVector const &lights,
218  GlfSimpleMaterial const &material,
219  GfVec4f const &sceneAmbient);
220 
222 
223  // ---------------------------------------------------------------------
226  // ---------------------------------------------------------------------
227 
231  USDIMAGINGGL_API
232  void SetSelected(SdfPathVector const& paths);
233 
236  USDIMAGINGGL_API
237  void ClearSelected();
238 
242  USDIMAGINGGL_API
243  void AddSelected(SdfPath const &path, int instanceIndex);
244 
246  USDIMAGINGGL_API
247  void SetSelectionColor(GfVec4f const& color);
248 
250 
251  // ---------------------------------------------------------------------
254  // ---------------------------------------------------------------------
255 
267  USDIMAGINGGL_API
268  bool TestIntersection(
269  const GfMatrix4d &viewMatrix,
270  const GfMatrix4d &projectionMatrix,
271  const GfMatrix4d &worldToLocalSpace,
272  const UsdPrim& root,
273  const UsdImagingGLRenderParams& params,
274  GfVec3d *outHitPoint,
275  SdfPath *outHitPrimPath = NULL,
276  SdfPath *outHitInstancerPath = NULL,
277  int *outHitInstanceIndex = NULL,
278  int *outHitElementIndex = NULL);
279 
288  USDIMAGINGGL_API
289  SdfPath GetRprimPathFromPrimId(int primId) const;
290 
301  USDIMAGINGGL_API
303  GfVec4i const & primIdColor,
304  GfVec4i const & instanceIdColor,
305  int * instanceIndexOut = NULL);
306 
327  USDIMAGINGGL_API
329  const SdfPath &protoRprimId,
330  int protoIndex,
331  int *instancerIndex=NULL,
332  SdfPath *masterCachePath=NULL,
333  SdfPathVector *instanceContext=NULL);
334 
336  static inline int DecodeIDRenderColor(unsigned char const idColor[4]) {
337  return HdxPickTask::DecodeIDRenderColor(idColor);
338  }
339 
341 
342  // ---------------------------------------------------------------------
345  // ---------------------------------------------------------------------
346 
348  USDIMAGINGGL_API
350 
352  USDIMAGINGGL_API
353  static std::string GetRendererDisplayName(TfToken const &id);
354 
356  USDIMAGINGGL_API
358 
361  USDIMAGINGGL_API
362  bool SetRendererPlugin(TfToken const &id);
363 
365 
366  // ---------------------------------------------------------------------
369  // ---------------------------------------------------------------------
370 
372  USDIMAGINGGL_API
374 
376  USDIMAGINGGL_API
377  bool SetRendererAov(TfToken const& id);
378 
380  USDIMAGINGGL_API
381  UsdImagingGLRendererSettingsList GetRendererSettingsList() const;
382 
384  USDIMAGINGGL_API
385  VtValue GetRendererSetting(TfToken const& id) const;
386 
388  USDIMAGINGGL_API
389  void SetRendererSetting(TfToken const& id,
390  VtValue const& value);
391 
393 
394  // ---------------------------------------------------------------------
397  // ---------------------------------------------------------------------
398 
400  USDIMAGINGGL_API
401  bool IsPauseRendererSupported() const;
402 
406  USDIMAGINGGL_API
407  bool PauseRenderer();
408 
412  USDIMAGINGGL_API
413  bool ResumeRenderer();
414 
416 
417  // ---------------------------------------------------------------------
420  // ---------------------------------------------------------------------
421 
426  USDIMAGINGGL_API
428  TfToken const& id,
429  GfVec2i const& framebufferResolution);
430 
432 
434  USDIMAGINGGL_API
435  static bool IsColorCorrectionCapable();
436 
437  // ---------------------------------------------------------------------
440  // ---------------------------------------------------------------------
441 
447  USDIMAGINGGL_API
449 
451 
452 
453 protected:
454 
456  friend class UsdImagingGL_UnitTestGLDrawing;
457 
460  USDIMAGINGGL_API
462 
463  USDIMAGINGGL_API
464  void _Execute(const UsdImagingGLRenderParams &params,
465  HdTaskSharedPtrVector tasks);
466 
467  // These functions factor batch preparation into separate steps so they
468  // can be reused by both the vectorized and non-vectorized API.
469  USDIMAGINGGL_API
470  bool _CanPrepareBatch(const UsdPrim& root,
471  const UsdImagingGLRenderParams& params);
472  USDIMAGINGGL_API
473  void _PreSetTime(const UsdPrim& root,
474  const UsdImagingGLRenderParams& params);
475  USDIMAGINGGL_API
476  void _PostSetTime(const UsdPrim& root,
477  const UsdImagingGLRenderParams& params);
478 
479  // Create a hydra collection given root paths and render params.
480  // Returns true if the collection was updated.
481  USDIMAGINGGL_API
482  static bool _UpdateHydraCollection(HdRprimCollection *collection,
483  SdfPathVector const& roots,
484  UsdImagingGLRenderParams const& params);
485  USDIMAGINGGL_API
486  static HdxRenderTaskParams _MakeHydraUsdImagingGLRenderParams(
487  UsdImagingGLRenderParams const& params);
488  USDIMAGINGGL_API
489  static void _ComputeRenderTags(UsdImagingGLRenderParams const& params,
490  TfTokenVector *renderTags);
491 
492  // This function disposes of: the render index, the render plugin,
493  // the task controller, and the usd imaging delegate.
494  USDIMAGINGGL_API
495  void _DeleteHydraResources();
496 
497  USDIMAGINGGL_API
498  static TfToken _GetDefaultRendererPluginId();
499 
500  HdEngine _engine;
501 
502  HdRenderIndex *_renderIndex;
503 
504  HdxSelectionTrackerSharedPtr _selTracker;
505  HdRprimCollection _renderCollection;
506  HdRprimCollection _intersectCollection;
507 
508  SdfPath const _delegateID;
509  UsdImagingDelegate *_delegate;
510 
511  HdRendererPlugin *_rendererPlugin;
512  TfToken _rendererId;
513  HdxTaskController *_taskController;
514 
515  GlfSimpleLightingContextRefPtr _lightingContextForOpenGLState;
516 
517  // Data we want to live across render plugin switches:
518  GfVec4f _selectionColor;
519 
520  SdfPath _rootPath;
521  SdfPathVector _excludedPrimPaths;
522  SdfPathVector _invisedPrimPaths;
523  bool _isPopulated;
524 
525  // An implementation of much of the engine functionality that doesn't
526  // invoke any of the advanced Hydra features. It is kept around for
527  // backwards compatibility and may one day be deprecated. Most of the
528  // time we expect this to be null. When it is not null, none of the other
529  // member variables of this class are used.
530  std::unique_ptr<UsdImagingGLLegacyEngine> _legacyImpl;
531 };
532 
533 
534 PXR_NAMESPACE_CLOSE_SCOPE
535 
536 #endif // USDIMAGINGGL_ENGINE_H
static USDIMAGINGGL_API bool IsHydraEnabled()
Returns true if Hydra is enabled for GL drawing.
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:119
static USDIMAGINGGL_API bool IsColorCorrectionCapable()
Returns true if the platform is color correction capable.
USDIMAGINGGL_API SdfPath GetRprimPathFromPrimId(int primId) const
Using an Id extracted from an Id render, returns the associated rprim path.
This class defines a renderer plugin interface for Hydra.
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
static int DecodeIDRenderColor(unsigned char const idColor[4])
Resolves a 4-byte pixel from an id render to an int32 prim ID.
Definition: engine.h:336
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.
USDIMAGINGGL_API SdfPath GetPrimPathFromInstanceIndex(const SdfPath &protoRprimId, int protoIndex, int *instancerIndex=NULL, SdfPath *masterCachePath=NULL, SdfPathVector *instanceContext=NULL)
Returns the rprim id path of the instancer being rendered by this engine that corresponds to the inst...
USDIMAGINGGL_API bool TestIntersection(const GfMatrix4d &viewMatrix, const GfMatrix4d &projectionMatrix, const GfMatrix4d &worldToLocalSpace, const UsdPrim &root, const UsdImagingGLRenderParams &params, GfVec3d *outHitPoint, SdfPath *outHitPrimPath=NULL, SdfPath *outHitInstancerPath=NULL, int *outHitInstanceIndex=NULL, int *outHitElementIndex=NULL)
Finds closest point of intersection with a frustum by rendering.
A map with string keys and VtValue values.
Definition: dictionary.h:61
static USDIMAGINGGL_API TfTokenVector GetRendererPlugins()
Return the vector of available render-graph delegate plugins.
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
A class representing a GL render target with mutliple image attachments.
Definition: drawTarget.h:68
USDIMAGINGGL_API VtDictionary GetRenderStats() const
Returns render statistics.
USDIMAGINGGL_API UsdImagingGLRendererSettingsList GetRendererSettingsList() const
Returns the list of renderer settings.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
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.
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:131
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
A named, semantic collection of objects.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
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.
static int DecodeIDRenderColor(unsigned char const idColor[4])
Utility: Given a UNorm8Vec4 pixel, unpack it into an int32 ID.
Definition: pickTask.h:190
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
USDIMAGINGGL_API void SetCameraStateFromOpenGL()
Helper function to extract camera and viewport state from opengl and then call SetCameraState and Set...
USDIMAGINGGL_API SdfPath GetPrimPathFromPrimIdColor(GfVec4i const &primIdColor, GfVec4i const &instanceIdColor, int *instanceIndexOut=NULL)
Using colors extracted from an Id render, returns the associated prim path and optional instance inde...
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:81
USDIMAGINGGL_API void SetLightingState(GlfSimpleLightingContextPtr const &src)
Copy lighting state from another lighting context.
USDIMAGINGGL_API void SetColorCorrectionSettings(TfToken const &id, GfVec2i const &framebufferResolution)
Set id to one of the HdxColorCorrectionTokens.
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:86
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:182
USDIMAGINGGL_API bool SetRendererPlugin(TfToken const &id)
Set the current render-graph delegate to id.