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  /// Is texture valid? Only correct after commit phase.
63  ///
64  /// E.g., no file at given file path. Consulted by clients to
65  /// determine whether to use the fallback value.
66  ///
67  HDST_API
68  virtual bool IsValid() const = 0;
69 
70  /// Get texture type
71  ///
72  HDST_API
73  virtual HdStTextureType GetTextureType() const = 0;
74 
75  HDST_API
76  virtual ~HdStTextureObject();
77 
78 protected:
80  const HdStTextureIdentifier &textureId,
81  HdSt_TextureObjectRegistry * textureObjectRegistry);
82 
83  HDST_API
85 
86  HDST_API
87  Hgi* _GetHgi() const;
88 
89  HDST_API
90  std::string _GetDebugName(const HdStTextureIdentifier &textureId) const;
91 
92  HDST_API
93  bool
94  _GetPremultiplyAlpha(const HdStSubtextureIdentifier * const subId) const;
95 
96  HDST_API
98  _GetSourceColorSpace(const HdStSubtextureIdentifier * const subId) const;
99 
100  /// Load texture to CPU (thread-safe)
101  ///
102  HDST_API
103  virtual void _Load() = 0;
104 
105  /// Commit texture to GPU (not thread-safe)
106  ///
107  HDST_API
108  virtual void _Commit() = 0;
109 
110  /// Add signed number to total texture memory amount maintained by
111  /// registry.
112  ///
113  HDST_API
114  void _AdjustTotalTextureMemory(int64_t memDiff);
115 
116  /// Compute memory of texture and add to total texture memory
117  /// amount maintained by registry.
118  HDST_API
120 
121  /// Compute memory of texture and subtract to total texture memory
122  /// amount maintained by registry.
123  HDST_API
125 
126 private:
128 
129  HdSt_TextureObjectRegistry * const _textureObjectRegistry;
130  const HdStTextureIdentifier _textureId;
131  size_t _targetMemory;
132 };
133 
134 /// \class HdStUvTextureObject
135 ///
136 /// A base class for uv textures.
137 ///
139 {
140 public:
141  HDST_API
142  ~HdStUvTextureObject() override;
143 
144  /// Get the handle to the actual GPU resource.
145  ///
146  /// Only valid after commit phase.
147  ///
148  HgiTextureHandle const &GetTexture() const {
149  return _gpuTexture;
150  }
151 
152  /// Opinion about wrapS and wrapT parameters from the texture file.
153  ///
154  /// Only valid after commit phase. Can be HdWrapNoOpinion.
155  const std::pair<HdWrap, HdWrap> &GetWrapParameters() const {
156  return _wrapParameters;
157  }
158 
159  HDST_API
160  HdStTextureType GetTextureType() const override final;
161 
162 protected:
163  HDST_API
165  const HdStTextureIdentifier &textureId,
166  HdSt_TextureObjectRegistry * textureObjectRegistry);
167 
168  HDST_API
169  void _SetWrapParameters(
170  const std::pair<HdWrap, HdWrap> &wrapParameters);
171 
172  HDST_API
173  void _SetCpuData(std::unique_ptr<HdStTextureCpuData> &&);
174  HDST_API
175  HdStTextureCpuData * _GetCpuData() const;
176 
177  HDST_API
178  void _CreateTexture(const HgiTextureDesc &desc);
179  HDST_API
180  void _GenerateMipmaps();
181  HDST_API
182  void _DestroyTexture();
183 
184 private:
185  std::pair<HdWrap, HdWrap> _wrapParameters;
186  std::unique_ptr<HdStTextureCpuData> _cpuData;
187  HgiTextureHandle _gpuTexture;
188 };
189 
190 /// \class HdAssetStUvTextureObject
191 ///
192 /// A uv texture loading the asset identified by the texture identifier.
193 ///
195 {
196 public:
197  HDST_API
199  const HdStTextureIdentifier &textureId,
200  HdSt_TextureObjectRegistry *textureObjectRegistry);
201 
202  HDST_API
203  ~HdStAssetUvTextureObject() override;
204 
205  HDST_API
206  bool IsValid() const override;
207 
208 protected:
209  HDST_API
210  void _Load() override;
211 
212  HDST_API
213  void _Commit() override;
214 
215 private:
216  bool _valid;
217 };
218 
219 /// \class HdStFieldTextureObject
220 ///
221 /// A uvw texture with a bounding box describing how to transform it.
222 ///
224 {
225 public:
226  HDST_API
228  const HdStTextureIdentifier &textureId,
229  HdSt_TextureObjectRegistry *textureObjectRegistry);
230 
231  HDST_API
232  ~HdStFieldTextureObject() override;
233 
234  /// Get the handle to the actual GPU resource.
235  ///
236  /// Only valid after commit phase.
237  ///
238  HgiTextureHandle const &GetTexture() const {
239  return _gpuTexture;
240  }
241 
242  /// The box the texture fills out.
243  ///
244  /// Only valid after the commit phase.
245  ///
246  const GfBBox3d &GetBoundingBox() const { return _bbox; }
247 
248  /// The sampling transform.
249  ///
250  /// Only valid after the commit phase.
251  ///
253  return _samplingTransform;
254  }
255 
256  HDST_API
257  bool IsValid() const override;
258 
259  HDST_API
260  HdStTextureType GetTextureType() const override;
261 
262 protected:
263  HDST_API
264  void _Load() override;
265 
266  HDST_API
267  void _Commit() override;
268 
269 private:
270  std::unique_ptr<HdStTextureCpuData> _cpuData;
271  GfBBox3d _bbox;
272  GfMatrix4d _samplingTransform;
273  HgiTextureHandle _gpuTexture;
274  bool _valid;
275 };
276 
277 template<HdStTextureType textureType>
279 
280 /// \class HdStTypedTextureObject
281 ///
282 /// A template alias such that, e.g., HdStUvTextureObject can be
283 /// accessed as HdStTypedTextureObject<HdStTextureType::Uv>.
284 ///
285 template<HdStTextureType textureType>
288 
289 template<>
292 };
293 
294 template<>
297 };
298 
300 
301 #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
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)
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:93
HDST_API HdStResourceRegistry * _GetResourceRegistry() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
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:30
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)