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 PXR_IMAGING_HD_ST_RESOURCE_REGISTRY_H
25 #define PXR_IMAGING_HD_ST_RESOURCE_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/vt/dictionary.h"
29 
30 #include "pxr/imaging/hdSt/api.h"
31 
32 #include "pxr/imaging/hgi/hgi.h"
33 
34 #include "pxr/imaging/hd/bufferArrayRange.h"
35 #include "pxr/imaging/hd/bufferArrayRegistry.h"
36 #include "pxr/imaging/hd/bufferSource.h"
37 #include "pxr/imaging/hd/bufferSpec.h"
38 #include "pxr/imaging/hd/instanceRegistry.h"
39 #include "pxr/imaging/hd/resourceRegistry.h"
40 
41 #include <boost/shared_ptr.hpp>
42 #include <tbb/concurrent_vector.h>
43 
44 #include <atomic>
45 #include <map>
46 #include <memory>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 using HdComputationSharedPtr = std::shared_ptr<class HdComputation>;
51 
52 using HdStDispatchBufferSharedPtr = std::shared_ptr<class HdStDispatchBuffer>;
53 
54 typedef boost::shared_ptr<class HdStGLSLProgram>
55  HdStGLSLProgramSharedPtr;
56 typedef boost::shared_ptr<class HdSt_BasisCurvesTopology>
57  HdSt_BasisCurvesTopologySharedPtr;
58 typedef boost::shared_ptr<class HdSt_GeometricShader>
59  HdSt_GeometricShaderSharedPtr;
60 
61 using HdStTextureResourceSharedPtr =
62  std::shared_ptr<class HdStTextureResource>;
63 using HdStTextureResourceHandleSharedPtr =
64  std::shared_ptr<class HdStTextureResourceHandle>;
65 
66 using HdStPersistentBufferSharedPtr =
67  std::shared_ptr<class HdStPersistentBuffer>;
68 
69 using HdStResourceRegistrySharedPtr =
70  std::shared_ptr<class HdStResourceRegistry>;
71 using Hd_VertexAdjacencySharedPtr =
72  std::shared_ptr<class Hd_VertexAdjacency>;
73 using HdSt_MeshTopologySharedPtr =
74  std::shared_ptr<class HdSt_MeshTopology>;
75 
81 public:
82  HF_MALLOC_TAG_NEW("new HdStResourceRegistry");
83 
84  HDST_API
86 
87  HDST_API
88  virtual ~HdStResourceRegistry();
89 
90  HDST_API
91  void InvalidateShaderRegistry() override;
92 
93  HDST_API
94  VtDictionary GetResourceAllocation() const override;
95 
96  HDST_API
97  void SetHgi(Hgi* hgi);
98 
105 
106  HDST_API
107  HdBufferArrayRangeSharedPtr AllocateNonUniformBufferArrayRange(
108  TfToken const &role,
109  HdBufferSpecVector const &bufferSpecs,
110  HdBufferArrayUsageHint usageHint);
111 
112  HDST_API
113  HdBufferArrayRangeSharedPtr AllocateNonUniformImmutableBufferArrayRange(
114  TfToken const &role,
115  HdBufferSpecVector const &bufferSpecs,
116  HdBufferArrayUsageHint usageHint);
117 
118  HDST_API
119  HdBufferArrayRangeSharedPtr AllocateUniformBufferArrayRange(
120  TfToken const &role,
121  HdBufferSpecVector const &bufferSpecs,
122  HdBufferArrayUsageHint usageHint);
123 
124  HDST_API
125  HdBufferArrayRangeSharedPtr AllocateShaderStorageBufferArrayRange(
126  TfToken const &role,
127  HdBufferSpecVector const &bufferSpecs,
128  HdBufferArrayUsageHint usageHint);
129 
130  HDST_API
131  HdBufferArrayRangeSharedPtr AllocateSingleBufferArrayRange(
132  TfToken const &role,
133  HdBufferSpecVector const &bufferSpecs,
134  HdBufferArrayUsageHint usageHint);
135 
151 
152  HDST_API
153  HdBufferArrayRangeSharedPtr UpdateNonUniformBufferArrayRange(
154  TfToken const &role,
155  HdBufferArrayRangeSharedPtr const& curRange,
156  HdBufferSpecVector const &updatedOrAddedSpecs,
157  HdBufferSpecVector const& removedSpecs,
158  HdBufferArrayUsageHint usageHint);
159 
160  HDST_API
161  HdBufferArrayRangeSharedPtr UpdateNonUniformImmutableBufferArrayRange(
162  TfToken const &role,
163  HdBufferArrayRangeSharedPtr const& curRange,
164  HdBufferSpecVector const &updatedOrAddedSpecs,
165  HdBufferSpecVector const& removedSpecs,
166  HdBufferArrayUsageHint usageHint);
167 
168  HDST_API
169  HdBufferArrayRangeSharedPtr UpdateUniformBufferArrayRange(
170  TfToken const &role,
171  HdBufferArrayRangeSharedPtr const& curRange,
172  HdBufferSpecVector const &updatedOrAddedSpecs,
173  HdBufferSpecVector const& removedSpecs,
174  HdBufferArrayUsageHint usageHint);
175 
176  HDST_API
177  HdBufferArrayRangeSharedPtr UpdateShaderStorageBufferArrayRange(
178  TfToken const &role,
179  HdBufferArrayRangeSharedPtr const& curRange,
180  HdBufferSpecVector const &updatedOrAddedSpecs,
181  HdBufferSpecVector const& removedSpecs,
182  HdBufferArrayUsageHint usageHint);
183 
187 
189  HDST_API
190  void AddSources(HdBufferArrayRangeSharedPtr const &range,
191  HdBufferSourceSharedPtrVector &sources);
192 
194  HDST_API
195  void AddSource(HdBufferArrayRangeSharedPtr const &range,
196  HdBufferSourceSharedPtr const &source);
197 
199  HDST_API
200  void AddSource(HdBufferSourceSharedPtr const &source);
201 
208  HDST_API
209  void AddComputation(HdBufferArrayRangeSharedPtr const &range,
210  HdComputationSharedPtr const &computaion);
211 
215 
218  HDST_API
219  HdStDispatchBufferSharedPtr RegisterDispatchBuffer(
220  TfToken const &role, int count, int commandNumUints);
221 
224  HDST_API
225  HdStPersistentBufferSharedPtr RegisterPersistentBuffer(
226  TfToken const &role, size_t dataSize, void *data);
227 
229  HDST_API
231 
233  HDST_API
235 
239 
250 
252  HDST_API
254  RegisterMeshTopology(HdInstance<HdSt_MeshTopologySharedPtr>::ID id);
255 
256  HDST_API
258  RegisterBasisCurvesTopology(
259  HdInstance<HdSt_BasisCurvesTopologySharedPtr>::ID id);
260 
261  HDST_API
263  RegisterVertexAdjacency(HdInstance<Hd_VertexAdjacencySharedPtr>::ID id);
264 
269  HDST_API
272  HdInstance<HdBufferArrayRangeSharedPtr>::ID id, TfToken const &name);
273 
274  HDST_API
276  RegisterBasisCurvesIndexRange(
277  HdInstance<HdBufferArrayRangeSharedPtr>::ID id, TfToken const &name);
278 
283  HDST_API
286  HdInstance<HdBufferArrayRangeSharedPtr>::ID id);
287 
292  HDST_API
295  HdInstance<HdBufferArrayRangeSharedPtr>::ID id);
296 
304  HDST_API
307 
310  HDST_API
312  FindTextureResource(TextureKey id, bool *found);
313 
315  HDST_API
317  RegisterGeometricShader(HdInstance<HdSt_GeometricShaderSharedPtr>::ID id);
318 
320  HDST_API
322  RegisterGLSLProgram(HdInstance<HdStGLSLProgramSharedPtr>::ID id);
323 
325  HDST_API
328  HdInstance<HdStTextureResourceHandleSharedPtr>::ID id);
329 
331  HDST_API
334  HdInstance<HdStTextureResourceHandleSharedPtr>::ID id, bool *found);
335 
336 public:
337  //
338  // Unit test API
339  //
340 
345  _nonUniformAggregationStrategy.reset(strategy);
346  }
347 
352  HdAggregationStrategy *strategy) {
353  _nonUniformImmutableAggregationStrategy.reset(strategy);
354  }
355 
359  _uniformUboAggregationStrategy.reset(strategy);
360  }
361 
365  _uniformSsboAggregationStrategy.reset(strategy);
366  }
367 
371  _singleAggregationStrategy.reset(strategy);
372  }
373 
375  HDST_API
376  friend std::ostream &operator <<(
377  std::ostream &out,
378  const HdStResourceRegistry& self);
379 
380 protected:
381  void _Commit() override;
382  void _GarbageCollect() override;
383  void _GarbageCollectBprims() override;
384 
385 private:
386  // Wrapper function for BAR allocation
387  HdBufferArrayRangeSharedPtr _AllocateBufferArrayRange(
388  HdAggregationStrategy *strategy,
389  HdBufferArrayRegistry &bufferArrayRegistry,
390  TfToken const &role,
391  HdBufferSpecVector const &bufferSpecs,
392  HdBufferArrayUsageHint usageHint);
393 
395  HdBufferArrayRangeSharedPtr _UpdateBufferArrayRange(
396  HdAggregationStrategy *strategy,
397  HdBufferArrayRegistry &bufferArrayRegistry,
398  TfToken const &role,
399  HdBufferArrayRangeSharedPtr const& curRange,
400  HdBufferSpecVector const &updatedOrAddedSpecs,
401  HdBufferSpecVector const& removedSpecs,
402  HdBufferArrayUsageHint usageHint);
403 
404  // Tally resources by key into the given dictionary. Any additions should
405  // be cumulative with the existing key values.
406  void _TallyResourceAllocation(VtDictionary *result) const;
407 
408  // TODO: this is a transient structure. we'll revisit the BufferSource
409  // interface later.
410  struct _PendingSource {
411  _PendingSource(HdBufferArrayRangeSharedPtr const &range)
412  : range(range)
413  , sources()
414  {
415  }
416 
417  _PendingSource(HdBufferArrayRangeSharedPtr const &range,
418  HdBufferSourceSharedPtr const &source)
419  : range(range)
420  , sources(1, source)
421  {
422  }
423 
424  HdBufferArrayRangeSharedPtr range;
425  HdBufferSourceSharedPtrVector sources;
426  };
427 
428  Hgi* _hgi;
429 
430  typedef tbb::concurrent_vector<_PendingSource> _PendingSourceList;
431  _PendingSourceList _pendingSources;
432  std::atomic_size_t _numBufferSourcesToResolve;
433 
434  struct _PendingComputation{
435  _PendingComputation(HdBufferArrayRangeSharedPtr const &range,
436  HdComputationSharedPtr const &computation)
437  : range(range), computation(computation) { }
438  HdBufferArrayRangeSharedPtr range;
439  HdComputationSharedPtr computation;
440  };
441 
442  typedef tbb::concurrent_vector<_PendingComputation> _PendingComputationList;
443  _PendingComputationList _pendingComputations;
444 
445  // aggregated buffer array
446  HdBufferArrayRegistry _nonUniformBufferArrayRegistry;
447  HdBufferArrayRegistry _nonUniformImmutableBufferArrayRegistry;
448  HdBufferArrayRegistry _uniformUboBufferArrayRegistry;
449  HdBufferArrayRegistry _uniformSsboBufferArrayRegistry;
450  HdBufferArrayRegistry _singleBufferArrayRegistry;
451 
452  // current aggregation strategies
453  std::unique_ptr<HdAggregationStrategy> _nonUniformAggregationStrategy;
454  std::unique_ptr<HdAggregationStrategy>
455  _nonUniformImmutableAggregationStrategy;
456  std::unique_ptr<HdAggregationStrategy> _uniformUboAggregationStrategy;
457  std::unique_ptr<HdAggregationStrategy> _uniformSsboAggregationStrategy;
458  std::unique_ptr<HdAggregationStrategy> _singleAggregationStrategy;
459 
460  typedef std::vector<HdStDispatchBufferSharedPtr>
461  _DispatchBufferRegistry;
462  _DispatchBufferRegistry _dispatchBufferRegistry;
463 
464  typedef std::vector<HdStPersistentBufferSharedPtr>
465  _PersistentBufferRegistry;
466  _PersistentBufferRegistry _persistentBufferRegistry;
467 
468  // Register mesh topology.
470  _meshTopologyRegistry;
471 
472  // Register basisCurves topology.
474  _basisCurvesTopologyRegistry;
475 
476  // Register vertex adjacency.
478  _vertexAdjacencyRegistry;
479 
480  // Register topology index buffers.
482  _TopologyIndexRangeInstanceRegistry;
483  typedef tbb::concurrent_unordered_map< TfToken,
484  _TopologyIndexRangeInstanceRegistry,
486  _TopologyIndexRangeInstanceRegMap;
487 
488  _TopologyIndexRangeInstanceRegMap _meshTopologyIndexRangeRegistry;
489  _TopologyIndexRangeInstanceRegMap _basisCurvesTopologyIndexRangeRegistry;
490 
491  // Register shared primvar buffers.
493  _primvarRangeRegistry;
494 
495  // Register ext computation resource.
497  _extComputationDataRangeRegistry;
498 
499  // texture resource registry
501  _textureResourceRegistry;
502  // geometric shader registry
504  _geometricShaderRegistry;
505 
506  // glsl shader program registry
508  _glslProgramRegistry;
509 
510  // texture resource handle registry
512  _textureResourceHandleRegistry;
513 
514 };
515 
516 
517 PXR_NAMESPACE_CLOSE_SCOPE
518 
519 #endif //PXR_IMAGING_HD_ST_RESOURCE_REGISTRY_H
The union provides a set of flags that provide hints to the memory management system about the proper...
Definition: bufferArray.h:70
void SetNonUniformAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for non uniform parameters (vertex, varying, facevarying) Takes ownershi...
Aggregation strategy base class.
Definition: strategyBase.h:48
HDST_API HdInstance< HdBufferArrayRangeSharedPtr > RegisterPrimvarRange(HdInstance< HdBufferArrayRangeSharedPtr >::ID id)
Primvar array range instancing Returns the HdInstance pointing to shared HdBufferArrayRange, distinguished by given ID.
HDST_API VtDictionary GetResourceAllocation() const override
Returns a report of resource allocation by role in bytes and a summary total allocation of GPU memory...
void SetSingleStorageAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for single buffers (for nested instancer).
A map with string keys and VtValue values.
Definition: dictionary.h:63
HDST_API void AddComputation(HdBufferArrayRangeSharedPtr const &range, HdComputationSharedPtr const &computaion)
Append a gpu computation into queue.
HDST_API HdInstance< HdStTextureResourceHandleSharedPtr > RegisterTextureResourceHandle(HdInstance< HdStTextureResourceHandleSharedPtr >::ID id)
Register a texture resource handle.
Functor to use for hash maps from tokens to other things.
Definition: token.h:166
HDST_API void GarbageCollectDispatchBuffers()
Remove any entries associated with expired dispatch buffers.
Manages the pool of buffer arrays.
HDST_API void AddSources(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtrVector &sources)
Resource update &amp; computation queuing API
HDST_API void AddSource(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtr const &source)
Append a source data for given range to be committed later.
HDST_API void GarbageCollectPersistentBuffers()
Remove any entries associated with expired persistently mapped buffers.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
void SetShaderStorageAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for SSBO (uniform primvars) Takes ownership of the passed in strategy ob...
A central registry of all GPU resources.
void _Commit() override
A hook for derived registries to perform additional resource commits.
HDST_API HdInstance< HdStTextureResourceSharedPtr > FindTextureResource(TextureKey id, bool *found)
Find a texture in the texture registry.
HDST_API HdStPersistentBufferSharedPtr RegisterPersistentBuffer(TfToken const &role, size_t dataSize, void *data)
Register a buffer initialized with dataSize bytes of data to be used as a persistently mapped shader ...
A central registry for resources.
HDST_API HdStDispatchBufferSharedPtr RegisterDispatchBuffer(TfToken const &role, int count, int commandNumUints)
Dispatch &amp; persistent buffer API
void SetUniformAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for uniform (shader globals) Takes ownership of the passed in strategy o...
Hydra Graphics Interface.
Definition: hgi.h:68
HDST_API HdBufferArrayRangeSharedPtr UpdateNonUniformBufferArrayRange(TfToken const &role, HdBufferArrayRangeSharedPtr const &curRange, HdBufferSpecVector const &updatedOrAddedSpecs, HdBufferSpecVector const &removedSpecs, HdBufferArrayUsageHint usageHint)
BAR allocation/migration/update API
HDST_API HdInstance< HdBufferArrayRangeSharedPtr > RegisterExtComputationDataRange(HdInstance< HdBufferArrayRangeSharedPtr >::ID id)
ExtComputation data array range instancing Returns the HdInstance pointing to shared HdBufferArrayRan...
HDST_API HdInstance< HdStTextureResourceHandleSharedPtr > FindTextureResourceHandle(HdInstance< HdStTextureResourceHandleSharedPtr >::ID id, bool *found)
Find a texture resource handle.
HDST_API HdInstance< HdStTextureResourceSharedPtr > RegisterTextureResource(TextureKey id)
Register a texture into the texture registry.
void _GarbageCollect() override
Hooks for derived registries to perform additional GC when GarbageCollect() or GarbageCollectBprims()...
HDST_API HdInstance< HdSt_GeometricShaderSharedPtr > RegisterGeometricShader(HdInstance< HdSt_GeometricShaderSharedPtr >::ID id)
Register a geometric shader.
size_t TextureKey
Globally unique id for texture, see HdRenderIndex::GetTextureKey() for details.
HDST_API friend std::ostream & operator<<(std::ostream &out, const HdStResourceRegistry &self)
Debug dump.
HDST_API HdInstance< HdSt_MeshTopologySharedPtr > RegisterMeshTopology(HdInstance< HdSt_MeshTopologySharedPtr >::ID id)
Instance Registries
void SetNonUniformImmutableAggregationStrategy(HdAggregationStrategy *strategy)
Set the aggregation strategy for non uniform immutable parameters (vertex, varying, facevarying) Takes ownership of the passed in strategy object.
This class is used as an interface to a shared instance in HdInstanceRegistry.
HDST_API HdInstance< HdBufferArrayRangeSharedPtr > RegisterMeshIndexRange(HdInstance< HdBufferArrayRangeSharedPtr >::ID id, TfToken const &name)
Topology Index buffer array range instancing Returns the HdInstance points to shared HdBufferArrayRan...
HDST_API HdBufferArrayRangeSharedPtr AllocateNonUniformBufferArrayRange(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
BAR allocation API
HDST_API HdInstance< HdStGLSLProgramSharedPtr > RegisterGLSLProgram(HdInstance< HdStGLSLProgramSharedPtr >::ID id)
Register a GLSL program into the program registry.
HDST_API void InvalidateShaderRegistry() override
Invalidate any shaders registered with this registry.