textureObject.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_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 
30 #include "pxr/imaging/hdSt/textureIdentifier.h"
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 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
44 class Hgi;
45 using HgiTextureHandle = HgiHandle<class HgiTexture>;
46 class HdSt_TextureObjectRegistry;
47 struct HgiTextureDesc;
48 class HdStTextureCpuData;
50 
51 using HdStTextureObjectSharedPtr = std::shared_ptr<class HdStTextureObject>;
52 
59  public std::enable_shared_from_this<HdStTextureObject>
60 {
61 public:
64  const HdStTextureIdentifier &
65  GetTextureIdentifier() const { return _textureId; }
66 
69  size_t GetTargetMemory() const { return _targetMemory; }
70 
76  HDST_API
77  void SetTargetMemory(size_t);
78 
84  HDST_API
85  virtual bool IsValid() const = 0;
86 
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
101  HdStResourceRegistry* _GetResourceRegistry() const;
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 
119  HDST_API
120  virtual void _Load() = 0;
121 
124  HDST_API
125  virtual void _Commit() = 0;
126 
130  HDST_API
131  void _AdjustTotalTextureMemory(int64_t memDiff);
132 
135  HDST_API
136  void _AddToTotalTextureMemory(const HgiTextureHandle &texture);
137 
140  HDST_API
141  void _SubtractFromTotalTextureMemory(const HgiTextureHandle &texture);
142 
143 private:
144  friend class HdSt_TextureObjectRegistry;
145 
146  HdSt_TextureObjectRegistry * const _textureObjectRegistry;
147  const HdStTextureIdentifier _textureId;
148  size_t _targetMemory;
149 };
150 
156 {
157 public:
158  ~HdStUvTextureObject() override;
159 
164  HgiTextureHandle const &GetTexture() const {
165  return _gpuTexture;
166  }
167 
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 
203 class HdStAssetUvTextureObject final : public HdStUvTextureObject
204 {
205 public:
206  HDST_API
207  HdStAssetUvTextureObject(
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 
233 {
234 public:
235  HDST_API
237  const HdStTextureIdentifier &textureId,
238  HdSt_TextureObjectRegistry *textureObjectRegistry);
239 
240  HDST_API
241  ~HdStFieldTextureObject() override;
242 
247  HgiTextureHandle const &GetTexture() const {
248  return _gpuTexture;
249  }
250 
255  const GfBBox3d &GetBoundingBox() const { return _bbox; }
256 
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>
287 struct HdSt_TypedTextureObjectHelper;
288 
294 template<HdTextureType textureType>
296  typename HdSt_TypedTextureObjectHelper<textureType>::type;
297 
298 template<>
299 struct HdSt_TypedTextureObjectHelper<HdTextureType::Uv> {
300  using type = HdStUvTextureObject;
301 };
302 
303 template<>
304 struct HdSt_TypedTextureObjectHelper<HdTextureType::Field> {
305  using type = HdStFieldTextureObject;
306 };
307 
308 PXR_NAMESPACE_CLOSE_SCOPE
309 
310 #endif
virtual HDST_API void _Load()=0
Load texture to CPU (thread-safe)
HDST_API void _AddToTotalTextureMemory(const HgiTextureHandle &texture)
Compute memory of texture and add to total texture memory amount maintained by registry.
virtual HDST_API HdTextureType GetTextureType() const =0
Get texture type.
Standard pointer typedefs.
size_t GetTargetMemory() const
Get the target memory for the texture.
Definition: textureObject.h:69
Base class for additional information to identify a texture in a file that can contain several textur...
HgiTextureHandle const & GetTexture() const
Get the handle to the actual GPU resource.
HDST_API void SetTargetMemory(size_t)
Set the target memory (in bytes).
Base class for a texture object.
Definition: textureObject.h:58
A template alias such that, e.g., HdStUvTextureObject can be accessed as HdStTypedTextureObject<HdTex...
Describes the properties needed to create a GPU texture.
Definition: texture.h:107
Represents CPU data that can be stored in a HdStUvTextureObject, mostly, likely during the load phase...
const GfMatrix4d & GetSamplingTransform() const
The sampling transform.
Class to identify a texture file or a texture within the texture file (e.g., a frame in a movie).
SourceColorSpace
Specifies the source color space in which the texture is encoded, with "Auto" indicating the texture ...
Definition: image.h:68
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
A central registry of all GPU resources.
HDST_API void _SubtractFromTotalTextureMemory(const HgiTextureHandle &texture)
Compute memory of texture and subtract to total texture memory amount maintained by registry.
HDST_API void _AdjustTotalTextureMemory(int64_t memDiff)
Add signed number to total texture memory amount maintained by registry.
const GfBBox3d & GetBoundingBox() const
The box the texture fills out.
virtual HDST_API void _Commit()=0
Commit texture to GPU (not thread-safe)
Hydra Graphics Interface.
Definition: hgi.h:110
A uvw texture with a bounding box describing how to transform it.
virtual HDST_API bool IsValid() const =0
Is texture valid? Only correct after commit phase.
HgiTextureHandle const & GetTexture() const
Get the handle to the actual GPU resource.
const HdStTextureIdentifier & GetTextureIdentifier() const
Get texture identifier.
Definition: textureObject.h:65
HDST_API HdTextureType GetTextureType() const override final
Get texture type.
A base class for uv textures.
Basic type: arbitrarily oriented 3D bounding box.
Definition: bbox3d.h:84
const std::pair< HdWrap, HdWrap > & GetWrapParameters() const
Opinion about wrapS and wrapT parameters from the texture file.