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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_ST_TEXTURE_OBJECT_H
8 #define PXR_IMAGING_HD_ST_TEXTURE_OBJECT_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
12 
14 #include "pxr/imaging/hdSt/enums.h"
15 #include "pxr/imaging/hd/types.h"
16 
17 #include "pxr/imaging/hgi/handle.h"
18 #include "pxr/imaging/hio/image.h"
19 
20 #include "pxr/base/gf/bbox3d.h"
22 
23 #include <memory>
24 
26 
27 class Hgi;
30 struct HgiTextureDesc;
31 class HdStTextureCpuData;
33 
34 using HdStTextureObjectSharedPtr = std::shared_ptr<class HdStTextureObject>;
35 
36 /// \class HdStTextureObject
37 ///
38 /// Base class for a texture object. The actual GPU resources will be
39 /// allocated during the commit phase.
40 ///
42  public std::enable_shared_from_this<HdStTextureObject>
43 {
44 public:
45  /// Get texture identifier
46  ///
47  const HdStTextureIdentifier &
48  GetTextureIdentifier() const { return _textureId; }
49 
50  /// Get the target memory for the texture.
51  ///
52  size_t GetTargetMemory() const { return _targetMemory; }
53 
54  /// Set the target memory (in bytes).
55  ///
56  /// When uploading the texture to the GPU, it will be downsampled
57  /// to meet this target memory.
58  ///
59  HDST_API
60  void SetTargetMemory(size_t);
61 
62  /// Returns the actual amount of memory committed to the GPU.
63  HDST_API
64  virtual size_t GetCommittedSize() const = 0;
65 
66  /// Is texture valid? Only correct after commit phase.
67  ///
68  /// E.g., no file at given file path. Consulted by clients to
69  /// determine whether to use the fallback value.
70  ///
71  HDST_API
72  virtual bool IsValid() const = 0;
73 
74  /// Get texture type
75  ///
76  HDST_API
77  virtual HdStTextureType GetTextureType() const = 0;
78 
79  HDST_API
80  virtual ~HdStTextureObject();
81 
82 protected:
84  const HdStTextureIdentifier &textureId,
85  HdSt_TextureObjectRegistry * textureObjectRegistry);
86 
87  HDST_API
89 
90  HDST_API
91  Hgi* _GetHgi() const;
92 
93  HDST_API
94  std::string _GetDebugName(const HdStTextureIdentifier &textureId) const;
95 
96  HDST_API
97  bool
98  _GetPremultiplyAlpha(const HdStSubtextureIdentifier * const subId) const;
99 
100  HDST_API
102  _GetSourceColorSpace(const HdStSubtextureIdentifier * const subId) const;
103 
104  /// Load texture to CPU (thread-safe)
105  ///
106  HDST_API
107  virtual void _Load() = 0;
108 
109  /// Commit texture to GPU (not thread-safe)
110  ///
111  HDST_API
112  virtual void _Commit() = 0;
113 
114  /// Add signed number to total texture memory amount maintained by
115  /// registry.
116  ///
117  HDST_API
118  void _AdjustTotalTextureMemory(int64_t memDiff);
119 
120  /// Compute memory of texture and add to total texture memory
121  /// amount maintained by registry.
122  HDST_API
124 
125  /// Compute memory of texture and subtract to total texture memory
126  /// amount maintained by registry.
127  HDST_API
129 
130 private:
132 
133  HdSt_TextureObjectRegistry * const _textureObjectRegistry;
134  const HdStTextureIdentifier _textureId;
135  size_t _targetMemory;
136 };
137 
138 /// \class HdStUvTextureObject
139 ///
140 /// A base class for uv textures (including cubemaps).
141 ///
143 {
144 public:
145  HDST_API
146  ~HdStUvTextureObject() override;
147 
148  /// Get the handle to the actual GPU resource.
149  ///
150  /// Only valid after commit phase.
151  ///
152  HgiTextureHandle const &GetTexture() const {
153  return _gpuTexture;
154  }
155 
156  /// Opinion about wrapS and wrapT parameters from the texture file.
157  ///
158  /// Only valid after commit phase. Can be HdWrapNoOpinion.
159  const std::pair<HdWrap, HdWrap> &GetWrapParameters() const {
160  return _wrapParameters;
161  }
162 
163  HDST_API
164  HdStTextureType GetTextureType() const override final;
165 
166  HDST_API
167  size_t GetCommittedSize() const override;
168 
169 protected:
170  HDST_API
172  const HdStTextureIdentifier &textureId,
173  HdSt_TextureObjectRegistry * textureObjectRegistry);
174 
175  HDST_API
176  void _SetWrapParameters(
177  const std::pair<HdWrap, HdWrap> &wrapParameters);
178 
179  HDST_API
180  void _SetCpuData(std::unique_ptr<HdStTextureCpuData> &&);
181  HDST_API
182  HdStTextureCpuData * _GetCpuData() const;
183 
184  HDST_API
185  void _CreateTexture(const HgiTextureDesc &desc);
186  HDST_API
187  void _GenerateMipmaps();
188  HDST_API
189  void _DestroyTexture();
190 
191 private:
192  std::pair<HdWrap, HdWrap> _wrapParameters;
193  std::unique_ptr<HdStTextureCpuData> _cpuData;
194  HgiTextureHandle _gpuTexture;
195  HdStTextureType _textureType;
196 };
197 
198 /// \class HdAssetStUvTextureObject
199 ///
200 /// A uv texture loading the asset identified by the texture identifier.
201 ///
203 {
204 public:
205  HDST_API
207  const HdStTextureIdentifier &textureId,
208  HdSt_TextureObjectRegistry *textureObjectRegistry);
209 
210  HDST_API
211  ~HdStAssetUvTextureObject() override;
212 
213  HDST_API
214  bool IsValid() const override;
215 
216 protected:
217  HDST_API
218  void _Load() override;
219 
220  HDST_API
221  void _Commit() override;
222 
223 private:
224  bool _valid;
225 };
226 
227 /// \class HdStFieldTextureObject
228 ///
229 /// A uvw texture with a bounding box describing how to transform it.
230 ///
232 {
233 public:
234  HDST_API
236  const HdStTextureIdentifier &textureId,
237  HdSt_TextureObjectRegistry *textureObjectRegistry);
238 
239  HDST_API
240  ~HdStFieldTextureObject() override;
241 
242  /// Get the handle to the actual GPU resource.
243  ///
244  /// Only valid after commit phase.
245  ///
246  HgiTextureHandle const &GetTexture() const {
247  return _gpuTexture;
248  }
249 
250  /// The box the texture fills out.
251  ///
252  /// Only valid after the commit phase.
253  ///
254  const GfBBox3d &GetBoundingBox() const { return _bbox; }
255 
256  /// The sampling transform.
257  ///
258  /// Only valid after the commit phase.
259  ///
261  return _samplingTransform;
262  }
263 
264  HDST_API
265  bool IsValid() const override;
266 
267  HDST_API
268  HdStTextureType GetTextureType() const override;
269 
270  HDST_API
271  size_t GetCommittedSize() const override;
272 
273 protected:
274  HDST_API
275  void _Load() override;
276 
277  HDST_API
278  void _Commit() override;
279 
280 private:
281  std::unique_ptr<HdStTextureCpuData> _cpuData;
282  GfBBox3d _bbox;
283  GfMatrix4d _samplingTransform;
284  HgiTextureHandle _gpuTexture;
285  bool _valid;
286 };
287 
288 template<HdStTextureType textureType>
290 
291 /// \class HdStTypedTextureObject
292 ///
293 /// A template alias such that, e.g., HdStUvTextureObject can be
294 /// accessed as HdStTypedTextureObject<HdStTextureType::Uv>.
295 ///
296 template<HdStTextureType textureType>
299 
300 template<>
303 };
304 
305 template<>
308 };
309 
310 template<>
313 };
314 
316 
317 #endif
HDST_API bool _GetPremultiplyAlpha(const HdStSubtextureIdentifier *const subId) const
HdWrap
Definition: types.h:37
*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:632
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
virtual HDST_API void _Load()=0
HDST_API void _SetCpuData(std::unique_ptr< HdStTextureCpuData > &&)
int HgiHandle< class HgiTexture > HgiTextureHandle
HDST_API HdStTextureType GetTextureType() const overridefinal
HDST_API void _DestroyTexture()
HDST_API void _AdjustTotalTextureMemory(int64_t memDiff)
const GfBBox3d & GetBoundingBox() const
HDST_API void _AddToTotalTextureMemory(const HgiTextureHandle &texture)
virtual HDST_API size_t GetCommittedSize() const =0
Returns the actual amount of memory committed to the GPU.
HDST_API Hgi * _GetHgi() const
HdStTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HDST_API HdStUvTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
virtual HDST_API ~HdStTextureObject()
HgiTextureHandle const & GetTexture() const
HDST_API void _SetWrapParameters(const std::pair< HdWrap, HdWrap > &wrapParameters)
const HdStTextureIdentifier & GetTextureIdentifier() const
Definition: textureObject.h:48
HDST_API HdStTextureCpuData * _GetCpuData() const
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
HDST_API void SetTargetMemory(size_t)
Definition: hgi.h:94
HDST_API size_t GetCommittedSize() const override
Returns the actual amount of memory committed to the GPU.
HDST_API HdStResourceRegistry * _GetResourceRegistry() const
HDST_API void _SubtractFromTotalTextureMemory(const HgiTextureHandle &texture)
virtual HDST_API bool IsValid() const =0
HDST_API HioImage::SourceColorSpace _GetSourceColorSpace(const HdStSubtextureIdentifier *const subId) const
HDST_API void _GenerateMipmaps()
const GfMatrix4d & GetSamplingTransform() const
#define HDST_API
Definition: api.h:23
HgiTextureHandle const & GetTexture() const
virtual HDST_API void _Commit()=0
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HdStTextureType
Definition: enums.h:32
SourceColorSpace
Definition: image.h:53
HDST_API std::string _GetDebugName(const HdStTextureIdentifier &textureId) const
GLuint texture
Definition: glcorearb.h:415
virtual HDST_API HdStTextureType GetTextureType() const =0
HDST_API ~HdStUvTextureObject() override
size_t GetTargetMemory() const
Definition: textureObject.h:52
const std::pair< HdWrap, HdWrap > & GetWrapParameters() const
HDST_API void _CreateTexture(const HgiTextureDesc &desc)