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