2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008-2009 VMware, Inc.
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
32 * The GL texture image functions in teximage.c basically just do
33 * error checking and data structure allocation. They in turn call
34 * device driver functions which actually copy/convert/store the user's
37 * However, most device drivers will be able to use the fallback functions
38 * in this file. That is, most drivers will have the following bit of
40 * ctx->Driver.TexImage = _mesa_store_teximage;
41 * ctx->Driver.TexSubImage = _mesa_store_texsubimage;
44 * Texture image processing is actually kind of complicated. We have to do:
45 * Format/type conversions
47 * pixel transfer (scale, bais, lookup, etc)
49 * These functions can handle most everything, including processing full
50 * images and sub-images.
55 #include "bufferobj.h"
57 #include "format_pack.h"
58 #include "format_utils.h"
66 #include "texcompress.h"
67 #include "texcompress_fxt1.h"
68 #include "texcompress_rgtc.h"
69 #include "texcompress_s3tc.h"
70 #include "texcompress_etc.h"
74 #include "glformats.h"
75 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
76 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
86 * Texture image storage function.
88 typedef GLboolean (*StoreTexImageFunc
)(TEXSTORE_PARAMS
);
108 #define MAP1(x) MAP4(x, ZERO, ZERO, ZERO)
109 #define MAP2(x,y) MAP4(x, y, ZERO, ZERO)
110 #define MAP3(x,y,z) MAP4(x, y, z, ZERO)
111 #define MAP4(x,y,z,w) { x, y, z, w, ZERO, ONE }
114 static const struct {
117 GLubyte from_rgba
[6];
118 } mappings
[MAX_IDX
] =
128 MAP4(ZERO
, ZERO
, ZERO
, 0),
158 MAP4(0, ZERO
, ZERO
, ONE
),
164 MAP4(ZERO
, 0, ZERO
, ONE
),
170 MAP4(ZERO
, ZERO
, 0, ONE
),
194 MAP4(0, 1, ZERO
, ONE
),
202 * Convert a GL image format enum to an IDX_* value (see above).
205 get_map_idx(GLenum value
)
209 case GL_LUMINANCE_INTEGER_EXT
:
210 return IDX_LUMINANCE
;
212 case GL_ALPHA_INTEGER
:
215 return IDX_INTENSITY
;
216 case GL_LUMINANCE_ALPHA
:
217 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
218 return IDX_LUMINANCE_ALPHA
;
223 case GL_RGBA_INTEGER
:
236 case GL_BGRA_INTEGER
:
244 _mesa_problem(NULL
, "Unexpected inFormat %s",
245 _mesa_lookup_enum_by_nr(value
));
252 * When promoting texture formats (see below) we need to compute the
253 * mapping of dest components back to source components.
254 * This function does that.
255 * \param inFormat the incoming format of the texture
256 * \param outFormat the final texture format
257 * \return map[6] a full 6-component map
260 compute_component_mapping(GLenum inFormat
, GLenum outFormat
,
263 const int inFmt
= get_map_idx(inFormat
);
264 const int outFmt
= get_map_idx(outFormat
);
265 const GLubyte
*in2rgba
= mappings
[inFmt
].to_rgba
;
266 const GLubyte
*rgba2out
= mappings
[outFmt
].from_rgba
;
269 for (i
= 0; i
< 4; i
++)
270 map
[i
] = in2rgba
[rgba2out
[i
]];
276 printf("from %x/%s to %x/%s map %d %d %d %d %d %d\n",
277 inFormat
, _mesa_lookup_enum_by_nr(inFormat
),
278 outFormat
, _mesa_lookup_enum_by_nr(outFormat
),
290 * Make a temporary (color) texture image with GLfloat components.
291 * Apply all needed pixel unpacking and pixel transfer operations.
292 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
293 * Suppose the user specifies GL_LUMINANCE as the internal texture format
294 * but the graphics hardware doesn't support luminance textures. So, we might
295 * use an RGB hardware format instead.
296 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
298 * \param ctx the rendering context
299 * \param dims image dimensions: 1, 2 or 3
300 * \param logicalBaseFormat basic texture derived from the user's
301 * internal texture format value
302 * \param textureBaseFormat the actual basic format of the texture
303 * \param srcWidth source image width
304 * \param srcHeight source image height
305 * \param srcDepth source image depth
306 * \param srcFormat source image format
307 * \param srcType source image type
308 * \param srcAddr source image address
309 * \param srcPacking source image pixel packing
310 * \return resulting image with format = textureBaseFormat and type = GLfloat.
313 _mesa_make_temp_float_image(struct gl_context
*ctx
, GLuint dims
,
314 GLenum logicalBaseFormat
,
315 GLenum textureBaseFormat
,
316 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
317 GLenum srcFormat
, GLenum srcType
,
318 const GLvoid
*srcAddr
,
319 const struct gl_pixelstore_attrib
*srcPacking
,
320 GLbitfield transferOps
)
323 const GLint components
= _mesa_components_in_format(logicalBaseFormat
);
324 const GLint srcStride
=
325 _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
329 ASSERT(dims
>= 1 && dims
<= 3);
331 ASSERT(logicalBaseFormat
== GL_RGBA
||
332 logicalBaseFormat
== GL_RGB
||
333 logicalBaseFormat
== GL_RG
||
334 logicalBaseFormat
== GL_RED
||
335 logicalBaseFormat
== GL_LUMINANCE_ALPHA
||
336 logicalBaseFormat
== GL_LUMINANCE
||
337 logicalBaseFormat
== GL_ALPHA
||
338 logicalBaseFormat
== GL_INTENSITY
||
339 logicalBaseFormat
== GL_DEPTH_COMPONENT
);
341 ASSERT(textureBaseFormat
== GL_RGBA
||
342 textureBaseFormat
== GL_RGB
||
343 textureBaseFormat
== GL_RG
||
344 textureBaseFormat
== GL_RED
||
345 textureBaseFormat
== GL_LUMINANCE_ALPHA
||
346 textureBaseFormat
== GL_LUMINANCE
||
347 textureBaseFormat
== GL_ALPHA
||
348 textureBaseFormat
== GL_INTENSITY
||
349 textureBaseFormat
== GL_DEPTH_COMPONENT
);
351 tempImage
= malloc(srcWidth
* srcHeight
* srcDepth
352 * components
* sizeof(GLfloat
));
357 for (img
= 0; img
< srcDepth
; img
++) {
359 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
363 for (row
= 0; row
< srcHeight
; row
++) {
364 _mesa_unpack_color_span_float(ctx
, srcWidth
, logicalBaseFormat
,
365 dst
, srcFormat
, srcType
, src
,
366 srcPacking
, transferOps
);
367 dst
+= srcWidth
* components
;
372 if (logicalBaseFormat
!= textureBaseFormat
) {
374 GLint texComponents
= _mesa_components_in_format(textureBaseFormat
);
375 GLint logComponents
= _mesa_components_in_format(logicalBaseFormat
);
380 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
381 ASSERT(textureBaseFormat
== GL_RGB
|| textureBaseFormat
== GL_RGBA
||
382 textureBaseFormat
== GL_LUMINANCE_ALPHA
);
384 /* The actual texture format should have at least as many components
385 * as the logical texture format.
387 ASSERT(texComponents
>= logComponents
);
389 newImage
= malloc(srcWidth
* srcHeight
* srcDepth
390 * texComponents
* sizeof(GLfloat
));
396 compute_component_mapping(logicalBaseFormat
, textureBaseFormat
, map
);
398 n
= srcWidth
* srcHeight
* srcDepth
;
399 for (i
= 0; i
< n
; i
++) {
401 for (k
= 0; k
< texComponents
; k
++) {
404 newImage
[i
* texComponents
+ k
] = 0.0F
;
406 newImage
[i
* texComponents
+ k
] = 1.0F
;
408 newImage
[i
* texComponents
+ k
] = tempImage
[i
* logComponents
+ j
];
413 tempImage
= newImage
;
421 * Make temporary image with uint pixel values. Used for unsigned
422 * integer-valued textures.
425 make_temp_uint_image(struct gl_context
*ctx
, GLuint dims
,
426 GLenum logicalBaseFormat
,
427 GLenum textureBaseFormat
,
428 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
429 GLenum srcFormat
, GLenum srcType
,
430 const GLvoid
*srcAddr
,
431 const struct gl_pixelstore_attrib
*srcPacking
)
434 const GLint components
= _mesa_components_in_format(logicalBaseFormat
);
435 const GLint srcStride
=
436 _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
440 ASSERT(dims
>= 1 && dims
<= 3);
442 ASSERT(logicalBaseFormat
== GL_RGBA
||
443 logicalBaseFormat
== GL_RGB
||
444 logicalBaseFormat
== GL_RG
||
445 logicalBaseFormat
== GL_RED
||
446 logicalBaseFormat
== GL_LUMINANCE_ALPHA
||
447 logicalBaseFormat
== GL_LUMINANCE
||
448 logicalBaseFormat
== GL_INTENSITY
||
449 logicalBaseFormat
== GL_ALPHA
);
451 ASSERT(textureBaseFormat
== GL_RGBA
||
452 textureBaseFormat
== GL_RGB
||
453 textureBaseFormat
== GL_RG
||
454 textureBaseFormat
== GL_RED
||
455 textureBaseFormat
== GL_LUMINANCE_ALPHA
||
456 textureBaseFormat
== GL_LUMINANCE
||
457 textureBaseFormat
== GL_INTENSITY
||
458 textureBaseFormat
== GL_ALPHA
);
460 tempImage
= malloc(srcWidth
* srcHeight
* srcDepth
461 * components
* sizeof(GLuint
));
466 for (img
= 0; img
< srcDepth
; img
++) {
468 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
472 for (row
= 0; row
< srcHeight
; row
++) {
473 _mesa_unpack_color_span_uint(ctx
, srcWidth
, logicalBaseFormat
,
474 dst
, srcFormat
, srcType
, src
,
476 dst
+= srcWidth
* components
;
481 if (logicalBaseFormat
!= textureBaseFormat
) {
483 GLint texComponents
= _mesa_components_in_format(textureBaseFormat
);
484 GLint logComponents
= _mesa_components_in_format(logicalBaseFormat
);
489 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
490 ASSERT(textureBaseFormat
== GL_RGB
|| textureBaseFormat
== GL_RGBA
||
491 textureBaseFormat
== GL_LUMINANCE_ALPHA
);
493 /* The actual texture format should have at least as many components
494 * as the logical texture format.
496 ASSERT(texComponents
>= logComponents
);
498 newImage
= malloc(srcWidth
* srcHeight
* srcDepth
499 * texComponents
* sizeof(GLuint
));
505 compute_component_mapping(logicalBaseFormat
, textureBaseFormat
, map
);
507 n
= srcWidth
* srcHeight
* srcDepth
;
508 for (i
= 0; i
< n
; i
++) {
510 for (k
= 0; k
< texComponents
; k
++) {
513 newImage
[i
* texComponents
+ k
] = 0;
515 newImage
[i
* texComponents
+ k
] = 1;
517 newImage
[i
* texComponents
+ k
] = tempImage
[i
* logComponents
+ j
];
522 tempImage
= newImage
;
531 * Make a temporary (color) texture image with GLubyte components.
532 * Apply all needed pixel unpacking and pixel transfer operations.
533 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
534 * Suppose the user specifies GL_LUMINANCE as the internal texture format
535 * but the graphics hardware doesn't support luminance textures. So, we might
536 * use an RGB hardware format instead.
537 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
539 * \param ctx the rendering context
540 * \param dims image dimensions: 1, 2 or 3
541 * \param logicalBaseFormat basic texture derived from the user's
542 * internal texture format value
543 * \param textureBaseFormat the actual basic format of the texture
544 * \param srcWidth source image width
545 * \param srcHeight source image height
546 * \param srcDepth source image depth
547 * \param srcFormat source image format
548 * \param srcType source image type
549 * \param srcAddr source image address
550 * \param srcPacking source image pixel packing
551 * \return resulting image with format = textureBaseFormat and type = GLubyte.
554 _mesa_make_temp_ubyte_image(struct gl_context
*ctx
, GLuint dims
,
555 GLenum logicalBaseFormat
,
556 GLenum textureBaseFormat
,
557 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
558 GLenum srcFormat
, GLenum srcType
,
559 const GLvoid
*srcAddr
,
560 const struct gl_pixelstore_attrib
*srcPacking
)
562 GLuint transferOps
= ctx
->_ImageTransferState
;
563 const GLint components
= _mesa_components_in_format(logicalBaseFormat
);
565 GLubyte
*tempImage
, *dst
;
567 ASSERT(dims
>= 1 && dims
<= 3);
569 ASSERT(logicalBaseFormat
== GL_RGBA
||
570 logicalBaseFormat
== GL_RGB
||
571 logicalBaseFormat
== GL_RG
||
572 logicalBaseFormat
== GL_RED
||
573 logicalBaseFormat
== GL_LUMINANCE_ALPHA
||
574 logicalBaseFormat
== GL_LUMINANCE
||
575 logicalBaseFormat
== GL_ALPHA
||
576 logicalBaseFormat
== GL_INTENSITY
);
578 ASSERT(textureBaseFormat
== GL_RGBA
||
579 textureBaseFormat
== GL_RGB
||
580 textureBaseFormat
== GL_RG
||
581 textureBaseFormat
== GL_RED
||
582 textureBaseFormat
== GL_LUMINANCE_ALPHA
||
583 textureBaseFormat
== GL_LUMINANCE
||
584 textureBaseFormat
== GL_ALPHA
||
585 textureBaseFormat
== GL_INTENSITY
);
587 /* unpack and transfer the source image */
588 tempImage
= malloc(srcWidth
* srcHeight
* srcDepth
589 * components
* sizeof(GLubyte
));
595 for (img
= 0; img
< srcDepth
; img
++) {
596 const GLint srcStride
=
597 _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
599 (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
603 for (row
= 0; row
< srcHeight
; row
++) {
604 _mesa_unpack_color_span_ubyte(ctx
, srcWidth
, logicalBaseFormat
, dst
,
605 srcFormat
, srcType
, src
, srcPacking
,
607 dst
+= srcWidth
* components
;
612 if (logicalBaseFormat
!= textureBaseFormat
) {
613 /* one more conversion step */
614 GLint texComponents
= _mesa_components_in_format(textureBaseFormat
);
615 GLint logComponents
= _mesa_components_in_format(logicalBaseFormat
);
620 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
621 ASSERT(textureBaseFormat
== GL_RGB
|| textureBaseFormat
== GL_RGBA
||
622 textureBaseFormat
== GL_LUMINANCE_ALPHA
);
624 /* The actual texture format should have at least as many components
625 * as the logical texture format.
627 ASSERT(texComponents
>= logComponents
);
629 newImage
= malloc(srcWidth
* srcHeight
* srcDepth
630 * texComponents
* sizeof(GLubyte
));
636 compute_component_mapping(logicalBaseFormat
, textureBaseFormat
, map
);
638 n
= srcWidth
* srcHeight
* srcDepth
;
639 for (i
= 0; i
< n
; i
++) {
641 for (k
= 0; k
< texComponents
; k
++) {
644 newImage
[i
* texComponents
+ k
] = 0;
646 newImage
[i
* texComponents
+ k
] = 255;
648 newImage
[i
* texComponents
+ k
] = tempImage
[i
* logComponents
+ j
];
653 tempImage
= newImage
;
660 static const GLubyte map_identity
[6] = { 0, 1, 2, 3, ZERO
, ONE
};
661 static const GLubyte map_3210
[6] = { 3, 2, 1, 0, ZERO
, ONE
};
662 static const GLubyte map_1032
[6] = { 1, 0, 3, 2, ZERO
, ONE
};
666 * Teximage storage routine for when a simple memcpy will do.
667 * No pixel transfer operations or special texel encodings allowed.
668 * 1D, 2D and 3D images supported.
671 memcpy_texture(struct gl_context
*ctx
,
673 mesa_format dstFormat
,
676 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
677 GLenum srcFormat
, GLenum srcType
,
678 const GLvoid
*srcAddr
,
679 const struct gl_pixelstore_attrib
*srcPacking
)
681 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
,
683 const GLint srcImageStride
= _mesa_image_image_stride(srcPacking
,
684 srcWidth
, srcHeight
, srcFormat
, srcType
);
685 const GLubyte
*srcImage
= (const GLubyte
*) _mesa_image_address(dimensions
,
686 srcPacking
, srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, 0, 0, 0);
687 const GLuint texelBytes
= _mesa_get_format_bytes(dstFormat
);
688 const GLint bytesPerRow
= srcWidth
* texelBytes
;
690 if (dstRowStride
== srcRowStride
&&
691 dstRowStride
== bytesPerRow
) {
692 /* memcpy image by image */
694 for (img
= 0; img
< srcDepth
; img
++) {
695 GLubyte
*dstImage
= dstSlices
[img
];
696 memcpy(dstImage
, srcImage
, bytesPerRow
* srcHeight
);
697 srcImage
+= srcImageStride
;
701 /* memcpy row by row */
703 for (img
= 0; img
< srcDepth
; img
++) {
704 const GLubyte
*srcRow
= srcImage
;
705 GLubyte
*dstRow
= dstSlices
[img
];
706 for (row
= 0; row
< srcHeight
; row
++) {
707 memcpy(dstRow
, srcRow
, bytesPerRow
);
708 dstRow
+= dstRowStride
;
709 srcRow
+= srcRowStride
;
711 srcImage
+= srcImageStride
;
718 * General-case function for storing a color texture images with
719 * components that can be represented with ubytes. Example destination
720 * texture formats are MESA_FORMAT_ARGB888, ARGB4444, RGB565.
723 store_ubyte_texture(TEXSTORE_PARAMS
)
725 const GLint srcRowStride
= srcWidth
* 4 * sizeof(GLubyte
);
726 GLubyte
*tempImage
, *src
;
729 tempImage
= _mesa_make_temp_ubyte_image(ctx
, dims
,
732 srcWidth
, srcHeight
, srcDepth
,
733 srcFormat
, srcType
, srcAddr
,
738 /* This way we will use the RGB versions of the packing functions and it
739 * will work for both RGB and sRGB textures*/
740 dstFormat
= _mesa_get_srgb_format_linear(dstFormat
);
743 for (img
= 0; img
< srcDepth
; img
++) {
744 _mesa_pack_ubyte_rgba_rect(dstFormat
, srcWidth
, srcHeight
,
746 dstSlices
[img
], dstRowStride
);
747 src
+= srcHeight
* srcRowStride
;
758 * Store a 32-bit integer or float depth component texture image.
761 _mesa_texstore_z32(TEXSTORE_PARAMS
)
763 const GLuint depthScale
= 0xffffffff;
766 ASSERT(dstFormat
== MESA_FORMAT_Z_UNORM32
||
767 dstFormat
== MESA_FORMAT_Z_FLOAT32
);
768 ASSERT(_mesa_get_format_bytes(dstFormat
) == sizeof(GLuint
));
770 if (dstFormat
== MESA_FORMAT_Z_UNORM32
)
771 dstType
= GL_UNSIGNED_INT
;
778 for (img
= 0; img
< srcDepth
; img
++) {
779 GLubyte
*dstRow
= dstSlices
[img
];
780 for (row
= 0; row
< srcHeight
; row
++) {
781 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
782 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
783 _mesa_unpack_depth_span(ctx
, srcWidth
,
785 depthScale
, srcType
, src
, srcPacking
);
786 dstRow
+= dstRowStride
;
795 * Store a 24-bit integer depth component texture image.
798 _mesa_texstore_x8_z24(TEXSTORE_PARAMS
)
800 const GLuint depthScale
= 0xffffff;
803 ASSERT(dstFormat
== MESA_FORMAT_Z24_UNORM_X8_UINT
);
808 for (img
= 0; img
< srcDepth
; img
++) {
809 GLubyte
*dstRow
= dstSlices
[img
];
810 for (row
= 0; row
< srcHeight
; row
++) {
811 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
812 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
813 _mesa_unpack_depth_span(ctx
, srcWidth
,
814 GL_UNSIGNED_INT
, (GLuint
*) dstRow
,
815 depthScale
, srcType
, src
, srcPacking
);
816 dstRow
+= dstRowStride
;
825 * Store a 24-bit integer depth component texture image.
828 _mesa_texstore_z24_x8(TEXSTORE_PARAMS
)
830 const GLuint depthScale
= 0xffffff;
833 ASSERT(dstFormat
== MESA_FORMAT_X8_UINT_Z24_UNORM
);
838 for (img
= 0; img
< srcDepth
; img
++) {
839 GLubyte
*dstRow
= dstSlices
[img
];
840 for (row
= 0; row
< srcHeight
; row
++) {
841 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
842 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
843 GLuint
*dst
= (GLuint
*) dstRow
;
845 _mesa_unpack_depth_span(ctx
, srcWidth
,
846 GL_UNSIGNED_INT
, dst
,
847 depthScale
, srcType
, src
, srcPacking
);
848 for (i
= 0; i
< srcWidth
; i
++)
850 dstRow
+= dstRowStride
;
859 * Store a 16-bit integer depth component texture image.
862 _mesa_texstore_z16(TEXSTORE_PARAMS
)
864 const GLuint depthScale
= 0xffff;
866 ASSERT(dstFormat
== MESA_FORMAT_Z_UNORM16
);
867 ASSERT(_mesa_get_format_bytes(dstFormat
) == sizeof(GLushort
));
872 for (img
= 0; img
< srcDepth
; img
++) {
873 GLubyte
*dstRow
= dstSlices
[img
];
874 for (row
= 0; row
< srcHeight
; row
++) {
875 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
876 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
877 GLushort
*dst16
= (GLushort
*) dstRow
;
878 _mesa_unpack_depth_span(ctx
, srcWidth
,
879 GL_UNSIGNED_SHORT
, dst16
, depthScale
,
880 srcType
, src
, srcPacking
);
881 dstRow
+= dstRowStride
;
890 * Store an rgb565 or rgb565_rev texture image.
893 _mesa_texstore_rgb565(TEXSTORE_PARAMS
)
895 ASSERT(dstFormat
== MESA_FORMAT_B5G6R5_UNORM
||
896 dstFormat
== MESA_FORMAT_R5G6B5_UNORM
);
897 ASSERT(_mesa_get_format_bytes(dstFormat
) == 2);
899 if (!ctx
->_ImageTransferState
&&
900 !srcPacking
->SwapBytes
&&
901 baseInternalFormat
== GL_RGB
&&
902 srcFormat
== GL_RGB
&&
903 srcType
== GL_UNSIGNED_BYTE
&&
905 /* do optimized tex store */
906 const GLint srcRowStride
=
907 _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
908 const GLubyte
*src
= (const GLubyte
*)
909 _mesa_image_address(dims
, srcPacking
, srcAddr
, srcWidth
, srcHeight
,
910 srcFormat
, srcType
, 0, 0, 0);
911 GLubyte
*dst
= dstSlices
[0];
913 for (row
= 0; row
< srcHeight
; row
++) {
914 const GLubyte
*srcUB
= (const GLubyte
*) src
;
915 GLushort
*dstUS
= (GLushort
*) dst
;
916 /* check for byteswapped format */
917 if (dstFormat
== MESA_FORMAT_B5G6R5_UNORM
) {
918 for (col
= 0; col
< srcWidth
; col
++) {
919 dstUS
[col
] = PACK_COLOR_565( srcUB
[0], srcUB
[1], srcUB
[2] );
924 for (col
= 0; col
< srcWidth
; col
++) {
925 dstUS
[col
] = PACK_COLOR_565_REV( srcUB
[0], srcUB
[1], srcUB
[2] );
940 * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_REV.
943 _mesa_texstore_ycbcr(TEXSTORE_PARAMS
)
945 const GLboolean littleEndian
= _mesa_little_endian();
947 (void) ctx
; (void) dims
; (void) baseInternalFormat
;
949 ASSERT((dstFormat
== MESA_FORMAT_YCBCR
) ||
950 (dstFormat
== MESA_FORMAT_YCBCR_REV
));
951 ASSERT(_mesa_get_format_bytes(dstFormat
) == 2);
952 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
953 ASSERT(srcFormat
== GL_YCBCR_MESA
);
954 ASSERT((srcType
== GL_UNSIGNED_SHORT_8_8_MESA
) ||
955 (srcType
== GL_UNSIGNED_SHORT_8_8_REV_MESA
));
956 ASSERT(baseInternalFormat
== GL_YCBCR_MESA
);
958 /* always just memcpy since no pixel transfer ops apply */
959 memcpy_texture(ctx
, dims
,
961 dstRowStride
, dstSlices
,
962 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
963 srcAddr
, srcPacking
);
965 /* Check if we need byte swapping */
966 /* XXX the logic here _might_ be wrong */
967 if (srcPacking
->SwapBytes
^
968 (srcType
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ^
969 (dstFormat
== MESA_FORMAT_YCBCR_REV
) ^
972 for (img
= 0; img
< srcDepth
; img
++) {
973 GLubyte
*dstRow
= dstSlices
[img
];
974 for (row
= 0; row
< srcHeight
; row
++) {
975 _mesa_swap2((GLushort
*) dstRow
, srcWidth
);
976 dstRow
+= dstRowStride
;
985 * Store a combined depth/stencil texture image.
988 _mesa_texstore_z24_s8(TEXSTORE_PARAMS
)
990 const GLuint depthScale
= 0xffffff;
991 const GLint srcRowStride
992 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
994 GLuint
*depth
= malloc(srcWidth
* sizeof(GLuint
));
995 GLubyte
*stencil
= malloc(srcWidth
* sizeof(GLubyte
));
997 ASSERT(dstFormat
== MESA_FORMAT_S8_UINT_Z24_UNORM
);
998 ASSERT(srcFormat
== GL_DEPTH_STENCIL_EXT
||
999 srcFormat
== GL_DEPTH_COMPONENT
||
1000 srcFormat
== GL_STENCIL_INDEX
);
1001 ASSERT(srcFormat
!= GL_DEPTH_STENCIL_EXT
||
1002 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
1003 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1005 if (!depth
|| !stencil
) {
1011 /* In case we only upload depth we need to preserve the stencil */
1012 for (img
= 0; img
< srcDepth
; img
++) {
1013 GLuint
*dstRow
= (GLuint
*) dstSlices
[img
];
1015 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
1016 srcWidth
, srcHeight
,
1019 for (row
= 0; row
< srcHeight
; row
++) {
1021 GLboolean keepdepth
= GL_FALSE
, keepstencil
= GL_FALSE
;
1023 if (srcFormat
== GL_DEPTH_COMPONENT
) { /* preserve stencil */
1024 keepstencil
= GL_TRUE
;
1026 else if (srcFormat
== GL_STENCIL_INDEX
) { /* preserve depth */
1027 keepdepth
= GL_TRUE
;
1030 if (keepdepth
== GL_FALSE
)
1031 /* the 24 depth bits will be in the low position: */
1032 _mesa_unpack_depth_span(ctx
, srcWidth
,
1033 GL_UNSIGNED_INT
, /* dst type */
1034 keepstencil
? depth
: dstRow
, /* dst addr */
1036 srcType
, src
, srcPacking
);
1038 if (keepstencil
== GL_FALSE
)
1039 /* get the 8-bit stencil values */
1040 _mesa_unpack_stencil_span(ctx
, srcWidth
,
1041 GL_UNSIGNED_BYTE
, /* dst type */
1042 stencil
, /* dst addr */
1043 srcType
, src
, srcPacking
,
1044 ctx
->_ImageTransferState
);
1046 for (i
= 0; i
< srcWidth
; i
++) {
1048 dstRow
[i
] = depth
[i
] << 8 | (dstRow
[i
] & 0x000000FF);
1050 dstRow
[i
] = (dstRow
[i
] & 0xFFFFFF00) | (stencil
[i
] & 0xFF);
1052 src
+= srcRowStride
;
1053 dstRow
+= dstRowStride
/ sizeof(GLuint
);
1064 * Store a combined depth/stencil texture image.
1067 _mesa_texstore_s8_z24(TEXSTORE_PARAMS
)
1069 const GLuint depthScale
= 0xffffff;
1070 const GLint srcRowStride
1071 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
1076 ASSERT(dstFormat
== MESA_FORMAT_Z24_UNORM_S8_UINT
);
1077 ASSERT(srcFormat
== GL_DEPTH_STENCIL_EXT
||
1078 srcFormat
== GL_DEPTH_COMPONENT
||
1079 srcFormat
== GL_STENCIL_INDEX
);
1080 ASSERT(srcFormat
!= GL_DEPTH_STENCIL_EXT
||
1081 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
1082 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1084 depth
= malloc(srcWidth
* sizeof(GLuint
));
1085 stencil
= malloc(srcWidth
* sizeof(GLubyte
));
1087 if (!depth
|| !stencil
) {
1093 for (img
= 0; img
< srcDepth
; img
++) {
1094 GLuint
*dstRow
= (GLuint
*) dstSlices
[img
];
1096 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
1097 srcWidth
, srcHeight
,
1100 for (row
= 0; row
< srcHeight
; row
++) {
1102 GLboolean keepdepth
= GL_FALSE
, keepstencil
= GL_FALSE
;
1104 if (srcFormat
== GL_DEPTH_COMPONENT
) { /* preserve stencil */
1105 keepstencil
= GL_TRUE
;
1107 else if (srcFormat
== GL_STENCIL_INDEX
) { /* preserve depth */
1108 keepdepth
= GL_TRUE
;
1111 if (keepdepth
== GL_FALSE
)
1112 /* the 24 depth bits will be in the low position: */
1113 _mesa_unpack_depth_span(ctx
, srcWidth
,
1114 GL_UNSIGNED_INT
, /* dst type */
1115 keepstencil
? depth
: dstRow
, /* dst addr */
1117 srcType
, src
, srcPacking
);
1119 if (keepstencil
== GL_FALSE
)
1120 /* get the 8-bit stencil values */
1121 _mesa_unpack_stencil_span(ctx
, srcWidth
,
1122 GL_UNSIGNED_BYTE
, /* dst type */
1123 stencil
, /* dst addr */
1124 srcType
, src
, srcPacking
,
1125 ctx
->_ImageTransferState
);
1127 /* merge stencil values into depth values */
1128 for (i
= 0; i
< srcWidth
; i
++) {
1130 dstRow
[i
] = depth
[i
] | (dstRow
[i
] & 0xFF000000);
1132 dstRow
[i
] = (dstRow
[i
] & 0xFFFFFF) | (stencil
[i
] << 24);
1135 src
+= srcRowStride
;
1136 dstRow
+= dstRowStride
/ sizeof(GLuint
);
1148 * Store simple 8-bit/value stencil texture data.
1151 _mesa_texstore_s8(TEXSTORE_PARAMS
)
1153 ASSERT(dstFormat
== MESA_FORMAT_S_UINT8
);
1154 ASSERT(srcFormat
== GL_STENCIL_INDEX
);
1157 const GLint srcRowStride
1158 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
1160 GLubyte
*stencil
= malloc(srcWidth
* sizeof(GLubyte
));
1165 for (img
= 0; img
< srcDepth
; img
++) {
1166 GLubyte
*dstRow
= dstSlices
[img
];
1168 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
1169 srcWidth
, srcHeight
,
1172 for (row
= 0; row
< srcHeight
; row
++) {
1175 /* get the 8-bit stencil values */
1176 _mesa_unpack_stencil_span(ctx
, srcWidth
,
1177 GL_UNSIGNED_BYTE
, /* dst type */
1178 stencil
, /* dst addr */
1179 srcType
, src
, srcPacking
,
1180 ctx
->_ImageTransferState
);
1181 /* merge stencil values into depth values */
1182 for (i
= 0; i
< srcWidth
; i
++)
1183 dstRow
[i
] = stencil
[i
];
1185 src
+= srcRowStride
;
1186 dstRow
+= dstRowStride
/ sizeof(GLubyte
);
1198 _mesa_texstore_z32f_x24s8(TEXSTORE_PARAMS
)
1201 const GLint srcRowStride
1202 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
)
1205 ASSERT(dstFormat
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
);
1206 ASSERT(srcFormat
== GL_DEPTH_STENCIL
||
1207 srcFormat
== GL_DEPTH_COMPONENT
||
1208 srcFormat
== GL_STENCIL_INDEX
);
1209 ASSERT(srcFormat
!= GL_DEPTH_STENCIL
||
1210 srcType
== GL_UNSIGNED_INT_24_8
||
1211 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1213 /* In case we only upload depth we need to preserve the stencil */
1214 for (img
= 0; img
< srcDepth
; img
++) {
1215 uint64_t *dstRow
= (uint64_t *) dstSlices
[img
];
1217 = (const uint64_t *) _mesa_image_address(dims
, srcPacking
, srcAddr
,
1218 srcWidth
, srcHeight
,
1221 for (row
= 0; row
< srcHeight
; row
++) {
1222 /* The unpack functions with:
1223 * dstType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV
1224 * only write their own dword, so the other dword (stencil
1225 * or depth) is preserved. */
1226 if (srcFormat
!= GL_STENCIL_INDEX
)
1227 _mesa_unpack_depth_span(ctx
, srcWidth
,
1228 GL_FLOAT_32_UNSIGNED_INT_24_8_REV
, /* dst type */
1229 dstRow
, /* dst addr */
1230 ~0U, srcType
, src
, srcPacking
);
1232 if (srcFormat
!= GL_DEPTH_COMPONENT
)
1233 _mesa_unpack_stencil_span(ctx
, srcWidth
,
1234 GL_FLOAT_32_UNSIGNED_INT_24_8_REV
, /* dst type */
1235 dstRow
, /* dst addr */
1236 srcType
, src
, srcPacking
,
1237 ctx
->_ImageTransferState
);
1239 src
+= srcRowStride
;
1240 dstRow
+= dstRowStride
/ sizeof(uint64_t);
1247 _mesa_texstore_argb2101010_uint(TEXSTORE_PARAMS
)
1249 const GLenum baseFormat
= _mesa_get_format_base_format(dstFormat
);
1251 ASSERT(dstFormat
== MESA_FORMAT_B10G10R10A2_UINT
);
1252 ASSERT(_mesa_get_format_bytes(dstFormat
) == 4);
1256 const GLuint
*tempImage
= make_temp_uint_image(ctx
, dims
,
1259 srcWidth
, srcHeight
,
1260 srcDepth
, srcFormat
,
1263 const GLuint
*src
= tempImage
;
1264 GLint img
, row
, col
;
1265 GLboolean is_unsigned
= _mesa_is_type_unsigned(srcType
);
1268 for (img
= 0; img
< srcDepth
; img
++) {
1269 GLubyte
*dstRow
= dstSlices
[img
];
1271 for (row
= 0; row
< srcHeight
; row
++) {
1272 GLuint
*dstUI
= (GLuint
*) dstRow
;
1274 for (col
= 0; col
< srcWidth
; col
++) {
1276 r
= MIN2(src
[RCOMP
], 0x3ff);
1277 g
= MIN2(src
[GCOMP
], 0x3ff);
1278 b
= MIN2(src
[BCOMP
], 0x3ff);
1279 a
= MIN2(src
[ACOMP
], 0x003);
1280 dstUI
[col
] = (a
<< 30) | (r
<< 20) | (g
<< 10) | (b
);
1284 for (col
= 0; col
< srcWidth
; col
++) {
1286 r
= CLAMP((GLint
) src
[RCOMP
], 0, 0x3ff);
1287 g
= CLAMP((GLint
) src
[GCOMP
], 0, 0x3ff);
1288 b
= CLAMP((GLint
) src
[BCOMP
], 0, 0x3ff);
1289 a
= CLAMP((GLint
) src
[ACOMP
], 0, 0x003);
1290 dstUI
[col
] = (a
<< 30) | (r
<< 20) | (g
<< 10) | (b
);
1294 dstRow
+= dstRowStride
;
1297 free((void *) tempImage
);
1303 _mesa_texstore_abgr2101010_uint(TEXSTORE_PARAMS
)
1305 const GLenum baseFormat
= _mesa_get_format_base_format(dstFormat
);
1307 ASSERT(dstFormat
== MESA_FORMAT_R10G10B10A2_UINT
);
1308 ASSERT(_mesa_get_format_bytes(dstFormat
) == 4);
1312 const GLuint
*tempImage
= make_temp_uint_image(ctx
, dims
,
1315 srcWidth
, srcHeight
,
1316 srcDepth
, srcFormat
,
1319 const GLuint
*src
= tempImage
;
1320 GLint img
, row
, col
;
1321 GLboolean is_unsigned
= _mesa_is_type_unsigned(srcType
);
1324 for (img
= 0; img
< srcDepth
; img
++) {
1325 GLubyte
*dstRow
= dstSlices
[img
];
1327 for (row
= 0; row
< srcHeight
; row
++) {
1328 GLuint
*dstUI
= (GLuint
*) dstRow
;
1330 for (col
= 0; col
< srcWidth
; col
++) {
1332 r
= MIN2(src
[RCOMP
], 0x3ff);
1333 g
= MIN2(src
[GCOMP
], 0x3ff);
1334 b
= MIN2(src
[BCOMP
], 0x3ff);
1335 a
= MIN2(src
[ACOMP
], 0x003);
1336 dstUI
[col
] = (a
<< 30) | (b
<< 20) | (g
<< 10) | (r
);
1340 for (col
= 0; col
< srcWidth
; col
++) {
1342 r
= CLAMP((GLint
) src
[RCOMP
], 0, 0x3ff);
1343 g
= CLAMP((GLint
) src
[GCOMP
], 0, 0x3ff);
1344 b
= CLAMP((GLint
) src
[BCOMP
], 0, 0x3ff);
1345 a
= CLAMP((GLint
) src
[ACOMP
], 0, 0x003);
1346 dstUI
[col
] = (a
<< 30) | (b
<< 20) | (g
<< 10) | (r
);
1350 dstRow
+= dstRowStride
;
1353 free((void *) tempImage
);
1360 texstore_depth_stencil(TEXSTORE_PARAMS
)
1362 static StoreTexImageFunc table
[MESA_FORMAT_COUNT
];
1363 static GLboolean initialized
= GL_FALSE
;
1366 memset(table
, 0, sizeof table
);
1368 table
[MESA_FORMAT_S8_UINT_Z24_UNORM
] = _mesa_texstore_z24_s8
;
1369 table
[MESA_FORMAT_Z24_UNORM_S8_UINT
] = _mesa_texstore_s8_z24
;
1370 table
[MESA_FORMAT_Z_UNORM16
] = _mesa_texstore_z16
;
1371 table
[MESA_FORMAT_Z24_UNORM_X8_UINT
] = _mesa_texstore_x8_z24
;
1372 table
[MESA_FORMAT_X8_UINT_Z24_UNORM
] = _mesa_texstore_z24_x8
;
1373 table
[MESA_FORMAT_Z_UNORM32
] = _mesa_texstore_z32
;
1374 table
[MESA_FORMAT_S_UINT8
] = _mesa_texstore_s8
;
1375 table
[MESA_FORMAT_Z_FLOAT32
] = _mesa_texstore_z32
;
1376 table
[MESA_FORMAT_Z32_FLOAT_S8X24_UINT
] = _mesa_texstore_z32f_x24s8
;
1378 initialized
= GL_TRUE
;
1381 ASSERT(table
[dstFormat
]);
1382 return table
[dstFormat
](ctx
, dims
, baseInternalFormat
,
1383 dstFormat
, dstRowStride
, dstSlices
,
1384 srcWidth
, srcHeight
, srcDepth
,
1385 srcFormat
, srcType
, srcAddr
, srcPacking
);
1389 texstore_compressed(TEXSTORE_PARAMS
)
1391 static StoreTexImageFunc table
[MESA_FORMAT_COUNT
];
1392 static GLboolean initialized
= GL_FALSE
;
1395 memset(table
, 0, sizeof table
);
1397 table
[MESA_FORMAT_SRGB_DXT1
] = _mesa_texstore_rgb_dxt1
;
1398 table
[MESA_FORMAT_SRGBA_DXT1
] = _mesa_texstore_rgba_dxt1
;
1399 table
[MESA_FORMAT_SRGBA_DXT3
] = _mesa_texstore_rgba_dxt3
;
1400 table
[MESA_FORMAT_SRGBA_DXT5
] = _mesa_texstore_rgba_dxt5
;
1401 table
[MESA_FORMAT_RGB_FXT1
] = _mesa_texstore_rgb_fxt1
;
1402 table
[MESA_FORMAT_RGBA_FXT1
] = _mesa_texstore_rgba_fxt1
;
1403 table
[MESA_FORMAT_RGB_DXT1
] = _mesa_texstore_rgb_dxt1
;
1404 table
[MESA_FORMAT_RGBA_DXT1
] = _mesa_texstore_rgba_dxt1
;
1405 table
[MESA_FORMAT_RGBA_DXT3
] = _mesa_texstore_rgba_dxt3
;
1406 table
[MESA_FORMAT_RGBA_DXT5
] = _mesa_texstore_rgba_dxt5
;
1407 table
[MESA_FORMAT_R_RGTC1_UNORM
] = _mesa_texstore_red_rgtc1
;
1408 table
[MESA_FORMAT_R_RGTC1_SNORM
] = _mesa_texstore_signed_red_rgtc1
;
1409 table
[MESA_FORMAT_RG_RGTC2_UNORM
] = _mesa_texstore_rg_rgtc2
;
1410 table
[MESA_FORMAT_RG_RGTC2_SNORM
] = _mesa_texstore_signed_rg_rgtc2
;
1411 table
[MESA_FORMAT_L_LATC1_UNORM
] = _mesa_texstore_red_rgtc1
;
1412 table
[MESA_FORMAT_L_LATC1_SNORM
] = _mesa_texstore_signed_red_rgtc1
;
1413 table
[MESA_FORMAT_LA_LATC2_UNORM
] = _mesa_texstore_rg_rgtc2
;
1414 table
[MESA_FORMAT_LA_LATC2_SNORM
] = _mesa_texstore_signed_rg_rgtc2
;
1415 table
[MESA_FORMAT_ETC1_RGB8
] = _mesa_texstore_etc1_rgb8
;
1416 table
[MESA_FORMAT_ETC2_RGB8
] = _mesa_texstore_etc2_rgb8
;
1417 table
[MESA_FORMAT_ETC2_SRGB8
] = _mesa_texstore_etc2_srgb8
;
1418 table
[MESA_FORMAT_ETC2_RGBA8_EAC
] = _mesa_texstore_etc2_rgba8_eac
;
1419 table
[MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
] = _mesa_texstore_etc2_srgb8_alpha8_eac
;
1420 table
[MESA_FORMAT_ETC2_R11_EAC
] = _mesa_texstore_etc2_r11_eac
;
1421 table
[MESA_FORMAT_ETC2_RG11_EAC
] = _mesa_texstore_etc2_rg11_eac
;
1422 table
[MESA_FORMAT_ETC2_SIGNED_R11_EAC
] = _mesa_texstore_etc2_signed_r11_eac
;
1423 table
[MESA_FORMAT_ETC2_SIGNED_RG11_EAC
] = _mesa_texstore_etc2_signed_rg11_eac
;
1424 table
[MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
] =
1425 _mesa_texstore_etc2_rgb8_punchthrough_alpha1
;
1426 table
[MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
] =
1427 _mesa_texstore_etc2_srgb8_punchthrough_alpha1
;
1429 initialized
= GL_TRUE
;
1432 ASSERT(table
[dstFormat
]);
1433 return table
[dstFormat
](ctx
, dims
, baseInternalFormat
,
1434 dstFormat
, dstRowStride
, dstSlices
,
1435 srcWidth
, srcHeight
, srcDepth
,
1436 srcFormat
, srcType
, srcAddr
, srcPacking
);
1440 invert_swizzle(uint8_t dst
[4], const uint8_t src
[4])
1444 dst
[0] = MESA_FORMAT_SWIZZLE_NONE
;
1445 dst
[1] = MESA_FORMAT_SWIZZLE_NONE
;
1446 dst
[2] = MESA_FORMAT_SWIZZLE_NONE
;
1447 dst
[3] = MESA_FORMAT_SWIZZLE_NONE
;
1449 for (i
= 0; i
< 4; ++i
)
1450 for (j
= 0; j
< 4; ++j
)
1451 if (src
[j
] == i
&& dst
[i
] == MESA_FORMAT_SWIZZLE_NONE
)
1455 /** Store a texture by per-channel conversions and swizzling.
1457 * This function attempts to perform a texstore operation by doing simple
1458 * per-channel conversions and swizzling. This covers a huge chunk of the
1459 * texture storage operations that anyone cares about. If this function is
1460 * incapable of performing the operation, it bails and returns GL_FALSE.
1463 texstore_swizzle(TEXSTORE_PARAMS
)
1465 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
,
1466 srcFormat
, srcType
);
1467 const GLint srcImageStride
= _mesa_image_image_stride(srcPacking
,
1468 srcWidth
, srcHeight
, srcFormat
, srcType
);
1469 const GLubyte
*srcImage
= (const GLubyte
*) _mesa_image_address(dims
,
1470 srcPacking
, srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, 0, 0, 0);
1471 const int src_components
= _mesa_components_in_format(srcFormat
);
1473 GLubyte swizzle
[4], rgba2base
[6], base2src
[6], rgba2dst
[4], dst2rgba
[4];
1474 const GLubyte
*swap
;
1477 bool is_array
, normalized
, need_swap
;
1479 const GLubyte
*src_row
;
1482 is_array
= _mesa_format_to_array(dstFormat
, &dst_type
, &dst_components
,
1483 rgba2dst
, &normalized
);
1490 case GL_UNSIGNED_BYTE
:
1492 case GL_UNSIGNED_SHORT
:
1494 case GL_UNSIGNED_INT
:
1496 /* If wa have to swap bytes in a multi-byte datatype, that means
1497 * we're not doing an array conversion anymore */
1498 if (srcPacking
->SwapBytes
)
1502 case GL_UNSIGNED_INT_8_8_8_8
:
1503 need_swap
= srcPacking
->SwapBytes
;
1504 if (_mesa_little_endian())
1505 need_swap
= !need_swap
;
1506 srcType
= GL_UNSIGNED_BYTE
;
1508 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1509 need_swap
= srcPacking
->SwapBytes
;
1510 if (!_mesa_little_endian())
1511 need_swap
= !need_swap
;
1512 srcType
= GL_UNSIGNED_BYTE
;
1517 swap
= need_swap
? map_3210
: map_identity
;
1519 compute_component_mapping(srcFormat
, baseInternalFormat
, base2src
);
1520 compute_component_mapping(baseInternalFormat
, GL_RGBA
, rgba2base
);
1521 invert_swizzle(dst2rgba
, rgba2dst
);
1523 for (i
= 0; i
< 4; i
++) {
1524 if (dst2rgba
[i
] == MESA_FORMAT_SWIZZLE_NONE
)
1525 swizzle
[i
] = MESA_FORMAT_SWIZZLE_NONE
;
1527 swizzle
[i
] = swap
[base2src
[rgba2base
[dst2rgba
[i
]]]];
1530 /* Is it normalized? */
1531 normalized
|= !_mesa_is_enum_format_integer(srcFormat
);
1533 for (img
= 0; img
< srcDepth
; img
++) {
1534 if (dstRowStride
== srcWidth
* dst_components
&&
1535 srcRowStride
== srcWidth
* src_components
) {
1536 _mesa_swizzle_and_convert(dstSlices
[img
], dst_type
, dst_components
,
1537 srcImage
, srcType
, src_components
,
1538 swizzle
, normalized
, srcWidth
* srcHeight
);
1541 dst_row
= dstSlices
[img
];
1542 for (row
= 0; row
< srcHeight
; row
++) {
1543 _mesa_swizzle_and_convert(dst_row
, dst_type
, dst_components
,
1544 src_row
, srcType
, src_components
,
1545 swizzle
, normalized
, srcWidth
);
1546 dst_row
+= dstRowStride
;
1547 src_row
+= srcRowStride
;
1550 srcImage
+= srcImageStride
;
1557 /** Stores a texture by converting float and then to the texture format
1559 * This function performs a texstore operation by converting to float,
1560 * applying pixel transfer ops, and then converting to the texture's
1561 * internal format using pixel store functions. This function will work
1562 * for any rgb or srgb textore format.
1565 texstore_via_float(TEXSTORE_PARAMS
)
1568 const GLint src_stride
=
1569 _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
1572 uint8_t *src_row
, *dst_row
, map
[4], rgba2base
[6], base2rgba
[6];
1574 tmp_row
= malloc(srcWidth
* 4 * sizeof(*tmp_row
));
1578 /* The GL spec (4.0, compatibility profile) only specifies srgb
1579 * conversion as something that is done in the sampler during the
1580 * filtering process before the colors are handed to the shader.
1581 * Furthermore, the flowchart (Figure 3.7 in the 4.0 compatibility spec)
1582 * does not list RGB <-> sRGB conversions anywhere. Therefore, we just
1583 * treat sRGB formats the same as RGB formats for the purposes of
1584 * texture upload and transfer ops.
1586 dstFormat
= _mesa_get_srgb_format_linear(dstFormat
);
1588 need_convert
= false;
1589 if (baseInternalFormat
!= _mesa_get_format_base_format(dstFormat
)) {
1590 compute_component_mapping(GL_RGBA
, baseInternalFormat
, base2rgba
);
1591 compute_component_mapping(baseInternalFormat
, GL_RGBA
, rgba2base
);
1592 for (i
= 0; i
< 4; ++i
) {
1593 map
[i
] = base2rgba
[rgba2base
[i
]];
1595 need_convert
= true;
1599 for (img
= 0; img
< srcDepth
; img
++) {
1600 dst_row
= dstSlices
[img
];
1601 src_row
= _mesa_image_address(dims
, srcPacking
, srcAddr
,
1602 srcWidth
, srcHeight
,
1605 for (row
= 0; row
< srcHeight
; row
++) {
1606 _mesa_unpack_color_span_float(ctx
, srcWidth
, GL_RGBA
, tmp_row
,
1607 srcFormat
, srcType
, src_row
,
1608 srcPacking
, ctx
->_ImageTransferState
);
1610 _mesa_swizzle_and_convert(tmp_row
, GL_FLOAT
, 4,
1611 tmp_row
, GL_FLOAT
, 4,
1612 map
, false, srcWidth
);
1613 _mesa_pack_float_rgba_row(dstFormat
, srcWidth
,
1614 (const GLfloat (*)[4])tmp_row
,
1616 dst_row
+= dstRowStride
;
1617 src_row
+= src_stride
;
1624 /** Stores an integer rgba texture
1626 * This function performs an integer texture storage operation by unpacking
1627 * the texture to 32-bit integers, and repacking it into the internal
1628 * format of the texture. This will work for any integer rgb texture
1629 * storage operation.
1632 texstore_rgba_integer(TEXSTORE_PARAMS
)
1634 GLuint i
, img
, row
, *tmp_row
;
1635 GLenum dst_type
, tmp_type
;
1636 const GLint src_stride
=
1637 _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
1638 int num_dst_components
;
1639 bool is_array
, normalized
;
1640 uint8_t *src_row
, *dst_row
;
1641 uint8_t swizzle
[4], rgba2base
[6], base2rgba
[6], rgba2dst
[4], dst2rgba
[4];
1643 tmp_row
= malloc(srcWidth
* 4 * sizeof(*tmp_row
));
1647 is_array
= _mesa_format_to_array(dstFormat
, &dst_type
, &num_dst_components
,
1648 rgba2dst
, &normalized
);
1650 assert(is_array
&& !normalized
);
1655 invert_swizzle(dst2rgba
, rgba2dst
);
1656 compute_component_mapping(GL_RGBA
, baseInternalFormat
, base2rgba
);
1657 compute_component_mapping(baseInternalFormat
, GL_RGBA
, rgba2base
);
1659 for (i
= 0; i
< 4; ++i
) {
1660 if (dst2rgba
[i
] == MESA_FORMAT_SWIZZLE_NONE
)
1661 swizzle
[i
] = MESA_FORMAT_SWIZZLE_NONE
;
1663 swizzle
[i
] = base2rgba
[rgba2base
[dst2rgba
[i
]]];
1666 if (_mesa_is_type_unsigned(srcType
)) {
1667 tmp_type
= GL_UNSIGNED_INT
;
1672 for (img
= 0; img
< srcDepth
; img
++) {
1673 dst_row
= dstSlices
[img
];
1674 src_row
= _mesa_image_address(dims
, srcPacking
, srcAddr
,
1675 srcWidth
, srcHeight
,
1678 for (row
= 0; row
< srcHeight
; row
++) {
1679 _mesa_unpack_color_span_uint(ctx
, srcWidth
, GL_RGBA
, tmp_row
,
1680 srcFormat
, srcType
, src_row
, srcPacking
);
1681 _mesa_swizzle_and_convert(dst_row
, dst_type
, num_dst_components
,
1682 tmp_row
, tmp_type
, 4,
1683 swizzle
, false, srcWidth
);
1684 dst_row
+= dstRowStride
;
1685 src_row
+= src_stride
;
1693 texstore_rgba(TEXSTORE_PARAMS
)
1695 static StoreTexImageFunc table
[MESA_FORMAT_COUNT
];
1696 static GLboolean initialized
= GL_FALSE
;
1699 memset(table
, 0, sizeof table
);
1701 table
[MESA_FORMAT_B5G6R5_UNORM
] = _mesa_texstore_rgb565
;
1702 table
[MESA_FORMAT_R5G6B5_UNORM
] = _mesa_texstore_rgb565
;
1703 table
[MESA_FORMAT_YCBCR
] = _mesa_texstore_ycbcr
;
1704 table
[MESA_FORMAT_YCBCR_REV
] = _mesa_texstore_ycbcr
;
1706 table
[MESA_FORMAT_B10G10R10A2_UINT
] = _mesa_texstore_argb2101010_uint
;
1707 table
[MESA_FORMAT_R10G10B10A2_UINT
] = _mesa_texstore_abgr2101010_uint
;
1709 initialized
= GL_TRUE
;
1712 if (table
[dstFormat
] && table
[dstFormat
](ctx
, dims
, baseInternalFormat
,
1713 dstFormat
, dstRowStride
, dstSlices
,
1714 srcWidth
, srcHeight
, srcDepth
,
1715 srcFormat
, srcType
, srcAddr
,
1720 if (texstore_swizzle(ctx
, dims
, baseInternalFormat
,
1722 dstRowStride
, dstSlices
,
1723 srcWidth
, srcHeight
, srcDepth
,
1724 srcFormat
, srcType
, srcAddr
, srcPacking
)) {
1728 if (_mesa_is_format_integer(dstFormat
)) {
1729 return texstore_rgba_integer(ctx
, dims
, baseInternalFormat
,
1730 dstFormat
, dstRowStride
, dstSlices
,
1731 srcWidth
, srcHeight
, srcDepth
,
1732 srcFormat
, srcType
, srcAddr
,
1734 } else if (_mesa_get_format_max_bits(dstFormat
) <= 8 &&
1735 !_mesa_is_format_signed(dstFormat
)) {
1736 return store_ubyte_texture(ctx
, dims
, baseInternalFormat
,
1738 dstRowStride
, dstSlices
,
1739 srcWidth
, srcHeight
, srcDepth
,
1740 srcFormat
, srcType
, srcAddr
, srcPacking
);
1742 return texstore_via_float(ctx
, dims
, baseInternalFormat
,
1743 dstFormat
, dstRowStride
, dstSlices
,
1744 srcWidth
, srcHeight
, srcDepth
,
1745 srcFormat
, srcType
, srcAddr
,
1751 _mesa_texstore_needs_transfer_ops(struct gl_context
*ctx
,
1752 GLenum baseInternalFormat
,
1753 mesa_format dstFormat
)
1757 /* There are different rules depending on the base format. */
1758 switch (baseInternalFormat
) {
1759 case GL_DEPTH_COMPONENT
:
1760 case GL_DEPTH_STENCIL
:
1761 return ctx
->Pixel
.DepthScale
!= 1.0f
||
1762 ctx
->Pixel
.DepthBias
!= 0.0f
;
1764 case GL_STENCIL_INDEX
:
1769 * Pixel transfer ops (scale, bias, table lookup) do not apply
1770 * to integer formats.
1772 dstType
= _mesa_get_format_datatype(dstFormat
);
1774 return dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
&&
1775 ctx
->_ImageTransferState
;
1781 _mesa_texstore_can_use_memcpy(struct gl_context
*ctx
,
1782 GLenum baseInternalFormat
, mesa_format dstFormat
,
1783 GLenum srcFormat
, GLenum srcType
,
1784 const struct gl_pixelstore_attrib
*srcPacking
)
1786 if (_mesa_texstore_needs_transfer_ops(ctx
, baseInternalFormat
, dstFormat
)) {
1790 /* The base internal format and the base Mesa format must match. */
1791 if (baseInternalFormat
!= _mesa_get_format_base_format(dstFormat
)) {
1795 /* The Mesa format must match the input format and type. */
1796 if (!_mesa_format_matches_format_and_type(dstFormat
, srcFormat
, srcType
,
1797 srcPacking
->SwapBytes
)) {
1801 /* Depth texture data needs clamping in following cases:
1802 * - Floating point dstFormat with signed srcType: clamp to [0.0, 1.0].
1803 * - Fixed point dstFormat with signed srcType: clamp to [0, 2^n -1].
1805 * All the cases except one (float dstFormat with float srcType) are ruled
1806 * out by _mesa_format_matches_format_and_type() check above. Handle the
1807 * remaining case here.
1809 if ((baseInternalFormat
== GL_DEPTH_COMPONENT
||
1810 baseInternalFormat
== GL_DEPTH_STENCIL
) &&
1811 (srcType
== GL_FLOAT
||
1812 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
)) {
1820 _mesa_texstore_memcpy(TEXSTORE_PARAMS
)
1822 if (!_mesa_texstore_can_use_memcpy(ctx
, baseInternalFormat
, dstFormat
,
1823 srcFormat
, srcType
, srcPacking
)) {
1827 memcpy_texture(ctx
, dims
,
1829 dstRowStride
, dstSlices
,
1830 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
1831 srcAddr
, srcPacking
);
1835 * Store user data into texture memory.
1836 * Called via glTex[Sub]Image1/2/3D()
1837 * \return GL_TRUE for success, GL_FALSE for failure (out of memory).
1840 _mesa_texstore(TEXSTORE_PARAMS
)
1842 if (_mesa_texstore_memcpy(ctx
, dims
, baseInternalFormat
,
1844 dstRowStride
, dstSlices
,
1845 srcWidth
, srcHeight
, srcDepth
,
1846 srcFormat
, srcType
, srcAddr
, srcPacking
)) {
1850 if (_mesa_is_depth_or_stencil_format(baseInternalFormat
)) {
1851 return texstore_depth_stencil(ctx
, dims
, baseInternalFormat
,
1852 dstFormat
, dstRowStride
, dstSlices
,
1853 srcWidth
, srcHeight
, srcDepth
,
1854 srcFormat
, srcType
, srcAddr
, srcPacking
);
1855 } else if (_mesa_is_format_compressed(dstFormat
)) {
1856 return texstore_compressed(ctx
, dims
, baseInternalFormat
,
1857 dstFormat
, dstRowStride
, dstSlices
,
1858 srcWidth
, srcHeight
, srcDepth
,
1859 srcFormat
, srcType
, srcAddr
, srcPacking
);
1861 return texstore_rgba(ctx
, dims
, baseInternalFormat
,
1862 dstFormat
, dstRowStride
, dstSlices
,
1863 srcWidth
, srcHeight
, srcDepth
,
1864 srcFormat
, srcType
, srcAddr
, srcPacking
);
1870 * Normally, we'll only _write_ texel data to a texture when we map it.
1871 * But if the user is providing depth or stencil values and the texture
1872 * image is a combined depth/stencil format, we'll actually read from
1873 * the texture buffer too (in order to insert the depth or stencil values.
1874 * \param userFormat the user-provided image format
1875 * \param texFormat the destination texture format
1878 get_read_write_mode(GLenum userFormat
, mesa_format texFormat
)
1880 if ((userFormat
== GL_STENCIL_INDEX
|| userFormat
== GL_DEPTH_COMPONENT
)
1881 && _mesa_get_format_base_format(texFormat
) == GL_DEPTH_STENCIL
)
1882 return GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1884 return GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
;
1889 * Helper function for storing 1D, 2D, 3D whole and subimages into texture
1891 * The source of the image data may be user memory or a PBO. In the later
1892 * case, we'll map the PBO, copy from it, then unmap it.
1895 store_texsubimage(struct gl_context
*ctx
,
1896 struct gl_texture_image
*texImage
,
1897 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1898 GLint width
, GLint height
, GLint depth
,
1899 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1900 const struct gl_pixelstore_attrib
*packing
,
1904 const GLbitfield mapMode
= get_read_write_mode(format
, texImage
->TexFormat
);
1905 const GLenum target
= texImage
->TexObject
->Target
;
1906 GLboolean success
= GL_FALSE
;
1907 GLuint dims
, slice
, numSlices
= 1, sliceOffset
= 0;
1908 GLint srcImageStride
= 0;
1911 assert(xoffset
+ width
<= texImage
->Width
);
1912 assert(yoffset
+ height
<= texImage
->Height
);
1913 assert(zoffset
+ depth
<= texImage
->Depth
);
1919 case GL_TEXTURE_2D_ARRAY
:
1920 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1928 /* get pointer to src pixels (may be in a pbo which we'll map here) */
1929 src
= (const GLubyte
*)
1930 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1931 format
, type
, pixels
, packing
, caller
);
1935 /* compute slice info (and do some sanity checks) */
1938 case GL_TEXTURE_RECTANGLE
:
1939 case GL_TEXTURE_CUBE_MAP
:
1940 case GL_TEXTURE_EXTERNAL_OES
:
1941 /* one image slice, nothing special needs to be done */
1944 assert(height
== 1);
1946 assert(yoffset
== 0);
1947 assert(zoffset
== 0);
1949 case GL_TEXTURE_1D_ARRAY
:
1951 assert(zoffset
== 0);
1953 sliceOffset
= yoffset
;
1956 srcImageStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
1958 case GL_TEXTURE_2D_ARRAY
:
1960 sliceOffset
= zoffset
;
1963 srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
1967 /* we'll store 3D images as a series of slices */
1969 sliceOffset
= zoffset
;
1970 srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
1973 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1975 sliceOffset
= zoffset
;
1976 srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
1980 _mesa_warning(ctx
, "Unexpected target 0x%x in store_texsubimage()", target
);
1984 assert(numSlices
== 1 || srcImageStride
!= 0);
1986 for (slice
= 0; slice
< numSlices
; slice
++) {
1990 ctx
->Driver
.MapTextureImage(ctx
, texImage
,
1991 slice
+ sliceOffset
,
1992 xoffset
, yoffset
, width
, height
,
1993 mapMode
, &dstMap
, &dstRowStride
);
1995 /* Note: we're only storing a 2D (or 1D) slice at a time but we need
1996 * to pass the right 'dims' value so that GL_UNPACK_SKIP_IMAGES is
1997 * used for 3D images.
1999 success
= _mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
2000 texImage
->TexFormat
,
2003 width
, height
, 1, /* w, h, d */
2004 format
, type
, src
, packing
);
2006 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ sliceOffset
);
2009 src
+= srcImageStride
;
2016 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
2018 _mesa_unmap_teximage_pbo(ctx
, packing
);
2024 * Fallback code for ctx->Driver.TexImage().
2025 * Basically, allocate storage for the texture image, then copy the
2026 * user's image into it.
2029 _mesa_store_teximage(struct gl_context
*ctx
,
2031 struct gl_texture_image
*texImage
,
2032 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2033 const struct gl_pixelstore_attrib
*packing
)
2035 assert(dims
== 1 || dims
== 2 || dims
== 3);
2037 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
2040 /* allocate storage for texture data */
2041 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
2042 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
2046 store_texsubimage(ctx
, texImage
,
2047 0, 0, 0, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
2048 format
, type
, pixels
, packing
, "glTexImage");
2053 * Fallback for Driver.TexSubImage().
2056 _mesa_store_texsubimage(struct gl_context
*ctx
, GLuint dims
,
2057 struct gl_texture_image
*texImage
,
2058 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2059 GLint width
, GLint height
, GLint depth
,
2060 GLenum format
, GLenum type
, const void *pixels
,
2061 const struct gl_pixelstore_attrib
*packing
)
2063 store_texsubimage(ctx
, texImage
,
2064 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2065 format
, type
, pixels
, packing
, "glTexSubImage");
2069 clear_image_to_zero(GLubyte
*dstMap
, GLint dstRowStride
,
2070 GLsizei width
, GLsizei height
,
2071 GLsizei clearValueSize
)
2075 for (y
= 0; y
< height
; y
++) {
2076 memset(dstMap
, 0, clearValueSize
* width
);
2077 dstMap
+= dstRowStride
;
2082 clear_image_to_value(GLubyte
*dstMap
, GLint dstRowStride
,
2083 GLsizei width
, GLsizei height
,
2084 const GLvoid
*clearValue
,
2085 GLsizei clearValueSize
)
2089 for (y
= 0; y
< height
; y
++) {
2090 for (x
= 0; x
< width
; x
++) {
2091 memcpy(dstMap
, clearValue
, clearValueSize
);
2092 dstMap
+= clearValueSize
;
2094 dstMap
+= dstRowStride
- clearValueSize
* width
;
2099 * Fallback for Driver.ClearTexSubImage().
2102 _mesa_store_cleartexsubimage(struct gl_context
*ctx
,
2103 struct gl_texture_image
*texImage
,
2104 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2105 GLsizei width
, GLsizei height
, GLsizei depth
,
2106 const GLvoid
*clearValue
)
2110 GLsizeiptr clearValueSize
;
2113 clearValueSize
= _mesa_get_format_bytes(texImage
->TexFormat
);
2115 for (z
= 0; z
< depth
; z
++) {
2116 ctx
->Driver
.MapTextureImage(ctx
, texImage
,
2117 z
+ zoffset
, xoffset
, yoffset
,
2120 &dstMap
, &dstRowStride
);
2121 if (dstMap
== NULL
) {
2122 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearTex*Image");
2127 clear_image_to_value(dstMap
, dstRowStride
,
2132 clear_image_to_zero(dstMap
, dstRowStride
,
2137 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, z
+ zoffset
);
2142 * Fallback for Driver.CompressedTexImage()
2145 _mesa_store_compressed_teximage(struct gl_context
*ctx
, GLuint dims
,
2146 struct gl_texture_image
*texImage
,
2147 GLsizei imageSize
, const GLvoid
*data
)
2149 /* only 2D and 3D compressed images are supported at this time */
2151 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
2155 /* This is pretty simple, because unlike the general texstore path we don't
2156 * have to worry about the usual image unpacking or image transfer
2160 ASSERT(texImage
->Width
> 0);
2161 ASSERT(texImage
->Height
> 0);
2162 ASSERT(texImage
->Depth
> 0);
2164 /* allocate storage for texture data */
2165 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
2166 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
2170 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
2172 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
2173 texImage
->TexFormat
,
2179 _mesa_compute_compressed_pixelstore(GLuint dims
, struct gl_texture_image
*texImage
,
2180 GLsizei width
, GLsizei height
, GLsizei depth
,
2181 const struct gl_pixelstore_attrib
*packing
,
2182 struct compressed_pixelstore
*store
)
2185 const mesa_format texFormat
= texImage
->TexFormat
;
2187 _mesa_get_format_block_size(texFormat
, &bw
, &bh
);
2189 store
->SkipBytes
= 0;
2190 store
->TotalBytesPerRow
= store
->CopyBytesPerRow
=
2191 _mesa_format_row_stride(texFormat
, width
);
2192 store
->TotalRowsPerSlice
= store
->CopyRowsPerSlice
=
2193 (height
+ bh
- 1) / bh
;
2194 store
->CopySlices
= depth
;
2196 if (packing
->CompressedBlockWidth
&&
2197 packing
->CompressedBlockSize
) {
2199 bw
= packing
->CompressedBlockWidth
;
2201 if (packing
->RowLength
) {
2202 store
->TotalBytesPerRow
= packing
->CompressedBlockSize
*
2203 (packing
->RowLength
+ bw
- 1) / bw
;
2206 store
->SkipBytes
+= packing
->SkipPixels
* packing
->CompressedBlockSize
/ bw
;
2209 if (dims
> 1 && packing
->CompressedBlockHeight
&&
2210 packing
->CompressedBlockSize
) {
2212 bh
= packing
->CompressedBlockHeight
;
2214 store
->SkipBytes
+= packing
->SkipRows
* store
->TotalBytesPerRow
/ bh
;
2215 store
->CopyRowsPerSlice
= (height
+ bh
- 1) / bh
; /* rows in blocks */
2217 if (packing
->ImageHeight
) {
2218 store
->TotalRowsPerSlice
= (packing
->ImageHeight
+ bh
- 1) / bh
;
2222 if (dims
> 2 && packing
->CompressedBlockDepth
&&
2223 packing
->CompressedBlockSize
) {
2225 int bd
= packing
->CompressedBlockDepth
;
2227 store
->SkipBytes
+= packing
->SkipImages
* store
->TotalBytesPerRow
*
2228 store
->TotalRowsPerSlice
/ bd
;
2234 * Fallback for Driver.CompressedTexSubImage()
2237 _mesa_store_compressed_texsubimage(struct gl_context
*ctx
, GLuint dims
,
2238 struct gl_texture_image
*texImage
,
2239 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2240 GLsizei width
, GLsizei height
, GLsizei depth
,
2242 GLsizei imageSize
, const GLvoid
*data
)
2244 struct compressed_pixelstore store
;
2251 _mesa_problem(ctx
, "Unexpected 1D compressed texsubimage call");
2255 _mesa_compute_compressed_pixelstore(dims
, texImage
, width
, height
, depth
,
2256 &ctx
->Unpack
, &store
);
2258 /* get pointer to src pixels (may be in a pbo which we'll map here) */
2259 data
= _mesa_validate_pbo_compressed_teximage(ctx
, dims
, imageSize
, data
,
2261 "glCompressedTexSubImage");
2265 src
= (const GLubyte
*) data
+ store
.SkipBytes
;
2267 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
2268 /* Map dest texture buffer */
2269 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
+ zoffset
,
2270 xoffset
, yoffset
, width
, height
,
2271 GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
,
2272 &dstMap
, &dstRowStride
);
2276 /* copy rows of blocks */
2277 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
2278 memcpy(dstMap
, src
, store
.CopyBytesPerRow
);
2279 dstMap
+= dstRowStride
;
2280 src
+= store
.TotalBytesPerRow
;
2283 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ zoffset
);
2285 /* advance to next slice */
2286 src
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
2289 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage%uD",
2294 _mesa_unmap_teximage_pbo(ctx
, &ctx
->Unpack
);