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_RenderBuffer.h"
24 #include "HUSD_RendererInfo.h"
25 #include "HUSD_Utils.h"
26 #include <UT/UT_BoundingBox.h>
27 #include <UT/UT_Function.h>
28 #include <UT/UT_Matrix3.h>
29 #include <UT/UT_Matrix4.h>
30 #include <UT/UT_NonCopyable.h>
31 #include <UT/UT_Options.h>
32 #include <UT/UT_Rect.h>
33 #include <UT/UT_StringArray.h>
34 #include <UT/UT_UniquePtr.h>
35 #include <UT/UT_Vector2.h>
36 #include <pxr/pxr.h>
37 
39 class VtValue;
43 
44 class COP_ApexProgram;
45 class HUSD_Compositor;
46 class HUSD_Scene;
47 class husd_DefaultRenderSettingContext;
48 class PXL_Raster;
49 class TIL_Raster;
50 
52 {
53 public:
54  HUSD_Imaging();
55  ~HUSD_Imaging();
56 
57  // Definition for various callbacks from HUSD_Imaging.
59 
60  // The scene is not owned by this class.
61  void setScene(HUSD_Scene *scene_ref);
62 
63  // only the USD modes that map to ours
64  enum DrawMode
65  {
70  DRAW_WIRE_SHADED_SMOOTH
71  };
72 
73  void showPurposeRender(bool enable);
74  void showPurposeProxy(bool enable);
75  void showPurposeGuide(bool enable);
76 
77  void setDrawMode(DrawMode mode);
78  void setDrawComplexity(float complexity);
79  void setBackfaceCull(bool cull);
80  void setStage(const HUSD_DataHandle &data_handle,
81  const HUSD_ConstOverridesPtr &overrides,
82  const HUSD_ConstPostLayersPtr &postlayers);
83  bool setFrame(fpreal frame);
84  bool setDefaultLights(bool doheadlight, bool dodomelight);
85  void setHeadlightIntensity(fpreal intensity);
86  void setLighting(bool enable);
87  void setMaterials(bool enable);
88  void setAspectPolicy(HUSD_AspectConformPolicy p);
89 
90  enum BufferSet
91  {
95  };
96  BufferSet hasAOVBuffers() const;
97 
98  // This callback is run after the UsdImagineEngineGL::_Execute method.
99  // This method will clear the current VAO when it exits when running in
100  // a core profile OpenGL context (i.e. always on Mac). So we need a
101  // chance ot notify the RE_OGLRender that the VAO has been unbound.
102  void setPostRenderCallback(ImagingCallback cb);
103  bool getUsingCoreProfile();
104 
105  bool canBackgroundRender(const UT_StringRef &name) const;
106 
107  // Fire off a render and return immediately.
108  // Only call if canBackgroundRender() returns true.
109  bool launchBackgroundRender(const UT_Matrix4D &view_matrix,
110  const UT_Matrix4D &proj_matrix,
111  const UT_DimRect &viewport_rect,
112  const UT_StringRef &renderer,
113  const UT_Options *render_opts,
114  bool cam_effects);
115  // Wait for the BG update to be finished.
116  void waitForUpdateToComplete();
117  // Check if the BG update is finished, and optionally do a render if it is.
118  bool checkRender(bool do_render);
119 
120  /// This struct holds the view metadata that's applied to the input layers
121  /// before they're piped to slapcomp.
123  {
124  fpreal myViewAspectRatio { 1.0 };
125  fpreal myAperture { 20.955 };
126  fpreal myHApertureOffset { 0.0 };
127  fpreal myVApertureOffset { 0.0 };
128  UT_Matrix4D myTransformMatrix { 1.0 };
129  fpreal myOrthoWidth { 2.0 };
130  fpreal myFocalLength { 50.0 };
131  fpreal myNearLimit { 0.01 };
132  fpreal myFarLimit { 100000.0 };
133  int myOrthoFlag { 1 };
134  };
135 
136  void updateComposite(bool free_buffers_if_missing,
137  const SlapcompViewInfo *v = nullptr);
138 
139  /// Returns one of the primary buffers.
140  HUSD_RenderBuffer getAOVBuffer(const UT_StringRef &name) const;
141  /// Returns a buffer even if it is an extra buffer.
142  HUSD_RenderBuffer getAOVBufferIncludingExtra(const UT_StringRef &name) const;
143  void getAOVRasters(const UT_Vector2i &res,
144  const UT_StringHolder &aovpattern,
145  UT_StringArray &aovnames,
147  const TIL_Raster *background_raster = nullptr,
148  int bleft = 0, int bbottom = 0,
149  int bright = 0, int btop = 0,
150  fpreal sx = 0.0, fpreal sy = 0.0,
151  fpreal sw = 1.0, fpreal sh = 1.0) const;
152  bool getAOVBufferInfo(UT_Vector2i &resolution,
153  UT_DimRect &data_window) const;
154 
155  // Fire off a render and block until done. It may return false if the
156  // render delegate fails to initialize, it which case another delegate
157  // should be chosen.
158  bool render(const UT_Matrix4D &view_matrix,
159  const UT_Matrix4D &proj_matrix,
160  const UT_DimRect &viewport_rect,
161  const UT_StringRef &renderer,
162  const UT_Options *render_opts,
163  bool cam_effects,
164  const SlapcompViewInfo *v = nullptr);
165 
166  // Set the camera being viewed through (can be null for no camera);
168  bool camera_synced = true)
169  {
170  if(path != myCameraPath)
171  {
172  myCameraPath = path;
173  mySettingsChanged = true;
174  }
175  // use camera for sampling parms, not frustum.
176  if(camera_synced != myCameraSynced)
177  {
178  myCameraSynced = camera_synced;
179  mySettingsChanged = true;
180  }
181  }
182 
184  { myCompositor = comp; }
185 
186  HUSD_Scene &scene()
187  { return *myScene; }
188  bool isConverged() const
189  { return !isUpdateRunning() && myConverged; }
190  void terminateRender(bool hard_halt = true);
191 
192  bool getBoundingBox(UT_BoundingBox &bbox,
193  const UT_Matrix3R *rot) const;
194 
196  { return myRendererName; }
197  void getRendererCommands(
198  UT_StringArray &command_names,
199  UT_StringArray &command_descriptions) const;
200  void invokeRendererCommand(
201  const UT_StringHolder &command_name) const;
202 
204  RUNNING_UPDATE_NOT_STARTED = 0,
207  RUNNING_UPDATE_FATAL
208  };
209  bool isUpdateRunning() const;
210  bool isUpdateComplete() const;
211 
212  // Control the pause state of the render. Return true if it is paused.
213  // Track pausing invoked by the user separately from "automatic" pausing
214  // which happens when switching between Houdini GL and another renderer.
215  void pauseRender();
216  void resumeRender();
217  bool canPause() const;
218  bool isPausedByUser() const;
219  bool isStoppedByUser() const;
220  bool rendererCreated() const;
221 
222  // Track whether this object should process updates from the stage. Also
223  // controls whether the renderer can be unpaused. We want to prevent the
224  // automatic unpausing of the render when the user explicitly pauses it.
225  bool allowUpdates() const
226  { return myAllowUpdates; }
227  void setAllowUpdates(bool allow_updates)
228  { myAllowUpdates = allow_updates; }
229 
230  static void initializeAvailableRenderers();
231  static bool getAvailableRenderers(HUSD_RendererInfoMap &info_map);
232 
233  void setRenderSettings(const UT_StringRef &settings_path,
234  int w=0, int h=0,
235  fpreal resscale=0.0);
236 
237  const UT_StringArray &rendererPlanes() const { return myPlaneList; }
238  bool setOutputPlane(const UT_StringRef &name);
239  const UT_StringHolder &outputPlane() const { return myOutputPlane; }
240  const UT_StringHolder &currentAov() const { return myCurrentAOV; }
241 
242  void getRenderStats(UT_Options &stats);
243 
244  void setRenderFocus(int x, int y) const;
245  void clearRenderFocus() const;
246 
247  // Apply options to the contained render settings context to control
248  // whether or not the data window from the render settings prim should be
249  // passed to the renderer or overridden (and the full image rendered).
250  // We only pass along the render settings data window if the render
251  // region is deactivated _and_ the data window is activated.
252  void setRenderRegionActive(bool active);
253  void setDataWindowActive(bool active);
254 
255  // Returns the paths associated with render keys from the primid and instid
256  // buffers. Stores the result in myRenderKeyToPathMap so future lookups
257  // are fast.
258 
259  void getPrimPathsFromRenderKeys(
260  const UT_Set<HUSD_RenderKey> &keys,
261  HUSD_RenderKeyPathMap &outkeypathmap);
262 
263  void updateDeferredPrims();
264 
265  /// Set the slapcomp program that should be used when slapcomp is enabled.
267  { mySlapcompProgram = program; }
268  /// Turn slapcomp on or off. setSlapcompProgram() should be called prior to
269  /// this.
270  void enableSlapcomp(bool do_slap);
271  /// Returns true if slapcomp is enabled.
272  bool isSlapcompEnabled() const
273  { return mySlapcompEnabled; }
274  /// If this returns true, then the last attempted application of slapcomp
275  /// may have changed the possible outputs and/or the current display AOV.
276  /// This flag is reset before each application of slapcomp.
277  bool slapcompStateChanged() const
278  { return mySlapcompStateChanged; }
279 
280  /// Returns true if the last run of slapcomp had errors.
281  bool hasSlapcompErrors() const
282  { return mySlapcompEnabled && mySlapcompHasErrors; }
283  /// Slapcomp error reporting. This also activates the mySlapcompHasErrors
284  /// flag.
285  void reportSlapcompError(const char* error);
286  /// Slapcomp warning reporting.
287  void reportSlapcompWarning(const char* error);
288  /// Sets a callback that gets called when reporting errors.
289  void setSlapcompErrorCallback(void (*cb)(void*,
290  const char*),
291  void* data)
292  {
293  mySlapcompErrorCB = cb;
294  mySlapcompErrorCBData = data;
295  }
296  /// Sets a callback that gets called when reporting warnings.
297  void setSlapcompWarningCallback(void (*cb)(void*,
298  const char*),
299  void* data)
300  {
301  mySlapcompWarningCB = cb;
302  mySlapcompWarningCBData = data;
303  }
304 
305  /// Called by our data micro node that monitors for changes to COP textures
306  /// used by karma renders. When this function is called we need to restart
307  /// the render.
308  void handleCopTextureChange(bool time_changed);
309  /// Set a callback that is run at the end of handleCopTextureChange.
310  void setCopTextureChangeCallback(ImagingCallback cb);
311 
312 private:
313  class husd_ImagingPrivate;
314  class husd_IMXRenderBuffer;
315 
316  /// Returns true if the given AOV is an output of slapcomp.
317  bool isSlapcompAOV(const UT_StringHolder& name) const;
318 
319  /// Registers all active AOVs with the slapcomp registry,
320  /// run on terminate or on convergence
321  void registerSlapcompAOVs(bool dostash, const SlapcompViewInfo *view_info);
322 
323  /// Internal helper function to run slapcomp. This function resets both
324  /// mySlapcompStateChanged and mySlapcompHasErrors before doing its work.
325  void runSlapcompIfNeeded(const SlapcompViewInfo* view_info);
326  /// Returns a pointer to HdRenderBuffer. This method fetches an output of
327  /// the specified name (slapcomp layer if it has an output of the given
328  /// name, otherwise the renderer's result).
329  void* getRenderOrSlapcompOutput(const UT_StringHolder& name,
330  husd_IMXRenderBuffer* b)
331  const;
332  /// Builds the slapcomp program and updates the list of available planes. If
333  /// the operation makes any changes to output planes, mySlapcompStateChanged
334  /// flag is activated. Returns true if everything succeeded, false
335  /// otherwise. If force is true, the plane list is reset and built anew;
336  /// otherwise, it's only rebuilt if the program required recompilation.
337  bool buildSlapcompProgramAndUpdatePlanes(bool force);
338 
339  void resetImagingEngine();
340  const HUSD_DataHandle &viewerLopDataHandle() const;
341  bool updateRestartCameraSettings(bool cam_effects) const;
342  bool anyRestartRenderSettingsChanged() const;
343  bool setupRenderer(const UT_StringRef &renderer_name,
344  const UT_Options *render_opts,
345  bool cam_effects);
346  void updateSettingIfRequired(const UT_StringRef &key,
347  const PXR_NS::VtValue &value,
348  bool from_usd_prim = false);
349  void updateSettingsIfRequired(HUSD_AutoReadLock &lock);
350  RunningStatus updateRenderData(const UT_Matrix4D &view_matrix,
351  const UT_Matrix4D &proj_matrix,
352  const UT_DimRect &viewport_rect,
353  bool cam_effects);
354  void gatherCopResolverDependencies();
355  void finishRender(bool do_render);
356 
358  fpreal myFrame;
359  HUSD_DataHandle myDataHandle;
361  HUSD_ConstOverridesPtr myOverrides;
362  HUSD_ConstPostLayersPtr myPostLayers;
363  unsigned myWantsHeadlight : 1,
364  myHasHeadlight : 1,
365  myWantsDomelight : 1,
366  myHasDomelight : 1,
367  myDoLighting : 1,
368  myDoMaterials : 1,
369  myConverged : 1,
370  mySettingsChanged : 1,
371  myCameraSynced : 1,
372  myValidRenderSettingsPrim : 1,
373  myAOVsStashed : 1;
374  bool myIsPaused;
375  bool myAllowUpdates;
376  HUSD_Scene *myScene;
377  UT_StringHolder myRendererName;
378  HUSD_Compositor *myCompositor;
379  ImagingCallback myPostRenderCallback;
380  ImagingCallback myCopTextureChangeCallback;
381  UT_Options myCurrentDisplayOptions;
382  SYS_AtomicInt32 myRunningInBackground;
383  UT_StringHolder myOutputPlane;
384  UT_StringHolder myCurrentAOV;
385  UT_StringHolder myCameraPath;
387  UT_UniquePtr<husd_DefaultRenderSettingContext> myRenderSettingsContext;
388  int myConformPolicy;
389  BufferSet myLastCompositedBufferSet;
390  UT_Map<HUSD_RenderKey, UT_StringHolder> myRenderKeyToPathMap;
391  fpreal myHeadlightIntensity;
392 
393  /// Track whether we are currently handling a COP texture change (we want
394  /// to avoid getting into a recursive loop of such calls, which should be
395  /// impossible, but better safe than sorry).
396  bool myHandlingCopTextureChange;
397 
398  /// The list of all planes that this object knows about. This is a union of
399  /// renderer's and slapcomp's plane, with duplicates removed (as slapcomp's
400  /// results take precedence over the renderer's).
401  UT_StringArray myPlaneList;
402  /// The list of planes that are outputs of the renderer.
403  UT_StringArray myRendererPlaneList;
404  /// The list of planes that are outputs of slapcomp.
405  UT_StringArray mySlapcompPlaneList;
406 
407  /// The slapcomp program.
408  COP_ApexProgram *mySlapcompProgram;
409  /// Callback function to report errors to the outside world.
410  void (*mySlapcompErrorCB)(void*, const char*);
411  /// Data to send to the error reporting callback.
412  void* mySlapcompErrorCBData;
413  /// Callback function to report warnings to the outside world.
414  void (*mySlapcompWarningCB)(void*,
415  const char*);
416  /// Data to send to the warning reporting callback.
417  void* mySlapcompWarningCBData;
418  /// Enable state of slapcomp.
419  bool mySlapcompEnabled;
420  /// If this flag is true, last application of slapcomp incurred a rebuild of
421  /// the program that may have changed the available output layers or the
422  /// current one.
423  bool mySlapcompStateChanged;
424  /// If this flag is true, last application of slapcomp had errors and
425  /// failed.
426  bool mySlapcompHasErrors;
427 
428  /// Tracks last view info so we can update on terminate.
429  SlapcompViewInfo myLastSlapcompViewInfo;
430 };
431 
432 #endif
433 
bool isConverged() const
Definition: HUSD_Imaging.h:188
Definition: UT_Set.h:58
void setSlapcompErrorCallback(void(*cb)(void *, const char *), void *data)
Sets a callback that gets called when reporting errors.
Definition: HUSD_Imaging.h:289
const UT_StringHolder & currentAov() const
Definition: HUSD_Imaging.h:240
void
Definition: png.h:1083
GLboolean * data
Definition: glcorearb.h:131
void setAOVCompositor(HUSD_Compositor *comp)
Definition: HUSD_Imaging.h:183
const GLdouble * v
Definition: glcorearb.h:837
XUSD_RenderSettings contains the HdRenderSettings for the render.
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
GA_API const UT_StringHolder rot
#define HUSD_API
Definition: HUSD_API.h:32
GLint y
Definition: glcorearb.h:103
void setSlapcompWarningCallback(void(*cb)(void *, const char *), void *data)
Sets a callback that gets called when reporting warnings.
Definition: HUSD_Imaging.h:297
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
< returns > If no error
Definition: snippets.dox:2
const UT_StringHolder & rendererName() const
Definition: HUSD_Imaging.h:195
const UT_StringHolder & outputPlane() const
Definition: HUSD_Imaging.h:239
Simple interface around HdRenderBuffer.
bool isSlapcompEnabled() const
Returns true if slapcomp is enabled.
Definition: HUSD_Imaging.h:272
bool slapcompStateChanged() const
Definition: HUSD_Imaging.h:277
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
void setSlapcompProgram(COP_ApexProgram *program)
Set the slapcomp program that should be used when slapcomp is enabled.
Definition: HUSD_Imaging.h:266
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:186
GLenum mode
Definition: glcorearb.h:99
void setCameraPath(const UT_StringRef &path, bool camera_synced=true)
Definition: HUSD_Imaging.h:167
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:237
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
SIM_API const UT_StringHolder force
fpreal64 fpreal
Definition: SYS_Types.h:277
HUSD_AspectConformPolicy
Definition: HUSD_Utils.h:106
void setAllowUpdates(bool allow_updates)
Definition: HUSD_Imaging.h:227
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool hasSlapcompErrors() const
Returns true if the last run of slapcomp had errors.
Definition: HUSD_Imaging.h:281
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
Definition: core.h:1131
UT_Function< void(HUSD_Imaging *imaging)> ImagingCallback
Definition: HUSD_Imaging.h:58
Definition: value.h:164
GLbitfield GLuint program
Definition: glcorearb.h:1931
bool allowUpdates() const
Definition: HUSD_Imaging.h:225
Definition: format.h:895