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 <hboost/unordered_map.hpp>
40 #include <unordered_map>
41 
42 
44 
45 
46 /// \class UsdImagingSkeletonAdapter
47 ///
48 /// Support for drawing bones of a UsdSkelSkeleton.
49 ///
51 {
52 public:
54 
56  : BaseAdapter()
57  {}
58 
61 
63  SdfPath
64  Populate(const UsdPrim& prim,
67  instancerContext=nullptr) override;
68 
70  bool IsSupported(const UsdImagingIndexProxy* index) const override;
71 
72  // ---------------------------------------------------------------------- //
73  /// \name Parallel Setup and Resolve
74  // ---------------------------------------------------------------------- //
75 
76  /// Thread Safe.
78  void TrackVariability(const UsdPrim& prim,
79  const SdfPath& cachePath,
80  HdDirtyBits* timeVaryingBits,
82  instancerContext = nullptr) const override;
83 
84  /// Thread Safe.
86  void UpdateForTime(const UsdPrim& prim,
87  const SdfPath& cachePath,
89  HdDirtyBits requestedBits,
91  instancerContext=nullptr) const override;
92 
93  // ---------------------------------------------------------------------- //
94  /// \name Change Processing API (public)
95  // ---------------------------------------------------------------------- //
96 
99  const SdfPath& cachePath,
100  const TfToken& propertyName) override;
101 
103  void ProcessPrimResync(SdfPath const& primPath,
104  UsdImagingIndexProxy* index) override;
105 
107  void ProcessPrimRemoval(SdfPath const& primPath,
108  UsdImagingIndexProxy* index) override;
109 
111  void MarkDirty(const UsdPrim& prim,
112  const SdfPath& cachePath,
113  HdDirtyBits dirty,
114  UsdImagingIndexProxy* index) override;
115 
117  void MarkRefineLevelDirty(UsdPrim const& prim,
118  SdfPath const& cachePath,
119  UsdImagingIndexProxy* index) override;
120 
122  void MarkReprDirty(UsdPrim const& prim,
123  SdfPath const& cachePath,
124  UsdImagingIndexProxy* index) override;
125 
127  void MarkCullStyleDirty(UsdPrim const& prim,
128  SdfPath const& cachePath,
129  UsdImagingIndexProxy* index) override;
130 
132  void MarkRenderTagDirty(UsdPrim const& prim,
133  SdfPath const& cachePath,
134  UsdImagingIndexProxy* index) override;
135 
137  void MarkTransformDirty(const UsdPrim& prim,
138  const SdfPath& cachePath,
139  UsdImagingIndexProxy* index) override;
140 
142  void MarkVisibilityDirty(const UsdPrim& prim,
143  const SdfPath& cachePath,
144  UsdImagingIndexProxy* index) override;
145 
147  void MarkMaterialDirty(const UsdPrim& prim,
148  const SdfPath& cachePath,
149  UsdImagingIndexProxy* index) override;
150 
151  // ---------------------------------------------------------------------- //
152  /// \name Computation API
153  // ---------------------------------------------------------------------- //
155  void InvokeComputation(SdfPath const& cachePath,
156  HdExtComputationContext* context) override;
157 
158  // ---------------------------------------------------------------------- //
159  /// \name Non-virtual public API
160  // ---------------------------------------------------------------------- //
161 
163  void RegisterSkelBinding(UsdSkelBinding const& binding);
164 
165  // ---------------------------------------------------------------------- //
166  /// \name Virtual public API
167  // ---------------------------------------------------------------------- //
168 
170  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
171  SdfPath const& cachePath,
172  UsdTimeCode time) const override;
173 
175  VtValue GetTopology(UsdPrim const& prim,
176  SdfPath const& cachePath,
177  UsdTimeCode time) const override;
178 
180  GfRange3d GetExtent(UsdPrim const& prim,
181  SdfPath const& cachePath,
182  UsdTimeCode time) const override;
183 
185  TfToken GetPurpose(UsdPrim const& prim,
186  SdfPath const& cachePath,
187  TfToken const& instanceInheritablePurpose)
188  const override;
189 
191  bool GetDoubleSided(UsdPrim const& prim,
192  SdfPath const& cachePath,
193  UsdTimeCode time) const override;
194 
195 
197  SdfPath GetMaterialId(UsdPrim const& prim,
198  SdfPath const& cachePath,
199  UsdTimeCode time) const override;
200 
201 
204  SdfPath const& cachePath) const override;
205 
208  GetExtComputationInputs(UsdPrim const& prim,
209  SdfPath const& cachePath,
210  const UsdImagingInstancerContext *instancerContext)
211  const override;
212 
214  GetExtComputationOutputs(UsdPrim const& prim,
215  SdfPath const& cachePath,
216  const UsdImagingInstancerContext* instancerContext)
217  const override;
218 
221  UsdPrim const& prim,
222  SdfPath const& cachePath,
223  HdInterpolation interpolation,
224  const UsdImagingInstancerContext* instancerContext) const override;
225 
227  VtValue
229  UsdPrim const& prim,
230  SdfPath const& cachePath,
231  TfToken const& name,
233  const UsdImagingInstancerContext* instancerContext) const override;
234 
236  size_t
238  UsdPrim const& prim,
239  SdfPath const& cachePath,
240  TfToken const& name,
242  const UsdImagingInstancerContext* instancerContext,
243  size_t maxSampleCount,
244  float *sampleTimes,
245  VtValue *sampleValues) override;
246 
248  std::string
250  UsdPrim const& prim,
251  SdfPath const& cachePath,
252  const UsdImagingInstancerContext* instancerContext) const override;
253 
254 
256  VtValue Get(UsdPrim const& prim,
257  SdfPath const& cachePath,
258  TfToken const& key,
260  VtIntArray *outIndices) const override;
261 
262 protected:
263  // ---------------------------------------------------------------------- //
264  /// \name Change Processing API (protected)
265  // ---------------------------------------------------------------------- //
266  void _RemovePrim(const SdfPath& cachePath,
267  UsdImagingIndexProxy* index) override;
268 
269 private:
270  // ---------------------------------------------------------------------- //
271  /// Handlers for the Bone Mesh
272  // ---------------------------------------------------------------------- //
273  bool _IsCallbackForSkeleton(const UsdPrim& prim) const;
274 
275  /// Returns a value holding color for \p prim,
276  /// taking into account explicitly authored color on the prim.
277  GfVec3f _GetSkeletonDisplayColor(const UsdPrim& prim,
278  UsdTimeCode time) const;
279 
280  /// Returns a value holding opacity for \p prim,
281  /// taking into account explicitly authored opacity on the prim.
282  float _GetSkeletonDisplayOpacity(const UsdPrim& prim,
283  UsdTimeCode time) const;
284 
285  void _TrackBoneMeshVariability(
286  const UsdPrim& prim,
287  const SdfPath& cachePath,
288  HdDirtyBits* timeVaryingBits,
290  instancerContext = nullptr) const;
291 
292  void _UpdateBoneMeshForTime(
293  const UsdPrim& prim,
294  const SdfPath& cachePath,
296  HdDirtyBits requestedBits,
297  const UsdImagingInstancerContext* instancerContext=nullptr) const;
298 
299  // ---------------------------------------------------------------------- //
300  /// Common utitily methods for skinning computations & skinned prims
301  // ---------------------------------------------------------------------- //
302  bool _IsAffectedByTimeVaryingSkelAnim(const SdfPath& skinnedPrimPath)
303  const;
304 
305  void _RemoveSkinnedPrimAndComputations(const SdfPath& cachePath,
307 
308  // ---------------------------------------------------------------------- //
309  /// Handlers for the skinning computations
310  // ---------------------------------------------------------------------- //
311  bool _IsSkinningComputationPath(const SdfPath& cachePath) const;
312 
313  bool
314  _IsSkinningInputAggregatorComputationPath(const SdfPath& cachePath)const;
315 
316  void _TrackSkinningComputationVariability(
317  const UsdPrim& skinnedPrim,
318  const SdfPath& computationPath,
319  HdDirtyBits* timeVaryingBits,
321  instancerContext = nullptr) const;
322 
323  VtVec3fArray _GetSkinnedPrimPoints(const UsdPrim& skinnedPrim,
324  const SdfPath& skinnedPrimCachePath,
325  UsdTimeCode time) const;
326 
327  SdfPath _GetSkinningComputationPath(const SdfPath& skinnedPrimPath) const;
328 
329  SdfPath _GetSkinningInputAggregatorComputationPath(
330  const SdfPath& skinnedPrimPath) const;
331 
332  // Static helper methods
333  static
334  std::string _LoadSkinningComputeKernel(const TfToken& kernelKey);
335 
336  static
337  const std::string& _GetLBSSkinningComputeKernel();
338 
339  static
340  const std::string& _GetDQSSkinningComputeKernel();
341 
342  // ---------------------------------------------------------------------- //
343  /// Handlers for the skinned prim
344  // ---------------------------------------------------------------------- //
345  bool _IsSkinnedPrimPath(const SdfPath& cachePath) const;
346 
347  void _TrackSkinnedPrimVariability(
348  const UsdPrim& prim,
349  const SdfPath& cachePath,
350  HdDirtyBits* timeVaryingBits,
352  instancerContext = nullptr) const;
353 
354  void _UpdateSkinnedPrimForTime(
355  const UsdPrim& prim,
356  const SdfPath& cachePath,
358  HdDirtyBits requestedBits,
359  const UsdImagingInstancerContext* instancerContext=nullptr) const;
360 
361  // ---------------------------------------------------------------------- //
362  /// GetExtComputationInput() helpers
363  // ---------------------------------------------------------------------- //
364 
365  VtValue
366  _GetExtComputationInputForSkinningComputation(
367  UsdPrim const& prim,
368  SdfPath const& cachePath,
369  TfToken const& name,
371  const UsdImagingInstancerContext* instancerContext) const;
372 
373  VtValue
374  _GetExtComputationInputForInputAggregator(
375  UsdPrim const& prim,
376  SdfPath const& cachePath,
377  TfToken const& name,
379  const UsdImagingInstancerContext* instancerContext) const;
380 
381  size_t
382  _SampleExtComputationInputForSkinningComputation(
383  UsdPrim const& prim,
384  SdfPath const& cachePath,
385  TfToken const& name,
387  const UsdImagingInstancerContext* instancerContext,
388  size_t maxSampleCount,
389  float *sampleTimes,
390  VtValue *sampleValues);
391 
392  size_t
393  _SampleExtComputationInputForInputAggregator(
394  UsdPrim const& prim,
395  SdfPath const& cachePath,
396  TfToken const& name,
398  const UsdImagingInstancerContext* instancerContext,
399  size_t maxSampleCount,
400  float *sampleTimes,
401  VtValue *sampleValues);
402 
403  /// Returns the sample time offset that should be reported for computation
404  /// inputs which are not time-varying.
405  double _GetDefaultSampleTime(UsdTimeCode time);
406 
407 
408  // ---------------------------------------------------------------------- //
409  /// Populated skeleton state
410  // ---------------------------------------------------------------------- //
411  /// Data for a skel instance.
412  struct _SkelData {
413 
414  UsdSkelSkeletonQuery skelQuery;
415  SdfPathSet skelRootPaths;
416 
417  /// Compute bone mesh topology, and intiailize
418  /// other rest-state data for imaging bones.
419  HdMeshTopology ComputeTopologyAndRestState();
420 
421  /// Compute animated bone mesh points.
422  VtVec3fArray ComputePoints(UsdTimeCode time) const;
423 
424  TfToken ComputePurpose() const;
425 
426  private:
427  // Cache of a mesh for a skeleton (at rest)
428  // TODO: Dedupe this infromation across UsdSkelSkeleton instances.
429  VtVec3fArray _boneMeshPoints;
430  VtIntArray _boneMeshJointIndices;
431  size_t _numJoints;
432  };
433 
434  _SkelData* _GetSkelData(const SdfPath& cachePath) const;
435 
436  UsdSkelCache _skelCache;
437  using _SkelDataMap =
438  std::unordered_map<SdfPath, std::shared_ptr<_SkelData>, SdfPath::Hash>;
439  _SkelDataMap _skelDataCache;
440 
441  // Data for each skinned prim.
442  struct _SkinnedPrimData {
443  _SkinnedPrimData() = default;
444 
445  /// Construct skinned prim, referencing the skeleton at \p skelPath.
446  /// XXX: We cannot extract the skel path from the skelQuery because
447  /// the skelQuery might refer to a prim within an instance master.
448  _SkinnedPrimData(const SdfPath& skelPath,
449  const UsdSkelSkeletonQuery& skelQuery,
450  const UsdSkelSkinningQuery& skinningQuery,
451  const SdfPath& skelRootPath);
452 
453  std::shared_ptr<UsdSkelBlendShapeQuery> blendShapeQuery;
454  UsdSkelSkinningQuery skinningQuery;
455  UsdSkelAnimQuery animQuery;
456  SdfPath skelPath, skelRootPath;
457  bool hasJointInfluences = false;
458  };
459 
460  const _SkinnedPrimData* _GetSkinnedPrimData(const SdfPath& cachePath) const;
461 
462  using _SkinnedPrimDataMap =
463  std::unordered_map<SdfPath, _SkinnedPrimData, SdfPath::Hash>;
464  _SkinnedPrimDataMap _skinnedPrimDataCache;
465 
466  // ---------------------------------------------------------------------- //
467  /// Skeleton -> Skinned Prim(s) state
468  /// (Populated via UsdSkelImagingSkelRootAdapter::Populate)
469  // ---------------------------------------------------------------------- //
470 
471  using _SkelBindingMap =
472  std::unordered_map<SdfPath, UsdSkelBinding, SdfPath::Hash>;
473  _SkelBindingMap _skelBindingMap;
474 };
475 
476 
478 
479 #endif // USDSKELIMAGING_SKELETONADAPTER
480 
#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:135
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
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:291
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:210
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:1441
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:167
USDSKELIMAGING_API void MarkCullStyleDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
std::vector< HdExtComputationPrimvarDescriptor > HdExtComputationPrimvarDescriptorVector