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
79  {
80  Origin,
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  exint getNumPurposes() const;
210  void getIntrinsicPurposes( UT_StringArray& purposes ) const;
211  exint getNumPurposes(const GU_PrimPacked *prim) const
212  { return getNumPurposes(); }
214  UT_StringArray& purposes ) const
215  { getIntrinsicPurposes(purposes); }
217  const UT_StringArray& purposes );
218 
219  GU_PackedFactory *getFactory() const override;
220  GU_PackedImpl *copy() const override;
221  void clearData() override;
222 
223  bool isValid() const override;
224  bool save(UT_Options &options, const GA_SaveMap &map) const override;
225  bool load(
226  GU_PrimPacked *prim,
227  const UT_Options &options,
228  const GA_LoadMap &map) override;
229  void update(
230  GU_PrimPacked *prim,
231  const UT_Options &options) override;
232 
233  bool getBounds(UT_BoundingBox &box) const override;
234  bool getRenderingBounds(UT_BoundingBox &box) const override;
235  void getVelocityRange(UT_Vector3 &min, UT_Vector3 &max) const override;
236  void getWidthRange(fpreal &min, fpreal &max) const override;
237 
238  bool getLocalTransform(UT_Matrix4D &m) const override;
239 
240  bool unpack(GU_Detail &destgdp,
241  const UT_Matrix4D *transform) const override;
242  bool unpackUsingPolygons(GU_Detail &destgdp,
243  const GU_PrimPacked *prim) const override;
244 
245 protected:
246  /// This signature is just for the questionable purpose of copying
247  /// primitive group membership from prim, so it might be removed
248  /// in the future.
249  bool unpackWithPrim(
250  GU_Detail& destgdp,
251  const UT_Matrix4D* transform,
252  const GU_PrimPacked* prim) const override;
253 
254 public:
255  bool visibleGT() const;
256  GT_PrimitiveHandle fullGT() const;
257 
258  // Return a structure that can be hashed to sort instances by prototype.
259  bool getInstanceKey(UT_Options& key) const;
260 
261  /// Report memory usage (includes all shared memory)
262  int64 getMemoryUsage(bool inclusive) const override;
263 
264  /// Count memory usage using a UT_MemoryCounter in order to count
265  /// shared memory correctly.
266  void countMemory(UT_MemoryCounter &counter, bool inclusive) const override;
267 
268  /// Get the underlying UsdPrim for this packed prim.
269  /// This may involve on-demand loading of a UsdStage to access the prim.
270  /// Any errors that occur while loading the stage and accessing the prim
271  /// will be reported on the currently scoped error manager with a severity
272  /// of \p sev.
274 
275  bool unpackGeometry(
276  GU_Detail &destgdp,
277  const GU_Detail* srcgdp,
278  const GA_Offset srcprimoff,
279  const UT_StringRef &primvarPattern,
280  bool importInheritedPrimvars,
281  const UT_StringRef &attributePattern,
282  bool translateSTtoUV,
283  const UT_StringRef& nonTransformingPrimvarPattern,
284  const UT_Matrix4D* transform,
285  const GT_RefineParms *parms = nullptr) const;
286 
287  /// Convert the USD geometry into one or more GU_Detail's. Use
288  /// mergeGeometry() to merge the geometry into a destination detail.
289  /// This signature can be used when performing the geometry conversion in
290  /// parallel for many prims.
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 UT_StringHolder &filePathAttrib,
301  const UT_StringHolder &primPathAttrib,
302  const GT_RefineParms *refineParms = nullptr) const;
303 
304  /// Merges the details together, and also updates the
305  /// usdconfigconstantattribs detail attribute.
306  static void mergeGeometry(GU_Detail &destgdp,
307  UT_Array<GU_DetailHandle> &details);
308 
309  const UT_Matrix4D& getUsdTransform() const;
310 
311 private:
312  bool unpackPrim(
313  UT_Array<GU_DetailHandle> &details,
314  const GU_Detail* srcgdp,
315  const GA_Offset srcprimoff,
316  UsdGeomImageable prim,
317  const SdfPath& primPath,
318  const UT_Matrix4D& xform,
319  const GT_RefineParms& rparms ) const;
320 
321  void resetCaches();
322  void updateTransform( GU_PrimPacked* prim );
323  void initializePivot(GU_PrimPacked *prim, PivotLocation pivotloc);
324 
325  /// Set the overall world transform. This will set the 'transform'
326  /// intrinsic and P so that this transform is produced when combined with
327  /// getUsdTransform() ('packedlocaltransform' intrinsic).
328  void setTransform( GU_PrimPacked* prim, const UT_Matrix4D& mx );
329 
330  // intrinsics
331  UT_StringHolder m_fileName;
332  UT_StringHolder m_altFileName;
333  SdfPath m_srcPrimPath;
334  int m_index;
335  SdfPath m_primPath;
336  UsdTimeCode m_frame;
337  GusdPurposeSet m_purposes;
338 
339  // caches
340  mutable UsdPrim m_usdPrim;
341  mutable bool m_transformCacheValid;
342  mutable UT_Matrix4D m_transformCache;
343  mutable GT_PrimitiveHandle m_gtPrimCache;
344  mutable bool m_prototypePathCacheValid;
345  mutable std::string m_prototypePathCache;
346 
347  // static
348  static GusdPackedUSDTracker thePackedUSDTracker;
349  static GA_PrimitiveTypeId theTypeId;
350 };
351 
353 
354 #endif // __GUSD_GU_PACKEDIMPL_H__
SDF_API const char * GetText() const
bool unpackUsingPolygons(GU_Detail &destgdp, const GU_PrimPacked *prim) const override
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
GLuint counter
Definition: glew.h:2745
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:629
GT_PrimitiveHandle fullGT() const
void
Definition: png.h:1083
exint getNumPurposes(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:211
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
void setSrcPrimPath(const UT_StringHolder &p)
void setFileName(GU_PrimPacked *prim, const UT_StringHolder &fileName)
GLenum src
Definition: glcorearb.h:1792
3D Vector class.
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
void getIntrinsicPurposes(const GU_PrimPacked *prim, UT_StringArray &purposes) const
Definition: GU_PackedUSD.h:213
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:234
bool getRenderingBounds(UT_BoundingBox &box) const override
GLsizeiptr size
Definition: glcorearb.h:663
double fpreal64
Definition: SYS_Types.h:201
GLuint GLenum GLenum transform
Definition: glew.h:15055
GA_Size GA_Offset
Definition: GA_Types.h:640
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 usdLocalToWorldTransform(fpreal64 *val, exint size) const
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
UT_StringHolder intrinsicType() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
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
bool getLocalTransform(UT_Matrix4D &m) const override
long long int64
Definition: SYS_Types.h:116
Definition: prim.h:132
GLfloat GLfloat p
Definition: glew.h:16656
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 setIntrinsicPurposes(GU_PrimPacked *prim, const UT_StringArray &purposes)
void setPurposes(GU_PrimPacked *prim, GusdPurposeSet purposes)
Definition: path.h:288
void setAltFileName(const UT_StringHolder &fileName)
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
GLuint GLfloat * val
Definition: glcorearb.h:1607
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
GLsizei const GLint box[]
Definition: glew.h:11654
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
void setIndex(exint i)
void getIntrinsicPurposes(UT_StringArray &purposes) const
GA_Size usdLocalToWorldTransformSize() const
Definition: GU_PackedUSD.h:188
fpreal64 fpreal
Definition: SYS_Types.h:277
exint getNumPurposes() const
GusdPurposeSet getPurposes() const
Definition: GU_PackedUSD.h:206
GLuint index
Definition: glcorearb.h:785
static void install(GA_PrimitiveFactory &factory)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
const GLdouble * m
Definition: glew.h:9166
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:40
exint index(const GU_PrimPacked *prim) const
Definition: GU_PackedUSD.h:175
UT_StringHolder intrinsicSrcPrimPath() const
Definition: GU_PackedUSD.h:163
bool getInstanceKey(UT_Options &key) const
GUSD_API double GetNumericTime(UsdTimeCode time)
Definition: USD_Utils.h:284
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:2764
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
const SdfPath & primPath() const
Definition: GU_PackedUSD.h:153