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 <UT/UT_BoundingBox.h>
32 #include <UT/UT_Function.h>
33 #include <UT/UT_Matrix3.h>
34 #include <UT/UT_Matrix4.h>
35 #include <UT/UT_NonCopyable.h>
36 #include <UT/UT_Options.h>
37 #include <UT/UT_Rect.h>
38 #include <UT/UT_StringArray.h>
39 #include <UT/UT_StringMap.h>
40 #include <UT/UT_UniquePtr.h>
41 #include <UT/UT_Vector2.h>
42 #include <pxr/pxr.h>
43 
45 class UT_OptionsHolder;
46 class IMG_Metadata;
47 
49 class VtValue;
53 
54 class HUSD_Compositor;
55 class HUSD_Scene;
56 class husd_DefaultRenderSettingContext;
57 class PXL_Raster;
58 class TIL_Raster;
59 
61 {
62 public:
63  HUSD_Imaging();
64  ~HUSD_Imaging();
65 
66  // Definition for various callbacks from HUSD_Imaging.
69 
70  // The scene is not owned by this class.
71  void setScene(HUSD_Scene *scene_ref);
72 
73  // Renderer is being deactivated
74  void deactivate();
75 
76  // only the USD modes that map to ours
77  enum DrawMode
78  {
83  DRAW_WIRE_SHADED_SMOOTH
84  };
85 
86  void showPurposeRender(bool enable);
87  void showPurposeProxy(bool enable);
88  void showPurposeGuide(bool enable);
89 
90  void setDrawMode(DrawMode mode);
91  void setDrawComplexity(float complexity);
92  void setBackfaceCull(bool cull);
93  void setStage(const HUSD_DataHandle &data_handle,
94  const HUSD_ConstOverridesPtr &overrides,
95  const HUSD_ConstPostLayersPtr &postlayers);
96  bool setFrame(fpreal frame);
97  bool setLightingMode(HUSD_LightingMode mode);
98  void setHeadlightIntensity(fpreal intensity);
99  void setThreePointLights(const UT_Vector3F &key_color,
100  const UT_Vector2D &key_dir,
101  const UT_Vector3F &fill_color,
102  const UT_Vector2D &fill_dir,
103  const UT_Vector3F &back_color,
104  const UT_Vector2D &back_dir);
105  void setDomeLight(const UT_StringHolder &env_name,
106  const UT_Vector3F &tint,
107  const UT_Vector3F &rotate);
108  void setPhysicalSky(const fpreal32 &sun_int,
109  const UT_Vector2D &sun_dir,
110  const fpreal32 &sky_int,
111  const UT_StringHolder &map_name);
112  void setLighting(bool enable);
113  void setMaterials(bool enable);
114  void setAspectPolicy(HUSD_AspectConformPolicy p);
115 
116  // Pass false to this method before setting up the renderer. This
117  // causes the XUSD_ImagingEngineGL object to be created with the
118  // "Null Hgi" instead of the standard OpenGL Hgi.
119  void setAllowStormRenderer(bool allow_storm);
120 
122  {
126  };
127  BufferSet hasAOVBuffers() const;
128 
129  // This callback is run after the UsdImagineEngineGL::_Execute method.
130  // This method will clear the current VAO when it exits when running in
131  // a core profile OpenGL context (i.e. always on Mac). So we need a
132  // chance ot notify the RE_OGLRender that the VAO has been unbound.
133  void setPostRenderCallback(ImagingCallback cb);
134  bool getUsingCoreProfile();
135 
136  bool canBackgroundRender(const UT_StringRef &name) const;
137 
138  // Fire off a render and return immediately.
139  // Only call if canBackgroundRender() returns true.
140  bool launchBackgroundRender(const UT_Matrix4D &view_matrix,
141  const UT_Matrix4D &proj_matrix,
142  const UT_DimRect &viewport_rect,
143  const UT_StringRef &renderer,
144  const UT_Options *render_opts,
145  bool cam_effects);
146  // Wait for the BG update to be finished.
147  void waitForUpdateToComplete();
148  // Check if the BG update is finished, and optionally do a render if it is.
149  bool checkRender(bool do_render);
150 
151  void updateComposite(
152  bool free_buffers_if_missing,
153  const COP_SlapCompViewInfo *v = nullptr);
154 
155 
156  /// Returns one of the primary buffers.
157  HUSD_RenderBuffer getAOVBuffer(const UT_StringRef &name) const;
158  /// Returns a buffer even if it is an extra buffer.
159  HUSD_RenderBuffer getAOVBufferIncludingExtra(const UT_StringRef &name) const;
160  void getAOVRasters(const UT_Vector2i &res,
161  const UT_StringHolder &aovpattern,
162  UT_StringArray &aovnames,
164  const PXL_Raster *background_raster = nullptr,
165  int bleft = -1, int bbottom = -1,
166  int bright = -1, int btop = -1,
167  fpreal sx = 0.0, fpreal sy = 0.0,
168  fpreal sw = 1.0, fpreal sh = 1.0) const;
169  bool getAOVBufferInfo(UT_Vector2i &resolution,
170  UT_DimRect &data_window) const;
171 
172  /// Return the set of color aovs
173  const UT_StringSet &getColorAOVs() const;
174  const UT_StringSet &getNonColorAOVs() const;
175 
176  // Fire off a render and block until done. It may return false if the
177  // render delegate fails to initialize, it which case another delegate
178  // should be chosen.
179  bool render(const UT_Matrix4D &view_matrix,
180  const UT_Matrix4D &proj_matrix,
181  const UT_DimRect &viewport_rect,
182  const UT_StringRef &renderer,
183  const UT_Options *render_opts,
184  bool cam_effects,
185  const COP_SlapCompViewInfo *v = nullptr);
186 
187  // Set the camera being viewed through (can be null for no camera);
189  bool camera_synced = true)
190  {
191  if(path != myCameraPath)
192  {
193  myCameraPath = path;
194  mySettingsChanged = true;
195  }
196  // use camera for sampling parms, not frustum.
197  if(camera_synced != myCameraSynced)
198  {
199  myCameraSynced = camera_synced;
200  mySettingsChanged = true;
201  }
202  }
203 
204  // Get/set the render pass being used (can be null for no pass)
205  // NOTE: this value is picked up and used in updateRenderData()
207  { return myRenderPassPath; }
209  { myRenderPassPath = path; }
210 
211  void showHdGpProcedurals(bool show)
212  { myShowHdGpProcedurals = show; }
213 
215  { myCompositor = comp; }
216 
217  HUSD_Scene &scene()
218  { return *myScene; }
219  bool isConverged() const
220  { return !isUpdateRunning() && myConverged; }
221  void terminateRender(bool hard_halt = true);
222 
223  bool getBoundingBox(UT_BoundingBox &bbox,
224  const UT_Matrix3R *rot) const;
225 
227  { return myRendererName; }
228  void getRendererCommands(
229  UT_StringArray &command_names,
230  UT_StringArray &command_descriptions) const;
231  void invokeRendererCommand(
232  const UT_StringHolder &command_name) const;
233 
235  RUNNING_UPDATE_NOT_STARTED = 0,
238  RUNNING_UPDATE_FATAL
239  };
240  bool isUpdateRunning() const;
241  bool isUpdateComplete() const;
242 
243  // Control the pause state of the render. Return true if it is paused.
244  // Track pausing invoked by the user separately from "automatic" pausing
245  // which happens when switching between Houdini GL and another renderer.
246  void pauseRender();
247  void resumeRender();
248  bool canPause() const;
249  bool isPausedByUser() const;
250  bool isStoppedByUser() const;
251  bool rendererCreated() const;
252 
253  // Track whether this object should process updates from the stage. Also
254  // controls whether the renderer can be unpaused. We want to prevent the
255  // automatic unpausing of the render when the user explicitly pauses it.
256  bool allowUpdates() const
257  { return myAllowUpdates; }
258  void setAllowUpdates(bool allow_updates)
259  { myAllowUpdates = allow_updates; }
260 
261  static void initializeAvailableRenderers();
262  static bool getAvailableRenderers(HUSD_RendererInfoMap &info_map);
263 
264  void setRenderSettings(const UT_StringRef &settings_path,
265  int w=0, int h=0,
266  fpreal resscale=0.0,
267  const UT_Vector4F &render_region={ 0.0, 0.0, 0.0, 0.0 });
268 
269  const UT_StringArray &rendererPlanes() const { return myPlaneList; }
270  bool setOutputPlane(const UT_StringRef &name);
271  const UT_StringHolder &outputPlane() const { return myOutputPlane; }
272  const UT_StringHolder &currentAov() const { return myCurrentAOV; }
273 
274  void getRenderStats(UT_Options &stats);
275 
276  // Embed the render statistics that getRenderStats() stores under the
277  // "__json" key in @p stats into @p metadata, under the same
278  // "husk:render_stats" key husk writes (via its UsdRenderers.json metadata
279  // template). This lets an image saved outside husk -- e.g. a viewport or
280  // render gallery snapshot -- carry the stats the HTML render report reads,
281  // equivalent to a husk-written EXR. Does nothing and returns false when
282  // @p stats has no "__json" entry.
283  static bool embedRenderStatsMetadata(const UT_Options &stats,
284  IMG_Metadata &metadata);
285 
286  void setRenderFocus(int x, int y) const;
287  void clearRenderFocus() const;
288 
289  // Apply options to the contained render settings context to control
290  // whether or not the data window from the render settings prim should be
291  // passed to the renderer.
292  void setRenderSettingsDataWindowActive(bool active);
293 
294  // Returns the paths associated with render keys from the primid and instid
295  // buffers. Stores the result in myRenderKeyToPathMap so future lookups
296  // are fast.
297 
298  void getPrimPathsFromRenderKeys(
299  const UT_Set<HUSD_RenderKey> &keys,
300  HUSD_RenderKeyPathMap &outkeypathmap);
301 
302  void updateDeferredPrims();
303 
305  COP_SlapCompDispatcher *manager)
306  { mySlapCompProgramManager = manager; }
307 
308  /// If this returns true, then the last attempted application of slapcomp
309  /// may have changed the possible outputs and/or the current display AOV.
310  /// This flag is reset before each application of slapcomp.
311  bool slapcompStateChanged() const
312  {
313  return mySlapCompProgramManager &&
314  mySlapCompProgramManager->hasStateChanged();
315  }
316 
317  /// Updates the list of available planes.
318  /// The plane list is reset and built anew.
319  void updatePlanes(bool ignore_slapcomp);
320 
321  /// Called by our data micro node that monitors for changes to COP textures
322  /// used by karma renders. When this function is called we need to restart
323  /// the render.
324  void handleCopTextureChange(bool time_changed);
325  /// Set a callback that is run at the end of handleCopTextureChange.
326  void setCopTextureChangeCallback(ImagingCallback cb);
327  void updateApexAnimateSceneIndex(
328  const UT_StringRef &scene_prim_path,
329  const UT_StringMap<GU_ConstDetailHandle> &graph_outputs) const;
330 
331 private:
332  class husd_ImagingPrivate;
333  class husd_IMXRenderBuffer;
334 
335  bool isSlapCompEnabled() const;
336  bool isViewportSlapCompEnabled() const;
337  void deactivateSlapComp();
338 
339  /// Returns true if the given AOV is an output of slapcomp.
340  bool isSlapCompAOV(const UT_StringHolder& name) const;
341 
342  /// Registers all active AOVs with the slapcomp registry,
343  /// run on terminate or on convergence
344  void registerSlapCompAOVs(
345  bool dostash,
346  const COP_SlapCompViewInfo *view_info);
347 
348  /// Internal helper function to run slapcomp.
349  void runSlapCompIfNeeded(
350  const COP_SlapCompViewInfo* view_info);
351  /// Returns a pointer to HdRenderBuffer. This method fetches an output of
352  /// the specified name (slapcomp layer if it has an output of the given
353  /// name, otherwise the renderer's result).
354  void* getRenderOrSlapCompOutput(const UT_StringHolder& name,
355  husd_IMXRenderBuffer* b)
356  const;
357 
358  void resetImagingEngine();
359  const HUSD_DataHandle &viewerLopDataHandle() const;
360  bool updateRestartCameraSettings(bool cam_effects) const;
361  bool anyRestartRenderSettingsChanged() const;
362  bool setupRenderer(const UT_StringRef &renderer_name,
363  const UT_Options *render_opts,
364  bool cam_effects);
365  void updateSettingIfRequired(const UT_StringRef &key,
366  const PXR_NS::VtValue &value,
367  bool from_usd_prim = false);
368  void updateSettingsIfRequired(HUSD_AutoReadLock &lock);
369  RunningStatus updateRenderData(const UT_Matrix4D &view_matrix,
370  const UT_Matrix4D &proj_matrix,
371  const UT_DimRect &viewport_rect,
372  bool cam_effects);
373  void gatherCopResolverDependencies();
374  void finishRender(bool do_render);
375 
377  fpreal myFrame;
378  HUSD_DataHandle myDataHandle;
380  HUSD_ConstOverridesPtr myOverrides;
381  HUSD_ConstPostLayersPtr myPostLayers;
382  unsigned myWantsHeadlight : 1,
383  myHasHeadlight : 1,
384  myWantsDomelight : 1,
385  myHasDomelight : 1,
386  myWantsThreePointlight : 1,
387  myHasThreePointlight : 1,
388  myWantsPhysicalSky : 1,
389  myHasPhysicalSky : 1,
390  myDoLighting : 1,
391  myDoMaterials : 1,
392  myConverged : 1,
393  mySettingsChanged : 1,
394  myCameraSynced : 1,
395  myWorkLightsChanged : 1,
396  myValidRenderSettingsPrim : 1,
397  myAOVsStashed : 1;
398  bool myIsPaused;
399  bool myAllowUpdates;
400  bool myAllowStormRenderer;
401  HUSD_Scene *myScene;
402  UT_StringHolder myRendererName;
403  HUSD_Compositor *myCompositor;
404  ImagingCallback myPostRenderCallback;
405  ImagingCallback myCopTextureChangeCallback;
406  UT_Options myCurrentDisplayOptions;
407  SYS_AtomicInt32 myRunningInBackground;
408  UT_StringHolder myOutputPlane;
409  UT_StringHolder myCurrentAOV;
410  UT_StringHolder myCameraPath;
412  UT_UniquePtr<husd_DefaultRenderSettingContext> myRenderSettingsContext;
413  HUSD_Path myRenderPassPath;
414  bool myShowHdGpProcedurals;
415  int myConformPolicy;
416  BufferSet myLastCompositedBufferSet;
417  UT_Map<HUSD_RenderKey, UT_StringHolder> myRenderKeyToPathMap;
419  fpreal myHeadlightIntensity;
420  UT_Vector3F myKeyColor;
421  UT_Vector2D myKeyDir;
422  UT_Vector3F myFillColor;
423  UT_Vector2D myFillDir;
424  UT_Vector3F myBackColor;
425  UT_Vector2D myBackDir;
426  UT_StringHolder myDomeMapName;
427  UT_Vector3F myDomeTint;
428  UT_Vector3F myDomeRotate;
429  fpreal32 mySkyIntensity;
430  fpreal32 mySunIntensity;
431  UT_Vector2D mySkyDir;
432  UT_StringHolder mySkyMapName;
433 
434  /// Track whether we are currently handling a COP texture change (we want
435  /// to avoid getting into a recursive loop of such calls, which should be
436  /// impossible, but better safe than sorry).
437  bool myHandlingCopTextureChange;
438 
439  /// The list of all planes that this object knows about. This is a union of
440  /// renderer's and slapcomp's plane, with duplicates removed (as slapcomp's
441  /// results take precedence over the renderer's).
442  UT_StringArray myPlaneList;
443  /// The list of planes that are outputs of the renderer.
444  UT_StringArray myRendererPlaneList;
445 
446  COP_SlapCompDispatcher *mySlapCompProgramManager;
447 
448  /// Tracks last view info so we can update on terminate.
449  COP_SlapCompViewInfo myLastSlapCompViewInfo;
450 };
451 
452 #endif
const HUSD_Path & renderPassPrimPath() const
Definition: HUSD_Imaging.h:206
bool isConverged() const
Definition: HUSD_Imaging.h:219
void deactivate(GridOrTree &, const typename GridOrTree::ValueType &value, const typename GridOrTree::ValueType &tolerance=zeroVal< typename GridOrTree::ValueType >(), const bool threaded=true)
Mark as inactive any active tiles or voxels in the given grid or tree whose values are equal to value...
Definition: Activate.h:203
Definition: UT_Set.h:58
Unsorted map container.
Definition: UT_Map.h:114
const UT_StringHolder & currentAov() const
Definition: HUSD_Imaging.h:272
UT_Map< HUSD_Path, UT_Array< UT_Vector3 > > HUSD_PointMap
Definition: HUSD_Imaging.h:68
void setAOVCompositor(HUSD_Compositor *comp)
Definition: HUSD_Imaging.h:214
const GLdouble * v
Definition: glcorearb.h:837
XUSD_RenderSettings contains the HdRenderSettings for the render.
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
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:226
void showHdGpProcedurals(bool show)
Definition: HUSD_Imaging.h:211
const UT_StringHolder & outputPlane() const
Definition: HUSD_Imaging.h:271
Simple interface around HdRenderBuffer.
bool slapcompStateChanged() const
Definition: HUSD_Imaging.h:311
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:217
GLenum mode
Definition: glcorearb.h:99
void setCameraPath(const UT_StringRef &path, bool camera_synced=true)
Definition: HUSD_Imaging.h:188
void setRenderPassPrimPath(const HUSD_Path &path)
Definition: HUSD_Imaging.h:208
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
A map of string to various well defined value types.
Definition: UT_Options.h:87
const UT_StringArray & rendererPlanes() const
Definition: HUSD_Imaging.h:269
fpreal64 fpreal
Definition: SYS_Types.h:283
HUSD_AspectConformPolicy
Definition: HUSD_Utils.h:111
void setAllowUpdates(bool allow_updates)
Definition: HUSD_Imaging.h:258
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void setSlapCompProgramManager(COP_SlapCompDispatcher *manager)
Definition: HUSD_Imaging.h:304
Map of metadata items.
Definition: IMG_Metadata.h:217
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
UT_Function< void(HUSD_Imaging *imaging)> ImagingCallback
Definition: HUSD_Imaging.h:67
Definition: value.h:89
bool allowUpdates() const
Definition: HUSD_Imaging.h:256