HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
uvTextureData.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_GLF_UV_TEXTURE_DATA_H
25 #define PXR_IMAGING_GLF_UV_TEXTURE_DATA_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/glf/api.h"
29 #include "pxr/imaging/glf/image.h"
31 
32 #include <memory>
33 #include <string>
34 
36 
38 
40 public:
41  struct Params {
42  Params()
43  : targetMemory(0)
44  , cropTop(0)
45  , cropBottom(0)
46  , cropLeft(0)
47  , cropRight(0)
48  { }
49 
50  bool operator==(const Params& rhs) const
51  {
52  return (targetMemory == rhs.targetMemory &&
53  cropTop == rhs.cropTop &&
54  cropBottom == rhs.cropBottom &&
55  cropLeft == rhs.cropLeft &&
56  cropRight == rhs.cropRight);
57  }
58 
59  bool operator!=(const Params& rhs) const
60  {
61  return !(*this == rhs);
62  }
63 
64  size_t targetMemory;
66  };
67 
68  GLF_API
69  static GlfUVTextureDataRefPtr
70  New(std::string const &filePath,
71  size_t targetMemory,
72  unsigned int cropTop,
73  unsigned int cropBottom,
74  unsigned int cropLeft,
75  unsigned int cropRight);
76 
77  GLF_API
78  static GlfUVTextureDataRefPtr
79  New(std::string const &filePath, Params const &params);
80 
81  int NumDimensions() const override;
82 
83  const Params& GetParams() const { return _params; }
84 
85  // GlfBaseTextureData overrides
86  GLF_API
87  int ResizedWidth(int mipLevel = 0) const override;
88 
89  GLF_API
90  int ResizedHeight(int mipLevel = 0) const override;
91 
92  GLF_API
93  int ResizedDepth(int mipLevel = 0) const override;
94 
95  GLenum GLInternalFormat() const override {
96  return _glInternalFormat;
97  };
98 
99  GLenum GLFormat() const override {
100  return _glFormat;
101  };
102 
103  GLenum GLType() const override {
104  return _glType;
105  };
106 
107  size_t TargetMemory() const override {
108  return _targetMemory;
109  };
110 
111  WrapInfo GetWrapInfo() const override {
112  return _wrapInfo;
113  };
114 
115  GLF_API
116  size_t ComputeBytesUsed() const override;
117 
118  GLF_API
119  size_t ComputeBytesUsedByMip(int mipLevel = 0) const override;
120 
121  GLF_API
122  bool HasRawBuffer(int mipLevel = 0) const override;
123 
124  GLF_API
125  unsigned char * GetRawBuffer(int mipLevel = 0) const override;
126 
127  GLF_API
128  bool Read(
129  int degradeLevel,
130  bool generateMipmap,
131  GlfImage::ImageOriginLocation originLocation =
132  GlfImage::OriginUpperLeft) override;
133 
134  GLF_API
135  int GetNumMipLevels() const override;
136 
137 private:
138  // A structure that keeps the mips loaded from disk in the format
139  // that the gpu needs.
140  struct Mip {
141  Mip()
142  : size(0), offset(0), width(0), height(0)
143  { }
144 
145  size_t size;
146  size_t offset;
147  int width;
148  int height;
149  };
150 
151  // A structure keeping a down-sampled image input and floats indicating the
152  // downsample rate (e.g., if the resolution changed from 2048x1024 to
153  // 512x256, scaleX=0.25 and scaleY=0.25).
154  struct _DegradedImageInput {
155  _DegradedImageInput(double scaleX, double scaleY,
156  GlfImageSharedPtr image) : scaleX(scaleX), scaleY(scaleY)
157  {
158  images.push_back(image);
159  }
160 
161  _DegradedImageInput(double scaleX, double scaleY)
162  : scaleX(scaleX), scaleY(scaleY)
163  { }
164 
165  double scaleX;
166  double scaleY;
167  std::vector<GlfImageSharedPtr> images;
168  };
169 
170  // Reads an image using GlfImage. If possible and requested, it will
171  // load a down-sampled version (when mipmapped .tex file) of the image.
172  // If targetMemory is > 0, it will iterate through the down-sampled version
173  // until the estimated required GPU memory is smaller than targetMemory.
174  // Otherwise, it will use the given degradeLevel.
175  // When estimating the required GPU memory, it will take into account that
176  // the GPU might generate MipMaps.
177  _DegradedImageInput _ReadDegradedImageInput(bool generateMipmap,
178  size_t targetMemory,
179  size_t degradeLevel);
180 
181  // Helper to read degraded image chains, given a starting mip and an
182  // ending mip it will fill the image chain.
183  _DegradedImageInput _GetDegradedImageInputChain(double scaleX,
184  double scaleY,
185  int startMip,
186  int lastMip);
187 
188  // Given a GlfImage it will return the number of mip levels that
189  // are actually valid to be loaded to the GPU. For instance, it will
190  // drop textures with non valid OpenGL pyramids.
191  int _GetNumMipLevelsValid(const GlfImageSharedPtr image) const;
192 
193  GlfUVTextureData(std::string const &filePath, Params const &params);
194  virtual ~GlfUVTextureData();
195 
196  const std::string _filePath;
197  const Params _params;
198 
199  size_t _targetMemory;
200 
201  int _nativeWidth, _nativeHeight;
202  int _resizedWidth, _resizedHeight;
203  int _bytesPerPixel;
204 
205  GLenum _glInternalFormat, _glFormat, _glType;
206 
207  WrapInfo _wrapInfo;
208 
209  size_t _size;
210 
211  std::unique_ptr<unsigned char[]> _rawBuffer;
212  std::vector<Mip> _rawBufferMips;
213 };
214 
215 
217 
218 #endif // PXR_IMAGING_GLF_UV_TEXTURE_DATA_H
GLF_API bool HasRawBuffer(int mipLevel=0) const override
GLsizeiptr size
Definition: glew.h:1681
#define GLF_API
Definition: api.h:40
int NumDimensions() const override
Is this a 1-, 2- or 3-dimensional texture.
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1252
ImageOriginLocation
Definition: image.h:58
GLF_API int ResizedDepth(int mipLevel=0) const override
GLenum GLInternalFormat() const override
Definition: uvTextureData.h:95
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1252
GLF_API int ResizedWidth(int mipLevel=0) const override
unsigned int GLenum
Definition: cl.hpp:166
size_t TargetMemory() const override
GLF_API unsigned char * GetRawBuffer(int mipLevel=0) const override
GLenum GLsizei GLenum GLenum const void * image
Definition: glew.h:4943
GLenum GLFormat() const override
Definition: uvTextureData.h:99
WrapInfo GetWrapInfo() const override
static GLF_API GlfUVTextureDataRefPtr New(std::string const &filePath, size_t targetMemory, unsigned int cropTop, unsigned int cropBottom, unsigned int cropLeft, unsigned int cropRight)
bool operator!=(const Params &rhs) const
Definition: uvTextureData.h:59
PXR_NAMESPACE_OPEN_SCOPE typedef std::shared_ptr< class GlfImage > GlfImageSharedPtr
Definition: image.h:44
GLsizei const GLchar *const * string
Definition: glew.h:1844
GLF_API size_t ComputeBytesUsedByMip(int mipLevel=0) const override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
GLF_API size_t ComputeBytesUsed() const override
GLF_API bool Read(int degradeLevel, bool generateMipmap, GlfImage::ImageOriginLocation originLocation=GlfImage::OriginUpperLeft) override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLF_API int ResizedHeight(int mipLevel=0) const override
GLF_API int GetNumMipLevels() const override
const GLfloat * params
Definition: glew.h:1531
const Params & GetParams() const
Definition: uvTextureData.h:83
bool operator==(const Params &rhs) const
Definition: uvTextureData.h:50
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_AND_REF_PTRS(GlfUVTextureData)
GLenum GLType() const override
GLintptr offset
Definition: glew.h:1682