primvarDescCache.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 PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
25 #define PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usdImaging/usdImaging/api.h"
31 #include "pxr/imaging/hd/sceneDelegate.h"
32 
33 #include "pxr/usd/sdf/path.h"
34 
35 #include "pxr/base/tf/token.h"
36 
37 #include <tbb/concurrent_unordered_map.h>
38 #include <tbb/concurrent_queue.h>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
47 {
48 public:
51  = delete;
52 
53  class Key
54  {
55  friend class UsdImagingPrimvarDescCache;
56  SdfPath _path;
57  TfToken _attribute;
58 
59  public:
60  Key(SdfPath const& path, TfToken const& attr)
61  : _path(path)
62  , _attribute(attr)
63  {}
64 
65  inline bool operator==(Key const& rhs) const {
66  return _path == rhs._path && _attribute == rhs._attribute;
67  }
68  inline bool operator!=(Key const& rhs) const {
69  return !(*this == rhs);
70  }
71 
72  struct Hash {
73  inline size_t operator()(Key const& key) const {
74  size_t hash = key._path.GetHash();
75  boost::hash_combine(hash, key._attribute.Hash());
76  return hash;
77  }
78  };
79 
80  private:
81  static Key Primvars(SdfPath const& path) {
82  static TfToken attr("primvars");
83  return Key(path, attr);
84  }
85  };
86 
88  : _locked(false)
89  { }
90 
91 private:
92  template <typename Element>
93  struct _TypedCache
94  {
95  typedef tbb::concurrent_unordered_map<Key, Element, Key::Hash> _MapType;
96  typedef typename _MapType::iterator _MapIt;
97  typedef typename _MapType::const_iterator _MapConstIt;
98  typedef tbb::concurrent_queue<_MapIt> _QueueType;
99 
100  _MapType _map;
101  };
102 
103 
106  template <typename T>
107  bool _Find(Key const& key, T* value) const {
108  typedef _TypedCache<T> Cache_t;
109 
110  Cache_t *cache = nullptr;
111 
112  _GetCache(&cache);
113  typename Cache_t::_MapConstIt it = cache->_map.find(key);
114  if (it == cache->_map.end()) {
115  return false;
116  }
117  *value = it->second;
118  return true;
119  }
120 
123  template <typename T>
124  void _Erase(Key const& key) {
125  if (!TF_VERIFY(!_locked)) {
126  return;
127  }
128 
129  typedef _TypedCache<T> Cache_t;
130 
131  Cache_t *cache = nullptr;
132  _GetCache(&cache);
133  cache->_map.unsafe_erase(key);
134  }
135 
139  template <typename T>
140  T& _Get(Key const& key) const {
141  typedef _TypedCache<T> Cache_t;
142 
143  Cache_t *cache = nullptr;
144  _GetCache(&cache);
145 
146  // With concurrent_unordered_map, multi-threaded insertion is safe.
147  std::pair<typename Cache_t::_MapIt, bool> res =
148  cache->_map.insert(std::make_pair(key, T()));
149 
150  return res.first->second;
151  }
152 
153 public:
154 
155  void EnableMutation() { _locked = false; }
156  void DisableMutation() { _locked = true; }
157 
159  void Clear(SdfPath const& path) {
160  _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
161  }
162 
163  HdPrimvarDescriptorVector& GetPrimvars(SdfPath const& path) const {
164  return _Get<HdPrimvarDescriptorVector>(Key::Primvars(path));
165  }
166 
167  bool FindPrimvars(SdfPath const& path, HdPrimvarDescriptorVector* value) const {
168  return _Find(Key::Primvars(path), value);
169  }
170 
171 private:
172  bool _locked;
173 
174  typedef _TypedCache<HdPrimvarDescriptorVector> _PviCache;
175  mutable _PviCache _pviCache;
176 
177  void _GetCache(_PviCache **cache) const {
178  *cache = &_pviCache;
179  }
180 };
181 
182 
183 PXR_NAMESPACE_CLOSE_SCOPE
184 
185 #endif // PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
A cache for primvar descriptors.
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...