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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
8 #define PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
9 
10 /// \file usdImaging/primvarDescCache.h
11 
12 #include "pxr/pxr.h"
15 
16 #include "pxr/usd/sdf/path.h"
17 
18 #include "pxr/base/tf/hash.h"
19 #include "pxr/base/tf/token.h"
20 
21 #include <tbb/concurrent_unordered_map.h>
22 #include <tbb/concurrent_queue.h>
23 
25 
26 /// \class UsdImagingPrimvarDescCache
27 ///
28 /// A cache for primvar descriptors.
29 ///
31 {
32 public:
35  = delete;
36 
37  class Key
38  {
40  SdfPath _path;
41  TfToken _attribute;
42 
43  public:
44  Key(SdfPath const& path, TfToken const& attr)
45  : _path(path)
46  , _attribute(attr)
47  {}
48 
49  inline bool operator==(Key const& rhs) const {
50  return _path == rhs._path && _attribute == rhs._attribute;
51  }
52  inline bool operator!=(Key const& rhs) const {
53  return !(*this == rhs);
54  }
55 
56  struct Hash {
57  inline size_t operator()(Key const& key) const {
58  return TfHash::Combine(key._path.GetHash(),
59  key._attribute.Hash());
60  }
61  };
62 
63  private:
64  static Key Primvars(SdfPath const& path) {
65  static TfToken attr("primvars");
66  return Key(path, attr);
67  }
68  };
69 
71  : _locked(false)
72  { }
73 
74 private:
75  template <typename Element>
76  struct _TypedCache
77  {
78  typedef tbb::concurrent_unordered_map<Key, Element, Key::Hash> _MapType;
79  typedef typename _MapType::iterator _MapIt;
80  typedef typename _MapType::const_iterator _MapConstIt;
81  typedef tbb::concurrent_queue<_MapIt> _QueueType;
82 
83  _MapType _map;
84  };
85 
86 
87  /// Locates the requested \p key then populates \p value and returns true if
88  /// found.
89  template <typename T>
90  bool _Find(Key const& key, T* value) const {
91  typedef _TypedCache<T> Cache_t;
92 
93  Cache_t *cache = nullptr;
94 
95  _GetCache(&cache);
96  typename Cache_t::_MapConstIt it = cache->_map.find(key);
97  if (it == cache->_map.end()) {
98  return false;
99  }
100  *value = it->second;
101  return true;
102  }
103 
104  /// Erases the given key from the value cache.
105  /// Not thread safe
106  template <typename T>
107  void _Erase(Key const& key) {
108  if (!TF_VERIFY(!_locked)) {
109  return;
110  }
111 
112  typedef _TypedCache<T> Cache_t;
113 
114  Cache_t *cache = nullptr;
115  _GetCache(&cache);
116  cache->_map.unsafe_erase(key);
117  }
118 
119  /// Returns a reference to the held value for \p key. Note that the entry
120  /// for \p key will created with a default-constructed instance of T if
121  /// there was no pre-existing entry.
122  template <typename T>
123  T& _Get(Key const& key) const {
124  typedef _TypedCache<T> Cache_t;
125 
126  Cache_t *cache = nullptr;
127  _GetCache(&cache);
128 
129  // With concurrent_unordered_map, multi-threaded insertion is safe.
130  std::pair<typename Cache_t::_MapIt, bool> res =
131  cache->_map.insert(std::make_pair(key, T()));
132 
133  return res.first->second;
134  }
135 
136 public:
137 
138  void EnableMutation() { _locked = false; }
139  void DisableMutation() { _locked = true; }
140 
141  /// Clear all data associated with a specific path.
142  void Clear(SdfPath const& path) {
143  _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
144  }
145 
147  return _Get<HdPrimvarDescriptorVector>(Key::Primvars(path));
148  }
149 
150  bool FindPrimvars(SdfPath const& path, HdPrimvarDescriptorVector* value) const {
151  return _Find(Key::Primvars(path), value);
152  }
153 
154 private:
155  bool _locked;
156 
157  typedef _TypedCache<HdPrimvarDescriptorVector> _PviCache;
158  mutable _PviCache _pviCache;
159 
160  void _GetCache(_PviCache **cache) const {
161  *cache = &_pviCache;
162  }
163 };
164 
165 
167 
168 #endif // PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
size_t GetHash() const
Equality operator.
Definition: path.h:957
HdPrimvarDescriptorVector & GetPrimvars(SdfPath const &path) const
UsdImagingPrimvarDescCache & operator=(const UsdImagingPrimvarDescCache &)=delete
Definition: token.h:70
size_t operator()(Key const &key) const
bool FindPrimvars(SdfPath const &path, HdPrimvarDescriptorVector *value) const
Definition: path.h:273
bool operator!=(Key const &rhs) const
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
bool operator==(Key const &rhs) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
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:415