All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
valueCache.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 USDIMAGING_VALUE_CACHE_H
25 #define USDIMAGING_VALUE_CACHE_H
26 
28 
29 #include "pxr/pxr.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"
36 
37 #include "pxr/usd/sdf/path.h"
38 #include "pxr/base/vt/value.h"
39 
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"
44 
45 #include <tbb/concurrent_unordered_map.h>
46 #include <tbb/concurrent_queue.h>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 
51 
57 public:
59  UsdImagingValueCache& operator=(const UsdImagingValueCache&) = delete;
60 
61  class Key {
62  friend class UsdImagingValueCache;
63  private:
64  SdfPath _path;
65  TfToken _attribute;
66  public:
67  Key(SdfPath const& path, TfToken const& attr)
68  : _path(path)
69  , _attribute(attr)
70  {}
71 
72  inline bool operator==(Key const& rhs) const {
73  return _path == rhs._path && _attribute == rhs._attribute;
74  }
75  inline bool operator!=(Key const& rhs) const {
76  return !(*this == rhs);
77  }
78 
79  struct Hash {
80  inline size_t operator()(Key const& key) const {
81  size_t hash = key._path.GetHash();
82  boost::hash_combine(hash, key._attribute.Hash());
83  return hash;
84  }
85  };
86 
87  private:
88  static Key Color(SdfPath const& path) {
89  static TfToken attr("displayColor");
90  return Key(path, attr);
91  }
92  static Key Opacity(SdfPath const& path) {
93  static TfToken attr("displayOpacity");
94  return Key(path, attr);
95  }
96  static Key DoubleSided(SdfPath const& path) {
97  static TfToken attr("doubleSided");
98  return Key(path, attr);
99  }
100  static Key CullStyle(SdfPath const& path) {
101  static TfToken attr("cullStyle");
102  return Key(path, attr);
103  }
104  static Key Extent(SdfPath const& path) {
105  static TfToken attr("extent");
106  return Key(path, attr);
107  }
108  static Key InstancerTransform(SdfPath const& path) {
109  static TfToken attr("instancerTransform");
110  return Key(path, attr);
111  }
112  static Key InstanceIndices(SdfPath const& path) {
113  static TfToken attr("instanceIndices");
114  return Key(path, attr);
115  }
116  static Key Points(SdfPath const& path) {
117  static TfToken attr("points");
118  return Key(path, attr);
119  }
120  static Key Purpose(SdfPath const& path) {
121  static TfToken attr("purpose");
122  return Key(path, attr);
123  }
124  static Key Primvars(SdfPath const& path) {
125  static TfToken attr("primvars");
126  return Key(path, attr);
127  }
128  static Key Topology(SdfPath const& path) {
129  static TfToken attr("topology");
130  return Key(path, attr);
131  }
132  static Key Transform(SdfPath const& path) {
133  static TfToken attr("transform");
134  return Key(path, attr);
135  }
136  static Key Visible(SdfPath const& path) {
137  static TfToken attr("visible");
138  return Key(path, attr);
139  }
140  static Key Widths(SdfPath const& path) {
141  static TfToken attr("widths");
142  return Key(path, attr);
143  }
144  static Key Normals(SdfPath const& path) {
145  static TfToken attr("normals");
146  return Key(path, attr);
147  }
148  static Key MaterialId(SdfPath const& path) {
149  static TfToken attr("materialId");
150  return Key(path, attr);
151  }
152  static Key MaterialResource(SdfPath const& path) {
153  static TfToken attr("materialResource");
154  return Key(path, attr);
155  }
156  // XXX: Shader API will be deprecated soon.
157  static Key SurfaceShaderSource(SdfPath const& path) {
158  static TfToken attr("surfaceShaderSource");
159  return Key(path, attr);
160  }
161  static Key DisplacementShaderSource(SdfPath const& path) {
162  static TfToken attr("displacementShaderSource");
163  return Key(path, attr);
164  }
165  static Key MaterialMetadata(SdfPath const& path) {
166  static TfToken attr("materialMetadata");
167  return Key(path, attr);
168  }
169  static Key MaterialParams(SdfPath const& path) {
170  static TfToken attr("surfaceShaderParams");
171  return Key(path, attr);
172  }
173  static Key ExtComputationSceneInputNames(SdfPath const& path) {
174  static TfToken attr("extComputationSceneInputNames");
175  return Key(path, attr);
176  }
177  static Key ExtComputationInputs(SdfPath const& path) {
178  static TfToken attr("extComputationInputs");
179  return Key(path, attr);
180  }
181  static Key ExtComputationOutputs(SdfPath const& path) {
182  static TfToken attr("extComputationOutputs");
183  return Key(path, attr);
184  }
185  static Key ExtComputationPrimvars(SdfPath const& path) {
186  const TfToken attr("extComputationPrimvars");
187  return Key(path, attr);
188  }
189  static Key ExtComputationKernel(SdfPath const& path) {
190  const TfToken attr("extComputationKernel");
191  return Key(path, attr);
192  }
193  static Key CameraParamNames(SdfPath const& path) {
194  static TfToken attr("CameraParamNames");
195  return Key(path, attr);
196  }
197  };
198 
200  : _locked(false)
201  { }
202 
203 private:
204  template <typename Element>
205  struct _TypedCache
206  {
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;
211 
212  _MapType _map;
213  _QueueType _deferredDeleteQueue;
214  };
215 
216 
219  template <typename T>
220  bool _Find(Key const& key, T* value) const {
221  typedef _TypedCache<T> Cache_t;
222 
223  Cache_t *cache = nullptr;
224 
225  _GetCache(&cache);
226  typename Cache_t::_MapConstIt it = cache->_map.find(key);
227  if (it == cache->_map.end()) {
228  return false;
229  }
230  *value = it->second;
231  return true;
232  }
233 
241  template <typename T>
242  bool _Extract(Key const& key, T* value) {
243  if (!TF_VERIFY(!_locked)) {
244  return false;
245  }
246 
247  typedef _TypedCache<T> Cache_t;
248  Cache_t *cache = nullptr;
249 
250  _GetCache(&cache);
251  typename Cache_t::_MapIt it = cache->_map.find(key);
252 
253  if (it == cache->_map.end()) {
254  return false;
255  }
256 
257  // If we're going to erase the old value, swap to avoid a copy.
258  std::swap(it->second, *value);
259  cache->_deferredDeleteQueue.push(it);
260  return true;
261  }
262 
263 
264 
267  template <typename T>
268  void _Erase(Key const& key) {
269  if (!TF_VERIFY(!_locked)) {
270  return;
271  }
272 
273  typedef _TypedCache<T> Cache_t;
274 
275  Cache_t *cache = nullptr;
276  _GetCache(&cache);
277  cache->_map.unsafe_erase(key);
278  }
279 
283  template <typename T>
284  T& _Get(Key const& key) const {
285  typedef _TypedCache<T> Cache_t;
286 
287  Cache_t *cache = nullptr;
288  _GetCache(&cache);
289 
290  // With concurrent_unordered_map, multi-threaded insertion is safe.
291  std::pair<typename Cache_t::_MapIt, bool> res =
292  cache->_map.insert(std::make_pair(key, T()));
293 
294  return res.first->second;
295  }
296 
300  template <typename T>
301  void _GarbageCollect(_TypedCache<T> &cache) {
302  typedef _TypedCache<T> Cache_t;
303 
304  typename Cache_t::_MapIt it;
305 
306  while (cache._deferredDeleteQueue.try_pop(it)) {
307  cache._map.unsafe_erase(it);
308  }
309  }
310 
311 public:
312 
313  void EnableMutation() { _locked = false; }
314  void DisableMutation() { _locked = true; }
315 
317  void Clear(SdfPath const& path) {
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));
333 
334  // PERFORMANCE: We're copying the primvar vector here, but we could
335  // access the map directly, if we need to for performance reasons.
336  HdPrimvarDescriptorVector vars;
337  if (FindPrimvars(path, &vars)) {
338  TF_FOR_ALL(pvIt, vars) {
339  _Erase<VtValue>(Key(path, pvIt->name));
340  }
341  _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
342  }
343 
344  // XXX: Shader API will be deprecated soon.
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));
352  }
353  _Erase<HdMaterialParamVector>(Key::MaterialParams(path));
354  }
355 
356  {
357  // ExtComputation related state
358  TfTokenVector sceneInputNames;
359  if (FindExtComputationSceneInputNames(path, &sceneInputNames)) {
360  // Add computation "config" params to the list of inputs
361  sceneInputNames.emplace_back(HdTokens->dispatchCount);
362  sceneInputNames.emplace_back(HdTokens->elementCount);
363  for (TfToken const& input : sceneInputNames) {
364  _Erase<VtValue>(Key(path, input));
365  }
366 
367  _Erase<TfTokenVector>(Key::ExtComputationSceneInputNames(path));
368  }
369 
370  // Computed inputs are tied to the computation that computes them.
371  // We don't walk the dependency chain to clear them.
372  _Erase<HdExtComputationInputDescriptorVector>(
373  Key::ExtComputationInputs(path));
374 
375  HdExtComputationOutputDescriptorVector outputDescs;
376  if (FindExtComputationOutputs(path, &outputDescs)) {
377  for (auto const& desc : outputDescs) {
378  _Erase<VtValue>(Key(path, desc.name));
379  }
380  _Erase<HdExtComputationOutputDescriptorVector>(
381  Key::ExtComputationOutputs(path));
382  }
383 
384  _Erase<HdExtComputationPrimvarDescriptorVector>(
385  Key::ExtComputationPrimvars(path));
386  _Erase<std::string>(Key::ExtComputationKernel(path));
387  }
388 
389  // Camera state
390  TfTokenVector CameraParamNames;
391  if (FindCameraParamNames(path, &CameraParamNames)) {
392  for (const TfToken& paramName : CameraParamNames) {
393  _Erase<VtValue>(Key(path, paramName));
394  }
395 
396  _Erase<TfTokenVector>(Key::CameraParamNames(path));
397  }
398  }
399 
400  VtValue& GetColor(SdfPath const& path) const {
401  return _Get<VtValue>(Key::Color(path));
402  }
403  VtValue& GetOpacity(SdfPath const& path) const {
404  return _Get<VtValue>(Key::Opacity(path));
405  }
406  bool& GetDoubleSided(SdfPath const& path) const {
407  return _Get<bool>(Key::DoubleSided(path));
408  }
409  HdCullStyle& GetCullStyle(SdfPath const& path) const {
410  return _Get<HdCullStyle>(Key::CullStyle(path));
411  }
412  GfRange3d& GetExtent(SdfPath const& path) const {
413  return _Get<GfRange3d>(Key::Extent(path));
414  }
415  GfMatrix4d& GetInstancerTransform(SdfPath const& path) const {
416  return _Get<GfMatrix4d>(Key::InstancerTransform(path));
417  }
418  VtValue& GetInstanceIndices(SdfPath const& path) const {
419  return _Get<VtValue>(Key::InstanceIndices(path));
420  }
421  VtValue& GetPoints(SdfPath const& path) const {
422  return _Get<VtValue>(Key::Points(path));
423  }
424  TfToken& GetPurpose(SdfPath const& path) const {
425  return _Get<TfToken>(Key::Purpose(path));
426  }
427  HdPrimvarDescriptorVector& GetPrimvars(SdfPath const& path) const {
428  return _Get<HdPrimvarDescriptorVector>(Key::Primvars(path));
429  }
430  VtValue& GetTopology(SdfPath const& path) const {
431  return _Get<VtValue>(Key::Topology(path));
432  }
433  GfMatrix4d& GetTransform(SdfPath const& path) const {
434  return _Get<GfMatrix4d>(Key::Transform(path));
435  }
436  bool& GetVisible(SdfPath const& path) const {
437  return _Get<bool>(Key::Visible(path));
438  }
439  VtValue& GetWidths(SdfPath const& path) const {
440  return _Get<VtValue>(Key::Widths(path));
441  }
442  VtValue& GetNormals(SdfPath const& path) const {
443  return _Get<VtValue>(Key::Normals(path));
444  }
445  VtValue& GetPrimvar(SdfPath const& path, TfToken const& name) const {
446  return _Get<VtValue>(Key(path, name));
447  }
448  SdfPath& GetMaterialId(SdfPath const& path) const {
449  return _Get<SdfPath>(Key::MaterialId(path));
450  }
451  VtValue& GetMaterialResource(SdfPath const& path) const {
452  return _Get<VtValue>(Key::MaterialResource(path));
453  }
454  // XXX: Shader API will be deprecated soon
455  std::string& GetSurfaceShaderSource(SdfPath const& path) const {
456  return _Get<std::string>(Key::SurfaceShaderSource(path));
457  }
458  std::string& GetDisplacementShaderSource(SdfPath const& path) const {
459  return _Get<std::string>(Key::DisplacementShaderSource(path));
460  }
461  VtValue& GetMaterialMetadata(SdfPath const& path) const {
462  return _Get<VtValue>(Key::MaterialMetadata(path));
463  }
464  HdMaterialParamVector& GetMaterialParams(SdfPath const& path) const {
465  return _Get<HdMaterialParamVector>(Key::MaterialParams(path));
466  }
467  VtValue& GetMaterialParam(SdfPath const& path, TfToken const& name) const {
468  return _Get<VtValue>(Key(path, name));
469  }
470  TfTokenVector& GetExtComputationSceneInputNames(SdfPath const& path) const {
471  return _Get<TfTokenVector>(Key::ExtComputationSceneInputNames(path));
472  }
473  HdExtComputationInputDescriptorVector&
474  GetExtComputationInputs(SdfPath const& path) const {
475  return _Get<HdExtComputationInputDescriptorVector>(
476  Key::ExtComputationInputs(path));
477  }
478  HdExtComputationOutputDescriptorVector&
479  GetExtComputationOutputs(SdfPath const& path) const {
480  return _Get<HdExtComputationOutputDescriptorVector>(
481  Key::ExtComputationOutputs(path));
482  }
483  HdExtComputationPrimvarDescriptorVector&
484  GetExtComputationPrimvars(SdfPath const& path) const {
485  return _Get<HdExtComputationPrimvarDescriptorVector>(
486  Key::ExtComputationPrimvars(path));
487  }
488  VtValue& GetExtComputationInput(SdfPath const& path,
489  TfToken const& name) const {
490  return _Get<VtValue>(Key(path, name));
491  }
492  std::string& GetExtComputationKernel(SdfPath const& path) const {
493  return _Get<std::string>(Key::ExtComputationKernel(path));
494  }
495  VtValue& GetCameraParam(SdfPath const& path, TfToken const& name) const {
496  return _Get<VtValue>(Key(path, name));
497  }
498  TfTokenVector& GetCameraParamNames(SdfPath const& path) const {
499  return _Get<TfTokenVector>(Key::CameraParamNames(path));
500  }
501 
502  bool FindPrimvar(SdfPath const& path, TfToken const& name, VtValue* value) const {
503  return _Find(Key(path, name), value);
504  }
505  bool FindColor(SdfPath const& path, VtValue* value) const {
506  return _Find(Key::Color(path), value);
507  }
508  bool FindOpacity(SdfPath const& path, VtValue* value) const {
509  return _Find(Key::Opacity(path), value);
510  }
511  bool FindDoubleSided(SdfPath const& path, bool* value) const {
512  return _Find(Key::DoubleSided(path), value);
513  }
514  bool FindCullStyle(SdfPath const& path, HdCullStyle* value) const {
515  return _Find(Key::CullStyle(path), value);
516  }
517  bool FindExtent(SdfPath const& path, GfRange3d* value) const {
518  return _Find(Key::Extent(path), value);
519  }
520  bool FindInstancerTransform(SdfPath const& path, GfMatrix4d* value) const {
521  return _Find(Key::InstancerTransform(path), value);
522  }
523  bool FindInstanceIndices(SdfPath const& path, VtValue* value) const {
524  return _Find(Key::InstanceIndices(path), value);
525  }
526  bool FindPoints(SdfPath const& path, VtValue* value) const {
527  return _Find(Key::Points(path), value);
528  }
529  bool FindPurpose(SdfPath const& path, TfToken* value) const {
530  return _Find(Key::Purpose(path), value);
531  }
532  bool FindPrimvars(SdfPath const& path, HdPrimvarDescriptorVector* value) const {
533  return _Find(Key::Primvars(path), value);
534  }
535  bool FindTopology(SdfPath const& path, VtValue* value) const {
536  return _Find(Key::Topology(path), value);
537  }
538  bool FindTransform(SdfPath const& path, GfMatrix4d* value) const {
539  return _Find(Key::Transform(path), value);
540  }
541  bool FindVisible(SdfPath const& path, bool* value) const {
542  return _Find(Key::Visible(path), value);
543  }
544  bool FindWidths(SdfPath const& path, VtValue* value) const {
545  return _Find(Key::Widths(path), value);
546  }
547  bool FindNormals(SdfPath const& path, VtValue* value) const {
548  return _Find(Key::Normals(path), value);
549  }
550  bool FindMaterialId(SdfPath const& path, SdfPath* value) const {
551  return _Find(Key::MaterialId(path), value);
552  }
553  bool FindMaterialResource(SdfPath const& path, VtValue* value) const {
554  return _Find(Key::MaterialResource(path), value);
555  }
556  // XXX: Shader API will be deprecated soon
557  bool FindSurfaceShaderSource(SdfPath const& path, std::string* value) const {
558  return _Find(Key::SurfaceShaderSource(path), value);
559  }
560  bool FindDisplacementShaderSource(SdfPath const& path, std::string* value) const {
561  return _Find(Key::DisplacementShaderSource(path), value);
562  }
563  bool FindMaterialMetadata(SdfPath const& path, VtValue* value) const {
564  return _Find(Key::MaterialMetadata(path), value);
565  }
566  bool FindMaterialParams(SdfPath const& path, HdMaterialParamVector* value) const {
567  return _Find(Key::MaterialParams(path), value);
568  }
569  bool FindMaterialParam(SdfPath const& path, TfToken const& name, VtValue* value) const {
570  return _Find(Key(path, name), value);
571  }
572  bool FindExtComputationSceneInputNames(SdfPath const& path,
573  TfTokenVector* value) const {
574  return _Find(Key::ExtComputationSceneInputNames(path), value);
575  }
576  bool FindExtComputationInputs(
577  SdfPath const& path,
578  HdExtComputationInputDescriptorVector* value) const {
579  return _Find(Key::ExtComputationInputs(path), value);
580  }
581  bool FindExtComputationOutputs(
582  SdfPath const& path,
583  HdExtComputationOutputDescriptorVector* value) const {
584  return _Find(Key::ExtComputationOutputs(path), value);
585  }
586  bool FindExtComputationPrimvars(
587  SdfPath const& path,
588  HdExtComputationPrimvarDescriptorVector* value) const {
589  return _Find(Key::ExtComputationPrimvars(path), value);
590  }
591  bool FindExtComputationInput(
592  SdfPath const& path, TfToken const& name, VtValue* value) const {
593  return _Find(Key(path, name), value);
594  }
595  bool FindExtComputationKernel(SdfPath const& path, std::string* value) const {
596  return _Find(Key::ExtComputationKernel(path), value);
597  }
598  bool FindCameraParam(SdfPath const& path, TfToken const& name,
599  VtValue* value) const {
600  return _Find(Key(path, name), value);
601  }
602  bool FindCameraParamNames(SdfPath const& path, TfTokenVector* value) const {
603  return _Find(Key::CameraParamNames(path), value);
604  }
605 
606  bool ExtractColor(SdfPath const& path, VtValue* value) {
607  return _Extract(Key::Color(path), value);
608  }
609  bool ExtractOpacity(SdfPath const& path, VtValue* value) {
610  return _Extract(Key::Opacity(path), value);
611  }
612  bool ExtractDoubleSided(SdfPath const& path, bool* value) {
613  return _Extract(Key::DoubleSided(path), value);
614  }
615  bool ExtractCullStyle(SdfPath const& path, HdCullStyle* value) {
616  return _Extract(Key::CullStyle(path), value);
617  }
618  bool ExtractExtent(SdfPath const& path, GfRange3d* value) {
619  return _Extract(Key::Extent(path), value);
620  }
621  bool ExtractInstancerTransform(SdfPath const& path, GfMatrix4d* value) {
622  return _Extract(Key::InstancerTransform(path), value);
623  }
624  bool ExtractInstanceIndices(SdfPath const& path, VtValue* value) {
625  return _Extract(Key::InstanceIndices(path), value);
626  }
627  bool ExtractPoints(SdfPath const& path, VtValue* value) {
628  return _Extract(Key::Points(path), value);
629  }
630  bool ExtractPurpose(SdfPath const& path, TfToken* value) {
631  return _Extract(Key::Purpose(path), value);
632  }
633  bool ExtractPrimvars(SdfPath const& path, HdPrimvarDescriptorVector* value) {
634  return _Extract(Key::Primvars(path), value);
635  }
636  bool ExtractTopology(SdfPath const& path, VtValue* value) {
637  return _Extract(Key::Topology(path), value);
638  }
639  bool ExtractTransform(SdfPath const& path, GfMatrix4d* value) {
640  return _Extract(Key::Transform(path), value);
641  }
642  bool ExtractVisible(SdfPath const& path, bool* value) {
643  return _Extract(Key::Visible(path), value);
644  }
645  bool ExtractWidths(SdfPath const& path, VtValue* value) {
646  return _Extract(Key::Widths(path), value);
647  }
648  bool ExtractNormals(SdfPath const& path, VtValue* value) {
649  return _Extract(Key::Normals(path), value);
650  }
651  bool ExtractMaterialId(SdfPath const& path, SdfPath* value) {
652  return _Extract(Key::MaterialId(path), value);
653  }
654  bool ExtractMaterialResource(SdfPath const& path, VtValue* value) {
655  return _Extract(Key::MaterialResource(path), value);
656  }
657  bool ExtractPrimvar(SdfPath const& path, TfToken const& name, VtValue* value) {
658  return _Extract(Key(path, name), value);
659  }
660  // XXX: Shader API will be deprecated soon
661  bool ExtractSurfaceShaderSource(SdfPath const& path, std::string* value) {
662  return _Extract(Key::SurfaceShaderSource(path), value);
663  }
664  bool ExtractDisplacementShaderSource(SdfPath const& path, std::string* value) {
665  return _Extract(Key::DisplacementShaderSource(path), value);
666  }
667  bool ExtractMaterialMetadata(SdfPath const& path, VtValue* value) {
668  return _Extract(Key::MaterialMetadata(path), value);
669  }
670  bool ExtractMaterialParams(SdfPath const& path, HdMaterialParamVector* value) {
671  return _Extract(Key::MaterialParams(path), value);
672  }
673  bool ExtractMaterialParam(SdfPath const& path, TfToken const& name, VtValue* value) {
674  return _Extract(Key(path, name), value);
675  }
676  bool ExtractExtComputationSceneInputNames(SdfPath const& path,
677  TfTokenVector* value) {
678  return _Extract(Key::ExtComputationSceneInputNames(path), value);
679  }
680  bool ExtractExtComputationInputs(
681  SdfPath const& path,
682  HdExtComputationInputDescriptorVector* value) {
683  return _Extract(Key::ExtComputationInputs(path), value);
684  }
685  bool ExtractExtComputationOutputs(
686  SdfPath const& path,
687  HdExtComputationOutputDescriptorVector* value) {
688  return _Extract(Key::ExtComputationOutputs(path), value);
689  }
690  bool ExtractExtComputationPrimvars(
691  SdfPath const& path,
692  HdExtComputationPrimvarDescriptorVector* value) {
693  return _Extract(Key::ExtComputationPrimvars(path), value);
694  }
695  bool ExtractExtComputationInput(SdfPath const& path, TfToken const& name,
696  VtValue* value) {
697  return _Extract(Key(path, name), value);
698  }
699  bool ExtractExtComputationKernel(SdfPath const& path, std::string* value) {
700  return _Extract(Key::ExtComputationKernel(path), value);
701  }
702  bool ExtractCameraParam(SdfPath const& path, TfToken const& name,
703  VtValue* value) {
704  return _Extract(Key(path, name), value);
705  }
706  // Skip adding ExtractCameraParamNames as we don't expose scene delegate
707  // functionality to query all available parameters on a camera.
708 
711  {
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);
722  // XXX: shader type caches, shader API will be deprecated soon
723  _GarbageCollect(_stringCache);
724  _GarbageCollect(_shaderParamCache);
725  _GarbageCollect(_extComputationInputsCache);
726  _GarbageCollect(_extComputationOutputsCache);
727  _GarbageCollect(_extComputationPrimvarsCache);
728  }
729 
730 private:
731  bool _locked;
732 
733  // visible, doubleSided
734  typedef _TypedCache<bool> _BoolCache;
735  mutable _BoolCache _boolCache;
736 
737  // purpose
738  typedef _TypedCache<TfToken> _TokenCache;
739  mutable _TokenCache _tokenCache;
740 
741  // extComputationSceneInputNames
742  typedef _TypedCache<TfTokenVector> _TokenVectorCache;
743  mutable _TokenVectorCache _tokenVectorCache;
744 
745  // extent
746  typedef _TypedCache<GfRange3d> _RangeCache;
747  mutable _RangeCache _rangeCache;
748 
749  // cullstyle
750  typedef _TypedCache<HdCullStyle> _CullStyleCache;
751  mutable _CullStyleCache _cullStyleCache;
752 
753  // transform
754  typedef _TypedCache<GfMatrix4d> _MatrixCache;
755  mutable _MatrixCache _matrixCache;
756 
757  // color (will be VtValue)
758  typedef _TypedCache<GfVec4f> _Vec4Cache;
759  mutable _Vec4Cache _vec4Cache;
760 
761  // sdfPath
762  typedef _TypedCache<SdfPath> _SdfPathCache;
763  mutable _SdfPathCache _sdfPathCache;
764 
765  // primvars, topology, materialResources, extCompInputs
766  typedef _TypedCache<VtValue> _ValueCache;
767  mutable _ValueCache _valueCache;
768 
769  typedef _TypedCache<HdPrimvarDescriptorVector> _PviCache;
770  mutable _PviCache _pviCache;
771 
772  // XXX: shader type caches, shader API will be deprecated soon
773  typedef _TypedCache<std::string> _StringCache;
774  mutable _StringCache _stringCache;
775 
776  typedef _TypedCache<HdMaterialParamVector> _MaterialParamCache;
777  mutable _MaterialParamCache _shaderParamCache;
778 
779  typedef _TypedCache<HdExtComputationInputDescriptorVector>
780  _ExtComputationInputsCache;
781  mutable _ExtComputationInputsCache _extComputationInputsCache;
782 
783  typedef _TypedCache<HdExtComputationOutputDescriptorVector>
784  _ExtComputationOutputsCache;
785  mutable _ExtComputationOutputsCache _extComputationOutputsCache;
786 
787  typedef _TypedCache<HdExtComputationPrimvarDescriptorVector>
788  _ExtComputationPrimvarsCache;
789  mutable _ExtComputationPrimvarsCache _extComputationPrimvarsCache;
790 
791  void _GetCache(_BoolCache **cache) const {
792  *cache = &_boolCache;
793  }
794  void _GetCache(_TokenCache **cache) const {
795  *cache = &_tokenCache;
796  }
797  void _GetCache(_TokenVectorCache **cache) const {
798  *cache = &_tokenVectorCache;
799  }
800  void _GetCache(_RangeCache **cache) const {
801  *cache = &_rangeCache;
802  }
803  void _GetCache(_CullStyleCache **cache) const {
804  *cache = &_cullStyleCache;
805  }
806  void _GetCache(_MatrixCache **cache) const {
807  *cache = &_matrixCache;
808  }
809  void _GetCache(_Vec4Cache **cache) const {
810  *cache = &_vec4Cache;
811  }
812  void _GetCache(_ValueCache **cache) const {
813  *cache = &_valueCache;
814  }
815  void _GetCache(_PviCache **cache) const {
816  *cache = &_pviCache;
817  }
818  void _GetCache(_SdfPathCache **cache) const {
819  *cache = &_sdfPathCache;
820  }
821  // XXX: shader type caches, shader API will be deprecated soon
822  void _GetCache(_StringCache **cache) const {
823  *cache = &_stringCache;
824  }
825  void _GetCache(_MaterialParamCache **cache) const {
826  *cache = &_shaderParamCache;
827  }
828  void _GetCache(_ExtComputationInputsCache **cache) const {
829  *cache = &_extComputationInputsCache;
830  }
831  void _GetCache(_ExtComputationOutputsCache **cache) const {
832  *cache = &_extComputationOutputsCache;
833  }
834  void _GetCache(_ExtComputationPrimvarsCache **cache) const {
835  *cache = &_extComputationPrimvarsCache;
836  }
837 };
838 
839 
840 PXR_NAMESPACE_CLOSE_SCOPE
841 
842 #endif // USDIMAGING_VALUE_CACHE_H
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
A heterogeneous value container without type erasure.
Definition: valueCache.h:56
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
Definition: valueCache.h:317
#define TF_FOR_ALL(iter, c)
Macro for iterating over a container.
Definition: iterator.h:390
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:289
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
void GarbageCollect()
Remove any items from the cache that are marked for defered deletion.
Definition: valueCache.h:710
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...
Definition: value.h:182