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  };
102 
103 
104  /// Locates the requested \p key then populates \p value and returns true if
105  /// found.
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 
121  /// Erases the given key from the value cache.
122  /// Not thread safe
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 
136  /// Returns a reference to the held value for \p key. Note that the entry
137  /// for \p key will created with a default-constructed instance of T if
138  /// there was no pre-existing entry.
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 
158  /// Clear all data associated with a specific path.
159  void Clear(SdfPath const& path) {
160  _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
161  }
162 
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 
184 
185 #endif // PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
size_t GetHash() const
Equality operator.
Definition: path.h:969
HdPrimvarDescriptorVector & GetPrimvars(SdfPath const &path) const
UsdImagingPrimvarDescCache & operator=(const UsdImagingPrimvarDescCache &)=delete
Definition: token.h:87
size_t operator()(Key const &key) const
bool FindPrimvars(SdfPath const &path, HdPrimvarDescriptorVector *value) const
Definition: path.h:291
bool operator!=(Key const &rhs) const
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
bool operator==(Key const &rhs) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: core.h:1131
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