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