HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HUSD_Imaging.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Side Effects Software Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef __HUSD_Imaging_h__
19 #define __HUSD_Imaging_h__
20 
21 #include "HUSD_API.h"
22 #include "HUSD_DataHandle.h"
23 #include "HUSD_Path.h"
24 #include "HUSD_RenderBuffer.h"
25 #include "HUSD_RendererInfo.h"
26 #include "HUSD_LightingMode.h"
27 #include "HUSD_Utils.h"
28 
31 #include <GU/GU_DetailHandle.h>
32 #include <UT/UT_BoundingBox.h>
33 #include <UT/UT_Function.h>
34 #include <UT/UT_Matrix3.h>
35 #include <UT/UT_Matrix4.h>
36 #include <UT/UT_NonCopyable.h>
37 #include <UT/UT_Options.h>
38 #include <UT/UT_Rect.h>
39 #include <UT/UT_StringArray.h>
40 #include <UT/UT_UniquePtr.h>
41 #include <UT/UT_Vector2.h>
42 #include <pxr/pxr.h>
43 
45 class VtValue;
49 
50 class HUSD_Compositor;
51 class HUSD_Scene;
52 class husd_DefaultRenderSettingContext;
53 class PXL_Raster;
54 class TIL_Raster;
55 
57 {
58 public:
59  HUSD_Imaging();
60  ~HUSD_Imaging();
61 
62  // Definition for various callbacks from HUSD_Imaging.
65 
66  // The scene is not owned by this class.
67  void setScene(HUSD_Scene *scene_ref);
68 
69  // only the USD modes that map to ours
70  enum DrawMode
71  {
76  DRAW_WIRE_SHADED_SMOOTH
77  };
78 
79  void showPurposeRender(bool enable);
80  void showPurposeProxy(bool enable);
81  void showPurposeGuide(bool enable);
82 
83  void setDrawMode(DrawMode mode);
84  void setDrawComplexity(float complexity);
85  void setBackfaceCull(bool cull);
86  void setStage(const HUSD_DataHandle &data_handle,
87  const HUSD_ConstOverridesPtr &overrides,
88  const HUSD_ConstPostLayersPtr &postlayers);
89  bool setFrame(fpreal frame);
90  bool setLightingMode(HUSD_LightingMode mode);
91  void setHeadlightIntensity(fpreal intensity);
92  void setThreePointLights(const UT_Vector3F &key_color,
93  const UT_Vector2D &key_dir,
94  const UT_Vector3F &fill_color,
95  const UT_Vector2D &fill_dir,
96  const UT_Vector3F &back_color,
97  const UT_Vector2D &back_dir);
98  void setDomeLight(const UT_StringHolder &env_name,
99  const UT_Vector3F &tint,
100  const UT_Vector3F &rotate);
101  void setPhysicalSky(const fpreal32 &sun_int,
102  const UT_Vector2D &sun_dir,
103  const fpreal32 &sky_int,
104  const UT_StringHolder &map_name);
105  void setLighting(bool enable);
106  void setMaterials(bool enable);
107  void setAspectPolicy(HUSD_AspectConformPolicy p);
108 
109  // Pass false to this method before setting up the renderer. This
110  // causes the XUSD_ImagingEngineGL object to be created with the
111  // "Null Hgi" instead of the standard OpenGL Hgi.
112  void setAllowStormRenderer(bool allow_storm);
113 
115  {
119  };
120  BufferSet hasAOVBuffers() const;
121 
122  // This callback is run after the UsdImagineEngineGL::_Execute method.
123  // This method will clear the current VAO when it exits when running in
124  // a core profile OpenGL context (i.e. always on Mac). So we need a
125  // chance ot notify the RE_OGLRender that the VAO has been unbound.
126  void setPostRenderCallback(ImagingCallback cb);
127  bool getUsingCoreProfile();
128 
129  bool canBackgroundRender(const UT_StringRef &name) const;
130 
131  // Fire off a render and return immediately.
132  // Only call if canBackgroundRender() returns true.
133  bool launchBackgroundRender(const UT_Matrix4D &view_matrix,
134  const UT_Matrix4D &proj_matrix,
135  const UT_DimRect &viewport_rect,
136  const UT_StringRef &renderer,
137  const UT_Options *render_opts,
138  bool cam_effects);
139  // Wait for the BG update to be finished.
140  void waitForUpdateToComplete();
141  // Check if the BG update is finished, and optionally do a render if it is.
142  bool checkRender(bool do_render);
143 
144  void updateComposite(
145  bool free_buffers_if_missing,
146  const COP_SlapcompViewInfo *v = nullptr);
147 
148  /// Returns one of the primary buffers.
149  HUSD_RenderBuffer getAOVBuffer(const UT_StringRef &name) const;
150  /// Returns a buffer even if it is an extra buffer.
151  HUSD_RenderBuffer getAOVBufferIncludingExtra(const UT_StringRef &name) const;
152  void getAOVRasters(const UT_Vector2i &res,
153  const UT_StringHolder &aovpattern,
154  UT_StringArray &aovnames,
156  const PXL_Raster *background_raster = nullptr,
157  int bleft = -1, int bbottom = -1,
158  int bright = -1, int btop = -1,
159  fpreal sx = 0.0, fpreal sy = 0.0,
160  fpreal sw = 1.0, fpreal sh = 1.0) const;
161  bool getAOVBufferInfo(UT_Vector2i &resolution,
162  UT_DimRect &data_window) const;
163 
164  // Fire off a render and block until done. It may return false if the
165  // render delegate fails to initialize, it which case another delegate
166  // should be chosen.
167  bool render(const UT_Matrix4D &view_matrix,
168  const UT_Matrix4D &proj_matrix,
169  const UT_DimRect &viewport_rect,
170  const UT_StringRef &renderer,
171  const UT_Options *render_opts,
172  bool cam_effects,
173  const COP_SlapcompViewInfo *v = nullptr);
174 
175  // Set the camera being viewed through (can be null for no camera);
177  bool camera_synced = true)
178  {
179  if(path != myCameraPath)
180  {
181  myCameraPath = path;
182  mySettingsChanged = true;
183  }
184  // use camera for sampling parms, not frustum.
185  if(camera_synced != myCameraSynced)
186  {
187  myCameraSynced = camera_synced;
188  mySettingsChanged = true;
189  }
190  }
191 
192  // Set the render pass being used (can be null for no pass)
193  // NOTE: this value is picked up and used in updateRenderData()
195  { myRenderPassPath = path; }
196 
198  { myCompositor = comp; }
199 
200  HUSD_Scene &scene()
201  { return *myScene; }
202  bool isConverged() const
203  { return !isUpdateRunning() && myConverged; }
204  void terminateRender(bool hard_halt = true);
205 
206  bool getBoundingBox(UT_BoundingBox &bbox,
207  const UT_Matrix3R *rot) const;
208 
210  { return myRendererName; }
211  void getRendererCommands(
212  UT_StringArray &command_names,
213  UT_StringArray &command_descriptions) const;
214  void invokeRendererCommand(
215  const UT_StringHolder &command_name) const;
216 
218  RUNNING_UPDATE_NOT_STARTED = 0,
221  RUNNING_UPDATE_FATAL
222  };
223  bool isUpdateRunning() const;
224  bool isUpdateComplete() const;
225 
226  // Control the pause state of the render. Return true if it is paused.
227  // Track pausing invoked by the user separately from "automatic" pausing
228  // which happens when switching between Houdini GL and another renderer.
229  void pauseRender();
230  void resumeRender();
231  bool canPause() const;
232  bool isPausedByUser() const;
233  bool isStoppedByUser() const;
234  bool rendererCreated() const;
235 
236  // Track whether this object should process updates from the stage. Also
237  // controls whether the renderer can be unpaused. We want to prevent the
238  // automatic unpausing of the render when the user explicitly pauses it.
239  bool allowUpdates() const
240  { return myAllowUpdates; }
241  void setAllowUpdates(bool allow_updates)
242  { myAllowUpdates = allow_updates; }
243 
244  static void initializeAvailableRenderers();
245  static bool getAvailableRenderers(HUSD_RendererInfoMap &info_map);
246 
247  void setRenderSettings(const UT_StringRef &settings_path,
248  int w=0, int h=0,
249  fpreal resscale=0.0,
250  const UT_Vector4F &render_region={ 0.0, 0.0, 0.0, 0.0 });
251 
252  const UT_StringArray &rendererPlanes() const { return myPlaneList; }
253  bool setOutputPlane(const UT_StringRef &name);
254  const UT_StringHolder &outputPlane() const { return myOutputPlane; }
255  const UT_StringHolder &currentAov() const { return myCurrentAOV; }
256 
257  void getRenderStats(UT_Options &stats);
258 
259  void setRenderFocus(int x, int y) const;
260  void clearRenderFocus() const;
261 
262  // Apply options to the contained render settings context to control
263  // whether or not the data window from the render settings prim should be
264  // passed to the renderer.
265  void setRenderSettingsDataWindowActive(bool active);
266 
267  // Returns the paths associated with render keys from the primid and instid
268  // buffers. Stores the result in myRenderKeyToPathMap so future lookups
269  // are fast.
270 
271  void getPrimPathsFromRenderKeys(
272  const UT_Set<HUSD_RenderKey> &keys,
273  HUSD_RenderKeyPathMap &outkeypathmap);
274 
275  void updateDeferredPrims();
276 
278  COP_SlapcompDispatcher *manager)
279  { mySlapcompProgramManager = manager; }
280 
281  /// Returns true if slapcomp is enabled.
282  bool isSlapcompEnabled() const
283  {
284  return mySlapcompProgramManager &&
285  mySlapcompProgramManager->isEnabled();
286  }
287  /// If this returns true, then the last attempted application of slapcomp
288  /// may have changed the possible outputs and/or the current display AOV.
289  /// This flag is reset before each application of slapcomp.
290  bool slapcompStateChanged() const
291  {
292  return mySlapcompProgramManager &&
293  mySlapcompProgramManager->hasStateChanged();
294  }
295 
296  /// Updates the list of available planes.
297  /// The plane list is reset and built anew.
298  void updatePlanes(bool ignore_slapcomp);
299 
300  /// Called by our data micro node that monitors for changes to COP textures
301  /// used by karma renders. When this function is called we need to restart
302  /// the render.
303  void handleCopTextureChange(bool time_changed);
304  /// Set a callback that is run at the end of handleCopTextureChange.
305  void setCopTextureChangeCallback(ImagingCallback cb);
306 
307 private:
308  class husd_ImagingPrivate;
309  class husd_IMXRenderBuffer;
310 
311  /// Returns true if the given AOV is an output of slapcomp.
312  bool isSlapcompAOV(const UT_StringHolder& name) const;
313 
314  /// Registers all active AOVs with the slapcomp registry,
315  /// run on terminate or on convergence
316  void registerSlapcompAOVs(
317  bool dostash,
318  const COP_SlapcompViewInfo *view_info);
319 
320  /// Internal helper function to run slapcomp.
321  void runSlapcompIfNeeded(
322  const COP_SlapcompViewInfo* view_info);
323  /// Returns a pointer to HdRenderBuffer. This method fetches an output of
324  /// the specified name (slapcomp layer if it has an output of the given
325  /// name, otherwise the renderer's result).
326  void* getRenderOrSlapcompOutput(const UT_StringHolder& name,
327  husd_IMXRenderBuffer* b)
328  const;
329 
330  void resetImagingEngine();
331  const HUSD_DataHandle &viewerLopDataHandle() const;
332  bool updateRestartCameraSettings(bool cam_effects) const;
333  bool anyRestartRenderSettingsChanged() const;
334  bool setupRenderer(const UT_StringRef &renderer_name,
335  const UT_Options *render_opts,
336  bool cam_effects);
337  void updateSettingIfRequired(const UT_StringRef &key,
338  const PXR_NS::VtValue &value,
339  bool from_usd_prim = false);
340  void updateSettingsIfRequired(HUSD_AutoReadLock &lock);
341  RunningStatus updateRenderData(const UT_Matrix4D &view_matrix,
342  const UT_Matrix4D &proj_matrix,
343  const UT_DimRect &viewport_rect,
344  bool cam_effects);
345  void gatherCopResolverDependencies();
346  void finishRender(bool do_render);
347 
349  fpreal myFrame;
350  HUSD_DataHandle myDataHandle;
352  HUSD_ConstOverridesPtr myOverrides;
353  HUSD_ConstPostLayersPtr myPostLayers;
354  unsigned myWantsHeadlight : 1,
355  myHasHeadlight : 1,
356  myWantsDomelight : 1,
357  myHasDomelight : 1,
358  myWantsThreePointlight : 1,
359  myHasThreePointlight : 1,
360  myWantsPhysicalSky : 1,
361  myHasPhysicalSky : 1,
362  myDoLighting : 1,
363  myDoMaterials : 1,
364  myConverged : 1,
365  mySettingsChanged : 1,
366  myCameraSynced : 1,
367  myWorkLightsChanged : 1,
368  myValidRenderSettingsPrim : 1,
369  myAOVsStashed : 1;
370  bool myIsPaused;
371  bool myAllowUpdates;
372  bool myAllowStormRenderer;
373  HUSD_Scene *myScene;
374  UT_StringHolder myRendererName;
375  HUSD_Compositor *myCompositor;
376  ImagingCallback myPostRenderCallback;
377  ImagingCallback myCopTextureChangeCallback;
378  UT_Options myCurrentDisplayOptions;
379  SYS_AtomicInt32 myRunningInBackground;
380  UT_StringHolder myOutputPlane;
381  UT_StringHolder myCurrentAOV;
382  UT_StringHolder myCameraPath;
384  UT_UniquePtr<husd_DefaultRenderSettingContext> myRenderSettingsContext;
385  HUSD_Path myRenderPassPath;
386  int myConformPolicy;
387  BufferSet myLastCompositedBufferSet;
388  UT_Map<HUSD_RenderKey, UT_StringHolder> myRenderKeyToPathMap;
390  fpreal myHeadlightIntensity;
391  UT_Vector3F myKeyColor;
392  UT_Vector2D myKeyDir;
393  UT_Vector3F myFillColor;
394  UT_Vector2D myFillDir;
395  UT_Vector3F myBackColor;
396  UT_Vector2D myBackDir;
397  UT_StringHolder myDomeMapName;
398  UT_Vector3F myDomeTint;
399  UT_Vector3F myDomeRotate;
400  fpreal32 mySkyIntensity;
401  fpreal32 mySunIntensity;
402  UT_Vector2D mySkyDir;
403  UT_StringHolder mySkyMapName;
404 
405  /// Track whether we are currently handling a COP texture change (we want
406  /// to avoid getting into a recursive loop of such calls, which should be
407  /// impossible, but better safe than sorry).
408  bool myHandlingCopTextureChange;
409 
410  /// The list of all planes that this object knows about. This is a union of
411  /// renderer's and slapcomp's plane, with duplicates removed (as slapcomp's
412  /// results take precedence over the renderer's).
413  UT_StringArray myPlaneList;
414  /// The list of planes that are outputs of the renderer.
415  UT_StringArray myRendererPlaneList;
416  /// The list of planes that are outputs of slapcomp.
417  UT_StringArray mySlapcompPlaneList;
418 
419  COP_SlapcompDispatcher *mySlapcompProgramManager;
420 
421  /// Tracks last view info so we can update on terminate.
422  COP_SlapcompViewInfo myLastSlapcompViewInfo;
423 };
424 
425 #endif
426 
bool isConverged() const
Definition: HUSD_Imaging.h:202
Definition: UT_Set.h:58
Unsorted map container.
Definition: UT_Map.h:109
const UT_StringHolder & currentAov() const
Definition: HUSD_Imaging.h:255
UT_Map< HUSD_Path, UT_Array< UT_Vector3 > > HUSD_PointMap
Definition: HUSD_Imaging.h:64
void setAOVCompositor(HUSD_Compositor *comp)
Definition: HUSD_Imaging.h:197
const GLdouble * v
Definition: glcorearb.h:837
XUSD_RenderSettings contains the HdRenderSettings for the render.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
GA_API const UT_StringHolder rot
#define HUSD_API
Definition: HUSD_API.h:31
GLint y
Definition: glcorearb.h:103
float fpreal32
Definition: SYS_Types.h:200
IFDmantra you can see code vm_image_mplay_direction endcode When SOHO starts a render
Definition: HDK_Image.dox:266
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
const UT_StringHolder & rendererName() const
Definition: HUSD_Imaging.h:209
const UT_StringHolder & outputPlane() const
Definition: HUSD_Imaging.h:254
Simple interface around HdRenderBuffer.
bool isSlapcompEnabled() const
Returns true if slapcomp is enabled.
Definition: HUSD_Imaging.h:282
bool slapcompStateChanged() const
Definition: HUSD_Imaging.h:290
HUSD_LightingMode
HUSD_API const char * resolution()
GLuint const GLchar * name
Definition: glcorearb.h:786
std::function< T > UT_Function
Definition: UT_Function.h:37
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
ImageBuf OIIO_API rotate(const ImageBuf &src, float angle, string_view filtername=string_view(), float filterwidth=0.0f, bool recompute_roi=false, ROI roi={}, int nthreads=0)
that also have some descendant prim *whose name begins with which in turn has a child named baz where *the predicate active
HUSD_Scene & scene()
Definition: HUSD_Imaging.h:200
GLenum mode
Definition: glcorearb.h:99
void setCameraPath(const UT_StringRef &path, bool camera_synced=true)
Definition: HUSD_Imaging.h:176
void setRenderPassPrimPath(const HUSD_Path &path)
Definition: HUSD_Imaging.h:194
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
A map of string to various well defined value types.
Definition: UT_Options.h:84
const UT_StringArray & rendererPlanes() const
Definition: HUSD_Imaging.h:252
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
fpreal64 fpreal
Definition: SYS_Types.h:278
HUSD_AspectConformPolicy
Definition: HUSD_Utils.h:112
void setAllowUpdates(bool allow_updates)
Definition: HUSD_Imaging.h:241
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
UT_Function< void(HUSD_Imaging *imaging)> ImagingCallback
Definition: HUSD_Imaging.h:63
void setSlapcompProgramManager(COP_SlapcompDispatcher *manager)
Definition: HUSD_Imaging.h:277
Definition: value.h:146
bool allowUpdates() const
Definition: HUSD_Imaging.h:239