HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
textureHandleRegistry.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_ST_TEXTURE_HANDLE_REGISTRY_H
8 #define PXR_IMAGING_HD_ST_TEXTURE_HANDLE_REGISTRY_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
12 
14 
15 #include "pxr/imaging/hd/enums.h"
16 
17 #include <tbb/concurrent_vector.h>
18 
19 #include <set>
20 #include <memory>
21 
23 
28 
30  std::weak_ptr<class HdStTextureHandle>;
32  std::shared_ptr<class HdStTextureHandle>;
34  std::weak_ptr<class HdStTextureObject>;
36  std::shared_ptr<class HdStTextureObject>;
38  std::shared_ptr<class HdStSamplerObject>;
39 using HdStShaderCodePtr =
40  std::weak_ptr<class HdStShaderCode>;
42  std::shared_ptr<class HdStShaderCode>;
43 
44 /// \class HdSt_TextureHandleRegistry
45 ///
46 /// Keeps track of texture handles and allocates the textures and
47 /// samplers using the HdSt_TextureObjectRegistry, respectively,
48 /// HdSt_SamplerObjectRegistry. Its responsibilities including
49 /// tracking what texture handles are associated to a texture,
50 /// computing the target memory of a texture from the memory requests
51 /// in the texture handles, triggering sampler and texture garbage
52 /// collection, and determining what HdStShaderCode instances are
53 /// affecting by (re-)committing a texture.
54 ///
56 {
57 public:
58  HDST_API
60 
61  HDST_API
63 
64  /// Allocate texture handle (thread-safe).
65  ///
66  /// See HdStResourceRegistry::AllocateTextureHandle for details.
67  ///
68  HDST_API
70  const HdStTextureIdentifier &textureId,
71  HdStTextureType textureType,
72  const HdSamplerParameters &samplerParams,
73  /// memoryRequest in bytes.
74  size_t memoryRequest,
75  HdStShaderCodePtr const &shaderCode);
76 
77  /// Mark texture dirty (thread-safe).
78  ///
79  /// If set, the target memory of the texture will be recomputed
80  /// during commit and the data structure tracking the associated
81  /// handles will be updated potentially triggering texture garbage
82  /// collection.
83  ///
84  HDST_API
86 
87  /// Mark shader dirty (thread-safe).
88  ///
89  /// If set, the shader is scheduled to be updated (i.e., have its
90  /// AddResourcesFromTextures called) on the next commit.
91  ///
92  HDST_API
93  void MarkDirty(HdStShaderCodePtr const &shader);
94 
95  /// Mark that sampler garbage collection needs to happen during
96  /// next commit (thead-safe).
97  ///
98  HDST_API
100 
101  /// Get texture object registry.
102  ///
104  return _textureObjectRegistry.get();
105  }
106 
107  /// Get sampler object registry.
108  ///
110  return _samplerObjectRegistry.get();
111  }
112 
113  /// Commit textures. Return shader code instances that
114  /// depend on the (re-)loaded textures so that they can add
115  /// buffer sources based on the texture meta-data.
116  ///
117  /// Also garbage collect textures and samplers if necessary.
118  ///
119  HDST_API
120  std::set<HdStShaderCodeSharedPtr> Commit();
121 
122  void GarbageCollect();
123 
124  /// Sets how much memory a single texture can consume in bytes by
125  /// texture type.
126  ///
127  /// Only has an effect if non-zero and only applies to textures if
128  /// no texture handle referencing the texture has a memory
129  /// request.
130  ///
131  HDST_API
132  void SetMemoryRequestForTextureType(HdStTextureType textureType, size_t memoryRequest);
133 
134  HDST_API
135  size_t GetNumberOfTextureHandles() const;
136 
137 private:
138  void _ComputeMemoryRequest(HdStTextureObjectSharedPtr const &);
139  void _ComputeMemoryRequests(const std::set<HdStTextureObjectSharedPtr> &);
140  void _ComputeAllMemoryRequests();
141 
142  bool _GarbageCollectHandlesAndComputeTargetMemory();
143  std::set<HdStShaderCodeSharedPtr> _Commit();
144 
145  class _TextureToHandlesMap;
146 
147  // Maps texture type to memory a single texture of that type can consume
148  // (in bytes).
149  // Will be taken into account when computing the maximum of all the
150  // memory requests of the texture handles.
151  std::map<HdStTextureType, size_t> _textureTypeToMemoryRequest;
152  // Has _textureTypeToMemoryRequest changed since the last commit.
153  bool _textureTypeToMemoryRequestChanged;
154 
155  // Handles that are new or for which the underlying texture has
156  // changed: samplers might need to be (re-)allocated and the
157  // corresponding shader code might need to update the shader bar.
158  tbb::concurrent_vector<HdStTextureHandlePtr> _dirtyHandles;
159 
160  // Textures whose set of associated handles and target memory
161  // might have changed.
162  tbb::concurrent_vector<HdStTextureObjectPtr> _dirtyTextures;
163 
164  // Shaders that dropped a texture handle also need to be notified
165  // (for example because they re-allocated the shader bar after dropping
166  // the texture).
167  tbb::concurrent_vector<HdStShaderCodePtr> _dirtyShaders;
168 
169  std::unique_ptr<class HdSt_SamplerObjectRegistry> _samplerObjectRegistry;
170  std::unique_ptr<class HdSt_TextureObjectRegistry> _textureObjectRegistry;
171  std::unique_ptr<_TextureToHandlesMap> _textureToHandlesMap;
172 
173 };
174 
176 
177 #endif
HDST_API std::set< HdStShaderCodeSharedPtr > Commit()
std::shared_ptr< class HdStShaderCode > HdStShaderCodeSharedPtr
std::weak_ptr< class HdStShaderCode > HdStShaderCodePtr
std::weak_ptr< class HdStTextureHandle > HdStTextureHandlePtr
HDST_API HdStTextureHandleSharedPtr AllocateTextureHandle(const HdStTextureIdentifier &textureId, HdStTextureType textureType, const HdSamplerParameters &samplerParams, size_t memoryRequest, HdStShaderCodePtr const &shaderCode)
HDST_API ~HdSt_TextureHandleRegistry()
HDST_API void MarkDirty(HdStTextureObjectPtr const &texture)
HDST_API void MarkSamplerGarbageCollectionNeeded()
HDST_API size_t GetNumberOfTextureHandles() const
HdSt_SamplerObjectRegistry * GetSamplerObjectRegistry() const
HDST_API void SetMemoryRequestForTextureType(HdStTextureType textureType, size_t memoryRequest)
HDST_API HdSt_TextureHandleRegistry(HdStResourceRegistry *registry)
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
GLuint shader
Definition: glcorearb.h:785
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::shared_ptr< class HdStTextureHandle > HdStTextureHandleSharedPtr
HdSt_TextureObjectRegistry * GetTextureObjectRegistry() const
#define HDST_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HdStTextureType
Definition: enums.h:30
std::shared_ptr< class HdStSamplerObject > HdStSamplerObjectSharedPtr
Definition: samplerObject.h:30
GLuint texture
Definition: glcorearb.h:415
std::weak_ptr< class HdStTextureObject > HdStTextureObjectPtr