HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_PackedUSD.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 
25 #ifndef __GUSD_GU_PACKEDIMPL_H__
26 #define __GUSD_GU_PACKEDIMPL_H__
27 
28 
29 #include <GU/GU_PackedImpl.h>
30 #include <GT/GT_Handles.h>
31 #include <UT/UT_Error.h>
32 
33 #include <pxr/pxr.h>
34 #include "pxr/usd/usd/prim.h"
36 #include "gusd/api.h"
37 #include "gusd/purpose.h"
38 #include "gusd/stageEdit.h"
39 #include "gusd/USD_Utils.h"
40 
41 class GusdPrimDef;
42 class GU_PrimPacked;
43 class GT_RefineParms;
44 
46 
47 /// A GU implementation of a packed USD prim.
48 ///
49 /// This is a file backed prim that holds a reference to a prim in a usd file
50 /// at a particular frame. The prim can be a group of prims.
51 ///
52 /// When a packed prim that references a USD group is unpacked, the result is
53 /// packed prims that represent the contents of that group. Those packed prims
54 /// may also be groups. To unpack down to the leafs, you may have to unpack may
55 /// times.
56 ///
57 /// When we write USD packed prim to a USD file, we write a reference to the
58 /// original file. USD has a limitation that it can only make references to root
59 /// nodes.
60 ///
61 /// When we write a reference into a USD file, we might want to use a different
62 /// file path than we use in the Houdini session. For example, we might want to
63 /// use a relative path vs. an absolute one. We might want to use a coalesced
64 /// file vs per frame files. We don't want to enshrine this logic in the core so
65 /// we provide a second alternative file name that can be used for this. If
66 /// this fileName is left empty, we just us the primary file name.
67 ///
68 
69 typedef void (*GusdPackedUSDTracker)(const GU_PackedImpl *prim, bool create);
70 
71 #if defined(WIN32)
72 class GusdGU_PackedUSD : public GU_PackedImpl
73 #else
75 #endif
76 {
77 public:
78  enum class PivotLocation : int
79  {
80  Origin = 0,
81  Centroid
82  };
83 
84  static GU_PrimPacked* Build(
85  GU_Detail& detail,
86  const UT_StringHolder& fileName,
87  const SdfPath& primPath,
89  const char* lod = nullptr,
91  const UsdPrim& prim = UsdPrim(),
92  const UT_Matrix4D* xform = nullptr,
94 
95  static GU_PrimPacked* Build(
96  GU_Detail& detail,
97  const UT_StringHolder& fileName,
98  const SdfPath& primPath,
99  const SdfPath& srcPrimPath,
100  int index,
101  UsdTimeCode frame,
102  const char* lod = nullptr,
104  const UsdPrim& prim = UsdPrim(),
105  const UT_Matrix4D* xform = nullptr,
107 
108  /// Convenience method for building a packed USD prim for \p prim.
109  static GU_PrimPacked* Build(
110  GU_Detail& detail,
111  const UsdPrim& prim,
112  UsdTimeCode frame,
113  const char* lod = nullptr,
115  const UT_Matrix4D* xform = nullptr,
117 
120  ~GusdGU_PackedUSD() override;
121 
122  static void install(GA_PrimitiveFactory &factory);
123 
124  /// Get the type ID for the packed USD primitive type.
125 #if defined(WIN32)
126  GUSD_API
127 #endif
128  static GA_PrimitiveTypeId typeId() { return theTypeId; }
129 
130  /// Sets a static callback that is used for tracking all packed USD prims.
131  /// This callback method lives in HUSD_LockedStageRegistry. It holds on to
132  /// an HUSD_LockedStagePtr for each GU_PackedUSD that exists. When the
133  /// packed prims are all destroyed, the locked stage can be released.
134 #if defined(WIN32)
135  GUSD_API
136 #endif
137  static void setPackedUSDTracker(GusdPackedUSDTracker tracker);
138 
139  const UT_StringHolder& fileName() const { return m_fileName; }
140  UT_StringHolder intrinsicFileName() const { return m_fileName; }
142  { return intrinsicFileName(); }
143  void setFileName(GU_PrimPacked *prim, const UT_StringHolder& fileName);
144 
145  const UT_StringHolder& altFileName() const { return m_altFileName; }
146  UT_StringHolder intrinsicAltFileName() const { return m_altFileName; }
147  void setAltFileName( const UT_StringHolder& fileName );
149  { return intrinsicAltFileName(); }
150  void setAltFileName(GU_PrimPacked *prim, const UT_StringHolder& fileName)
151  { setAltFileName(fileName); }
152 
153  const SdfPath& primPath() const { return m_primPath; }
154  UT_StringHolder intrinsicPrimPath() const { return m_primPath.GetText(); }
156  { return intrinsicPrimPath(); }
157  void setPrimPath(GU_PrimPacked *prim, const UT_StringHolder& p);
158  void setPrimPath(GU_PrimPacked *prim, const SdfPath& p);
159 
160  // If this prim was unpacked from a point instancer, srcPrimPath is the path
161  // to the instancer.
162  const SdfPath& srcPrimPath() const { return m_srcPrimPath; }
163  UT_StringHolder intrinsicSrcPrimPath() const { return m_srcPrimPath.GetText(); }
164  void setSrcPrimPath( const UT_StringHolder& p );
165  void setSrcPrimPath( const SdfPath& primPath );
167  { return intrinsicSrcPrimPath(); }
169  { setSrcPrimPath(p); }
170 
171  // If this prim was unpacked from a point instancer, index is the array
172  // index in the source point instancer.
173  exint index() const { return m_index; }
174  void setIndex( exint i );
175  exint index(const GU_PrimPacked *prim) const
176  { return index(); }
177  void setIndex(GU_PrimPacked *prim, exint i)
178  { setIndex(i); }
179 
180  // Return true if this is a prim that has been unpacked from a point instancer.
181  bool isPointInstance() const { return m_index >= 0; }
182 
183  // return the USD prim type
186  { return intrinsicType(); }
187 
188  GA_Size usdLocalToWorldTransformSize() const { return 16; }
191  { return 16; }
193  fpreal64* val, exint size) const
194  { usdLocalToWorldTransform(val, size); }
195 
196  UsdTimeCode frame() const { return m_frame; }
199  { return intrinsicFrame(); }
200  void setFrame(GU_PrimPacked *prim, fpreal frame);
201 #if defined(WIN32)
202  GUSD_API
203 #endif
204  void setFrame(GU_PrimPacked *prim, UsdTimeCode frame);
205 
206  GusdPurposeSet getPurposes() const { return m_purposes; }
207  void setPurposes( GU_PrimPacked *prim, GusdPurposeSet purposes );
208 
209  /// Returns the purpose of the USD prim.
211 
212  /// @{
213  /// Returns the viewport purpose(s) which are used for traversals when
214  /// displaying or unpacking the prim.
216  void getIntrinsicViewportPurposes( UT_StringArray& purposes ) const;
218  { return getNumViewportPurposes(); }
220  UT_StringArray& purposes ) const
221  { getIntrinsicViewportPurposes(purposes); }
223  const UT_StringArray& purposes );
224  /// @}
225 
226  GU_PackedFactory *getFactory() const override;
227  GU_PackedImpl *copy() const override;
228  void clearData() override;
229 
230  bool isValid() const override;
231  bool save(UT_Options &options, const GA_SaveMap &map) const override;
232  bool load(
233  GU_PrimPacked *prim,
234  const UT_Options &options,
235  const GA_LoadMap &map) override;
236  void update(
237  GU_PrimPacked *prim,
238  const UT_Options &options) override;
239 
240  bool getBounds(UT_BoundingBox &box) const override;
241  bool getRenderingBounds(UT_BoundingBox &box) const override;
242  void getVelocityRange(UT_Vector3 &min, UT_Vector3 &max) const override;
243  void getWidthRange(fpreal &min, fpreal &max) const override;
244 
245  bool getLocalTransform(UT_Matrix4D &m) const override;
246 
247  bool unpack(GU_Detail &destgdp,
248  const UT_Matrix4D *transform) const override;
249  bool unpackUsingPolygons(GU_Detail &destgdp,
250  const GU_PrimPacked *prim) const override;
251 
252 protected:
253  /// This signature is just for the questionable purpose of copying
254  /// primitive group membership from prim, so it might be removed
255  /// in the future.
256  bool unpackWithPrim(
257  GU_Detail& destgdp,
258  const UT_Matrix4D* transform,
259  const GU_PrimPacked* prim) const override;
260 
261 public:
262  bool visibleGT() const;
263  GT_PrimitiveHandle fullGT() const;
264 
265  // Return a structure that can be hashed to sort instances by prototype.
266  bool getInstanceKey(UT_Options& key) const;
267 
268  /// Report memory usage (includes all shared memory)
269  int64 getMemoryUsage(bool inclusive) const override;
270 
271  /// Count memory usage using a UT_MemoryCounter in order to count
272  /// shared memory correctly.
273  void countMemory(UT_MemoryCounter &counter, bool inclusive) const override;
274 
275  /// Get the underlying UsdPrim for this packed prim.
276  /// This may involve on-demand loading of a UsdStage to access the prim.
277  /// Any errors that occur while loading the stage and accessing the prim
278  /// will be reported on the currently scoped error manager with a severity
279  /// of \p sev.
281 
282  bool unpackGeometry(
283  GU_Detail &destgdp,
284  const GU_Detail* srcgdp,
285  const GA_Offset srcprimoff,
286  const UT_StringRef &primvarPattern,
287  bool importInheritedPrimvars,
288  const UT_StringRef &attributePattern,
289  bool translateSTtoUV,
290  const UT_StringRef& nonTransformingPrimvarPattern,
291  const UT_Matrix4D* transform,
292  const GT_RefineParms *parms = nullptr) const;
293 
294  /// Convert the USD geometry into one or more GU_Detail's. Use
295  /// mergeGeometry() to merge the geometry into a destination detail.
296  /// This signature can be used when performing the geometry conversion in
297  /// parallel for many prims.
299  const GU_Detail *srcgdp,
300  const GA_Offset srcprimoff,
301  const UT_StringRef &primvarPattern,
302  bool importInheritedPrimvars,
303  const UT_StringRef &attributePattern,
304  bool translateSTtoUV,
305  const UT_StringRef &nonTransformingPrimvarPattern,
306  const UT_Matrix4D *transform,
307  const UT_StringHolder &filePathAttrib,
308  const UT_StringHolder &primPathAttrib,
309  const GT_RefineParms *refineParms = nullptr) const;
310 
311  /// Merges the details together, and also updates the
312  /// usdconfigconstantattribs detail attribute.
313  static void mergeGeometry(GU_Detail &destgdp,
314  UT_Array<GU_DetailHandle> &details);
315 
316  const UT_Matrix4D& getUsdTransform() const;
317 
318 private:
319  bool unpackPrim(
320  UT_Array<GU_DetailHandle> &details,
321  const GU_Detail* srcgdp,
322  const GA_Offset srcprimoff,
323  UsdGeomImageable prim,
324  const SdfPath& primPath,
325  const UT_Matrix4D* xform,
326  const GT_RefineParms& rparms ) const;
327 
328  void resetCaches();
329  void updateTransform( GU_PrimPacked* prim );
330  void initializePivot(GU_PrimPacked *prim, PivotLocation pivotloc);
331 
332  /// Set the overall world transform. This will set the 'transform'
333  /// intrinsic and P so that this transform is produced when combined with
334  /// getUsdTransform() ('packedlocaltransform' intrinsic).
335  void setTransform( GU_PrimPacked* prim, const UT_Matrix4D& mx );
336 
337  // intrinsics
338  UT_StringHolder m_fileName;
339  UT_StringHolder m_altFileName;
340  SdfPath m_srcPrimPath;
341  int m_index;
342  SdfPath m_primPath;
343  UsdTimeCode m_frame;
344  GusdPurposeSet m_purposes;
345 
346  // caches
347  mutable UsdPrim m_usdPrim;
348  mutable bool m_transformCacheValid;
349  mutable UT_Matrix4D m_transformCache;
350  mutable GT_PrimitiveHandle m_gtPrimCache;
351  mutable bool m_prototypePathCacheValid;
352  mutable std::string m_prototypePathCache;
353 
354  // static
355  static GusdPackedUSDTracker thePackedUSDTracker;
356  static GA_PrimitiveTypeId theTypeId;
357 };
358 
360 
361 #endif // __GUSD_GU_PACKEDIMPL_H__
SDF_API const char * GetText() const
bool unpackUsingPolygons(GU_Detail &destgdp, const GU_PrimPacked *prim) const override
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
void setSrcPrimPath(GU_PrimPacked *prim, const UT_StringHolder &p)
Definition: GU_PackedUSD.h:168
UT_StringHolder intrinsicType(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:185
GU_PackedImpl * copy() const override
Create a copy of this resolver.
UT_StringHolder intrinsicAltFileName() const
Definition: GU_PackedUSD.h:146
UsdTimeCode frame() const
Definition: GU_PackedUSD.h:196
Used to pass options and map offset values during saving.
Definition: GA_SaveMap.h:48
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
GT_PrimitiveHandle fullGT() const
void
Definition: png.h:1083
GA_Size usdLocalToWorldTransformSize(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:190
UT_StringHolder intrinsicAltFileName(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:148
UT_StringHolder intrinsicFileName() const
Definition: GU_PackedUSD.h:140
int64 exint
Definition: SYS_Types.h:125
bool load(GU_PrimPacked *prim, const UT_Options &options, const GA_LoadMap &map) override
Give a UT_Options of load data, create resolver data for the primitive.
UsdPrim getUsdPrim(UT_ErrorSeverity sev=UT_ERROR_ABORT) const
UT_ErrorSeverity
Definition: UT_Error.h:25
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void setSrcPrimPath(const UT_StringHolder &p)
void setFileName(GU_PrimPacked *prim, const UT_StringHolder &fileName)
fpreal intrinsicFrame(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:198
static GA_PrimitiveTypeId typeId()
Get the type ID for the packed USD primitive type.
Definition: GU_PackedUSD.h:128
const UT_StringHolder & altFileName() const
Definition: GU_PackedUSD.h:145
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:236
bool getRenderingBounds(UT_BoundingBox &box) const override
double fpreal64
Definition: SYS_Types.h:201
GA_Size GA_Offset
Definition: GA_Types.h:646
UT_StringHolder intrinsicPrimPath() const
Definition: GU_PackedUSD.h:154
void getWidthRange(fpreal &min, fpreal &max) const override
bool save(UT_Options &options, const GA_SaveMap &map) const override
Copy the resolver data into the UT_Options for saving.
const UT_Matrix4D & getUsdTransform() const
bool visibleGT() const
void getIntrinsicViewportPurposes(UT_StringArray &purposes) const
void usdLocalToWorldTransform(fpreal64 *val, exint size) const
UT_StringHolder intrinsicType() const
GU_PackedFactory * getFactory() const override
Get the factory associated with this procedural.
UT_StringHolder intrinsicPrimPath(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:155
void setIntrinsicViewportPurposes(GU_PrimPacked *prim, const UT_StringArray &purposes)
bool getLocalTransform(UT_Matrix4D &m) const override
UT_StringHolder intrinsicPurpose(const GU_PrimPacked *prim) const
Returns the purpose of the USD prim.
long long int64
Definition: SYS_Types.h:116
Definition: prim.h:116
void setFrame(GU_PrimPacked *prim, fpreal frame)
void usdLocalToWorldTransform(const GU_PrimPacked *prim, fpreal64 *val, exint size) const
Definition: GU_PackedUSD.h:192
Options during loading.
Definition: GA_LoadMap.h:42
~GusdGU_PackedUSD() override
bool getBounds(UT_BoundingBox &box) const override
Get the bounding box for the geometry (not including transforms)
void setAltFileName(GU_PrimPacked *prim, const UT_StringHolder &fileName)
Definition: GU_PackedUSD.h:150
void setPurposes(GU_PrimPacked *prim, GusdPurposeSet purposes)
Definition: path.h:273
void setAltFileName(const UT_StringHolder &fileName)
GA_API const UT_StringHolder transform
bool isValid() const override
Test whether the deferred load primitive data is valid.
PXR_NAMESPACE_OPEN_SCOPE typedef void(* GusdPackedUSDTracker)(const GU_PackedImpl *prim, bool create)
Definition: GU_PackedUSD.h:69
const UT_StringHolder & fileName() const
Definition: GU_PackedUSD.h:139
void getVelocityRange(UT_Vector3 &min, UT_Vector3 &max) const override
UT_StringHolder intrinsicSrcPrimPath(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:166
GLsizeiptr size
Definition: glcorearb.h:664
void setPrimPath(GU_PrimPacked *prim, const UT_StringHolder &p)
static void mergeGeometry(GU_Detail &destgdp, UT_Array< GU_DetailHandle > &details)
A map of string to various well defined value types.
Definition: UT_Options.h:84
GA_API const UT_StringHolder parms
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
void setIndex(exint i)
GA_Size usdLocalToWorldTransformSize() const
Definition: GU_PackedUSD.h:188
fpreal64 fpreal
Definition: SYS_Types.h:278
GusdPurposeSet getPurposes() const
Definition: GU_PackedUSD.h:206
GLuint index
Definition: glcorearb.h:786
static void install(GA_PrimitiveFactory &factory)
void getIntrinsicViewportPurposes(const GU_PrimPacked *prim, UT_StringArray &purposes) const
Definition: GU_PackedUSD.h:219
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void countMemory(UT_MemoryCounter &counter, bool inclusive) const override
static void setPackedUSDTracker(GusdPackedUSDTracker tracker)
bool unpackGeometry(GU_Detail &destgdp, const GU_Detail *srcgdp, const GA_Offset srcprimoff, const UT_StringRef &primvarPattern, bool importInheritedPrimvars, const UT_StringRef &attributePattern, bool translateSTtoUV, const UT_StringRef &nonTransformingPrimvarPattern, const UT_Matrix4D *transform, const GT_RefineParms *parms=nullptr) const
#define GUSD_API
Definition: api.h:42
exint index(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:175
UT_StringHolder intrinsicSrcPrimPath() const
Definition: GU_PackedUSD.h:163
exint getNumViewportPurposes(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:217
exint getNumViewportPurposes() const
bool getInstanceKey(UT_Options &key) const
GUSD_API double GetNumericTime(UsdTimeCode time)
Definition: USD_Utils.h:285
bool unpack(GU_Detail &destgdp, const UT_Matrix4D *transform) const override
const SdfPath & srcPrimPath() const
Definition: GU_PackedUSD.h:162
bool unpackWithPrim(GU_Detail &destgdp, const UT_Matrix4D *transform, const GU_PrimPacked *prim) const override
GusdPurposeSet
Definition: purpose.h:39
static GU_PrimPacked * Build(GU_Detail &detail, const UT_StringHolder &fileName, const SdfPath &primPath, UsdTimeCode frame, const char *lod=nullptr, GusdPurposeSet purposes=GUSD_PURPOSE_PROXY, const UsdPrim &prim=UsdPrim(), const UT_Matrix4D *xform=nullptr, PivotLocation pivotloc=PivotLocation::Origin)
void update(GU_PrimPacked *prim, const UT_Options &options) override
exint index() const
Definition: GU_PackedUSD.h:173
GLint lod
Definition: glcorearb.h:2765
bool isPointInstance() const
Definition: GU_PackedUSD.h:181
void clearData() override
UT_StringHolder intrinsicFileName(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:141
fpreal intrinsicFrame() const
Definition: GU_PackedUSD.h:197
int64 getMemoryUsage(bool inclusive) const override
Report memory usage (includes all shared memory)
void setIndex(GU_PrimPacked *prim, exint i)
Definition: GU_PackedUSD.h:177
GLenum src
Definition: glcorearb.h:1793
const SdfPath & primPath() const
Definition: GU_PackedUSD.h:153