All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
textureHandleRegistry.h
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 
30 #include "pxr/imaging/hdSt/textureObject.h"
31 
32 #include "pxr/imaging/hd/enums.h"
33 
34 #include <tbb/concurrent_vector.h>
35 
36 #include <set>
37 #include <memory>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
44 class HdSt_SamplerObjectRegistry;
45 
46 using HdStTextureHandlePtr =
47  std::weak_ptr<class HdStTextureHandle>;
48 using HdStTextureHandleSharedPtr =
49  std::shared_ptr<class HdStTextureHandle>;
50 using HdStTextureObjectPtr =
51  std::weak_ptr<class HdStTextureObject>;
52 using HdStTextureObjectSharedPtr =
53  std::shared_ptr<class HdStTextureObject>;
54 using HdStSamplerObjectSharedPtr =
55  std::shared_ptr<class HdStSamplerObject>;
56 using HdStShaderCodePtr =
57  std::weak_ptr<class HdStShaderCode>;
58 using HdStShaderCodeSharedPtr =
59  std::shared_ptr<class HdStShaderCode>;
60 
72 class HdSt_TextureHandleRegistry final
73 {
74 public:
75  HDST_API
76  explicit HdSt_TextureHandleRegistry(HdStResourceRegistry * registry);
77 
78  HDST_API
79  ~HdSt_TextureHandleRegistry();
80 
85  HDST_API
86  HdStTextureHandleSharedPtr AllocateTextureHandle(
87  const HdStTextureIdentifier &textureId,
88  HdTextureType textureType,
89  const HdSamplerParameters &samplerParams,
91  size_t memoryRequest,
92  bool createBindlessHandle,
93  HdStShaderCodePtr const &shaderCode);
94 
102  HDST_API
103  void MarkDirty(HdStTextureObjectPtr const &texture);
104 
110  HDST_API
111  void MarkDirty(HdStShaderCodePtr const &shader);
112 
116  HDST_API
117  void MarkSamplerGarbageCollectionNeeded();
118 
121  HdSt_TextureObjectRegistry * GetTextureObjectRegistry() const {
122  return _textureObjectRegistry.get();
123  }
124 
127  HdSt_SamplerObjectRegistry * GetSamplerObjectRegistry() const {
128  return _samplerObjectRegistry.get();
129  }
130 
137  HDST_API
138  std::set<HdStShaderCodeSharedPtr> Commit();
139 
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 
192 PXR_NAMESPACE_CLOSE_SCOPE
193 
194 #endif
Class to identify a texture file or a texture within the texture file (e.g., a frame in a movie)...
A central registry of all GPU resources.
Collection of standard parameters such as wrap modes to sample a texture.
Definition: types.h:109