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.
32 #include "glformats.h"
40 #include "util/half_float.h"
41 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
42 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
47 bytes_per_pixel(GLenum datatype
, GLuint comps
)
51 if (datatype
== GL_UNSIGNED_INT_8_24_REV_MESA
||
52 datatype
== GL_UNSIGNED_INT_24_8_MESA
)
55 b
= _mesa_sizeof_packed_type(datatype
);
58 if (_mesa_type_is_packed(datatype
))
66 * \name Support macros for do_row and do_row_3d
68 * The macro madness is here for two reasons. First, it compacts the code
69 * slightly. Second, it makes it much easier to adjust the specifics of the
70 * filter to tune the rounding characteristics.
73 #define DECLARE_ROW_POINTERS(t, e) \
74 const t(*rowA)[e] = (const t(*)[e]) srcRowA; \
75 const t(*rowB)[e] = (const t(*)[e]) srcRowB; \
76 const t(*rowC)[e] = (const t(*)[e]) srcRowC; \
77 const t(*rowD)[e] = (const t(*)[e]) srcRowD; \
78 t(*dst)[e] = (t(*)[e]) dstRow
80 #define DECLARE_ROW_POINTERS0(t) \
81 const t *rowA = (const t *) srcRowA; \
82 const t *rowB = (const t *) srcRowB; \
83 const t *rowC = (const t *) srcRowC; \
84 const t *rowD = (const t *) srcRowD; \
87 #define FILTER_SUM_3D(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
88 ((unsigned) Aj + (unsigned) Ak \
89 + (unsigned) Bj + (unsigned) Bk \
90 + (unsigned) Cj + (unsigned) Ck \
91 + (unsigned) Dj + (unsigned) Dk \
94 #define FILTER_3D(e) \
96 dst[i][e] = FILTER_SUM_3D(rowA[j][e], rowA[k][e], \
97 rowB[j][e], rowB[k][e], \
98 rowC[j][e], rowC[k][e], \
99 rowD[j][e], rowD[k][e]); \
102 #define FILTER_SUM_3D_SIGNED(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
109 #define FILTER_3D_SIGNED(e) \
111 dst[i][e] = FILTER_SUM_3D_SIGNED(rowA[j][e], rowA[k][e], \
112 rowB[j][e], rowB[k][e], \
113 rowC[j][e], rowC[k][e], \
114 rowD[j][e], rowD[k][e]); \
117 #define FILTER_F_3D(e) \
119 dst[i][e] = (rowA[j][e] + rowA[k][e] \
120 + rowB[j][e] + rowB[k][e] \
121 + rowC[j][e] + rowC[k][e] \
122 + rowD[j][e] + rowD[k][e]) * 0.125F; \
125 #define FILTER_HF_3D(e) \
127 const GLfloat aj = _mesa_half_to_float(rowA[j][e]); \
128 const GLfloat ak = _mesa_half_to_float(rowA[k][e]); \
129 const GLfloat bj = _mesa_half_to_float(rowB[j][e]); \
130 const GLfloat bk = _mesa_half_to_float(rowB[k][e]); \
131 const GLfloat cj = _mesa_half_to_float(rowC[j][e]); \
132 const GLfloat ck = _mesa_half_to_float(rowC[k][e]); \
133 const GLfloat dj = _mesa_half_to_float(rowD[j][e]); \
134 const GLfloat dk = _mesa_half_to_float(rowD[k][e]); \
135 dst[i][e] = _mesa_float_to_half((aj + ak + bj + bk + cj + ck + dj + dk) \
142 * Average together two rows of a source image to produce a single new
143 * row in the dest image. It's legal for the two source rows to point
144 * to the same data. The source width must be equal to either the
145 * dest width or two times the dest width.
146 * \param datatype GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc.
147 * \param comps number of components per pixel (1..4)
150 do_row(GLenum datatype
, GLuint comps
, GLint srcWidth
,
151 const GLvoid
*srcRowA
, const GLvoid
*srcRowB
,
152 GLint dstWidth
, GLvoid
*dstRow
)
154 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
155 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
160 /* This assertion is no longer valid with non-power-of-2 textures
161 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
164 if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 4) {
166 const GLubyte(*rowA
)[4] = (const GLubyte(*)[4]) srcRowA
;
167 const GLubyte(*rowB
)[4] = (const GLubyte(*)[4]) srcRowB
;
168 GLubyte(*dst
)[4] = (GLubyte(*)[4]) dstRow
;
169 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
170 i
++, j
+= colStride
, k
+= colStride
) {
171 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
172 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
173 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
174 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] + rowB
[j
][3] + rowB
[k
][3]) / 4;
177 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 3) {
179 const GLubyte(*rowA
)[3] = (const GLubyte(*)[3]) srcRowA
;
180 const GLubyte(*rowB
)[3] = (const GLubyte(*)[3]) srcRowB
;
181 GLubyte(*dst
)[3] = (GLubyte(*)[3]) dstRow
;
182 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
183 i
++, j
+= colStride
, k
+= colStride
) {
184 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
185 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
186 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
189 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 2) {
191 const GLubyte(*rowA
)[2] = (const GLubyte(*)[2]) srcRowA
;
192 const GLubyte(*rowB
)[2] = (const GLubyte(*)[2]) srcRowB
;
193 GLubyte(*dst
)[2] = (GLubyte(*)[2]) 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]) >> 2;
197 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) >> 2;
200 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 1) {
202 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
203 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
204 GLubyte
*dst
= (GLubyte
*) dstRow
;
205 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
206 i
++, j
+= colStride
, k
+= colStride
) {
207 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) >> 2;
211 else if (datatype
== GL_BYTE
&& comps
== 4) {
213 const GLbyte(*rowA
)[4] = (const GLbyte(*)[4]) srcRowA
;
214 const GLbyte(*rowB
)[4] = (const GLbyte(*)[4]) srcRowB
;
215 GLbyte(*dst
)[4] = (GLbyte(*)[4]) dstRow
;
216 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
217 i
++, j
+= colStride
, k
+= colStride
) {
218 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
219 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
220 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
221 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] + rowB
[j
][3] + rowB
[k
][3]) / 4;
224 else if (datatype
== GL_BYTE
&& comps
== 3) {
226 const GLbyte(*rowA
)[3] = (const GLbyte(*)[3]) srcRowA
;
227 const GLbyte(*rowB
)[3] = (const GLbyte(*)[3]) srcRowB
;
228 GLbyte(*dst
)[3] = (GLbyte(*)[3]) dstRow
;
229 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
230 i
++, j
+= colStride
, k
+= colStride
) {
231 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
232 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
233 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
236 else if (datatype
== GL_BYTE
&& comps
== 2) {
238 const GLbyte(*rowA
)[2] = (const GLbyte(*)[2]) srcRowA
;
239 const GLbyte(*rowB
)[2] = (const GLbyte(*)[2]) srcRowB
;
240 GLbyte(*dst
)[2] = (GLbyte(*)[2]) 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;
247 else if (datatype
== GL_BYTE
&& comps
== 1) {
249 const GLbyte
*rowA
= (const GLbyte
*) srcRowA
;
250 const GLbyte
*rowB
= (const GLbyte
*) srcRowB
;
251 GLbyte
*dst
= (GLbyte
*) dstRow
;
252 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
253 i
++, j
+= colStride
, k
+= colStride
) {
254 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) / 4;
258 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 4) {
260 const GLushort(*rowA
)[4] = (const GLushort(*)[4]) srcRowA
;
261 const GLushort(*rowB
)[4] = (const GLushort(*)[4]) srcRowB
;
262 GLushort(*dst
)[4] = (GLushort(*)[4]) dstRow
;
263 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
264 i
++, j
+= colStride
, k
+= colStride
) {
265 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
266 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
267 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
268 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] + rowB
[j
][3] + rowB
[k
][3]) / 4;
271 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 3) {
273 const GLushort(*rowA
)[3] = (const GLushort(*)[3]) srcRowA
;
274 const GLushort(*rowB
)[3] = (const GLushort(*)[3]) srcRowB
;
275 GLushort(*dst
)[3] = (GLushort(*)[3]) dstRow
;
276 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
277 i
++, j
+= colStride
, k
+= colStride
) {
278 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
279 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
280 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
283 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 2) {
285 const GLushort(*rowA
)[2] = (const GLushort(*)[2]) srcRowA
;
286 const GLushort(*rowB
)[2] = (const GLushort(*)[2]) srcRowB
;
287 GLushort(*dst
)[2] = (GLushort(*)[2]) 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;
294 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 1) {
296 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
297 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
298 GLushort
*dst
= (GLushort
*) dstRow
;
299 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
300 i
++, j
+= colStride
, k
+= colStride
) {
301 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) / 4;
305 else if (datatype
== GL_SHORT
&& comps
== 4) {
307 const GLshort(*rowA
)[4] = (const GLshort(*)[4]) srcRowA
;
308 const GLshort(*rowB
)[4] = (const GLshort(*)[4]) srcRowB
;
309 GLshort(*dst
)[4] = (GLshort(*)[4]) dstRow
;
310 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
311 i
++, j
+= colStride
, k
+= colStride
) {
312 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
313 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
314 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
315 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] + rowB
[j
][3] + rowB
[k
][3]) / 4;
318 else if (datatype
== GL_SHORT
&& comps
== 3) {
320 const GLshort(*rowA
)[3] = (const GLshort(*)[3]) srcRowA
;
321 const GLshort(*rowB
)[3] = (const GLshort(*)[3]) srcRowB
;
322 GLshort(*dst
)[3] = (GLshort(*)[3]) dstRow
;
323 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
324 i
++, j
+= colStride
, k
+= colStride
) {
325 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] + rowB
[j
][0] + rowB
[k
][0]) / 4;
326 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] + rowB
[j
][1] + rowB
[k
][1]) / 4;
327 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] + rowB
[j
][2] + rowB
[k
][2]) / 4;
330 else if (datatype
== GL_SHORT
&& comps
== 2) {
332 const GLshort(*rowA
)[2] = (const GLshort(*)[2]) srcRowA
;
333 const GLshort(*rowB
)[2] = (const GLshort(*)[2]) srcRowB
;
334 GLshort(*dst
)[2] = (GLshort(*)[2]) 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;
341 else if (datatype
== GL_SHORT
&& comps
== 1) {
343 const GLshort
*rowA
= (const GLshort
*) srcRowA
;
344 const GLshort
*rowB
= (const GLshort
*) srcRowB
;
345 GLshort
*dst
= (GLshort
*) dstRow
;
346 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
347 i
++, j
+= colStride
, k
+= colStride
) {
348 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) / 4;
352 else if (datatype
== GL_FLOAT
&& comps
== 4) {
354 const GLfloat(*rowA
)[4] = (const GLfloat(*)[4]) srcRowA
;
355 const GLfloat(*rowB
)[4] = (const GLfloat(*)[4]) srcRowB
;
356 GLfloat(*dst
)[4] = (GLfloat(*)[4]) dstRow
;
357 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
358 i
++, j
+= colStride
, k
+= colStride
) {
359 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
360 rowB
[j
][0] + rowB
[k
][0]) * 0.25F
;
361 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
362 rowB
[j
][1] + rowB
[k
][1]) * 0.25F
;
363 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
364 rowB
[j
][2] + rowB
[k
][2]) * 0.25F
;
365 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
366 rowB
[j
][3] + rowB
[k
][3]) * 0.25F
;
369 else if (datatype
== GL_FLOAT
&& comps
== 3) {
371 const GLfloat(*rowA
)[3] = (const GLfloat(*)[3]) srcRowA
;
372 const GLfloat(*rowB
)[3] = (const GLfloat(*)[3]) srcRowB
;
373 GLfloat(*dst
)[3] = (GLfloat(*)[3]) dstRow
;
374 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
375 i
++, j
+= colStride
, k
+= colStride
) {
376 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
377 rowB
[j
][0] + rowB
[k
][0]) * 0.25F
;
378 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
379 rowB
[j
][1] + rowB
[k
][1]) * 0.25F
;
380 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
381 rowB
[j
][2] + rowB
[k
][2]) * 0.25F
;
384 else if (datatype
== GL_FLOAT
&& comps
== 2) {
386 const GLfloat(*rowA
)[2] = (const GLfloat(*)[2]) srcRowA
;
387 const GLfloat(*rowB
)[2] = (const GLfloat(*)[2]) srcRowB
;
388 GLfloat(*dst
)[2] = (GLfloat(*)[2]) dstRow
;
389 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
390 i
++, j
+= colStride
, k
+= colStride
) {
391 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
392 rowB
[j
][0] + rowB
[k
][0]) * 0.25F
;
393 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
394 rowB
[j
][1] + rowB
[k
][1]) * 0.25F
;
397 else if (datatype
== GL_FLOAT
&& comps
== 1) {
399 const GLfloat
*rowA
= (const GLfloat
*) srcRowA
;
400 const GLfloat
*rowB
= (const GLfloat
*) srcRowB
;
401 GLfloat
*dst
= (GLfloat
*) dstRow
;
402 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
403 i
++, j
+= colStride
, k
+= colStride
) {
404 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) * 0.25F
;
408 else if (datatype
== GL_HALF_FLOAT_ARB
&& comps
== 4) {
409 GLuint i
, j
, k
, comp
;
410 const GLhalfARB(*rowA
)[4] = (const GLhalfARB(*)[4]) srcRowA
;
411 const GLhalfARB(*rowB
)[4] = (const GLhalfARB(*)[4]) srcRowB
;
412 GLhalfARB(*dst
)[4] = (GLhalfARB(*)[4]) dstRow
;
413 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
414 i
++, j
+= colStride
, k
+= colStride
) {
415 for (comp
= 0; comp
< 4; comp
++) {
416 GLfloat aj
, ak
, bj
, bk
;
417 aj
= _mesa_half_to_float(rowA
[j
][comp
]);
418 ak
= _mesa_half_to_float(rowA
[k
][comp
]);
419 bj
= _mesa_half_to_float(rowB
[j
][comp
]);
420 bk
= _mesa_half_to_float(rowB
[k
][comp
]);
421 dst
[i
][comp
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
425 else if (datatype
== GL_HALF_FLOAT_ARB
&& comps
== 3) {
426 GLuint i
, j
, k
, comp
;
427 const GLhalfARB(*rowA
)[3] = (const GLhalfARB(*)[3]) srcRowA
;
428 const GLhalfARB(*rowB
)[3] = (const GLhalfARB(*)[3]) srcRowB
;
429 GLhalfARB(*dst
)[3] = (GLhalfARB(*)[3]) dstRow
;
430 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
431 i
++, j
+= colStride
, k
+= colStride
) {
432 for (comp
= 0; comp
< 3; comp
++) {
433 GLfloat aj
, ak
, bj
, bk
;
434 aj
= _mesa_half_to_float(rowA
[j
][comp
]);
435 ak
= _mesa_half_to_float(rowA
[k
][comp
]);
436 bj
= _mesa_half_to_float(rowB
[j
][comp
]);
437 bk
= _mesa_half_to_float(rowB
[k
][comp
]);
438 dst
[i
][comp
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
442 else if (datatype
== GL_HALF_FLOAT_ARB
&& comps
== 2) {
443 GLuint i
, j
, k
, comp
;
444 const GLhalfARB(*rowA
)[2] = (const GLhalfARB(*)[2]) srcRowA
;
445 const GLhalfARB(*rowB
)[2] = (const GLhalfARB(*)[2]) srcRowB
;
446 GLhalfARB(*dst
)[2] = (GLhalfARB(*)[2]) dstRow
;
447 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
448 i
++, j
+= colStride
, k
+= colStride
) {
449 for (comp
= 0; comp
< 2; comp
++) {
450 GLfloat aj
, ak
, bj
, bk
;
451 aj
= _mesa_half_to_float(rowA
[j
][comp
]);
452 ak
= _mesa_half_to_float(rowA
[k
][comp
]);
453 bj
= _mesa_half_to_float(rowB
[j
][comp
]);
454 bk
= _mesa_half_to_float(rowB
[k
][comp
]);
455 dst
[i
][comp
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
459 else if (datatype
== GL_HALF_FLOAT_ARB
&& comps
== 1) {
461 const GLhalfARB
*rowA
= (const GLhalfARB
*) srcRowA
;
462 const GLhalfARB
*rowB
= (const GLhalfARB
*) srcRowB
;
463 GLhalfARB
*dst
= (GLhalfARB
*) dstRow
;
464 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
465 i
++, j
+= colStride
, k
+= colStride
) {
466 GLfloat aj
, ak
, bj
, bk
;
467 aj
= _mesa_half_to_float(rowA
[j
]);
468 ak
= _mesa_half_to_float(rowA
[k
]);
469 bj
= _mesa_half_to_float(rowB
[j
]);
470 bk
= _mesa_half_to_float(rowB
[k
]);
471 dst
[i
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
475 else if (datatype
== GL_UNSIGNED_INT
&& comps
== 1) {
477 const GLuint
*rowA
= (const GLuint
*) srcRowA
;
478 const GLuint
*rowB
= (const GLuint
*) srcRowB
;
479 GLuint
*dst
= (GLuint
*) dstRow
;
480 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
481 i
++, j
+= colStride
, k
+= colStride
) {
482 dst
[i
] = rowA
[j
] / 4 + rowA
[k
] / 4 + rowB
[j
] / 4 + rowB
[k
] / 4;
486 else if (datatype
== GL_UNSIGNED_SHORT_5_6_5
&& comps
== 3) {
488 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
489 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
490 GLushort
*dst
= (GLushort
*) dstRow
;
491 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
492 i
++, j
+= colStride
, k
+= colStride
) {
493 const GLint rowAr0
= rowA
[j
] & 0x1f;
494 const GLint rowAr1
= rowA
[k
] & 0x1f;
495 const GLint rowBr0
= rowB
[j
] & 0x1f;
496 const GLint rowBr1
= rowB
[k
] & 0x1f;
497 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x3f;
498 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x3f;
499 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x3f;
500 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x3f;
501 const GLint rowAb0
= (rowA
[j
] >> 11) & 0x1f;
502 const GLint rowAb1
= (rowA
[k
] >> 11) & 0x1f;
503 const GLint rowBb0
= (rowB
[j
] >> 11) & 0x1f;
504 const GLint rowBb1
= (rowB
[k
] >> 11) & 0x1f;
505 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
506 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
507 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
508 dst
[i
] = (blue
<< 11) | (green
<< 5) | red
;
511 else if (datatype
== GL_UNSIGNED_SHORT_4_4_4_4
&& comps
== 4) {
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
] & 0xf;
519 const GLint rowAr1
= rowA
[k
] & 0xf;
520 const GLint rowBr0
= rowB
[j
] & 0xf;
521 const GLint rowBr1
= rowB
[k
] & 0xf;
522 const GLint rowAg0
= (rowA
[j
] >> 4) & 0xf;
523 const GLint rowAg1
= (rowA
[k
] >> 4) & 0xf;
524 const GLint rowBg0
= (rowB
[j
] >> 4) & 0xf;
525 const GLint rowBg1
= (rowB
[k
] >> 4) & 0xf;
526 const GLint rowAb0
= (rowA
[j
] >> 8) & 0xf;
527 const GLint rowAb1
= (rowA
[k
] >> 8) & 0xf;
528 const GLint rowBb0
= (rowB
[j
] >> 8) & 0xf;
529 const GLint rowBb1
= (rowB
[k
] >> 8) & 0xf;
530 const GLint rowAa0
= (rowA
[j
] >> 12) & 0xf;
531 const GLint rowAa1
= (rowA
[k
] >> 12) & 0xf;
532 const GLint rowBa0
= (rowB
[j
] >> 12) & 0xf;
533 const GLint rowBa1
= (rowB
[k
] >> 12) & 0xf;
534 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
535 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
536 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
537 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 2;
538 dst
[i
] = (alpha
<< 12) | (blue
<< 8) | (green
<< 4) | red
;
541 else if (datatype
== GL_UNSIGNED_SHORT_1_5_5_5_REV
&& comps
== 4) {
543 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
544 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
545 GLushort
*dst
= (GLushort
*) dstRow
;
546 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
547 i
++, j
+= colStride
, k
+= colStride
) {
548 const GLint rowAr0
= rowA
[j
] & 0x1f;
549 const GLint rowAr1
= rowA
[k
] & 0x1f;
550 const GLint rowBr0
= rowB
[j
] & 0x1f;
551 const GLint rowBr1
= rowB
[k
] & 0x1f;
552 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x1f;
553 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x1f;
554 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x1f;
555 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x1f;
556 const GLint rowAb0
= (rowA
[j
] >> 10) & 0x1f;
557 const GLint rowAb1
= (rowA
[k
] >> 10) & 0x1f;
558 const GLint rowBb0
= (rowB
[j
] >> 10) & 0x1f;
559 const GLint rowBb1
= (rowB
[k
] >> 10) & 0x1f;
560 const GLint rowAa0
= (rowA
[j
] >> 15) & 0x1;
561 const GLint rowAa1
= (rowA
[k
] >> 15) & 0x1;
562 const GLint rowBa0
= (rowB
[j
] >> 15) & 0x1;
563 const GLint rowBa1
= (rowB
[k
] >> 15) & 0x1;
564 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
565 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
566 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
567 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 2;
568 dst
[i
] = (alpha
<< 15) | (blue
<< 10) | (green
<< 5) | red
;
571 else if (datatype
== GL_UNSIGNED_SHORT_5_5_5_1
&& comps
== 4) {
573 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
574 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
575 GLushort
*dst
= (GLushort
*) dstRow
;
576 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
577 i
++, j
+= colStride
, k
+= colStride
) {
578 const GLint rowAr0
= (rowA
[j
] >> 11) & 0x1f;
579 const GLint rowAr1
= (rowA
[k
] >> 11) & 0x1f;
580 const GLint rowBr0
= (rowB
[j
] >> 11) & 0x1f;
581 const GLint rowBr1
= (rowB
[k
] >> 11) & 0x1f;
582 const GLint rowAg0
= (rowA
[j
] >> 6) & 0x1f;
583 const GLint rowAg1
= (rowA
[k
] >> 6) & 0x1f;
584 const GLint rowBg0
= (rowB
[j
] >> 6) & 0x1f;
585 const GLint rowBg1
= (rowB
[k
] >> 6) & 0x1f;
586 const GLint rowAb0
= (rowA
[j
] >> 1) & 0x1f;
587 const GLint rowAb1
= (rowA
[k
] >> 1) & 0x1f;
588 const GLint rowBb0
= (rowB
[j
] >> 1) & 0x1f;
589 const GLint rowBb1
= (rowB
[k
] >> 1) & 0x1f;
590 const GLint rowAa0
= (rowA
[j
] & 0x1);
591 const GLint rowAa1
= (rowA
[k
] & 0x1);
592 const GLint rowBa0
= (rowB
[j
] & 0x1);
593 const GLint rowBa1
= (rowB
[k
] & 0x1);
594 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
595 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
596 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
597 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 2;
598 dst
[i
] = (red
<< 11) | (green
<< 6) | (blue
<< 1) | alpha
;
602 else if (datatype
== GL_UNSIGNED_BYTE_3_3_2
&& comps
== 3) {
604 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
605 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
606 GLubyte
*dst
= (GLubyte
*) dstRow
;
607 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
608 i
++, j
+= colStride
, k
+= colStride
) {
609 const GLint rowAr0
= rowA
[j
] & 0x3;
610 const GLint rowAr1
= rowA
[k
] & 0x3;
611 const GLint rowBr0
= rowB
[j
] & 0x3;
612 const GLint rowBr1
= rowB
[k
] & 0x3;
613 const GLint rowAg0
= (rowA
[j
] >> 2) & 0x7;
614 const GLint rowAg1
= (rowA
[k
] >> 2) & 0x7;
615 const GLint rowBg0
= (rowB
[j
] >> 2) & 0x7;
616 const GLint rowBg1
= (rowB
[k
] >> 2) & 0x7;
617 const GLint rowAb0
= (rowA
[j
] >> 5) & 0x7;
618 const GLint rowAb1
= (rowA
[k
] >> 5) & 0x7;
619 const GLint rowBb0
= (rowB
[j
] >> 5) & 0x7;
620 const GLint rowBb1
= (rowB
[k
] >> 5) & 0x7;
621 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
622 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
623 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
624 dst
[i
] = (blue
<< 5) | (green
<< 2) | red
;
628 else if (datatype
== MESA_UNSIGNED_BYTE_4_4
&& comps
== 2) {
630 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
631 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
632 GLubyte
*dst
= (GLubyte
*) dstRow
;
633 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
634 i
++, j
+= colStride
, k
+= colStride
) {
635 const GLint rowAr0
= rowA
[j
] & 0xf;
636 const GLint rowAr1
= rowA
[k
] & 0xf;
637 const GLint rowBr0
= rowB
[j
] & 0xf;
638 const GLint rowBr1
= rowB
[k
] & 0xf;
639 const GLint rowAg0
= (rowA
[j
] >> 4) & 0xf;
640 const GLint rowAg1
= (rowA
[k
] >> 4) & 0xf;
641 const GLint rowBg0
= (rowB
[j
] >> 4) & 0xf;
642 const GLint rowBg1
= (rowB
[k
] >> 4) & 0xf;
643 const GLint r
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
644 const GLint g
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
645 dst
[i
] = (g
<< 4) | r
;
649 else if (datatype
== GL_UNSIGNED_INT_2_10_10_10_REV
&& comps
== 4) {
651 const GLuint
*rowA
= (const GLuint
*) srcRowA
;
652 const GLuint
*rowB
= (const GLuint
*) srcRowB
;
653 GLuint
*dst
= (GLuint
*) dstRow
;
654 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
655 i
++, j
+= colStride
, k
+= colStride
) {
656 const GLint rowAr0
= rowA
[j
] & 0x3ff;
657 const GLint rowAr1
= rowA
[k
] & 0x3ff;
658 const GLint rowBr0
= rowB
[j
] & 0x3ff;
659 const GLint rowBr1
= rowB
[k
] & 0x3ff;
660 const GLint rowAg0
= (rowA
[j
] >> 10) & 0x3ff;
661 const GLint rowAg1
= (rowA
[k
] >> 10) & 0x3ff;
662 const GLint rowBg0
= (rowB
[j
] >> 10) & 0x3ff;
663 const GLint rowBg1
= (rowB
[k
] >> 10) & 0x3ff;
664 const GLint rowAb0
= (rowA
[j
] >> 20) & 0x3ff;
665 const GLint rowAb1
= (rowA
[k
] >> 20) & 0x3ff;
666 const GLint rowBb0
= (rowB
[j
] >> 20) & 0x3ff;
667 const GLint rowBb1
= (rowB
[k
] >> 20) & 0x3ff;
668 const GLint rowAa0
= (rowA
[j
] >> 30) & 0x3;
669 const GLint rowAa1
= (rowA
[k
] >> 30) & 0x3;
670 const GLint rowBa0
= (rowB
[j
] >> 30) & 0x3;
671 const GLint rowBa1
= (rowB
[k
] >> 30) & 0x3;
672 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
673 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
674 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
675 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 2;
676 dst
[i
] = (alpha
<< 30) | (blue
<< 20) | (green
<< 10) | red
;
680 else if (datatype
== GL_UNSIGNED_INT_5_9_9_9_REV
&& comps
== 3) {
682 const GLuint
*rowA
= (const GLuint
*) srcRowA
;
683 const GLuint
*rowB
= (const GLuint
*) srcRowB
;
684 GLuint
*dst
= (GLuint
*)dstRow
;
685 GLfloat res
[3], rowAj
[3], rowBj
[3], rowAk
[3], rowBk
[3];
686 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
687 i
++, j
+= colStride
, k
+= colStride
) {
688 rgb9e5_to_float3(rowA
[j
], rowAj
);
689 rgb9e5_to_float3(rowB
[j
], rowBj
);
690 rgb9e5_to_float3(rowA
[k
], rowAk
);
691 rgb9e5_to_float3(rowB
[k
], rowBk
);
692 res
[0] = (rowAj
[0] + rowAk
[0] + rowBj
[0] + rowBk
[0]) * 0.25F
;
693 res
[1] = (rowAj
[1] + rowAk
[1] + rowBj
[1] + rowBk
[1]) * 0.25F
;
694 res
[2] = (rowAj
[2] + rowAk
[2] + rowBj
[2] + rowBk
[2]) * 0.25F
;
695 dst
[i
] = float3_to_rgb9e5(res
);
699 else if (datatype
== GL_UNSIGNED_INT_10F_11F_11F_REV
&& comps
== 3) {
701 const GLuint
*rowA
= (const GLuint
*) srcRowA
;
702 const GLuint
*rowB
= (const GLuint
*) srcRowB
;
703 GLuint
*dst
= (GLuint
*)dstRow
;
704 GLfloat res
[3], rowAj
[3], rowBj
[3], rowAk
[3], rowBk
[3];
705 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
706 i
++, j
+= colStride
, k
+= colStride
) {
707 r11g11b10f_to_float3(rowA
[j
], rowAj
);
708 r11g11b10f_to_float3(rowB
[j
], rowBj
);
709 r11g11b10f_to_float3(rowA
[k
], rowAk
);
710 r11g11b10f_to_float3(rowB
[k
], rowBk
);
711 res
[0] = (rowAj
[0] + rowAk
[0] + rowBj
[0] + rowBk
[0]) * 0.25F
;
712 res
[1] = (rowAj
[1] + rowAk
[1] + rowBj
[1] + rowBk
[1]) * 0.25F
;
713 res
[2] = (rowAj
[2] + rowAk
[2] + rowBj
[2] + rowBk
[2]) * 0.25F
;
714 dst
[i
] = float3_to_r11g11b10f(res
);
718 else if (datatype
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
&& comps
== 1) {
720 const GLfloat
*rowA
= (const GLfloat
*) srcRowA
;
721 const GLfloat
*rowB
= (const GLfloat
*) srcRowB
;
722 GLfloat
*dst
= (GLfloat
*) dstRow
;
723 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
724 i
++, j
+= colStride
, k
+= colStride
) {
725 dst
[i
*2] = (rowA
[j
*2] + rowA
[k
*2] + rowB
[j
*2] + rowB
[k
*2]) * 0.25F
;
729 else if (datatype
== GL_UNSIGNED_INT_24_8_MESA
&& comps
== 2) {
731 const GLuint
*rowA
= (const GLuint
*) srcRowA
;
732 const GLuint
*rowB
= (const GLuint
*) srcRowB
;
733 GLuint
*dst
= (GLuint
*) dstRow
;
734 /* note: averaging stencil values seems weird, but what else? */
735 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
736 i
++, j
+= colStride
, k
+= colStride
) {
737 GLuint z
= (((rowA
[j
] >> 8) + (rowA
[k
] >> 8) +
738 (rowB
[j
] >> 8) + (rowB
[k
] >> 8)) / 4) << 8;
739 GLuint s
= ((rowA
[j
] & 0xff) + (rowA
[k
] & 0xff) +
740 (rowB
[j
] & 0xff) + (rowB
[k
] & 0xff)) / 4;
744 else if (datatype
== GL_UNSIGNED_INT_8_24_REV_MESA
&& comps
== 2) {
746 const GLuint
*rowA
= (const GLuint
*) srcRowA
;
747 const GLuint
*rowB
= (const GLuint
*) srcRowB
;
748 GLuint
*dst
= (GLuint
*) dstRow
;
749 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
750 i
++, j
+= colStride
, k
+= colStride
) {
751 GLuint z
= ((rowA
[j
] & 0xffffff) + (rowA
[k
] & 0xffffff) +
752 (rowB
[j
] & 0xffffff) + (rowB
[k
] & 0xffffff)) / 4;
753 GLuint s
= (((rowA
[j
] >> 24) + (rowA
[k
] >> 24) +
754 (rowB
[j
] >> 24) + (rowB
[k
] >> 24)) / 4) << 24;
760 _mesa_problem(NULL
, "bad format in do_row()");
766 * Average together four rows of a source image to produce a single new
767 * row in the dest image. It's legal for the two source rows to point
768 * to the same data. The source width must be equal to either the
769 * dest width or two times the dest width.
771 * \param datatype GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT,
773 * \param comps number of components per pixel (1..4)
774 * \param srcWidth Width of a row in the source data
775 * \param srcRowA Pointer to one of the rows of source data
776 * \param srcRowB Pointer to one of the rows of source data
777 * \param srcRowC Pointer to one of the rows of source data
778 * \param srcRowD Pointer to one of the rows of source data
779 * \param dstWidth Width of a row in the destination data
780 * \param srcRowA Pointer to the row of destination data
783 do_row_3D(GLenum datatype
, GLuint comps
, GLint srcWidth
,
784 const GLvoid
*srcRowA
, const GLvoid
*srcRowB
,
785 const GLvoid
*srcRowC
, const GLvoid
*srcRowD
,
786 GLint dstWidth
, GLvoid
*dstRow
)
788 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
789 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
795 if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 4)) {
796 DECLARE_ROW_POINTERS(GLubyte
, 4);
798 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
799 i
++, j
+= colStride
, k
+= colStride
) {
806 else if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 3)) {
807 DECLARE_ROW_POINTERS(GLubyte
, 3);
809 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
810 i
++, j
+= colStride
, k
+= colStride
) {
816 else if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 2)) {
817 DECLARE_ROW_POINTERS(GLubyte
, 2);
819 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
820 i
++, j
+= colStride
, k
+= colStride
) {
825 else if ((datatype
== GL_UNSIGNED_BYTE
) && (comps
== 1)) {
826 DECLARE_ROW_POINTERS(GLubyte
, 1);
828 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
829 i
++, j
+= colStride
, k
+= colStride
) {
833 else if ((datatype
== GL_BYTE
) && (comps
== 4)) {
834 DECLARE_ROW_POINTERS(GLbyte
, 4);
836 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
837 i
++, j
+= colStride
, k
+= colStride
) {
844 else if ((datatype
== GL_BYTE
) && (comps
== 3)) {
845 DECLARE_ROW_POINTERS(GLbyte
, 3);
847 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
848 i
++, j
+= colStride
, k
+= colStride
) {
854 else if ((datatype
== GL_BYTE
) && (comps
== 2)) {
855 DECLARE_ROW_POINTERS(GLbyte
, 2);
857 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
858 i
++, j
+= colStride
, k
+= colStride
) {
863 else if ((datatype
== GL_BYTE
) && (comps
== 1)) {
864 DECLARE_ROW_POINTERS(GLbyte
, 1);
866 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
867 i
++, j
+= colStride
, k
+= colStride
) {
871 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 4)) {
872 DECLARE_ROW_POINTERS(GLushort
, 4);
874 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
875 i
++, j
+= colStride
, k
+= colStride
) {
882 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 3)) {
883 DECLARE_ROW_POINTERS(GLushort
, 3);
885 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
886 i
++, j
+= colStride
, k
+= colStride
) {
892 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 2)) {
893 DECLARE_ROW_POINTERS(GLushort
, 2);
895 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
896 i
++, j
+= colStride
, k
+= colStride
) {
901 else if ((datatype
== GL_UNSIGNED_SHORT
) && (comps
== 1)) {
902 DECLARE_ROW_POINTERS(GLushort
, 1);
904 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
905 i
++, j
+= colStride
, k
+= colStride
) {
909 else if ((datatype
== GL_SHORT
) && (comps
== 4)) {
910 DECLARE_ROW_POINTERS(GLshort
, 4);
912 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
913 i
++, j
+= colStride
, k
+= colStride
) {
920 else if ((datatype
== GL_SHORT
) && (comps
== 3)) {
921 DECLARE_ROW_POINTERS(GLshort
, 3);
923 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
924 i
++, j
+= colStride
, k
+= colStride
) {
930 else if ((datatype
== GL_SHORT
) && (comps
== 2)) {
931 DECLARE_ROW_POINTERS(GLshort
, 2);
933 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
934 i
++, j
+= colStride
, k
+= colStride
) {
939 else if ((datatype
== GL_SHORT
) && (comps
== 1)) {
940 DECLARE_ROW_POINTERS(GLshort
, 1);
942 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
943 i
++, j
+= colStride
, k
+= colStride
) {
947 else if ((datatype
== GL_FLOAT
) && (comps
== 4)) {
948 DECLARE_ROW_POINTERS(GLfloat
, 4);
950 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
951 i
++, j
+= colStride
, k
+= colStride
) {
958 else if ((datatype
== GL_FLOAT
) && (comps
== 3)) {
959 DECLARE_ROW_POINTERS(GLfloat
, 3);
961 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
962 i
++, j
+= colStride
, k
+= colStride
) {
968 else if ((datatype
== GL_FLOAT
) && (comps
== 2)) {
969 DECLARE_ROW_POINTERS(GLfloat
, 2);
971 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
972 i
++, j
+= colStride
, k
+= colStride
) {
977 else if ((datatype
== GL_FLOAT
) && (comps
== 1)) {
978 DECLARE_ROW_POINTERS(GLfloat
, 1);
980 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
981 i
++, j
+= colStride
, k
+= colStride
) {
985 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 4)) {
986 DECLARE_ROW_POINTERS(GLhalfARB
, 4);
988 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
989 i
++, j
+= colStride
, k
+= colStride
) {
996 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 3)) {
997 DECLARE_ROW_POINTERS(GLhalfARB
, 3);
999 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1000 i
++, j
+= colStride
, k
+= colStride
) {
1006 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 2)) {
1007 DECLARE_ROW_POINTERS(GLhalfARB
, 2);
1009 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1010 i
++, j
+= colStride
, k
+= colStride
) {
1015 else if ((datatype
== GL_HALF_FLOAT_ARB
) && (comps
== 1)) {
1016 DECLARE_ROW_POINTERS(GLhalfARB
, 1);
1018 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1019 i
++, j
+= colStride
, k
+= colStride
) {
1023 else if ((datatype
== GL_UNSIGNED_INT
) && (comps
== 1)) {
1024 const GLuint
*rowA
= (const GLuint
*) srcRowA
;
1025 const GLuint
*rowB
= (const GLuint
*) srcRowB
;
1026 const GLuint
*rowC
= (const GLuint
*) srcRowC
;
1027 const GLuint
*rowD
= (const GLuint
*) srcRowD
;
1028 GLfloat
*dst
= (GLfloat
*) dstRow
;
1030 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1031 i
++, j
+= colStride
, k
+= colStride
) {
1032 const uint64_t tmp
= (((uint64_t) rowA
[j
] + (uint64_t) rowA
[k
])
1033 + ((uint64_t) rowB
[j
] + (uint64_t) rowB
[k
])
1034 + ((uint64_t) rowC
[j
] + (uint64_t) rowC
[k
])
1035 + ((uint64_t) rowD
[j
] + (uint64_t) rowD
[k
]));
1036 dst
[i
] = (GLfloat
)((double) tmp
* 0.125);
1039 else if ((datatype
== GL_UNSIGNED_SHORT_5_6_5
) && (comps
== 3)) {
1040 DECLARE_ROW_POINTERS0(GLushort
);
1042 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1043 i
++, j
+= colStride
, k
+= colStride
) {
1044 const GLint rowAr0
= rowA
[j
] & 0x1f;
1045 const GLint rowAr1
= rowA
[k
] & 0x1f;
1046 const GLint rowBr0
= rowB
[j
] & 0x1f;
1047 const GLint rowBr1
= rowB
[k
] & 0x1f;
1048 const GLint rowCr0
= rowC
[j
] & 0x1f;
1049 const GLint rowCr1
= rowC
[k
] & 0x1f;
1050 const GLint rowDr0
= rowD
[j
] & 0x1f;
1051 const GLint rowDr1
= rowD
[k
] & 0x1f;
1052 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x3f;
1053 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x3f;
1054 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x3f;
1055 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x3f;
1056 const GLint rowCg0
= (rowC
[j
] >> 5) & 0x3f;
1057 const GLint rowCg1
= (rowC
[k
] >> 5) & 0x3f;
1058 const GLint rowDg0
= (rowD
[j
] >> 5) & 0x3f;
1059 const GLint rowDg1
= (rowD
[k
] >> 5) & 0x3f;
1060 const GLint rowAb0
= (rowA
[j
] >> 11) & 0x1f;
1061 const GLint rowAb1
= (rowA
[k
] >> 11) & 0x1f;
1062 const GLint rowBb0
= (rowB
[j
] >> 11) & 0x1f;
1063 const GLint rowBb1
= (rowB
[k
] >> 11) & 0x1f;
1064 const GLint rowCb0
= (rowC
[j
] >> 11) & 0x1f;
1065 const GLint rowCb1
= (rowC
[k
] >> 11) & 0x1f;
1066 const GLint rowDb0
= (rowD
[j
] >> 11) & 0x1f;
1067 const GLint rowDb1
= (rowD
[k
] >> 11) & 0x1f;
1068 const GLint r
= FILTER_SUM_3D(rowAr0
, rowAr1
, rowBr0
, rowBr1
,
1069 rowCr0
, rowCr1
, rowDr0
, rowDr1
);
1070 const GLint g
= FILTER_SUM_3D(rowAg0
, rowAg1
, rowBg0
, rowBg1
,
1071 rowCg0
, rowCg1
, rowDg0
, rowDg1
);
1072 const GLint b
= FILTER_SUM_3D(rowAb0
, rowAb1
, rowBb0
, rowBb1
,
1073 rowCb0
, rowCb1
, rowDb0
, rowDb1
);
1074 dst
[i
] = (b
<< 11) | (g
<< 5) | r
;
1077 else if ((datatype
== GL_UNSIGNED_SHORT_4_4_4_4
) && (comps
== 4)) {
1078 DECLARE_ROW_POINTERS0(GLushort
);
1080 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1081 i
++, j
+= colStride
, k
+= colStride
) {
1082 const GLint rowAr0
= rowA
[j
] & 0xf;
1083 const GLint rowAr1
= rowA
[k
] & 0xf;
1084 const GLint rowBr0
= rowB
[j
] & 0xf;
1085 const GLint rowBr1
= rowB
[k
] & 0xf;
1086 const GLint rowCr0
= rowC
[j
] & 0xf;
1087 const GLint rowCr1
= rowC
[k
] & 0xf;
1088 const GLint rowDr0
= rowD
[j
] & 0xf;
1089 const GLint rowDr1
= rowD
[k
] & 0xf;
1090 const GLint rowAg0
= (rowA
[j
] >> 4) & 0xf;
1091 const GLint rowAg1
= (rowA
[k
] >> 4) & 0xf;
1092 const GLint rowBg0
= (rowB
[j
] >> 4) & 0xf;
1093 const GLint rowBg1
= (rowB
[k
] >> 4) & 0xf;
1094 const GLint rowCg0
= (rowC
[j
] >> 4) & 0xf;
1095 const GLint rowCg1
= (rowC
[k
] >> 4) & 0xf;
1096 const GLint rowDg0
= (rowD
[j
] >> 4) & 0xf;
1097 const GLint rowDg1
= (rowD
[k
] >> 4) & 0xf;
1098 const GLint rowAb0
= (rowA
[j
] >> 8) & 0xf;
1099 const GLint rowAb1
= (rowA
[k
] >> 8) & 0xf;
1100 const GLint rowBb0
= (rowB
[j
] >> 8) & 0xf;
1101 const GLint rowBb1
= (rowB
[k
] >> 8) & 0xf;
1102 const GLint rowCb0
= (rowC
[j
] >> 8) & 0xf;
1103 const GLint rowCb1
= (rowC
[k
] >> 8) & 0xf;
1104 const GLint rowDb0
= (rowD
[j
] >> 8) & 0xf;
1105 const GLint rowDb1
= (rowD
[k
] >> 8) & 0xf;
1106 const GLint rowAa0
= (rowA
[j
] >> 12) & 0xf;
1107 const GLint rowAa1
= (rowA
[k
] >> 12) & 0xf;
1108 const GLint rowBa0
= (rowB
[j
] >> 12) & 0xf;
1109 const GLint rowBa1
= (rowB
[k
] >> 12) & 0xf;
1110 const GLint rowCa0
= (rowC
[j
] >> 12) & 0xf;
1111 const GLint rowCa1
= (rowC
[k
] >> 12) & 0xf;
1112 const GLint rowDa0
= (rowD
[j
] >> 12) & 0xf;
1113 const GLint rowDa1
= (rowD
[k
] >> 12) & 0xf;
1114 const GLint r
= FILTER_SUM_3D(rowAr0
, rowAr1
, rowBr0
, rowBr1
,
1115 rowCr0
, rowCr1
, rowDr0
, rowDr1
);
1116 const GLint g
= FILTER_SUM_3D(rowAg0
, rowAg1
, rowBg0
, rowBg1
,
1117 rowCg0
, rowCg1
, rowDg0
, rowDg1
);
1118 const GLint b
= FILTER_SUM_3D(rowAb0
, rowAb1
, rowBb0
, rowBb1
,
1119 rowCb0
, rowCb1
, rowDb0
, rowDb1
);
1120 const GLint a
= FILTER_SUM_3D(rowAa0
, rowAa1
, rowBa0
, rowBa1
,
1121 rowCa0
, rowCa1
, rowDa0
, rowDa1
);
1123 dst
[i
] = (a
<< 12) | (b
<< 8) | (g
<< 4) | r
;
1126 else if ((datatype
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) && (comps
== 4)) {
1127 DECLARE_ROW_POINTERS0(GLushort
);
1129 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1130 i
++, j
+= colStride
, k
+= colStride
) {
1131 const GLint rowAr0
= rowA
[j
] & 0x1f;
1132 const GLint rowAr1
= rowA
[k
] & 0x1f;
1133 const GLint rowBr0
= rowB
[j
] & 0x1f;
1134 const GLint rowBr1
= rowB
[k
] & 0x1f;
1135 const GLint rowCr0
= rowC
[j
] & 0x1f;
1136 const GLint rowCr1
= rowC
[k
] & 0x1f;
1137 const GLint rowDr0
= rowD
[j
] & 0x1f;
1138 const GLint rowDr1
= rowD
[k
] & 0x1f;
1139 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x1f;
1140 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x1f;
1141 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x1f;
1142 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x1f;
1143 const GLint rowCg0
= (rowC
[j
] >> 5) & 0x1f;
1144 const GLint rowCg1
= (rowC
[k
] >> 5) & 0x1f;
1145 const GLint rowDg0
= (rowD
[j
] >> 5) & 0x1f;
1146 const GLint rowDg1
= (rowD
[k
] >> 5) & 0x1f;
1147 const GLint rowAb0
= (rowA
[j
] >> 10) & 0x1f;
1148 const GLint rowAb1
= (rowA
[k
] >> 10) & 0x1f;
1149 const GLint rowBb0
= (rowB
[j
] >> 10) & 0x1f;
1150 const GLint rowBb1
= (rowB
[k
] >> 10) & 0x1f;
1151 const GLint rowCb0
= (rowC
[j
] >> 10) & 0x1f;
1152 const GLint rowCb1
= (rowC
[k
] >> 10) & 0x1f;
1153 const GLint rowDb0
= (rowD
[j
] >> 10) & 0x1f;
1154 const GLint rowDb1
= (rowD
[k
] >> 10) & 0x1f;
1155 const GLint rowAa0
= (rowA
[j
] >> 15) & 0x1;
1156 const GLint rowAa1
= (rowA
[k
] >> 15) & 0x1;
1157 const GLint rowBa0
= (rowB
[j
] >> 15) & 0x1;
1158 const GLint rowBa1
= (rowB
[k
] >> 15) & 0x1;
1159 const GLint rowCa0
= (rowC
[j
] >> 15) & 0x1;
1160 const GLint rowCa1
= (rowC
[k
] >> 15) & 0x1;
1161 const GLint rowDa0
= (rowD
[j
] >> 15) & 0x1;
1162 const GLint rowDa1
= (rowD
[k
] >> 15) & 0x1;
1163 const GLint r
= FILTER_SUM_3D(rowAr0
, rowAr1
, rowBr0
, rowBr1
,
1164 rowCr0
, rowCr1
, rowDr0
, rowDr1
);
1165 const GLint g
= FILTER_SUM_3D(rowAg0
, rowAg1
, rowBg0
, rowBg1
,
1166 rowCg0
, rowCg1
, rowDg0
, rowDg1
);
1167 const GLint b
= FILTER_SUM_3D(rowAb0
, rowAb1
, rowBb0
, rowBb1
,
1168 rowCb0
, rowCb1
, rowDb0
, rowDb1
);
1169 const GLint a
= FILTER_SUM_3D(rowAa0
, rowAa1
, rowBa0
, rowBa1
,
1170 rowCa0
, rowCa1
, rowDa0
, rowDa1
);
1172 dst
[i
] = (a
<< 15) | (b
<< 10) | (g
<< 5) | r
;
1175 else if ((datatype
== GL_UNSIGNED_SHORT_5_5_5_1
) && (comps
== 4)) {
1176 DECLARE_ROW_POINTERS0(GLushort
);
1178 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1179 i
++, j
+= colStride
, k
+= colStride
) {
1180 const GLint rowAr0
= (rowA
[j
] >> 11) & 0x1f;
1181 const GLint rowAr1
= (rowA
[k
] >> 11) & 0x1f;
1182 const GLint rowBr0
= (rowB
[j
] >> 11) & 0x1f;
1183 const GLint rowBr1
= (rowB
[k
] >> 11) & 0x1f;
1184 const GLint rowCr0
= (rowC
[j
] >> 11) & 0x1f;
1185 const GLint rowCr1
= (rowC
[k
] >> 11) & 0x1f;
1186 const GLint rowDr0
= (rowD
[j
] >> 11) & 0x1f;
1187 const GLint rowDr1
= (rowD
[k
] >> 11) & 0x1f;
1188 const GLint rowAg0
= (rowA
[j
] >> 6) & 0x1f;
1189 const GLint rowAg1
= (rowA
[k
] >> 6) & 0x1f;
1190 const GLint rowBg0
= (rowB
[j
] >> 6) & 0x1f;
1191 const GLint rowBg1
= (rowB
[k
] >> 6) & 0x1f;
1192 const GLint rowCg0
= (rowC
[j
] >> 6) & 0x1f;
1193 const GLint rowCg1
= (rowC
[k
] >> 6) & 0x1f;
1194 const GLint rowDg0
= (rowD
[j
] >> 6) & 0x1f;
1195 const GLint rowDg1
= (rowD
[k
] >> 6) & 0x1f;
1196 const GLint rowAb0
= (rowA
[j
] >> 1) & 0x1f;
1197 const GLint rowAb1
= (rowA
[k
] >> 1) & 0x1f;
1198 const GLint rowBb0
= (rowB
[j
] >> 1) & 0x1f;
1199 const GLint rowBb1
= (rowB
[k
] >> 1) & 0x1f;
1200 const GLint rowCb0
= (rowC
[j
] >> 1) & 0x1f;
1201 const GLint rowCb1
= (rowC
[k
] >> 1) & 0x1f;
1202 const GLint rowDb0
= (rowD
[j
] >> 1) & 0x1f;
1203 const GLint rowDb1
= (rowD
[k
] >> 1) & 0x1f;
1204 const GLint rowAa0
= (rowA
[j
] & 0x1);
1205 const GLint rowAa1
= (rowA
[k
] & 0x1);
1206 const GLint rowBa0
= (rowB
[j
] & 0x1);
1207 const GLint rowBa1
= (rowB
[k
] & 0x1);
1208 const GLint rowCa0
= (rowC
[j
] & 0x1);
1209 const GLint rowCa1
= (rowC
[k
] & 0x1);
1210 const GLint rowDa0
= (rowD
[j
] & 0x1);
1211 const GLint rowDa1
= (rowD
[k
] & 0x1);
1212 const GLint r
= FILTER_SUM_3D(rowAr0
, rowAr1
, rowBr0
, rowBr1
,
1213 rowCr0
, rowCr1
, rowDr0
, rowDr1
);
1214 const GLint g
= FILTER_SUM_3D(rowAg0
, rowAg1
, rowBg0
, rowBg1
,
1215 rowCg0
, rowCg1
, rowDg0
, rowDg1
);
1216 const GLint b
= FILTER_SUM_3D(rowAb0
, rowAb1
, rowBb0
, rowBb1
,
1217 rowCb0
, rowCb1
, rowDb0
, rowDb1
);
1218 const GLint a
= FILTER_SUM_3D(rowAa0
, rowAa1
, rowBa0
, rowBa1
,
1219 rowCa0
, rowCa1
, rowDa0
, rowDa1
);
1221 dst
[i
] = (r
<< 11) | (g
<< 6) | (b
<< 1) | a
;
1224 else if ((datatype
== GL_UNSIGNED_BYTE_3_3_2
) && (comps
== 3)) {
1225 DECLARE_ROW_POINTERS0(GLubyte
);
1227 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1228 i
++, j
+= colStride
, k
+= colStride
) {
1229 const GLint rowAr0
= rowA
[j
] & 0x3;
1230 const GLint rowAr1
= rowA
[k
] & 0x3;
1231 const GLint rowBr0
= rowB
[j
] & 0x3;
1232 const GLint rowBr1
= rowB
[k
] & 0x3;
1233 const GLint rowCr0
= rowC
[j
] & 0x3;
1234 const GLint rowCr1
= rowC
[k
] & 0x3;
1235 const GLint rowDr0
= rowD
[j
] & 0x3;
1236 const GLint rowDr1
= rowD
[k
] & 0x3;
1237 const GLint rowAg0
= (rowA
[j
] >> 2) & 0x7;
1238 const GLint rowAg1
= (rowA
[k
] >> 2) & 0x7;
1239 const GLint rowBg0
= (rowB
[j
] >> 2) & 0x7;
1240 const GLint rowBg1
= (rowB
[k
] >> 2) & 0x7;
1241 const GLint rowCg0
= (rowC
[j
] >> 2) & 0x7;
1242 const GLint rowCg1
= (rowC
[k
] >> 2) & 0x7;
1243 const GLint rowDg0
= (rowD
[j
] >> 2) & 0x7;
1244 const GLint rowDg1
= (rowD
[k
] >> 2) & 0x7;
1245 const GLint rowAb0
= (rowA
[j
] >> 5) & 0x7;
1246 const GLint rowAb1
= (rowA
[k
] >> 5) & 0x7;
1247 const GLint rowBb0
= (rowB
[j
] >> 5) & 0x7;
1248 const GLint rowBb1
= (rowB
[k
] >> 5) & 0x7;
1249 const GLint rowCb0
= (rowC
[j
] >> 5) & 0x7;
1250 const GLint rowCb1
= (rowC
[k
] >> 5) & 0x7;
1251 const GLint rowDb0
= (rowD
[j
] >> 5) & 0x7;
1252 const GLint rowDb1
= (rowD
[k
] >> 5) & 0x7;
1253 const GLint r
= FILTER_SUM_3D(rowAr0
, rowAr1
, rowBr0
, rowBr1
,
1254 rowCr0
, rowCr1
, rowDr0
, rowDr1
);
1255 const GLint g
= FILTER_SUM_3D(rowAg0
, rowAg1
, rowBg0
, rowBg1
,
1256 rowCg0
, rowCg1
, rowDg0
, rowDg1
);
1257 const GLint b
= FILTER_SUM_3D(rowAb0
, rowAb1
, rowBb0
, rowBb1
,
1258 rowCb0
, rowCb1
, rowDb0
, rowDb1
);
1259 dst
[i
] = (b
<< 5) | (g
<< 2) | r
;
1262 else if (datatype
== MESA_UNSIGNED_BYTE_4_4
&& comps
== 2) {
1263 DECLARE_ROW_POINTERS0(GLubyte
);
1265 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1266 i
++, j
+= colStride
, k
+= colStride
) {
1267 const GLint rowAr0
= rowA
[j
] & 0xf;
1268 const GLint rowAr1
= rowA
[k
] & 0xf;
1269 const GLint rowBr0
= rowB
[j
] & 0xf;
1270 const GLint rowBr1
= rowB
[k
] & 0xf;
1271 const GLint rowCr0
= rowC
[j
] & 0xf;
1272 const GLint rowCr1
= rowC
[k
] & 0xf;
1273 const GLint rowDr0
= rowD
[j
] & 0xf;
1274 const GLint rowDr1
= rowD
[k
] & 0xf;
1275 const GLint rowAg0
= (rowA
[j
] >> 4) & 0xf;
1276 const GLint rowAg1
= (rowA
[k
] >> 4) & 0xf;
1277 const GLint rowBg0
= (rowB
[j
] >> 4) & 0xf;
1278 const GLint rowBg1
= (rowB
[k
] >> 4) & 0xf;
1279 const GLint rowCg0
= (rowC
[j
] >> 4) & 0xf;
1280 const GLint rowCg1
= (rowC
[k
] >> 4) & 0xf;
1281 const GLint rowDg0
= (rowD
[j
] >> 4) & 0xf;
1282 const GLint rowDg1
= (rowD
[k
] >> 4) & 0xf;
1283 const GLint r
= FILTER_SUM_3D(rowAr0
, rowAr1
, rowBr0
, rowBr1
,
1284 rowCr0
, rowCr1
, rowDr0
, rowDr1
);
1285 const GLint g
= FILTER_SUM_3D(rowAg0
, rowAg1
, rowBg0
, rowBg1
,
1286 rowCg0
, rowCg1
, rowDg0
, rowDg1
);
1287 dst
[i
] = (g
<< 4) | r
;
1290 else if ((datatype
== GL_UNSIGNED_INT_2_10_10_10_REV
) && (comps
== 4)) {
1291 DECLARE_ROW_POINTERS0(GLuint
);
1293 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1294 i
++, j
+= colStride
, k
+= colStride
) {
1295 const GLint rowAr0
= rowA
[j
] & 0x3ff;
1296 const GLint rowAr1
= rowA
[k
] & 0x3ff;
1297 const GLint rowBr0
= rowB
[j
] & 0x3ff;
1298 const GLint rowBr1
= rowB
[k
] & 0x3ff;
1299 const GLint rowCr0
= rowC
[j
] & 0x3ff;
1300 const GLint rowCr1
= rowC
[k
] & 0x3ff;
1301 const GLint rowDr0
= rowD
[j
] & 0x3ff;
1302 const GLint rowDr1
= rowD
[k
] & 0x3ff;
1303 const GLint rowAg0
= (rowA
[j
] >> 10) & 0x3ff;
1304 const GLint rowAg1
= (rowA
[k
] >> 10) & 0x3ff;
1305 const GLint rowBg0
= (rowB
[j
] >> 10) & 0x3ff;
1306 const GLint rowBg1
= (rowB
[k
] >> 10) & 0x3ff;
1307 const GLint rowCg0
= (rowC
[j
] >> 10) & 0x3ff;
1308 const GLint rowCg1
= (rowC
[k
] >> 10) & 0x3ff;
1309 const GLint rowDg0
= (rowD
[j
] >> 10) & 0x3ff;
1310 const GLint rowDg1
= (rowD
[k
] >> 10) & 0x3ff;
1311 const GLint rowAb0
= (rowA
[j
] >> 20) & 0x3ff;
1312 const GLint rowAb1
= (rowA
[k
] >> 20) & 0x3ff;
1313 const GLint rowBb0
= (rowB
[j
] >> 20) & 0x3ff;
1314 const GLint rowBb1
= (rowB
[k
] >> 20) & 0x3ff;
1315 const GLint rowCb0
= (rowC
[j
] >> 20) & 0x3ff;
1316 const GLint rowCb1
= (rowC
[k
] >> 20) & 0x3ff;
1317 const GLint rowDb0
= (rowD
[j
] >> 20) & 0x3ff;
1318 const GLint rowDb1
= (rowD
[k
] >> 20) & 0x3ff;
1319 const GLint rowAa0
= (rowA
[j
] >> 30) & 0x3;
1320 const GLint rowAa1
= (rowA
[k
] >> 30) & 0x3;
1321 const GLint rowBa0
= (rowB
[j
] >> 30) & 0x3;
1322 const GLint rowBa1
= (rowB
[k
] >> 30) & 0x3;
1323 const GLint rowCa0
= (rowC
[j
] >> 30) & 0x3;
1324 const GLint rowCa1
= (rowC
[k
] >> 30) & 0x3;
1325 const GLint rowDa0
= (rowD
[j
] >> 30) & 0x3;
1326 const GLint rowDa1
= (rowD
[k
] >> 30) & 0x3;
1327 const GLint r
= FILTER_SUM_3D(rowAr0
, rowAr1
, rowBr0
, rowBr1
,
1328 rowCr0
, rowCr1
, rowDr0
, rowDr1
);
1329 const GLint g
= FILTER_SUM_3D(rowAg0
, rowAg1
, rowBg0
, rowBg1
,
1330 rowCg0
, rowCg1
, rowDg0
, rowDg1
);
1331 const GLint b
= FILTER_SUM_3D(rowAb0
, rowAb1
, rowBb0
, rowBb1
,
1332 rowCb0
, rowCb1
, rowDb0
, rowDb1
);
1333 const GLint a
= FILTER_SUM_3D(rowAa0
, rowAa1
, rowBa0
, rowBa1
,
1334 rowCa0
, rowCa1
, rowDa0
, rowDa1
);
1336 dst
[i
] = (a
<< 30) | (b
<< 20) | (g
<< 10) | r
;
1340 else if (datatype
== GL_UNSIGNED_INT_5_9_9_9_REV
&& comps
== 3) {
1341 DECLARE_ROW_POINTERS0(GLuint
);
1344 GLfloat rowAj
[3], rowBj
[3], rowCj
[3], rowDj
[3];
1345 GLfloat rowAk
[3], rowBk
[3], rowCk
[3], rowDk
[3];
1347 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1348 i
++, j
+= colStride
, k
+= colStride
) {
1349 rgb9e5_to_float3(rowA
[j
], rowAj
);
1350 rgb9e5_to_float3(rowB
[j
], rowBj
);
1351 rgb9e5_to_float3(rowC
[j
], rowCj
);
1352 rgb9e5_to_float3(rowD
[j
], rowDj
);
1353 rgb9e5_to_float3(rowA
[k
], rowAk
);
1354 rgb9e5_to_float3(rowB
[k
], rowBk
);
1355 rgb9e5_to_float3(rowC
[k
], rowCk
);
1356 rgb9e5_to_float3(rowD
[k
], rowDk
);
1357 res
[0] = (rowAj
[0] + rowAk
[0] + rowBj
[0] + rowBk
[0] +
1358 rowCj
[0] + rowCk
[0] + rowDj
[0] + rowDk
[0]) * 0.125F
;
1359 res
[1] = (rowAj
[1] + rowAk
[1] + rowBj
[1] + rowBk
[1] +
1360 rowCj
[1] + rowCk
[1] + rowDj
[1] + rowDk
[1]) * 0.125F
;
1361 res
[2] = (rowAj
[2] + rowAk
[2] + rowBj
[2] + rowBk
[2] +
1362 rowCj
[2] + rowCk
[2] + rowDj
[2] + rowDk
[2]) * 0.125F
;
1363 dst
[i
] = float3_to_rgb9e5(res
);
1367 else if (datatype
== GL_UNSIGNED_INT_10F_11F_11F_REV
&& comps
== 3) {
1368 DECLARE_ROW_POINTERS0(GLuint
);
1371 GLfloat rowAj
[3], rowBj
[3], rowCj
[3], rowDj
[3];
1372 GLfloat rowAk
[3], rowBk
[3], rowCk
[3], rowDk
[3];
1374 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1375 i
++, j
+= colStride
, k
+= colStride
) {
1376 r11g11b10f_to_float3(rowA
[j
], rowAj
);
1377 r11g11b10f_to_float3(rowB
[j
], rowBj
);
1378 r11g11b10f_to_float3(rowC
[j
], rowCj
);
1379 r11g11b10f_to_float3(rowD
[j
], rowDj
);
1380 r11g11b10f_to_float3(rowA
[k
], rowAk
);
1381 r11g11b10f_to_float3(rowB
[k
], rowBk
);
1382 r11g11b10f_to_float3(rowC
[k
], rowCk
);
1383 r11g11b10f_to_float3(rowD
[k
], rowDk
);
1384 res
[0] = (rowAj
[0] + rowAk
[0] + rowBj
[0] + rowBk
[0] +
1385 rowCj
[0] + rowCk
[0] + rowDj
[0] + rowDk
[0]) * 0.125F
;
1386 res
[1] = (rowAj
[1] + rowAk
[1] + rowBj
[1] + rowBk
[1] +
1387 rowCj
[1] + rowCk
[1] + rowDj
[1] + rowDk
[1]) * 0.125F
;
1388 res
[2] = (rowAj
[2] + rowAk
[2] + rowBj
[2] + rowBk
[2] +
1389 rowCj
[2] + rowCk
[2] + rowDj
[2] + rowDk
[2]) * 0.125F
;
1390 dst
[i
] = float3_to_r11g11b10f(res
);
1394 else if (datatype
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
&& comps
== 1) {
1395 DECLARE_ROW_POINTERS(GLfloat
, 2);
1397 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1398 i
++, j
+= colStride
, k
+= colStride
) {
1404 _mesa_problem(NULL
, "bad format in do_row()");
1410 * These functions generate a 1/2-size mipmap image from a source image.
1411 * Texture borders are handled by copying or averaging the source image's
1412 * border texels, depending on the scale-down factor.
1416 make_1d_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
1417 GLint srcWidth
, const GLubyte
*srcPtr
,
1418 GLint dstWidth
, GLubyte
*dstPtr
)
1420 const GLint bpt
= bytes_per_pixel(datatype
, comps
);
1424 /* skip the border pixel, if any */
1425 src
= srcPtr
+ border
* bpt
;
1426 dst
= dstPtr
+ border
* bpt
;
1428 /* we just duplicate the input row, kind of hack, saves code */
1429 do_row(datatype
, comps
, srcWidth
- 2 * border
, src
, src
,
1430 dstWidth
- 2 * border
, dst
);
1433 /* copy left-most pixel from source */
1436 memcpy(dstPtr
, srcPtr
, bpt
);
1437 /* copy right-most pixel from source */
1438 memcpy(dstPtr
+ (dstWidth
- 1) * bpt
,
1439 srcPtr
+ (srcWidth
- 1) * bpt
,
1446 make_2d_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
1447 GLint srcWidth
, GLint srcHeight
,
1448 const GLubyte
*srcPtr
, GLint srcRowStride
,
1449 GLint dstWidth
, GLint dstHeight
,
1450 GLubyte
*dstPtr
, GLint dstRowStride
)
1452 const GLint bpt
= bytes_per_pixel(datatype
, comps
);
1453 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
1454 const GLint dstWidthNB
= dstWidth
- 2 * border
;
1455 const GLint dstHeightNB
= dstHeight
- 2 * border
;
1456 const GLubyte
*srcA
, *srcB
;
1458 GLint row
, srcRowStep
;
1460 /* Compute src and dst pointers, skipping any border */
1461 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
1462 if (srcHeight
> 1 && srcHeight
> dstHeight
) {
1463 /* sample from two source rows */
1464 srcB
= srcA
+ srcRowStride
;
1468 /* sample from one source row */
1473 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
1475 for (row
= 0; row
< dstHeightNB
; row
++) {
1476 do_row(datatype
, comps
, srcWidthNB
, srcA
, srcB
,
1478 srcA
+= srcRowStep
* srcRowStride
;
1479 srcB
+= srcRowStep
* srcRowStride
;
1480 dst
+= dstRowStride
;
1483 /* This is ugly but probably won't be used much */
1485 /* fill in dest border */
1486 /* lower-left border pixel */
1489 memcpy(dstPtr
, srcPtr
, bpt
);
1490 /* lower-right border pixel */
1491 memcpy(dstPtr
+ (dstWidth
- 1) * bpt
,
1492 srcPtr
+ (srcWidth
- 1) * bpt
, bpt
);
1493 /* upper-left border pixel */
1494 memcpy(dstPtr
+ dstWidth
* (dstHeight
- 1) * bpt
,
1495 srcPtr
+ srcWidth
* (srcHeight
- 1) * bpt
, bpt
);
1496 /* upper-right border pixel */
1497 memcpy(dstPtr
+ (dstWidth
* dstHeight
- 1) * bpt
,
1498 srcPtr
+ (srcWidth
* srcHeight
- 1) * bpt
, bpt
);
1500 do_row(datatype
, comps
, srcWidthNB
,
1503 dstWidthNB
, dstPtr
+ bpt
);
1505 do_row(datatype
, comps
, srcWidthNB
,
1506 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
1507 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
1509 dstPtr
+ (dstWidth
* (dstHeight
- 1) + 1) * bpt
);
1510 /* left and right borders */
1511 if (srcHeight
== dstHeight
) {
1512 /* copy border pixel from src to dst */
1513 for (row
= 1; row
< srcHeight
; row
++) {
1514 memcpy(dstPtr
+ dstWidth
* row
* bpt
,
1515 srcPtr
+ srcWidth
* row
* bpt
, bpt
);
1516 memcpy(dstPtr
+ (dstWidth
* row
+ dstWidth
- 1) * bpt
,
1517 srcPtr
+ (srcWidth
* row
+ srcWidth
- 1) * bpt
, bpt
);
1521 /* average two src pixels each dest pixel */
1522 for (row
= 0; row
< dstHeightNB
; row
+= 2) {
1523 do_row(datatype
, comps
, 1,
1524 srcPtr
+ (srcWidth
* (row
* 2 + 1)) * bpt
,
1525 srcPtr
+ (srcWidth
* (row
* 2 + 2)) * bpt
,
1526 1, dstPtr
+ (dstWidth
* row
+ 1) * bpt
);
1527 do_row(datatype
, comps
, 1,
1528 srcPtr
+ (srcWidth
* (row
* 2 + 1) + srcWidth
- 1) * bpt
,
1529 srcPtr
+ (srcWidth
* (row
* 2 + 2) + srcWidth
- 1) * bpt
,
1530 1, dstPtr
+ (dstWidth
* row
+ 1 + dstWidth
- 1) * bpt
);
1538 make_3d_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
1539 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1540 const GLubyte
**srcPtr
, GLint srcRowStride
,
1541 GLint dstWidth
, GLint dstHeight
, GLint dstDepth
,
1542 GLubyte
**dstPtr
, GLint dstRowStride
)
1544 const GLint bpt
= bytes_per_pixel(datatype
, comps
);
1545 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
1546 const GLint srcDepthNB
= srcDepth
- 2 * border
;
1547 const GLint dstWidthNB
= dstWidth
- 2 * border
;
1548 const GLint dstHeightNB
= dstHeight
- 2 * border
;
1549 const GLint dstDepthNB
= dstDepth
- 2 * border
;
1551 GLint bytesPerSrcImage
, bytesPerDstImage
;
1552 GLint srcImageOffset
, srcRowOffset
;
1554 (void) srcDepthNB
; /* silence warnings */
1556 bytesPerSrcImage
= srcRowStride
* srcHeight
* bpt
;
1557 bytesPerDstImage
= dstRowStride
* dstHeight
* bpt
;
1559 /* Offset between adjacent src images to be averaged together */
1560 srcImageOffset
= (srcDepth
== dstDepth
) ? 0 : 1;
1562 /* Offset between adjacent src rows to be averaged together */
1563 srcRowOffset
= (srcHeight
== dstHeight
) ? 0 : srcRowStride
;
1566 * Need to average together up to 8 src pixels for each dest pixel.
1567 * Break that down into 3 operations:
1568 * 1. take two rows from source image and average them together.
1569 * 2. take two rows from next source image and average them together.
1570 * 3. take the two averaged rows and average them for the final dst row.
1574 printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1575 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1578 for (img
= 0; img
< dstDepthNB
; img
++) {
1579 /* first source image pointer, skipping border */
1580 const GLubyte
*imgSrcA
= srcPtr
[img
* 2 + border
]
1581 + srcRowStride
* border
+ bpt
* border
;
1582 /* second source image pointer, skipping border */
1583 const GLubyte
*imgSrcB
= srcPtr
[img
* 2 + srcImageOffset
+ border
]
1584 + srcRowStride
* border
+ bpt
* border
;
1586 /* address of the dest image, skipping border */
1587 GLubyte
*imgDst
= dstPtr
[img
+ border
]
1588 + dstRowStride
* border
+ bpt
* border
;
1590 /* setup the four source row pointers and the dest row pointer */
1591 const GLubyte
*srcImgARowA
= imgSrcA
;
1592 const GLubyte
*srcImgARowB
= imgSrcA
+ srcRowOffset
;
1593 const GLubyte
*srcImgBRowA
= imgSrcB
;
1594 const GLubyte
*srcImgBRowB
= imgSrcB
+ srcRowOffset
;
1595 GLubyte
*dstImgRow
= imgDst
;
1597 for (row
= 0; row
< dstHeightNB
; row
++) {
1598 do_row_3D(datatype
, comps
, srcWidthNB
,
1599 srcImgARowA
, srcImgARowB
,
1600 srcImgBRowA
, srcImgBRowB
,
1601 dstWidthNB
, dstImgRow
);
1603 /* advance to next rows */
1604 srcImgARowA
+= srcRowStride
+ srcRowOffset
;
1605 srcImgARowB
+= srcRowStride
+ srcRowOffset
;
1606 srcImgBRowA
+= srcRowStride
+ srcRowOffset
;
1607 srcImgBRowB
+= srcRowStride
+ srcRowOffset
;
1608 dstImgRow
+= dstRowStride
;
1613 /* Luckily we can leverage the make_2d_mipmap() function here! */
1615 /* do front border image */
1616 make_2d_mipmap(datatype
, comps
, 1,
1617 srcWidth
, srcHeight
, srcPtr
[0], srcRowStride
,
1618 dstWidth
, dstHeight
, dstPtr
[0], dstRowStride
);
1619 /* do back border image */
1620 make_2d_mipmap(datatype
, comps
, 1,
1621 srcWidth
, srcHeight
, srcPtr
[srcDepth
- 1], srcRowStride
,
1622 dstWidth
, dstHeight
, dstPtr
[dstDepth
- 1], dstRowStride
);
1624 /* do four remaining border edges that span the image slices */
1625 if (srcDepth
== dstDepth
) {
1626 /* just copy border pixels from src to dst */
1627 for (img
= 0; img
< dstDepthNB
; img
++) {
1631 /* do border along [img][row=0][col=0] */
1632 src
= srcPtr
[img
* 2];
1634 memcpy(dst
, src
, bpt
);
1636 /* do border along [img][row=dstHeight-1][col=0] */
1637 src
= srcPtr
[img
* 2] + (srcHeight
- 1) * srcRowStride
;
1638 dst
= dstPtr
[img
] + (dstHeight
- 1) * dstRowStride
;
1639 memcpy(dst
, src
, bpt
);
1641 /* do border along [img][row=0][col=dstWidth-1] */
1642 src
= srcPtr
[img
* 2] + (srcWidth
- 1) * bpt
;
1643 dst
= dstPtr
[img
] + (dstWidth
- 1) * bpt
;
1644 memcpy(dst
, src
, bpt
);
1646 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1647 src
= srcPtr
[img
* 2] + (bytesPerSrcImage
- bpt
);
1648 dst
= dstPtr
[img
] + (bytesPerDstImage
- bpt
);
1649 memcpy(dst
, src
, bpt
);
1653 /* average border pixels from adjacent src image pairs */
1654 assert(srcDepthNB
== 2 * dstDepthNB
);
1655 for (img
= 0; img
< dstDepthNB
; img
++) {
1656 const GLubyte
*srcA
, *srcB
;
1659 /* do border along [img][row=0][col=0] */
1660 srcA
= srcPtr
[img
* 2 + 0];
1661 srcB
= srcPtr
[img
* 2 + srcImageOffset
];
1663 do_row(datatype
, comps
, 1, srcA
, srcB
, 1, dst
);
1665 /* do border along [img][row=dstHeight-1][col=0] */
1666 srcA
= srcPtr
[img
* 2 + 0]
1667 + (srcHeight
- 1) * srcRowStride
;
1668 srcB
= srcPtr
[img
* 2 + srcImageOffset
]
1669 + (srcHeight
- 1) * srcRowStride
;
1670 dst
= dstPtr
[img
] + (dstHeight
- 1) * dstRowStride
;
1671 do_row(datatype
, comps
, 1, srcA
, srcB
, 1, dst
);
1673 /* do border along [img][row=0][col=dstWidth-1] */
1674 srcA
= srcPtr
[img
* 2 + 0] + (srcWidth
- 1) * bpt
;
1675 srcB
= srcPtr
[img
* 2 + srcImageOffset
] + (srcWidth
- 1) * bpt
;
1676 dst
= dstPtr
[img
] + (dstWidth
- 1) * bpt
;
1677 do_row(datatype
, comps
, 1, srcA
, srcB
, 1, dst
);
1679 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1680 srcA
= srcPtr
[img
* 2 + 0] + (bytesPerSrcImage
- bpt
);
1681 srcB
= srcPtr
[img
* 2 + srcImageOffset
] + (bytesPerSrcImage
- bpt
);
1682 dst
= dstPtr
[img
] + (bytesPerDstImage
- bpt
);
1683 do_row(datatype
, comps
, 1, srcA
, srcB
, 1, dst
);
1691 * Down-sample a texture image to produce the next lower mipmap level.
1692 * \param comps components per texel (1, 2, 3 or 4)
1693 * \param srcData array[slice] of pointers to source image slices
1694 * \param dstData array[slice] of pointers to dest image slices
1695 * \param srcRowStride stride between source rows, in bytes
1696 * \param dstRowStride stride between destination rows, in bytes
1699 _mesa_generate_mipmap_level(GLenum target
,
1700 GLenum datatype
, GLuint comps
,
1702 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1703 const GLubyte
**srcData
,
1705 GLint dstWidth
, GLint dstHeight
, GLint dstDepth
,
1713 make_1d_mipmap(datatype
, comps
, border
,
1714 srcWidth
, srcData
[0],
1715 dstWidth
, dstData
[0]);
1718 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1719 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1720 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1721 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1722 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1723 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1724 make_2d_mipmap(datatype
, comps
, border
,
1725 srcWidth
, srcHeight
, srcData
[0], srcRowStride
,
1726 dstWidth
, dstHeight
, dstData
[0], dstRowStride
);
1729 make_3d_mipmap(datatype
, comps
, border
,
1730 srcWidth
, srcHeight
, srcDepth
,
1731 srcData
, srcRowStride
,
1732 dstWidth
, dstHeight
, dstDepth
,
1733 dstData
, dstRowStride
);
1735 case GL_TEXTURE_1D_ARRAY_EXT
:
1736 assert(srcHeight
== 1);
1737 assert(dstHeight
== 1);
1738 for (i
= 0; i
< dstDepth
; i
++) {
1739 make_1d_mipmap(datatype
, comps
, border
,
1740 srcWidth
, srcData
[i
],
1741 dstWidth
, dstData
[i
]);
1744 case GL_TEXTURE_2D_ARRAY_EXT
:
1745 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1746 for (i
= 0; i
< dstDepth
; i
++) {
1747 make_2d_mipmap(datatype
, comps
, border
,
1748 srcWidth
, srcHeight
, srcData
[i
], srcRowStride
,
1749 dstWidth
, dstHeight
, dstData
[i
], dstRowStride
);
1752 case GL_TEXTURE_RECTANGLE_NV
:
1753 case GL_TEXTURE_EXTERNAL_OES
:
1754 /* no mipmaps, do nothing */
1757 _mesa_problem(NULL
, "bad tex target in _mesa_generate_mipmaps");
1764 * compute next (level+1) image size
1765 * \return GL_FALSE if no smaller size can be generated (eg. src is 1x1x1 size)
1768 _mesa_next_mipmap_level_size(GLenum target
, GLint border
,
1769 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1770 GLint
*dstWidth
, GLint
*dstHeight
, GLint
*dstDepth
)
1772 if (srcWidth
- 2 * border
> 1) {
1773 *dstWidth
= (srcWidth
- 2 * border
) / 2 + 2 * border
;
1776 *dstWidth
= srcWidth
; /* can't go smaller */
1779 if ((srcHeight
- 2 * border
> 1) &&
1780 (target
!= GL_TEXTURE_1D_ARRAY_EXT
)) {
1781 *dstHeight
= (srcHeight
- 2 * border
) / 2 + 2 * border
;
1784 *dstHeight
= srcHeight
; /* can't go smaller */
1787 if ((srcDepth
- 2 * border
> 1) &&
1788 (target
!= GL_TEXTURE_2D_ARRAY_EXT
&&
1789 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
)) {
1790 *dstDepth
= (srcDepth
- 2 * border
) / 2 + 2 * border
;
1793 *dstDepth
= srcDepth
; /* can't go smaller */
1796 if (*dstWidth
== srcWidth
&&
1797 *dstHeight
== srcHeight
&&
1798 *dstDepth
== srcDepth
) {
1808 * Helper function for mipmap generation.
1809 * Make sure the specified destination mipmap level is the right size/format
1810 * for mipmap generation. If not, (re) allocate it.
1811 * \return GL_TRUE if successful, GL_FALSE if mipmap generation should stop
1814 _mesa_prepare_mipmap_level(struct gl_context
*ctx
,
1815 struct gl_texture_object
*texObj
, GLuint level
,
1816 GLsizei width
, GLsizei height
, GLsizei depth
,
1817 GLsizei border
, GLenum intFormat
, mesa_format format
)
1819 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1822 if (texObj
->Immutable
) {
1823 /* The texture was created with glTexStorage() so the number/size of
1824 * mipmap levels is fixed and the storage for all images is already
1827 if (!texObj
->Image
[0][level
]) {
1828 /* No more levels to create - we're done */
1832 /* Nothing to do - the texture memory must have already been
1833 * allocated to the right size so we're all set.
1839 for (face
= 0; face
< numFaces
; face
++) {
1840 struct gl_texture_image
*dstImage
;
1841 const GLenum target
= _mesa_cube_face_target(texObj
->Target
, face
);
1843 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
1849 if (dstImage
->Width
!= width
||
1850 dstImage
->Height
!= height
||
1851 dstImage
->Depth
!= depth
||
1852 dstImage
->Border
!= border
||
1853 dstImage
->InternalFormat
!= intFormat
||
1854 dstImage
->TexFormat
!= format
) {
1855 /* need to (re)allocate image */
1856 ctx
->Driver
.FreeTextureImageBuffer(ctx
, dstImage
);
1858 _mesa_init_teximage_fields(ctx
, dstImage
,
1859 width
, height
, depth
,
1860 border
, intFormat
, format
);
1862 ctx
->Driver
.AllocTextureImageBuffer(ctx
, dstImage
);
1864 /* in case the mipmap level is part of an FBO: */
1865 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
1867 ctx
->NewState
|= _NEW_TEXTURE
;
1876 generate_mipmap_uncompressed(struct gl_context
*ctx
, GLenum target
,
1877 struct gl_texture_object
*texObj
,
1878 const struct gl_texture_image
*srcImage
,
1885 _mesa_uncompressed_format_to_type_and_comps(srcImage
->TexFormat
, &datatype
, &comps
);
1887 for (level
= texObj
->BaseLevel
; level
< maxLevel
; level
++) {
1888 /* generate image[level+1] from image[level] */
1889 struct gl_texture_image
*srcImage
, *dstImage
;
1890 GLint srcRowStride
, dstRowStride
;
1891 GLint srcWidth
, srcHeight
, srcDepth
;
1892 GLint dstWidth
, dstHeight
, dstDepth
;
1895 GLboolean nextLevel
;
1896 GLubyte
**srcMaps
, **dstMaps
;
1897 GLboolean success
= GL_TRUE
;
1899 /* get src image parameters */
1900 srcImage
= _mesa_select_tex_image(texObj
, target
, level
);
1902 srcWidth
= srcImage
->Width
;
1903 srcHeight
= srcImage
->Height
;
1904 srcDepth
= srcImage
->Depth
;
1905 border
= srcImage
->Border
;
1907 nextLevel
= _mesa_next_mipmap_level_size(target
, border
,
1908 srcWidth
, srcHeight
, srcDepth
,
1909 &dstWidth
, &dstHeight
, &dstDepth
);
1913 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, level
+ 1,
1914 dstWidth
, dstHeight
, dstDepth
,
1915 border
, srcImage
->InternalFormat
,
1916 srcImage
->TexFormat
)) {
1920 /* get dest gl_texture_image */
1921 dstImage
= _mesa_select_tex_image(texObj
, target
, level
+ 1);
1924 if (target
== GL_TEXTURE_1D_ARRAY
) {
1925 srcDepth
= srcHeight
;
1926 dstDepth
= dstHeight
;
1931 /* Map src texture image slices */
1932 srcMaps
= calloc(srcDepth
, sizeof(GLubyte
*));
1934 for (slice
= 0; slice
< srcDepth
; slice
++) {
1935 ctx
->Driver
.MapTextureImage(ctx
, srcImage
, slice
,
1936 0, 0, srcWidth
, srcHeight
,
1938 &srcMaps
[slice
], &srcRowStride
);
1939 if (!srcMaps
[slice
]) {
1949 /* Map dst texture image slices */
1950 dstMaps
= calloc(dstDepth
, sizeof(GLubyte
*));
1952 for (slice
= 0; slice
< dstDepth
; slice
++) {
1953 ctx
->Driver
.MapTextureImage(ctx
, dstImage
, slice
,
1954 0, 0, dstWidth
, dstHeight
,
1956 &dstMaps
[slice
], &dstRowStride
);
1957 if (!dstMaps
[slice
]) {
1968 /* generate one mipmap level (for 1D/2D/3D/array/etc texture) */
1969 _mesa_generate_mipmap_level(target
, datatype
, comps
, border
,
1970 srcWidth
, srcHeight
, srcDepth
,
1971 (const GLubyte
**) srcMaps
, srcRowStride
,
1972 dstWidth
, dstHeight
, dstDepth
,
1973 dstMaps
, dstRowStride
);
1976 /* Unmap src image slices */
1978 for (slice
= 0; slice
< srcDepth
; slice
++) {
1979 if (srcMaps
[slice
]) {
1980 ctx
->Driver
.UnmapTextureImage(ctx
, srcImage
, slice
);
1986 /* Unmap dst image slices */
1988 for (slice
= 0; slice
< dstDepth
; slice
++) {
1989 if (dstMaps
[slice
]) {
1990 ctx
->Driver
.UnmapTextureImage(ctx
, dstImage
, slice
);
1997 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "mipmap generation");
2000 } /* loop over mipmap levels */
2005 generate_mipmap_compressed(struct gl_context
*ctx
, GLenum target
,
2006 struct gl_texture_object
*texObj
,
2007 struct gl_texture_image
*srcImage
,
2011 mesa_format temp_format
;
2013 GLuint temp_src_row_stride
, temp_src_img_stride
; /* in bytes */
2014 GLubyte
*temp_src
= NULL
, *temp_dst
= NULL
;
2015 GLenum temp_datatype
;
2016 GLenum temp_base_format
;
2017 GLubyte
**temp_src_slices
= NULL
, **temp_dst_slices
= NULL
;
2019 /* only two types of compressed textures at this time */
2020 assert(texObj
->Target
== GL_TEXTURE_2D
||
2021 texObj
->Target
== GL_TEXTURE_2D_ARRAY
||
2022 texObj
->Target
== GL_TEXTURE_CUBE_MAP
||
2023 texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
);
2026 * Choose a format for the temporary, uncompressed base image.
2027 * Then, get number of components, choose temporary image datatype,
2028 * and get base format.
2030 temp_format
= _mesa_get_uncompressed_format(srcImage
->TexFormat
);
2032 components
= _mesa_format_num_components(temp_format
);
2034 switch (_mesa_get_format_datatype(srcImage
->TexFormat
)) {
2036 temp_datatype
= GL_FLOAT
;
2038 case GL_SIGNED_NORMALIZED
:
2039 /* Revisit this if we get compressed formats with >8 bits per component */
2040 temp_datatype
= GL_BYTE
;
2043 temp_datatype
= GL_UNSIGNED_BYTE
;
2046 temp_base_format
= _mesa_get_format_base_format(temp_format
);
2049 /* allocate storage for the temporary, uncompressed image */
2050 temp_src_row_stride
= _mesa_format_row_stride(temp_format
, srcImage
->Width
);
2051 temp_src_img_stride
= _mesa_format_image_size(temp_format
, srcImage
->Width
,
2052 srcImage
->Height
, 1);
2053 temp_src
= malloc(temp_src_img_stride
* srcImage
->Depth
);
2055 /* Allocate storage for arrays of slice pointers */
2056 temp_src_slices
= malloc(srcImage
->Depth
* sizeof(GLubyte
*));
2057 temp_dst_slices
= malloc(srcImage
->Depth
* sizeof(GLubyte
*));
2059 if (!temp_src
|| !temp_src_slices
|| !temp_dst_slices
) {
2060 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
2064 /* decompress base image to the temporary src buffer */
2066 /* save pixel packing mode */
2067 struct gl_pixelstore_attrib save
= ctx
->Pack
;
2068 /* use default/tight packing parameters */
2069 ctx
->Pack
= ctx
->DefaultPacking
;
2071 /* Get the uncompressed image */
2072 assert(srcImage
->Level
== texObj
->BaseLevel
);
2073 ctx
->Driver
.GetTexSubImage(ctx
,
2075 srcImage
->Width
, srcImage
->Height
,
2077 temp_base_format
, temp_datatype
,
2078 temp_src
, srcImage
);
2079 /* restore packing mode */
2083 for (level
= texObj
->BaseLevel
; level
< maxLevel
; level
++) {
2084 /* generate image[level+1] from image[level] */
2085 const struct gl_texture_image
*srcImage
;
2086 struct gl_texture_image
*dstImage
;
2087 GLint srcWidth
, srcHeight
, srcDepth
;
2088 GLint dstWidth
, dstHeight
, dstDepth
;
2090 GLboolean nextLevel
;
2091 GLuint temp_dst_row_stride
, temp_dst_img_stride
; /* in bytes */
2094 /* get src image parameters */
2095 srcImage
= _mesa_select_tex_image(texObj
, target
, level
);
2097 srcWidth
= srcImage
->Width
;
2098 srcHeight
= srcImage
->Height
;
2099 srcDepth
= srcImage
->Depth
;
2100 border
= srcImage
->Border
;
2102 nextLevel
= _mesa_next_mipmap_level_size(target
, border
,
2103 srcWidth
, srcHeight
, srcDepth
,
2104 &dstWidth
, &dstHeight
, &dstDepth
);
2108 if (!_mesa_prepare_mipmap_level(ctx
, texObj
, level
+ 1,
2109 dstWidth
, dstHeight
, dstDepth
,
2110 border
, srcImage
->InternalFormat
,
2111 srcImage
->TexFormat
)) {
2116 /* get dest gl_texture_image */
2117 dstImage
= _mesa_select_tex_image(texObj
, target
, level
+ 1);
2120 /* Compute dst image strides and alloc memory on first iteration */
2121 temp_dst_row_stride
= _mesa_format_row_stride(temp_format
, dstWidth
);
2122 temp_dst_img_stride
= _mesa_format_image_size(temp_format
, dstWidth
,
2125 temp_dst
= malloc(temp_dst_img_stride
* dstDepth
);
2127 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
2132 /* for 2D arrays, setup array[depth] of slice pointers */
2133 for (i
= 0; i
< srcDepth
; i
++) {
2134 temp_src_slices
[i
] = temp_src
+ temp_src_img_stride
* i
;
2136 for (i
= 0; i
< dstDepth
; i
++) {
2137 temp_dst_slices
[i
] = temp_dst
+ temp_dst_img_stride
* i
;
2140 /* Rescale src image to dest image.
2141 * This will loop over the slices of a 2D array.
2143 _mesa_generate_mipmap_level(target
, temp_datatype
, components
, border
,
2144 srcWidth
, srcHeight
, srcDepth
,
2145 (const GLubyte
**) temp_src_slices
,
2146 temp_src_row_stride
,
2147 dstWidth
, dstHeight
, dstDepth
,
2148 temp_dst_slices
, temp_dst_row_stride
);
2150 /* The image space was allocated above so use glTexSubImage now */
2151 ctx
->Driver
.TexSubImage(ctx
, 2, dstImage
,
2152 0, 0, 0, dstWidth
, dstHeight
, dstDepth
,
2153 temp_base_format
, temp_datatype
,
2154 temp_dst
, &ctx
->DefaultPacking
);
2156 /* swap src and dest pointers */
2158 GLubyte
*temp
= temp_src
;
2159 temp_src
= temp_dst
;
2161 temp_src_row_stride
= temp_dst_row_stride
;
2162 temp_src_img_stride
= temp_dst_img_stride
;
2164 } /* loop over mipmap levels */
2169 free(temp_src_slices
);
2170 free(temp_dst_slices
);
2174 * Automatic mipmap generation.
2175 * This is the fallback/default function for ctx->Driver.GenerateMipmap().
2176 * Generate a complete set of mipmaps from texObj's BaseLevel image.
2177 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
2178 * For cube maps, target will be one of
2179 * GL_TEXTURE_CUBE_MAP_POSITIVE/NEGATIVE_X/Y/Z; never GL_TEXTURE_CUBE_MAP.
2182 _mesa_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
2183 struct gl_texture_object
*texObj
)
2185 struct gl_texture_image
*srcImage
;
2189 srcImage
= _mesa_select_tex_image(texObj
, target
, texObj
->BaseLevel
);
2192 maxLevel
= _mesa_max_texture_levels(ctx
, texObj
->Target
) - 1;
2193 assert(maxLevel
>= 0); /* bad target */
2195 maxLevel
= MIN2(maxLevel
, texObj
->MaxLevel
);
2197 if (_mesa_is_format_compressed(srcImage
->TexFormat
)) {
2198 generate_mipmap_compressed(ctx
, target
, texObj
, srcImage
, maxLevel
);
2200 generate_mipmap_uncompressed(ctx
, target
, texObj
, srcImage
, maxLevel
);