skeletonAdapter.h
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"
29 #include "pxr/usdImaging/usdSkelImaging/api.h"
30 
31 #include "pxr/imaging/hd/meshTopology.h"
32 
35 #include "pxr/usd/usdSkel/cache.h"
38 
39 #include <boost/unordered_map.hpp>
40 #include <unordered_map>
41 
42 
43 PXR_NAMESPACE_OPEN_SCOPE
44 
45 
50 class UsdSkelImagingSkeletonAdapter : public UsdImagingPrimAdapter
51 {
52 public:
53  using BaseAdapter = UsdImagingPrimAdapter;
54 
55  UsdSkelImagingSkeletonAdapter()
56  : BaseAdapter()
57  {}
58 
59  USDSKELIMAGING_API
60  virtual ~UsdSkelImagingSkeletonAdapter();
61 
62  USDSKELIMAGING_API
63  SdfPath
64  Populate(const UsdPrim& prim,
65  UsdImagingIndexProxy* index,
67  instancerContext=nullptr) override;
68 
69  USDSKELIMAGING_API
70  bool IsSupported(const UsdImagingIndexProxy* index) const override;
71 
72  // ---------------------------------------------------------------------- //
74  // ---------------------------------------------------------------------- //
75 
77  USDSKELIMAGING_API
78  void TrackVariability(const UsdPrim& prim,
79  const SdfPath& cachePath,
80  HdDirtyBits* timeVaryingBits,
82  instancerContext = nullptr) const override;
83 
85  USDSKELIMAGING_API
86  void UpdateForTime(const UsdPrim& prim,
87  const SdfPath& cachePath,
88  UsdTimeCode time,
89  HdDirtyBits requestedBits,
91  instancerContext=nullptr) const override;
92 
93  // ---------------------------------------------------------------------- //
95  // ---------------------------------------------------------------------- //
96 
97  USDSKELIMAGING_API
98  HdDirtyBits ProcessPropertyChange(const UsdPrim& prim,
99  const SdfPath& cachePath,
100  const TfToken& propertyName) override;
101 
102  USDSKELIMAGING_API
103  void ProcessPrimResync(SdfPath const& primPath,
104  UsdImagingIndexProxy* index) override;
105 
106  USDSKELIMAGING_API
107  void ProcessPrimRemoval(SdfPath const& primPath,
108  UsdImagingIndexProxy* index) override;
109 
110  USDSKELIMAGING_API
111  void MarkDirty(const UsdPrim& prim,
112  const SdfPath& cachePath,
113  HdDirtyBits dirty,
114  UsdImagingIndexProxy* index) override;
115 
116  USDSKELIMAGING_API
117  void MarkRefineLevelDirty(UsdPrim const& prim,
118  SdfPath const& cachePath,
119  UsdImagingIndexProxy* index) override;
120 
121  USDSKELIMAGING_API
122  void MarkReprDirty(UsdPrim const& prim,
123  SdfPath const& cachePath,
124  UsdImagingIndexProxy* index) override;
125 
126  USDSKELIMAGING_API
127  void MarkCullStyleDirty(UsdPrim const& prim,
128  SdfPath const& cachePath,
129  UsdImagingIndexProxy* index) override;
130 
131  USDSKELIMAGING_API
132  void MarkRenderTagDirty(UsdPrim const& prim,
133  SdfPath const& cachePath,
134  UsdImagingIndexProxy* index) override;
135 
136  USDSKELIMAGING_API
137  void MarkTransformDirty(const UsdPrim& prim,
138  const SdfPath& cachePath,
139  UsdImagingIndexProxy* index) override;
140 
141  USDSKELIMAGING_API
142  void MarkVisibilityDirty(const UsdPrim& prim,
143  const SdfPath& cachePath,
144  UsdImagingIndexProxy* index) override;
145 
146  USDSKELIMAGING_API
147  void MarkMaterialDirty(const UsdPrim& prim,
148  const SdfPath& cachePath,
149  UsdImagingIndexProxy* index) override;
150 
151  // ---------------------------------------------------------------------- //
153  // ---------------------------------------------------------------------- //
154  USDSKELIMAGING_API
155  void InvokeComputation(SdfPath const& cachePath,
156  HdExtComputationContext* context) override;
157 
158  // ---------------------------------------------------------------------- //
160  // ---------------------------------------------------------------------- //
161 
162  USDSKELIMAGING_API
163  void RegisterSkelBinding(UsdSkelBinding const& binding);
164 
165  // ---------------------------------------------------------------------- //
167  // ---------------------------------------------------------------------- //
168 
169  USDSKELIMAGING_API
170  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
171  SdfPath const& cachePath,
172  UsdTimeCode time) const override;
173 
174  USDSKELIMAGING_API
175  VtValue GetTopology(UsdPrim const& prim,
176  SdfPath const& cachePath,
177  UsdTimeCode time) const override;
178 
179  USDSKELIMAGING_API
180  GfRange3d GetExtent(UsdPrim const& prim,
181  SdfPath const& cachePath,
182  UsdTimeCode time) const override;
183 
184  USDSKELIMAGING_API
185  TfToken GetPurpose(UsdPrim const& prim,
186  SdfPath const& cachePath,
187  TfToken const& instanceInheritablePurpose)
188  const override;
189 
190  USDSKELIMAGING_API
191  bool GetDoubleSided(UsdPrim const& prim,
192  SdfPath const& cachePath,
193  UsdTimeCode time) const override;
194 
195 
196  USDSKELIMAGING_API
197  SdfPath GetMaterialId(UsdPrim const& prim,
198  SdfPath const& cachePath,
199  UsdTimeCode time) const override;
200 
201 
202  USDSKELIMAGING_API
203  const TfTokenVector &GetExtComputationSceneInputNames(
204  SdfPath const& cachePath) const override;
205 
206  USDSKELIMAGING_API
207  HdExtComputationInputDescriptorVector
208  GetExtComputationInputs(UsdPrim const& prim,
209  SdfPath const& cachePath,
210  const UsdImagingInstancerContext *instancerContext)
211  const override;
212 
213  HdExtComputationOutputDescriptorVector
214  GetExtComputationOutputs(UsdPrim const& prim,
215  SdfPath const& cachePath,
216  const UsdImagingInstancerContext* instancerContext)
217  const override;
218 
219  HdExtComputationPrimvarDescriptorVector
220  GetExtComputationPrimvars(
221  UsdPrim const& prim,
222  SdfPath const& cachePath,
223  HdInterpolation interpolation,
224  const UsdImagingInstancerContext* instancerContext) const override;
225 
226  USDIMAGING_API
227  VtValue
228  GetExtComputationInput(
229  UsdPrim const& prim,
230  SdfPath const& cachePath,
231  TfToken const& name,
232  UsdTimeCode time,
233  const UsdImagingInstancerContext* instancerContext) const override;
234 
235  USDIMAGING_API
236  size_t
237  SampleExtComputationInput(
238  UsdPrim const& prim,
239  SdfPath const& cachePath,
240  TfToken const& name,
241  UsdTimeCode time,
242  const UsdImagingInstancerContext* instancerContext,
243  size_t maxSampleCount,
244  float *sampleTimes,
245  VtValue *sampleValues) override;
246 
247  USDIMAGING_API
248  std::string
249  GetExtComputationKernel(
250  UsdPrim const& prim,
251  SdfPath const& cachePath,
252  const UsdImagingInstancerContext* instancerContext) const override;
253 
254 
255  USDSKELIMAGING_API
256  VtValue Get(UsdPrim const& prim,
257  SdfPath const& cachePath,
258  TfToken const& key,
259  UsdTimeCode time,
260  VtIntArray *outIndices) const override;
261 
262 protected:
263  // ---------------------------------------------------------------------- //
265  // ---------------------------------------------------------------------- //
266  void _RemovePrim(const SdfPath& cachePath,
267  UsdImagingIndexProxy* index) override;
268 
269 private:
270  // ---------------------------------------------------------------------- //
272  // ---------------------------------------------------------------------- //
273  bool _IsCallbackForSkeleton(const UsdPrim& prim) const;
274 
277  GfVec3f _GetSkeletonDisplayColor(const UsdPrim& prim,
278  UsdTimeCode time) const;
279 
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,
295  UsdTimeCode time,
296  HdDirtyBits requestedBits,
297  const UsdImagingInstancerContext* instancerContext=nullptr) const;
298 
299  // ---------------------------------------------------------------------- //
301  // ---------------------------------------------------------------------- //
302  bool _IsAffectedByTimeVaryingSkelAnim(const SdfPath& skinnedPrimPath)
303  const;
304 
305  void _RemoveSkinnedPrimAndComputations(const SdfPath& cachePath,
306  UsdImagingIndexProxy* index);
307 
308  // ---------------------------------------------------------------------- //
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();
335 
336  static
337  const std::string& _GetSkinningComputeKernel();
338 
339  // ---------------------------------------------------------------------- //
341  // ---------------------------------------------------------------------- //
342  bool _IsSkinnedPrimPath(const SdfPath& cachePath) const;
343 
344  void _TrackSkinnedPrimVariability(
345  const UsdPrim& prim,
346  const SdfPath& cachePath,
347  HdDirtyBits* timeVaryingBits,
349  instancerContext = nullptr) const;
350 
351  void _UpdateSkinnedPrimForTime(
352  const UsdPrim& prim,
353  const SdfPath& cachePath,
354  UsdTimeCode time,
355  HdDirtyBits requestedBits,
356  const UsdImagingInstancerContext* instancerContext=nullptr) const;
357 
358  // ---------------------------------------------------------------------- //
360  // ---------------------------------------------------------------------- //
361 
362  VtValue
363  _GetExtComputationInputForSkinningComputation(
364  UsdPrim const& prim,
365  SdfPath const& cachePath,
366  TfToken const& name,
367  UsdTimeCode time,
368  const UsdImagingInstancerContext* instancerContext) const;
369 
370  VtValue
371  _GetExtComputationInputForInputAggregator(
372  UsdPrim const& prim,
373  SdfPath const& cachePath,
374  TfToken const& name,
375  UsdTimeCode time,
376  const UsdImagingInstancerContext* instancerContext) const;
377 
378  size_t
379  _SampleExtComputationInputForSkinningComputation(
380  UsdPrim const& prim,
381  SdfPath const& cachePath,
382  TfToken const& name,
383  UsdTimeCode time,
384  const UsdImagingInstancerContext* instancerContext,
385  size_t maxSampleCount,
386  float *sampleTimes,
387  VtValue *sampleValues);
388 
389  size_t
390  _SampleExtComputationInputForInputAggregator(
391  UsdPrim const& prim,
392  SdfPath const& cachePath,
393  TfToken const& name,
394  UsdTimeCode time,
395  const UsdImagingInstancerContext* instancerContext,
396  size_t maxSampleCount,
397  float *sampleTimes,
398  VtValue *sampleValues);
399 
400 
401  // ---------------------------------------------------------------------- //
403  // ---------------------------------------------------------------------- //
405  struct _SkelData {
406 
407  UsdSkelSkeletonQuery skelQuery;
408  SdfPathSet skelRootPaths;
409 
412  HdMeshTopology ComputeTopologyAndRestState();
413 
415  VtVec3fArray ComputePoints(UsdTimeCode time) const;
416 
417  TfToken ComputePurpose() const;
418 
419  private:
420  // Cache of a mesh for a skeleton (at rest)
421  // TODO: Dedupe this infromation across UsdSkelSkeleton instances.
422  VtVec3fArray _boneMeshPoints;
423  VtIntArray _boneMeshJointIndices;
424  size_t _numJoints;
425  };
426 
427  _SkelData* _GetSkelData(const SdfPath& cachePath) const;
428 
429  UsdSkelCache _skelCache;
430  using _SkelDataMap =
431  std::unordered_map<SdfPath, std::shared_ptr<_SkelData>, SdfPath::Hash>;
432  _SkelDataMap _skelDataCache;
433 
434  // Data for each skinned prim.
435  struct _SkinnedPrimData {
436  _SkinnedPrimData() = default;
437 
441  _SkinnedPrimData(const SdfPath& skelPath,
442  const UsdSkelSkeletonQuery& skelQuery,
443  const UsdSkelSkinningQuery& skinningQuery,
444  const SdfPath& skelRootPath);
445 
446  std::shared_ptr<UsdSkelBlendShapeQuery> blendShapeQuery;
447  UsdSkelSkinningQuery skinningQuery;
448  UsdSkelAnimQuery animQuery;
449  SdfPath skelPath, skelRootPath;
450  bool hasJointInfluences = false;
451  };
452 
453  const _SkinnedPrimData* _GetSkinnedPrimData(const SdfPath& cachePath) const;
454 
455  using _SkinnedPrimDataMap =
456  std::unordered_map<SdfPath, _SkinnedPrimData, SdfPath::Hash>;
457  _SkinnedPrimDataMap _skinnedPrimDataCache;
458 
459  // ---------------------------------------------------------------------- //
462  // ---------------------------------------------------------------------- //
463 
464  using _SkelBindingMap =
465  std::unordered_map<SdfPath, UsdSkelBinding, SdfPath::Hash>;
466  _SkelBindingMap _skelBindingMap;
467 };
468 
469 
470 PXR_NAMESPACE_CLOSE_SCOPE
471 
472 #endif // USDSKELIMAGING_SKELETONADAPTER
473 
Interface class that defines the execution environment for the client to run a computation.
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:43
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
Object used for querying resolved bindings for skinning.
Definition: skinningQuery.h:50
Thread-safe cache for accessing query objects for evaluating skeletal data.
Definition: cache.h:57
Class providing efficient queries of primitives that provide skel animation.
Definition: animQuery.h:54
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
virtual USDIMAGING_API PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const
Get the subdiv tags for this prim.
This proxy class exposes a subset of the private Delegate API to PrimAdapters.
Definition: indexProxy.h:47
virtual void UpdateForTime(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time, HdDirtyBits requestedBits, UsdImagingInstancerContext const *instancerContext=nullptr) const =0
Populates the cache for the given prim, time and requestedBits.
virtual USDIMAGING_API void ProcessPrimRemoval(SdfPath const &cachePath, UsdImagingIndexProxy *index)
Removes all associated Rprims and dependencies from the render index without scheduling them for repo...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
virtual USDIMAGING_API GfRange3d GetExtent(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const
Reads the extent from the given prim.
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
Base class for all PrimAdapters.
Definition: primAdapter.h:67
virtual void TrackVariability(UsdPrim const &prim, SdfPath const &cachePath, HdDirtyBits *timeVaryingBits, UsdImagingInstancerContext const *instancerContext=nullptr) const =0
For the given prim, variability is detected and stored in timeVaryingBits.
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:132
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
Helper object that describes the binding of a skeleton to a set of skinnable objects.
Definition: binding.h:44
virtual USDIMAGING_API VtValue GetTopology(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const
Gets the topology object of a specific Usd prim.
virtual USDIMAGING_API TfToken GetPurpose(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &instanceInheritablePurpose) const
Returns the purpose token for prim.
virtual USDIMAGING_API bool GetDoubleSided(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const
Reads double-sided from the given prim. If not authored, returns false.
Primary interface to reading bound skeleton data.
Definition: skeletonQuery.h:70
Object used by instancer prim adapters to pass along context about the instancer and instance prim to...
Topology data for meshes.
Definition: meshTopology.h:55
virtual HdDirtyBits ProcessPropertyChange(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &propertyName)=0
Returns a bit mask of attributes to be updated, or HdChangeTracker::AllDirty if the entire prim must ...
virtual SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=nullptr)=0
Called to populate the RenderIndex for this UsdPrim.
virtual USDIMAGING_API void ProcessPrimResync(SdfPath const &cachePath, UsdImagingIndexProxy *index)
When a PrimResync event occurs, the prim may have been deleted entirely, adapter plug-ins should over...
virtual USDIMAGING_API VtValue Get(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &key, UsdTimeCode time, VtIntArray *outIndices) const
Gets the value of the parameter named key for the given prim (which has the given cache path) and giv...
virtual bool IsSupported(UsdImagingIndexProxy const *index) const
Returns true if the adapter can be populated into the target index.
Definition: primAdapter.h:623
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166