2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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.
55 #include "bufferobj.h"
62 #include "texcompress.h"
63 #include "texformat.h"
70 /*** NEWTEXSTORE ***********************************************************/
72 static const GLint ZERO
= 1000, ONE
= 1001;
75 * When promoting texture formats (see below) we need to compute the
76 * mapping of dest components back to source components.
77 * This function does that.
78 * \param logicalBaseFormat the logical format of the texture
79 * \param textureBaseFormat the final texture format
80 * \return map[4] the four mapping values
83 compute_component_mapping(GLenum logicalBaseFormat
, GLenum textureBaseFormat
,
86 /* compute mapping from dest components back to src components */
87 switch (logicalBaseFormat
) {
89 map
[0] = map
[1] = map
[2] = 0;
90 if (textureBaseFormat
== GL_RGBA
)
94 ASSERT(textureBaseFormat
== GL_RGBA
);
95 map
[0] = map
[1] = map
[2] = ZERO
;
99 map
[0] = map
[1] = map
[2] = 0;
100 if (textureBaseFormat
== GL_RGBA
)
103 case GL_LUMINANCE_ALPHA
:
104 ASSERT(textureBaseFormat
== GL_RGBA
);
105 map
[0] = map
[1] = map
[2] = 0;
109 ASSERT(textureBaseFormat
== GL_RGBA
);
116 _mesa_problem(NULL
, "Unexpected logicalBaseFormat");
117 map
[0] = map
[1] = map
[2] = map
[3] = 0;
123 * Make a temporary (color) texture image with GLfloat components.
124 * Apply all needed pixel unpacking and pixel transfer operations.
125 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
126 * Suppose the user specifies GL_LUMINANCE as the internal texture format
127 * but the graphics hardware doesn't support luminance textures. So, might
128 * use an RGB hardware format instead.
129 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
131 * \param ctx the rendering context
132 * \param dims image dimensions: 1, 2 or 3
133 * \param logicalBaseFormat basic texture derived from the user's
134 * internal texture format value
135 * \param textureBaseFormat the actual basic format of the texture
136 * \param srcWidth source image width
137 * \param srcHeight source image height
138 * \param srcDepth source image depth
139 * \param srcFormat source image format
140 * \param srcType source image type
141 * \param srcAddr source image address
142 * \param srcPacking source image pixel packing
143 * \return resulting image with format = textureBaseFormat and type = GLfloat.
146 make_temp_float_image(GLcontext
*ctx
, GLuint dims
,
147 GLenum logicalBaseFormat
,
148 GLenum textureBaseFormat
,
149 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
150 GLenum srcFormat
, GLenum srcType
,
151 const GLvoid
*srcAddr
,
152 const struct gl_pixelstore_attrib
*srcPacking
)
154 GLuint transferOps
= ctx
->_ImageTransferState
;
157 ASSERT(dims
>= 1 && dims
<= 3);
159 ASSERT(logicalBaseFormat
== GL_RGBA
||
160 logicalBaseFormat
== GL_RGB
||
161 logicalBaseFormat
== GL_LUMINANCE_ALPHA
||
162 logicalBaseFormat
== GL_LUMINANCE
||
163 logicalBaseFormat
== GL_ALPHA
||
164 logicalBaseFormat
== GL_INTENSITY
||
165 logicalBaseFormat
== GL_COLOR_INDEX
||
166 logicalBaseFormat
== GL_DEPTH_COMPONENT
);
168 ASSERT(textureBaseFormat
== GL_RGBA
||
169 textureBaseFormat
== GL_RGB
||
170 textureBaseFormat
== GL_LUMINANCE_ALPHA
||
171 textureBaseFormat
== GL_LUMINANCE
||
172 textureBaseFormat
== GL_ALPHA
||
173 textureBaseFormat
== GL_INTENSITY
||
174 textureBaseFormat
== GL_COLOR_INDEX
||
175 textureBaseFormat
== GL_DEPTH_COMPONENT
);
177 /* conventional color image */
179 if ((dims
== 1 && ctx
->Pixel
.Convolution1DEnabled
) ||
180 (dims
>= 2 && ctx
->Pixel
.Convolution2DEnabled
) ||
181 (dims
>= 2 && ctx
->Pixel
.Separable2DEnabled
)) {
182 /* need image convolution */
183 const GLuint preConvTransferOps
184 = (transferOps
& IMAGE_PRE_CONVOLUTION_BITS
) | IMAGE_CLAMP_BIT
;
185 const GLuint postConvTransferOps
186 = (transferOps
& IMAGE_POST_CONVOLUTION_BITS
) | IMAGE_CLAMP_BIT
;
188 GLint convWidth
, convHeight
;
191 /* pre-convolution image buffer (3D) */
192 tempImage
= (GLfloat
*) _mesa_malloc(srcWidth
* srcHeight
* srcDepth
193 * 4 * sizeof(GLfloat
));
197 /* post-convolution image buffer (2D) */
198 convImage
= (GLfloat
*) _mesa_malloc(srcWidth
* srcHeight
199 * 4 * sizeof(GLfloat
));
201 _mesa_free(tempImage
);
205 /* loop over 3D image slices */
206 for (img
= 0; img
< srcDepth
; img
++) {
207 GLfloat
*dst
= tempImage
+ img
* (srcWidth
* srcHeight
* 4);
209 /* unpack and do transfer ops up to convolution */
210 for (row
= 0; row
< srcHeight
; row
++) {
211 const GLvoid
*src
= _mesa_image_address(srcPacking
,
212 srcAddr
, srcWidth
, srcHeight
,
213 srcFormat
, srcType
, img
, row
, 0);
214 _mesa_unpack_color_span_float(ctx
, srcWidth
, GL_RGBA
, dst
,
215 srcFormat
, srcType
, src
,
223 GLfloat
*src
= tempImage
+ img
* (srcWidth
* srcHeight
* 4);
224 convWidth
= srcWidth
;
225 convHeight
= srcHeight
;
227 ASSERT(ctx
->Pixel
.Convolution1DEnabled
);
228 _mesa_convolve_1d_image(ctx
, &convWidth
, src
, convImage
);
231 if (ctx
->Pixel
.Convolution2DEnabled
) {
232 _mesa_convolve_2d_image(ctx
, &convWidth
, &convHeight
,
236 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
237 _mesa_convolve_sep_image(ctx
, &convWidth
, &convHeight
,
243 /* do post-convolution transfer and pack into tempImage */
245 const GLfloat
*src
= convImage
;
246 GLfloat
*dst
= tempImage
+ img
* (convWidth
* convHeight
* 4);
247 for (row
= 0; row
< convHeight
; row
++) {
248 _mesa_pack_rgba_span_float(ctx
, convWidth
,
249 (const GLfloat (*)[4]) src
,
250 logicalBaseFormat
, GL_FLOAT
,
251 dst
, &ctx
->DefaultPacking
,
252 postConvTransferOps
);
253 src
+= convWidth
* 4;
254 dst
+= convWidth
* 4;
257 } /* loop over 3D image slices */
259 _mesa_free(convImage
);
261 /* might need these below */
262 srcWidth
= convWidth
;
263 srcHeight
= convHeight
;
267 const GLint components
= _mesa_components_in_format(logicalBaseFormat
);
268 const GLint srcStride
= _mesa_image_row_stride(srcPacking
,
269 srcWidth
, srcFormat
, srcType
);
273 tempImage
= (GLfloat
*) _mesa_malloc(srcWidth
* srcHeight
* srcDepth
274 * components
* sizeof(GLfloat
));
279 for (img
= 0; img
< srcDepth
; img
++) {
281 = (const GLubyte
*) _mesa_image_address(srcPacking
, srcAddr
,
285 for (row
= 0; row
< srcHeight
; row
++) {
286 _mesa_unpack_color_span_float(ctx
, srcWidth
, logicalBaseFormat
,
287 dst
, srcFormat
, srcType
, src
,
288 srcPacking
, transferOps
);
289 dst
+= srcWidth
* components
;
295 if (logicalBaseFormat
!= textureBaseFormat
) {
297 GLint texComponents
= _mesa_components_in_format(textureBaseFormat
);
298 GLint logComponents
= _mesa_components_in_format(logicalBaseFormat
);
303 /* we only promote up to RGB and RGBA formats for now */
304 ASSERT(textureBaseFormat
== GL_RGB
|| textureBaseFormat
== GL_RGBA
);
306 /* The actual texture format should have at least as many components
307 * as the logical texture format.
309 ASSERT(texComponents
>= logComponents
);
311 newImage
= (GLfloat
*) _mesa_malloc(srcWidth
* srcHeight
* srcDepth
312 * texComponents
* sizeof(GLfloat
));
314 _mesa_free(tempImage
);
318 compute_component_mapping(logicalBaseFormat
, textureBaseFormat
, map
);
320 n
= srcWidth
* srcHeight
* srcDepth
;
321 for (i
= 0; i
< n
; i
++) {
323 for (k
= 0; k
< texComponents
; k
++) {
326 newImage
[i
* texComponents
+ k
] = 0.0F
;
328 newImage
[i
* texComponents
+ k
] = 1.0F
;
330 newImage
[i
* texComponents
+ k
] = tempImage
[i
* logComponents
+ j
];
334 _mesa_free(tempImage
);
335 tempImage
= newImage
;
343 * Make a temporary (color) texture image with GLchan components.
344 * Apply all needed pixel unpacking and pixel transfer operations.
345 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
346 * Suppose the user specifies GL_LUMINANCE as the internal texture format
347 * but the graphics hardware doesn't support luminance textures. So, might
348 * use an RGB hardware format instead.
349 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
351 * \param ctx the rendering context
352 * \param dims image dimensions: 1, 2 or 3
353 * \param logicalBaseFormat basic texture derived from the user's
354 * internal texture format value
355 * \param textureBaseFormat the actual basic format of the texture
356 * \param srcWidth source image width
357 * \param srcHeight source image height
358 * \param srcDepth source image depth
359 * \param srcFormat source image format
360 * \param srcType source image type
361 * \param srcAddr source image address
362 * \param srcPacking source image pixel packing
363 * \return resulting image with format = textureBaseFormat and type = GLchan.
366 make_temp_chan_image(GLcontext
*ctx
, GLuint dims
,
367 GLenum logicalBaseFormat
,
368 GLenum textureBaseFormat
,
369 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
370 GLenum srcFormat
, GLenum srcType
,
371 const GLvoid
*srcAddr
,
372 const struct gl_pixelstore_attrib
*srcPacking
)
374 GLuint transferOps
= ctx
->_ImageTransferState
;
375 const GLint components
= _mesa_components_in_format(logicalBaseFormat
);
376 GLboolean freeSrcImage
= GL_FALSE
;
378 GLchan
*tempImage
, *dst
;
380 ASSERT(dims
>= 1 && dims
<= 3);
382 ASSERT(logicalBaseFormat
== GL_RGBA
||
383 logicalBaseFormat
== GL_RGB
||
384 logicalBaseFormat
== GL_LUMINANCE_ALPHA
||
385 logicalBaseFormat
== GL_LUMINANCE
||
386 logicalBaseFormat
== GL_ALPHA
||
387 logicalBaseFormat
== GL_INTENSITY
);
389 ASSERT(textureBaseFormat
== GL_RGBA
||
390 textureBaseFormat
== GL_RGB
||
391 textureBaseFormat
== GL_LUMINANCE_ALPHA
||
392 textureBaseFormat
== GL_LUMINANCE
||
393 textureBaseFormat
== GL_ALPHA
||
394 textureBaseFormat
== GL_INTENSITY
);
396 if ((dims
== 1 && ctx
->Pixel
.Convolution1DEnabled
) ||
397 (dims
>= 2 && ctx
->Pixel
.Convolution2DEnabled
) ||
398 (dims
>= 2 && ctx
->Pixel
.Separable2DEnabled
)) {
399 /* get convolved image */
400 GLfloat
*convImage
= make_temp_float_image(ctx
, dims
,
403 srcWidth
, srcHeight
, srcDepth
,
405 srcAddr
, srcPacking
);
408 /* the convolved image is our new source image */
410 srcFormat
= logicalBaseFormat
;
412 srcPacking
= &ctx
->DefaultPacking
;
413 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
415 freeSrcImage
= GL_TRUE
;
418 /* unpack and transfer the source image */
419 tempImage
= (GLchan
*) _mesa_malloc(srcWidth
* srcHeight
* srcDepth
420 * components
* sizeof(GLchan
));
425 for (img
= 0; img
< srcDepth
; img
++) {
426 const GLint srcStride
= _mesa_image_row_stride(srcPacking
,
430 = (const GLubyte
*) _mesa_image_address(srcPacking
, srcAddr
,
434 for (row
= 0; row
< srcHeight
; row
++) {
435 _mesa_unpack_color_span_chan(ctx
, srcWidth
, logicalBaseFormat
,
436 dst
, srcFormat
, srcType
, src
,
437 srcPacking
, transferOps
);
438 dst
+= srcWidth
* components
;
443 /* If we made a temporary image for convolution, free it here */
445 _mesa_free((void *) srcAddr
);
448 if (logicalBaseFormat
!= textureBaseFormat
) {
449 /* one more conversion step */
450 GLint texComponents
= _mesa_components_in_format(textureBaseFormat
);
451 GLint logComponents
= _mesa_components_in_format(logicalBaseFormat
);
456 /* we only promote up to RGB and RGBA formats for now */
457 ASSERT(textureBaseFormat
== GL_RGB
|| textureBaseFormat
== GL_RGBA
);
459 /* The actual texture format should have at least as many components
460 * as the logical texture format.
462 ASSERT(texComponents
>= logComponents
);
464 newImage
= (GLchan
*) _mesa_malloc(srcWidth
* srcHeight
* srcDepth
465 * texComponents
* sizeof(GLchan
));
467 _mesa_free(tempImage
);
471 compute_component_mapping(logicalBaseFormat
, textureBaseFormat
, map
);
473 n
= srcWidth
* srcHeight
* srcDepth
;
474 for (i
= 0; i
< n
; i
++) {
476 for (k
= 0; k
< texComponents
; k
++) {
479 newImage
[i
* texComponents
+ k
] = 0;
481 newImage
[i
* texComponents
+ k
] = CHAN_MAX
;
483 newImage
[i
* texComponents
+ k
] = tempImage
[i
* logComponents
+ j
];
487 _mesa_free(tempImage
);
488 tempImage
= newImage
;
497 * Teximage storage routine for when a simple memcpy will do.
498 * No pixel transfer operations or special texel encodings allowed.
499 * 1D, 2D and 3D images supported.
502 memcpy_texture(const struct gl_texture_format
*dstFormat
,
504 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
505 GLint dstRowStride
, GLint dstImageStride
,
506 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
507 GLenum srcFormat
, GLenum srcType
,
508 const GLvoid
*srcAddr
,
509 const struct gl_pixelstore_attrib
*srcPacking
)
511 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
,
513 const GLint srcImageStride
= _mesa_image_image_stride(srcPacking
,
514 srcWidth
, srcHeight
, srcFormat
, srcType
);
515 const GLubyte
*srcImage
= (const GLubyte
*) _mesa_image_address(srcPacking
,
516 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, 0, 0, 0);
517 const GLint bytesPerRow
= srcWidth
* dstFormat
->TexelBytes
;
518 const GLint bytesPerImage
= srcHeight
* bytesPerRow
;
519 const GLint bytesPerTexture
= srcDepth
* bytesPerImage
;
520 GLubyte
*dstImage
= (GLubyte
*) dstAddr
521 + dstZoffset
* dstImageStride
522 + dstYoffset
* dstRowStride
523 + dstXoffset
* dstFormat
->TexelBytes
;
525 if (dstRowStride
== srcRowStride
&&
526 dstRowStride
== bytesPerRow
&&
527 ((dstImageStride
== srcImageStride
&&
528 dstImageStride
== bytesPerImage
) ||
531 _mesa_memcpy(dstImage
, srcImage
, bytesPerTexture
);
535 for (img
= 0; img
< srcDepth
; img
++) {
536 const GLubyte
*srcRow
= srcImage
;
537 GLubyte
*dstRow
= dstImage
;
538 for (row
= 0; row
< srcHeight
; row
++) {
539 _mesa_memcpy(dstRow
, srcRow
, bytesPerRow
);
540 dstRow
+= dstRowStride
;
541 srcRow
+= srcRowStride
;
543 srcImage
+= srcImageStride
;
544 dstImage
+= dstImageStride
;
552 * Store an image in any of the formats:
553 * _mesa_texformat_rgba
554 * _mesa_texformat_rgb
555 * _mesa_texformat_alpha
556 * _mesa_texformat_luminance
557 * _mesa_texformat_luminance_alpha
558 * _mesa_texformat_intensity
560 * \param dims either 1 or 2 or 3
561 * \param baseInternalFormat user-specified base internal format
562 * \param dstFormat destination Mesa texture format
563 * \param dstAddr destination image address
564 * \param dstX/Y/Zoffset destination x/y/z offset (ala TexSubImage), in texels
565 * \param dstRowStride destination image row stride, in bytes
566 * \param dstImageStride destination image layer stride, in bytes
567 * \param srcWidth/Height/Depth source image size, in pixels
568 * \param srcFormat incoming image format
569 * \param srcType incoming image data type
570 * \param srcAddr source image address
571 * \param srcPacking source image packing parameters
574 _mesa_texstore_rgba(GLcontext
*ctx
, GLuint dims
,
575 GLenum baseInternalFormat
,
576 const struct gl_texture_format
*dstFormat
,
578 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
579 GLint dstRowStride
, GLint dstImageStride
,
580 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
581 GLenum srcFormat
, GLenum srcType
,
582 const GLvoid
*srcAddr
,
583 const struct gl_pixelstore_attrib
*srcPacking
)
585 const GLint components
= _mesa_components_in_format(baseInternalFormat
);
587 ASSERT(dstFormat
== &_mesa_texformat_rgba
||
588 dstFormat
== &_mesa_texformat_rgb
||
589 dstFormat
== &_mesa_texformat_alpha
||
590 dstFormat
== &_mesa_texformat_luminance
||
591 dstFormat
== &_mesa_texformat_luminance_alpha
||
592 dstFormat
== &_mesa_texformat_intensity
);
593 ASSERT(baseInternalFormat
== GL_RGBA
||
594 baseInternalFormat
== GL_RGB
||
595 baseInternalFormat
== GL_ALPHA
||
596 baseInternalFormat
== GL_LUMINANCE
||
597 baseInternalFormat
== GL_LUMINANCE_ALPHA
||
598 baseInternalFormat
== GL_INTENSITY
);
599 ASSERT(dstFormat
->TexelBytes
== components
* sizeof(GLchan
));
601 if (!ctx
->_ImageTransferState
&&
602 !srcPacking
->SwapBytes
&&
603 baseInternalFormat
== srcFormat
&&
604 srcType
== CHAN_TYPE
) {
605 /* simple memcpy path */
606 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
607 dstRowStride
, dstImageStride
,
608 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
609 srcAddr
, srcPacking
);
611 else if (!ctx
->_ImageTransferState
&&
612 !srcPacking
->SwapBytes
&&
613 dstFormat
== &_mesa_texformat_rgb
&&
614 srcFormat
== GL_RGBA
&&
615 srcType
== CHAN_TYPE
) {
616 /* extract RGB from RGBA */
618 GLchan
*dstImage
= (GLchan
*) (GLubyte
*) dstAddr
619 + dstZoffset
* dstImageStride
620 + dstYoffset
* dstRowStride
621 + dstXoffset
* dstFormat
->TexelBytes
;
622 for (img
= 0; img
< srcDepth
; img
++) {
623 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
624 srcWidth
, srcFormat
, srcType
);
625 GLchan
*srcRow
= _mesa_image_address(srcPacking
, srcAddr
, srcWidth
,
626 srcHeight
, srcFormat
, srcType
,
628 GLchan
*dstRow
= dstImage
;
629 for (row
= 0; row
< srcHeight
; row
++) {
630 for (col
= 0; col
< srcWidth
; col
++) {
631 dstRow
[col
* 3 + RCOMP
] = srcRow
[col
* 4 + RCOMP
];
632 dstRow
[col
* 3 + GCOMP
] = srcRow
[col
* 4 + GCOMP
];
633 dstRow
[col
* 3 + BCOMP
] = srcRow
[col
* 4 + BCOMP
];
635 dstRow
+= dstRowStride
;
636 srcRow
= (GLchan
*) ((GLubyte
*) srcRow
+ srcRowStride
);
638 dstImage
+= dstImageStride
;
643 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
645 dstFormat
->BaseFormat
,
646 srcWidth
, srcHeight
, srcDepth
,
647 srcFormat
, srcType
, srcAddr
,
649 const GLchan
*src
= tempImage
;
650 const GLint bytesPerRow
= srcWidth
* components
* sizeof(GLchan
);
651 GLubyte
*dstImage
= (GLubyte
*) dstAddr
652 + dstZoffset
* dstImageStride
653 + dstYoffset
* dstRowStride
654 + dstXoffset
* dstFormat
->TexelBytes
;
658 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
659 for (img
= 0; img
< srcDepth
; img
++) {
660 GLubyte
*dstRow
= dstImage
;
661 for (row
= 0; row
< srcHeight
; row
++) {
662 _mesa_memcpy(dstRow
, src
, bytesPerRow
);
663 dstRow
+= dstRowStride
;
664 src
+= srcWidth
* components
;
666 dstImage
+= dstImageStride
;
669 _mesa_free((void *) tempImage
);
676 * Store a color index texture image
679 _mesa_texstore_color_index(STORE_PARAMS
)
681 ASSERT(dstFormat
== &_mesa_texformat_color_index
);
682 ASSERT(dstFormat
->TexelBytes
== 1 * sizeof(GLchan
));
684 if (!ctx
->_ImageTransferState
&&
685 !srcPacking
->SwapBytes
&&
686 baseInternalFormat
== GL_COLOR_INDEX
&&
687 srcFormat
== GL_COLOR_INDEX
&&
688 srcType
== CHAN_TYPE
) {
689 /* simple memcpy path */
690 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
691 dstRowStride
, dstImageStride
,
692 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
693 srcAddr
, srcPacking
);
697 GLubyte
*dstImage
= (GLubyte
*) dstAddr
698 + dstZoffset
* dstImageStride
699 + dstYoffset
* dstRowStride
700 + dstXoffset
* dstFormat
->TexelBytes
;
702 for (img
= 0; img
< srcDepth
; img
++) {
703 GLubyte
*dstRow
= dstImage
;
704 for (row
= 0; row
< srcHeight
; row
++) {
705 const GLvoid
*src
= _mesa_image_address(srcPacking
,
706 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
707 _mesa_unpack_index_span(ctx
, srcWidth
, CHAN_TYPE
, dstRow
,
708 srcType
, src
, srcPacking
,
709 ctx
->_ImageTransferState
);
710 dstRow
+= dstRowStride
;
712 dstImage
+= dstImageStride
;
720 * Store a floating point depth component texture image.
723 _mesa_texstore_depth_component_float32(STORE_PARAMS
)
725 ASSERT(dstFormat
== &_mesa_texformat_depth_component_float32
);
726 ASSERT(dstFormat
->TexelBytes
== sizeof(GLfloat
));
728 if (!ctx
->_ImageTransferState
&&
729 !srcPacking
->SwapBytes
&&
730 baseInternalFormat
== GL_DEPTH_COMPONENT
&&
731 srcFormat
== GL_DEPTH_COMPONENT
&&
732 srcType
== GL_FLOAT
) {
733 /* simple memcpy path */
734 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
735 dstRowStride
, dstImageStride
,
736 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
737 srcAddr
, srcPacking
);
741 GLubyte
*dstImage
= (GLubyte
*) dstAddr
742 + dstZoffset
* dstImageStride
743 + dstYoffset
* dstRowStride
744 + dstXoffset
* dstFormat
->TexelBytes
;
746 for (img
= 0; img
< srcDepth
; img
++) {
747 GLubyte
*dstRow
= dstImage
;
748 for (row
= 0; row
< srcHeight
; row
++) {
749 const GLvoid
*src
= _mesa_image_address(srcPacking
,
750 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
751 _mesa_unpack_depth_span(ctx
, srcWidth
, (GLfloat
*) dstRow
,
752 srcType
, src
, srcPacking
);
753 dstRow
+= dstRowStride
;
755 dstImage
+= dstImageStride
;
763 * Store a 16-bit integer depth component texture image.
766 _mesa_texstore_depth_component16(STORE_PARAMS
)
768 ASSERT(dstFormat
== &_mesa_texformat_depth_component16
);
769 ASSERT(dstFormat
->TexelBytes
== sizeof(GLushort
));
771 if (!ctx
->_ImageTransferState
&&
772 !srcPacking
->SwapBytes
&&
773 baseInternalFormat
== GL_DEPTH_COMPONENT
&&
774 srcFormat
== GL_DEPTH_COMPONENT
&&
775 srcType
== GL_UNSIGNED_SHORT
) {
776 /* simple memcpy path */
777 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
778 dstRowStride
, dstImageStride
,
779 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
780 srcAddr
, srcPacking
);
784 GLubyte
*dstImage
= (GLubyte
*) dstAddr
785 + dstZoffset
* dstImageStride
786 + dstYoffset
* dstRowStride
787 + dstXoffset
* dstFormat
->TexelBytes
;
789 for (img
= 0; img
< srcDepth
; img
++) {
790 GLubyte
*dstRow
= dstImage
;
791 for (row
= 0; row
< srcHeight
; row
++) {
792 GLfloat depthTemp
[MAX_WIDTH
];
793 const GLvoid
*src
= _mesa_image_address(srcPacking
,
794 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
795 GLushort
*dst16
= (GLushort
*) dstRow
;
796 _mesa_unpack_depth_span(ctx
, srcWidth
, depthTemp
,
797 srcType
, src
, srcPacking
);
798 for (col
= 0; col
< srcWidth
; col
++) {
799 dst16
[col
] = (GLushort
) (depthTemp
[col
] * 65535.0F
);
801 dstRow
+= dstRowStride
;
803 dstImage
+= dstImageStride
;
813 * Store an rgb565 texture image.
816 _mesa_texstore_rgb565(STORE_PARAMS
)
818 ASSERT(dstFormat
== &_mesa_texformat_rgb565
);
819 ASSERT(dstFormat
->TexelBytes
== 2);
821 if (!ctx
->_ImageTransferState
&&
822 !srcPacking
->SwapBytes
&&
823 baseInternalFormat
== GL_RGB
&&
824 srcFormat
== GL_RGB
&&
825 srcType
== GL_UNSIGNED_SHORT_5_6_5
) {
826 /* simple memcpy path */
827 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
828 dstRowStride
, dstImageStride
,
829 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
830 srcAddr
, srcPacking
);
832 else if (!ctx
->_ImageTransferState
&&
833 !srcPacking
->SwapBytes
&&
834 baseInternalFormat
== GL_RGB
&&
835 srcFormat
== GL_RGB
&&
836 srcType
== GL_UNSIGNED_BYTE
&&
838 /* do optimized tex store */
839 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
,
841 const GLubyte
*src
= (const GLubyte
*)
842 _mesa_image_address(srcPacking
, srcAddr
, srcWidth
, srcHeight
,
843 srcFormat
, srcType
, 0, 0, 0);
844 GLubyte
*dst
= (GLubyte
*) dstAddr
845 + dstZoffset
* dstImageStride
846 + dstYoffset
* dstRowStride
847 + dstXoffset
* dstFormat
->TexelBytes
;
849 for (row
= 0; row
< srcHeight
; row
++) {
850 const GLubyte
*srcUB
= (const GLubyte
*) src
;
851 GLushort
*dstUS
= (GLushort
*) dst
;
852 for (col
= 0; col
< srcWidth
; col
++) {
853 dstUS
[col
] = PACK_COLOR_565( srcUB
[0], srcUB
[1], srcUB
[2] );
862 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
864 dstFormat
->BaseFormat
,
865 srcWidth
, srcHeight
, srcDepth
,
866 srcFormat
, srcType
, srcAddr
,
868 const GLchan
*src
= tempImage
;
869 GLubyte
*dstImage
= (GLubyte
*) dstAddr
870 + dstZoffset
* dstImageStride
871 + dstYoffset
* dstRowStride
872 + dstXoffset
* dstFormat
->TexelBytes
;
876 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
877 for (img
= 0; img
< srcDepth
; img
++) {
878 GLubyte
*dstRow
= dstImage
;
879 for (row
= 0; row
< srcHeight
; row
++) {
880 GLushort
*dstUS
= (GLushort
*) dstRow
;
881 for (col
= 0; col
< srcWidth
; col
++) {
882 dstUS
[col
] = PACK_COLOR_565( CHAN_TO_UBYTE(src
[RCOMP
]),
883 CHAN_TO_UBYTE(src
[GCOMP
]),
884 CHAN_TO_UBYTE(src
[BCOMP
]) );
887 dstRow
+= dstRowStride
;
889 dstImage
+= dstImageStride
;
891 _mesa_free((void *) tempImage
);
898 _mesa_texstore_rgba8888(STORE_PARAMS
)
901 const GLubyte littleEndian
= *((const GLubyte
*) &ui
);
903 ASSERT(dstFormat
== &_mesa_texformat_rgba8888
);
904 ASSERT(dstFormat
->TexelBytes
== 4);
906 if (!ctx
->_ImageTransferState
&&
907 !srcPacking
->SwapBytes
&&
908 baseInternalFormat
== GL_RGBA
&&
909 srcFormat
== GL_RGBA
&&
910 ((srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
&& littleEndian
) ||
911 (srcType
== GL_UNSIGNED_INT_8_8_8_8
&& !littleEndian
))) {
912 /* simple memcpy path */
913 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
914 dstRowStride
, dstImageStride
,
915 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
916 srcAddr
, srcPacking
);
920 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
922 dstFormat
->BaseFormat
,
923 srcWidth
, srcHeight
, srcDepth
,
924 srcFormat
, srcType
, srcAddr
,
926 const GLchan
*src
= tempImage
;
927 GLubyte
*dstImage
= (GLubyte
*) dstAddr
928 + dstZoffset
* dstImageStride
929 + dstYoffset
* dstRowStride
930 + dstXoffset
* dstFormat
->TexelBytes
;
934 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
935 for (img
= 0; img
< srcDepth
; img
++) {
936 GLubyte
*dstRow
= dstImage
;
937 for (row
= 0; row
< srcHeight
; row
++) {
938 GLuint
*dstUI
= (GLuint
*) dstRow
;
939 for (col
= 0; col
< srcWidth
; col
++) {
940 dstUI
[col
] = PACK_COLOR_8888( CHAN_TO_UBYTE(src
[RCOMP
]),
941 CHAN_TO_UBYTE(src
[GCOMP
]),
942 CHAN_TO_UBYTE(src
[BCOMP
]),
943 CHAN_TO_UBYTE(src
[ACOMP
]) );
946 dstRow
+= dstRowStride
;
948 dstImage
+= dstImageStride
;
950 _mesa_free((void *) tempImage
);
957 _mesa_texstore_argb8888(STORE_PARAMS
)
960 const GLubyte littleEndian
= *((const GLubyte
*) &ui
);
962 ASSERT(dstFormat
== &_mesa_texformat_argb8888
);
963 ASSERT(dstFormat
->TexelBytes
== 4);
965 if (!ctx
->_ImageTransferState
&&
966 !srcPacking
->SwapBytes
&&
967 baseInternalFormat
== GL_RGBA
&&
968 srcFormat
== GL_BGRA
&&
969 ((srcType
== GL_UNSIGNED_BYTE
&& littleEndian
) ||
970 (srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
&& littleEndian
) ||
971 (srcType
== GL_UNSIGNED_INT_8_8_8_8
&& !littleEndian
))) {
972 /* simple memcpy path */
973 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
974 dstRowStride
, dstImageStride
,
975 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
976 srcAddr
, srcPacking
);
980 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
982 dstFormat
->BaseFormat
,
983 srcWidth
, srcHeight
, srcDepth
,
984 srcFormat
, srcType
, srcAddr
,
986 const GLchan
*src
= tempImage
;
987 GLubyte
*dstImage
= (GLubyte
*) dstAddr
988 + dstZoffset
* dstImageStride
989 + dstYoffset
* dstRowStride
990 + dstXoffset
* dstFormat
->TexelBytes
;
994 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
995 for (img
= 0; img
< srcDepth
; img
++) {
996 GLubyte
*dstRow
= dstImage
;
997 for (row
= 0; row
< srcHeight
; row
++) {
998 GLuint
*dstUI
= (GLuint
*) dstRow
;
999 for (col
= 0; col
< srcWidth
; col
++) {
1000 dstUI
[col
] = PACK_COLOR_8888( CHAN_TO_UBYTE(src
[ACOMP
]),
1001 CHAN_TO_UBYTE(src
[RCOMP
]),
1002 CHAN_TO_UBYTE(src
[GCOMP
]),
1003 CHAN_TO_UBYTE(src
[BCOMP
]) );
1006 dstRow
+= dstRowStride
;
1008 dstImage
+= dstImageStride
;
1010 _mesa_free((void *) tempImage
);
1018 _mesa_texstore_rgb888(STORE_PARAMS
)
1020 const GLuint ui
= 1;
1021 const GLubyte littleEndian
= *((const GLubyte
*) &ui
);
1023 ASSERT(dstFormat
== &_mesa_texformat_rgb888
);
1024 ASSERT(dstFormat
->TexelBytes
== 3);
1026 if (!ctx
->_ImageTransferState
&&
1027 !srcPacking
->SwapBytes
&&
1028 baseInternalFormat
== GL_RGB
&&
1029 srcFormat
== GL_BGR
&&
1030 srcType
== GL_UNSIGNED_BYTE
&&
1032 /* simple memcpy path */
1033 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1034 dstRowStride
, dstImageStride
,
1035 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1036 srcAddr
, srcPacking
);
1038 else if (!ctx
->_ImageTransferState
&&
1039 !srcPacking
->SwapBytes
&&
1040 srcFormat
== GL_RGBA
&&
1041 srcType
== GL_UNSIGNED_BYTE
) {
1042 /* extract BGR from RGBA */
1044 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1045 + dstZoffset
* dstImageStride
1046 + dstYoffset
* dstRowStride
1047 + dstXoffset
* dstFormat
->TexelBytes
;
1048 for (img
= 0; img
< srcDepth
; img
++) {
1049 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
1050 srcWidth
, srcFormat
, srcType
);
1051 GLubyte
*srcRow
= _mesa_image_address(srcPacking
, srcAddr
, srcWidth
,
1052 srcHeight
, srcFormat
, srcType
,
1054 GLubyte
*dstRow
= dstImage
;
1055 for (row
= 0; row
< srcHeight
; row
++) {
1056 for (col
= 0; col
< srcWidth
; col
++) {
1057 dstRow
[col
* 3 + 0] = srcRow
[col
* 4 + BCOMP
];
1058 dstRow
[col
* 3 + 1] = srcRow
[col
* 4 + GCOMP
];
1059 dstRow
[col
* 3 + 2] = srcRow
[col
* 4 + RCOMP
];
1061 dstRow
+= dstRowStride
;
1062 srcRow
+= srcRowStride
;
1064 dstImage
+= dstImageStride
;
1069 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
1071 dstFormat
->BaseFormat
,
1072 srcWidth
, srcHeight
, srcDepth
,
1073 srcFormat
, srcType
, srcAddr
,
1075 const GLchan
*src
= (const GLubyte
*) tempImage
;
1076 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1077 + dstZoffset
* dstImageStride
1078 + dstYoffset
* dstRowStride
1079 + dstXoffset
* dstFormat
->TexelBytes
;
1080 GLint img
, row
, col
;
1083 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1084 for (img
= 0; img
< srcDepth
; img
++) {
1085 GLubyte
*dstRow
= dstImage
;
1086 for (row
= 0; row
< srcHeight
; row
++) {
1089 for (col
= 0; col
< srcWidth
; col
++) {
1090 dstRow
[col
* 3 + 0] = CHAN_TO_UBYTE(src
[RCOMP
]);
1091 dstRow
[col
* 3 + 1] = CHAN_TO_UBYTE(src
[GCOMP
]);
1092 dstRow
[col
* 3 + 2] = CHAN_TO_UBYTE(src
[BCOMP
]);
1097 for (col
= 0; col
< srcWidth
; col
++) {
1098 dstRow
[col
* 3 + 0] = srcUB
[BCOMP
];
1099 dstRow
[col
* 3 + 1] = srcUB
[GCOMP
];
1100 dstRow
[col
* 3 + 2] = srcUB
[RCOMP
];
1105 for (col
= 0; col
< srcWidth
; col
++) {
1106 dstRow
[col
* 3 + 0] = CHAN_TO_UBYTE(src
[BCOMP
]);
1107 dstRow
[col
* 3 + 1] = CHAN_TO_UBYTE(src
[GCOMP
]);
1108 dstRow
[col
* 3 + 2] = CHAN_TO_UBYTE(src
[RCOMP
]);
1112 dstRow
+= dstRowStride
;
1114 dstImage
+= dstImageStride
;
1116 _mesa_free((void *) tempImage
);
1123 _mesa_texstore_argb4444(STORE_PARAMS
)
1125 const GLuint ui
= 1;
1126 const GLubyte littleEndian
= *((const GLubyte
*) &ui
);
1128 ASSERT(dstFormat
== &_mesa_texformat_argb4444
);
1129 ASSERT(dstFormat
->TexelBytes
== 2);
1131 if (!ctx
->_ImageTransferState
&&
1132 !srcPacking
->SwapBytes
&&
1133 baseInternalFormat
== GL_RGBA
&&
1134 srcFormat
== GL_BGRA
&&
1135 ((srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
&& littleEndian
) ||
1136 (srcType
== GL_UNSIGNED_SHORT_4_4_4_4
&& !littleEndian
))) {
1137 /* simple memcpy path */
1138 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1139 dstRowStride
, dstImageStride
,
1140 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1141 srcAddr
, srcPacking
);
1145 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
1147 dstFormat
->BaseFormat
,
1148 srcWidth
, srcHeight
, srcDepth
,
1149 srcFormat
, srcType
, srcAddr
,
1151 const GLchan
*src
= tempImage
;
1152 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1153 + dstZoffset
* dstImageStride
1154 + dstYoffset
* dstRowStride
1155 + dstXoffset
* dstFormat
->TexelBytes
;
1156 GLint img
, row
, col
;
1159 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1160 for (img
= 0; img
< srcDepth
; img
++) {
1161 GLubyte
*dstRow
= dstImage
;
1162 for (row
= 0; row
< srcHeight
; row
++) {
1163 GLushort
*dstUS
= (GLushort
*) dstRow
;
1164 for (col
= 0; col
< srcWidth
; col
++) {
1165 dstUS
[col
] = PACK_COLOR_4444( CHAN_TO_UBYTE(src
[ACOMP
]),
1166 CHAN_TO_UBYTE(src
[RCOMP
]),
1167 CHAN_TO_UBYTE(src
[GCOMP
]),
1168 CHAN_TO_UBYTE(src
[BCOMP
]) );
1171 dstRow
+= dstRowStride
;
1173 dstImage
+= dstImageStride
;
1175 _mesa_free((void *) tempImage
);
1182 _mesa_texstore_argb1555(STORE_PARAMS
)
1184 const GLuint ui
= 1;
1185 const GLubyte littleEndian
= *((const GLubyte
*) &ui
);
1187 ASSERT(dstFormat
== &_mesa_texformat_argb1555
);
1188 ASSERT(dstFormat
->TexelBytes
== 2);
1190 if (!ctx
->_ImageTransferState
&&
1191 !srcPacking
->SwapBytes
&&
1192 baseInternalFormat
== GL_RGBA
&&
1193 srcFormat
== GL_BGRA
&&
1194 ((srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
&& littleEndian
) ||
1195 (srcType
== GL_UNSIGNED_SHORT_5_5_5_1
&& !littleEndian
))) {
1196 /* simple memcpy path */
1197 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1198 dstRowStride
, dstImageStride
,
1199 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1200 srcAddr
, srcPacking
);
1204 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
1206 dstFormat
->BaseFormat
,
1207 srcWidth
, srcHeight
, srcDepth
,
1208 srcFormat
, srcType
, srcAddr
,
1210 const GLchan
*src
=tempImage
;
1211 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1212 + dstZoffset
* dstImageStride
1213 + dstYoffset
* dstRowStride
1214 + dstXoffset
* dstFormat
->TexelBytes
;
1215 GLint img
, row
, col
;
1218 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1219 for (img
= 0; img
< srcDepth
; img
++) {
1220 GLubyte
*dstRow
= dstImage
;
1221 for (row
= 0; row
< srcHeight
; row
++) {
1222 GLushort
*dstUS
= (GLushort
*) dstRow
;
1223 for (col
= 0; col
< srcWidth
; col
++) {
1224 dstUS
[col
] = PACK_COLOR_1555( CHAN_TO_UBYTE(src
[ACOMP
]),
1225 CHAN_TO_UBYTE(src
[RCOMP
]),
1226 CHAN_TO_UBYTE(src
[GCOMP
]),
1227 CHAN_TO_UBYTE(src
[BCOMP
]) );
1230 dstRow
+= dstRowStride
;
1232 dstImage
+= dstImageStride
;
1234 _mesa_free((void *) tempImage
);
1241 _mesa_texstore_al88(STORE_PARAMS
)
1243 const GLuint ui
= 1;
1244 const GLubyte littleEndian
= *((const GLubyte
*) &ui
);
1246 ASSERT(dstFormat
== &_mesa_texformat_al88
);
1247 ASSERT(dstFormat
->TexelBytes
== 2);
1249 if (!ctx
->_ImageTransferState
&&
1250 !srcPacking
->SwapBytes
&&
1251 baseInternalFormat
== GL_LUMINANCE_ALPHA
&&
1252 srcFormat
== GL_LUMINANCE_ALPHA
&&
1253 srcType
== GL_UNSIGNED_BYTE
&&
1255 /* simple memcpy path */
1256 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1257 dstRowStride
, dstImageStride
,
1258 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1259 srcAddr
, srcPacking
);
1263 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
1265 dstFormat
->BaseFormat
,
1266 srcWidth
, srcHeight
, srcDepth
,
1267 srcFormat
, srcType
, srcAddr
,
1269 const GLchan
*src
= tempImage
;
1270 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1271 + dstZoffset
* dstImageStride
1272 + dstYoffset
* dstRowStride
1273 + dstXoffset
* dstFormat
->TexelBytes
;
1274 GLint img
, row
, col
;
1277 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1278 for (img
= 0; img
< srcDepth
; img
++) {
1279 GLubyte
*dstRow
= dstImage
;
1280 for (row
= 0; row
< srcHeight
; row
++) {
1281 GLushort
*dstUS
= (GLushort
*) dstRow
;
1282 for (col
= 0; col
< srcWidth
; col
++) {
1283 dstUS
[col
] = PACK_COLOR_88( CHAN_TO_UBYTE(src
[ACOMP
]),
1284 CHAN_TO_UBYTE(src
[RCOMP
]) );
1287 dstRow
+= dstRowStride
;
1289 dstImage
+= dstImageStride
;
1291 _mesa_free((void *) tempImage
);
1298 _mesa_texstore_rgb332(STORE_PARAMS
)
1300 ASSERT(dstFormat
== &_mesa_texformat_rgb332
);
1301 ASSERT(dstFormat
->TexelBytes
== 1);
1303 if (!ctx
->_ImageTransferState
&&
1304 !srcPacking
->SwapBytes
&&
1305 baseInternalFormat
== GL_RGB
&&
1306 srcFormat
== GL_RGB
&& srcType
== GL_UNSIGNED_BYTE_3_3_2
) {
1307 /* simple memcpy path */
1308 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1309 dstRowStride
, dstImageStride
,
1310 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1311 srcAddr
, srcPacking
);
1315 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
1317 dstFormat
->BaseFormat
,
1318 srcWidth
, srcHeight
, srcDepth
,
1319 srcFormat
, srcType
, srcAddr
,
1321 const GLchan
*src
= tempImage
;
1322 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1323 + dstZoffset
* dstImageStride
1324 + dstYoffset
* dstRowStride
1325 + dstXoffset
* dstFormat
->TexelBytes
;
1326 GLint img
, row
, col
;
1329 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1330 for (img
= 0; img
< srcDepth
; img
++) {
1331 GLubyte
*dstRow
= dstImage
;
1332 for (row
= 0; row
< srcHeight
; row
++) {
1333 for (col
= 0; col
< srcWidth
; col
++) {
1334 dstRow
[col
] = PACK_COLOR_332( CHAN_TO_UBYTE(src
[RCOMP
]),
1335 CHAN_TO_UBYTE(src
[GCOMP
]),
1336 CHAN_TO_UBYTE(src
[BCOMP
]) );
1339 dstRow
+= dstRowStride
;
1341 dstImage
+= dstImageStride
;
1343 _mesa_free((void *) tempImage
);
1351 * Texstore for _mesa_texformat_a8, _mesa_texformat_l8, _mesa_texformat_i8.
1354 _mesa_texstore_a8(STORE_PARAMS
)
1356 ASSERT(dstFormat
== &_mesa_texformat_a8
||
1357 dstFormat
== &_mesa_texformat_l8
||
1358 dstFormat
== &_mesa_texformat_i8
);
1359 ASSERT(dstFormat
->TexelBytes
== 1);
1361 if (!ctx
->_ImageTransferState
&&
1362 !srcPacking
->SwapBytes
&&
1363 baseInternalFormat
== srcFormat
&&
1364 srcType
== GL_UNSIGNED_BYTE
) {
1365 /* simple memcpy path */
1366 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1367 dstRowStride
, dstImageStride
,
1368 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1369 srcAddr
, srcPacking
);
1373 const GLchan
*tempImage
= make_temp_chan_image(ctx
, dims
,
1375 dstFormat
->BaseFormat
,
1376 srcWidth
, srcHeight
, srcDepth
,
1377 srcFormat
, srcType
, srcAddr
,
1379 const GLchan
*src
= tempImage
;
1380 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1381 + dstZoffset
* dstImageStride
1382 + dstYoffset
* dstRowStride
1383 + dstXoffset
* dstFormat
->TexelBytes
;
1384 GLint img
, row
, col
;
1387 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1388 for (img
= 0; img
< srcDepth
; img
++) {
1389 GLubyte
*dstRow
= dstImage
;
1390 for (row
= 0; row
< srcHeight
; row
++) {
1391 for (col
= 0; col
< srcWidth
; col
++) {
1392 dstRow
[col
] = CHAN_TO_UBYTE(src
[col
]);
1394 dstRow
+= dstRowStride
;
1397 dstImage
+= dstImageStride
;
1399 _mesa_free((void *) tempImage
);
1407 _mesa_texstore_ci8(STORE_PARAMS
)
1409 ASSERT(dstFormat
== &_mesa_texformat_ci8
);
1410 ASSERT(dstFormat
->TexelBytes
== 1);
1411 ASSERT(baseInternalFormat
== GL_COLOR_INDEX
);
1413 if (!ctx
->_ImageTransferState
&&
1414 !srcPacking
->SwapBytes
&&
1415 srcFormat
== GL_COLOR_INDEX
&&
1416 srcType
== GL_UNSIGNED_BYTE
) {
1417 /* simple memcpy path */
1418 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1419 dstRowStride
, dstImageStride
,
1420 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1421 srcAddr
, srcPacking
);
1425 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1426 + dstZoffset
* dstImageStride
1427 + dstYoffset
* dstRowStride
1428 + dstXoffset
* dstFormat
->TexelBytes
;
1430 for (img
= 0; img
< srcDepth
; img
++) {
1431 GLubyte
*dstRow
= dstImage
;
1432 for (row
= 0; row
< srcHeight
; row
++) {
1433 const GLvoid
*src
= _mesa_image_address(srcPacking
,
1434 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
1435 _mesa_unpack_index_span(ctx
, srcWidth
, GL_UNSIGNED_BYTE
, dstRow
,
1436 srcType
, src
, srcPacking
,
1437 ctx
->_ImageTransferState
);
1438 dstRow
+= dstRowStride
;
1440 dstImage
+= dstImageStride
;
1448 * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_rev.
1451 _mesa_texstore_ycbcr(STORE_PARAMS
)
1453 const GLuint ui
= 1;
1454 const GLubyte littleEndian
= *((const GLubyte
*) &ui
);
1456 ASSERT((dstFormat
== &_mesa_texformat_ycbcr
) ||
1457 (dstFormat
== &_mesa_texformat_ycbcr_rev
));
1458 ASSERT(dstFormat
->TexelBytes
== 2);
1459 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1460 ASSERT(srcFormat
== GL_YCBCR_MESA
);
1461 ASSERT((srcType
== GL_UNSIGNED_SHORT_8_8_MESA
) ||
1462 (srcType
== GL_UNSIGNED_SHORT_8_8_REV_MESA
));
1463 ASSERT(baseInternalFormat
== GL_YCBCR_MESA
);
1465 /* always just memcpy since no pixel transfer ops apply */
1466 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1467 dstRowStride
, dstImageStride
,
1468 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1469 srcAddr
, srcPacking
);
1471 /* Check if we need byte swapping */
1472 /* XXX the logic here _might_ be wrong */
1473 if (srcPacking
->SwapBytes
^
1474 (srcType
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ^
1475 (dstFormat
== &_mesa_texformat_ycbcr_rev
) ^
1477 GLushort
*pImage
= (GLushort
*) ((GLubyte
*) dstAddr
1478 + dstZoffset
* dstImageStride
1479 + dstYoffset
* dstRowStride
1480 + dstXoffset
* dstFormat
->TexelBytes
);
1482 for (img
= 0; img
< srcDepth
; img
++) {
1483 GLushort
*pRow
= pImage
;
1484 for (row
= 0; row
< srcHeight
; row
++) {
1485 _mesa_swap2(pRow
, srcWidth
);
1486 pRow
+= dstRowStride
;
1488 pImage
+= dstImageStride
;
1498 * Store an image in any of the formats:
1499 * _mesa_texformat_rgba_float32
1500 * _mesa_texformat_rgb_float32
1501 * _mesa_texformat_alpha_float32
1502 * _mesa_texformat_luminance_float32
1503 * _mesa_texformat_luminance_alpha_float32
1504 * _mesa_texformat_intensity_float32
1507 _mesa_texstore_rgba_float32(STORE_PARAMS
)
1509 const GLint components
= _mesa_components_in_format(baseInternalFormat
);
1511 ASSERT(dstFormat
== &_mesa_texformat_rgba_float32
||
1512 dstFormat
== &_mesa_texformat_rgb_float32
||
1513 dstFormat
== &_mesa_texformat_alpha_float32
||
1514 dstFormat
== &_mesa_texformat_luminance_float32
||
1515 dstFormat
== &_mesa_texformat_luminance_alpha_float32
||
1516 dstFormat
== &_mesa_texformat_intensity_float32
);
1517 ASSERT(baseInternalFormat
== GL_RGBA
||
1518 baseInternalFormat
== GL_RGB
||
1519 baseInternalFormat
== GL_ALPHA
||
1520 baseInternalFormat
== GL_LUMINANCE
||
1521 baseInternalFormat
== GL_LUMINANCE_ALPHA
||
1522 baseInternalFormat
== GL_INTENSITY
);
1523 ASSERT(dstFormat
->TexelBytes
== components
* sizeof(GLfloat
));
1525 if (!ctx
->_ImageTransferState
&&
1526 !srcPacking
->SwapBytes
&&
1527 baseInternalFormat
== srcFormat
&&
1528 srcType
== GL_FLOAT
) {
1529 /* simple memcpy path */
1530 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1531 dstRowStride
, dstImageStride
,
1532 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1533 srcAddr
, srcPacking
);
1537 const GLfloat
*tempImage
= make_temp_float_image(ctx
, dims
,
1539 dstFormat
->BaseFormat
,
1540 srcWidth
, srcHeight
, srcDepth
,
1541 srcFormat
, srcType
, srcAddr
,
1543 const GLfloat
*src
= tempImage
;
1544 const GLint bytesPerRow
= srcWidth
* components
* sizeof(GLfloat
);
1545 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1546 + dstZoffset
* dstImageStride
1547 + dstYoffset
* dstRowStride
1548 + dstXoffset
* dstFormat
->TexelBytes
;
1552 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1553 for (img
= 0; img
< srcDepth
; img
++) {
1554 GLubyte
*dst
= dstImage
;
1555 for (row
= 0; row
< srcHeight
; row
++) {
1556 _mesa_memcpy(dst
, src
, bytesPerRow
);
1557 dst
+= dstRowStride
;
1558 src
+= srcWidth
* components
;
1560 dstImage
+= dstImageStride
;
1563 _mesa_free((void *) tempImage
);
1570 * As above, but store 16-bit floats.
1573 _mesa_texstore_rgba_float16(STORE_PARAMS
)
1575 const GLint components
= _mesa_components_in_format(baseInternalFormat
);
1577 ASSERT(dstFormat
== &_mesa_texformat_rgba_float16
||
1578 dstFormat
== &_mesa_texformat_rgb_float16
||
1579 dstFormat
== &_mesa_texformat_alpha_float16
||
1580 dstFormat
== &_mesa_texformat_luminance_float16
||
1581 dstFormat
== &_mesa_texformat_luminance_alpha_float16
||
1582 dstFormat
== &_mesa_texformat_intensity_float16
);
1583 ASSERT(baseInternalFormat
== GL_RGBA
||
1584 baseInternalFormat
== GL_RGB
||
1585 baseInternalFormat
== GL_ALPHA
||
1586 baseInternalFormat
== GL_LUMINANCE
||
1587 baseInternalFormat
== GL_LUMINANCE_ALPHA
||
1588 baseInternalFormat
== GL_INTENSITY
);
1589 ASSERT(dstFormat
->TexelBytes
== components
* sizeof(GLhalfARB
));
1591 if (!ctx
->_ImageTransferState
&&
1592 !srcPacking
->SwapBytes
&&
1593 baseInternalFormat
== srcFormat
&&
1594 srcType
== GL_HALF_FLOAT_ARB
) {
1595 /* simple memcpy path */
1596 memcpy_texture(dstFormat
, dstAddr
, dstXoffset
, dstYoffset
, dstZoffset
,
1597 dstRowStride
, dstImageStride
,
1598 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1599 srcAddr
, srcPacking
);
1603 const GLfloat
*tempImage
= make_temp_float_image(ctx
, dims
,
1605 dstFormat
->BaseFormat
,
1606 srcWidth
, srcHeight
, srcDepth
,
1607 srcFormat
, srcType
, srcAddr
,
1609 const GLfloat
*src
= tempImage
;
1610 GLubyte
*dstImage
= (GLubyte
*) dstAddr
1611 + dstZoffset
* dstImageStride
1612 + dstYoffset
* dstRowStride
1613 + dstXoffset
* dstFormat
->TexelBytes
;
1617 _mesa_adjust_image_for_convolution(ctx
, dims
, &srcWidth
, &srcHeight
);
1618 for (img
= 0; img
< srcDepth
; img
++) {
1619 GLubyte
*dstRow
= dstImage
;
1620 for (row
= 0; row
< srcHeight
; row
++) {
1621 GLhalfARB
*dstTexel
= (GLhalfARB
*) dstRow
;
1623 for (i
= 0; i
< srcWidth
* components
; i
++) {
1624 dstTexel
[i
] = _mesa_float_to_half(src
[i
]);
1626 dstRow
+= dstRowStride
;
1627 src
+= srcWidth
* components
;
1629 dstImage
+= dstImageStride
;
1632 _mesa_free((void *) tempImage
);
1638 #if !NEWTEXSTORE /*****************************************************/
1642 * Given an internal texture format enum or 1, 2, 3, 4 return the
1643 * corresponding _base_ internal format: GL_ALPHA, GL_LUMINANCE,
1644 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA. Return the
1645 * number of components for the format. Return -1 if invalid enum.
1648 components_in_intformat( GLint format
)
1661 case GL_LUMINANCE12
:
1662 case GL_LUMINANCE16
:
1665 case GL_LUMINANCE_ALPHA
:
1666 case GL_LUMINANCE4_ALPHA4
:
1667 case GL_LUMINANCE6_ALPHA2
:
1668 case GL_LUMINANCE8_ALPHA8
:
1669 case GL_LUMINANCE12_ALPHA4
:
1670 case GL_LUMINANCE12_ALPHA12
:
1671 case GL_LUMINANCE16_ALPHA16
:
1676 case GL_INTENSITY12
:
1677 case GL_INTENSITY16
:
1699 case GL_COLOR_INDEX
:
1700 case GL_COLOR_INDEX1_EXT
:
1701 case GL_COLOR_INDEX2_EXT
:
1702 case GL_COLOR_INDEX4_EXT
:
1703 case GL_COLOR_INDEX8_EXT
:
1704 case GL_COLOR_INDEX12_EXT
:
1705 case GL_COLOR_INDEX16_EXT
:
1707 case GL_DEPTH_COMPONENT
:
1708 case GL_DEPTH_COMPONENT16_SGIX
:
1709 case GL_DEPTH_COMPONENT24_SGIX
:
1710 case GL_DEPTH_COMPONENT32_SGIX
:
1713 return 2; /* Y + (Cb or Cr) */
1715 return -1; /* error */
1721 * This function is used to transfer the user's image data into a texture
1722 * image buffer. We handle both full texture images and subtexture images.
1723 * We also take care of all image transfer operations here, including
1724 * convolution, scale/bias, colortables, etc.
1726 * The destination texel type is always GLchan.
1727 * The destination texel format is one of the 6 basic types.
1729 * A hardware driver may use this as a helper routine to unpack and
1730 * apply pixel transfer ops into a temporary image buffer. Then,
1731 * convert the temporary image into the special hardware format.
1734 * dimensions - 1, 2, or 3
1735 * texDestFormat - GL_LUMINANCE, GL_INTENSITY, GL_LUMINANCE_ALPHA, GL_ALPHA,
1736 * GL_RGB or GL_RGBA (the destination format)
1737 * texDestAddr - destination image address
1738 * srcWidth, srcHeight, srcDepth - size (in pixels) of src and dest images
1739 * dstXoffset, dstYoffset, dstZoffset - position to store the image within
1740 * the destination 3D texture
1741 * dstRowStride, dstImageStride - dest image strides in bytes
1742 * srcFormat - source image format (GL_ALPHA, GL_RED, GL_RGB, etc)
1743 * srcType - GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5, GL_FLOAT, etc
1744 * srcPacking - describes packing of incoming image.
1745 * transferOps - mask of pixel transfer operations
1748 transfer_teximage(GLcontext
*ctx
, GLuint dimensions
,
1749 GLenum texDestFormat
, GLvoid
*texDestAddr
,
1750 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
1751 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
1752 GLint dstRowStride
, GLint dstImageStride
,
1753 GLenum srcFormat
, GLenum srcType
,
1754 const GLvoid
*srcAddr
,
1755 const struct gl_pixelstore_attrib
*srcPacking
,
1758 GLint texComponents
;
1761 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1762 ASSERT(texDestFormat
== GL_LUMINANCE
||
1763 texDestFormat
== GL_INTENSITY
||
1764 texDestFormat
== GL_LUMINANCE_ALPHA
||
1765 texDestFormat
== GL_ALPHA
||
1766 texDestFormat
== GL_RGB
||
1767 texDestFormat
== GL_RGBA
||
1768 texDestFormat
== GL_COLOR_INDEX
||
1769 texDestFormat
== GL_DEPTH_COMPONENT
);
1770 ASSERT(texDestAddr
);
1771 ASSERT(srcWidth
>= 0);
1772 ASSERT(srcHeight
>= 0);
1773 ASSERT(srcDepth
>= 0);
1774 ASSERT(dstXoffset
>= 0);
1775 ASSERT(dstYoffset
>= 0);
1776 ASSERT(dstZoffset
>= 0);
1777 ASSERT(dstRowStride
>= 0);
1778 ASSERT(dstImageStride
>= 0);
1782 texComponents
= components_in_intformat(texDestFormat
);
1784 /* try common 2D texture cases first */
1785 if (!transferOps
&& dimensions
== 2 && srcType
== CHAN_TYPE
) {
1787 if (srcFormat
== texDestFormat
) {
1788 /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA,
1789 * GL_LUMINANCE_ALPHA, etc. texture formats. Use memcpy().
1791 const GLchan
*src
= (const GLchan
*) _mesa_image_address(
1792 srcPacking
, srcAddr
, srcWidth
, srcHeight
,
1793 srcFormat
, srcType
, 0, 0, 0);
1794 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
1795 srcWidth
, srcFormat
, srcType
);
1796 const GLint widthInBytes
= srcWidth
* texComponents
* sizeof(GLchan
);
1797 GLchan
*dst
= (GLchan
*) texDestAddr
1798 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
1799 + dstXoffset
* texComponents
;
1800 if (srcRowStride
== widthInBytes
&& dstRowStride
== widthInBytes
) {
1801 MEMCPY(dst
, src
, srcHeight
* widthInBytes
);
1805 for (i
= 0; i
< srcHeight
; i
++) {
1806 MEMCPY(dst
, src
, widthInBytes
);
1807 src
+= (srcRowStride
/ sizeof(GLchan
));
1808 dst
+= (dstRowStride
/ sizeof(GLchan
));
1811 return; /* all done */
1813 else if (srcFormat
== GL_RGBA
&& texDestFormat
== GL_RGB
) {
1814 /* commonly used by Quake */
1815 const GLchan
*src
= (const GLchan
*) _mesa_image_address(
1816 srcPacking
, srcAddr
, srcWidth
, srcHeight
,
1817 srcFormat
, srcType
, 0, 0, 0);
1818 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
,
1819 srcWidth
, srcFormat
, srcType
);
1820 GLchan
*dst
= (GLchan
*) texDestAddr
1821 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
1822 + dstXoffset
* texComponents
;
1824 for (i
= 0; i
< srcHeight
; i
++) {
1825 const GLchan
*s
= src
;
1827 for (j
= 0; j
< srcWidth
; j
++) {
1828 *d
++ = *s
++; /*red*/
1829 *d
++ = *s
++; /*green*/
1830 *d
++ = *s
++; /*blue*/
1833 src
+= (srcRowStride
/ sizeof(GLchan
));
1834 dst
+= (dstRowStride
/ sizeof(GLchan
));
1836 return; /* all done */
1841 * General case solutions
1843 if (texDestFormat
== GL_COLOR_INDEX
) {
1844 /* color index texture */
1845 const GLenum texType
= CHAN_TYPE
;
1847 GLchan
*dest
= (GLchan
*) texDestAddr
1848 + dstZoffset
* (dstImageStride
/ sizeof(GLchan
))
1849 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
1850 + dstXoffset
* texComponents
;
1851 for (img
= 0; img
< srcDepth
; img
++) {
1852 GLchan
*destRow
= dest
;
1853 for (row
= 0; row
< srcHeight
; row
++) {
1854 const GLvoid
*src
= _mesa_image_address(srcPacking
,
1855 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
1856 _mesa_unpack_index_span(ctx
, srcWidth
, texType
, destRow
,
1857 srcType
, src
, srcPacking
, transferOps
);
1858 destRow
+= (dstRowStride
/ sizeof(GLchan
));
1860 dest
+= dstImageStride
;
1863 else if (texDestFormat
== GL_YCBCR_MESA
) {
1866 GLushort
*dest
= (GLushort
*) texDestAddr
1867 + dstZoffset
* (dstImageStride
/ sizeof(GLushort
))
1868 + dstYoffset
* (dstRowStride
/ sizeof(GLushort
))
1869 + dstXoffset
* texComponents
;
1870 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1871 for (img
= 0; img
< srcDepth
; img
++) {
1872 GLushort
*destRow
= dest
;
1873 for (row
= 0; row
< srcHeight
; row
++) {
1874 const GLvoid
*srcRow
= _mesa_image_address(srcPacking
,
1875 srcAddr
, srcWidth
, srcHeight
,
1876 srcFormat
, srcType
, img
, row
, 0);
1877 MEMCPY(destRow
, srcRow
, srcWidth
* sizeof(GLushort
));
1878 destRow
+= (dstRowStride
/ sizeof(GLushort
));
1880 dest
+= dstImageStride
/ sizeof(GLushort
);
1883 else if (texDestFormat
== GL_DEPTH_COMPONENT
) {
1884 /* Depth texture (shadow maps) */
1886 GLubyte
*dest
= (GLubyte
*) texDestAddr
1887 + dstZoffset
* dstImageStride
1888 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
1889 + dstXoffset
* texComponents
;
1890 for (img
= 0; img
< srcDepth
; img
++) {
1891 GLubyte
*destRow
= dest
;
1892 for (row
= 0; row
< srcHeight
; row
++) {
1893 const GLvoid
*src
= _mesa_image_address(srcPacking
,
1894 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
1895 _mesa_unpack_depth_span(ctx
, srcWidth
, (GLfloat
*) destRow
,
1896 srcType
, src
, srcPacking
);
1897 destRow
+= (dstRowStride
/ sizeof(GLchan
));
1899 dest
+= dstImageStride
;
1903 /* regular, color texture */
1904 if ((dimensions
== 1 && ctx
->Pixel
.Convolution1DEnabled
) ||
1905 (dimensions
>= 2 && ctx
->Pixel
.Convolution2DEnabled
) ||
1906 (dimensions
>= 2 && ctx
->Pixel
.Separable2DEnabled
)) {
1908 * Fill texture image with convolution
1911 GLint convWidth
= srcWidth
, convHeight
= srcHeight
;
1912 GLfloat
*tmpImage
, *convImage
;
1913 tmpImage
= (GLfloat
*) MALLOC(srcWidth
* srcHeight
* 4 * sizeof(GLfloat
));
1915 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1918 convImage
= (GLfloat
*) MALLOC(srcWidth
* srcHeight
* 4 * sizeof(GLfloat
));
1920 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1925 for (img
= 0; img
< srcDepth
; img
++) {
1926 const GLfloat
*srcf
;
1927 GLfloat
*dstf
= tmpImage
;
1930 /* unpack and do transfer ops up to convolution */
1931 for (row
= 0; row
< srcHeight
; row
++) {
1932 const GLvoid
*src
= _mesa_image_address(srcPacking
,
1933 srcAddr
, srcWidth
, srcHeight
,
1934 srcFormat
, srcType
, img
, row
, 0);
1935 _mesa_unpack_color_span_float(ctx
, srcWidth
, GL_RGBA
, dstf
,
1936 srcFormat
, srcType
, src
, srcPacking
,
1937 (transferOps
& IMAGE_PRE_CONVOLUTION_BITS
) | IMAGE_CLAMP_BIT
);
1938 dstf
+= srcWidth
* 4;
1942 if (dimensions
== 1) {
1943 ASSERT(ctx
->Pixel
.Convolution1DEnabled
);
1944 _mesa_convolve_1d_image(ctx
, &convWidth
, tmpImage
, convImage
);
1947 if (ctx
->Pixel
.Convolution2DEnabled
) {
1948 _mesa_convolve_2d_image(ctx
, &convWidth
, &convHeight
,
1949 tmpImage
, convImage
);
1952 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
1953 _mesa_convolve_sep_image(ctx
, &convWidth
, &convHeight
,
1954 tmpImage
, convImage
);
1958 /* packing and transfer ops after convolution */
1960 dest
= (GLchan
*) texDestAddr
1961 + (dstZoffset
+ img
) * (dstImageStride
/ sizeof(GLchan
))
1962 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
));
1963 for (row
= 0; row
< convHeight
; row
++) {
1964 _mesa_pack_rgba_span_float(ctx
, convWidth
,
1965 (const GLfloat (*)[4]) srcf
,
1966 texDestFormat
, CHAN_TYPE
,
1967 dest
, &ctx
->DefaultPacking
,
1969 & IMAGE_POST_CONVOLUTION_BITS
);
1970 srcf
+= convWidth
* 4;
1971 dest
+= (dstRowStride
/ sizeof(GLchan
));
1983 GLchan
*dest
= (GLchan
*) texDestAddr
1984 + dstZoffset
* (dstImageStride
/ sizeof(GLchan
))
1985 + dstYoffset
* (dstRowStride
/ sizeof(GLchan
))
1986 + dstXoffset
* texComponents
;
1987 for (img
= 0; img
< srcDepth
; img
++) {
1988 GLchan
*destRow
= dest
;
1989 for (row
= 0; row
< srcHeight
; row
++) {
1990 const GLvoid
*srcRow
= _mesa_image_address(srcPacking
,
1991 srcAddr
, srcWidth
, srcHeight
,
1992 srcFormat
, srcType
, img
, row
, 0);
1993 _mesa_unpack_color_span_chan(ctx
, srcWidth
, texDestFormat
,
1994 destRow
, srcFormat
, srcType
, srcRow
,
1995 srcPacking
, transferOps
);
1996 destRow
+= (dstRowStride
/ sizeof(GLchan
));
1998 dest
+= dstImageStride
/ sizeof(GLchan
);
2007 * Transfer a texture image from user space to <destAddr> applying all
2008 * needed image transfer operations and storing the result in the format
2009 * specified by <dstFormat>. <dstFormat> may be any format from texformat.h.
2011 * dimensions - 1, 2 or 3
2012 * baseInternalFormat - base format of the internal texture format
2013 * specified by the user. This is very important, see below.
2014 * dstFormat - destination image format
2015 * dstAddr - destination address
2016 * srcWidth, srcHeight, srcDepth - size of source iamge
2017 * dstX/Y/Zoffset - as specified by glTexSubImage
2018 * dstRowStride - stride between dest rows in bytes
2019 * dstImageStride - stride between dest images in bytes
2020 * srcFormat, srcType - incoming image format and data type
2021 * srcAddr - source image address
2022 * srcPacking - packing params of source image
2024 * XXX this function is a bit more complicated than it should be. If
2025 * _mesa_convert_texsubimage[123]d could handle any dest/source formats
2026 * or if transfer_teximage() could store in any MESA_FORMAT_* format, we
2027 * could simplify things here.
2030 _mesa_transfer_teximage(GLcontext
*ctx
, GLuint dimensions
,
2031 GLenum baseInternalFormat
,
2032 const struct gl_texture_format
*dstFormat
,
2034 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
2035 GLint dstXoffset
, GLint dstYoffset
, GLint dstZoffset
,
2036 GLint dstRowStride
, GLint dstImageStride
,
2037 GLenum srcFormat
, GLenum srcType
,
2038 const GLvoid
*srcAddr
,
2039 const struct gl_pixelstore_attrib
*srcPacking
)
2041 const GLint dstRowStridePixels
= dstRowStride
/ dstFormat
->TexelBytes
;
2042 const GLint dstImageStridePixels
= dstImageStride
/ dstFormat
->TexelBytes
;
2044 GLuint transferOps
= ctx
->_ImageTransferState
;
2045 GLboolean freeSourceData
= GL_FALSE
;
2046 GLint postConvWidth
= srcWidth
, postConvHeight
= srcHeight
;
2048 assert(baseInternalFormat
> 0);
2049 ASSERT(baseInternalFormat
== GL_LUMINANCE
||
2050 baseInternalFormat
== GL_INTENSITY
||
2051 baseInternalFormat
== GL_LUMINANCE_ALPHA
||
2052 baseInternalFormat
== GL_ALPHA
||
2053 baseInternalFormat
== GL_RGB
||
2054 baseInternalFormat
== GL_RGBA
||
2055 baseInternalFormat
== GL_YCBCR_MESA
||
2056 baseInternalFormat
== GL_COLOR_INDEX
||
2057 baseInternalFormat
== GL_DEPTH_COMPONENT
);
2059 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
2060 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
2065 * Consider this scenario: The user's source image is GL_RGB and the
2066 * requested internal format is GL_LUMINANCE. Now suppose the device
2067 * driver doesn't support GL_LUMINANCE and instead uses RGB16 as the
2068 * texture format. In that case we still need to do an intermediate
2069 * conversion to luminance format so that the incoming red channel gets
2070 * replicated into the dest red, green and blue channels. The following
2071 * code takes care of that.
2073 if (dstFormat
->BaseFormat
!= baseInternalFormat
) {
2074 /* Allocate storage for temporary image in the baseInternalFormat */
2075 const GLint texelSize
= _mesa_components_in_format(baseInternalFormat
)
2077 const GLint bytes
= texelSize
* postConvWidth
* postConvHeight
*srcDepth
;
2078 const GLint tmpRowStride
= texelSize
* postConvWidth
;
2079 const GLint tmpImgStride
= texelSize
* postConvWidth
* postConvHeight
;
2080 GLvoid
*tmpImage
= MALLOC(bytes
);
2083 transfer_teximage(ctx
, dimensions
, baseInternalFormat
, tmpImage
,
2084 srcWidth
, srcHeight
, srcDepth
,
2085 0, 0, 0, /* x/y/zoffset */
2086 tmpRowStride
, tmpImgStride
,
2087 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
2089 /* this is our new source image */
2090 srcWidth
= postConvWidth
;
2091 srcHeight
= postConvHeight
;
2092 srcFormat
= baseInternalFormat
;
2093 srcType
= CHAN_TYPE
;
2095 srcPacking
= &ctx
->DefaultPacking
;
2096 freeSourceData
= GL_TRUE
;
2097 transferOps
= 0; /* image transfer ops were completed */
2100 /* Let the optimized tex conversion functions take a crack at the
2101 * image conversion if the dest format is a h/w format.
2103 if (_mesa_is_hardware_tex_format(dstFormat
)) {
2108 if (dimensions
== 1) {
2109 makeTemp
= !_mesa_convert_texsubimage1d(dstFormat
->MesaFormat
,
2113 srcPacking
, srcAddr
,
2116 else if (dimensions
== 2) {
2117 makeTemp
= !_mesa_convert_texsubimage2d(dstFormat
->MesaFormat
,
2118 dstXoffset
, dstYoffset
,
2119 srcWidth
, srcHeight
,
2122 srcPacking
, srcAddr
,
2126 assert(dimensions
== 3);
2127 makeTemp
= !_mesa_convert_texsubimage3d(dstFormat
->MesaFormat
,
2128 dstXoffset
, dstYoffset
, dstZoffset
,
2129 srcWidth
, srcHeight
, srcDepth
,
2130 dstRowStridePixels
, dstImageStridePixels
,
2132 srcPacking
, srcAddr
, dstAddr
);
2137 FREE((void *) srcAddr
);
2143 /* software texture format */
2144 makeTemp
= GL_FALSE
;
2148 GLint postConvWidth
= srcWidth
, postConvHeight
= srcHeight
;
2150 GLuint tmpComps
, tmpTexelSize
;
2151 GLint tmpRowStride
, tmpImageStride
;
2154 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
2155 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
2159 tmpFormat
= dstFormat
->BaseFormat
;
2160 tmpComps
= _mesa_components_in_format(tmpFormat
);
2161 tmpTexelSize
= tmpComps
* sizeof(GLchan
);
2162 tmpRowStride
= postConvWidth
* tmpTexelSize
;
2163 tmpImageStride
= postConvWidth
* postConvHeight
* tmpTexelSize
;
2164 tmpImage
= (GLubyte
*) MALLOC(postConvWidth
* postConvHeight
*
2165 srcDepth
* tmpTexelSize
);
2168 FREE((void *) srcAddr
);
2172 transfer_teximage(ctx
, dimensions
, tmpFormat
, tmpImage
,
2173 srcWidth
, srcHeight
, srcDepth
,
2174 0, 0, 0, /* x/y/zoffset */
2175 tmpRowStride
, tmpImageStride
,
2176 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
2179 FREE((void *) srcAddr
);
2181 /* the temp image is our new source image */
2182 srcWidth
= postConvWidth
;
2183 srcHeight
= postConvHeight
;
2184 srcFormat
= tmpFormat
;
2185 srcType
= CHAN_TYPE
;
2187 srcPacking
= &ctx
->DefaultPacking
;
2188 freeSourceData
= GL_TRUE
;
2191 if (_mesa_is_hardware_tex_format(dstFormat
)) {
2193 if (dimensions
== 1) {
2195 b
= _mesa_convert_texsubimage1d(dstFormat
->MesaFormat
,
2199 srcPacking
, srcAddr
,
2204 else if (dimensions
== 2) {
2206 b
= _mesa_convert_texsubimage2d(dstFormat
->MesaFormat
,
2207 dstXoffset
, dstYoffset
,
2208 srcWidth
, srcHeight
,
2211 srcPacking
, srcAddr
,
2218 b
= _mesa_convert_texsubimage3d(dstFormat
->MesaFormat
,
2219 dstXoffset
, dstYoffset
, dstZoffset
,
2220 srcWidth
, srcHeight
, srcDepth
,
2221 dstRowStridePixels
, dstImageStridePixels
,
2223 srcPacking
, srcAddr
, dstAddr
);
2229 /* software format */
2231 transfer_teximage(ctx
, dimensions
, dstFormat
->BaseFormat
, dstAddr
,
2232 srcWidth
, srcHeight
, srcDepth
,
2233 dstXoffset
, dstYoffset
, dstZoffset
,
2234 dstRowStride
, dstImageStride
,
2235 srcFormat
, srcType
, srcAddr
, srcPacking
, transferOps
);
2239 FREE((void *) srcAddr
); /* the temp image */
2245 * Given a user's uncompressed texture image, this function takes care of
2246 * pixel unpacking, pixel transfer, format conversion and compression.
2249 transfer_compressed_teximage(GLcontext
*ctx
, GLuint dimensions
,
2250 GLsizei width
, GLsizei height
, GLsizei depth
,
2251 GLenum srcFormat
, GLenum srcType
,
2252 const struct gl_pixelstore_attrib
*unpacking
,
2253 const GLvoid
*source
,
2254 const struct gl_texture_format
*dstFormat
,
2258 GLchan
*tempImage
= NULL
;
2262 ASSERT(dimensions
== 2);
2263 /* TexelBytes is zero if and only if it's a compressed format */
2264 ASSERT(dstFormat
->TexelBytes
== 0);
2266 baseFormat
= dstFormat
->BaseFormat
;
2268 if (srcFormat
!= baseFormat
|| srcType
!= CHAN_TYPE
||
2269 ctx
->_ImageTransferState
!= 0 || unpacking
->SwapBytes
) {
2270 /* need to convert user's image to texImage->Format, GLchan */
2271 GLint comps
= components_in_intformat(baseFormat
);
2272 GLint postConvWidth
= width
, postConvHeight
= height
;
2274 /* XXX convolution untested */
2275 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
2276 _mesa_adjust_image_for_convolution(ctx
, dimensions
, &postConvWidth
,
2280 tempImage
= (GLchan
*) MALLOC(width
* height
* comps
* sizeof(GLchan
));
2282 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2285 transfer_teximage(ctx
, dimensions
,
2286 baseFormat
, /* dest format */
2287 tempImage
, /* dst address */
2288 width
, height
, depth
, /* src size */
2289 0, 0, 0, /* x/y/zoffset */
2290 comps
* width
, /* dst row stride */
2291 comps
* width
* height
, /* dst image stride */
2292 srcFormat
, srcType
, /* src format, type */
2293 source
, unpacking
, /* src and src packing */
2294 ctx
->_ImageTransferState
);
2296 width
= postConvWidth
;
2297 height
= postConvHeight
;
2298 srcRowStride
= width
;
2301 if (unpacking
->RowLength
)
2302 srcRowStride
= unpacking
->RowLength
;
2304 srcRowStride
= width
;
2307 _mesa_compress_teximage(ctx
, width
, height
, baseFormat
,
2308 (const GLchan
*) source
, srcRowStride
,
2309 dstFormat
, dest
, dstRowStride
);
2316 #endif /************************************************************/
2322 * Validate acces to a PBO for texture data.
2324 * \todo If the PBO is really resident in VRAM, this won't work; the
2325 * device driver should check for that and do the right thing.
2327 static const GLvoid
*
2328 validate_pbo_teximage( GLsizei width
, GLsizei height
, GLsizei depth
,
2329 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2330 const struct gl_pixelstore_attrib
*unpack
)
2332 if (unpack
->BufferObj
->Name
== 0) {
2336 else if (_mesa_validate_pbo_access(unpack
, width
, height
, depth
, format
,
2338 return ADD_POINTERS(unpack
->BufferObj
->Data
, pixels
);
2346 * Validate that unpacking compressed texture image data from a PBO
2347 * won't go out of bounds.
2349 * \todo If the PBO is really resident in VRAM, this won't work; the
2350 * device driver should check for that and do the right thing.
2352 static const GLvoid
*
2353 validate_pbo_compressed_teximage(GLsizei imageSize
, const GLvoid
*pixels
,
2354 const struct gl_pixelstore_attrib
*packing
)
2356 if (packing
->BufferObj
->Name
== 0) {
2357 /* not using a PBO - return pointer unchanged */
2362 if ((const GLubyte
*) pixels
+ imageSize
>
2363 (const GLubyte
*) packing
->BufferObj
->Size
) {
2364 /* out of bounds read! */
2368 return ADD_POINTERS(packing
->BufferObj
->Data
, pixels
);
2375 * This is the software fallback for Driver.TexImage1D()
2376 * and Driver.CopyTexImage1D().
2379 _mesa_store_teximage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
2380 GLint internalFormat
,
2381 GLint width
, GLint border
,
2382 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2383 const struct gl_pixelstore_attrib
*packing
,
2384 struct gl_texture_object
*texObj
,
2385 struct gl_texture_image
*texImage
)
2387 GLint postConvWidth
= width
;
2390 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
2391 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2394 /* choose the texture format */
2395 assert(ctx
->Driver
.ChooseTextureFormat
);
2396 texImage
->TexFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
,
2398 assert(texImage
->TexFormat
);
2399 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2400 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2402 /* allocate memory */
2403 if (texImage
->IsCompressed
)
2404 sizeInBytes
= texImage
->CompressedSize
;
2406 sizeInBytes
= postConvWidth
* texImage
->TexFormat
->TexelBytes
;
2407 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
2408 if (!texImage
->Data
) {
2409 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2413 pixels
= validate_pbo_teximage(width
, 1, 1, format
, type
, pixels
, packing
);
2419 const GLint dstRowStride
= 0, dstImageStride
= 0;
2421 ASSERT(texImage
->TexFormat
->StoreImage
);
2422 success
= texImage
->TexFormat
->StoreImage(ctx
, 1, texImage
->Format
,
2423 texImage
->TexFormat
,
2425 0, 0, 0, /* dstX/Y/Zoffset */
2426 dstRowStride
, dstImageStride
,
2428 format
, type
, pixels
, packing
);
2430 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2435 /* unpack image, apply transfer ops and store in texImage->Data */
2436 if (texImage
->IsCompressed
) {
2437 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2439 transfer_compressed_teximage(ctx
, 1, width
, 1, 1,
2440 format
, type
, packing
,
2441 pixels
, texImage
->TexFormat
,
2442 (GLubyte
*) texImage
->Data
, dstRowStride
);
2445 _mesa_transfer_teximage(ctx
, 1,
2446 texImage
->Format
, /* base format */
2447 texImage
->TexFormat
, texImage
->Data
,
2448 width
, 1, 1, /* src size */
2449 0, 0, 0, /* dstX/Y/Zoffset */
2450 0, /* dstRowStride */
2451 0, /* dstImageStride */
2452 format
, type
, pixels
, packing
);
2456 /* GL_SGIS_generate_mipmap */
2457 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
2458 _mesa_generate_mipmap(ctx
, target
,
2459 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
2466 * This is the software fallback for Driver.TexImage2D()
2467 * and Driver.CopyTexImage2D().
2468 * Reasons why a driver might override this function:
2469 * - Special memory allocation needs
2470 * - Unusual row/image strides
2471 * - Special housekeeping
2474 _mesa_store_teximage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
2475 GLint internalFormat
,
2476 GLint width
, GLint height
, GLint border
,
2477 GLenum format
, GLenum type
, const void *pixels
,
2478 const struct gl_pixelstore_attrib
*packing
,
2479 struct gl_texture_object
*texObj
,
2480 struct gl_texture_image
*texImage
)
2482 GLint postConvWidth
= width
, postConvHeight
= height
;
2483 GLint texelBytes
, sizeInBytes
;
2485 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
2486 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2490 /* choose the texture format */
2491 assert(ctx
->Driver
.ChooseTextureFormat
);
2492 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2493 internalFormat
, format
, type
);
2494 assert(texImage
->TexFormat
);
2495 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2496 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2498 texelBytes
= texImage
->TexFormat
->TexelBytes
;
2500 /* allocate memory */
2501 if (texImage
->IsCompressed
)
2502 sizeInBytes
= texImage
->CompressedSize
;
2504 sizeInBytes
= postConvWidth
* postConvHeight
* texelBytes
;
2505 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
2506 if (!texImage
->Data
) {
2507 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2511 pixels
= validate_pbo_teximage(width
, height
, 1,
2512 format
, type
, pixels
, packing
);
2518 GLint dstRowStride
, dstImageStride
= 0;
2520 if (texImage
->IsCompressed
) {
2521 dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,width
);
2524 dstRowStride
= width
* texImage
->TexFormat
->TexelBytes
;
2526 ASSERT(texImage
->TexFormat
->StoreImage
);
2527 success
= texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->Format
,
2528 texImage
->TexFormat
,
2530 0, 0, 0, /* dstX/Y/Zoffset */
2531 dstRowStride
, dstImageStride
,
2533 format
, type
, pixels
, packing
);
2535 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2540 /* unpack image, apply transfer ops and store in texImage->Data */
2541 if (texImage
->IsCompressed
) {
2542 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2544 transfer_compressed_teximage(ctx
, 2, width
, height
, 1,
2545 format
, type
, packing
,
2546 pixels
, texImage
->TexFormat
,
2547 (GLubyte
*) texImage
->Data
, dstRowStride
);
2550 _mesa_transfer_teximage(ctx
, 2,
2552 texImage
->TexFormat
, texImage
->Data
,
2553 width
, height
, 1, /* src size */
2554 0, 0, 0, /* dstX/Y/Zoffset */
2555 texImage
->Width
* texelBytes
, /* dstRowStride */
2556 0, /* dstImageStride */
2557 format
, type
, pixels
, packing
);
2561 /* GL_SGIS_generate_mipmap */
2562 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
2563 _mesa_generate_mipmap(ctx
, target
,
2564 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
2572 * This is the software fallback for Driver.TexImage3D()
2573 * and Driver.CopyTexImage3D().
2576 _mesa_store_teximage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
2577 GLint internalFormat
,
2578 GLint width
, GLint height
, GLint depth
, GLint border
,
2579 GLenum format
, GLenum type
, const void *pixels
,
2580 const struct gl_pixelstore_attrib
*packing
,
2581 struct gl_texture_object
*texObj
,
2582 struct gl_texture_image
*texImage
)
2584 GLint texelBytes
, sizeInBytes
;
2586 /* choose the texture format */
2587 assert(ctx
->Driver
.ChooseTextureFormat
);
2588 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2589 internalFormat
, format
, type
);
2590 assert(texImage
->TexFormat
);
2591 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2592 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2594 texelBytes
= texImage
->TexFormat
->TexelBytes
;
2596 /* allocate memory */
2597 if (texImage
->IsCompressed
)
2598 sizeInBytes
= texImage
->CompressedSize
;
2600 sizeInBytes
= width
* height
* depth
* texelBytes
;
2601 texImage
->Data
= MESA_PBUFFER_ALLOC(sizeInBytes
);
2602 if (!texImage
->Data
) {
2603 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2607 pixels
= validate_pbo_teximage(width
, height
, depth
,
2608 format
, type
, pixels
, packing
);
2612 /* unpack image, apply transfer ops and store in texImage->Data */
2615 GLint dstRowStride
, dstImageStride
;
2617 if (texImage
->IsCompressed
) {
2618 dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,width
);
2622 dstRowStride
= width
* texImage
->TexFormat
->TexelBytes
;
2623 dstImageStride
= dstRowStride
* height
;
2625 ASSERT(texImage
->TexFormat
->StoreImage
);
2626 success
= texImage
->TexFormat
->StoreImage(ctx
, 3, texImage
->Format
,
2627 texImage
->TexFormat
,
2629 0, 0, 0, /* dstX/Y/Zoffset */
2630 dstRowStride
, dstImageStride
,
2631 width
, height
, depth
,
2632 format
, type
, pixels
, packing
);
2634 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2639 if (texImage
->IsCompressed
) {
2640 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2642 transfer_compressed_teximage(ctx
, 3, width
, height
, depth
,
2643 format
, type
, packing
,
2644 pixels
, texImage
->TexFormat
,
2645 (GLubyte
*) texImage
->Data
, dstRowStride
);
2648 _mesa_transfer_teximage(ctx
, 3,
2650 texImage
->TexFormat
, texImage
->Data
,
2651 width
, height
, depth
, /* src size */
2652 0, 0, 0, /* dstX/Y/Zoffset */
2653 texImage
->Width
* texelBytes
, /* dstRowStride */
2654 texImage
->Width
* texImage
->Height
* texelBytes
,
2655 format
, type
, pixels
, packing
);
2659 /* GL_SGIS_generate_mipmap */
2660 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
2661 _mesa_generate_mipmap(ctx
, target
,
2662 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
2671 * This is the software fallback for Driver.TexSubImage1D()
2672 * and Driver.CopyTexSubImage1D().
2675 _mesa_store_texsubimage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
2676 GLint xoffset
, GLint width
,
2677 GLenum format
, GLenum type
, const void *pixels
,
2678 const struct gl_pixelstore_attrib
*packing
,
2679 struct gl_texture_object
*texObj
,
2680 struct gl_texture_image
*texImage
)
2682 pixels
= validate_pbo_teximage(width
, 1, 1,
2683 format
, type
, pixels
, packing
);
2689 const GLint dstRowStride
= 0, dstImageStride
= 0;
2691 ASSERT(texImage
->TexFormat
->StoreImage
);
2692 success
= texImage
->TexFormat
->StoreImage(ctx
, 1, texImage
->Format
,
2693 texImage
->TexFormat
,
2695 xoffset
, 0, 0, /* offsets */
2696 dstRowStride
, dstImageStride
,
2698 format
, type
, pixels
, packing
);
2700 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage1D");
2705 if (texImage
->IsCompressed
) {
2706 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2708 GLubyte
*dest
= _mesa_compressed_image_address(xoffset
, 0, 0,
2709 texImage
->IntFormat
,
2711 (GLubyte
*) texImage
->Data
);
2712 transfer_compressed_teximage(ctx
, 1, /* dimensions */
2713 width
, 1, 1, /* size to replace */
2714 format
, type
, /* source format/type */
2715 packing
, /* source packing */
2716 pixels
, /* source data */
2717 texImage
->TexFormat
,/* dest format */
2718 dest
, dstRowStride
);
2721 _mesa_transfer_teximage(ctx
, 1,
2723 texImage
->TexFormat
, texImage
->Data
,
2724 width
, 1, 1, /* src size */
2725 xoffset
, 0, 0, /* dest offsets */
2726 0, /* dstRowStride */
2727 0, /* dstImageStride */
2728 format
, type
, pixels
, packing
);
2732 /* GL_SGIS_generate_mipmap */
2733 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
2734 _mesa_generate_mipmap(ctx
, target
,
2735 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
2743 * This is the software fallback for Driver.TexSubImage2D()
2744 * and Driver.CopyTexSubImage2D().
2747 _mesa_store_texsubimage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
2748 GLint xoffset
, GLint yoffset
,
2749 GLint width
, GLint height
,
2750 GLenum format
, GLenum type
, const void *pixels
,
2751 const struct gl_pixelstore_attrib
*packing
,
2752 struct gl_texture_object
*texObj
,
2753 struct gl_texture_image
*texImage
)
2755 pixels
= validate_pbo_teximage(width
, height
, 1,
2756 format
, type
, pixels
, packing
);
2762 GLint dstRowStride
= 0, dstImageStride
= 0;
2764 if (texImage
->IsCompressed
) {
2765 dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2769 dstRowStride
= texImage
->Width
* texImage
->TexFormat
->TexelBytes
;
2771 ASSERT(texImage
->TexFormat
->StoreImage
);
2772 success
= texImage
->TexFormat
->StoreImage(ctx
, 2, texImage
->Format
,
2773 texImage
->TexFormat
,
2775 xoffset
, yoffset
, 0,
2776 dstRowStride
, dstImageStride
,
2778 format
, type
, pixels
, packing
);
2780 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
2786 if (texImage
->IsCompressed
) {
2787 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2789 GLubyte
*dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
2790 texImage
->IntFormat
,
2792 (GLubyte
*) texImage
->Data
);
2793 transfer_compressed_teximage(ctx
, 2, /* dimensions */
2794 width
, height
, 1, /* size to replace */
2795 format
, type
, /* source format/type */
2796 packing
, /* source packing */
2797 pixels
, /* source data */
2798 texImage
->TexFormat
,/* dest format */
2799 dest
, dstRowStride
);
2803 _mesa_transfer_teximage(ctx
, 2,
2805 texImage
->TexFormat
, texImage
->Data
,
2806 width
, height
, 1, /* src size */
2807 xoffset
, yoffset
, 0, /* dest offsets */
2808 texImage
->Width
*texImage
->TexFormat
->TexelBytes
,
2809 0, /* dstImageStride */
2810 format
, type
, pixels
, packing
);
2814 /* GL_SGIS_generate_mipmap */
2815 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
2816 _mesa_generate_mipmap(ctx
, target
,
2817 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
2824 * This is the software fallback for Driver.TexSubImage3D().
2825 * and Driver.CopyTexSubImage3D().
2828 _mesa_store_texsubimage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
2829 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2830 GLint width
, GLint height
, GLint depth
,
2831 GLenum format
, GLenum type
, const void *pixels
,
2832 const struct gl_pixelstore_attrib
*packing
,
2833 struct gl_texture_object
*texObj
,
2834 struct gl_texture_image
*texImage
)
2836 pixels
= validate_pbo_teximage(width
, height
, depth
,
2837 format
, type
, pixels
, packing
);
2843 GLint dstRowStride
, dstImageStride
;
2845 if (texImage
->IsCompressed
) {
2846 dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2848 dstImageStride
= 0; /* XXX fix */
2851 dstRowStride
= texImage
->Width
* texImage
->TexFormat
->TexelBytes
;
2852 dstImageStride
= dstRowStride
* texImage
->Height
;
2854 ASSERT(texImage
->TexFormat
->StoreImage
);
2855 success
= texImage
->TexFormat
->StoreImage(ctx
, 3, texImage
->Format
,
2856 texImage
->TexFormat
,
2858 xoffset
, yoffset
, zoffset
,
2859 dstRowStride
, dstImageStride
,
2860 width
, height
, depth
,
2861 format
, type
, pixels
, packing
);
2863 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage3D");
2868 if (texImage
->IsCompressed
) {
2869 GLint dstRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
2871 GLubyte
*dest
= _mesa_compressed_image_address(xoffset
, yoffset
, zoffset
,
2872 texImage
->IntFormat
,
2874 (GLubyte
*) texImage
->Data
);
2875 transfer_compressed_teximage(ctx
, 3, /* dimensions */
2876 width
, height
, depth
,/* size to replace */
2877 format
, type
, /* source format/type */
2878 packing
, /* source packing */
2879 pixels
, /* source data */
2880 texImage
->TexFormat
,/* dest format */
2881 dest
, dstRowStride
);
2884 const GLint texelBytes
= texImage
->TexFormat
->TexelBytes
;
2885 _mesa_transfer_teximage(ctx
, 3,
2887 texImage
->TexFormat
, texImage
->Data
,
2888 width
, height
, depth
, /* src size */
2889 xoffset
, yoffset
, zoffset
, /* dest offsets */
2890 texImage
->Width
* texelBytes
, /* dst row stride */
2891 texImage
->Width
* texImage
->Height
* texelBytes
,
2892 format
, type
, pixels
, packing
);
2895 /* GL_SGIS_generate_mipmap */
2896 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
2897 _mesa_generate_mipmap(ctx
, target
,
2898 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
2905 * Fallback for Driver.CompressedTexImage1D()
2908 _mesa_store_compressed_teximage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
2909 GLint internalFormat
,
2910 GLint width
, GLint border
,
2911 GLsizei imageSize
, const GLvoid
*data
,
2912 struct gl_texture_object
*texObj
,
2913 struct gl_texture_image
*texImage
)
2915 /* this space intentionally left blank */
2921 * Fallback for Driver.CompressedTexImage2D()
2924 _mesa_store_compressed_teximage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
2925 GLint internalFormat
,
2926 GLint width
, GLint height
, GLint border
,
2927 GLsizei imageSize
, const GLvoid
*data
,
2928 struct gl_texture_object
*texObj
,
2929 struct gl_texture_image
*texImage
)
2931 /* This is pretty simple, basically just do a memcpy without worrying
2932 * about the usual image unpacking or image transfer operations.
2936 ASSERT(texImage
->Width
> 0);
2937 ASSERT(texImage
->Height
> 0);
2938 ASSERT(texImage
->Depth
== 1);
2939 ASSERT(texImage
->Data
== NULL
); /* was freed in glCompressedTexImage2DARB */
2941 /* choose the texture format */
2942 assert(ctx
->Driver
.ChooseTextureFormat
);
2943 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2944 internalFormat
, 0, 0);
2945 assert(texImage
->TexFormat
);
2946 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2947 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2949 /* allocate storage */
2950 texImage
->Data
= MESA_PBUFFER_ALLOC(imageSize
);
2951 if (!texImage
->Data
) {
2952 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2DARB");
2956 data
= validate_pbo_compressed_teximage(imageSize
, data
, &ctx
->Unpack
);
2961 ASSERT(texImage
->CompressedSize
== (GLuint
) imageSize
);
2962 MEMCPY(texImage
->Data
, data
, imageSize
);
2968 * Fallback for Driver.CompressedTexImage3D()
2971 _mesa_store_compressed_teximage3d(GLcontext
*ctx
, GLenum target
, GLint level
,
2972 GLint internalFormat
,
2973 GLint width
, GLint height
, GLint depth
,
2975 GLsizei imageSize
, const GLvoid
*data
,
2976 struct gl_texture_object
*texObj
,
2977 struct gl_texture_image
*texImage
)
2979 /* this space intentionally left blank */
2985 * Fallback for Driver.CompressedTexSubImage1D()
2988 _mesa_store_compressed_texsubimage1d(GLcontext
*ctx
, GLenum target
,
2990 GLint xoffset
, GLsizei width
,
2992 GLsizei imageSize
, const GLvoid
*data
,
2993 struct gl_texture_object
*texObj
,
2994 struct gl_texture_image
*texImage
)
2996 /* this space intentionally left blank */
3001 * Fallback for Driver.CompressedTexSubImage2D()
3004 _mesa_store_compressed_texsubimage2d(GLcontext
*ctx
, GLenum target
,
3006 GLint xoffset
, GLint yoffset
,
3007 GLsizei width
, GLsizei height
,
3009 GLsizei imageSize
, const GLvoid
*data
,
3010 struct gl_texture_object
*texObj
,
3011 struct gl_texture_image
*texImage
)
3013 GLint bytesPerRow
, destRowStride
, srcRowStride
;
3018 /* these should have been caught sooner */
3019 ASSERT((width
& 3) == 0 || width
== 2 || width
== 1);
3020 ASSERT((height
& 3) == 0 || height
== 2 || height
== 1);
3021 ASSERT((xoffset
& 3) == 0);
3022 ASSERT((yoffset
& 3) == 0);
3024 data
= validate_pbo_compressed_teximage(imageSize
, data
, &ctx
->Unpack
);
3028 srcRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
, width
);
3029 src
= (const GLubyte
*) data
;
3031 destRowStride
= _mesa_compressed_row_stride(texImage
->IntFormat
,
3033 dest
= _mesa_compressed_image_address(xoffset
, yoffset
, 0,
3034 texImage
->IntFormat
,
3036 (GLubyte
*) texImage
->Data
);
3038 bytesPerRow
= srcRowStride
;
3041 for (i
= 0; i
< rows
; i
++) {
3042 MEMCPY(dest
, src
, bytesPerRow
);
3043 dest
+= destRowStride
;
3044 src
+= srcRowStride
;
3050 * Fallback for Driver.CompressedTexSubImage3D()
3053 _mesa_store_compressed_texsubimage3d(GLcontext
*ctx
, GLenum target
,
3055 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3056 GLsizei width
, GLsizei height
, GLsizei depth
,
3058 GLsizei imageSize
, const GLvoid
*data
,
3059 struct gl_texture_object
*texObj
,
3060 struct gl_texture_image
*texImage
)
3062 /* this space intentionally left blank */
3067 * Average together two rows of a source image to produce a single new
3068 * row in the dest image. It's legal for the two source rows to point
3069 * to the same data. The source width must be equal to either the
3070 * dest width or two times the dest width.
3073 do_row(const struct gl_texture_format
*format
, GLint srcWidth
,
3074 const GLvoid
*srcRowA
, const GLvoid
*srcRowB
,
3075 GLint dstWidth
, GLvoid
*dstRow
)
3077 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
3078 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
3080 /* This assertion is no longer valid with non-power-of-2 textures
3081 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
3084 switch (format
->MesaFormat
) {
3085 case MESA_FORMAT_RGBA
:
3088 const GLchan (*rowA
)[4] = (const GLchan (*)[4]) srcRowA
;
3089 const GLchan (*rowB
)[4] = (const GLchan (*)[4]) srcRowB
;
3090 GLchan (*dst
)[4] = (GLchan (*)[4]) dstRow
;
3091 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3092 i
++, j
+= colStride
, k
+= colStride
) {
3093 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3094 rowB
[j
][0] + rowB
[k
][0]) / 4;
3095 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3096 rowB
[j
][1] + rowB
[k
][1]) / 4;
3097 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
3098 rowB
[j
][2] + rowB
[k
][2]) / 4;
3099 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
3100 rowB
[j
][3] + rowB
[k
][3]) / 4;
3104 case MESA_FORMAT_RGB
:
3107 const GLchan (*rowA
)[3] = (const GLchan (*)[3]) srcRowA
;
3108 const GLchan (*rowB
)[3] = (const GLchan (*)[3]) srcRowB
;
3109 GLchan (*dst
)[3] = (GLchan (*)[3]) dstRow
;
3110 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3111 i
++, j
+= colStride
, k
+= colStride
) {
3112 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3113 rowB
[j
][0] + rowB
[k
][0]) / 4;
3114 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3115 rowB
[j
][1] + rowB
[k
][1]) / 4;
3116 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
3117 rowB
[j
][2] + rowB
[k
][2]) / 4;
3121 case MESA_FORMAT_ALPHA
:
3122 case MESA_FORMAT_LUMINANCE
:
3123 case MESA_FORMAT_INTENSITY
:
3124 case MESA_FORMAT_COLOR_INDEX
:
3127 const GLchan
*rowA
= (const GLchan
*) srcRowA
;
3128 const GLchan
*rowB
= (const GLchan
*) srcRowB
;
3129 GLchan
*dst
= (GLchan
*) dstRow
;
3130 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3131 i
++, j
+= colStride
, k
+= colStride
) {
3132 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) / 4;
3136 case MESA_FORMAT_LUMINANCE_ALPHA
:
3139 const GLchan (*rowA
)[2] = (const GLchan (*)[2]) srcRowA
;
3140 const GLchan (*rowB
)[2] = (const GLchan (*)[2]) srcRowB
;
3141 GLchan (*dst
)[2] = (GLchan (*)[2]) dstRow
;
3142 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3143 i
++, j
+= colStride
, k
+= colStride
) {
3144 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3145 rowB
[j
][0] + rowB
[k
][0]) / 4;
3146 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3147 rowB
[j
][1] + rowB
[k
][1]) / 4;
3151 case MESA_FORMAT_DEPTH_COMPONENT_FLOAT32
:
3154 const GLfloat
*rowA
= (const GLfloat
*) srcRowA
;
3155 const GLfloat
*rowB
= (const GLfloat
*) srcRowB
;
3156 GLfloat
*dst
= (GLfloat
*) dstRow
;
3157 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3158 i
++, j
+= colStride
, k
+= colStride
) {
3159 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) * 0.25F
;
3163 case MESA_FORMAT_DEPTH_COMPONENT16
:
3166 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
3167 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
3168 GLushort
*dst
= (GLushort
*) dstRow
;
3169 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3170 i
++, j
+= colStride
, k
+= colStride
) {
3171 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) / 4;
3175 /* Begin hardware formats */
3176 case MESA_FORMAT_RGBA8888
:
3177 case MESA_FORMAT_ARGB8888
:
3180 const GLubyte (*rowA
)[4] = (const GLubyte (*)[4]) srcRowA
;
3181 const GLubyte (*rowB
)[4] = (const GLubyte (*)[4]) srcRowB
;
3182 GLubyte (*dst
)[4] = (GLubyte (*)[4]) dstRow
;
3183 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3184 i
++, j
+= colStride
, k
+= colStride
) {
3185 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3186 rowB
[j
][0] + rowB
[k
][0]) / 4;
3187 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3188 rowB
[j
][1] + rowB
[k
][1]) / 4;
3189 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
3190 rowB
[j
][2] + rowB
[k
][2]) / 4;
3191 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
3192 rowB
[j
][3] + rowB
[k
][3]) / 4;
3196 case MESA_FORMAT_RGB888
:
3199 const GLubyte (*rowA
)[3] = (const GLubyte (*)[3]) srcRowA
;
3200 const GLubyte (*rowB
)[3] = (const GLubyte (*)[3]) srcRowB
;
3201 GLubyte (*dst
)[3] = (GLubyte (*)[3]) dstRow
;
3202 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3203 i
++, j
+= colStride
, k
+= colStride
) {
3204 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3205 rowB
[j
][0] + rowB
[k
][0]) / 4;
3206 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3207 rowB
[j
][1] + rowB
[k
][1]) / 4;
3208 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
3209 rowB
[j
][2] + rowB
[k
][2]) / 4;
3213 case MESA_FORMAT_RGB565
:
3216 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
3217 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
3218 GLushort
*dst
= (GLushort
*) dstRow
;
3219 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3220 i
++, j
+= colStride
, k
+= colStride
) {
3221 const GLint rowAr0
= rowA
[j
] & 0x1f;
3222 const GLint rowAr1
= rowA
[k
] & 0x1f;
3223 const GLint rowBr0
= rowB
[j
] & 0x1f;
3224 const GLint rowBr1
= rowB
[k
] & 0x1f;
3225 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x3f;
3226 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x3f;
3227 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x3f;
3228 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x3f;
3229 const GLint rowAb0
= (rowA
[j
] >> 11) & 0x1f;
3230 const GLint rowAb1
= (rowA
[k
] >> 11) & 0x1f;
3231 const GLint rowBb0
= (rowB
[j
] >> 11) & 0x1f;
3232 const GLint rowBb1
= (rowB
[k
] >> 11) & 0x1f;
3233 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
3234 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
3235 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
3236 dst
[i
] = (blue
<< 11) | (green
<< 5) | red
;
3240 case MESA_FORMAT_ARGB4444
:
3243 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
3244 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
3245 GLushort
*dst
= (GLushort
*) dstRow
;
3246 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3247 i
++, j
+= colStride
, k
+= colStride
) {
3248 const GLint rowAr0
= rowA
[j
] & 0xf;
3249 const GLint rowAr1
= rowA
[k
] & 0xf;
3250 const GLint rowBr0
= rowB
[j
] & 0xf;
3251 const GLint rowBr1
= rowB
[k
] & 0xf;
3252 const GLint rowAg0
= (rowA
[j
] >> 4) & 0xf;
3253 const GLint rowAg1
= (rowA
[k
] >> 4) & 0xf;
3254 const GLint rowBg0
= (rowB
[j
] >> 4) & 0xf;
3255 const GLint rowBg1
= (rowB
[k
] >> 4) & 0xf;
3256 const GLint rowAb0
= (rowA
[j
] >> 8) & 0xf;
3257 const GLint rowAb1
= (rowA
[k
] >> 8) & 0xf;
3258 const GLint rowBb0
= (rowB
[j
] >> 8) & 0xf;
3259 const GLint rowBb1
= (rowB
[k
] >> 8) & 0xf;
3260 const GLint rowAa0
= (rowA
[j
] >> 12) & 0xf;
3261 const GLint rowAa1
= (rowA
[k
] >> 12) & 0xf;
3262 const GLint rowBa0
= (rowB
[j
] >> 12) & 0xf;
3263 const GLint rowBa1
= (rowB
[k
] >> 12) & 0xf;
3264 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
3265 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
3266 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
3267 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 2;
3268 dst
[i
] = (alpha
<< 12) | (blue
<< 8) | (green
<< 4) | red
;
3272 case MESA_FORMAT_ARGB1555
:
3275 const GLushort
*rowA
= (const GLushort
*) srcRowA
;
3276 const GLushort
*rowB
= (const GLushort
*) srcRowB
;
3277 GLushort
*dst
= (GLushort
*) dstRow
;
3278 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3279 i
++, j
+= colStride
, k
+= colStride
) {
3280 const GLint rowAr0
= rowA
[j
] & 0x1f;
3281 const GLint rowAr1
= rowA
[k
] & 0x1f;
3282 const GLint rowBr0
= rowB
[j
] & 0x1f;
3283 const GLint rowBr1
= rowB
[k
] & 0xf;
3284 const GLint rowAg0
= (rowA
[j
] >> 5) & 0x1f;
3285 const GLint rowAg1
= (rowA
[k
] >> 5) & 0x1f;
3286 const GLint rowBg0
= (rowB
[j
] >> 5) & 0x1f;
3287 const GLint rowBg1
= (rowB
[k
] >> 5) & 0x1f;
3288 const GLint rowAb0
= (rowA
[j
] >> 10) & 0x1f;
3289 const GLint rowAb1
= (rowA
[k
] >> 10) & 0x1f;
3290 const GLint rowBb0
= (rowB
[j
] >> 10) & 0x1f;
3291 const GLint rowBb1
= (rowB
[k
] >> 10) & 0x1f;
3292 const GLint rowAa0
= (rowA
[j
] >> 15) & 0x1;
3293 const GLint rowAa1
= (rowA
[k
] >> 15) & 0x1;
3294 const GLint rowBa0
= (rowB
[j
] >> 15) & 0x1;
3295 const GLint rowBa1
= (rowB
[k
] >> 15) & 0x1;
3296 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
3297 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
3298 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
3299 const GLint alpha
= (rowAa0
+ rowAa1
+ rowBa0
+ rowBa1
) >> 2;
3300 dst
[i
] = (alpha
<< 15) | (blue
<< 10) | (green
<< 5) | red
;
3304 case MESA_FORMAT_AL88
:
3307 const GLubyte (*rowA
)[2] = (const GLubyte (*)[2]) srcRowA
;
3308 const GLubyte (*rowB
)[2] = (const GLubyte (*)[2]) srcRowB
;
3309 GLubyte (*dst
)[2] = (GLubyte (*)[2]) dstRow
;
3310 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3311 i
++, j
+= colStride
, k
+= colStride
) {
3312 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3313 rowB
[j
][0] + rowB
[k
][0]) >> 2;
3314 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3315 rowB
[j
][1] + rowB
[k
][1]) >> 2;
3319 case MESA_FORMAT_RGB332
:
3322 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
3323 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
3324 GLubyte
*dst
= (GLubyte
*) dstRow
;
3325 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3326 i
++, j
+= colStride
, k
+= colStride
) {
3327 const GLint rowAr0
= rowA
[j
] & 0x3;
3328 const GLint rowAr1
= rowA
[k
] & 0x3;
3329 const GLint rowBr0
= rowB
[j
] & 0x3;
3330 const GLint rowBr1
= rowB
[k
] & 0x3;
3331 const GLint rowAg0
= (rowA
[j
] >> 2) & 0x7;
3332 const GLint rowAg1
= (rowA
[k
] >> 2) & 0x7;
3333 const GLint rowBg0
= (rowB
[j
] >> 2) & 0x7;
3334 const GLint rowBg1
= (rowB
[k
] >> 2) & 0x7;
3335 const GLint rowAb0
= (rowA
[j
] >> 5) & 0x7;
3336 const GLint rowAb1
= (rowA
[k
] >> 5) & 0x7;
3337 const GLint rowBb0
= (rowB
[j
] >> 5) & 0x7;
3338 const GLint rowBb1
= (rowB
[k
] >> 5) & 0x7;
3339 const GLint red
= (rowAr0
+ rowAr1
+ rowBr0
+ rowBr1
) >> 2;
3340 const GLint green
= (rowAg0
+ rowAg1
+ rowBg0
+ rowBg1
) >> 2;
3341 const GLint blue
= (rowAb0
+ rowAb1
+ rowBb0
+ rowBb1
) >> 2;
3342 dst
[i
] = (blue
<< 5) | (green
<< 2) | red
;
3346 case MESA_FORMAT_A8
:
3347 case MESA_FORMAT_L8
:
3348 case MESA_FORMAT_I8
:
3349 case MESA_FORMAT_CI8
:
3352 const GLubyte
*rowA
= (const GLubyte
*) srcRowA
;
3353 const GLubyte
*rowB
= (const GLubyte
*) srcRowB
;
3354 GLubyte
*dst
= (GLubyte
*) dstRow
;
3355 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3356 i
++, j
+= colStride
, k
+= colStride
) {
3357 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) >> 2;
3361 case MESA_FORMAT_RGBA_FLOAT32
:
3364 const GLfloat (*rowA
)[4] = (const GLfloat (*)[4]) srcRowA
;
3365 const GLfloat (*rowB
)[4] = (const GLfloat (*)[4]) srcRowB
;
3366 GLfloat (*dst
)[4] = (GLfloat (*)[4]) dstRow
;
3367 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3368 i
++, j
+= colStride
, k
+= colStride
) {
3369 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3370 rowB
[j
][0] + rowB
[k
][0]) * 0.25F
;
3371 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3372 rowB
[j
][1] + rowB
[k
][1]) * 0.25F
;
3373 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
3374 rowB
[j
][2] + rowB
[k
][2]) * 0.25F
;
3375 dst
[i
][3] = (rowA
[j
][3] + rowA
[k
][3] +
3376 rowB
[j
][3] + rowB
[k
][3]) * 0.25F
;
3380 case MESA_FORMAT_RGBA_FLOAT16
:
3382 GLuint i
, j
, k
, comp
;
3383 const GLhalfARB (*rowA
)[4] = (const GLhalfARB (*)[4]) srcRowA
;
3384 const GLhalfARB (*rowB
)[4] = (const GLhalfARB (*)[4]) srcRowB
;
3385 GLhalfARB (*dst
)[4] = (GLhalfARB (*)[4]) dstRow
;
3386 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3387 i
++, j
+= colStride
, k
+= colStride
) {
3388 for (comp
= 0; comp
< 4; comp
++) {
3389 GLfloat aj
, ak
, bj
, bk
;
3390 aj
= _mesa_half_to_float(rowA
[j
][comp
]);
3391 ak
= _mesa_half_to_float(rowA
[k
][comp
]);
3392 bj
= _mesa_half_to_float(rowB
[j
][comp
]);
3393 bk
= _mesa_half_to_float(rowB
[k
][comp
]);
3394 dst
[i
][comp
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
3399 case MESA_FORMAT_RGB_FLOAT32
:
3402 const GLfloat (*rowA
)[3] = (const GLfloat (*)[3]) srcRowA
;
3403 const GLfloat (*rowB
)[3] = (const GLfloat (*)[3]) srcRowB
;
3404 GLfloat (*dst
)[3] = (GLfloat (*)[3]) dstRow
;
3405 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3406 i
++, j
+= colStride
, k
+= colStride
) {
3407 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3408 rowB
[j
][0] + rowB
[k
][0]) * 0.25F
;
3409 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3410 rowB
[j
][1] + rowB
[k
][1]) * 0.25F
;
3411 dst
[i
][2] = (rowA
[j
][2] + rowA
[k
][2] +
3412 rowB
[j
][2] + rowB
[k
][2]) * 0.25F
;
3416 case MESA_FORMAT_RGB_FLOAT16
:
3418 GLuint i
, j
, k
, comp
;
3419 const GLhalfARB (*rowA
)[3] = (const GLhalfARB (*)[3]) srcRowA
;
3420 const GLhalfARB (*rowB
)[3] = (const GLhalfARB (*)[3]) srcRowB
;
3421 GLhalfARB (*dst
)[3] = (GLhalfARB (*)[3]) dstRow
;
3422 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3423 i
++, j
+= colStride
, k
+= colStride
) {
3424 for (comp
= 0; comp
< 3; comp
++) {
3425 GLfloat aj
, ak
, bj
, bk
;
3426 aj
= _mesa_half_to_float(rowA
[j
][comp
]);
3427 ak
= _mesa_half_to_float(rowA
[k
][comp
]);
3428 bj
= _mesa_half_to_float(rowB
[j
][comp
]);
3429 bk
= _mesa_half_to_float(rowB
[k
][comp
]);
3430 dst
[i
][comp
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
3435 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
:
3438 const GLfloat (*rowA
)[2] = (const GLfloat (*)[2]) srcRowA
;
3439 const GLfloat (*rowB
)[2] = (const GLfloat (*)[2]) srcRowB
;
3440 GLfloat (*dst
)[2] = (GLfloat (*)[2]) dstRow
;
3441 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3442 i
++, j
+= colStride
, k
+= colStride
) {
3443 dst
[i
][0] = (rowA
[j
][0] + rowA
[k
][0] +
3444 rowB
[j
][0] + rowB
[k
][0]) * 0.25F
;
3445 dst
[i
][1] = (rowA
[j
][1] + rowA
[k
][1] +
3446 rowB
[j
][1] + rowB
[k
][1]) * 0.25F
;
3450 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
:
3452 GLuint i
, j
, k
, comp
;
3453 const GLhalfARB (*rowA
)[2] = (const GLhalfARB (*)[2]) srcRowA
;
3454 const GLhalfARB (*rowB
)[2] = (const GLhalfARB (*)[2]) srcRowB
;
3455 GLhalfARB (*dst
)[2] = (GLhalfARB (*)[2]) dstRow
;
3456 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3457 i
++, j
+= colStride
, k
+= colStride
) {
3458 for (comp
= 0; comp
< 2; comp
++) {
3459 GLfloat aj
, ak
, bj
, bk
;
3460 aj
= _mesa_half_to_float(rowA
[j
][comp
]);
3461 ak
= _mesa_half_to_float(rowA
[k
][comp
]);
3462 bj
= _mesa_half_to_float(rowB
[j
][comp
]);
3463 bk
= _mesa_half_to_float(rowB
[k
][comp
]);
3464 dst
[i
][comp
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
3469 case MESA_FORMAT_ALPHA_FLOAT32
:
3470 case MESA_FORMAT_LUMINANCE_FLOAT32
:
3471 case MESA_FORMAT_INTENSITY_FLOAT32
:
3474 const GLfloat
*rowA
= (const GLfloat
*) srcRowA
;
3475 const GLfloat
*rowB
= (const GLfloat
*) srcRowB
;
3476 GLfloat
*dst
= (GLfloat
*) dstRow
;
3477 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3478 i
++, j
+= colStride
, k
+= colStride
) {
3479 dst
[i
] = (rowA
[j
] + rowA
[k
] + rowB
[j
] + rowB
[k
]) * 0.25F
;
3483 case MESA_FORMAT_ALPHA_FLOAT16
:
3484 case MESA_FORMAT_LUMINANCE_FLOAT16
:
3485 case MESA_FORMAT_INTENSITY_FLOAT16
:
3488 const GLhalfARB
*rowA
= (const GLhalfARB
*) srcRowA
;
3489 const GLhalfARB
*rowB
= (const GLhalfARB
*) srcRowB
;
3490 GLhalfARB
*dst
= (GLhalfARB
*) dstRow
;
3491 for (i
= j
= 0, k
= k0
; i
< (GLuint
) dstWidth
;
3492 i
++, j
+= colStride
, k
+= colStride
) {
3493 GLfloat aj
, ak
, bj
, bk
;
3494 aj
= _mesa_half_to_float(rowA
[j
]);
3495 ak
= _mesa_half_to_float(rowA
[k
]);
3496 bj
= _mesa_half_to_float(rowB
[j
]);
3497 bk
= _mesa_half_to_float(rowB
[k
]);
3498 dst
[i
] = _mesa_float_to_half((aj
+ ak
+ bj
+ bk
) * 0.25F
);
3504 _mesa_problem(NULL
, "bad format in do_row()");
3510 * These functions generate a 1/2-size mipmap image from a source image.
3511 * Texture borders are handled by copying or averaging the source image's
3512 * border texels, depending on the scale-down factor.
3516 make_1d_mipmap(const struct gl_texture_format
*format
, GLint border
,
3517 GLint srcWidth
, const GLubyte
*srcPtr
,
3518 GLint dstWidth
, GLubyte
*dstPtr
)
3520 const GLint bpt
= format
->TexelBytes
;
3524 /* skip the border pixel, if any */
3525 src
= srcPtr
+ border
* bpt
;
3526 dst
= dstPtr
+ border
* bpt
;
3528 /* we just duplicate the input row, kind of hack, saves code */
3529 do_row(format
, srcWidth
- 2 * border
, src
, src
,
3530 dstWidth
- 2 * border
, dst
);
3533 /* copy left-most pixel from source */
3534 MEMCPY(dstPtr
, srcPtr
, bpt
);
3535 /* copy right-most pixel from source */
3536 MEMCPY(dstPtr
+ (dstWidth
- 1) * bpt
,
3537 srcPtr
+ (srcWidth
- 1) * bpt
,
3544 make_2d_mipmap(const struct gl_texture_format
*format
, GLint border
,
3545 GLint srcWidth
, GLint srcHeight
, const GLubyte
*srcPtr
,
3546 GLint dstWidth
, GLint dstHeight
, GLubyte
*dstPtr
)
3548 const GLint bpt
= format
->TexelBytes
;
3549 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
3550 const GLint dstWidthNB
= dstWidth
- 2 * border
;
3551 const GLint dstHeightNB
= dstHeight
- 2 * border
;
3552 const GLint srcRowStride
= bpt
* srcWidth
;
3553 const GLint dstRowStride
= bpt
* dstWidth
;
3554 const GLubyte
*srcA
, *srcB
;
3556 GLint row
, colStride
;
3558 colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
3560 /* Compute src and dst pointers, skipping any border */
3561 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
3563 srcB
= srcA
+ srcRowStride
;
3566 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
3568 for (row
= 0; row
< dstHeightNB
; row
++) {
3569 do_row(format
, srcWidthNB
, srcA
, srcB
,
3571 srcA
+= 2 * srcRowStride
;
3572 srcB
+= 2 * srcRowStride
;
3573 dst
+= dstRowStride
;
3576 /* This is ugly but probably won't be used much */
3578 /* fill in dest border */
3579 /* lower-left border pixel */
3580 MEMCPY(dstPtr
, srcPtr
, bpt
);
3581 /* lower-right border pixel */
3582 MEMCPY(dstPtr
+ (dstWidth
- 1) * bpt
,
3583 srcPtr
+ (srcWidth
- 1) * bpt
, bpt
);
3584 /* upper-left border pixel */
3585 MEMCPY(dstPtr
+ dstWidth
* (dstHeight
- 1) * bpt
,
3586 srcPtr
+ srcWidth
* (srcHeight
- 1) * bpt
, bpt
);
3587 /* upper-right border pixel */
3588 MEMCPY(dstPtr
+ (dstWidth
* dstHeight
- 1) * bpt
,
3589 srcPtr
+ (srcWidth
* srcHeight
- 1) * bpt
, bpt
);
3591 do_row(format
, srcWidthNB
,
3594 dstWidthNB
, dstPtr
+ bpt
);
3596 do_row(format
, srcWidthNB
,
3597 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
3598 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
3600 dstPtr
+ (dstWidth
* (dstHeight
- 1) + 1) * bpt
);
3601 /* left and right borders */
3602 if (srcHeight
== dstHeight
) {
3603 /* copy border pixel from src to dst */
3604 for (row
= 1; row
< srcHeight
; row
++) {
3605 MEMCPY(dstPtr
+ dstWidth
* row
* bpt
,
3606 srcPtr
+ srcWidth
* row
* bpt
, bpt
);
3607 MEMCPY(dstPtr
+ (dstWidth
* row
+ dstWidth
- 1) * bpt
,
3608 srcPtr
+ (srcWidth
* row
+ srcWidth
- 1) * bpt
, bpt
);
3612 /* average two src pixels each dest pixel */
3613 for (row
= 0; row
< dstHeightNB
; row
+= 2) {
3615 srcPtr
+ (srcWidth
* (row
* 2 + 1)) * bpt
,
3616 srcPtr
+ (srcWidth
* (row
* 2 + 2)) * bpt
,
3617 1, dstPtr
+ (dstWidth
* row
+ 1) * bpt
);
3619 srcPtr
+ (srcWidth
* (row
* 2 + 1) + srcWidth
- 1) * bpt
,
3620 srcPtr
+ (srcWidth
* (row
* 2 + 2) + srcWidth
- 1) * bpt
,
3621 1, dstPtr
+ (dstWidth
* row
+ 1 + dstWidth
- 1) * bpt
);
3629 make_3d_mipmap(const struct gl_texture_format
*format
, GLint border
,
3630 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
3631 const GLubyte
*srcPtr
,
3632 GLint dstWidth
, GLint dstHeight
, GLint dstDepth
,
3635 const GLint bpt
= format
->TexelBytes
;
3636 const GLint srcWidthNB
= srcWidth
- 2 * border
; /* sizes w/out border */
3637 const GLint srcDepthNB
= srcDepth
- 2 * border
;
3638 const GLint dstWidthNB
= dstWidth
- 2 * border
;
3639 const GLint dstHeightNB
= dstHeight
- 2 * border
;
3640 const GLint dstDepthNB
= dstDepth
- 2 * border
;
3641 GLvoid
*tmpRowA
, *tmpRowB
;
3643 GLint bytesPerSrcImage
, bytesPerDstImage
;
3644 GLint bytesPerSrcRow
, bytesPerDstRow
;
3645 GLint srcImageOffset
, srcRowOffset
;
3647 (void) srcDepthNB
; /* silence warnings */
3649 /* Need two temporary row buffers */
3650 tmpRowA
= MALLOC(srcWidth
* bpt
);
3653 tmpRowB
= MALLOC(srcWidth
* bpt
);
3659 bytesPerSrcImage
= srcWidth
* srcHeight
* bpt
;
3660 bytesPerDstImage
= dstWidth
* dstHeight
* bpt
;
3662 bytesPerSrcRow
= srcWidth
* bpt
;
3663 bytesPerDstRow
= dstWidth
* bpt
;
3665 /* Offset between adjacent src images to be averaged together */
3666 srcImageOffset
= (srcDepth
== dstDepth
) ? 0 : bytesPerSrcImage
;
3668 /* Offset between adjacent src rows to be averaged together */
3669 srcRowOffset
= (srcHeight
== dstHeight
) ? 0 : srcWidth
* bpt
;
3672 * Need to average together up to 8 src pixels for each dest pixel.
3673 * Break that down into 3 operations:
3674 * 1. take two rows from source image and average them together.
3675 * 2. take two rows from next source image and average them together.
3676 * 3. take the two averaged rows and average them for the final dst row.
3680 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
3681 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
3684 for (img
= 0; img
< dstDepthNB
; img
++) {
3685 /* first source image pointer, skipping border */
3686 const GLubyte
*imgSrcA
= srcPtr
3687 + (bytesPerSrcImage
+ bytesPerSrcRow
+ border
) * bpt
* border
3688 + img
* (bytesPerSrcImage
+ srcImageOffset
);
3689 /* second source image pointer, skipping border */
3690 const GLubyte
*imgSrcB
= imgSrcA
+ srcImageOffset
;
3691 /* address of the dest image, skipping border */
3692 GLubyte
*imgDst
= dstPtr
3693 + (bytesPerDstImage
+ bytesPerDstRow
+ border
) * bpt
* border
3694 + img
* bytesPerDstImage
;
3696 /* setup the four source row pointers and the dest row pointer */
3697 const GLubyte
*srcImgARowA
= imgSrcA
;
3698 const GLubyte
*srcImgARowB
= imgSrcA
+ srcRowOffset
;
3699 const GLubyte
*srcImgBRowA
= imgSrcB
;
3700 const GLubyte
*srcImgBRowB
= imgSrcB
+ srcRowOffset
;
3701 GLubyte
*dstImgRow
= imgDst
;
3703 for (row
= 0; row
< dstHeightNB
; row
++) {
3704 /* Average together two rows from first src image */
3705 do_row(format
, srcWidthNB
, srcImgARowA
, srcImgARowB
,
3706 srcWidthNB
, tmpRowA
);
3707 /* Average together two rows from second src image */
3708 do_row(format
, srcWidthNB
, srcImgBRowA
, srcImgBRowB
,
3709 srcWidthNB
, tmpRowB
);
3710 /* Average together the temp rows to make the final row */
3711 do_row(format
, srcWidthNB
, tmpRowA
, tmpRowB
,
3712 dstWidthNB
, dstImgRow
);
3713 /* advance to next rows */
3714 srcImgARowA
+= bytesPerSrcRow
+ srcRowOffset
;
3715 srcImgARowB
+= bytesPerSrcRow
+ srcRowOffset
;
3716 srcImgBRowA
+= bytesPerSrcRow
+ srcRowOffset
;
3717 srcImgBRowB
+= bytesPerSrcRow
+ srcRowOffset
;
3718 dstImgRow
+= bytesPerDstRow
;
3725 /* Luckily we can leverage the make_2d_mipmap() function here! */
3727 /* do front border image */
3728 make_2d_mipmap(format
, 1, srcWidth
, srcHeight
, srcPtr
,
3729 dstWidth
, dstHeight
, dstPtr
);
3730 /* do back border image */
3731 make_2d_mipmap(format
, 1, srcWidth
, srcHeight
,
3732 srcPtr
+ bytesPerSrcImage
* (srcDepth
- 1),
3733 dstWidth
, dstHeight
,
3734 dstPtr
+ bytesPerDstImage
* (dstDepth
- 1));
3735 /* do four remaining border edges that span the image slices */
3736 if (srcDepth
== dstDepth
) {
3737 /* just copy border pixels from src to dst */
3738 for (img
= 0; img
< dstDepthNB
; img
++) {
3742 /* do border along [img][row=0][col=0] */
3743 src
= srcPtr
+ (img
+ 1) * bytesPerSrcImage
;
3744 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
;
3745 MEMCPY(dst
, src
, bpt
);
3747 /* do border along [img][row=dstHeight-1][col=0] */
3748 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
3749 + (srcHeight
- 1) * bytesPerSrcRow
;
3750 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
3751 + (dstHeight
- 1) * bytesPerDstRow
;
3752 MEMCPY(dst
, src
, bpt
);
3754 /* do border along [img][row=0][col=dstWidth-1] */
3755 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
3756 + (srcWidth
- 1) * bpt
;
3757 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
3758 + (dstWidth
- 1) * bpt
;
3759 MEMCPY(dst
, src
, bpt
);
3761 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
3762 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
3763 + (bytesPerSrcImage
- bpt
);
3764 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
3765 + (bytesPerDstImage
- bpt
);
3766 MEMCPY(dst
, src
, bpt
);
3770 /* average border pixels from adjacent src image pairs */
3771 ASSERT(srcDepthNB
== 2 * dstDepthNB
);
3772 for (img
= 0; img
< dstDepthNB
; img
++) {
3776 /* do border along [img][row=0][col=0] */
3777 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
;
3778 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
;
3779 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
3781 /* do border along [img][row=dstHeight-1][col=0] */
3782 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
3783 + (srcHeight
- 1) * bytesPerSrcRow
;
3784 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
3785 + (dstHeight
- 1) * bytesPerDstRow
;
3786 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
3788 /* do border along [img][row=0][col=dstWidth-1] */
3789 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
3790 + (srcWidth
- 1) * bpt
;
3791 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
3792 + (dstWidth
- 1) * bpt
;
3793 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
3795 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
3796 src
= srcPtr
+ (img
* 2 + 1) * bytesPerSrcImage
3797 + (bytesPerSrcImage
- bpt
);
3798 dst
= dstPtr
+ (img
+ 1) * bytesPerDstImage
3799 + (bytesPerDstImage
- bpt
);
3800 do_row(format
, 1, src
, src
+ srcImageOffset
, 1, dst
);
3808 * For GL_SGIX_generate_mipmap:
3809 * Generate a complete set of mipmaps from texObj's base-level image.
3810 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
3813 _mesa_generate_mipmap(GLcontext
*ctx
, GLenum target
,
3814 const struct gl_texture_unit
*texUnit
,
3815 struct gl_texture_object
*texObj
)
3817 const struct gl_texture_image
*srcImage
;
3818 const struct gl_texture_format
*convertFormat
;
3819 const GLubyte
*srcData
= NULL
;
3820 GLubyte
*dstData
= NULL
;
3821 GLint level
, maxLevels
;
3824 srcImage
= texObj
->Image
[0][texObj
->BaseLevel
];
3827 maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
3828 ASSERT(maxLevels
> 0); /* bad target */
3830 /* Find convertFormat - the format that do_row() will process */
3831 if (srcImage
->IsCompressed
) {
3832 /* setup for compressed textures */
3834 GLint components
, size
;
3837 assert(texObj
->Target
== GL_TEXTURE_2D
);
3839 if (srcImage
->Format
== GL_RGB
) {
3840 convertFormat
= &_mesa_texformat_rgb
;
3843 else if (srcImage
->Format
== GL_RGBA
) {
3844 convertFormat
= &_mesa_texformat_rgba
;
3848 _mesa_problem(ctx
, "bad srcImage->Format in _mesa_generate_mipmaps");
3852 /* allocate storage for uncompressed GL_RGB or GL_RGBA images */
3853 size
= _mesa_bytes_per_pixel(srcImage
->Format
, CHAN_TYPE
)
3854 * srcImage
->Width
* srcImage
->Height
* srcImage
->Depth
+ 20;
3855 /* 20 extra bytes, just be safe when calling last FetchTexel */
3856 srcData
= (GLubyte
*) MALLOC(size
);
3858 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
3861 dstData
= (GLubyte
*) MALLOC(size
/ 2); /* 1/4 would probably be OK */
3863 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
3864 FREE((void *) srcData
);
3868 /* decompress base image here */
3869 dst
= (GLchan
*) srcData
;
3870 for (row
= 0; row
< srcImage
->Height
; row
++) {
3872 for (col
= 0; col
< srcImage
->Width
; col
++) {
3873 srcImage
->FetchTexelc(srcImage
, col
, row
, 0, dst
);
3880 convertFormat
= srcImage
->TexFormat
;
3883 for (level
= texObj
->BaseLevel
; level
< texObj
->MaxLevel
3884 && level
< maxLevels
- 1; level
++) {
3885 /* generate image[level+1] from image[level] */
3886 const struct gl_texture_image
*srcImage
;
3887 struct gl_texture_image
*dstImage
;
3888 GLint srcWidth
, srcHeight
, srcDepth
;
3889 GLint dstWidth
, dstHeight
, dstDepth
;
3890 GLint border
, bytesPerTexel
;
3892 /* get src image parameters */
3893 srcImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3895 srcWidth
= srcImage
->Width
;
3896 srcHeight
= srcImage
->Height
;
3897 srcDepth
= srcImage
->Depth
;
3898 border
= srcImage
->Border
;
3900 /* compute next (level+1) image size */
3901 if (srcWidth
- 2 * border
> 1) {
3902 dstWidth
= (srcWidth
- 2 * border
) / 2 + 2 * border
;
3905 dstWidth
= srcWidth
; /* can't go smaller */
3907 if (srcHeight
- 2 * border
> 1) {
3908 dstHeight
= (srcHeight
- 2 * border
) / 2 + 2 * border
;
3911 dstHeight
= srcHeight
; /* can't go smaller */
3913 if (srcDepth
- 2 * border
> 1) {
3914 dstDepth
= (srcDepth
- 2 * border
) / 2 + 2 * border
;
3917 dstDepth
= srcDepth
; /* can't go smaller */
3920 if (dstWidth
== srcWidth
&&
3921 dstHeight
== srcHeight
&&
3922 dstDepth
== srcDepth
) {
3924 if (srcImage
->IsCompressed
) {
3925 FREE((void *) srcData
);
3931 /* get dest gl_texture_image */
3932 dstImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
+ 1);
3934 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
3938 /* Free old image data */
3940 MESA_PBUFFER_FREE(dstImage
->Data
);
3942 /* initialize new image */
3943 _mesa_init_teximage_fields(ctx
, target
, dstImage
, dstWidth
, dstHeight
,
3944 dstDepth
, border
, srcImage
->IntFormat
);
3945 dstImage
->DriverData
= NULL
;
3946 dstImage
->TexFormat
= srcImage
->TexFormat
;
3947 dstImage
->FetchTexelc
= srcImage
->FetchTexelc
;
3948 dstImage
->FetchTexelf
= srcImage
->FetchTexelf
;
3949 ASSERT(dstImage
->TexFormat
);
3950 ASSERT(dstImage
->FetchTexelc
);
3951 ASSERT(dstImage
->FetchTexelf
);
3953 /* Alloc new teximage data buffer.
3954 * Setup src and dest data pointers.
3956 if (dstImage
->IsCompressed
) {
3957 ASSERT(dstImage
->CompressedSize
> 0); /* set by init_teximage_fields*/
3958 dstImage
->Data
= MESA_PBUFFER_ALLOC(dstImage
->CompressedSize
);
3959 if (!dstImage
->Data
) {
3960 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
3963 /* srcData and dstData are already set */
3968 bytesPerTexel
= srcImage
->TexFormat
->TexelBytes
;
3969 ASSERT(dstWidth
* dstHeight
* dstDepth
* bytesPerTexel
> 0);
3970 dstImage
->Data
= MESA_PBUFFER_ALLOC(dstWidth
* dstHeight
* dstDepth
3972 if (!dstImage
->Data
) {
3973 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
3976 srcData
= (const GLubyte
*) srcImage
->Data
;
3977 dstData
= (GLubyte
*) dstImage
->Data
;
3981 * We use simple 2x2 averaging to compute the next mipmap level.
3985 make_1d_mipmap(convertFormat
, border
,
3990 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
3991 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
3992 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
3993 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
3994 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
3995 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
3996 make_2d_mipmap(convertFormat
, border
,
3997 srcWidth
, srcHeight
, srcData
,
3998 dstWidth
, dstHeight
, dstData
);
4001 make_3d_mipmap(convertFormat
, border
,
4002 srcWidth
, srcHeight
, srcDepth
, srcData
,
4003 dstWidth
, dstHeight
, dstDepth
, dstData
);
4005 case GL_TEXTURE_RECTANGLE_NV
:
4006 /* no mipmaps, do nothing */
4009 _mesa_problem(ctx
, "bad dimensions in _mesa_generate_mipmaps");
4013 if (dstImage
->IsCompressed
) {
4015 /* compress image from dstData into dstImage->Data */
4016 const GLenum srcFormat
= convertFormat
->BaseFormat
;
4017 GLint dstRowStride
= _mesa_compressed_row_stride(srcImage
->IntFormat
,
4019 ASSERT(srcFormat
== GL_RGB
|| srcFormat
== GL_RGBA
);
4020 _mesa_compress_teximage(ctx
,
4021 dstWidth
, dstHeight
, /* size */
4022 srcFormat
, /* source format */
4023 (const GLchan
*) dstData
, /* source buffer */
4024 dstWidth
, /* source row stride */
4025 dstImage
->TexFormat
, /* dest format */
4026 (GLubyte
*) dstImage
->Data
, /* dest buffer */
4027 dstRowStride
); /* dest row stride */
4029 /* swap src and dest pointers */
4030 temp
= (GLubyte
*) srcData
;
4035 } /* loop over mipmap levels */