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
:
538 GLuint face
= ((GLuint
) target
-
539 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
540 tObj
->Image
[face
][level
] = texImage
;
543 case GL_TEXTURE_RECTANGLE_NV
:
545 tObj
->Image
[0][level
] = texImage
;
548 _mesa_problem(NULL
, "bad target in _mesa_set_tex_image()");
551 /* Set the 'back' pointer */
552 texImage
->TexObject
= tObj
;
557 * Allocate a texture image structure.
559 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
562 * \return a pointer to gl_texture_image struct with all fields initialized to
565 struct gl_texture_image
*
566 _mesa_new_texture_image( GLcontext
*ctx
)
569 return CALLOC_STRUCT(gl_texture_image
);
574 * Free texture image.
576 * \param teximage texture image.
578 * Free the texture image structure and the associated image data if it's not
579 * marked as client data.
582 _mesa_delete_texture_image( struct gl_texture_image
*teximage
)
584 if (teximage
->Data
&& !teximage
->IsClientData
) {
585 MESA_PBUFFER_FREE( teximage
->Data
);
586 teximage
->Data
= NULL
;
593 * Test if a target is a proxy target.
595 * \param target texture target.
597 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
600 is_proxy_target(GLenum target
)
602 return (target
== GL_PROXY_TEXTURE_1D
||
603 target
== GL_PROXY_TEXTURE_2D
||
604 target
== GL_PROXY_TEXTURE_3D
||
605 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
606 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
);
611 * Get the texture object that corresponds to the target of the given texture unit.
613 * \param ctx GL context.
614 * \param texUnit texture unit.
615 * \param target texture target.
617 * \return pointer to the texture object on success, or NULL on failure.
619 * \sa gl_texture_unit.
621 struct gl_texture_object
*
622 _mesa_select_tex_object(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
627 return texUnit
->Current1D
;
628 case GL_PROXY_TEXTURE_1D
:
629 return ctx
->Texture
.Proxy1D
;
631 return texUnit
->Current2D
;
632 case GL_PROXY_TEXTURE_2D
:
633 return ctx
->Texture
.Proxy2D
;
635 return texUnit
->Current3D
;
636 case GL_PROXY_TEXTURE_3D
:
637 return ctx
->Texture
.Proxy3D
;
638 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
639 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
640 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
641 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
642 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
643 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
644 case GL_TEXTURE_CUBE_MAP_ARB
:
645 return ctx
->Extensions
.ARB_texture_cube_map
646 ? texUnit
->CurrentCubeMap
: NULL
;
647 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
648 return ctx
->Extensions
.ARB_texture_cube_map
649 ? ctx
->Texture
.ProxyCubeMap
: NULL
;
650 case GL_TEXTURE_RECTANGLE_NV
:
651 return ctx
->Extensions
.NV_texture_rectangle
652 ? texUnit
->CurrentRect
: NULL
;
653 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
654 return ctx
->Extensions
.NV_texture_rectangle
655 ? ctx
->Texture
.ProxyRect
: NULL
;
657 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
664 * Get the texture image struct which corresponds to target and level
665 * of the given texture unit.
667 * \param ctx GL context.
668 * \param texUnit texture unit.
669 * \param target texture target.
670 * \param level image level.
672 * \return pointer to the texture image structure on success, or NULL on failure.
674 * \sa gl_texture_unit.
676 struct gl_texture_image
*
677 _mesa_select_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
678 GLenum target
, GLint level
)
681 ASSERT(level
< MAX_TEXTURE_LEVELS
);
684 return texUnit
->Current1D
->Image
[0][level
];
685 case GL_PROXY_TEXTURE_1D
:
686 return ctx
->Texture
.Proxy1D
->Image
[0][level
];
688 return texUnit
->Current2D
->Image
[0][level
];
689 case GL_PROXY_TEXTURE_2D
:
690 return ctx
->Texture
.Proxy2D
->Image
[0][level
];
692 return texUnit
->Current3D
->Image
[0][level
];
693 case GL_PROXY_TEXTURE_3D
:
694 return ctx
->Texture
.Proxy3D
->Image
[0][level
];
695 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
696 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
697 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
698 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
699 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
700 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
701 if (ctx
->Extensions
.ARB_texture_cube_map
) {
702 GLuint face
= ((GLuint
) target
-
703 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
704 return texUnit
->CurrentCubeMap
->Image
[face
][level
];
708 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
709 if (ctx
->Extensions
.ARB_texture_cube_map
)
710 return ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
713 case GL_TEXTURE_RECTANGLE_NV
:
714 if (ctx
->Extensions
.NV_texture_rectangle
) {
716 return texUnit
->CurrentRect
->Image
[0][level
];
721 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
722 if (ctx
->Extensions
.NV_texture_rectangle
) {
724 return ctx
->Texture
.ProxyRect
->Image
[0][level
];
730 _mesa_problem(ctx
, "bad target in _mesa_select_tex_image()");
737 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
738 * it and install it. Only return NULL if passed a bad parameter or run
741 struct gl_texture_image
*
742 _mesa_get_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
743 GLenum target
, GLint level
)
745 struct gl_texture_image
*texImage
;
746 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
748 struct gl_texture_object
*texObj
;
749 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
751 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
754 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
756 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
763 * Return pointer to the specified proxy texture image.
764 * Note that proxy textures are per-context, not per-texture unit.
765 * \return pointer to texture image or NULL if invalid target, invalid
766 * level, or out of memory.
768 struct gl_texture_image
*
769 _mesa_get_proxy_tex_image(GLcontext
*ctx
, GLenum target
, GLint level
)
771 struct gl_texture_image
*texImage
;
777 case GL_PROXY_TEXTURE_1D
:
778 if (level
>= ctx
->Const
.MaxTextureLevels
)
780 texImage
= ctx
->Texture
.Proxy1D
->Image
[0][level
];
782 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
784 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
787 ctx
->Texture
.Proxy1D
->Image
[0][level
] = texImage
;
788 /* Set the 'back' pointer */
789 texImage
->TexObject
= ctx
->Texture
.Proxy1D
;
792 case GL_PROXY_TEXTURE_2D
:
793 if (level
>= ctx
->Const
.MaxTextureLevels
)
795 texImage
= ctx
->Texture
.Proxy2D
->Image
[0][level
];
797 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
799 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
802 ctx
->Texture
.Proxy2D
->Image
[0][level
] = texImage
;
803 /* Set the 'back' pointer */
804 texImage
->TexObject
= ctx
->Texture
.Proxy2D
;
807 case GL_PROXY_TEXTURE_3D
:
808 if (level
>= ctx
->Const
.Max3DTextureLevels
)
810 texImage
= ctx
->Texture
.Proxy3D
->Image
[0][level
];
812 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
814 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
817 ctx
->Texture
.Proxy3D
->Image
[0][level
] = texImage
;
818 /* Set the 'back' pointer */
819 texImage
->TexObject
= ctx
->Texture
.Proxy3D
;
822 case GL_PROXY_TEXTURE_CUBE_MAP
:
823 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
825 texImage
= ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
827 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
829 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
832 ctx
->Texture
.ProxyCubeMap
->Image
[0][level
] = texImage
;
833 /* Set the 'back' pointer */
834 texImage
->TexObject
= ctx
->Texture
.ProxyCubeMap
;
837 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
840 texImage
= ctx
->Texture
.ProxyRect
->Image
[0][level
];
842 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
844 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
847 ctx
->Texture
.ProxyRect
->Image
[0][level
] = texImage
;
848 /* Set the 'back' pointer */
849 texImage
->TexObject
= ctx
->Texture
.ProxyRect
;
859 * Get the maximum number of allowed mipmap levels.
861 * \param ctx GL context.
862 * \param target texture target.
864 * \return the maximum number of allowed mipmap levels for the given
865 * texture target, or zero if passed a bad target.
870 _mesa_max_texture_levels(GLcontext
*ctx
, GLenum target
)
874 case GL_PROXY_TEXTURE_1D
:
876 case GL_PROXY_TEXTURE_2D
:
877 return ctx
->Const
.MaxTextureLevels
;
879 case GL_PROXY_TEXTURE_3D
:
880 return ctx
->Const
.Max3DTextureLevels
;
881 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
882 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
883 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
884 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
885 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
886 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
887 case GL_TEXTURE_CUBE_MAP_ARB
:
888 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
889 return ctx
->Const
.MaxCubeTextureLevels
;
890 case GL_TEXTURE_RECTANGLE_NV
:
891 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
894 return 0; /* bad target */
900 #if 000 /* not used anymore */
902 * glTexImage[123]D can accept a NULL image pointer. In this case we
903 * create a texture image with unspecified image contents per the OpenGL
907 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
909 const GLint components
= _mesa_components_in_format(format
);
910 const GLint numPixels
= width
* height
* depth
;
911 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
915 * Let's see if anyone finds this. If glTexImage2D() is called with
916 * a NULL image pointer then load the texture image with something
917 * interesting instead of leaving it indeterminate.
920 static const char message
[8][32] = {
924 " X X XXXX XXX XXXXX ",
927 " X X XXXXX XXX X X ",
931 GLubyte
*imgPtr
= data
;
933 for (h
= 0; h
< depth
; h
++) {
934 for (i
= 0; i
< height
; i
++) {
935 GLint srcRow
= 7 - (i
% 8);
936 for (j
= 0; j
< width
; j
++) {
937 GLint srcCol
= j
% 32;
938 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
939 for (k
= 0; k
< components
; k
++) {
955 * Reset the fields of a gl_texture_image struct to zero.
957 * \param img texture image structure.
959 * This is called when a proxy texture test fails, we set all the
960 * image members (except DriverData) to zero.
961 * It's also used in glTexImage[123]D as a safeguard to be sure all
962 * required fields get initialized properly by the Driver.TexImage[123]D
966 clear_teximage_fields(struct gl_texture_image
*img
)
983 img
->TexFormat
= &_mesa_null_texformat
;
984 img
->FetchTexelc
= NULL
;
985 img
->FetchTexelf
= NULL
;
986 img
->IsCompressed
= 0;
987 img
->CompressedSize
= 0;
992 * Initialize basic fields of the gl_texture_image struct.
994 * \param ctx GL context.
995 * \param target texture target.
996 * \param img texture image structure to be initialized.
997 * \param width image width.
998 * \param height image height.
999 * \param depth image depth.
1000 * \param border image border.
1001 * \param internalFormat internal format.
1003 * Fills in the fields of \p img with the given information.
1004 * Note: width, height and depth include the border.
1007 _mesa_init_teximage_fields(GLcontext
*ctx
, GLenum target
,
1008 struct gl_texture_image
*img
,
1009 GLsizei width
, GLsizei height
, GLsizei depth
,
1010 GLint border
, GLenum internalFormat
)
1013 img
->Format
= _mesa_base_tex_format( ctx
, internalFormat
);
1014 ASSERT(img
->Format
> 0);
1015 img
->IntFormat
= internalFormat
;
1016 img
->Border
= border
;
1018 img
->Height
= height
;
1020 img
->RowStride
= width
;
1021 img
->WidthLog2
= logbase2(width
- 2 * border
);
1022 if (height
== 1) /* 1-D texture */
1023 img
->HeightLog2
= 0;
1025 img
->HeightLog2
= logbase2(height
- 2 * border
);
1026 if (depth
== 1) /* 2-D texture */
1029 img
->DepthLog2
= logbase2(depth
- 2 * border
);
1030 img
->Width2
= width
- 2 * border
; /*1 << img->WidthLog2;*/
1031 img
->Height2
= height
- 2 * border
; /*1 << img->HeightLog2;*/
1032 img
->Depth2
= depth
- 2 * border
; /*1 << img->DepthLog2;*/
1033 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1034 img
->IsCompressed
= is_compressed_format(ctx
, internalFormat
);
1035 if (img
->IsCompressed
)
1036 img
->CompressedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
,
1037 height
, depth
, internalFormat
);
1039 img
->CompressedSize
= 0;
1041 if ((width
== 1 || _mesa_bitcount(width
- 2 * border
) == 1) &&
1042 (height
== 1 || _mesa_bitcount(height
- 2 * border
) == 1) &&
1043 (depth
== 1 || _mesa_bitcount(depth
- 2 * border
) == 1))
1044 img
->_IsPowerOfTwo
= GL_TRUE
;
1046 img
->_IsPowerOfTwo
= GL_FALSE
;
1048 /* Compute Width/Height/DepthScale for mipmap lod computation */
1049 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1050 /* scale = 1.0 since texture coords directly map to texels */
1051 img
->WidthScale
= 1.0;
1052 img
->HeightScale
= 1.0;
1053 img
->DepthScale
= 1.0;
1056 img
->WidthScale
= (GLfloat
) img
->Width
;
1057 img
->HeightScale
= (GLfloat
) img
->Height
;
1058 img
->DepthScale
= (GLfloat
) img
->Depth
;
1064 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1065 * level, width, height and depth against the ctx->Const limits for textures.
1067 * A hardware driver might override this function if, for example, the
1068 * max 3D texture size is 512x512x64 (i.e. not a cube).
1070 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1071 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1072 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1073 * \param level as passed to glTexImage
1074 * \param internalFormat as passed to glTexImage
1075 * \param format as passed to glTexImage
1076 * \param type as passed to glTexImage
1077 * \param width as passed to glTexImage
1078 * \param height as passed to glTexImage
1079 * \param depth as passed to glTexImage
1080 * \param border as passed to glTexImage
1081 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1084 _mesa_test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1085 GLint internalFormat
, GLenum format
, GLenum type
,
1086 GLint width
, GLint height
, GLint depth
, GLint border
)
1090 (void) internalFormat
;
1095 case GL_PROXY_TEXTURE_1D
:
1096 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1097 if (width
< 2 * border
|| width
> 2 + maxSize
||
1098 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1099 _mesa_bitcount(width
- 2 * border
) != 1) ||
1100 level
>= ctx
->Const
.MaxTextureLevels
) {
1101 /* bad width or level */
1105 case GL_PROXY_TEXTURE_2D
:
1106 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1107 if (width
< 2 * border
|| width
> 2 + maxSize
||
1108 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1109 _mesa_bitcount(width
- 2 * border
) != 1) ||
1110 height
< 2 * border
|| height
> 2 + maxSize
||
1111 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1112 _mesa_bitcount(height
- 2 * border
) != 1) ||
1113 level
>= ctx
->Const
.MaxTextureLevels
) {
1114 /* bad width or height or level */
1118 case GL_PROXY_TEXTURE_3D
:
1119 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1120 if (width
< 2 * border
|| width
> 2 + maxSize
||
1121 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1122 _mesa_bitcount(width
- 2 * border
) != 1) ||
1123 height
< 2 * border
|| height
> 2 + maxSize
||
1124 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1125 _mesa_bitcount(height
- 2 * border
) != 1) ||
1126 depth
< 2 * border
|| depth
> 2 + maxSize
||
1127 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1128 _mesa_bitcount(depth
- 2 * border
) != 1) ||
1129 level
>= ctx
->Const
.Max3DTextureLevels
) {
1130 /* bad width or height or depth or level */
1134 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1135 if (width
< 1 || width
> ctx
->Const
.MaxTextureRectSize
||
1136 height
< 1 || height
> ctx
->Const
.MaxTextureRectSize
||
1138 /* bad width or height or level */
1142 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1143 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1144 if (width
< 2 * border
|| width
> 2 + maxSize
||
1145 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1146 _mesa_bitcount(width
- 2 * border
) != 1) ||
1147 height
< 2 * border
|| height
> 2 + maxSize
||
1148 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1149 _mesa_bitcount(height
- 2 * border
) != 1) ||
1150 level
>= ctx
->Const
.MaxCubeTextureLevels
) {
1151 /* bad width or height */
1156 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1163 * Test the glTexImage[123]D() parameters for errors.
1165 * \param ctx GL context.
1166 * \param target texture target given by the user.
1167 * \param level image level given by the user.
1168 * \param internalFormat internal format given by the user.
1169 * \param format pixel data format given by the user.
1170 * \param type pixel data type given by the user.
1171 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1172 * \param width image width given by the user.
1173 * \param height image height given by the user.
1174 * \param depth image depth given by the user.
1175 * \param border image border given by the user.
1177 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1179 * Verifies each of the parameters against the constants specified in
1180 * __GLcontextRec::Const and the supported extensions, and according to the
1181 * OpenGL specification.
1184 texture_error_check( GLcontext
*ctx
, GLenum target
,
1185 GLint level
, GLint internalFormat
,
1186 GLenum format
, GLenum type
,
1188 GLint width
, GLint height
,
1189 GLint depth
, GLint border
)
1191 const GLboolean isProxy
= is_proxy_target(target
);
1193 GLboolean colorFormat
, indexFormat
;
1195 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1196 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1198 _mesa_error(ctx
, GL_INVALID_VALUE
,
1199 "glTexImage%dD(level=%d)", dimensions
, level
);
1205 if (border
< 0 || border
> 1 ||
1206 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1207 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1209 _mesa_error(ctx
, GL_INVALID_VALUE
,
1210 "glTexImage%dD(border=%d)", dimensions
, border
);
1215 if (width
< 0 || height
< 0 || depth
< 0) {
1217 _mesa_error(ctx
, GL_INVALID_VALUE
,
1218 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1223 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1224 * level, width, height and depth.
1226 if (dimensions
== 1) {
1227 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1228 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1229 level
, internalFormat
,
1231 width
, 1, 1, border
);
1234 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1238 else if (dimensions
== 2) {
1239 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1240 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1241 level
, internalFormat
,
1243 width
, height
, 1, border
);
1245 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1246 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1247 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1248 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1249 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1252 sizeOK
= (width
== height
) &&
1253 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1254 level
, internalFormat
, format
, type
,
1255 width
, height
, 1, border
);
1257 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1258 target
== GL_TEXTURE_RECTANGLE_NV
) {
1259 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1260 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1263 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1264 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1265 level
, internalFormat
,
1267 width
, height
, 1, border
);
1270 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1274 else if (dimensions
== 3) {
1275 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1276 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_3D
,
1277 level
, internalFormat
,
1279 width
, height
, depth
, border
);
1282 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1287 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1293 _mesa_error(ctx
, GL_INVALID_VALUE
,
1294 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1295 dimensions
, level
, width
, height
, depth
);
1300 /* Check internalFormat */
1301 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1303 _mesa_error(ctx
, GL_INVALID_VALUE
,
1304 "glTexImage%dD(internalFormat=0x%x)",
1305 dimensions
, internalFormat
);
1310 /* Check incoming image format and type */
1311 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1312 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1313 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1316 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1317 "glTexImage%dD(format or type)", dimensions
);
1322 /* make sure internal format and format basically agree */
1323 colorFormat
= is_color_format(format
);
1324 indexFormat
= is_index_format(format
);
1325 if ((is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1326 (is_index_format(internalFormat
) && !indexFormat
) ||
1327 (is_depth_format(internalFormat
) != is_depth_format(format
)) ||
1328 (is_ycbcr_format(internalFormat
) != is_ycbcr_format(format
))) {
1330 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1331 "glTexImage(internalFormat/format)");
1335 /* additional checks for ycbcr textures */
1336 if (internalFormat
== GL_YCBCR_MESA
) {
1337 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1338 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1339 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1341 _mesa_sprintf(message
,
1342 "glTexImage%d(format/type YCBCR mismatch", dimensions
);
1343 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
1344 return GL_TRUE
; /* error */
1346 if (target
!= GL_TEXTURE_2D
&&
1347 target
!= GL_PROXY_TEXTURE_2D
&&
1348 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1349 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1351 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1357 _mesa_sprintf(message
,
1358 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1359 dimensions
, border
);
1360 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1366 /* additional checks for depth textures */
1367 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1368 /* Only 1D and 2D textures supported */
1369 if (target
!= GL_TEXTURE_1D
&&
1370 target
!= GL_PROXY_TEXTURE_1D
&&
1371 target
!= GL_TEXTURE_2D
&&
1372 target
!= GL_PROXY_TEXTURE_2D
) {
1374 _mesa_error(ctx
, GL_INVALID_ENUM
,
1375 "glTexImage(target/internalFormat)");
1380 /* additional checks for compressed textures */
1381 if (is_compressed_format(ctx
, internalFormat
)) {
1382 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1385 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1386 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1387 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1388 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1393 _mesa_error(ctx
, GL_INVALID_ENUM
,
1394 "glTexImage%d(target)", dimensions
);
1400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1401 "glTexImage%D(border!=0)", dimensions
);
1407 /* if we get here, the parameters are OK */
1413 * Test glTexSubImage[123]D() parameters for errors.
1415 * \param ctx GL context.
1416 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1417 * \param target texture target given by the user.
1418 * \param level image level given by the user.
1419 * \param xoffset sub-image x offset given by the user.
1420 * \param yoffset sub-image y offset given by the user.
1421 * \param zoffset sub-image z offset given by the user.
1422 * \param format pixel data format given by the user.
1423 * \param type pixel data type given by the user.
1424 * \param width image width given by the user.
1425 * \param height image height given by the user.
1426 * \param depth image depth given by the user.
1428 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1430 * Verifies each of the parameters against the constants specified in
1431 * __GLcontextRec::Const and the supported extensions, and according to the
1432 * OpenGL specification.
1435 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1436 GLenum target
, GLint level
,
1437 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1438 GLint width
, GLint height
, GLint depth
,
1439 GLenum format
, GLenum type
)
1441 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1442 struct gl_texture_image
*destTex
;
1445 if (dimensions
== 1) {
1446 if (target
!= GL_TEXTURE_1D
) {
1447 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1451 else if (dimensions
== 2) {
1452 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1453 target
<=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1454 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1455 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1459 else if (ctx
->Extensions
.NV_texture_rectangle
&&
1460 target
== GL_TEXTURE_RECTANGLE_NV
) {
1461 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1462 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1466 else if (target
!= GL_TEXTURE_2D
) {
1467 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1471 else if (dimensions
== 3) {
1472 if (target
!= GL_TEXTURE_3D
) {
1473 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1478 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1482 /* Basic level check */
1483 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1484 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1489 _mesa_error(ctx
, GL_INVALID_VALUE
,
1490 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1493 if (height
< 0 && dimensions
> 1) {
1494 _mesa_error(ctx
, GL_INVALID_VALUE
,
1495 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1498 if (depth
< 0 && dimensions
> 2) {
1499 _mesa_error(ctx
, GL_INVALID_VALUE
,
1500 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1504 destTex
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1507 /* undefined image level */
1508 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1512 if (xoffset
< -((GLint
)destTex
->Border
)) {
1513 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1517 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1518 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1522 if (dimensions
> 1) {
1523 if (yoffset
< -((GLint
)destTex
->Border
)) {
1524 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1528 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1529 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1534 if (dimensions
> 2) {
1535 if (zoffset
< -((GLint
)destTex
->Border
)) {
1536 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1539 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1540 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1545 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1546 _mesa_error(ctx
, GL_INVALID_ENUM
,
1547 "glTexSubImage%dD(format or type)", dimensions
);
1551 if (destTex
->IsCompressed
) {
1552 const struct gl_texture_unit
*texUnit
;
1553 const struct gl_texture_image
*texImage
;
1554 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1555 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1557 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1560 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1561 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1562 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1563 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1567 _mesa_error(ctx
, GL_INVALID_ENUM
,
1568 "glTexSubImage%D(target)", dimensions
);
1571 /* offset must be multiple of 4 */
1572 if ((xoffset
& 3) || (yoffset
& 3)) {
1573 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1574 "glTexSubImage%D(xoffset or yoffset)", dimensions
);
1577 /* size must be multiple of 4 or equal to whole texture size */
1578 if ((width
& 3) && (GLuint
) width
!= texImage
->Width
) {
1579 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1580 "glTexSubImage%D(width)", dimensions
);
1583 if ((height
& 3) && (GLuint
) height
!= texImage
->Height
) {
1584 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1585 "glTexSubImage%D(width)", dimensions
);
1595 * Test glCopyTexImage[12]D() parameters for errors.
1597 * \param ctx GL context.
1598 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1599 * \param target texture target given by the user.
1600 * \param level image level given by the user.
1601 * \param internalFormat internal format given by the user.
1602 * \param width image width given by the user.
1603 * \param height image height given by the user.
1604 * \param depth image depth given by the user.
1605 * \param border texture border.
1607 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1609 * Verifies each of the parameters against the constants specified in
1610 * __GLcontextRec::Const and the supported extensions, and according to the
1611 * OpenGL specification.
1614 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1615 GLenum target
, GLint level
, GLint internalFormat
,
1616 GLint width
, GLint height
, GLint border
)
1618 GLenum format
, type
;
1621 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1622 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1623 _mesa_error(ctx
, GL_INVALID_VALUE
,
1624 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1629 if (border
< 0 || border
> 1 ||
1630 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1631 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1635 /* The format and type aren't really significant here, but we need to pass
1636 * something to TestProxyTexImage().
1638 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1641 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1642 * level, width, height and depth.
1644 if (dimensions
== 1) {
1645 if (target
== GL_TEXTURE_1D
) {
1646 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1647 level
, internalFormat
,
1649 width
, 1, 1, border
);
1652 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1656 else if (dimensions
== 2) {
1657 if (target
== GL_TEXTURE_2D
) {
1658 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1659 level
, internalFormat
,
1661 width
, height
, 1, border
);
1663 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1664 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1665 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1666 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1669 sizeOK
= (width
== height
) &&
1670 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1671 level
, internalFormat
, format
, type
,
1672 width
, height
, 1, border
);
1674 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1675 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1676 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1679 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1680 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1681 level
, internalFormat
,
1683 width
, height
, 1, border
);
1686 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1691 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1696 if (dimensions
== 1) {
1697 _mesa_error(ctx
, GL_INVALID_VALUE
,
1698 "glCopyTexImage1D(width=%d)", width
);
1701 ASSERT(dimensions
== 2);
1702 _mesa_error(ctx
, GL_INVALID_VALUE
,
1703 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1708 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1709 _mesa_error(ctx
, GL_INVALID_VALUE
,
1710 "glCopyTexImage%dD(internalFormat)", dimensions
);
1714 if (is_compressed_format(ctx
, internalFormat
)) {
1715 if (target
!= GL_TEXTURE_2D
) {
1716 _mesa_error(ctx
, GL_INVALID_ENUM
,
1717 "glCopyTexImage%d(target)", dimensions
);
1721 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1722 "glCopyTexImage%D(border!=0)", dimensions
);
1727 /* if we get here, the parameters are OK */
1733 * Test glCopyTexImage[12]D() parameters for errors.
1735 * \param ctx GL context.
1736 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1737 * \param target texture target given by the user.
1738 * \param level image level given by the user.
1739 * \param xoffset sub-image x offset given by the user.
1740 * \param yoffset sub-image y offset given by the user.
1741 * \param zoffset sub-image z offset given by the user.
1742 * \param width image width given by the user.
1743 * \param height image height given by the user.
1745 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1747 * Verifies each of the parameters against the constants specified in
1748 * __GLcontextRec::Const and the supported extensions, and according to the
1749 * OpenGL specification.
1752 copytexsubimage_error_check( GLcontext
*ctx
, GLuint dimensions
,
1753 GLenum target
, GLint level
,
1754 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1755 GLsizei width
, GLsizei height
)
1757 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1758 struct gl_texture_image
*teximage
;
1761 if (dimensions
== 1) {
1762 if (target
!= GL_TEXTURE_1D
) {
1763 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1767 else if (dimensions
== 2) {
1768 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1769 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1770 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1771 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1775 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1776 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1777 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1781 else if (target
!= GL_TEXTURE_2D
) {
1782 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1786 else if (dimensions
== 3) {
1787 if (target
!= GL_TEXTURE_3D
) {
1788 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1794 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1795 _mesa_error(ctx
, GL_INVALID_VALUE
,
1796 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1802 _mesa_error(ctx
, GL_INVALID_VALUE
,
1803 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1806 if (dimensions
> 1 && height
< 0) {
1807 _mesa_error(ctx
, GL_INVALID_VALUE
,
1808 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1812 teximage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1814 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1815 "glCopyTexSubImage%dD(undefined texture level: %d)",
1820 if (xoffset
< -((GLint
)teximage
->Border
)) {
1821 _mesa_error(ctx
, GL_INVALID_VALUE
,
1822 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1825 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1826 _mesa_error(ctx
, GL_INVALID_VALUE
,
1827 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1830 if (dimensions
> 1) {
1831 if (yoffset
< -((GLint
)teximage
->Border
)) {
1832 _mesa_error(ctx
, GL_INVALID_VALUE
,
1833 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1836 /* NOTE: we're adding the border here, not subtracting! */
1837 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1838 _mesa_error(ctx
, GL_INVALID_VALUE
,
1839 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1844 if (dimensions
> 2) {
1845 if (zoffset
< -((GLint
)teximage
->Border
)) {
1846 _mesa_error(ctx
, GL_INVALID_VALUE
,
1847 "glCopyTexSubImage%dD(zoffset)", dimensions
);
1850 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1851 _mesa_error(ctx
, GL_INVALID_VALUE
,
1852 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1857 if (teximage
->IsCompressed
) {
1858 if (target
!= GL_TEXTURE_2D
) {
1859 _mesa_error(ctx
, GL_INVALID_ENUM
,
1860 "glCopyTexSubImage%d(target)", dimensions
);
1863 /* offset must be multiple of 4 */
1864 if ((xoffset
& 3) || (yoffset
& 3)) {
1865 _mesa_error(ctx
, GL_INVALID_VALUE
,
1866 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions
);
1869 /* size must be multiple of 4 */
1870 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
1871 _mesa_error(ctx
, GL_INVALID_VALUE
,
1872 "glCopyTexSubImage%D(width)", dimensions
);
1875 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
1876 _mesa_error(ctx
, GL_INVALID_VALUE
,
1877 "glCopyTexSubImage%D(height)", dimensions
);
1882 if (teximage
->IntFormat
== GL_YCBCR_MESA
) {
1883 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
1887 /* if we get here, the parameters are OK */
1893 * Get texture image. Called by glGetTexImage.
1895 * \param target texture target.
1896 * \param level image level.
1897 * \param format pixel data format for returned image.
1898 * \param type pixel data type for returned image.
1899 * \param pixels returned pixel data.
1902 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1903 GLenum type
, GLvoid
*pixels
)
1905 const struct gl_texture_unit
*texUnit
;
1906 const struct gl_texture_object
*texObj
;
1907 const struct gl_texture_image
*texImage
;
1908 GLint maxLevels
= 0;
1909 GET_CURRENT_CONTEXT(ctx
);
1910 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1912 texUnit
= &(ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
]);
1913 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1914 if (!texObj
|| is_proxy_target(target
)) {
1915 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
1919 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1920 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
1922 if (level
< 0 || level
>= maxLevels
) {
1923 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
1927 if (_mesa_sizeof_packed_type(type
) <= 0) {
1928 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
1932 if (_mesa_components_in_format(format
) <= 0 ||
1933 format
== GL_STENCIL_INDEX
) {
1934 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
1938 if (!ctx
->Extensions
.EXT_paletted_texture
&& is_index_format(format
)) {
1939 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1942 if (!ctx
->Extensions
.SGIX_depth_texture
&& is_depth_format(format
)) {
1943 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1946 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& is_ycbcr_format(format
)) {
1947 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1953 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1955 /* invalid mipmap level, not an error */
1959 /* Make sure the requested image format is compatible with the
1960 * texture's format. We let the colorformat-indexformat go through,
1961 * because the texelfetcher will dequantize to full rgba.
1963 if (is_color_format(format
)
1964 && !is_color_format(texImage
->TexFormat
->BaseFormat
)
1965 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1966 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1969 else if (is_index_format(format
)
1970 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1971 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1974 else if (is_depth_format(format
)
1975 && !is_depth_format(texImage
->TexFormat
->BaseFormat
)) {
1976 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1979 else if (is_ycbcr_format(format
)
1980 && !is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
1981 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1985 /* typically, this will call _mesa_get_teximage() */
1986 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
1993 * Called from the API. Note that width includes the border.
1996 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
1997 GLsizei width
, GLint border
, GLenum format
,
1998 GLenum type
, const GLvoid
*pixels
)
2000 GLsizei postConvWidth
= width
;
2001 GET_CURRENT_CONTEXT(ctx
);
2002 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2004 if (is_color_format(internalFormat
)) {
2005 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2008 if (target
== GL_TEXTURE_1D
) {
2009 /* non-proxy target */
2010 struct gl_texture_unit
*texUnit
;
2011 struct gl_texture_object
*texObj
;
2012 struct gl_texture_image
*texImage
;
2014 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2015 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2016 return; /* error was recorded */
2019 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2020 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2021 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2024 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2027 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2028 /* free the old texture data */
2029 MESA_PBUFFER_FREE(texImage
->Data
);
2031 texImage
->Data
= NULL
;
2032 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2033 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2034 postConvWidth
, 1, 1,
2035 border
, internalFormat
);
2037 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2038 _mesa_update_state(ctx
);
2040 ASSERT(ctx
->Driver
.TexImage1D
);
2042 /* Give the texture to the driver! <pixels> may be null! */
2043 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
2044 width
, border
, format
, type
, pixels
,
2045 &ctx
->Unpack
, texObj
, texImage
);
2047 ASSERT(texImage
->TexFormat
);
2049 /* If driver didn't explicitly set this, use the defaults */
2050 if (!texImage
->FetchTexelc
)
2051 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2052 if (!texImage
->FetchTexelf
)
2053 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2054 ASSERT(texImage
->FetchTexelc
);
2055 ASSERT(texImage
->FetchTexelf
);
2058 texObj
->Complete
= GL_FALSE
;
2059 ctx
->NewState
|= _NEW_TEXTURE
;
2061 else if (target
== GL_PROXY_TEXTURE_1D
) {
2062 /* Proxy texture: check for errors and update proxy state */
2063 struct gl_texture_image
*texImage
;
2064 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2065 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2066 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2067 /* when error, clear all proxy texture image parameters */
2069 clear_teximage_fields(texImage
);
2072 /* no error, set the tex image parameters */
2074 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2075 postConvWidth
, 1, 1,
2076 border
, internalFormat
);
2077 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2078 internalFormat
, format
, type
);
2082 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2089 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2090 GLsizei width
, GLsizei height
, GLint border
,
2091 GLenum format
, GLenum type
,
2092 const GLvoid
*pixels
)
2094 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2095 GET_CURRENT_CONTEXT(ctx
);
2096 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2098 if (is_color_format(internalFormat
)) {
2099 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2103 if (target
== GL_TEXTURE_2D
||
2104 (ctx
->Extensions
.ARB_texture_cube_map
&&
2105 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2106 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2107 (ctx
->Extensions
.NV_texture_rectangle
&&
2108 target
== GL_TEXTURE_RECTANGLE_NV
)) {
2109 /* non-proxy target */
2110 struct gl_texture_unit
*texUnit
;
2111 struct gl_texture_object
*texObj
;
2112 struct gl_texture_image
*texImage
;
2114 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2115 format
, type
, 2, postConvWidth
, postConvHeight
,
2117 return; /* error was recorded */
2120 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2121 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2122 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2124 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2127 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2128 /* free the old texture data */
2129 MESA_PBUFFER_FREE(texImage
->Data
);
2131 texImage
->Data
= NULL
;
2132 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2133 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2134 postConvWidth
, postConvHeight
, 1,
2135 border
, internalFormat
);
2137 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2138 _mesa_update_state(ctx
);
2140 ASSERT(ctx
->Driver
.TexImage2D
);
2142 /* Give the texture to the driver! <pixels> may be null! */
2143 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
2144 width
, height
, border
, format
, type
, pixels
,
2145 &ctx
->Unpack
, texObj
, texImage
);
2147 ASSERT(texImage
->TexFormat
);
2149 /* If driver didn't explicitly set these, use the defaults */
2150 if (!texImage
->FetchTexelc
)
2151 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2152 if (!texImage
->FetchTexelf
)
2153 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2154 ASSERT(texImage
->FetchTexelc
);
2155 ASSERT(texImage
->FetchTexelf
);
2158 texObj
->Complete
= GL_FALSE
;
2159 ctx
->NewState
|= _NEW_TEXTURE
;
2161 else if (target
== GL_PROXY_TEXTURE_2D
||
2162 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2163 ctx
->Extensions
.ARB_texture_cube_map
) ||
2164 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2165 ctx
->Extensions
.NV_texture_rectangle
)) {
2166 /* Proxy texture: check for errors and update proxy state */
2167 struct gl_texture_image
*texImage
;
2168 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2169 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2170 format
, type
, 2, postConvWidth
, postConvHeight
,
2172 /* when error, clear all proxy texture image parameters */
2174 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[0][level
]);
2177 /* no error, set the tex image parameters */
2178 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2179 postConvWidth
, postConvHeight
, 1,
2180 border
, internalFormat
);
2181 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2182 internalFormat
, format
, type
);
2186 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2193 * Called by the API or display list executor.
2194 * Note that width and height include the border.
2197 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2198 GLsizei width
, GLsizei height
, GLsizei depth
,
2199 GLint border
, GLenum format
, GLenum type
,
2200 const GLvoid
*pixels
)
2202 GET_CURRENT_CONTEXT(ctx
);
2203 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2205 if (target
== GL_TEXTURE_3D
) {
2206 struct gl_texture_unit
*texUnit
;
2207 struct gl_texture_object
*texObj
;
2208 struct gl_texture_image
*texImage
;
2209 /* non-proxy target */
2211 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2212 format
, type
, 3, width
, height
, depth
, border
)) {
2213 return; /* error was recorded */
2216 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2217 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2218 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2220 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2223 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2224 MESA_PBUFFER_FREE(texImage
->Data
);
2226 texImage
->Data
= NULL
;
2227 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2228 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2229 width
, height
, depth
,
2230 border
, internalFormat
);
2232 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2233 _mesa_update_state(ctx
);
2235 ASSERT(ctx
->Driver
.TexImage3D
);
2237 /* Give the texture to the driver! <pixels> may be null! */
2238 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, internalFormat
,
2239 width
, height
, depth
, border
, format
, type
,
2240 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2242 ASSERT(texImage
->TexFormat
);
2244 /* If driver didn't explicitly set these, use the defaults */
2245 if (!texImage
->FetchTexelc
)
2246 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2247 if (!texImage
->FetchTexelf
)
2248 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2249 ASSERT(texImage
->FetchTexelc
);
2250 ASSERT(texImage
->FetchTexelf
);
2253 texObj
->Complete
= GL_FALSE
;
2254 ctx
->NewState
|= _NEW_TEXTURE
;
2256 else if (target
== GL_PROXY_TEXTURE_3D
) {
2257 /* Proxy texture: check for errors and update proxy state */
2258 struct gl_texture_image
*texImage
;
2259 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2260 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2261 format
, type
, 3, width
, height
, depth
, border
)) {
2262 /* when error, clear all proxy texture image parameters */
2264 clear_teximage_fields(texImage
);
2267 /* no error, set the tex image parameters */
2268 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2269 border
, internalFormat
);
2270 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2271 internalFormat
, format
, type
);
2275 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2282 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2283 GLsizei width
, GLsizei height
, GLsizei depth
,
2284 GLint border
, GLenum format
, GLenum type
,
2285 const GLvoid
*pixels
)
2287 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2288 depth
, border
, format
, type
, pixels
);
2294 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2295 GLint xoffset
, GLsizei width
,
2296 GLenum format
, GLenum type
,
2297 const GLvoid
*pixels
)
2299 GLsizei postConvWidth
= width
;
2300 struct gl_texture_unit
*texUnit
;
2301 struct gl_texture_object
*texObj
;
2302 struct gl_texture_image
*texImage
;
2303 GET_CURRENT_CONTEXT(ctx
);
2304 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2306 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2307 _mesa_update_state(ctx
);
2309 /* XXX should test internal format */
2310 if (is_color_format(format
)) {
2311 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2314 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2315 postConvWidth
, 1, 1, format
, type
)) {
2316 return; /* error was detected */
2319 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2320 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2321 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2325 return; /* no-op, not an error */
2327 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2328 xoffset
+= texImage
->Border
;
2330 ASSERT(ctx
->Driver
.TexSubImage1D
);
2331 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
2332 format
, type
, pixels
, &ctx
->Unpack
,
2334 ctx
->NewState
|= _NEW_TEXTURE
;
2339 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2340 GLint xoffset
, GLint yoffset
,
2341 GLsizei width
, GLsizei height
,
2342 GLenum format
, GLenum type
,
2343 const GLvoid
*pixels
)
2345 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2346 struct gl_texture_unit
*texUnit
;
2347 struct gl_texture_object
*texObj
;
2348 struct gl_texture_image
*texImage
;
2349 GET_CURRENT_CONTEXT(ctx
);
2350 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2352 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2353 _mesa_update_state(ctx
);
2355 /* XXX should test internal format */
2356 if (is_color_format(format
)) {
2357 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2361 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2362 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2363 return; /* error was detected */
2366 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2367 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2368 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2371 if (width
== 0 || height
== 0)
2372 return; /* no-op, not an error */
2374 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2375 xoffset
+= texImage
->Border
;
2376 yoffset
+= texImage
->Border
;
2378 ASSERT(ctx
->Driver
.TexSubImage2D
);
2379 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
2380 width
, height
, format
, type
, pixels
,
2381 &ctx
->Unpack
, texObj
, texImage
);
2382 ctx
->NewState
|= _NEW_TEXTURE
;
2388 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2389 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2390 GLsizei width
, GLsizei height
, GLsizei depth
,
2391 GLenum format
, GLenum type
,
2392 const GLvoid
*pixels
)
2394 struct gl_texture_unit
*texUnit
;
2395 struct gl_texture_object
*texObj
;
2396 struct gl_texture_image
*texImage
;
2397 GET_CURRENT_CONTEXT(ctx
);
2398 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2400 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2401 _mesa_update_state(ctx
);
2403 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2404 width
, height
, depth
, format
, type
)) {
2405 return; /* error was detected */
2408 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2409 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2410 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2413 if (width
== 0 || height
== 0 || height
== 0)
2414 return; /* no-op, not an error */
2416 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2417 xoffset
+= texImage
->Border
;
2418 yoffset
+= texImage
->Border
;
2419 zoffset
+= texImage
->Border
;
2421 ASSERT(ctx
->Driver
.TexSubImage3D
);
2422 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
2423 xoffset
, yoffset
, zoffset
,
2424 width
, height
, depth
,
2425 format
, type
, pixels
,
2426 &ctx
->Unpack
, texObj
, texImage
);
2427 ctx
->NewState
|= _NEW_TEXTURE
;
2433 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2434 GLenum internalFormat
,
2436 GLsizei width
, GLint border
)
2438 struct gl_texture_unit
*texUnit
;
2439 struct gl_texture_object
*texObj
;
2440 struct gl_texture_image
*texImage
;
2441 GLsizei postConvWidth
= width
;
2442 GET_CURRENT_CONTEXT(ctx
);
2443 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2445 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2446 _mesa_update_state(ctx
);
2448 if (is_color_format(internalFormat
)) {
2449 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2452 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2453 postConvWidth
, 1, border
))
2456 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2457 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2458 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2460 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2463 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2464 /* free the old texture data */
2465 MESA_PBUFFER_FREE(texImage
->Data
);
2467 texImage
->Data
= NULL
;
2469 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2470 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2471 border
, internalFormat
);
2474 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2475 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
2476 x
, y
, width
, border
);
2478 ASSERT(texImage
->TexFormat
);
2480 /* If driver didn't explicitly set these, use the defaults */
2481 if (!texImage
->FetchTexelc
)
2482 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2483 if (!texImage
->FetchTexelf
)
2484 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2485 ASSERT(texImage
->FetchTexelc
);
2486 ASSERT(texImage
->FetchTexelf
);
2489 texObj
->Complete
= GL_FALSE
;
2490 ctx
->NewState
|= _NEW_TEXTURE
;
2496 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2497 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2500 struct gl_texture_unit
*texUnit
;
2501 struct gl_texture_object
*texObj
;
2502 struct gl_texture_image
*texImage
;
2503 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2504 GET_CURRENT_CONTEXT(ctx
);
2505 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2507 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2508 _mesa_update_state(ctx
);
2510 if (is_color_format(internalFormat
)) {
2511 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2515 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2516 postConvWidth
, postConvHeight
, border
))
2519 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2520 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2521 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2523 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2526 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2527 /* free the old texture data */
2528 MESA_PBUFFER_FREE(texImage
->Data
);
2530 texImage
->Data
= NULL
;
2532 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2533 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2534 postConvWidth
, postConvHeight
, 1,
2535 border
, internalFormat
);
2537 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2538 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
2539 x
, y
, width
, height
, border
);
2541 ASSERT(texImage
->TexFormat
);
2543 /* If driver didn't explicitly set these, use the defaults */
2544 if (!texImage
->FetchTexelc
)
2545 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2546 if (!texImage
->FetchTexelf
)
2547 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2548 ASSERT(texImage
->FetchTexelc
);
2549 ASSERT(texImage
->FetchTexelf
);
2552 texObj
->Complete
= GL_FALSE
;
2553 ctx
->NewState
|= _NEW_TEXTURE
;
2559 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2560 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2562 struct gl_texture_unit
*texUnit
;
2563 struct gl_texture_image
*texImage
;
2564 GLsizei postConvWidth
= width
;
2565 GET_CURRENT_CONTEXT(ctx
);
2566 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2568 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2569 _mesa_update_state(ctx
);
2571 /* XXX should test internal format */
2572 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2574 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
2575 xoffset
, 0, 0, postConvWidth
, 1))
2578 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2579 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2582 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2583 xoffset
+= texImage
->Border
;
2585 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2586 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
2587 ctx
->NewState
|= _NEW_TEXTURE
;
2593 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2594 GLint xoffset
, GLint yoffset
,
2595 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2597 struct gl_texture_unit
*texUnit
;
2598 struct gl_texture_image
*texImage
;
2599 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2600 GET_CURRENT_CONTEXT(ctx
);
2601 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2603 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2604 _mesa_update_state(ctx
);
2606 /* XXX should test internal format */
2607 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2609 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2610 postConvWidth
, postConvHeight
))
2613 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2614 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2617 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2618 xoffset
+= texImage
->Border
;
2619 yoffset
+= texImage
->Border
;
2621 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
2622 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
2623 xoffset
, yoffset
, x
, y
, width
, height
);
2624 ctx
->NewState
|= _NEW_TEXTURE
;
2630 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2631 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2632 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2634 struct gl_texture_unit
*texUnit
;
2635 struct gl_texture_image
*texImage
;
2636 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2637 GET_CURRENT_CONTEXT(ctx
);
2638 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2640 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2641 _mesa_update_state(ctx
);
2643 /* XXX should test internal format */
2644 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2646 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2647 zoffset
, postConvWidth
, postConvHeight
))
2650 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2651 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2654 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2655 xoffset
+= texImage
->Border
;
2656 yoffset
+= texImage
->Border
;
2657 zoffset
+= texImage
->Border
;
2659 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2660 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2661 xoffset
, yoffset
, zoffset
,
2662 x
, y
, width
, height
);
2663 ctx
->NewState
|= _NEW_TEXTURE
;
2669 /**********************************************************************/
2670 /****** Compressed Textures ******/
2671 /**********************************************************************/
2675 * Error checking for glCompressedTexImage[123]D().
2676 * \return error code or GL_NO_ERROR.
2679 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
2680 GLenum target
, GLint level
,
2681 GLenum internalFormat
, GLsizei width
,
2682 GLsizei height
, GLsizei depth
, GLint border
,
2685 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2687 if (dimensions
== 1) {
2688 /* 1D compressed textures not allowed */
2689 return GL_INVALID_ENUM
;
2691 else if (dimensions
== 2) {
2692 if (target
== GL_PROXY_TEXTURE_2D
) {
2693 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2695 else if (target
== GL_TEXTURE_2D
) {
2696 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2698 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2699 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2700 return GL_INVALID_ENUM
; /*target*/
2701 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2703 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2704 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2705 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2706 return GL_INVALID_ENUM
; /*target*/
2707 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2710 return GL_INVALID_ENUM
; /*target*/
2713 else if (dimensions
== 3) {
2714 /* 3D compressed textures not allowed */
2715 return GL_INVALID_ENUM
;
2718 maxTextureSize
= 1 << (maxLevels
- 1);
2720 if (!is_compressed_format(ctx
, internalFormat
))
2721 return GL_INVALID_ENUM
;
2723 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
2724 return GL_INVALID_ENUM
;
2727 return GL_INVALID_VALUE
;
2730 * XXX We should probably use the proxy texture error check function here.
2732 if (width
< 1 || width
> maxTextureSize
||
2733 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(width
) != 1))
2734 return GL_INVALID_VALUE
;
2736 if ((height
< 1 || height
> maxTextureSize
||
2737 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(height
) != 1))
2739 return GL_INVALID_VALUE
;
2741 if ((depth
< 1 || depth
> maxTextureSize
||
2742 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(depth
) != 1))
2744 return GL_INVALID_VALUE
;
2746 /* For cube map, width must equal height */
2747 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2748 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
2749 return GL_INVALID_VALUE
;
2751 if (level
< 0 || level
>= maxLevels
)
2752 return GL_INVALID_VALUE
;
2754 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2756 if (expectedSize
!= imageSize
)
2757 return GL_INVALID_VALUE
;
2764 * Error checking for glCompressedTexSubImage[123]D().
2765 * \warning There are some bad assumptions here about the size of compressed
2766 * texture tiles (multiple of 4) used to test the validity of the
2767 * offset and size parameters.
2768 * \return error code or GL_NO_ERROR.
2771 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
2772 GLenum target
, GLint level
,
2773 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2774 GLsizei width
, GLsizei height
, GLsizei depth
,
2775 GLenum format
, GLsizei imageSize
)
2777 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2780 if (dimensions
== 1) {
2781 /* 1D compressed textures not allowed */
2782 return GL_INVALID_ENUM
;
2784 else if (dimensions
== 2) {
2785 if (target
== GL_PROXY_TEXTURE_2D
) {
2786 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2788 else if (target
== GL_TEXTURE_2D
) {
2789 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2791 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2792 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2793 return GL_INVALID_ENUM
; /*target*/
2794 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2796 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2797 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2798 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2799 return GL_INVALID_ENUM
; /*target*/
2800 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2803 return GL_INVALID_ENUM
; /*target*/
2806 else if (dimensions
== 3) {
2807 /* 3D compressed textures not allowed */
2808 return GL_INVALID_ENUM
;
2811 maxTextureSize
= 1 << (maxLevels
- 1);
2813 if (!is_compressed_format(ctx
, format
))
2814 return GL_INVALID_ENUM
;
2816 if (width
< 1 || width
> maxTextureSize
)
2817 return GL_INVALID_VALUE
;
2819 if ((height
< 1 || height
> maxTextureSize
)
2821 return GL_INVALID_VALUE
;
2823 if (level
< 0 || level
>= maxLevels
)
2824 return GL_INVALID_VALUE
;
2826 /* XXX these tests are specific to the compressed format.
2827 * this code should be generalized in some way.
2829 if ((xoffset
& 3) != 0 || (yoffset
& 3) != 0)
2830 return GL_INVALID_VALUE
;
2832 if ((width
& 3) != 0 && width
!= 2 && width
!= 1)
2833 return GL_INVALID_VALUE
;
2835 if ((height
& 3) != 0 && height
!= 2 && height
!= 1)
2836 return GL_INVALID_VALUE
;
2838 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2840 if (expectedSize
!= imageSize
)
2841 return GL_INVALID_VALUE
;
2849 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2850 GLenum internalFormat
, GLsizei width
,
2851 GLint border
, GLsizei imageSize
,
2854 GET_CURRENT_CONTEXT(ctx
);
2855 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2857 if (target
== GL_TEXTURE_1D
) {
2858 /* non-proxy target */
2859 struct gl_texture_unit
*texUnit
;
2860 struct gl_texture_object
*texObj
;
2861 struct gl_texture_image
*texImage
;
2862 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2863 internalFormat
, width
, 1, 1, border
, imageSize
);
2865 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
2869 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2870 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2871 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2873 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
2876 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2877 MESA_PBUFFER_FREE(texImage
->Data
);
2879 texImage
->Data
= NULL
;
2881 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2882 border
, internalFormat
);
2884 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2885 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2886 internalFormat
, width
, border
,
2891 texObj
->Complete
= GL_FALSE
;
2892 ctx
->NewState
|= _NEW_TEXTURE
;
2894 else if (target
== GL_PROXY_TEXTURE_1D
) {
2895 /* Proxy texture: check for errors and update proxy state */
2896 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2897 internalFormat
, width
, 1, 1, border
, imageSize
);
2899 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2900 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2901 internalFormat
, GL_NONE
, GL_NONE
,
2902 width
, 1, 1, border
);
2905 /* if error, clear all proxy texture image parameters */
2906 struct gl_texture_image
*texImage
;
2907 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2909 clear_teximage_fields(texImage
);
2912 /* store the teximage parameters */
2913 struct gl_texture_unit
*texUnit
;
2914 struct gl_texture_image
*texImage
;
2915 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2916 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2917 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2918 border
, internalFormat
);
2922 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
2929 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
2930 GLenum internalFormat
, GLsizei width
,
2931 GLsizei height
, GLint border
, GLsizei imageSize
,
2934 GET_CURRENT_CONTEXT(ctx
);
2935 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2937 if (target
== GL_TEXTURE_2D
||
2938 (ctx
->Extensions
.ARB_texture_cube_map
&&
2939 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2940 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
2941 /* non-proxy target */
2942 struct gl_texture_unit
*texUnit
;
2943 struct gl_texture_object
*texObj
;
2944 struct gl_texture_image
*texImage
;
2945 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2946 internalFormat
, width
, height
, 1, border
, imageSize
);
2948 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
2952 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2953 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2954 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2956 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
2959 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2960 MESA_PBUFFER_FREE(texImage
->Data
);
2962 texImage
->Data
= NULL
;
2964 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2965 border
, internalFormat
);
2967 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
2968 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
2969 internalFormat
, width
, height
,
2970 border
, imageSize
, data
,
2974 texObj
->Complete
= GL_FALSE
;
2975 ctx
->NewState
|= _NEW_TEXTURE
;
2977 else if (target
== GL_PROXY_TEXTURE_2D
||
2978 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2979 ctx
->Extensions
.ARB_texture_cube_map
)) {
2980 /* Proxy texture: check for errors and update proxy state */
2981 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2982 internalFormat
, width
, height
, 1, border
, imageSize
);
2984 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2985 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2986 internalFormat
, GL_NONE
, GL_NONE
,
2987 width
, height
, 1, border
);
2990 /* if error, clear all proxy texture image parameters */
2991 struct gl_texture_image
*texImage
;
2992 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2994 clear_teximage_fields(texImage
);
2997 /* store the teximage parameters */
2998 struct gl_texture_unit
*texUnit
;
2999 struct gl_texture_image
*texImage
;
3000 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3001 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3002 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3003 border
, internalFormat
);
3007 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3014 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3015 GLenum internalFormat
, GLsizei width
,
3016 GLsizei height
, GLsizei depth
, GLint border
,
3017 GLsizei imageSize
, const GLvoid
*data
)
3019 GET_CURRENT_CONTEXT(ctx
);
3020 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3022 if (target
== GL_TEXTURE_3D
) {
3023 /* non-proxy target */
3024 struct gl_texture_unit
*texUnit
;
3025 struct gl_texture_object
*texObj
;
3026 struct gl_texture_image
*texImage
;
3027 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3028 internalFormat
, width
, height
, depth
, border
, imageSize
);
3030 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3034 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3035 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3036 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3038 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3041 else if (texImage
->Data
&& !texImage
->IsClientData
) {
3042 MESA_PBUFFER_FREE(texImage
->Data
);
3044 texImage
->Data
= NULL
;
3046 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, depth
,
3047 border
, internalFormat
);
3049 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3050 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
3052 width
, height
, depth
,
3053 border
, imageSize
, data
,
3057 texObj
->Complete
= GL_FALSE
;
3058 ctx
->NewState
|= _NEW_TEXTURE
;
3060 else if (target
== GL_PROXY_TEXTURE_3D
) {
3061 /* Proxy texture: check for errors and update proxy state */
3062 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3063 internalFormat
, width
, height
, depth
, border
, imageSize
);
3065 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3066 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3067 internalFormat
, GL_NONE
, GL_NONE
,
3068 width
, height
, depth
, border
);
3071 /* if error, clear all proxy texture image parameters */
3072 struct gl_texture_image
*texImage
;
3073 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3075 clear_teximage_fields(texImage
);
3078 /* store the teximage parameters */
3079 struct gl_texture_unit
*texUnit
;
3080 struct gl_texture_image
*texImage
;
3081 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3082 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3083 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3084 depth
, border
, internalFormat
);
3088 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3095 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3096 GLsizei width
, GLenum format
,
3097 GLsizei imageSize
, const GLvoid
*data
)
3099 struct gl_texture_unit
*texUnit
;
3100 struct gl_texture_object
*texObj
;
3101 struct gl_texture_image
*texImage
;
3103 GET_CURRENT_CONTEXT(ctx
);
3104 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3106 error
= compressed_subtexture_error_check(ctx
, 1, target
, level
,
3107 xoffset
, 0, 0, width
, 1, 1, format
, imageSize
);
3109 _mesa_error(ctx
, error
, "glCompressedTexSubImage1D");
3113 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3114 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3115 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3118 if ((GLint
) format
!= texImage
->IntFormat
) {
3119 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3120 "glCompressedTexSubImage1D(format)");
3124 if ((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) {
3125 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage1D(width)");
3130 return; /* no-op, not an error */
3132 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3133 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
3135 format
, imageSize
, data
,
3138 ctx
->NewState
|= _NEW_TEXTURE
;
3143 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3144 GLint yoffset
, GLsizei width
, GLsizei height
,
3145 GLenum format
, GLsizei imageSize
,
3148 struct gl_texture_unit
*texUnit
;
3149 struct gl_texture_object
*texObj
;
3150 struct gl_texture_image
*texImage
;
3152 GET_CURRENT_CONTEXT(ctx
);
3153 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3155 error
= compressed_subtexture_error_check(ctx
, 2, target
, level
,
3156 xoffset
, yoffset
, 0, width
, height
, 1, format
, imageSize
);
3158 /* XXX proxy target? */
3159 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3163 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3164 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3165 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3168 if ((GLint
) format
!= texImage
->IntFormat
) {
3169 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3170 "glCompressedTexSubImage2D(format)");
3174 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3175 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
)) {
3176 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage2D(size)");
3180 if (width
== 0 || height
== 0)
3181 return; /* no-op, not an error */
3183 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3184 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
3185 xoffset
, yoffset
, width
, height
,
3186 format
, imageSize
, data
,
3189 ctx
->NewState
|= _NEW_TEXTURE
;
3194 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3195 GLint yoffset
, GLint zoffset
, GLsizei width
,
3196 GLsizei height
, GLsizei depth
, GLenum format
,
3197 GLsizei imageSize
, const GLvoid
*data
)
3199 struct gl_texture_unit
*texUnit
;
3200 struct gl_texture_object
*texObj
;
3201 struct gl_texture_image
*texImage
;
3203 GET_CURRENT_CONTEXT(ctx
);
3204 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3206 error
= compressed_subtexture_error_check(ctx
, 3, target
, level
,
3207 xoffset
, yoffset
, zoffset
, width
, height
, depth
, format
, imageSize
);
3209 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3213 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3214 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3215 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3218 if ((GLint
) format
!= texImage
->IntFormat
) {
3219 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3220 "glCompressedTexSubImage3D(format)");
3224 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3225 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
) ||
3226 ((depth
== 1 || depth
== 2) && (GLuint
) depth
!= texImage
->Depth
)) {
3227 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage3D(size)");
3231 if (width
== 0 || height
== 0 || depth
== 0)
3232 return; /* no-op, not an error */
3234 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3235 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
3236 xoffset
, yoffset
, zoffset
,
3237 width
, height
, depth
,
3238 format
, imageSize
, data
,
3241 ctx
->NewState
|= _NEW_TEXTURE
;
3246 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
3248 const struct gl_texture_unit
*texUnit
;
3249 const struct gl_texture_object
*texObj
;
3250 struct gl_texture_image
*texImage
;
3252 GET_CURRENT_CONTEXT(ctx
);
3253 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3255 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3256 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3258 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
3262 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3263 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
3265 if (level
< 0 || level
>= maxLevels
) {
3266 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3270 if (is_proxy_target(target
)) {
3271 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
3275 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3277 /* probably invalid mipmap level */
3278 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3282 if (!texImage
->IsCompressed
) {
3283 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
3287 /* this typically calls _mesa_get_compressed_teximage() */
3288 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
, texObj
,texImage
);