1 /* $Id: texstore.c,v 1.39 2002/09/16 17:57:14 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #include "texformat.h"
45 * Given an internal texture format enum or 1, 2, 3, 4 return the
46 * corresponding _base_ internal format: GL_ALPHA, GL_LUMINANCE,
47 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA. Return the
48 * number of components for the format. Return -1 if invalid enum.
50 * GH: Do we really need this? We have the number of bytes per texel
51 * in the texture format structures, so why don't we just use that?
54 components_in_intformat( GLint format
)
71 case GL_LUMINANCE_ALPHA
:
72 case GL_LUMINANCE4_ALPHA4
:
73 case GL_LUMINANCE6_ALPHA2
:
74 case GL_LUMINANCE8_ALPHA8
:
75 case GL_LUMINANCE12_ALPHA4
:
76 case GL_LUMINANCE12_ALPHA12
:
77 case GL_LUMINANCE16_ALPHA16
:
106 case GL_COLOR_INDEX1_EXT
:
107 case GL_COLOR_INDEX2_EXT
:
108 case GL_COLOR_INDEX4_EXT
:
109 case GL_COLOR_INDEX8_EXT
:
110 case GL_COLOR_INDEX12_EXT
:
111 case GL_COLOR_INDEX16_EXT
:
113 case GL_DEPTH_COMPONENT
:
114 case GL_DEPTH_COMPONENT16_SGIX
:
115 case GL_DEPTH_COMPONENT24_SGIX
:
116 case GL_DEPTH_COMPONENT32_SGIX
:
119 return -1; /* error */
125 * This function is used to transfer the user's image data into a texture
126 * image buffer. We handle both full texture images and subtexture images.
127 * We also take care of all image transfer operations here, including
128 * convolution, scale/bias, colortables, etc.
130 * The destination texel channel type is always GLchan.
132 * A hardware driver may use this as a helper routine to unpack and
133 * apply pixel transfer ops into a temporary image buffer. Then,
134 * convert the temporary image into the special hardware format.
137 * dimensions - 1, 2, or 3
138 * texFormat - GL_LUMINANCE, GL_INTENSITY, GL_LUMINANCE_ALPHA, GL_ALPHA,
140 * texDestAddr - destination image address
141 * srcWidth, srcHeight, srcDepth - size (in pixels) of src and dest images
142 * dstXoffset, dstYoffset, dstZoffset - position to store the image within
143 * the destination 3D texture
144 * dstRowStride, dstImageStride - dest image strides in bytes
145 * srcFormat - source image format (GL_ALPHA, GL_RED, GL_RGB, etc)
146 * srcType - GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5, GL_FLOAT, etc
147 * srcPacking - describes packing of incoming image.
148 * transferOps - mask of pixel transfer operations
151 transfer_teximage(GLcontext
*ctx
, GLuint dimensions
,
152 GLenum texDestFormat
, GLvoid
*texDestAddr
,
153 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
154 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
155 GLint dstRowStride
, GLint dstImageStride
,
156 GLenum srcFormat
, GLenum srcType
,
157 const GLvoid
*srcAddr
,
158 const struct gl_pixelstore_attrib
*srcPacking
,
164 ASSERT(dimensions
>= 1 && dimensions
<= 3);
166 ASSERT(srcWidth
>= 1);
167 ASSERT(srcHeight
>= 1);
168 ASSERT(srcDepth
>= 1);
169 ASSERT(dstXoffset
>= 0);
170 ASSERT(dstYoffset
>= 0);
171 ASSERT(dstZoffset
>= 0);
172 ASSERT(dstRowStride
>= 0);
173 ASSERT(dstImageStride
>= 0);
177 texComponents
= components_in_intformat(texDestFormat
);
179 /* try common 2D texture cases first */
180 if (!transferOps
&& dimensions
== 2 && srcType
== CHAN_TYPE
) {
182 if (srcFormat
== texDestFormat
) {
183 /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA,
184 * GL_LUMINANCE_ALPHA, etc. texture formats. Use memcpy().
186 const GLchan
*src
= (const GLchan
*) _mesa_image_address(
187 srcPacking
, srcAddr
, srcWidth
, srcHeight
,
188 srcFormat
, srcType
, 0, 0, 0);
189 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
190 srcWidth
, srcFormat
, srcType
);
191 const GLint widthInBytes
= srcWidth
* texComponents
* sizeof(GLchan
);
192 GLchan
*dst
= (GLchan
*) texDestAddr
193 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
194 + dstXoffset
* texComponents
;
195 if (srcRowStride
== widthInBytes
&& dstRowStride
== widthInBytes
) {
196 MEMCPY(dst
, src
, srcHeight
* widthInBytes
);
200 for (i
= 0; i
< srcHeight
; i
++) {
201 MEMCPY(dst
, src
, widthInBytes
);
202 src
+= (srcRowStride
/ sizeof(GLchan
));
203 dst
+= (dstRowStride
/ sizeof(GLchan
));
206 return; /* all done */
208 else if (srcFormat
== GL_RGBA
&& texDestFormat
== GL_RGB
) {
209 /* commonly used by Quake */
210 const GLchan
*src
= (const GLchan
*) _mesa_image_address(
211 srcPacking
, srcAddr
, srcWidth
, srcHeight
,
212 srcFormat
, srcType
, 0, 0, 0);
213 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
214 srcWidth
, srcFormat
, srcType
);
215 GLchan
*dst
= (GLchan
*) texDestAddr
216 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
217 + dstXoffset
* texComponents
;
219 for (i
= 0; i
< srcHeight
; i
++) {
220 const GLchan
*s
= src
;
222 for (j
= 0; j
< srcWidth
; j
++) {
224 *d
++ = *s
++; /*green*/
225 *d
++ = *s
++; /*blue*/
228 src
+= (srcRowStride
/ sizeof(GLchan
));
229 dst
+= (dstRowStride
/ sizeof(GLchan
));
231 return; /* all done */
236 * General case solutions
238 if (texDestFormat
== GL_COLOR_INDEX
) {
239 /* color index texture */
240 const GLenum texType
= CHAN_TYPE
;
242 GLchan
*dest
= (GLchan
*) texDestAddr
243 + dstZoffset
* (dstImageStride
/ sizeof(GLchan
))
244 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
245 + dstXoffset
* texComponents
;
246 for (img
= 0; img
< srcDepth
; img
++) {
247 GLchan
*destRow
= dest
;
248 for (row
= 0; row
< srcHeight
; row
++) {
249 const GLvoid
*src
= _mesa_image_address(srcPacking
,
250 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
251 _mesa_unpack_index_span(ctx
, srcWidth
, texType
, destRow
,
252 srcType
, src
, srcPacking
, transferOps
);
253 destRow
+= (dstRowStride
/ sizeof(GLchan
));
255 dest
+= dstImageStride
;
258 else if (texDestFormat
== GL_DEPTH_COMPONENT
) {
259 /* Depth texture (shadow maps) */
261 GLubyte
*dest
= (GLubyte
*) texDestAddr
262 + dstZoffset
* dstImageStride
263 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
264 + dstXoffset
* texComponents
;
265 for (img
= 0; img
< srcDepth
; img
++) {
266 GLubyte
*destRow
= dest
;
267 for (row
= 0; row
< srcHeight
; row
++) {
268 const GLvoid
*src
= _mesa_image_address(srcPacking
,
269 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
270 _mesa_unpack_depth_span(ctx
, srcWidth
, (GLfloat
*) destRow
,
271 srcType
, src
, srcPacking
);
272 destRow
+= (dstRowStride
/ sizeof(GLchan
));
274 dest
+= dstImageStride
;
278 /* regular, color texture */
279 if ((dimensions
== 1 && ctx
->Pixel
.Convolution1DEnabled
) ||
280 (dimensions
>= 2 && ctx
->Pixel
.Convolution2DEnabled
) ||
281 (dimensions
>= 2 && ctx
->Pixel
.Separable2DEnabled
)) {
283 * Fill texture image with convolution
286 GLint convWidth
= srcWidth
, convHeight
= srcHeight
;
287 GLfloat
*tmpImage
, *convImage
;
288 tmpImage
= (GLfloat
*) MALLOC(srcWidth
* srcHeight
* 4 * sizeof(GLfloat
));
290 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
293 convImage
= (GLfloat
*) MALLOC(srcWidth
* srcHeight
* 4 * sizeof(GLfloat
));
295 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
300 for (img
= 0; img
< srcDepth
; img
++) {
302 GLfloat
*dstf
= tmpImage
;
305 /* unpack and do transfer ops up to convolution */
306 for (row
= 0; row
< srcHeight
; row
++) {
307 const GLvoid
*src
= _mesa_image_address(srcPacking
,
308 srcAddr
, srcWidth
, srcHeight
,
309 srcFormat
, srcType
, img
, row
, 0);
310 _mesa_unpack_float_color_span(ctx
, srcWidth
, GL_RGBA
, dstf
,
311 srcFormat
, srcType
, src
, srcPacking
,
312 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
314 dstf
+= srcWidth
* 4;
318 if (dimensions
== 1) {
319 ASSERT(ctx
->Pixel
.Convolution1DEnabled
);
320 _mesa_convolve_1d_image(ctx
, &convWidth
, tmpImage
, convImage
);
323 if (ctx
->Pixel
.Convolution2DEnabled
) {
324 _mesa_convolve_2d_image(ctx
, &convWidth
, &convHeight
,
325 tmpImage
, convImage
);
328 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
329 _mesa_convolve_sep_image(ctx
, &convWidth
, &convHeight
,
330 tmpImage
, convImage
);
334 /* packing and transfer ops after convolution */
336 dest
= (GLchan
*) texDestAddr
337 + (dstZoffset
+ img
) * (dstImageStride
/ sizeof(GLchan
))
338 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
));
339 for (row
= 0; row
< convHeight
; row
++) {
340 _mesa_pack_float_rgba_span(ctx
, convWidth
,
341 (const GLfloat (*)[4]) srcf
,
342 texDestFormat
, CHAN_TYPE
,
343 dest
, &_mesa_native_packing
,
345 & IMAGE_POST_CONVOLUTION_BITS
);
346 srcf
+= convWidth
* 4;
347 dest
+= (dstRowStride
/ sizeof(GLchan
));
359 GLchan
*dest
= (GLchan
*) texDestAddr
360 + dstZoffset
* (dstImageStride
/ sizeof(GLchan
))
361 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
362 + dstXoffset
* texComponents
;
363 for (img
= 0; img
< srcDepth
; img
++) {
364 GLchan
*destRow
= dest
;
365 for (row
= 0; row
< srcHeight
; row
++) {
366 const GLvoid
*srcRow
= _mesa_image_address(srcPacking
,
367 srcAddr
, srcWidth
, srcHeight
,
368 srcFormat
, srcType
, img
, row
, 0);
369 _mesa_unpack_chan_color_span(ctx
, srcWidth
, texDestFormat
,
370 destRow
, srcFormat
, srcType
, srcRow
,
371 srcPacking
, transferOps
);
372 destRow
+= (dstRowStride
/ sizeof(GLchan
));
374 dest
+= dstImageStride
/ sizeof(GLchan
);
383 * Transfer a texture image from user space to <destAddr> applying all
384 * needed image transfer operations and storing the result in the format
385 * specified by <dstFormat>. <dstFormat> may be any format from texformat.h.
387 * dimensions - 1, 2 or 3
388 * baseInternalFormat - base format of the internal texture format
389 * specified by the user. This is very important, see below.
390 * dstFormat - destination image format
391 * dstAddr - destination address
392 * srcWidth, srcHeight, srcDepth - size of source iamge
393 * dstX/Y/Zoffset - as specified by glTexSubImage
394 * dstRowStride - stride between dest rows in bytes
395 * dstImageStride - stride between dest images in bytes
396 * srcFormat, srcType - incoming image format and datatype
397 * srcAddr - source image address
398 * srcPacking - packing params of source image
400 * XXX this function is a bit more complicated than it should be. If
401 * _mesa_convert_texsubimage[123]d could handle any dest/source formats
402 * or if transfer_teximage() could store in any MESA_FORMAT_* format, we
403 * could simplify things here.
406 _mesa_transfer_teximage(GLcontext
*ctx
, GLuint dimensions
,
407 GLenum baseInternalFormat
,
408 const struct gl_texture_format
*dstFormat
,
410 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
411 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
412 GLint dstRowStride
, GLint dstImageStride
,
413 GLenum srcFormat
, GLenum srcType
,
414 const GLvoid
*srcAddr
,
415 const struct gl_pixelstore_attrib
*srcPacking
)
417 const GLint dstRowStridePixels
= dstRowStride
/ dstFormat
->TexelBytes
;
418 const GLint dstImageStridePixels
= dstImageStride
/ dstFormat
->TexelBytes
;
420 GLuint transferOps
= ctx
->_ImageTransferState
;
421 GLboolean freeSourceData
= GL_FALSE
;
422 GLint postConvWidth
= srcWidth
, postConvHeight
= srcHeight
;
424 assert(baseInternalFormat
> 0);
426 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
427 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
432 * Consider this scenario: The user's source image is GL_RGB and the
433 * requested internal format is GL_LUMINANCE. Now suppose the device
434 * driver doesn't support GL_LUMINANCE and instead uses RGB16 as the
435 * texture format. In that case we still need to do an intermediate
436 * conversion to luminance format so that the incoming red channel gets
437 * replicated into the dest red, green and blue channels. The following
438 * code takes care of that.
440 if (dstFormat
->BaseFormat
!= baseInternalFormat
) {
441 /* Allocate storage for temporary image in the baseInternalFormat */
442 const GLint texelSize
= _mesa_components_in_format(baseInternalFormat
)
444 const GLint bytes
= texelSize
* postConvWidth
* postConvHeight
*srcDepth
;
445 const GLint tmpRowStride
= texelSize
* postConvWidth
;
446 const GLint tmpImgStride
= texelSize
* postConvWidth
* postConvHeight
;
447 GLvoid
*tmpImage
= MALLOC(bytes
);
450 transfer_teximage(ctx
, dimensions
, baseInternalFormat
, tmpImage
,
451 srcWidth
, srcHeight
, srcDepth
,
452 0, 0, 0, /* x/y/zoffset */
453 tmpRowStride
, tmpImgStride
,
454 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
456 /* this is our new source image */
457 srcWidth
= postConvWidth
;
458 srcHeight
= postConvHeight
;
459 srcFormat
= baseInternalFormat
;
462 srcPacking
= &_mesa_native_packing
;
463 freeSourceData
= GL_TRUE
;
464 transferOps
= 0; /* image transfer ops were completed */
467 /* Let the optimized tex conversion functions take a crack at the
468 * image conversion if the dest format is a h/w format.
470 if (_mesa_is_hardware_tex_format(dstFormat
)) {
475 if (dimensions
== 1) {
476 makeTemp
= !_mesa_convert_texsubimage1d(dstFormat
->MesaFormat
,
483 else if (dimensions
== 2) {
484 makeTemp
= !_mesa_convert_texsubimage2d(dstFormat
->MesaFormat
,
485 dstXoffset
, dstYoffset
,
493 assert(dimensions
== 3);
494 makeTemp
= !_mesa_convert_texsubimage3d(dstFormat
->MesaFormat
,
495 dstXoffset
, dstYoffset
, dstZoffset
,
496 srcWidth
, srcHeight
, srcDepth
,
497 dstRowStridePixels
, dstImageStridePixels
,
499 srcPacking
, srcAddr
, dstAddr
);
504 FREE((void *) srcAddr
);
510 /* software texture format */
515 GLint postConvWidth
= srcWidth
, postConvHeight
= srcHeight
;
517 GLuint tmpComps
, tmpTexelSize
;
518 GLint tmpRowStride
, tmpImageStride
;
521 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
522 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
526 tmpFormat
= dstFormat
->BaseFormat
;
527 tmpComps
= _mesa_components_in_format(tmpFormat
);
528 tmpTexelSize
= tmpComps
* sizeof(GLchan
);
529 tmpRowStride
= postConvWidth
* tmpTexelSize
;
530 tmpImageStride
= postConvWidth
* postConvHeight
* tmpTexelSize
;
531 tmpImage
= (GLubyte
*) MALLOC(postConvWidth
* postConvHeight
*
532 srcDepth
* tmpTexelSize
);
535 FREE((void *) srcAddr
);
539 transfer_teximage(ctx
, dimensions
, tmpFormat
, tmpImage
,
540 srcWidth
, srcHeight
, srcDepth
,
541 0, 0, 0, /* x/y/zoffset */
542 tmpRowStride
, tmpImageStride
,
543 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
546 FREE((void *) srcAddr
);
548 /* the temp image is our new source image */
549 srcWidth
= postConvWidth
;
550 srcHeight
= postConvHeight
;
551 srcFormat
= tmpFormat
;
554 srcPacking
= &_mesa_native_packing
;
555 freeSourceData
= GL_TRUE
;
558 if (_mesa_is_hardware_tex_format(dstFormat
)) {
560 if (dimensions
== 1) {
562 b
= _mesa_convert_texsubimage1d(dstFormat
->MesaFormat
,
570 else if (dimensions
== 2) {
572 b
= _mesa_convert_texsubimage2d(dstFormat
->MesaFormat
,
573 dstXoffset
, dstYoffset
,
583 b
= _mesa_convert_texsubimage3d(dstFormat
->MesaFormat
,
584 dstXoffset
, dstYoffset
, dstZoffset
,
585 srcWidth
, srcHeight
, srcDepth
,
586 dstRowStridePixels
, dstImageStridePixels
,
588 srcPacking
, srcAddr
, dstAddr
);
593 /* software format */
595 transfer_teximage(ctx
, dimensions
, dstFormat
->BaseFormat
, dstAddr
,
596 srcWidth
, srcHeight
, srcDepth
,
597 dstXoffset
, dstYoffset
, dstZoffset
,
598 dstRowStride
, dstImageStride
,
599 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
603 FREE((void *) srcAddr
); /* the temp image */
608 * This is the software fallback for Driver.TexImage1D().
609 * The texture image type will be GLchan.
610 * The texture image format will be GL_COLOR_INDEX, GL_INTENSITY,
611 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_RGB or GL_RGBA.
615 _mesa_store_teximage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
616 GLint internalFormat
,
617 GLint width
, GLint border
,
618 GLenum format
, GLenum type
, const GLvoid
*pixels
,
619 const struct gl_pixelstore_attrib
*packing
,
620 struct gl_texture_object
*texObj
,
621 struct gl_texture_image
*texImage
)
623 GLint postConvWidth
= width
;
624 GLint texelBytes
, sizeInBytes
;
626 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
627 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
630 /* choose the texture format */
631 assert(ctx
->Driver
.ChooseTextureFormat
);
632 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
633 internalFormat
, format
, type
);
634 assert(texImage
->TexFormat
);
635 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel1D
;
637 texelBytes
= texImage
->TexFormat
->TexelBytes
;
639 /* Compute image size, in bytes */
640 if (texImage
->IsCompressed
) {
641 assert(ctx
->Driver
.CompressedTextureSize
);
642 sizeInBytes
= ctx
->Driver
.CompressedTextureSize(ctx
, texImage
);
643 assert(sizeInBytes
> 0);
644 texImage
->CompressedSize
= sizeInBytes
;
647 sizeInBytes
= postConvWidth
* texelBytes
;
650 /* allocate memory */
651 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
652 if (!texImage
->Data
) {
653 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
658 /* unpack image, apply transfer ops and store in texImage->Data */
659 _mesa_transfer_teximage(ctx
, 1,
660 _mesa_base_tex_format(ctx
, internalFormat
),
661 texImage
->TexFormat
, texImage
->Data
,
662 width
, 1, 1, 0, 0, 0,
663 0, /* dstRowStride */
664 0, /* dstImageStride */
665 format
, type
, pixels
, packing
);
667 /* GL_SGIS_generate_mipmap */
668 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
669 _mesa_generate_mipmap(ctx
, target
,
670 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
678 * This is the software fallback for Driver.TexImage2D().
679 * The texture image type will be GLchan.
680 * The texture image format will be GL_COLOR_INDEX, GL_INTENSITY,
681 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_RGB or GL_RGBA.
683 * NOTE: if real texture compression is supported, this whole function
684 * will need to be overridden.
687 _mesa_store_teximage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
688 GLint internalFormat
,
689 GLint width
, GLint height
, GLint border
,
690 GLenum format
, GLenum type
, const void *pixels
,
691 const struct gl_pixelstore_attrib
*packing
,
692 struct gl_texture_object
*texObj
,
693 struct gl_texture_image
*texImage
)
695 GLint postConvWidth
= width
, postConvHeight
= height
;
696 GLint texelBytes
, sizeInBytes
;
698 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
699 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
703 /* choose the texture format */
704 assert(ctx
->Driver
.ChooseTextureFormat
);
705 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
706 internalFormat
, format
, type
);
707 assert(texImage
->TexFormat
);
708 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel2D
;
710 texelBytes
= texImage
->TexFormat
->TexelBytes
;
712 /* Compute image size, in bytes */
713 if (texImage
->IsCompressed
) {
714 assert(ctx
->Driver
.CompressedTextureSize
);
715 sizeInBytes
= ctx
->Driver
.CompressedTextureSize(ctx
, texImage
);
716 assert(sizeInBytes
> 0);
717 texImage
->CompressedSize
= sizeInBytes
;
720 sizeInBytes
= postConvWidth
* postConvHeight
* texelBytes
;
723 /* allocate memory */
724 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
725 if (!texImage
->Data
) {
726 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
731 /* unpack image, apply transfer ops and store in texImage->Data */
732 _mesa_transfer_teximage(ctx
, 2,
733 _mesa_base_tex_format(ctx
, internalFormat
),
734 texImage
->TexFormat
, texImage
->Data
,
735 width
, height
, 1, 0, 0, 0,
736 texImage
->Width
* texelBytes
,
737 0, /* dstImageStride */
738 format
, type
, pixels
, packing
);
740 /* GL_SGIS_generate_mipmap */
741 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
742 _mesa_generate_mipmap(ctx
, target
,
743 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
752 * This is the software fallback for Driver.TexImage3D().
753 * The texture image type will be GLchan.
754 * The texture image format will be GL_COLOR_INDEX, GL_INTENSITY,
755 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA, GL_RGB or GL_RGBA.
759 _mesa_store_teximage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
760 GLint internalFormat
,
761 GLint width
, GLint height
, GLint depth
, GLint border
,
762 GLenum format
, GLenum type
, const void *pixels
,
763 const struct gl_pixelstore_attrib
*packing
,
764 struct gl_texture_object
*texObj
,
765 struct gl_texture_image
*texImage
)
767 GLint texelBytes
, sizeInBytes
;
769 /* choose the texture format */
770 assert(ctx
->Driver
.ChooseTextureFormat
);
771 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
772 internalFormat
, format
, type
);
773 assert(texImage
->TexFormat
);
774 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel3D
;
776 texelBytes
= texImage
->TexFormat
->TexelBytes
;
778 /* Compute image size, in bytes */
779 if (texImage
->IsCompressed
) {
780 assert(ctx
->Driver
.CompressedTextureSize
);
781 sizeInBytes
= ctx
->Driver
.CompressedTextureSize(ctx
, texImage
);
782 assert(sizeInBytes
> 0);
783 texImage
->CompressedSize
= sizeInBytes
;
786 sizeInBytes
= width
* height
* depth
* texelBytes
;
789 /* allocate memory */
790 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
791 if (!texImage
->Data
) {
792 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
797 /* unpack image, apply transfer ops and store in texImage->Data */
798 _mesa_transfer_teximage(ctx
, 3,
799 _mesa_base_tex_format(ctx
, internalFormat
),
800 texImage
->TexFormat
, texImage
->Data
,
801 width
, height
, depth
, 0, 0, 0,
802 texImage
->Width
* texelBytes
,
803 texImage
->Width
* texImage
->Height
* texelBytes
,
804 format
, type
, pixels
, packing
);
806 /* GL_SGIS_generate_mipmap */
807 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
808 _mesa_generate_mipmap(ctx
, target
,
809 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
819 * This is the software fallback for Driver.TexSubImage1D().
822 _mesa_store_texsubimage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
823 GLint xoffset
, GLint width
,
824 GLenum format
, GLenum type
, const void *pixels
,
825 const struct gl_pixelstore_attrib
*packing
,
826 struct gl_texture_object
*texObj
,
827 struct gl_texture_image
*texImage
)
829 _mesa_transfer_teximage(ctx
, 1,
830 _mesa_base_tex_format(ctx
, texImage
->IntFormat
),
831 texImage
->TexFormat
, texImage
->Data
,
832 width
, 1, 1, /* src size */
833 xoffset
, 0, 0, /* dest offsets */
834 0, /* dstRowStride */
835 0, /* dstImageStride */
836 format
, type
, pixels
, packing
);
838 /* GL_SGIS_generate_mipmap */
839 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
840 _mesa_generate_mipmap(ctx
, target
,
841 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
848 * This is the software fallback for Driver.TexSubImage2D().
851 _mesa_store_texsubimage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
852 GLint xoffset
, GLint yoffset
,
853 GLint width
, GLint height
,
854 GLenum format
, GLenum type
, const void *pixels
,
855 const struct gl_pixelstore_attrib
*packing
,
856 struct gl_texture_object
*texObj
,
857 struct gl_texture_image
*texImage
)
859 _mesa_transfer_teximage(ctx
, 2,
860 _mesa_base_tex_format(ctx
, texImage
->IntFormat
),
861 texImage
->TexFormat
, texImage
->Data
,
862 width
, height
, 1, /* src size */
863 xoffset
, yoffset
, 0, /* dest offsets */
864 texImage
->Width
* texImage
->TexFormat
->TexelBytes
,
865 0, /* dstImageStride */
866 format
, type
, pixels
, packing
);
868 /* GL_SGIS_generate_mipmap */
869 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
870 _mesa_generate_mipmap(ctx
, target
,
871 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
878 * This is the software fallback for Driver.TexSubImage3D().
881 _mesa_store_texsubimage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
882 GLint xoffset
, GLint yoffset
, GLint zoffset
,
883 GLint width
, GLint height
, GLint depth
,
884 GLenum format
, GLenum type
, const void *pixels
,
885 const struct gl_pixelstore_attrib
*packing
,
886 struct gl_texture_object
*texObj
,
887 struct gl_texture_image
*texImage
)
889 const GLint texelBytes
= texImage
->TexFormat
->TexelBytes
;
890 _mesa_transfer_teximage(ctx
, 3,
891 _mesa_base_tex_format(ctx
, texImage
->IntFormat
),
892 texImage
->TexFormat
, texImage
->Data
,
893 width
, height
, depth
, /* src size */
894 xoffset
, yoffset
, xoffset
, /* dest offsets */
895 texImage
->Width
* texelBytes
,
896 texImage
->Width
* texImage
->Height
* texelBytes
,
897 format
, type
, pixels
, packing
);
898 /* GL_SGIS_generate_mipmap */
899 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
900 _mesa_generate_mipmap(ctx
, target
,
901 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
910 * Fallback for Driver.CompressedTexImage1D()
913 _mesa_store_compressed_teximage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
914 GLint internalFormat
,
915 GLint width
, GLint border
,
916 GLsizei imageSize
, const GLvoid
*data
,
917 struct gl_texture_object
*texObj
,
918 struct gl_texture_image
*texImage
)
921 * The device driver has to do it all.
928 * Fallback for Driver.CompressedTexImage2D()
931 _mesa_store_compressed_teximage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
932 GLint internalFormat
,
933 GLint width
, GLint height
, GLint border
,
934 GLsizei imageSize
, const GLvoid
*data
,
935 struct gl_texture_object
*texObj
,
936 struct gl_texture_image
*texImage
)
939 * The device driver has to do it all.
946 * Fallback for Driver.CompressedTexImage3D()
949 _mesa_store_compressed_teximage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
950 GLint internalFormat
,
951 GLint width
, GLint height
, GLint depth
,
953 GLsizei imageSize
, const GLvoid
*data
,
954 struct gl_texture_object
*texObj
,
955 struct gl_texture_image
*texImage
)
958 * The device driver has to do it all.
965 * Fallback for Driver.GetCompressedTexImage3D()
966 * This will probably work find for hardware drivers. That is, hardware
967 * drivers won't have to override this function, unless the compressed
968 * texture must first be fetched from the TRAM.
971 _mesa_get_compressed_teximage(GLcontext
*ctx
, GLenum target
,
972 GLint level
, void *image
,
973 const struct gl_texture_object
*texObj
,
974 struct gl_texture_image
*texImage
)
976 assert(texImage
->IsCompressed
);
977 assert(texImage
->CompressedSize
> 0);
978 MEMCPY(image
, texImage
->Data
, texImage
->CompressedSize
);
984 * This is the fallback for Driver.TestProxyTexImage().
987 _mesa_test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
988 GLint internalFormat
, GLenum format
, GLenum type
,
989 GLint width
, GLint height
, GLint depth
, GLint border
)
991 struct gl_texture_unit
*texUnit
;
992 struct gl_texture_object
*texObj
;
993 struct gl_texture_image
*texImage
;
998 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
999 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1000 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1003 * The core Mesa code will have already tested the image size, etc.
1004 * If a driver has more stringent texture limits to enforce it will
1005 * have to override this function.
1007 /* choose the texture format */
1008 assert(ctx
->Driver
.ChooseTextureFormat
);
1009 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
1010 internalFormat
, format
, type
);
1011 assert(texImage
->TexFormat
);
1019 * Average together two rows of a source image to produce a single new
1020 * row in the dest image. It's legal for the two source rows to point
1021 * to the same data. The source width must be equal to either the
1022 * dest width or two times the dest width.
1025 do_row(const struct gl_texture_format
*format
, GLint srcWidth
,
1026 const GLvoid
*srcRowA
, const GLvoid
*srcRowB
,
1027 GLint dstWidth
, GLvoid
*dstRow
)
1029 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
1030 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
1032 assert(srcWidth
== dstWidth
|| srcWidth
== 2 * dstWidth
);
1034 switch (format
->MesaFormat
) {
1035 case MESA_FORMAT_RGBA
:
1038 const GLchan (*rowA
)[4] = (const GLchan (*)[4]) srcRowA
;
1039 const GLchan (*rowB
)[4] = (const GLchan (*)[4]) srcRowB
;
1040 GLchan (*dst
)[4] = (GLchan (*)[4]) dstRow
;
1041 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1042 i
++, j
+= colStride
, k
+= colStride
) {
1043 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1044 rowB
[j
][0] + rowB
[k
][0]) / 4;
1045 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1046 rowB
[j
][1] + rowB
[k
][1]) / 4;
1047 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1048 rowB
[j
][2] + rowB
[k
][2]) / 4;
1049 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
1050 rowB
[j
][3] + rowB
[k
][3]) / 4;
1054 case MESA_FORMAT_RGB
:
1057 const GLchan (*rowA
)[3] = (const GLchan (*)[3]) srcRowA
;
1058 const GLchan (*rowB
)[3] = (const GLchan (*)[3]) srcRowB
;
1059 GLchan (*dst
)[3] = (GLchan (*)[3]) dstRow
;
1060 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1061 i
++, j
+= colStride
, k
+= colStride
) {
1062 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1063 rowB
[j
][0] + rowB
[k
][0]) / 4;
1064 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1065 rowB
[j
][1] + rowB
[k
][1]) / 4;
1066 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1067 rowB
[j
][2] + rowB
[k
][2]) / 4;
1071 case MESA_FORMAT_ALPHA
:
1072 case MESA_FORMAT_LUMINANCE
:
1073 case MESA_FORMAT_INTENSITY
:
1074 case MESA_FORMAT_COLOR_INDEX
:
1077 const GLchan
*rowA
= (const GLchan
*) srcRowA
;
1078 const GLchan
*rowB
= (const GLchan
*) srcRowB
;
1079 GLchan
*dst
= (GLchan
*) dstRow
;
1080 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1081 i
++, j
+= colStride
, k
+= colStride
) {
1082 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) / 4;
1086 case MESA_FORMAT_LUMINANCE_ALPHA
:
1089 const GLchan (*rowA
)[2] = (const GLchan (*)[2]) srcRowA
;
1090 const GLchan (*rowB
)[2] = (const GLchan (*)[2]) srcRowB
;
1091 GLchan (*dst
)[2] = (GLchan (*)[2]) dstRow
;
1092 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1093 i
++, j
+= colStride
, k
+= colStride
) {
1094 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1095 rowB
[j
][0] + rowB
[k
][0]) / 4;
1096 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1097 rowB
[j
][1] + rowB
[k
][1]) / 4;
1101 case MESA_FORMAT_DEPTH_COMPONENT
:
1104 const GLfloat
*rowA
= (const GLfloat
*) srcRowA
;
1105 const GLfloat
*rowB
= (const GLfloat
*) srcRowB
;
1106 GLfloat
*dst
= (GLfloat
*) dstRow
;
1107 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1108 i
++, j
+= colStride
, k
+= colStride
) {
1109 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) * 0.25F
;
1113 /* Begin hardware formats */
1114 case MESA_FORMAT_RGBA8888
:
1115 case MESA_FORMAT_ARGB8888
:
1118 const GLubyte (*rowA
)[4] = (const GLubyte (*)[4]) srcRowA
;
1119 const GLubyte (*rowB
)[4] = (const GLubyte (*)[4]) srcRowB
;
1120 GLubyte (*dst
)[4] = (GLubyte (*)[4]) dstRow
;
1121 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1122 i
++, j
+= colStride
, k
+= colStride
) {
1123 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1124 rowB
[j
][0] + rowB
[k
][0]) / 4;
1125 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1126 rowB
[j
][1] + rowB
[k
][1]) / 4;
1127 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1128 rowB
[j
][2] + rowB
[k
][2]) / 4;
1129 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
1130 rowB
[j
][3] + rowB
[k
][3]) / 4;
1134 case MESA_FORMAT_RGB888
:
1137 const GLubyte (*rowA
)[3] = (const GLubyte (*)[3]) srcRowA
;
1138 const GLubyte (*rowB
)[3] = (const GLubyte (*)[3]) srcRowB
;
1139 GLubyte (*dst
)[3] = (GLubyte (*)[3]) dstRow
;
1140 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1141 i
++, j
+= colStride
, k
+= colStride
) {
1142 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1143 rowB
[j
][0] + rowB
[k
][0]) / 4;
1144 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1145 rowB
[j
][1] + rowB
[k
][1]) / 4;
1146 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
1147 rowB
[j
][2] + rowB
[k
][2]) / 4;
1151 case MESA_FORMAT_RGB565
:
1154 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
1155 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
1156 GLushort
*dst
= (GLushort
*) dstRow
;
1157 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1158 i
++, j
+= colStride
, k
+= colStride
) {
1159 const GLint rowAr0
= rowA
[j
] & 0x1f;
1160 const GLint rowAr1
= rowA
[k
] & 0x1f;
1161 const GLint rowBr0
= rowB
[j
] & 0x1f;
1162 const GLint rowBr1
= rowB
[k
] & 0x1f;
1163 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x3f;
1164 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x3f;
1165 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x3f;
1166 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x3f;
1167 const GLint rowAb0
= (rowA
[j
] >> 11) & 0x1f;
1168 const GLint rowAb1
= (rowA
[k
] >> 11) & 0x1f;
1169 const GLint rowBb0
= (rowB
[j
] >> 11) & 0x1f;
1170 const GLint rowBb1
= (rowB
[k
] >> 11) & 0x1f;
1171 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1172 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1173 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1174 dst
[i
] = (blue
<< 11) | (green
<< 5) | red
;
1178 case MESA_FORMAT_ARGB4444
:
1181 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
1182 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
1183 GLushort
*dst
= (GLushort
*) dstRow
;
1184 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1185 i
++, j
+= colStride
, k
+= colStride
) {
1186 const GLint rowAr0
= rowA
[j
] & 0xf;
1187 const GLint rowAr1
= rowA
[k
] & 0xf;
1188 const GLint rowBr0
= rowB
[j
] & 0xf;
1189 const GLint rowBr1
= rowB
[k
] & 0xf;
1190 const GLint rowAg0
= (rowA
[j
] >> 4) & 0xf;
1191 const GLint rowAg1
= (rowA
[k
] >> 4) & 0xf;
1192 const GLint rowBg0
= (rowB
[j
] >> 4) & 0xf;
1193 const GLint rowBg1
= (rowB
[k
] >> 4) & 0xf;
1194 const GLint rowAb0
= (rowA
[j
] >> 8) & 0xf;
1195 const GLint rowAb1
= (rowA
[k
] >> 8) & 0xf;
1196 const GLint rowBb0
= (rowB
[j
] >> 8) & 0xf;
1197 const GLint rowBb1
= (rowB
[k
] >> 8) & 0xf;
1198 const GLint rowAa0
= (rowA
[j
] >> 12) & 0xf;
1199 const GLint rowAa1
= (rowA
[k
] >> 12) & 0xf;
1200 const GLint rowBa0
= (rowB
[j
] >> 12) & 0xf;
1201 const GLint rowBa1
= (rowB
[k
] >> 12) & 0xf;
1202 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1203 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1204 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1205 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 4;
1206 dst
[i
] = (alpha
<< 12) | (blue
<< 8) | (green
<< 4) | red
;
1210 case MESA_FORMAT_ARGB1555
:
1213 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
1214 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
1215 GLushort
*dst
= (GLushort
*) dstRow
;
1216 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1217 i
++, j
+= colStride
, k
+= colStride
) {
1218 const GLint rowAr0
= rowA
[j
] & 0x1f;
1219 const GLint rowAr1
= rowA
[k
] & 0x1f;
1220 const GLint rowBr0
= rowB
[j
] & 0x1f;
1221 const GLint rowBr1
= rowB
[k
] & 0xf;
1222 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x1f;
1223 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x1f;
1224 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x1f;
1225 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x1f;
1226 const GLint rowAb0
= (rowA
[j
] >> 10) & 0x1f;
1227 const GLint rowAb1
= (rowA
[k
] >> 10) & 0x1f;
1228 const GLint rowBb0
= (rowB
[j
] >> 10) & 0x1f;
1229 const GLint rowBb1
= (rowB
[k
] >> 10) & 0x1f;
1230 const GLint rowAa0
= (rowA
[j
] >> 15) & 0x1;
1231 const GLint rowAa1
= (rowA
[k
] >> 15) & 0x1;
1232 const GLint rowBa0
= (rowB
[j
] >> 15) & 0x1;
1233 const GLint rowBa1
= (rowB
[k
] >> 15) & 0x1;
1234 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1235 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1236 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1237 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 4;
1238 dst
[i
] = (alpha
<< 15) | (blue
<< 10) | (green
<< 5) | red
;
1242 case MESA_FORMAT_AL88
:
1245 const GLubyte (*rowA
)[2] = (const GLubyte (*)[2]) srcRowA
;
1246 const GLubyte (*rowB
)[2] = (const GLubyte (*)[2]) srcRowB
;
1247 GLubyte (*dst
)[2] = (GLubyte (*)[2]) dstRow
;
1248 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1249 i
++, j
+= colStride
, k
+= colStride
) {
1250 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
1251 rowB
[j
][0] + rowB
[k
][0]) >> 2;
1252 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
1253 rowB
[j
][1] + rowB
[k
][1]) >> 2;
1257 case MESA_FORMAT_RGB332
:
1260 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
1261 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
1262 GLubyte
*dst
= (GLubyte
*) dstRow
;
1263 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1264 i
++, j
+= colStride
, k
+= colStride
) {
1265 const GLint rowAr0
= rowA
[j
] & 0x3;
1266 const GLint rowAr1
= rowA
[k
] & 0x3;
1267 const GLint rowBr0
= rowB
[j
] & 0x3;
1268 const GLint rowBr1
= rowB
[k
] & 0x3;
1269 const GLint rowAg0
= (rowA
[j
] >> 2) & 0x7;
1270 const GLint rowAg1
= (rowA
[k
] >> 2) & 0x7;
1271 const GLint rowBg0
= (rowB
[j
] >> 2) & 0x7;
1272 const GLint rowBg1
= (rowB
[k
] >> 2) & 0x7;
1273 const GLint rowAb0
= (rowA
[j
] >> 5) & 0x7;
1274 const GLint rowAb1
= (rowA
[k
] >> 5) & 0x7;
1275 const GLint rowBb0
= (rowB
[j
] >> 5) & 0x7;
1276 const GLint rowBb1
= (rowB
[k
] >> 5) & 0x7;
1277 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 4;
1278 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 4;
1279 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 4;
1280 dst
[i
] = (blue
<< 5) | (green
<< 2) | red
;
1284 case MESA_FORMAT_A8
:
1285 case MESA_FORMAT_L8
:
1286 case MESA_FORMAT_I8
:
1287 case MESA_FORMAT_CI8
:
1290 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
1291 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
1292 GLubyte
*dst
= (GLubyte
*) dstRow
;
1293 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
1294 i
++, j
+= colStride
, k
+= colStride
) {
1295 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) >> 2;
1300 _mesa_problem(NULL
, "bad format in do_row()");
1306 * These functions generate a 1/2-size mipmap image from a source image.
1307 * Texture borders are handled by copying or averaging the source image's
1308 * border texels, depending on the scale-down factor.
1312 make_1d_mipmap(const struct gl_texture_format
*format
, GLint border
,
1313 GLint srcWidth
, const GLubyte
*srcPtr
,
1314 GLint dstWidth
, GLubyte
*dstPtr
)
1316 const GLint bpt
= format
->TexelBytes
;
1320 /* skip the border pixel, if any */
1321 src
= srcPtr
+ border
* bpt
;
1322 dst
= dstPtr
+ border
* bpt
;
1324 /* we just duplicate the input row, kind of hack, saves code */
1325 do_row(format
, srcWidth
- 2 * border
, src
, src
,
1326 dstWidth
- 2 * border
, dst
);
1329 /* copy left-most pixel from source */
1330 MEMCPY(dstPtr
, srcPtr
, bpt
);
1331 /* copy right-most pixel from source */
1332 MEMCPY(dstPtr
+ (dstWidth
- 1) * bpt
,
1333 srcPtr
+ (srcWidth
- 1) * bpt
,
1340 make_2d_mipmap(const struct gl_texture_format
*format
, GLint border
,
1341 GLint srcWidth
, GLint srcHeight
, const GLubyte
*srcPtr
,
1342 GLint dstWidth
, GLint dstHeight
, GLubyte
*dstPtr
)
1344 const GLint bpt
= format
->TexelBytes
;
1345 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
1346 const GLint dstWidthNB
= dstWidth
- 2 * border
;
1347 const GLint dstHeightNB
= dstHeight
- 2 * border
;
1348 const GLint srcRowStride
= bpt
* srcWidth
;
1349 const GLint dstRowStride
= bpt
* dstWidth
;
1350 const GLubyte
*srcA
, *srcB
;
1352 GLint row
, colStride
;
1354 colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
1356 /* Compute src and dst pointers, skipping any border */
1357 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
1359 srcB
= srcA
+ srcRowStride
;
1362 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
1364 for (row
= 0; row
< dstHeightNB
; row
++) {
1365 do_row(format
, srcWidthNB
, srcA
, srcB
,
1367 srcA
+= 2 * srcRowStride
;
1368 srcB
+= 2 * srcRowStride
;
1369 dst
+= dstRowStride
;
1372 /* This is ugly but probably won't be used much */
1374 /* fill in dest border */
1375 /* lower-left border pixel */
1376 MEMCPY(dstPtr
, srcPtr
, bpt
);
1377 /* lower-right border pixel */
1378 MEMCPY(dstPtr
+ (dstWidth
- 1) * bpt
,
1379 srcPtr
+ (srcWidth
- 1) * bpt
, bpt
);
1380 /* upper-left border pixel */
1381 MEMCPY(dstPtr
+ dstWidth
* (dstHeight
- 1) * bpt
,
1382 srcPtr
+ srcWidth
* (srcHeight
- 1) * bpt
, bpt
);
1383 /* upper-right border pixel */
1384 MEMCPY(dstPtr
+ (dstWidth
* dstHeight
- 1) * bpt
,
1385 srcPtr
+ (srcWidth
* srcHeight
- 1) * bpt
, bpt
);
1387 do_row(format
, srcWidthNB
,
1390 dstWidthNB
, dstPtr
+ bpt
);
1392 do_row(format
, srcWidthNB
,
1393 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
1394 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
1396 dstPtr
+ (dstWidth
* (dstHeight
- 1) + 1) * bpt
);
1397 /* left and right borders */
1398 if (srcHeight
== dstHeight
) {
1399 /* copy border pixel from src to dst */
1400 for (row
= 1; row
< srcHeight
; row
++) {
1401 MEMCPY(dstPtr
+ dstWidth
* row
* bpt
,
1402 srcPtr
+ srcWidth
* row
* bpt
, bpt
);
1403 MEMCPY(dstPtr
+ (dstWidth
* row
+ dstWidth
- 1) * bpt
,
1404 srcPtr
+ (srcWidth
* row
+ srcWidth
- 1) * bpt
, bpt
);
1408 /* average two src pixels each dest pixel */
1409 for (row
= 0; row
< dstHeightNB
; row
+= 2) {
1411 srcPtr
+ (srcWidth
* (row
* 2 + 1)) * bpt
,
1412 srcPtr
+ (srcWidth
* (row
* 2 + 2)) * bpt
,
1413 1, dstPtr
+ (dstWidth
* row
+ 1) * bpt
);
1415 srcPtr
+ (srcWidth
* (row
* 2 + 1) + srcWidth
- 1) * bpt
,
1416 srcPtr
+ (srcWidth
* (row
* 2 + 2) + srcWidth
- 1) * bpt
,
1417 1, dstPtr
+ (dstWidth
* row
+ 1 + dstWidth
- 1) * bpt
);
1425 make_3d_mipmap(const struct gl_texture_format
*format
, GLint border
,
1426 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1427 const GLubyte
*srcPtr
,
1428 GLint dstWidth
, GLint dstHeight
, GLint dstDepth
,
1431 const GLint bpt
= format
->TexelBytes
;
1432 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
1433 const GLint srcDepthNB
= srcDepth
- 2 * border
;
1434 const GLint dstWidthNB
= dstWidth
- 2 * border
;
1435 const GLint dstHeightNB
= dstHeight
- 2 * border
;
1436 const GLint dstDepthNB
= dstDepth
- 2 * border
;
1437 GLvoid
*tmpRowA
, *tmpRowB
;
1439 GLint bytesPerSrcImage
, bytesPerDstImage
;
1440 GLint bytesPerSrcRow
, bytesPerDstRow
;
1441 GLint srcImageOffset
, srcRowOffset
;
1443 (void) srcDepthNB
; /* silence warnings */
1445 /* Need two temporary row buffers */
1446 tmpRowA
= MALLOC(srcWidth
* bpt
);
1449 tmpRowB
= MALLOC(srcWidth
* bpt
);
1455 bytesPerSrcImage
= srcWidth
* srcHeight
* bpt
;
1456 bytesPerDstImage
= dstWidth
* dstHeight
* bpt
;
1458 bytesPerSrcRow
= srcWidth
* bpt
;
1459 bytesPerDstRow
= dstWidth
* bpt
;
1461 /* Offset between adjacent src images to be averaged together */
1462 srcImageOffset
= (srcDepth
== dstDepth
) ? 0 : bytesPerSrcImage
;
1464 /* Offset between adjacent src rows to be averaged together */
1465 srcRowOffset
= (srcHeight
== dstHeight
) ? 0 : srcWidth
* bpt
;
1468 * Need to average together up to 8 src pixels for each dest pixel.
1469 * Break that down into 3 operations:
1470 * 1. take two rows from source image and average them together.
1471 * 2. take two rows from next source image and average them together.
1472 * 3. take the two averaged rows and average them for the final dst row.
1476 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1477 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1480 for (img
= 0; img
< dstDepthNB
; img
++) {
1481 /* first source image pointer, skipping border */
1482 const GLubyte
*imgSrcA
= srcPtr
1483 + (bytesPerSrcImage
+ bytesPerSrcRow
+ border
) * bpt
* border
1484 + img
* (bytesPerSrcImage
+ srcImageOffset
);
1485 /* second source image pointer, skipping border */
1486 const GLubyte
*imgSrcB
= imgSrcA
+ srcImageOffset
;
1487 /* address of the dest image, skipping border */
1488 GLubyte
*imgDst
= dstPtr
1489 + (bytesPerDstImage
+ bytesPerDstRow
+ border
) * bpt
* border
1490 + img
* bytesPerDstImage
;
1492 /* setup the four source row pointers and the dest row pointer */
1493 const GLubyte
*srcImgARowA
= imgSrcA
;
1494 const GLubyte
*srcImgARowB
= imgSrcA
+ srcRowOffset
;
1495 const GLubyte
*srcImgBRowA
= imgSrcB
;
1496 const GLubyte
*srcImgBRowB
= imgSrcB
+ srcRowOffset
;
1497 GLubyte
*dstImgRow
= imgDst
;
1499 for (row
= 0; row
< dstHeightNB
; row
++) {
1500 /* Average together two rows from first src image */
1501 do_row(format
, srcWidthNB
, srcImgARowA
, srcImgARowB
,
1502 srcWidthNB
, tmpRowA
);
1503 /* Average together two rows from second src image */
1504 do_row(format
, srcWidthNB
, srcImgBRowA
, srcImgBRowB
,
1505 srcWidthNB
, tmpRowB
);
1506 /* Average together the temp rows to make the final row */
1507 do_row(format
, srcWidthNB
, tmpRowA
, tmpRowB
,
1508 dstWidthNB
, dstImgRow
);
1509 /* advance to next rows */
1510 srcImgARowA
+= bytesPerSrcRow
+ srcRowOffset
;
1511 srcImgARowB
+= bytesPerSrcRow
+ srcRowOffset
;
1512 srcImgBRowA
+= bytesPerSrcRow
+ srcRowOffset
;
1513 srcImgBRowB
+= bytesPerSrcRow
+ srcRowOffset
;
1514 dstImgRow
+= bytesPerDstRow
;
1521 /* Luckily we can leverage the make_2d_mipmap() function here! */
1523 /* do front border image */
1524 make_2d_mipmap(format
, 1, srcWidth
, srcHeight
, srcPtr
,
1525 dstWidth
, dstHeight
, dstPtr
);
1526 /* do back border image */
1527 make_2d_mipmap(format
, 1, srcWidth
, srcHeight
,
1528 srcPtr
+ bytesPerSrcImage
* (srcDepth
- 1),
1529 dstWidth
, dstHeight
,
1530 dstPtr
+ bytesPerDstImage
* (dstDepth
- 1));
1531 /* do four remaining border edges that span the image slices */
1532 if (srcDepth
== dstDepth
) {
1533 /* just copy border pixels from src to dst */
1534 for (img
= 0; img
< dstDepthNB
; img
++) {
1538 /* do border along [img][row=0][col=0] */
1539 src
= srcPtr
+ (img
+ 1) * bytesPerSrcImage
;
1540 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
;
1541 MEMCPY(dst
, src
, bpt
);
1543 /* do border along [img][row=dstHeight-1][col=0] */
1544 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1545 + (srcHeight
- 1) * bytesPerSrcRow
;
1546 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1547 + (dstHeight
- 1) * bytesPerDstRow
;
1548 MEMCPY(dst
, src
, bpt
);
1550 /* do border along [img][row=0][col=dstWidth-1] */
1551 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1552 + (srcWidth
- 1) * bpt
;
1553 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1554 + (dstWidth
- 1) * bpt
;
1555 MEMCPY(dst
, src
, bpt
);
1557 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1558 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1559 + (bytesPerSrcImage
- bpt
);
1560 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1561 + (bytesPerDstImage
- bpt
);
1562 MEMCPY(dst
, src
, bpt
);
1566 /* average border pixels from adjacent src image pairs */
1567 ASSERT(srcDepthNB
== 2 * dstDepthNB
);
1568 for (img
= 0; img
< dstDepthNB
; img
++) {
1572 /* do border along [img][row=0][col=0] */
1573 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
;
1574 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
;
1575 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1577 /* do border along [img][row=dstHeight-1][col=0] */
1578 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1579 + (srcHeight
- 1) * bytesPerSrcRow
;
1580 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1581 + (dstHeight
- 1) * bytesPerDstRow
;
1582 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1584 /* do border along [img][row=0][col=dstWidth-1] */
1585 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1586 + (srcWidth
- 1) * bpt
;
1587 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1588 + (dstWidth
- 1) * bpt
;
1589 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1591 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1592 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
1593 + (bytesPerSrcImage
- bpt
);
1594 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
1595 + (bytesPerDstImage
- bpt
);
1596 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
1604 * For GL_SGIX_generate_mipmap:
1605 * Generate a complete set of mipmaps from texObj's base-level image.
1606 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
1609 _mesa_generate_mipmap(GLcontext
*ctx
, GLenum target
,
1610 const struct gl_texture_unit
*texUnit
,
1611 struct gl_texture_object
*texObj
)
1614 GLint maxLevels
= 0;
1617 ASSERT(texObj
->Image
[texObj
->BaseLevel
]);
1619 switch (texObj
->Target
) {
1621 maxLevels
= ctx
->Const
.MaxTextureLevels
;
1624 maxLevels
= ctx
->Const
.MaxTextureLevels
;
1627 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
1629 case GL_TEXTURE_CUBE_MAP_ARB
:
1630 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
1632 case GL_TEXTURE_RECTANGLE_NV
:
1637 "Bad texture object dimension in _mesa_generate_mipmaps");
1641 for (level
= texObj
->BaseLevel
; level
< texObj
->MaxLevel
1642 && level
< maxLevels
- 1; level
++) {
1643 /* generate image[level+1] from image[level] */
1644 const struct gl_texture_image
*srcImage
;
1645 struct gl_texture_image
*dstImage
;
1646 GLint srcWidth
, srcHeight
, srcDepth
;
1647 GLint dstWidth
, dstHeight
, dstDepth
;
1648 GLint border
, bytesPerTexel
;
1651 srcImage
= texObj
->Image
[level
];
1653 srcWidth
= srcImage
->Width
;
1654 srcHeight
= srcImage
->Height
;
1655 srcDepth
= srcImage
->Depth
;
1656 border
= srcImage
->Border
;
1657 bytesPerTexel
= srcImage
->TexFormat
->TexelBytes
;
1659 /* compute next (level+1) image size */
1660 if (srcWidth
- 2 * border
> 1) {
1661 dstWidth
= (srcWidth
- 2 * border
) / 2 + 2 * border
;
1664 dstWidth
= srcWidth
; /* can't go smaller */
1666 if (srcHeight
- 2 * border
> 1) {
1667 dstHeight
= (srcHeight
- 2 * border
) / 2 + 2 * border
;
1670 dstHeight
= srcHeight
; /* can't go smaller */
1672 if (srcDepth
- 2 * border
> 1) {
1673 dstDepth
= (srcDepth
- 2 * border
) / 2 + 2 * border
;
1676 dstDepth
= srcDepth
; /* can't go smaller */
1679 if (dstWidth
== srcWidth
&&
1680 dstHeight
== srcHeight
&&
1681 dstDepth
== srcDepth
) {
1686 /* get dest gl_texture_image */
1687 dstImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
+1);
1689 dstImage
= _mesa_alloc_texture_image();
1691 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
1694 _mesa_set_tex_image(texObj
, target
, level
+ 1, dstImage
);
1697 /* Free old image data */
1699 MESA_PBUFFER_FREE(dstImage
->Data
);
1701 /* initialize new image */
1702 _mesa_init_teximage_fields(ctx
, t
, dstImage
, dstWidth
, dstHeight
,
1703 dstDepth
, border
, srcImage
->Format
);
1704 dstImage
->DriverData
= NULL
;
1705 dstImage
->TexFormat
= srcImage
->TexFormat
;
1706 dstImage
->FetchTexel
= srcImage
->FetchTexel
;
1707 ASSERT(dstImage
->TexFormat
);
1708 ASSERT(dstImage
->FetchTexel
);
1710 ASSERT(dstWidth
* dstHeight
* dstDepth
* bytesPerTexel
> 0);
1712 /* alloc new image buffer */
1713 dstImage
->Data
= MESA_PBUFFER_ALLOC(dstWidth
* dstHeight
* dstDepth
1715 if (!dstImage
->Data
) {
1716 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
1721 * We use simple 2x2 averaging to compute the next mipmap level.
1725 make_1d_mipmap(srcImage
->TexFormat
, border
,
1726 srcWidth
, (const GLubyte
*) srcImage
->Data
,
1727 dstWidth
, (GLubyte
*) dstImage
->Data
);
1730 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
1731 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
1732 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
1733 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
1734 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
1735 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
1736 make_2d_mipmap(srcImage
->TexFormat
, border
,
1737 srcWidth
, srcHeight
, (const GLubyte
*) srcImage
->Data
,
1738 dstWidth
, dstHeight
, (GLubyte
*) dstImage
->Data
);
1741 make_3d_mipmap(srcImage
->TexFormat
, border
,
1742 srcWidth
, srcHeight
, srcDepth
,
1743 (const GLubyte
*) srcImage
->Data
,
1744 dstWidth
, dstHeight
, dstDepth
,
1745 (GLubyte
*) dstImage
->Data
);
1747 case GL_TEXTURE_RECTANGLE_NV
:
1748 /* no mipmaps, do nothing */
1751 _mesa_problem(ctx
, "bad dimensions in _mesa_generate_mipmaps");
1754 } /* loop over mipmap levels */