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