HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
skeletonAdapter.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_IMAGING_USD_SKEL_IMAGING_SKELETON_ADAPTER_H
8 #define PXR_USD_IMAGING_USD_SKEL_IMAGING_SKELETON_ADAPTER_H
9 
10 #include "pxr/pxr.h"
13 
15 
18 #include "pxr/usd/usdSkel/cache.h"
21 
22 #include <unordered_map>
23 
25 
26 /// \class UsdSkelImagingSkeletonAdapter
27 ///
28 /// Prim adapter for UsdSkel's Skeleton.
29 ///
31 {
32 public:
34 
36  : BaseAdapter()
37  {}
38 
41 
43  SdfPath
44  Populate(const UsdPrim& prim,
47  instancerContext=nullptr) override;
48 
50  bool IsSupported(const UsdImagingIndexProxy* index) const override;
51 
52  // ---------------------------------------------------------------------- //
53  /// \name Parallel Setup and Resolve
54  // ---------------------------------------------------------------------- //
55 
56  /// Thread Safe.
58  void TrackVariability(const UsdPrim& prim,
59  const SdfPath& cachePath,
60  HdDirtyBits* timeVaryingBits,
62  instancerContext = nullptr) const override;
63 
64  /// Thread Safe.
66  void UpdateForTime(const UsdPrim& prim,
67  const SdfPath& cachePath,
69  HdDirtyBits requestedBits,
71  instancerContext=nullptr) const override;
72 
73  // ---------------------------------------------------------------------- //
74  /// \name Change Processing API (public)
75  // ---------------------------------------------------------------------- //
76 
79  const SdfPath& cachePath,
80  const TfToken& propertyName) override;
81 
83  void ProcessPrimResync(SdfPath const& primPath,
84  UsdImagingIndexProxy* index) override;
85 
87  void ProcessPrimRemoval(SdfPath const& primPath,
88  UsdImagingIndexProxy* index) override;
89 
91  void MarkDirty(const UsdPrim& prim,
92  const SdfPath& cachePath,
93  HdDirtyBits dirty,
94  UsdImagingIndexProxy* index) override;
95 
97  void MarkRefineLevelDirty(UsdPrim const& prim,
98  SdfPath const& cachePath,
99  UsdImagingIndexProxy* index) override;
100 
102  void MarkReprDirty(UsdPrim const& prim,
103  SdfPath const& cachePath,
104  UsdImagingIndexProxy* index) override;
105 
107  void MarkCullStyleDirty(UsdPrim const& prim,
108  SdfPath const& cachePath,
109  UsdImagingIndexProxy* index) override;
110 
112  void MarkRenderTagDirty(UsdPrim const& prim,
113  SdfPath const& cachePath,
114  UsdImagingIndexProxy* index) override;
115 
117  void MarkTransformDirty(const UsdPrim& prim,
118  const SdfPath& cachePath,
119  UsdImagingIndexProxy* index) override;
120 
122  void MarkVisibilityDirty(const UsdPrim& prim,
123  const SdfPath& cachePath,
124  UsdImagingIndexProxy* index) override;
125 
127  void MarkMaterialDirty(const UsdPrim& prim,
128  const SdfPath& cachePath,
129  UsdImagingIndexProxy* index) override;
130 
131  // ---------------------------------------------------------------------- //
132  /// \name Computation API
133  // ---------------------------------------------------------------------- //
135  void InvokeComputation(SdfPath const& cachePath,
136  HdExtComputationContext* context) override;
137 
138  // ---------------------------------------------------------------------- //
139  /// \name Non-virtual public API
140  // ---------------------------------------------------------------------- //
141 
143  void RegisterSkelBinding(UsdSkelBinding const& binding);
144 
145  // ---------------------------------------------------------------------- //
146  /// \name Virtual public API
147  // ---------------------------------------------------------------------- //
148 
150  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
151  SdfPath const& cachePath,
152  UsdTimeCode time) const override;
153 
155  VtValue GetTopology(UsdPrim const& prim,
156  SdfPath const& cachePath,
157  UsdTimeCode time) const override;
158 
160  GfRange3d GetExtent(UsdPrim const& prim,
161  SdfPath const& cachePath,
162  UsdTimeCode time) const override;
163 
165  TfToken GetPurpose(UsdPrim const& prim,
166  SdfPath const& cachePath,
167  TfToken const& instanceInheritablePurpose)
168  const override;
169 
171  bool GetDoubleSided(UsdPrim const& prim,
172  SdfPath const& cachePath,
173  UsdTimeCode time) const override;
174 
175 
177  SdfPath GetMaterialId(UsdPrim const& prim,
178  SdfPath const& cachePath,
179  UsdTimeCode time) const override;
180 
181 
184  SdfPath const& cachePath) const override;
185 
188  GetExtComputationInputs(UsdPrim const& prim,
189  SdfPath const& cachePath,
190  const UsdImagingInstancerContext *instancerContext)
191  const override;
192 
195  GetExtComputationOutputs(UsdPrim const& prim,
196  SdfPath const& cachePath,
197  const UsdImagingInstancerContext* instancerContext)
198  const override;
199 
203  UsdPrim const& prim,
204  SdfPath const& cachePath,
205  HdInterpolation interpolation,
206  const UsdImagingInstancerContext* instancerContext) const override;
207 
209  VtValue
211  UsdPrim const& prim,
212  SdfPath const& cachePath,
213  TfToken const& name,
215  const UsdImagingInstancerContext* instancerContext) const override;
216 
218  size_t
220  UsdPrim const& prim,
221  SdfPath const& cachePath,
222  TfToken const& name,
224  const UsdImagingInstancerContext* instancerContext,
225  size_t maxSampleCount,
226  float *sampleTimes,
227  VtValue *sampleValues) override;
228 
230  std::string
232  UsdPrim const& prim,
233  SdfPath const& cachePath,
234  const UsdImagingInstancerContext* instancerContext) const override;
235 
236 
238  VtValue Get(UsdPrim const& prim,
239  SdfPath const& cachePath,
240  TfToken const& key,
242  VtIntArray *outIndices) const override;
243 
244  // ---------------------------------------------------------------------- //
245  /// \name Scene Index Support
246  // ---------------------------------------------------------------------- //
248  TfTokenVector GetImagingSubprims(UsdPrim const &prim) override;
249 
252  UsdPrim const &prim,
253  TfToken const &subprim) override;
254 
256  HdContainerDataSourceHandle GetImagingSubprimData(
257  UsdPrim const& prim,
258  TfToken const& subprim,
259  const UsdImagingDataSourceStageGlobals &stageGlobals) override;
260 
263  UsdPrim const& prim,
264  TfToken const& subprim,
265  TfTokenVector const& properties,
266  UsdImagingPropertyInvalidationType invalidationType) override;
267 
268 protected:
269  // ---------------------------------------------------------------------- //
270  /// \name Change Processing API (protected)
271  // ---------------------------------------------------------------------- //
272  void _RemovePrim(const SdfPath& cachePath,
273  UsdImagingIndexProxy* index) override;
274 
275 private:
276  enum class _ComputationType : uint8_t {
277  Points,
278  Normals
279  };
280 
281  // ---------------------------------------------------------------------- //
282  /// Handlers for the Bone Mesh
283  // ---------------------------------------------------------------------- //
284  bool _IsCallbackForSkeleton(const UsdPrim& prim) const;
285 
286  /// Returns a value holding color for \p prim,
287  /// taking into account explicitly authored color on the prim.
288  GfVec3f _GetSkeletonDisplayColor(const UsdPrim& prim,
289  UsdTimeCode time) const;
290 
291  /// Returns a value holding opacity for \p prim,
292  /// taking into account explicitly authored opacity on the prim.
293  float _GetSkeletonDisplayOpacity(const UsdPrim& prim,
294  UsdTimeCode time) const;
295 
296  void _TrackBoneMeshVariability(
297  const UsdPrim& prim,
298  const SdfPath& cachePath,
299  HdDirtyBits* timeVaryingBits,
301  instancerContext = nullptr) const;
302 
303  void _UpdateBoneMeshForTime(
304  const UsdPrim& prim,
305  const SdfPath& cachePath,
307  HdDirtyBits requestedBits,
308  const UsdImagingInstancerContext* instancerContext=nullptr) const;
309 
310  // ---------------------------------------------------------------------- //
311  /// Common utitily methods for skinning computations & skinned prims
312  // ---------------------------------------------------------------------- //
313  bool _IsAffectedByTimeVaryingSkelAnim(const SdfPath& skinnedPrimPath)
314  const;
315 
316  void _RemoveSkinnedPrimAndComputations(const SdfPath& cachePath,
318 
319  // ---------------------------------------------------------------------- //
320  /// Handlers for the skinning computations
321  // ---------------------------------------------------------------------- //
322  bool _IsSkinningPointsComputationPath(const SdfPath& cachePath) const;
323 
324  bool _IsSkinningNormalsComputationPath(const SdfPath& cachePath) const;
325 
326  _ComputationType _GetSkinningComputationType(
327  const SdfPath& cachePath) const;
328 
329  bool _IsSkinningPointsInputAggregatorComputationPath(
330  const SdfPath& cachePath) const;
331 
332  bool _IsSkinningNormalsInputAggregatorComputationPath(
333  const SdfPath& cachePath) const;
334 
335  void _TrackSkinningComputationVariability(
336  const UsdPrim& skinnedPrim,
337  const SdfPath& computationPath,
338  HdDirtyBits* timeVaryingBits,
340  instancerContext = nullptr) const;
341 
342  VtVec3fArray _GetSkinnedPrimPoints(const UsdPrim& skinnedPrim,
343  const SdfPath& skinnedPrimCachePath,
344  UsdTimeCode time) const;
345 
346  VtVec3fArray _GetSkinnedPrimNormals(const UsdPrim& skinnedPrim,
347  const SdfPath& skinnedPrimCachePath,
348  UsdTimeCode time) const;
349 
350  VtIntArray _GetSkinnedPrimFaceVertexIndices(const UsdPrim& skinnedPrim,
351  const SdfPath& skinnedPrimCachePath,
352  UsdTimeCode time) const;
353 
354  SdfPath _GetSkinningComputationPath(
355  const SdfPath& skinnedPrimPath,
356  _ComputationType computationType) const;
357 
358  SdfPath _GetSkinningInputAggregatorComputationPath(
359  const SdfPath& skinnedPrimPath,
360  _ComputationType computationType) const;
361 
362  // Static helper methods
363  static
364  std::string _LoadSkinningComputeKernel(const TfToken& kernelKey);
365 
366  static
367  const std::string& _GetLBSSkinningComputeKernel(
368  _ComputationType computationType);
369 
370  static
371  const std::string& _GetDQSSkinningComputeKernel(
372  _ComputationType computationType);
373 
374  // ---------------------------------------------------------------------- //
375  /// Handlers for the skinned prim
376  // ---------------------------------------------------------------------- //
377  bool _IsSkinnedPrimPath(const SdfPath& cachePath) const;
378 
379  void _TrackSkinnedPrimVariability(
380  const UsdPrim& prim,
381  const SdfPath& cachePath,
382  HdDirtyBits* timeVaryingBits,
384  instancerContext = nullptr) const;
385 
386  void _UpdateSkinnedPrimForTime(
387  const UsdPrim& prim,
388  const SdfPath& cachePath,
390  HdDirtyBits requestedBits,
391  const UsdImagingInstancerContext* instancerContext=nullptr) const;
392 
393  // ---------------------------------------------------------------------- //
394  /// GetExtComputationInput() helpers
395  // ---------------------------------------------------------------------- //
396 
397  VtValue
398  _GetExtComputationInputForSkinningComputation(
399  UsdPrim const& prim,
400  SdfPath const& cachePath,
401  TfToken const& name,
403  const UsdImagingInstancerContext* instancerContext) const;
404 
405  VtValue
406  _GetExtComputationInputForInputAggregator(
407  UsdPrim const& prim,
408  SdfPath const& cachePath,
409  TfToken const& name,
411  const UsdImagingInstancerContext* instancerContext) const;
412 
413  size_t
414  _SampleExtComputationInputForSkinningComputation(
415  UsdPrim const& prim,
416  SdfPath const& cachePath,
417  TfToken const& name,
419  const UsdImagingInstancerContext* instancerContext,
420  size_t maxSampleCount,
421  float *sampleTimes,
422  VtValue *sampleValues);
423 
424  size_t
425  _SampleExtComputationInputForInputAggregator(
426  UsdPrim const& prim,
427  SdfPath const& cachePath,
428  TfToken const& name,
430  const UsdImagingInstancerContext* instancerContext,
431  size_t maxSampleCount,
432  float *sampleTimes,
433  VtValue *sampleValues);
434 
435  /// Returns the sample time offset that should be reported for computation
436  /// inputs which are not time-varying.
437  double _GetDefaultSampleTime(UsdTimeCode time);
438 
439  // ---------------------------------------------------------------------- //
440  /// Matrix helpers
441  // ---------------------------------------------------------------------- //
442  static
443  bool
444  _ExtractSkinningScaleXforms(
445  const VtMatrix4fArray& skinningXforms,
446  _ComputationType computationType,
447  VtMatrix3fArray* skinningScaleXforms);
448 
449  static
450  bool
451  _ExtractSkinningDualQuats(
452  const VtMatrix4fArray& skinningXforms,
453  _ComputationType computationType,
454  VtVec4fArray* skinningDualQuats);
455 
456  // ---------------------------------------------------------------------- //
457  /// Populated skeleton state
458  // ---------------------------------------------------------------------- //
459  /// Data for a skel instance.
460  struct _SkelData {
461 
462  UsdSkelSkeletonQuery skelQuery;
463  SdfPathSet skelRootPaths;
464 
465  /// Compute bone mesh topology, and intiailize
466  /// other rest-state data for imaging bones.
467  HdMeshTopology ComputeTopologyAndRestState();
468 
469  /// Compute animated bone mesh points.
470  VtVec3fArray ComputePoints(UsdTimeCode time) const;
471 
472  /// Returns the purpose opinion authored on the skeleton prim or its
473  /// ancestors. If none exists, returns an empty token.
474  TfToken ComputePurpose() const;
475 
476  private:
477  // Cache of a mesh for a skeleton (at rest)
478  // TODO: Dedupe this infromation across UsdSkelSkeleton instances.
479  VtVec3fArray _boneMeshPoints;
480  VtIntArray _boneMeshJointIndices;
481  size_t _numJoints;
482  };
483 
484  _SkelData* _GetSkelData(const SdfPath& cachePath) const;
485 
486  UsdSkelCache _skelCache;
487  using _SkelDataMap =
488  std::unordered_map<SdfPath, std::shared_ptr<_SkelData>, SdfPath::Hash>;
489  _SkelDataMap _skelDataCache;
490 
491  // Data for each skinned prim.
492  struct _SkinnedPrimData {
493  _SkinnedPrimData() = default;
494 
495  /// Construct skinned prim, referencing the skeleton at \p skelPath.
496  /// XXX: We cannot extract the skel path from the skelQuery because
497  /// the skelQuery might refer to a prim within an instance master.
498  _SkinnedPrimData(const SdfPath& skelPath,
499  const UsdSkelSkeletonQuery& skelQuery,
500  const UsdSkelSkinningQuery& skinningQuery,
501  const SdfPath& skelRootPath,
502  const UsdSkelImagingSkeletonAdapter* adapter);
503 
504  std::shared_ptr<UsdSkelBlendShapeQuery> blendShapeQuery;
505  UsdSkelSkinningQuery skinningQuery;
506  UsdSkelAnimQuery animQuery;
507  SdfPath skelPath, skelRootPath;
508  bool hasJointInfluences = false;
509  TfToken normalsInterpolation;
510  };
511 
512  const _SkinnedPrimData* _GetSkinnedPrimData(const SdfPath& cachePath) const;
513 
514  using _SkinnedPrimDataMap =
515  std::unordered_map<SdfPath, _SkinnedPrimData, SdfPath::Hash>;
516  _SkinnedPrimDataMap _skinnedPrimDataCache;
517 
518  // ---------------------------------------------------------------------- //
519  /// Skeleton -> Skinned Prim(s) state
520  /// (Populated via UsdSkelImagingSkelRootAdapter::Populate)
521  // ---------------------------------------------------------------------- //
522 
523  using _SkelBindingMap =
524  std::unordered_map<SdfPath, UsdSkelBinding, SdfPath::Hash>;
525  _SkelBindingMap _skelBindingMap;
526 };
527 
528 
530 
531 #endif // USDSKELIMAGING_SKELETONADAPTER
532 
USDSKELIMAGING_API HdDataSourceLocatorSet InvalidateImagingSubprim(UsdPrim const &prim, TfToken const &subprim, TfTokenVector const &properties, UsdImagingPropertyInvalidationType invalidationType) override
GT_API const UT_StringHolder time
uint32_t HdDirtyBits
Definition: types.h:143
USDSKELIMAGING_API void MarkDirty(const UsdPrim &prim, const SdfPath &cachePath, HdDirtyBits dirty, UsdImagingIndexProxy *index) override
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
USDSKELIMAGING_API void TrackVariability(const UsdPrim &prim, const SdfPath &cachePath, HdDirtyBits *timeVaryingBits, const UsdImagingInstancerContext *instancerContext=nullptr) const override
Thread Safe.
USDSKELIMAGING_API bool IsSupported(const UsdImagingIndexProxy *index) const override
Returns true if the adapter can be populated into the target index.
std::vector< HdExtComputationInputDescriptor > HdExtComputationInputDescriptorVector
virtual USDSKELIMAGING_API ~UsdSkelImagingSkeletonAdapter()
Definition: vec3f.h:45
USDSKELIMAGING_API HdExtComputationPrimvarDescriptorVector GetExtComputationPrimvars(UsdPrim const &prim, SdfPath const &cachePath, HdInterpolation interpolation, const UsdImagingInstancerContext *instancerContext) const override
USDSKELIMAGING_API VtValue GetExtComputationInput(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &name, UsdTimeCode time, const UsdImagingInstancerContext *instancerContext) const override
std::vector< HdExtComputationOutputDescriptor > HdExtComputationOutputDescriptorVector
USDSKELIMAGING_API void ProcessPrimRemoval(SdfPath const &primPath, UsdImagingIndexProxy *index) override
USDSKELIMAGING_API void MarkMaterialDirty(const UsdPrim &prim, const SdfPath &cachePath, UsdImagingIndexProxy *index) override
USDSKELIMAGING_API TfToken GetImagingSubprimType(UsdPrim const &prim, TfToken const &subprim) override
USDSKELIMAGING_API SdfPath Populate(const UsdPrim &prim, UsdImagingIndexProxy *index, const UsdImagingInstancerContext *instancerContext=nullptr) override
USDSKELIMAGING_API void MarkRefineLevelDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Definition: token.h:70
USDSKELIMAGING_API VtValue GetTopology(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
USDSKELIMAGING_API GfRange3d GetExtent(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
USDSKELIMAGING_API void InvokeComputation(SdfPath const &cachePath, HdExtComputationContext *context) override
USDSKELIMAGING_API std::string GetExtComputationKernel(UsdPrim const &prim, SdfPath const &cachePath, const UsdImagingInstancerContext *instancerContext) const override
void _RemovePrim(const SdfPath &cachePath, UsdImagingIndexProxy *index) override
Definition: prim.h:116
USDSKELIMAGING_API HdContainerDataSourceHandle GetImagingSubprimData(UsdPrim const &prim, TfToken const &subprim, const UsdImagingDataSourceStageGlobals &stageGlobals) override
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
GLuint const GLchar * name
Definition: glcorearb.h:786
USDSKELIMAGING_API void MarkRenderTagDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Definition: path.h:280
USDSKELIMAGING_API SdfPath GetMaterialId(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
The root transform provided by the delegate.
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:199
USDSKELIMAGING_API void MarkVisibilityDirty(const UsdPrim &prim, const SdfPath &cachePath, UsdImagingIndexProxy *index) override
USDSKELIMAGING_API HdDirtyBits ProcessPropertyChange(const UsdPrim &prim, const SdfPath &cachePath, const TfToken &propertyName) override
USDSKELIMAGING_API void MarkTransformDirty(const UsdPrim &prim, const SdfPath &cachePath, UsdImagingIndexProxy *index) override
USDSKELIMAGING_API TfTokenVector GetImagingSubprims(UsdPrim const &prim) override
#define USDSKELIMAGING_API
Definition: api.h:23
USDSKELIMAGING_API VtValue Get(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &key, UsdTimeCode time, VtIntArray *outIndices) const override
USDSKELIMAGING_API void ProcessPrimResync(SdfPath const &primPath, UsdImagingIndexProxy *index) override
GLuint index
Definition: glcorearb.h:786
HdInterpolation
Definition: enums.h:177
USDSKELIMAGING_API HdExtComputationInputDescriptorVector GetExtComputationInputs(UsdPrim const &prim, SdfPath const &cachePath, const UsdImagingInstancerContext *instancerContext) const override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDSKELIMAGING_API PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
USDSKELIMAGING_API void MarkReprDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
USDSKELIMAGING_API bool GetDoubleSided(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Reads double-sided from the given prim. If not authored, returns false.
USDSKELIMAGING_API void RegisterSkelBinding(UsdSkelBinding const &binding)
USDSKELIMAGING_API size_t SampleExtComputationInput(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &name, UsdTimeCode time, const UsdImagingInstancerContext *instancerContext, size_t maxSampleCount, float *sampleTimes, VtValue *sampleValues) override
USDSKELIMAGING_API void UpdateForTime(const UsdPrim &prim, const SdfPath &cachePath, UsdTimeCode time, HdDirtyBits requestedBits, const UsdImagingInstancerContext *instancerContext=nullptr) const override
Thread Safe.
USDSKELIMAGING_API TfToken GetPurpose(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &instanceInheritablePurpose) const override
USDSKELIMAGING_API HdExtComputationOutputDescriptorVector GetExtComputationOutputs(UsdPrim const &prim, SdfPath const &cachePath, const UsdImagingInstancerContext *instancerContext) const override
USDSKELIMAGING_API const TfTokenVector & GetExtComputationSceneInputNames(SdfPath const &cachePath) const override
Definition: value.h:89
UsdImagingPropertyInvalidationType
Definition: types.h:17
USDSKELIMAGING_API void MarkCullStyleDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
std::vector< HdExtComputationPrimvarDescriptor > HdExtComputationPrimvarDescriptorVector