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
);
1192 GLboolean colorFormat
, indexFormat
;
1194 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1195 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1197 _mesa_error(ctx
, GL_INVALID_VALUE
,
1198 "glTexImage%dD(level=%d)", dimensions
, level
);
1204 if (border
< 0 || border
> 1 ||
1205 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1206 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1208 _mesa_error(ctx
, GL_INVALID_VALUE
,
1209 "glTexImage%dD(border=%d)", dimensions
, border
);
1214 if (width
< 0 || height
< 0 || depth
< 0) {
1216 _mesa_error(ctx
, GL_INVALID_VALUE
,
1217 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1222 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1223 * level, width, height and depth.
1225 if (dimensions
== 1) {
1226 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1227 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1228 level
, internalFormat
,
1230 width
, 1, 1, border
);
1233 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1237 else if (dimensions
== 2) {
1238 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1239 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1240 level
, internalFormat
,
1242 width
, height
, 1, border
);
1244 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1245 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1246 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1247 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1248 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1251 sizeOK
= (width
== height
) &&
1252 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1253 level
, internalFormat
, format
, type
,
1254 width
, height
, 1, border
);
1256 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1257 target
== GL_TEXTURE_RECTANGLE_NV
) {
1258 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1259 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1262 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1263 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1264 level
, internalFormat
,
1266 width
, height
, 1, border
);
1269 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1273 else if (dimensions
== 3) {
1274 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1275 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_3D
,
1276 level
, internalFormat
,
1278 width
, height
, depth
, border
);
1281 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1286 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1292 _mesa_error(ctx
, GL_INVALID_VALUE
,
1293 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1294 dimensions
, level
, width
, height
, depth
);
1299 /* Check internalFormat */
1300 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1302 _mesa_error(ctx
, GL_INVALID_VALUE
,
1303 "glTexImage%dD(internalFormat=0x%x)",
1304 dimensions
, internalFormat
);
1309 /* Check incoming image format and type */
1310 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1311 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1312 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1315 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1316 "glTexImage%dD(format or type)", dimensions
);
1321 /* make sure internal format and format basically agree */
1322 colorFormat
= is_color_format(format
);
1323 indexFormat
= is_index_format(format
);
1324 if ((is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1325 (is_index_format(internalFormat
) && !indexFormat
) ||
1326 (is_depth_format(internalFormat
) != is_depth_format(format
)) ||
1327 (is_ycbcr_format(internalFormat
) != is_ycbcr_format(format
))) {
1329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1330 "glTexImage(internalFormat/format)");
1334 /* additional checks for ycbcr textures */
1335 if (internalFormat
== GL_YCBCR_MESA
) {
1336 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1337 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1338 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1340 _mesa_sprintf(message
,
1341 "glTexImage%d(format/type YCBCR mismatch", dimensions
);
1342 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
1343 return GL_TRUE
; /* error */
1345 if (target
!= GL_TEXTURE_2D
&&
1346 target
!= GL_PROXY_TEXTURE_2D
&&
1347 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1348 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1350 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1356 _mesa_sprintf(message
,
1357 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1358 dimensions
, border
);
1359 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1365 /* additional checks for depth textures */
1366 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1367 /* Only 1D and 2D textures supported */
1368 if (target
!= GL_TEXTURE_1D
&&
1369 target
!= GL_PROXY_TEXTURE_1D
&&
1370 target
!= GL_TEXTURE_2D
&&
1371 target
!= GL_PROXY_TEXTURE_2D
) {
1373 _mesa_error(ctx
, GL_INVALID_ENUM
,
1374 "glTexImage(target/internalFormat)");
1379 /* additional checks for compressed textures */
1380 if (is_compressed_format(ctx
, internalFormat
)) {
1381 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1384 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1385 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1386 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1387 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1392 _mesa_error(ctx
, GL_INVALID_ENUM
,
1393 "glTexImage%d(target)", dimensions
);
1399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1400 "glTexImage%D(border!=0)", dimensions
);
1406 /* if we get here, the parameters are OK */
1412 * Test glTexSubImage[123]D() parameters for errors.
1414 * \param ctx GL context.
1415 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1416 * \param target texture target given by the user.
1417 * \param level image level given by the user.
1418 * \param xoffset sub-image x offset given by the user.
1419 * \param yoffset sub-image y offset given by the user.
1420 * \param zoffset sub-image z offset given by the user.
1421 * \param format pixel data format given by the user.
1422 * \param type pixel data type given by the user.
1423 * \param width image width given by the user.
1424 * \param height image height given by the user.
1425 * \param depth image depth given by the user.
1427 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1429 * Verifies each of the parameters against the constants specified in
1430 * __GLcontextRec::Const and the supported extensions, and according to the
1431 * OpenGL specification.
1434 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1435 GLenum target
, GLint level
,
1436 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1437 GLint width
, GLint height
, GLint depth
,
1438 GLenum format
, GLenum type
)
1440 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1441 struct gl_texture_image
*destTex
;
1444 if (dimensions
== 1) {
1445 if (target
!= GL_TEXTURE_1D
) {
1446 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1450 else if (dimensions
== 2) {
1451 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1452 target
<=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1453 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1454 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1458 else if (ctx
->Extensions
.NV_texture_rectangle
&&
1459 target
== GL_TEXTURE_RECTANGLE_NV
) {
1460 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1461 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1465 else if (target
!= GL_TEXTURE_2D
) {
1466 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1470 else if (dimensions
== 3) {
1471 if (target
!= GL_TEXTURE_3D
) {
1472 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1477 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1481 /* Basic level check */
1482 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1483 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1488 _mesa_error(ctx
, GL_INVALID_VALUE
,
1489 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1492 if (height
< 0 && dimensions
> 1) {
1493 _mesa_error(ctx
, GL_INVALID_VALUE
,
1494 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1497 if (depth
< 0 && dimensions
> 2) {
1498 _mesa_error(ctx
, GL_INVALID_VALUE
,
1499 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1503 destTex
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1506 /* undefined image level */
1507 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1511 if (xoffset
< -((GLint
)destTex
->Border
)) {
1512 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1516 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1517 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1521 if (dimensions
> 1) {
1522 if (yoffset
< -((GLint
)destTex
->Border
)) {
1523 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1527 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1528 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1533 if (dimensions
> 2) {
1534 if (zoffset
< -((GLint
)destTex
->Border
)) {
1535 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1538 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1539 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1544 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1545 _mesa_error(ctx
, GL_INVALID_ENUM
,
1546 "glTexSubImage%dD(format or type)", dimensions
);
1550 if (destTex
->IsCompressed
) {
1551 const struct gl_texture_unit
*texUnit
;
1552 const struct gl_texture_image
*texImage
;
1553 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1554 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1556 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1559 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1560 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1561 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1562 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1566 _mesa_error(ctx
, GL_INVALID_ENUM
,
1567 "glTexSubImage%D(target)", dimensions
);
1570 /* offset must be multiple of 4 */
1571 if ((xoffset
& 3) || (yoffset
& 3)) {
1572 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1573 "glTexSubImage%D(xoffset or yoffset)", dimensions
);
1576 /* size must be multiple of 4 or equal to whole texture size */
1577 if ((width
& 3) && (GLuint
) width
!= texImage
->Width
) {
1578 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1579 "glTexSubImage%D(width)", dimensions
);
1582 if ((height
& 3) && (GLuint
) height
!= texImage
->Height
) {
1583 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1584 "glTexSubImage%D(width)", dimensions
);
1594 * Test glCopyTexImage[12]D() parameters for errors.
1596 * \param ctx GL context.
1597 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1598 * \param target texture target given by the user.
1599 * \param level image level given by the user.
1600 * \param internalFormat internal format given by the user.
1601 * \param width image width given by the user.
1602 * \param height image height given by the user.
1603 * \param depth image depth given by the user.
1604 * \param border texture border.
1606 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1608 * Verifies each of the parameters against the constants specified in
1609 * __GLcontextRec::Const and the supported extensions, and according to the
1610 * OpenGL specification.
1613 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1614 GLenum target
, GLint level
, GLint internalFormat
,
1615 GLint width
, GLint height
, GLint border
)
1617 GLenum format
, type
;
1620 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1621 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1622 _mesa_error(ctx
, GL_INVALID_VALUE
,
1623 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1628 if (border
< 0 || border
> 1 ||
1629 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1630 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1634 /* The format and type aren't really significant here, but we need to pass
1635 * something to TestProxyTexImage().
1637 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1640 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1641 * level, width, height and depth.
1643 if (dimensions
== 1) {
1644 if (target
== GL_TEXTURE_1D
) {
1645 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1646 level
, internalFormat
,
1648 width
, 1, 1, border
);
1651 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1655 else if (dimensions
== 2) {
1656 if (target
== GL_TEXTURE_2D
) {
1657 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1658 level
, internalFormat
,
1660 width
, height
, 1, border
);
1662 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1663 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1664 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1665 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1668 sizeOK
= (width
== height
) &&
1669 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1670 level
, internalFormat
, format
, type
,
1671 width
, height
, 1, border
);
1673 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1674 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1675 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1678 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1679 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1680 level
, internalFormat
,
1682 width
, height
, 1, border
);
1685 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1690 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1695 if (dimensions
== 1) {
1696 _mesa_error(ctx
, GL_INVALID_VALUE
,
1697 "glCopyTexImage1D(width=%d)", width
);
1700 ASSERT(dimensions
== 2);
1701 _mesa_error(ctx
, GL_INVALID_VALUE
,
1702 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1707 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1708 _mesa_error(ctx
, GL_INVALID_VALUE
,
1709 "glCopyTexImage%dD(internalFormat)", dimensions
);
1713 if (is_compressed_format(ctx
, internalFormat
)) {
1714 if (target
!= GL_TEXTURE_2D
) {
1715 _mesa_error(ctx
, GL_INVALID_ENUM
,
1716 "glCopyTexImage%d(target)", dimensions
);
1720 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1721 "glCopyTexImage%D(border!=0)", dimensions
);
1726 /* if we get here, the parameters are OK */
1732 * Test glCopyTexImage[12]D() parameters for errors.
1734 * \param ctx GL context.
1735 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1736 * \param target texture target given by the user.
1737 * \param level image level given by the user.
1738 * \param xoffset sub-image x offset given by the user.
1739 * \param yoffset sub-image y offset given by the user.
1740 * \param zoffset sub-image z offset given by the user.
1741 * \param width image width given by the user.
1742 * \param height image height given by the user.
1744 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1746 * Verifies each of the parameters against the constants specified in
1747 * __GLcontextRec::Const and the supported extensions, and according to the
1748 * OpenGL specification.
1751 copytexsubimage_error_check( GLcontext
*ctx
, GLuint dimensions
,
1752 GLenum target
, GLint level
,
1753 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1754 GLsizei width
, GLsizei height
)
1756 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1757 struct gl_texture_image
*teximage
;
1760 if (dimensions
== 1) {
1761 if (target
!= GL_TEXTURE_1D
) {
1762 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1766 else if (dimensions
== 2) {
1767 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1768 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1769 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1770 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1774 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1775 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1776 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1780 else if (target
!= GL_TEXTURE_2D
) {
1781 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1785 else if (dimensions
== 3) {
1786 if (target
!= GL_TEXTURE_3D
) {
1787 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1793 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1794 _mesa_error(ctx
, GL_INVALID_VALUE
,
1795 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1801 _mesa_error(ctx
, GL_INVALID_VALUE
,
1802 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1805 if (dimensions
> 1 && height
< 0) {
1806 _mesa_error(ctx
, GL_INVALID_VALUE
,
1807 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1811 teximage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1813 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1814 "glCopyTexSubImage%dD(undefined texture level: %d)",
1819 if (xoffset
< -((GLint
)teximage
->Border
)) {
1820 _mesa_error(ctx
, GL_INVALID_VALUE
,
1821 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1824 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1825 _mesa_error(ctx
, GL_INVALID_VALUE
,
1826 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1829 if (dimensions
> 1) {
1830 if (yoffset
< -((GLint
)teximage
->Border
)) {
1831 _mesa_error(ctx
, GL_INVALID_VALUE
,
1832 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1835 /* NOTE: we're adding the border here, not subtracting! */
1836 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1837 _mesa_error(ctx
, GL_INVALID_VALUE
,
1838 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1843 if (dimensions
> 2) {
1844 if (zoffset
< -((GLint
)teximage
->Border
)) {
1845 _mesa_error(ctx
, GL_INVALID_VALUE
,
1846 "glCopyTexSubImage%dD(zoffset)", dimensions
);
1849 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1850 _mesa_error(ctx
, GL_INVALID_VALUE
,
1851 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1856 if (teximage
->IsCompressed
) {
1857 if (target
!= GL_TEXTURE_2D
) {
1858 _mesa_error(ctx
, GL_INVALID_ENUM
,
1859 "glCopyTexSubImage%d(target)", dimensions
);
1862 /* offset must be multiple of 4 */
1863 if ((xoffset
& 3) || (yoffset
& 3)) {
1864 _mesa_error(ctx
, GL_INVALID_VALUE
,
1865 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions
);
1868 /* size must be multiple of 4 */
1869 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
1870 _mesa_error(ctx
, GL_INVALID_VALUE
,
1871 "glCopyTexSubImage%D(width)", dimensions
);
1874 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
1875 _mesa_error(ctx
, GL_INVALID_VALUE
,
1876 "glCopyTexSubImage%D(height)", dimensions
);
1881 if (teximage
->IntFormat
== GL_YCBCR_MESA
) {
1882 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
1886 /* if we get here, the parameters are OK */
1892 * Get texture image. Called by glGetTexImage.
1894 * \param target texture target.
1895 * \param level image level.
1896 * \param format pixel data format for returned image.
1897 * \param type pixel data type for returned image.
1898 * \param pixels returned pixel data.
1901 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1902 GLenum type
, GLvoid
*pixels
)
1904 const struct gl_texture_unit
*texUnit
;
1905 const struct gl_texture_object
*texObj
;
1906 const struct gl_texture_image
*texImage
;
1907 GLint maxLevels
= 0;
1908 GET_CURRENT_CONTEXT(ctx
);
1909 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1911 texUnit
= &(ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
]);
1912 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1913 if (!texObj
|| is_proxy_target(target
)) {
1914 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
1918 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1919 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
1921 if (level
< 0 || level
>= maxLevels
) {
1922 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
1926 if (_mesa_sizeof_packed_type(type
) <= 0) {
1927 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
1931 if (_mesa_components_in_format(format
) <= 0 ||
1932 format
== GL_STENCIL_INDEX
) {
1933 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
1937 if (!ctx
->Extensions
.EXT_paletted_texture
&& is_index_format(format
)) {
1938 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1941 if (!ctx
->Extensions
.SGIX_depth_texture
&& is_depth_format(format
)) {
1942 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1945 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& is_ycbcr_format(format
)) {
1946 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1952 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1954 /* invalid mipmap level, not an error */
1958 /* Make sure the requested image format is compatible with the
1959 * texture's format. We let the colorformat-indexformat go through,
1960 * because the texelfetcher will dequantize to full rgba.
1962 if (is_color_format(format
)
1963 && !is_color_format(texImage
->TexFormat
->BaseFormat
)
1964 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1965 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1968 else if (is_index_format(format
)
1969 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1970 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1973 else if (is_depth_format(format
)
1974 && !is_depth_format(texImage
->TexFormat
->BaseFormat
)) {
1975 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1978 else if (is_ycbcr_format(format
)
1979 && !is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
1980 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1984 /* typically, this will call _mesa_get_teximage() */
1985 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
1992 * Called from the API. Note that width includes the border.
1995 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
1996 GLsizei width
, GLint border
, GLenum format
,
1997 GLenum type
, const GLvoid
*pixels
)
1999 GLsizei postConvWidth
= width
;
2000 GET_CURRENT_CONTEXT(ctx
);
2001 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2003 if (is_color_format(internalFormat
)) {
2004 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2007 if (target
== GL_TEXTURE_1D
) {
2008 struct gl_texture_unit
*texUnit
;
2009 struct gl_texture_object
*texObj
;
2010 struct gl_texture_image
*texImage
;
2012 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2013 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2014 return; /* error was recorded */
2017 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2018 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2019 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2022 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2025 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2026 /* free the old texture data */
2027 MESA_PBUFFER_FREE(texImage
->Data
);
2029 texImage
->Data
= NULL
;
2030 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2031 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2032 postConvWidth
, 1, 1,
2033 border
, internalFormat
);
2035 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2036 _mesa_update_state(ctx
);
2038 ASSERT(ctx
->Driver
.TexImage1D
);
2040 /* Give the texture to the driver! <pixels> may be null! */
2041 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
2042 width
, border
, format
, type
, pixels
,
2043 &ctx
->Unpack
, texObj
, texImage
);
2045 ASSERT(texImage
->TexFormat
);
2047 /* If driver didn't explicitly set this, use the defaults */
2048 if (!texImage
->FetchTexelc
)
2049 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2050 if (!texImage
->FetchTexelf
)
2051 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2052 ASSERT(texImage
->FetchTexelc
);
2053 ASSERT(texImage
->FetchTexelf
);
2056 texObj
->Complete
= GL_FALSE
;
2057 ctx
->NewState
|= _NEW_TEXTURE
;
2059 else if (target
== GL_PROXY_TEXTURE_1D
) {
2060 /* Proxy texture: check for errors and update proxy state */
2061 struct gl_texture_image
*texImage
;
2062 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2063 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2064 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2065 /* when error, clear all proxy texture image parameters */
2067 clear_teximage_fields(texImage
);
2070 /* no error, set the tex image parameters */
2072 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2073 postConvWidth
, 1, 1,
2074 border
, internalFormat
);
2075 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2076 internalFormat
, format
, type
);
2080 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2087 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2088 GLsizei width
, GLsizei height
, GLint border
,
2089 GLenum format
, GLenum type
,
2090 const GLvoid
*pixels
)
2092 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2093 GET_CURRENT_CONTEXT(ctx
);
2094 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2096 if (is_color_format(internalFormat
)) {
2097 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2101 if (target
== GL_TEXTURE_2D
||
2102 (ctx
->Extensions
.ARB_texture_cube_map
&&
2103 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2104 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2105 (ctx
->Extensions
.NV_texture_rectangle
&&
2106 target
== GL_TEXTURE_RECTANGLE_NV
)) {
2107 /* non-proxy target */
2108 struct gl_texture_unit
*texUnit
;
2109 struct gl_texture_object
*texObj
;
2110 struct gl_texture_image
*texImage
;
2112 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2113 format
, type
, 2, postConvWidth
, postConvHeight
,
2115 return; /* error was recorded */
2118 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2119 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2120 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2122 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2125 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2126 /* free the old texture data */
2127 MESA_PBUFFER_FREE(texImage
->Data
);
2129 texImage
->Data
= NULL
;
2130 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2131 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2132 postConvWidth
, postConvHeight
, 1,
2133 border
, internalFormat
);
2135 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2136 _mesa_update_state(ctx
);
2138 ASSERT(ctx
->Driver
.TexImage2D
);
2140 /* Give the texture to the driver! <pixels> may be null! */
2141 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
2142 width
, height
, border
, format
, type
, pixels
,
2143 &ctx
->Unpack
, texObj
, texImage
);
2145 ASSERT(texImage
->TexFormat
);
2147 /* If driver didn't explicitly set these, use the defaults */
2148 if (!texImage
->FetchTexelc
)
2149 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2150 if (!texImage
->FetchTexelf
)
2151 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2152 ASSERT(texImage
->FetchTexelc
);
2153 ASSERT(texImage
->FetchTexelf
);
2156 texObj
->Complete
= GL_FALSE
;
2157 ctx
->NewState
|= _NEW_TEXTURE
;
2159 else if (target
== GL_PROXY_TEXTURE_2D
||
2160 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2161 ctx
->Extensions
.ARB_texture_cube_map
) ||
2162 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2163 ctx
->Extensions
.NV_texture_rectangle
)) {
2164 /* Proxy texture: check for errors and update proxy state */
2165 struct gl_texture_image
*texImage
;
2166 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2167 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2168 format
, type
, 2, postConvWidth
, postConvHeight
,
2170 /* when error, clear all proxy texture image parameters */
2172 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[0][level
]);
2175 /* no error, set the tex image parameters */
2176 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2177 postConvWidth
, postConvHeight
, 1,
2178 border
, internalFormat
);
2179 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2180 internalFormat
, format
, type
);
2184 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2191 * Called by the API or display list executor.
2192 * Note that width and height include the border.
2195 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2196 GLsizei width
, GLsizei height
, GLsizei depth
,
2197 GLint border
, GLenum format
, GLenum type
,
2198 const GLvoid
*pixels
)
2200 GET_CURRENT_CONTEXT(ctx
);
2201 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2203 if (target
== GL_TEXTURE_3D
) {
2204 struct gl_texture_unit
*texUnit
;
2205 struct gl_texture_object
*texObj
;
2206 struct gl_texture_image
*texImage
;
2208 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2209 format
, type
, 3, width
, height
, depth
, border
)) {
2210 return; /* error was recorded */
2213 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2214 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2215 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2217 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2220 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2221 MESA_PBUFFER_FREE(texImage
->Data
);
2223 texImage
->Data
= NULL
;
2224 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2225 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2226 width
, height
, depth
,
2227 border
, internalFormat
);
2229 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2230 _mesa_update_state(ctx
);
2232 ASSERT(ctx
->Driver
.TexImage3D
);
2234 /* Give the texture to the driver! <pixels> may be null! */
2235 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, internalFormat
,
2236 width
, height
, depth
, border
, format
, type
,
2237 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2239 ASSERT(texImage
->TexFormat
);
2241 /* If driver didn't explicitly set these, use the defaults */
2242 if (!texImage
->FetchTexelc
)
2243 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2244 if (!texImage
->FetchTexelf
)
2245 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2246 ASSERT(texImage
->FetchTexelc
);
2247 ASSERT(texImage
->FetchTexelf
);
2250 texObj
->Complete
= GL_FALSE
;
2251 ctx
->NewState
|= _NEW_TEXTURE
;
2253 else if (target
== GL_PROXY_TEXTURE_3D
) {
2254 /* Proxy texture: check for errors and update proxy state */
2255 struct gl_texture_image
*texImage
;
2256 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2257 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2258 format
, type
, 3, width
, height
, depth
, border
)) {
2259 /* when error, clear all proxy texture image parameters */
2261 clear_teximage_fields(texImage
);
2264 /* no error, set the tex image parameters */
2265 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2266 border
, internalFormat
);
2267 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2268 internalFormat
, format
, type
);
2272 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2279 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2280 GLsizei width
, GLsizei height
, GLsizei depth
,
2281 GLint border
, GLenum format
, GLenum type
,
2282 const GLvoid
*pixels
)
2284 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2285 depth
, border
, format
, type
, pixels
);
2291 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2292 GLint xoffset
, GLsizei width
,
2293 GLenum format
, GLenum type
,
2294 const GLvoid
*pixels
)
2296 GLsizei postConvWidth
= width
;
2297 struct gl_texture_unit
*texUnit
;
2298 struct gl_texture_object
*texObj
;
2299 struct gl_texture_image
*texImage
;
2300 GET_CURRENT_CONTEXT(ctx
);
2301 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2303 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2304 _mesa_update_state(ctx
);
2306 /* XXX should test internal format */
2307 if (is_color_format(format
)) {
2308 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2311 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2312 postConvWidth
, 1, 1, format
, type
)) {
2313 return; /* error was detected */
2316 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2317 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2318 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2322 return; /* no-op, not an error */
2324 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2325 xoffset
+= texImage
->Border
;
2327 ASSERT(ctx
->Driver
.TexSubImage1D
);
2328 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
2329 format
, type
, pixels
, &ctx
->Unpack
,
2331 ctx
->NewState
|= _NEW_TEXTURE
;
2336 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2337 GLint xoffset
, GLint yoffset
,
2338 GLsizei width
, GLsizei height
,
2339 GLenum format
, GLenum type
,
2340 const GLvoid
*pixels
)
2342 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2343 struct gl_texture_unit
*texUnit
;
2344 struct gl_texture_object
*texObj
;
2345 struct gl_texture_image
*texImage
;
2346 GET_CURRENT_CONTEXT(ctx
);
2347 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2349 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2350 _mesa_update_state(ctx
);
2352 /* XXX should test internal format */
2353 if (is_color_format(format
)) {
2354 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2358 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2359 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2360 return; /* error was detected */
2363 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2364 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2365 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2368 if (width
== 0 || height
== 0)
2369 return; /* no-op, not an error */
2371 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2372 xoffset
+= texImage
->Border
;
2373 yoffset
+= texImage
->Border
;
2375 ASSERT(ctx
->Driver
.TexSubImage2D
);
2376 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
2377 width
, height
, format
, type
, pixels
,
2378 &ctx
->Unpack
, texObj
, texImage
);
2379 ctx
->NewState
|= _NEW_TEXTURE
;
2385 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2386 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2387 GLsizei width
, GLsizei height
, GLsizei depth
,
2388 GLenum format
, GLenum type
,
2389 const GLvoid
*pixels
)
2391 struct gl_texture_unit
*texUnit
;
2392 struct gl_texture_object
*texObj
;
2393 struct gl_texture_image
*texImage
;
2394 GET_CURRENT_CONTEXT(ctx
);
2395 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2397 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2398 _mesa_update_state(ctx
);
2400 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2401 width
, height
, depth
, format
, type
)) {
2402 return; /* error was detected */
2405 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2406 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2407 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2410 if (width
== 0 || height
== 0 || height
== 0)
2411 return; /* no-op, not an error */
2413 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2414 xoffset
+= texImage
->Border
;
2415 yoffset
+= texImage
->Border
;
2416 zoffset
+= texImage
->Border
;
2418 ASSERT(ctx
->Driver
.TexSubImage3D
);
2419 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
2420 xoffset
, yoffset
, zoffset
,
2421 width
, height
, depth
,
2422 format
, type
, pixels
,
2423 &ctx
->Unpack
, texObj
, texImage
);
2424 ctx
->NewState
|= _NEW_TEXTURE
;
2430 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2431 GLenum internalFormat
,
2433 GLsizei width
, GLint border
)
2435 struct gl_texture_unit
*texUnit
;
2436 struct gl_texture_object
*texObj
;
2437 struct gl_texture_image
*texImage
;
2438 GLsizei postConvWidth
= width
;
2439 GET_CURRENT_CONTEXT(ctx
);
2440 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2442 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2443 _mesa_update_state(ctx
);
2445 if (is_color_format(internalFormat
)) {
2446 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2449 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2450 postConvWidth
, 1, border
))
2453 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2454 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2455 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2457 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2460 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2461 /* free the old texture data */
2462 MESA_PBUFFER_FREE(texImage
->Data
);
2464 texImage
->Data
= NULL
;
2466 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2467 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2468 border
, internalFormat
);
2471 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2472 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
2473 x
, y
, width
, border
);
2475 ASSERT(texImage
->TexFormat
);
2477 /* If driver didn't explicitly set these, use the defaults */
2478 if (!texImage
->FetchTexelc
)
2479 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2480 if (!texImage
->FetchTexelf
)
2481 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2482 ASSERT(texImage
->FetchTexelc
);
2483 ASSERT(texImage
->FetchTexelf
);
2486 texObj
->Complete
= GL_FALSE
;
2487 ctx
->NewState
|= _NEW_TEXTURE
;
2493 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2494 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2497 struct gl_texture_unit
*texUnit
;
2498 struct gl_texture_object
*texObj
;
2499 struct gl_texture_image
*texImage
;
2500 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2501 GET_CURRENT_CONTEXT(ctx
);
2502 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2504 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2505 _mesa_update_state(ctx
);
2507 if (is_color_format(internalFormat
)) {
2508 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2512 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2513 postConvWidth
, postConvHeight
, border
))
2516 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2517 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2518 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2520 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2523 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2524 /* free the old texture data */
2525 MESA_PBUFFER_FREE(texImage
->Data
);
2527 texImage
->Data
= NULL
;
2529 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2530 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2531 postConvWidth
, postConvHeight
, 1,
2532 border
, internalFormat
);
2534 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2535 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
2536 x
, y
, width
, height
, border
);
2538 ASSERT(texImage
->TexFormat
);
2540 /* If driver didn't explicitly set these, use the defaults */
2541 if (!texImage
->FetchTexelc
)
2542 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2543 if (!texImage
->FetchTexelf
)
2544 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2545 ASSERT(texImage
->FetchTexelc
);
2546 ASSERT(texImage
->FetchTexelf
);
2549 texObj
->Complete
= GL_FALSE
;
2550 ctx
->NewState
|= _NEW_TEXTURE
;
2556 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2557 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2559 struct gl_texture_unit
*texUnit
;
2560 struct gl_texture_image
*texImage
;
2561 GLsizei postConvWidth
= width
;
2562 GET_CURRENT_CONTEXT(ctx
);
2563 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2565 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2566 _mesa_update_state(ctx
);
2568 /* XXX should test internal format */
2569 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2571 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
2572 xoffset
, 0, 0, postConvWidth
, 1))
2575 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2576 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2579 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2580 xoffset
+= texImage
->Border
;
2582 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2583 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
2584 ctx
->NewState
|= _NEW_TEXTURE
;
2590 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2591 GLint xoffset
, GLint yoffset
,
2592 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2594 struct gl_texture_unit
*texUnit
;
2595 struct gl_texture_image
*texImage
;
2596 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2597 GET_CURRENT_CONTEXT(ctx
);
2598 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2600 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2601 _mesa_update_state(ctx
);
2603 /* XXX should test internal format */
2604 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2606 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2607 postConvWidth
, postConvHeight
))
2610 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2611 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2614 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2615 xoffset
+= texImage
->Border
;
2616 yoffset
+= texImage
->Border
;
2618 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
2619 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
2620 xoffset
, yoffset
, x
, y
, width
, height
);
2621 ctx
->NewState
|= _NEW_TEXTURE
;
2627 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2628 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2629 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2631 struct gl_texture_unit
*texUnit
;
2632 struct gl_texture_image
*texImage
;
2633 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2634 GET_CURRENT_CONTEXT(ctx
);
2635 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2637 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2638 _mesa_update_state(ctx
);
2640 /* XXX should test internal format */
2641 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2643 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2644 zoffset
, postConvWidth
, postConvHeight
))
2647 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2648 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2651 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2652 xoffset
+= texImage
->Border
;
2653 yoffset
+= texImage
->Border
;
2654 zoffset
+= texImage
->Border
;
2656 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2657 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2658 xoffset
, yoffset
, zoffset
,
2659 x
, y
, width
, height
);
2660 ctx
->NewState
|= _NEW_TEXTURE
;
2666 /**********************************************************************/
2667 /****** Compressed Textures ******/
2668 /**********************************************************************/
2672 * Error checking for glCompressedTexImage[123]D().
2673 * \return error code or GL_NO_ERROR.
2676 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
2677 GLenum target
, GLint level
,
2678 GLenum internalFormat
, GLsizei width
,
2679 GLsizei height
, GLsizei depth
, GLint border
,
2682 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2684 if (dimensions
== 1) {
2685 /* 1D compressed textures not allowed */
2686 return GL_INVALID_ENUM
;
2688 else if (dimensions
== 2) {
2689 if (target
== GL_PROXY_TEXTURE_2D
) {
2690 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2692 else if (target
== GL_TEXTURE_2D
) {
2693 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2695 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2696 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2697 return GL_INVALID_ENUM
; /*target*/
2698 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2700 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2701 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2702 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2703 return GL_INVALID_ENUM
; /*target*/
2704 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2707 return GL_INVALID_ENUM
; /*target*/
2710 else if (dimensions
== 3) {
2711 /* 3D compressed textures not allowed */
2712 return GL_INVALID_ENUM
;
2715 maxTextureSize
= 1 << (maxLevels
- 1);
2717 if (!is_compressed_format(ctx
, internalFormat
))
2718 return GL_INVALID_ENUM
;
2720 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
2721 return GL_INVALID_ENUM
;
2724 return GL_INVALID_VALUE
;
2727 * XXX We should probably use the proxy texture error check function here.
2729 if (width
< 1 || width
> maxTextureSize
||
2730 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(width
) != 1))
2731 return GL_INVALID_VALUE
;
2733 if ((height
< 1 || height
> maxTextureSize
||
2734 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(height
) != 1))
2736 return GL_INVALID_VALUE
;
2738 if ((depth
< 1 || depth
> maxTextureSize
||
2739 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(depth
) != 1))
2741 return GL_INVALID_VALUE
;
2743 /* For cube map, width must equal height */
2744 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2745 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
2746 return GL_INVALID_VALUE
;
2748 if (level
< 0 || level
>= maxLevels
)
2749 return GL_INVALID_VALUE
;
2751 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2753 if (expectedSize
!= imageSize
)
2754 return GL_INVALID_VALUE
;
2761 * Error checking for glCompressedTexSubImage[123]D().
2762 * \warning There are some bad assumptions here about the size of compressed
2763 * texture tiles (multiple of 4) used to test the validity of the
2764 * offset and size parameters.
2765 * \return error code or GL_NO_ERROR.
2768 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
2769 GLenum target
, GLint level
,
2770 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2771 GLsizei width
, GLsizei height
, GLsizei depth
,
2772 GLenum format
, GLsizei imageSize
)
2774 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2777 if (dimensions
== 1) {
2778 /* 1D compressed textures not allowed */
2779 return GL_INVALID_ENUM
;
2781 else if (dimensions
== 2) {
2782 if (target
== GL_PROXY_TEXTURE_2D
) {
2783 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2785 else if (target
== GL_TEXTURE_2D
) {
2786 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2788 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2789 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2790 return GL_INVALID_ENUM
; /*target*/
2791 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2793 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2794 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2795 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2796 return GL_INVALID_ENUM
; /*target*/
2797 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2800 return GL_INVALID_ENUM
; /*target*/
2803 else if (dimensions
== 3) {
2804 /* 3D compressed textures not allowed */
2805 return GL_INVALID_ENUM
;
2808 maxTextureSize
= 1 << (maxLevels
- 1);
2810 if (!is_compressed_format(ctx
, format
))
2811 return GL_INVALID_ENUM
;
2813 if (width
< 1 || width
> maxTextureSize
)
2814 return GL_INVALID_VALUE
;
2816 if ((height
< 1 || height
> maxTextureSize
)
2818 return GL_INVALID_VALUE
;
2820 if (level
< 0 || level
>= maxLevels
)
2821 return GL_INVALID_VALUE
;
2823 /* XXX these tests are specific to the compressed format.
2824 * this code should be generalized in some way.
2826 if ((xoffset
& 3) != 0 || (yoffset
& 3) != 0)
2827 return GL_INVALID_VALUE
;
2829 if ((width
& 3) != 0 && width
!= 2 && width
!= 1)
2830 return GL_INVALID_VALUE
;
2832 if ((height
& 3) != 0 && height
!= 2 && height
!= 1)
2833 return GL_INVALID_VALUE
;
2835 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2837 if (expectedSize
!= imageSize
)
2838 return GL_INVALID_VALUE
;
2846 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2847 GLenum internalFormat
, GLsizei width
,
2848 GLint border
, GLsizei imageSize
,
2851 GET_CURRENT_CONTEXT(ctx
);
2852 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2854 if (target
== GL_TEXTURE_1D
) {
2855 struct gl_texture_unit
*texUnit
;
2856 struct gl_texture_object
*texObj
;
2857 struct gl_texture_image
*texImage
;
2858 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2859 internalFormat
, width
, 1, 1, border
, imageSize
);
2861 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
2865 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2866 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2867 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2869 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
2872 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2873 MESA_PBUFFER_FREE(texImage
->Data
);
2875 texImage
->Data
= NULL
;
2877 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2878 border
, internalFormat
);
2880 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2881 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2882 internalFormat
, width
, border
,
2887 texObj
->Complete
= GL_FALSE
;
2888 ctx
->NewState
|= _NEW_TEXTURE
;
2890 else if (target
== GL_PROXY_TEXTURE_1D
) {
2891 /* Proxy texture: check for errors and update proxy state */
2892 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2893 internalFormat
, width
, 1, 1, border
, imageSize
);
2895 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2896 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2897 internalFormat
, GL_NONE
, GL_NONE
,
2898 width
, 1, 1, border
);
2901 /* if error, clear all proxy texture image parameters */
2902 struct gl_texture_image
*texImage
;
2903 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2905 clear_teximage_fields(texImage
);
2908 /* store the teximage parameters */
2909 struct gl_texture_unit
*texUnit
;
2910 struct gl_texture_image
*texImage
;
2911 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2912 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2913 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2914 border
, internalFormat
);
2918 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
2925 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
2926 GLenum internalFormat
, GLsizei width
,
2927 GLsizei height
, GLint border
, GLsizei imageSize
,
2930 GET_CURRENT_CONTEXT(ctx
);
2931 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2933 if (target
== GL_TEXTURE_2D
||
2934 (ctx
->Extensions
.ARB_texture_cube_map
&&
2935 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2936 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
2937 struct gl_texture_unit
*texUnit
;
2938 struct gl_texture_object
*texObj
;
2939 struct gl_texture_image
*texImage
;
2940 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2941 internalFormat
, width
, height
, 1, border
, imageSize
);
2943 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
2947 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2948 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2949 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2951 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
2954 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2955 MESA_PBUFFER_FREE(texImage
->Data
);
2957 texImage
->Data
= NULL
;
2959 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2960 border
, internalFormat
);
2962 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
2963 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
2964 internalFormat
, width
, height
,
2965 border
, imageSize
, data
,
2969 texObj
->Complete
= GL_FALSE
;
2970 ctx
->NewState
|= _NEW_TEXTURE
;
2972 else if (target
== GL_PROXY_TEXTURE_2D
||
2973 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2974 ctx
->Extensions
.ARB_texture_cube_map
)) {
2975 /* Proxy texture: check for errors and update proxy state */
2976 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2977 internalFormat
, width
, height
, 1, border
, imageSize
);
2979 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2980 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2981 internalFormat
, GL_NONE
, GL_NONE
,
2982 width
, height
, 1, border
);
2985 /* if error, clear all proxy texture image parameters */
2986 struct gl_texture_image
*texImage
;
2987 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2989 clear_teximage_fields(texImage
);
2992 /* store the teximage parameters */
2993 struct gl_texture_unit
*texUnit
;
2994 struct gl_texture_image
*texImage
;
2995 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2996 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2997 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2998 border
, internalFormat
);
3002 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3009 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3010 GLenum internalFormat
, GLsizei width
,
3011 GLsizei height
, GLsizei depth
, GLint border
,
3012 GLsizei imageSize
, const GLvoid
*data
)
3014 GET_CURRENT_CONTEXT(ctx
);
3015 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3017 if (target
== GL_TEXTURE_3D
) {
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
, 3, target
, level
,
3022 internalFormat
, width
, height
, depth
, border
, imageSize
);
3024 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
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
, "glCompressedTexImage3D");
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
, depth
,
3041 border
, internalFormat
);
3043 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3044 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
3046 width
, height
, depth
,
3047 border
, imageSize
, data
,
3051 texObj
->Complete
= GL_FALSE
;
3052 ctx
->NewState
|= _NEW_TEXTURE
;
3054 else if (target
== GL_PROXY_TEXTURE_3D
) {
3055 /* Proxy texture: check for errors and update proxy state */
3056 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3057 internalFormat
, width
, height
, depth
, border
, imageSize
);
3059 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3060 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3061 internalFormat
, GL_NONE
, GL_NONE
,
3062 width
, height
, depth
, border
);
3065 /* if error, clear all proxy texture image parameters */
3066 struct gl_texture_image
*texImage
;
3067 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3069 clear_teximage_fields(texImage
);
3072 /* store the teximage parameters */
3073 struct gl_texture_unit
*texUnit
;
3074 struct gl_texture_image
*texImage
;
3075 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3076 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3077 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3078 depth
, border
, internalFormat
);
3082 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3089 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3090 GLsizei width
, GLenum format
,
3091 GLsizei imageSize
, const GLvoid
*data
)
3093 struct gl_texture_unit
*texUnit
;
3094 struct gl_texture_object
*texObj
;
3095 struct gl_texture_image
*texImage
;
3097 GET_CURRENT_CONTEXT(ctx
);
3098 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3100 error
= compressed_subtexture_error_check(ctx
, 1, target
, level
,
3101 xoffset
, 0, 0, width
, 1, 1, format
, imageSize
);
3103 _mesa_error(ctx
, error
, "glCompressedTexSubImage1D");
3107 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3108 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3109 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3112 if ((GLint
) format
!= texImage
->IntFormat
) {
3113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3114 "glCompressedTexSubImage1D(format)");
3118 if ((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) {
3119 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage1D(width)");
3124 return; /* no-op, not an error */
3126 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3127 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
3129 format
, imageSize
, data
,
3132 ctx
->NewState
|= _NEW_TEXTURE
;
3137 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3138 GLint yoffset
, GLsizei width
, GLsizei height
,
3139 GLenum format
, GLsizei imageSize
,
3142 struct gl_texture_unit
*texUnit
;
3143 struct gl_texture_object
*texObj
;
3144 struct gl_texture_image
*texImage
;
3146 GET_CURRENT_CONTEXT(ctx
);
3147 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3149 error
= compressed_subtexture_error_check(ctx
, 2, target
, level
,
3150 xoffset
, yoffset
, 0, width
, height
, 1, format
, imageSize
);
3152 /* XXX proxy target? */
3153 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3157 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3158 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3159 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3162 if ((GLint
) format
!= texImage
->IntFormat
) {
3163 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3164 "glCompressedTexSubImage2D(format)");
3168 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3169 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
)) {
3170 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage2D(size)");
3174 if (width
== 0 || height
== 0)
3175 return; /* no-op, not an error */
3177 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3178 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
3179 xoffset
, yoffset
, width
, height
,
3180 format
, imageSize
, data
,
3183 ctx
->NewState
|= _NEW_TEXTURE
;
3188 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3189 GLint yoffset
, GLint zoffset
, GLsizei width
,
3190 GLsizei height
, GLsizei depth
, GLenum format
,
3191 GLsizei imageSize
, const GLvoid
*data
)
3193 struct gl_texture_unit
*texUnit
;
3194 struct gl_texture_object
*texObj
;
3195 struct gl_texture_image
*texImage
;
3197 GET_CURRENT_CONTEXT(ctx
);
3198 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3200 error
= compressed_subtexture_error_check(ctx
, 3, target
, level
,
3201 xoffset
, yoffset
, zoffset
, width
, height
, depth
, format
, imageSize
);
3203 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3207 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3208 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3209 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3212 if ((GLint
) format
!= texImage
->IntFormat
) {
3213 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3214 "glCompressedTexSubImage3D(format)");
3218 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3219 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
) ||
3220 ((depth
== 1 || depth
== 2) && (GLuint
) depth
!= texImage
->Depth
)) {
3221 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage3D(size)");
3225 if (width
== 0 || height
== 0 || depth
== 0)
3226 return; /* no-op, not an error */
3228 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3229 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
3230 xoffset
, yoffset
, zoffset
,
3231 width
, height
, depth
,
3232 format
, imageSize
, data
,
3235 ctx
->NewState
|= _NEW_TEXTURE
;
3240 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
3242 const struct gl_texture_unit
*texUnit
;
3243 const struct gl_texture_object
*texObj
;
3244 struct gl_texture_image
*texImage
;
3246 GET_CURRENT_CONTEXT(ctx
);
3247 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3249 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3250 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3252 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
3256 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3257 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
3259 if (level
< 0 || level
>= maxLevels
) {
3260 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3264 if (is_proxy_target(target
)) {
3265 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
3269 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3271 /* probably invalid mipmap level */
3272 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3276 if (!texImage
->IsCompressed
) {
3277 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
3281 /* this typically calls _mesa_get_compressed_teximage() */
3282 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
, texObj
,texImage
);