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