HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_USD.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_GU_USD_H_
25 #define _GUSD_GU_USD_H_
26 
27 #include "api.h"
28 #include "defaultArray.h"
29 #include "stageCache.h"
30 #include "USD_Traverse.h"
31 #include "USD_Utils.h"
32 #include "GU_PackedUSD.h"
33 
34 #include <GA/GA_Attribute.h>
35 #include <GA/GA_Handle.h>
36 #include <GA/GA_Names.h>
37 #include <GA/GA_Range.h>
38 #include <UT/UT_ErrorManager.h>
39 #include <UT/UT_StringHolder.h>
40 
41 #include "pxr/pxr.h"
42 
43 class GA_AttributeFilter;
44 class GT_RefineParms;
45 class GU_Detail;
46 class GU_PrimPacked;
47 
49 
51 
52 /** The default names of the USD ref attributes.
53  @{ */
54 #define GUSD_PATH_ATTR "usdpath"
55 #define GUSD_PRIMPATH_ATTR "usdprimpath"
56 #define GUSD_FRAME_ATTR "frame"
57 #define GUSD_VARIANTS_ATTR "usdvariants"
58 #define GUSD_CONSTRAINT_ATTR "usdconstraint"
59 #define GUSD_PURPOSE_ATTR "usdpurpose"
60 #define GUSD_ACTIVE_ATTR "usdactive"
61 #define GUSD_VISIBLE_ATTR "usdvisible"
62 
63 #define GUSD_OVERTRANSFORMS_ATTR "usdovertransforms"
64 #define GUSD_OVERPOINTS_ATTR "usdoverpoints"
65 #define GUSD_OVERPRIMVARS_ATTR "usdoverprimvars"
66 #define GUSD_OVERALL_ATTR "usdoverall"
67 
68 #define GUSD_WRITESTATICTOPOLOGY_ATTR "usdwritestatictopology"
69 #define GUSD_WRITESTATICPRIMVARS_ATTR "usdwritestaticprimvars"
70 #define GUSD_WRITESTATICGEO_ATTR "usdwritestaticgeo"
71 /** @} */
72 
73 /** USD-related options for GT_RefineParms.
74  @{ */
75 #define GUSD_REFINE_ADDPATHATTRIB "usd:addPathAttribute"
76 #define GUSD_REFINE_PATHATTRIB "usd:pathAttribute"
77 #define GUSD_REFINE_ADDPRIMPATHATTRIB "usd:addPrimPathAttribute"
78 #define GUSD_REFINE_PRIMPATHATTRIB "usd:primPathAttribute"
79 #define GUSD_REFINE_ADDINSTANCELEVELATTRIB "usd:addInstanceLevelAttrib"_UTsh
80 #define GUSD_REFINE_INSTANCELEVELATTRIB "usd:instanceLevelAttrib"_UTsh
81 #define GUSD_REFINE_ADDMATERIALPATHATTRIB "usd:addMaterialPathAttribute"
82 #define GUSD_REFINE_ADDVISIBILITYATTRIB "usd:addVisibilityAttribute"
83 #define GUSD_REFINE_IMPORTCOMPUTEDVISIBILITY "usd:importComputedVisibility"
84 #define GUSD_REFINE_ADDXFORMATTRIB "usd:addXformAttribute"
85 #define GUSD_REFINE_NONTRANSFORMINGPATTERN "usd:nonTransformingPrimvarPattern"
86 #define GUSD_REFINE_PRIMVARPATTERN "usd:primvarPattern"
87 #define GUSD_REFINE_IMPORTINHERITEDPRIMVARS "usd:importInheritedPrimvars"
88 #define GUSD_REFINE_ATTRIBUTEPATTERN "usd:attributePattern"
89 #define GUSD_REFINE_RELATIONSHIPPATTERN "usd:relationshipPattern"_UTsh
90 #define GUSD_REFINE_TRANSLATESTTOUV "usd:translateSTtoUV"
91 #define GUSD_REFINE_ITERATIONS "usd:iterations"_UTsh
92 #define GUSD_REFINE_UNPACKTOPOLYGONS "usd:unpacktopolygons"_UTsh
93 #define GUSD_REFINE_PIVOTLOCATION "usd:pivotlocation"_UTsh
94 #define GUSD_REFINE_PROMOTECONSTANTATTRIBS "usd:promoteconstantattrs"_UTsh
95 /** @} */
96 
97 /** Set of helpers for working with ranges of prims/points, etc.*/
99 {
100 public:
101  /** Compute an array of offsets from a range.*/
102  static bool OffsetArrayFromRange(const GA_Range& r,
104 
105  /** Compute an array mapping offset->range_index for the given range.*/
106  static bool ComputeRangeIndexMap(const GA_Range& r,
107  GA_OffsetArray& indexMap);
108 
109  static bool
110  BindPrims(GusdStageCacheReader& cache,
111  UT_Array<UsdPrim>& prims,
112  const GA_Detail& gd,
113  const GA_Range& rng,
114  UT_Array<SdfPath>* variants=nullptr,
115  GusdDefaultArray<GusdPurposeSet>* purposes=nullptr,
116  GusdDefaultArray<UsdTimeCode>* times=nullptr,
118 
119  /** Bind prims from references defined in the given attributes.
120  This creates an entry in @a prims for each entry in the given range,
121  mapped to the corresponding prim.
122  If @a variants is non-null, resolved variant paths are stored
123  in the given array.*/
124  static bool
125  BindPrimsFromAttrs(GusdStageCacheReader& cache,
126  UT_Array<UsdPrim>& prims,
127  const GA_Range& rng,
128  const GA_Attribute& pathAttr,
129  const GA_Attribute& primPathAttr,
130  const GA_Attribute* variantsAttr,
131  UT_Array<SdfPath>* variants=nullptr,
133 
134  static bool
135  BindPrimsFromPackedPrims(UT_Array<UsdPrim>& prims,
136  const GA_Range& rng,
137  UT_Array<SdfPath>* variants=nullptr,
138  UT_Array<GusdPurposeSet>* purposes=nullptr,
140 
141  static bool GetTimeCodesFromAttr(const GA_Range& rng,
142  const GA_Attribute& attr,
143  UT_Array<UsdTimeCode>& times);
144 
145  static bool GetTimeCodesFromPackedPrims(const GA_Range& rng,
146  UT_Array<UsdTimeCode>& times);
147 
148  /** Given a string attribute that represents prim paths,
149  return an array of actual prim paths.
150  @{ */
151  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
152  UT_Array<SdfPath>& paths,
154 
155  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
156  const GA_Range& rng,
157  UT_Array<SdfPath>& paths,
159  /** @} */
160 
161 
162  /** Givena string attribute, return an array of tokens.
163  @{ */
164  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
165  UT_Array<TfToken>& tokens,
166  const char* nameSpace=nullptr);
167 
168  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
169  const GA_Range& rng,
170  UT_Array<TfToken>& tokens,
171  const char* nameSpace=nullptr);
172  /** @} */
173 
174 
175  /** Append points to a detail that represent references to prims.
176  The point offsets are contiguous, and the offset of the first
177  point is returned. If any failures occur, and invalid
178  offset is returned.*/
179  static GA_Offset AppendRefPoints(
180  GU_Detail& gd,
181  const UT_Array<UsdPrim>& prims,
182  const char* pathAttrName=GUSD_PATH_ATTR,
183  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
184 
185  typedef GU_PrimPacked* (*PackedPrimBuildFunc)(
186  GU_Detail& detail,
187  const UT_StringHolder& fileName,
188  const SdfPath& primPath,
189  const UsdTimeCode& frame,
190  const char* lod,
191  const GusdPurposeSet purposes );
192 
193  /** Register a function to be used by AppendPackedPrims to build
194  a packed prim of the given type. */
195  static void RegisterPackedPrimBuildFunc( const TfToken& typeName,
196  PackedPrimBuildFunc func );
197 
198  /** Append packed prims to the given detail that reference the
199  given prims with the given variants. */
200  static bool AppendPackedPrims(
201  GU_Detail& gd,
202  const UT_Array<UsdPrim>& prims,
203  const UT_Array<SdfPath>& variants,
204  const GusdDefaultArray<UsdTimeCode>& times,
206  const GusdDefaultArray<GusdPurposeSet>& purposes,
208  /** More specialized function for appending prims to the given detail
209  from a LOP node where the variants are already chosen, and the LOP
210  node path needs to be set as the file path on the packed prim. */
211  static bool AppendPackedPrimsFromLopNode(
212  GU_Detail& gd,
213  const UT_Array<UsdPrim>& prims,
214  const GusdDefaultArray<UT_StringHolder> &stageids,
215  const GusdDefaultArray<UsdTimeCode>& times,
217  const GusdDefaultArray<GusdPurposeSet>& purposes,
219  /** Append one packed prim per point instance of @a instancer, for the
220  given positional instance indices, from a LOP node stage identified
221  by @a stageid. Each packed prim points at the instance's prototype
222  prim and records the instancer path and instance index. The result
223  matches unpacking a packed prim for the instancer prim, including the
224  instancer's per-instance primvars and attributes as point
225  attributes. */
226  static bool AppendPackedPointInstancesFromLopNode(
227  GU_Detail& gd,
228  const UsdGeomPointInstancer& instancer,
229  const UT_Array<exint>& instance_indices,
230  const UT_StringHolder& stageid,
232  const char* lod,
233  GusdPurposeSet purposes,
235 
237 
238  /** Append prims @a prims, as an expansion of prims defined on
239  @a srcRange. The prim index pairs provide the prim found in
240  the expansion, and the index of the prim in the source range
241  whose expansion produced that prim.
242  Attributes and groups matching @a filter are copied from the source
243  to the newly created ref points.*/
244  static GA_Offset AppendExpandedRefPoints(
245  GU_Detail& gd,
246  const GA_Detail& srcGd,
247  const GA_Range& srcRng,
248  const UT_Array<PrimIndexPair>& prims,
249  const GA_AttributeFilter& filter,
250  const char* pathAttrName=GUSD_PATH_ATTR,
251  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
252 
253  static bool AppendExpandedPackedPrims(
254  GU_Detail& gd,
255  const GA_Detail& srcGd,
256  const GA_Range& srcRng,
257  const UT_Array<PrimIndexPair>& primIndexPairs,
258  const UT_Array<SdfPath>& variants,
259  const GusdDefaultArray<UsdTimeCode>& times,
260  const GA_AttributeFilter& filter,
261  bool unpackToPolygons,
262  const UT_String& primvarPattern,
263  const UT_String& attributePattern,
264  bool translateSTtoUV,
265  const UT_StringRef& nonTransformingPrimvarPattern,
267 
268  static bool AppendExpandedPackedPrimsFromLopNode(
269  GU_Detail& gd,
270  const GA_Detail& srcGd,
271  const GA_Range& srcRng,
272  const UT_Array<PrimIndexPair>& primIndexPairs,
273  const GusdDefaultArray<UsdTimeCode>& times,
274  const GA_AttributeFilter& filter,
275  bool unpackToPolygons,
276  const UT_String& primvarPattern,
277  bool importInheritedPrimvars,
278  const UT_String& attributePattern,
279  bool translateSTtoUV,
280  const UT_StringRef &nonTransformingPrimvarPattern,
282  const UT_StringHolder &filePathAttrib = GUSD_PATH_ATTR,
283  const UT_StringHolder &primPathAttrib = GUSD_PRIMPATH_ATTR,
284  const GT_RefineParms *refineParms = nullptr);
285 
286  /** Apply all variant selections in @a selections to each prim
287  in the range, storing the resulting variant path in @a variantsAttr.
288  For each source prim, this will first validate that the
289  variant selection is valid on the target prims.
290  If @a prevVariants is supplied, the variant selections are added
291  on top of any variant selections in the given paths.*/
292  static bool WriteVariantSelectionsToAttr(
293  GU_Detail& gd,
294  const GA_Range& rng,
295  const UT_Array<UsdPrim>& prims,
296  const GusdUSD_Utils::VariantSelArray& selections,
297  const char* variantsAttr=GUSD_VARIANTS_ATTR,
298  const UT_Array<SdfPath>* prevVariants=nullptr);
299 
300  static bool WriteVariantSelectionsToPackedPrims(
301  GU_Detail& gd,
302  const GA_Range& rng,
303  const UT_Array<UsdPrim>& prims,
304  const GusdUSD_Utils::VariantSelArray& selections,
305  const UT_Array<SdfPath>* prevVariants=nullptr);
306 
307  /** Append variant selections defined by @a orderedVariants and
308  @a variantIndices as an expansion of prims from @a srcRng.
309  Attributes and groups matching @a attrs filter are copied from the
310  source to the newly created ref points.*/
311  static GA_Offset AppendRefPointsForExpandedVariants(
312  GU_Detail& gd,
313  const GA_Detail& srcGd,
314  const GA_Range& srcRng,
315  const UT_Array<UT_StringHolder>& orderedVariants,
316  const GusdUSD_Utils::IndexPairArray& variantIndices,
317  const GA_AttributeFilter& filter,
318  const char* variantsAttr=GUSD_VARIANTS_ATTR);
319 
320  static GA_Offset AppendPackedPrimsForExpandedVariants(
321  GU_Detail& gd,
322  const GA_Detail& srcGd,
323  const GA_Range& srcRng,
324  const UT_Array<UT_StringHolder>& orderedVariants,
325  const GusdUSD_Utils::IndexPairArray& variantIndices,
326  const GA_AttributeFilter& filter);
327 
328  static bool GetPackedPrimStageIdsViewportLODsAndPurposes(
329  const GA_Detail& gd,
330  const GA_OffsetArray& offsets,
331  UT_StringArray& stageIds,
332  UT_StringArray& viewportLODs,
333  UT_Array<GusdPurposeSet>& purposes);
334 
335  /** Compute world transforms from attributes over an array of offsets.
336 
337  In addition to using the standard instancing attributes,
338  this supports an additional schema for non-orthonormal transforms,
339  where the basis vectors (rows) of a rotation matrix are stored
340  as normal attributes 'i', 'j', 'k'.*/
341  static bool ComputeTransformsFromAttrs(const GA_Detail& gd,
342  GA_AttributeOwner owner,
343  const GA_OffsetArray& offsets,
344  UT_Matrix4D* xforms);
345 
346  static bool ComputeTransformsFromPackedPrims(const GA_Detail& gd,
347  const GA_OffsetArray& offsets,
348  UT_Matrix4D* xforms);
349 
350  /** Support representations of attributes when authoring new transforms.*/
352  {
353  ORIENTATTR_ORIENT, //! quaternion orient.
354  ORIENTATTR_IJK, //! vec3 i,j,k (can be non-orthogonal).
355  ORIENTATTR_IGNORE
356  };
357 
359  {
360  SCALEATTR_SCALE, //! scale (vec3).
361  SCALEATTR_PSCALE, //! single pscale.
362  SCALEATTR_IGNORE
363  };
364 
365  /** Create and set transform attributes over the given range.
366  The @a indexMap maps offset->range_index, as computed by
367  ComputeRangeIndexMap().*/
368  static bool SetTransformAttrs(GU_Detail& gd,
369  const GA_Range& r,
370  const GA_OffsetArray& indexMap,
371  OrientAttrRepresentation orientRep,
372  ScaleAttrRepresentation scaleRep,
373  const UT_Matrix4D* xforms);
374 
375  static bool SetPackedPrimTransforms(GU_Detail& gd,
376  const GA_Range& r,
377  const UT_Matrix4D* xforms);
378 
379  static bool MultTransformableAttrs(GU_Detail& gd,
380  const GA_Range& r,
381  const GA_OffsetArray& indexMap,
382  const UT_Matrix4D* xforms,
383  bool keepLengths=false,
384  const GA_AttributeFilter* filter=nullptr);
385 
386  /// Imports \p prim as unpacked geometry in \p gd.
387  static bool ImportPrimUnpacked(GU_Detail& gd,
388  const UsdPrim& prim,
390  const char* lod = nullptr,
391  GusdPurposeSet purpose = GusdPurposeSet(
393  const UT_StringRef &primvarPattern = "*",
394  const UT_StringRef &attributePattern = UT_StringHolder::theEmptyString,
395  bool translateSTtoUV = true,
396  const UT_StringRef &nonTransformingPrimvarPattern = GA_Names::rest,
397  const UT_Matrix4D* xform = nullptr,
398  const GT_RefineParms* refineParms = nullptr);
399 };
400 
402 
403 #endif /*_GUSD_GU_USD_H_*/
Definition of a geometry attribute.
Definition: GA_Attribute.h:203
GT_API const UT_StringHolder time
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
UT_ErrorSeverity
Definition: UT_Error.h:25
quaternion orient.
Definition: GU_USD.h:354
#define GUSD_PRIMPATH_ATTR
Definition: GU_USD.h:55
A range of elements in an index-map.
Definition: GA_Range.h:42
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2621
#define GUSD_PATH_ATTR
Definition: GU_USD.h:54
GA_Size GA_Offset
Definition: GA_Types.h:653
GusdUSD_Traverse::PrimIndexPair PrimIndexPair
Definition: GU_USD.h:236
Definition: token.h:70
static const UT_StringHolder theEmptyString
Methods for USD scene traversal.
std::pair< UsdPrim, exint > PrimIndexPair
Definition: USD_Traverse.h:60
OrientAttrRepresentation
Definition: GU_USD.h:351
Definition: prim.h:116
Definition: path.h:280
GA_AttributeOwner
Definition: GA_Types.h:35
GLenum func
Definition: glcorearb.h:783
#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)
Container class for all geometry.
Definition: GA_Detail.h:105
#define GUSD_API
Definition: api.h:42
#define GUSD_VARIANTS_ATTR
Definition: GU_USD.h:57
ScaleAttrRepresentation
Definition: GU_USD.h:358
GLboolean r
Definition: glcorearb.h:1222
GusdPurposeSet
Definition: purpose.h:39
GA_API const UT_StringHolder rest
GLint lod
Definition: glcorearb.h:2765
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297