2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
27 * \file mipmap.c mipmap generation and teximage resizing functions.
33 #include "glformats.h"
41 #include "util/half_float.h"
42 #include "util/format_rgb9e5.h"
43 #include "util/format_r11g11b10f.h"
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.
53 _mesa_compute_num_levels(struct gl_context
*ctx
,
54 struct gl_texture_object
*texObj
,
57 const struct gl_texture_image
*baseImage
;
60 baseImage
= _mesa_get_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
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);
72 bytes_per_pixel(GLenum datatype
, GLuint comps
)
76 if (datatype
== GL_UNSIGNED_INT_8_24_REV_MESA
||
77 datatype
== GL_UNSIGNED_INT_24_8_MESA
)
80 b
= _mesa_sizeof_packed_type(datatype
);
83 if (_mesa_type_is_packed(datatype
))
91 * \name Support macros for do_row and do_row_3d
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.
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
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
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 \
119 #define FILTER_3D(e) \
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]); \
127 #define FILTER_SUM_3D_SIGNED(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
134 #define FILTER_3D_SIGNED(e) \
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]); \
142 #define FILTER_F_3D(e) \
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; \
150 #define FILTER_HF_3D(e) \
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) \
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)
175 do_row(GLenum datatype
, GLuint comps
, GLint srcWidth
,
176 const GLvoid
*srcRowA
, const GLvoid
*srcRowB
,
177 GLint dstWidth
, GLvoid
*dstRow
)
179 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
180 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
185 /* This assertion is no longer valid with non-power-of-2 textures
186 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
189 if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 4) {
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;
202 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 3) {
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;
214 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 2) {
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;
225 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 1) {
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;
236 else if (datatype
== GL_BYTE
&& comps
== 4) {
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;
249 else if (datatype
== GL_BYTE
&& comps
== 3) {
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;
261 else if (datatype
== GL_BYTE
&& comps
== 2) {
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;
272 else if (datatype
== GL_BYTE
&& comps
== 1) {
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;
283 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 4) {
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;
296 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 3) {
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;
308 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 2) {
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;
319 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 1) {
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;
330 else if (datatype
== GL_SHORT
&& comps
== 4) {
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;
343 else if (datatype
== GL_SHORT
&& comps
== 3) {
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;
355 else if (datatype
== GL_SHORT
&& comps
== 2) {
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;
366 else if (datatype
== GL_SHORT
&& comps
== 1) {
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;
377 else if (datatype
== GL_FLOAT
&& comps
== 4) {
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
;
394 else if (datatype
== GL_FLOAT
&& comps
== 3) {
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
;
409 else if (datatype
== GL_FLOAT
&& comps
== 2) {
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
;
422 else if (datatype
== GL_FLOAT
&& comps
== 1) {
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
;
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
);
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
);
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
);
484 else if (datatype
== GL_HALF_FLOAT_ARB
&& comps
== 1) {
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
);
500 else if (datatype
== GL_UNSIGNED_INT
&& comps
== 1) {
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;
511 else if (datatype
== GL_UNSIGNED_SHORT_5_6_5
&& comps
== 3) {
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
;
536 else if (datatype
== GL_UNSIGNED_SHORT_4_4_4_4
&& comps
== 4) {
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
;
566 else if (datatype
== GL_UNSIGNED_SHORT_1_5_5_5_REV
&& comps
== 4) {
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
;
596 else if (datatype
== GL_UNSIGNED_SHORT_5_5_5_1
&& comps
== 4) {
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
;
627 else if (datatype
== GL_UNSIGNED_BYTE_3_3_2
&& comps
== 3) {
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
;
653 else if (datatype
== MESA_UNSIGNED_BYTE_4_4
&& comps
== 2) {
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
;
674 else if (datatype
== GL_UNSIGNED_INT_2_10_10_10_REV
&& comps
== 4) {
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
;
705 else if (datatype
== GL_UNSIGNED_INT_5_9_9_9_REV
&& comps
== 3) {
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
);
724 else if (datatype
== GL_UNSIGNED_INT_10F_11F_11F_REV
&& comps
== 3) {
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
);
743 else if (datatype
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
&& comps
== 1) {
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
;
754 else if (datatype
== GL_UNSIGNED_INT_24_8_MESA
&& comps
== 2) {
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;
769 else if (datatype
== GL_UNSIGNED_INT_8_24_REV_MESA
&& comps
== 2) {
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;
785 unreachable("bad format in do_row()");
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.
796 * \param datatype GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT,
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
808 do_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
)
813 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
814 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
820 if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 4)) {
821 DECLARE_ROW_POINTERS(GLubyte
, 4);
823 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
824 i
++, j
+= colStride
, k
+= colStride
) {
831 else if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 3)) {
832 DECLARE_ROW_POINTERS(GLubyte
, 3);
834 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
835 i
++, j
+= colStride
, k
+= colStride
) {
841 else if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 2)) {
842 DECLARE_ROW_POINTERS(GLubyte
, 2);
844 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
845 i
++, j
+= colStride
, k
+= colStride
) {
850 else if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 1)) {
851 DECLARE_ROW_POINTERS(GLubyte
, 1);
853 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
854 i
++, j
+= colStride
, k
+= colStride
) {
858 else if ((datatype
== GL_BYTE
) && (comps
== 4)) {
859 DECLARE_ROW_POINTERS(GLbyte
, 4);
861 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
862 i
++, j
+= colStride
, k
+= colStride
) {
869 else if ((datatype
== GL_BYTE
) && (comps
== 3)) {
870 DECLARE_ROW_POINTERS(GLbyte
, 3);
872 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
873 i
++, j
+= colStride
, k
+= colStride
) {
879 else if ((datatype
== GL_BYTE
) && (comps
== 2)) {
880 DECLARE_ROW_POINTERS(GLbyte
, 2);
882 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
883 i
++, j
+= colStride
, k
+= colStride
) {
888 else if ((datatype
== GL_BYTE
) && (comps
== 1)) {
889 DECLARE_ROW_POINTERS(GLbyte
, 1);
891 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
892 i
++, j
+= colStride
, k
+= colStride
) {
896 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 4)) {
897 DECLARE_ROW_POINTERS(GLushort
, 4);
899 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
900 i
++, j
+= colStride
, k
+= colStride
) {
907 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 3)) {
908 DECLARE_ROW_POINTERS(GLushort
, 3);
910 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
911 i
++, j
+= colStride
, k
+= colStride
) {
917 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 2)) {
918 DECLARE_ROW_POINTERS(GLushort
, 2);
920 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
921 i
++, j
+= colStride
, k
+= colStride
) {
926 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 1)) {
927 DECLARE_ROW_POINTERS(GLushort
, 1);
929 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
930 i
++, j
+= colStride
, k
+= colStride
) {
934 else if ((datatype
== GL_SHORT
) && (comps
== 4)) {
935 DECLARE_ROW_POINTERS(GLshort
, 4);
937 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
938 i
++, j
+= colStride
, k
+= colStride
) {
945 else if ((datatype
== GL_SHORT
) && (comps
== 3)) {
946 DECLARE_ROW_POINTERS(GLshort
, 3);
948 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
949 i
++, j
+= colStride
, k
+= colStride
) {
955 else if ((datatype
== GL_SHORT
) && (comps
== 2)) {
956 DECLARE_ROW_POINTERS(GLshort
, 2);
958 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
959 i
++, j
+= colStride
, k
+= colStride
) {
964 else if ((datatype
== GL_SHORT
) && (comps
== 1)) {
965 DECLARE_ROW_POINTERS(GLshort
, 1);
967 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
968 i
++, j
+= colStride
, k
+= colStride
) {
972 else if ((datatype
== GL_FLOAT
) && (comps
== 4)) {
973 DECLARE_ROW_POINTERS(GLfloat
, 4);
975 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
976 i
++, j
+= colStride
, k
+= colStride
) {
983 else if ((datatype
== GL_FLOAT
) && (comps
== 3)) {
984 DECLARE_ROW_POINTERS(GLfloat
, 3);
986 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
987 i
++, j
+= colStride
, k
+= colStride
) {
993 else if ((datatype
== GL_FLOAT
) && (comps
== 2)) {
994 DECLARE_ROW_POINTERS(GLfloat
, 2);
996 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
997 i
++, j
+= colStride
, k
+= colStride
) {
1002 else if ((datatype
== GL_FLOAT
) && (comps
== 1)) {
1003 DECLARE_ROW_POINTERS(GLfloat
, 1);
1005 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1006 i
++, j
+= colStride
, k
+= colStride
) {
1010 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 4)) {
1011 DECLARE_ROW_POINTERS(GLhalfARB
, 4);
1013 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1014 i
++, j
+= colStride
, k
+= colStride
) {
1021 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 3)) {
1022 DECLARE_ROW_POINTERS(GLhalfARB
, 3);
1024 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1025 i
++, j
+= colStride
, k
+= colStride
) {
1031 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 2)) {
1032 DECLARE_ROW_POINTERS(GLhalfARB
, 2);
1034 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1035 i
++, j
+= colStride
, k
+= colStride
) {
1040 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 1)) {
1041 DECLARE_ROW_POINTERS(GLhalfARB
, 1);
1043 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1044 i
++, j
+= colStride
, k
+= colStride
) {
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
;
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);
1064 else if ((datatype
== GL_UNSIGNED_SHORT_5_6_5
) && (comps
== 3)) {
1065 DECLARE_ROW_POINTERS0(GLushort
);
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
;
1102 else if ((datatype
== GL_UNSIGNED_SHORT_4_4_4_4
) && (comps
== 4)) {
1103 DECLARE_ROW_POINTERS0(GLushort
);
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
);
1148 dst
[i
] = (a
<< 12) | (b
<< 8) | (g
<< 4) | r
;
1151 else if ((datatype
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) && (comps
== 4)) {
1152 DECLARE_ROW_POINTERS0(GLushort
);
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
);
1197 dst
[i
] = (a
<< 15) | (b
<< 10) | (g
<< 5) | r
;
1200 else if ((datatype
== GL_UNSIGNED_SHORT_5_5_5_1
) && (comps
== 4)) {
1201 DECLARE_ROW_POINTERS0(GLushort
);
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
);
1246 dst
[i
] = (r
<< 11) | (g
<< 6) | (b
<< 1) | a
;
1249 else if ((datatype
== GL_UNSIGNED_BYTE_3_3_2
) && (comps
== 3)) {
1250 DECLARE_ROW_POINTERS0(GLubyte
);
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
;
1287 else if (datatype
== MESA_UNSIGNED_BYTE_4_4
&& comps
== 2) {
1288 DECLARE_ROW_POINTERS0(GLubyte
);
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
;
1315 else if ((datatype
== GL_UNSIGNED_INT_2_10_10_10_REV
) && (comps
== 4)) {
1316 DECLARE_ROW_POINTERS0(GLuint
);
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
);
1361 dst
[i
] = (a
<< 30) | (b
<< 20) | (g
<< 10) | r
;
1365 else if (datatype
== GL_UNSIGNED_INT_5_9_9_9_REV
&& comps
== 3) {
1366 DECLARE_ROW_POINTERS0(GLuint
);
1369 GLfloat rowAj
[3], rowBj
[3], rowCj
[3], rowDj
[3];
1370 GLfloat rowAk
[3], rowBk
[3], rowCk
[3], rowDk
[3];
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
);
1392 else if (datatype
== GL_UNSIGNED_INT_10F_11F_11F_REV
&& comps
== 3) {
1393 DECLARE_ROW_POINTERS0(GLuint
);
1396 GLfloat rowAj
[3], rowBj
[3], rowCj
[3], rowDj
[3];
1397 GLfloat rowAk
[3], rowBk
[3], rowCk
[3], rowDk
[3];
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
);
1419 else if (datatype
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
&& comps
== 1) {
1420 DECLARE_ROW_POINTERS(GLfloat
, 2);
1422 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1423 i
++, j
+= colStride
, k
+= colStride
) {
1429 unreachable("bad format in do_row()");
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.
1441 make_1d_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
1442 GLint srcWidth
, const GLubyte
*srcPtr
,
1443 GLint dstWidth
, GLubyte
*dstPtr
)
1445 const GLint bpt
= bytes_per_pixel(datatype
, comps
);
1449 /* skip the border pixel, if any */
1450 src
= srcPtr
+ border
* bpt
;
1451 dst
= dstPtr
+ border
* bpt
;
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
);
1458 /* copy left-most pixel from source */
1461 memcpy(dstPtr
, srcPtr
, bpt
);
1462 /* copy right-most pixel from source */
1463 memcpy(dstPtr
+ (dstWidth
- 1) * bpt
,
1464 srcPtr
+ (srcWidth
- 1) * bpt
,
1471 make_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
)
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
;
1483 GLint row
, srcRowStep
;
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
;
1493 /* sample from one source row */
1498 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
1500 for (row
= 0; row
< dstHeightNB
; row
++) {
1501 do_row(datatype
, comps
, srcWidthNB
, srcA
, srcB
,
1503 srcA
+= srcRowStep
* srcRowStride
;
1504 srcB
+= srcRowStep
* srcRowStride
;
1505 dst
+= dstRowStride
;
1508 /* This is ugly but probably won't be used much */
1510 /* fill in dest border */
1511 /* lower-left border pixel */
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
);
1525 do_row(datatype
, comps
, srcWidthNB
,
1528 dstWidthNB
, dstPtr
+ bpt
);
1530 do_row(datatype
, comps
, srcWidthNB
,
1531 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
1532 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
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
);
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
);
1563 make_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
)
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
;
1576 GLint bytesPerSrcImage
, bytesPerDstImage
;
1577 GLint srcImageOffset
, srcRowOffset
;
1579 (void) srcDepthNB
; /* silence warnings */
1581 bytesPerSrcImage
= srcRowStride
* srcHeight
* bpt
;
1582 bytesPerDstImage
= dstRowStride
* dstHeight
* bpt
;
1584 /* Offset between adjacent src images to be averaged together */
1585 srcImageOffset
= (srcDepth
== dstDepth
) ? 0 : 1;
1587 /* Offset between adjacent src rows to be averaged together */
1588 srcRowOffset
= (srcHeight
== dstHeight
) ? 0 : srcRowStride
;
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.
1599 printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1600 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
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
;
1611 /* address of the dest image, skipping border */
1612 GLubyte
*imgDst
= dstPtr
[img
+ border
]
1613 + dstRowStride
* border
+ bpt
* border
;
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
;
1622 for (row
= 0; row
< dstHeightNB
; row
++) {
1623 do_row_3D(datatype
, comps
, srcWidthNB
,
1624 srcImgARowA
, srcImgARowB
,
1625 srcImgBRowA
, srcImgBRowB
,
1626 dstWidthNB
, dstImgRow
);
1628 /* advance to next rows */
1629 srcImgARowA
+= srcRowStride
+ srcRowOffset
;
1630 srcImgARowB
+= srcRowStride
+ srcRowOffset
;
1631 srcImgBRowA
+= srcRowStride
+ srcRowOffset
;
1632 srcImgBRowB
+= srcRowStride
+ srcRowOffset
;
1633 dstImgRow
+= dstRowStride
;
1638 /* Luckily we can leverage the make_2d_mipmap() function here! */
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
);
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
++) {
1656 /* do border along [img][row=0][col=0] */
1657 src
= srcPtr
[img
* 2];
1659 memcpy(dst
, src
, bpt
);
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
);
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
);
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
);
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
;
1684 /* do border along [img][row=0][col=0] */
1685 srcA
= srcPtr
[img
* 2 + 0];
1686 srcB
= srcPtr
[img
* 2 + srcImageOffset
];
1688 do_row(datatype
, comps
, 1, srcA
, srcB
, 1, dst
);
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
);
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
);
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
);
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
1724 _mesa_generate_mipmap_level(GLenum target
,
1725 GLenum datatype
, GLuint comps
,
1727 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1728 const GLubyte
**srcData
,
1730 GLint dstWidth
, GLint dstHeight
, GLint dstDepth
,
1738 make_1d_mipmap(datatype
, comps
, border
,
1739 srcWidth
, srcData
[0],
1740 dstWidth
, dstData
[0]);
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
);
1754 make_3d_mipmap(datatype
, comps
, border
,
1755 srcWidth
, srcHeight
, srcDepth
,
1756 srcData
, srcRowStride
,
1757 dstWidth
, dstHeight
, dstDepth
,
1758 dstData
, dstRowStride
);
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
]);
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
);
1777 case GL_TEXTURE_RECTANGLE_NV
:
1778 case GL_TEXTURE_EXTERNAL_OES
:
1779 /* no mipmaps, do nothing */
1782 unreachable("bad tex target in _mesa_generate_mipmaps");
1788 * compute next (level+1) image size
1789 * \return GL_FALSE if no smaller size can be generated (eg. src is 1x1x1 size)
1792 _mesa_next_mipmap_level_size(GLenum target
, GLint border
,
1793 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1794 GLint
*dstWidth
, GLint
*dstHeight
, GLint
*dstDepth
)
1796 if (srcWidth
- 2 * border
> 1) {
1797 *dstWidth
= (srcWidth
- 2 * border
) / 2 + 2 * border
;
1800 *dstWidth
= srcWidth
; /* can't go smaller */
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
;
1809 *dstHeight
= srcHeight
; /* can't go smaller */
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
;
1820 *dstDepth
= srcDepth
; /* can't go smaller */
1823 if (*dstWidth
== srcWidth
&&
1824 *dstHeight
== srcHeight
&&
1825 *dstDepth
== srcDepth
) {
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
1841 prepare_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
)
1846 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
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
1854 if (!texObj
->Image
[0][level
]) {
1855 /* No more levels to create - we're done */
1859 /* Nothing to do - the texture memory must have already been
1860 * allocated to the right size so we're all set.
1866 for (face
= 0; face
< numFaces
; face
++) {
1867 struct gl_texture_image
*dstImage
;
1868 const GLenum target
= _mesa_cube_face_target(texObj
->Target
, face
);
1870 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
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
);
1885 _mesa_init_teximage_fields(ctx
, dstImage
,
1886 width
, height
, depth
,
1887 border
, intFormat
, format
);
1889 ctx
->Driver
.AllocTextureImageBuffer(ctx
, dstImage
);
1891 /* in case the mipmap level is part of an FBO: */
1892 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
1894 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
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).
1909 _mesa_prepare_mipmap_levels(struct gl_context
*ctx
,
1910 struct gl_texture_object
*texObj
,
1911 unsigned baseLevel
, unsigned maxLevel
)
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
;
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
)) {
1932 if (!prepare_mipmap_level(ctx
, texObj
, level
,
1933 newWidth
, newHeight
, newDepth
,
1934 border
, intFormat
, texFormat
)) {
1946 generate_mipmap_uncompressed(struct gl_context
*ctx
, GLenum target
,
1947 struct gl_texture_object
*texObj
,
1948 const struct gl_texture_image
*srcImage
,
1955 _mesa_uncompressed_format_to_type_and_comps(srcImage
->TexFormat
, &datatype
, &comps
);
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
;
1965 GLubyte
**srcMaps
, **dstMaps
;
1966 GLboolean success
= GL_TRUE
;
1968 /* get src image parameters */
1969 srcImage
= _mesa_select_tex_image(texObj
, target
, level
);
1971 srcWidth
= srcImage
->Width
;
1972 srcHeight
= srcImage
->Height
;
1973 srcDepth
= srcImage
->Depth
;
1974 border
= srcImage
->Border
;
1976 /* get dest gl_texture_image */
1977 dstImage
= _mesa_select_tex_image(texObj
, target
, level
+ 1);
1981 dstWidth
= dstImage
->Width
;
1982 dstHeight
= dstImage
->Height
;
1983 dstDepth
= dstImage
->Depth
;
1985 if (target
== GL_TEXTURE_1D_ARRAY
) {
1986 srcDepth
= srcHeight
;
1987 dstDepth
= dstHeight
;
1992 /* Map src texture image slices */
1993 srcMaps
= calloc(srcDepth
, sizeof(GLubyte
*));
1995 for (slice
= 0; slice
< srcDepth
; slice
++) {
1996 ctx
->Driver
.MapTextureImage(ctx
, srcImage
, slice
,
1997 0, 0, srcWidth
, srcHeight
,
1999 &srcMaps
[slice
], &srcRowStride
);
2000 if (!srcMaps
[slice
]) {
2010 /* Map dst texture image slices */
2011 dstMaps
= calloc(dstDepth
, sizeof(GLubyte
*));
2013 for (slice
= 0; slice
< dstDepth
; slice
++) {
2014 ctx
->Driver
.MapTextureImage(ctx
, dstImage
, slice
,
2015 0, 0, dstWidth
, dstHeight
,
2017 &dstMaps
[slice
], &dstRowStride
);
2018 if (!dstMaps
[slice
]) {
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
);
2037 /* Unmap src image slices */
2039 for (slice
= 0; slice
< srcDepth
; slice
++) {
2040 if (srcMaps
[slice
]) {
2041 ctx
->Driver
.UnmapTextureImage(ctx
, srcImage
, slice
);
2047 /* Unmap dst image slices */
2049 for (slice
= 0; slice
< dstDepth
; slice
++) {
2050 if (dstMaps
[slice
]) {
2051 ctx
->Driver
.UnmapTextureImage(ctx
, dstImage
, slice
);
2058 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "mipmap generation");
2061 } /* loop over mipmap levels */
2066 generate_mipmap_compressed(struct gl_context
*ctx
, GLenum target
,
2067 struct gl_texture_object
*texObj
,
2068 struct gl_texture_image
*srcImage
,
2072 mesa_format temp_format
;
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
;
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
);
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.
2091 temp_format
= _mesa_get_uncompressed_format(srcImage
->TexFormat
);
2093 components
= _mesa_format_num_components(temp_format
);
2095 switch (_mesa_get_format_datatype(srcImage
->TexFormat
)) {
2097 temp_datatype
= GL_FLOAT
;
2099 case GL_SIGNED_NORMALIZED
:
2100 /* Revisit this if we get compressed formats with >8 bits per component */
2101 temp_datatype
= GL_BYTE
;
2104 temp_datatype
= GL_UNSIGNED_BYTE
;
2107 temp_base_format
= _mesa_get_format_base_format(temp_format
);
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
);
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
*));
2120 if (!temp_src
|| !temp_src_slices
|| !temp_dst_slices
) {
2121 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
2125 /* decompress base image to the temporary src buffer */
2127 /* save pixel packing mode */
2128 struct gl_pixelstore_attrib save
= ctx
->Pack
;
2129 /* use default/tight packing parameters */
2130 ctx
->Pack
= ctx
->DefaultPacking
;
2132 /* Get the uncompressed image */
2133 assert(srcImage
->Level
== texObj
->BaseLevel
);
2134 ctx
->Driver
.GetTexSubImage(ctx
,
2136 srcImage
->Width
, srcImage
->Height
,
2138 temp_base_format
, temp_datatype
,
2139 temp_src
, srcImage
);
2140 /* restore packing mode */
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
;
2151 GLuint temp_dst_row_stride
, temp_dst_img_stride
; /* in bytes */
2154 /* get src image parameters */
2155 srcImage
= _mesa_select_tex_image(texObj
, target
, level
);
2157 srcWidth
= srcImage
->Width
;
2158 srcHeight
= srcImage
->Height
;
2159 srcDepth
= srcImage
->Depth
;
2160 border
= srcImage
->Border
;
2162 /* get dest gl_texture_image */
2163 dstImage
= _mesa_select_tex_image(texObj
, target
, level
+ 1);
2167 dstWidth
= dstImage
->Width
;
2168 dstHeight
= dstImage
->Height
;
2169 dstDepth
= dstImage
->Depth
;
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
,
2176 temp_dst
= malloc(temp_dst_img_stride
* dstDepth
);
2178 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
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
;
2187 for (i
= 0; i
< dstDepth
; i
++) {
2188 temp_dst_slices
[i
] = temp_dst
+ temp_dst_img_stride
* i
;
2191 /* Rescale src image to dest image.
2192 * This will loop over the slices of a 2D array.
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
);
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
);
2207 /* swap src and dest pointers */
2209 GLubyte
*temp
= temp_src
;
2210 temp_src
= temp_dst
;
2212 temp_src_row_stride
= temp_dst_row_stride
;
2213 temp_src_img_stride
= temp_dst_img_stride
;
2215 } /* loop over mipmap levels */
2220 free(temp_src_slices
);
2221 free(temp_dst_slices
);
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.
2233 _mesa_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
2234 struct gl_texture_object
*texObj
)
2236 struct gl_texture_image
*srcImage
;
2240 srcImage
= _mesa_select_tex_image(texObj
, target
, texObj
->BaseLevel
);
2243 maxLevel
= _mesa_max_texture_levels(ctx
, texObj
->Target
) - 1;
2244 assert(maxLevel
>= 0); /* bad target */
2246 maxLevel
= MIN2(maxLevel
, texObj
->MaxLevel
);
2248 _mesa_prepare_mipmap_levels(ctx
, texObj
, texObj
->BaseLevel
, maxLevel
);
2250 if (_mesa_is_format_compressed(srcImage
->TexFormat
)) {
2251 generate_mipmap_compressed(ctx
, target
, texObj
, srcImage
, maxLevel
);
2253 generate_mipmap_uncompressed(ctx
, target
, texObj
, srcImage
, maxLevel
);