Loading...
Searching...
No Matches
textureUtils.h
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
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
22PXR_NAMESPACE_OPEN_SCOPE
23
24class Hgi;
26
32{
33public:
39 void(*)(const void * src,
40 size_t numTexels,
41 void * dst);
42
47 HDST_API
48 static
49 HgiFormat GetHgiFormat(
50 HioFormat hioFormat,
51 bool premultiplyAlpha);
52
57 HDST_API
58 static
60 HioFormat hioFormat,
61 bool premultiplyAlpha);
62
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
78 ComputeDimensionsFromTargetMemory(
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
95 ReadAndConvertImage(
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>
108 class AlignedBuffer
109 {
110 public:
111 AlignedBuffer()
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
134 HDST_API
135 static
136 AlignedBuffer<uint8_t>
138 HgiTextureHandle const & texture,
139 size_t * bufferSize);
140
142 template <typename T>
143 static
144 AlignedBuffer<T>
145 HgiTextureReadback(Hgi * const hgi,
146 HgiTextureHandle const & texture,
147 size_t * bufferSize);
148};
149
150template <typename T>
151HdStTextureUtils::AlignedBuffer<T>
153 HgiTextureHandle const & texture,
154 size_t * bufferSize)
155{
156 HdStTextureUtils::AlignedBuffer<uint8_t> buffer =
157 HdStTextureUtils::HgiTextureReadback(hgi, texture, bufferSize);
158
159 T * typedData = reinterpret_cast<T *>(buffer.release());
160 return HdStTextureUtils::AlignedBuffer<T>(typedData);
161}
162
163PXR_NAMESPACE_CLOSE_SCOPE
164
165#endif
Provide architecture-specific memory-alignment information.
Basic type for a vector of 3 int components.
Definition: vec3i.h:44
Helpers for loading textures.
Definition: textureUtils.h:32
void(*)(const void *src, size_t numTexels, void *dst) ConversionFunction
Converts given number of texels.
Definition: textureUtils.h:41
static HDST_API ConversionFunction GetHioToHgiConversion(HioFormat hioFormat, bool premultiplyAlpha)
Returns the conversion function to return a HioFormat to the corresponding HgiFormat given by GetHgiF...
static HDST_API HgiFormat GetHgiFormat(HioFormat hioFormat, bool premultiplyAlpha)
Get the Hgi format suitable for a given Hio format.
static HDST_API std::vector< HioImageSharedPtr > GetAllMipImages(const std::string &filePath, HioImage::SourceColorSpace sourceColorSpace)
Get all mip levels from a file.
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.
Hydra Graphics Interface.
Definition: hgi.h:95
SourceColorSpace
Specifies the source color space in which the texture is encoded, with "Auto" indicating the texture ...
Definition: image.h:54
ARCH_API void ArchAlignedFree(void *ptr)
Free memory allocated by ArchAlignedAlloc.
HgiMipInfo describes size and other info for a mip level.
Definition: types.h:120