All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
resourceRegistry.h
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 HD_RESOURCE_REGISTRY_H
25 #define HD_RESOURCE_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 
31 #include "pxr/imaging/hd/bufferArrayRange.h"
32 #include "pxr/imaging/hd/bufferArrayRegistry.h"
33 #include "pxr/imaging/hd/bufferSource.h"
34 #include "pxr/imaging/hd/bufferSpec.h"
35 #include "pxr/imaging/hd/instanceRegistry.h"
36 #include "pxr/imaging/hd/meshTopology.h"
37 #include "pxr/imaging/hd/perfLog.h"
38 #include "pxr/imaging/hd/strategyBase.h"
39 #include "pxr/imaging/hd/textureResource.h"
40 
41 #include "pxr/imaging/hf/perfLog.h"
42 
43 #include "pxr/base/vt/dictionary.h"
44 #include "pxr/base/tf/singleton.h"
45 #include "pxr/base/tf/token.h"
46 
47 #include <boost/noncopyable.hpp>
48 #include <boost/shared_ptr.hpp>
49 #include <boost/weak_ptr.hpp>
50 #include <map>
51 #include <memory>
52 #include <atomic>
53 #include <tbb/concurrent_vector.h>
54 
55 PXR_NAMESPACE_OPEN_SCOPE
56 
57 
58 typedef boost::shared_ptr<class HdBufferArray> HdBufferArraySharedPtr;
59 typedef boost::shared_ptr<class HdMeshTopology> HdMeshTopologySharedPtr;
60 typedef boost::shared_ptr<class HdBasisCurvesTopology>
61  HdBasisCurvesTopologySharedPtr;
62 typedef boost::weak_ptr<class HdBufferArrayRange> HdBufferArrayRangePtr;
63 typedef boost::shared_ptr<class HdComputation> HdComputationSharedPtr;
64 typedef boost::shared_ptr<class Hd_VertexAdjacency> Hd_VertexAdjacencySharedPtr;
65 typedef boost::shared_ptr<class HdResourceRegistry> HdResourceRegistrySharedPtr;
66 
71 class HdResourceRegistry : public boost::noncopyable {
72 public:
73  HF_MALLOC_TAG_NEW("new HdResourceRegistry");
74 
75  HD_API
77 
78  HD_API
79  virtual ~HdResourceRegistry();
81  HD_API
82  HdBufferArrayRangeSharedPtr AllocateNonUniformBufferArrayRange(
83  TfToken const &role,
84  HdBufferSpecVector const &bufferSpecs,
85  HdBufferArrayUsageHint usageHint);
86 
88  HD_API
89  HdBufferArrayRangeSharedPtr AllocateNonUniformImmutableBufferArrayRange(
90  TfToken const &role,
91  HdBufferSpecVector const &bufferSpecs,
92  HdBufferArrayUsageHint usageHint);
93 
95  HD_API
96  HdBufferArrayRangeSharedPtr AllocateUniformBufferArrayRange(
97  TfToken const &role,
98  HdBufferSpecVector const &bufferSpecs,
99  HdBufferArrayUsageHint usageHint);
100 
102  HD_API
103  HdBufferArrayRangeSharedPtr AllocateShaderStorageBufferArrayRange(
104  TfToken const &role,
105  HdBufferSpecVector const &bufferSpecs,
106  HdBufferArrayUsageHint usageHint);
107 
109  HD_API
110  HdBufferArrayRangeSharedPtr AllocateSingleBufferArrayRange(
111  TfToken const &role,
112  HdBufferSpecVector const &bufferSpecs,
113  HdBufferArrayUsageHint usageHint);
114 
116  HD_API
117  void AddSources(HdBufferArrayRangeSharedPtr const &range,
118  HdBufferSourceVector &sources);
119 
121  HD_API
122  void AddSource(HdBufferArrayRangeSharedPtr const &range,
123  HdBufferSourceSharedPtr const &source);
124 
126  HD_API
127  void AddSource(HdBufferSourceSharedPtr const &source);
128 
135  HD_API
136  void AddComputation(HdBufferArrayRangeSharedPtr const &range,
137  HdComputationSharedPtr const &computaion);
138 
141  HD_API
142  void Commit();
143 
145  HD_API
146  void GarbageCollect();
147 
149  HD_API
150  void GarbageCollectBprims();
151 
156  _nonUniformAggregationStrategy.reset(strategy);
157  }
158 
163  HdAggregationStrategy *strategy) {
164  _nonUniformImmutableAggregationStrategy.reset(strategy);
165  }
166 
170  _uniformUboAggregationStrategy.reset(strategy);
171  }
172 
176  _uniformSsboAggregationStrategy.reset(strategy);
177  }
178 
182  _singleAggregationStrategy.reset(strategy);
183  }
184 
187  return _nonUniformAggregationStrategy.get();
188  }
189 
193  return _nonUniformImmutableAggregationStrategy.get();
194  }
195 
198  return _uniformUboAggregationStrategy.get();
199  }
200 
203  return _uniformSsboAggregationStrategy.get();
204  }
205 
208  return _singleAggregationStrategy.get();
209  }
210 
213  HD_API
215 
229  HD_API
230  std::unique_lock<std::mutex> RegisterMeshTopology(HdTopology::ID id,
232 
233  HD_API
234  std::unique_lock<std::mutex> RegisterBasisCurvesTopology(HdTopology::ID id,
236 
237  HD_API
238  std::unique_lock<std::mutex> RegisterVertexAdjacency(HdTopology::ID id,
240 
245  HD_API
246  std::unique_lock<std::mutex> RegisterMeshIndexRange(
247  HdTopology::ID id, TfToken const &name,
249 
250  HD_API
251  std::unique_lock<std::mutex> RegisterBasisCurvesIndexRange(
252  HdTopology::ID id, TfToken const &name,
254 
259  HD_API
260  std::unique_lock<std::mutex> RegisterPrimvarRange(
261  HdTopology::ID id,
263 
268  HD_API
269  std::unique_lock<std::mutex> RegisterExtComputationDataRange(
270  HdTopology::ID id,
272 
275  typedef size_t TextureKey;
276 
284  HD_API
285  std::unique_lock<std::mutex> RegisterTextureResource(
286  TextureKey id,
288  *pInstance);
289 
292  HD_API
293  std::unique_lock<std::mutex> FindTextureResource(
294  TextureKey id,
296  *instance, bool *found);
297 
299  HD_API
300  virtual void InvalidateShaderRegistry();
301 
303  HD_API
304  friend std::ostream &operator <<(std::ostream &out,
305  const HdResourceRegistry& self);
306 
307 protected:
310  virtual void _GarbageCollect();
311 
315  virtual void _TallyResourceAllocation(VtDictionary *result) const;
316 
317 protected:
318 
319  // aggregated buffer array
320  HdBufferArrayRegistry _nonUniformBufferArrayRegistry;
321  HdBufferArrayRegistry _nonUniformImmutableBufferArrayRegistry;
322  HdBufferArrayRegistry _uniformUboBufferArrayRegistry;
323  HdBufferArrayRegistry _uniformSsboBufferArrayRegistry;
324  HdBufferArrayRegistry _singleBufferArrayRegistry;
325 
326  // current aggregation strategies
327  std::unique_ptr<HdAggregationStrategy> _nonUniformAggregationStrategy;
328  std::unique_ptr<HdAggregationStrategy>
329  _nonUniformImmutableAggregationStrategy;
330  std::unique_ptr<HdAggregationStrategy> _uniformUboAggregationStrategy;
331  std::unique_ptr<HdAggregationStrategy> _uniformSsboAggregationStrategy;
332  std::unique_ptr<HdAggregationStrategy> _singleAggregationStrategy;
333 
334 private:
335 
336  // TODO: this is a transient structure. we'll revisit the BufferSource
337  // interface later.
338  struct _PendingSource {
339  _PendingSource(HdBufferArrayRangeSharedPtr const &range)
340  : range(range)
341  , sources()
342  {
343  }
344 
345  _PendingSource(HdBufferArrayRangeSharedPtr const &range,
346  HdBufferSourceSharedPtr const &source)
347  : range(range)
348  , sources(1, source)
349  {
350  }
351 
352  HdBufferArrayRangeSharedPtr range;
353  HdBufferSourceVector sources;
354  };
355 
356  typedef tbb::concurrent_vector<_PendingSource> _PendingSourceList;
357  _PendingSourceList _pendingSources;
358  std::atomic_size_t _numBufferSourcesToResolve;
359 
360 
361 
362  struct _PendingComputation{
363  _PendingComputation(HdBufferArrayRangeSharedPtr const &range,
364  HdComputationSharedPtr const &computation)
365  : range(range), computation(computation) { }
366  HdBufferArrayRangeSharedPtr range;
367  HdComputationSharedPtr computation;
368  };
369 
370  typedef tbb::concurrent_vector<_PendingComputation> _PendingComputationList;
371  _PendingComputationList _pendingComputations;
372 
373 
374  // instancing registries
375  // meshTopology to HdMeshTopology
377  _MeshTopologyInstance;
378  HdInstanceRegistry<_MeshTopologyInstance> _meshTopologyRegistry;
379 
381  _BasisCurvesTopologyInstance;
383  _basisCurvesTopologyRegistry;
384 
385  // topology to vertex adjacency
387  _VertexAdjacencyInstance;
388  HdInstanceRegistry<_VertexAdjacencyInstance> _vertexAdjacencyRegistry;
389 
390  // topology to HdBufferArrayRange
392  _TopologyIndexRangeInstance;
394  _TopologyIndexRangeInstanceRegistry;
395  typedef tbb::concurrent_unordered_map< TfToken,
396  _TopologyIndexRangeInstanceRegistry,
398  _TopologyIndexRangeInstanceRegMap;
399 
400  _TopologyIndexRangeInstanceRegMap _meshTopologyIndexRangeRegistry;
401  _TopologyIndexRangeInstanceRegMap _basisCurvesTopologyIndexRangeRegistry;
402 
404  _PrimvarRangeInstance;
405  HdInstanceRegistry<_PrimvarRangeInstance> _primvarRangeRegistry;
406 
408  _ExtComputationDataRangeInstance;
410  _extComputationDataRangeRegistry;
411 
412  // texture resource registry
414  _TextureResourceRegistry;
415  HdInstanceRegistry<_TextureResourceRegistry> _textureResourceRegistry;
416 };
417 
418 PXR_NAMESPACE_CLOSE_SCOPE
419 
420 #endif //HD_RESOURCE_REGISTRY_H
The union provides a set of flags that provide hints to the memory management system about the proper...
Definition: bufferArray.h:74
virtual void _TallyResourceAllocation(VtDictionary *result) const
A hook for derived registries to tally their resources by key into the given dictionary.
HD_API HdBufferArrayRangeSharedPtr AllocateUniformBufferArrayRange(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Allocate new uniform buffer range.
Aggregation strategy base class.
Definition: strategyBase.h:48
A map with string keys and VtValue values.
Definition: dictionary.h:61
HD_API HdBufferArrayRangeSharedPtr AllocateNonUniformBufferArrayRange(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Allocate new non uniform buffer array range.
HD_API HdBufferArrayRangeSharedPtr AllocateSingleBufferArrayRange(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Allocate single entry (non-aggregated) buffer array range.
virtual void _GarbageCollect()
A hook for derived registries to perform additional GC when GarbageCollect() is invoked.
Functor to use for hash maps from tokens to other things.
Definition: token.h:168
HD_API std::unique_lock< std::mutex > RegisterExtComputationDataRange(HdTopology::ID id, HdInstance< HdTopology::ID, HdBufferArrayRangeSharedPtr > *pInstance)
ExtComputation data array range instancing Returns the HdInstance pointing to shared HdBufferArrayRan...
Manages the pool of buffer arrays.
HdInstanceRegistry is a dictionary container of HdInstance.
void SetSingleStorageAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for single buffers (for nested instancer).
bool HasNonUniformAggregationStrategy() const
Returns whether an aggregation strategy is set for non uniform params.
HD_API std::unique_lock< std::mutex > FindTextureResource(TextureKey id, HdInstance< TextureKey, HdTextureResourceSharedPtr > *instance, bool *found)
Find a texture in the texture registry.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
HD_API HdBufferArrayRangeSharedPtr AllocateNonUniformImmutableBufferArrayRange(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Allocate new immutable non uniform buffer array range.
void SetUniformAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for uniform (shader globals) Takes ownership of the passed in strategy o...
HD_API std::unique_lock< std::mutex > RegisterMeshIndexRange(HdTopology::ID id, TfToken const &name, HdInstance< HdTopology::ID, HdBufferArrayRangeSharedPtr > *pInstance)
Index buffer array range instancing Returns the HdInstance points to shared HdBufferArrayRange, distinguished by given ID.
bool HasShaderStorageAggregationStrategy() const
Returns whether an aggregation strategy is set for SSBO.
HD_API void GarbageCollect()
cleanup all buffers and remove if empty
HD_API friend std::ostream & operator<<(std::ostream &out, const HdResourceRegistry &self)
Debug dump.
HD_API VtDictionary GetResourceAllocation() const
Returns a report of resource allocation by role in bytes and a summary total allocation of GPU memory...
bool HasUniformAggregationStrategy() const
Returns whether an aggregation strategy is set for uniform params.
HD_API void AddSources(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceVector &sources)
Append source data for given range to be committed later.
A central registry of all GPU resources.
HD_API void AddComputation(HdBufferArrayRangeSharedPtr const &range, HdComputationSharedPtr const &computaion)
Append a gpu computation into queue.
void SetNonUniformAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for non uniform parameters (vertex, varying, facevarying) Takes ownershi...
HD_API void Commit()
Commits all in-flight source data to the GPU, freeing the source buffers.
bool HasSingleStorageAggregationStrategy() const
Returns whether an aggregation strategy is set for single buffers.
HD_API std::unique_lock< std::mutex > RegisterMeshTopology(HdTopology::ID id, HdInstance< HdTopology::ID, HdMeshTopologySharedPtr > *pInstance)
Topology instancing Returns (in the pointer) the HdInstance points to shared HdMeshTopology, distinguished by given ID.
bool HasNonUniformImmutableAggregationStrategy() const
Returns whether an aggregation strategy is set for non uniform immutable params.
HD_API void GarbageCollectBprims()
cleanup all Bprim registries
HD_API std::unique_lock< std::mutex > RegisterTextureResource(TextureKey id, HdInstance< TextureKey, HdTextureResourceSharedPtr > *pInstance)
Register a texture into the texture registry.
HD_API std::unique_lock< std::mutex > RegisterPrimvarRange(HdTopology::ID id, HdInstance< HdTopology::ID, HdBufferArrayRangeSharedPtr > *pInstance)
Primvar array range instancing Returns the HdInstance pointing to shared HdBufferArrayRange, distinguished by given ID.
size_t TextureKey
Globally unique id for texture, see RegisterTextureResource() for details.
virtual HD_API void InvalidateShaderRegistry()
Invalidate any shaders registered with this registry.
This class is used as a pointer to the shared instance in HdInstanceRegistry.
void SetShaderStorageAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for SSBO (uniform primvars) Takes ownership of the passed in strategy ob...
HD_API void AddSource(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtr const &source)
Append a source data for given range to be committed later.
void SetNonUniformImmutableAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for non uniform immutable parameters (vertex, varying, facevarying) Takes ownership of the passed in strategy object.
HD_API HdBufferArrayRangeSharedPtr AllocateShaderStorageBufferArrayRange(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Allocate new shader storage buffer range.