24 #ifndef USDIMAGING_VALUE_CACHE_H
25 #define USDIMAGING_VALUE_CACHE_H
30 #include "pxr/usdImaging/usdImaging/api.h"
31 #include "pxr/imaging/hd/enums.h"
32 #include "pxr/imaging/hd/tokens.h"
33 #include "pxr/imaging/hd/materialParam.h"
34 #include "pxr/imaging/hd/sceneDelegate.h"
35 #include "pxr/imaging/hd/version.h"
37 #include "pxr/usd/sdf/path.h"
38 #include "pxr/base/vt/value.h"
40 #include "pxr/base/gf/matrix4d.h"
41 #include "pxr/base/gf/range3d.h"
42 #include "pxr/base/gf/vec4f.h"
43 #include "pxr/base/tf/token.h"
45 #include <tbb/concurrent_unordered_map.h>
46 #include <tbb/concurrent_queue.h>
48 PXR_NAMESPACE_OPEN_SCOPE
73 return _path == rhs._path && _attribute == rhs._attribute;
75 inline bool operator!=(Key
const& rhs)
const {
76 return !(*
this == rhs);
80 inline size_t operator()(Key
const& key)
const {
81 size_t hash = key._path.GetHash();
82 boost::hash_combine(hash, key._attribute.Hash());
88 static Key Color(
SdfPath const& path) {
89 static TfToken attr(
"displayColor");
90 return Key(path, attr);
92 static Key Opacity(
SdfPath const& path) {
93 static TfToken attr(
"displayOpacity");
94 return Key(path, attr);
96 static Key DoubleSided(
SdfPath const& path) {
97 static TfToken attr(
"doubleSided");
98 return Key(path, attr);
100 static Key CullStyle(
SdfPath const& path) {
101 static TfToken attr(
"cullStyle");
102 return Key(path, attr);
104 static Key Extent(
SdfPath const& path) {
106 return Key(path, attr);
108 static Key InstancerTransform(
SdfPath const& path) {
109 static TfToken attr(
"instancerTransform");
110 return Key(path, attr);
112 static Key InstanceIndices(
SdfPath const& path) {
113 static TfToken attr(
"instanceIndices");
114 return Key(path, attr);
116 static Key Points(
SdfPath const& path) {
118 return Key(path, attr);
120 static Key Purpose(
SdfPath const& path) {
121 static TfToken attr(
"purpose");
122 return Key(path, attr);
124 static Key Primvars(
SdfPath const& path) {
125 static TfToken attr(
"primvars");
126 return Key(path, attr);
128 static Key Topology(
SdfPath const& path) {
129 static TfToken attr(
"topology");
130 return Key(path, attr);
132 static Key Transform(
SdfPath const& path) {
133 static TfToken attr(
"transform");
134 return Key(path, attr);
136 static Key Visible(
SdfPath const& path) {
137 static TfToken attr(
"visible");
138 return Key(path, attr);
140 static Key Widths(
SdfPath const& path) {
142 return Key(path, attr);
144 static Key Normals(
SdfPath const& path) {
145 static TfToken attr(
"normals");
146 return Key(path, attr);
148 static Key MaterialId(
SdfPath const& path) {
149 static TfToken attr(
"materialId");
150 return Key(path, attr);
152 static Key MaterialResource(
SdfPath const& path) {
153 static TfToken attr(
"materialResource");
154 return Key(path, attr);
157 static Key SurfaceShaderSource(
SdfPath const& path) {
158 static TfToken attr(
"surfaceShaderSource");
159 return Key(path, attr);
161 static Key DisplacementShaderSource(
SdfPath const& path) {
162 static TfToken attr(
"displacementShaderSource");
163 return Key(path, attr);
165 static Key MaterialMetadata(
SdfPath const& path) {
166 static TfToken attr(
"materialMetadata");
167 return Key(path, attr);
169 static Key MaterialParams(
SdfPath const& path) {
170 static TfToken attr(
"surfaceShaderParams");
171 return Key(path, attr);
173 static Key ExtComputationSceneInputNames(
SdfPath const& path) {
174 static TfToken attr(
"extComputationSceneInputNames");
175 return Key(path, attr);
177 static Key ExtComputationInputs(
SdfPath const& path) {
178 static TfToken attr(
"extComputationInputs");
179 return Key(path, attr);
181 static Key ExtComputationOutputs(
SdfPath const& path) {
182 static TfToken attr(
"extComputationOutputs");
183 return Key(path, attr);
185 static Key ExtComputationPrimvars(
SdfPath const& path) {
186 const TfToken attr(
"extComputationPrimvars");
187 return Key(path, attr);
189 static Key ExtComputationKernel(
SdfPath const& path) {
190 const TfToken attr(
"extComputationKernel");
191 return Key(path, attr);
193 static Key CameraParamNames(
SdfPath const& path) {
194 static TfToken attr(
"CameraParamNames");
195 return Key(path, attr);
204 template <
typename Element>
207 typedef tbb::concurrent_unordered_map<Key, Element, Key::Hash> _MapType;
208 typedef typename _MapType::iterator _MapIt;
209 typedef typename _MapType::const_iterator _MapConstIt;
210 typedef tbb::concurrent_queue<_MapIt> _QueueType;
213 _QueueType _deferredDeleteQueue;
219 template <
typename T>
220 bool _Find(Key
const& key, T* value)
const {
221 typedef _TypedCache<T> Cache_t;
223 Cache_t *cache =
nullptr;
226 typename Cache_t::_MapConstIt it = cache->_map.find(key);
227 if (it == cache->_map.end()) {
241 template <
typename T>
242 bool _Extract(Key
const& key, T* value) {
247 typedef _TypedCache<T> Cache_t;
248 Cache_t *cache =
nullptr;
251 typename Cache_t::_MapIt it = cache->_map.find(key);
253 if (it == cache->_map.end()) {
259 cache->_deferredDeleteQueue.push(it);
267 template <
typename T>
268 void _Erase(Key
const& key) {
273 typedef _TypedCache<T> Cache_t;
275 Cache_t *cache =
nullptr;
277 cache->_map.unsafe_erase(key);
283 template <
typename T>
284 T& _Get(Key
const& key)
const {
285 typedef _TypedCache<T> Cache_t;
287 Cache_t *cache =
nullptr;
291 std::pair<typename Cache_t::_MapIt, bool> res =
292 cache->_map.insert(std::make_pair(key, T()));
294 return res.first->second;
300 template <
typename T>
301 void _GarbageCollect(_TypedCache<T> &cache) {
302 typedef _TypedCache<T> Cache_t;
304 typename Cache_t::_MapIt it;
306 while (cache._deferredDeleteQueue.try_pop(it)) {
307 cache._map.unsafe_erase(it);
313 void EnableMutation() { _locked =
false; }
314 void DisableMutation() { _locked =
true; }
318 _Erase<VtValue>(Key::Color(path));
319 _Erase<VtValue>(Key::Opacity(path));
320 _Erase<bool>(Key::DoubleSided(path));
321 _Erase<HdCullStyle>(Key::CullStyle(path));
322 _Erase<GfRange3d>(Key::Extent(path));
323 _Erase<VtValue>(Key::InstanceIndices(path));
324 _Erase<TfToken>(Key::Purpose(path));
325 _Erase<VtValue>(Key::Topology(path));
326 _Erase<GfMatrix4d>(Key::Transform(path));
327 _Erase<bool>(Key::Visible(path));
328 _Erase<VtValue>(Key::Points(path));
329 _Erase<VtValue>(Key::Widths(path));
330 _Erase<VtValue>(Key::Normals(path));
331 _Erase<VtValue>(Key::MaterialId(path));
332 _Erase<VtValue>(Key::MaterialResource(path));
336 HdPrimvarDescriptorVector vars;
337 if (FindPrimvars(path, &vars)) {
339 _Erase<VtValue>(Key(path, pvIt->name));
341 _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
345 _Erase<std::string>(Key::SurfaceShaderSource(path));
346 _Erase<std::string>(Key::DisplacementShaderSource(path));
347 _Erase<VtValue>(Key::MaterialMetadata(path));
348 HdMaterialParamVector shaderVars;
349 if (FindMaterialParams(path, &shaderVars)) {
350 for (HdMaterialParam
const& param : shaderVars) {
351 _Erase<VtValue>(Key(path, param.name));
353 _Erase<HdMaterialParamVector>(Key::MaterialParams(path));
359 if (FindExtComputationSceneInputNames(path, &sceneInputNames)) {
361 sceneInputNames.emplace_back(HdTokens->dispatchCount);
362 sceneInputNames.emplace_back(HdTokens->elementCount);
363 for (
TfToken const& input : sceneInputNames) {
364 _Erase<VtValue>(Key(path, input));
367 _Erase<TfTokenVector>(Key::ExtComputationSceneInputNames(path));
372 _Erase<HdExtComputationInputDescriptorVector>(
373 Key::ExtComputationInputs(path));
375 HdExtComputationOutputDescriptorVector outputDescs;
376 if (FindExtComputationOutputs(path, &outputDescs)) {
377 for (
auto const& desc : outputDescs) {
378 _Erase<VtValue>(Key(path, desc.name));
380 _Erase<HdExtComputationOutputDescriptorVector>(
381 Key::ExtComputationOutputs(path));
384 _Erase<HdExtComputationPrimvarDescriptorVector>(
385 Key::ExtComputationPrimvars(path));
386 _Erase<std::string>(Key::ExtComputationKernel(path));
391 if (FindCameraParamNames(path, &CameraParamNames)) {
392 for (
const TfToken& paramName : CameraParamNames) {
393 _Erase<VtValue>(Key(path, paramName));
396 _Erase<TfTokenVector>(Key::CameraParamNames(path));
401 return _Get<VtValue>(Key::Color(path));
404 return _Get<VtValue>(Key::Opacity(path));
406 bool& GetDoubleSided(
SdfPath const& path)
const {
407 return _Get<bool>(Key::DoubleSided(path));
409 HdCullStyle& GetCullStyle(
SdfPath const& path)
const {
410 return _Get<HdCullStyle>(Key::CullStyle(path));
413 return _Get<GfRange3d>(Key::Extent(path));
416 return _Get<GfMatrix4d>(Key::InstancerTransform(path));
419 return _Get<VtValue>(Key::InstanceIndices(path));
422 return _Get<VtValue>(Key::Points(path));
425 return _Get<TfToken>(Key::Purpose(path));
427 HdPrimvarDescriptorVector& GetPrimvars(
SdfPath const& path)
const {
428 return _Get<HdPrimvarDescriptorVector>(Key::Primvars(path));
431 return _Get<VtValue>(Key::Topology(path));
434 return _Get<GfMatrix4d>(Key::Transform(path));
436 bool& GetVisible(
SdfPath const& path)
const {
437 return _Get<bool>(Key::Visible(path));
440 return _Get<VtValue>(Key::Widths(path));
443 return _Get<VtValue>(Key::Normals(path));
446 return _Get<VtValue>(Key(path, name));
449 return _Get<SdfPath>(Key::MaterialId(path));
452 return _Get<VtValue>(Key::MaterialResource(path));
455 std::string& GetSurfaceShaderSource(
SdfPath const& path)
const {
456 return _Get<std::string>(Key::SurfaceShaderSource(path));
458 std::string& GetDisplacementShaderSource(
SdfPath const& path)
const {
459 return _Get<std::string>(Key::DisplacementShaderSource(path));
462 return _Get<VtValue>(Key::MaterialMetadata(path));
464 HdMaterialParamVector& GetMaterialParams(
SdfPath const& path)
const {
465 return _Get<HdMaterialParamVector>(Key::MaterialParams(path));
468 return _Get<VtValue>(Key(path, name));
471 return _Get<TfTokenVector>(Key::ExtComputationSceneInputNames(path));
473 HdExtComputationInputDescriptorVector&
474 GetExtComputationInputs(
SdfPath const& path)
const {
475 return _Get<HdExtComputationInputDescriptorVector>(
476 Key::ExtComputationInputs(path));
478 HdExtComputationOutputDescriptorVector&
479 GetExtComputationOutputs(
SdfPath const& path)
const {
480 return _Get<HdExtComputationOutputDescriptorVector>(
481 Key::ExtComputationOutputs(path));
483 HdExtComputationPrimvarDescriptorVector&
484 GetExtComputationPrimvars(
SdfPath const& path)
const {
485 return _Get<HdExtComputationPrimvarDescriptorVector>(
486 Key::ExtComputationPrimvars(path));
490 return _Get<VtValue>(Key(path, name));
492 std::string& GetExtComputationKernel(
SdfPath const& path)
const {
493 return _Get<std::string>(Key::ExtComputationKernel(path));
496 return _Get<VtValue>(Key(path, name));
499 return _Get<TfTokenVector>(Key::CameraParamNames(path));
503 return _Find(Key(path, name), value);
506 return _Find(Key::Color(path), value);
509 return _Find(Key::Opacity(path), value);
511 bool FindDoubleSided(
SdfPath const& path,
bool* value)
const {
512 return _Find(Key::DoubleSided(path), value);
514 bool FindCullStyle(
SdfPath const& path, HdCullStyle* value)
const {
515 return _Find(Key::CullStyle(path), value);
518 return _Find(Key::Extent(path), value);
521 return _Find(Key::InstancerTransform(path), value);
523 bool FindInstanceIndices(
SdfPath const& path,
VtValue* value)
const {
524 return _Find(Key::InstanceIndices(path), value);
527 return _Find(Key::Points(path), value);
530 return _Find(Key::Purpose(path), value);
532 bool FindPrimvars(
SdfPath const& path, HdPrimvarDescriptorVector* value)
const {
533 return _Find(Key::Primvars(path), value);
536 return _Find(Key::Topology(path), value);
539 return _Find(Key::Transform(path), value);
541 bool FindVisible(
SdfPath const& path,
bool* value)
const {
542 return _Find(Key::Visible(path), value);
545 return _Find(Key::Widths(path), value);
548 return _Find(Key::Normals(path), value);
551 return _Find(Key::MaterialId(path), value);
553 bool FindMaterialResource(
SdfPath const& path,
VtValue* value)
const {
554 return _Find(Key::MaterialResource(path), value);
557 bool FindSurfaceShaderSource(
SdfPath const& path, std::string* value)
const {
558 return _Find(Key::SurfaceShaderSource(path), value);
560 bool FindDisplacementShaderSource(
SdfPath const& path, std::string* value)
const {
561 return _Find(Key::DisplacementShaderSource(path), value);
563 bool FindMaterialMetadata(
SdfPath const& path,
VtValue* value)
const {
564 return _Find(Key::MaterialMetadata(path), value);
566 bool FindMaterialParams(
SdfPath const& path, HdMaterialParamVector* value)
const {
567 return _Find(Key::MaterialParams(path), value);
570 return _Find(Key(path, name), value);
572 bool FindExtComputationSceneInputNames(
SdfPath const& path,
574 return _Find(Key::ExtComputationSceneInputNames(path), value);
576 bool FindExtComputationInputs(
578 HdExtComputationInputDescriptorVector* value)
const {
579 return _Find(Key::ExtComputationInputs(path), value);
581 bool FindExtComputationOutputs(
583 HdExtComputationOutputDescriptorVector* value)
const {
584 return _Find(Key::ExtComputationOutputs(path), value);
586 bool FindExtComputationPrimvars(
588 HdExtComputationPrimvarDescriptorVector* value)
const {
589 return _Find(Key::ExtComputationPrimvars(path), value);
591 bool FindExtComputationInput(
593 return _Find(Key(path, name), value);
595 bool FindExtComputationKernel(
SdfPath const& path, std::string* value)
const {
596 return _Find(Key::ExtComputationKernel(path), value);
600 return _Find(Key(path, name), value);
603 return _Find(Key::CameraParamNames(path), value);
607 return _Extract(Key::Color(path), value);
610 return _Extract(Key::Opacity(path), value);
612 bool ExtractDoubleSided(
SdfPath const& path,
bool* value) {
613 return _Extract(Key::DoubleSided(path), value);
615 bool ExtractCullStyle(
SdfPath const& path, HdCullStyle* value) {
616 return _Extract(Key::CullStyle(path), value);
619 return _Extract(Key::Extent(path), value);
622 return _Extract(Key::InstancerTransform(path), value);
624 bool ExtractInstanceIndices(
SdfPath const& path,
VtValue* value) {
625 return _Extract(Key::InstanceIndices(path), value);
628 return _Extract(Key::Points(path), value);
631 return _Extract(Key::Purpose(path), value);
633 bool ExtractPrimvars(
SdfPath const& path, HdPrimvarDescriptorVector* value) {
634 return _Extract(Key::Primvars(path), value);
637 return _Extract(Key::Topology(path), value);
640 return _Extract(Key::Transform(path), value);
642 bool ExtractVisible(
SdfPath const& path,
bool* value) {
643 return _Extract(Key::Visible(path), value);
646 return _Extract(Key::Widths(path), value);
649 return _Extract(Key::Normals(path), value);
652 return _Extract(Key::MaterialId(path), value);
654 bool ExtractMaterialResource(
SdfPath const& path,
VtValue* value) {
655 return _Extract(Key::MaterialResource(path), value);
658 return _Extract(Key(path, name), value);
661 bool ExtractSurfaceShaderSource(
SdfPath const& path, std::string* value) {
662 return _Extract(Key::SurfaceShaderSource(path), value);
664 bool ExtractDisplacementShaderSource(
SdfPath const& path, std::string* value) {
665 return _Extract(Key::DisplacementShaderSource(path), value);
667 bool ExtractMaterialMetadata(
SdfPath const& path,
VtValue* value) {
668 return _Extract(Key::MaterialMetadata(path), value);
670 bool ExtractMaterialParams(
SdfPath const& path, HdMaterialParamVector* value) {
671 return _Extract(Key::MaterialParams(path), value);
674 return _Extract(Key(path, name), value);
676 bool ExtractExtComputationSceneInputNames(
SdfPath const& path,
678 return _Extract(Key::ExtComputationSceneInputNames(path), value);
680 bool ExtractExtComputationInputs(
682 HdExtComputationInputDescriptorVector* value) {
683 return _Extract(Key::ExtComputationInputs(path), value);
685 bool ExtractExtComputationOutputs(
687 HdExtComputationOutputDescriptorVector* value) {
688 return _Extract(Key::ExtComputationOutputs(path), value);
690 bool ExtractExtComputationPrimvars(
692 HdExtComputationPrimvarDescriptorVector* value) {
693 return _Extract(Key::ExtComputationPrimvars(path), value);
695 bool ExtractExtComputationInput(
SdfPath const& path,
TfToken const& name,
697 return _Extract(Key(path, name), value);
699 bool ExtractExtComputationKernel(
SdfPath const& path, std::string* value) {
700 return _Extract(Key::ExtComputationKernel(path), value);
704 return _Extract(Key(path, name), value);
712 _GarbageCollect(_boolCache);
713 _GarbageCollect(_tokenCache);
714 _GarbageCollect(_tokenVectorCache);
715 _GarbageCollect(_rangeCache);
716 _GarbageCollect(_cullStyleCache);
717 _GarbageCollect(_matrixCache);
718 _GarbageCollect(_vec4Cache);
719 _GarbageCollect(_valueCache);
720 _GarbageCollect(_pviCache);
721 _GarbageCollect(_sdfPathCache);
723 _GarbageCollect(_stringCache);
724 _GarbageCollect(_shaderParamCache);
725 _GarbageCollect(_extComputationInputsCache);
726 _GarbageCollect(_extComputationOutputsCache);
727 _GarbageCollect(_extComputationPrimvarsCache);
734 typedef _TypedCache<bool> _BoolCache;
735 mutable _BoolCache _boolCache;
738 typedef _TypedCache<TfToken> _TokenCache;
739 mutable _TokenCache _tokenCache;
742 typedef _TypedCache<TfTokenVector> _TokenVectorCache;
743 mutable _TokenVectorCache _tokenVectorCache;
746 typedef _TypedCache<GfRange3d> _RangeCache;
747 mutable _RangeCache _rangeCache;
750 typedef _TypedCache<HdCullStyle> _CullStyleCache;
751 mutable _CullStyleCache _cullStyleCache;
754 typedef _TypedCache<GfMatrix4d> _MatrixCache;
755 mutable _MatrixCache _matrixCache;
758 typedef _TypedCache<GfVec4f> _Vec4Cache;
759 mutable _Vec4Cache _vec4Cache;
762 typedef _TypedCache<SdfPath> _SdfPathCache;
763 mutable _SdfPathCache _sdfPathCache;
766 typedef _TypedCache<VtValue> _ValueCache;
767 mutable _ValueCache _valueCache;
769 typedef _TypedCache<HdPrimvarDescriptorVector> _PviCache;
770 mutable _PviCache _pviCache;
773 typedef _TypedCache<std::string> _StringCache;
774 mutable _StringCache _stringCache;
776 typedef _TypedCache<HdMaterialParamVector> _MaterialParamCache;
777 mutable _MaterialParamCache _shaderParamCache;
779 typedef _TypedCache<HdExtComputationInputDescriptorVector>
780 _ExtComputationInputsCache;
781 mutable _ExtComputationInputsCache _extComputationInputsCache;
783 typedef _TypedCache<HdExtComputationOutputDescriptorVector>
784 _ExtComputationOutputsCache;
785 mutable _ExtComputationOutputsCache _extComputationOutputsCache;
787 typedef _TypedCache<HdExtComputationPrimvarDescriptorVector>
788 _ExtComputationPrimvarsCache;
789 mutable _ExtComputationPrimvarsCache _extComputationPrimvarsCache;
791 void _GetCache(_BoolCache **cache)
const {
792 *cache = &_boolCache;
794 void _GetCache(_TokenCache **cache)
const {
795 *cache = &_tokenCache;
797 void _GetCache(_TokenVectorCache **cache)
const {
798 *cache = &_tokenVectorCache;
800 void _GetCache(_RangeCache **cache)
const {
801 *cache = &_rangeCache;
803 void _GetCache(_CullStyleCache **cache)
const {
804 *cache = &_cullStyleCache;
806 void _GetCache(_MatrixCache **cache)
const {
807 *cache = &_matrixCache;
809 void _GetCache(_Vec4Cache **cache)
const {
810 *cache = &_vec4Cache;
812 void _GetCache(_ValueCache **cache)
const {
813 *cache = &_valueCache;
815 void _GetCache(_PviCache **cache)
const {
818 void _GetCache(_SdfPathCache **cache)
const {
819 *cache = &_sdfPathCache;
822 void _GetCache(_StringCache **cache)
const {
823 *cache = &_stringCache;
825 void _GetCache(_MaterialParamCache **cache)
const {
826 *cache = &_shaderParamCache;
828 void _GetCache(_ExtComputationInputsCache **cache)
const {
829 *cache = &_extComputationInputsCache;
831 void _GetCache(_ExtComputationOutputsCache **cache)
const {
832 *cache = &_extComputationOutputsCache;
834 void _GetCache(_ExtComputationPrimvarsCache **cache)
const {
835 *cache = &_extComputationPrimvarsCache;
840 PXR_NAMESPACE_CLOSE_SCOPE
842 #endif // USDIMAGING_VALUE_CACHE_H
Basic type: 3-dimensional floating point range.
A heterogeneous value container without type erasure.
Token for efficient comparison, assignment, and hashing of known strings.
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
#define TF_FOR_ALL(iter, c)
Macro for iterating over a container.
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
Stores a 4x4 matrix of double elements.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
std::vector< TfToken > TfTokenVector
Convenience types.
A path value used to locate objects in layers or scenegraphs.
void GarbageCollect()
Remove any items from the cache that are marked for defered deletion.
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
Provides a container which may hold any type, and provides introspection and iteration over array typ...