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  HdStShaderCodePtr const &shaderCode);
93 
94  /// Mark texture dirty (thread-safe).
95  ///
96  /// If set, the target memory of the texture will be recomputed
97  /// during commit and the data structure tracking the associated
98  /// handles will be updated potentially triggering texture garbage
99  /// collection.
100  ///
101  HDST_API
103 
104  /// Mark shader dirty (thread-safe).
105  ///
106  /// If set, the shader is scheduled to be updated (i.e., have its
107  /// AddResourcesFromTextures called) on the next commit.
108  ///
109  HDST_API
110  void MarkDirty(HdStShaderCodePtr const &shader);
111 
112  /// Mark that sampler garbage collection needs to happen during
113  /// next commit (thead-safe).
114  ///
115  HDST_API
117 
118  /// Get texture object registry.
119  ///
121  return _textureObjectRegistry.get();
122  }
123 
124  /// Get sampler object registry.
125  ///
127  return _samplerObjectRegistry.get();
128  }
129 
130  /// Commit textures. Return shader code instances that
131  /// depend on the (re-)loaded textures so that they can add
132  /// buffer sources based on the texture meta-data.
133  ///
134  /// Also garbage collect textures and samplers if necessary.
135  ///
136  HDST_API
137  std::set<HdStShaderCodeSharedPtr> Commit();
138 
139  /// Sets how much memory a single texture can consume in bytes by
140  /// texture type.
141  ///
142  /// Only has an effect if non-zero and only applies to textures if
143  /// no texture handle referencing the texture has a memory
144  /// request.
145  ///
146  HDST_API
147  void SetMemoryRequestForTextureType(HdTextureType textureType, size_t memoryRequest);
148 
149  HDST_API
150  size_t GetNumberOfTextureHandles() const;
151 
152 private:
153  void _ComputeMemoryRequest(HdStTextureObjectSharedPtr const &);
154  void _ComputeMemoryRequests(const std::set<HdStTextureObjectSharedPtr> &);
155  void _ComputeAllMemoryRequests();
156 
157  bool _GarbageCollectHandlesAndComputeTargetMemory();
158  void _GarbageCollectAndComputeTargetMemory();
159  std::set<HdStShaderCodeSharedPtr> _Commit();
160 
161  class _TextureToHandlesMap;
162 
163  // Maps texture type to memory a single texture of that type can consume
164  // (in bytes).
165  // Will be taken into account when computing the maximum of all the
166  // memory requests of the texture handles.
167  std::map<HdTextureType, size_t> _textureTypeToMemoryRequest;
168  // Has _textureTypeToMemoryRequest changed since the last commit.
169  bool _textureTypeToMemoryRequestChanged;
170 
171  // Handles that are new or for which the underlying texture has
172  // changed: samplers might need to be (re-)allocated and the
173  // corresponding shader code might need to update the shader bar.
174  tbb::concurrent_vector<HdStTextureHandlePtr> _dirtyHandles;
175 
176  // Textures whose set of associated handles and target memory
177  // might have changed.
178  tbb::concurrent_vector<HdStTextureObjectPtr> _dirtyTextures;
179 
180  // Shaders that dropped a texture handle also need to be notified
181  // (for example because they re-allocated the shader bar after dropping
182  // the texture).
183  tbb::concurrent_vector<HdStShaderCodePtr> _dirtyShaders;
184 
185  std::unique_ptr<class HdSt_SamplerObjectRegistry> _samplerObjectRegistry;
186  std::unique_ptr<class HdSt_TextureObjectRegistry> _textureObjectRegistry;
187  std::unique_ptr<_TextureToHandlesMap> _textureToHandlesMap;
188 
189 };
190 
192 
193 #endif
HDST_API std::set< HdStShaderCodeSharedPtr > Commit()
std::shared_ptr< class HdStShaderCode > HdStShaderCodeSharedPtr
HDST_API HdStTextureHandleSharedPtr AllocateTextureHandle(const HdStTextureIdentifier &textureId, HdTextureType textureType, const HdSamplerParameters &samplerParams, size_t memoryRequest, HdStShaderCodePtr const &shaderCode)
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:785
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
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
std::shared_ptr< class HdStSamplerObject > HdStSamplerObjectSharedPtr
Definition: samplerObject.h:47
GLuint texture
Definition: glcorearb.h:415
std::weak_ptr< class HdStTextureObject > HdStTextureObjectPtr
HDST_API void SetMemoryRequestForTextureType(HdTextureType textureType, size_t memoryRequest)