HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
textureUtils.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_UTILS_H
8 #define PXR_IMAGING_HD_ST_TEXTURE_UTILS_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
12 
13 #include "pxr/imaging/hio/image.h"
14 
15 #include "pxr/imaging/hgi/handle.h"
16 #include "pxr/imaging/hgi/types.h"
17 
18 #include "pxr/base/arch/align.h"
19 
20 #include <memory>
21 
23 
24 class Hgi;
26 
27 /// \class HdStTextureUtils
28 ///
29 /// Helpers for loading textures.
30 ///
32 {
33 public:
34  /// Converts given number of texels.
35  ///
36  /// Conversion can be in-place if the ends of the source and destination
37  /// buffers align.
38  using ConversionFunction =
39  void(*)(const void * src,
40  size_t numTexels,
41  void * dst);
42 
43  /// Get the Hgi format suitable for a given Hio format.
44  ///
45  /// Premultiply alpha indicates whether a conversion function
46  /// multiplying RGB with alpha should be created.
47  HDST_API
48  static
50  HioFormat hioFormat,
51  bool premultiplyAlpha);
52 
53  /// Returns the conversion function to return a HioFormat
54  /// to the corresponding HgiFormat given by GetHgiFormat.
55  ///
56  /// Returns nullptr if no conversion necessary.
57  HDST_API
58  static
60  HioFormat hioFormat,
61  bool premultiplyAlpha);
62 
63  /// Get all mip levels from a file.
64  HDST_API
65  static
66  std::vector<HioImageSharedPtr> GetAllMipImages(
67  const std::string &filePath,
68  HioImage::SourceColorSpace sourceColorSpace);
69 
70  // Compute dimensions so that all tiles fit into the given target memory.
71  // First by traversing the given images and then by computing a mip chain
72  // starting with the lowest resolution image.
73  // Optionally, can also give the index of the image in mips that was used
74  // to compute the dimensions.
75  HDST_API
76  static
77  GfVec3i
79  const std::vector<HioImageSharedPtr> &mips,
80  HgiFormat targetFormat,
81  size_t tileCount,
82  size_t targetMemory,
83  size_t * mipIndex = nullptr);
84 
85  // Read given HioImage and convert it to corresponding Hgi format.
86  // Returns false if reading the HioImage failed.
87  //
88  // bufferStart is assumed to point at the beginning of a mip chain
89  // with mipInfo describing what mip level of the mip chain to be
90  // filled. layer gives the layer number if the mip chain is for an
91  // array texture.
92  HDST_API
93  static
94  bool
96  HioImageSharedPtr const &image,
97  bool flipped,
98  bool premultiplyAlpha,
99  const HgiMipInfo &mipInfo,
100  size_t layer,
101  void * bufferStart);
102 
103  // Because the underlying graphics API may have alignment
104  // restrictions we use this wrapper class to manage the
105  // allocation of the returned buffer data, and expose a
106  // restricted subset of underlyling pointer's access methods.
107  template <typename T>
109  {
110  public:
112  : AlignedBuffer(nullptr)
113  { }
114 
115  T *get() const {
116  return _alignedPtr.get();
117  }
118 
119  private:
120  friend class HdStTextureUtils;
121 
122  explicit AlignedBuffer(T * alignedPtr)
123  : _alignedPtr(alignedPtr, ArchAlignedFree)
124  { }
125 
126  T *release() {
127  return _alignedPtr.release();
128  }
129 
130  std::unique_ptr<T[], decltype(ArchAlignedFree)*> _alignedPtr;
131  };
132 
133  /// Returns an unsigned byte buffer with data read back from \p texture.
134  HDST_API
135  static
136  AlignedBuffer<uint8_t>
137  HgiTextureReadback(Hgi * const hgi,
138  HgiTextureHandle const & texture,
139  size_t * bufferSize);
140 
141  /// Returns a buffer with data of type T read back from \p texture.
142  template <typename T>
143  static
144  AlignedBuffer<T>
145  HgiTextureReadback(Hgi * const hgi,
146  HgiTextureHandle const & texture,
147  size_t * bufferSize);
148 };
149 
150 template <typename T>
153  HgiTextureHandle const & texture,
154  size_t * bufferSize)
155 {
157  HdStTextureUtils::HgiTextureReadback(hgi, texture, bufferSize);
158 
159  T * typedData = reinterpret_cast<T *>(buffer.release());
160  return HdStTextureUtils::AlignedBuffer<T>(typedData);
161 }
162 
164 
165 #endif
static HDST_API HgiFormat GetHgiFormat(HioFormat hioFormat, bool premultiplyAlpha)
void
Definition: png.h:1083
void(*)(const void *src, size_t numTexels, void *dst) ConversionFunction
Definition: textureUtils.h:41
int HgiHandle< class HgiTexture > HgiTextureHandle
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
HgiFormat
Definition: types.h:28
static HDST_API ConversionFunction GetHioToHgiConversion(HioFormat hioFormat, bool premultiplyAlpha)
GLuint buffer
Definition: glcorearb.h:660
ARCH_API void ArchAlignedFree(void *ptr)
Free memory allocated by ArchAlignedAlloc.
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
static HDST_API bool ReadAndConvertImage(HioImageSharedPtr const &image, bool flipped, bool premultiplyAlpha, const HgiMipInfo &mipInfo, size_t layer, void *bufferStart)
static HDST_API GfVec3i ComputeDimensionsFromTargetMemory(const std::vector< HioImageSharedPtr > &mips, HgiFormat targetFormat, size_t tileCount, size_t targetMemory, size_t *mipIndex=nullptr)
Definition: vec3i.h:43
HioFormat
Definition: types.h:25
Definition: hgi.h:93
GLenum GLenum dst
Definition: glcorearb.h:1793
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::shared_ptr< class HioImage > HioImageSharedPtr
Definition: image.h:27
#define HDST_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SourceColorSpace
Definition: image.h:53
GLuint texture
Definition: glcorearb.h:415
static HDST_API AlignedBuffer< uint8_t > HgiTextureReadback(Hgi *const hgi, HgiTextureHandle const &texture, size_t *bufferSize)
Returns an unsigned byte buffer with data read back from texture.
static HDST_API std::vector< HioImageSharedPtr > GetAllMipImages(const std::string &filePath, HioImage::SourceColorSpace sourceColorSpace)
Get all mip levels from a file.
GLenum src
Definition: glcorearb.h:1793