All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ptexMipmapTextureLoader.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_PTEX_MIPMAP_TEXTURE_LOADER_H
25 #define GLF_PTEX_MIPMAP_TEXTURE_LOADER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/glf/api.h"
29 #include <Ptexture.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <vector>
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 
37 class GlfPtexMipmapTextureLoader {
38 public:
39  GLF_API
40  GlfPtexMipmapTextureLoader(PtexTexture *ptex,
41  int maxNumPages,
42  int maxLevels = -1,
43  size_t targetMemory = 0,
44  bool seamlessMipmap = true);
45 
46  GLF_API
47  ~GlfPtexMipmapTextureLoader();
48 
49  const unsigned char * GetLayoutBuffer() const {
50  return _layoutBuffer;
51  }
52  const unsigned char * GetTexelBuffer() const {
53  return _texelBuffer;
54  }
55  int GetNumFaces() const {
56  return (int)_blocks.size();
57  }
58  int GetNumPages() const {
59  return (int)_pages.size();
60  }
61  int GetPageWidth() const {
62  return _pageWidth;
63  }
64  int GetPageHeight() const {
65  return _pageHeight;
66  }
67  size_t GetMemoryUsage() const {
68  return _memoryUsage;
69  }
70 
71 /*
72  block : atomic texture unit
73  XXX: face of 128x128 or more (64kb~) texels should be considered separately
74  using ARB_sparse_texture...?
75 
76  . : per-face texels for each mipmap level
77  x : guttering pixel
78 
79  xxxxxxxxxxxxxx
80  x........xx..x 2x2
81  x........xx..x
82  x........xxxxx
83  x..8x8...xxxxxxx
84  x........xx....x
85  x........xx....x 4x4
86  x........xx....x
87  x........xx....x
88  xxxxxxxxxxxxxxxx
89 
90  For each face (w*h), texels with guttering and mipmap is stored into
91  (w+2+w/2+2)*(h+2) area as above.
92 
93  */
94 
95 /*
96  Ptex loader
97 
98  Texels buffer : the packed texels
99 
100  */
101 
102 private:
103  struct Block {
104  int index; // ptex index
105  int nMipmaps;
106  uint16_t u, v; // top-left texel offset
107  uint16_t width, height; // texel dimension (includes mipmap)
108  uint16_t adjSizeDiffs; // maximum tile size difference around each vertices
109  int8_t ulog2, vlog2; // texel dimension log2 (original tile)
110 
111  void Generate(GlfPtexMipmapTextureLoader *loader, PtexTexture *ptex,
112  unsigned char *destination,
113  int bpp, int width, int maxLevels);
114 
115  void SetSize(unsigned char ulog2_, unsigned char vlog2_, bool mipmap);
116 
117  int GetNumTexels() const {
118  return width*height;
119  }
120 
121  void guttering(GlfPtexMipmapTextureLoader *loader, PtexTexture *ptex,
122  int level, int width, int height,
123  unsigned char *pptr, int bpp, int stride);
124 
125  static bool sort(const Block *a, const Block *b) {
126  return (a->height > b->height) ||
127  ((a->height == b->height) && (a->width > b->width));
128  }
129 
130  static bool sortByArea(const Block *a, const Block *b) {
131  return (a->GetNumTexels() > b->GetNumTexels());
132  }
133  };
134 
135  struct Page;
136  class CornerIterator;
137 
138  void generateBuffers();
139  void optimizePacking(int maxNumPages, size_t targetMemory);
140  int getLevelDiff(int face, int edge);
141  bool getCornerPixel(float *resultPixel, int numchannels,
142  int face, int edge, int8_t res);
143  void sampleNeighbor(unsigned char *border,
144  int face, int edge, int length, int bpp);
145  int resampleBorder(int face, int edgeId, unsigned char *result,
146  int dstLength, int bpp,
147  float srcStart = 0.0f, float srcEnd = 1.0f);
148 
149  std::vector<Block> _blocks;
150  std::vector<Page *> _pages;
151 
152  PtexTexture *_ptex;
153  int _maxLevels;
154  int _bpp;
155  int _pageWidth, _pageHeight;
156 
157  unsigned char *_texelBuffer;
158  unsigned char *_layoutBuffer;
159 
160  size_t _memoryUsage;
161 };
162 
163 
164 PXR_NAMESPACE_CLOSE_SCOPE
165 
166 #endif // GLF_PTEX_MIPMAP_TEXTURE_LOADER_H