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