HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bufferArrayRegistry.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_IMAGING_HD_ST_BUFFER_ARRAY_REGISTRY_H
8 #define PXR_IMAGING_HD_ST_BUFFER_ARRAY_REGISTRY_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
13 
16 #include "pxr/imaging/hd/perfLog.h"
17 
18 #include "pxr/imaging/hf/perfLog.h"
19 
20 #include "pxr/base/vt/dictionary.h"
21 #include "pxr/base/tf/token.h"
22 
23 #include <tbb/concurrent_unordered_map.h>
24 
25 #include <condition_variable>
26 #include <memory>
27 #include <mutex>
28 
30 
31 
32 using HdBufferArraySharedPtr = std::shared_ptr<class HdBufferArray>;
33 
34 /// \class HdStBufferArrayRegistry
35 ///
36 /// Manages the pool of buffer arrays.
37 ///
39 {
40 public:
41  HF_MALLOC_TAG_NEW("new HdStBufferArrayRegistry");
42 
43  HDST_API
45  ~HdStBufferArrayRegistry() = default;
46 
47  /// Allocate new buffer array range using strategy
48  /// Thread-Safe
49  HDST_API
51  HdStAggregationStrategy *strategy,
52  TfToken const &role,
53  HdBufferSpecVector const &bufferSpecs,
54  HdBufferArrayUsageHint usageHint);
55 
56  /// Triggers reallocation on all buffers managed by the registry.
57  HDST_API
58  void ReallocateAll(HdStAggregationStrategy *strategy);
59 
60  /// Frees up buffers that no longer contain any allocated ranges.
61  HDST_API
62  void GarbageCollect();
63 
64  /// Generate a report on resources consumed by the managed
65  /// buffer array. The returned size is an esitmate of the
66  /// gpu memory consumed by the buffers
67  HDST_API
69  VtDictionary &result) const;
70 
71  /// Debug dump
72  HDST_API
73  friend std::ostream &operator <<(std::ostream &out,
74  const HdStBufferArrayRegistry& self);
75 
76 private:
77 
78  HdStBufferArrayRegistry(const HdStBufferArrayRegistry &) = delete;
79  HdStBufferArrayRegistry &operator=(const HdStBufferArrayRegistry &)=delete;
80 
81  typedef std::list<HdBufferArraySharedPtr> _HdBufferArraySharedPtrList;
82 
83  /// \struct _Entry
84  ///
85  /// Entry in the buffer array cache. The list is the buffer arrays which
86  /// all have the same format. There is as a lock for modifications to the
87  /// entry and a condition used to determine if the entry has been
88  /// construction.
89  ///
90  /// A constructed entry always has at least 1 buffer array in its list.
91  ///
92  struct _Entry
93  {
94  _HdBufferArraySharedPtrList bufferArrays;
95  std::mutex lock;
96  std::condition_variable emptyCondition;
97 
98  // Default / Copy constructors needed for std::make_pair.
99  // as the version of TBB doesn't have emplace() yet.
100  _Entry() {}
101  _Entry(const _Entry &other) { TF_VERIFY(bufferArrays.empty()); }
102  };
103 
104 
105  /// Predicate class for determine if an entry
106  /// has been consturcted (determined by there
107  /// being at least 1 entry in the buffer array list.
108  class _EntryIsNotEmpty
109  {
110  public:
111  _EntryIsNotEmpty(const _Entry &entry) : _entry(entry) {}
112 
113  bool operator()() {
114  return (!(_entry.bufferArrays.empty()));
115  }
116 
117  private:
118  const _Entry &_entry;
119  };
120 
121  using _BufferArrayIndex = tbb::concurrent_unordered_map<
123  _BufferArrayIndex _entries;
124 
125  /// Concurrently adds a new buffer to an entry in the cache.
126  /// If expectedTail differs from the buffer at the end of the
127  /// entries list after locking, then this function fails and
128  /// does not add a new buffer (because another thread beat it
129  /// to it).
130  /// strategy is the factory class used to create the BufferArray.
131  /// role and bufferSpecs are parameters to the BufferArray creation.
132  void _InsertNewBufferArray(_Entry &entry,
133  const HdBufferArraySharedPtr &expectedTail,
134  HdStAggregationStrategy *strategy,
135  TfToken const &role,
136  HdBufferSpecVector const &bufferSpecs,
137  HdBufferArrayUsageHint usageHint);
138 };
139 
140 
142 
143 #endif // PXR_IMAGING_HD_ST_BUFFER_ARRAY_REGISTRY_H
~HdStBufferArrayRegistry()=default
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:26
HDST_API size_t GetResourceAllocation(HdStAggregationStrategy *strategy, VtDictionary &result) const
**But if you need a result
Definition: thread.h:622
HDST_API HdBufferArrayRangeSharedPtr AllocateRange(HdStAggregationStrategy *strategy, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:34
Definition: token.h:70
HDST_API void ReallocateAll(HdStAggregationStrategy *strategy)
Triggers reallocation on all buffers managed by the registry.
HDST_API HdStBufferArrayRegistry()
std::vector< struct HdBufferSpec > HdBufferSpecVector
HDST_API void GarbageCollect()
Frees up buffers that no longer contain any allocated ranges.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HDST_API friend std::ostream & operator<<(std::ostream &out, const HdStBufferArrayRegistry &self)
Debug dump.
#define HDST_API
Definition: api.h:23
uint32_t HdBufferArrayUsageHint
Definition: bufferArray.h:60
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
HF_MALLOC_TAG_NEW("new HdStBufferArrayRegistry")