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