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.
39 #include "bufferobj.h"
46 #include "texcompress.h"
47 #include "texformat.h"
55 static void PrintTexture(GLcontext
*ctx
, const struct gl_texture_image
*img
)
57 #if CHAN_TYPE == GL_FLOAT
58 _mesa_problem(NULL
, "PrintTexture doesn't support float channels");
61 const GLchan
*data
= (const GLchan
*) img
->Data
;
64 _mesa_printf("No texture data\n");
68 switch (img
->Format
) {
75 case GL_LUMINANCE_ALPHA
:
85 _mesa_problem(NULL
, "error in PrintTexture\n");
89 for (i
= 0; i
< img
->Height
; i
++) {
90 for (j
= 0; j
< img
->Width
; j
++) {
92 _mesa_printf("%02x ", data
[0]);
94 _mesa_printf("%02x%02x ", data
[0], data
[1]);
96 _mesa_printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
98 _mesa_printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
99 data
+= (img
->RowStride
- img
->Width
) * c
;
109 * Compute floor(log_base_2(n)).
110 * If n < 0 return -1.
139 * Return the simple base format for a given internal texture format.
140 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
142 * \param ctx GL context.
143 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
145 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
146 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
148 * This is the format which is used during texture application (i.e. the
149 * texture format and env mode determine the arithmetic used.
152 _mesa_base_tex_format( GLcontext
*ctx
, GLint internalFormat
)
154 switch (internalFormat
) {
169 case GL_LUMINANCE_ALPHA
:
170 case GL_LUMINANCE4_ALPHA4
:
171 case GL_LUMINANCE6_ALPHA2
:
172 case GL_LUMINANCE8_ALPHA8
:
173 case GL_LUMINANCE12_ALPHA4
:
174 case GL_LUMINANCE12_ALPHA12
:
175 case GL_LUMINANCE16_ALPHA16
:
176 return GL_LUMINANCE_ALPHA
;
207 if (ctx
->Extensions
.EXT_paletted_texture
) {
208 switch (internalFormat
) {
210 case GL_COLOR_INDEX1_EXT
:
211 case GL_COLOR_INDEX2_EXT
:
212 case GL_COLOR_INDEX4_EXT
:
213 case GL_COLOR_INDEX8_EXT
:
214 case GL_COLOR_INDEX12_EXT
:
215 case GL_COLOR_INDEX16_EXT
:
216 return GL_COLOR_INDEX
;
222 if (ctx
->Extensions
.SGIX_depth_texture
) {
223 switch (internalFormat
) {
224 case GL_DEPTH_COMPONENT
:
225 case GL_DEPTH_COMPONENT16_SGIX
:
226 case GL_DEPTH_COMPONENT24_SGIX
:
227 case GL_DEPTH_COMPONENT32_SGIX
:
228 return GL_DEPTH_COMPONENT
;
234 if (ctx
->Extensions
.ARB_texture_compression
) {
235 switch (internalFormat
) {
236 case GL_COMPRESSED_ALPHA
:
238 case GL_COMPRESSED_LUMINANCE
:
240 case GL_COMPRESSED_LUMINANCE_ALPHA
:
241 return GL_LUMINANCE_ALPHA
;
242 case GL_COMPRESSED_INTENSITY
:
244 case GL_COMPRESSED_RGB
:
246 case GL_COMPRESSED_RGBA
:
253 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
254 switch (internalFormat
) {
255 case GL_COMPRESSED_RGB_FXT1_3DFX
:
257 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
264 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
265 switch (internalFormat
) {
266 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
268 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
269 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
270 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
277 if (ctx
->Extensions
.S3_s3tc
) {
278 switch (internalFormat
) {
290 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
291 if (internalFormat
== GL_YCBCR_MESA
)
292 return GL_YCBCR_MESA
;
295 if (ctx
->Extensions
.ARB_texture_float
) {
296 switch (internalFormat
) {
297 case GL_ALPHA16F_ARB
:
298 case GL_ALPHA32F_ARB
:
306 case GL_INTENSITY16F_ARB
:
307 case GL_INTENSITY32F_ARB
:
309 case GL_LUMINANCE16F_ARB
:
310 case GL_LUMINANCE32F_ARB
:
312 case GL_LUMINANCE_ALPHA16F_ARB
:
313 case GL_LUMINANCE_ALPHA32F_ARB
:
314 return GL_LUMINANCE_ALPHA
;
320 return -1; /* error */
325 * Test if the given image format is a color/RGBA format (i.e., not color
326 * index, depth, stencil, etc).
327 * \param format the image format value (may by an internal texture format)
328 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
331 is_color_format(GLenum format
)
349 case GL_LUMINANCE_ALPHA
:
350 case GL_LUMINANCE4_ALPHA4
:
351 case GL_LUMINANCE6_ALPHA2
:
352 case GL_LUMINANCE8_ALPHA8
:
353 case GL_LUMINANCE12_ALPHA4
:
354 case GL_LUMINANCE12_ALPHA12
:
355 case GL_LUMINANCE16_ALPHA16
:
382 /* float texture formats */
383 case GL_ALPHA16F_ARB
:
384 case GL_ALPHA32F_ARB
:
385 case GL_LUMINANCE16F_ARB
:
386 case GL_LUMINANCE32F_ARB
:
387 case GL_LUMINANCE_ALPHA16F_ARB
:
388 case GL_LUMINANCE_ALPHA32F_ARB
:
389 case GL_INTENSITY16F_ARB
:
390 case GL_INTENSITY32F_ARB
:
395 /* compressed formats */
396 case GL_COMPRESSED_ALPHA
:
397 case GL_COMPRESSED_LUMINANCE
:
398 case GL_COMPRESSED_LUMINANCE_ALPHA
:
399 case GL_COMPRESSED_INTENSITY
:
400 case GL_COMPRESSED_RGB
:
401 case GL_COMPRESSED_RGBA
:
406 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
407 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
408 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
409 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
410 case GL_COMPRESSED_RGB_FXT1_3DFX
:
411 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
413 case GL_YCBCR_MESA
: /* not considered to be RGB */
421 * Test if the given image format is a color index format.
424 is_index_format(GLenum format
)
428 case GL_COLOR_INDEX1_EXT
:
429 case GL_COLOR_INDEX2_EXT
:
430 case GL_COLOR_INDEX4_EXT
:
431 case GL_COLOR_INDEX8_EXT
:
432 case GL_COLOR_INDEX12_EXT
:
433 case GL_COLOR_INDEX16_EXT
:
442 * Test if the given image format is a depth component format.
445 is_depth_format(GLenum format
)
448 case GL_DEPTH_COMPONENT16_ARB
:
449 case GL_DEPTH_COMPONENT24_ARB
:
450 case GL_DEPTH_COMPONENT32_ARB
:
451 case GL_DEPTH_COMPONENT
:
460 * Test if the given image format is a YCbCr format.
463 is_ycbcr_format(GLenum format
)
475 * Test if it is a supported compressed format.
477 * \param internalFormat the internal format token provided by the user.
479 * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
480 * GL_FALSE otherwise.
482 * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
486 is_compressed_format(GLcontext
*ctx
, GLenum internalFormat
)
489 switch (internalFormat
) {
490 case GL_COMPRESSED_RGB_FXT1_3DFX
:
491 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
492 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
493 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
494 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
495 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
508 * Store a gl_texture_image pointer in a gl_texture_object structure
509 * according to the target and level parameters.
511 * \param tObj texture object.
512 * \param target texture target.
513 * \param level image level.
514 * \param texImage texture image.
516 * This was basically prompted by the introduction of cube maps.
519 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
520 GLenum target
, GLint level
,
521 struct gl_texture_image
*texImage
)
529 tObj
->Image
[0][level
] = texImage
;
531 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
533 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
534 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
535 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
536 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
: {
537 GLuint face
= ((GLuint
) target
-
538 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
539 tObj
->Image
[face
][level
] = texImage
;
542 case GL_TEXTURE_RECTANGLE_NV
:
544 tObj
->Image
[0][level
] = texImage
;
547 _mesa_problem(NULL
, "bad target in _mesa_set_tex_image()");
550 /* Set the 'back' pointer */
551 texImage
->TexObject
= tObj
;
556 * Allocate a texture image structure.
558 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
561 * \return a pointer to gl_texture_image struct with all fields initialized to
564 struct gl_texture_image
*
565 _mesa_new_texture_image( GLcontext
*ctx
)
568 return CALLOC_STRUCT(gl_texture_image
);
573 * Free texture image.
575 * \param teximage texture image.
577 * Free the texture image structure and the associated image data if it's not
578 * marked as client data.
581 _mesa_delete_texture_image( struct gl_texture_image
*teximage
)
583 if (teximage
->Data
&& !teximage
->IsClientData
) {
584 MESA_PBUFFER_FREE( teximage
->Data
);
585 teximage
->Data
= NULL
;
592 * Test if a target is a proxy target.
594 * \param target texture target.
596 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
599 is_proxy_target(GLenum target
)
601 return (target
== GL_PROXY_TEXTURE_1D
||
602 target
== GL_PROXY_TEXTURE_2D
||
603 target
== GL_PROXY_TEXTURE_3D
||
604 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
605 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
);
610 * Get the texture object that corresponds to the target of the given texture unit.
612 * \param ctx GL context.
613 * \param texUnit texture unit.
614 * \param target texture target.
616 * \return pointer to the texture object on success, or NULL on failure.
618 * \sa gl_texture_unit.
620 struct gl_texture_object
*
621 _mesa_select_tex_object(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
626 return texUnit
->Current1D
;
627 case GL_PROXY_TEXTURE_1D
:
628 return ctx
->Texture
.Proxy1D
;
630 return texUnit
->Current2D
;
631 case GL_PROXY_TEXTURE_2D
:
632 return ctx
->Texture
.Proxy2D
;
634 return texUnit
->Current3D
;
635 case GL_PROXY_TEXTURE_3D
:
636 return ctx
->Texture
.Proxy3D
;
637 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
638 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
639 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
640 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
641 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
642 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
643 case GL_TEXTURE_CUBE_MAP_ARB
:
644 return ctx
->Extensions
.ARB_texture_cube_map
645 ? texUnit
->CurrentCubeMap
: NULL
;
646 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
647 return ctx
->Extensions
.ARB_texture_cube_map
648 ? ctx
->Texture
.ProxyCubeMap
: NULL
;
649 case GL_TEXTURE_RECTANGLE_NV
:
650 return ctx
->Extensions
.NV_texture_rectangle
651 ? texUnit
->CurrentRect
: NULL
;
652 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
653 return ctx
->Extensions
.NV_texture_rectangle
654 ? ctx
->Texture
.ProxyRect
: NULL
;
656 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
663 * Get the texture image struct which corresponds to target and level
664 * of the given texture unit.
666 * \param ctx GL context.
667 * \param texUnit texture unit.
668 * \param target texture target.
669 * \param level image level.
671 * \return pointer to the texture image structure on success, or NULL on failure.
673 * \sa gl_texture_unit.
675 struct gl_texture_image
*
676 _mesa_select_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
677 GLenum target
, GLint level
)
680 ASSERT(level
< MAX_TEXTURE_LEVELS
);
683 return texUnit
->Current1D
->Image
[0][level
];
684 case GL_PROXY_TEXTURE_1D
:
685 return ctx
->Texture
.Proxy1D
->Image
[0][level
];
687 return texUnit
->Current2D
->Image
[0][level
];
688 case GL_PROXY_TEXTURE_2D
:
689 return ctx
->Texture
.Proxy2D
->Image
[0][level
];
691 return texUnit
->Current3D
->Image
[0][level
];
692 case GL_PROXY_TEXTURE_3D
:
693 return ctx
->Texture
.Proxy3D
->Image
[0][level
];
694 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
695 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
696 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
697 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
698 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
699 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
700 if (ctx
->Extensions
.ARB_texture_cube_map
) {
701 GLuint face
= ((GLuint
) target
-
702 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
703 return texUnit
->CurrentCubeMap
->Image
[face
][level
];
707 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
708 if (ctx
->Extensions
.ARB_texture_cube_map
)
709 return ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
712 case GL_TEXTURE_RECTANGLE_NV
:
713 if (ctx
->Extensions
.NV_texture_rectangle
) {
715 return texUnit
->CurrentRect
->Image
[0][level
];
720 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
721 if (ctx
->Extensions
.NV_texture_rectangle
) {
723 return ctx
->Texture
.ProxyRect
->Image
[0][level
];
729 _mesa_problem(ctx
, "bad target in _mesa_select_tex_image()");
736 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
737 * it and install it. Only return NULL if passed a bad parameter or run
740 struct gl_texture_image
*
741 _mesa_get_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
742 GLenum target
, GLint level
)
744 struct gl_texture_image
*texImage
;
745 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
747 struct gl_texture_object
*texObj
;
748 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
750 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
753 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
755 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
762 * Return pointer to the specified proxy texture image.
763 * Note that proxy textures are per-context, not per-texture unit.
764 * \return pointer to texture image or NULL if invalid target, invalid
765 * level, or out of memory.
767 struct gl_texture_image
*
768 _mesa_get_proxy_tex_image(GLcontext
*ctx
, GLenum target
, GLint level
)
770 struct gl_texture_image
*texImage
;
776 case GL_PROXY_TEXTURE_1D
:
777 if (level
>= ctx
->Const
.MaxTextureLevels
)
779 texImage
= ctx
->Texture
.Proxy1D
->Image
[0][level
];
781 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
783 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
786 ctx
->Texture
.Proxy1D
->Image
[0][level
] = texImage
;
787 /* Set the 'back' pointer */
788 texImage
->TexObject
= ctx
->Texture
.Proxy1D
;
791 case GL_PROXY_TEXTURE_2D
:
792 if (level
>= ctx
->Const
.MaxTextureLevels
)
794 texImage
= ctx
->Texture
.Proxy2D
->Image
[0][level
];
796 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
798 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
801 ctx
->Texture
.Proxy2D
->Image
[0][level
] = texImage
;
802 /* Set the 'back' pointer */
803 texImage
->TexObject
= ctx
->Texture
.Proxy2D
;
806 case GL_PROXY_TEXTURE_3D
:
807 if (level
>= ctx
->Const
.Max3DTextureLevels
)
809 texImage
= ctx
->Texture
.Proxy3D
->Image
[0][level
];
811 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
813 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
816 ctx
->Texture
.Proxy3D
->Image
[0][level
] = texImage
;
817 /* Set the 'back' pointer */
818 texImage
->TexObject
= ctx
->Texture
.Proxy3D
;
821 case GL_PROXY_TEXTURE_CUBE_MAP
:
822 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
824 texImage
= ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
826 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
828 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
831 ctx
->Texture
.ProxyCubeMap
->Image
[0][level
] = texImage
;
832 /* Set the 'back' pointer */
833 texImage
->TexObject
= ctx
->Texture
.ProxyCubeMap
;
836 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
839 texImage
= ctx
->Texture
.ProxyRect
->Image
[0][level
];
841 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
843 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
846 ctx
->Texture
.ProxyRect
->Image
[0][level
] = texImage
;
847 /* Set the 'back' pointer */
848 texImage
->TexObject
= ctx
->Texture
.ProxyRect
;
858 * Get the maximum number of allowed mipmap levels.
860 * \param ctx GL context.
861 * \param target texture target.
863 * \return the maximum number of allowed mipmap levels for the given
864 * texture target, or zero if passed a bad target.
869 _mesa_max_texture_levels(GLcontext
*ctx
, GLenum target
)
873 case GL_PROXY_TEXTURE_1D
:
875 case GL_PROXY_TEXTURE_2D
:
876 return ctx
->Const
.MaxTextureLevels
;
878 case GL_PROXY_TEXTURE_3D
:
879 return ctx
->Const
.Max3DTextureLevels
;
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
881 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
882 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
883 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
884 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
885 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
886 case GL_TEXTURE_CUBE_MAP_ARB
:
887 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
888 return ctx
->Const
.MaxCubeTextureLevels
;
889 case GL_TEXTURE_RECTANGLE_NV
:
890 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
893 return 0; /* bad target */
899 #if 000 /* not used anymore */
901 * glTexImage[123]D can accept a NULL image pointer. In this case we
902 * create a texture image with unspecified image contents per the OpenGL
906 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
908 const GLint components
= _mesa_components_in_format(format
);
909 const GLint numPixels
= width
* height
* depth
;
910 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
914 * Let's see if anyone finds this. If glTexImage2D() is called with
915 * a NULL image pointer then load the texture image with something
916 * interesting instead of leaving it indeterminate.
919 static const char message
[8][32] = {
923 " X X XXXX XXX XXXXX ",
926 " X X XXXXX XXX X X ",
930 GLubyte
*imgPtr
= data
;
932 for (h
= 0; h
< depth
; h
++) {
933 for (i
= 0; i
< height
; i
++) {
934 GLint srcRow
= 7 - (i
% 8);
935 for (j
= 0; j
< width
; j
++) {
936 GLint srcCol
= j
% 32;
937 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
938 for (k
= 0; k
< components
; k
++) {
954 * Reset the fields of a gl_texture_image struct to zero.
956 * \param img texture image structure.
958 * This is called when a proxy texture test fails, we set all the
959 * image members (except DriverData) to zero.
960 * It's also used in glTexImage[123]D as a safeguard to be sure all
961 * required fields get initialized properly by the Driver.TexImage[123]D
965 clear_teximage_fields(struct gl_texture_image
*img
)
982 img
->TexFormat
= &_mesa_null_texformat
;
983 img
->FetchTexelc
= NULL
;
984 img
->FetchTexelf
= NULL
;
985 img
->IsCompressed
= 0;
986 img
->CompressedSize
= 0;
991 * Initialize basic fields of the gl_texture_image struct.
993 * \param ctx GL context.
994 * \param target texture target.
995 * \param img texture image structure to be initialized.
996 * \param width image width.
997 * \param height image height.
998 * \param depth image depth.
999 * \param border image border.
1000 * \param internalFormat internal format.
1002 * Fills in the fields of \p img with the given information.
1003 * Note: width, height and depth include the border.
1006 _mesa_init_teximage_fields(GLcontext
*ctx
, GLenum target
,
1007 struct gl_texture_image
*img
,
1008 GLsizei width
, GLsizei height
, GLsizei depth
,
1009 GLint border
, GLenum internalFormat
)
1012 img
->Format
= _mesa_base_tex_format( ctx
, internalFormat
);
1013 ASSERT(img
->Format
> 0);
1014 img
->IntFormat
= internalFormat
;
1015 img
->Border
= border
;
1017 img
->Height
= height
;
1019 img
->RowStride
= width
;
1020 img
->WidthLog2
= logbase2(width
- 2 * border
);
1021 if (height
== 1) /* 1-D texture */
1022 img
->HeightLog2
= 0;
1024 img
->HeightLog2
= logbase2(height
- 2 * border
);
1025 if (depth
== 1) /* 2-D texture */
1028 img
->DepthLog2
= logbase2(depth
- 2 * border
);
1029 img
->Width2
= width
- 2 * border
; /*1 << img->WidthLog2;*/
1030 img
->Height2
= height
- 2 * border
; /*1 << img->HeightLog2;*/
1031 img
->Depth2
= depth
- 2 * border
; /*1 << img->DepthLog2;*/
1032 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1033 img
->IsCompressed
= is_compressed_format(ctx
, internalFormat
);
1034 if (img
->IsCompressed
)
1035 img
->CompressedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
,
1036 height
, depth
, internalFormat
);
1038 img
->CompressedSize
= 0;
1040 if ((width
== 1 || _mesa_bitcount(width
- 2 * border
) == 1) &&
1041 (height
== 1 || _mesa_bitcount(height
- 2 * border
) == 1) &&
1042 (depth
== 1 || _mesa_bitcount(depth
- 2 * border
) == 1))
1043 img
->_IsPowerOfTwo
= GL_TRUE
;
1045 img
->_IsPowerOfTwo
= GL_FALSE
;
1047 /* Compute Width/Height/DepthScale for mipmap lod computation */
1048 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1049 /* scale = 1.0 since texture coords directly map to texels */
1050 img
->WidthScale
= 1.0;
1051 img
->HeightScale
= 1.0;
1052 img
->DepthScale
= 1.0;
1055 img
->WidthScale
= (GLfloat
) img
->Width
;
1056 img
->HeightScale
= (GLfloat
) img
->Height
;
1057 img
->DepthScale
= (GLfloat
) img
->Depth
;
1063 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1064 * level, width, height and depth against the ctx->Const limits for textures.
1066 * A hardware driver might override this function if, for example, the
1067 * max 3D texture size is 512x512x64 (i.e. not a cube).
1069 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1070 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1071 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1072 * \param level as passed to glTexImage
1073 * \param internalFormat as passed to glTexImage
1074 * \param format as passed to glTexImage
1075 * \param type as passed to glTexImage
1076 * \param width as passed to glTexImage
1077 * \param height as passed to glTexImage
1078 * \param depth as passed to glTexImage
1079 * \param border as passed to glTexImage
1080 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1083 _mesa_test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1084 GLint internalFormat
, GLenum format
, GLenum type
,
1085 GLint width
, GLint height
, GLint depth
, GLint border
)
1089 (void) internalFormat
;
1094 case GL_PROXY_TEXTURE_1D
:
1095 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1096 if (width
< 2 * border
|| width
> 2 + maxSize
||
1097 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1098 _mesa_bitcount(width
- 2 * border
) != 1) ||
1099 level
>= ctx
->Const
.MaxTextureLevels
) {
1100 /* bad width or level */
1104 case GL_PROXY_TEXTURE_2D
:
1105 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1106 if (width
< 2 * border
|| width
> 2 + maxSize
||
1107 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1108 _mesa_bitcount(width
- 2 * border
) != 1) ||
1109 height
< 2 * border
|| height
> 2 + maxSize
||
1110 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1111 _mesa_bitcount(height
- 2 * border
) != 1) ||
1112 level
>= ctx
->Const
.MaxTextureLevels
) {
1113 /* bad width or height or level */
1117 case GL_PROXY_TEXTURE_3D
:
1118 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1119 if (width
< 2 * border
|| width
> 2 + maxSize
||
1120 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1121 _mesa_bitcount(width
- 2 * border
) != 1) ||
1122 height
< 2 * border
|| height
> 2 + maxSize
||
1123 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1124 _mesa_bitcount(height
- 2 * border
) != 1) ||
1125 depth
< 2 * border
|| depth
> 2 + maxSize
||
1126 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1127 _mesa_bitcount(depth
- 2 * border
) != 1) ||
1128 level
>= ctx
->Const
.Max3DTextureLevels
) {
1129 /* bad width or height or depth or level */
1133 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1134 if (width
< 1 || width
> ctx
->Const
.MaxTextureRectSize
||
1135 height
< 1 || height
> ctx
->Const
.MaxTextureRectSize
||
1137 /* bad width or height or level */
1141 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1142 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1143 if (width
< 2 * border
|| width
> 2 + maxSize
||
1144 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1145 _mesa_bitcount(width
- 2 * border
) != 1) ||
1146 height
< 2 * border
|| height
> 2 + maxSize
||
1147 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1148 _mesa_bitcount(height
- 2 * border
) != 1) ||
1149 level
>= ctx
->Const
.MaxCubeTextureLevels
) {
1150 /* bad width or height */
1155 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1162 * Test the glTexImage[123]D() parameters for errors.
1164 * \param ctx GL context.
1165 * \param target texture target given by the user.
1166 * \param level image level given by the user.
1167 * \param internalFormat internal format given by the user.
1168 * \param format pixel data format given by the user.
1169 * \param type pixel data type given by the user.
1170 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1171 * \param width image width given by the user.
1172 * \param height image height given by the user.
1173 * \param depth image depth given by the user.
1174 * \param border image border given by the user.
1176 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1178 * Verifies each of the parameters against the constants specified in
1179 * __GLcontextRec::Const and the supported extensions, and according to the
1180 * OpenGL specification.
1183 texture_error_check( GLcontext
*ctx
, GLenum target
,
1184 GLint level
, GLint internalFormat
,
1185 GLenum format
, GLenum type
,
1187 GLint width
, GLint height
,
1188 GLint depth
, GLint border
)
1190 const GLboolean isProxy
= is_proxy_target(target
);
1193 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1194 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1196 _mesa_error(ctx
, GL_INVALID_VALUE
,
1197 "glTexImage%dD(level=%d)", dimensions
, level
);
1203 if (border
< 0 || border
> 1 ||
1204 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1205 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1207 _mesa_error(ctx
, GL_INVALID_VALUE
,
1208 "glTexImage%dD(border=%d)", dimensions
, border
);
1213 if (width
< 0 || height
< 0 || depth
< 0) {
1215 _mesa_error(ctx
, GL_INVALID_VALUE
,
1216 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1221 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1222 * level, width, height and depth.
1224 if (dimensions
== 1) {
1225 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1226 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1227 level
, internalFormat
,
1229 width
, 1, 1, border
);
1232 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1236 else if (dimensions
== 2) {
1237 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1238 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1239 level
, internalFormat
,
1241 width
, height
, 1, border
);
1243 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1244 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1245 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1246 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1247 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1250 sizeOK
= (width
== height
) &&
1251 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1252 level
, internalFormat
, format
, type
,
1253 width
, height
, 1, border
);
1255 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1256 target
== GL_TEXTURE_RECTANGLE_NV
) {
1257 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1258 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1261 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1262 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1263 level
, internalFormat
,
1265 width
, height
, 1, border
);
1268 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1272 else if (dimensions
== 3) {
1273 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1274 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_3D
,
1275 level
, internalFormat
,
1277 width
, height
, depth
, border
);
1280 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1285 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1291 _mesa_error(ctx
, GL_INVALID_VALUE
,
1292 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1293 dimensions
, level
, width
, height
, depth
);
1298 /* Check internalFormat */
1299 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1301 _mesa_error(ctx
, GL_INVALID_VALUE
,
1302 "glTexImage%dD(internalFormat=0x%x)",
1303 dimensions
, internalFormat
);
1308 /* Check incoming image format and type */
1309 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1310 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1311 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1314 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1315 "glTexImage%dD(format or type)", dimensions
);
1320 /* make sure internal format and format basically agree */
1321 if ((is_color_format(internalFormat
) != is_color_format(format
)) ||
1322 (is_index_format(internalFormat
) != is_index_format(format
)) ||
1323 (is_depth_format(internalFormat
) != is_depth_format(format
)) ||
1324 (is_ycbcr_format(internalFormat
) != is_ycbcr_format(format
))) {
1326 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1327 "glTexImage(internalFormat/format)");
1331 /* additional checks for ycbcr textures */
1332 if (internalFormat
== GL_YCBCR_MESA
) {
1333 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1334 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1335 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1337 _mesa_sprintf(message
,
1338 "glTexImage%d(format/type YCBCR mismatch", dimensions
);
1339 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
1340 return GL_TRUE
; /* error */
1342 if (target
!= GL_TEXTURE_2D
&&
1343 target
!= GL_PROXY_TEXTURE_2D
&&
1344 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1345 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1347 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1353 _mesa_sprintf(message
,
1354 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1355 dimensions
, border
);
1356 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1362 /* additional checks for depth textures */
1363 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1364 /* Only 1D and 2D textures supported */
1365 if (target
!= GL_TEXTURE_1D
&&
1366 target
!= GL_PROXY_TEXTURE_1D
&&
1367 target
!= GL_TEXTURE_2D
&&
1368 target
!= GL_PROXY_TEXTURE_2D
) {
1370 _mesa_error(ctx
, GL_INVALID_ENUM
,
1371 "glTexImage(target/internalFormat)");
1376 /* additional checks for compressed textures */
1377 if (is_compressed_format(ctx
, internalFormat
)) {
1378 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1381 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1382 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1383 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1384 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1389 _mesa_error(ctx
, GL_INVALID_ENUM
,
1390 "glTexImage%d(target)", dimensions
);
1396 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1397 "glTexImage%D(border!=0)", dimensions
);
1403 /* if we get here, the parameters are OK */
1409 * Test glTexSubImage[123]D() parameters for errors.
1411 * \param ctx GL context.
1412 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1413 * \param target texture target given by the user.
1414 * \param level image level given by the user.
1415 * \param xoffset sub-image x offset given by the user.
1416 * \param yoffset sub-image y offset given by the user.
1417 * \param zoffset sub-image z offset given by the user.
1418 * \param format pixel data format given by the user.
1419 * \param type pixel data type given by the user.
1420 * \param width image width given by the user.
1421 * \param height image height given by the user.
1422 * \param depth image depth given by the user.
1424 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1426 * Verifies each of the parameters against the constants specified in
1427 * __GLcontextRec::Const and the supported extensions, and according to the
1428 * OpenGL specification.
1431 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1432 GLenum target
, GLint level
,
1433 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1434 GLint width
, GLint height
, GLint depth
,
1435 GLenum format
, GLenum type
)
1437 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1438 struct gl_texture_image
*destTex
;
1441 if (dimensions
== 1) {
1442 if (target
!= GL_TEXTURE_1D
) {
1443 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1447 else if (dimensions
== 2) {
1448 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1449 target
<=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1450 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1451 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1455 else if (ctx
->Extensions
.NV_texture_rectangle
&&
1456 target
== GL_TEXTURE_RECTANGLE_NV
) {
1457 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1458 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1462 else if (target
!= GL_TEXTURE_2D
) {
1463 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1467 else if (dimensions
== 3) {
1468 if (target
!= GL_TEXTURE_3D
) {
1469 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1474 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1478 /* Basic level check */
1479 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1480 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1485 _mesa_error(ctx
, GL_INVALID_VALUE
,
1486 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1489 if (height
< 0 && dimensions
> 1) {
1490 _mesa_error(ctx
, GL_INVALID_VALUE
,
1491 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1494 if (depth
< 0 && dimensions
> 2) {
1495 _mesa_error(ctx
, GL_INVALID_VALUE
,
1496 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1500 destTex
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1503 /* undefined image level */
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1508 if (xoffset
< -((GLint
)destTex
->Border
)) {
1509 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1513 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1514 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1518 if (dimensions
> 1) {
1519 if (yoffset
< -((GLint
)destTex
->Border
)) {
1520 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1524 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1525 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1530 if (dimensions
> 2) {
1531 if (zoffset
< -((GLint
)destTex
->Border
)) {
1532 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1535 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1536 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1541 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1542 _mesa_error(ctx
, GL_INVALID_ENUM
,
1543 "glTexSubImage%dD(format or type)", dimensions
);
1547 if (destTex
->IsCompressed
) {
1548 const struct gl_texture_unit
*texUnit
;
1549 const struct gl_texture_image
*texImage
;
1550 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1551 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1553 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1556 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1557 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1558 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1559 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1563 _mesa_error(ctx
, GL_INVALID_ENUM
,
1564 "glTexSubImage%D(target)", dimensions
);
1567 /* offset must be multiple of 4 */
1568 if ((xoffset
& 3) || (yoffset
& 3)) {
1569 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1570 "glTexSubImage%D(xoffset or yoffset)", dimensions
);
1573 /* size must be multiple of 4 or equal to whole texture size */
1574 if ((width
& 3) && (GLuint
) width
!= texImage
->Width
) {
1575 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1576 "glTexSubImage%D(width)", dimensions
);
1579 if ((height
& 3) && (GLuint
) height
!= texImage
->Height
) {
1580 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1581 "glTexSubImage%D(width)", dimensions
);
1591 * Test glCopyTexImage[12]D() parameters for errors.
1593 * \param ctx GL context.
1594 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1595 * \param target texture target given by the user.
1596 * \param level image level given by the user.
1597 * \param internalFormat internal format given by the user.
1598 * \param width image width given by the user.
1599 * \param height image height given by the user.
1600 * \param depth image depth given by the user.
1601 * \param border texture border.
1603 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1605 * Verifies each of the parameters against the constants specified in
1606 * __GLcontextRec::Const and the supported extensions, and according to the
1607 * OpenGL specification.
1610 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1611 GLenum target
, GLint level
, GLint internalFormat
,
1612 GLint width
, GLint height
, GLint border
)
1614 GLenum format
, type
;
1617 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1618 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1619 _mesa_error(ctx
, GL_INVALID_VALUE
,
1620 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1625 if (border
< 0 || border
> 1 ||
1626 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1627 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1631 /* The format and type aren't really significant here, but we need to pass
1632 * something to TestProxyTexImage().
1634 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1637 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1638 * level, width, height and depth.
1640 if (dimensions
== 1) {
1641 if (target
== GL_TEXTURE_1D
) {
1642 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1643 level
, internalFormat
,
1645 width
, 1, 1, border
);
1648 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1652 else if (dimensions
== 2) {
1653 if (target
== GL_TEXTURE_2D
) {
1654 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1655 level
, internalFormat
,
1657 width
, height
, 1, border
);
1659 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1660 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1661 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1662 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1665 sizeOK
= (width
== height
) &&
1666 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1667 level
, internalFormat
, format
, type
,
1668 width
, height
, 1, border
);
1670 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1671 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1672 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1675 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1676 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1677 level
, internalFormat
,
1679 width
, height
, 1, border
);
1682 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1687 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1692 if (dimensions
== 1) {
1693 _mesa_error(ctx
, GL_INVALID_VALUE
,
1694 "glCopyTexImage1D(width=%d)", width
);
1697 ASSERT(dimensions
== 2);
1698 _mesa_error(ctx
, GL_INVALID_VALUE
,
1699 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1704 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1705 _mesa_error(ctx
, GL_INVALID_VALUE
,
1706 "glCopyTexImage%dD(internalFormat)", dimensions
);
1710 if (is_compressed_format(ctx
, internalFormat
)) {
1711 if (target
!= GL_TEXTURE_2D
) {
1712 _mesa_error(ctx
, GL_INVALID_ENUM
,
1713 "glCopyTexImage%d(target)", dimensions
);
1717 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1718 "glCopyTexImage%D(border!=0)", dimensions
);
1723 /* if we get here, the parameters are OK */
1729 * Test glCopyTexImage[12]D() parameters for errors.
1731 * \param ctx GL context.
1732 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1733 * \param target texture target given by the user.
1734 * \param level image level given by the user.
1735 * \param xoffset sub-image x offset given by the user.
1736 * \param yoffset sub-image y offset given by the user.
1737 * \param zoffset sub-image z offset given by the user.
1738 * \param width image width given by the user.
1739 * \param height image height given by the user.
1741 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1743 * Verifies each of the parameters against the constants specified in
1744 * __GLcontextRec::Const and the supported extensions, and according to the
1745 * OpenGL specification.
1748 copytexsubimage_error_check( GLcontext
*ctx
, GLuint dimensions
,
1749 GLenum target
, GLint level
,
1750 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1751 GLsizei width
, GLsizei height
)
1753 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1754 struct gl_texture_image
*teximage
;
1757 if (dimensions
== 1) {
1758 if (target
!= GL_TEXTURE_1D
) {
1759 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1763 else if (dimensions
== 2) {
1764 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1765 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1766 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1767 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1771 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1772 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1773 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1777 else if (target
!= GL_TEXTURE_2D
) {
1778 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1782 else if (dimensions
== 3) {
1783 if (target
!= GL_TEXTURE_3D
) {
1784 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1790 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1791 _mesa_error(ctx
, GL_INVALID_VALUE
,
1792 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1798 _mesa_error(ctx
, GL_INVALID_VALUE
,
1799 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1802 if (dimensions
> 1 && height
< 0) {
1803 _mesa_error(ctx
, GL_INVALID_VALUE
,
1804 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1808 teximage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1810 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1811 "glCopyTexSubImage%dD(undefined texture level: %d)",
1816 if (xoffset
< -((GLint
)teximage
->Border
)) {
1817 _mesa_error(ctx
, GL_INVALID_VALUE
,
1818 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1821 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1822 _mesa_error(ctx
, GL_INVALID_VALUE
,
1823 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1826 if (dimensions
> 1) {
1827 if (yoffset
< -((GLint
)teximage
->Border
)) {
1828 _mesa_error(ctx
, GL_INVALID_VALUE
,
1829 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1832 /* NOTE: we're adding the border here, not subtracting! */
1833 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1834 _mesa_error(ctx
, GL_INVALID_VALUE
,
1835 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1840 if (dimensions
> 2) {
1841 if (zoffset
< -((GLint
)teximage
->Border
)) {
1842 _mesa_error(ctx
, GL_INVALID_VALUE
,
1843 "glCopyTexSubImage%dD(zoffset)", dimensions
);
1846 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1847 _mesa_error(ctx
, GL_INVALID_VALUE
,
1848 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1853 if (teximage
->IsCompressed
) {
1854 if (target
!= GL_TEXTURE_2D
) {
1855 _mesa_error(ctx
, GL_INVALID_ENUM
,
1856 "glCopyTexSubImage%d(target)", dimensions
);
1859 /* offset must be multiple of 4 */
1860 if ((xoffset
& 3) || (yoffset
& 3)) {
1861 _mesa_error(ctx
, GL_INVALID_VALUE
,
1862 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions
);
1865 /* size must be multiple of 4 */
1866 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
1867 _mesa_error(ctx
, GL_INVALID_VALUE
,
1868 "glCopyTexSubImage%D(width)", dimensions
);
1871 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
1872 _mesa_error(ctx
, GL_INVALID_VALUE
,
1873 "glCopyTexSubImage%D(height)", dimensions
);
1878 if (teximage
->IntFormat
== GL_YCBCR_MESA
) {
1879 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
1883 /* if we get here, the parameters are OK */
1889 * Get texture image. Called by glGetTexImage.
1891 * \param target texture target.
1892 * \param level image level.
1893 * \param format pixel data format for returned image.
1894 * \param type pixel data type for returned image.
1895 * \param pixels returned pixel data.
1898 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1899 GLenum type
, GLvoid
*pixels
)
1901 const struct gl_texture_unit
*texUnit
;
1902 const struct gl_texture_object
*texObj
;
1903 const struct gl_texture_image
*texImage
;
1904 GLint maxLevels
= 0;
1905 GET_CURRENT_CONTEXT(ctx
);
1906 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1908 texUnit
= &(ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
]);
1909 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1910 if (!texObj
|| is_proxy_target(target
)) {
1911 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
1915 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1916 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
1918 if (level
< 0 || level
>= maxLevels
) {
1919 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
1923 if (_mesa_sizeof_packed_type(type
) <= 0) {
1924 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
1928 if (_mesa_components_in_format(format
) <= 0 ||
1929 format
== GL_STENCIL_INDEX
) {
1930 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
1934 if (!ctx
->Extensions
.EXT_paletted_texture
&& is_index_format(format
)) {
1935 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1938 if (!ctx
->Extensions
.SGIX_depth_texture
&& is_depth_format(format
)) {
1939 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1942 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& is_ycbcr_format(format
)) {
1943 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1949 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1951 /* invalid mipmap level, not an error */
1955 /* Make sure the requested image format is compatible with the
1956 * texture's format. We let the colorformat-indexformat go through,
1957 * because the texelfetcher will dequantize to full rgba.
1959 if (is_color_format(format
)
1960 && !is_color_format(texImage
->TexFormat
->BaseFormat
)
1961 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1962 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1965 else if (is_index_format(format
)
1966 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1967 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1970 else if (is_depth_format(format
)
1971 && !is_depth_format(texImage
->TexFormat
->BaseFormat
)) {
1972 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1975 else if (is_ycbcr_format(format
)
1976 && !is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
1977 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1981 /* typically, this will call _mesa_get_teximage() */
1982 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
1989 * Called from the API. Note that width includes the border.
1992 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
1993 GLsizei width
, GLint border
, GLenum format
,
1994 GLenum type
, const GLvoid
*pixels
)
1996 GLsizei postConvWidth
= width
;
1997 GET_CURRENT_CONTEXT(ctx
);
1998 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2000 if (is_color_format(internalFormat
)) {
2001 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2004 if (target
== GL_TEXTURE_1D
) {
2005 struct gl_texture_unit
*texUnit
;
2006 struct gl_texture_object
*texObj
;
2007 struct gl_texture_image
*texImage
;
2009 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2010 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2011 return; /* error was recorded */
2014 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2015 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2016 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2019 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2022 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2023 /* free the old texture data */
2024 MESA_PBUFFER_FREE(texImage
->Data
);
2026 texImage
->Data
= NULL
;
2027 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2028 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2029 postConvWidth
, 1, 1,
2030 border
, internalFormat
);
2032 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2033 _mesa_update_state(ctx
);
2035 ASSERT(ctx
->Driver
.TexImage1D
);
2037 /* Give the texture to the driver! <pixels> may be null! */
2038 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
2039 width
, border
, format
, type
, pixels
,
2040 &ctx
->Unpack
, texObj
, texImage
);
2042 ASSERT(texImage
->TexFormat
);
2044 /* If driver didn't explicitly set this, use the defaults */
2045 if (!texImage
->FetchTexelc
)
2046 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2047 if (!texImage
->FetchTexelf
)
2048 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2049 ASSERT(texImage
->FetchTexelc
);
2050 ASSERT(texImage
->FetchTexelf
);
2053 texObj
->Complete
= GL_FALSE
;
2054 ctx
->NewState
|= _NEW_TEXTURE
;
2056 else if (target
== GL_PROXY_TEXTURE_1D
) {
2057 /* Proxy texture: check for errors and update proxy state */
2058 struct gl_texture_image
*texImage
;
2059 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2060 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2061 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2062 /* when error, clear all proxy texture image parameters */
2064 clear_teximage_fields(texImage
);
2067 /* no error, set the tex image parameters */
2069 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2070 postConvWidth
, 1, 1,
2071 border
, internalFormat
);
2072 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2073 internalFormat
, format
, type
);
2077 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2084 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2085 GLsizei width
, GLsizei height
, GLint border
,
2086 GLenum format
, GLenum type
,
2087 const GLvoid
*pixels
)
2089 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2090 GET_CURRENT_CONTEXT(ctx
);
2091 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2093 if (is_color_format(internalFormat
)) {
2094 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2098 if (target
== GL_TEXTURE_2D
||
2099 (ctx
->Extensions
.ARB_texture_cube_map
&&
2100 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2101 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2102 (ctx
->Extensions
.NV_texture_rectangle
&&
2103 target
== GL_TEXTURE_RECTANGLE_NV
)) {
2104 /* non-proxy target */
2105 struct gl_texture_unit
*texUnit
;
2106 struct gl_texture_object
*texObj
;
2107 struct gl_texture_image
*texImage
;
2109 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2110 format
, type
, 2, postConvWidth
, postConvHeight
,
2112 return; /* error was recorded */
2115 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2116 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2117 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2119 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2122 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2123 /* free the old texture data */
2124 MESA_PBUFFER_FREE(texImage
->Data
);
2126 texImage
->Data
= NULL
;
2127 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2128 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2129 postConvWidth
, postConvHeight
, 1,
2130 border
, internalFormat
);
2132 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2133 _mesa_update_state(ctx
);
2135 ASSERT(ctx
->Driver
.TexImage2D
);
2137 /* Give the texture to the driver! <pixels> may be null! */
2138 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
2139 width
, height
, border
, format
, type
, pixels
,
2140 &ctx
->Unpack
, texObj
, texImage
);
2142 ASSERT(texImage
->TexFormat
);
2144 /* If driver didn't explicitly set these, use the defaults */
2145 if (!texImage
->FetchTexelc
)
2146 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2147 if (!texImage
->FetchTexelf
)
2148 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2149 ASSERT(texImage
->FetchTexelc
);
2150 ASSERT(texImage
->FetchTexelf
);
2153 texObj
->Complete
= GL_FALSE
;
2154 ctx
->NewState
|= _NEW_TEXTURE
;
2156 else if (target
== GL_PROXY_TEXTURE_2D
||
2157 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2158 ctx
->Extensions
.ARB_texture_cube_map
) ||
2159 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2160 ctx
->Extensions
.NV_texture_rectangle
)) {
2161 /* Proxy texture: check for errors and update proxy state */
2162 struct gl_texture_image
*texImage
;
2163 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2164 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2165 format
, type
, 2, postConvWidth
, postConvHeight
,
2167 /* when error, clear all proxy texture image parameters */
2169 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[0][level
]);
2172 /* no error, set the tex image parameters */
2173 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2174 postConvWidth
, postConvHeight
, 1,
2175 border
, internalFormat
);
2176 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2177 internalFormat
, format
, type
);
2181 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2188 * Called by the API or display list executor.
2189 * Note that width and height include the border.
2192 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2193 GLsizei width
, GLsizei height
, GLsizei depth
,
2194 GLint border
, GLenum format
, GLenum type
,
2195 const GLvoid
*pixels
)
2197 GET_CURRENT_CONTEXT(ctx
);
2198 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2200 if (target
== GL_TEXTURE_3D
) {
2201 struct gl_texture_unit
*texUnit
;
2202 struct gl_texture_object
*texObj
;
2203 struct gl_texture_image
*texImage
;
2205 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2206 format
, type
, 3, width
, height
, depth
, border
)) {
2207 return; /* error was recorded */
2210 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2211 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2212 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2214 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2217 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2218 MESA_PBUFFER_FREE(texImage
->Data
);
2220 texImage
->Data
= NULL
;
2221 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2222 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2223 width
, height
, depth
,
2224 border
, internalFormat
);
2226 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2227 _mesa_update_state(ctx
);
2229 ASSERT(ctx
->Driver
.TexImage3D
);
2231 /* Give the texture to the driver! <pixels> may be null! */
2232 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, internalFormat
,
2233 width
, height
, depth
, border
, format
, type
,
2234 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2236 ASSERT(texImage
->TexFormat
);
2238 /* If driver didn't explicitly set these, use the defaults */
2239 if (!texImage
->FetchTexelc
)
2240 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2241 if (!texImage
->FetchTexelf
)
2242 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2243 ASSERT(texImage
->FetchTexelc
);
2244 ASSERT(texImage
->FetchTexelf
);
2247 texObj
->Complete
= GL_FALSE
;
2248 ctx
->NewState
|= _NEW_TEXTURE
;
2250 else if (target
== GL_PROXY_TEXTURE_3D
) {
2251 /* Proxy texture: check for errors and update proxy state */
2252 struct gl_texture_image
*texImage
;
2253 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2254 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2255 format
, type
, 3, width
, height
, depth
, border
)) {
2256 /* when error, clear all proxy texture image parameters */
2258 clear_teximage_fields(texImage
);
2261 /* no error, set the tex image parameters */
2262 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2263 border
, internalFormat
);
2264 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2265 internalFormat
, format
, type
);
2269 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2276 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2277 GLsizei width
, GLsizei height
, GLsizei depth
,
2278 GLint border
, GLenum format
, GLenum type
,
2279 const GLvoid
*pixels
)
2281 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2282 depth
, border
, format
, type
, pixels
);
2288 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2289 GLint xoffset
, GLsizei width
,
2290 GLenum format
, GLenum type
,
2291 const GLvoid
*pixels
)
2293 GLsizei postConvWidth
= width
;
2294 struct gl_texture_unit
*texUnit
;
2295 struct gl_texture_object
*texObj
;
2296 struct gl_texture_image
*texImage
;
2297 GET_CURRENT_CONTEXT(ctx
);
2298 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2300 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2301 _mesa_update_state(ctx
);
2303 /* XXX should test internal format */
2304 if (is_color_format(format
)) {
2305 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2308 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2309 postConvWidth
, 1, 1, format
, type
)) {
2310 return; /* error was detected */
2313 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2314 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2315 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2319 return; /* no-op, not an error */
2321 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2322 xoffset
+= texImage
->Border
;
2324 ASSERT(ctx
->Driver
.TexSubImage1D
);
2325 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
2326 format
, type
, pixels
, &ctx
->Unpack
,
2328 ctx
->NewState
|= _NEW_TEXTURE
;
2333 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2334 GLint xoffset
, GLint yoffset
,
2335 GLsizei width
, GLsizei height
,
2336 GLenum format
, GLenum type
,
2337 const GLvoid
*pixels
)
2339 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2340 struct gl_texture_unit
*texUnit
;
2341 struct gl_texture_object
*texObj
;
2342 struct gl_texture_image
*texImage
;
2343 GET_CURRENT_CONTEXT(ctx
);
2344 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2346 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2347 _mesa_update_state(ctx
);
2349 /* XXX should test internal format */
2350 if (is_color_format(format
)) {
2351 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2355 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2356 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2357 return; /* error was detected */
2360 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2361 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2362 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2365 if (width
== 0 || height
== 0)
2366 return; /* no-op, not an error */
2368 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2369 xoffset
+= texImage
->Border
;
2370 yoffset
+= texImage
->Border
;
2372 ASSERT(ctx
->Driver
.TexSubImage2D
);
2373 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
2374 width
, height
, format
, type
, pixels
,
2375 &ctx
->Unpack
, texObj
, texImage
);
2376 ctx
->NewState
|= _NEW_TEXTURE
;
2382 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2383 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2384 GLsizei width
, GLsizei height
, GLsizei depth
,
2385 GLenum format
, GLenum type
,
2386 const GLvoid
*pixels
)
2388 struct gl_texture_unit
*texUnit
;
2389 struct gl_texture_object
*texObj
;
2390 struct gl_texture_image
*texImage
;
2391 GET_CURRENT_CONTEXT(ctx
);
2392 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2394 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2395 _mesa_update_state(ctx
);
2397 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2398 width
, height
, depth
, format
, type
)) {
2399 return; /* error was detected */
2402 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2403 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2404 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2407 if (width
== 0 || height
== 0 || height
== 0)
2408 return; /* no-op, not an error */
2410 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2411 xoffset
+= texImage
->Border
;
2412 yoffset
+= texImage
->Border
;
2413 zoffset
+= texImage
->Border
;
2415 ASSERT(ctx
->Driver
.TexSubImage3D
);
2416 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
2417 xoffset
, yoffset
, zoffset
,
2418 width
, height
, depth
,
2419 format
, type
, pixels
,
2420 &ctx
->Unpack
, texObj
, texImage
);
2421 ctx
->NewState
|= _NEW_TEXTURE
;
2427 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2428 GLenum internalFormat
,
2430 GLsizei width
, GLint border
)
2432 struct gl_texture_unit
*texUnit
;
2433 struct gl_texture_object
*texObj
;
2434 struct gl_texture_image
*texImage
;
2435 GLsizei postConvWidth
= width
;
2436 GET_CURRENT_CONTEXT(ctx
);
2437 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2439 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2440 _mesa_update_state(ctx
);
2442 if (is_color_format(internalFormat
)) {
2443 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2446 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2447 postConvWidth
, 1, border
))
2450 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2451 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2452 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2454 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2457 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2458 /* free the old texture data */
2459 MESA_PBUFFER_FREE(texImage
->Data
);
2461 texImage
->Data
= NULL
;
2463 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2464 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2465 border
, internalFormat
);
2468 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2469 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
2470 x
, y
, width
, border
);
2472 ASSERT(texImage
->TexFormat
);
2474 /* If driver didn't explicitly set these, use the defaults */
2475 if (!texImage
->FetchTexelc
)
2476 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2477 if (!texImage
->FetchTexelf
)
2478 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2479 ASSERT(texImage
->FetchTexelc
);
2480 ASSERT(texImage
->FetchTexelf
);
2483 texObj
->Complete
= GL_FALSE
;
2484 ctx
->NewState
|= _NEW_TEXTURE
;
2490 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2491 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2494 struct gl_texture_unit
*texUnit
;
2495 struct gl_texture_object
*texObj
;
2496 struct gl_texture_image
*texImage
;
2497 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2498 GET_CURRENT_CONTEXT(ctx
);
2499 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2501 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2502 _mesa_update_state(ctx
);
2504 if (is_color_format(internalFormat
)) {
2505 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2509 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2510 postConvWidth
, postConvHeight
, border
))
2513 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2514 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2515 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2517 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2520 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2521 /* free the old texture data */
2522 MESA_PBUFFER_FREE(texImage
->Data
);
2524 texImage
->Data
= NULL
;
2526 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2527 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2528 postConvWidth
, postConvHeight
, 1,
2529 border
, internalFormat
);
2531 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2532 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
2533 x
, y
, width
, height
, border
);
2535 ASSERT(texImage
->TexFormat
);
2537 /* If driver didn't explicitly set these, use the defaults */
2538 if (!texImage
->FetchTexelc
)
2539 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2540 if (!texImage
->FetchTexelf
)
2541 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2542 ASSERT(texImage
->FetchTexelc
);
2543 ASSERT(texImage
->FetchTexelf
);
2546 texObj
->Complete
= GL_FALSE
;
2547 ctx
->NewState
|= _NEW_TEXTURE
;
2553 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2554 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2556 struct gl_texture_unit
*texUnit
;
2557 struct gl_texture_image
*texImage
;
2558 GLsizei postConvWidth
= width
;
2559 GET_CURRENT_CONTEXT(ctx
);
2560 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2562 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2563 _mesa_update_state(ctx
);
2565 /* XXX should test internal format */
2566 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2568 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
2569 xoffset
, 0, 0, postConvWidth
, 1))
2572 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2573 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2576 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2577 xoffset
+= texImage
->Border
;
2579 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2580 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
2581 ctx
->NewState
|= _NEW_TEXTURE
;
2587 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2588 GLint xoffset
, GLint yoffset
,
2589 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2591 struct gl_texture_unit
*texUnit
;
2592 struct gl_texture_image
*texImage
;
2593 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2594 GET_CURRENT_CONTEXT(ctx
);
2595 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2597 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2598 _mesa_update_state(ctx
);
2600 /* XXX should test internal format */
2601 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2603 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2604 postConvWidth
, postConvHeight
))
2607 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2608 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2611 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2612 xoffset
+= texImage
->Border
;
2613 yoffset
+= texImage
->Border
;
2615 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
2616 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
2617 xoffset
, yoffset
, x
, y
, width
, height
);
2618 ctx
->NewState
|= _NEW_TEXTURE
;
2624 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2625 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2626 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2628 struct gl_texture_unit
*texUnit
;
2629 struct gl_texture_image
*texImage
;
2630 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2631 GET_CURRENT_CONTEXT(ctx
);
2632 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2634 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2635 _mesa_update_state(ctx
);
2637 /* XXX should test internal format */
2638 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2640 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2641 zoffset
, postConvWidth
, postConvHeight
))
2644 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2645 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2648 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2649 xoffset
+= texImage
->Border
;
2650 yoffset
+= texImage
->Border
;
2651 zoffset
+= texImage
->Border
;
2653 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2654 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2655 xoffset
, yoffset
, zoffset
,
2656 x
, y
, width
, height
);
2657 ctx
->NewState
|= _NEW_TEXTURE
;
2663 /**********************************************************************/
2664 /****** Compressed Textures ******/
2665 /**********************************************************************/
2669 * Error checking for glCompressedTexImage[123]D().
2670 * \return error code or GL_NO_ERROR.
2673 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
2674 GLenum target
, GLint level
,
2675 GLenum internalFormat
, GLsizei width
,
2676 GLsizei height
, GLsizei depth
, GLint border
,
2679 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2681 if (dimensions
== 1) {
2682 /* 1D compressed textures not allowed */
2683 return GL_INVALID_ENUM
;
2685 else if (dimensions
== 2) {
2686 if (target
== GL_PROXY_TEXTURE_2D
) {
2687 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2689 else if (target
== GL_TEXTURE_2D
) {
2690 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2692 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2693 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2694 return GL_INVALID_ENUM
; /*target*/
2695 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2697 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2698 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2699 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2700 return GL_INVALID_ENUM
; /*target*/
2701 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2704 return GL_INVALID_ENUM
; /*target*/
2707 else if (dimensions
== 3) {
2708 /* 3D compressed textures not allowed */
2709 return GL_INVALID_ENUM
;
2712 maxTextureSize
= 1 << (maxLevels
- 1);
2714 if (!is_compressed_format(ctx
, internalFormat
))
2715 return GL_INVALID_ENUM
;
2717 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
2718 return GL_INVALID_ENUM
;
2721 return GL_INVALID_VALUE
;
2724 * XXX We should probably use the proxy texture error check function here.
2726 if (width
< 1 || width
> maxTextureSize
||
2727 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(width
) < 0))
2728 return GL_INVALID_VALUE
;
2730 if ((height
< 1 || height
> maxTextureSize
||
2731 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(height
) < 0))
2733 return GL_INVALID_VALUE
;
2735 if ((depth
< 1 || depth
> maxTextureSize
||
2736 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(depth
) < 0))
2738 return GL_INVALID_VALUE
;
2740 /* For cube map, width must equal height */
2741 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2742 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
2743 return GL_INVALID_VALUE
;
2745 if (level
< 0 || level
>= maxLevels
)
2746 return GL_INVALID_VALUE
;
2748 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2750 if (expectedSize
!= imageSize
)
2751 return GL_INVALID_VALUE
;
2758 * Error checking for glCompressedTexSubImage[123]D().
2759 * \warning There are some bad assumptions here about the size of compressed
2760 * texture tiles (multiple of 4) used to test the validity of the
2761 * offset and size parameters.
2762 * \return error code or GL_NO_ERROR.
2765 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
2766 GLenum target
, GLint level
,
2767 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2768 GLsizei width
, GLsizei height
, GLsizei depth
,
2769 GLenum format
, GLsizei imageSize
)
2771 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2774 if (dimensions
== 1) {
2775 /* 1D compressed textures not allowed */
2776 return GL_INVALID_ENUM
;
2778 else if (dimensions
== 2) {
2779 if (target
== GL_PROXY_TEXTURE_2D
) {
2780 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2782 else if (target
== GL_TEXTURE_2D
) {
2783 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2785 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2786 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2787 return GL_INVALID_ENUM
; /*target*/
2788 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2790 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2791 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2792 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2793 return GL_INVALID_ENUM
; /*target*/
2794 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2797 return GL_INVALID_ENUM
; /*target*/
2800 else if (dimensions
== 3) {
2801 /* 3D compressed textures not allowed */
2802 return GL_INVALID_ENUM
;
2805 maxTextureSize
= 1 << (maxLevels
- 1);
2807 if (!is_compressed_format(ctx
, format
))
2808 return GL_INVALID_ENUM
;
2810 if (width
< 1 || width
> maxTextureSize
)
2811 return GL_INVALID_VALUE
;
2813 if ((height
< 1 || height
> maxTextureSize
)
2815 return GL_INVALID_VALUE
;
2817 if (level
< 0 || level
>= maxLevels
)
2818 return GL_INVALID_VALUE
;
2820 /* XXX these tests are specific to the compressed format.
2821 * this code should be generalized in some way.
2823 if ((xoffset
& 3) != 0 || (yoffset
& 3) != 0)
2824 return GL_INVALID_VALUE
;
2826 if ((width
& 3) != 0 && width
!= 2 && width
!= 1)
2827 return GL_INVALID_VALUE
;
2829 if ((height
& 3) != 0 && height
!= 2 && height
!= 1)
2830 return GL_INVALID_VALUE
;
2832 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2834 if (expectedSize
!= imageSize
)
2835 return GL_INVALID_VALUE
;
2843 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2844 GLenum internalFormat
, GLsizei width
,
2845 GLint border
, GLsizei imageSize
,
2848 GET_CURRENT_CONTEXT(ctx
);
2849 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2851 if (target
== GL_TEXTURE_1D
) {
2852 struct gl_texture_unit
*texUnit
;
2853 struct gl_texture_object
*texObj
;
2854 struct gl_texture_image
*texImage
;
2855 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2856 internalFormat
, width
, 1, 1, border
, imageSize
);
2858 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
2862 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2863 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2864 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2866 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
2869 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2870 MESA_PBUFFER_FREE(texImage
->Data
);
2872 texImage
->Data
= NULL
;
2874 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2875 border
, internalFormat
);
2877 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2878 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2879 internalFormat
, width
, border
,
2884 texObj
->Complete
= GL_FALSE
;
2885 ctx
->NewState
|= _NEW_TEXTURE
;
2887 else if (target
== GL_PROXY_TEXTURE_1D
) {
2888 /* Proxy texture: check for errors and update proxy state */
2889 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2890 internalFormat
, width
, 1, 1, border
, imageSize
);
2892 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2893 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2894 internalFormat
, GL_NONE
, GL_NONE
,
2895 width
, 1, 1, border
);
2898 /* if error, clear all proxy texture image parameters */
2899 struct gl_texture_image
*texImage
;
2900 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2902 clear_teximage_fields(texImage
);
2905 /* store the teximage parameters */
2906 struct gl_texture_unit
*texUnit
;
2907 struct gl_texture_image
*texImage
;
2908 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2909 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2910 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2911 border
, internalFormat
);
2915 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
2922 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
2923 GLenum internalFormat
, GLsizei width
,
2924 GLsizei height
, GLint border
, GLsizei imageSize
,
2927 GET_CURRENT_CONTEXT(ctx
);
2928 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2930 if (target
== GL_TEXTURE_2D
||
2931 (ctx
->Extensions
.ARB_texture_cube_map
&&
2932 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2933 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
2934 struct gl_texture_unit
*texUnit
;
2935 struct gl_texture_object
*texObj
;
2936 struct gl_texture_image
*texImage
;
2937 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2938 internalFormat
, width
, height
, 1, border
, imageSize
);
2940 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
2944 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2945 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2946 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2948 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
2951 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2952 MESA_PBUFFER_FREE(texImage
->Data
);
2954 texImage
->Data
= NULL
;
2956 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2957 border
, internalFormat
);
2959 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
2960 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
2961 internalFormat
, width
, height
,
2962 border
, imageSize
, data
,
2966 texObj
->Complete
= GL_FALSE
;
2967 ctx
->NewState
|= _NEW_TEXTURE
;
2969 else if (target
== GL_PROXY_TEXTURE_2D
||
2970 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2971 ctx
->Extensions
.ARB_texture_cube_map
)) {
2972 /* Proxy texture: check for errors and update proxy state */
2973 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2974 internalFormat
, width
, height
, 1, border
, imageSize
);
2976 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2977 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2978 internalFormat
, GL_NONE
, GL_NONE
,
2979 width
, height
, 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
, height
, 1,
2995 border
, internalFormat
);
2999 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3006 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3007 GLenum internalFormat
, GLsizei width
,
3008 GLsizei height
, GLsizei depth
, GLint border
,
3009 GLsizei imageSize
, const GLvoid
*data
)
3011 GET_CURRENT_CONTEXT(ctx
);
3012 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3014 if (target
== GL_TEXTURE_3D
) {
3015 struct gl_texture_unit
*texUnit
;
3016 struct gl_texture_object
*texObj
;
3017 struct gl_texture_image
*texImage
;
3018 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3019 internalFormat
, width
, height
, depth
, border
, imageSize
);
3021 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3025 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3026 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3027 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3029 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3032 else if (texImage
->Data
&& !texImage
->IsClientData
) {
3033 MESA_PBUFFER_FREE(texImage
->Data
);
3035 texImage
->Data
= NULL
;
3037 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, depth
,
3038 border
, internalFormat
);
3040 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3041 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
3043 width
, height
, depth
,
3044 border
, imageSize
, data
,
3048 texObj
->Complete
= GL_FALSE
;
3049 ctx
->NewState
|= _NEW_TEXTURE
;
3051 else if (target
== GL_PROXY_TEXTURE_3D
) {
3052 /* Proxy texture: check for errors and update proxy state */
3053 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3054 internalFormat
, width
, height
, depth
, border
, imageSize
);
3056 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3057 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3058 internalFormat
, GL_NONE
, GL_NONE
,
3059 width
, height
, depth
, border
);
3062 /* if error, clear all proxy texture image parameters */
3063 struct gl_texture_image
*texImage
;
3064 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3066 clear_teximage_fields(texImage
);
3069 /* store the teximage parameters */
3070 struct gl_texture_unit
*texUnit
;
3071 struct gl_texture_image
*texImage
;
3072 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3073 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3074 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3075 depth
, border
, internalFormat
);
3079 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3086 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3087 GLsizei width
, GLenum format
,
3088 GLsizei imageSize
, const GLvoid
*data
)
3090 struct gl_texture_unit
*texUnit
;
3091 struct gl_texture_object
*texObj
;
3092 struct gl_texture_image
*texImage
;
3094 GET_CURRENT_CONTEXT(ctx
);
3095 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3097 error
= compressed_subtexture_error_check(ctx
, 1, target
, level
,
3098 xoffset
, 0, 0, width
, 1, 1, format
, imageSize
);
3100 _mesa_error(ctx
, error
, "glCompressedTexSubImage1D");
3104 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3105 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3106 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3109 if ((GLint
) format
!= texImage
->IntFormat
) {
3110 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3111 "glCompressedTexSubImage1D(format)");
3115 if ((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) {
3116 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage1D(width)");
3121 return; /* no-op, not an error */
3123 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3124 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
3126 format
, imageSize
, data
,
3129 ctx
->NewState
|= _NEW_TEXTURE
;
3134 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3135 GLint yoffset
, GLsizei width
, GLsizei height
,
3136 GLenum format
, GLsizei imageSize
,
3139 struct gl_texture_unit
*texUnit
;
3140 struct gl_texture_object
*texObj
;
3141 struct gl_texture_image
*texImage
;
3143 GET_CURRENT_CONTEXT(ctx
);
3144 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3146 error
= compressed_subtexture_error_check(ctx
, 2, target
, level
,
3147 xoffset
, yoffset
, 0, width
, height
, 1, format
, imageSize
);
3149 /* XXX proxy target? */
3150 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3154 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3155 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3156 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3159 if ((GLint
) format
!= texImage
->IntFormat
) {
3160 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3161 "glCompressedTexSubImage2D(format)");
3165 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3166 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
)) {
3167 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage2D(size)");
3171 if (width
== 0 || height
== 0)
3172 return; /* no-op, not an error */
3174 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3175 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
3176 xoffset
, yoffset
, width
, height
,
3177 format
, imageSize
, data
,
3180 ctx
->NewState
|= _NEW_TEXTURE
;
3185 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3186 GLint yoffset
, GLint zoffset
, GLsizei width
,
3187 GLsizei height
, GLsizei depth
, GLenum format
,
3188 GLsizei imageSize
, const GLvoid
*data
)
3190 struct gl_texture_unit
*texUnit
;
3191 struct gl_texture_object
*texObj
;
3192 struct gl_texture_image
*texImage
;
3194 GET_CURRENT_CONTEXT(ctx
);
3195 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3197 error
= compressed_subtexture_error_check(ctx
, 3, target
, level
,
3198 xoffset
, yoffset
, zoffset
, width
, height
, depth
, format
, imageSize
);
3200 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3204 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3205 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3206 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3209 if ((GLint
) format
!= texImage
->IntFormat
) {
3210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3211 "glCompressedTexSubImage3D(format)");
3215 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3216 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
) ||
3217 ((depth
== 1 || depth
== 2) && (GLuint
) depth
!= texImage
->Depth
)) {
3218 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage3D(size)");
3222 if (width
== 0 || height
== 0 || depth
== 0)
3223 return; /* no-op, not an error */
3225 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3226 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
3227 xoffset
, yoffset
, zoffset
,
3228 width
, height
, depth
,
3229 format
, imageSize
, data
,
3232 ctx
->NewState
|= _NEW_TEXTURE
;
3237 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
3239 const struct gl_texture_unit
*texUnit
;
3240 const struct gl_texture_object
*texObj
;
3241 struct gl_texture_image
*texImage
;
3243 GET_CURRENT_CONTEXT(ctx
);
3244 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3246 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3247 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3249 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
3253 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3254 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
3256 if (level
< 0 || level
>= maxLevels
) {
3257 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3261 if (is_proxy_target(target
)) {
3262 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
3266 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3268 /* probably invalid mipmap level */
3269 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3273 if (!texImage
->IsCompressed
) {
3274 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
3278 /* this typically calls _mesa_get_compressed_teximage() */
3279 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
, texObj
,texImage
);