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 
41 class Hgi;
42 
46 
48  std::weak_ptr<class HdStTextureHandle>;
50  std::shared_ptr<class HdStTextureHandle>;
52  std::weak_ptr<class HdStTextureObject>;
54  std::shared_ptr<class HdStTextureObject>;
56  std::shared_ptr<class HdStSamplerObject>;
57 using HdStShaderCodePtr =
58  std::weak_ptr<class HdStShaderCode>;
60  std::shared_ptr<class HdStShaderCode>;
61 
62 /// \class HdSt_TextureHandleRegistry
63 ///
64 /// Keeps track of texture handles and allocates the textures and
65 /// samplers using the HdSt_TextureObjectRegistry, respectively,
66 /// HdSt_SamplerObjectRegistry. Its responsibilities including
67 /// tracking what texture handles are associated to a texture,
68 /// computing the target memory of a texture from the memory requests
69 /// in the texture handles, triggering sampler and texture garbage
70 /// collection, and determining what HdStShaderCode instances are
71 /// affecting by (re-)committing a texture.
72 ///
74 {
75 public:
76  explicit HdSt_TextureHandleRegistry(Hgi * hgi);
78 
79  /// Allocate texture handle (thread-safe).
80  ///
81  /// See HdStResourceRegistry::AllocateTextureHandle for details.
82  ///
83  HDST_API
85  const HdStTextureIdentifier &textureId,
86  HdTextureType textureType,
87  const HdSamplerParameters &samplerParams,
88  /// memoryRequest in bytes.
89  size_t memoryRequest,
90  bool createBindlessHandle,
91  HdStShaderCodePtr const &shaderCode);
92 
93  /// Mark texture dirty (thread-safe).
94  ///
95  /// If set, the target memory of the texture will be recomputed
96  /// during commit and the data structure tracking the associated
97  /// handles will be updated potentially triggering texture garbage
98  /// collection.
99  ///
101 
102  /// Mark shader dirty (thread-safe).
103  ///
104  /// If set, the shader is scheduled to be updated (i.e., have its
105  /// AddResourcesFromTextures called) on the next commit.
106  ///
107  void MarkDirty(HdStShaderCodePtr const &shader);
108 
109  /// Mark that sampler garbage collection needs to happen during
110  /// next commit (thead-safe).
111  ///
113 
114  /// Get texture object registry.
115  ///
117  return _textureObjectRegistry.get();
118  }
119 
120  /// Get sampler object registry.
121  ///
123  return _samplerObjectRegistry.get();
124  }
125 
126  /// Commit textures. Return shader code instances that
127  /// depend on the (re-)loaded textures so that they can add
128  /// buffer sources based on the texture meta-data.
129  ///
130  /// Also garbage collect textures and samplers if necessary.
131  ///
132  std::set<HdStShaderCodeSharedPtr> Commit();
133 
134 private:
135  void _ComputeMemoryRequest(HdStTextureObjectSharedPtr const &);
136  void _ComputeMemoryRequests(const std::set<HdStTextureObjectSharedPtr> &);
137 
138  bool _GarbageCollectHandlesAndComputeTargetMemory();
139  void _GarbageCollectAndComputeTargetMemory();
140  std::set<HdStShaderCodeSharedPtr> _Commit();
141 
142  class _TextureToHandlesMap;
143 
144  bool _samplerGarbageCollectionNeeded;
145 
146  // Handles that are new or for which the underlying texture has
147  // changed: samplers might need to be (re-)allocated and the
148  // corresponding shader code might need to update the shader bar.
149  tbb::concurrent_vector<HdStTextureHandlePtr> _dirtyHandles;
150 
151  // Textures whose set of associated handles and target memory
152  // might have changed.
153  tbb::concurrent_vector<HdStTextureObjectPtr> _dirtyTextures;
154 
155  // Shaders that dropped a texture handle also need to be notified
156  // (for example because they re-allocated the shader bar after dropping
157  // the texture).
158  tbb::concurrent_vector<HdStShaderCodePtr> _dirtyShaders;
159 
160  std::unique_ptr<class HdSt_SamplerObjectRegistry> _samplerObjectRegistry;
161  std::unique_ptr<class HdSt_TextureObjectRegistry> _textureObjectRegistry;
162  std::unique_ptr<_TextureToHandlesMap> _textureToHandlesMap;
163 
164 };
165 
167 
168 #endif
std::weak_ptr< class HdStShaderCode > HdStShaderCodePtr
std::weak_ptr< class HdStTextureHandle > HdStTextureHandlePtr
GLuint shader
Definition: glew.h:1813
HdSt_TextureHandleRegistry(Hgi *hgi)
HdSt_SamplerObjectRegistry * GetSamplerObjectRegistry() const
std::set< HdStShaderCodeSharedPtr > Commit()
HdTextureType
Definition: enums.h:221
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
Definition: hgi.h:106
void MarkDirty(HdStTextureObjectPtr const &texture)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
std::shared_ptr< class HdStTextureHandle > HdStTextureHandleSharedPtr
HdSt_TextureObjectRegistry * GetTextureObjectRegistry() const
#define HDST_API
Definition: api.h:40
GLenum GLuint texture
Definition: glew.h:2361
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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