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