Loading...
Searching...
No Matches
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 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
34PXR_NAMESPACE_OPEN_SCOPE
35
36
37class HdStPtexMipmapTextureLoader final
38{
39public:
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
48 ~HdStPtexMipmapTextureLoader();
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
103private:
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
165PXR_NAMESPACE_CLOSE_SCOPE
166
167#endif // HDST_PTEX_MIPMAP_TEXTURE_LOADER_H