HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XUSD_RenderSettings.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  * NAME: XUSD_RenderSettings.h (karma Library, C++)
17  *
18  * COMMENTS:
19  */
20 
21 #ifndef __XUSD_RenderSettings__
22 #define __XUSD_RenderSettings__
23 
24 #include "HUSD_API.h"
25 #include "HUSD_Utils.h"
26 #include <PXL/PXL_Common.h>
27 #include <UT/UT_Array.h>
28 #include <UT/UT_NonCopyable.h>
29 #include <UT/UT_Rect.h>
30 #include <UT/UT_StringHolder.h>
31 #include <UT/UT_UniquePtr.h>
32 #include <SYS/SYS_Types.h>
33 #include <pxr/pxr.h>
34 #include <pxr/imaging/hd/aov.h>
38 #include <pxr/usd/usdRender/var.h>
39 
40 #include <string>
41 
42 class UT_JSONWriter;
43 
45 
46 class XUSD_RenderProduct;
48 
50  : public UT_NonCopyable
51 {
52 public:
54  virtual ~XUSD_RenderSettingsContext();
55 
56  /// Update any settings from the render settings primitive. This allows
57  /// the context to look at custom attributes on the RenderSettings.
58  ///
59  /// This function will always be called - even if there are no settings.
60  virtual void initFromUSD(UsdRenderSettings &settings) { }
61 
62  /// Override the path to the camera
63  virtual SdfPath overrideCamera() const
64  {
65  return SdfPath();
66  }
67 
68  /// Return the default resolution for rendering products
69  virtual GfVec2i defaultResolution() const = 0;
70 
71  /// Optionally override the resolution of the product
72  virtual GfVec2i overrideResolution(const GfVec2i &res) const
73  {
74  return res;
75  }
76 
77  /// Optionally, override the pixel aspect ratio.
78  virtual fpreal overridePixelAspect(fpreal pa) const { return pa; }
79 
80  /// Optionally, override the data window
81  virtual GfVec4f overrideDataWindow(const GfVec4f &w) const { return w; }
82 
83  /// Optionally, override the disableMotionBlur
84  virtual bool overrideDisableMotionBlur(bool v) const { return v; }
85 
86  /// Return if there's an overridden purpose for the render
87  virtual const char *overridePurpose() const { return nullptr; }
88 
89  /// Return the default purpose (this is a comma separated list)
90  virtual const char *defaultPurpose() const
91  {
92  const char *p = overridePurpose();
93  if (!p)
94  p = "geometry,render";
95  return p;
96  }
97 
98  /// Start frame for a render sequence
99  virtual fpreal startFrame() const = 0;
100 
101  /// Frame increment, when computing sequences
102  virtual fpreal frameInc() const { return 1; }
103 
104  /// Return the number of frames being rendered
105  virtual int frameCount() const { return 1; }
106 
107  /// Start frame for a render sequence
108  virtual const std::vector<fpreal> *frameList() const { return nullptr; }
109 
110  /// Return the fps
111  virtual fpreal fps() const { return 24; }
112 
113  /// Current frame in the render sequence
114  virtual UsdTimeCode evalTime() const = 0;
115 
116  /// Get a default rendering descriptor for a given AOV
117  virtual HdAovDescriptor defaultAovDescriptor(const TfToken &aov) const
118  {
119  return HdAovDescriptor();
120  }
121 
122  /// Default product name
123  virtual const char *defaultProductName() const { return nullptr; }
124 
125  /// Return a product name override
126  virtual const char *overrideProductName(const XUSD_RenderProduct &p,
127  int pidx) const
128  {
129  return nullptr;
130  }
131 
132  /// Optionally, override the path to the snapshots
133  virtual const char *overrideSnapshotPath(const XUSD_RenderProduct &p,
134  int pidx) const
135  {
136  return nullptr;
137  }
138  /// Optionally, override the suffix on snapshots
139  virtual const char *overrideSnapshotSuffix(const XUSD_RenderProduct &p,
140  int pidx) const
141  {
142  return "_part";
143  }
144 
145  /// When rendering with tiles, adjust an product filename for a unique name
146  /// for the given tile.
147  virtual UT_StringHolder addTileSuffix(const UT_StringHolder &fname) const
148  {
149  return fname;
150  }
151 
152  /// Build initial render settings map
153  virtual void setDefaultSettings(const XUSD_RenderSettings &rset,
154  HdRenderSettingsMap &settings) const
155  { }
156 
157  /// After the products have been loaded, apply any overrides
158  virtual void overrideSettings(const XUSD_RenderSettings &rset,
159  HdRenderSettingsMap &settings) const
160  { }
161 
162  /// Allow render options to be applied without a camera present.
163  virtual bool allowCameraless() const { return false; }
164 };
165 
167  : public UT_NonCopyable
168 {
169 public:
170  XUSD_RenderVar();
171  virtual ~XUSD_RenderVar();
172 
173  bool loadFrom(const UsdRenderVar &prim,
174  const XUSD_RenderSettingsContext &ctx);
175  bool resolveFrom(const UsdRenderVar &prim,
176  const XUSD_RenderSettingsContext &ctx);
177  bool buildDefault(const XUSD_RenderSettingsContext &ctx);
178 
179  virtual UT_UniquePtr<XUSD_RenderVar> clone() const;
180 
181  const std::string &aovName() const { return myAovName; }
182  const TfToken &aovToken() const { return myAovToken; }
183  const TfToken &dataType() const;
184  const std::string &sourceName() const;
185  const TfToken &sourceType() const;
186 
187  const HdAovDescriptor &desc() const { return myHdDesc; }
188  PXL_DataFormat pxlFormat() const { return myDataFormat; }
189  PXL_Packing pxlPacking() const { return myPacking; }
190 
191  /// Print out the settings
192  void dump(UT_JSONWriter &w) const;
193 
194 protected:
196  std::string myAovName;
200 };
201 
203  : public UT_NonCopyable
204 {
205 public:
208 
210  virtual ~XUSD_RenderProduct();
211 
212  bool loadFrom(const UsdStageRefPtr &usd,
213  const UsdRenderProduct &prim,
214  const XUSD_RenderSettingsContext &ctx);
215  bool resolveFrom(const UsdStageRefPtr &usd,
216  const UsdRenderProduct &prim,
217  const XUSD_RenderSettingsContext &ctx);
218  bool buildDefault(const XUSD_RenderSettingsContext &ctx);
219  bool buildDummyRaster(const XUSD_RenderSettingsContext &ctx,
220  const XUSD_RenderProduct &src);
221 
222  void updateSettings(const UsdStageRefPtr &use,
223  const UsdRenderProduct &prim,
224  const XUSD_RenderSettingsContext &ctx);
225 
226  const TfToken &productType() const;
227  TfToken productName(int frame = 0) const;
228 
229  bool isRaster() const;
230 
231  // Current output filename (with all variables expanded)
232  const UT_StringHolder &outputName() const { return myFilename; }
233 
234  // Full filename (without tile suffix)
235  const UT_StringHolder &fullOutputName() const { return myFullFilename; }
236 
237  const RenderVarList &vars() const { return myVars; }
238 
239  /// Create a "partial" filename by taking the original filename and
240  /// inserting the @c extra string before the file suffix. If the @c path
241  /// is not null, the path will be replaced.
242  static UT_StringHolder insertExtraBeforeExtension(
243  const UT_StringHolder &filename,
244  const char *extra,
245  const char *path = nullptr);
246 
247  /// @{
248  /// Properties that can override settings defined on a render settings
249  /// primitive The functions return true if they are authored on the
250  /// product.
251  template <typename T>
253  {
254  void clear() { myAuthored = false; }
255  bool import(T &val) const
256  {
257  if (myAuthored)
258  {
259  val = myValue;
260  return true;
261  }
262  return false;
263  }
265  bool myAuthored = false;
266  };
267  bool cameraPath(SdfPath &val) const
268  {
269  if (myCameraPath.IsEmpty())
270  return false;
271  val = myCameraPath;
272  return true;
273  }
274  bool shutter(GfVec2d &val) const { return myShutter.import(val); }
275  bool res(GfVec2i &val) const { return myRes.import(val); }
276  bool pixelAspect(float &val) const { return myPixelAspect.import(val); }
277  bool dataWindow(GfVec4f &val) const { return myDataWindowF.import(val); }
278  bool disableMotionBlur(bool &val) const;
279  bool disableDepthOfField(bool &val) const;
280  /// @}
281 
282  /// @{
283  /// Test whether the product list has a specific value for the given
284  /// attribute. If so, overwrite the value with the value of the product
285  /// list. Note, all products must author the attribute and have the same
286  /// value.
288  static bool specificRes(GfVec2i &val, const ProductList &products);
289  static bool specificPixelAspect(float &val, const ProductList &products);
290  static bool specificDataWindow(GfVec4f &val, const ProductList &products);
291  static bool specificDisableMotionBlur(bool &val, const ProductList &products);
292  static bool specificDisableDepthOfField(bool &val, const ProductList &products);
293  /// @}
294 
295  const SdfPath &cameraPath() const { return myCameraPath; }
296 
297  const_iterator begin() const { return myVars.begin(); }
298  const_iterator end() const { return myVars.end(); }
299 
300  /// Expand product name variables. Returns false if there are multiple
301  /// frames, but no frame expansion. The @c product_index is -1 for
302  /// non-raster products or the offset into the list of raster products.
303  bool expandProduct(const XUSD_RenderSettingsContext &opts,
304  int product_index, int frame,
305  UT_Set<UT_StringHolder> &other_products);
306  bool collectAovs(TfTokenVector &aovs,
307  HdAovDescriptorList &descs) const;
308 
309  bool collectImageFilterLists(const UsdStageRefPtr &usd,
310  UT_StringArray &paths,
311  UT_Array<UsdPrim> &prims) const;
312 
313  /// User settings for this product
314  const HdAovSettingsMap &settings() const { return mySettings; }
315 
316  /// Print out the settings
317  void dump(UT_JSONWriter &w) const;
318 
319  bool isDefault() const { return myIsDefault; }
320  void setIsDefault() { myIsDefault = true; }
321 
322 protected:
323  /// If you have a sub-class of XUSD_RenderVar, you can create it here
325  {
326  return UTmakeUnique<XUSD_RenderVar>();
327  }
328 
329  // Member data
336 
337  // Override values
346 
347 };
348 
349 /// XUSD_RenderSettings contains the HdRenderSettings for the render
351  : public UT_NonCopyable
352 {
353 public:
358 
360  virtual ~XUSD_RenderSettings();
361 
362  static void findCameras(UT_Array<SdfPath> &list, UsdPrim prim);
363 
364  /// Since the settings primitive may specify values used by the render
365  /// settings context (like frame count, etc.) we pass in a
366  /// non-const @c context so the initialization process so we can call
367  /// initFromUSD() once we've found the render settings.
368  bool init(const UsdStageRefPtr &usd,
369  const SdfPath &settings_path,
371  /// Alternative initialization with a string path
372  bool init(const UsdStageRefPtr &usd,
373  const UT_StringHolder &settings_path,
375 
376  /// Update the frame
377  bool updateFrame(const UsdStageRefPtr &usd,
379  HUSD_CustomProductAction custom_product_action);
380 
381  /// Resolve products/vars
382  bool resolveProducts(const UsdStageRefPtr &usd,
383  const XUSD_RenderSettingsContext &ctx,
384  HUSD_CustomProductAction custom_product_action);
385 
386  /// Get the render settings
387  UsdPrim prim() const { return myUsdSettings.GetPrim(); }
388 
389  /// Properties from the render settings which cannot be overridden per
390  /// product.
391  const VtArray<TfToken> &purpose() const { return myPurpose; }
392 
393  /// Properties which a render product might override
394  SdfPath cameraPath(const XUSD_RenderProduct *p) const;
395  double shutterOpen(const XUSD_RenderProduct *p) const;
396  double shutterClose(const XUSD_RenderProduct *p) const;
397  int xres(const XUSD_RenderProduct *p) const;
398  int yres(const XUSD_RenderProduct *p) const;
399  GfVec2i res(const XUSD_RenderProduct *p) const;
400  float pixelAspect(const XUSD_RenderProduct *p) const;
401  GfVec4f dataWindowF(const XUSD_RenderProduct *p) const;
402  UT_DimRect dataWindow(const XUSD_RenderProduct *p) const;
403  bool disableMotionBlur(const XUSD_RenderProduct *p) const;
404  bool disableDepthOfField(const XUSD_RenderProduct *p) const;
405  UT_StringHolder outputName(int product_group) const;
406 
407  const HdRenderSettingsMap &renderSettings() const { return mySettings; }
408 
409  /// @{
410  /// Render Products
411  const ProductGroupList &productGroups() const { return myProductGroups; }
412  const ProductList &products() const { return myProducts; }
413  const_iterator begin() const { return myProducts.begin(); }
414  const_iterator end() const { return myProducts.end(); }
415  /// @}
416 
417  /// Expand product name variables
418  bool expandProducts(const XUSD_RenderSettingsContext &ctx,
419  int frame,
420  int product_group,
421  bool delegate_products);
422 
423  /// Print out the settings to UT_ErrorLog
424  void printSettings() const;
425  void dump() const; // Dump for a debug build
426  void dump(UT_JSONWriter &w) const;
427 
428  bool collectAovs(TfTokenVector &aovs,
429  HUSD_CustomProductAction custom_product_action,
430  HdAovDescriptorList &descs) const;
431 
432  bool collectImageFilterLists(const UsdStageRefPtr &usd,
433  UT_StringArray &paths,
434  UT_Array<UsdPrim> &prims,
435  bool collect_products) const;
436 
437 
438  static HUSD_AspectConformPolicy conformPolicy(const TfToken &t);
439  static const TfToken &conformPolicy(HUSD_AspectConformPolicy policy);
440 
441  // Helper function for calculating a UT_DimRect consistently based on
442  // floating point reoslution and datawindow fractional values.
443  static UT_InclusiveRect computeDataWindow(const GfVec2i &res,
444  const GfVec4f &win);
445 
446  /// When the camera aspect ratio doesn't match the image aspect ratio, USD
447  /// specifies five different approatches to resolving this difference.
448  /// HoudiniGL and Karma only use the vertical aperture and thus have a
449  /// fixed way to resolve aspect ratio differences. This method will adjust
450  /// the vertical aspect or pixel aspect ratio to fit with the five
451  /// different methods described in USD. The method returns true if values
452  /// were changed. The method is templated on single/double precision
453  template <typename T>
454  static bool aspectConform(HUSD_AspectConformPolicy conform,
455  T &vaperture, T &pixel_aspect,
456  T cam_aspect, T img_aspect);
457 
458  /// This method assumes you have render settings defined
459  template <typename T>
460  bool aspectConform(const XUSD_RenderSettingsContext &ctx,
461  T &vaperture, T &pixel_aspect,
462  T cam_aspect, T img_aspect) const;
463 
464  HUSD_AspectConformPolicy conformPolicy(
465  const XUSD_RenderSettingsContext &c) const;
466 
467  // Return a VtValue for all non-raster render products for the delegate
468  // render product interface.
469  VtValue delegateRenderProducts(int product_group) const
470  { return renderProducts(product_group, false); }
471 
472  // Return a VtValue for the raster render products.
473  VtValue rasterRenderProducts(int product_group) const
474  { return renderProducts(product_group, true); }
475 
476  virtual bool supportedDelegate(const TfToken &name) const;
477 
478  /// Check if the karma random seed was authored
479  bool getKarmaRandomSeed(int64 &seed,
480  const XUSD_RenderSettingsContext &ctx) const;
481 
482  /// Given a set of AOV names/descriptors, split them into two mutially
483  /// exclusive sets. The set of all AOVs which generate a color buffer, and
484  /// the set of AOVs which don't (e.g. depth, normal or velocity).
485  static void splitAOVs(const UT_StringMap<HdAovDescriptor> &aovs,
486  UT_StringSet &color_aovs,
487  UT_StringSet &non_color_aovs);
488  /// Update HoudiniImageFilterList
489  static bool updateHoudiniImageFilterList(
490  const UsdStageRefPtr &stage,
491  const SdfPath &usdprim,
492  const UT_StringHolder &coppath,
493  fpreal frame,
494  const UT_StringSet &color_aov_set,
495  const UT_StringSet &non_color_aov_set);
496 
497  static SYS_HashType hashHoudiniImageFilterList(const UsdPrim &prim,
498  fpreal frame);
499 
500 protected:
502  { return UTmakeUnique<XUSD_RenderProduct>(); }
503 
504  void partitionProducts();
505  void computeImageWindows(const UsdStageRefPtr &usd,
506  const XUSD_RenderSettingsContext &ctx);
507  void setDefaults(const UsdStageRefPtr &usd,
508  const XUSD_RenderSettingsContext &ctx);
509  bool loadFromPrim(const UsdStageRefPtr &usd,
510  const XUSD_RenderSettingsContext &ctx);
511  bool loadFromOptions(const UsdStageRefPtr &usd,
512  const XUSD_RenderSettingsContext &ctx);
513  void buildRenderSettings(const UsdStageRefPtr &usd,
514  const XUSD_RenderSettingsContext &ctx);
515  bool isDefaultProduct() const
516  { return myProducts.size() >= 1 && myProducts[0]->isDefault(); }
517  /// Check to see whether there have been any unexpected products added (as
518  /// in the mplay monitor or the dummy raster product). Returns true if the
519  /// path list size matches.
520  bool accountForExtraProducts(const SdfPathVector &paths) const;
521 
522  VtValue renderProducts(int product_group, bool raster) const;
523 
540 };
541 
543 
544 #endif
GT_API const UT_StringHolder filename
const_iterator end() const
Definition: vec2i.h:43
const RenderVarList & vars() const
SettingOverride< bool > myDisableMotionBlur
virtual SdfPath overrideCamera() const
Override the path to the camera.
UsdRenderSettings myUsdSettings
PXL_DataFormat pxlFormat() const
const VtArray< TfToken > & purpose() const
const GLdouble * v
Definition: glcorearb.h:837
XUSD_RenderSettings contains the HdRenderSettings for the render.
virtual void setDefaultSettings(const XUSD_RenderSettings &rset, HdRenderSettingsMap &settings) const
Build initial render settings map.
UT_StringHolder myFullFilename
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
virtual const char * overrideSnapshotPath(const XUSD_RenderProduct &p, int pidx) const
Optionally, override the path to the snapshots.
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
bool cameraPath(SdfPath &val) const
ProductGroupList myProductGroups
int64 exint
Definition: SYS_Types.h:125
#define HUSD_API
Definition: HUSD_API.h:31
virtual GfVec2i overrideResolution(const GfVec2i &res) const
Optionally override the resolution of the product.
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
bool pixelAspect(float &val) const
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:39
const SdfPath & cameraPath() const
const UT_StringHolder & outputName() const
const_iterator begin() const
SettingOverride< GfVec4f > myDataWindowF
Definition: vec2d.h:45
virtual const char * defaultPurpose() const
Return the default purpose (this is a comma separated list)
virtual bool overrideDisableMotionBlur(bool v) const
Optionally, override the disableMotionBlur.
SettingOverride< GfVec2d > myShutter
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
const TfToken & aovToken() const
virtual const char * overrideProductName(const XUSD_RenderProduct &p, int pidx) const
Return a product name override.
virtual UT_UniquePtr< XUSD_RenderVar > newRenderVar() const
If you have a sub-class of XUSD_RenderVar, you can create it here.
virtual UT_StringHolder addTileSuffix(const UT_StringHolder &fname) const
virtual void overrideSettings(const XUSD_RenderSettings &rset, HdRenderSettingsMap &settings) const
After the products have been loaded, apply any overrides.
virtual const char * defaultProductName() const
Default product name.
Definition: token.h:70
PXL_DataFormat myDataFormat
const_iterator begin() const
std::vector< class SdfPath > SdfPathVector
PXR_NAMESPACE_OPEN_SCOPE typedef TfHashMap< TfToken, VtValue, TfToken::HashFunctor > HdAovSettingsMap
Definition: aov.h:21
bool isDefaultProduct() const
HdAovSettingsMap mySettings
const HdAovDescriptor & desc() const
virtual HdAovDescriptor defaultAovDescriptor(const TfToken &aov) const
Get a default rendering descriptor for a given AOV.
SettingOverride< bool > myDisableDepthOfField
const_iterator end() const
const HdAovSettingsMap & settings() const
User settings for this product.
UT_StringHolder myFilename
const HdRenderSettingsMap & renderSettings() const
VtValue rasterRenderProducts(int product_group) const
PXL_Packing
Definition: PXL_Common.h:32
HUSD_API const char * raster()
long long int64
Definition: SYS_Types.h:116
Definition: prim.h:116
std::vector< HdAovDescriptor > HdAovDescriptorList
Definition: aov.h:62
virtual const char * overrideSnapshotSuffix(const XUSD_RenderProduct &p, int pidx) const
Optionally, override the suffix on snapshots.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
GLuint const GLchar * name
Definition: glcorearb.h:786
PXL_DataFormat
Definition: PXL_Common.h:20
virtual GfVec4f overrideDataWindow(const GfVec4f &w) const
Optionally, override the data window.
Definition: path.h:280
HdAovDescriptor myHdDesc
virtual bool allowCameraless() const
Allow render options to be applied without a camera present.
const ProductList & products() const
GLdouble t
Definition: glad.h:2397
bool res(GfVec2i &val) const
VtValue delegateRenderProducts(int product_group) const
PXL_Packing pxlPacking() const
VtArray< TfToken > myPurpose
Definition: vec4f.h:45
const ProductGroupList & productGroups() const
UT_StringHolder myPartname
SettingOverride< GfVec2i > myRes
bool dataWindow(GfVec4f &val) const
UsdPrim GetPrim() const
Definition: prim.h:2806
const std::string & aovName() const
virtual void initFromUSD(UsdRenderSettings &settings)
fpreal64 fpreal
Definition: SYS_Types.h:283
virtual const std::vector< fpreal > * frameList() const
Start frame for a render sequence.
virtual const char * overridePurpose() const
Return if there's an overridden purpose for the render.
HUSD_AspectConformPolicy
Definition: HUSD_Utils.h:111
virtual UT_UniquePtr< XUSD_RenderProduct > newRenderProduct() const
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HdRenderSettingsMap mySettings
virtual fpreal frameInc() const
Frame increment, when computing sequences.
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
HUSD_API const char * productType()
const UT_StringHolder & fullOutputName() const
SettingOverride< bool > myInstantaneousShutter
UsdPrim prim() const
Get the render settings.
SettingOverride< float > myPixelAspect
virtual fpreal fps() const
Return the fps.
HUSD_API const char * dataType()
Definition: value.h:89
HUSD_API const char * productName()
bool shutter(GfVec2d &val) const
virtual fpreal overridePixelAspect(fpreal pa) const
Optionally, override the pixel aspect ratio.
virtual int frameCount() const
Return the number of frames being rendered.
HUSD_API const char * disableMotionBlur()
GLenum src
Definition: glcorearb.h:1793
HUSD_CustomProductAction
Definition: HUSD_Utils.h:128