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 * XXX Move this code into a new driver fall-back function
1986 const GLint width
= texImage
->Width
;
1987 const GLint height
= texImage
->Height
;
1988 const GLint depth
= texImage
->Depth
;
1990 for (img
= 0; img
< depth
; img
++) {
1991 for (row
= 0; row
< height
; row
++) {
1992 /* compute destination address in client memory */
1993 GLvoid
*dest
= _mesa_image_address( &ctx
->Pack
, pixels
,
1994 width
, height
, format
, type
,
1998 if (format
== GL_COLOR_INDEX
) {
1999 GLuint indexRow
[MAX_WIDTH
];
2001 /* Can't use FetchTexel here because that returns RGBA */
2002 if (texImage
->TexFormat
->IndexBits
== 8) {
2003 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
2004 for (col
= 0; col
< width
; col
++) {
2005 indexRow
[col
] = src
[texImage
->Width
*
2006 (img
* texImage
->Height
+ row
) + col
];
2009 else if (texImage
->TexFormat
->IndexBits
== 16) {
2010 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
2011 for (col
= 0; col
< width
; col
++) {
2012 indexRow
[col
] = src
[texImage
->Width
*
2013 (img
* texImage
->Height
+ row
) + col
];
2018 "Color index problem in _mesa_GetTexImage");
2021 _mesa_pack_index_span(ctx
, width
, type
, dest
,
2022 indexRow
, &ctx
->Pack
,
2023 0 /* no image transfer */);
2025 else if (format
== GL_DEPTH_COMPONENT
) {
2026 GLfloat depthRow
[MAX_WIDTH
];
2028 for (col
= 0; col
< width
; col
++) {
2029 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
,
2032 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
2033 depthRow
, &ctx
->Pack
);
2035 else if (format
== GL_YCBCR_MESA
) {
2036 /* No pixel transfer */
2037 const GLint rowstride
= texImage
->RowStride
;
2039 (const GLushort
*) texImage
->Data
+ row
* rowstride
,
2040 width
* sizeof(GLushort
));
2041 /* check for byte swapping */
2042 if ((texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR
2043 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
2044 (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR_REV
2045 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
2046 if (!ctx
->Pack
.SwapBytes
)
2047 _mesa_swap2((GLushort
*) dest
, width
);
2049 else if (ctx
->Pack
.SwapBytes
) {
2050 _mesa_swap2((GLushort
*) dest
, width
);
2054 /* general case: convert row to RGBA format */
2055 GLfloat rgba
[MAX_WIDTH
][4];
2057 for (col
= 0; col
< width
; col
++) {
2058 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
2060 _mesa_pack_rgba_span_float(ctx
, width
,
2061 (const GLfloat (*)[4]) rgba
,
2062 format
, type
, dest
, &ctx
->Pack
,
2063 0 /* no image transfer */);
2073 * Called from the API. Note that width includes the border.
2076 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2077 GLsizei width
, GLint border
, GLenum format
,
2078 GLenum type
, const GLvoid
*pixels
)
2080 GLsizei postConvWidth
= width
;
2081 GET_CURRENT_CONTEXT(ctx
);
2082 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2084 if (is_color_format(internalFormat
)) {
2085 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2088 if (target
== GL_TEXTURE_1D
) {
2089 struct gl_texture_unit
*texUnit
;
2090 struct gl_texture_object
*texObj
;
2091 struct gl_texture_image
*texImage
;
2093 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2094 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2095 return; /* error was recorded */
2098 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2099 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2100 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2103 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2106 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2107 /* free the old texture data */
2108 MESA_PBUFFER_FREE(texImage
->Data
);
2110 texImage
->Data
= NULL
;
2111 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2112 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2113 postConvWidth
, 1, 1,
2114 border
, internalFormat
);
2116 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2117 _mesa_update_state(ctx
);
2119 ASSERT(ctx
->Driver
.TexImage1D
);
2121 /* Give the texture to the driver! <pixels> may be null! */
2122 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
2123 width
, border
, format
, type
, pixels
,
2124 &ctx
->Unpack
, texObj
, texImage
);
2126 ASSERT(texImage
->TexFormat
);
2128 /* If driver didn't explicitly set this, use the defaults */
2129 if (!texImage
->FetchTexelc
)
2130 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2131 if (!texImage
->FetchTexelf
)
2132 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2133 ASSERT(texImage
->FetchTexelc
);
2134 ASSERT(texImage
->FetchTexelf
);
2137 texObj
->Complete
= GL_FALSE
;
2138 ctx
->NewState
|= _NEW_TEXTURE
;
2140 else if (target
== GL_PROXY_TEXTURE_1D
) {
2141 /* Proxy texture: check for errors and update proxy state */
2142 struct gl_texture_image
*texImage
;
2143 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2144 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2145 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2146 /* when error, clear all proxy texture image parameters */
2148 clear_teximage_fields(texImage
);
2151 /* no error, set the tex image parameters */
2153 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2154 postConvWidth
, 1, 1,
2155 border
, internalFormat
);
2156 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2157 internalFormat
, format
, type
);
2161 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2168 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2169 GLsizei width
, GLsizei height
, GLint border
,
2170 GLenum format
, GLenum type
,
2171 const GLvoid
*pixels
)
2173 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2174 GET_CURRENT_CONTEXT(ctx
);
2175 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2177 if (is_color_format(internalFormat
)) {
2178 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2182 if (target
== GL_TEXTURE_2D
||
2183 (ctx
->Extensions
.ARB_texture_cube_map
&&
2184 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2185 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2186 (ctx
->Extensions
.NV_texture_rectangle
&&
2187 target
== GL_TEXTURE_RECTANGLE_NV
)) {
2188 /* non-proxy target */
2189 struct gl_texture_unit
*texUnit
;
2190 struct gl_texture_object
*texObj
;
2191 struct gl_texture_image
*texImage
;
2193 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2194 format
, type
, 2, postConvWidth
, postConvHeight
,
2196 return; /* error was recorded */
2199 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2200 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2201 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2203 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2206 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2207 /* free the old texture data */
2208 MESA_PBUFFER_FREE(texImage
->Data
);
2210 texImage
->Data
= NULL
;
2211 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2212 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2213 postConvWidth
, postConvHeight
, 1,
2214 border
, internalFormat
);
2216 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2217 _mesa_update_state(ctx
);
2219 ASSERT(ctx
->Driver
.TexImage2D
);
2221 /* Give the texture to the driver! <pixels> may be null! */
2222 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
2223 width
, height
, border
, format
, type
, pixels
,
2224 &ctx
->Unpack
, texObj
, texImage
);
2226 ASSERT(texImage
->TexFormat
);
2228 /* If driver didn't explicitly set these, use the defaults */
2229 if (!texImage
->FetchTexelc
)
2230 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2231 if (!texImage
->FetchTexelf
)
2232 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2233 ASSERT(texImage
->FetchTexelc
);
2234 ASSERT(texImage
->FetchTexelf
);
2237 texObj
->Complete
= GL_FALSE
;
2238 ctx
->NewState
|= _NEW_TEXTURE
;
2240 else if (target
== GL_PROXY_TEXTURE_2D
||
2241 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2242 ctx
->Extensions
.ARB_texture_cube_map
) ||
2243 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2244 ctx
->Extensions
.NV_texture_rectangle
)) {
2245 /* Proxy texture: check for errors and update proxy state */
2246 struct gl_texture_image
*texImage
;
2247 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2248 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2249 format
, type
, 2, postConvWidth
, postConvHeight
,
2251 /* when error, clear all proxy texture image parameters */
2253 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[0][level
]);
2256 /* no error, set the tex image parameters */
2257 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2258 postConvWidth
, postConvHeight
, 1,
2259 border
, internalFormat
);
2260 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2261 internalFormat
, format
, type
);
2265 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2272 * Called by the API or display list executor.
2273 * Note that width and height include the border.
2276 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2277 GLsizei width
, GLsizei height
, GLsizei depth
,
2278 GLint border
, GLenum format
, GLenum type
,
2279 const GLvoid
*pixels
)
2281 GET_CURRENT_CONTEXT(ctx
);
2282 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2284 if (target
== GL_TEXTURE_3D
) {
2285 struct gl_texture_unit
*texUnit
;
2286 struct gl_texture_object
*texObj
;
2287 struct gl_texture_image
*texImage
;
2289 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2290 format
, type
, 3, width
, height
, depth
, border
)) {
2291 return; /* error was recorded */
2294 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2295 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2296 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2298 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2301 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2302 MESA_PBUFFER_FREE(texImage
->Data
);
2304 texImage
->Data
= NULL
;
2305 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2306 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2307 width
, height
, depth
,
2308 border
, internalFormat
);
2310 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2311 _mesa_update_state(ctx
);
2313 ASSERT(ctx
->Driver
.TexImage3D
);
2315 /* Give the texture to the driver! <pixels> may be null! */
2316 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, internalFormat
,
2317 width
, height
, depth
, border
, format
, type
,
2318 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2320 ASSERT(texImage
->TexFormat
);
2322 /* If driver didn't explicitly set these, use the defaults */
2323 if (!texImage
->FetchTexelc
)
2324 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2325 if (!texImage
->FetchTexelf
)
2326 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2327 ASSERT(texImage
->FetchTexelc
);
2328 ASSERT(texImage
->FetchTexelf
);
2331 texObj
->Complete
= GL_FALSE
;
2332 ctx
->NewState
|= _NEW_TEXTURE
;
2334 else if (target
== GL_PROXY_TEXTURE_3D
) {
2335 /* Proxy texture: check for errors and update proxy state */
2336 struct gl_texture_image
*texImage
;
2337 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2338 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2339 format
, type
, 3, width
, height
, depth
, border
)) {
2340 /* when error, clear all proxy texture image parameters */
2342 clear_teximage_fields(texImage
);
2345 /* no error, set the tex image parameters */
2346 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2347 border
, internalFormat
);
2348 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2349 internalFormat
, format
, type
);
2353 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2360 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2361 GLsizei width
, GLsizei height
, GLsizei depth
,
2362 GLint border
, GLenum format
, GLenum type
,
2363 const GLvoid
*pixels
)
2365 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2366 depth
, border
, format
, type
, pixels
);
2372 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2373 GLint xoffset
, GLsizei width
,
2374 GLenum format
, GLenum type
,
2375 const GLvoid
*pixels
)
2377 GLsizei postConvWidth
= width
;
2378 struct gl_texture_unit
*texUnit
;
2379 struct gl_texture_object
*texObj
;
2380 struct gl_texture_image
*texImage
;
2381 GET_CURRENT_CONTEXT(ctx
);
2382 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2384 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2385 _mesa_update_state(ctx
);
2387 /* XXX should test internal format */
2388 if (is_color_format(format
)) {
2389 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2392 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2393 postConvWidth
, 1, 1, format
, type
)) {
2394 return; /* error was detected */
2397 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2398 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2399 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2403 return; /* no-op, not an error */
2405 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2406 xoffset
+= texImage
->Border
;
2408 ASSERT(ctx
->Driver
.TexSubImage1D
);
2409 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
2410 format
, type
, pixels
, &ctx
->Unpack
,
2412 ctx
->NewState
|= _NEW_TEXTURE
;
2417 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2418 GLint xoffset
, GLint yoffset
,
2419 GLsizei width
, GLsizei height
,
2420 GLenum format
, GLenum type
,
2421 const GLvoid
*pixels
)
2423 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2424 struct gl_texture_unit
*texUnit
;
2425 struct gl_texture_object
*texObj
;
2426 struct gl_texture_image
*texImage
;
2427 GET_CURRENT_CONTEXT(ctx
);
2428 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2430 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2431 _mesa_update_state(ctx
);
2433 /* XXX should test internal format */
2434 if (is_color_format(format
)) {
2435 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2439 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2440 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2441 return; /* error was detected */
2444 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2445 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2446 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2449 if (width
== 0 || height
== 0)
2450 return; /* no-op, not an error */
2452 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2453 xoffset
+= texImage
->Border
;
2454 yoffset
+= texImage
->Border
;
2456 ASSERT(ctx
->Driver
.TexSubImage2D
);
2457 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
2458 width
, height
, format
, type
, pixels
,
2459 &ctx
->Unpack
, texObj
, texImage
);
2460 ctx
->NewState
|= _NEW_TEXTURE
;
2466 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2467 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2468 GLsizei width
, GLsizei height
, GLsizei depth
,
2469 GLenum format
, GLenum type
,
2470 const GLvoid
*pixels
)
2472 struct gl_texture_unit
*texUnit
;
2473 struct gl_texture_object
*texObj
;
2474 struct gl_texture_image
*texImage
;
2475 GET_CURRENT_CONTEXT(ctx
);
2476 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2478 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2479 _mesa_update_state(ctx
);
2481 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2482 width
, height
, depth
, format
, type
)) {
2483 return; /* error was detected */
2486 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2487 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2488 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2491 if (width
== 0 || height
== 0 || height
== 0)
2492 return; /* no-op, not an error */
2494 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2495 xoffset
+= texImage
->Border
;
2496 yoffset
+= texImage
->Border
;
2497 zoffset
+= texImage
->Border
;
2499 ASSERT(ctx
->Driver
.TexSubImage3D
);
2500 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
2501 xoffset
, yoffset
, zoffset
,
2502 width
, height
, depth
,
2503 format
, type
, pixels
,
2504 &ctx
->Unpack
, texObj
, texImage
);
2505 ctx
->NewState
|= _NEW_TEXTURE
;
2511 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2512 GLenum internalFormat
,
2514 GLsizei width
, GLint border
)
2516 struct gl_texture_unit
*texUnit
;
2517 struct gl_texture_object
*texObj
;
2518 struct gl_texture_image
*texImage
;
2519 GLsizei postConvWidth
= width
;
2520 GET_CURRENT_CONTEXT(ctx
);
2521 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2523 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2524 _mesa_update_state(ctx
);
2526 if (is_color_format(internalFormat
)) {
2527 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2530 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2531 postConvWidth
, 1, border
))
2534 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2535 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2536 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2538 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2541 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2542 /* free the old texture data */
2543 MESA_PBUFFER_FREE(texImage
->Data
);
2545 texImage
->Data
= NULL
;
2547 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2548 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2549 border
, internalFormat
);
2552 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2553 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
2554 x
, y
, width
, border
);
2556 ASSERT(texImage
->TexFormat
);
2558 /* If driver didn't explicitly set these, use the defaults */
2559 if (!texImage
->FetchTexelc
)
2560 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2561 if (!texImage
->FetchTexelf
)
2562 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2563 ASSERT(texImage
->FetchTexelc
);
2564 ASSERT(texImage
->FetchTexelf
);
2567 texObj
->Complete
= GL_FALSE
;
2568 ctx
->NewState
|= _NEW_TEXTURE
;
2574 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2575 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2578 struct gl_texture_unit
*texUnit
;
2579 struct gl_texture_object
*texObj
;
2580 struct gl_texture_image
*texImage
;
2581 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2582 GET_CURRENT_CONTEXT(ctx
);
2583 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2585 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2586 _mesa_update_state(ctx
);
2588 if (is_color_format(internalFormat
)) {
2589 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2593 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2594 postConvWidth
, postConvHeight
, border
))
2597 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2598 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2599 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2601 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2604 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2605 /* free the old texture data */
2606 MESA_PBUFFER_FREE(texImage
->Data
);
2608 texImage
->Data
= NULL
;
2610 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2611 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2612 postConvWidth
, postConvHeight
, 1,
2613 border
, internalFormat
);
2615 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2616 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
2617 x
, y
, width
, height
, border
);
2619 ASSERT(texImage
->TexFormat
);
2621 /* If driver didn't explicitly set these, use the defaults */
2622 if (!texImage
->FetchTexelc
)
2623 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2624 if (!texImage
->FetchTexelf
)
2625 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2626 ASSERT(texImage
->FetchTexelc
);
2627 ASSERT(texImage
->FetchTexelf
);
2630 texObj
->Complete
= GL_FALSE
;
2631 ctx
->NewState
|= _NEW_TEXTURE
;
2637 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2638 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2640 struct gl_texture_unit
*texUnit
;
2641 struct gl_texture_image
*texImage
;
2642 GLsizei postConvWidth
= width
;
2643 GET_CURRENT_CONTEXT(ctx
);
2644 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2646 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2647 _mesa_update_state(ctx
);
2649 /* XXX should test internal format */
2650 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2652 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
2653 xoffset
, 0, 0, postConvWidth
, 1))
2656 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2657 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2660 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2661 xoffset
+= texImage
->Border
;
2663 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2664 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
2665 ctx
->NewState
|= _NEW_TEXTURE
;
2671 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2672 GLint xoffset
, GLint yoffset
,
2673 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2675 struct gl_texture_unit
*texUnit
;
2676 struct gl_texture_image
*texImage
;
2677 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2678 GET_CURRENT_CONTEXT(ctx
);
2679 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2681 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2682 _mesa_update_state(ctx
);
2684 /* XXX should test internal format */
2685 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2687 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2688 postConvWidth
, postConvHeight
))
2691 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2692 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2695 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2696 xoffset
+= texImage
->Border
;
2697 yoffset
+= texImage
->Border
;
2699 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
2700 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
2701 xoffset
, yoffset
, x
, y
, width
, height
);
2702 ctx
->NewState
|= _NEW_TEXTURE
;
2708 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2709 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2710 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2712 struct gl_texture_unit
*texUnit
;
2713 struct gl_texture_image
*texImage
;
2714 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2715 GET_CURRENT_CONTEXT(ctx
);
2716 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2718 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2719 _mesa_update_state(ctx
);
2721 /* XXX should test internal format */
2722 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2724 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2725 zoffset
, postConvWidth
, postConvHeight
))
2728 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2729 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2732 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2733 xoffset
+= texImage
->Border
;
2734 yoffset
+= texImage
->Border
;
2735 zoffset
+= texImage
->Border
;
2737 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2738 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2739 xoffset
, yoffset
, zoffset
,
2740 x
, y
, width
, height
);
2741 ctx
->NewState
|= _NEW_TEXTURE
;
2747 /**********************************************************************/
2748 /****** Compressed Textures ******/
2749 /**********************************************************************/
2753 * Error checking for glCompressedTexImage[123]D().
2754 * \return error code or GL_NO_ERROR.
2757 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
2758 GLenum target
, GLint level
,
2759 GLenum internalFormat
, GLsizei width
,
2760 GLsizei height
, GLsizei depth
, GLint border
,
2763 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2765 if (dimensions
== 1) {
2766 /* 1D compressed textures not allowed */
2767 return GL_INVALID_ENUM
;
2769 else if (dimensions
== 2) {
2770 if (target
== GL_PROXY_TEXTURE_2D
) {
2771 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2773 else if (target
== GL_TEXTURE_2D
) {
2774 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2776 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2777 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2778 return GL_INVALID_ENUM
; /*target*/
2779 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2781 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2782 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2783 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2784 return GL_INVALID_ENUM
; /*target*/
2785 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2788 return GL_INVALID_ENUM
; /*target*/
2791 else if (dimensions
== 3) {
2792 /* 3D compressed textures not allowed */
2793 return GL_INVALID_ENUM
;
2796 maxTextureSize
= 1 << (maxLevels
- 1);
2798 if (!is_compressed_format(ctx
, internalFormat
))
2799 return GL_INVALID_ENUM
;
2801 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
2802 return GL_INVALID_ENUM
;
2805 return GL_INVALID_VALUE
;
2808 * XXX We should probably use the proxy texture error check function here.
2810 if (width
< 1 || width
> maxTextureSize
||
2811 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(width
) < 0))
2812 return GL_INVALID_VALUE
;
2814 if ((height
< 1 || height
> maxTextureSize
||
2815 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(height
) < 0))
2817 return GL_INVALID_VALUE
;
2819 if ((depth
< 1 || depth
> maxTextureSize
||
2820 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(depth
) < 0))
2822 return GL_INVALID_VALUE
;
2824 /* For cube map, width must equal height */
2825 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2826 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
2827 return GL_INVALID_VALUE
;
2829 if (level
< 0 || level
>= maxLevels
)
2830 return GL_INVALID_VALUE
;
2832 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2834 if (expectedSize
!= imageSize
)
2835 return GL_INVALID_VALUE
;
2842 * Error checking for glCompressedTexSubImage[123]D().
2843 * \warning There are some bad assumptions here about the size of compressed
2844 * texture tiles (multiple of 4) used to test the validity of the
2845 * offset and size parameters.
2846 * \return error code or GL_NO_ERROR.
2849 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
2850 GLenum target
, GLint level
,
2851 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2852 GLsizei width
, GLsizei height
, GLsizei depth
,
2853 GLenum format
, GLsizei imageSize
)
2855 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2858 if (dimensions
== 1) {
2859 /* 1D compressed textures not allowed */
2860 return GL_INVALID_ENUM
;
2862 else if (dimensions
== 2) {
2863 if (target
== GL_PROXY_TEXTURE_2D
) {
2864 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2866 else if (target
== GL_TEXTURE_2D
) {
2867 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2869 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2870 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2871 return GL_INVALID_ENUM
; /*target*/
2872 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2874 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2875 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2876 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2877 return GL_INVALID_ENUM
; /*target*/
2878 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2881 return GL_INVALID_ENUM
; /*target*/
2884 else if (dimensions
== 3) {
2885 /* 3D compressed textures not allowed */
2886 return GL_INVALID_ENUM
;
2889 maxTextureSize
= 1 << (maxLevels
- 1);
2891 if (!is_compressed_format(ctx
, format
))
2892 return GL_INVALID_ENUM
;
2894 if (width
< 1 || width
> maxTextureSize
)
2895 return GL_INVALID_VALUE
;
2897 if ((height
< 1 || height
> maxTextureSize
)
2899 return GL_INVALID_VALUE
;
2901 if (level
< 0 || level
>= maxLevels
)
2902 return GL_INVALID_VALUE
;
2904 /* XXX these tests are specific to the compressed format.
2905 * this code should be generalized in some way.
2907 if ((xoffset
& 3) != 0 || (yoffset
& 3) != 0)
2908 return GL_INVALID_VALUE
;
2910 if ((width
& 3) != 0 && width
!= 2 && width
!= 1)
2911 return GL_INVALID_VALUE
;
2913 if ((height
& 3) != 0 && height
!= 2 && height
!= 1)
2914 return GL_INVALID_VALUE
;
2916 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2918 if (expectedSize
!= imageSize
)
2919 return GL_INVALID_VALUE
;
2927 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2928 GLenum internalFormat
, GLsizei width
,
2929 GLint border
, GLsizei imageSize
,
2932 GET_CURRENT_CONTEXT(ctx
);
2933 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2935 if (target
== GL_TEXTURE_1D
) {
2936 struct gl_texture_unit
*texUnit
;
2937 struct gl_texture_object
*texObj
;
2938 struct gl_texture_image
*texImage
;
2939 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2940 internalFormat
, width
, 1, 1, border
, imageSize
);
2942 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
2946 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2947 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2948 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2950 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
2953 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2954 MESA_PBUFFER_FREE(texImage
->Data
);
2956 texImage
->Data
= NULL
;
2958 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2959 border
, internalFormat
);
2961 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2962 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2963 internalFormat
, width
, border
,
2968 texObj
->Complete
= GL_FALSE
;
2969 ctx
->NewState
|= _NEW_TEXTURE
;
2971 else if (target
== GL_PROXY_TEXTURE_1D
) {
2972 /* Proxy texture: check for errors and update proxy state */
2973 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2974 internalFormat
, width
, 1, 1, border
, imageSize
);
2976 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2977 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2978 internalFormat
, GL_NONE
, GL_NONE
,
2979 width
, 1, 1, border
);
2982 /* if error, clear all proxy texture image parameters */
2983 struct gl_texture_image
*texImage
;
2984 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2986 clear_teximage_fields(texImage
);
2989 /* store the teximage parameters */
2990 struct gl_texture_unit
*texUnit
;
2991 struct gl_texture_image
*texImage
;
2992 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2993 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2994 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2995 border
, internalFormat
);
2999 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
3006 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3007 GLenum internalFormat
, GLsizei width
,
3008 GLsizei height
, GLint border
, GLsizei imageSize
,
3011 GET_CURRENT_CONTEXT(ctx
);
3012 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3014 if (target
== GL_TEXTURE_2D
||
3015 (ctx
->Extensions
.ARB_texture_cube_map
&&
3016 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3017 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
3018 struct gl_texture_unit
*texUnit
;
3019 struct gl_texture_object
*texObj
;
3020 struct gl_texture_image
*texImage
;
3021 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3022 internalFormat
, width
, height
, 1, border
, imageSize
);
3024 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
3028 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3029 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3030 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3032 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
3035 else if (texImage
->Data
&& !texImage
->IsClientData
) {
3036 MESA_PBUFFER_FREE(texImage
->Data
);
3038 texImage
->Data
= NULL
;
3040 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3041 border
, internalFormat
);
3043 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3044 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
3045 internalFormat
, width
, height
,
3046 border
, imageSize
, data
,
3050 texObj
->Complete
= GL_FALSE
;
3051 ctx
->NewState
|= _NEW_TEXTURE
;
3053 else if (target
== GL_PROXY_TEXTURE_2D
||
3054 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
3055 ctx
->Extensions
.ARB_texture_cube_map
)) {
3056 /* Proxy texture: check for errors and update proxy state */
3057 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3058 internalFormat
, width
, height
, 1, border
, imageSize
);
3060 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3061 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3062 internalFormat
, GL_NONE
, GL_NONE
,
3063 width
, height
, 1, border
);
3066 /* if error, clear all proxy texture image parameters */
3067 struct gl_texture_image
*texImage
;
3068 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3070 clear_teximage_fields(texImage
);
3073 /* store the teximage parameters */
3074 struct gl_texture_unit
*texUnit
;
3075 struct gl_texture_image
*texImage
;
3076 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3077 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3078 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3079 border
, internalFormat
);
3083 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3090 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3091 GLenum internalFormat
, GLsizei width
,
3092 GLsizei height
, GLsizei depth
, GLint border
,
3093 GLsizei imageSize
, const GLvoid
*data
)
3095 GET_CURRENT_CONTEXT(ctx
);
3096 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3098 if (target
== GL_TEXTURE_3D
) {
3099 struct gl_texture_unit
*texUnit
;
3100 struct gl_texture_object
*texObj
;
3101 struct gl_texture_image
*texImage
;
3102 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3103 internalFormat
, width
, height
, depth
, border
, imageSize
);
3105 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3109 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3110 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3111 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3113 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3116 else if (texImage
->Data
&& !texImage
->IsClientData
) {
3117 MESA_PBUFFER_FREE(texImage
->Data
);
3119 texImage
->Data
= NULL
;
3121 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, depth
,
3122 border
, internalFormat
);
3124 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3125 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
3127 width
, height
, depth
,
3128 border
, imageSize
, data
,
3132 texObj
->Complete
= GL_FALSE
;
3133 ctx
->NewState
|= _NEW_TEXTURE
;
3135 else if (target
== GL_PROXY_TEXTURE_3D
) {
3136 /* Proxy texture: check for errors and update proxy state */
3137 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3138 internalFormat
, width
, height
, depth
, border
, imageSize
);
3140 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3141 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3142 internalFormat
, GL_NONE
, GL_NONE
,
3143 width
, height
, depth
, border
);
3146 /* if error, clear all proxy texture image parameters */
3147 struct gl_texture_image
*texImage
;
3148 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3150 clear_teximage_fields(texImage
);
3153 /* store the teximage parameters */
3154 struct gl_texture_unit
*texUnit
;
3155 struct gl_texture_image
*texImage
;
3156 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3157 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3158 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3159 depth
, border
, internalFormat
);
3163 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3170 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3171 GLsizei width
, GLenum format
,
3172 GLsizei imageSize
, const GLvoid
*data
)
3174 struct gl_texture_unit
*texUnit
;
3175 struct gl_texture_object
*texObj
;
3176 struct gl_texture_image
*texImage
;
3178 GET_CURRENT_CONTEXT(ctx
);
3179 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3181 error
= compressed_subtexture_error_check(ctx
, 1, target
, level
,
3182 xoffset
, 0, 0, width
, 1, 1, format
, imageSize
);
3184 _mesa_error(ctx
, error
, "glCompressedTexSubImage1D");
3188 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3189 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3190 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3193 if ((GLint
) format
!= texImage
->IntFormat
) {
3194 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3195 "glCompressedTexSubImage1D(format)");
3199 if ((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) {
3200 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage1D(width)");
3205 return; /* no-op, not an error */
3207 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3208 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
3210 format
, imageSize
, data
,
3213 ctx
->NewState
|= _NEW_TEXTURE
;
3218 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3219 GLint yoffset
, GLsizei width
, GLsizei height
,
3220 GLenum format
, GLsizei imageSize
,
3223 struct gl_texture_unit
*texUnit
;
3224 struct gl_texture_object
*texObj
;
3225 struct gl_texture_image
*texImage
;
3227 GET_CURRENT_CONTEXT(ctx
);
3228 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3230 error
= compressed_subtexture_error_check(ctx
, 2, target
, level
,
3231 xoffset
, yoffset
, 0, width
, height
, 1, format
, imageSize
);
3233 /* XXX proxy target? */
3234 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3238 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3239 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3240 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3243 if ((GLint
) format
!= texImage
->IntFormat
) {
3244 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3245 "glCompressedTexSubImage2D(format)");
3249 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3250 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
)) {
3251 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage2D(size)");
3255 if (width
== 0 || height
== 0)
3256 return; /* no-op, not an error */
3258 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3259 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
3260 xoffset
, yoffset
, width
, height
,
3261 format
, imageSize
, data
,
3264 ctx
->NewState
|= _NEW_TEXTURE
;
3269 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3270 GLint yoffset
, GLint zoffset
, GLsizei width
,
3271 GLsizei height
, GLsizei depth
, GLenum format
,
3272 GLsizei imageSize
, const GLvoid
*data
)
3274 struct gl_texture_unit
*texUnit
;
3275 struct gl_texture_object
*texObj
;
3276 struct gl_texture_image
*texImage
;
3278 GET_CURRENT_CONTEXT(ctx
);
3279 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3281 error
= compressed_subtexture_error_check(ctx
, 3, target
, level
,
3282 xoffset
, yoffset
, zoffset
, width
, height
, depth
, format
, imageSize
);
3284 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3288 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3289 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3290 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3293 if ((GLint
) format
!= texImage
->IntFormat
) {
3294 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3295 "glCompressedTexSubImage3D(format)");
3299 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3300 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
) ||
3301 ((depth
== 1 || depth
== 2) && (GLuint
) depth
!= texImage
->Depth
)) {
3302 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage3D(size)");
3306 if (width
== 0 || height
== 0 || depth
== 0)
3307 return; /* no-op, not an error */
3309 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3310 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
3311 xoffset
, yoffset
, zoffset
,
3312 width
, height
, depth
,
3313 format
, imageSize
, data
,
3316 ctx
->NewState
|= _NEW_TEXTURE
;
3321 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
3323 const struct gl_texture_unit
*texUnit
;
3324 const struct gl_texture_object
*texObj
;
3325 struct gl_texture_image
*texImage
;
3327 GET_CURRENT_CONTEXT(ctx
);
3328 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3330 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3331 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3333 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
3337 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3338 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
3340 if (level
< 0 || level
>= maxLevels
) {
3341 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3345 if (is_proxy_target(target
)) {
3346 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
3350 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3352 /* probably invalid mipmap level */
3353 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3357 if (!texImage
->IsCompressed
) {
3358 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
3365 /* just memcpy, no pixelstore or pixel transfer */
3366 MEMCPY(img
, texImage
->Data
, texImage
->CompressedSize
);