HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
frameRecorder.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 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_USD_IMAGING_USD_APP_UTILS_FRAME_RECORDER_H
25 #define PXR_USD_IMAGING_USD_APP_UTILS_FRAME_RECORDER_H
26 
27 /// \file usdAppUtils/frameRecorder.h
28 
29 #include "pxr/pxr.h"
31 
32 #include "pxr/base/tf/diagnostic.h"
33 #include "pxr/base/tf/token.h"
34 #include "pxr/usd/usd/stage.h"
35 #include "pxr/usd/usd/timeCode.h"
36 #include "pxr/usd/usdGeom/camera.h"
38 
39 #include <string>
40 
41 
43 
44 
45 /// \class UsdAppUtilsFrameRecorder
46 ///
47 /// A utility class for recording images of USD stages.
48 ///
49 /// UsdAppUtilsFrameRecorder uses Hydra to produce recorded images of a USD
50 /// stage looking through a particular UsdGeomCamera on that stage at a
51 /// particular UsdTimeCode. The images generated will be effectively the same
52 /// as what you would see in the viewer in usdview.
53 ///
54 /// Note that it is assumed that an OpenGL context has already been setup for
55 /// the UsdAppUtilsFrameRecorder if OpenGL is being used as the underlying HGI
56 /// device. This is not required for Metal or Vulkan.
58 {
59 public:
60  /// The \p rendererPluginId argument indicates the renderer plugin that
61  /// Hyrda should use. If the empty token is passed in, a default renderer
62  /// plugin will be chosen depending on the value of \p gpuEnabled.
63  /// The \p gpuEnabled argument determines if the UsdAppUtilsFrameRecorder
64  /// instance will allow Hydra to use the GPU to produce images.
65  /// The \p renderSettingsPrimPath argument is used to set the active
66  /// render settings prim path in Hydra.
69  const TfToken& rendererPluginId = TfToken(),
70  bool gpuEnabled = true,
71  const SdfPath& renderSettingsPrimPath = SdfPath());
72 
73  /// Gets the ID of the Hydra renderer plugin that will be used for
74  /// recording.
76  return _imagingEngine.GetCurrentRendererId();
77  }
78 
79  /// Sets the Hydra renderer plugin to be used for recording.
80  /// This also resets the presentation flag on the HdxPresentTask to false,
81  /// to avoid the need for an OpenGL context.
82  ///
83  /// Note that the renderer plugins that may be set will be restricted if
84  /// this UsdAppUtilsFrameRecorder instance has disabled the GPU.
85  bool SetRendererPlugin(const TfToken& id) {
86  const bool succeeded = _imagingEngine.SetRendererPlugin(id);
87  _imagingEngine.SetEnablePresentation(false);
88 
89  return succeeded;
90  }
91 
92  /// Sets the width of the recorded image.
93  ///
94  /// The height of the recorded image will be computed using this value and
95  /// the aspect ratio of the camera used for recording.
96  ///
97  /// The default image width is 960 pixels.
98  void SetImageWidth(const size_t imageWidth) {
99  if (imageWidth == 0u) {
100  TF_CODING_ERROR("Image width cannot be zero");
101  return;
102  }
103  _imageWidth = imageWidth;
104  }
105 
106  /// Sets the level of refinement complexity.
107  ///
108  /// The default complexity is "low" (1.0).
109  void SetComplexity(const float complexity) {
110  _complexity = complexity;
111  }
112 
113  /// Sets the color correction mode to be used for recording.
114  ///
115  /// By default, color correction is disabled.
117  void SetColorCorrectionMode(const TfToken& colorCorrectionMode);
118 
119  /// Sets the UsdGeomImageable purposes to be used for rendering
120  ///
121  /// We will __always__ include "default" purpose, and by default,
122  /// we will also include UsdGeomTokens->proxy. Use this method
123  /// to explicitly enumerate an alternate set of purposes to be
124  /// included along with "default".
126  void SetIncludedPurposes(const TfTokenVector& purposes);
127 
128  /// Records an image and writes the result to \p outputImagePath.
129  ///
130  /// The recorded image will represent the view from \p usdCamera looking at
131  /// the imageable prims on USD stage \p stage at time \p timeCode.
132  ///
133  /// If \p usdCamera is not a valid camera, a camera will be computed
134  /// to automatically frame the stage geometry.
135  ///
136  /// When we are using a RenderSettings prim, the generated image will be
137  /// written to the file indicated on the connected RenderProducts,
138  /// instead of the given \p outputImagePath. Note that in this case the
139  /// given \p usdCamera will later be overridden by the one authored on the
140  /// RenderSettings Prim.
141  ///
142  /// Returns true if the image was generated and written successfully, or
143  /// false otherwise.
145  bool Record(
146  const UsdStagePtr& stage,
147  const UsdGeomCamera& usdCamera,
148  const UsdTimeCode timeCode,
149  const std::string& outputImagePath);
150 
151 private:
152  UsdImagingGLEngine _imagingEngine;
153  size_t _imageWidth;
154  float _complexity;
155  TfToken _colorCorrectionMode;
156  TfTokenVector _purposes;
157  SdfPath _renderSettingsPrimPath;
158 };
159 
160 
162 
163 
164 #endif
USDIMAGINGGL_API TfToken GetCurrentRendererId() const
Return the id of the currently used renderer plugin.
bool SetRendererPlugin(const TfToken &id)
Definition: frameRecorder.h:85
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
#define TF_CODING_ERROR
USDAPPUTILS_API UsdAppUtilsFrameRecorder(const TfToken &rendererPluginId=TfToken(), bool gpuEnabled=true, const SdfPath &renderSettingsPrimPath=SdfPath())
#define USDAPPUTILS_API
Definition: api.h:40
USDAPPUTILS_API void SetColorCorrectionMode(const TfToken &colorCorrectionMode)
USDIMAGINGGL_API bool SetRendererPlugin(TfToken const &id)
Definition: token.h:87
USDAPPUTILS_API bool Record(const UsdStagePtr &stage, const UsdGeomCamera &usdCamera, const UsdTimeCode timeCode, const std::string &outputImagePath)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
Definition: path.h:290
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
TfToken GetCurrentRendererId() const
Definition: frameRecorder.h:75
USDAPPUTILS_API void SetIncludedPurposes(const TfTokenVector &purposes)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
void SetImageWidth(const size_t imageWidth)
Definition: frameRecorder.h:98
USDIMAGINGGL_API void SetEnablePresentation(bool enabled)
void SetComplexity(const float complexity)