All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 USDSKELIMAGING_SKELETONADAPTER_H
25 #define USDSKELIMAGING_SKELETONADAPTER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usdImaging/usdImaging/primAdapter.h"
29 #include "pxr/usdImaging/usdSkelImaging/api.h"
30 
31 #include "pxr/imaging/hd/meshTopology.h"
32 
33 #include "pxr/usd/usdSkel/binding.h"
34 #include "pxr/usd/usdSkel/blendShapeQuery.h"
35 #include "pxr/usd/usdSkel/cache.h"
36 #include "pxr/usd/usdSkel/skeleton.h"
37 #include "pxr/usd/usdSkel/skeletonQuery.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 public:
52  using BaseAdapter = UsdImagingPrimAdapter;
53 
54  UsdSkelImagingSkeletonAdapter()
55  : BaseAdapter()
56  {}
57 
58  USDSKELIMAGING_API
59  virtual ~UsdSkelImagingSkeletonAdapter();
60 
61  USDSKELIMAGING_API
62  SdfPath
63  Populate(const UsdPrim& prim,
64  UsdImagingIndexProxy* index,
66  instancerContext=nullptr) override;
67 
68  USDSKELIMAGING_API
69  bool IsSupported(const UsdImagingIndexProxy* index) const override;
70 
71  // ---------------------------------------------------------------------- //
73  // ---------------------------------------------------------------------- //
74 
76  USDSKELIMAGING_API
77  void TrackVariability(const UsdPrim& prim,
78  const SdfPath& cachePath,
79  HdDirtyBits* timeVaryingBits,
81  instancerContext = nullptr) const override;
82 
84  USDSKELIMAGING_API
85  void UpdateForTime(const UsdPrim& prim,
86  const SdfPath& cachePath,
87  UsdTimeCode time,
88  HdDirtyBits requestedBits,
90  instancerContext=nullptr) const override;
91 
92  // ---------------------------------------------------------------------- //
94  // ---------------------------------------------------------------------- //
95 
96  USDSKELIMAGING_API
97  HdDirtyBits ProcessPropertyChange(const UsdPrim& prim,
98  const SdfPath& cachePath,
99  const TfToken& propertyName) override;
100 
101  USDSKELIMAGING_API
102  void ProcessPrimResync(SdfPath const& primPath,
103  UsdImagingIndexProxy* index) override;
104 
105  USDSKELIMAGING_API
106  void ProcessPrimRemoval(SdfPath const& primPath,
107  UsdImagingIndexProxy* index) override;
108 
109  USDSKELIMAGING_API
110  void MarkDirty(const UsdPrim& prim,
111  const SdfPath& cachePath,
112  HdDirtyBits dirty,
113  UsdImagingIndexProxy* index) override;
114 
115  USDSKELIMAGING_API
116  void MarkRefineLevelDirty(UsdPrim const& prim,
117  SdfPath const& cachePath,
118  UsdImagingIndexProxy* index) override;
119 
120  USDSKELIMAGING_API
121  void MarkReprDirty(UsdPrim const& prim,
122  SdfPath const& cachePath,
123  UsdImagingIndexProxy* index) override;
124 
125  USDSKELIMAGING_API
126  void MarkCullStyleDirty(UsdPrim const& prim,
127  SdfPath const& cachePath,
128  UsdImagingIndexProxy* index) override;
129 
130  USDSKELIMAGING_API
131  void MarkRenderTagDirty(UsdPrim const& prim,
132  SdfPath const& cachePath,
133  UsdImagingIndexProxy* index) override;
134 
135  USDSKELIMAGING_API
136  void MarkTransformDirty(const UsdPrim& prim,
137  const SdfPath& cachePath,
138  UsdImagingIndexProxy* index) override;
139 
140  USDSKELIMAGING_API
141  void MarkVisibilityDirty(const UsdPrim& prim,
142  const SdfPath& cachePath,
143  UsdImagingIndexProxy* index) override;
144 
145  USDSKELIMAGING_API
146  void MarkMaterialDirty(const UsdPrim& prim,
147  const SdfPath& cachePath,
148  UsdImagingIndexProxy* index) override;
149 
150  // ---------------------------------------------------------------------- //
152  // ---------------------------------------------------------------------- //
153  USDSKELIMAGING_API
154  void InvokeComputation(SdfPath const& computationPath,
155  HdExtComputationContext* context) override;
156 
157  // ---------------------------------------------------------------------- //
159  // ---------------------------------------------------------------------- //
160 
161  USDSKELIMAGING_API
162  void RegisterSkelBinding(UsdSkelBinding const& binding);
163 
164  // ---------------------------------------------------------------------- //
166  // ---------------------------------------------------------------------- //
167 
168  USDSKELIMAGING_API
169  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
170  SdfPath const& cachePath,
171  UsdTimeCode time) const override;
172 
173 protected:
174  // ---------------------------------------------------------------------- //
176  // ---------------------------------------------------------------------- //
177  void _RemovePrim(const SdfPath& cachePath,
178  UsdImagingIndexProxy* index) override;
179 
180 private:
181  // ---------------------------------------------------------------------- //
183  // ---------------------------------------------------------------------- //
184  bool _IsCallbackForSkeleton(const UsdPrim& prim) const;
185 
189  GfRange3d _GetExtent(const UsdPrim& prim, UsdTimeCode time) const;
190 
193  GfVec3f _GetSkeletonDisplayColor(const UsdPrim& prim,
194  UsdTimeCode time) const;
195 
198  float _GetSkeletonDisplayOpacity(const UsdPrim& prim,
199  UsdTimeCode time) const;
200 
201  void _TrackBoneMeshVariability(
202  const UsdPrim& prim,
203  const SdfPath& cachePath,
204  HdDirtyBits* timeVaryingBits,
206  instancerContext = nullptr) const;
207 
208  void _UpdateBoneMeshForTime(
209  const UsdPrim& prim,
210  const SdfPath& cachePath,
211  UsdTimeCode time,
212  HdDirtyBits requestedBits,
213  const UsdImagingInstancerContext* instancerContext=nullptr) const;
214 
215  // ---------------------------------------------------------------------- //
217  // ---------------------------------------------------------------------- //
218  bool _IsAffectedByTimeVaryingSkelAnim(const SdfPath& skinnedPrimPath)
219  const;
220 
221  void _RemoveSkinnedPrimAndComputations(const SdfPath& cachePath,
222  UsdImagingIndexProxy* index);
223 
224  // ---------------------------------------------------------------------- //
226  // ---------------------------------------------------------------------- //
227  bool _IsSkinningComputationPath(const SdfPath& cachePath) const;
228 
229  bool
230  _IsSkinningInputAggregatorComputationPath(const SdfPath& cachePath)const;
231 
232  void _TrackSkinningComputationVariability(
233  const UsdPrim& skinnedPrim,
234  const SdfPath& computationPath,
235  HdDirtyBits* timeVaryingBits,
237  instancerContext = nullptr) const;
238 
239  VtVec3fArray _GetSkinnedPrimPoints(const UsdPrim& skinnedPrim,
240  const SdfPath& skinnedPrimCachePath,
241  UsdTimeCode time) const;
242 
243  void _UpdateSkinningComputationForTime(
244  const UsdPrim& skinnedPrim,
245  const SdfPath& computationPath,
246  UsdTimeCode time,
247  HdDirtyBits requestedBits,
248  const UsdImagingInstancerContext* instancerContext=nullptr) const;
249 
250  void _UpdateSkinningInputAggregatorComputationForTime(
251  const UsdPrim& skinnedPrim,
252  const SdfPath& computationPath,
253  UsdTimeCode time,
254  HdDirtyBits requestedBits,
255  const UsdImagingInstancerContext* instancerContext=nullptr) const;
256 
257  SdfPath _GetSkinningComputationPath(const SdfPath& skinnedPrimPath) const;
258 
259  SdfPath _GetSkinningInputAggregatorComputationPath(
260  const SdfPath& skinnedPrimPath) const;
261 
262  // Static helper methods
263  static
264  std::string _LoadSkinningComputeKernel();
265 
266  static
267  const std::string& _GetSkinningComputeKernel();
268 
269  // ---------------------------------------------------------------------- //
271  // ---------------------------------------------------------------------- //
272  bool _IsSkinnedPrimPath(const SdfPath& cachePath) const;
273 
274  void _TrackSkinnedPrimVariability(
275  const UsdPrim& prim,
276  const SdfPath& cachePath,
277  HdDirtyBits* timeVaryingBits,
279  instancerContext = nullptr) const;
280 
281  void _UpdateSkinnedPrimForTime(
282  const UsdPrim& prim,
283  const SdfPath& cachePath,
284  UsdTimeCode time,
285  HdDirtyBits requestedBits,
286  const UsdImagingInstancerContext* instancerContext=nullptr) const;
287 
288 
289  // ---------------------------------------------------------------------- //
291  // ---------------------------------------------------------------------- //
293  struct _SkelData {
294 
295  UsdSkelSkeletonQuery skelQuery;
296  SdfPathSet skelRootPaths;
297 
300  HdMeshTopology ComputeTopologyAndRestState();
301 
303  VtVec3fArray ComputePoints(UsdTimeCode time) const;
304 
305  TfToken ComputePurpose() const;
306 
307  private:
308  // Cache of a mesh for a skeleton (at rest)
309  // TODO: Dedupe this infromation across UsdSkelSkeleton instances.
310  VtVec3fArray _boneMeshPoints;
311  VtIntArray _boneMeshJointIndices;
312  size_t _numJoints;
313  };
314 
315  _SkelData* _GetSkelData(const SdfPath& cachePath) const;
316 
317  UsdSkelCache _skelCache;
318  using _SkelDataMap =
319  std::unordered_map<SdfPath, std::shared_ptr<_SkelData>, SdfPath::Hash>;
320  _SkelDataMap _skelDataCache;
321 
322  // Data for each skinned prim.
323  struct _SkinnedPrimData {
324  _SkinnedPrimData() = default;
325  _SkinnedPrimData(const UsdSkelSkeletonQuery& skelQuery,
326  const UsdSkelSkinningQuery& skinningQuery,
327  const SdfPath& skelRootPath);
328 
329  std::shared_ptr<UsdSkelBlendShapeQuery> blendShapeQuery;
330  UsdSkelAnimMapper jointMapper;
331  UsdSkelAnimMapper blendShapeMapper;
332  SdfPath skelPath, skelRootPath;
333  bool hasJointInfluences = false;
334  };
335 
336  const _SkinnedPrimData* _GetSkinnedPrimData(const SdfPath& cachePath) const;
337 
338  using _SkinnedPrimDataMap =
339  std::unordered_map<SdfPath, _SkinnedPrimData, SdfPath::Hash>;
340  _SkinnedPrimDataMap _skinnedPrimDataCache;
341 
342  // ---------------------------------------------------------------------- //
345  // ---------------------------------------------------------------------- //
346 
347  using _SkelBindingMap =
348  std::unordered_map<SdfPath, UsdSkelBinding, SdfPath::Hash>;
349  _SkelBindingMap _skelBindingMap;
350 };
351 
352 
353 PXR_NAMESPACE_CLOSE_SCOPE
354 
355 #endif // USDSKELIMAGING_SKELETONADAPTER
356 
Interface class that defines the execution environment for the client to run a computation.
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:41
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
virtual void UpdateForTime(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time, HdDirtyBits requestedBits, UsdImagingInstancerContext const *instancerContext=NULL) const =0
Populates the cache for the given prim, time and requestedBits.
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:58
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 USDIMAGING_API void ProcessPrimRemoval(SdfPath const &cachePath, UsdImagingIndexProxy *index)
Removes all associated Rprims and dependencies from the render index without scheduling them for repo...
virtual SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=NULL)=0
Called to populate the RenderIndex for this UsdPrim.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
virtual bool IsSupported(UsdImagingIndexProxy const *index) const
Returns true if the adapter can be populated into the target index.
Definition: primAdapter.h:437
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
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
virtual void TrackVariability(UsdPrim const &prim, SdfPath const &cachePath, HdDirtyBits *timeVaryingBits, UsdImagingInstancerContext const *instancerContext=NULL) const =0
For the given prim, variability is detected and stored in timeVaryingBits.
Helper object that describes the binding of a skeleton to a set of skinnable objects.
Definition: binding.h:44
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 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...