HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
27 /// \file usdImaging/primvarDescCache.h
28 
29 #include "pxr/pxr.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 
41 
42 /// \class UsdImagingPrimvarDescCache
43 ///
44 /// A cache for primvar descriptors.
45 ///
47 {
48 public:
51  = delete;
52 
53  class Key
54  {
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  hboost::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  _QueueType _deferredDeleteQueue;
102  };
103 
104 
105  /// Locates the requested \p key then populates \p value and returns true if
106  /// found.
107  template <typename T>
108  bool _Find(Key const& key, T* value) const {
109  typedef _TypedCache<T> Cache_t;
110 
111  Cache_t *cache = nullptr;
112 
113  _GetCache(&cache);
114  typename Cache_t::_MapConstIt it = cache->_map.find(key);
115  if (it == cache->_map.end()) {
116  return false;
117  }
118  *value = it->second;
119  return true;
120  }
121 
122  /// Locates the requested \p key then populates \p value, swap the value
123  /// from the entry and queues the entry up for deletion.
124  /// Returns true if found.
125  /// This function is thread-safe, but Garbage collection must be called
126  /// to perform the actual deletion.
127  /// Note: second hit on same key will be sucessful, but return whatever
128  /// value was passed into the first _Extract.
129  template <typename T>
130  bool _Extract(Key const& key, T* value) {
131  if (!TF_VERIFY(!_locked)) {
132  return false;
133  }
134 
135  typedef _TypedCache<T> Cache_t;
136  Cache_t *cache = nullptr;
137 
138  _GetCache(&cache);
139  typename Cache_t::_MapIt it = cache->_map.find(key);
140 
141  if (it == cache->_map.end()) {
142  return false;
143  }
144 
145  // If we're going to erase the old value, swap to avoid a copy.
146  std::swap(it->second, *value);
147  cache->_deferredDeleteQueue.push(it);
148  return true;
149  }
150 
151  /// Erases the given key from the value cache.
152  /// Not thread safe
153  template <typename T>
154  void _Erase(Key const& key) {
155  if (!TF_VERIFY(!_locked)) {
156  return;
157  }
158 
159  typedef _TypedCache<T> Cache_t;
160 
161  Cache_t *cache = nullptr;
162  _GetCache(&cache);
163  cache->_map.unsafe_erase(key);
164  }
165 
166  /// Returns a reference to the held value for \p key. Note that the entry
167  /// for \p key will created with a default-constructed instance of T if
168  /// there was no pre-existing entry.
169  template <typename T>
170  T& _Get(Key const& key) const {
171  typedef _TypedCache<T> Cache_t;
172 
173  Cache_t *cache = nullptr;
174  _GetCache(&cache);
175 
176  // With concurrent_unordered_map, multi-threaded insertion is safe.
177  std::pair<typename Cache_t::_MapIt, bool> res =
178  cache->_map.insert(std::make_pair(key, T()));
179 
180  return res.first->second;
181  }
182 
183  /// Removes items from the cache that are marked for deletion.
184  /// This is not thread-safe and designed to be called after
185  /// all the worker threads have been joined.
186  template <typename T>
187  void _GarbageCollect(_TypedCache<T> &cache) {
188  typedef _TypedCache<T> Cache_t;
189 
190  typename Cache_t::_MapIt it;
191 
192  while (cache._deferredDeleteQueue.try_pop(it)) {
193  cache._map.unsafe_erase(it);
194  }
195  }
196 
197 public:
198 
199  void EnableMutation() { _locked = false; }
200  void DisableMutation() { _locked = true; }
201 
202  /// Clear all data associated with a specific path.
203  void Clear(SdfPath const& path) {
204  _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
205  }
206 
208  return _Get<HdPrimvarDescriptorVector>(Key::Primvars(path));
209  }
210 
211  bool FindPrimvars(SdfPath const& path, HdPrimvarDescriptorVector* value) const {
212  return _Find(Key::Primvars(path), value);
213  }
214 
216  return _Extract(Key::Primvars(path), value);
217  }
218 
219  /// Remove any items from the cache that are marked for defered deletion.
221  {
222  _GarbageCollect(_pviCache);
223  }
224 
225 private:
226  bool _locked;
227 
228  typedef _TypedCache<HdPrimvarDescriptorVector> _PviCache;
229  mutable _PviCache _pviCache;
230 
231  void _GetCache(_PviCache **cache) const {
232  *cache = &_pviCache;
233  }
234 };
235 
236 
238 
239 #endif // PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Definition: UT_ArraySet.h:1629
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
void GarbageCollect()
Remove any items from the cache that are marked for defered deletion.
size_t GetHash() const
Definition: path.h:920
HdPrimvarDescriptorVector & GetPrimvars(SdfPath const &path) const
UsdImagingPrimvarDescCache & operator=(const UsdImagingPrimvarDescCache &)=delete
Definition: token.h:87
bool ExtractPrimvars(SdfPath const &path, HdPrimvarDescriptorVector *value)
size_t operator()(Key const &key) const
bool FindPrimvars(SdfPath const &path, HdPrimvarDescriptorVector *value) const
Definition: path.h:288
bool operator!=(Key const &rhs) const
GLuint res
Definition: glew.h:11549
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
bool operator==(Key const &rhs) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLsizei const GLfloat * value
Definition: glcorearb.h:823
std::vector< HdPrimvarDescriptor > HdPrimvarDescriptorVector
Key(SdfPath const &path, TfToken const &attr)
size_t Hash() const
Return a size_t hash for this token.
Definition: token.h:417