mesa, util: move RGB9E5 conversion functions to gallium/util
[mesa.git] / src / mesa / main / mipmap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file mipmap.c mipmap generation and teximage resizing functions.
28 */
29
30 #include "imports.h"
31 #include "formats.h"
32 #include "mipmap.h"
33 #include "mtypes.h"
34 #include "teximage.h"
35 #include "texstore.h"
36 #include "image.h"
37 #include "macros.h"
38 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
39
40
41
42 static GLint
43 bytes_per_pixel(GLenum datatype, GLuint comps)
44 {
45 GLint b = _mesa_sizeof_packed_type(datatype);
46 assert(b >= 0);
47
48 if (_mesa_type_is_packed(datatype))
49 return b;
50 else
51 return b * comps;
52 }
53
54
55 /**
56 * \name Support macros for do_row and do_row_3d
57 *
58 * The macro madness is here for two reasons. First, it compacts the code
59 * slightly. Second, it makes it much easier to adjust the specifics of the
60 * filter to tune the rounding characteristics.
61 */
62 /*@{*/
63 #define DECLARE_ROW_POINTERS(t, e) \
64 const t(*rowA)[e] = (const t(*)[e]) srcRowA; \
65 const t(*rowB)[e] = (const t(*)[e]) srcRowB; \
66 const t(*rowC)[e] = (const t(*)[e]) srcRowC; \
67 const t(*rowD)[e] = (const t(*)[e]) srcRowD; \
68 t(*dst)[e] = (t(*)[e]) dstRow
69
70 #define DECLARE_ROW_POINTERS0(t) \
71 const t *rowA = (const t *) srcRowA; \
72 const t *rowB = (const t *) srcRowB; \
73 const t *rowC = (const t *) srcRowC; \
74 const t *rowD = (const t *) srcRowD; \
75 t *dst = (t *) dstRow
76
77 #define FILTER_SUM_3D(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
78 ((unsigned) Aj + (unsigned) Ak \
79 + (unsigned) Bj + (unsigned) Bk \
80 + (unsigned) Cj + (unsigned) Ck \
81 + (unsigned) Dj + (unsigned) Dk \
82 + 4) >> 3
83
84 #define FILTER_3D(e) \
85 do { \
86 dst[i][e] = FILTER_SUM_3D(rowA[j][e], rowA[k][e], \
87 rowB[j][e], rowB[k][e], \
88 rowC[j][e], rowC[k][e], \
89 rowD[j][e], rowD[k][e]); \
90 } while(0)
91
92 #define FILTER_SUM_3D_SIGNED(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
93 (Aj + Ak \
94 + Bj + Bk \
95 + Cj + Ck \
96 + Dj + Dk \
97 + 4) / 8
98
99 #define FILTER_3D_SIGNED(e) \
100 do { \
101 dst[i][e] = FILTER_SUM_3D_SIGNED(rowA[j][e], rowA[k][e], \
102 rowB[j][e], rowB[k][e], \
103 rowC[j][e], rowC[k][e], \
104 rowD[j][e], rowD[k][e]); \
105 } while(0)
106
107 #define FILTER_F_3D(e) \
108 do { \
109 dst[i][e] = (rowA[j][e] + rowA[k][e] \
110 + rowB[j][e] + rowB[k][e] \
111 + rowC[j][e] + rowC[k][e] \
112 + rowD[j][e] + rowD[k][e]) * 0.125F; \
113 } while(0)
114
115 #define FILTER_HF_3D(e) \
116 do { \
117 const GLfloat aj = _mesa_half_to_float(rowA[j][e]); \
118 const GLfloat ak = _mesa_half_to_float(rowA[k][e]); \
119 const GLfloat bj = _mesa_half_to_float(rowB[j][e]); \
120 const GLfloat bk = _mesa_half_to_float(rowB[k][e]); \
121 const GLfloat cj = _mesa_half_to_float(rowC[j][e]); \
122 const GLfloat ck = _mesa_half_to_float(rowC[k][e]); \
123 const GLfloat dj = _mesa_half_to_float(rowD[j][e]); \
124 const GLfloat dk = _mesa_half_to_float(rowD[k][e]); \
125 dst[i][e] = _mesa_float_to_half((aj + ak + bj + bk + cj + ck + dj + dk) \
126 * 0.125F); \
127 } while(0)
128 /*@}*/
129
130
131 /**
132 * Average together two rows of a source image to produce a single new
133 * row in the dest image. It's legal for the two source rows to point
134 * to the same data. The source width must be equal to either the
135 * dest width or two times the dest width.
136 * \param datatype GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc.
137 * \param comps number of components per pixel (1..4)
138 */
139 static void
140 do_row(GLenum datatype, GLuint comps, GLint srcWidth,
141 const GLvoid *srcRowA, const GLvoid *srcRowB,
142 GLint dstWidth, GLvoid *dstRow)
143 {
144 const GLuint k0 = (srcWidth == dstWidth) ? 0 : 1;
145 const GLuint colStride = (srcWidth == dstWidth) ? 1 : 2;
146
147 ASSERT(comps >= 1);
148 ASSERT(comps <= 4);
149
150 /* This assertion is no longer valid with non-power-of-2 textures
151 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
152 */
153
154 if (datatype == GL_UNSIGNED_BYTE && comps == 4) {
155 GLuint i, j, k;
156 const GLubyte(*rowA)[4] = (const GLubyte(*)[4]) srcRowA;
157 const GLubyte(*rowB)[4] = (const GLubyte(*)[4]) srcRowB;
158 GLubyte(*dst)[4] = (GLubyte(*)[4]) dstRow;
159 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
160 i++, j += colStride, k += colStride) {
161 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
162 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
163 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
164 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
165 }
166 }
167 else if (datatype == GL_UNSIGNED_BYTE && comps == 3) {
168 GLuint i, j, k;
169 const GLubyte(*rowA)[3] = (const GLubyte(*)[3]) srcRowA;
170 const GLubyte(*rowB)[3] = (const GLubyte(*)[3]) srcRowB;
171 GLubyte(*dst)[3] = (GLubyte(*)[3]) dstRow;
172 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
173 i++, j += colStride, k += colStride) {
174 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
175 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
176 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
177 }
178 }
179 else if (datatype == GL_UNSIGNED_BYTE && comps == 2) {
180 GLuint i, j, k;
181 const GLubyte(*rowA)[2] = (const GLubyte(*)[2]) srcRowA;
182 const GLubyte(*rowB)[2] = (const GLubyte(*)[2]) srcRowB;
183 GLubyte(*dst)[2] = (GLubyte(*)[2]) dstRow;
184 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
185 i++, j += colStride, k += colStride) {
186 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) >> 2;
187 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2;
188 }
189 }
190 else if (datatype == GL_UNSIGNED_BYTE && comps == 1) {
191 GLuint i, j, k;
192 const GLubyte *rowA = (const GLubyte *) srcRowA;
193 const GLubyte *rowB = (const GLubyte *) srcRowB;
194 GLubyte *dst = (GLubyte *) dstRow;
195 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
196 i++, j += colStride, k += colStride) {
197 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) >> 2;
198 }
199 }
200
201 else if (datatype == GL_BYTE && comps == 4) {
202 GLuint i, j, k;
203 const GLbyte(*rowA)[4] = (const GLbyte(*)[4]) srcRowA;
204 const GLbyte(*rowB)[4] = (const GLbyte(*)[4]) srcRowB;
205 GLbyte(*dst)[4] = (GLbyte(*)[4]) dstRow;
206 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
207 i++, j += colStride, k += colStride) {
208 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
209 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
210 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
211 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
212 }
213 }
214 else if (datatype == GL_BYTE && comps == 3) {
215 GLuint i, j, k;
216 const GLbyte(*rowA)[3] = (const GLbyte(*)[3]) srcRowA;
217 const GLbyte(*rowB)[3] = (const GLbyte(*)[3]) srcRowB;
218 GLbyte(*dst)[3] = (GLbyte(*)[3]) dstRow;
219 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
220 i++, j += colStride, k += colStride) {
221 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
222 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
223 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
224 }
225 }
226 else if (datatype == GL_BYTE && comps == 2) {
227 GLuint i, j, k;
228 const GLbyte(*rowA)[2] = (const GLbyte(*)[2]) srcRowA;
229 const GLbyte(*rowB)[2] = (const GLbyte(*)[2]) srcRowB;
230 GLbyte(*dst)[2] = (GLbyte(*)[2]) dstRow;
231 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
232 i++, j += colStride, k += colStride) {
233 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
234 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
235 }
236 }
237 else if (datatype == GL_BYTE && comps == 1) {
238 GLuint i, j, k;
239 const GLbyte *rowA = (const GLbyte *) srcRowA;
240 const GLbyte *rowB = (const GLbyte *) srcRowB;
241 GLbyte *dst = (GLbyte *) dstRow;
242 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
243 i++, j += colStride, k += colStride) {
244 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
245 }
246 }
247
248 else if (datatype == GL_UNSIGNED_SHORT && comps == 4) {
249 GLuint i, j, k;
250 const GLushort(*rowA)[4] = (const GLushort(*)[4]) srcRowA;
251 const GLushort(*rowB)[4] = (const GLushort(*)[4]) srcRowB;
252 GLushort(*dst)[4] = (GLushort(*)[4]) dstRow;
253 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
254 i++, j += colStride, k += colStride) {
255 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
256 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
257 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
258 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
259 }
260 }
261 else if (datatype == GL_UNSIGNED_SHORT && comps == 3) {
262 GLuint i, j, k;
263 const GLushort(*rowA)[3] = (const GLushort(*)[3]) srcRowA;
264 const GLushort(*rowB)[3] = (const GLushort(*)[3]) srcRowB;
265 GLushort(*dst)[3] = (GLushort(*)[3]) dstRow;
266 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
267 i++, j += colStride, k += colStride) {
268 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
269 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
270 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
271 }
272 }
273 else if (datatype == GL_UNSIGNED_SHORT && comps == 2) {
274 GLuint i, j, k;
275 const GLushort(*rowA)[2] = (const GLushort(*)[2]) srcRowA;
276 const GLushort(*rowB)[2] = (const GLushort(*)[2]) srcRowB;
277 GLushort(*dst)[2] = (GLushort(*)[2]) dstRow;
278 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
279 i++, j += colStride, k += colStride) {
280 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
281 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
282 }
283 }
284 else if (datatype == GL_UNSIGNED_SHORT && comps == 1) {
285 GLuint i, j, k;
286 const GLushort *rowA = (const GLushort *) srcRowA;
287 const GLushort *rowB = (const GLushort *) srcRowB;
288 GLushort *dst = (GLushort *) dstRow;
289 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
290 i++, j += colStride, k += colStride) {
291 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
292 }
293 }
294
295 else if (datatype == GL_SHORT && comps == 4) {
296 GLuint i, j, k;
297 const GLshort(*rowA)[4] = (const GLshort(*)[4]) srcRowA;
298 const GLshort(*rowB)[4] = (const GLshort(*)[4]) srcRowB;
299 GLshort(*dst)[4] = (GLshort(*)[4]) dstRow;
300 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
301 i++, j += colStride, k += colStride) {
302 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
303 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
304 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
305 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
306 }
307 }
308 else if (datatype == GL_SHORT && comps == 3) {
309 GLuint i, j, k;
310 const GLshort(*rowA)[3] = (const GLshort(*)[3]) srcRowA;
311 const GLshort(*rowB)[3] = (const GLshort(*)[3]) srcRowB;
312 GLshort(*dst)[3] = (GLshort(*)[3]) dstRow;
313 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
314 i++, j += colStride, k += colStride) {
315 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
316 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
317 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
318 }
319 }
320 else if (datatype == GL_SHORT && comps == 2) {
321 GLuint i, j, k;
322 const GLshort(*rowA)[2] = (const GLshort(*)[2]) srcRowA;
323 const GLshort(*rowB)[2] = (const GLshort(*)[2]) srcRowB;
324 GLshort(*dst)[2] = (GLshort(*)[2]) dstRow;
325 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
326 i++, j += colStride, k += colStride) {
327 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
328 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
329 }
330 }
331 else if (datatype == GL_SHORT && comps == 1) {
332 GLuint i, j, k;
333 const GLshort *rowA = (const GLshort *) srcRowA;
334 const GLshort *rowB = (const GLshort *) srcRowB;
335 GLshort *dst = (GLshort *) dstRow;
336 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
337 i++, j += colStride, k += colStride) {
338 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
339 }
340 }
341
342 else if (datatype == GL_FLOAT && comps == 4) {
343 GLuint i, j, k;
344 const GLfloat(*rowA)[4] = (const GLfloat(*)[4]) srcRowA;
345 const GLfloat(*rowB)[4] = (const GLfloat(*)[4]) srcRowB;
346 GLfloat(*dst)[4] = (GLfloat(*)[4]) dstRow;
347 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
348 i++, j += colStride, k += colStride) {
349 dst[i][0] = (rowA[j][0] + rowA[k][0] +
350 rowB[j][0] + rowB[k][0]) * 0.25F;
351 dst[i][1] = (rowA[j][1] + rowA[k][1] +
352 rowB[j][1] + rowB[k][1]) * 0.25F;
353 dst[i][2] = (rowA[j][2] + rowA[k][2] +
354 rowB[j][2] + rowB[k][2]) * 0.25F;
355 dst[i][3] = (rowA[j][3] + rowA[k][3] +
356 rowB[j][3] + rowB[k][3]) * 0.25F;
357 }
358 }
359 else if (datatype == GL_FLOAT && comps == 3) {
360 GLuint i, j, k;
361 const GLfloat(*rowA)[3] = (const GLfloat(*)[3]) srcRowA;
362 const GLfloat(*rowB)[3] = (const GLfloat(*)[3]) srcRowB;
363 GLfloat(*dst)[3] = (GLfloat(*)[3]) dstRow;
364 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
365 i++, j += colStride, k += colStride) {
366 dst[i][0] = (rowA[j][0] + rowA[k][0] +
367 rowB[j][0] + rowB[k][0]) * 0.25F;
368 dst[i][1] = (rowA[j][1] + rowA[k][1] +
369 rowB[j][1] + rowB[k][1]) * 0.25F;
370 dst[i][2] = (rowA[j][2] + rowA[k][2] +
371 rowB[j][2] + rowB[k][2]) * 0.25F;
372 }
373 }
374 else if (datatype == GL_FLOAT && comps == 2) {
375 GLuint i, j, k;
376 const GLfloat(*rowA)[2] = (const GLfloat(*)[2]) srcRowA;
377 const GLfloat(*rowB)[2] = (const GLfloat(*)[2]) srcRowB;
378 GLfloat(*dst)[2] = (GLfloat(*)[2]) dstRow;
379 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
380 i++, j += colStride, k += colStride) {
381 dst[i][0] = (rowA[j][0] + rowA[k][0] +
382 rowB[j][0] + rowB[k][0]) * 0.25F;
383 dst[i][1] = (rowA[j][1] + rowA[k][1] +
384 rowB[j][1] + rowB[k][1]) * 0.25F;
385 }
386 }
387 else if (datatype == GL_FLOAT && comps == 1) {
388 GLuint i, j, k;
389 const GLfloat *rowA = (const GLfloat *) srcRowA;
390 const GLfloat *rowB = (const GLfloat *) srcRowB;
391 GLfloat *dst = (GLfloat *) dstRow;
392 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
393 i++, j += colStride, k += colStride) {
394 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F;
395 }
396 }
397
398 else if (datatype == GL_HALF_FLOAT_ARB && comps == 4) {
399 GLuint i, j, k, comp;
400 const GLhalfARB(*rowA)[4] = (const GLhalfARB(*)[4]) srcRowA;
401 const GLhalfARB(*rowB)[4] = (const GLhalfARB(*)[4]) srcRowB;
402 GLhalfARB(*dst)[4] = (GLhalfARB(*)[4]) dstRow;
403 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
404 i++, j += colStride, k += colStride) {
405 for (comp = 0; comp < 4; comp++) {
406 GLfloat aj, ak, bj, bk;
407 aj = _mesa_half_to_float(rowA[j][comp]);
408 ak = _mesa_half_to_float(rowA[k][comp]);
409 bj = _mesa_half_to_float(rowB[j][comp]);
410 bk = _mesa_half_to_float(rowB[k][comp]);
411 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
412 }
413 }
414 }
415 else if (datatype == GL_HALF_FLOAT_ARB && comps == 3) {
416 GLuint i, j, k, comp;
417 const GLhalfARB(*rowA)[3] = (const GLhalfARB(*)[3]) srcRowA;
418 const GLhalfARB(*rowB)[3] = (const GLhalfARB(*)[3]) srcRowB;
419 GLhalfARB(*dst)[3] = (GLhalfARB(*)[3]) dstRow;
420 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
421 i++, j += colStride, k += colStride) {
422 for (comp = 0; comp < 3; comp++) {
423 GLfloat aj, ak, bj, bk;
424 aj = _mesa_half_to_float(rowA[j][comp]);
425 ak = _mesa_half_to_float(rowA[k][comp]);
426 bj = _mesa_half_to_float(rowB[j][comp]);
427 bk = _mesa_half_to_float(rowB[k][comp]);
428 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
429 }
430 }
431 }
432 else if (datatype == GL_HALF_FLOAT_ARB && comps == 2) {
433 GLuint i, j, k, comp;
434 const GLhalfARB(*rowA)[2] = (const GLhalfARB(*)[2]) srcRowA;
435 const GLhalfARB(*rowB)[2] = (const GLhalfARB(*)[2]) srcRowB;
436 GLhalfARB(*dst)[2] = (GLhalfARB(*)[2]) dstRow;
437 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
438 i++, j += colStride, k += colStride) {
439 for (comp = 0; comp < 2; comp++) {
440 GLfloat aj, ak, bj, bk;
441 aj = _mesa_half_to_float(rowA[j][comp]);
442 ak = _mesa_half_to_float(rowA[k][comp]);
443 bj = _mesa_half_to_float(rowB[j][comp]);
444 bk = _mesa_half_to_float(rowB[k][comp]);
445 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
446 }
447 }
448 }
449 else if (datatype == GL_HALF_FLOAT_ARB && comps == 1) {
450 GLuint i, j, k;
451 const GLhalfARB *rowA = (const GLhalfARB *) srcRowA;
452 const GLhalfARB *rowB = (const GLhalfARB *) srcRowB;
453 GLhalfARB *dst = (GLhalfARB *) dstRow;
454 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
455 i++, j += colStride, k += colStride) {
456 GLfloat aj, ak, bj, bk;
457 aj = _mesa_half_to_float(rowA[j]);
458 ak = _mesa_half_to_float(rowA[k]);
459 bj = _mesa_half_to_float(rowB[j]);
460 bk = _mesa_half_to_float(rowB[k]);
461 dst[i] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
462 }
463 }
464
465 else if (datatype == GL_UNSIGNED_INT && comps == 1) {
466 GLuint i, j, k;
467 const GLuint *rowA = (const GLuint *) srcRowA;
468 const GLuint *rowB = (const GLuint *) srcRowB;
469 GLuint *dst = (GLuint *) dstRow;
470 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
471 i++, j += colStride, k += colStride) {
472 dst[i] = (GLfloat)(rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4);
473 }
474 }
475
476 else if (datatype == GL_UNSIGNED_SHORT_5_6_5 && comps == 3) {
477 GLuint i, j, k;
478 const GLushort *rowA = (const GLushort *) srcRowA;
479 const GLushort *rowB = (const GLushort *) srcRowB;
480 GLushort *dst = (GLushort *) dstRow;
481 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
482 i++, j += colStride, k += colStride) {
483 const GLint rowAr0 = rowA[j] & 0x1f;
484 const GLint rowAr1 = rowA[k] & 0x1f;
485 const GLint rowBr0 = rowB[j] & 0x1f;
486 const GLint rowBr1 = rowB[k] & 0x1f;
487 const GLint rowAg0 = (rowA[j] >> 5) & 0x3f;
488 const GLint rowAg1 = (rowA[k] >> 5) & 0x3f;
489 const GLint rowBg0 = (rowB[j] >> 5) & 0x3f;
490 const GLint rowBg1 = (rowB[k] >> 5) & 0x3f;
491 const GLint rowAb0 = (rowA[j] >> 11) & 0x1f;
492 const GLint rowAb1 = (rowA[k] >> 11) & 0x1f;
493 const GLint rowBb0 = (rowB[j] >> 11) & 0x1f;
494 const GLint rowBb1 = (rowB[k] >> 11) & 0x1f;
495 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
496 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
497 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
498 dst[i] = (blue << 11) | (green << 5) | red;
499 }
500 }
501 else if (datatype == GL_UNSIGNED_SHORT_4_4_4_4 && comps == 4) {
502 GLuint i, j, k;
503 const GLushort *rowA = (const GLushort *) srcRowA;
504 const GLushort *rowB = (const GLushort *) srcRowB;
505 GLushort *dst = (GLushort *) dstRow;
506 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
507 i++, j += colStride, k += colStride) {
508 const GLint rowAr0 = rowA[j] & 0xf;
509 const GLint rowAr1 = rowA[k] & 0xf;
510 const GLint rowBr0 = rowB[j] & 0xf;
511 const GLint rowBr1 = rowB[k] & 0xf;
512 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
513 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
514 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
515 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
516 const GLint rowAb0 = (rowA[j] >> 8) & 0xf;
517 const GLint rowAb1 = (rowA[k] >> 8) & 0xf;
518 const GLint rowBb0 = (rowB[j] >> 8) & 0xf;
519 const GLint rowBb1 = (rowB[k] >> 8) & 0xf;
520 const GLint rowAa0 = (rowA[j] >> 12) & 0xf;
521 const GLint rowAa1 = (rowA[k] >> 12) & 0xf;
522 const GLint rowBa0 = (rowB[j] >> 12) & 0xf;
523 const GLint rowBa1 = (rowB[k] >> 12) & 0xf;
524 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
525 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
526 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
527 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
528 dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
529 }
530 }
531 else if (datatype == GL_UNSIGNED_SHORT_1_5_5_5_REV && comps == 4) {
532 GLuint i, j, k;
533 const GLushort *rowA = (const GLushort *) srcRowA;
534 const GLushort *rowB = (const GLushort *) srcRowB;
535 GLushort *dst = (GLushort *) dstRow;
536 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
537 i++, j += colStride, k += colStride) {
538 const GLint rowAr0 = rowA[j] & 0x1f;
539 const GLint rowAr1 = rowA[k] & 0x1f;
540 const GLint rowBr0 = rowB[j] & 0x1f;
541 const GLint rowBr1 = rowB[k] & 0x1f;
542 const GLint rowAg0 = (rowA[j] >> 5) & 0x1f;
543 const GLint rowAg1 = (rowA[k] >> 5) & 0x1f;
544 const GLint rowBg0 = (rowB[j] >> 5) & 0x1f;
545 const GLint rowBg1 = (rowB[k] >> 5) & 0x1f;
546 const GLint rowAb0 = (rowA[j] >> 10) & 0x1f;
547 const GLint rowAb1 = (rowA[k] >> 10) & 0x1f;
548 const GLint rowBb0 = (rowB[j] >> 10) & 0x1f;
549 const GLint rowBb1 = (rowB[k] >> 10) & 0x1f;
550 const GLint rowAa0 = (rowA[j] >> 15) & 0x1;
551 const GLint rowAa1 = (rowA[k] >> 15) & 0x1;
552 const GLint rowBa0 = (rowB[j] >> 15) & 0x1;
553 const GLint rowBa1 = (rowB[k] >> 15) & 0x1;
554 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
555 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
556 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
557 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
558 dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
559 }
560 }
561 else if (datatype == GL_UNSIGNED_SHORT_5_5_5_1 && comps == 4) {
562 GLuint i, j, k;
563 const GLushort *rowA = (const GLushort *) srcRowA;
564 const GLushort *rowB = (const GLushort *) srcRowB;
565 GLushort *dst = (GLushort *) dstRow;
566 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
567 i++, j += colStride, k += colStride) {
568 const GLint rowAr0 = (rowA[j] >> 11) & 0x1f;
569 const GLint rowAr1 = (rowA[k] >> 11) & 0x1f;
570 const GLint rowBr0 = (rowB[j] >> 11) & 0x1f;
571 const GLint rowBr1 = (rowB[k] >> 11) & 0x1f;
572 const GLint rowAg0 = (rowA[j] >> 6) & 0x1f;
573 const GLint rowAg1 = (rowA[k] >> 6) & 0x1f;
574 const GLint rowBg0 = (rowB[j] >> 6) & 0x1f;
575 const GLint rowBg1 = (rowB[k] >> 6) & 0x1f;
576 const GLint rowAb0 = (rowA[j] >> 1) & 0x1f;
577 const GLint rowAb1 = (rowA[k] >> 1) & 0x1f;
578 const GLint rowBb0 = (rowB[j] >> 1) & 0x1f;
579 const GLint rowBb1 = (rowB[k] >> 1) & 0x1f;
580 const GLint rowAa0 = (rowA[j] & 0x1);
581 const GLint rowAa1 = (rowA[k] & 0x1);
582 const GLint rowBa0 = (rowB[j] & 0x1);
583 const GLint rowBa1 = (rowB[k] & 0x1);
584 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
585 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
586 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
587 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
588 dst[i] = (red << 11) | (green << 6) | (blue << 1) | alpha;
589 }
590 }
591
592 else if (datatype == GL_UNSIGNED_BYTE_3_3_2 && comps == 3) {
593 GLuint i, j, k;
594 const GLubyte *rowA = (const GLubyte *) srcRowA;
595 const GLubyte *rowB = (const GLubyte *) srcRowB;
596 GLubyte *dst = (GLubyte *) dstRow;
597 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
598 i++, j += colStride, k += colStride) {
599 const GLint rowAr0 = rowA[j] & 0x3;
600 const GLint rowAr1 = rowA[k] & 0x3;
601 const GLint rowBr0 = rowB[j] & 0x3;
602 const GLint rowBr1 = rowB[k] & 0x3;
603 const GLint rowAg0 = (rowA[j] >> 2) & 0x7;
604 const GLint rowAg1 = (rowA[k] >> 2) & 0x7;
605 const GLint rowBg0 = (rowB[j] >> 2) & 0x7;
606 const GLint rowBg1 = (rowB[k] >> 2) & 0x7;
607 const GLint rowAb0 = (rowA[j] >> 5) & 0x7;
608 const GLint rowAb1 = (rowA[k] >> 5) & 0x7;
609 const GLint rowBb0 = (rowB[j] >> 5) & 0x7;
610 const GLint rowBb1 = (rowB[k] >> 5) & 0x7;
611 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
612 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
613 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
614 dst[i] = (blue << 5) | (green << 2) | red;
615 }
616 }
617
618 else if (datatype == MESA_UNSIGNED_BYTE_4_4 && comps == 2) {
619 GLuint i, j, k;
620 const GLubyte *rowA = (const GLubyte *) srcRowA;
621 const GLubyte *rowB = (const GLubyte *) srcRowB;
622 GLubyte *dst = (GLubyte *) dstRow;
623 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
624 i++, j += colStride, k += colStride) {
625 const GLint rowAr0 = rowA[j] & 0xf;
626 const GLint rowAr1 = rowA[k] & 0xf;
627 const GLint rowBr0 = rowB[j] & 0xf;
628 const GLint rowBr1 = rowB[k] & 0xf;
629 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
630 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
631 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
632 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
633 const GLint r = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
634 const GLint g = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
635 dst[i] = (g << 4) | r;
636 }
637 }
638
639 else if (datatype == GL_UNSIGNED_INT_2_10_10_10_REV && comps == 4) {
640 GLuint i, j, k;
641 const GLuint *rowA = (const GLuint *) srcRowA;
642 const GLuint *rowB = (const GLuint *) srcRowB;
643 GLuint *dst = (GLuint *) dstRow;
644 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
645 i++, j += colStride, k += colStride) {
646 const GLint rowAr0 = rowA[j] & 0x3ff;
647 const GLint rowAr1 = rowA[k] & 0x3ff;
648 const GLint rowBr0 = rowB[j] & 0x3ff;
649 const GLint rowBr1 = rowB[k] & 0x3ff;
650 const GLint rowAg0 = (rowA[j] >> 10) & 0x3ff;
651 const GLint rowAg1 = (rowA[k] >> 10) & 0x3ff;
652 const GLint rowBg0 = (rowB[j] >> 10) & 0x3ff;
653 const GLint rowBg1 = (rowB[k] >> 10) & 0x3ff;
654 const GLint rowAb0 = (rowA[j] >> 20) & 0x3ff;
655 const GLint rowAb1 = (rowA[k] >> 20) & 0x3ff;
656 const GLint rowBb0 = (rowB[j] >> 20) & 0x3ff;
657 const GLint rowBb1 = (rowB[k] >> 20) & 0x3ff;
658 const GLint rowAa0 = (rowA[j] >> 30) & 0x3;
659 const GLint rowAa1 = (rowA[k] >> 30) & 0x3;
660 const GLint rowBa0 = (rowB[j] >> 30) & 0x3;
661 const GLint rowBa1 = (rowB[k] >> 30) & 0x3;
662 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
663 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
664 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
665 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
666 dst[i] = (alpha << 30) | (blue << 20) | (green << 10) | red;
667 }
668 }
669
670 else if (datatype == GL_UNSIGNED_INT_5_9_9_9_REV && comps == 3) {
671 GLuint i, j, k;
672 const GLuint *rowA = (const GLuint*) srcRowA;
673 const GLuint *rowB = (const GLuint*) srcRowB;
674 GLuint *dst = (GLuint*)dstRow;
675 GLfloat res[3], rowAj[3], rowBj[3], rowAk[3], rowBk[3];
676 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
677 i++, j += colStride, k += colStride) {
678 rgb9e5_to_float3(rowA[j], rowAj);
679 rgb9e5_to_float3(rowB[j], rowBj);
680 rgb9e5_to_float3(rowA[k], rowAk);
681 rgb9e5_to_float3(rowB[k], rowBk);
682 res[0] = (rowAj[0] + rowAk[0] + rowBj[0] + rowBk[0]) * 0.25F;
683 res[1] = (rowAj[1] + rowAk[1] + rowBj[1] + rowBk[1]) * 0.25F;
684 res[2] = (rowAj[2] + rowAk[2] + rowBj[2] + rowBk[2]) * 0.25F;
685 dst[i] = float3_to_rgb9e5(res);
686 }
687 }
688
689 else {
690 _mesa_problem(NULL, "bad format in do_row()");
691 }
692 }
693
694
695 /**
696 * Average together four rows of a source image to produce a single new
697 * row in the dest image. It's legal for the two source rows to point
698 * to the same data. The source width must be equal to either the
699 * dest width or two times the dest width.
700 *
701 * \param datatype GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT,
702 * \c GL_FLOAT, etc.
703 * \param comps number of components per pixel (1..4)
704 * \param srcWidth Width of a row in the source data
705 * \param srcRowA Pointer to one of the rows of source data
706 * \param srcRowB Pointer to one of the rows of source data
707 * \param srcRowC Pointer to one of the rows of source data
708 * \param srcRowD Pointer to one of the rows of source data
709 * \param dstWidth Width of a row in the destination data
710 * \param srcRowA Pointer to the row of destination data
711 */
712 static void
713 do_row_3D(GLenum datatype, GLuint comps, GLint srcWidth,
714 const GLvoid *srcRowA, const GLvoid *srcRowB,
715 const GLvoid *srcRowC, const GLvoid *srcRowD,
716 GLint dstWidth, GLvoid *dstRow)
717 {
718 const GLuint k0 = (srcWidth == dstWidth) ? 0 : 1;
719 const GLuint colStride = (srcWidth == dstWidth) ? 1 : 2;
720 GLuint i, j, k;
721
722 ASSERT(comps >= 1);
723 ASSERT(comps <= 4);
724
725 if ((datatype == GL_UNSIGNED_BYTE) && (comps == 4)) {
726 DECLARE_ROW_POINTERS(GLubyte, 4);
727
728 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
729 i++, j += colStride, k += colStride) {
730 FILTER_3D(0);
731 FILTER_3D(1);
732 FILTER_3D(2);
733 FILTER_3D(3);
734 }
735 }
736 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 3)) {
737 DECLARE_ROW_POINTERS(GLubyte, 3);
738
739 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
740 i++, j += colStride, k += colStride) {
741 FILTER_3D(0);
742 FILTER_3D(1);
743 FILTER_3D(2);
744 }
745 }
746 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 2)) {
747 DECLARE_ROW_POINTERS(GLubyte, 2);
748
749 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
750 i++, j += colStride, k += colStride) {
751 FILTER_3D(0);
752 FILTER_3D(1);
753 }
754 }
755 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 1)) {
756 DECLARE_ROW_POINTERS(GLubyte, 1);
757
758 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
759 i++, j += colStride, k += colStride) {
760 FILTER_3D(0);
761 }
762 }
763 else if ((datatype == GL_BYTE) && (comps == 4)) {
764 DECLARE_ROW_POINTERS(GLbyte, 4);
765
766 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
767 i++, j += colStride, k += colStride) {
768 FILTER_3D_SIGNED(0);
769 FILTER_3D_SIGNED(1);
770 FILTER_3D_SIGNED(2);
771 FILTER_3D_SIGNED(3);
772 }
773 }
774 else if ((datatype == GL_BYTE) && (comps == 3)) {
775 DECLARE_ROW_POINTERS(GLbyte, 3);
776
777 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
778 i++, j += colStride, k += colStride) {
779 FILTER_3D_SIGNED(0);
780 FILTER_3D_SIGNED(1);
781 FILTER_3D_SIGNED(2);
782 }
783 }
784 else if ((datatype == GL_BYTE) && (comps == 2)) {
785 DECLARE_ROW_POINTERS(GLbyte, 2);
786
787 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
788 i++, j += colStride, k += colStride) {
789 FILTER_3D_SIGNED(0);
790 FILTER_3D_SIGNED(1);
791 }
792 }
793 else if ((datatype == GL_BYTE) && (comps == 1)) {
794 DECLARE_ROW_POINTERS(GLbyte, 1);
795
796 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
797 i++, j += colStride, k += colStride) {
798 FILTER_3D_SIGNED(0);
799 }
800 }
801 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 4)) {
802 DECLARE_ROW_POINTERS(GLushort, 4);
803
804 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
805 i++, j += colStride, k += colStride) {
806 FILTER_3D(0);
807 FILTER_3D(1);
808 FILTER_3D(2);
809 FILTER_3D(3);
810 }
811 }
812 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 3)) {
813 DECLARE_ROW_POINTERS(GLushort, 3);
814
815 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
816 i++, j += colStride, k += colStride) {
817 FILTER_3D(0);
818 FILTER_3D(1);
819 FILTER_3D(2);
820 }
821 }
822 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 2)) {
823 DECLARE_ROW_POINTERS(GLushort, 2);
824
825 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
826 i++, j += colStride, k += colStride) {
827 FILTER_3D(0);
828 FILTER_3D(1);
829 }
830 }
831 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 1)) {
832 DECLARE_ROW_POINTERS(GLushort, 1);
833
834 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
835 i++, j += colStride, k += colStride) {
836 FILTER_3D(0);
837 }
838 }
839 else if ((datatype == GL_SHORT) && (comps == 4)) {
840 DECLARE_ROW_POINTERS(GLshort, 4);
841
842 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
843 i++, j += colStride, k += colStride) {
844 FILTER_3D(0);
845 FILTER_3D(1);
846 FILTER_3D(2);
847 FILTER_3D(3);
848 }
849 }
850 else if ((datatype == GL_SHORT) && (comps == 3)) {
851 DECLARE_ROW_POINTERS(GLshort, 3);
852
853 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
854 i++, j += colStride, k += colStride) {
855 FILTER_3D(0);
856 FILTER_3D(1);
857 FILTER_3D(2);
858 }
859 }
860 else if ((datatype == GL_SHORT) && (comps == 2)) {
861 DECLARE_ROW_POINTERS(GLshort, 2);
862
863 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
864 i++, j += colStride, k += colStride) {
865 FILTER_3D(0);
866 FILTER_3D(1);
867 }
868 }
869 else if ((datatype == GL_SHORT) && (comps == 1)) {
870 DECLARE_ROW_POINTERS(GLshort, 1);
871
872 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
873 i++, j += colStride, k += colStride) {
874 FILTER_3D(0);
875 }
876 }
877 else if ((datatype == GL_FLOAT) && (comps == 4)) {
878 DECLARE_ROW_POINTERS(GLfloat, 4);
879
880 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
881 i++, j += colStride, k += colStride) {
882 FILTER_F_3D(0);
883 FILTER_F_3D(1);
884 FILTER_F_3D(2);
885 FILTER_F_3D(3);
886 }
887 }
888 else if ((datatype == GL_FLOAT) && (comps == 3)) {
889 DECLARE_ROW_POINTERS(GLfloat, 3);
890
891 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
892 i++, j += colStride, k += colStride) {
893 FILTER_F_3D(0);
894 FILTER_F_3D(1);
895 FILTER_F_3D(2);
896 }
897 }
898 else if ((datatype == GL_FLOAT) && (comps == 2)) {
899 DECLARE_ROW_POINTERS(GLfloat, 2);
900
901 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
902 i++, j += colStride, k += colStride) {
903 FILTER_F_3D(0);
904 FILTER_F_3D(1);
905 }
906 }
907 else if ((datatype == GL_FLOAT) && (comps == 1)) {
908 DECLARE_ROW_POINTERS(GLfloat, 1);
909
910 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
911 i++, j += colStride, k += colStride) {
912 FILTER_F_3D(0);
913 }
914 }
915 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 4)) {
916 DECLARE_ROW_POINTERS(GLhalfARB, 4);
917
918 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
919 i++, j += colStride, k += colStride) {
920 FILTER_HF_3D(0);
921 FILTER_HF_3D(1);
922 FILTER_HF_3D(2);
923 FILTER_HF_3D(3);
924 }
925 }
926 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 3)) {
927 DECLARE_ROW_POINTERS(GLhalfARB, 4);
928
929 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
930 i++, j += colStride, k += colStride) {
931 FILTER_HF_3D(0);
932 FILTER_HF_3D(1);
933 FILTER_HF_3D(2);
934 }
935 }
936 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 2)) {
937 DECLARE_ROW_POINTERS(GLhalfARB, 4);
938
939 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
940 i++, j += colStride, k += colStride) {
941 FILTER_HF_3D(0);
942 FILTER_HF_3D(1);
943 }
944 }
945 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 1)) {
946 DECLARE_ROW_POINTERS(GLhalfARB, 4);
947
948 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
949 i++, j += colStride, k += colStride) {
950 FILTER_HF_3D(0);
951 }
952 }
953 else if ((datatype == GL_UNSIGNED_INT) && (comps == 1)) {
954 const GLuint *rowA = (const GLuint *) srcRowA;
955 const GLuint *rowB = (const GLuint *) srcRowB;
956 const GLuint *rowC = (const GLuint *) srcRowC;
957 const GLuint *rowD = (const GLuint *) srcRowD;
958 GLfloat *dst = (GLfloat *) dstRow;
959
960 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
961 i++, j += colStride, k += colStride) {
962 const uint64_t tmp = (((uint64_t) rowA[j] + (uint64_t) rowA[k])
963 + ((uint64_t) rowB[j] + (uint64_t) rowB[k])
964 + ((uint64_t) rowC[j] + (uint64_t) rowC[k])
965 + ((uint64_t) rowD[j] + (uint64_t) rowD[k]));
966 dst[i] = (GLfloat)((double) tmp * 0.125);
967 }
968 }
969 else if ((datatype == GL_UNSIGNED_SHORT_5_6_5) && (comps == 3)) {
970 DECLARE_ROW_POINTERS0(GLushort);
971
972 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
973 i++, j += colStride, k += colStride) {
974 const GLint rowAr0 = rowA[j] & 0x1f;
975 const GLint rowAr1 = rowA[k] & 0x1f;
976 const GLint rowBr0 = rowB[j] & 0x1f;
977 const GLint rowBr1 = rowB[k] & 0x1f;
978 const GLint rowCr0 = rowC[j] & 0x1f;
979 const GLint rowCr1 = rowC[k] & 0x1f;
980 const GLint rowDr0 = rowD[j] & 0x1f;
981 const GLint rowDr1 = rowD[k] & 0x1f;
982 const GLint rowAg0 = (rowA[j] >> 5) & 0x3f;
983 const GLint rowAg1 = (rowA[k] >> 5) & 0x3f;
984 const GLint rowBg0 = (rowB[j] >> 5) & 0x3f;
985 const GLint rowBg1 = (rowB[k] >> 5) & 0x3f;
986 const GLint rowCg0 = (rowC[j] >> 5) & 0x3f;
987 const GLint rowCg1 = (rowC[k] >> 5) & 0x3f;
988 const GLint rowDg0 = (rowD[j] >> 5) & 0x3f;
989 const GLint rowDg1 = (rowD[k] >> 5) & 0x3f;
990 const GLint rowAb0 = (rowA[j] >> 11) & 0x1f;
991 const GLint rowAb1 = (rowA[k] >> 11) & 0x1f;
992 const GLint rowBb0 = (rowB[j] >> 11) & 0x1f;
993 const GLint rowBb1 = (rowB[k] >> 11) & 0x1f;
994 const GLint rowCb0 = (rowC[j] >> 11) & 0x1f;
995 const GLint rowCb1 = (rowC[k] >> 11) & 0x1f;
996 const GLint rowDb0 = (rowD[j] >> 11) & 0x1f;
997 const GLint rowDb1 = (rowD[k] >> 11) & 0x1f;
998 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
999 rowCr0, rowCr1, rowDr0, rowDr1);
1000 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1001 rowCg0, rowCg1, rowDg0, rowDg1);
1002 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1003 rowCb0, rowCb1, rowDb0, rowDb1);
1004 dst[i] = (b << 11) | (g << 5) | r;
1005 }
1006 }
1007 else if ((datatype == GL_UNSIGNED_SHORT_4_4_4_4) && (comps == 4)) {
1008 DECLARE_ROW_POINTERS0(GLushort);
1009
1010 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1011 i++, j += colStride, k += colStride) {
1012 const GLint rowAr0 = rowA[j] & 0xf;
1013 const GLint rowAr1 = rowA[k] & 0xf;
1014 const GLint rowBr0 = rowB[j] & 0xf;
1015 const GLint rowBr1 = rowB[k] & 0xf;
1016 const GLint rowCr0 = rowC[j] & 0xf;
1017 const GLint rowCr1 = rowC[k] & 0xf;
1018 const GLint rowDr0 = rowD[j] & 0xf;
1019 const GLint rowDr1 = rowD[k] & 0xf;
1020 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
1021 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
1022 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
1023 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
1024 const GLint rowCg0 = (rowC[j] >> 4) & 0xf;
1025 const GLint rowCg1 = (rowC[k] >> 4) & 0xf;
1026 const GLint rowDg0 = (rowD[j] >> 4) & 0xf;
1027 const GLint rowDg1 = (rowD[k] >> 4) & 0xf;
1028 const GLint rowAb0 = (rowA[j] >> 8) & 0xf;
1029 const GLint rowAb1 = (rowA[k] >> 8) & 0xf;
1030 const GLint rowBb0 = (rowB[j] >> 8) & 0xf;
1031 const GLint rowBb1 = (rowB[k] >> 8) & 0xf;
1032 const GLint rowCb0 = (rowC[j] >> 8) & 0xf;
1033 const GLint rowCb1 = (rowC[k] >> 8) & 0xf;
1034 const GLint rowDb0 = (rowD[j] >> 8) & 0xf;
1035 const GLint rowDb1 = (rowD[k] >> 8) & 0xf;
1036 const GLint rowAa0 = (rowA[j] >> 12) & 0xf;
1037 const GLint rowAa1 = (rowA[k] >> 12) & 0xf;
1038 const GLint rowBa0 = (rowB[j] >> 12) & 0xf;
1039 const GLint rowBa1 = (rowB[k] >> 12) & 0xf;
1040 const GLint rowCa0 = (rowC[j] >> 12) & 0xf;
1041 const GLint rowCa1 = (rowC[k] >> 12) & 0xf;
1042 const GLint rowDa0 = (rowD[j] >> 12) & 0xf;
1043 const GLint rowDa1 = (rowD[k] >> 12) & 0xf;
1044 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1045 rowCr0, rowCr1, rowDr0, rowDr1);
1046 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1047 rowCg0, rowCg1, rowDg0, rowDg1);
1048 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1049 rowCb0, rowCb1, rowDb0, rowDb1);
1050 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
1051 rowCa0, rowCa1, rowDa0, rowDa1);
1052
1053 dst[i] = (a << 12) | (b << 8) | (g << 4) | r;
1054 }
1055 }
1056 else if ((datatype == GL_UNSIGNED_SHORT_1_5_5_5_REV) && (comps == 4)) {
1057 DECLARE_ROW_POINTERS0(GLushort);
1058
1059 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1060 i++, j += colStride, k += colStride) {
1061 const GLint rowAr0 = rowA[j] & 0x1f;
1062 const GLint rowAr1 = rowA[k] & 0x1f;
1063 const GLint rowBr0 = rowB[j] & 0x1f;
1064 const GLint rowBr1 = rowB[k] & 0x1f;
1065 const GLint rowCr0 = rowC[j] & 0x1f;
1066 const GLint rowCr1 = rowC[k] & 0x1f;
1067 const GLint rowDr0 = rowD[j] & 0x1f;
1068 const GLint rowDr1 = rowD[k] & 0x1f;
1069 const GLint rowAg0 = (rowA[j] >> 5) & 0x1f;
1070 const GLint rowAg1 = (rowA[k] >> 5) & 0x1f;
1071 const GLint rowBg0 = (rowB[j] >> 5) & 0x1f;
1072 const GLint rowBg1 = (rowB[k] >> 5) & 0x1f;
1073 const GLint rowCg0 = (rowC[j] >> 5) & 0x1f;
1074 const GLint rowCg1 = (rowC[k] >> 5) & 0x1f;
1075 const GLint rowDg0 = (rowD[j] >> 5) & 0x1f;
1076 const GLint rowDg1 = (rowD[k] >> 5) & 0x1f;
1077 const GLint rowAb0 = (rowA[j] >> 10) & 0x1f;
1078 const GLint rowAb1 = (rowA[k] >> 10) & 0x1f;
1079 const GLint rowBb0 = (rowB[j] >> 10) & 0x1f;
1080 const GLint rowBb1 = (rowB[k] >> 10) & 0x1f;
1081 const GLint rowCb0 = (rowC[j] >> 10) & 0x1f;
1082 const GLint rowCb1 = (rowC[k] >> 10) & 0x1f;
1083 const GLint rowDb0 = (rowD[j] >> 10) & 0x1f;
1084 const GLint rowDb1 = (rowD[k] >> 10) & 0x1f;
1085 const GLint rowAa0 = (rowA[j] >> 15) & 0x1;
1086 const GLint rowAa1 = (rowA[k] >> 15) & 0x1;
1087 const GLint rowBa0 = (rowB[j] >> 15) & 0x1;
1088 const GLint rowBa1 = (rowB[k] >> 15) & 0x1;
1089 const GLint rowCa0 = (rowC[j] >> 15) & 0x1;
1090 const GLint rowCa1 = (rowC[k] >> 15) & 0x1;
1091 const GLint rowDa0 = (rowD[j] >> 15) & 0x1;
1092 const GLint rowDa1 = (rowD[k] >> 15) & 0x1;
1093 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1094 rowCr0, rowCr1, rowDr0, rowDr1);
1095 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1096 rowCg0, rowCg1, rowDg0, rowDg1);
1097 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1098 rowCb0, rowCb1, rowDb0, rowDb1);
1099 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
1100 rowCa0, rowCa1, rowDa0, rowDa1);
1101
1102 dst[i] = (a << 15) | (b << 10) | (g << 5) | r;
1103 }
1104 }
1105 else if ((datatype == GL_UNSIGNED_SHORT_5_5_5_1) && (comps == 4)) {
1106 DECLARE_ROW_POINTERS0(GLushort);
1107
1108 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1109 i++, j += colStride, k += colStride) {
1110 const GLint rowAr0 = (rowA[j] >> 11) & 0x1f;
1111 const GLint rowAr1 = (rowA[k] >> 11) & 0x1f;
1112 const GLint rowBr0 = (rowB[j] >> 11) & 0x1f;
1113 const GLint rowBr1 = (rowB[k] >> 11) & 0x1f;
1114 const GLint rowCr0 = (rowC[j] >> 11) & 0x1f;
1115 const GLint rowCr1 = (rowC[k] >> 11) & 0x1f;
1116 const GLint rowDr0 = (rowD[j] >> 11) & 0x1f;
1117 const GLint rowDr1 = (rowD[k] >> 11) & 0x1f;
1118 const GLint rowAg0 = (rowA[j] >> 6) & 0x1f;
1119 const GLint rowAg1 = (rowA[k] >> 6) & 0x1f;
1120 const GLint rowBg0 = (rowB[j] >> 6) & 0x1f;
1121 const GLint rowBg1 = (rowB[k] >> 6) & 0x1f;
1122 const GLint rowCg0 = (rowC[j] >> 6) & 0x1f;
1123 const GLint rowCg1 = (rowC[k] >> 6) & 0x1f;
1124 const GLint rowDg0 = (rowD[j] >> 6) & 0x1f;
1125 const GLint rowDg1 = (rowD[k] >> 6) & 0x1f;
1126 const GLint rowAb0 = (rowA[j] >> 1) & 0x1f;
1127 const GLint rowAb1 = (rowA[k] >> 1) & 0x1f;
1128 const GLint rowBb0 = (rowB[j] >> 1) & 0x1f;
1129 const GLint rowBb1 = (rowB[k] >> 1) & 0x1f;
1130 const GLint rowCb0 = (rowC[j] >> 1) & 0x1f;
1131 const GLint rowCb1 = (rowC[k] >> 1) & 0x1f;
1132 const GLint rowDb0 = (rowD[j] >> 1) & 0x1f;
1133 const GLint rowDb1 = (rowD[k] >> 1) & 0x1f;
1134 const GLint rowAa0 = (rowA[j] & 0x1);
1135 const GLint rowAa1 = (rowA[k] & 0x1);
1136 const GLint rowBa0 = (rowB[j] & 0x1);
1137 const GLint rowBa1 = (rowB[k] & 0x1);
1138 const GLint rowCa0 = (rowC[j] & 0x1);
1139 const GLint rowCa1 = (rowC[k] & 0x1);
1140 const GLint rowDa0 = (rowD[j] & 0x1);
1141 const GLint rowDa1 = (rowD[k] & 0x1);
1142 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1143 rowCr0, rowCr1, rowDr0, rowDr1);
1144 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1145 rowCg0, rowCg1, rowDg0, rowDg1);
1146 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1147 rowCb0, rowCb1, rowDb0, rowDb1);
1148 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
1149 rowCa0, rowCa1, rowDa0, rowDa1);
1150
1151 dst[i] = (r << 11) | (g << 6) | (b << 1) | a;
1152 }
1153 }
1154 else if ((datatype == GL_UNSIGNED_BYTE_3_3_2) && (comps == 3)) {
1155 DECLARE_ROW_POINTERS0(GLubyte);
1156
1157 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1158 i++, j += colStride, k += colStride) {
1159 const GLint rowAr0 = rowA[j] & 0x3;
1160 const GLint rowAr1 = rowA[k] & 0x3;
1161 const GLint rowBr0 = rowB[j] & 0x3;
1162 const GLint rowBr1 = rowB[k] & 0x3;
1163 const GLint rowCr0 = rowC[j] & 0x3;
1164 const GLint rowCr1 = rowC[k] & 0x3;
1165 const GLint rowDr0 = rowD[j] & 0x3;
1166 const GLint rowDr1 = rowD[k] & 0x3;
1167 const GLint rowAg0 = (rowA[j] >> 2) & 0x7;
1168 const GLint rowAg1 = (rowA[k] >> 2) & 0x7;
1169 const GLint rowBg0 = (rowB[j] >> 2) & 0x7;
1170 const GLint rowBg1 = (rowB[k] >> 2) & 0x7;
1171 const GLint rowCg0 = (rowC[j] >> 2) & 0x7;
1172 const GLint rowCg1 = (rowC[k] >> 2) & 0x7;
1173 const GLint rowDg0 = (rowD[j] >> 2) & 0x7;
1174 const GLint rowDg1 = (rowD[k] >> 2) & 0x7;
1175 const GLint rowAb0 = (rowA[j] >> 5) & 0x7;
1176 const GLint rowAb1 = (rowA[k] >> 5) & 0x7;
1177 const GLint rowBb0 = (rowB[j] >> 5) & 0x7;
1178 const GLint rowBb1 = (rowB[k] >> 5) & 0x7;
1179 const GLint rowCb0 = (rowC[j] >> 5) & 0x7;
1180 const GLint rowCb1 = (rowC[k] >> 5) & 0x7;
1181 const GLint rowDb0 = (rowD[j] >> 5) & 0x7;
1182 const GLint rowDb1 = (rowD[k] >> 5) & 0x7;
1183 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1184 rowCr0, rowCr1, rowDr0, rowDr1);
1185 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1186 rowCg0, rowCg1, rowDg0, rowDg1);
1187 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1188 rowCb0, rowCb1, rowDb0, rowDb1);
1189 dst[i] = (b << 5) | (g << 2) | r;
1190 }
1191 }
1192 else if (datatype == MESA_UNSIGNED_BYTE_4_4 && comps == 2) {
1193 DECLARE_ROW_POINTERS0(GLubyte);
1194
1195 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1196 i++, j += colStride, k += colStride) {
1197 const GLint rowAr0 = rowA[j] & 0xf;
1198 const GLint rowAr1 = rowA[k] & 0xf;
1199 const GLint rowBr0 = rowB[j] & 0xf;
1200 const GLint rowBr1 = rowB[k] & 0xf;
1201 const GLint rowCr0 = rowC[j] & 0xf;
1202 const GLint rowCr1 = rowC[k] & 0xf;
1203 const GLint rowDr0 = rowD[j] & 0xf;
1204 const GLint rowDr1 = rowD[k] & 0xf;
1205 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
1206 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
1207 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
1208 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
1209 const GLint rowCg0 = (rowC[j] >> 4) & 0xf;
1210 const GLint rowCg1 = (rowC[k] >> 4) & 0xf;
1211 const GLint rowDg0 = (rowD[j] >> 4) & 0xf;
1212 const GLint rowDg1 = (rowD[k] >> 4) & 0xf;
1213 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1214 rowCr0, rowCr1, rowDr0, rowDr1);
1215 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1216 rowCg0, rowCg1, rowDg0, rowDg1);
1217 dst[i] = (g << 4) | r;
1218 }
1219 }
1220 else if ((datatype == GL_UNSIGNED_INT_2_10_10_10_REV) && (comps == 4)) {
1221 DECLARE_ROW_POINTERS0(GLuint);
1222
1223 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1224 i++, j += colStride, k += colStride) {
1225 const GLint rowAr0 = rowA[j] & 0x3ff;
1226 const GLint rowAr1 = rowA[k] & 0x3ff;
1227 const GLint rowBr0 = rowB[j] & 0x3ff;
1228 const GLint rowBr1 = rowB[k] & 0x3ff;
1229 const GLint rowCr0 = rowC[j] & 0x3ff;
1230 const GLint rowCr1 = rowC[k] & 0x3ff;
1231 const GLint rowDr0 = rowD[j] & 0x3ff;
1232 const GLint rowDr1 = rowD[k] & 0x3ff;
1233 const GLint rowAg0 = (rowA[j] >> 10) & 0x3ff;
1234 const GLint rowAg1 = (rowA[k] >> 10) & 0x3ff;
1235 const GLint rowBg0 = (rowB[j] >> 10) & 0x3ff;
1236 const GLint rowBg1 = (rowB[k] >> 10) & 0x3ff;
1237 const GLint rowCg0 = (rowC[j] >> 10) & 0x3ff;
1238 const GLint rowCg1 = (rowC[k] >> 10) & 0x3ff;
1239 const GLint rowDg0 = (rowD[j] >> 10) & 0x3ff;
1240 const GLint rowDg1 = (rowD[k] >> 10) & 0x3ff;
1241 const GLint rowAb0 = (rowA[j] >> 20) & 0x3ff;
1242 const GLint rowAb1 = (rowA[k] >> 20) & 0x3ff;
1243 const GLint rowBb0 = (rowB[j] >> 20) & 0x3ff;
1244 const GLint rowBb1 = (rowB[k] >> 20) & 0x3ff;
1245 const GLint rowCb0 = (rowC[j] >> 20) & 0x3ff;
1246 const GLint rowCb1 = (rowC[k] >> 20) & 0x3ff;
1247 const GLint rowDb0 = (rowD[j] >> 20) & 0x3ff;
1248 const GLint rowDb1 = (rowD[k] >> 20) & 0x3ff;
1249 const GLint rowAa0 = (rowA[j] >> 30) & 0x3;
1250 const GLint rowAa1 = (rowA[k] >> 30) & 0x3;
1251 const GLint rowBa0 = (rowB[j] >> 30) & 0x3;
1252 const GLint rowBa1 = (rowB[k] >> 30) & 0x3;
1253 const GLint rowCa0 = (rowC[j] >> 30) & 0x3;
1254 const GLint rowCa1 = (rowC[k] >> 30) & 0x3;
1255 const GLint rowDa0 = (rowD[j] >> 30) & 0x3;
1256 const GLint rowDa1 = (rowD[k] >> 30) & 0x3;
1257 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1258 rowCr0, rowCr1, rowDr0, rowDr1);
1259 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1260 rowCg0, rowCg1, rowDg0, rowDg1);
1261 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1262 rowCb0, rowCb1, rowDb0, rowDb1);
1263 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
1264 rowCa0, rowCa1, rowDa0, rowDa1);
1265
1266 dst[i] = (a << 30) | (b << 20) | (g << 10) | r;
1267 }
1268 }
1269
1270 else if (datatype == GL_UNSIGNED_INT_5_9_9_9_REV && comps == 3) {
1271 DECLARE_ROW_POINTERS0(GLuint);
1272
1273 GLfloat res[3];
1274 GLfloat rowAj[3], rowBj[3], rowCj[3], rowDj[3];
1275 GLfloat rowAk[3], rowBk[3], rowCk[3], rowDk[3];
1276
1277 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1278 i++, j += colStride, k += colStride) {
1279 rgb9e5_to_float3(rowA[j], rowAj);
1280 rgb9e5_to_float3(rowB[j], rowBj);
1281 rgb9e5_to_float3(rowC[j], rowCj);
1282 rgb9e5_to_float3(rowD[j], rowDj);
1283 rgb9e5_to_float3(rowA[k], rowAk);
1284 rgb9e5_to_float3(rowB[k], rowBk);
1285 rgb9e5_to_float3(rowC[k], rowCk);
1286 rgb9e5_to_float3(rowD[k], rowDk);
1287 res[0] = (rowAj[0] + rowAk[0] + rowBj[0] + rowBk[0] +
1288 rowCj[0] + rowCk[0] + rowDj[0] + rowDk[0]) * 0.125F;
1289 res[1] = (rowAj[1] + rowAk[1] + rowBj[1] + rowBk[1] +
1290 rowCj[1] + rowCk[1] + rowDj[1] + rowDk[1]) * 0.125F;
1291 res[2] = (rowAj[2] + rowAk[2] + rowBj[2] + rowBk[2] +
1292 rowCj[2] + rowCk[2] + rowDj[2] + rowDk[2]) * 0.125F;
1293 dst[i] = float3_to_rgb9e5(res);
1294 }
1295 }
1296
1297 else {
1298 _mesa_problem(NULL, "bad format in do_row()");
1299 }
1300 }
1301
1302
1303 /*
1304 * These functions generate a 1/2-size mipmap image from a source image.
1305 * Texture borders are handled by copying or averaging the source image's
1306 * border texels, depending on the scale-down factor.
1307 */
1308
1309 static void
1310 make_1d_mipmap(GLenum datatype, GLuint comps, GLint border,
1311 GLint srcWidth, const GLubyte *srcPtr,
1312 GLint dstWidth, GLubyte *dstPtr)
1313 {
1314 const GLint bpt = bytes_per_pixel(datatype, comps);
1315 const GLubyte *src;
1316 GLubyte *dst;
1317
1318 /* skip the border pixel, if any */
1319 src = srcPtr + border * bpt;
1320 dst = dstPtr + border * bpt;
1321
1322 /* we just duplicate the input row, kind of hack, saves code */
1323 do_row(datatype, comps, srcWidth - 2 * border, src, src,
1324 dstWidth - 2 * border, dst);
1325
1326 if (border) {
1327 /* copy left-most pixel from source */
1328 assert(dstPtr);
1329 assert(srcPtr);
1330 memcpy(dstPtr, srcPtr, bpt);
1331 /* copy right-most pixel from source */
1332 memcpy(dstPtr + (dstWidth - 1) * bpt,
1333 srcPtr + (srcWidth - 1) * bpt,
1334 bpt);
1335 }
1336 }
1337
1338
1339 static void
1340 make_2d_mipmap(GLenum datatype, GLuint comps, GLint border,
1341 GLint srcWidth, GLint srcHeight,
1342 const GLubyte *srcPtr, GLint srcRowStride,
1343 GLint dstWidth, GLint dstHeight,
1344 GLubyte *dstPtr, GLint dstRowStride)
1345 {
1346 const GLint bpt = bytes_per_pixel(datatype, comps);
1347 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1348 const GLint dstWidthNB = dstWidth - 2 * border;
1349 const GLint dstHeightNB = dstHeight - 2 * border;
1350 const GLint srcRowBytes = bpt * srcRowStride;
1351 const GLint dstRowBytes = bpt * dstRowStride;
1352 const GLubyte *srcA, *srcB;
1353 GLubyte *dst;
1354 GLint row, srcRowStep;
1355
1356 /* Compute src and dst pointers, skipping any border */
1357 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
1358 if (srcHeight > 1 && srcHeight > dstHeight) {
1359 /* sample from two source rows */
1360 srcB = srcA + srcRowBytes;
1361 srcRowStep = 2;
1362 }
1363 else {
1364 /* sample from one source row */
1365 srcB = srcA;
1366 srcRowStep = 1;
1367 }
1368
1369 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1370
1371 for (row = 0; row < dstHeightNB; row++) {
1372 do_row(datatype, comps, srcWidthNB, srcA, srcB,
1373 dstWidthNB, dst);
1374 srcA += srcRowStep * srcRowBytes;
1375 srcB += srcRowStep * srcRowBytes;
1376 dst += dstRowBytes;
1377 }
1378
1379 /* This is ugly but probably won't be used much */
1380 if (border > 0) {
1381 /* fill in dest border */
1382 /* lower-left border pixel */
1383 assert(dstPtr);
1384 assert(srcPtr);
1385 memcpy(dstPtr, srcPtr, bpt);
1386 /* lower-right border pixel */
1387 memcpy(dstPtr + (dstWidth - 1) * bpt,
1388 srcPtr + (srcWidth - 1) * bpt, bpt);
1389 /* upper-left border pixel */
1390 memcpy(dstPtr + dstWidth * (dstHeight - 1) * bpt,
1391 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
1392 /* upper-right border pixel */
1393 memcpy(dstPtr + (dstWidth * dstHeight - 1) * bpt,
1394 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
1395 /* lower border */
1396 do_row(datatype, comps, srcWidthNB,
1397 srcPtr + bpt,
1398 srcPtr + bpt,
1399 dstWidthNB, dstPtr + bpt);
1400 /* upper border */
1401 do_row(datatype, comps, srcWidthNB,
1402 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1403 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1404 dstWidthNB,
1405 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
1406 /* left and right borders */
1407 if (srcHeight == dstHeight) {
1408 /* copy border pixel from src to dst */
1409 for (row = 1; row < srcHeight; row++) {
1410 memcpy(dstPtr + dstWidth * row * bpt,
1411 srcPtr + srcWidth * row * bpt, bpt);
1412 memcpy(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
1413 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
1414 }
1415 }
1416 else {
1417 /* average two src pixels each dest pixel */
1418 for (row = 0; row < dstHeightNB; row += 2) {
1419 do_row(datatype, comps, 1,
1420 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
1421 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
1422 1, dstPtr + (dstWidth * row + 1) * bpt);
1423 do_row(datatype, comps, 1,
1424 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
1425 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
1426 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
1427 }
1428 }
1429 }
1430 }
1431
1432
1433 static void
1434 make_3d_mipmap(GLenum datatype, GLuint comps, GLint border,
1435 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1436 const GLubyte *srcPtr, GLint srcRowStride,
1437 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1438 GLubyte *dstPtr, GLint dstRowStride)
1439 {
1440 const GLint bpt = bytes_per_pixel(datatype, comps);
1441 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1442 const GLint srcDepthNB = srcDepth - 2 * border;
1443 const GLint dstWidthNB = dstWidth - 2 * border;
1444 const GLint dstHeightNB = dstHeight - 2 * border;
1445 const GLint dstDepthNB = dstDepth - 2 * border;
1446 GLint img, row;
1447 GLint bytesPerSrcImage, bytesPerDstImage;
1448 GLint bytesPerSrcRow, bytesPerDstRow;
1449 GLint srcImageOffset, srcRowOffset;
1450
1451 (void) srcDepthNB; /* silence warnings */
1452
1453
1454 bytesPerSrcImage = srcWidth * srcHeight * bpt;
1455 bytesPerDstImage = dstWidth * dstHeight * bpt;
1456
1457 bytesPerSrcRow = srcWidth * bpt;
1458 bytesPerDstRow = dstWidth * bpt;
1459
1460 /* Offset between adjacent src images to be averaged together */
1461 srcImageOffset = (srcDepth == dstDepth) ? 0 : bytesPerSrcImage;
1462
1463 /* Offset between adjacent src rows to be averaged together */
1464 srcRowOffset = (srcHeight == dstHeight) ? 0 : srcWidth * bpt;
1465
1466 /*
1467 * Need to average together up to 8 src pixels for each dest pixel.
1468 * Break that down into 3 operations:
1469 * 1. take two rows from source image and average them together.
1470 * 2. take two rows from next source image and average them together.
1471 * 3. take the two averaged rows and average them for the final dst row.
1472 */
1473
1474 /*
1475 printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1476 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1477 */
1478
1479 for (img = 0; img < dstDepthNB; img++) {
1480 /* first source image pointer, skipping border */
1481 const GLubyte *imgSrcA = srcPtr
1482 + (bytesPerSrcImage + bytesPerSrcRow + border) * bpt * border
1483 + img * (bytesPerSrcImage + srcImageOffset);
1484 /* second source image pointer, skipping border */
1485 const GLubyte *imgSrcB = imgSrcA + srcImageOffset;
1486 /* address of the dest image, skipping border */
1487 GLubyte *imgDst = dstPtr
1488 + (bytesPerDstImage + bytesPerDstRow + border) * bpt * border
1489 + img * bytesPerDstImage;
1490
1491 /* setup the four source row pointers and the dest row pointer */
1492 const GLubyte *srcImgARowA = imgSrcA;
1493 const GLubyte *srcImgARowB = imgSrcA + srcRowOffset;
1494 const GLubyte *srcImgBRowA = imgSrcB;
1495 const GLubyte *srcImgBRowB = imgSrcB + srcRowOffset;
1496 GLubyte *dstImgRow = imgDst;
1497
1498 for (row = 0; row < dstHeightNB; row++) {
1499 do_row_3D(datatype, comps, srcWidthNB,
1500 srcImgARowA, srcImgARowB,
1501 srcImgBRowA, srcImgBRowB,
1502 dstWidthNB, dstImgRow);
1503
1504 /* advance to next rows */
1505 srcImgARowA += bytesPerSrcRow + srcRowOffset;
1506 srcImgARowB += bytesPerSrcRow + srcRowOffset;
1507 srcImgBRowA += bytesPerSrcRow + srcRowOffset;
1508 srcImgBRowB += bytesPerSrcRow + srcRowOffset;
1509 dstImgRow += bytesPerDstRow;
1510 }
1511 }
1512
1513
1514 /* Luckily we can leverage the make_2d_mipmap() function here! */
1515 if (border > 0) {
1516 /* do front border image */
1517 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight, srcPtr, srcRowStride,
1518 dstWidth, dstHeight, dstPtr, dstRowStride);
1519 /* do back border image */
1520 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight,
1521 srcPtr + bytesPerSrcImage * (srcDepth - 1), srcRowStride,
1522 dstWidth, dstHeight,
1523 dstPtr + bytesPerDstImage * (dstDepth - 1), dstRowStride);
1524 /* do four remaining border edges that span the image slices */
1525 if (srcDepth == dstDepth) {
1526 /* just copy border pixels from src to dst */
1527 for (img = 0; img < dstDepthNB; img++) {
1528 const GLubyte *src;
1529 GLubyte *dst;
1530
1531 /* do border along [img][row=0][col=0] */
1532 src = srcPtr + (img + 1) * bytesPerSrcImage;
1533 dst = dstPtr + (img + 1) * bytesPerDstImage;
1534 memcpy(dst, src, bpt);
1535
1536 /* do border along [img][row=dstHeight-1][col=0] */
1537 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1538 + (srcHeight - 1) * bytesPerSrcRow;
1539 dst = dstPtr + (img + 1) * bytesPerDstImage
1540 + (dstHeight - 1) * bytesPerDstRow;
1541 memcpy(dst, src, bpt);
1542
1543 /* do border along [img][row=0][col=dstWidth-1] */
1544 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1545 + (srcWidth - 1) * bpt;
1546 dst = dstPtr + (img + 1) * bytesPerDstImage
1547 + (dstWidth - 1) * bpt;
1548 memcpy(dst, src, bpt);
1549
1550 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1551 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1552 + (bytesPerSrcImage - bpt);
1553 dst = dstPtr + (img + 1) * bytesPerDstImage
1554 + (bytesPerDstImage - bpt);
1555 memcpy(dst, src, bpt);
1556 }
1557 }
1558 else {
1559 /* average border pixels from adjacent src image pairs */
1560 ASSERT(srcDepthNB == 2 * dstDepthNB);
1561 for (img = 0; img < dstDepthNB; img++) {
1562 const GLubyte *src;
1563 GLubyte *dst;
1564
1565 /* do border along [img][row=0][col=0] */
1566 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage;
1567 dst = dstPtr + (img + 1) * bytesPerDstImage;
1568 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1569
1570 /* do border along [img][row=dstHeight-1][col=0] */
1571 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1572 + (srcHeight - 1) * bytesPerSrcRow;
1573 dst = dstPtr + (img + 1) * bytesPerDstImage
1574 + (dstHeight - 1) * bytesPerDstRow;
1575 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1576
1577 /* do border along [img][row=0][col=dstWidth-1] */
1578 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1579 + (srcWidth - 1) * bpt;
1580 dst = dstPtr + (img + 1) * bytesPerDstImage
1581 + (dstWidth - 1) * bpt;
1582 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1583
1584 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1585 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1586 + (bytesPerSrcImage - bpt);
1587 dst = dstPtr + (img + 1) * bytesPerDstImage
1588 + (bytesPerDstImage - bpt);
1589 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1590 }
1591 }
1592 }
1593 }
1594
1595
1596 static void
1597 make_1d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
1598 GLint srcWidth, const GLubyte *srcPtr, GLuint srcRowStride,
1599 GLint dstWidth, GLint dstHeight,
1600 GLubyte *dstPtr, GLuint dstRowStride )
1601 {
1602 const GLint bpt = bytes_per_pixel(datatype, comps);
1603 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1604 const GLint dstWidthNB = dstWidth - 2 * border;
1605 const GLint dstHeightNB = dstHeight - 2 * border;
1606 const GLint srcRowBytes = bpt * srcRowStride;
1607 const GLint dstRowBytes = bpt * dstRowStride;
1608 const GLubyte *src;
1609 GLubyte *dst;
1610 GLint row;
1611
1612 /* Compute src and dst pointers, skipping any border */
1613 src = srcPtr + border * ((srcWidth + 1) * bpt);
1614 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1615
1616 for (row = 0; row < dstHeightNB; row++) {
1617 do_row(datatype, comps, srcWidthNB, src, src,
1618 dstWidthNB, dst);
1619 src += srcRowBytes;
1620 dst += dstRowBytes;
1621 }
1622
1623 if (border) {
1624 /* copy left-most pixel from source */
1625 assert(dstPtr);
1626 assert(srcPtr);
1627 memcpy(dstPtr, srcPtr, bpt);
1628 /* copy right-most pixel from source */
1629 memcpy(dstPtr + (dstWidth - 1) * bpt,
1630 srcPtr + (srcWidth - 1) * bpt,
1631 bpt);
1632 }
1633 }
1634
1635
1636 /**
1637 * \bug
1638 * There is quite a bit of refactoring that could be done with this function
1639 * and \c make_2d_mipmap.
1640 */
1641 static void
1642 make_2d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
1643 GLint srcWidth, GLint srcHeight,
1644 const GLubyte *srcPtr, GLint srcRowStride,
1645 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1646 GLubyte *dstPtr, GLint dstRowStride)
1647 {
1648 const GLint bpt = bytes_per_pixel(datatype, comps);
1649 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1650 const GLint dstWidthNB = dstWidth - 2 * border;
1651 const GLint dstHeightNB = dstHeight - 2 * border;
1652 const GLint dstDepthNB = dstDepth - 2 * border;
1653 const GLint srcRowBytes = bpt * srcRowStride;
1654 const GLint dstRowBytes = bpt * dstRowStride;
1655 const GLubyte *srcA, *srcB;
1656 GLubyte *dst;
1657 GLint layer;
1658 GLint row;
1659
1660 /* Compute src and dst pointers, skipping any border */
1661 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
1662 if (srcHeight > 1)
1663 srcB = srcA + srcRowBytes;
1664 else
1665 srcB = srcA;
1666 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1667
1668 for (layer = 0; layer < dstDepthNB; layer++) {
1669 for (row = 0; row < dstHeightNB; row++) {
1670 do_row(datatype, comps, srcWidthNB, srcA, srcB,
1671 dstWidthNB, dst);
1672 srcA += 2 * srcRowBytes;
1673 srcB += 2 * srcRowBytes;
1674 dst += dstRowBytes;
1675 }
1676
1677 /* This is ugly but probably won't be used much */
1678 if (border > 0) {
1679 /* fill in dest border */
1680 /* lower-left border pixel */
1681 assert(dstPtr);
1682 assert(srcPtr);
1683 memcpy(dstPtr, srcPtr, bpt);
1684 /* lower-right border pixel */
1685 memcpy(dstPtr + (dstWidth - 1) * bpt,
1686 srcPtr + (srcWidth - 1) * bpt, bpt);
1687 /* upper-left border pixel */
1688 memcpy(dstPtr + dstWidth * (dstHeight - 1) * bpt,
1689 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
1690 /* upper-right border pixel */
1691 memcpy(dstPtr + (dstWidth * dstHeight - 1) * bpt,
1692 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
1693 /* lower border */
1694 do_row(datatype, comps, srcWidthNB,
1695 srcPtr + bpt,
1696 srcPtr + bpt,
1697 dstWidthNB, dstPtr + bpt);
1698 /* upper border */
1699 do_row(datatype, comps, srcWidthNB,
1700 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1701 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1702 dstWidthNB,
1703 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
1704 /* left and right borders */
1705 if (srcHeight == dstHeight) {
1706 /* copy border pixel from src to dst */
1707 for (row = 1; row < srcHeight; row++) {
1708 memcpy(dstPtr + dstWidth * row * bpt,
1709 srcPtr + srcWidth * row * bpt, bpt);
1710 memcpy(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
1711 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
1712 }
1713 }
1714 else {
1715 /* average two src pixels each dest pixel */
1716 for (row = 0; row < dstHeightNB; row += 2) {
1717 do_row(datatype, comps, 1,
1718 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
1719 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
1720 1, dstPtr + (dstWidth * row + 1) * bpt);
1721 do_row(datatype, comps, 1,
1722 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
1723 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
1724 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
1725 }
1726 }
1727 }
1728 }
1729 }
1730
1731
1732 /**
1733 * Down-sample a texture image to produce the next lower mipmap level.
1734 * \param comps components per texel (1, 2, 3 or 4)
1735 * \param srcRowStride stride between source rows, in texels
1736 * \param dstRowStride stride between destination rows, in texels
1737 */
1738 void
1739 _mesa_generate_mipmap_level(GLenum target,
1740 GLenum datatype, GLuint comps,
1741 GLint border,
1742 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1743 const GLubyte *srcData,
1744 GLint srcRowStride,
1745 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1746 GLubyte *dstData,
1747 GLint dstRowStride)
1748 {
1749 /*
1750 * We use simple 2x2 averaging to compute the next mipmap level.
1751 */
1752 switch (target) {
1753 case GL_TEXTURE_1D:
1754 make_1d_mipmap(datatype, comps, border,
1755 srcWidth, srcData,
1756 dstWidth, dstData);
1757 break;
1758 case GL_TEXTURE_2D:
1759 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
1760 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
1761 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
1762 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
1763 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
1764 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
1765 make_2d_mipmap(datatype, comps, border,
1766 srcWidth, srcHeight, srcData, srcRowStride,
1767 dstWidth, dstHeight, dstData, dstRowStride);
1768 break;
1769 case GL_TEXTURE_3D:
1770 make_3d_mipmap(datatype, comps, border,
1771 srcWidth, srcHeight, srcDepth,
1772 srcData, srcRowStride,
1773 dstWidth, dstHeight, dstDepth,
1774 dstData, dstRowStride);
1775 break;
1776 case GL_TEXTURE_1D_ARRAY_EXT:
1777 make_1d_stack_mipmap(datatype, comps, border,
1778 srcWidth, srcData, srcRowStride,
1779 dstWidth, dstHeight,
1780 dstData, dstRowStride);
1781 break;
1782 case GL_TEXTURE_2D_ARRAY_EXT:
1783 make_2d_stack_mipmap(datatype, comps, border,
1784 srcWidth, srcHeight,
1785 srcData, srcRowStride,
1786 dstWidth, dstHeight,
1787 dstDepth, dstData, dstRowStride);
1788 break;
1789 case GL_TEXTURE_RECTANGLE_NV:
1790 /* no mipmaps, do nothing */
1791 break;
1792 default:
1793 _mesa_problem(NULL, "bad dimensions in _mesa_generate_mipmaps");
1794 return;
1795 }
1796 }
1797
1798
1799 /**
1800 * compute next (level+1) image size
1801 * \return GL_FALSE if no smaller size can be generated (eg. src is 1x1x1 size)
1802 */
1803 static GLboolean
1804 next_mipmap_level_size(GLenum target, GLint border,
1805 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1806 GLint *dstWidth, GLint *dstHeight, GLint *dstDepth)
1807 {
1808 if (srcWidth - 2 * border > 1) {
1809 *dstWidth = (srcWidth - 2 * border) / 2 + 2 * border;
1810 }
1811 else {
1812 *dstWidth = srcWidth; /* can't go smaller */
1813 }
1814
1815 if ((srcHeight - 2 * border > 1) &&
1816 (target != GL_TEXTURE_1D_ARRAY_EXT)) {
1817 *dstHeight = (srcHeight - 2 * border) / 2 + 2 * border;
1818 }
1819 else {
1820 *dstHeight = srcHeight; /* can't go smaller */
1821 }
1822
1823 if ((srcDepth - 2 * border > 1) &&
1824 (target != GL_TEXTURE_2D_ARRAY_EXT)) {
1825 *dstDepth = (srcDepth - 2 * border) / 2 + 2 * border;
1826 }
1827 else {
1828 *dstDepth = srcDepth; /* can't go smaller */
1829 }
1830
1831 if (*dstWidth == srcWidth &&
1832 *dstHeight == srcHeight &&
1833 *dstDepth == srcDepth) {
1834 return GL_FALSE;
1835 }
1836 else {
1837 return GL_TRUE;
1838 }
1839 }
1840
1841
1842
1843
1844 /**
1845 * Automatic mipmap generation.
1846 * This is the fallback/default function for ctx->Driver.GenerateMipmap().
1847 * Generate a complete set of mipmaps from texObj's BaseLevel image.
1848 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
1849 * For cube maps, target will be one of
1850 * GL_TEXTURE_CUBE_MAP_POSITIVE/NEGATIVE_X/Y/Z; never GL_TEXTURE_CUBE_MAP.
1851 */
1852 void
1853 _mesa_generate_mipmap(struct gl_context *ctx, GLenum target,
1854 struct gl_texture_object *texObj)
1855 {
1856 const struct gl_texture_image *srcImage;
1857 gl_format convertFormat;
1858 const GLubyte *srcData = NULL;
1859 GLubyte *dstData = NULL;
1860 GLint level, maxLevels;
1861 GLenum datatype;
1862 GLuint comps;
1863
1864 ASSERT(texObj);
1865 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
1866 ASSERT(srcImage);
1867
1868 maxLevels = _mesa_max_texture_levels(ctx, texObj->Target);
1869 ASSERT(maxLevels > 0); /* bad target */
1870
1871 /* Find convertFormat - the format that do_row() will process */
1872
1873 if (_mesa_is_format_compressed(srcImage->TexFormat)) {
1874 /* setup for compressed textures - need to allocate temporary
1875 * image buffers to hold uncompressed images.
1876 */
1877 GLuint row;
1878 GLint components, size;
1879 GLchan *dst;
1880
1881 assert(texObj->Target == GL_TEXTURE_2D ||
1882 texObj->Target == GL_TEXTURE_CUBE_MAP_ARB);
1883
1884 if (srcImage->_BaseFormat == GL_RGB) {
1885 convertFormat = MESA_FORMAT_RGB888;
1886 components = 3;
1887 } else if (srcImage->_BaseFormat == GL_RED) {
1888 convertFormat = MESA_FORMAT_R8;
1889 components = 1;
1890 } else if (srcImage->_BaseFormat == GL_RG) {
1891 convertFormat = MESA_FORMAT_RG88;
1892 components = 2;
1893 } else if (srcImage->_BaseFormat == GL_RGBA) {
1894 convertFormat = MESA_FORMAT_RGBA8888;
1895 components = 4;
1896 } else if (srcImage->_BaseFormat == GL_LUMINANCE) {
1897 convertFormat = MESA_FORMAT_L8;
1898 components = 1;
1899 } else if (srcImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
1900 convertFormat = MESA_FORMAT_AL88;
1901 components = 2;
1902 } else {
1903 _mesa_problem(ctx, "bad srcImage->_BaseFormat in _mesa_generate_mipmaps");
1904 return;
1905 }
1906
1907 /* allocate storage for uncompressed GL_RGB or GL_RGBA images */
1908 size = _mesa_bytes_per_pixel(srcImage->_BaseFormat, CHAN_TYPE)
1909 * srcImage->Width * srcImage->Height * srcImage->Depth + 20;
1910 /* 20 extra bytes, just be safe when calling last FetchTexel */
1911 srcData = (GLubyte *) malloc(size);
1912 if (!srcData) {
1913 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1914 return;
1915 }
1916 dstData = (GLubyte *) malloc(size / 2); /* 1/4 would probably be OK */
1917 if (!dstData) {
1918 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1919 free((void *) srcData);
1920 return;
1921 }
1922
1923 /* decompress base image here */
1924 dst = (GLchan *) srcData;
1925 for (row = 0; row < srcImage->Height; row++) {
1926 GLuint col;
1927 for (col = 0; col < srcImage->Width; col++) {
1928 srcImage->FetchTexelc(srcImage, col, row, 0, dst);
1929 dst += components;
1930 }
1931 }
1932 }
1933 else {
1934 /* uncompressed */
1935 convertFormat = srcImage->TexFormat;
1936 }
1937
1938 _mesa_format_to_type_and_comps(convertFormat, &datatype, &comps);
1939
1940 for (level = texObj->BaseLevel; level < texObj->MaxLevel
1941 && level < maxLevels - 1; level++) {
1942 /* generate image[level+1] from image[level] */
1943 const struct gl_texture_image *srcImage;
1944 struct gl_texture_image *dstImage;
1945 GLint srcWidth, srcHeight, srcDepth;
1946 GLint dstWidth, dstHeight, dstDepth;
1947 GLint border;
1948 GLboolean nextLevel;
1949
1950 /* get src image parameters */
1951 srcImage = _mesa_select_tex_image(ctx, texObj, target, level);
1952 ASSERT(srcImage);
1953 srcWidth = srcImage->Width;
1954 srcHeight = srcImage->Height;
1955 srcDepth = srcImage->Depth;
1956 border = srcImage->Border;
1957
1958 nextLevel = next_mipmap_level_size(target, border,
1959 srcWidth, srcHeight, srcDepth,
1960 &dstWidth, &dstHeight, &dstDepth);
1961 if (!nextLevel) {
1962 /* all done */
1963 if (_mesa_is_format_compressed(srcImage->TexFormat)) {
1964 free((void *) srcData);
1965 free(dstData);
1966 }
1967 return;
1968 }
1969
1970 /* get dest gl_texture_image */
1971 dstImage = _mesa_get_tex_image(ctx, texObj, target, level + 1);
1972 if (!dstImage) {
1973 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1974 return;
1975 }
1976
1977 /* Free old image data */
1978 if (dstImage->Data)
1979 ctx->Driver.FreeTexImageData(ctx, dstImage);
1980
1981 /* initialize new image */
1982 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
1983 dstDepth, border, srcImage->InternalFormat,
1984 srcImage->TexFormat);
1985 dstImage->DriverData = NULL;
1986 dstImage->FetchTexelc = srcImage->FetchTexelc;
1987 dstImage->FetchTexelf = srcImage->FetchTexelf;
1988
1989 /* Alloc new teximage data buffer */
1990 {
1991 GLuint size = _mesa_format_image_size(dstImage->TexFormat,
1992 dstWidth, dstHeight, dstDepth);
1993 dstImage->Data = _mesa_alloc_texmemory(size);
1994 if (!dstImage->Data) {
1995 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1996 return;
1997 }
1998 }
1999
2000 /* Setup src and dest data pointers */
2001 if (_mesa_is_format_compressed(dstImage->TexFormat)) {
2002 /* srcData and dstData are already set */
2003 ASSERT(srcData);
2004 ASSERT(dstData);
2005 }
2006 else {
2007 srcData = (const GLubyte *) srcImage->Data;
2008 dstData = (GLubyte *) dstImage->Data;
2009 }
2010
2011 ASSERT(dstImage->TexFormat);
2012 ASSERT(dstImage->FetchTexelc);
2013 ASSERT(dstImage->FetchTexelf);
2014
2015 _mesa_generate_mipmap_level(target, datatype, comps, border,
2016 srcWidth, srcHeight, srcDepth,
2017 srcData, srcImage->RowStride,
2018 dstWidth, dstHeight, dstDepth,
2019 dstData, dstImage->RowStride);
2020
2021
2022 if (_mesa_is_format_compressed(dstImage->TexFormat)) {
2023 GLubyte *temp;
2024 /* compress image from dstData into dstImage->Data */
2025 const GLenum srcFormat = _mesa_get_format_base_format(convertFormat);
2026 GLint dstRowStride
2027 = _mesa_format_row_stride(dstImage->TexFormat, dstWidth);
2028
2029 _mesa_texstore(ctx, 2, dstImage->_BaseFormat,
2030 dstImage->TexFormat,
2031 dstImage->Data,
2032 0, 0, 0, /* dstX/Y/Zoffset */
2033 dstRowStride, 0, /* strides */
2034 dstWidth, dstHeight, 1, /* size */
2035 srcFormat, CHAN_TYPE,
2036 dstData, /* src data, actually */
2037 &ctx->DefaultPacking);
2038
2039 /* swap src and dest pointers */
2040 temp = (GLubyte *) srcData;
2041 srcData = dstData;
2042 dstData = temp;
2043 }
2044
2045 } /* loop over mipmap levels */
2046 }
2047
2048
2049 /**
2050 * Helper function for drivers which need to rescale texture images to
2051 * certain aspect ratios.
2052 * Nearest filtering only (for broken hardware that can't support
2053 * all aspect ratios). This can be made a lot faster, but I don't
2054 * really care enough...
2055 */
2056 void
2057 _mesa_rescale_teximage2d(GLuint bytesPerPixel,
2058 GLuint srcStrideInPixels,
2059 GLuint dstRowStride,
2060 GLint srcWidth, GLint srcHeight,
2061 GLint dstWidth, GLint dstHeight,
2062 const GLvoid *srcImage, GLvoid *dstImage)
2063 {
2064 GLint row, col;
2065
2066 #define INNER_LOOP( TYPE, HOP, WOP ) \
2067 for ( row = 0 ; row < dstHeight ; row++ ) { \
2068 GLint srcRow = row HOP hScale; \
2069 for ( col = 0 ; col < dstWidth ; col++ ) { \
2070 GLint srcCol = col WOP wScale; \
2071 dst[col] = src[srcRow * srcStrideInPixels + srcCol]; \
2072 } \
2073 dst = (TYPE *) ((GLubyte *) dst + dstRowStride); \
2074 } \
2075
2076 #define RESCALE_IMAGE( TYPE ) \
2077 do { \
2078 const TYPE *src = (const TYPE *)srcImage; \
2079 TYPE *dst = (TYPE *)dstImage; \
2080 \
2081 if ( srcHeight < dstHeight ) { \
2082 const GLint hScale = dstHeight / srcHeight; \
2083 if ( srcWidth < dstWidth ) { \
2084 const GLint wScale = dstWidth / srcWidth; \
2085 INNER_LOOP( TYPE, /, / ); \
2086 } \
2087 else { \
2088 const GLint wScale = srcWidth / dstWidth; \
2089 INNER_LOOP( TYPE, /, * ); \
2090 } \
2091 } \
2092 else { \
2093 const GLint hScale = srcHeight / dstHeight; \
2094 if ( srcWidth < dstWidth ) { \
2095 const GLint wScale = dstWidth / srcWidth; \
2096 INNER_LOOP( TYPE, *, / ); \
2097 } \
2098 else { \
2099 const GLint wScale = srcWidth / dstWidth; \
2100 INNER_LOOP( TYPE, *, * ); \
2101 } \
2102 } \
2103 } while (0)
2104
2105 switch ( bytesPerPixel ) {
2106 case 4:
2107 RESCALE_IMAGE( GLuint );
2108 break;
2109
2110 case 2:
2111 RESCALE_IMAGE( GLushort );
2112 break;
2113
2114 case 1:
2115 RESCALE_IMAGE( GLubyte );
2116 break;
2117 default:
2118 _mesa_problem(NULL,"unexpected bytes/pixel in _mesa_rescale_teximage2d");
2119 }
2120 }
2121
2122
2123 /**
2124 * Upscale an image by replication, not (typical) stretching.
2125 * We use this when the image width or height is less than a
2126 * certain size (4, 8) and we need to upscale an image.
2127 */
2128 void
2129 _mesa_upscale_teximage2d(GLsizei inWidth, GLsizei inHeight,
2130 GLsizei outWidth, GLsizei outHeight,
2131 GLint comps, const GLchan *src, GLint srcRowStride,
2132 GLchan *dest )
2133 {
2134 GLint i, j, k;
2135
2136 ASSERT(outWidth >= inWidth);
2137 ASSERT(outHeight >= inHeight);
2138 #if 0
2139 ASSERT(inWidth == 1 || inWidth == 2 || inHeight == 1 || inHeight == 2);
2140 ASSERT((outWidth & 3) == 0);
2141 ASSERT((outHeight & 3) == 0);
2142 #endif
2143
2144 for (i = 0; i < outHeight; i++) {
2145 const GLint ii = i % inHeight;
2146 for (j = 0; j < outWidth; j++) {
2147 const GLint jj = j % inWidth;
2148 for (k = 0; k < comps; k++) {
2149 dest[(i * outWidth + j) * comps + k]
2150 = src[ii * srcRowStride + jj * comps + k];
2151 }
2152 }
2153 }
2154 }
2155