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