Loading...
Searching...
No Matches
ptexMipmapTextureLoader.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef HDST_PTEX_MIPMAP_TEXTURE_LOADER_H
8#define HDST_PTEX_MIPMAP_TEXTURE_LOADER_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hdSt/api.h"
12#include <Ptexture.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <vector>
16
17PXR_NAMESPACE_OPEN_SCOPE
18
19
20class HdStPtexMipmapTextureLoader final
21{
22public:
23 HDST_API
24 HdStPtexMipmapTextureLoader(PtexTexture *ptex,
25 int maxNumPages,
26 int maxLevels = -1,
27 size_t targetMemory = 0,
28 bool seamlessMipmap = true);
29
30 HDST_API
31 ~HdStPtexMipmapTextureLoader();
32
33 const unsigned char * GetLayoutBuffer() const {
34 return _layoutBuffer;
35 }
36 const unsigned char * GetTexelBuffer() const {
37 return _texelBuffer;
38 }
39 int GetNumFaces() const {
40 return (int)_blocks.size();
41 }
42 int GetNumPages() const {
43 return (int)_pages.size();
44 }
45 int GetPageWidth() const {
46 return _pageWidth;
47 }
48 int GetPageHeight() const {
49 return _pageHeight;
50 }
51 size_t GetMemoryUsage() const {
52 return _memoryUsage;
53 }
54
55/*
56 block : atomic texture unit
57 XXX: face of 128x128 or more (64kb~) texels should be considered separately
58 using ARB_sparse_texture...?
59
60 . : per-face texels for each mipmap level
61 x : guttering pixel
62
63 xxxxxxxxxxxxxx
64 x........xx..x 2x2
65 x........xx..x
66 x........xxxxx
67 x..8x8...xxxxxxx
68 x........xx....x
69 x........xx....x 4x4
70 x........xx....x
71 x........xx....x
72 xxxxxxxxxxxxxxxx
73
74 For each face (w*h), texels with guttering and mipmap is stored into
75 (w+2+w/2+2)*(h+2) area as above.
76
77 */
78
79/*
80 Ptex loader
81
82 Texels buffer : the packed texels
83
84 */
85
86private:
87 struct Block {
88 int index; // ptex index
89 int nMipmaps;
90 uint16_t u, v; // top-left texel offset
91 uint16_t width, height; // texel dimension (includes mipmap)
92 uint16_t adjSizeDiffs; // maximum tile size difference around each vertices
93 int8_t ulog2, vlog2; // texel dimension log2 (original tile)
94
95 void Generate(HdStPtexMipmapTextureLoader *loader, PtexTexture *ptex,
96 unsigned char *destination,
97 int bpp, int width, int maxLevels);
98
99 void SetSize(unsigned char ulog2_, unsigned char vlog2_, bool mipmap);
100
101 int GetNumTexels() const {
102 return width*height;
103 }
104
105 void guttering(HdStPtexMipmapTextureLoader *loader, PtexTexture *ptex,
106 int level, int width, int height,
107 unsigned char *pptr, int bpp, int stride);
108
109 static bool sort(const Block *a, const Block *b) {
110 return (a->height > b->height) ||
111 ((a->height == b->height) && (a->width > b->width));
112 }
113
114 static bool sortByArea(const Block *a, const Block *b) {
115 return (a->GetNumTexels() > b->GetNumTexels());
116 }
117 };
118
119 struct Page;
120 class CornerIterator;
121
122 void generateBuffers();
123 void optimizePacking(int maxNumPages, size_t targetMemory);
124 int getLevelDiff(int face, int edge);
125 bool getCornerPixel(float *resultPixel, int numchannels,
126 int face, int edge, int8_t res);
127 void sampleNeighbor(unsigned char *border,
128 int face, int edge, int length, int bpp);
129 int resampleBorder(int face, int edgeId, unsigned char *result,
130 int dstLength, int bpp,
131 float srcStart = 0.0f, float srcEnd = 1.0f);
132
133 std::vector<Block> _blocks;
134 std::vector<Page *> _pages;
135
136 PtexTexture *_ptex;
137 int _maxLevels;
138 int _bpp;
139 int _pageWidth, _pageHeight;
140
141 unsigned char *_texelBuffer;
142 unsigned char *_layoutBuffer;
143
144 size_t _memoryUsage;
145};
146
147
148PXR_NAMESPACE_CLOSE_SCOPE
149
150#endif // HDST_PTEX_MIPMAP_TEXTURE_LOADER_H