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 * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
59 _mesa_alloc_texmemory(GLsizei bytes
)
61 return _mesa_align_malloc(bytes
, 512);
66 * Free texture memory allocated with _mesa_alloc_texmemory()
69 _mesa_free_texmemory(void *m
)
78 static void PrintTexture(GLcontext
*ctx
, const struct gl_texture_image
*img
)
80 #if CHAN_TYPE == GL_FLOAT
81 _mesa_problem(NULL
, "PrintTexture doesn't support float channels");
84 const GLchan
*data
= (const GLchan
*) img
->Data
;
87 _mesa_printf("No texture data\n");
91 switch (img
->Format
) {
98 case GL_LUMINANCE_ALPHA
:
108 _mesa_problem(NULL
, "error in PrintTexture\n");
112 for (i
= 0; i
< img
->Height
; i
++) {
113 for (j
= 0; j
< img
->Width
; j
++) {
115 _mesa_printf("%02x ", data
[0]);
117 _mesa_printf("%02x%02x ", data
[0], data
[1]);
119 _mesa_printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
121 _mesa_printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
122 data
+= (img
->RowStride
- img
->Width
) * c
;
132 * Compute floor(log_base_2(n)).
133 * If n < 0 return -1.
162 * Return the simple base format for a given internal texture format.
163 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
165 * \param ctx GL context.
166 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
168 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
169 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
171 * This is the format which is used during texture application (i.e. the
172 * texture format and env mode determine the arithmetic used.
175 _mesa_base_tex_format( GLcontext
*ctx
, GLint internalFormat
)
177 switch (internalFormat
) {
192 case GL_LUMINANCE_ALPHA
:
193 case GL_LUMINANCE4_ALPHA4
:
194 case GL_LUMINANCE6_ALPHA2
:
195 case GL_LUMINANCE8_ALPHA8
:
196 case GL_LUMINANCE12_ALPHA4
:
197 case GL_LUMINANCE12_ALPHA12
:
198 case GL_LUMINANCE16_ALPHA16
:
199 return GL_LUMINANCE_ALPHA
;
230 if (ctx
->Extensions
.EXT_paletted_texture
) {
231 switch (internalFormat
) {
233 case GL_COLOR_INDEX1_EXT
:
234 case GL_COLOR_INDEX2_EXT
:
235 case GL_COLOR_INDEX4_EXT
:
236 case GL_COLOR_INDEX8_EXT
:
237 case GL_COLOR_INDEX12_EXT
:
238 case GL_COLOR_INDEX16_EXT
:
239 return GL_COLOR_INDEX
;
245 if (ctx
->Extensions
.SGIX_depth_texture
||
246 ctx
->Extensions
.ARB_depth_texture
) {
247 switch (internalFormat
) {
248 case GL_DEPTH_COMPONENT
:
249 case GL_DEPTH_COMPONENT16_SGIX
:
250 case GL_DEPTH_COMPONENT24_SGIX
:
251 case GL_DEPTH_COMPONENT32_SGIX
:
252 return GL_DEPTH_COMPONENT
;
258 if (ctx
->Extensions
.ARB_texture_compression
) {
259 switch (internalFormat
) {
260 case GL_COMPRESSED_ALPHA
:
262 case GL_COMPRESSED_LUMINANCE
:
264 case GL_COMPRESSED_LUMINANCE_ALPHA
:
265 return GL_LUMINANCE_ALPHA
;
266 case GL_COMPRESSED_INTENSITY
:
268 case GL_COMPRESSED_RGB
:
270 case GL_COMPRESSED_RGBA
:
277 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
278 switch (internalFormat
) {
279 case GL_COMPRESSED_RGB_FXT1_3DFX
:
281 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
288 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
289 switch (internalFormat
) {
290 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
292 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
293 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
294 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
301 if (ctx
->Extensions
.S3_s3tc
) {
302 switch (internalFormat
) {
314 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
315 if (internalFormat
== GL_YCBCR_MESA
)
316 return GL_YCBCR_MESA
;
319 if (ctx
->Extensions
.ARB_texture_float
) {
320 switch (internalFormat
) {
321 case GL_ALPHA16F_ARB
:
322 case GL_ALPHA32F_ARB
:
330 case GL_INTENSITY16F_ARB
:
331 case GL_INTENSITY32F_ARB
:
333 case GL_LUMINANCE16F_ARB
:
334 case GL_LUMINANCE32F_ARB
:
336 case GL_LUMINANCE_ALPHA16F_ARB
:
337 case GL_LUMINANCE_ALPHA32F_ARB
:
338 return GL_LUMINANCE_ALPHA
;
344 return -1; /* error */
349 * Test if the given image format is a color/RGBA format (i.e., not color
350 * index, depth, stencil, etc).
351 * \param format the image format value (may by an internal texture format)
352 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
355 is_color_format(GLenum format
)
373 case GL_LUMINANCE_ALPHA
:
374 case GL_LUMINANCE4_ALPHA4
:
375 case GL_LUMINANCE6_ALPHA2
:
376 case GL_LUMINANCE8_ALPHA8
:
377 case GL_LUMINANCE12_ALPHA4
:
378 case GL_LUMINANCE12_ALPHA12
:
379 case GL_LUMINANCE16_ALPHA16
:
406 /* float texture formats */
407 case GL_ALPHA16F_ARB
:
408 case GL_ALPHA32F_ARB
:
409 case GL_LUMINANCE16F_ARB
:
410 case GL_LUMINANCE32F_ARB
:
411 case GL_LUMINANCE_ALPHA16F_ARB
:
412 case GL_LUMINANCE_ALPHA32F_ARB
:
413 case GL_INTENSITY16F_ARB
:
414 case GL_INTENSITY32F_ARB
:
419 /* compressed formats */
420 case GL_COMPRESSED_ALPHA
:
421 case GL_COMPRESSED_LUMINANCE
:
422 case GL_COMPRESSED_LUMINANCE_ALPHA
:
423 case GL_COMPRESSED_INTENSITY
:
424 case GL_COMPRESSED_RGB
:
425 case GL_COMPRESSED_RGBA
:
430 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
431 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
432 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
433 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
434 case GL_COMPRESSED_RGB_FXT1_3DFX
:
435 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
437 case GL_YCBCR_MESA
: /* not considered to be RGB */
445 * Test if the given image format is a color index format.
448 is_index_format(GLenum format
)
452 case GL_COLOR_INDEX1_EXT
:
453 case GL_COLOR_INDEX2_EXT
:
454 case GL_COLOR_INDEX4_EXT
:
455 case GL_COLOR_INDEX8_EXT
:
456 case GL_COLOR_INDEX12_EXT
:
457 case GL_COLOR_INDEX16_EXT
:
466 * Test if the given image format is a depth component format.
469 is_depth_format(GLenum format
)
472 case GL_DEPTH_COMPONENT16_ARB
:
473 case GL_DEPTH_COMPONENT24_ARB
:
474 case GL_DEPTH_COMPONENT32_ARB
:
475 case GL_DEPTH_COMPONENT
:
484 * Test if the given image format is a YCbCr format.
487 is_ycbcr_format(GLenum format
)
499 * Test if it is a supported compressed format.
501 * \param internalFormat the internal format token provided by the user.
503 * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
504 * GL_FALSE otherwise.
506 * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
510 is_compressed_format(GLcontext
*ctx
, GLenum internalFormat
)
513 switch (internalFormat
) {
514 case GL_COMPRESSED_RGB_FXT1_3DFX
:
515 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
516 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
517 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
518 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
519 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
532 * Store a gl_texture_image pointer in a gl_texture_object structure
533 * according to the target and level parameters.
535 * \param tObj texture object.
536 * \param target texture target.
537 * \param level image level.
538 * \param texImage texture image.
540 * This was basically prompted by the introduction of cube maps.
543 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
544 GLenum target
, GLint level
,
545 struct gl_texture_image
*texImage
)
553 tObj
->Image
[0][level
] = texImage
;
555 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
556 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
557 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
558 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
559 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
560 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
562 GLuint face
= ((GLuint
) target
-
563 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
564 tObj
->Image
[face
][level
] = texImage
;
567 case GL_TEXTURE_RECTANGLE_NV
:
569 tObj
->Image
[0][level
] = texImage
;
572 _mesa_problem(NULL
, "bad target in _mesa_set_tex_image()");
575 /* Set the 'back' pointer */
576 texImage
->TexObject
= tObj
;
581 * Allocate a texture image structure.
583 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
586 * \return a pointer to gl_texture_image struct with all fields initialized to
589 struct gl_texture_image
*
590 _mesa_new_texture_image( GLcontext
*ctx
)
593 return CALLOC_STRUCT(gl_texture_image
);
598 * Free texture image data.
599 * This function is a fallback called via ctx->Driver.FreeTexImageData().
601 * \param teximage texture image.
603 * Free the texture image data if it's not marked as client data.
606 _mesa_free_texture_image_data(GLcontext
*ctx
,
607 struct gl_texture_image
*texImage
)
609 if (texImage
->Data
&& !texImage
->IsClientData
) {
610 /* free the old texture data */
611 _mesa_free_texmemory(texImage
->Data
);
614 texImage
->Data
= NULL
;
619 * Free texture image.
621 * \param teximage texture image.
623 * Free the texture image structure and the associated image data.
626 _mesa_delete_texture_image( GLcontext
*ctx
, struct gl_texture_image
*texImage
)
628 if (texImage
->Data
) {
629 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
631 ASSERT(texImage
->Data
== NULL
);
637 * Test if a target is a proxy target.
639 * \param target texture target.
641 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
644 is_proxy_target(GLenum target
)
646 return (target
== GL_PROXY_TEXTURE_1D
||
647 target
== GL_PROXY_TEXTURE_2D
||
648 target
== GL_PROXY_TEXTURE_3D
||
649 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
650 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
);
655 * Get the texture object that corresponds to the target of the given texture unit.
657 * \param ctx GL context.
658 * \param texUnit texture unit.
659 * \param target texture target.
661 * \return pointer to the texture object on success, or NULL on failure.
663 * \sa gl_texture_unit.
665 struct gl_texture_object
*
666 _mesa_select_tex_object(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
671 return texUnit
->Current1D
;
672 case GL_PROXY_TEXTURE_1D
:
673 return ctx
->Texture
.Proxy1D
;
675 return texUnit
->Current2D
;
676 case GL_PROXY_TEXTURE_2D
:
677 return ctx
->Texture
.Proxy2D
;
679 return texUnit
->Current3D
;
680 case GL_PROXY_TEXTURE_3D
:
681 return ctx
->Texture
.Proxy3D
;
682 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
683 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
684 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
685 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
686 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
687 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
688 case GL_TEXTURE_CUBE_MAP_ARB
:
689 return ctx
->Extensions
.ARB_texture_cube_map
690 ? texUnit
->CurrentCubeMap
: NULL
;
691 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
692 return ctx
->Extensions
.ARB_texture_cube_map
693 ? ctx
->Texture
.ProxyCubeMap
: NULL
;
694 case GL_TEXTURE_RECTANGLE_NV
:
695 return ctx
->Extensions
.NV_texture_rectangle
696 ? texUnit
->CurrentRect
: NULL
;
697 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
698 return ctx
->Extensions
.NV_texture_rectangle
699 ? ctx
->Texture
.ProxyRect
: NULL
;
701 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
708 * Get the texture image struct which corresponds to target and level
709 * of the given texture unit.
711 * \param ctx GL context.
712 * \param texUnit texture unit.
713 * \param target texture target.
714 * \param level image level.
716 * \return pointer to the texture image structure on success, or NULL on failure.
718 * \sa gl_texture_unit.
720 struct gl_texture_image
*
721 _mesa_select_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
722 GLenum target
, GLint level
)
725 ASSERT(level
< MAX_TEXTURE_LEVELS
);
728 return texUnit
->Current1D
->Image
[0][level
];
729 case GL_PROXY_TEXTURE_1D
:
730 return ctx
->Texture
.Proxy1D
->Image
[0][level
];
732 return texUnit
->Current2D
->Image
[0][level
];
733 case GL_PROXY_TEXTURE_2D
:
734 return ctx
->Texture
.Proxy2D
->Image
[0][level
];
736 return texUnit
->Current3D
->Image
[0][level
];
737 case GL_PROXY_TEXTURE_3D
:
738 return ctx
->Texture
.Proxy3D
->Image
[0][level
];
739 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
740 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
741 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
742 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
743 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
744 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
745 if (ctx
->Extensions
.ARB_texture_cube_map
) {
746 GLuint face
= ((GLuint
) target
-
747 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
748 return texUnit
->CurrentCubeMap
->Image
[face
][level
];
752 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
753 if (ctx
->Extensions
.ARB_texture_cube_map
)
754 return ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
757 case GL_TEXTURE_RECTANGLE_NV
:
758 if (ctx
->Extensions
.NV_texture_rectangle
) {
760 return texUnit
->CurrentRect
->Image
[0][level
];
765 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
766 if (ctx
->Extensions
.NV_texture_rectangle
) {
768 return ctx
->Texture
.ProxyRect
->Image
[0][level
];
774 _mesa_problem(ctx
, "bad target in _mesa_select_tex_image()");
781 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
782 * it and install it. Only return NULL if passed a bad parameter or run
785 struct gl_texture_image
*
786 _mesa_get_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
787 GLenum target
, GLint level
)
789 struct gl_texture_image
*texImage
;
790 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
792 struct gl_texture_object
*texObj
;
793 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
795 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
798 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
800 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
807 * Return pointer to the specified proxy texture image.
808 * Note that proxy textures are per-context, not per-texture unit.
809 * \return pointer to texture image or NULL if invalid target, invalid
810 * level, or out of memory.
812 struct gl_texture_image
*
813 _mesa_get_proxy_tex_image(GLcontext
*ctx
, GLenum target
, GLint level
)
815 struct gl_texture_image
*texImage
;
821 case GL_PROXY_TEXTURE_1D
:
822 if (level
>= ctx
->Const
.MaxTextureLevels
)
824 texImage
= ctx
->Texture
.Proxy1D
->Image
[0][level
];
826 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
828 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
831 ctx
->Texture
.Proxy1D
->Image
[0][level
] = texImage
;
832 /* Set the 'back' pointer */
833 texImage
->TexObject
= ctx
->Texture
.Proxy1D
;
836 case GL_PROXY_TEXTURE_2D
:
837 if (level
>= ctx
->Const
.MaxTextureLevels
)
839 texImage
= ctx
->Texture
.Proxy2D
->Image
[0][level
];
841 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
843 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
846 ctx
->Texture
.Proxy2D
->Image
[0][level
] = texImage
;
847 /* Set the 'back' pointer */
848 texImage
->TexObject
= ctx
->Texture
.Proxy2D
;
851 case GL_PROXY_TEXTURE_3D
:
852 if (level
>= ctx
->Const
.Max3DTextureLevels
)
854 texImage
= ctx
->Texture
.Proxy3D
->Image
[0][level
];
856 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
858 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
861 ctx
->Texture
.Proxy3D
->Image
[0][level
] = texImage
;
862 /* Set the 'back' pointer */
863 texImage
->TexObject
= ctx
->Texture
.Proxy3D
;
866 case GL_PROXY_TEXTURE_CUBE_MAP
:
867 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
869 texImage
= ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
871 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
873 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
876 ctx
->Texture
.ProxyCubeMap
->Image
[0][level
] = texImage
;
877 /* Set the 'back' pointer */
878 texImage
->TexObject
= ctx
->Texture
.ProxyCubeMap
;
881 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
884 texImage
= ctx
->Texture
.ProxyRect
->Image
[0][level
];
886 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
888 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
891 ctx
->Texture
.ProxyRect
->Image
[0][level
] = texImage
;
892 /* Set the 'back' pointer */
893 texImage
->TexObject
= ctx
->Texture
.ProxyRect
;
903 * Get the maximum number of allowed mipmap levels.
905 * \param ctx GL context.
906 * \param target texture target.
908 * \return the maximum number of allowed mipmap levels for the given
909 * texture target, or zero if passed a bad target.
914 _mesa_max_texture_levels(GLcontext
*ctx
, GLenum target
)
918 case GL_PROXY_TEXTURE_1D
:
920 case GL_PROXY_TEXTURE_2D
:
921 return ctx
->Const
.MaxTextureLevels
;
923 case GL_PROXY_TEXTURE_3D
:
924 return ctx
->Const
.Max3DTextureLevels
;
925 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
926 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
927 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
928 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
929 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
930 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
931 case GL_TEXTURE_CUBE_MAP_ARB
:
932 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
933 return ctx
->Const
.MaxCubeTextureLevels
;
934 case GL_TEXTURE_RECTANGLE_NV
:
935 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
938 return 0; /* bad target */
944 #if 000 /* not used anymore */
946 * glTexImage[123]D can accept a NULL image pointer. In this case we
947 * create a texture image with unspecified image contents per the OpenGL
951 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
953 const GLint components
= _mesa_components_in_format(format
);
954 const GLint numPixels
= width
* height
* depth
;
955 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
959 * Let's see if anyone finds this. If glTexImage2D() is called with
960 * a NULL image pointer then load the texture image with something
961 * interesting instead of leaving it indeterminate.
964 static const char message
[8][32] = {
968 " X X XXXX XXX XXXXX ",
971 " X X XXXXX XXX X X ",
975 GLubyte
*imgPtr
= data
;
977 for (h
= 0; h
< depth
; h
++) {
978 for (i
= 0; i
< height
; i
++) {
979 GLint srcRow
= 7 - (i
% 8);
980 for (j
= 0; j
< width
; j
++) {
981 GLint srcCol
= j
% 32;
982 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
983 for (k
= 0; k
< components
; k
++) {
999 * Reset the fields of a gl_texture_image struct to zero.
1001 * \param img texture image structure.
1003 * This is called when a proxy texture test fails, we set all the
1004 * image members (except DriverData) to zero.
1005 * It's also used in glTexImage[123]D as a safeguard to be sure all
1006 * required fields get initialized properly by the Driver.TexImage[123]D
1010 clear_teximage_fields(struct gl_texture_image
*img
)
1024 img
->HeightLog2
= 0;
1027 img
->TexFormat
= &_mesa_null_texformat
;
1028 img
->FetchTexelc
= NULL
;
1029 img
->FetchTexelf
= NULL
;
1030 img
->IsCompressed
= 0;
1031 img
->CompressedSize
= 0;
1036 * Initialize basic fields of the gl_texture_image struct.
1038 * \param ctx GL context.
1039 * \param target texture target.
1040 * \param img texture image structure to be initialized.
1041 * \param width image width.
1042 * \param height image height.
1043 * \param depth image depth.
1044 * \param border image border.
1045 * \param internalFormat internal format.
1047 * Fills in the fields of \p img with the given information.
1048 * Note: width, height and depth include the border.
1051 _mesa_init_teximage_fields(GLcontext
*ctx
, GLenum target
,
1052 struct gl_texture_image
*img
,
1053 GLsizei width
, GLsizei height
, GLsizei depth
,
1054 GLint border
, GLenum internalFormat
)
1057 img
->Format
= _mesa_base_tex_format( ctx
, internalFormat
);
1058 ASSERT(img
->Format
> 0);
1059 img
->IntFormat
= internalFormat
;
1060 img
->Border
= border
;
1062 img
->Height
= height
;
1064 img
->RowStride
= width
;
1065 img
->WidthLog2
= logbase2(width
- 2 * border
);
1066 if (height
== 1) /* 1-D texture */
1067 img
->HeightLog2
= 0;
1069 img
->HeightLog2
= logbase2(height
- 2 * border
);
1070 if (depth
== 1) /* 2-D texture */
1073 img
->DepthLog2
= logbase2(depth
- 2 * border
);
1074 img
->Width2
= width
- 2 * border
; /*1 << img->WidthLog2;*/
1075 img
->Height2
= height
- 2 * border
; /*1 << img->HeightLog2;*/
1076 img
->Depth2
= depth
- 2 * border
; /*1 << img->DepthLog2;*/
1077 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1078 img
->IsCompressed
= is_compressed_format(ctx
, internalFormat
);
1079 if (img
->IsCompressed
)
1080 img
->CompressedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
,
1081 height
, depth
, internalFormat
);
1083 img
->CompressedSize
= 0;
1085 if ((width
== 1 || _mesa_bitcount(width
- 2 * border
) == 1) &&
1086 (height
== 1 || _mesa_bitcount(height
- 2 * border
) == 1) &&
1087 (depth
== 1 || _mesa_bitcount(depth
- 2 * border
) == 1))
1088 img
->_IsPowerOfTwo
= GL_TRUE
;
1090 img
->_IsPowerOfTwo
= GL_FALSE
;
1092 /* Compute Width/Height/DepthScale for mipmap lod computation */
1093 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1094 /* scale = 1.0 since texture coords directly map to texels */
1095 img
->WidthScale
= 1.0;
1096 img
->HeightScale
= 1.0;
1097 img
->DepthScale
= 1.0;
1100 img
->WidthScale
= (GLfloat
) img
->Width
;
1101 img
->HeightScale
= (GLfloat
) img
->Height
;
1102 img
->DepthScale
= (GLfloat
) img
->Depth
;
1108 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1109 * level, width, height and depth against the ctx->Const limits for textures.
1111 * A hardware driver might override this function if, for example, the
1112 * max 3D texture size is 512x512x64 (i.e. not a cube).
1114 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1115 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1116 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1117 * \param level as passed to glTexImage
1118 * \param internalFormat as passed to glTexImage
1119 * \param format as passed to glTexImage
1120 * \param type as passed to glTexImage
1121 * \param width as passed to glTexImage
1122 * \param height as passed to glTexImage
1123 * \param depth as passed to glTexImage
1124 * \param border as passed to glTexImage
1125 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1128 _mesa_test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1129 GLint internalFormat
, GLenum format
, GLenum type
,
1130 GLint width
, GLint height
, GLint depth
, GLint border
)
1134 (void) internalFormat
;
1139 case GL_PROXY_TEXTURE_1D
:
1140 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1141 if (width
< 2 * border
|| width
> 2 + maxSize
||
1142 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1143 _mesa_bitcount(width
- 2 * border
) != 1) ||
1144 level
>= ctx
->Const
.MaxTextureLevels
) {
1145 /* bad width or level */
1149 case GL_PROXY_TEXTURE_2D
:
1150 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1151 if (width
< 2 * border
|| width
> 2 + maxSize
||
1152 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1153 _mesa_bitcount(width
- 2 * border
) != 1) ||
1154 height
< 2 * border
|| height
> 2 + maxSize
||
1155 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1156 _mesa_bitcount(height
- 2 * border
) != 1) ||
1157 level
>= ctx
->Const
.MaxTextureLevels
) {
1158 /* bad width or height or level */
1162 case GL_PROXY_TEXTURE_3D
:
1163 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1164 if (width
< 2 * border
|| width
> 2 + maxSize
||
1165 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1166 _mesa_bitcount(width
- 2 * border
) != 1) ||
1167 height
< 2 * border
|| height
> 2 + maxSize
||
1168 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1169 _mesa_bitcount(height
- 2 * border
) != 1) ||
1170 depth
< 2 * border
|| depth
> 2 + maxSize
||
1171 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1172 _mesa_bitcount(depth
- 2 * border
) != 1) ||
1173 level
>= ctx
->Const
.Max3DTextureLevels
) {
1174 /* bad width or height or depth or level */
1178 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1179 if (width
< 1 || width
> ctx
->Const
.MaxTextureRectSize
||
1180 height
< 1 || height
> ctx
->Const
.MaxTextureRectSize
||
1182 /* bad width or height or level */
1186 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1187 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1188 if (width
< 2 * border
|| width
> 2 + maxSize
||
1189 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1190 _mesa_bitcount(width
- 2 * border
) != 1) ||
1191 height
< 2 * border
|| height
> 2 + maxSize
||
1192 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1193 _mesa_bitcount(height
- 2 * border
) != 1) ||
1194 level
>= ctx
->Const
.MaxCubeTextureLevels
) {
1195 /* bad width or height */
1200 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1207 * Test the glTexImage[123]D() parameters for errors.
1209 * \param ctx GL context.
1210 * \param target texture target given by the user.
1211 * \param level image level given by the user.
1212 * \param internalFormat internal format given by the user.
1213 * \param format pixel data format given by the user.
1214 * \param type pixel data type given by the user.
1215 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1216 * \param width image width given by the user.
1217 * \param height image height given by the user.
1218 * \param depth image depth given by the user.
1219 * \param border image border given by the user.
1221 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1223 * Verifies each of the parameters against the constants specified in
1224 * __GLcontextRec::Const and the supported extensions, and according to the
1225 * OpenGL specification.
1228 texture_error_check( GLcontext
*ctx
, GLenum target
,
1229 GLint level
, GLint internalFormat
,
1230 GLenum format
, GLenum type
,
1232 GLint width
, GLint height
,
1233 GLint depth
, GLint border
)
1235 const GLboolean isProxy
= is_proxy_target(target
);
1237 GLboolean colorFormat
, indexFormat
;
1239 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1240 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1242 _mesa_error(ctx
, GL_INVALID_VALUE
,
1243 "glTexImage%dD(level=%d)", dimensions
, level
);
1249 if (border
< 0 || border
> 1 ||
1250 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1251 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1253 _mesa_error(ctx
, GL_INVALID_VALUE
,
1254 "glTexImage%dD(border=%d)", dimensions
, border
);
1259 if (width
< 0 || height
< 0 || depth
< 0) {
1261 _mesa_error(ctx
, GL_INVALID_VALUE
,
1262 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1267 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1268 * level, width, height and depth.
1270 if (dimensions
== 1) {
1271 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1272 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1273 level
, internalFormat
,
1275 width
, 1, 1, border
);
1278 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1282 else if (dimensions
== 2) {
1283 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1284 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1285 level
, internalFormat
,
1287 width
, height
, 1, border
);
1289 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1290 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1291 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1292 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1293 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1296 sizeOK
= (width
== height
) &&
1297 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1298 level
, internalFormat
, format
, type
,
1299 width
, height
, 1, border
);
1301 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1302 target
== GL_TEXTURE_RECTANGLE_NV
) {
1303 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1304 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1307 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1308 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1309 level
, internalFormat
,
1311 width
, height
, 1, border
);
1314 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1318 else if (dimensions
== 3) {
1319 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1320 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_3D
,
1321 level
, internalFormat
,
1323 width
, height
, depth
, border
);
1326 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1331 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1337 _mesa_error(ctx
, GL_INVALID_VALUE
,
1338 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1339 dimensions
, level
, width
, height
, depth
);
1344 /* Check internalFormat */
1345 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1347 _mesa_error(ctx
, GL_INVALID_VALUE
,
1348 "glTexImage%dD(internalFormat=0x%x)",
1349 dimensions
, internalFormat
);
1354 /* Check incoming image format and type */
1355 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1356 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1357 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1361 "glTexImage%dD(format or type)", dimensions
);
1366 /* make sure internal format and format basically agree */
1367 colorFormat
= is_color_format(format
);
1368 indexFormat
= is_index_format(format
);
1369 if ((is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1370 (is_index_format(internalFormat
) && !indexFormat
) ||
1371 (is_depth_format(internalFormat
) != is_depth_format(format
)) ||
1372 (is_ycbcr_format(internalFormat
) != is_ycbcr_format(format
))) {
1374 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1375 "glTexImage(internalFormat/format)");
1379 /* additional checks for ycbcr textures */
1380 if (internalFormat
== GL_YCBCR_MESA
) {
1381 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1382 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1383 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1385 _mesa_sprintf(message
,
1386 "glTexImage%d(format/type YCBCR mismatch", dimensions
);
1387 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
1388 return GL_TRUE
; /* error */
1390 if (target
!= GL_TEXTURE_2D
&&
1391 target
!= GL_PROXY_TEXTURE_2D
&&
1392 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1393 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1395 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1401 _mesa_sprintf(message
,
1402 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1403 dimensions
, border
);
1404 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1410 /* additional checks for depth textures */
1411 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1412 /* Only 1D and 2D textures supported */
1413 if (target
!= GL_TEXTURE_1D
&&
1414 target
!= GL_PROXY_TEXTURE_1D
&&
1415 target
!= GL_TEXTURE_2D
&&
1416 target
!= GL_PROXY_TEXTURE_2D
) {
1418 _mesa_error(ctx
, GL_INVALID_ENUM
,
1419 "glTexImage(target/internalFormat)");
1424 /* additional checks for compressed textures */
1425 if (is_compressed_format(ctx
, internalFormat
)) {
1426 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1429 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1430 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1431 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1432 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1437 _mesa_error(ctx
, GL_INVALID_ENUM
,
1438 "glTexImage%d(target)", dimensions
);
1444 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1445 "glTexImage%D(border!=0)", dimensions
);
1451 /* if we get here, the parameters are OK */
1457 * Test glTexSubImage[123]D() parameters for errors.
1459 * \param ctx GL context.
1460 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1461 * \param target texture target given by the user.
1462 * \param level image level given by the user.
1463 * \param xoffset sub-image x offset given by the user.
1464 * \param yoffset sub-image y offset given by the user.
1465 * \param zoffset sub-image z offset given by the user.
1466 * \param format pixel data format given by the user.
1467 * \param type pixel data type given by the user.
1468 * \param width image width given by the user.
1469 * \param height image height given by the user.
1470 * \param depth image depth given by the user.
1472 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1474 * Verifies each of the parameters against the constants specified in
1475 * __GLcontextRec::Const and the supported extensions, and according to the
1476 * OpenGL specification.
1479 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1480 GLenum target
, GLint level
,
1481 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1482 GLint width
, GLint height
, GLint depth
,
1483 GLenum format
, GLenum type
)
1485 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1486 struct gl_texture_image
*destTex
;
1489 if (dimensions
== 1) {
1490 if (target
!= GL_TEXTURE_1D
) {
1491 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1495 else if (dimensions
== 2) {
1496 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1497 target
<=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1498 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1499 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1503 else if (ctx
->Extensions
.NV_texture_rectangle
&&
1504 target
== GL_TEXTURE_RECTANGLE_NV
) {
1505 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1506 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1510 else if (target
!= GL_TEXTURE_2D
) {
1511 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1515 else if (dimensions
== 3) {
1516 if (target
!= GL_TEXTURE_3D
) {
1517 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1522 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1526 /* Basic level check */
1527 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1528 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1533 _mesa_error(ctx
, GL_INVALID_VALUE
,
1534 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1537 if (height
< 0 && dimensions
> 1) {
1538 _mesa_error(ctx
, GL_INVALID_VALUE
,
1539 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1542 if (depth
< 0 && dimensions
> 2) {
1543 _mesa_error(ctx
, GL_INVALID_VALUE
,
1544 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1548 destTex
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1551 /* undefined image level */
1552 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1556 if (xoffset
< -((GLint
)destTex
->Border
)) {
1557 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1561 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1562 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1566 if (dimensions
> 1) {
1567 if (yoffset
< -((GLint
)destTex
->Border
)) {
1568 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1572 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1573 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1578 if (dimensions
> 2) {
1579 if (zoffset
< -((GLint
)destTex
->Border
)) {
1580 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1583 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1584 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1589 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1590 _mesa_error(ctx
, GL_INVALID_ENUM
,
1591 "glTexSubImage%dD(format or type)", dimensions
);
1595 if (destTex
->IsCompressed
) {
1596 const struct gl_texture_unit
*texUnit
;
1597 const struct gl_texture_image
*texImage
;
1598 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1599 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1601 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1604 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1605 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1606 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1607 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1611 _mesa_error(ctx
, GL_INVALID_ENUM
,
1612 "glTexSubImage%D(target)", dimensions
);
1615 /* offset must be multiple of 4 */
1616 if ((xoffset
& 3) || (yoffset
& 3)) {
1617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1618 "glTexSubImage%D(xoffset or yoffset)", dimensions
);
1621 /* size must be multiple of 4 or equal to whole texture size */
1622 if ((width
& 3) && (GLuint
) width
!= texImage
->Width
) {
1623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1624 "glTexSubImage%D(width)", dimensions
);
1627 if ((height
& 3) && (GLuint
) height
!= texImage
->Height
) {
1628 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1629 "glTexSubImage%D(width)", dimensions
);
1639 * Test glCopyTexImage[12]D() parameters for errors.
1641 * \param ctx GL context.
1642 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1643 * \param target texture target given by the user.
1644 * \param level image level given by the user.
1645 * \param internalFormat internal format given by the user.
1646 * \param width image width given by the user.
1647 * \param height image height given by the user.
1648 * \param depth image depth given by the user.
1649 * \param border texture border.
1651 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1653 * Verifies each of the parameters against the constants specified in
1654 * __GLcontextRec::Const and the supported extensions, and according to the
1655 * OpenGL specification.
1658 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1659 GLenum target
, GLint level
, GLint internalFormat
,
1660 GLint width
, GLint height
, GLint border
)
1662 GLenum format
, type
;
1665 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1666 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1667 _mesa_error(ctx
, GL_INVALID_VALUE
,
1668 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1673 if (border
< 0 || border
> 1 ||
1674 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1675 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1679 /* The format and type aren't really significant here, but we need to pass
1680 * something to TestProxyTexImage().
1682 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1685 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1686 * level, width, height and depth.
1688 if (dimensions
== 1) {
1689 if (target
== GL_TEXTURE_1D
) {
1690 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1691 level
, internalFormat
,
1693 width
, 1, 1, border
);
1696 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1700 else if (dimensions
== 2) {
1701 if (target
== GL_TEXTURE_2D
) {
1702 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1703 level
, internalFormat
,
1705 width
, height
, 1, border
);
1707 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1708 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1709 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1710 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1713 sizeOK
= (width
== height
) &&
1714 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1715 level
, internalFormat
, format
, type
,
1716 width
, height
, 1, border
);
1718 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1719 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1720 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1723 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1724 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1725 level
, internalFormat
,
1727 width
, height
, 1, border
);
1730 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1735 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1740 if (dimensions
== 1) {
1741 _mesa_error(ctx
, GL_INVALID_VALUE
,
1742 "glCopyTexImage1D(width=%d)", width
);
1745 ASSERT(dimensions
== 2);
1746 _mesa_error(ctx
, GL_INVALID_VALUE
,
1747 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1752 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1753 _mesa_error(ctx
, GL_INVALID_VALUE
,
1754 "glCopyTexImage%dD(internalFormat)", dimensions
);
1758 if (is_compressed_format(ctx
, internalFormat
)) {
1759 if (target
!= GL_TEXTURE_2D
) {
1760 _mesa_error(ctx
, GL_INVALID_ENUM
,
1761 "glCopyTexImage%d(target)", dimensions
);
1765 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1766 "glCopyTexImage%D(border!=0)", dimensions
);
1771 /* if we get here, the parameters are OK */
1777 * Test glCopyTexImage[12]D() parameters for errors.
1779 * \param ctx GL context.
1780 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1781 * \param target texture target given by the user.
1782 * \param level image level given by the user.
1783 * \param xoffset sub-image x offset given by the user.
1784 * \param yoffset sub-image y offset given by the user.
1785 * \param zoffset sub-image z offset given by the user.
1786 * \param width image width given by the user.
1787 * \param height image height given by the user.
1789 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1791 * Verifies each of the parameters against the constants specified in
1792 * __GLcontextRec::Const and the supported extensions, and according to the
1793 * OpenGL specification.
1796 copytexsubimage_error_check( GLcontext
*ctx
, GLuint dimensions
,
1797 GLenum target
, GLint level
,
1798 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1799 GLsizei width
, GLsizei height
)
1801 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1802 struct gl_texture_image
*teximage
;
1805 if (dimensions
== 1) {
1806 if (target
!= GL_TEXTURE_1D
) {
1807 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1811 else if (dimensions
== 2) {
1812 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1813 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1814 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1815 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1819 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1820 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1821 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1825 else if (target
!= GL_TEXTURE_2D
) {
1826 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1830 else if (dimensions
== 3) {
1831 if (target
!= GL_TEXTURE_3D
) {
1832 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1838 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1839 _mesa_error(ctx
, GL_INVALID_VALUE
,
1840 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1846 _mesa_error(ctx
, GL_INVALID_VALUE
,
1847 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1850 if (dimensions
> 1 && height
< 0) {
1851 _mesa_error(ctx
, GL_INVALID_VALUE
,
1852 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1856 teximage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1858 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1859 "glCopyTexSubImage%dD(undefined texture level: %d)",
1864 if (xoffset
< -((GLint
)teximage
->Border
)) {
1865 _mesa_error(ctx
, GL_INVALID_VALUE
,
1866 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1869 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1870 _mesa_error(ctx
, GL_INVALID_VALUE
,
1871 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1874 if (dimensions
> 1) {
1875 if (yoffset
< -((GLint
)teximage
->Border
)) {
1876 _mesa_error(ctx
, GL_INVALID_VALUE
,
1877 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1880 /* NOTE: we're adding the border here, not subtracting! */
1881 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1882 _mesa_error(ctx
, GL_INVALID_VALUE
,
1883 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1888 if (dimensions
> 2) {
1889 if (zoffset
< -((GLint
)teximage
->Border
)) {
1890 _mesa_error(ctx
, GL_INVALID_VALUE
,
1891 "glCopyTexSubImage%dD(zoffset)", dimensions
);
1894 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1895 _mesa_error(ctx
, GL_INVALID_VALUE
,
1896 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1901 if (teximage
->IsCompressed
) {
1902 if (target
!= GL_TEXTURE_2D
) {
1903 _mesa_error(ctx
, GL_INVALID_ENUM
,
1904 "glCopyTexSubImage%d(target)", dimensions
);
1907 /* offset must be multiple of 4 */
1908 if ((xoffset
& 3) || (yoffset
& 3)) {
1909 _mesa_error(ctx
, GL_INVALID_VALUE
,
1910 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions
);
1913 /* size must be multiple of 4 */
1914 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
1915 _mesa_error(ctx
, GL_INVALID_VALUE
,
1916 "glCopyTexSubImage%D(width)", dimensions
);
1919 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
1920 _mesa_error(ctx
, GL_INVALID_VALUE
,
1921 "glCopyTexSubImage%D(height)", dimensions
);
1926 if (teximage
->IntFormat
== GL_YCBCR_MESA
) {
1927 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
1931 /* if we get here, the parameters are OK */
1937 * Get texture image. Called by glGetTexImage.
1939 * \param target texture target.
1940 * \param level image level.
1941 * \param format pixel data format for returned image.
1942 * \param type pixel data type for returned image.
1943 * \param pixels returned pixel data.
1946 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1947 GLenum type
, GLvoid
*pixels
)
1949 const struct gl_texture_unit
*texUnit
;
1950 const struct gl_texture_object
*texObj
;
1951 const struct gl_texture_image
*texImage
;
1952 GLint maxLevels
= 0;
1953 GET_CURRENT_CONTEXT(ctx
);
1954 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1956 texUnit
= &(ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
]);
1957 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1958 if (!texObj
|| is_proxy_target(target
)) {
1959 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
1963 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1964 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
1966 if (level
< 0 || level
>= maxLevels
) {
1967 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
1971 if (_mesa_sizeof_packed_type(type
) <= 0) {
1972 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
1976 if (_mesa_components_in_format(format
) <= 0 ||
1977 format
== GL_STENCIL_INDEX
) {
1978 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
1982 if (!ctx
->Extensions
.EXT_paletted_texture
&& is_index_format(format
)) {
1983 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1986 if (!ctx
->Extensions
.SGIX_depth_texture
&&
1987 !ctx
->Extensions
.ARB_depth_texture
&& is_depth_format(format
)) {
1988 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1991 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& is_ycbcr_format(format
)) {
1992 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1998 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2000 /* invalid mipmap level, not an error */
2004 /* Make sure the requested image format is compatible with the
2005 * texture's format. We let the colorformat-indexformat go through,
2006 * because the texelfetcher will dequantize to full rgba.
2008 if (is_color_format(format
)
2009 && !is_color_format(texImage
->TexFormat
->BaseFormat
)
2010 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
2011 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
2014 else if (is_index_format(format
)
2015 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
2016 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
2019 else if (is_depth_format(format
)
2020 && !is_depth_format(texImage
->TexFormat
->BaseFormat
)) {
2021 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
2024 else if (is_ycbcr_format(format
)
2025 && !is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
2026 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
2030 /* typically, this will call _mesa_get_teximage() */
2031 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
2038 * Called from the API. Note that width includes the border.
2041 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2042 GLsizei width
, GLint border
, GLenum format
,
2043 GLenum type
, const GLvoid
*pixels
)
2045 GLsizei postConvWidth
= width
;
2046 GET_CURRENT_CONTEXT(ctx
);
2047 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2049 if (is_color_format(internalFormat
)) {
2050 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2053 if (target
== GL_TEXTURE_1D
) {
2054 /* non-proxy target */
2055 struct gl_texture_unit
*texUnit
;
2056 struct gl_texture_object
*texObj
;
2057 struct gl_texture_image
*texImage
;
2059 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2060 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2061 return; /* error was recorded */
2064 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2065 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2066 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2069 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2072 else if (texImage
->Data
) {
2073 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2075 ASSERT(texImage
->Data
== NULL
);
2076 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2077 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2078 postConvWidth
, 1, 1,
2079 border
, internalFormat
);
2081 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2082 _mesa_update_state(ctx
);
2084 ASSERT(ctx
->Driver
.TexImage1D
);
2086 /* Give the texture to the driver! <pixels> may be null! */
2087 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
2088 width
, border
, format
, type
, pixels
,
2089 &ctx
->Unpack
, texObj
, texImage
);
2091 ASSERT(texImage
->TexFormat
);
2093 /* If driver didn't explicitly set this, use the defaults */
2094 if (!texImage
->FetchTexelc
)
2095 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2096 if (!texImage
->FetchTexelf
)
2097 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2098 ASSERT(texImage
->FetchTexelc
);
2099 ASSERT(texImage
->FetchTexelf
);
2102 texObj
->Complete
= GL_FALSE
;
2103 ctx
->NewState
|= _NEW_TEXTURE
;
2105 else if (target
== GL_PROXY_TEXTURE_1D
) {
2106 /* Proxy texture: check for errors and update proxy state */
2107 struct gl_texture_image
*texImage
;
2108 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2109 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2110 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2111 /* when error, clear all proxy texture image parameters */
2113 clear_teximage_fields(texImage
);
2116 /* no error, set the tex image parameters */
2118 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2119 postConvWidth
, 1, 1,
2120 border
, internalFormat
);
2121 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2122 internalFormat
, format
, type
);
2126 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2133 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2134 GLsizei width
, GLsizei height
, GLint border
,
2135 GLenum format
, GLenum type
,
2136 const GLvoid
*pixels
)
2138 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2139 GET_CURRENT_CONTEXT(ctx
);
2140 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2142 if (is_color_format(internalFormat
)) {
2143 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2147 if (target
== GL_TEXTURE_2D
||
2148 (ctx
->Extensions
.ARB_texture_cube_map
&&
2149 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2150 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2151 (ctx
->Extensions
.NV_texture_rectangle
&&
2152 target
== GL_TEXTURE_RECTANGLE_NV
)) {
2153 /* non-proxy target */
2154 struct gl_texture_unit
*texUnit
;
2155 struct gl_texture_object
*texObj
;
2156 struct gl_texture_image
*texImage
;
2158 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2159 format
, type
, 2, postConvWidth
, postConvHeight
,
2161 return; /* error was recorded */
2164 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2165 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2166 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2168 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2171 else if (texImage
->Data
) {
2172 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2174 ASSERT(texImage
->Data
== NULL
);
2175 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2176 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2177 postConvWidth
, postConvHeight
, 1,
2178 border
, internalFormat
);
2180 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2181 _mesa_update_state(ctx
);
2183 ASSERT(ctx
->Driver
.TexImage2D
);
2185 /* Give the texture to the driver! <pixels> may be null! */
2186 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
2187 width
, height
, border
, format
, type
, pixels
,
2188 &ctx
->Unpack
, texObj
, texImage
);
2190 ASSERT(texImage
->TexFormat
);
2192 /* If driver didn't explicitly set these, use the defaults */
2193 if (!texImage
->FetchTexelc
)
2194 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2195 if (!texImage
->FetchTexelf
)
2196 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2197 ASSERT(texImage
->FetchTexelc
);
2198 ASSERT(texImage
->FetchTexelf
);
2201 texObj
->Complete
= GL_FALSE
;
2202 ctx
->NewState
|= _NEW_TEXTURE
;
2204 else if (target
== GL_PROXY_TEXTURE_2D
||
2205 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2206 ctx
->Extensions
.ARB_texture_cube_map
) ||
2207 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2208 ctx
->Extensions
.NV_texture_rectangle
)) {
2209 /* Proxy texture: check for errors and update proxy state */
2210 struct gl_texture_image
*texImage
;
2211 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2212 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2213 format
, type
, 2, postConvWidth
, postConvHeight
,
2215 /* when error, clear all proxy texture image parameters */
2217 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[0][level
]);
2220 /* no error, set the tex image parameters */
2221 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2222 postConvWidth
, postConvHeight
, 1,
2223 border
, internalFormat
);
2224 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2225 internalFormat
, format
, type
);
2229 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2236 * Called by the API or display list executor.
2237 * Note that width and height include the border.
2240 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2241 GLsizei width
, GLsizei height
, GLsizei depth
,
2242 GLint border
, GLenum format
, GLenum type
,
2243 const GLvoid
*pixels
)
2245 GET_CURRENT_CONTEXT(ctx
);
2246 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2248 if (target
== GL_TEXTURE_3D
) {
2249 struct gl_texture_unit
*texUnit
;
2250 struct gl_texture_object
*texObj
;
2251 struct gl_texture_image
*texImage
;
2252 /* non-proxy target */
2254 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2255 format
, type
, 3, width
, height
, depth
, border
)) {
2256 return; /* error was recorded */
2259 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2260 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2261 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2263 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2266 else if (texImage
->Data
) {
2267 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2269 ASSERT(texImage
->Data
== NULL
);
2270 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2271 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2272 width
, height
, depth
,
2273 border
, internalFormat
);
2275 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2276 _mesa_update_state(ctx
);
2278 ASSERT(ctx
->Driver
.TexImage3D
);
2280 /* Give the texture to the driver! <pixels> may be null! */
2281 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, internalFormat
,
2282 width
, height
, depth
, border
, format
, type
,
2283 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2285 ASSERT(texImage
->TexFormat
);
2287 /* If driver didn't explicitly set these, use the defaults */
2288 if (!texImage
->FetchTexelc
)
2289 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2290 if (!texImage
->FetchTexelf
)
2291 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2292 ASSERT(texImage
->FetchTexelc
);
2293 ASSERT(texImage
->FetchTexelf
);
2296 texObj
->Complete
= GL_FALSE
;
2297 ctx
->NewState
|= _NEW_TEXTURE
;
2299 else if (target
== GL_PROXY_TEXTURE_3D
) {
2300 /* Proxy texture: check for errors and update proxy state */
2301 struct gl_texture_image
*texImage
;
2302 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2303 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2304 format
, type
, 3, width
, height
, depth
, border
)) {
2305 /* when error, clear all proxy texture image parameters */
2307 clear_teximage_fields(texImage
);
2310 /* no error, set the tex image parameters */
2311 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2312 border
, internalFormat
);
2313 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2314 internalFormat
, format
, type
);
2318 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2325 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2326 GLsizei width
, GLsizei height
, GLsizei depth
,
2327 GLint border
, GLenum format
, GLenum type
,
2328 const GLvoid
*pixels
)
2330 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2331 depth
, border
, format
, type
, pixels
);
2337 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2338 GLint xoffset
, GLsizei width
,
2339 GLenum format
, GLenum type
,
2340 const GLvoid
*pixels
)
2342 GLsizei postConvWidth
= width
;
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
, 1, &postConvWidth
, NULL
);
2357 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2358 postConvWidth
, 1, 1, format
, type
)) {
2359 return; /* error was detected */
2362 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2363 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2364 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2368 return; /* no-op, not an error */
2370 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2371 xoffset
+= texImage
->Border
;
2373 ASSERT(ctx
->Driver
.TexSubImage1D
);
2374 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
2375 format
, type
, pixels
, &ctx
->Unpack
,
2377 ctx
->NewState
|= _NEW_TEXTURE
;
2382 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2383 GLint xoffset
, GLint yoffset
,
2384 GLsizei width
, GLsizei height
,
2385 GLenum format
, GLenum type
,
2386 const GLvoid
*pixels
)
2388 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2389 struct gl_texture_unit
*texUnit
;
2390 struct gl_texture_object
*texObj
;
2391 struct gl_texture_image
*texImage
;
2392 GET_CURRENT_CONTEXT(ctx
);
2393 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2395 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2396 _mesa_update_state(ctx
);
2398 /* XXX should test internal format */
2399 if (is_color_format(format
)) {
2400 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2404 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2405 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2406 return; /* error was detected */
2409 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2410 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2411 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2414 if (width
== 0 || height
== 0)
2415 return; /* no-op, not an error */
2417 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2418 xoffset
+= texImage
->Border
;
2419 yoffset
+= texImage
->Border
;
2421 ASSERT(ctx
->Driver
.TexSubImage2D
);
2422 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
2423 width
, height
, format
, type
, pixels
,
2424 &ctx
->Unpack
, texObj
, texImage
);
2425 ctx
->NewState
|= _NEW_TEXTURE
;
2431 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2432 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2433 GLsizei width
, GLsizei height
, GLsizei depth
,
2434 GLenum format
, GLenum type
,
2435 const GLvoid
*pixels
)
2437 struct gl_texture_unit
*texUnit
;
2438 struct gl_texture_object
*texObj
;
2439 struct gl_texture_image
*texImage
;
2440 GET_CURRENT_CONTEXT(ctx
);
2441 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2443 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2444 _mesa_update_state(ctx
);
2446 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2447 width
, height
, depth
, format
, type
)) {
2448 return; /* error was detected */
2451 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2452 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2453 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2456 if (width
== 0 || height
== 0 || height
== 0)
2457 return; /* no-op, not an error */
2459 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2460 xoffset
+= texImage
->Border
;
2461 yoffset
+= texImage
->Border
;
2462 zoffset
+= texImage
->Border
;
2464 ASSERT(ctx
->Driver
.TexSubImage3D
);
2465 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
2466 xoffset
, yoffset
, zoffset
,
2467 width
, height
, depth
,
2468 format
, type
, pixels
,
2469 &ctx
->Unpack
, texObj
, texImage
);
2470 ctx
->NewState
|= _NEW_TEXTURE
;
2476 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2477 GLenum internalFormat
,
2479 GLsizei width
, GLint border
)
2481 struct gl_texture_unit
*texUnit
;
2482 struct gl_texture_object
*texObj
;
2483 struct gl_texture_image
*texImage
;
2484 GLsizei postConvWidth
= width
;
2485 GET_CURRENT_CONTEXT(ctx
);
2486 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2488 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2489 _mesa_update_state(ctx
);
2491 if (is_color_format(internalFormat
)) {
2492 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2495 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2496 postConvWidth
, 1, border
))
2499 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2500 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2501 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2503 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2506 else if (texImage
->Data
) {
2507 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2509 ASSERT(texImage
->Data
== NULL
);
2511 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2512 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2513 border
, internalFormat
);
2516 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2517 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
2518 x
, y
, width
, border
);
2520 ASSERT(texImage
->TexFormat
);
2522 /* If driver didn't explicitly set these, use the defaults */
2523 if (!texImage
->FetchTexelc
)
2524 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2525 if (!texImage
->FetchTexelf
)
2526 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2527 ASSERT(texImage
->FetchTexelc
);
2528 ASSERT(texImage
->FetchTexelf
);
2531 texObj
->Complete
= GL_FALSE
;
2532 ctx
->NewState
|= _NEW_TEXTURE
;
2538 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2539 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2542 struct gl_texture_unit
*texUnit
;
2543 struct gl_texture_object
*texObj
;
2544 struct gl_texture_image
*texImage
;
2545 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2546 GET_CURRENT_CONTEXT(ctx
);
2547 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2549 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2550 _mesa_update_state(ctx
);
2552 if (is_color_format(internalFormat
)) {
2553 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2557 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2558 postConvWidth
, postConvHeight
, border
))
2561 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2562 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2563 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2565 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2568 else if (texImage
->Data
) {
2569 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2571 ASSERT(texImage
->Data
== NULL
);
2573 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2574 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2575 postConvWidth
, postConvHeight
, 1,
2576 border
, internalFormat
);
2578 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2579 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
2580 x
, y
, width
, height
, border
);
2582 ASSERT(texImage
->TexFormat
);
2584 /* If driver didn't explicitly set these, use the defaults */
2585 if (!texImage
->FetchTexelc
)
2586 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2587 if (!texImage
->FetchTexelf
)
2588 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2589 ASSERT(texImage
->FetchTexelc
);
2590 ASSERT(texImage
->FetchTexelf
);
2593 texObj
->Complete
= GL_FALSE
;
2594 ctx
->NewState
|= _NEW_TEXTURE
;
2600 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2601 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2603 struct gl_texture_unit
*texUnit
;
2604 struct gl_texture_image
*texImage
;
2605 GLsizei postConvWidth
= width
;
2606 GET_CURRENT_CONTEXT(ctx
);
2607 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2609 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2610 _mesa_update_state(ctx
);
2612 /* XXX should test internal format */
2613 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2615 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
2616 xoffset
, 0, 0, postConvWidth
, 1))
2619 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2620 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2623 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2624 xoffset
+= texImage
->Border
;
2626 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2627 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
2628 ctx
->NewState
|= _NEW_TEXTURE
;
2634 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2635 GLint xoffset
, GLint yoffset
,
2636 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2638 struct gl_texture_unit
*texUnit
;
2639 struct gl_texture_image
*texImage
;
2640 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2641 GET_CURRENT_CONTEXT(ctx
);
2642 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2644 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2645 _mesa_update_state(ctx
);
2647 /* XXX should test internal format */
2648 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2650 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2651 postConvWidth
, postConvHeight
))
2654 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2655 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2658 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2659 xoffset
+= texImage
->Border
;
2660 yoffset
+= texImage
->Border
;
2662 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
2663 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
2664 xoffset
, yoffset
, x
, y
, width
, height
);
2665 ctx
->NewState
|= _NEW_TEXTURE
;
2671 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2672 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2673 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2675 struct gl_texture_unit
*texUnit
;
2676 struct gl_texture_image
*texImage
;
2677 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2678 GET_CURRENT_CONTEXT(ctx
);
2679 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2681 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2682 _mesa_update_state(ctx
);
2684 /* XXX should test internal format */
2685 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2687 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2688 zoffset
, postConvWidth
, postConvHeight
))
2691 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2692 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2695 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2696 xoffset
+= texImage
->Border
;
2697 yoffset
+= texImage
->Border
;
2698 zoffset
+= texImage
->Border
;
2700 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2701 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2702 xoffset
, yoffset
, zoffset
,
2703 x
, y
, width
, height
);
2704 ctx
->NewState
|= _NEW_TEXTURE
;
2710 /**********************************************************************/
2711 /****** Compressed Textures ******/
2712 /**********************************************************************/
2716 * Error checking for glCompressedTexImage[123]D().
2717 * \return error code or GL_NO_ERROR.
2720 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
2721 GLenum target
, GLint level
,
2722 GLenum internalFormat
, GLsizei width
,
2723 GLsizei height
, GLsizei depth
, GLint border
,
2726 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2728 if (dimensions
== 1) {
2729 /* 1D compressed textures not allowed */
2730 return GL_INVALID_ENUM
;
2732 else if (dimensions
== 2) {
2733 if (target
== GL_PROXY_TEXTURE_2D
) {
2734 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2736 else if (target
== GL_TEXTURE_2D
) {
2737 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2739 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2740 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2741 return GL_INVALID_ENUM
; /*target*/
2742 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2744 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2745 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2746 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2747 return GL_INVALID_ENUM
; /*target*/
2748 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2751 return GL_INVALID_ENUM
; /*target*/
2754 else if (dimensions
== 3) {
2755 /* 3D compressed textures not allowed */
2756 return GL_INVALID_ENUM
;
2759 maxTextureSize
= 1 << (maxLevels
- 1);
2761 if (!is_compressed_format(ctx
, internalFormat
))
2762 return GL_INVALID_ENUM
;
2764 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
2765 return GL_INVALID_ENUM
;
2768 return GL_INVALID_VALUE
;
2771 * XXX We should probably use the proxy texture error check function here.
2773 if (width
< 1 || width
> maxTextureSize
||
2774 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(width
) != 1))
2775 return GL_INVALID_VALUE
;
2777 if ((height
< 1 || height
> maxTextureSize
||
2778 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(height
) != 1))
2780 return GL_INVALID_VALUE
;
2782 if ((depth
< 1 || depth
> maxTextureSize
||
2783 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& _mesa_bitcount(depth
) != 1))
2785 return GL_INVALID_VALUE
;
2787 /* For cube map, width must equal height */
2788 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2789 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
2790 return GL_INVALID_VALUE
;
2792 if (level
< 0 || level
>= maxLevels
)
2793 return GL_INVALID_VALUE
;
2795 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2797 if (expectedSize
!= imageSize
)
2798 return GL_INVALID_VALUE
;
2805 * Error checking for glCompressedTexSubImage[123]D().
2806 * \warning There are some bad assumptions here about the size of compressed
2807 * texture tiles (multiple of 4) used to test the validity of the
2808 * offset and size parameters.
2809 * \return error code or GL_NO_ERROR.
2812 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
2813 GLenum target
, GLint level
,
2814 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2815 GLsizei width
, GLsizei height
, GLsizei depth
,
2816 GLenum format
, GLsizei imageSize
)
2818 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2821 if (dimensions
== 1) {
2822 /* 1D compressed textures not allowed */
2823 return GL_INVALID_ENUM
;
2825 else if (dimensions
== 2) {
2826 if (target
== GL_PROXY_TEXTURE_2D
) {
2827 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2829 else if (target
== GL_TEXTURE_2D
) {
2830 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2832 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2833 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2834 return GL_INVALID_ENUM
; /*target*/
2835 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2837 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2838 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2839 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2840 return GL_INVALID_ENUM
; /*target*/
2841 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2844 return GL_INVALID_ENUM
; /*target*/
2847 else if (dimensions
== 3) {
2848 /* 3D compressed textures not allowed */
2849 return GL_INVALID_ENUM
;
2852 maxTextureSize
= 1 << (maxLevels
- 1);
2854 if (!is_compressed_format(ctx
, format
))
2855 return GL_INVALID_ENUM
;
2857 if (width
< 1 || width
> maxTextureSize
)
2858 return GL_INVALID_VALUE
;
2860 if ((height
< 1 || height
> maxTextureSize
)
2862 return GL_INVALID_VALUE
;
2864 if (level
< 0 || level
>= maxLevels
)
2865 return GL_INVALID_VALUE
;
2867 /* XXX these tests are specific to the compressed format.
2868 * this code should be generalized in some way.
2870 if ((xoffset
& 3) != 0 || (yoffset
& 3) != 0)
2871 return GL_INVALID_VALUE
;
2873 if ((width
& 3) != 0 && width
!= 2 && width
!= 1)
2874 return GL_INVALID_VALUE
;
2876 if ((height
& 3) != 0 && height
!= 2 && height
!= 1)
2877 return GL_INVALID_VALUE
;
2879 expectedSize
= ctx
->Driver
.CompressedTextureSize(ctx
, width
, height
, depth
,
2881 if (expectedSize
!= imageSize
)
2882 return GL_INVALID_VALUE
;
2890 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2891 GLenum internalFormat
, GLsizei width
,
2892 GLint border
, GLsizei imageSize
,
2895 GET_CURRENT_CONTEXT(ctx
);
2896 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2898 if (target
== GL_TEXTURE_1D
) {
2899 /* non-proxy target */
2900 struct gl_texture_unit
*texUnit
;
2901 struct gl_texture_object
*texObj
;
2902 struct gl_texture_image
*texImage
;
2903 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2904 internalFormat
, width
, 1, 1, border
, imageSize
);
2906 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
2910 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2911 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2912 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2914 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
2917 else if (texImage
->Data
) {
2918 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2920 ASSERT(texImage
->Data
== NULL
);
2922 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2923 border
, internalFormat
);
2925 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2926 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2927 internalFormat
, width
, border
,
2932 texObj
->Complete
= GL_FALSE
;
2933 ctx
->NewState
|= _NEW_TEXTURE
;
2935 else if (target
== GL_PROXY_TEXTURE_1D
) {
2936 /* Proxy texture: check for errors and update proxy state */
2937 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2938 internalFormat
, width
, 1, 1, border
, imageSize
);
2940 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2941 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2942 internalFormat
, GL_NONE
, GL_NONE
,
2943 width
, 1, 1, border
);
2946 /* if error, clear all proxy texture image parameters */
2947 struct gl_texture_image
*texImage
;
2948 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2950 clear_teximage_fields(texImage
);
2953 /* store the teximage parameters */
2954 struct gl_texture_unit
*texUnit
;
2955 struct gl_texture_image
*texImage
;
2956 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2957 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2958 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2959 border
, internalFormat
);
2963 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
2970 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
2971 GLenum internalFormat
, GLsizei width
,
2972 GLsizei height
, GLint border
, GLsizei imageSize
,
2975 GET_CURRENT_CONTEXT(ctx
);
2976 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2978 if (target
== GL_TEXTURE_2D
||
2979 (ctx
->Extensions
.ARB_texture_cube_map
&&
2980 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2981 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
2982 /* non-proxy target */
2983 struct gl_texture_unit
*texUnit
;
2984 struct gl_texture_object
*texObj
;
2985 struct gl_texture_image
*texImage
;
2986 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2987 internalFormat
, width
, height
, 1, border
, imageSize
);
2989 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
2993 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2994 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2995 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2997 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
3000 else if (texImage
->Data
) {
3001 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3003 ASSERT(texImage
->Data
== NULL
);
3005 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3006 border
, internalFormat
);
3008 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3009 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
3010 internalFormat
, width
, height
,
3011 border
, imageSize
, data
,
3015 texObj
->Complete
= GL_FALSE
;
3016 ctx
->NewState
|= _NEW_TEXTURE
;
3018 else if (target
== GL_PROXY_TEXTURE_2D
||
3019 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
3020 ctx
->Extensions
.ARB_texture_cube_map
)) {
3021 /* Proxy texture: check for errors and update proxy state */
3022 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3023 internalFormat
, width
, height
, 1, border
, imageSize
);
3025 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3026 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3027 internalFormat
, GL_NONE
, GL_NONE
,
3028 width
, height
, 1, border
);
3031 /* if error, clear all proxy texture image parameters */
3032 struct gl_texture_image
*texImage
;
3033 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3035 clear_teximage_fields(texImage
);
3038 /* store the teximage parameters */
3039 struct gl_texture_unit
*texUnit
;
3040 struct gl_texture_image
*texImage
;
3041 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3042 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3043 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3044 border
, internalFormat
);
3048 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3055 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3056 GLenum internalFormat
, GLsizei width
,
3057 GLsizei height
, GLsizei depth
, GLint border
,
3058 GLsizei imageSize
, const GLvoid
*data
)
3060 GET_CURRENT_CONTEXT(ctx
);
3061 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3063 if (target
== GL_TEXTURE_3D
) {
3064 /* non-proxy target */
3065 struct gl_texture_unit
*texUnit
;
3066 struct gl_texture_object
*texObj
;
3067 struct gl_texture_image
*texImage
;
3068 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3069 internalFormat
, width
, height
, depth
, border
, imageSize
);
3071 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3075 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3076 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3077 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3079 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3082 else if (texImage
->Data
) {
3083 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3085 ASSERT(texImage
->Data
== NULL
);
3087 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, depth
,
3088 border
, internalFormat
);
3090 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3091 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
3093 width
, height
, depth
,
3094 border
, imageSize
, data
,
3098 texObj
->Complete
= GL_FALSE
;
3099 ctx
->NewState
|= _NEW_TEXTURE
;
3101 else if (target
== GL_PROXY_TEXTURE_3D
) {
3102 /* Proxy texture: check for errors and update proxy state */
3103 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3104 internalFormat
, width
, height
, depth
, border
, imageSize
);
3106 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3107 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3108 internalFormat
, GL_NONE
, GL_NONE
,
3109 width
, height
, depth
, border
);
3112 /* if error, clear all proxy texture image parameters */
3113 struct gl_texture_image
*texImage
;
3114 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3116 clear_teximage_fields(texImage
);
3119 /* store the teximage parameters */
3120 struct gl_texture_unit
*texUnit
;
3121 struct gl_texture_image
*texImage
;
3122 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3123 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3124 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3125 depth
, border
, internalFormat
);
3129 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3136 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3137 GLsizei width
, GLenum format
,
3138 GLsizei imageSize
, const GLvoid
*data
)
3140 struct gl_texture_unit
*texUnit
;
3141 struct gl_texture_object
*texObj
;
3142 struct gl_texture_image
*texImage
;
3144 GET_CURRENT_CONTEXT(ctx
);
3145 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3147 error
= compressed_subtexture_error_check(ctx
, 1, target
, level
,
3148 xoffset
, 0, 0, width
, 1, 1, format
, imageSize
);
3150 _mesa_error(ctx
, error
, "glCompressedTexSubImage1D");
3154 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3155 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3156 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3159 if ((GLint
) format
!= texImage
->IntFormat
) {
3160 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3161 "glCompressedTexSubImage1D(format)");
3165 if ((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) {
3166 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage1D(width)");
3171 return; /* no-op, not an error */
3173 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3174 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
3176 format
, imageSize
, data
,
3179 ctx
->NewState
|= _NEW_TEXTURE
;
3184 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3185 GLint yoffset
, GLsizei width
, GLsizei height
,
3186 GLenum format
, GLsizei imageSize
,
3189 struct gl_texture_unit
*texUnit
;
3190 struct gl_texture_object
*texObj
;
3191 struct gl_texture_image
*texImage
;
3193 GET_CURRENT_CONTEXT(ctx
);
3194 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3196 error
= compressed_subtexture_error_check(ctx
, 2, target
, level
,
3197 xoffset
, yoffset
, 0, width
, height
, 1, format
, imageSize
);
3199 /* XXX proxy target? */
3200 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3204 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3205 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3206 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3209 if ((GLint
) format
!= texImage
->IntFormat
) {
3210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3211 "glCompressedTexSubImage2D(format)");
3215 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3216 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
)) {
3217 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage2D(size)");
3221 if (width
== 0 || height
== 0)
3222 return; /* no-op, not an error */
3224 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3225 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
3226 xoffset
, yoffset
, width
, height
,
3227 format
, imageSize
, data
,
3230 ctx
->NewState
|= _NEW_TEXTURE
;
3235 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3236 GLint yoffset
, GLint zoffset
, GLsizei width
,
3237 GLsizei height
, GLsizei depth
, GLenum format
,
3238 GLsizei imageSize
, const GLvoid
*data
)
3240 struct gl_texture_unit
*texUnit
;
3241 struct gl_texture_object
*texObj
;
3242 struct gl_texture_image
*texImage
;
3244 GET_CURRENT_CONTEXT(ctx
);
3245 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3247 error
= compressed_subtexture_error_check(ctx
, 3, target
, level
,
3248 xoffset
, yoffset
, zoffset
, width
, height
, depth
, format
, imageSize
);
3250 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3254 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3255 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3256 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3259 if ((GLint
) format
!= texImage
->IntFormat
) {
3260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3261 "glCompressedTexSubImage3D(format)");
3265 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3266 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
) ||
3267 ((depth
== 1 || depth
== 2) && (GLuint
) depth
!= texImage
->Depth
)) {
3268 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage3D(size)");
3272 if (width
== 0 || height
== 0 || depth
== 0)
3273 return; /* no-op, not an error */
3275 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3276 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
3277 xoffset
, yoffset
, zoffset
,
3278 width
, height
, depth
,
3279 format
, imageSize
, data
,
3282 ctx
->NewState
|= _NEW_TEXTURE
;
3287 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
3289 const struct gl_texture_unit
*texUnit
;
3290 const struct gl_texture_object
*texObj
;
3291 struct gl_texture_image
*texImage
;
3293 GET_CURRENT_CONTEXT(ctx
);
3294 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3296 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3297 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3299 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
3303 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3304 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
3306 if (level
< 0 || level
>= maxLevels
) {
3307 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3311 if (is_proxy_target(target
)) {
3312 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
3316 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3318 /* probably invalid mipmap level */
3319 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3323 if (!texImage
->IsCompressed
) {
3324 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
3328 /* this typically calls _mesa_get_compressed_teximage() */
3329 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
, texObj
,texImage
);