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 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 PXR_USD_IMAGING_USD_SKEL_IMAGING_SKELETON_ADAPTER_H
25 #define PXR_USD_IMAGING_USD_SKEL_IMAGING_SKELETON_ADAPTER_H
26 
27 #include "pxr/pxr.h"
30 
32 
35 #include "pxr/usd/usdSkel/cache.h"
38 
39 #include <unordered_map>
40 
41 
43 
44 
45 /// \class UsdImagingSkeletonAdapter
46 ///
47 /// Support for drawing bones of a UsdSkelSkeleton.
48 ///
50 {
51 public:
53 
55  : BaseAdapter()
56  {}
57 
60 
62  SdfPath
63  Populate(const UsdPrim& prim,
66  instancerContext=nullptr) override;
67 
69  bool IsSupported(const UsdImagingIndexProxy* index) const override;
70 
71  // ---------------------------------------------------------------------- //
72  /// \name Parallel Setup and Resolve
73  // ---------------------------------------------------------------------- //
74 
75  /// Thread Safe.
77  void TrackVariability(const UsdPrim& prim,
78  const SdfPath& cachePath,
79  HdDirtyBits* timeVaryingBits,
81  instancerContext = nullptr) const override;
82 
83  /// Thread Safe.
85  void UpdateForTime(const UsdPrim& prim,
86  const SdfPath& cachePath,
88  HdDirtyBits requestedBits,
90  instancerContext=nullptr) const override;
91 
92  // ---------------------------------------------------------------------- //
93  /// \name Change Processing API (public)
94  // ---------------------------------------------------------------------- //
95 
98  const SdfPath& cachePath,
99  const TfToken& propertyName) override;
100 
102  void ProcessPrimResync(SdfPath const& primPath,
103  UsdImagingIndexProxy* index) override;
104 
106  void ProcessPrimRemoval(SdfPath const& primPath,
107  UsdImagingIndexProxy* index) override;
108 
110  void MarkDirty(const UsdPrim& prim,
111  const SdfPath& cachePath,
112  HdDirtyBits dirty,
113  UsdImagingIndexProxy* index) override;
114 
116  void MarkRefineLevelDirty(UsdPrim const& prim,
117  SdfPath const& cachePath,
118  UsdImagingIndexProxy* index) override;
119 
121  void MarkReprDirty(UsdPrim const& prim,
122  SdfPath const& cachePath,
123  UsdImagingIndexProxy* index) override;
124 
126  void MarkCullStyleDirty(UsdPrim const& prim,
127  SdfPath const& cachePath,
128  UsdImagingIndexProxy* index) override;
129 
131  void MarkRenderTagDirty(UsdPrim const& prim,
132  SdfPath const& cachePath,
133  UsdImagingIndexProxy* index) override;
134 
136  void MarkTransformDirty(const UsdPrim& prim,
137  const SdfPath& cachePath,
138  UsdImagingIndexProxy* index) override;
139 
141  void MarkVisibilityDirty(const UsdPrim& prim,
142  const SdfPath& cachePath,
143  UsdImagingIndexProxy* index) override;
144 
146  void MarkMaterialDirty(const UsdPrim& prim,
147  const SdfPath& cachePath,
148  UsdImagingIndexProxy* index) override;
149 
150  // ---------------------------------------------------------------------- //
151  /// \name Computation API
152  // ---------------------------------------------------------------------- //
154  void InvokeComputation(SdfPath const& cachePath,
155  HdExtComputationContext* context) override;
156 
157  // ---------------------------------------------------------------------- //
158  /// \name Non-virtual public API
159  // ---------------------------------------------------------------------- //
160 
162  void RegisterSkelBinding(UsdSkelBinding const& binding);
163 
164  // ---------------------------------------------------------------------- //
165  /// \name Virtual public API
166  // ---------------------------------------------------------------------- //
167 
169  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
170  SdfPath const& cachePath,
171  UsdTimeCode time) const override;
172 
174  VtValue GetTopology(UsdPrim const& prim,
175  SdfPath const& cachePath,
176  UsdTimeCode time) const override;
177 
179  GfRange3d GetExtent(UsdPrim const& prim,
180  SdfPath const& cachePath,
181  UsdTimeCode time) const override;
182 
184  TfToken GetPurpose(UsdPrim const& prim,
185  SdfPath const& cachePath,
186  TfToken const& instanceInheritablePurpose)
187  const override;
188 
190  bool GetDoubleSided(UsdPrim const& prim,
191  SdfPath const& cachePath,
192  UsdTimeCode time) const override;
193 
194 
196  SdfPath GetMaterialId(UsdPrim const& prim,
197  SdfPath const& cachePath,
198  UsdTimeCode time) const override;
199 
200 
203  SdfPath const& cachePath) const override;
204 
207  GetExtComputationInputs(UsdPrim const& prim,
208  SdfPath const& cachePath,
209  const UsdImagingInstancerContext *instancerContext)
210  const override;
211 
213  GetExtComputationOutputs(UsdPrim const& prim,
214  SdfPath const& cachePath,
215  const UsdImagingInstancerContext* instancerContext)
216  const override;
217 
220  UsdPrim const& prim,
221  SdfPath const& cachePath,
222  HdInterpolation interpolation,
223  const UsdImagingInstancerContext* instancerContext) const override;
224 
226  VtValue
228  UsdPrim const& prim,
229  SdfPath const& cachePath,
230  TfToken const& name,
232  const UsdImagingInstancerContext* instancerContext) const override;
233 
235  size_t
237  UsdPrim const& prim,
238  SdfPath const& cachePath,
239  TfToken const& name,
241  const UsdImagingInstancerContext* instancerContext,
242  size_t maxSampleCount,
243  float *sampleTimes,
244  VtValue *sampleValues) override;
245 
247  std::string
249  UsdPrim const& prim,
250  SdfPath const& cachePath,
251  const UsdImagingInstancerContext* instancerContext) const override;
252 
253 
255  VtValue Get(UsdPrim const& prim,
256  SdfPath const& cachePath,
257  TfToken const& key,
259  VtIntArray *outIndices) const override;
260 
261 protected:
262  // ---------------------------------------------------------------------- //
263  /// \name Change Processing API (protected)
264  // ---------------------------------------------------------------------- //
265  void _RemovePrim(const SdfPath& cachePath,
266  UsdImagingIndexProxy* index) override;
267 
268 private:
269  // ---------------------------------------------------------------------- //
270  /// Handlers for the Bone Mesh
271  // ---------------------------------------------------------------------- //
272  bool _IsCallbackForSkeleton(const UsdPrim& prim) const;
273 
274  /// Returns a value holding color for \p prim,
275  /// taking into account explicitly authored color on the prim.
276  GfVec3f _GetSkeletonDisplayColor(const UsdPrim& prim,
277  UsdTimeCode time) const;
278 
279  /// Returns a value holding opacity for \p prim,
280  /// taking into account explicitly authored opacity on the prim.
281  float _GetSkeletonDisplayOpacity(const UsdPrim& prim,
282  UsdTimeCode time) const;
283 
284  void _TrackBoneMeshVariability(
285  const UsdPrim& prim,
286  const SdfPath& cachePath,
287  HdDirtyBits* timeVaryingBits,
289  instancerContext = nullptr) const;
290 
291  void _UpdateBoneMeshForTime(
292  const UsdPrim& prim,
293  const SdfPath& cachePath,
295  HdDirtyBits requestedBits,
296  const UsdImagingInstancerContext* instancerContext=nullptr) const;
297 
298  // ---------------------------------------------------------------------- //
299  /// Common utitily methods for skinning computations & skinned prims
300  // ---------------------------------------------------------------------- //
301  bool _IsAffectedByTimeVaryingSkelAnim(const SdfPath& skinnedPrimPath)
302  const;
303 
304  void _RemoveSkinnedPrimAndComputations(const SdfPath& cachePath,
306 
307  // ---------------------------------------------------------------------- //
308  /// Handlers for the skinning computations
309  // ---------------------------------------------------------------------- //
310  bool _IsSkinningComputationPath(const SdfPath& cachePath) const;
311 
312  bool
313  _IsSkinningInputAggregatorComputationPath(const SdfPath& cachePath)const;
314 
315  void _TrackSkinningComputationVariability(
316  const UsdPrim& skinnedPrim,
317  const SdfPath& computationPath,
318  HdDirtyBits* timeVaryingBits,
320  instancerContext = nullptr) const;
321 
322  VtVec3fArray _GetSkinnedPrimPoints(const UsdPrim& skinnedPrim,
323  const SdfPath& skinnedPrimCachePath,
324  UsdTimeCode time) const;
325 
326  SdfPath _GetSkinningComputationPath(const SdfPath& skinnedPrimPath) const;
327 
328  SdfPath _GetSkinningInputAggregatorComputationPath(
329  const SdfPath& skinnedPrimPath) const;
330 
331  // Static helper methods
332  static
333  std::string _LoadSkinningComputeKernel(const TfToken& kernelKey);
334 
335  static
336  const std::string& _GetLBSSkinningComputeKernel();
337 
338  static
339  const std::string& _GetDQSSkinningComputeKernel();
340 
341  // ---------------------------------------------------------------------- //
342  /// Handlers for the skinned prim
343  // ---------------------------------------------------------------------- //
344  bool _IsSkinnedPrimPath(const SdfPath& cachePath) const;
345 
346  void _TrackSkinnedPrimVariability(
347  const UsdPrim& prim,
348  const SdfPath& cachePath,
349  HdDirtyBits* timeVaryingBits,
351  instancerContext = nullptr) const;
352 
353  void _UpdateSkinnedPrimForTime(
354  const UsdPrim& prim,
355  const SdfPath& cachePath,
357  HdDirtyBits requestedBits,
358  const UsdImagingInstancerContext* instancerContext=nullptr) const;
359 
360  // ---------------------------------------------------------------------- //
361  /// GetExtComputationInput() helpers
362  // ---------------------------------------------------------------------- //
363 
364  VtValue
365  _GetExtComputationInputForSkinningComputation(
366  UsdPrim const& prim,
367  SdfPath const& cachePath,
368  TfToken const& name,
370  const UsdImagingInstancerContext* instancerContext) const;
371 
372  VtValue
373  _GetExtComputationInputForInputAggregator(
374  UsdPrim const& prim,
375  SdfPath const& cachePath,
376  TfToken const& name,
378  const UsdImagingInstancerContext* instancerContext) const;
379 
380  size_t
381  _SampleExtComputationInputForSkinningComputation(
382  UsdPrim const& prim,
383  SdfPath const& cachePath,
384  TfToken const& name,
386  const UsdImagingInstancerContext* instancerContext,
387  size_t maxSampleCount,
388  float *sampleTimes,
389  VtValue *sampleValues);
390 
391  size_t
392  _SampleExtComputationInputForInputAggregator(
393  UsdPrim const& prim,
394  SdfPath const& cachePath,
395  TfToken const& name,
397  const UsdImagingInstancerContext* instancerContext,
398  size_t maxSampleCount,
399  float *sampleTimes,
400  VtValue *sampleValues);
401 
402  /// Returns the sample time offset that should be reported for computation
403  /// inputs which are not time-varying.
404  double _GetDefaultSampleTime(UsdTimeCode time);
405 
406 
407  // ---------------------------------------------------------------------- //
408  /// Populated skeleton state
409  // ---------------------------------------------------------------------- //
410  /// Data for a skel instance.
411  struct _SkelData {
412 
413  UsdSkelSkeletonQuery skelQuery;
414  SdfPathSet skelRootPaths;
415 
416  /// Compute bone mesh topology, and intiailize
417  /// other rest-state data for imaging bones.
418  HdMeshTopology ComputeTopologyAndRestState();
419 
420  /// Compute animated bone mesh points.
421  VtVec3fArray ComputePoints(UsdTimeCode time) const;
422 
423  /// Returns the purpose opinion authored on the skeleton prim or its
424  /// ancestors. If none exists, returns an empty token.
425  TfToken ComputePurpose() const;
426 
427  private:
428  // Cache of a mesh for a skeleton (at rest)
429  // TODO: Dedupe this infromation across UsdSkelSkeleton instances.
430  VtVec3fArray _boneMeshPoints;
431  VtIntArray _boneMeshJointIndices;
432  size_t _numJoints;
433  };
434 
435  _SkelData* _GetSkelData(const SdfPath& cachePath) const;
436 
437  UsdSkelCache _skelCache;
438  using _SkelDataMap =
439  std::unordered_map<SdfPath, std::shared_ptr<_SkelData>, SdfPath::Hash>;
440  _SkelDataMap _skelDataCache;
441 
442  // Data for each skinned prim.
443  struct _SkinnedPrimData {
444  _SkinnedPrimData() = default;
445 
446  /// Construct skinned prim, referencing the skeleton at \p skelPath.
447  /// XXX: We cannot extract the skel path from the skelQuery because
448  /// the skelQuery might refer to a prim within an instance master.
449  _SkinnedPrimData(const SdfPath& skelPath,
450  const UsdSkelSkeletonQuery& skelQuery,
451  const UsdSkelSkinningQuery& skinningQuery,
452  const SdfPath& skelRootPath);
453 
454  std::shared_ptr<UsdSkelBlendShapeQuery> blendShapeQuery;
455  UsdSkelSkinningQuery skinningQuery;
456  UsdSkelAnimQuery animQuery;
457  SdfPath skelPath, skelRootPath;
458  bool hasJointInfluences = false;
459  };
460 
461  const _SkinnedPrimData* _GetSkinnedPrimData(const SdfPath& cachePath) const;
462 
463  using _SkinnedPrimDataMap =
464  std::unordered_map<SdfPath, _SkinnedPrimData, SdfPath::Hash>;
465  _SkinnedPrimDataMap _skinnedPrimDataCache;
466 
467  // ---------------------------------------------------------------------- //
468  /// Skeleton -> Skinned Prim(s) state
469  /// (Populated via UsdSkelImagingSkelRootAdapter::Populate)
470  // ---------------------------------------------------------------------- //
471 
472  using _SkelBindingMap =
473  std::unordered_map<SdfPath, UsdSkelBinding, SdfPath::Hash>;
474  _SkelBindingMap _skelBindingMap;
475 };
476 
477 
479 
480 #endif // USDSKELIMAGING_SKELETONADAPTER
481 
#define USDIMAGING_API
Definition: api.h:40
USDIMAGING_API std::string GetExtComputationKernel(UsdPrim const &prim, SdfPath const &cachePath, const UsdImagingInstancerContext *instancerContext) const override
GT_API const UT_StringHolder time
uint32_t HdDirtyBits
Definition: types.h:158
USDSKELIMAGING_API void MarkDirty(const UsdPrim &prim, const SdfPath &cachePath, HdDirtyBits dirty, UsdImagingIndexProxy *index) override
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
USDSKELIMAGING_API void TrackVariability(const UsdPrim &prim, const SdfPath &cachePath, HdDirtyBits *timeVaryingBits, const UsdImagingInstancerContext *instancerContext=nullptr) const override
Thread Safe.
HdExtComputationOutputDescriptorVector GetExtComputationOutputs(UsdPrim const &prim, SdfPath const &cachePath, const UsdImagingInstancerContext *instancerContext) const override
USDIMAGING_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 bool IsSupported(const UsdImagingIndexProxy *index) const override
Returns true if the adapter can be populated into the target index.
std::vector< HdExtComputationInputDescriptor > HdExtComputationInputDescriptorVector
HdExtComputationPrimvarDescriptorVector GetExtComputationPrimvars(UsdPrim const &prim, SdfPath const &cachePath, HdInterpolation interpolation, const UsdImagingInstancerContext *instancerContext) const override
virtual USDSKELIMAGING_API ~UsdSkelImagingSkeletonAdapter()
Definition: vec3f.h:62
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 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:87
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
void _RemovePrim(const SdfPath &cachePath, UsdImagingIndexProxy *index) override
Definition: prim.h:133
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
USDIMAGING_API VtValue GetExtComputationInput(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &name, UsdTimeCode time, const UsdImagingInstancerContext *instancerContext) const override
GLuint const GLchar * name
Definition: glcorearb.h:786
USDSKELIMAGING_API void MarkRenderTagDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Definition: path.h:290
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:209
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
#define USDSKELIMAGING_API
Definition: api.h:40
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
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:194
USDSKELIMAGING_API HdExtComputationInputDescriptorVector GetExtComputationInputs(UsdPrim const &prim, SdfPath const &cachePath, const UsdImagingInstancerContext *instancerContext) const override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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 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 const TfTokenVector & GetExtComputationSceneInputNames(SdfPath const &cachePath) const override
Definition: value.h:164
USDSKELIMAGING_API void MarkCullStyleDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
std::vector< HdExtComputationPrimvarDescriptor > HdExtComputationPrimvarDescriptorVector