HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
textureObject.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_OBJECT_H
25 #define PXR_IMAGING_HD_ST_TEXTURE_OBJECT_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 
31 #include "pxr/imaging/hd/enums.h"
32 #include "pxr/imaging/hd/types.h"
33 
34 #include "pxr/imaging/hgi/handle.h"
35 #include "pxr/imaging/hio/image.h"
36 
37 #include "pxr/base/gf/bbox3d.h"
39 
40 #include <memory>
41 
43 
44 class Hgi;
47 struct HgiTextureDesc;
48 class HdStTextureCpuData;
50 
51 using HdStTextureObjectSharedPtr = std::shared_ptr<class HdStTextureObject>;
52 
53 /// \class HdStTextureObject
54 ///
55 /// Base class for a texture object. The actual GPU resources will be
56 /// allocated during the commit phase.
57 ///
59  public std::enable_shared_from_this<HdStTextureObject>
60 {
61 public:
62  /// Get texture identifier
63  ///
64  const HdStTextureIdentifier &
65  GetTextureIdentifier() const { return _textureId; }
66 
67  /// Get the target memory for the texture.
68  ///
69  size_t GetTargetMemory() const { return _targetMemory; }
70 
71  /// Set the target memory (in bytes).
72  ///
73  /// When uploading the texture to the GPU, it will be downsampled
74  /// to meet this target memory.
75  ///
76  HDST_API
77  void SetTargetMemory(size_t);
78 
79  /// Is texture valid? Only correct after commit phase.
80  ///
81  /// E.g., no file at given file path. Consulted by clients to
82  /// determine whether to use the fallback value.
83  ///
84  HDST_API
85  virtual bool IsValid() const = 0;
86 
87  /// Get texture type
88  ///
89  HDST_API
90  virtual HdTextureType GetTextureType() const = 0;
91 
92  HDST_API
93  virtual ~HdStTextureObject();
94 
95 protected:
97  const HdStTextureIdentifier &textureId,
98  HdSt_TextureObjectRegistry * textureObjectRegistry);
99 
100  HDST_API
102 
103  HDST_API
104  Hgi* _GetHgi() const;
105 
106  HDST_API
107  std::string _GetDebugName(const HdStTextureIdentifier &textureId) const;
108 
109  HDST_API
110  bool
111  _GetPremultiplyAlpha(const HdStSubtextureIdentifier * const subId) const;
112 
113  HDST_API
115  _GetSourceColorSpace(const HdStSubtextureIdentifier * const subId) const;
116 
117  /// Load texture to CPU (thread-safe)
118  ///
119  HDST_API
120  virtual void _Load() = 0;
121 
122  /// Commit texture to GPU (not thread-safe)
123  ///
124  HDST_API
125  virtual void _Commit() = 0;
126 
127  /// Add signed number to total texture memory amount maintained by
128  /// registry.
129  ///
130  HDST_API
131  void _AdjustTotalTextureMemory(int64_t memDiff);
132 
133  /// Compute memory of texture and add to total texture memory
134  /// amount maintained by registry.
135  HDST_API
137 
138  /// Compute memory of texture and subtract to total texture memory
139  /// amount maintained by registry.
140  HDST_API
142 
143 private:
145 
146  HdSt_TextureObjectRegistry * const _textureObjectRegistry;
147  const HdStTextureIdentifier _textureId;
148  size_t _targetMemory;
149 };
150 
151 /// \class HdStUvTextureObject
152 ///
153 /// A base class for uv textures.
154 ///
156 {
157 public:
158  ~HdStUvTextureObject() override;
159 
160  /// Get the handle to the actual GPU resource.
161  ///
162  /// Only valid after commit phase.
163  ///
164  HgiTextureHandle const &GetTexture() const {
165  return _gpuTexture;
166  }
167 
168  /// Opinion about wrapS and wrapT parameters from the texture file.
169  ///
170  /// Only valid after commit phase. Can be HdWrapNoOpinion.
171  const std::pair<HdWrap, HdWrap> &GetWrapParameters() const {
172  return _wrapParameters;
173  }
174 
175  HDST_API
176  HdTextureType GetTextureType() const override final;
177 
178 protected:
180  const HdStTextureIdentifier &textureId,
181  HdSt_TextureObjectRegistry * textureObjectRegistry);
182 
183  void _SetWrapParameters(
184  const std::pair<HdWrap, HdWrap> &wrapParameters);
185 
186  void _SetCpuData(std::unique_ptr<HdStTextureCpuData> &&);
187  HdStTextureCpuData * _GetCpuData() const;
188 
189  void _CreateTexture(const HgiTextureDesc &desc);
190  void _GenerateMipmaps();
191  void _DestroyTexture();
192 
193 private:
194  std::pair<HdWrap, HdWrap> _wrapParameters;
195  std::unique_ptr<HdStTextureCpuData> _cpuData;
196  HgiTextureHandle _gpuTexture;
197 };
198 
199 /// \class HdAssetStUvTextureObject
200 ///
201 /// A uv texture loading the asset identified by the texture identifier.
202 ///
204 {
205 public:
206  HDST_API
208  const HdStTextureIdentifier &textureId,
209  HdSt_TextureObjectRegistry *textureObjectRegistry);
210 
211  HDST_API
212  ~HdStAssetUvTextureObject() override;
213 
214  HDST_API
215  bool IsValid() const override;
216 
217 protected:
218  HDST_API
219  void _Load() override;
220 
221  HDST_API
222  void _Commit() override;
223 
224 private:
225  bool _valid;
226 };
227 
228 /// \class HdStFieldTextureObject
229 ///
230 /// A uvw texture with a bounding box describing how to transform it.
231 ///
233 {
234 public:
235  HDST_API
237  const HdStTextureIdentifier &textureId,
238  HdSt_TextureObjectRegistry *textureObjectRegistry);
239 
240  HDST_API
241  ~HdStFieldTextureObject() override;
242 
243  /// Get the handle to the actual GPU resource.
244  ///
245  /// Only valid after commit phase.
246  ///
247  HgiTextureHandle const &GetTexture() const {
248  return _gpuTexture;
249  }
250 
251  /// The box the texture fills out.
252  ///
253  /// Only valid after the commit phase.
254  ///
255  const GfBBox3d &GetBoundingBox() const { return _bbox; }
256 
257  /// The sampling transform.
258  ///
259  /// Only valid after the commit phase.
260  ///
262  return _samplingTransform;
263  }
264 
265  HDST_API
266  bool IsValid() const override;
267 
268  HDST_API
269  HdTextureType GetTextureType() const override;
270 
271 protected:
272  HDST_API
273  void _Load() override;
274 
275  HDST_API
276  void _Commit() override;
277 
278 private:
279  std::unique_ptr<HdStTextureCpuData> _cpuData;
280  GfBBox3d _bbox;
281  GfMatrix4d _samplingTransform;
282  HgiTextureHandle _gpuTexture;
283  bool _valid;
284 };
285 
286 template<HdTextureType textureType>
288 
289 /// \class HdStTypedTextureObject
290 ///
291 /// A template alias such that, e.g., HdStUvTextureObject can be
292 /// accessed as HdStTypedTextureObject<HdTextureType::Uv>.
293 ///
294 template<HdTextureType textureType>
297 
298 template<>
301 };
302 
303 template<>
306 };
307 
309 
310 #endif
HDST_API bool _GetPremultiplyAlpha(const HdStSubtextureIdentifier *const subId) const
HdWrap
Definition: types.h:54
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
virtual HDST_API HdTextureType GetTextureType() const =0
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
virtual HDST_API void _Load()=0
void _CreateTexture(const HgiTextureDesc &desc)
int HgiHandle< class HgiTexture > HgiTextureHandle
HDST_API void _AdjustTotalTextureMemory(int64_t memDiff)
const GfBBox3d & GetBoundingBox() const
HDST_API void _AddToTotalTextureMemory(const HgiTextureHandle &texture)
HdStUvTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HDST_API Hgi * _GetHgi() const
HdStTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
~HdStUvTextureObject() override
HdTextureType
Definition: enums.h:221
virtual HDST_API ~HdStTextureObject()
HgiTextureHandle const & GetTexture() const
const HdStTextureIdentifier & GetTextureIdentifier() const
Definition: textureObject.h:65
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
HDST_API void SetTargetMemory(size_t)
void _SetCpuData(std::unique_ptr< HdStTextureCpuData > &&)
Definition: hgi.h:110
void _SetWrapParameters(const std::pair< HdWrap, HdWrap > &wrapParameters)
HDST_API HdStResourceRegistry * _GetResourceRegistry() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
HDST_API void _SubtractFromTotalTextureMemory(const HgiTextureHandle &texture)
HdStTextureCpuData * _GetCpuData() const
virtual HDST_API bool IsValid() const =0
HDST_API HdTextureType GetTextureType() const overridefinal
HDST_API HioImage::SourceColorSpace _GetSourceColorSpace(const HdStSubtextureIdentifier *const subId) const
const GfMatrix4d & GetSamplingTransform() const
#define HDST_API
Definition: api.h:40
HgiTextureHandle const & GetTexture() const
virtual HDST_API void _Commit()=0
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SourceColorSpace
Definition: image.h:68
HDST_API std::string _GetDebugName(const HdStTextureIdentifier &textureId) const
GLuint texture
Definition: glcorearb.h:415
#define const
Definition: zconf.h:214
size_t GetTargetMemory() const
Definition: textureObject.h:69
const std::pair< HdWrap, HdWrap > & GetWrapParameters() const