24#ifndef PXR_IMAGING_HD_ST_RESOURCE_REGISTRY_H
25#define PXR_IMAGING_HD_ST_RESOURCE_REGISTRY_H
30#include "pxr/imaging/hdSt/api.h"
31#include "pxr/imaging/hdSt/bufferArrayRegistry.h"
33#include "pxr/imaging/hgi/hgi.h"
35#include "pxr/imaging/hd/bufferArrayRange.h"
36#include "pxr/imaging/hd/bufferSource.h"
37#include "pxr/imaging/hd/bufferSpec.h"
38#include "pxr/imaging/hd/enums.h"
39#include "pxr/imaging/hd/instanceRegistry.h"
40#include "pxr/imaging/hd/resourceRegistry.h"
42#include <tbb/concurrent_vector.h>
48#ifdef PXR_MATERIALX_SUPPORT_ENABLED
49#include <MaterialXCore/Library.h>
50MATERIALX_NAMESPACE_BEGIN
51 using ShaderPtr = std::shared_ptr<class Shader>;
52MATERIALX_NAMESPACE_END
55PXR_NAMESPACE_OPEN_SCOPE
57using HdStComputationSharedPtr = std::shared_ptr<class HdStComputation>;
58using HdStDispatchBufferSharedPtr = std::shared_ptr<class HdStDispatchBuffer>;
59using HdStGLSLProgramSharedPtr = std::shared_ptr<class HdStGLSLProgram>;
60using HioGlslfxSharedPtr = std::shared_ptr<class HioGlslfx>;
62using HdSt_BasisCurvesTopologySharedPtr =
63 std::shared_ptr<class HdSt_BasisCurvesTopology>;
65using HdStShaderCodePtr =
66 std::weak_ptr<class HdStShaderCode>;
67using HdSt_GeometricShaderSharedPtr =
68 std::shared_ptr<class HdSt_GeometricShader>;
70using HdStTextureHandleSharedPtr =
71 std::shared_ptr<class HdStTextureHandle>;
72using HdStTextureObjectSharedPtr =
73 std::shared_ptr<class HdStTextureObject>;
74using HdStBufferResourceSharedPtr =
75 std::shared_ptr<class HdStBufferResource>;
76using HdStResourceRegistrySharedPtr =
77 std::shared_ptr<class HdStResourceRegistry>;
78using HdSt_VertexAdjacencyBuilderSharedPtr =
79 std::shared_ptr<class HdSt_VertexAdjacencyBuilder>;
80using HdSt_MeshTopologySharedPtr =
81 std::shared_ptr<class HdSt_MeshTopology>;
82using HgiResourceBindingsSharedPtr =
83 std::shared_ptr<HgiResourceBindingsHandle>;
84using HgiGraphicsPipelineSharedPtr =
85 std::shared_ptr<HgiGraphicsPipelineHandle>;
86using HgiComputePipelineSharedPtr =
87 std::shared_ptr<HgiComputePipelineHandle>;
106enum HdStComputeQueue {
107 HdStComputeQueueZero=0,
110 HdStComputeQueueThree,
111 HdStComputeQueueCount};
113using HdStComputationComputeQueuePairVector =
114 std::vector<std::pair<HdStComputationSharedPtr, HdStComputeQueue>>;
124 HF_MALLOC_TAG_NEW(
"new HdStResourceRegistry");
137 std::string
const& path)
override;
164 HdTextureType textureType,
172 size_t memoryRequest,
177 HdStShaderCodePtr
const &shaderCode);
191 HdTextureType textureType);
202 HdTextureType textureType,
203 size_t memoryRequest);
215 HdBufferSpecVector
const &bufferSpecs,
216 HdBufferArrayUsageHint usageHint);
219 HdBufferArrayRangeSharedPtr AllocateNonUniformImmutableBufferArrayRange(
221 HdBufferSpecVector
const &bufferSpecs,
222 HdBufferArrayUsageHint usageHint);
225 HdBufferArrayRangeSharedPtr AllocateUniformBufferArrayRange(
227 HdBufferSpecVector
const &bufferSpecs,
228 HdBufferArrayUsageHint usageHint);
231 HdBufferArrayRangeSharedPtr AllocateShaderStorageBufferArrayRange(
233 HdBufferSpecVector
const &bufferSpecs,
234 HdBufferArrayUsageHint usageHint);
237 HdBufferArrayRangeSharedPtr AllocateSingleBufferArrayRange(
239 HdBufferSpecVector
const &bufferSpecs,
240 HdBufferArrayUsageHint usageHint);
261 HdBufferArrayRangeSharedPtr
const& curRange,
262 HdBufferSpecVector
const &updatedOrAddedSpecs,
263 HdBufferSpecVector
const& removedSpecs,
264 HdBufferArrayUsageHint usageHint);
267 HdBufferArrayRangeSharedPtr UpdateNonUniformImmutableBufferArrayRange(
269 HdBufferArrayRangeSharedPtr
const& curRange,
270 HdBufferSpecVector
const &updatedOrAddedSpecs,
271 HdBufferSpecVector
const& removedSpecs,
272 HdBufferArrayUsageHint usageHint);
275 HdBufferArrayRangeSharedPtr UpdateUniformBufferArrayRange(
277 HdBufferArrayRangeSharedPtr
const& curRange,
278 HdBufferSpecVector
const &updatedOrAddedSpecs,
279 HdBufferSpecVector
const& removedSpecs,
280 HdBufferArrayUsageHint usageHint);
283 HdBufferArrayRangeSharedPtr UpdateShaderStorageBufferArrayRange(
285 HdBufferArrayRangeSharedPtr
const& curRange,
286 HdBufferSpecVector
const &updatedOrAddedSpecs,
287 HdBufferSpecVector
const& removedSpecs,
288 HdBufferArrayUsageHint usageHint);
297 HdBufferSourceSharedPtrVector &&sources);
301 void AddSource(HdBufferArrayRangeSharedPtr
const &range,
302 HdBufferSourceSharedPtr
const &source);
316 HdStComputationSharedPtr
const &computation,
317 HdStComputeQueue
const queue);
327 TfToken const &role,
int count,
int commandNumUints);
338 HgiBufferUsage bufferUsage);
370 RegisterBasisCurvesTopology(
371 HdInstance<HdSt_BasisCurvesTopologySharedPtr>::ID
id);
375 RegisterVertexAdjacencyBuilder(
376 HdInstance<HdSt_VertexAdjacencyBuilderSharedPtr>::ID
id);
385 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id,
TfToken const &name);
389 RegisterBasisCurvesIndexRange(
390 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id,
TfToken const &name);
399 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id);
408 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id);
425#ifdef PXR_MATERIALX_SUPPORT_ENABLED
429 RegisterMaterialXShader(HdInstance<MaterialX::ShaderPtr>::ID
id);
460 const std::string& identifier,
461 const std::function<std::unique_ptr<HdResourceRegistry>()>& factory);
478 HgiComputeDispatch dispatchMethod = HgiComputeDispatchSerial);
509 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
510 _nonUniformAggregationStrategy = std::move(strategy);
517 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
518 _nonUniformImmutableAggregationStrategy = std::move(strategy);
524 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
525 _uniformUboAggregationStrategy = std::move(strategy);
531 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
532 _uniformSsboAggregationStrategy = std::move(strategy);
538 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
539 _singleAggregationStrategy = std::move(strategy);
553 void _CommitTextures();
555 HdBufferArrayRangeSharedPtr _AllocateBufferArrayRange(
559 HdBufferSpecVector
const &bufferSpecs,
560 HdBufferArrayUsageHint usageHint);
563 HdBufferArrayRangeSharedPtr _UpdateBufferArrayRange(
567 HdBufferArrayRangeSharedPtr
const& curRange,
568 HdBufferSpecVector
const &updatedOrAddedSpecs,
569 HdBufferSpecVector
const& removedSpecs,
570 HdBufferArrayUsageHint usageHint);
574 void _TallyResourceAllocation(
VtDictionary *result)
const;
578 struct _PendingSource {
579 _PendingSource(HdBufferArrayRangeSharedPtr
const &range)
585 _PendingSource(HdBufferArrayRangeSharedPtr
const &range,
586 HdBufferSourceSharedPtr
const &source)
592 _PendingSource(HdBufferArrayRangeSharedPtr
const &range,
593 HdBufferSourceSharedPtrVector && sources)
595 , sources(
std::move(sources))
599 HdBufferArrayRangeSharedPtr range;
600 HdBufferSourceSharedPtrVector sources;
605 typedef tbb::concurrent_vector<_PendingSource> _PendingSourceList;
606 _PendingSourceList _pendingSources;
607 std::atomic_size_t _numBufferSourcesToResolve;
609 struct _PendingComputation{
610 _PendingComputation(HdBufferArrayRangeSharedPtr
const &range,
611 HdStComputationSharedPtr
const &computation)
612 : range(range), computation(computation) { }
613 HdBufferArrayRangeSharedPtr range;
614 HdStComputationSharedPtr computation;
620 typedef tbb::concurrent_vector<_PendingComputation> _PendingComputationList;
621 _PendingComputationList _pendingComputations[HdStComputeQueueCount];
631 std::unique_ptr<HdStAggregationStrategy> _nonUniformAggregationStrategy;
632 std::unique_ptr<HdStAggregationStrategy>
633 _nonUniformImmutableAggregationStrategy;
634 std::unique_ptr<HdStAggregationStrategy> _uniformUboAggregationStrategy;
635 std::unique_ptr<HdStAggregationStrategy> _uniformSsboAggregationStrategy;
636 std::unique_ptr<HdStAggregationStrategy> _singleAggregationStrategy;
638 typedef std::vector<HdStDispatchBufferSharedPtr>
639 _DispatchBufferRegistry;
640 _DispatchBufferRegistry _dispatchBufferRegistry;
642 typedef std::vector<HdStBufferResourceSharedPtr>
643 _BufferResourceRegistry;
644 _BufferResourceRegistry _bufferResourceRegistry;
648 _meshTopologyRegistry;
652 _basisCurvesTopologyRegistry;
656 _vertexAdjacencyBuilderRegistry;
660 _TopologyIndexRangeInstanceRegistry;
661 typedef tbb::concurrent_unordered_map<
TfToken,
662 _TopologyIndexRangeInstanceRegistry,
664 _TopologyIndexRangeInstanceRegMap;
666 _TopologyIndexRangeInstanceRegMap _meshTopologyIndexRangeRegistry;
667 _TopologyIndexRangeInstanceRegMap _basisCurvesTopologyIndexRangeRegistry;
671 _primvarRangeRegistry;
675 _extComputationDataRangeRegistry;
679 _geometricShaderRegistry;
683 _glslProgramRegistry;
689#ifdef PXR_MATERIALX_SUPPORT_ENABLED
695 std::unique_ptr<class HdSt_TextureHandleRegistry> _textureHandleRegistry;
699 _resourceBindingsRegistry;
703 _graphicsPipelineRegistry;
707 _computePipelineRegistry;
709 using _SubResourceRegistryMap =
710 tbb::concurrent_unordered_map<std::string,
711 std::unique_ptr<HdResourceRegistry>>;
712 _SubResourceRegistryMap _subResourceRegistries;
714 HgiBlitCmdsUniquePtr _blitCmds;
715 HgiComputeCmdsUniquePtr _computeCmds;
717 std::unique_ptr<HdStStagingBuffer> _stagingBuffer;
721PXR_NAMESPACE_CLOSE_SCOPE
This class is used as an interface to a shared instance in HdInstanceRegistry.
HdInstanceRegistry is a dictionary container of HdInstance.
A central registry for resources.
Collection of standard parameters such as wrap modes to sample a texture.
Aggregation strategy base class.
Manages the pool of buffer arrays.
A central registry of all GPU resources.
HDST_API HdResourceRegistry * FindOrCreateSubResourceRegistry(const std::string &identifier, const std::function< std::unique_ptr< HdResourceRegistry >()> &factory)
Finds a sub resource registry for the given identifier if it already exists or creates one by invokin...
HDST_API void SubmitComputeWork(HgiSubmitWaitType wait=HgiSubmitWaitTypeNoWait)
Submits compute work queued in global compute cmds for GPU execution.
HDST_API void AddSources(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtrVector &&sources)
Append source data for given range to be committed later.
HDST_API void AddSource(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtr const &source)
Append a source data for given range to be committed later.
HDST_API void AddSource(HdBufferSourceSharedPtr const &source)
Append a source data just to be resolved (used for cpu computations).
void SetShaderStorageAggregationStrategy(std::unique_ptr< HdStAggregationStrategy > &&strategy)
Set the aggregation strategy for SSBO (uniform primvars) Takes ownership of the passed in strategy ob...
HDST_API HdInstance< HdSt_MeshTopologySharedPtr > RegisterMeshTopology(HdInstance< HdSt_MeshTopologySharedPtr >::ID id)
These registries implement sharing and deduplication of data based on computed hash identifiers.
HDST_API void SetMemoryRequestForTextureType(HdTextureType textureType, size_t memoryRequest)
Sets how much memory a single texture can consume in bytes by texture type.
void SetNonUniformImmutableAggregationStrategy(std::unique_ptr< HdStAggregationStrategy > &&strategy)
Set the aggregation strategy for non uniform immutable parameters (vertex, varying,...
HDST_API HdInstance< HioGlslfxSharedPtr > RegisterGLSLFXFile(HdInstance< HioGlslfxSharedPtr >::ID id)
Register a GLSLFX file.
void _GarbageCollect() override
Hooks for derived registries to perform additional GC when GarbageCollect() is invoked.
HDST_API HdInstance< HdStGLSLProgramSharedPtr > RegisterGLSLProgram(HdInstance< HdStGLSLProgramSharedPtr >::ID id)
Register a GLSL program into the program registry.
HDST_API HdInstance< HdBufferArrayRangeSharedPtr > RegisterExtComputationDataRange(HdInstance< HdBufferArrayRangeSharedPtr >::ID id)
ExtComputation data array range instancing Returns the HdInstance pointing to shared HdBufferArrayRan...
HDST_API HgiBlitCmds * GetGlobalBlitCmds()
Returns the global hgi blit command queue for recording blitting work.
HDST_API void AddComputation(HdBufferArrayRangeSharedPtr const &range, HdStComputationSharedPtr const &computation, HdStComputeQueue const queue)
Append a gpu computation into queue.
void SetSingleStorageAggregationStrategy(std::unique_ptr< HdStAggregationStrategy > &&strategy)
Set the aggregation strategy for single buffers (for nested instancer).
void SetUniformAggregationStrategy(std::unique_ptr< HdStAggregationStrategy > &&strategy)
Set the aggregation strategy for uniform (shader globals) Takes ownership of the passed in strategy o...
HDST_API HdBufferArrayRangeSharedPtr UpdateNonUniformBufferArrayRange(TfToken const &role, HdBufferArrayRangeSharedPtr const &curRange, HdBufferSpecVector const &updatedOrAddedSpecs, HdBufferSpecVector const &removedSpecs, HdBufferArrayUsageHint usageHint)
HDST_API friend std::ostream & operator<<(std::ostream &out, const HdStResourceRegistry &self)
Debug dump.
HDST_API HdInstance< HgiComputePipelineSharedPtr > RegisterComputePipeline(HdInstance< HgiComputePipelineSharedPtr >::ID id)
Register a Hgi compute pipeline into the registry.
HDST_API void GarbageCollectDispatchBuffers()
Remove any entries associated with expired dispatch buffers.
HDST_API HdStStagingBuffer * GetStagingBuffer()
Returns the staging buffer used when committing data to the GPU.
HDST_API HdInstance< HgiResourceBindingsSharedPtr > RegisterResourceBindings(HdInstance< HgiResourceBindingsSharedPtr >::ID id)
Register a Hgi resource bindings into the registry.
HDST_API void InvalidateShaderRegistry() override
Invalidate any shaders registered with this registry.
HDST_API VtDictionary GetResourceAllocation() const override
Returns a report of resource allocation by role in bytes and a summary total allocation of GPU memory...
HDST_API HdStBufferResourceSharedPtr RegisterBufferResource(TfToken const &role, HdTupleType tupleType, HgiBufferUsage bufferUsage)
Register a misc buffer resource.
HDST_API Hgi * GetHgi()
Returns Hgi used to create/destroy GPU resources.
void _Commit() override
A hook for derived registries to perform additional resource commits.
HDST_API void SubmitBlitWork(HgiSubmitWaitType wait=HgiSubmitWaitTypeNoWait)
Submits blit work queued in global blit cmds for GPU execution.
HDST_API HdStTextureHandleSharedPtr AllocateTextureHandle(const HdStTextureIdentifier &textureId, HdTextureType textureType, const HdSamplerParameters &samplerParams, size_t memoryRequest, HdStShaderCodePtr const &shaderCode)
Allocate texture handle (encapsulates texture and sampler object, memory request and callback to shad...
HDST_API HdInstance< HdBufferArrayRangeSharedPtr > RegisterPrimvarRange(HdInstance< HdBufferArrayRangeSharedPtr >::ID id)
Primvar array range instancing Returns the HdInstance pointing to shared HdBufferArrayRange,...
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 HgiComputeCmds * GetGlobalComputeCmds(HgiComputeDispatch dispatchMethod=HgiComputeDispatchSerial)
Returns the global hgi compute cmd queue for recording compute work.
HDST_API HdInstance< HdSt_GeometricShaderSharedPtr > RegisterGeometricShader(HdInstance< HdSt_GeometricShaderSharedPtr >::ID id)
Register a geometric shader.
HDST_API void GarbageCollectBufferResources()
Remove any entries associated with expired misc buffers.
HDST_API void ReloadResource(TfToken const &resourceType, std::string const &path) override
Generic method to inform RenderDelegate a resource needs to be reloaded.
HDST_API HdBufferArrayRangeSharedPtr AllocateNonUniformBufferArrayRange(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
HDST_API HdInstance< HgiGraphicsPipelineSharedPtr > RegisterGraphicsPipeline(HdInstance< HgiGraphicsPipelineSharedPtr >::ID id)
Register a Hgi graphics pipeline into the registry.
HDST_API HdStTextureObjectSharedPtr AllocateTextureObject(const HdStTextureIdentifier &textureId, HdTextureType textureType)
Allocate texture object.
void SetNonUniformAggregationStrategy(std::unique_ptr< HdStAggregationStrategy > &&strategy)
Set the aggregation strategy for non uniform parameters (vertex, varying, facevarying) Takes ownershi...
HDST_API HdStDispatchBufferSharedPtr RegisterDispatchBuffer(TfToken const &role, int count, int commandNumUints)
Register a buffer allocated with count * commandNumUints * sizeof(uint32_t) to be used as an indirect...
Provides a staging buffer for CPU writes of triple-buffered resources.
Class to identify a texture file or a texture within the texture file (e.g., a frame in a movie).
A graphics API independent abstraction of resource copy commands.
A graphics API independent abstraction of compute commands.
Hydra Graphics Interface.
Token for efficient comparison, assignment, and hashing of known strings.
A map with string keys and VtValue values.
HdTupleType represents zero, one, or more values of the same HdType.
Functor to use for hash maps from tokens to other things.