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/base/gf/bbox3d.h"
37 
38 #include <memory>
39 
41 
42 #ifdef PXR_PTEX_SUPPORT_ENABLED
43 TF_DECLARE_WEAK_AND_REF_PTRS(GlfPtexTexture);
44 #endif
46 
47 class Hgi;
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  HDST_API
65  const HdStTextureIdentifier &
66  GetTextureIdentifier() const { return _textureId; }
67 
68  /// Get the target memory for the texture.
69  ///
70  HDST_API
71  size_t GetTargetMemory() const { return _targetMemory; }
72 
73  /// Set the target memory (in bytes).
74  ///
75  /// When uploading the texture to the GPU, it will be downsampled
76  /// to meet this target memory.
77  ///
78  HDST_API
79  void SetTargetMemory(size_t);
80 
81  /// Is texture valid? Only correct after commit phase.
82  ///
83  /// E.g., no file at given file path. Consulted by clients to
84  /// determine whether to use the fallback value.
85  ///
86  HDST_API
87  virtual bool IsValid() const = 0;
88 
89  /// Get texture type
90  ///
91  HDST_API
92  virtual HdTextureType GetTextureType() const = 0;
93 
94  HDST_API
95  virtual ~HdStTextureObject();
96 
97 protected:
99  const HdStTextureIdentifier &textureId,
100  HdSt_TextureObjectRegistry * textureObjectRegistry);
101 
102  Hgi* _GetHgi() 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 private:
116 
117  HdSt_TextureObjectRegistry * const _textureObjectRegistry;
118  const HdStTextureIdentifier _textureId;
119  size_t _targetMemory;
120 };
121 
122 /// \class HdStUvTextureObject
123 ///
124 /// A base class for uv textures.
125 ///
127 {
128 public:
129  /// Get the handle to the actual GPU resource.
130  ///
131  /// Only valid after commit phase.
132  ///
133  HDST_API
134  virtual HgiTextureHandle const &GetTexture() const = 0;
135 
136  /// Opinion about wrapS and wrapT parameters from the texture file.
137  ///
138  /// Only valid after commit phase. Can be HdWrapNoOpinion.
139  HDST_API
140  virtual const std::pair<HdWrap, HdWrap> &GetWrapParameters() const = 0;
141 
142  HDST_API
143  HdTextureType GetTextureType() const override final;
144 
145 protected:
147  const HdStTextureIdentifier &textureId,
148  HdSt_TextureObjectRegistry * textureObjectRegistry);
149 };
150 
151 /// \class HdAssetStUvTextureObject
152 ///
153 /// A uv texture loading the asset identified by the texture identifier.
154 ///
156 {
157 public:
158  HDST_API
160  const HdStTextureIdentifier &textureId,
161  HdSt_TextureObjectRegistry *textureObjectRegistry);
162 
163  HDST_API
164  ~HdStAssetUvTextureObject() override;
165 
166  /// Get the handle to the actual GPU resource.
167  ///
168  /// Only valid after commit phase.
169  ///
170  HDST_API
171  HgiTextureHandle const &GetTexture() const override {
172  return _gpuTexture;
173  }
174 
175  /// Opinion about wrapS and wrapT parameters from the texture file.
176  ///
177  /// Only valid after commit phase. Can be HdWrapNoOpinion.
178  HDST_API
179  const std::pair<HdWrap, HdWrap> &GetWrapParameters() const override {
180  return _wrapParameters;
181  }
182 
183  HDST_API
184  bool IsValid() const override;
185 
186 protected:
187  HDST_API
188  void _Load() override;
189 
190  HDST_API
191  void _Commit() override;
192 
193 private:
194  std::unique_ptr<class HdSt_TextureObjectCpuData> _cpuData;
195  HgiTextureHandle _gpuTexture;
196  std::pair<HdWrap, HdWrap> _wrapParameters;
197 };
198 
199 /// \class HdStFieldTextureObject
200 ///
201 /// A uvw texture with a bounding box describing how to transform it.
202 ///
204 {
205 public:
206  HDST_API
208  const HdStTextureIdentifier &textureId,
209  HdSt_TextureObjectRegistry *textureObjectRegistry);
210 
211  HDST_API
212  ~HdStFieldTextureObject() override;
213 
214  /// Get the handle to the actual GPU resource.
215  ///
216  /// Only valid after commit phase.
217  ///
218  HDST_API
219  HgiTextureHandle const &GetTexture() const {
220  return _gpuTexture;
221  }
222 
223  /// The box the texture fills out.
224  ///
225  /// Only valid after the commit phase.
226  ///
227  HDST_API
228  const GfBBox3d &GetBoundingBox() const { return _bbox; }
229 
230  /// The sampling transform.
231  ///
232  /// Only valid after the commit phase.
233  ///
234  HDST_API
236  return _samplingTransform;
237  }
238 
239  HDST_API
240  bool IsValid() const override;
241 
242  HDST_API
243  HdTextureType GetTextureType() const override;
244 
245 protected:
246  HDST_API
247  void _Load() override;
248 
249  HDST_API
250  void _Commit() override;
251 
252 private:
253  std::unique_ptr<class HdSt_TextureObjectCpuData> _cpuData;
254  GfBBox3d _bbox;
255  GfMatrix4d _samplingTransform;
256  HgiTextureHandle _gpuTexture;
257 };
258 
259 /// \class HdStPtexTextureObject
260 ///
261 /// A ptex texture - it is using Glf to both load the texture
262 /// and allocate the GPU resources (unlike the other texture
263 /// types).
264 ///
266 {
267 public:
268  HDST_API
270  const HdStTextureIdentifier &textureId,
271  HdSt_TextureObjectRegistry *textureObjectRegistry);
272 
273  HDST_API
274  ~HdStPtexTextureObject() override;
275 
276  /// Get the GL texture name for the texels
277  ///
278  /// Only valid after commit phase.
279  ///
280  HDST_API
281  uint32_t GetTexelGLTextureName() const { return _texelGLTextureName; }
282 
283  /// Get the GL texture name for the layout
284  ///
285  /// Only valid after commit phase.
286  ///
287  HDST_API
288  uint32_t GetLayoutGLTextureName() const { return _layoutGLTextureName; }
289 
290  HDST_API
291  bool IsValid() const override;
292 
293  HDST_API
294  HdTextureType GetTextureType() const override;
295 
296 protected:
297  HDST_API
298  void _Load() override;
299 
300  HDST_API
301  void _Commit() override;
302 
303 private:
304 #ifdef PXR_PTEX_SUPPORT_ENABLED
305  GlfPtexTextureRefPtr _gpuTexture;
306 #endif
307 
308  uint32_t _texelGLTextureName;
309  uint32_t _layoutGLTextureName;
310 };
311 
312 /// \class HdStUdimTextureObject
313 ///
314 /// A udim texture - it is using Glf to both load the texture
315 /// and allocate the GPU resources (unlike the other texture
316 /// types).
317 ///
319 {
320 public:
321  HDST_API
323  const HdStTextureIdentifier &textureId,
324  HdSt_TextureObjectRegistry *textureObjectRegistry);
325 
326  HDST_API
327  ~HdStUdimTextureObject() override;
328 
329  /// Get the GL texture name for the texels
330  ///
331  /// Only valid after commit phase.
332  ///
333  HDST_API
334  uint32_t GetTexelGLTextureName() const { return _texelGLTextureName; }
335 
336  /// Get the GL texture name for the layout
337  ///
338  /// Only valid after commit phase.
339  ///
340  HDST_API
341  uint32_t GetLayoutGLTextureName() const { return _layoutGLTextureName; }
342 
343  HDST_API
344  bool IsValid() const override;
345 
346  HDST_API
347  HdTextureType GetTextureType() const override;
348 
349 protected:
350  HDST_API
351  void _Load() override;
352 
353  HDST_API
354  void _Commit() override;
355 
356 private:
357  std::vector<std::tuple<int, TfToken>> _tiles;
358 
359  GlfUdimTextureRefPtr _gpuTexture;
360 
361  uint32_t _texelGLTextureName;
362  uint32_t _layoutGLTextureName;
363 };
364 
365 template<HdTextureType textureType>
367 
368 /// \class HdStTypedTextureObject
369 ///
370 /// A template alias such that, e.g., HdStUvTextureObject can be
371 /// accessed as HdStTypedTextureObject<HdTextureType::Uv>.
372 ///
373 template<HdTextureType textureType>
376 
377 template<>
380 };
381 
382 template<>
385 };
386 
387 template<>
390 };
391 
392 template<>
395 };
396 
398 
399 #endif
HDST_API bool IsValid() const override
HDST_API HdTextureType GetTextureType() const override
HDST_API void _Load() override
HDST_API uint32_t GetLayoutGLTextureName() const
virtual HDST_API HdTextureType GetTextureType() const =0
HDST_API bool IsValid() const override
virtual HDST_API void _Load()=0
HDST_API ~HdStAssetUvTextureObject() override
HDST_API uint32_t GetTexelGLTextureName() const
HDST_API const HdStTextureIdentifier & GetTextureIdentifier() const
Definition: textureObject.h:66
HDST_API void _Load() override
HDST_API bool IsValid() const override
HDST_API void _Commit() override
HDST_API HdStAssetUvTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HDST_API const GfMatrix4d & GetSamplingTransform() const
HDST_API uint32_t GetLayoutGLTextureName() const
HdStUvTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HdStTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HDST_API void _Load() override
virtual HDST_API const std::pair< HdWrap, HdWrap > & GetWrapParameters() const =0
HdTextureType
Definition: enums.h:221
virtual HDST_API ~HdStTextureObject()
HDST_API HdStFieldTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HDST_API ~HdStPtexTextureObject() override
HDST_API HgiTextureHandle const & GetTexture() const
HDST_API const GfBBox3d & GetBoundingBox() const
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
HDST_API void SetTargetMemory(size_t)
HDST_API const std::pair< HdWrap, HdWrap > & GetWrapParameters() const override
HDST_API void _Commit() override
Definition: hgi.h:106
Hgi * _GetHgi() const
HDST_API HdTextureType GetTextureType() const override
HDST_API void _Load() override
virtual HDST_API HgiTextureHandle const & GetTexture() const =0
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
HDST_API ~HdStUdimTextureObject() override
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_AND_REF_PTRS(GlfUdimTexture)
virtual HDST_API bool IsValid() const =0
HDST_API HdTextureType GetTextureType() const overridefinal
#define HDST_API
Definition: api.h:40
HDST_API void _Commit() override
virtual HDST_API void _Commit()=0
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HDST_API uint32_t GetTexelGLTextureName() const
HDST_API HgiTextureHandle const & GetTexture() const override
HDST_API size_t GetTargetMemory() const
Definition: textureObject.h:71
HDST_API HdTextureType GetTextureType() const override
HDST_API HdStUdimTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HDST_API bool IsValid() const override
HDST_API ~HdStFieldTextureObject() override
HDST_API HdStPtexTextureObject(const HdStTextureIdentifier &textureId, HdSt_TextureObjectRegistry *textureObjectRegistry)
HDST_API void _Commit() override