7#ifndef PXR_IMAGING_HD_ST_RESOURCE_REGISTRY_H
8#define PXR_IMAGING_HD_ST_RESOURCE_REGISTRY_H
13#include "pxr/imaging/hdSt/api.h"
14#include "pxr/imaging/hdSt/bufferArrayRegistry.h"
15#include "pxr/imaging/hdSt/enums.h"
17#include "pxr/imaging/hgi/hgi.h"
19#include "pxr/imaging/hd/bufferArrayRange.h"
20#include "pxr/imaging/hd/bufferSource.h"
21#include "pxr/imaging/hd/bufferSpec.h"
22#include "pxr/imaging/hd/instanceRegistry.h"
23#include "pxr/imaging/hd/resourceRegistry.h"
25#include <tbb/concurrent_vector.h>
31#ifdef PXR_MATERIALX_SUPPORT_ENABLED
32#include <MaterialXCore/Library.h>
33MATERIALX_NAMESPACE_BEGIN
34 using ShaderPtr = std::shared_ptr<class Shader>;
35MATERIALX_NAMESPACE_END
38PXR_NAMESPACE_OPEN_SCOPE
40using HdStComputationSharedPtr = std::shared_ptr<class HdStComputation>;
41using HdStDispatchBufferSharedPtr = std::shared_ptr<class HdStDispatchBuffer>;
42using HdStGLSLProgramSharedPtr = std::shared_ptr<class HdStGLSLProgram>;
43using HioGlslfxSharedPtr = std::shared_ptr<class HioGlslfx>;
45using HdSt_BasisCurvesTopologySharedPtr =
46 std::shared_ptr<class HdSt_BasisCurvesTopology>;
48using HdStShaderCodePtr =
49 std::weak_ptr<class HdStShaderCode>;
50using HdSt_GeometricShaderSharedPtr =
51 std::shared_ptr<class HdSt_GeometricShader>;
53using HdStTextureHandleSharedPtr =
54 std::shared_ptr<class HdStTextureHandle>;
55using HdStTextureObjectSharedPtr =
56 std::shared_ptr<class HdStTextureObject>;
57using HdStBufferResourceSharedPtr =
58 std::shared_ptr<class HdStBufferResource>;
59using HdStResourceRegistrySharedPtr =
60 std::shared_ptr<class HdStResourceRegistry>;
61using HdSt_VertexAdjacencyBuilderSharedPtr =
62 std::shared_ptr<class HdSt_VertexAdjacencyBuilder>;
63using HdSt_MeshTopologySharedPtr =
64 std::shared_ptr<class HdSt_MeshTopology>;
65using HgiResourceBindingsSharedPtr =
66 std::shared_ptr<HgiResourceBindingsHandle>;
67using HgiGraphicsPipelineSharedPtr =
68 std::shared_ptr<HgiGraphicsPipelineHandle>;
69using HgiComputePipelineSharedPtr =
70 std::shared_ptr<HgiComputePipelineHandle>;
89enum HdStComputeQueue {
90 HdStComputeQueueZero=0,
93 HdStComputeQueueThree,
94 HdStComputeQueueCount};
96using HdStComputationComputeQueuePairVector =
97 std::vector<std::pair<HdStComputationSharedPtr, HdStComputeQueue>>;
107 HF_MALLOC_TAG_NEW(
"new HdStResourceRegistry");
120 std::string
const& path)
override;
147 HdStTextureType textureType,
155 size_t memoryRequest,
160 HdStShaderCodePtr
const &shaderCode);
174 HdStTextureType textureType);
185 HdStTextureType textureType,
186 size_t memoryRequest);
198 HdBufferSpecVector
const &bufferSpecs,
199 HdBufferArrayUsageHint usageHint);
202 HdBufferArrayRangeSharedPtr AllocateNonUniformImmutableBufferArrayRange(
204 HdBufferSpecVector
const &bufferSpecs,
205 HdBufferArrayUsageHint usageHint);
208 HdBufferArrayRangeSharedPtr AllocateUniformBufferArrayRange(
210 HdBufferSpecVector
const &bufferSpecs,
211 HdBufferArrayUsageHint usageHint);
214 HdBufferArrayRangeSharedPtr AllocateShaderStorageBufferArrayRange(
216 HdBufferSpecVector
const &bufferSpecs,
217 HdBufferArrayUsageHint usageHint);
220 HdBufferArrayRangeSharedPtr AllocateSingleBufferArrayRange(
222 HdBufferSpecVector
const &bufferSpecs,
223 HdBufferArrayUsageHint usageHint);
244 HdBufferArrayRangeSharedPtr
const& curRange,
245 HdBufferSpecVector
const &updatedOrAddedSpecs,
246 HdBufferSpecVector
const& removedSpecs,
247 HdBufferArrayUsageHint usageHint);
250 HdBufferArrayRangeSharedPtr UpdateNonUniformImmutableBufferArrayRange(
252 HdBufferArrayRangeSharedPtr
const& curRange,
253 HdBufferSpecVector
const &updatedOrAddedSpecs,
254 HdBufferSpecVector
const& removedSpecs,
255 HdBufferArrayUsageHint usageHint);
258 HdBufferArrayRangeSharedPtr UpdateUniformBufferArrayRange(
260 HdBufferArrayRangeSharedPtr
const& curRange,
261 HdBufferSpecVector
const &updatedOrAddedSpecs,
262 HdBufferSpecVector
const& removedSpecs,
263 HdBufferArrayUsageHint usageHint);
266 HdBufferArrayRangeSharedPtr UpdateShaderStorageBufferArrayRange(
268 HdBufferArrayRangeSharedPtr
const& curRange,
269 HdBufferSpecVector
const &updatedOrAddedSpecs,
270 HdBufferSpecVector
const& removedSpecs,
271 HdBufferArrayUsageHint usageHint);
280 HdBufferSourceSharedPtrVector &&sources);
284 void AddSource(HdBufferArrayRangeSharedPtr
const &range,
285 HdBufferSourceSharedPtr
const &source);
299 HdStComputationSharedPtr
const &computation,
300 HdStComputeQueue
const queue);
310 TfToken const &role,
int count,
int commandNumUints);
321 HgiBufferUsage bufferUsage);
353 RegisterBasisCurvesTopology(
354 HdInstance<HdSt_BasisCurvesTopologySharedPtr>::ID
id);
358 RegisterVertexAdjacencyBuilder(
359 HdInstance<HdSt_VertexAdjacencyBuilderSharedPtr>::ID
id);
368 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id,
TfToken const &name);
372 RegisterBasisCurvesIndexRange(
373 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id,
TfToken const &name);
382 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id);
391 HdInstance<HdBufferArrayRangeSharedPtr>::ID
id);
408#ifdef PXR_MATERIALX_SUPPORT_ENABLED
412 RegisterMaterialXShader(HdInstance<MaterialX::ShaderPtr>::ID
id);
443 const std::string& identifier,
444 const std::function<std::unique_ptr<HdResourceRegistry>()>& factory);
461 HgiComputeDispatch dispatchMethod = HgiComputeDispatchSerial);
492 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
493 _nonUniformAggregationStrategy = std::move(strategy);
500 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
501 _nonUniformImmutableAggregationStrategy = std::move(strategy);
507 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
508 _uniformUboAggregationStrategy = std::move(strategy);
514 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
515 _uniformSsboAggregationStrategy = std::move(strategy);
521 std::unique_ptr<HdStAggregationStrategy> &&strategy) {
522 _singleAggregationStrategy = std::move(strategy);
536 void _CommitTextures();
538 HdBufferArrayRangeSharedPtr _AllocateBufferArrayRange(
542 HdBufferSpecVector
const &bufferSpecs,
543 HdBufferArrayUsageHint usageHint);
546 HdBufferArrayRangeSharedPtr _UpdateBufferArrayRange(
550 HdBufferArrayRangeSharedPtr
const& curRange,
551 HdBufferSpecVector
const &updatedOrAddedSpecs,
552 HdBufferSpecVector
const& removedSpecs,
553 HdBufferArrayUsageHint usageHint);
557 void _TallyResourceAllocation(
VtDictionary *result)
const;
561 struct _PendingSource {
562 _PendingSource(HdBufferArrayRangeSharedPtr
const &range)
568 _PendingSource(HdBufferArrayRangeSharedPtr
const &range,
569 HdBufferSourceSharedPtr
const &source)
575 _PendingSource(HdBufferArrayRangeSharedPtr
const &range,
576 HdBufferSourceSharedPtrVector && sources)
578 , sources(
std::move(sources))
582 HdBufferArrayRangeSharedPtr range;
583 HdBufferSourceSharedPtrVector sources;
588 typedef tbb::concurrent_vector<_PendingSource> _PendingSourceList;
589 _PendingSourceList _pendingSources;
590 std::atomic_size_t _numBufferSourcesToResolve;
592 struct _PendingComputation{
593 _PendingComputation(HdBufferArrayRangeSharedPtr
const &range,
594 HdStComputationSharedPtr
const &computation)
595 : range(range), computation(computation) { }
596 HdBufferArrayRangeSharedPtr range;
597 HdStComputationSharedPtr computation;
603 typedef tbb::concurrent_vector<_PendingComputation> _PendingComputationList;
604 _PendingComputationList _pendingComputations[HdStComputeQueueCount];
614 std::unique_ptr<HdStAggregationStrategy> _nonUniformAggregationStrategy;
615 std::unique_ptr<HdStAggregationStrategy>
616 _nonUniformImmutableAggregationStrategy;
617 std::unique_ptr<HdStAggregationStrategy> _uniformUboAggregationStrategy;
618 std::unique_ptr<HdStAggregationStrategy> _uniformSsboAggregationStrategy;
619 std::unique_ptr<HdStAggregationStrategy> _singleAggregationStrategy;
621 typedef std::vector<HdStDispatchBufferSharedPtr>
622 _DispatchBufferRegistry;
623 _DispatchBufferRegistry _dispatchBufferRegistry;
625 typedef std::vector<HdStBufferResourceSharedPtr>
626 _BufferResourceRegistry;
627 _BufferResourceRegistry _bufferResourceRegistry;
631 _meshTopologyRegistry;
635 _basisCurvesTopologyRegistry;
639 _vertexAdjacencyBuilderRegistry;
643 _TopologyIndexRangeInstanceRegistry;
644 typedef tbb::concurrent_unordered_map<
TfToken,
645 _TopologyIndexRangeInstanceRegistry,
647 _TopologyIndexRangeInstanceRegMap;
649 _TopologyIndexRangeInstanceRegMap _meshTopologyIndexRangeRegistry;
650 _TopologyIndexRangeInstanceRegMap _basisCurvesTopologyIndexRangeRegistry;
654 _primvarRangeRegistry;
658 _extComputationDataRangeRegistry;
662 _geometricShaderRegistry;
666 _glslProgramRegistry;
672#ifdef PXR_MATERIALX_SUPPORT_ENABLED
678 std::unique_ptr<class HdSt_TextureHandleRegistry> _textureHandleRegistry;
682 _resourceBindingsRegistry;
686 _graphicsPipelineRegistry;
690 _computePipelineRegistry;
692 using _SubResourceRegistryMap =
693 tbb::concurrent_unordered_map<std::string,
694 std::unique_ptr<HdResourceRegistry>>;
695 _SubResourceRegistryMap _subResourceRegistries;
697 HgiBlitCmdsUniquePtr _blitCmds;
698 HgiComputeCmdsUniquePtr _computeCmds;
700 std::unique_ptr<HdStStagingBuffer> _stagingBuffer;
704PXR_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 HdStTextureObjectSharedPtr AllocateTextureObject(const HdStTextureIdentifier &textureId, HdStTextureType textureType)
Allocate texture object.
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 SetMemoryRequestForTextureType(HdStTextureType textureType, size_t memoryRequest)
Sets how much memory a single texture can consume in bytes by texture type.
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 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 HdStTextureHandleSharedPtr AllocateTextureHandle(const HdStTextureIdentifier &textureId, HdStTextureType 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< 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.
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.