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 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_TEXTURE_HANDLE_REGISTRY_H
25 #define PXR_IMAGING_HD_ST_TEXTURE_HANDLE_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 
31 
32 #include "pxr/imaging/hd/enums.h"
33 
34 #include <tbb/concurrent_vector.h>
35 
36 #include <set>
37 #include <memory>
38 
40 
45 
47  std::weak_ptr<class HdStTextureHandle>;
49  std::shared_ptr<class HdStTextureHandle>;
51  std::weak_ptr<class HdStTextureObject>;
53  std::shared_ptr<class HdStTextureObject>;
55  std::shared_ptr<class HdStSamplerObject>;
56 using HdStShaderCodePtr =
57  std::weak_ptr<class HdStShaderCode>;
59  std::shared_ptr<class HdStShaderCode>;
60 
61 /// \class HdSt_TextureHandleRegistry
62 ///
63 /// Keeps track of texture handles and allocates the textures and
64 /// samplers using the HdSt_TextureObjectRegistry, respectively,
65 /// HdSt_SamplerObjectRegistry. Its responsibilities including
66 /// tracking what texture handles are associated to a texture,
67 /// computing the target memory of a texture from the memory requests
68 /// in the texture handles, triggering sampler and texture garbage
69 /// collection, and determining what HdStShaderCode instances are
70 /// affecting by (re-)committing a texture.
71 ///
73 {
74 public:
75  HDST_API
77 
78  HDST_API
80 
81  /// Allocate texture handle (thread-safe).
82  ///
83  /// See HdStResourceRegistry::AllocateTextureHandle for details.
84  ///
85  HDST_API
87  const HdStTextureIdentifier &textureId,
88  HdTextureType textureType,
89  const HdSamplerParameters &samplerParams,
90  /// memoryRequest in bytes.
91  size_t memoryRequest,
92  bool createBindlessHandle,
93  HdStShaderCodePtr const &shaderCode);
94 
95  /// Mark texture dirty (thread-safe).
96  ///
97  /// If set, the target memory of the texture will be recomputed
98  /// during commit and the data structure tracking the associated
99  /// handles will be updated potentially triggering texture garbage
100  /// collection.
101  ///
102  HDST_API
104 
105  /// Mark shader dirty (thread-safe).
106  ///
107  /// If set, the shader is scheduled to be updated (i.e., have its
108  /// AddResourcesFromTextures called) on the next commit.
109  ///
110  HDST_API
111  void MarkDirty(HdStShaderCodePtr const &shader);
112 
113  /// Mark that sampler garbage collection needs to happen during
114  /// next commit (thead-safe).
115  ///
116  HDST_API
118 
119  /// Get texture object registry.
120  ///
122  return _textureObjectRegistry.get();
123  }
124 
125  /// Get sampler object registry.
126  ///
128  return _samplerObjectRegistry.get();
129  }
130 
131  /// Commit textures. Return shader code instances that
132  /// depend on the (re-)loaded textures so that they can add
133  /// buffer sources based on the texture meta-data.
134  ///
135  /// Also garbage collect textures and samplers if necessary.
136  ///
137  HDST_API
138  std::set<HdStShaderCodeSharedPtr> Commit();
139 
140  /// Sets how much memory a single texture can consume in bytes by
141  /// texture type.
142  ///
143  /// Only has an effect if non-zero and only applies to textures if
144  /// no texture handle referencing the texture has a memory
145  /// request.
146  ///
147  HDST_API
148  void SetMemoryRequestForTextureType(HdTextureType textureType, size_t memoryRequest);
149 
150  HDST_API
151  size_t GetNumberOfTextureHandles() const;
152 
153 private:
154  void _ComputeMemoryRequest(HdStTextureObjectSharedPtr const &);
155  void _ComputeMemoryRequests(const std::set<HdStTextureObjectSharedPtr> &);
156  void _ComputeAllMemoryRequests();
157 
158  bool _GarbageCollectHandlesAndComputeTargetMemory();
159  void _GarbageCollectAndComputeTargetMemory();
160  std::set<HdStShaderCodeSharedPtr> _Commit();
161 
162  class _TextureToHandlesMap;
163 
164  // Maps texture type to memory a single texture of that type can consume
165  // (in bytes).
166  // Will be taken into account when computing the maximum of all the
167  // memory requests of the texture handles.
168  std::map<HdTextureType, size_t> _textureTypeToMemoryRequest;
169  // Has _textureTypeToMemoryRequest changed since the last commit.
170  bool _textureTypeToMemoryRequestChanged;
171 
172  // Handles that are new or for which the underlying texture has
173  // changed: samplers might need to be (re-)allocated and the
174  // corresponding shader code might need to update the shader bar.
175  tbb::concurrent_vector<HdStTextureHandlePtr> _dirtyHandles;
176 
177  // Textures whose set of associated handles and target memory
178  // might have changed.
179  tbb::concurrent_vector<HdStTextureObjectPtr> _dirtyTextures;
180 
181  // Shaders that dropped a texture handle also need to be notified
182  // (for example because they re-allocated the shader bar after dropping
183  // the texture).
184  tbb::concurrent_vector<HdStShaderCodePtr> _dirtyShaders;
185 
186  std::unique_ptr<class HdSt_SamplerObjectRegistry> _samplerObjectRegistry;
187  std::unique_ptr<class HdSt_TextureObjectRegistry> _textureObjectRegistry;
188  std::unique_ptr<_TextureToHandlesMap> _textureToHandlesMap;
189 
190 };
191 
193 
194 #endif
HDST_API std::set< HdStShaderCodeSharedPtr > Commit()
std::weak_ptr< class HdStShaderCode > HdStShaderCodePtr
std::weak_ptr< class HdStTextureHandle > HdStTextureHandlePtr
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
HdTextureType
Definition: enums.h:221
HDST_API HdSt_TextureHandleRegistry(HdStResourceRegistry *registry)
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
GLuint shader
Definition: glcorearb.h:784
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
std::shared_ptr< class HdStTextureHandle > HdStTextureHandleSharedPtr
HdSt_TextureObjectRegistry * GetTextureObjectRegistry() const
#define HDST_API
Definition: api.h:40
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLuint texture
Definition: glcorearb.h:414
std::shared_ptr< class HdStSamplerObject > HdStSamplerObjectSharedPtr
Definition: samplerObject.h:47
HDST_API HdStTextureHandleSharedPtr AllocateTextureHandle(const HdStTextureIdentifier &textureId, HdTextureType textureType, const HdSamplerParameters &samplerParams, size_t memoryRequest, bool createBindlessHandle, HdStShaderCodePtr const &shaderCode)
std::weak_ptr< class HdStTextureObject > HdStTextureObjectPtr
std::shared_ptr< class HdStShaderCode > HdStShaderCodeSharedPtr
Definition: drawItem.h:34
HDST_API void SetMemoryRequestForTextureType(HdTextureType textureType, size_t memoryRequest)