1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// generatemip.inl: Defines the GenerateMip function, templated on the format
// type of the image for which mip levels are being generated.
#include "common/mathutil.h"
#include "image_util/imageformats.h"
namespace angle
{
namespace priv
{
template <typename T>
static inline T *GetPixel(uint8_t *data, size_t x, size_t y, size_t z, size_t rowPitch, size_t depthPitch)
{
return reinterpret_cast<T*>(data + (x * sizeof(T)) + (y * rowPitch) + (z * depthPitch));
}
template <typename T>
static inline const T *GetPixel(const uint8_t *data, size_t x, size_t y, size_t z, size_t rowPitch, size_t depthPitch)
{
return reinterpret_cast<const T*>(data + (x * sizeof(T)) + (y * rowPitch) + (z * depthPitch));
}
template <typename T>
static void GenerateMip_Y(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
ASSERT(sourceWidth == 1);
ASSERT(sourceHeight > 1);
ASSERT(sourceDepth == 1);
for (size_t y = 0; y < destHeight; y++)
{
const T *src0 = GetPixel<T>(sourceData, 0, y * 2, 0, sourceRowPitch, sourceDepthPitch);
const T *src1 = GetPixel<T>(sourceData, 0, y * 2 + 1, 0, sourceRowPitch, sourceDepthPitch);
T *dst = GetPixel<T>(destData, 0, y, 0, destRowPitch, destDepthPitch);
T::average(dst, src0, src1);
}
}
template <typename T>
static void GenerateMip_X(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
ASSERT(sourceWidth > 1);
ASSERT(sourceHeight == 1);
ASSERT(sourceDepth == 1);
for (size_t x = 0; x < destWidth; x++)
{
const T *src0 = GetPixel<T>(sourceData, x * 2, 0, 0, sourceRowPitch, sourceDepthPitch);
const T *src1 = GetPixel<T>(sourceData, x * 2 + 1, 0, 0, sourceRowPitch, sourceDepthPitch);
T *dst = GetPixel<T>(destData, x, 0, 0, destRowPitch, destDepthPitch);
T::average(dst, src0, src1);
}
}
template <typename T>
static void GenerateMip_Z(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
ASSERT(sourceWidth == 1);
ASSERT(sourceHeight == 1);
ASSERT(sourceDepth > 1);
for (size_t z = 0; z < destDepth; z++)
{
const T *src0 = GetPixel<T>(sourceData, 0, 0, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src1 = GetPixel<T>(sourceData, 0, 0, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
T *dst = GetPixel<T>(destData, 0, 0, z, destRowPitch, destDepthPitch);
T::average(dst, src0, src1);
}
}
template <typename T>
static void GenerateMip_XY(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
ASSERT(sourceWidth > 1);
ASSERT(sourceHeight > 1);
ASSERT(sourceDepth == 1);
for (size_t y = 0; y < destHeight; y++)
{
for (size_t x = 0; x < destWidth; x++)
{
const T *src0 = GetPixel<T>(sourceData, x * 2, y * 2, 0, sourceRowPitch, sourceDepthPitch);
const T *src1 = GetPixel<T>(sourceData, x * 2, y * 2 + 1, 0, sourceRowPitch, sourceDepthPitch);
const T *src2 = GetPixel<T>(sourceData, x * 2 + 1, y * 2, 0, sourceRowPitch, sourceDepthPitch);
const T *src3 = GetPixel<T>(sourceData, x * 2 + 1, y * 2 + 1, 0, sourceRowPitch, sourceDepthPitch);
T *dst = GetPixel<T>(destData, x, y, 0, destRowPitch, destDepthPitch);
T tmp0, tmp1;
T::average(&tmp0, src0, src1);
T::average(&tmp1, src2, src3);
T::average(dst, &tmp0, &tmp1);
}
}
}
template <typename T>
static void GenerateMip_YZ(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
ASSERT(sourceWidth == 1);
ASSERT(sourceHeight > 1);
ASSERT(sourceDepth > 1);
for (size_t z = 0; z < destDepth; z++)
{
for (size_t y = 0; y < destHeight; y++)
{
const T *src0 = GetPixel<T>(sourceData, 0, y * 2, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src1 = GetPixel<T>(sourceData, 0, y * 2, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
const T *src2 = GetPixel<T>(sourceData, 0, y * 2 + 1, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src3 = GetPixel<T>(sourceData, 0, y * 2 + 1, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
T *dst = GetPixel<T>(destData, 0, y, z, destRowPitch, destDepthPitch);
T tmp0, tmp1;
T::average(&tmp0, src0, src1);
T::average(&tmp1, src2, src3);
T::average(dst, &tmp0, &tmp1);
}
}
}
template <typename T>
static void GenerateMip_XZ(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
ASSERT(sourceWidth > 1);
ASSERT(sourceHeight == 1);
ASSERT(sourceDepth > 1);
for (size_t z = 0; z < destDepth; z++)
{
for (size_t x = 0; x < destWidth; x++)
{
const T *src0 = GetPixel<T>(sourceData, x * 2, 0, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src1 = GetPixel<T>(sourceData, x * 2, 0, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
const T *src2 = GetPixel<T>(sourceData, x * 2 + 1, 0, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src3 = GetPixel<T>(sourceData, x * 2 + 1, 0, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
T *dst = GetPixel<T>(destData, x, 0, z, destRowPitch, destDepthPitch);
T tmp0, tmp1;
T::average(&tmp0, src0, src1);
T::average(&tmp1, src2, src3);
T::average(dst, &tmp0, &tmp1);
}
}
}
template <typename T>
static void GenerateMip_XYZ(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
ASSERT(sourceWidth > 1);
ASSERT(sourceHeight > 1);
ASSERT(sourceDepth > 1);
for (size_t z = 0; z < destDepth; z++)
{
for (size_t y = 0; y < destHeight; y++)
{
for (size_t x = 0; x < destWidth; x++)
{
const T *src0 = GetPixel<T>(sourceData, x * 2, y * 2, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src1 = GetPixel<T>(sourceData, x * 2, y * 2, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
const T *src2 = GetPixel<T>(sourceData, x * 2, y * 2 + 1, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src3 = GetPixel<T>(sourceData, x * 2, y * 2 + 1, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
const T *src4 = GetPixel<T>(sourceData, x * 2 + 1, y * 2, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src5 = GetPixel<T>(sourceData, x * 2 + 1, y * 2, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
const T *src6 = GetPixel<T>(sourceData, x * 2 + 1, y * 2 + 1, z * 2, sourceRowPitch, sourceDepthPitch);
const T *src7 = GetPixel<T>(sourceData, x * 2 + 1, y * 2 + 1, z * 2 + 1, sourceRowPitch, sourceDepthPitch);
T *dst = GetPixel<T>(destData, x, y, z, destRowPitch, destDepthPitch);
T tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
T::average(&tmp0, src0, src1);
T::average(&tmp1, src2, src3);
T::average(&tmp2, src4, src5);
T::average(&tmp3, src6, src7);
T::average(&tmp4, &tmp0, &tmp1);
T::average(&tmp5, &tmp2, &tmp3);
T::average(dst, &tmp4, &tmp5);
}
}
}
}
typedef void (*MipGenerationFunction)(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
size_t destWidth, size_t destHeight, size_t destDepth,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch);
template <typename T>
static MipGenerationFunction GetMipGenerationFunction(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth)
{
uint8_t index = ((sourceWidth > 1) ? 1 : 0) |
((sourceHeight > 1) ? 2 : 0) |
((sourceDepth > 1) ? 4 : 0);
switch (index)
{
case 0: return NULL;
case 1: return GenerateMip_X<T>; // W x 1 x 1
case 2: return GenerateMip_Y<T>; // 1 x H x 1
case 3: return GenerateMip_XY<T>; // W x H x 1
case 4: return GenerateMip_Z<T>; // 1 x 1 x D
case 5: return GenerateMip_XZ<T>; // W x 1 x D
case 6: return GenerateMip_YZ<T>; // 1 x H x D
case 7: return GenerateMip_XYZ<T>; // W x H x D
}
UNREACHABLE();
return NULL;
}
} // namespace priv
template <typename T>
inline void GenerateMip(size_t sourceWidth, size_t sourceHeight, size_t sourceDepth,
const uint8_t *sourceData, size_t sourceRowPitch, size_t sourceDepthPitch,
uint8_t *destData, size_t destRowPitch, size_t destDepthPitch)
{
size_t mipWidth = std::max<size_t>(1, sourceWidth >> 1);
size_t mipHeight = std::max<size_t>(1, sourceHeight >> 1);
size_t mipDepth = std::max<size_t>(1, sourceDepth >> 1);
priv::MipGenerationFunction generationFunction = priv::GetMipGenerationFunction<T>(sourceWidth, sourceHeight, sourceDepth);
ASSERT(generationFunction != NULL);
generationFunction(sourceWidth, sourceHeight, sourceDepth, sourceData, sourceRowPitch, sourceDepthPitch,
mipWidth, mipHeight, mipDepth, destData, destRowPitch, destDepthPitch);
}
} // namespace angle
|