HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primWrapper.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 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 __GUSD_PRIMWRAPPER_H__
25 #define __GUSD_PRIMWRAPPER_H__
26 
27 #include <GT/GT_Primitive.h>
29 #include <UT/UT_Function.h>
30 
31 #include "gusd/api.h"
32 
33 #include "pxr/pxr.h"
34 #include "pxr/usd/usd/prim.h"
35 #include "pxr/usd/usd/timeCode.h"
37 
38 #include "GT_Utils.h"
39 #include "purpose.h"
40 
41 class GU_DetailHandle;
42 class GU_PackedImpl;
43 
45 
46 class GusdGT_AttrFilter;
47 class UsdGeomXformCache;
48 class GusdContext;
49 
50 /// \class GusdPrimWrapper
51 /// \brief A GT_Primitive that wraps a USD Prim.
52 ///
53 /// A GusdPrimWrapper is responsible for copying attribute data between
54 /// USD and GT.
55 ///
56 /// To write USD geometry, the following steps are taken:
57 ///
58 /// The ROP uses GusdRefiner to refine the cooked geometry to GT primitive types
59 /// that have a matching USD type.
60 ///
61 /// For each GT primitive we create a primWrapper by calling the defineForWrite
62 /// method. This will create a usd prim on the current stage.
63 ///
64 /// On each frame updateFromGTPrim is called to copy attribtutes from the
65 /// GT prim to the USD prim.
66 ///
67 /// We support:
68 /// Writing a sequence of frames from one process.
69 /// Writing each frame of a sequence to a seperate file from a seperate process.
70 /// Writing each frame of a sequence to a seperate file from one process.
71 ///
72 /// When writing all frames to a single file, we try and compress attribtute values.
73 /// The data we need to do this compression is kept in the prim wrapper.
74 ///
75 /// In the rare case where we want to sequentially write a sequence to per frame
76 /// files, we need the primWrapper to persist across the sequence so we can do
77 /// the attribute compression. However, we need to create the USD prim on each
78 /// per frame file. The "redefine" method is used for this.
79 ///
80 /// To read USD geometry we start with a GusdGU_PackedUSD prim. A GusdGT_PrimCollect
81 /// object has been registered to convert these prims to GT_Primitives for drawing
82 /// in the view port. This object will call the "fullGT" method of the GU prim
83 /// which in turn calls the "defineForRead" to create a GusdPrimWrapper. These
84 /// prims can be refined into native GT_Primitives that the viewport can draw.
85 
86 
87 typedef std::map<SdfPath,UT_Matrix4D> GusdSimpleXformCache;
88 
90 {
91 public:
92 
94  (const GT_PrimitiveHandle&, /* sourcePrim */
95  const UsdStagePtr&,
96  const SdfPath& /* path */,
97  const GusdContext&)>
99 
101  (const UsdGeomImageable&,
102  UsdTimeCode,
105 
106  typedef UT_Function<bool
107  (const GT_PrimitiveHandle&,
108  std::string &primName)>
110 
112  ( const GT_DataArrayHandle & )>
114 
115  /// \brief Given a GT_Primitive, create a USD prim of the proper type.
116  ///
117  /// When writing a USD file, we refine the geometry to a set of prims that we
118  /// can deal with then we call this method on each of those prims.
119  static GT_PrimitiveHandle
120  defineForWrite( const GT_PrimitiveHandle& sourcePrim,
121  const UsdStagePtr& stage,
122  const SdfPath& path,
123  const GusdContext& ctxt);
124 
125  /// If prim type can generate a useful name for a prim, sets primName
126  /// and returns true.
127  /// So far only F3D volumes do this. They can derive a name from meta
128  /// data stored in the f3d file.
129  static bool
130  getPrimName( const GT_PrimitiveHandle &sourcePrim,
131  std::string &primName );
132 
133  // When we write USD for the given type, we will use a name like $USDNAME_0.
134  // where USDNAME is the name registered for this type
135  static const char*
136  getUsdName( int gtPrimId );
137 
138  // When we USD for an object that is marked as a group type, we write
139  // the object and then all its children.
140  static bool
141  isGroupType( int gtPrimId );
142 
143  /// \brief Given a USD prim, create a GusdPrimWrapper of the proper type.
144  ///
145  /// When reading a USD file, we call this function to create a Gusd_GTPrimitive
146  /// for each USD prim, we then refine that to something that can be
147  /// used in a detail.
148  static GT_PrimitiveHandle
149  defineForRead( const UsdGeomImageable& sourcePrim,
151  GusdPurposeSet purposes );
152 
153  /// \brief Is this gt prim a point instancer?
154  ///
155  /// This is used to know if we need to write the instance prototypes.
156  static bool
157  isPointInstancerPrim(const GT_PrimitiveHandle& prim,
158  const GusdContext& ctxt);
159 
160  /// Register function for creating new USD prims from GT_Primitives and, optionally,
161  /// a function for giving these prims a name.
162  static bool registerPrimDefinitionFuncForWrite(int gtPrimId,
164  GetPrimNameFunction getNameFunction = NULL,
165  bool isGroupType = false,
166  const char* usdName = NULL );
167 
168  /// Register function for creating new GusdPrimWrappers from USD prim.
169  static bool registerPrimDefinitionFuncForRead(const TfToken& usdTypeName,
170  DefinitionForReadFunction function);
171 
172  /// Return true is the give prim can be supported directly in USD. This
173  /// is used by the refiner to know when to stop refining.
174  static bool isGTPrimSupported(const GT_PrimitiveHandle& prim);
175 
176  GusdPrimWrapper();
177  GusdPrimWrapper( const UsdTimeCode &time, const GusdPurposeSet &purposes );
178  GusdPrimWrapper( const GusdPrimWrapper &in );
179  ~GusdPrimWrapper() override;
180 
181  /// Return true if the underlying USD prim is valid
182  virtual bool isValid() const;
183 
184  virtual const UsdGeomImageable getUsdPrim() const = 0;
185 
186  virtual bool unpack(
187  UT_Array<GU_DetailHandle> &details,
188  const UT_StringRef& fileName,
189  const SdfPath& primPath,
190  const UT_Matrix4D* xform,
191  fpreal frame,
192  const char * viewportLod,
193  GusdPurposeSet purposes,
194  const GT_RefineParms &rparms) const;
195 
196  /// \brief Create a new USD prim to match GT primitive.
197  ///
198  /// When writing per frame USD files, we need to recreate the stage
199  /// and all the primitives on it each frame. However, there is some
200  /// data we want to persist across frames. So we keep the GusdPrimWrappers
201  /// and ask them to redefine their USD prims on each frame.
202  virtual bool redefine(
203  const UsdStagePtr& stage,
204  const SdfPath& path,
205  const GusdContext& ctxt,
206  const GT_PrimitiveHandle& sourcePrim );
207 
208  /// Fill a USD prim's attribute samples for a frame from the
209  /// attributes in a GT primitive.
210  ///
211  /// If \p sourcePrim is an instance, \p localXform is the instance transform
212  /// otherwise it is the primitive transform from the prim.
213  virtual bool updateFromGTPrim(
214  const GT_PrimitiveHandle& sourcePrim,
215  const UT_Matrix4D& houXform,
216  const GusdContext& ctxt,
217  GusdSimpleXformCache& xformCache );
218 
219  /// Add a sample just before the current time that invises this prim.
220  /// For points and instances this means writing a empty point attribute.
221  /// Other prims set their visibility flag.
222  /// It might be possible to avoid this if we are on the first frame.
223  virtual void addLeadingBookend( double curFrame, double startFrame );
224 
225  /// Add a sample at the current frame, invising this from.
226  virtual void addTrailingBookend( double curFrame );
227 
228  /// Keep track of the visibility state of the prim for book marks.
229  void markVisible( bool in ) { m_visible = in; }
230  bool isVisible() const { return m_visible; }
231 
232  virtual void setVisibility(const TfToken& visibility, UsdTimeCode time);
233 
234  static GT_DataArrayHandle convertPrimvarData(
235  const UsdGeomPrimvar& primvar,
236  UsdTimeCode time );
237 
238  static GT_DataArrayHandle convertAttributeData(
239  const UsdAttribute& attr,
240  const VtValue& val);
241 
242  /// Import geometry subsets as either partition attributes or primitive
243  /// groups.
244  static void loadSubsets(const UsdGeomImageable &prim,
245  GT_FaceSetMapPtr &facesets,
246  GT_AttributeListHandle &uniform_attribs,
247  const GT_RefineParms *parms,
248  const int numFaces,
249  UsdTimeCode time);
250 
251  /// Load primvars for prim from USD.
252  /// remapIndicies is used to expand curve primvars into point attributes if
253  /// needed.
254  void loadPrimvars(
255  const UsdPrimDefinition& prim_defn,
257  const GT_RefineParms* rparms,
258  int minUniform,
259  int minPoint,
260  int minVertex,
261  const std::string& primPath,
263  GT_AttributeListHandle* point,
264  GT_AttributeListHandle* primitive,
265  GT_AttributeListHandle* constant,
266  const GT_DataArrayHandle& remapIndicies = GT_DataArrayHandle() ) const;
267 
268  // Map to translate from GT_Owner enums to USD interpolation type tokens
269  static std::map<GT_Owner, TfToken> s_ownerToUsdInterp;
270  static std::map<GT_Owner, TfToken> s_ownerToUsdInterpCurve;
271 
272 protected:
273 
274  /// Look for "visible" attribute on sourcePrim. If it doesn't exist
275  /// set a visibility sample based on isVisible()
276  void updateVisibilityFromGTPrim( const GT_PrimitiveHandle& sourcePrim,
277  UsdTimeCode time,
278  bool forceWrite = true );
279 
280  /// Look for a "usdactive" attribute on sourcePrim. UsdPrim::SetActive
281  /// based on this value. If attribute doesn't exist, do nothing.
282  void updateActiveFromGTPrim( const GT_PrimitiveHandle& sourcePrim,
283  UsdTimeCode time );
284 
285  void updateTransformFromGTPrim( const GfMatrix4d &xform, UsdTimeCode time, bool force );
286 
287  bool updateAttributeFromGTPrim( GT_Owner owner,
288  const std::string& name,
289  const GT_DataArrayHandle& houAttr,
290  UsdAttribute& usdAttr,
291  UsdTimeCode time );
292 
293  bool updatePrimvarFromGTPrim(
294  const TfToken& name,
295  const GT_Owner& owner,
296  const TfToken& interpolation,
297  UsdTimeCode time,
298  const GT_DataArrayHandle& data );
299 
300  /// Write primvar values from a GT attribute list to USD.
301  bool updatePrimvarFromGTPrim( const GT_AttributeListHandle& gtAttrs,
302  const GusdGT_AttrFilter& primvarFilter,
303  const TfToken& interpolation,
304  UsdTimeCode time );
305 
306  void clearCaches();
307 
308  /// Compute a USD transform from a Houdini transform.
309  ///
310  /// \p houXform is the transform from world to the prim's space in Houdini.
311  /// This includes the object node transformation and the transform of any
312  /// containing packed prim.
313  ///
314  /// \p xformCache is a map of the transforms of any groups that have been
315  /// written on the current frame.
316  static GfMatrix4d computeTransform(
317  const UsdPrim& prim,
318  UsdTimeCode time,
319  const UT_Matrix4D& houXform,
320  const GusdSimpleXformCache& xformCache );
321 
322 protected:
323 
326 
327  bool m_visible;
328 
329  //////////////
330  // Support for collapsing transform values across frames
331 
335 
336  //////////////
337  // Support from collapsing attribute values across frames
338 
340 
342  data = data_;
343  lastSet = time;
344  lastCompared = time;
345  }
346 
350  };
351 
352  typedef std::pair<GT_Owner, std::string> AttrLastValueKeyType;
354 
356 
357  //////////////
358 
359 private:
360 
361  struct GTTypeInfo {
362  DefinitionForWriteFunction writeFunc;
363  GetPrimNameFunction primNameFunc;
364  bool isGroupType;
365  const char * templateName;
366 
367  GTTypeInfo() :
368  writeFunc( NULL ),
369  primNameFunc( NULL ),
370  isGroupType( false ),
371  templateName(NULL) {}
372  GTTypeInfo(
373  DefinitionForWriteFunction writeFunc_,
374  GetPrimNameFunction primNameFunc_,
375  bool isGroupType_,
376  const char * templateName_ ) :
377  writeFunc( writeFunc_ ),
378  primNameFunc( primNameFunc_ ),
379  isGroupType( isGroupType_ ),
380  templateName( templateName_ ) {}
381  };
382 
383  struct TfTokenHashCmp {
384  static bool equal(const TfToken& a, const TfToken& b)
385  { return a == b; }
386 
387  static size_t hash(const TfToken& key)
388  { return key.Hash(); }
389  };
390 
391  typedef UT_Map<int64, GTTypeInfo> GTTypeInfoMap;
392  typedef UT_Set<int64> GTTypeSet;
393  typedef UT_ConcurrentHashMap<
394  TfToken, DefinitionForReadFunction, TfTokenHashCmp>
395  USDTypeToDefineFuncMap;
396 
397  static GTTypeInfoMap s_gtTypeInfoMap;
398  static USDTypeToDefineFuncMap s_usdTypeToFuncMap;
399  static GTTypeSet s_supportedNativeGTTypes;
400 };
401 
403 
404 #endif // __GUSD_PRIMWRAPPER_H__
SIM_API const UT_StringHolder vertex
void markVisible(bool in)
Keep track of the visibility state of the prim for book marks.
Definition: primWrapper.h:229
A GT_Primitive that wraps a USD Prim.
Definition: primWrapper.h:89
Definition: UT_Set.h:58
UsdTimeCode m_lastXformSet
Definition: primWrapper.h:333
AttrLastValueDict m_lastAttrValueDict
Definition: primWrapper.h:355
UsdTimeCode m_time
Definition: primWrapper.h:324
GT_API const UT_StringHolder time
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
AttrLastValueEntry(const UsdTimeCode &time, GT_DataArrayHandle data_)
Definition: primWrapper.h:341
std::pair< GT_Owner, std::string > AttrLastValueKeyType
Definition: primWrapper.h:352
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
static std::map< GT_Owner, TfToken > s_ownerToUsdInterp
Definition: primWrapper.h:269
UT_Function< GT_PrimitiveHandle(const UsdGeomImageable &, UsdTimeCode, GusdPurposeSet)> DefinitionForReadFunction
Definition: primWrapper.h:104
GusdPurposeSet m_purposes
Definition: primWrapper.h:325
Definition: token.h:87
GT_DataArrayHandle data
Definition: primWrapper.h:347
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:32
UT_Function< GT_PrimitiveHandle(const GT_PrimitiveHandle &, const UsdStagePtr &, const SdfPath &, const GusdContext &)> DefinitionForWriteFunction
Definition: primWrapper.h:98
Definition: prim.h:135
UsdTimeCode lastSet
Definition: primWrapper.h:348
GLuint const GLchar * name
Definition: glcorearb.h:786
GfMatrix4d m_xformCache
Definition: primWrapper.h:332
Definition: path.h:291
std::function< T > UT_Function
Definition: UT_Function.h:37
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
The base class for all GT primitive types.
Definition: GT_Primitive.h:43
GT_Owner
Definition: GT_Types.h:90
#define UT_ConcurrentHashMap
UsdTimeCode m_lastXformCompared
Definition: primWrapper.h:334
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
SIM_API const UT_StringHolder force
UT_Function< bool(const GT_PrimitiveHandle &, std::string &primName)> GetPrimNameFunction
Definition: primWrapper.h:109
fpreal64 fpreal
Definition: SYS_Types.h:277
Definition: primWrapper.h:339
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
UsdTimeCode lastCompared
Definition: primWrapper.h:349
UT_Function< GT_DataArrayHandle(const GT_DataArrayHandle &)> ResampleArrayFunction
Definition: primWrapper.h:113
#define GUSD_API
Definition: api.h:40
bool isVisible() const
Definition: primWrapper.h:230
UT_IntrusivePtr< GT_Primitive > GT_PrimitiveHandle
Definition: GT_Handles.h:33
static std::map< GT_Owner, TfToken > s_ownerToUsdInterpCurve
Definition: primWrapper.h:270
GusdPurposeSet
Definition: purpose.h:39
size_t Hash() const
Return a size_t hash for this token.
Definition: token.h:417
std::map< SdfPath, UT_Matrix4D > GusdSimpleXformCache
Definition: primWrapper.h:87
Definition: value.h:167
Definition: format.h:895
UT_Map< AttrLastValueKeyType, AttrLastValueEntry > AttrLastValueDict
Definition: primWrapper.h:353