All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cacheImpl.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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 USDSKEL_CACHEIMPL
25 #define USDSKEL_CACHEIMPL
26 
28 
29 #include "pxr/usd/usd/attribute.h"
30 #include "pxr/usd/usd/prim.h"
31 #include "pxr/usd/usd/relationship.h"
32 
33 #include "pxr/usd/usdSkel/animMapper.h"
34 #include "pxr/usd/usdSkel/animQueryImpl.h"
35 #include "pxr/usd/usdSkel/animQuery.h"
36 #include "pxr/usd/usdSkel/skeleton.h"
37 #include "pxr/usd/usdSkel/skelDefinition.h"
38 #include "pxr/usd/usdSkel/skeletonQuery.h"
39 #include "pxr/usd/usdSkel/skinningQuery.h"
40 
41 #include <boost/optional.hpp>
42 
43 #include <tbb/concurrent_hash_map.h>
44 #include <tbb/queuing_rw_mutex.h>
45 
46 #include <unordered_map>
47 
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
51 
52 class UsdGeomXformCache;
53 class UsdSkelRoot;
54 
55 
56 struct UsdSkel_HashPrim {
57  inline size_t operator()(const UsdPrim& prim) const {
58  return hash_value(prim);
59  }
60 
61  static bool equal(const UsdPrim& a, const UsdPrim& b) {
62  return a == b;
63  }
64 
65  static size_t hash(const UsdPrim& prim) {
66  return hash_value(prim);
67  }
68 };
69 
70 
73 {
74 public:
75  using RWMutex = tbb::queuing_rw_mutex;
76 
77  struct SkinningQueryKey {
78  UsdAttribute jointIndicesAttr;
79  UsdAttribute jointWeightsAttr;
80  UsdAttribute geomBindTransformAttr;
81  UsdAttribute jointsAttr;
82  UsdAttribute blendShapesAttr;
83  UsdRelationship blendShapeTargetsRel;
84  UsdPrim skel;
85  };
86 
89  struct ReadScope {
91 
92  // Getters for properties with a direct prim association.
93  // These are produced on-demand rather than through Populate().
94 
96  FindOrCreateAnimQuery(const UsdPrim& prim);
97 
98  UsdSkel_SkelDefinitionRefPtr
99  FindOrCreateSkelDefinition(const UsdPrim& prim);
100 
102  FindOrCreateSkelQuery(const UsdPrim& prim);
103 
107  bool Populate(const UsdSkelRoot& root);
108 
109  // Getters for properties added to the cache through Populate().
110 
112  GetSkinningQuery(const UsdPrim& prim) const;
113 
114  private:
115 
117  _FindOrCreateSkinningQuery(const UsdPrim& skinnedPrim,
118  const SkinningQueryKey& key);
119 
120  using _PrimToSkinMap =
121  std::unordered_map<UsdPrim,SkinningQueryKey,UsdSkel_HashPrim>;
122 
125  void _RecursivePopulate(const SdfPath& rootPath,
126  const UsdPrim& prim,
127  SkinningQueryKey key,
128  UsdSkelAnimQuery animQuery,
129  _PrimToSkinMap* skinBindingMap,
130  size_t depth=1);
131 
132  private:
133  UsdSkel_CacheImpl* _cache;
134  RWMutex::scoped_lock _lock;
135  };
136 
139  struct WriteScope {
141 
142  void Clear();
143 
144  private:
145  UsdSkel_CacheImpl* _cache;
146  RWMutex::scoped_lock _lock;
147  };
148 
149 private:
150 
151  using _PrimToAnimMap =
152  tbb::concurrent_hash_map<UsdPrim,
153  UsdSkel_AnimQueryImplRefPtr,
154  UsdSkel_HashPrim>;
155 
156  using _PrimToSkelDefinitionMap =
157  tbb::concurrent_hash_map<UsdPrim,
158  UsdSkel_SkelDefinitionRefPtr,
159  UsdSkel_HashPrim>;
160 
161  using _PrimToSkelQueryMap =
162  tbb::concurrent_hash_map<UsdPrim,
164  UsdSkel_HashPrim>;
165 
166  using _PrimToSkinningQueryMap =
167  tbb::concurrent_hash_map<UsdPrim,
169  UsdSkel_HashPrim>;
170 
171  using _RWMutex = tbb::queuing_rw_mutex;
172 
173  _PrimToAnimMap _animQueryCache;
174  _PrimToSkelDefinitionMap _skelDefinitionCache;
175  _PrimToSkelQueryMap _skelQueryCache;
176  _PrimToSkinningQueryMap _primSkinningQueryCache;
177 
179  mutable _RWMutex _mutex; // XXX: Not recursive!
180 };
181 
182 
183 PXR_NAMESPACE_CLOSE_SCOPE
184 
185 #endif // USDSKEL_CACHEIMPL
Object used for querying resolved bindings for skinning.
Definition: skinningQuery.h:50
bool Populate(const UsdSkelRoot &root)
Method for populating the cache with cache properties, for the set of properties that depend on inher...
Internal cache implementation.
Definition: cacheImpl.h:72
Class providing efficient queries of primitives that provide skel animation.
Definition: animQuery.h:54
Scope for performing read-only operations on the cache.
Definition: cacheImpl.h:89
Scope for performing write operations on the cache.
Definition: cacheImpl.h:139
Scenegraph object for authoring and retrieving numeric, string, and array valued data, sampled over time.
Definition: attribute.h:176
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
A UsdRelationship creates dependencies between scenegraph objects by allowing a prim to target other ...
Definition: relationship.h:128
Boundable prim type used to identify a scope beneath which skeletally-posed primitives are defined...
Definition: root.h:63
Primary interface to reading bound skeleton data.
Definition: skeletonQuery.h:70
A caching mechanism for transform matrices.
Definition: xformCache.h:58