2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 * The GL texture image functions in teximage.c basically just do
32 * error checking and data structure allocation. They in turn call
33 * device driver functions which actually copy/convert/store the user's
36 * However, most device drivers will be able to use the fallback functions
37 * in this file. That is, most drivers will have the following bit of
39 * ctx->Driver.TexImage1D = _mesa_store_teximage1d;
40 * ctx->Driver.TexImage2D = _mesa_store_teximage2d;
41 * ctx->Driver.TexImage3D = _mesa_store_teximage3d;
44 * Texture image processing is actually kind of complicated. We have to do:
45 * Format/type conversions
47 * pixel transfer (scale, bais, lookup, convolution!, etc)
49 * These functions can handle most everything, including processing full
50 * images and sub-images.
61 #include "texcompress.h"
62 #include "texformat.h"
69 * Given an internal texture format enum or 1, 2, 3, 4 return the
70 * corresponding _base_ internal format: GL_ALPHA, GL_LUMINANCE,
71 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA. Return the
72 * number of components for the format. Return -1 if invalid enum.
75 components_in_intformat( GLint format
)
92 case GL_LUMINANCE_ALPHA
:
93 case GL_LUMINANCE4_ALPHA4
:
94 case GL_LUMINANCE6_ALPHA2
:
95 case GL_LUMINANCE8_ALPHA8
:
96 case GL_LUMINANCE12_ALPHA4
:
97 case GL_LUMINANCE12_ALPHA12
:
98 case GL_LUMINANCE16_ALPHA16
:
127 case GL_COLOR_INDEX1_EXT
:
128 case GL_COLOR_INDEX2_EXT
:
129 case GL_COLOR_INDEX4_EXT
:
130 case GL_COLOR_INDEX8_EXT
:
131 case GL_COLOR_INDEX12_EXT
:
132 case GL_COLOR_INDEX16_EXT
:
134 case GL_DEPTH_COMPONENT
:
135 case GL_DEPTH_COMPONENT16_SGIX
:
136 case GL_DEPTH_COMPONENT24_SGIX
:
137 case GL_DEPTH_COMPONENT32_SGIX
:
140 return 2; /* Y + (Cb or Cr) */
142 return -1; /* error */
148 * This function is used to transfer the user's image data into a texture
149 * image buffer. We handle both full texture images and subtexture images.
150 * We also take care of all image transfer operations here, including
151 * convolution, scale/bias, colortables, etc.
153 * The destination texel type is always GLchan.
154 * The destination texel format is one of the 6 basic types.
156 * A hardware driver may use this as a helper routine to unpack and
157 * apply pixel transfer ops into a temporary image buffer. Then,
158 * convert the temporary image into the special hardware format.
161 * dimensions - 1, 2, or 3
162 * texDestFormat - GL_LUMINANCE, GL_INTENSITY, GL_LUMINANCE_ALPHA, GL_ALPHA,
163 * GL_RGB or GL_RGBA (the destination format)
164 * texDestAddr - destination image address
165 * srcWidth, srcHeight, srcDepth - size (in pixels) of src and dest images
166 * dstXoffset, dstYoffset, dstZoffset - position to store the image within
167 * the destination 3D texture
168 * dstRowStride, dstImageStride - dest image strides in bytes
169 * srcFormat - source image format (GL_ALPHA, GL_RED, GL_RGB, etc)
170 * srcType - GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5, GL_FLOAT, etc
171 * srcPacking - describes packing of incoming image.
172 * transferOps - mask of pixel transfer operations
175 transfer_teximage(GLcontext
*ctx
, GLuint dimensions
,
176 GLenum texDestFormat
, GLvoid
*texDestAddr
,
177 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
178 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
179 GLint dstRowStride
, GLint dstImageStride
,
180 GLenum srcFormat
, GLenum srcType
,
181 const GLvoid
*srcAddr
,
182 const struct gl_pixelstore_attrib
*srcPacking
,
188 ASSERT(dimensions
>= 1 && dimensions
<= 3);
189 ASSERT(texDestFormat
== GL_LUMINANCE
||
190 texDestFormat
== GL_INTENSITY
||
191 texDestFormat
== GL_LUMINANCE_ALPHA
||
192 texDestFormat
== GL_ALPHA
||
193 texDestFormat
== GL_RGB
||
194 texDestFormat
== GL_RGBA
||
195 texDestFormat
== GL_COLOR_INDEX
||
196 texDestFormat
== GL_DEPTH_COMPONENT
);
198 ASSERT(srcWidth
>= 0);
199 ASSERT(srcHeight
>= 0);
200 ASSERT(srcDepth
>= 0);
201 ASSERT(dstXoffset
>= 0);
202 ASSERT(dstYoffset
>= 0);
203 ASSERT(dstZoffset
>= 0);
204 ASSERT(dstRowStride
>= 0);
205 ASSERT(dstImageStride
>= 0);
209 texComponents
= components_in_intformat(texDestFormat
);
211 /* try common 2D texture cases first */
212 if (!transferOps
&& dimensions
== 2 && srcType
== CHAN_TYPE
) {
214 if (srcFormat
== texDestFormat
) {
215 /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA,
216 * GL_LUMINANCE_ALPHA, etc. texture formats. Use memcpy().
218 const GLchan
*src
= (const GLchan
*) _mesa_image_address(
219 srcPacking
, srcAddr
, srcWidth
, srcHeight
,
220 srcFormat
, srcType
, 0, 0, 0);
221 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
222 srcWidth
, srcFormat
, srcType
);
223 const GLint widthInBytes
= srcWidth
* texComponents
* sizeof(GLchan
);
224 GLchan
*dst
= (GLchan
*) texDestAddr
225 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
226 + dstXoffset
* texComponents
;
227 if (srcRowStride
== widthInBytes
&& dstRowStride
== widthInBytes
) {
228 MEMCPY(dst
, src
, srcHeight
* widthInBytes
);
232 for (i
= 0; i
< srcHeight
; i
++) {
233 MEMCPY(dst
, src
, widthInBytes
);
234 src
+= (srcRowStride
/ sizeof(GLchan
));
235 dst
+= (dstRowStride
/ sizeof(GLchan
));
238 return; /* all done */
240 else if (srcFormat
== GL_RGBA
&& texDestFormat
== GL_RGB
) {
241 /* commonly used by Quake */
242 const GLchan
*src
= (const GLchan
*) _mesa_image_address(
243 srcPacking
, srcAddr
, srcWidth
, srcHeight
,
244 srcFormat
, srcType
, 0, 0, 0);
245 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
246 srcWidth
, srcFormat
, srcType
);
247 GLchan
*dst
= (GLchan
*) texDestAddr
248 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
249 + dstXoffset
* texComponents
;
251 for (i
= 0; i
< srcHeight
; i
++) {
252 const GLchan
*s
= src
;
254 for (j
= 0; j
< srcWidth
; j
++) {
256 *d
++ = *s
++; /*green*/
257 *d
++ = *s
++; /*blue*/
260 src
+= (srcRowStride
/ sizeof(GLchan
));
261 dst
+= (dstRowStride
/ sizeof(GLchan
));
263 return; /* all done */
268 * General case solutions
270 if (texDestFormat
== GL_COLOR_INDEX
) {
271 /* color index texture */
272 const GLenum texType
= CHAN_TYPE
;
274 GLchan
*dest
= (GLchan
*) texDestAddr
275 + dstZoffset
* (dstImageStride
/ sizeof(GLchan
))
276 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
277 + dstXoffset
* texComponents
;
278 for (img
= 0; img
< srcDepth
; img
++) {
279 GLchan
*destRow
= dest
;
280 for (row
= 0; row
< srcHeight
; row
++) {
281 const GLvoid
*src
= _mesa_image_address(srcPacking
,
282 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
283 _mesa_unpack_index_span(ctx
, srcWidth
, texType
, destRow
,
284 srcType
, src
, srcPacking
, transferOps
);
285 destRow
+= (dstRowStride
/ sizeof(GLchan
));
287 dest
+= dstImageStride
;
290 else if (texDestFormat
== GL_YCBCR_MESA
) {
293 GLushort
*dest
= (GLushort
*) texDestAddr
294 + dstZoffset
* (dstImageStride
/ sizeof(GLushort
))
295 + dstYoffset
* (dstRowStride
/ sizeof(GLushort
))
296 + dstXoffset
* texComponents
;
297 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
298 for (img
= 0; img
< srcDepth
; img
++) {
299 GLushort
*destRow
= dest
;
300 for (row
= 0; row
< srcHeight
; row
++) {
301 const GLvoid
*srcRow
= _mesa_image_address(srcPacking
,
302 srcAddr
, srcWidth
, srcHeight
,
303 srcFormat
, srcType
, img
, row
, 0);
304 MEMCPY(destRow
, srcRow
, srcWidth
* sizeof(GLushort
));
305 destRow
+= (dstRowStride
/ sizeof(GLushort
));
307 dest
+= dstImageStride
/ sizeof(GLushort
);
310 else if (texDestFormat
== GL_DEPTH_COMPONENT
) {
311 /* Depth texture (shadow maps) */
313 GLubyte
*dest
= (GLubyte
*) texDestAddr
314 + dstZoffset
* dstImageStride
315 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
316 + dstXoffset
* texComponents
;
317 for (img
= 0; img
< srcDepth
; img
++) {
318 GLubyte
*destRow
= dest
;
319 for (row
= 0; row
< srcHeight
; row
++) {
320 const GLvoid
*src
= _mesa_image_address(srcPacking
,
321 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
322 _mesa_unpack_depth_span(ctx
, srcWidth
, (GLfloat
*) destRow
,
323 srcType
, src
, srcPacking
);
324 destRow
+= (dstRowStride
/ sizeof(GLchan
));
326 dest
+= dstImageStride
;
330 /* regular, color texture */
331 if ((dimensions
== 1 && ctx
->Pixel
.Convolution1DEnabled
) ||
332 (dimensions
>= 2 && ctx
->Pixel
.Convolution2DEnabled
) ||
333 (dimensions
>= 2 && ctx
->Pixel
.Separable2DEnabled
)) {
335 * Fill texture image with convolution
338 GLint convWidth
= srcWidth
, convHeight
= srcHeight
;
339 GLfloat
*tmpImage
, *convImage
;
340 tmpImage
= (GLfloat
*) MALLOC(srcWidth
* srcHeight
* 4 * sizeof(GLfloat
));
342 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
345 convImage
= (GLfloat
*) MALLOC(srcWidth
* srcHeight
* 4 * sizeof(GLfloat
));
347 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
352 for (img
= 0; img
< srcDepth
; img
++) {
354 GLfloat
*dstf
= tmpImage
;
357 /* unpack and do transfer ops up to convolution */
358 for (row
= 0; row
< srcHeight
; row
++) {
359 const GLvoid
*src
= _mesa_image_address(srcPacking
,
360 srcAddr
, srcWidth
, srcHeight
,
361 srcFormat
, srcType
, img
, row
, 0);
362 _mesa_unpack_float_color_span(ctx
, srcWidth
, GL_RGBA
, dstf
,
363 srcFormat
, srcType
, src
, srcPacking
,
364 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
366 dstf
+= srcWidth
* 4;
370 if (dimensions
== 1) {
371 ASSERT(ctx
->Pixel
.Convolution1DEnabled
);
372 _mesa_convolve_1d_image(ctx
, &convWidth
, tmpImage
, convImage
);
375 if (ctx
->Pixel
.Convolution2DEnabled
) {
376 _mesa_convolve_2d_image(ctx
, &convWidth
, &convHeight
,
377 tmpImage
, convImage
);
380 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
381 _mesa_convolve_sep_image(ctx
, &convWidth
, &convHeight
,
382 tmpImage
, convImage
);
386 /* packing and transfer ops after convolution */
388 dest
= (GLchan
*) texDestAddr
389 + (dstZoffset
+ img
) * (dstImageStride
/ sizeof(GLchan
))
390 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
));
391 for (row
= 0; row
< convHeight
; row
++) {
392 _mesa_pack_float_rgba_span(ctx
, convWidth
,
393 (const GLfloat (*)[4]) srcf
,
394 texDestFormat
, CHAN_TYPE
,
395 dest
, &_mesa_native_packing
,
397 & IMAGE_POST_CONVOLUTION_BITS
);
398 srcf
+= convWidth
* 4;
399 dest
+= (dstRowStride
/ sizeof(GLchan
));
411 GLchan
*dest
= (GLchan
*) texDestAddr
412 + dstZoffset
* (dstImageStride
/ sizeof(GLchan
))
413 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
414 + dstXoffset
* texComponents
;
415 for (img
= 0; img
< srcDepth
; img
++) {
416 GLchan
*destRow
= dest
;
417 for (row
= 0; row
< srcHeight
; row
++) {
418 const GLvoid
*srcRow
= _mesa_image_address(srcPacking
,
419 srcAddr
, srcWidth
, srcHeight
,
420 srcFormat
, srcType
, img
, row
, 0);
421 _mesa_unpack_chan_color_span(ctx
, srcWidth
, texDestFormat
,
422 destRow
, srcFormat
, srcType
, srcRow
,
423 srcPacking
, transferOps
);
424 destRow
+= (dstRowStride
/ sizeof(GLchan
));
426 dest
+= dstImageStride
/ sizeof(GLchan
);
435 * Transfer a texture image from user space to <destAddr> applying all
436 * needed image transfer operations and storing the result in the format
437 * specified by <dstFormat>. <dstFormat> may be any format from texformat.h.
439 * dimensions - 1, 2 or 3
440 * baseInternalFormat - base format of the internal texture format
441 * specified by the user. This is very important, see below.
442 * dstFormat - destination image format
443 * dstAddr - destination address
444 * srcWidth, srcHeight, srcDepth - size of source iamge
445 * dstX/Y/Zoffset - as specified by glTexSubImage
446 * dstRowStride - stride between dest rows in bytes
447 * dstImageStride - stride between dest images in bytes
448 * srcFormat, srcType - incoming image format and data type
449 * srcAddr - source image address
450 * srcPacking - packing params of source image
452 * XXX this function is a bit more complicated than it should be. If
453 * _mesa_convert_texsubimage[123]d could handle any dest/source formats
454 * or if transfer_teximage() could store in any MESA_FORMAT_* format, we
455 * could simplify things here.
458 _mesa_transfer_teximage(GLcontext
*ctx
, GLuint dimensions
,
459 GLenum baseInternalFormat
,
460 const struct gl_texture_format
*dstFormat
,
462 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
463 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
464 GLint dstRowStride
, GLint dstImageStride
,
465 GLenum srcFormat
, GLenum srcType
,
466 const GLvoid
*srcAddr
,
467 const struct gl_pixelstore_attrib
*srcPacking
)
469 const GLint dstRowStridePixels
= dstRowStride
/ dstFormat
->TexelBytes
;
470 const GLint dstImageStridePixels
= dstImageStride
/ dstFormat
->TexelBytes
;
472 GLuint transferOps
= ctx
->_ImageTransferState
;
473 GLboolean freeSourceData
= GL_FALSE
;
474 GLint postConvWidth
= srcWidth
, postConvHeight
= srcHeight
;
476 assert(baseInternalFormat
> 0);
477 ASSERT(baseInternalFormat
== GL_LUMINANCE
||
478 baseInternalFormat
== GL_INTENSITY
||
479 baseInternalFormat
== GL_LUMINANCE_ALPHA
||
480 baseInternalFormat
== GL_ALPHA
||
481 baseInternalFormat
== GL_RGB
||
482 baseInternalFormat
== GL_RGBA
||
483 baseInternalFormat
== GL_COLOR_INDEX
||
484 baseInternalFormat
== GL_DEPTH_COMPONENT
);
486 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
487 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
492 * Consider this scenario: The user's source image is GL_RGB and the
493 * requested internal format is GL_LUMINANCE. Now suppose the device
494 * driver doesn't support GL_LUMINANCE and instead uses RGB16 as the
495 * texture format. In that case we still need to do an intermediate
496 * conversion to luminance format so that the incoming red channel gets
497 * replicated into the dest red, green and blue channels. The following
498 * code takes care of that.
500 if (dstFormat
->BaseFormat
!= baseInternalFormat
) {
501 /* Allocate storage for temporary image in the baseInternalFormat */
502 const GLint texelSize
= _mesa_components_in_format(baseInternalFormat
)
504 const GLint bytes
= texelSize
* postConvWidth
* postConvHeight
*srcDepth
;
505 const GLint tmpRowStride
= texelSize
* postConvWidth
;
506 const GLint tmpImgStride
= texelSize
* postConvWidth
* postConvHeight
;
507 GLvoid
*tmpImage
= MALLOC(bytes
);
510 transfer_teximage(ctx
, dimensions
, baseInternalFormat
, tmpImage
,
511 srcWidth
, srcHeight
, srcDepth
,
512 0, 0, 0, /* x/y/zoffset */
513 tmpRowStride
, tmpImgStride
,
514 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
516 /* this is our new source image */
517 srcWidth
= postConvWidth
;
518 srcHeight
= postConvHeight
;
519 srcFormat
= baseInternalFormat
;
522 srcPacking
= &_mesa_native_packing
;
523 freeSourceData
= GL_TRUE
;
524 transferOps
= 0; /* image transfer ops were completed */
527 /* Let the optimized tex conversion functions take a crack at the
528 * image conversion if the dest format is a h/w format.
530 if (_mesa_is_hardware_tex_format(dstFormat
)) {
535 if (dimensions
== 1) {
536 makeTemp
= !_mesa_convert_texsubimage1d(dstFormat
->MesaFormat
,
543 else if (dimensions
== 2) {
544 makeTemp
= !_mesa_convert_texsubimage2d(dstFormat
->MesaFormat
,
545 dstXoffset
, dstYoffset
,
553 assert(dimensions
== 3);
554 makeTemp
= !_mesa_convert_texsubimage3d(dstFormat
->MesaFormat
,
555 dstXoffset
, dstYoffset
, dstZoffset
,
556 srcWidth
, srcHeight
, srcDepth
,
557 dstRowStridePixels
, dstImageStridePixels
,
559 srcPacking
, srcAddr
, dstAddr
);
564 FREE((void *) srcAddr
);
570 /* software texture format */
575 GLint postConvWidth
= srcWidth
, postConvHeight
= srcHeight
;
577 GLuint tmpComps
, tmpTexelSize
;
578 GLint tmpRowStride
, tmpImageStride
;
581 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
582 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
586 tmpFormat
= dstFormat
->BaseFormat
;
587 tmpComps
= _mesa_components_in_format(tmpFormat
);
588 tmpTexelSize
= tmpComps
* sizeof(GLchan
);
589 tmpRowStride
= postConvWidth
* tmpTexelSize
;
590 tmpImageStride
= postConvWidth
* postConvHeight
* tmpTexelSize
;
591 tmpImage
= (GLubyte
*) MALLOC(postConvWidth
* postConvHeight
*
592 srcDepth
* tmpTexelSize
);
595 FREE((void *) srcAddr
);
599 transfer_teximage(ctx
, dimensions
, tmpFormat
, tmpImage
,
600 srcWidth
, srcHeight
, srcDepth
,
601 0, 0, 0, /* x/y/zoffset */
602 tmpRowStride
, tmpImageStride
,
603 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
606 FREE((void *) srcAddr
);
608 /* the temp image is our new source image */
609 srcWidth
= postConvWidth
;
610 srcHeight
= postConvHeight
;
611 srcFormat
= tmpFormat
;
614 srcPacking
= &_mesa_native_packing
;
615 freeSourceData
= GL_TRUE
;
618 if (_mesa_is_hardware_tex_format(dstFormat
)) {
620 if (dimensions
== 1) {
622 b
= _mesa_convert_texsubimage1d(dstFormat
->MesaFormat
,
630 else if (dimensions
== 2) {
632 b
= _mesa_convert_texsubimage2d(dstFormat
->MesaFormat
,
633 dstXoffset
, dstYoffset
,
643 b
= _mesa_convert_texsubimage3d(dstFormat
->MesaFormat
,
644 dstXoffset
, dstYoffset
, dstZoffset
,
645 srcWidth
, srcHeight
, srcDepth
,
646 dstRowStridePixels
, dstImageStridePixels
,
648 srcPacking
, srcAddr
, dstAddr
);
653 /* software format */
655 transfer_teximage(ctx
, dimensions
, dstFormat
->BaseFormat
, dstAddr
,
656 srcWidth
, srcHeight
, srcDepth
,
657 dstXoffset
, dstYoffset
, dstZoffset
,
658 dstRowStride
, dstImageStride
,
659 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
663 FREE((void *) srcAddr
); /* the temp image */
669 * Given a user's uncompressed texture image, this function takes care of
670 * pixel unpacking, pixel transfer, format conversion and compression.
673 transfer_compressed_teximage(GLcontext
*ctx
, GLuint dimensions
,
674 GLsizei width
, GLsizei height
, GLsizei depth
,
675 GLenum srcFormat
, GLenum srcType
,
676 const struct gl_pixelstore_attrib
*unpacking
,
677 const GLvoid
*source
,
678 const struct gl_texture_format
*dstFormat
,
682 GLchan
*tempImage
= NULL
;
686 ASSERT(dimensions
== 2);
687 /* TexelBytes is zero if and only if it's a compressed format */
688 ASSERT(dstFormat
->TexelBytes
== 0);
690 baseFormat
= dstFormat
->BaseFormat
;
692 if (srcFormat
!= baseFormat
|| srcType
!= CHAN_TYPE
||
693 ctx
->_ImageTransferState
!= 0 || unpacking
->SwapBytes
) {
694 /* need to convert user's image to texImage->Format, GLchan */
695 GLint comps
= components_in_intformat(baseFormat
);
696 GLint postConvWidth
= width
, postConvHeight
= height
;
698 /* XXX convolution untested */
699 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
700 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
704 tempImage
= (GLchan
*) MALLOC(width
* height
* comps
* sizeof(GLchan
));
706 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
709 transfer_teximage(ctx
, dimensions
,
710 baseFormat
, /* dest format */
711 tempImage
, /* dst address */
712 width
, height
, depth
, /* src size */
713 0, 0, 0, /* x/y/zoffset */
714 comps
* width
, /* dst row stride */
715 comps
* width
* height
, /* dst image stride */
716 srcFormat
, srcType
, /* src format, type */
717 source
, unpacking
, /* src and src packing */
718 ctx
->_ImageTransferState
);
720 width
= postConvWidth
;
721 height
= postConvHeight
;
722 srcRowStride
= width
;
725 if (unpacking
->RowLength
)
726 srcRowStride
= unpacking
->RowLength
;
728 srcRowStride
= width
;
731 _mesa_compress_teximage(ctx
, width
, height
, baseFormat
,
732 (const GLchan
*) source
, srcRowStride
,
733 dstFormat
, dest
, dstRowStride
);
742 * This is the software fallback for Driver.TexImage1D()
743 * and Driver.CopyTexImage2D().
744 * The texture image type will be GLchan.
745 * The texture image format will be GL_COLOR_INDEX, GL_INTENSITY,
746 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_RGB or GL_RGBA.
749 _mesa_store_teximage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
750 GLint internalFormat
,
751 GLint width
, GLint border
,
752 GLenum format
, GLenum type
, const GLvoid
*pixels
,
753 const struct gl_pixelstore_attrib
*packing
,
754 struct gl_texture_object
*texObj
,
755 struct gl_texture_image
*texImage
)
757 GLint postConvWidth
= width
;
758 GLint texelBytes
, sizeInBytes
;
760 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
761 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
764 /* choose the texture format */
765 assert(ctx
->Driver
.ChooseTextureFormat
);
766 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
767 internalFormat
, format
, type
);
768 assert(texImage
->TexFormat
);
769 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel1D
;
771 texelBytes
= texImage
->TexFormat
->TexelBytes
;
773 /* allocate memory */
774 if (texImage
->IsCompressed
)
775 sizeInBytes
= texImage
->CompressedSize
;
777 sizeInBytes
= postConvWidth
* texelBytes
;
778 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
779 if (!texImage
->Data
) {
780 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
787 /* unpack image, apply transfer ops and store in texImage->Data */
788 if (texImage
->IsCompressed
) {
789 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
791 transfer_compressed_teximage(ctx
, 1, width
, 1, 1,
792 format
, type
, packing
,
793 pixels
, texImage
->TexFormat
,
794 (GLubyte
*) texImage
->Data
, dstRowStride
);
797 _mesa_transfer_teximage(ctx
, 1,
798 texImage
->Format
, /* base format */
799 texImage
->TexFormat
, texImage
->Data
,
800 width
, 1, 1, /* src size */
801 0, 0, 0, /* dstX/Y/Zoffset */
802 0, /* dstRowStride */
803 0, /* dstImageStride */
804 format
, type
, pixels
, packing
);
807 /* GL_SGIS_generate_mipmap */
808 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
809 _mesa_generate_mipmap(ctx
, target
,
810 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
817 * This is the software fallback for Driver.TexImage2D()
818 * and Driver.CopyTexImage2D().
819 * The texture image type will be GLchan.
820 * The texture image format will be GL_COLOR_INDEX, GL_INTENSITY,
821 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_RGB or GL_RGBA.
824 _mesa_store_teximage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
825 GLint internalFormat
,
826 GLint width
, GLint height
, GLint border
,
827 GLenum format
, GLenum type
, const void *pixels
,
828 const struct gl_pixelstore_attrib
*packing
,
829 struct gl_texture_object
*texObj
,
830 struct gl_texture_image
*texImage
)
832 GLint postConvWidth
= width
, postConvHeight
= height
;
833 GLint texelBytes
, sizeInBytes
;
835 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
836 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
840 /* choose the texture format */
841 assert(ctx
->Driver
.ChooseTextureFormat
);
842 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
843 internalFormat
, format
, type
);
844 assert(texImage
->TexFormat
);
845 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel2D
;
847 texelBytes
= texImage
->TexFormat
->TexelBytes
;
849 /* allocate memory */
850 if (texImage
->IsCompressed
)
851 sizeInBytes
= texImage
->CompressedSize
;
853 sizeInBytes
= postConvWidth
* postConvHeight
* texelBytes
;
854 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
855 if (!texImage
->Data
) {
856 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
863 /* unpack image, apply transfer ops and store in texImage->Data */
864 if (texImage
->IsCompressed
) {
865 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
867 transfer_compressed_teximage(ctx
, 2, width
, height
, 1,
868 format
, type
, packing
,
869 pixels
, texImage
->TexFormat
,
870 (GLubyte
*) texImage
->Data
, dstRowStride
);
873 _mesa_transfer_teximage(ctx
, 2,
875 texImage
->TexFormat
, texImage
->Data
,
876 width
, height
, 1, /* src size */
877 0, 0, 0, /* dstX/Y/Zoffset */
878 texImage
->Width
* texelBytes
, /* dstRowStride */
879 0, /* dstImageStride */
880 format
, type
, pixels
, packing
);
883 /* GL_SGIS_generate_mipmap */
884 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
885 _mesa_generate_mipmap(ctx
, target
,
886 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
894 * This is the software fallback for Driver.TexImage3D()
895 * and Driver.CopyTexImage3D().
896 * The texture image type will be GLchan.
897 * The texture image format will be GL_COLOR_INDEX, GL_INTENSITY,
898 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_RGB or GL_RGBA.
901 _mesa_store_teximage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
902 GLint internalFormat
,
903 GLint width
, GLint height
, GLint depth
, GLint border
,
904 GLenum format
, GLenum type
, const void *pixels
,
905 const struct gl_pixelstore_attrib
*packing
,
906 struct gl_texture_object
*texObj
,
907 struct gl_texture_image
*texImage
)
909 GLint texelBytes
, sizeInBytes
;
911 /* choose the texture format */
912 assert(ctx
->Driver
.ChooseTextureFormat
);
913 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
914 internalFormat
, format
, type
);
915 assert(texImage
->TexFormat
);
916 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel3D
;
918 texelBytes
= texImage
->TexFormat
->TexelBytes
;
920 /* allocate memory */
921 if (texImage
->IsCompressed
)
922 sizeInBytes
= texImage
->CompressedSize
;
924 sizeInBytes
= width
* height
* depth
* texelBytes
;
925 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
926 if (!texImage
->Data
) {
927 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
934 /* unpack image, apply transfer ops and store in texImage->Data */
935 if (texImage
->IsCompressed
) {
936 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
938 transfer_compressed_teximage(ctx
, 3, width
, height
, depth
,
939 format
, type
, packing
,
940 pixels
, texImage
->TexFormat
,
941 (GLubyte
*) texImage
->Data
, dstRowStride
);
944 _mesa_transfer_teximage(ctx
, 3,
946 texImage
->TexFormat
, texImage
->Data
,
947 width
, height
, depth
, /* src size */
948 0, 0, 0, /* dstX/Y/Zoffset */
949 texImage
->Width
* texelBytes
, /* dstRowStride */
950 texImage
->Width
* texImage
->Height
* texelBytes
,
951 format
, type
, pixels
, packing
);
954 /* GL_SGIS_generate_mipmap */
955 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
956 _mesa_generate_mipmap(ctx
, target
,
957 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
966 * This is the software fallback for Driver.TexSubImage1D()
967 * and Driver.CopyTexSubImage1D().
970 _mesa_store_texsubimage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
971 GLint xoffset
, GLint width
,
972 GLenum format
, GLenum type
, const void *pixels
,
973 const struct gl_pixelstore_attrib
*packing
,
974 struct gl_texture_object
*texObj
,
975 struct gl_texture_image
*texImage
)
977 if (texImage
->IsCompressed
) {
978 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
980 GLubyte
*dest
= _mesa_compressed_image_address(xoffset
, 0, 0,
983 (GLubyte
*) texImage
->Data
);
984 transfer_compressed_teximage(ctx
, 1, /* dimensions */
985 width
, 1, 1, /* size to replace */
986 format
, type
, /* source format/type */
987 packing
, /* source packing */
988 pixels
, /* source data */
989 texImage
->TexFormat
,/* dest format */
993 _mesa_transfer_teximage(ctx
, 1,
995 texImage
->TexFormat
, texImage
->Data
,
996 width
, 1, 1, /* src size */
997 xoffset
, 0, 0, /* dest offsets */
998 0, /* dstRowStride */
999 0, /* dstImageStride */
1000 format
, type
, pixels
, packing
);
1003 /* GL_SGIS_generate_mipmap */
1004 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1005 _mesa_generate_mipmap(ctx
, target
,
1006 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
1014 * This is the software fallback for Driver.TexSubImage2D()
1015 * and Driver.CopyTexSubImage2D().
1018 _mesa_store_texsubimage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
1019 GLint xoffset
, GLint yoffset
,
1020 GLint width
, GLint height
,
1021 GLenum format
, GLenum type
, const void *pixels
,
1022 const struct gl_pixelstore_attrib
*packing
,
1023 struct gl_texture_object
*texObj
,
1024 struct gl_texture_image
*texImage
)
1026 if (texImage
->IsCompressed
) {
1027 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
1029 GLubyte
*dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
1030 texImage
->IntFormat
,
1032 (GLubyte
*) texImage
->Data
);
1033 transfer_compressed_teximage(ctx
, 2, /* dimensions */
1034 width
, height
, 1, /* size to replace */
1035 format
, type
, /* source format/type */
1036 packing
, /* source packing */
1037 pixels
, /* source data */
1038 texImage
->TexFormat
,/* dest format */
1039 dest
, dstRowStride
);
1042 _mesa_transfer_teximage(ctx
, 2,
1044 texImage
->TexFormat
, texImage
->Data
,
1045 width
, height
, 1, /* src size */
1046 xoffset
, yoffset
, 0, /* dest offsets */
1047 texImage
->Width
*texImage
->TexFormat
->TexelBytes
,
1048 0, /* dstImageStride */
1049 format
, type
, pixels
, packing
);
1052 /* GL_SGIS_generate_mipmap */
1053 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1054 _mesa_generate_mipmap(ctx
, target
,
1055 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
1062 * This is the software fallback for Driver.TexSubImage3D().
1063 * and Driver.CopyTexSubImage3D().
1066 _mesa_store_texsubimage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
1067 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1068 GLint width
, GLint height
, GLint depth
,
1069 GLenum format
, GLenum type
, const void *pixels
,
1070 const struct gl_pixelstore_attrib
*packing
,
1071 struct gl_texture_object
*texObj
,
1072 struct gl_texture_image
*texImage
)
1074 if (texImage
->IsCompressed
) {
1075 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
1077 GLubyte
*dest
= _mesa_compressed_image_address(xoffset
, yoffset
, zoffset
,
1078 texImage
->IntFormat
,
1080 (GLubyte
*) texImage
->Data
);
1081 transfer_compressed_teximage(ctx
, 3, /* dimensions */
1082 width
, height
, depth
,/* size to replace */
1083 format
, type
, /* source format/type */
1084 packing
, /* source packing */
1085 pixels
, /* source data */
1086 texImage
->TexFormat
,/* dest format */
1087 dest
, dstRowStride
);
1090 const GLint texelBytes
= texImage
->TexFormat
->TexelBytes
;
1091 _mesa_transfer_teximage(ctx
, 3,
1093 texImage
->TexFormat
, texImage
->Data
,
1094 width
, height
, depth
, /* src size */
1095 xoffset
, yoffset
, zoffset
, /* dest offsets */
1096 texImage
->Width
* texelBytes
, /* dst row stride */
1097 texImage
->Width
* texImage
->Height
* texelBytes
,
1098 format
, type
, pixels
, packing
);
1101 /* GL_SGIS_generate_mipmap */
1102 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1103 _mesa_generate_mipmap(ctx
, target
,
1104 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
1113 * Fallback for Driver.CompressedTexImage1D()
1116 _mesa_store_compressed_teximage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
1117 GLint internalFormat
,
1118 GLint width
, GLint border
,
1119 GLsizei imageSize
, const GLvoid
*data
,
1120 struct gl_texture_object
*texObj
,
1121 struct gl_texture_image
*texImage
)
1123 /* this space intentionally left blank */
1129 * Fallback for Driver.CompressedTexImage2D()
1132 _mesa_store_compressed_teximage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
1133 GLint internalFormat
,
1134 GLint width
, GLint height
, GLint border
,
1135 GLsizei imageSize
, const GLvoid
*data
,
1136 struct gl_texture_object
*texObj
,
1137 struct gl_texture_image
*texImage
)
1139 /* This is pretty simple, basically just do a memcpy without worrying
1140 * about the usual image unpacking or image transfer operations.
1144 ASSERT(texImage
->Width
> 0);
1145 ASSERT(texImage
->Height
> 0);
1146 ASSERT(texImage
->Depth
== 1);
1147 ASSERT(texImage
->Data
== NULL
); /* was freed in glCompressedTexImage2DARB */
1149 /* choose the texture format */
1150 assert(ctx
->Driver
.ChooseTextureFormat
);
1151 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1152 internalFormat
, 0, 0);
1153 assert(texImage
->TexFormat
);
1154 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel2D
;
1156 /* allocate storage */
1157 texImage
->Data
= MESA_PBUFFER_ALLOC(imageSize
);
1158 if (!texImage
->Data
) {
1159 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2DARB");
1164 ASSERT(texImage
->CompressedSize
== (GLuint
) imageSize
);
1165 MEMCPY(texImage
->Data
, data
, imageSize
);
1171 * Fallback for Driver.CompressedTexImage3D()
1174 _mesa_store_compressed_teximage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
1175 GLint internalFormat
,
1176 GLint width
, GLint height
, GLint depth
,
1178 GLsizei imageSize
, const GLvoid
*data
,
1179 struct gl_texture_object
*texObj
,
1180 struct gl_texture_image
*texImage
)
1182 /* this space intentionally left blank */
1188 * Fallback for Driver.CompressedTexSubImage1D()
1191 _mesa_store_compressed_texsubimage1d(GLcontext
*ctx
, GLenum target
,
1193 GLint xoffset
, GLsizei width
,
1195 GLsizei imageSize
, const GLvoid
*data
,
1196 struct gl_texture_object
*texObj
,
1197 struct gl_texture_image
*texImage
)
1199 /* this space intentionally left blank */
1204 * Fallback for Driver.CompressedTexSubImage2D()
1207 _mesa_store_compressed_texsubimage2d(GLcontext
*ctx
, GLenum target
,
1209 GLint xoffset
, GLint yoffset
,
1210 GLsizei width
, GLsizei height
,
1212 GLsizei imageSize
, const GLvoid
*data
,
1213 struct gl_texture_object
*texObj
,
1214 struct gl_texture_image
*texImage
)
1216 GLint bytesPerRow
, destRowStride
, srcRowStride
;
1221 /* these should have been caught sooner */
1222 ASSERT((width
& 3) == 0 || width
== 2 || width
== 1);
1223 ASSERT((height
& 3) == 0 || height
== 2 || height
== 1);
1224 ASSERT((xoffset
& 3) == 0);
1225 ASSERT((yoffset
& 3) == 0);
1227 srcRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
, width
);
1228 src
= (const GLubyte
*) data
;
1230 destRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
1232 dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
1233 texImage
->IntFormat
,
1235 (GLubyte
*) texImage
->Data
);
1237 bytesPerRow
= srcRowStride
;
1240 for (i
= 0; i
< rows
; i
++) {
1241 MEMCPY(dest
, src
, bytesPerRow
);
1242 dest
+= destRowStride
;
1243 src
+= srcRowStride
;
1249 * Fallback for Driver.CompressedTexSubImage3D()
1252 _mesa_store_compressed_texsubimage3d(GLcontext
*ctx
, GLenum target
,
1254 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1255 GLsizei width
, GLsizei height
, GLsizei depth
,
1257 GLsizei imageSize
, const GLvoid
*data
,
1258 struct gl_texture_object
*texObj
,
1259 struct gl_texture_image
*texImage
)
1261 /* this space intentionally left blank */
1266 * Average together two rows of a source image to produce a single new
1267 * row in the dest image. It's legal for the two source rows to point
1268 * to the same data. The source width must be equal to either the
1269 * dest width or two times the dest width.
1272 do_row(const struct gl_texture_format
*format
, GLint srcWidth
,
1273 const GLvoid
*srcRowA
, const GLvoid
*srcRowB
,
1274 GLint dstWidth
, GLvoid
*dstRow
)
1276 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
1277 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
1279 assert(srcWidth
== dstWidth
|| srcWidth
== 2 * dstWidth
);
1281 switch (format
->MesaFormat
) {
1282 case MESA_FORMAT_RGBA
:
1285 const GLchan (*rowA
)[4] = (const GLchan (*)[4]) srcRowA
;
1286 const GLchan (*rowB
)[4] = (const GLchan (*)[4]) srcRowB
;
1287 GLchan (*dst
)[4] = (GLchan (*)[4]) dstRow
;
1288 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1289 i
++, j
+= colStride
, k
+= colStride
) {
1290 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1291 rowB
[j
][0] + rowB
[k
][0]) / 4;
1292 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1293 rowB
[j
][1] + rowB
[k
][1]) / 4;
1294 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1295 rowB
[j
][2] + rowB
[k
][2]) / 4;
1296 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
1297 rowB
[j
][3] + rowB
[k
][3]) / 4;
1301 case MESA_FORMAT_RGB
:
1304 const GLchan (*rowA
)[3] = (const GLchan (*)[3]) srcRowA
;
1305 const GLchan (*rowB
)[3] = (const GLchan (*)[3]) srcRowB
;
1306 GLchan (*dst
)[3] = (GLchan (*)[3]) dstRow
;
1307 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1308 i
++, j
+= colStride
, k
+= colStride
) {
1309 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1310 rowB
[j
][0] + rowB
[k
][0]) / 4;
1311 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1312 rowB
[j
][1] + rowB
[k
][1]) / 4;
1313 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1314 rowB
[j
][2] + rowB
[k
][2]) / 4;
1318 case MESA_FORMAT_ALPHA
:
1319 case MESA_FORMAT_LUMINANCE
:
1320 case MESA_FORMAT_INTENSITY
:
1321 case MESA_FORMAT_COLOR_INDEX
:
1324 const GLchan
*rowA
= (const GLchan
*) srcRowA
;
1325 const GLchan
*rowB
= (const GLchan
*) srcRowB
;
1326 GLchan
*dst
= (GLchan
*) dstRow
;
1327 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1328 i
++, j
+= colStride
, k
+= colStride
) {
1329 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) / 4;
1333 case MESA_FORMAT_LUMINANCE_ALPHA
:
1336 const GLchan (*rowA
)[2] = (const GLchan (*)[2]) srcRowA
;
1337 const GLchan (*rowB
)[2] = (const GLchan (*)[2]) srcRowB
;
1338 GLchan (*dst
)[2] = (GLchan (*)[2]) dstRow
;
1339 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1340 i
++, j
+= colStride
, k
+= colStride
) {
1341 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1342 rowB
[j
][0] + rowB
[k
][0]) / 4;
1343 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1344 rowB
[j
][1] + rowB
[k
][1]) / 4;
1348 case MESA_FORMAT_DEPTH_COMPONENT
:
1351 const GLfloat
*rowA
= (const GLfloat
*) srcRowA
;
1352 const GLfloat
*rowB
= (const GLfloat
*) srcRowB
;
1353 GLfloat
*dst
= (GLfloat
*) dstRow
;
1354 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1355 i
++, j
+= colStride
, k
+= colStride
) {
1356 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) * 0.25F
;
1360 /* Begin hardware formats */
1361 case MESA_FORMAT_RGBA8888
:
1362 case MESA_FORMAT_ARGB8888
:
1365 const GLubyte (*rowA
)[4] = (const GLubyte (*)[4]) srcRowA
;
1366 const GLubyte (*rowB
)[4] = (const GLubyte (*)[4]) srcRowB
;
1367 GLubyte (*dst
)[4] = (GLubyte (*)[4]) dstRow
;
1368 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1369 i
++, j
+= colStride
, k
+= colStride
) {
1370 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1371 rowB
[j
][0] + rowB
[k
][0]) / 4;
1372 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1373 rowB
[j
][1] + rowB
[k
][1]) / 4;
1374 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1375 rowB
[j
][2] + rowB
[k
][2]) / 4;
1376 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
1377 rowB
[j
][3] + rowB
[k
][3]) / 4;
1381 case MESA_FORMAT_RGB888
:
1384 const GLubyte (*rowA
)[3] = (const GLubyte (*)[3]) srcRowA
;
1385 const GLubyte (*rowB
)[3] = (const GLubyte (*)[3]) srcRowB
;
1386 GLubyte (*dst
)[3] = (GLubyte (*)[3]) dstRow
;
1387 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1388 i
++, j
+= colStride
, k
+= colStride
) {
1389 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1390 rowB
[j
][0] + rowB
[k
][0]) / 4;
1391 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1392 rowB
[j
][1] + rowB
[k
][1]) / 4;
1393 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1394 rowB
[j
][2] + rowB
[k
][2]) / 4;
1398 case MESA_FORMAT_RGB565
:
1401 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
1402 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
1403 GLushort
*dst
= (GLushort
*) dstRow
;
1404 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1405 i
++, j
+= colStride
, k
+= colStride
) {
1406 const GLint rowAr0
= rowA
[j
] & 0x1f;
1407 const GLint rowAr1
= rowA
[k
] & 0x1f;
1408 const GLint rowBr0
= rowB
[j
] & 0x1f;
1409 const GLint rowBr1
= rowB
[k
] & 0x1f;
1410 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x3f;
1411 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x3f;
1412 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x3f;
1413 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x3f;
1414 const GLint rowAb0
= (rowA
[j
] >> 11) & 0x1f;
1415 const GLint rowAb1
= (rowA
[k
] >> 11) & 0x1f;
1416 const GLint rowBb0
= (rowB
[j
] >> 11) & 0x1f;
1417 const GLint rowBb1
= (rowB
[k
] >> 11) & 0x1f;
1418 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1419 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1420 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1421 dst
[i
] = (blue
<< 11) | (green
<< 5) | red
;
1425 case MESA_FORMAT_ARGB4444
:
1428 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
1429 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
1430 GLushort
*dst
= (GLushort
*) dstRow
;
1431 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1432 i
++, j
+= colStride
, k
+= colStride
) {
1433 const GLint rowAr0
= rowA
[j
] & 0xf;
1434 const GLint rowAr1
= rowA
[k
] & 0xf;
1435 const GLint rowBr0
= rowB
[j
] & 0xf;
1436 const GLint rowBr1
= rowB
[k
] & 0xf;
1437 const GLint rowAg0
= (rowA
[j
] >> 4) & 0xf;
1438 const GLint rowAg1
= (rowA
[k
] >> 4) & 0xf;
1439 const GLint rowBg0
= (rowB
[j
] >> 4) & 0xf;
1440 const GLint rowBg1
= (rowB
[k
] >> 4) & 0xf;
1441 const GLint rowAb0
= (rowA
[j
] >> 8) & 0xf;
1442 const GLint rowAb1
= (rowA
[k
] >> 8) & 0xf;
1443 const GLint rowBb0
= (rowB
[j
] >> 8) & 0xf;
1444 const GLint rowBb1
= (rowB
[k
] >> 8) & 0xf;
1445 const GLint rowAa0
= (rowA
[j
] >> 12) & 0xf;
1446 const GLint rowAa1
= (rowA
[k
] >> 12) & 0xf;
1447 const GLint rowBa0
= (rowB
[j
] >> 12) & 0xf;
1448 const GLint rowBa1
= (rowB
[k
] >> 12) & 0xf;
1449 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1450 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1451 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1452 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 4;
1453 dst
[i
] = (alpha
<< 12) | (blue
<< 8) | (green
<< 4) | red
;
1457 case MESA_FORMAT_ARGB1555
:
1460 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
1461 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
1462 GLushort
*dst
= (GLushort
*) dstRow
;
1463 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1464 i
++, j
+= colStride
, k
+= colStride
) {
1465 const GLint rowAr0
= rowA
[j
] & 0x1f;
1466 const GLint rowAr1
= rowA
[k
] & 0x1f;
1467 const GLint rowBr0
= rowB
[j
] & 0x1f;
1468 const GLint rowBr1
= rowB
[k
] & 0xf;
1469 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x1f;
1470 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x1f;
1471 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x1f;
1472 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x1f;
1473 const GLint rowAb0
= (rowA
[j
] >> 10) & 0x1f;
1474 const GLint rowAb1
= (rowA
[k
] >> 10) & 0x1f;
1475 const GLint rowBb0
= (rowB
[j
] >> 10) & 0x1f;
1476 const GLint rowBb1
= (rowB
[k
] >> 10) & 0x1f;
1477 const GLint rowAa0
= (rowA
[j
] >> 15) & 0x1;
1478 const GLint rowAa1
= (rowA
[k
] >> 15) & 0x1;
1479 const GLint rowBa0
= (rowB
[j
] >> 15) & 0x1;
1480 const GLint rowBa1
= (rowB
[k
] >> 15) & 0x1;
1481 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1482 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1483 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1484 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 4;
1485 dst
[i
] = (alpha
<< 15) | (blue
<< 10) | (green
<< 5) | red
;
1489 case MESA_FORMAT_AL88
:
1492 const GLubyte (*rowA
)[2] = (const GLubyte (*)[2]) srcRowA
;
1493 const GLubyte (*rowB
)[2] = (const GLubyte (*)[2]) srcRowB
;
1494 GLubyte (*dst
)[2] = (GLubyte (*)[2]) dstRow
;
1495 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1496 i
++, j
+= colStride
, k
+= colStride
) {
1497 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1498 rowB
[j
][0] + rowB
[k
][0]) >> 2;
1499 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1500 rowB
[j
][1] + rowB
[k
][1]) >> 2;
1504 case MESA_FORMAT_RGB332
:
1507 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
1508 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
1509 GLubyte
*dst
= (GLubyte
*) dstRow
;
1510 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1511 i
++, j
+= colStride
, k
+= colStride
) {
1512 const GLint rowAr0
= rowA
[j
] & 0x3;
1513 const GLint rowAr1
= rowA
[k
] & 0x3;
1514 const GLint rowBr0
= rowB
[j
] & 0x3;
1515 const GLint rowBr1
= rowB
[k
] & 0x3;
1516 const GLint rowAg0
= (rowA
[j
] >> 2) & 0x7;
1517 const GLint rowAg1
= (rowA
[k
] >> 2) & 0x7;
1518 const GLint rowBg0
= (rowB
[j
] >> 2) & 0x7;
1519 const GLint rowBg1
= (rowB
[k
] >> 2) & 0x7;
1520 const GLint rowAb0
= (rowA
[j
] >> 5) & 0x7;
1521 const GLint rowAb1
= (rowA
[k
] >> 5) & 0x7;
1522 const GLint rowBb0
= (rowB
[j
] >> 5) & 0x7;
1523 const GLint rowBb1
= (rowB
[k
] >> 5) & 0x7;
1524 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1525 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1526 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1527 dst
[i
] = (blue
<< 5) | (green
<< 2) | red
;
1531 case MESA_FORMAT_A8
:
1532 case MESA_FORMAT_L8
:
1533 case MESA_FORMAT_I8
:
1534 case MESA_FORMAT_CI8
:
1537 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
1538 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
1539 GLubyte
*dst
= (GLubyte
*) dstRow
;
1540 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1541 i
++, j
+= colStride
, k
+= colStride
) {
1542 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) >> 2;
1547 _mesa_problem(NULL
, "bad format in do_row()");
1553 * These functions generate a 1/2-size mipmap image from a source image.
1554 * Texture borders are handled by copying or averaging the source image's
1555 * border texels, depending on the scale-down factor.
1559 make_1d_mipmap(const struct gl_texture_format
*format
, GLint border
,
1560 GLint srcWidth
, const GLubyte
*srcPtr
,
1561 GLint dstWidth
, GLubyte
*dstPtr
)
1563 const GLint bpt
= format
->TexelBytes
;
1567 /* skip the border pixel, if any */
1568 src
= srcPtr
+ border
* bpt
;
1569 dst
= dstPtr
+ border
* bpt
;
1571 /* we just duplicate the input row, kind of hack, saves code */
1572 do_row(format
, srcWidth
- 2 * border
, src
, src
,
1573 dstWidth
- 2 * border
, dst
);
1576 /* copy left-most pixel from source */
1577 MEMCPY(dstPtr
, srcPtr
, bpt
);
1578 /* copy right-most pixel from source */
1579 MEMCPY(dstPtr
+ (dstWidth
- 1) * bpt
,
1580 srcPtr
+ (srcWidth
- 1) * bpt
,
1587 make_2d_mipmap(const struct gl_texture_format
*format
, GLint border
,
1588 GLint srcWidth
, GLint srcHeight
, const GLubyte
*srcPtr
,
1589 GLint dstWidth
, GLint dstHeight
, GLubyte
*dstPtr
)
1591 const GLint bpt
= format
->TexelBytes
;
1592 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
1593 const GLint dstWidthNB
= dstWidth
- 2 * border
;
1594 const GLint dstHeightNB
= dstHeight
- 2 * border
;
1595 const GLint srcRowStride
= bpt
* srcWidth
;
1596 const GLint dstRowStride
= bpt
* dstWidth
;
1597 const GLubyte
*srcA
, *srcB
;
1599 GLint row
, colStride
;
1601 colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
1603 /* Compute src and dst pointers, skipping any border */
1604 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
1606 srcB
= srcA
+ srcRowStride
;
1609 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
1611 for (row
= 0; row
< dstHeightNB
; row
++) {
1612 do_row(format
, srcWidthNB
, srcA
, srcB
,
1614 srcA
+= 2 * srcRowStride
;
1615 srcB
+= 2 * srcRowStride
;
1616 dst
+= dstRowStride
;
1619 /* This is ugly but probably won't be used much */
1621 /* fill in dest border */
1622 /* lower-left border pixel */
1623 MEMCPY(dstPtr
, srcPtr
, bpt
);
1624 /* lower-right border pixel */
1625 MEMCPY(dstPtr
+ (dstWidth
- 1) * bpt
,
1626 srcPtr
+ (srcWidth
- 1) * bpt
, bpt
);
1627 /* upper-left border pixel */
1628 MEMCPY(dstPtr
+ dstWidth
* (dstHeight
- 1) * bpt
,
1629 srcPtr
+ srcWidth
* (srcHeight
- 1) * bpt
, bpt
);
1630 /* upper-right border pixel */
1631 MEMCPY(dstPtr
+ (dstWidth
* dstHeight
- 1) * bpt
,
1632 srcPtr
+ (srcWidth
* srcHeight
- 1) * bpt
, bpt
);
1634 do_row(format
, srcWidthNB
,
1637 dstWidthNB
, dstPtr
+ bpt
);
1639 do_row(format
, srcWidthNB
,
1640 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
1641 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
1643 dstPtr
+ (dstWidth
* (dstHeight
- 1) + 1) * bpt
);
1644 /* left and right borders */
1645 if (srcHeight
== dstHeight
) {
1646 /* copy border pixel from src to dst */
1647 for (row
= 1; row
< srcHeight
; row
++) {
1648 MEMCPY(dstPtr
+ dstWidth
* row
* bpt
,
1649 srcPtr
+ srcWidth
* row
* bpt
, bpt
);
1650 MEMCPY(dstPtr
+ (dstWidth
* row
+ dstWidth
- 1) * bpt
,
1651 srcPtr
+ (srcWidth
* row
+ srcWidth
- 1) * bpt
, bpt
);
1655 /* average two src pixels each dest pixel */
1656 for (row
= 0; row
< dstHeightNB
; row
+= 2) {
1658 srcPtr
+ (srcWidth
* (row
* 2 + 1)) * bpt
,
1659 srcPtr
+ (srcWidth
* (row
* 2 + 2)) * bpt
,
1660 1, dstPtr
+ (dstWidth
* row
+ 1) * bpt
);
1662 srcPtr
+ (srcWidth
* (row
* 2 + 1) + srcWidth
- 1) * bpt
,
1663 srcPtr
+ (srcWidth
* (row
* 2 + 2) + srcWidth
- 1) * bpt
,
1664 1, dstPtr
+ (dstWidth
* row
+ 1 + dstWidth
- 1) * bpt
);
1672 make_3d_mipmap(const struct gl_texture_format
*format
, GLint border
,
1673 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1674 const GLubyte
*srcPtr
,
1675 GLint dstWidth
, GLint dstHeight
, GLint dstDepth
,
1678 const GLint bpt
= format
->TexelBytes
;
1679 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
1680 const GLint srcDepthNB
= srcDepth
- 2 * border
;
1681 const GLint dstWidthNB
= dstWidth
- 2 * border
;
1682 const GLint dstHeightNB
= dstHeight
- 2 * border
;
1683 const GLint dstDepthNB
= dstDepth
- 2 * border
;
1684 GLvoid
*tmpRowA
, *tmpRowB
;
1686 GLint bytesPerSrcImage
, bytesPerDstImage
;
1687 GLint bytesPerSrcRow
, bytesPerDstRow
;
1688 GLint srcImageOffset
, srcRowOffset
;
1690 (void) srcDepthNB
; /* silence warnings */
1692 /* Need two temporary row buffers */
1693 tmpRowA
= MALLOC(srcWidth
* bpt
);
1696 tmpRowB
= MALLOC(srcWidth
* bpt
);
1702 bytesPerSrcImage
= srcWidth
* srcHeight
* bpt
;
1703 bytesPerDstImage
= dstWidth
* dstHeight
* bpt
;
1705 bytesPerSrcRow
= srcWidth
* bpt
;
1706 bytesPerDstRow
= dstWidth
* bpt
;
1708 /* Offset between adjacent src images to be averaged together */
1709 srcImageOffset
= (srcDepth
== dstDepth
) ? 0 : bytesPerSrcImage
;
1711 /* Offset between adjacent src rows to be averaged together */
1712 srcRowOffset
= (srcHeight
== dstHeight
) ? 0 : srcWidth
* bpt
;
1715 * Need to average together up to 8 src pixels for each dest pixel.
1716 * Break that down into 3 operations:
1717 * 1. take two rows from source image and average them together.
1718 * 2. take two rows from next source image and average them together.
1719 * 3. take the two averaged rows and average them for the final dst row.
1723 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1724 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1727 for (img
= 0; img
< dstDepthNB
; img
++) {
1728 /* first source image pointer, skipping border */
1729 const GLubyte
*imgSrcA
= srcPtr
1730 + (bytesPerSrcImage
+ bytesPerSrcRow
+ border
) * bpt
* border
1731 + img
* (bytesPerSrcImage
+ srcImageOffset
);
1732 /* second source image pointer, skipping border */
1733 const GLubyte
*imgSrcB
= imgSrcA
+ srcImageOffset
;
1734 /* address of the dest image, skipping border */
1735 GLubyte
*imgDst
= dstPtr
1736 + (bytesPerDstImage
+ bytesPerDstRow
+ border
) * bpt
* border
1737 + img
* bytesPerDstImage
;
1739 /* setup the four source row pointers and the dest row pointer */
1740 const GLubyte
*srcImgARowA
= imgSrcA
;
1741 const GLubyte
*srcImgARowB
= imgSrcA
+ srcRowOffset
;
1742 const GLubyte
*srcImgBRowA
= imgSrcB
;
1743 const GLubyte
*srcImgBRowB
= imgSrcB
+ srcRowOffset
;
1744 GLubyte
*dstImgRow
= imgDst
;
1746 for (row
= 0; row
< dstHeightNB
; row
++) {
1747 /* Average together two rows from first src image */
1748 do_row(format
, srcWidthNB
, srcImgARowA
, srcImgARowB
,
1749 srcWidthNB
, tmpRowA
);
1750 /* Average together two rows from second src image */
1751 do_row(format
, srcWidthNB
, srcImgBRowA
, srcImgBRowB
,
1752 srcWidthNB
, tmpRowB
);
1753 /* Average together the temp rows to make the final row */
1754 do_row(format
, srcWidthNB
, tmpRowA
, tmpRowB
,
1755 dstWidthNB
, dstImgRow
);
1756 /* advance to next rows */
1757 srcImgARowA
+= bytesPerSrcRow
+ srcRowOffset
;
1758 srcImgARowB
+= bytesPerSrcRow
+ srcRowOffset
;
1759 srcImgBRowA
+= bytesPerSrcRow
+ srcRowOffset
;
1760 srcImgBRowB
+= bytesPerSrcRow
+ srcRowOffset
;
1761 dstImgRow
+= bytesPerDstRow
;
1768 /* Luckily we can leverage the make_2d_mipmap() function here! */
1770 /* do front border image */
1771 make_2d_mipmap(format
, 1, srcWidth
, srcHeight
, srcPtr
,
1772 dstWidth
, dstHeight
, dstPtr
);
1773 /* do back border image */
1774 make_2d_mipmap(format
, 1, srcWidth
, srcHeight
,
1775 srcPtr
+ bytesPerSrcImage
* (srcDepth
- 1),
1776 dstWidth
, dstHeight
,
1777 dstPtr
+ bytesPerDstImage
* (dstDepth
- 1));
1778 /* do four remaining border edges that span the image slices */
1779 if (srcDepth
== dstDepth
) {
1780 /* just copy border pixels from src to dst */
1781 for (img
= 0; img
< dstDepthNB
; img
++) {
1785 /* do border along [img][row=0][col=0] */
1786 src
= srcPtr
+ (img
+ 1) * bytesPerSrcImage
;
1787 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
;
1788 MEMCPY(dst
, src
, bpt
);
1790 /* do border along [img][row=dstHeight-1][col=0] */
1791 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1792 + (srcHeight
- 1) * bytesPerSrcRow
;
1793 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1794 + (dstHeight
- 1) * bytesPerDstRow
;
1795 MEMCPY(dst
, src
, bpt
);
1797 /* do border along [img][row=0][col=dstWidth-1] */
1798 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1799 + (srcWidth
- 1) * bpt
;
1800 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1801 + (dstWidth
- 1) * bpt
;
1802 MEMCPY(dst
, src
, bpt
);
1804 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1805 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1806 + (bytesPerSrcImage
- bpt
);
1807 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1808 + (bytesPerDstImage
- bpt
);
1809 MEMCPY(dst
, src
, bpt
);
1813 /* average border pixels from adjacent src image pairs */
1814 ASSERT(srcDepthNB
== 2 * dstDepthNB
);
1815 for (img
= 0; img
< dstDepthNB
; img
++) {
1819 /* do border along [img][row=0][col=0] */
1820 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
;
1821 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
;
1822 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1824 /* do border along [img][row=dstHeight-1][col=0] */
1825 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1826 + (srcHeight
- 1) * bytesPerSrcRow
;
1827 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1828 + (dstHeight
- 1) * bytesPerDstRow
;
1829 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1831 /* do border along [img][row=0][col=dstWidth-1] */
1832 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1833 + (srcWidth
- 1) * bpt
;
1834 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1835 + (dstWidth
- 1) * bpt
;
1836 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1838 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1839 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1840 + (bytesPerSrcImage
- bpt
);
1841 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1842 + (bytesPerDstImage
- bpt
);
1843 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1851 * For GL_SGIX_generate_mipmap:
1852 * Generate a complete set of mipmaps from texObj's base-level image.
1853 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
1856 _mesa_generate_mipmap(GLcontext
*ctx
, GLenum target
,
1857 const struct gl_texture_unit
*texUnit
,
1858 struct gl_texture_object
*texObj
)
1860 const struct gl_texture_image
*srcImage
;
1861 const struct gl_texture_format
*convertFormat
;
1862 const GLubyte
*srcData
= NULL
;
1863 GLubyte
*dstData
= NULL
;
1864 GLint level
, maxLevels
;
1867 srcImage
= texObj
->Image
[texObj
->BaseLevel
];
1870 maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1871 ASSERT(maxLevels
> 0); /* bad target */
1873 /* Find convertFormat - the format that do_row() will process */
1874 if (srcImage
->IsCompressed
) {
1875 /* setup for compressed textures */
1877 GLint components
, size
;
1880 assert(texObj
->Target
== GL_TEXTURE_2D
);
1882 if (srcImage
->Format
== GL_RGB
) {
1883 convertFormat
= &_mesa_texformat_rgb
;
1886 else if (srcImage
->Format
== GL_RGBA
) {
1887 convertFormat
= &_mesa_texformat_rgba
;
1891 _mesa_problem(ctx
, "bad srcImage->Format in _mesa_generate_mipmaps");
1895 /* allocate storage for uncompressed GL_RGB or GL_RGBA images */
1896 size
= _mesa_bytes_per_pixel(srcImage
->Format
, CHAN_TYPE
)
1897 * srcImage
->Width
* srcImage
->Height
* srcImage
->Depth
+ 20;
1898 /* 20 extra bytes, just be safe when calling last FetchTexel */
1899 srcData
= (GLubyte
*) MALLOC(size
);
1901 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
1904 dstData
= (GLubyte
*) MALLOC(size
/ 2); /* 1/4 would probably be OK */
1906 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
1907 FREE((void *) srcData
);
1911 /* decompress base image here */
1912 dst
= (GLchan
*) srcData
;
1913 for (row
= 0; row
< srcImage
->Height
; row
++) {
1915 for (col
= 0; col
< srcImage
->Width
; col
++) {
1916 (*srcImage
->FetchTexel
)(srcImage
, col
, row
, 0, (GLvoid
*) dst
);
1923 convertFormat
= srcImage
->TexFormat
;
1926 for (level
= texObj
->BaseLevel
; level
< texObj
->MaxLevel
1927 && level
< maxLevels
- 1; level
++) {
1928 /* generate image[level+1] from image[level] */
1929 const struct gl_texture_image
*srcImage
;
1930 struct gl_texture_image
*dstImage
;
1931 GLint srcWidth
, srcHeight
, srcDepth
;
1932 GLint dstWidth
, dstHeight
, dstDepth
;
1933 GLint border
, bytesPerTexel
;
1935 /* get src image parameters */
1936 srcImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1938 srcWidth
= srcImage
->Width
;
1939 srcHeight
= srcImage
->Height
;
1940 srcDepth
= srcImage
->Depth
;
1941 border
= srcImage
->Border
;
1943 /* compute next (level+1) image size */
1944 if (srcWidth
- 2 * border
> 1) {
1945 dstWidth
= (srcWidth
- 2 * border
) / 2 + 2 * border
;
1948 dstWidth
= srcWidth
; /* can't go smaller */
1950 if (srcHeight
- 2 * border
> 1) {
1951 dstHeight
= (srcHeight
- 2 * border
) / 2 + 2 * border
;
1954 dstHeight
= srcHeight
; /* can't go smaller */
1956 if (srcDepth
- 2 * border
> 1) {
1957 dstDepth
= (srcDepth
- 2 * border
) / 2 + 2 * border
;
1960 dstDepth
= srcDepth
; /* can't go smaller */
1963 if (dstWidth
== srcWidth
&&
1964 dstHeight
== srcHeight
&&
1965 dstDepth
== srcDepth
) {
1967 if (srcImage
->IsCompressed
) {
1968 FREE((void *) srcData
);
1974 /* get dest gl_texture_image */
1975 dstImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
+ 1);
1977 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
1981 /* Free old image data */
1983 MESA_PBUFFER_FREE(dstImage
->Data
);
1985 /* initialize new image */
1986 _mesa_init_teximage_fields(ctx
, target
, dstImage
, dstWidth
, dstHeight
,
1987 dstDepth
, border
, srcImage
->IntFormat
);
1988 dstImage
->DriverData
= NULL
;
1989 dstImage
->TexFormat
= srcImage
->TexFormat
;
1990 dstImage
->FetchTexel
= srcImage
->FetchTexel
;
1991 ASSERT(dstImage
->TexFormat
);
1992 ASSERT(dstImage
->FetchTexel
);
1994 /* Alloc new teximage data buffer.
1995 * Setup src and dest data pointers.
1997 if (dstImage
->IsCompressed
) {
1998 ASSERT(dstImage
->CompressedSize
> 0); /* set by init_teximage_fields*/
1999 dstImage
->Data
= MESA_PBUFFER_ALLOC(dstImage
->CompressedSize
);
2000 if (!dstImage
->Data
) {
2001 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
2004 /* srcData and dstData are already set */
2009 bytesPerTexel
= srcImage
->TexFormat
->TexelBytes
;
2010 ASSERT(dstWidth
* dstHeight
* dstDepth
* bytesPerTexel
> 0);
2011 dstImage
->Data
= MESA_PBUFFER_ALLOC(dstWidth
* dstHeight
* dstDepth
2013 if (!dstImage
->Data
) {
2014 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
2017 srcData
= (const GLubyte
*) srcImage
->Data
;
2018 dstData
= (GLubyte
*) dstImage
->Data
;
2022 * We use simple 2x2 averaging to compute the next mipmap level.
2026 make_1d_mipmap(convertFormat
, border
,
2031 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
2032 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
2033 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
2034 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
2035 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
2036 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
2037 make_2d_mipmap(convertFormat
, border
,
2038 srcWidth
, srcHeight
, srcData
,
2039 dstWidth
, dstHeight
, dstData
);
2042 make_3d_mipmap(convertFormat
, border
,
2043 srcWidth
, srcHeight
, srcDepth
, srcData
,
2044 dstWidth
, dstHeight
, dstDepth
, dstData
);
2046 case GL_TEXTURE_RECTANGLE_NV
:
2047 /* no mipmaps, do nothing */
2050 _mesa_problem(ctx
, "bad dimensions in _mesa_generate_mipmaps");
2054 if (dstImage
->IsCompressed
) {
2056 /* compress image from dstData into dstImage->Data */
2057 const GLenum srcFormat
= convertFormat
->BaseFormat
;
2058 GLint dstRowStride
= _mesa_compressed_row_stride(srcImage
->IntFormat
,
2060 ASSERT(srcFormat
== GL_RGB
|| srcFormat
== GL_RGBA
);
2061 _mesa_compress_teximage(ctx
,
2062 dstWidth
, dstHeight
, /* size */
2063 srcFormat
, /* source format */
2064 (const GLchan
*) dstData
, /* source buffer */
2065 dstWidth
, /* source row stride */
2066 dstImage
->TexFormat
, /* dest format */
2067 (GLubyte
*) dstImage
->Data
, /* dest buffer */
2068 dstRowStride
); /* dest row stride */
2070 /* swap src and dest pointers */
2071 temp
= (GLubyte
*) srcData
;
2076 } /* loop over mipmap levels */