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.
28 * Texture images manipulation functions.
30 * \note Mesa's native texture data type is GLchan. Native formats are
31 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA, and
34 * \note Device drivers are free to implement any internal format they want.
45 #include "texcompress.h"
46 #include "texformat.h"
54 static void PrintTexture(GLcontext
*ctx
, const struct gl_texture_image
*img
)
56 #if CHAN_TYPE == GL_FLOAT
57 _mesa_problem(NULL
, "PrintTexture doesn't support float channels");
60 const GLchan
*data
= (const GLchan
*) img
->Data
;
63 _mesa_printf("No texture data\n");
67 switch (img
->Format
) {
74 case GL_LUMINANCE_ALPHA
:
84 _mesa_problem(NULL
, "error in PrintTexture\n");
88 for (i
= 0; i
< img
->Height
; i
++) {
89 for (j
= 0; j
< img
->Width
; j
++) {
91 _mesa_printf("%02x ", data
[0]);
93 _mesa_printf("%02x%02x ", data
[0], data
[1]);
95 _mesa_printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
97 _mesa_printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
98 data
+= (img
->RowStride
- img
->Width
) * c
;
108 * Compute floor(log_base_2(n)).
109 * If n < 0 return -1.
138 * Return the simple base format for a given internal texture format.
139 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
141 * \param ctx GL context.
142 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
144 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
145 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
147 * This is the format which is used during texture application (i.e. the
148 * texture format and env mode determine the arithmetic used.
151 _mesa_base_tex_format( GLcontext
*ctx
, GLint internalFormat
)
153 switch (internalFormat
) {
168 case GL_LUMINANCE_ALPHA
:
169 case GL_LUMINANCE4_ALPHA4
:
170 case GL_LUMINANCE6_ALPHA2
:
171 case GL_LUMINANCE8_ALPHA8
:
172 case GL_LUMINANCE12_ALPHA4
:
173 case GL_LUMINANCE12_ALPHA12
:
174 case GL_LUMINANCE16_ALPHA16
:
175 return GL_LUMINANCE_ALPHA
;
206 if (ctx
->Extensions
.EXT_paletted_texture
) {
207 switch (internalFormat
) {
209 case GL_COLOR_INDEX1_EXT
:
210 case GL_COLOR_INDEX2_EXT
:
211 case GL_COLOR_INDEX4_EXT
:
212 case GL_COLOR_INDEX8_EXT
:
213 case GL_COLOR_INDEX12_EXT
:
214 case GL_COLOR_INDEX16_EXT
:
215 return GL_COLOR_INDEX
;
221 if (ctx
->Extensions
.SGIX_depth_texture
) {
222 switch (internalFormat
) {
223 case GL_DEPTH_COMPONENT
:
224 case GL_DEPTH_COMPONENT16_SGIX
:
225 case GL_DEPTH_COMPONENT24_SGIX
:
226 case GL_DEPTH_COMPONENT32_SGIX
:
227 return GL_DEPTH_COMPONENT
;
233 if (ctx
->Extensions
.ARB_texture_compression
) {
234 switch (internalFormat
) {
235 case GL_COMPRESSED_ALPHA
:
237 case GL_COMPRESSED_LUMINANCE
:
239 case GL_COMPRESSED_LUMINANCE_ALPHA
:
240 return GL_LUMINANCE_ALPHA
;
241 case GL_COMPRESSED_INTENSITY
:
243 case GL_COMPRESSED_RGB
:
245 case GL_COMPRESSED_RGBA
:
252 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
253 switch (internalFormat
) {
254 case GL_COMPRESSED_RGB_FXT1_3DFX
:
256 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
263 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
264 switch (internalFormat
) {
265 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
267 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
268 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
269 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
276 if (ctx
->Extensions
.S3_s3tc
) {
277 switch (internalFormat
) {
289 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
290 if (internalFormat
== GL_YCBCR_MESA
)
291 return GL_YCBCR_MESA
;
294 if (ctx
->Extensions
.ARB_texture_float
) {
295 switch (internalFormat
) {
296 case GL_ALPHA16F_ARB
:
297 case GL_ALPHA32F_ARB
:
305 case GL_INTENSITY16F_ARB
:
306 case GL_INTENSITY32F_ARB
:
308 case GL_LUMINANCE16F_ARB
:
309 case GL_LUMINANCE32F_ARB
:
311 case GL_LUMINANCE_ALPHA16F_ARB
:
312 case GL_LUMINANCE_ALPHA32F_ARB
:
313 return GL_LUMINANCE_ALPHA
;
319 return -1; /* error */
324 * Test if the given image format is a color/RGBA format (i.e., not color
325 * index, depth, stencil, etc).
326 * \param format the image format value (may by an internal texture format)
327 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
330 is_color_format(GLenum format
)
345 case GL_LUMINANCE_ALPHA
:
346 case GL_LUMINANCE4_ALPHA4
:
347 case GL_LUMINANCE6_ALPHA2
:
348 case GL_LUMINANCE8_ALPHA8
:
349 case GL_LUMINANCE12_ALPHA4
:
350 case GL_LUMINANCE12_ALPHA12
:
351 case GL_LUMINANCE16_ALPHA16
:
378 /* float texture formats */
379 case GL_ALPHA16F_ARB
:
380 case GL_ALPHA32F_ARB
:
381 case GL_LUMINANCE16F_ARB
:
382 case GL_LUMINANCE32F_ARB
:
383 case GL_LUMINANCE_ALPHA16F_ARB
:
384 case GL_LUMINANCE_ALPHA32F_ARB
:
385 case GL_INTENSITY16F_ARB
:
386 case GL_INTENSITY32F_ARB
:
391 /* compressed formats */
392 case GL_COMPRESSED_ALPHA
:
393 case GL_COMPRESSED_LUMINANCE
:
394 case GL_COMPRESSED_LUMINANCE_ALPHA
:
395 case GL_COMPRESSED_INTENSITY
:
396 case GL_COMPRESSED_RGB
:
397 case GL_COMPRESSED_RGBA
:
402 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
403 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
404 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
405 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
406 case GL_COMPRESSED_RGB_FXT1_3DFX
:
407 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
409 case GL_YCBCR_MESA
: /* not considered to be RGB */
417 * Test if the given image format is a color index format.
420 is_index_format(GLenum format
)
424 case GL_COLOR_INDEX1_EXT
:
425 case GL_COLOR_INDEX2_EXT
:
426 case GL_COLOR_INDEX4_EXT
:
427 case GL_COLOR_INDEX8_EXT
:
428 case GL_COLOR_INDEX12_EXT
:
429 case GL_COLOR_INDEX16_EXT
:
438 * Test if the given image format is a depth component format.
441 is_depth_format(GLenum format
)
444 case GL_DEPTH_COMPONENT16_ARB
:
445 case GL_DEPTH_COMPONENT24_ARB
:
446 case GL_DEPTH_COMPONENT32_ARB
:
447 case GL_DEPTH_COMPONENT
:
456 * Test if the given image format is a YCbCr format.
459 is_ycbcr_format(GLenum format
)
471 * Test if it is a supported compressed format.
473 * \param internalFormat the internal format token provided by the user.
475 * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
476 * GL_FALSE otherwise.
478 * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
482 is_compressed_format(GLcontext
*ctx
, GLenum internalFormat
)
485 switch (internalFormat
) {
486 case GL_COMPRESSED_RGB_FXT1_3DFX
:
487 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
488 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
489 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
490 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
491 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
504 * Store a gl_texture_image pointer in a gl_texture_object structure
505 * according to the target and level parameters.
507 * \param tObj texture object.
508 * \param target texture target.
509 * \param level image level.
510 * \param texImage texture image.
512 * This was basically prompted by the introduction of cube maps.
515 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
516 GLenum target
, GLint level
,
517 struct gl_texture_image
*texImage
)
525 tObj
->Image
[0][level
] = texImage
;
527 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
529 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
530 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
531 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
: {
533 GLuint face
= ((GLuint
) target
-
534 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
535 tObj
->Image
[face
][level
] = texImage
;
538 case GL_TEXTURE_RECTANGLE_NV
:
540 tObj
->Image
[0][level
] = texImage
;
543 _mesa_problem(NULL
, "bad target in _mesa_set_tex_image()");
546 /* Set the 'back' pointer */
547 texImage
->TexObject
= tObj
;
552 * Allocate a texture image structure.
554 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
557 * \return a pointer to gl_texture_image struct with all fields initialized to
560 struct gl_texture_image
*
561 _mesa_new_texture_image( GLcontext
*ctx
)
564 return CALLOC_STRUCT(gl_texture_image
);
569 * Free texture image.
571 * \param teximage texture image.
573 * Free the texture image structure and the associated image data if it's not
574 * marked as client data.
577 _mesa_delete_texture_image( struct gl_texture_image
*teximage
)
579 if (teximage
->Data
&& !teximage
->IsClientData
) {
580 MESA_PBUFFER_FREE( teximage
->Data
);
581 teximage
->Data
= NULL
;
588 * Test if a target is a proxy target.
590 * \param target texture target.
592 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
595 is_proxy_target(GLenum target
)
597 return (target
== GL_PROXY_TEXTURE_1D
||
598 target
== GL_PROXY_TEXTURE_2D
||
599 target
== GL_PROXY_TEXTURE_3D
||
600 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
601 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
);
606 * Get the texture object that corresponds to the target of the given texture unit.
608 * \param ctx GL context.
609 * \param texUnit texture unit.
610 * \param target texture target.
612 * \return pointer to the texture object on success, or NULL on failure.
614 * \sa gl_texture_unit.
616 struct gl_texture_object
*
617 _mesa_select_tex_object(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
622 return texUnit
->Current1D
;
623 case GL_PROXY_TEXTURE_1D
:
624 return ctx
->Texture
.Proxy1D
;
626 return texUnit
->Current2D
;
627 case GL_PROXY_TEXTURE_2D
:
628 return ctx
->Texture
.Proxy2D
;
630 return texUnit
->Current3D
;
631 case GL_PROXY_TEXTURE_3D
:
632 return ctx
->Texture
.Proxy3D
;
633 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
634 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
635 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
636 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
637 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
638 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
639 case GL_TEXTURE_CUBE_MAP_ARB
:
640 return ctx
->Extensions
.ARB_texture_cube_map
641 ? texUnit
->CurrentCubeMap
: NULL
;
642 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
643 return ctx
->Extensions
.ARB_texture_cube_map
644 ? ctx
->Texture
.ProxyCubeMap
: NULL
;
645 case GL_TEXTURE_RECTANGLE_NV
:
646 return ctx
->Extensions
.NV_texture_rectangle
647 ? texUnit
->CurrentRect
: NULL
;
648 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
649 return ctx
->Extensions
.NV_texture_rectangle
650 ? ctx
->Texture
.ProxyRect
: NULL
;
652 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
659 * Get the texture image struct which corresponds to target and level
660 * of the given texture unit.
662 * \param ctx GL context.
663 * \param texUnit texture unit.
664 * \param target texture target.
665 * \param level image level.
667 * \return pointer to the texture image structure on success, or NULL on failure.
669 * \sa gl_texture_unit.
671 struct gl_texture_image
*
672 _mesa_select_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
673 GLenum target
, GLint level
)
676 ASSERT(level
< MAX_TEXTURE_LEVELS
);
679 return texUnit
->Current1D
->Image
[0][level
];
680 case GL_PROXY_TEXTURE_1D
:
681 return ctx
->Texture
.Proxy1D
->Image
[0][level
];
683 return texUnit
->Current2D
->Image
[0][level
];
684 case GL_PROXY_TEXTURE_2D
:
685 return ctx
->Texture
.Proxy2D
->Image
[0][level
];
687 return texUnit
->Current3D
->Image
[0][level
];
688 case GL_PROXY_TEXTURE_3D
:
689 return ctx
->Texture
.Proxy3D
->Image
[0][level
];
690 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
691 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
692 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
693 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
694 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
695 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
696 if (ctx
->Extensions
.ARB_texture_cube_map
) {
697 GLuint face
= ((GLuint
) target
-
698 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
699 return texUnit
->CurrentCubeMap
->Image
[face
][level
];
703 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
704 if (ctx
->Extensions
.ARB_texture_cube_map
)
705 return ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
708 case GL_TEXTURE_RECTANGLE_NV
:
709 if (ctx
->Extensions
.NV_texture_rectangle
) {
711 return texUnit
->CurrentRect
->Image
[0][level
];
716 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
717 if (ctx
->Extensions
.NV_texture_rectangle
) {
719 return ctx
->Texture
.ProxyRect
->Image
[0][level
];
725 _mesa_problem(ctx
, "bad target in _mesa_select_tex_image()");
732 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
733 * it and install it. Only return NULL if passed a bad parameter or run
736 struct gl_texture_image
*
737 _mesa_get_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
738 GLenum target
, GLint level
)
740 struct gl_texture_image
*texImage
;
741 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
743 struct gl_texture_object
*texObj
;
744 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
746 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
749 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
751 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
758 * Return pointer to the specified proxy texture image.
759 * Note that proxy textures are per-context, not per-texture unit.
760 * \return pointer to texture image or NULL if invalid target, invalid
761 * level, or out of memory.
763 struct gl_texture_image
*
764 _mesa_get_proxy_tex_image(GLcontext
*ctx
, GLenum target
, GLint level
)
766 struct gl_texture_image
*texImage
;
772 case GL_PROXY_TEXTURE_1D
:
773 if (level
>= ctx
->Const
.MaxTextureLevels
)
775 texImage
= ctx
->Texture
.Proxy1D
->Image
[0][level
];
777 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
779 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
782 ctx
->Texture
.Proxy1D
->Image
[0][level
] = texImage
;
783 /* Set the 'back' pointer */
784 texImage
->TexObject
= ctx
->Texture
.Proxy1D
;
787 case GL_PROXY_TEXTURE_2D
:
788 if (level
>= ctx
->Const
.MaxTextureLevels
)
790 texImage
= ctx
->Texture
.Proxy2D
->Image
[0][level
];
792 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
794 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
797 ctx
->Texture
.Proxy2D
->Image
[0][level
] = texImage
;
798 /* Set the 'back' pointer */
799 texImage
->TexObject
= ctx
->Texture
.Proxy2D
;
802 case GL_PROXY_TEXTURE_3D
:
803 if (level
>= ctx
->Const
.Max3DTextureLevels
)
805 texImage
= ctx
->Texture
.Proxy3D
->Image
[0][level
];
807 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
809 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
812 ctx
->Texture
.Proxy3D
->Image
[0][level
] = texImage
;
813 /* Set the 'back' pointer */
814 texImage
->TexObject
= ctx
->Texture
.Proxy3D
;
817 case GL_PROXY_TEXTURE_CUBE_MAP
:
818 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
820 texImage
= ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
822 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
824 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
827 ctx
->Texture
.ProxyCubeMap
->Image
[0][level
] = texImage
;
828 /* Set the 'back' pointer */
829 texImage
->TexObject
= ctx
->Texture
.ProxyCubeMap
;
832 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
835 texImage
= ctx
->Texture
.ProxyRect
->Image
[0][level
];
837 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
839 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
842 ctx
->Texture
.ProxyRect
->Image
[0][level
] = texImage
;
843 /* Set the 'back' pointer */
844 texImage
->TexObject
= ctx
->Texture
.ProxyRect
;
854 * Get the maximum number of allowed mipmap levels.
856 * \param ctx GL context.
857 * \param target texture target.
859 * \return the maximum number of allowed mipmap levels for the given
860 * texture target, or zero if passed a bad target.
865 _mesa_max_texture_levels(GLcontext
*ctx
, GLenum target
)
869 case GL_PROXY_TEXTURE_1D
:
871 case GL_PROXY_TEXTURE_2D
:
872 return ctx
->Const
.MaxTextureLevels
;
874 case GL_PROXY_TEXTURE_3D
:
875 return ctx
->Const
.Max3DTextureLevels
;
876 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
877 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
878 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
879 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
881 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
882 case GL_TEXTURE_CUBE_MAP_ARB
:
883 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
884 return ctx
->Const
.MaxCubeTextureLevels
;
886 case GL_TEXTURE_RECTANGLE_NV
:
887 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
891 return 0; /* bad target */
897 #if 000 /* not used anymore */
899 * glTexImage[123]D can accept a NULL image pointer. In this case we
900 * create a texture image with unspecified image contents per the OpenGL
904 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
906 const GLint components
= _mesa_components_in_format(format
);
907 const GLint numPixels
= width
* height
* depth
;
908 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
912 * Let's see if anyone finds this. If glTexImage2D() is called with
913 * a NULL image pointer then load the texture image with something
914 * interesting instead of leaving it indeterminate.
917 static const char message
[8][32] = {
921 " X X XXXX XXX XXXXX ",
924 " X X XXXXX XXX X X ",
928 GLubyte
*imgPtr
= data
;
930 for (h
= 0; h
< depth
; h
++) {
931 for (i
= 0; i
< height
; i
++) {
932 GLint srcRow
= 7 - (i
% 8);
933 for (j
= 0; j
< width
; j
++) {
934 GLint srcCol
= j
% 32;
935 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
936 for (k
= 0; k
< components
; k
++) {
952 * Reset the fields of a gl_texture_image struct to zero.
954 * \param img texture image structure.
956 * This is called when a proxy texture test fails, we set all the
957 * image members (except DriverData) to zero.
958 * It's also used in glTexImage[123]D as a safeguard to be sure all
959 * required fields get initialized properly by the Driver.TexImage[123]D
963 clear_teximage_fields(struct gl_texture_image
*img
)
980 img
->TexFormat
= &_mesa_null_texformat
;
981 img
->FetchTexelc
= NULL
;
982 img
->FetchTexelf
= NULL
;
983 img
->IsCompressed
= 0;
984 img
->CompressedSize
= 0;
989 * Initialize basic fields of the gl_texture_image struct.
991 * \param ctx GL context.
992 * \param target texture target.
993 * \param img texture image structure to be initialized.
994 * \param width image width.
995 * \param height image height.
996 * \param depth image depth.
997 * \param border image border.
998 * \param internalFormat internal format.
1000 * Fills in the fields of \p img with the given information.
1001 * Note: width, height and depth include the border.
1004 _mesa_init_teximage_fields(GLcontext
*ctx
, GLenum target
,
1005 struct gl_texture_image
*img
,
1006 GLsizei width
, GLsizei height
, GLsizei depth
,
1007 GLint border
, GLenum internalFormat
)
1010 img
->Format
= _mesa_base_tex_format( ctx
, internalFormat
);
1011 ASSERT(img
->Format
> 0);
1012 img
->IntFormat
= internalFormat
;
1013 img
->Border
= border
;
1015 img
->Height
= height
;
1017 img
->RowStride
= width
;
1018 img
->WidthLog2
= logbase2(width
- 2 * border
);
1019 if (height
== 1) /* 1-D texture */
1020 img
->HeightLog2
= 0;
1022 img
->HeightLog2
= logbase2(height
- 2 * border
);
1023 if (depth
== 1) /* 2-D texture */
1026 img
->DepthLog2
= logbase2(depth
- 2 * border
);
1027 img
->Width2
= width
- 2 * border
; /*1 << img->WidthLog2;*/
1028 img
->Height2
= height
- 2 * border
; /*1 << img->HeightLog2;*/
1029 img
->Depth2
= depth
- 2 * border
; /*1 << img->DepthLog2;*/
1030 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1031 img
->IsCompressed
= is_compressed_format(ctx
, internalFormat
);
1032 if (img
->IsCompressed
)
1033 img
->CompressedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
,
1034 height
, depth
, internalFormat
);
1036 img
->CompressedSize
= 0;
1038 if ((width
== 1 || _mesa_bitcount(width
- 2 * border
) == 1) &&
1039 (height
== 1 || _mesa_bitcount(height
- 2 * border
) == 1) &&
1040 (depth
== 1 || _mesa_bitcount(depth
- 2 * border
) == 1))
1041 img
->_IsPowerOfTwo
= GL_TRUE
;
1043 img
->_IsPowerOfTwo
= GL_FALSE
;
1045 /* Compute Width/Height/DepthScale for mipmap lod computation */
1046 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1047 /* scale = 1.0 since texture coords directly map to texels */
1048 img
->WidthScale
= 1.0;
1049 img
->HeightScale
= 1.0;
1050 img
->DepthScale
= 1.0;
1053 img
->WidthScale
= (GLfloat
) img
->Width
;
1054 img
->HeightScale
= (GLfloat
) img
->Height
;
1055 img
->DepthScale
= (GLfloat
) img
->Depth
;
1061 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1062 * level, width, height and depth against the ctx->Const limits for textures.
1064 * A hardware driver might override this function if, for example, the
1065 * max 3D texture size is 512x512x64 (i.e. not a cube).
1067 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1068 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1069 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1070 * \param level as passed to glTexImage
1071 * \param internalFormat as passed to glTexImage
1072 * \param format as passed to glTexImage
1073 * \param type as passed to glTexImage
1074 * \param width as passed to glTexImage
1075 * \param height as passed to glTexImage
1076 * \param depth as passed to glTexImage
1077 * \param border as passed to glTexImage
1078 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1081 _mesa_test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1082 GLint internalFormat
, GLenum format
, GLenum type
,
1083 GLint width
, GLint height
, GLint depth
, GLint border
)
1087 (void) internalFormat
;
1092 case GL_PROXY_TEXTURE_1D
:
1093 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1094 if (width
< 2 * border
|| width
> 2 + maxSize
||
1095 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1096 _mesa_bitcount(width
- 2 * border
) != 1) ||
1097 level
>= ctx
->Const
.MaxTextureLevels
) {
1098 /* bad width or level */
1102 case GL_PROXY_TEXTURE_2D
:
1103 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1104 if (width
< 2 * border
|| width
> 2 + maxSize
||
1105 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1106 _mesa_bitcount(width
- 2 * border
) != 1) ||
1107 height
< 2 * border
|| height
> 2 + maxSize
||
1108 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1109 _mesa_bitcount(height
- 2 * border
) != 1) ||
1110 level
>= ctx
->Const
.MaxTextureLevels
) {
1111 /* bad width or height or level */
1115 case GL_PROXY_TEXTURE_3D
:
1116 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1117 if (width
< 2 * border
|| width
> 2 + maxSize
||
1118 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1119 _mesa_bitcount(width
- 2 * border
) != 1) ||
1120 height
< 2 * border
|| height
> 2 + maxSize
||
1121 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1122 _mesa_bitcount(height
- 2 * border
) != 1) ||
1123 depth
< 2 * border
|| depth
> 2 + maxSize
||
1124 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1125 _mesa_bitcount(depth
- 2 * border
) != 1) ||
1126 level
>= ctx
->Const
.Max3DTextureLevels
) {
1127 /* bad width or height or depth or level */
1131 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1132 if (width
< 1 || width
> ctx
->Const
.MaxTextureRectSize
||
1133 height
< 1 || height
> ctx
->Const
.MaxTextureRectSize
||
1135 /* bad width or height or level */
1139 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1140 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1141 if (width
< 2 * border
|| width
> 2 + maxSize
||
1142 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1143 _mesa_bitcount(width
- 2 * border
) != 1) ||
1144 height
< 2 * border
|| height
> 2 + maxSize
||
1145 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1146 _mesa_bitcount(height
- 2 * border
) != 1) ||
1147 level
>= ctx
->Const
.MaxCubeTextureLevels
) {
1148 /* bad width or height */
1153 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1160 * Test the glTexImage[123]D() parameters for errors.
1162 * \param ctx GL context.
1163 * \param target texture target given by the user.
1164 * \param level image level given by the user.
1165 * \param internalFormat internal format given by the user.
1166 * \param format pixel data format given by the user.
1167 * \param type pixel data type given by the user.
1168 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1169 * \param width image width given by the user.
1170 * \param height image height given by the user.
1171 * \param depth image depth given by the user.
1172 * \param border image border given by the user.
1174 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1176 * Verifies each of the parameters against the constants specified in
1177 * __GLcontextRec::Const and the supported extensions, and according to the
1178 * OpenGL specification.
1181 texture_error_check( GLcontext
*ctx
, GLenum target
,
1182 GLint level
, GLint internalFormat
,
1183 GLenum format
, GLenum type
,
1185 GLint width
, GLint height
,
1186 GLint depth
, GLint border
)
1188 const GLboolean isProxy
= is_proxy_target(target
);
1191 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1192 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1194 _mesa_error(ctx
, GL_INVALID_VALUE
,
1195 "glTexImage%dD(level=%d)", dimensions
, level
);
1201 if (border
< 0 || border
> 1 ||
1202 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1203 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1205 _mesa_error(ctx
, GL_INVALID_VALUE
,
1206 "glTexImage%dD(border=%d)", dimensions
, border
);
1211 if (width
< 0 || height
< 0 || depth
< 0) {
1213 _mesa_error(ctx
, GL_INVALID_VALUE
,
1214 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1219 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1220 * level, width, height and depth.
1222 if (dimensions
== 1) {
1223 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1224 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1225 level
, internalFormat
,
1227 width
, 1, 1, border
);
1230 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1234 else if (dimensions
== 2) {
1235 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1236 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1237 level
, internalFormat
,
1239 width
, height
, 1, border
);
1241 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1242 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1243 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1244 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1245 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1248 sizeOK
= (width
== height
) &&
1249 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1250 level
, internalFormat
, format
, type
,
1251 width
, height
, 1, border
);
1253 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1254 target
== GL_TEXTURE_RECTANGLE_NV
) {
1255 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1256 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1259 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1260 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1261 level
, internalFormat
,
1263 width
, height
, 1, border
);
1266 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1270 else if (dimensions
== 3) {
1271 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1272 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_3D
,
1273 level
, internalFormat
,
1275 width
, height
, depth
, border
);
1278 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1283 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1289 _mesa_error(ctx
, GL_INVALID_VALUE
,
1290 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1291 dimensions
, level
, width
, height
, depth
);
1296 /* Check internalFormat */
1297 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1299 _mesa_error(ctx
, GL_INVALID_VALUE
,
1300 "glTexImage%dD(internalFormat=0x%x)",
1301 dimensions
, internalFormat
);
1306 /* Check incoming image format and type */
1307 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1308 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1309 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1312 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1313 "glTexImage%dD(format or type)", dimensions
);
1318 /* make sure internal format and format basically agree */
1319 if ((is_color_format(internalFormat
) != is_color_format(format
)) ||
1320 (is_index_format(internalFormat
) != is_index_format(format
)) ||
1321 (is_depth_format(internalFormat
) != is_depth_format(format
)) ||
1322 (is_ycbcr_format(internalFormat
) != is_ycbcr_format(format
))) {
1324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1325 "glTexImage(internalFormat/format)");
1329 /* additional checks for ycbcr textures */
1330 if (internalFormat
== GL_YCBCR_MESA
) {
1331 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1332 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1333 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1335 _mesa_sprintf(message
,
1336 "glTexImage%d(format/type YCBCR mismatch", dimensions
);
1337 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
1338 return GL_TRUE
; /* error */
1340 if (target
!= GL_TEXTURE_2D
&&
1341 target
!= GL_PROXY_TEXTURE_2D
&&
1342 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1343 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1345 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1351 _mesa_sprintf(message
,
1352 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1353 dimensions
, border
);
1354 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1360 /* additional checks for depth textures */
1361 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1362 /* Only 1D and 2D textures supported */
1363 if (target
!= GL_TEXTURE_1D
&&
1364 target
!= GL_PROXY_TEXTURE_1D
&&
1365 target
!= GL_TEXTURE_2D
&&
1366 target
!= GL_PROXY_TEXTURE_2D
) {
1368 _mesa_error(ctx
, GL_INVALID_ENUM
,
1369 "glTexImage(target/internalFormat)");
1374 /* additional checks for compressed textures */
1375 if (is_compressed_format(ctx
, internalFormat
)) {
1376 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1379 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1380 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1381 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1382 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1387 _mesa_error(ctx
, GL_INVALID_ENUM
,
1388 "glTexImage%d(target)", dimensions
);
1394 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1395 "glTexImage%D(border!=0)", dimensions
);
1401 /* if we get here, the parameters are OK */
1407 * Test glTexSubImage[123]D() parameters for errors.
1409 * \param ctx GL context.
1410 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1411 * \param target texture target given by the user.
1412 * \param level image level given by the user.
1413 * \param xoffset sub-image x offset given by the user.
1414 * \param yoffset sub-image y offset given by the user.
1415 * \param zoffset sub-image z offset given by the user.
1416 * \param format pixel data format given by the user.
1417 * \param type pixel data type given by the user.
1418 * \param width image width given by the user.
1419 * \param height image height given by the user.
1420 * \param depth image depth given by the user.
1422 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1424 * Verifies each of the parameters against the constants specified in
1425 * __GLcontextRec::Const and the supported extensions, and according to the
1426 * OpenGL specification.
1429 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1430 GLenum target
, GLint level
,
1431 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1432 GLint width
, GLint height
, GLint depth
,
1433 GLenum format
, GLenum type
)
1435 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1436 struct gl_texture_image
*destTex
;
1439 if (dimensions
== 1) {
1440 if (target
!= GL_TEXTURE_1D
) {
1441 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1445 else if (dimensions
== 2) {
1446 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1447 target
<=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1448 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1449 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1453 else if (ctx
->Extensions
.NV_texture_rectangle
&&
1454 target
== GL_TEXTURE_RECTANGLE_NV
) {
1455 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1456 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1460 else if (target
!= GL_TEXTURE_2D
) {
1461 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1465 else if (dimensions
== 3) {
1466 if (target
!= GL_TEXTURE_3D
) {
1467 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1472 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1476 /* Basic level check */
1477 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1478 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1483 _mesa_error(ctx
, GL_INVALID_VALUE
,
1484 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1487 if (height
< 0 && dimensions
> 1) {
1488 _mesa_error(ctx
, GL_INVALID_VALUE
,
1489 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1492 if (depth
< 0 && dimensions
> 2) {
1493 _mesa_error(ctx
, GL_INVALID_VALUE
,
1494 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1498 destTex
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1501 /* undefined image level */
1502 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1506 if (xoffset
< -((GLint
)destTex
->Border
)) {
1507 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1511 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1512 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1516 if (dimensions
> 1) {
1517 if (yoffset
< -((GLint
)destTex
->Border
)) {
1518 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1522 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1523 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1528 if (dimensions
> 2) {
1529 if (zoffset
< -((GLint
)destTex
->Border
)) {
1530 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1533 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1534 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1539 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1540 _mesa_error(ctx
, GL_INVALID_ENUM
,
1541 "glTexSubImage%dD(format or type)", dimensions
);
1545 if (destTex
->IsCompressed
) {
1546 const struct gl_texture_unit
*texUnit
;
1547 const struct gl_texture_image
*texImage
;
1548 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1549 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1551 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1554 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1555 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1556 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1557 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1561 _mesa_error(ctx
, GL_INVALID_ENUM
,
1562 "glTexSubImage%D(target)", dimensions
);
1565 /* offset must be multiple of 4 */
1566 if ((xoffset
& 3) || (yoffset
& 3)) {
1567 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1568 "glTexSubImage%D(xoffset or yoffset)", dimensions
);
1571 /* size must be multiple of 4 or equal to whole texture size */
1572 if ((width
& 3) && (GLuint
) width
!= texImage
->Width
) {
1573 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1574 "glTexSubImage%D(width)", dimensions
);
1577 if ((height
& 3) && (GLuint
) height
!= texImage
->Height
) {
1578 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1579 "glTexSubImage%D(width)", dimensions
);
1589 * Test glCopyTexImage[12]D() parameters for errors.
1591 * \param ctx GL context.
1592 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1593 * \param target texture target given by the user.
1594 * \param level image level given by the user.
1595 * \param internalFormat internal format given by the user.
1596 * \param width image width given by the user.
1597 * \param height image height given by the user.
1598 * \param depth image depth given by the user.
1599 * \param border texture border.
1601 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1603 * Verifies each of the parameters against the constants specified in
1604 * __GLcontextRec::Const and the supported extensions, and according to the
1605 * OpenGL specification.
1608 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1609 GLenum target
, GLint level
, GLint internalFormat
,
1610 GLint width
, GLint height
, GLint border
)
1612 GLenum format
, type
;
1615 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1616 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1617 _mesa_error(ctx
, GL_INVALID_VALUE
,
1618 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1623 if (border
< 0 || border
> 1 ||
1624 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1625 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1629 /* The format and type aren't really significant here, but we need to pass
1630 * something to TestProxyTexImage().
1632 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1635 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1636 * level, width, height and depth.
1638 if (dimensions
== 1) {
1639 if (target
== GL_TEXTURE_1D
) {
1640 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1641 level
, internalFormat
,
1643 width
, 1, 1, border
);
1646 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1650 else if (dimensions
== 2) {
1651 if (target
== GL_TEXTURE_2D
) {
1652 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1653 level
, internalFormat
,
1655 width
, height
, 1, border
);
1657 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1658 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1659 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1660 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1663 sizeOK
= (width
== height
) &&
1664 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1665 level
, internalFormat
, format
, type
,
1666 width
, height
, 1, border
);
1668 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1669 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1670 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1673 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1674 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1675 level
, internalFormat
,
1677 width
, height
, 1, border
);
1680 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1685 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1690 if (dimensions
== 1) {
1691 _mesa_error(ctx
, GL_INVALID_VALUE
,
1692 "glCopyTexImage1D(width=%d)", width
);
1695 ASSERT(dimensions
== 2);
1696 _mesa_error(ctx
, GL_INVALID_VALUE
,
1697 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1702 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1703 _mesa_error(ctx
, GL_INVALID_VALUE
,
1704 "glCopyTexImage%dD(internalFormat)", dimensions
);
1708 if (is_compressed_format(ctx
, internalFormat
)) {
1709 if (target
!= GL_TEXTURE_2D
) {
1710 _mesa_error(ctx
, GL_INVALID_ENUM
,
1711 "glCopyTexImage%d(target)", dimensions
);
1715 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1716 "glCopyTexImage%D(border!=0)", dimensions
);
1721 /* if we get here, the parameters are OK */
1727 * Test glCopyTexImage[12]D() parameters for errors.
1729 * \param ctx GL context.
1730 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1731 * \param target texture target given by the user.
1732 * \param level image level given by the user.
1733 * \param xoffset sub-image x offset given by the user.
1734 * \param yoffset sub-image y offset given by the user.
1735 * \param zoffset sub-image z offset given by the user.
1736 * \param width image width given by the user.
1737 * \param height image height given by the user.
1739 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1741 * Verifies each of the parameters against the constants specified in
1742 * __GLcontextRec::Const and the supported extensions, and according to the
1743 * OpenGL specification.
1746 copytexsubimage_error_check( GLcontext
*ctx
, GLuint dimensions
,
1747 GLenum target
, GLint level
,
1748 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1749 GLsizei width
, GLsizei height
)
1751 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1752 struct gl_texture_image
*teximage
;
1755 if (dimensions
== 1) {
1756 if (target
!= GL_TEXTURE_1D
) {
1757 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1761 else if (dimensions
== 2) {
1762 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1763 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1764 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1765 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1769 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1770 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1771 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1775 else if (target
!= GL_TEXTURE_2D
) {
1776 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1780 else if (dimensions
== 3) {
1781 if (target
!= GL_TEXTURE_3D
) {
1782 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1788 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1789 _mesa_error(ctx
, GL_INVALID_VALUE
,
1790 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1796 _mesa_error(ctx
, GL_INVALID_VALUE
,
1797 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1800 if (dimensions
> 1 && height
< 0) {
1801 _mesa_error(ctx
, GL_INVALID_VALUE
,
1802 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1806 teximage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1808 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1809 "glCopyTexSubImage%dD(undefined texture level: %d)",
1814 if (xoffset
< -((GLint
)teximage
->Border
)) {
1815 _mesa_error(ctx
, GL_INVALID_VALUE
,
1816 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1819 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1820 _mesa_error(ctx
, GL_INVALID_VALUE
,
1821 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1824 if (dimensions
> 1) {
1825 if (yoffset
< -((GLint
)teximage
->Border
)) {
1826 _mesa_error(ctx
, GL_INVALID_VALUE
,
1827 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1830 /* NOTE: we're adding the border here, not subtracting! */
1831 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1832 _mesa_error(ctx
, GL_INVALID_VALUE
,
1833 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1838 if (dimensions
> 2) {
1839 if (zoffset
< -((GLint
)teximage
->Border
)) {
1840 _mesa_error(ctx
, GL_INVALID_VALUE
,
1841 "glCopyTexSubImage%dD(zoffset)", dimensions
);
1844 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1845 _mesa_error(ctx
, GL_INVALID_VALUE
,
1846 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1851 if (teximage
->IsCompressed
) {
1852 if (target
!= GL_TEXTURE_2D
) {
1853 _mesa_error(ctx
, GL_INVALID_ENUM
,
1854 "glCopyTexSubImage%d(target)", dimensions
);
1857 /* offset must be multiple of 4 */
1858 if ((xoffset
& 3) || (yoffset
& 3)) {
1859 _mesa_error(ctx
, GL_INVALID_VALUE
,
1860 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions
);
1863 /* size must be multiple of 4 */
1864 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
1865 _mesa_error(ctx
, GL_INVALID_VALUE
,
1866 "glCopyTexSubImage%D(width)", dimensions
);
1869 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
1870 _mesa_error(ctx
, GL_INVALID_VALUE
,
1871 "glCopyTexSubImage%D(height)", dimensions
);
1876 if (teximage
->IntFormat
== GL_YCBCR_MESA
) {
1877 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
1881 /* if we get here, the parameters are OK */
1887 * Get texture image. Called by glGetTexImage.
1889 * \param target texture target.
1890 * \param level image level.
1891 * \param format pixel data format for returned image.
1892 * \param type pixel data type for returned image.
1893 * \param pixels returned pixel data.
1896 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1897 GLenum type
, GLvoid
*pixels
)
1899 const struct gl_texture_unit
*texUnit
;
1900 const struct gl_texture_object
*texObj
;
1901 const struct gl_texture_image
*texImage
;
1902 GLint maxLevels
= 0;
1903 GET_CURRENT_CONTEXT(ctx
);
1904 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1906 texUnit
= &(ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
]);
1907 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1908 if (!texObj
|| is_proxy_target(target
)) {
1909 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
1913 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1914 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
1916 if (level
< 0 || level
>= maxLevels
) {
1917 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
1921 if (_mesa_sizeof_packed_type(type
) <= 0) {
1922 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
1926 if (_mesa_components_in_format(format
) <= 0 ||
1927 format
== GL_STENCIL_INDEX
) {
1928 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
1932 if (!ctx
->Extensions
.EXT_paletted_texture
&& is_index_format(format
)) {
1933 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1936 if (!ctx
->Extensions
.SGIX_depth_texture
&& is_depth_format(format
)) {
1937 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1940 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& is_ycbcr_format(format
)) {
1941 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1947 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1949 /* invalid mipmap level, not an error */
1953 if (!texImage
->Data
) {
1954 /* no image data, not an error */
1958 /* Make sure the requested image format is compatible with the
1961 if (is_color_format(format
)
1962 && !is_color_format(texImage
->TexFormat
->BaseFormat
)) {
1963 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1966 else if (is_index_format(format
)
1967 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1968 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1971 else if (is_depth_format(format
)
1972 && !is_depth_format(texImage
->TexFormat
->BaseFormat
)) {
1973 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1976 else if (is_ycbcr_format(format
)
1977 && !is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
1978 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1983 const GLint width
= texImage
->Width
;
1984 const GLint height
= texImage
->Height
;
1985 const GLint depth
= texImage
->Depth
;
1987 for (img
= 0; img
< depth
; img
++) {
1988 for (row
= 0; row
< height
; row
++) {
1989 /* compute destination address in client memory */
1990 GLvoid
*dest
= _mesa_image_address( &ctx
->Pack
, pixels
,
1991 width
, height
, format
, type
,
1995 if (format
== GL_COLOR_INDEX
) {
1996 GLuint indexRow
[MAX_WIDTH
];
1998 /* Can't use FetchTexel here because that returns RGBA */
1999 if (texImage
->TexFormat
->IndexBits
== 8) {
2000 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
2001 for (col
= 0; col
< width
; col
++) {
2002 indexRow
[col
] = src
[texImage
->Width
*
2003 (img
* texImage
->Height
+ row
) + col
];
2006 else if (texImage
->TexFormat
->IndexBits
== 16) {
2007 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
2008 for (col
= 0; col
< width
; col
++) {
2009 indexRow
[col
] = src
[texImage
->Width
*
2010 (img
* texImage
->Height
+ row
) + col
];
2015 "Color index problem in _mesa_GetTexImage");
2018 _mesa_pack_index_span(ctx
, width
, type
, dest
,
2019 indexRow
, &ctx
->Pack
,
2020 0 /* no image transfer */);
2022 else if (format
== GL_DEPTH_COMPONENT
) {
2023 GLfloat depthRow
[MAX_WIDTH
];
2025 for (col
= 0; col
< width
; col
++) {
2026 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
,
2029 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
2030 depthRow
, &ctx
->Pack
);
2032 else if (format
== GL_YCBCR_MESA
) {
2033 /* No pixel transfer */
2034 const GLint rowstride
= texImage
->RowStride
;
2036 (const GLushort
*) texImage
->Data
+ row
* rowstride
,
2037 width
* sizeof(GLushort
));
2038 /* check for byte swapping */
2039 if ((texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR
2040 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
2041 (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR_REV
2042 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
2043 if (!ctx
->Pack
.SwapBytes
)
2044 _mesa_swap2((GLushort
*) dest
, width
);
2046 else if (ctx
->Pack
.SwapBytes
) {
2047 _mesa_swap2((GLushort
*) dest
, width
);
2051 /* general case: convert row to RGBA format */
2052 GLfloat rgba
[MAX_WIDTH
][4];
2054 for (col
= 0; col
< width
; col
++) {
2055 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
2057 _mesa_pack_rgba_span_float(ctx
, width
,
2058 (const GLfloat (*)[4]) rgba
,
2059 format
, type
, dest
, &ctx
->Pack
,
2060 0 /* no image transfer */);
2070 * Called from the API. Note that width includes the border.
2073 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2074 GLsizei width
, GLint border
, GLenum format
,
2075 GLenum type
, const GLvoid
*pixels
)
2077 GLsizei postConvWidth
= width
;
2078 GET_CURRENT_CONTEXT(ctx
);
2079 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2081 if (is_color_format(internalFormat
)) {
2082 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2085 if (target
== GL_TEXTURE_1D
) {
2086 struct gl_texture_unit
*texUnit
;
2087 struct gl_texture_object
*texObj
;
2088 struct gl_texture_image
*texImage
;
2090 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2091 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2092 return; /* error was recorded */
2095 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2096 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2097 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2100 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2103 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2104 /* free the old texture data */
2105 MESA_PBUFFER_FREE(texImage
->Data
);
2107 texImage
->Data
= NULL
;
2108 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2109 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2110 postConvWidth
, 1, 1,
2111 border
, internalFormat
);
2113 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2114 _mesa_update_state(ctx
);
2116 ASSERT(ctx
->Driver
.TexImage1D
);
2118 /* Give the texture to the driver! <pixels> may be null! */
2119 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
2120 width
, border
, format
, type
, pixels
,
2121 &ctx
->Unpack
, texObj
, texImage
);
2123 ASSERT(texImage
->TexFormat
);
2125 /* If driver didn't explicitly set this, use the defaults */
2126 if (!texImage
->FetchTexelc
)
2127 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2128 if (!texImage
->FetchTexelf
)
2129 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2130 ASSERT(texImage
->FetchTexelc
);
2131 ASSERT(texImage
->FetchTexelf
);
2134 texObj
->Complete
= GL_FALSE
;
2135 ctx
->NewState
|= _NEW_TEXTURE
;
2137 else if (target
== GL_PROXY_TEXTURE_1D
) {
2138 /* Proxy texture: check for errors and update proxy state */
2139 struct gl_texture_image
*texImage
;
2140 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2141 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2142 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2143 /* when error, clear all proxy texture image parameters */
2145 clear_teximage_fields(texImage
);
2148 /* no error, set the tex image parameters */
2150 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2151 postConvWidth
, 1, 1,
2152 border
, internalFormat
);
2153 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2154 internalFormat
, format
, type
);
2158 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2165 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2166 GLsizei width
, GLsizei height
, GLint border
,
2167 GLenum format
, GLenum type
,
2168 const GLvoid
*pixels
)
2170 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2171 GET_CURRENT_CONTEXT(ctx
);
2172 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2174 if (is_color_format(internalFormat
)) {
2175 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2179 if (target
== GL_TEXTURE_2D
||
2180 (ctx
->Extensions
.ARB_texture_cube_map
&&
2181 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2182 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2183 (ctx
->Extensions
.NV_texture_rectangle
&&
2184 target
== GL_TEXTURE_RECTANGLE_NV
)) {
2185 /* non-proxy target */
2186 struct gl_texture_unit
*texUnit
;
2187 struct gl_texture_object
*texObj
;
2188 struct gl_texture_image
*texImage
;
2190 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2191 format
, type
, 2, postConvWidth
, postConvHeight
,
2193 return; /* error was recorded */
2196 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2197 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2198 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2200 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2203 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2204 /* free the old texture data */
2205 MESA_PBUFFER_FREE(texImage
->Data
);
2207 texImage
->Data
= NULL
;
2208 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2209 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2210 postConvWidth
, postConvHeight
, 1,
2211 border
, internalFormat
);
2213 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2214 _mesa_update_state(ctx
);
2216 ASSERT(ctx
->Driver
.TexImage2D
);
2218 /* Give the texture to the driver! <pixels> may be null! */
2219 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
2220 width
, height
, border
, format
, type
, pixels
,
2221 &ctx
->Unpack
, texObj
, texImage
);
2223 ASSERT(texImage
->TexFormat
);
2225 /* If driver didn't explicitly set these, use the defaults */
2226 if (!texImage
->FetchTexelc
)
2227 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2228 if (!texImage
->FetchTexelf
)
2229 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2230 ASSERT(texImage
->FetchTexelc
);
2231 ASSERT(texImage
->FetchTexelf
);
2234 texObj
->Complete
= GL_FALSE
;
2235 ctx
->NewState
|= _NEW_TEXTURE
;
2237 else if (target
== GL_PROXY_TEXTURE_2D
||
2238 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2239 ctx
->Extensions
.ARB_texture_cube_map
) ||
2240 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2241 ctx
->Extensions
.NV_texture_rectangle
)) {
2242 /* Proxy texture: check for errors and update proxy state */
2243 struct gl_texture_image
*texImage
;
2244 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2245 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2246 format
, type
, 2, postConvWidth
, postConvHeight
,
2248 /* when error, clear all proxy texture image parameters */
2250 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[0][level
]);
2253 /* no error, set the tex image parameters */
2254 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2255 postConvWidth
, postConvHeight
, 1,
2256 border
, internalFormat
);
2257 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2258 internalFormat
, format
, type
);
2262 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2269 * Called by the API or display list executor.
2270 * Note that width and height include the border.
2273 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2274 GLsizei width
, GLsizei height
, GLsizei depth
,
2275 GLint border
, GLenum format
, GLenum type
,
2276 const GLvoid
*pixels
)
2278 GET_CURRENT_CONTEXT(ctx
);
2279 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2281 if (target
== GL_TEXTURE_3D
) {
2282 struct gl_texture_unit
*texUnit
;
2283 struct gl_texture_object
*texObj
;
2284 struct gl_texture_image
*texImage
;
2286 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2287 format
, type
, 3, width
, height
, depth
, border
)) {
2288 return; /* error was recorded */
2291 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2292 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2293 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2295 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2298 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2299 MESA_PBUFFER_FREE(texImage
->Data
);
2301 texImage
->Data
= NULL
;
2302 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2303 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2304 width
, height
, depth
,
2305 border
, internalFormat
);
2307 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2308 _mesa_update_state(ctx
);
2310 ASSERT(ctx
->Driver
.TexImage3D
);
2312 /* Give the texture to the driver! <pixels> may be null! */
2313 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, internalFormat
,
2314 width
, height
, depth
, border
, format
, type
,
2315 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2317 ASSERT(texImage
->TexFormat
);
2319 /* If driver didn't explicitly set these, use the defaults */
2320 if (!texImage
->FetchTexelc
)
2321 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2322 if (!texImage
->FetchTexelf
)
2323 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2324 ASSERT(texImage
->FetchTexelc
);
2325 ASSERT(texImage
->FetchTexelf
);
2328 texObj
->Complete
= GL_FALSE
;
2329 ctx
->NewState
|= _NEW_TEXTURE
;
2331 else if (target
== GL_PROXY_TEXTURE_3D
) {
2332 /* Proxy texture: check for errors and update proxy state */
2333 struct gl_texture_image
*texImage
;
2334 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2335 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2336 format
, type
, 3, width
, height
, depth
, border
)) {
2337 /* when error, clear all proxy texture image parameters */
2339 clear_teximage_fields(texImage
);
2342 /* no error, set the tex image parameters */
2343 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2344 border
, internalFormat
);
2345 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2346 internalFormat
, format
, type
);
2350 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2357 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2358 GLsizei width
, GLsizei height
, GLsizei depth
,
2359 GLint border
, GLenum format
, GLenum type
,
2360 const GLvoid
*pixels
)
2362 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2363 depth
, border
, format
, type
, pixels
);
2369 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2370 GLint xoffset
, GLsizei width
,
2371 GLenum format
, GLenum type
,
2372 const GLvoid
*pixels
)
2374 GLsizei postConvWidth
= width
;
2375 struct gl_texture_unit
*texUnit
;
2376 struct gl_texture_object
*texObj
;
2377 struct gl_texture_image
*texImage
;
2378 GET_CURRENT_CONTEXT(ctx
);
2379 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2381 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2382 _mesa_update_state(ctx
);
2384 /* XXX should test internal format */
2385 if (is_color_format(format
)) {
2386 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2389 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2390 postConvWidth
, 1, 1, format
, type
)) {
2391 return; /* error was detected */
2394 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2395 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2396 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2399 if (width
== 0 || !pixels
)
2400 return; /* no-op, not an error */
2402 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2403 xoffset
+= texImage
->Border
;
2405 ASSERT(ctx
->Driver
.TexSubImage1D
);
2406 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
2407 format
, type
, pixels
, &ctx
->Unpack
,
2409 ctx
->NewState
|= _NEW_TEXTURE
;
2414 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2415 GLint xoffset
, GLint yoffset
,
2416 GLsizei width
, GLsizei height
,
2417 GLenum format
, GLenum type
,
2418 const GLvoid
*pixels
)
2420 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2421 struct gl_texture_unit
*texUnit
;
2422 struct gl_texture_object
*texObj
;
2423 struct gl_texture_image
*texImage
;
2424 GET_CURRENT_CONTEXT(ctx
);
2425 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2427 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2428 _mesa_update_state(ctx
);
2430 /* XXX should test internal format */
2431 if (is_color_format(format
)) {
2432 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2436 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2437 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2438 return; /* error was detected */
2441 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2442 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2443 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2446 if (width
== 0 || height
== 0 || !pixels
)
2447 return; /* no-op, not an error */
2449 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2450 xoffset
+= texImage
->Border
;
2451 yoffset
+= texImage
->Border
;
2453 ASSERT(ctx
->Driver
.TexSubImage2D
);
2454 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
2455 width
, height
, format
, type
, pixels
,
2456 &ctx
->Unpack
, texObj
, texImage
);
2457 ctx
->NewState
|= _NEW_TEXTURE
;
2463 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2464 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2465 GLsizei width
, GLsizei height
, GLsizei depth
,
2466 GLenum format
, GLenum type
,
2467 const GLvoid
*pixels
)
2469 struct gl_texture_unit
*texUnit
;
2470 struct gl_texture_object
*texObj
;
2471 struct gl_texture_image
*texImage
;
2472 GET_CURRENT_CONTEXT(ctx
);
2473 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2475 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2476 _mesa_update_state(ctx
);
2478 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2479 width
, height
, depth
, format
, type
)) {
2480 return; /* error was detected */
2483 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2484 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2485 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2488 if (width
== 0 || height
== 0 || height
== 0 || !pixels
)
2489 return; /* no-op, not an error */
2491 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2492 xoffset
+= texImage
->Border
;
2493 yoffset
+= texImage
->Border
;
2494 zoffset
+= texImage
->Border
;
2496 ASSERT(ctx
->Driver
.TexSubImage3D
);
2497 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
2498 xoffset
, yoffset
, zoffset
,
2499 width
, height
, depth
,
2500 format
, type
, pixels
,
2501 &ctx
->Unpack
, texObj
, texImage
);
2502 ctx
->NewState
|= _NEW_TEXTURE
;
2508 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2509 GLenum internalFormat
,
2511 GLsizei width
, GLint border
)
2513 struct gl_texture_unit
*texUnit
;
2514 struct gl_texture_object
*texObj
;
2515 struct gl_texture_image
*texImage
;
2516 GLsizei postConvWidth
= width
;
2517 GET_CURRENT_CONTEXT(ctx
);
2518 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2520 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2521 _mesa_update_state(ctx
);
2523 if (is_color_format(internalFormat
)) {
2524 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2527 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2528 postConvWidth
, 1, border
))
2531 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2532 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2533 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2535 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2538 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2539 /* free the old texture data */
2540 MESA_PBUFFER_FREE(texImage
->Data
);
2542 texImage
->Data
= NULL
;
2544 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2545 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2546 border
, internalFormat
);
2549 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2550 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
2551 x
, y
, width
, border
);
2553 ASSERT(texImage
->TexFormat
);
2555 /* If driver didn't explicitly set these, use the defaults */
2556 if (!texImage
->FetchTexelc
)
2557 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2558 if (!texImage
->FetchTexelf
)
2559 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2560 ASSERT(texImage
->FetchTexelc
);
2561 ASSERT(texImage
->FetchTexelf
);
2564 texObj
->Complete
= GL_FALSE
;
2565 ctx
->NewState
|= _NEW_TEXTURE
;
2571 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2572 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2575 struct gl_texture_unit
*texUnit
;
2576 struct gl_texture_object
*texObj
;
2577 struct gl_texture_image
*texImage
;
2578 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2579 GET_CURRENT_CONTEXT(ctx
);
2580 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2582 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2583 _mesa_update_state(ctx
);
2585 if (is_color_format(internalFormat
)) {
2586 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2590 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2591 postConvWidth
, postConvHeight
, border
))
2594 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2595 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2596 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2598 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2601 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2602 /* free the old texture data */
2603 MESA_PBUFFER_FREE(texImage
->Data
);
2605 texImage
->Data
= NULL
;
2607 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2608 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2609 postConvWidth
, postConvHeight
, 1,
2610 border
, internalFormat
);
2612 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2613 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
2614 x
, y
, width
, height
, border
);
2616 ASSERT(texImage
->TexFormat
);
2618 /* If driver didn't explicitly set these, use the defaults */
2619 if (!texImage
->FetchTexelc
)
2620 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2621 if (!texImage
->FetchTexelf
)
2622 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2623 ASSERT(texImage
->FetchTexelc
);
2624 ASSERT(texImage
->FetchTexelf
);
2627 texObj
->Complete
= GL_FALSE
;
2628 ctx
->NewState
|= _NEW_TEXTURE
;
2634 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2635 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2637 struct gl_texture_unit
*texUnit
;
2638 struct gl_texture_image
*texImage
;
2639 GLsizei postConvWidth
= width
;
2640 GET_CURRENT_CONTEXT(ctx
);
2641 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2643 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2644 _mesa_update_state(ctx
);
2646 /* XXX should test internal format */
2647 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2649 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
2650 xoffset
, 0, 0, postConvWidth
, 1))
2653 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2654 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2657 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2658 xoffset
+= texImage
->Border
;
2660 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2661 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
2662 ctx
->NewState
|= _NEW_TEXTURE
;
2668 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2669 GLint xoffset
, GLint yoffset
,
2670 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2672 struct gl_texture_unit
*texUnit
;
2673 struct gl_texture_image
*texImage
;
2674 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2675 GET_CURRENT_CONTEXT(ctx
);
2676 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2678 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2679 _mesa_update_state(ctx
);
2681 /* XXX should test internal format */
2682 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2684 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2685 postConvWidth
, postConvHeight
))
2688 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2689 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2692 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2693 xoffset
+= texImage
->Border
;
2694 yoffset
+= texImage
->Border
;
2696 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
2697 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
2698 xoffset
, yoffset
, x
, y
, width
, height
);
2699 ctx
->NewState
|= _NEW_TEXTURE
;
2705 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2706 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2707 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2709 struct gl_texture_unit
*texUnit
;
2710 struct gl_texture_image
*texImage
;
2711 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2712 GET_CURRENT_CONTEXT(ctx
);
2713 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2715 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2716 _mesa_update_state(ctx
);
2718 /* XXX should test internal format */
2719 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2721 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2722 zoffset
, postConvWidth
, postConvHeight
))
2725 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2726 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2729 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2730 xoffset
+= texImage
->Border
;
2731 yoffset
+= texImage
->Border
;
2732 zoffset
+= texImage
->Border
;
2734 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2735 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2736 xoffset
, yoffset
, zoffset
,
2737 x
, y
, width
, height
);
2738 ctx
->NewState
|= _NEW_TEXTURE
;
2744 /**********************************************************************/
2745 /****** Compressed Textures ******/
2746 /**********************************************************************/
2750 * Error checking for glCompressedTexImage[123]D().
2751 * \return error code or GL_NO_ERROR.
2754 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
2755 GLenum target
, GLint level
,
2756 GLenum internalFormat
, GLsizei width
,
2757 GLsizei height
, GLsizei depth
, GLint border
,
2760 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2762 if (dimensions
== 1) {
2763 /* 1D compressed textures not allowed */
2764 return GL_INVALID_ENUM
;
2766 else if (dimensions
== 2) {
2767 if (target
== GL_PROXY_TEXTURE_2D
) {
2768 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2770 else if (target
== GL_TEXTURE_2D
) {
2771 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2773 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2774 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2775 return GL_INVALID_ENUM
; /*target*/
2776 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2778 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2779 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2780 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2781 return GL_INVALID_ENUM
; /*target*/
2782 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2785 return GL_INVALID_ENUM
; /*target*/
2788 else if (dimensions
== 3) {
2789 /* 3D compressed textures not allowed */
2790 return GL_INVALID_ENUM
;
2793 maxTextureSize
= 1 << (maxLevels
- 1);
2795 if (!is_compressed_format(ctx
, internalFormat
))
2796 return GL_INVALID_ENUM
;
2799 return GL_INVALID_VALUE
;
2802 * XXX We should probably use the proxy texture error check function here.
2804 if (width
< 1 || width
> maxTextureSize
||
2805 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(width
) < 0))
2806 return GL_INVALID_VALUE
;
2808 if ((height
< 1 || height
> maxTextureSize
||
2809 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(height
) < 0))
2811 return GL_INVALID_VALUE
;
2813 if ((depth
< 1 || depth
> maxTextureSize
||
2814 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(depth
) < 0))
2816 return GL_INVALID_VALUE
;
2818 /* For cube map, width must equal height */
2819 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2820 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
2821 return GL_INVALID_VALUE
;
2823 if (level
< 0 || level
>= maxLevels
)
2824 return GL_INVALID_VALUE
;
2826 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2828 if (expectedSize
!= imageSize
)
2829 return GL_INVALID_VALUE
;
2836 * Error checking for glCompressedTexSubImage[123]D().
2837 * \warning There are some bad assumptions here about the size of compressed
2838 * texture tiles (multiple of 4) used to test the validity of the
2839 * offset and size parameters.
2840 * \return error code or GL_NO_ERROR.
2843 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
2844 GLenum target
, GLint level
,
2845 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2846 GLsizei width
, GLsizei height
, GLsizei depth
,
2847 GLenum format
, GLsizei imageSize
)
2849 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2852 if (dimensions
== 1) {
2853 /* 1D compressed textures not allowed */
2854 return GL_INVALID_ENUM
;
2856 else if (dimensions
== 2) {
2857 if (target
== GL_PROXY_TEXTURE_2D
) {
2858 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2860 else if (target
== GL_TEXTURE_2D
) {
2861 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2863 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2864 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2865 return GL_INVALID_ENUM
; /*target*/
2866 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2868 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2869 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2870 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2871 return GL_INVALID_ENUM
; /*target*/
2872 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2875 return GL_INVALID_ENUM
; /*target*/
2878 else if (dimensions
== 3) {
2879 /* 3D compressed textures not allowed */
2880 return GL_INVALID_ENUM
;
2883 maxTextureSize
= 1 << (maxLevels
- 1);
2885 if (!is_compressed_format(ctx
, format
))
2886 return GL_INVALID_ENUM
;
2888 if (width
< 1 || width
> maxTextureSize
)
2889 return GL_INVALID_VALUE
;
2891 if ((height
< 1 || height
> maxTextureSize
)
2893 return GL_INVALID_VALUE
;
2895 if (level
< 0 || level
>= maxLevels
)
2896 return GL_INVALID_VALUE
;
2898 /* XXX these tests are specific to the compressed format.
2899 * this code should be generalized in some way.
2901 if ((xoffset
& 3) != 0 || (yoffset
& 3) != 0)
2902 return GL_INVALID_VALUE
;
2904 if ((width
& 3) != 0 && width
!= 2 && width
!= 1)
2905 return GL_INVALID_VALUE
;
2907 if ((height
& 3) != 0 && height
!= 2 && height
!= 1)
2908 return GL_INVALID_VALUE
;
2910 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2912 if (expectedSize
!= imageSize
)
2913 return GL_INVALID_VALUE
;
2921 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2922 GLenum internalFormat
, GLsizei width
,
2923 GLint border
, GLsizei imageSize
,
2926 GET_CURRENT_CONTEXT(ctx
);
2927 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2929 if (target
== GL_TEXTURE_1D
) {
2930 struct gl_texture_unit
*texUnit
;
2931 struct gl_texture_object
*texObj
;
2932 struct gl_texture_image
*texImage
;
2933 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2934 internalFormat
, width
, 1, 1, border
, imageSize
);
2936 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
2940 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2941 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2942 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2944 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
2947 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2948 MESA_PBUFFER_FREE(texImage
->Data
);
2950 texImage
->Data
= NULL
;
2952 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2953 border
, internalFormat
);
2955 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2956 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2957 internalFormat
, width
, border
,
2962 texObj
->Complete
= GL_FALSE
;
2963 ctx
->NewState
|= _NEW_TEXTURE
;
2965 else if (target
== GL_PROXY_TEXTURE_1D
) {
2966 /* Proxy texture: check for errors and update proxy state */
2967 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2968 internalFormat
, width
, 1, 1, border
, imageSize
);
2970 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2971 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2972 internalFormat
, GL_NONE
, GL_NONE
,
2973 width
, 1, 1, border
);
2976 /* if error, clear all proxy texture image parameters */
2977 struct gl_texture_image
*texImage
;
2978 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2980 clear_teximage_fields(texImage
);
2983 /* store the teximage parameters */
2984 struct gl_texture_unit
*texUnit
;
2985 struct gl_texture_image
*texImage
;
2986 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2987 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2988 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2989 border
, internalFormat
);
2993 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
3000 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3001 GLenum internalFormat
, GLsizei width
,
3002 GLsizei height
, GLint border
, GLsizei imageSize
,
3005 GET_CURRENT_CONTEXT(ctx
);
3006 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3008 if (target
== GL_TEXTURE_2D
||
3009 (ctx
->Extensions
.ARB_texture_cube_map
&&
3010 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3011 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
3012 struct gl_texture_unit
*texUnit
;
3013 struct gl_texture_object
*texObj
;
3014 struct gl_texture_image
*texImage
;
3015 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3016 internalFormat
, width
, height
, 1, border
, imageSize
);
3018 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
3022 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3023 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3024 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3026 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
3029 else if (texImage
->Data
&& !texImage
->IsClientData
) {
3030 MESA_PBUFFER_FREE(texImage
->Data
);
3032 texImage
->Data
= NULL
;
3034 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3035 border
, internalFormat
);
3037 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3038 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
3039 internalFormat
, width
, height
,
3040 border
, imageSize
, data
,
3044 texObj
->Complete
= GL_FALSE
;
3045 ctx
->NewState
|= _NEW_TEXTURE
;
3047 else if (target
== GL_PROXY_TEXTURE_2D
||
3048 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
3049 ctx
->Extensions
.ARB_texture_cube_map
)) {
3050 /* Proxy texture: check for errors and update proxy state */
3051 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3052 internalFormat
, width
, height
, 1, border
, imageSize
);
3054 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3055 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3056 internalFormat
, GL_NONE
, GL_NONE
,
3057 width
, height
, 1, border
);
3060 /* if error, clear all proxy texture image parameters */
3061 struct gl_texture_image
*texImage
;
3062 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3064 clear_teximage_fields(texImage
);
3067 /* store the teximage parameters */
3068 struct gl_texture_unit
*texUnit
;
3069 struct gl_texture_image
*texImage
;
3070 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3071 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3072 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3073 border
, internalFormat
);
3077 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3084 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3085 GLenum internalFormat
, GLsizei width
,
3086 GLsizei height
, GLsizei depth
, GLint border
,
3087 GLsizei imageSize
, const GLvoid
*data
)
3089 GET_CURRENT_CONTEXT(ctx
);
3090 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3092 if (target
== GL_TEXTURE_3D
) {
3093 struct gl_texture_unit
*texUnit
;
3094 struct gl_texture_object
*texObj
;
3095 struct gl_texture_image
*texImage
;
3096 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3097 internalFormat
, width
, height
, depth
, border
, imageSize
);
3099 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3103 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3104 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3105 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3107 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3110 else if (texImage
->Data
&& !texImage
->IsClientData
) {
3111 MESA_PBUFFER_FREE(texImage
->Data
);
3113 texImage
->Data
= NULL
;
3115 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, depth
,
3116 border
, internalFormat
);
3118 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3119 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
3121 width
, height
, depth
,
3122 border
, imageSize
, data
,
3126 texObj
->Complete
= GL_FALSE
;
3127 ctx
->NewState
|= _NEW_TEXTURE
;
3129 else if (target
== GL_PROXY_TEXTURE_3D
) {
3130 /* Proxy texture: check for errors and update proxy state */
3131 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3132 internalFormat
, width
, height
, depth
, border
, imageSize
);
3134 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3135 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3136 internalFormat
, GL_NONE
, GL_NONE
,
3137 width
, height
, depth
, border
);
3140 /* if error, clear all proxy texture image parameters */
3141 struct gl_texture_image
*texImage
;
3142 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3144 clear_teximage_fields(texImage
);
3147 /* store the teximage parameters */
3148 struct gl_texture_unit
*texUnit
;
3149 struct gl_texture_image
*texImage
;
3150 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3151 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3152 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3153 depth
, border
, internalFormat
);
3157 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3164 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3165 GLsizei width
, GLenum format
,
3166 GLsizei imageSize
, const GLvoid
*data
)
3168 struct gl_texture_unit
*texUnit
;
3169 struct gl_texture_object
*texObj
;
3170 struct gl_texture_image
*texImage
;
3172 GET_CURRENT_CONTEXT(ctx
);
3173 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3175 error
= compressed_subtexture_error_check(ctx
, 1, target
, level
,
3176 xoffset
, 0, 0, width
, 1, 1, format
, imageSize
);
3178 _mesa_error(ctx
, error
, "glCompressedTexSubImage1D");
3182 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3183 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3184 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3187 if ((GLint
) format
!= texImage
->IntFormat
) {
3188 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3189 "glCompressedTexSubImage1D(format)");
3193 if ((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) {
3194 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage1D(width)");
3198 if (width
== 0 || !data
)
3199 return; /* no-op, not an error */
3201 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3202 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
3204 format
, imageSize
, data
,
3207 ctx
->NewState
|= _NEW_TEXTURE
;
3212 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3213 GLint yoffset
, GLsizei width
, GLsizei height
,
3214 GLenum format
, GLsizei imageSize
,
3217 struct gl_texture_unit
*texUnit
;
3218 struct gl_texture_object
*texObj
;
3219 struct gl_texture_image
*texImage
;
3221 GET_CURRENT_CONTEXT(ctx
);
3222 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3224 error
= compressed_subtexture_error_check(ctx
, 2, target
, level
,
3225 xoffset
, yoffset
, 0, width
, height
, 1, format
, imageSize
);
3227 /* XXX proxy target? */
3228 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3232 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3233 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3234 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3237 if ((GLint
) format
!= texImage
->IntFormat
) {
3238 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3239 "glCompressedTexSubImage2D(format)");
3243 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3244 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
)) {
3245 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage2D(size)");
3249 if (width
== 0 || height
== 0 || !data
)
3250 return; /* no-op, not an error */
3252 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3253 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
3254 xoffset
, yoffset
, width
, height
,
3255 format
, imageSize
, data
,
3258 ctx
->NewState
|= _NEW_TEXTURE
;
3263 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3264 GLint yoffset
, GLint zoffset
, GLsizei width
,
3265 GLsizei height
, GLsizei depth
, GLenum format
,
3266 GLsizei imageSize
, const GLvoid
*data
)
3268 struct gl_texture_unit
*texUnit
;
3269 struct gl_texture_object
*texObj
;
3270 struct gl_texture_image
*texImage
;
3272 GET_CURRENT_CONTEXT(ctx
);
3273 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3275 error
= compressed_subtexture_error_check(ctx
, 3, target
, level
,
3276 xoffset
, yoffset
, zoffset
, width
, height
, depth
, format
, imageSize
);
3278 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3282 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3283 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3284 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3287 if ((GLint
) format
!= texImage
->IntFormat
) {
3288 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3289 "glCompressedTexSubImage3D(format)");
3293 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3294 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
) ||
3295 ((depth
== 1 || depth
== 2) && (GLuint
) depth
!= texImage
->Depth
)) {
3296 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage3D(size)");
3300 if (width
== 0 || height
== 0 || depth
== 0 || !data
)
3301 return; /* no-op, not an error */
3303 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3304 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
3305 xoffset
, yoffset
, zoffset
,
3306 width
, height
, depth
,
3307 format
, imageSize
, data
,
3310 ctx
->NewState
|= _NEW_TEXTURE
;
3315 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
3317 const struct gl_texture_unit
*texUnit
;
3318 const struct gl_texture_object
*texObj
;
3319 struct gl_texture_image
*texImage
;
3321 GET_CURRENT_CONTEXT(ctx
);
3322 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3324 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3325 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3327 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
3331 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3332 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
3334 if (level
< 0 || level
>= maxLevels
) {
3335 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3339 if (is_proxy_target(target
)) {
3340 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
3344 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3346 /* probably invalid mipmap level */
3347 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3351 if (!texImage
->IsCompressed
) {
3352 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
3359 /* just memcpy, no pixelstore or pixel transfer */
3360 MEMCPY(img
, texImage
->Data
, texImage
->CompressedSize
);