24 #ifndef PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
25 #define PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
30 #include "pxr/usdImaging/usdImaging/api.h"
31 #include "pxr/imaging/hd/sceneDelegate.h"
33 #include "pxr/usd/sdf/path.h"
37 #include <tbb/concurrent_unordered_map.h>
38 #include <tbb/concurrent_queue.h>
40 PXR_NAMESPACE_OPEN_SCOPE
66 return _path == rhs._path && _attribute == rhs._attribute;
68 inline bool operator!=(Key
const& rhs)
const {
69 return !(*
this == rhs);
73 inline size_t operator()(Key
const& key)
const {
74 size_t hash = key._path.GetHash();
75 boost::hash_combine(hash, key._attribute.Hash());
81 static Key Primvars(
SdfPath const& path) {
82 static TfToken attr(
"primvars");
83 return Key(path, attr);
92 template <
typename Element>
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;
101 _QueueType _deferredDeleteQueue;
107 template <
typename T>
108 bool _Find(Key
const& key, T* value)
const {
109 typedef _TypedCache<T> Cache_t;
111 Cache_t *cache =
nullptr;
114 typename Cache_t::_MapConstIt it = cache->_map.find(key);
115 if (it == cache->_map.end()) {
129 template <
typename T>
130 bool _Extract(Key
const& key, T* value) {
135 typedef _TypedCache<T> Cache_t;
136 Cache_t *cache =
nullptr;
139 typename Cache_t::_MapIt it = cache->_map.find(key);
141 if (it == cache->_map.end()) {
147 cache->_deferredDeleteQueue.push(it);
153 template <
typename T>
154 void _Erase(Key
const& key) {
159 typedef _TypedCache<T> Cache_t;
161 Cache_t *cache =
nullptr;
163 cache->_map.unsafe_erase(key);
169 template <
typename T>
170 T& _Get(Key
const& key)
const {
171 typedef _TypedCache<T> Cache_t;
173 Cache_t *cache =
nullptr;
177 std::pair<typename Cache_t::_MapIt, bool> res =
178 cache->_map.insert(std::make_pair(key, T()));
180 return res.first->second;
186 template <
typename T>
187 void _GarbageCollect(_TypedCache<T> &cache) {
188 typedef _TypedCache<T> Cache_t;
190 typename Cache_t::_MapIt it;
192 while (cache._deferredDeleteQueue.try_pop(it)) {
193 cache._map.unsafe_erase(it);
199 void EnableMutation() { _locked =
false; }
200 void DisableMutation() { _locked =
true; }
204 _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
207 HdPrimvarDescriptorVector& GetPrimvars(
SdfPath const& path)
const {
208 return _Get<HdPrimvarDescriptorVector>(Key::Primvars(path));
211 bool FindPrimvars(
SdfPath const& path, HdPrimvarDescriptorVector* value)
const {
212 return _Find(Key::Primvars(path), value);
215 bool ExtractPrimvars(
SdfPath const& path, HdPrimvarDescriptorVector* value) {
216 return _Extract(Key::Primvars(path), value);
222 _GarbageCollect(_pviCache);
228 typedef _TypedCache<HdPrimvarDescriptorVector> _PviCache;
229 mutable _PviCache _pviCache;
231 void _GetCache(_PviCache **cache)
const {
237 PXR_NAMESPACE_CLOSE_SCOPE
239 #endif // PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
Token for efficient comparison, assignment, and hashing of known strings.
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
void GarbageCollect()
Remove any items from the cache that are marked for defered deletion.
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
A path value used to locate objects in layers or scenegraphs.
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...