2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Texture images manipulation functions.
30 * \note Mesa's native texture data type is GLchan. Native formats are
31 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA, and
34 * \note Device drivers are free to implement any internal format they want.
45 #include "texcompress.h"
46 #include "texformat.h"
54 static void PrintTexture(GLcontext
*ctx
, const struct gl_texture_image
*img
)
56 #if CHAN_TYPE == GL_FLOAT
57 _mesa_problem(NULL
, "PrintTexture doesn't support float channels");
60 const GLchan
*data
= (const GLchan
*) img
->Data
;
63 _mesa_printf("No texture data\n");
67 switch (img
->Format
) {
74 case GL_LUMINANCE_ALPHA
:
84 _mesa_problem(NULL
, "error in PrintTexture\n");
88 for (i
= 0; i
< img
->Height
; i
++) {
89 for (j
= 0; j
< img
->Width
; j
++) {
91 _mesa_printf("%02x ", data
[0]);
93 _mesa_printf("%02x%02x ", data
[0], data
[1]);
95 _mesa_printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
97 _mesa_printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
98 data
+= (img
->RowStride
- img
->Width
) * c
;
108 * Compute floor(log_base_2(n)).
109 * If n < 0 return -1.
138 * Return the simple base format for a given internal texture format.
139 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
141 * \param ctx GL context.
142 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
144 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
145 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
147 * This is the format which is used during texture application (i.e. the
148 * texture format and env mode determine the arithmetic used.
151 _mesa_base_tex_format( GLcontext
*ctx
, GLint internalFormat
)
153 switch (internalFormat
) {
168 case GL_LUMINANCE_ALPHA
:
169 case GL_LUMINANCE4_ALPHA4
:
170 case GL_LUMINANCE6_ALPHA2
:
171 case GL_LUMINANCE8_ALPHA8
:
172 case GL_LUMINANCE12_ALPHA4
:
173 case GL_LUMINANCE12_ALPHA12
:
174 case GL_LUMINANCE16_ALPHA16
:
175 return GL_LUMINANCE_ALPHA
;
206 if (ctx
->Extensions
.EXT_paletted_texture
) {
207 switch (internalFormat
) {
209 case GL_COLOR_INDEX1_EXT
:
210 case GL_COLOR_INDEX2_EXT
:
211 case GL_COLOR_INDEX4_EXT
:
212 case GL_COLOR_INDEX8_EXT
:
213 case GL_COLOR_INDEX12_EXT
:
214 case GL_COLOR_INDEX16_EXT
:
215 return GL_COLOR_INDEX
;
221 if (ctx
->Extensions
.SGIX_depth_texture
) {
222 switch (internalFormat
) {
223 case GL_DEPTH_COMPONENT
:
224 case GL_DEPTH_COMPONENT16_SGIX
:
225 case GL_DEPTH_COMPONENT24_SGIX
:
226 case GL_DEPTH_COMPONENT32_SGIX
:
227 return GL_DEPTH_COMPONENT
;
233 if (ctx
->Extensions
.ARB_texture_compression
) {
234 switch (internalFormat
) {
235 case GL_COMPRESSED_ALPHA
:
237 case GL_COMPRESSED_LUMINANCE
:
239 case GL_COMPRESSED_LUMINANCE_ALPHA
:
240 return GL_LUMINANCE_ALPHA
;
241 case GL_COMPRESSED_INTENSITY
:
243 case GL_COMPRESSED_RGB
:
245 case GL_COMPRESSED_RGBA
:
252 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
253 switch (internalFormat
) {
254 case GL_COMPRESSED_RGB_FXT1_3DFX
:
256 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
258 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
265 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
266 switch (internalFormat
) {
267 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
268 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
269 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
276 if (ctx
->Extensions
.S3_s3tc
) {
277 switch (internalFormat
) {
289 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
290 if (internalFormat
== GL_YCBCR_MESA
)
291 return GL_YCBCR_MESA
;
294 if (ctx
->Extensions
.ARB_texture_float
) {
295 switch (internalFormat
) {
296 case GL_ALPHA16F_ARB
:
297 case GL_ALPHA32F_ARB
:
305 case GL_INTENSITY16F_ARB
:
306 case GL_INTENSITY32F_ARB
:
308 case GL_LUMINANCE16F_ARB
:
309 case GL_LUMINANCE32F_ARB
:
311 case GL_LUMINANCE_ALPHA16F_ARB
:
312 case GL_LUMINANCE_ALPHA32F_ARB
:
313 return GL_LUMINANCE_ALPHA
;
319 return -1; /* error */
324 * Test if the given internal texture format is a color/RGBA format
325 * (i.e., not color index, depth, stencil, etc).
326 * \param internalFormat an internal texture format token (or 1, 2, 3, or 4)
327 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
330 is_color_format(GLenum internalFormat
)
332 switch (internalFormat
) {
345 case GL_LUMINANCE_ALPHA
:
346 case GL_LUMINANCE4_ALPHA4
:
347 case GL_LUMINANCE6_ALPHA2
:
348 case GL_LUMINANCE8_ALPHA8
:
349 case GL_LUMINANCE12_ALPHA4
:
350 case GL_LUMINANCE12_ALPHA12
:
351 case GL_LUMINANCE16_ALPHA16
:
375 /* float texture formats */
376 case GL_ALPHA16F_ARB
:
377 case GL_ALPHA32F_ARB
:
378 case GL_LUMINANCE16F_ARB
:
379 case GL_LUMINANCE32F_ARB
:
380 case GL_LUMINANCE_ALPHA16F_ARB
:
381 case GL_LUMINANCE_ALPHA32F_ARB
:
382 case GL_INTENSITY16F_ARB
:
383 case GL_INTENSITY32F_ARB
:
389 case GL_YCBCR_MESA
: /* not considered to be RGB */
397 * Test if the given internal texture format is a color index format.
400 is_index_format(GLenum internalFormat
)
402 switch (internalFormat
) {
404 case GL_COLOR_INDEX1_EXT
:
405 case GL_COLOR_INDEX2_EXT
:
406 case GL_COLOR_INDEX4_EXT
:
407 case GL_COLOR_INDEX8_EXT
:
408 case GL_COLOR_INDEX12_EXT
:
409 case GL_COLOR_INDEX16_EXT
:
418 * Test if the given internal texture format is a depth component format.
421 is_depth_format(GLenum internalFormat
)
423 switch (internalFormat
) {
424 case GL_DEPTH_COMPONENT16_ARB
:
425 case GL_DEPTH_COMPONENT24_ARB
:
426 case GL_DEPTH_COMPONENT32_ARB
:
427 case GL_DEPTH_COMPONENT
:
436 * Test if the given internal texture format is a YCbCr format.
439 is_ycbcr_format(GLenum internalFormat
)
441 switch (internalFormat
) {
451 * Test if it is a supported compressed format.
453 * \param internalFormat the internal format token provided by the user.
455 * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
456 * GL_FALSE otherwise.
458 * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
462 is_compressed_format(GLcontext
*ctx
, GLenum internalFormat
)
464 switch (internalFormat
) {
465 case GL_COMPRESSED_RGB_FXT1_3DFX
:
466 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
467 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
468 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
469 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
470 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
477 if (ctx
->Driver
.IsCompressedFormat
) {
478 return ctx
->Driver
.IsCompressedFormat(ctx
, internalFormat
);
486 * Store a gl_texture_image pointer in a gl_texture_object structure
487 * according to the target and level parameters.
489 * \param tObj texture object.
490 * \param target texture target.
491 * \param level image level.
492 * \param texImage texture image.
494 * This was basically prompted by the introduction of cube maps.
497 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
498 GLenum target
, GLint level
,
499 struct gl_texture_image
*texImage
)
507 tObj
->Image
[0][level
] = texImage
;
509 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
510 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
511 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
512 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
513 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
514 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
: {
515 GLuint face
= ((GLuint
) target
-
516 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
517 tObj
->Image
[face
][level
] = texImage
;
520 case GL_TEXTURE_RECTANGLE_NV
:
522 tObj
->Image
[0][level
] = texImage
;
525 _mesa_problem(NULL
, "bad target in _mesa_set_tex_image()");
532 * Allocate a texture image structure.
534 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
537 * \return a pointer to gl_texture_image struct with all fields initialized to
540 struct gl_texture_image
*
541 _mesa_new_texture_image( GLcontext
*ctx
)
544 return CALLOC_STRUCT(gl_texture_image
);
549 * Free texture image.
551 * \param teximage texture image.
553 * Free the texture image structure and the associated image data if it's not
554 * marked as client data.
557 _mesa_delete_texture_image( struct gl_texture_image
*teximage
)
559 if (teximage
->Data
&& !teximage
->IsClientData
) {
560 MESA_PBUFFER_FREE( teximage
->Data
);
561 teximage
->Data
= NULL
;
568 * Test if a target is a proxy target.
570 * \param target texture target.
572 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
575 is_proxy_target(GLenum target
)
577 return (target
== GL_PROXY_TEXTURE_1D
||
578 target
== GL_PROXY_TEXTURE_2D
||
579 target
== GL_PROXY_TEXTURE_3D
||
580 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
581 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
);
586 * Get the texture object that corresponds to the target of the given texture unit.
588 * \param ctx GL context.
589 * \param texUnit texture unit.
590 * \param target texture target.
592 * \return pointer to the texture object on success, or NULL on failure.
594 * \sa gl_texture_unit.
596 struct gl_texture_object
*
597 _mesa_select_tex_object(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
602 return texUnit
->Current1D
;
603 case GL_PROXY_TEXTURE_1D
:
604 return ctx
->Texture
.Proxy1D
;
606 return texUnit
->Current2D
;
607 case GL_PROXY_TEXTURE_2D
:
608 return ctx
->Texture
.Proxy2D
;
610 return texUnit
->Current3D
;
611 case GL_PROXY_TEXTURE_3D
:
612 return ctx
->Texture
.Proxy3D
;
613 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
614 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
615 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
616 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
617 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
618 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
619 case GL_TEXTURE_CUBE_MAP_ARB
:
620 return ctx
->Extensions
.ARB_texture_cube_map
621 ? texUnit
->CurrentCubeMap
: NULL
;
622 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
623 return ctx
->Extensions
.ARB_texture_cube_map
624 ? ctx
->Texture
.ProxyCubeMap
: NULL
;
625 case GL_TEXTURE_RECTANGLE_NV
:
626 return ctx
->Extensions
.NV_texture_rectangle
627 ? texUnit
->CurrentRect
: NULL
;
628 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
629 return ctx
->Extensions
.NV_texture_rectangle
630 ? ctx
->Texture
.ProxyRect
: NULL
;
632 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
639 * Get the texture image struct which corresponds to target and level
640 * of the given texture unit.
642 * \param ctx GL context.
643 * \param texUnit texture unit.
644 * \param target texture target.
645 * \param level image level.
647 * \return pointer to the texture image structure on success, or NULL on failure.
649 * \sa gl_texture_unit.
651 struct gl_texture_image
*
652 _mesa_select_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
653 GLenum target
, GLint level
)
656 ASSERT(level
< MAX_TEXTURE_LEVELS
);
659 return texUnit
->Current1D
->Image
[0][level
];
660 case GL_PROXY_TEXTURE_1D
:
661 return ctx
->Texture
.Proxy1D
->Image
[0][level
];
663 return texUnit
->Current2D
->Image
[0][level
];
664 case GL_PROXY_TEXTURE_2D
:
665 return ctx
->Texture
.Proxy2D
->Image
[0][level
];
667 return texUnit
->Current3D
->Image
[0][level
];
668 case GL_PROXY_TEXTURE_3D
:
669 return ctx
->Texture
.Proxy3D
->Image
[0][level
];
670 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
671 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
672 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
673 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
674 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
675 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
676 if (ctx
->Extensions
.ARB_texture_cube_map
) {
677 GLuint face
= ((GLuint
) target
-
678 (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
679 return texUnit
->CurrentCubeMap
->Image
[face
][level
];
683 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
684 if (ctx
->Extensions
.ARB_texture_cube_map
)
685 return ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
688 case GL_TEXTURE_RECTANGLE_NV
:
689 if (ctx
->Extensions
.NV_texture_rectangle
) {
691 return texUnit
->CurrentRect
->Image
[0][level
];
696 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
697 if (ctx
->Extensions
.NV_texture_rectangle
) {
699 return ctx
->Texture
.ProxyRect
->Image
[0][level
];
705 _mesa_problem(ctx
, "bad target in _mesa_select_tex_image()");
712 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
713 * it and install it. Only return NULL if passed a bad parameter or run
716 struct gl_texture_image
*
717 _mesa_get_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
718 GLenum target
, GLint level
)
720 struct gl_texture_image
*texImage
;
721 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
723 struct gl_texture_object
*texObj
;
724 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
726 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
729 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
731 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
738 * Return pointer to the specified proxy texture image.
739 * Note that proxy textures are per-context, not per-texture unit.
740 * \return pointer to texture image or NULL if invalid target, invalid
741 * level, or out of memory.
743 struct gl_texture_image
*
744 _mesa_get_proxy_tex_image(GLcontext
*ctx
, GLenum target
, GLint level
)
746 struct gl_texture_image
*texImage
;
752 case GL_PROXY_TEXTURE_1D
:
753 if (level
>= ctx
->Const
.MaxTextureLevels
)
755 texImage
= ctx
->Texture
.Proxy1D
->Image
[0][level
];
757 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
759 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
762 ctx
->Texture
.Proxy1D
->Image
[0][level
] = texImage
;
765 case GL_PROXY_TEXTURE_2D
:
766 if (level
>= ctx
->Const
.MaxTextureLevels
)
768 texImage
= ctx
->Texture
.Proxy2D
->Image
[0][level
];
770 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
772 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
775 ctx
->Texture
.Proxy2D
->Image
[0][level
] = texImage
;
778 case GL_PROXY_TEXTURE_3D
:
779 if (level
>= ctx
->Const
.Max3DTextureLevels
)
781 texImage
= ctx
->Texture
.Proxy3D
->Image
[0][level
];
783 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
785 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
788 ctx
->Texture
.Proxy3D
->Image
[0][level
] = texImage
;
791 case GL_PROXY_TEXTURE_CUBE_MAP
:
792 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
794 texImage
= ctx
->Texture
.ProxyCubeMap
->Image
[0][level
];
796 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
798 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
801 ctx
->Texture
.ProxyCubeMap
->Image
[0][level
] = texImage
;
804 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
807 texImage
= ctx
->Texture
.ProxyRect
->Image
[0][level
];
809 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
811 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
814 ctx
->Texture
.ProxyRect
->Image
[0][level
] = texImage
;
824 * Get the maximum number of allowed mipmap levels.
826 * \param ctx GL context.
827 * \param target texture target.
829 * \return the maximum number of allowed mipmap levels for the given
830 * texture target, or zero if passed a bad target.
835 _mesa_max_texture_levels(GLcontext
*ctx
, GLenum target
)
839 case GL_PROXY_TEXTURE_1D
:
841 case GL_PROXY_TEXTURE_2D
:
842 return ctx
->Const
.MaxTextureLevels
;
844 case GL_PROXY_TEXTURE_3D
:
845 return ctx
->Const
.Max3DTextureLevels
;
846 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
847 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
848 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
849 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
850 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
851 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
852 case GL_TEXTURE_CUBE_MAP_ARB
:
853 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
854 return ctx
->Const
.MaxCubeTextureLevels
;
856 case GL_TEXTURE_RECTANGLE_NV
:
857 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
861 return 0; /* bad target */
867 #if 000 /* not used anymore */
869 * glTexImage[123]D can accept a NULL image pointer. In this case we
870 * create a texture image with unspecified image contents per the OpenGL
874 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
876 const GLint components
= _mesa_components_in_format(format
);
877 const GLint numPixels
= width
* height
* depth
;
878 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
882 * Let's see if anyone finds this. If glTexImage2D() is called with
883 * a NULL image pointer then load the texture image with something
884 * interesting instead of leaving it indeterminate.
887 static const char message
[8][32] = {
891 " X X XXXX XXX XXXXX ",
894 " X X XXXXX XXX X X ",
898 GLubyte
*imgPtr
= data
;
900 for (h
= 0; h
< depth
; h
++) {
901 for (i
= 0; i
< height
; i
++) {
902 GLint srcRow
= 7 - (i
% 8);
903 for (j
= 0; j
< width
; j
++) {
904 GLint srcCol
= j
% 32;
905 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
906 for (k
= 0; k
< components
; k
++) {
922 * Reset the fields of a gl_texture_image struct to zero.
924 * \param img texture image structure.
926 * This is called when a proxy texture test fails, we set all the
927 * image members (except DriverData) to zero.
928 * It's also used in glTexImage[123]D as a safeguard to be sure all
929 * required fields get initialized properly by the Driver.TexImage[123]D
933 clear_teximage_fields(struct gl_texture_image
*img
)
950 img
->TexFormat
= &_mesa_null_texformat
;
951 img
->FetchTexelc
= NULL
;
952 img
->FetchTexelf
= NULL
;
953 img
->IsCompressed
= 0;
954 img
->CompressedSize
= 0;
959 * Initialize basic fields of the gl_texture_image struct.
961 * \param ctx GL context.
962 * \param target texture target.
963 * \param img texture image structure to be initialized.
964 * \param width image width.
965 * \param height image height.
966 * \param depth image depth.
967 * \param border image border.
968 * \param internalFormat internal format.
970 * Fills in the fields of \p img with the given information.
971 * Note: width, height and depth include the border.
974 _mesa_init_teximage_fields(GLcontext
*ctx
, GLenum target
,
975 struct gl_texture_image
*img
,
976 GLsizei width
, GLsizei height
, GLsizei depth
,
977 GLint border
, GLenum internalFormat
)
980 img
->Format
= _mesa_base_tex_format( ctx
, internalFormat
);
981 ASSERT(img
->Format
> 0);
982 img
->IntFormat
= internalFormat
;
983 img
->Border
= border
;
985 img
->Height
= height
;
987 img
->RowStride
= width
;
988 img
->WidthLog2
= logbase2(width
- 2 * border
);
989 if (height
== 1) /* 1-D texture */
992 img
->HeightLog2
= logbase2(height
- 2 * border
);
993 if (depth
== 1) /* 2-D texture */
996 img
->DepthLog2
= logbase2(depth
- 2 * border
);
997 img
->Width2
= width
- 2 * border
; /*1 << img->WidthLog2;*/
998 img
->Height2
= height
- 2 * border
; /*1 << img->HeightLog2;*/
999 img
->Depth2
= depth
- 2 * border
; /*1 << img->DepthLog2;*/
1000 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1001 img
->IsCompressed
= is_compressed_format(ctx
, internalFormat
);
1002 if (img
->IsCompressed
)
1003 img
->CompressedSize
= _mesa_compressed_texture_size(ctx
, width
, height
,
1004 depth
, internalFormat
);
1006 img
->CompressedSize
= 0;
1008 if ((width
== 1 || _mesa_bitcount(width
- 2 * border
) == 1) &&
1009 (height
== 1 || _mesa_bitcount(height
- 2 * border
) == 1) &&
1010 (depth
== 1 || _mesa_bitcount(depth
- 2 * border
) == 1))
1011 img
->_IsPowerOfTwo
= GL_TRUE
;
1013 img
->_IsPowerOfTwo
= GL_FALSE
;
1015 /* Compute Width/Height/DepthScale for mipmap lod computation */
1016 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1017 /* scale = 1.0 since texture coords directly map to texels */
1018 img
->WidthScale
= 1.0;
1019 img
->HeightScale
= 1.0;
1020 img
->DepthScale
= 1.0;
1023 img
->WidthScale
= (GLfloat
) img
->Width
;
1024 img
->HeightScale
= (GLfloat
) img
->Height
;
1025 img
->DepthScale
= (GLfloat
) img
->Depth
;
1031 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1032 * level, width, height and depth against the ctx->Const limits for textures.
1034 * A hardware driver might override this function if, for example, the
1035 * max 3D texture size is 512x512x64 (i.e. not a cube).
1037 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1038 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1039 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1040 * \param level as passed to glTexImage
1041 * \param internalFormat as passed to glTexImage
1042 * \param format as passed to glTexImage
1043 * \param type as passed to glTexImage
1044 * \param width as passed to glTexImage
1045 * \param height as passed to glTexImage
1046 * \param depth as passed to glTexImage
1047 * \param border as passed to glTexImage
1048 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1051 _mesa_test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1052 GLint internalFormat
, GLenum format
, GLenum type
,
1053 GLint width
, GLint height
, GLint depth
, GLint border
)
1057 (void) internalFormat
;
1062 case GL_PROXY_TEXTURE_1D
:
1063 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1064 if (width
< 2 * border
|| width
> 2 + maxSize
||
1065 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1066 _mesa_bitcount(width
- 2 * border
) != 1) ||
1067 level
>= ctx
->Const
.MaxTextureLevels
) {
1068 /* bad width or level */
1072 case GL_PROXY_TEXTURE_2D
:
1073 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1074 if (width
< 2 * border
|| width
> 2 + maxSize
||
1075 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1076 _mesa_bitcount(width
- 2 * border
) != 1) ||
1077 height
< 2 * border
|| height
> 2 + maxSize
||
1078 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1079 _mesa_bitcount(height
- 2 * border
) != 1) ||
1080 level
>= ctx
->Const
.MaxTextureLevels
) {
1081 /* bad width or height or level */
1085 case GL_PROXY_TEXTURE_3D
:
1086 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1087 if (width
< 2 * border
|| width
> 2 + maxSize
||
1088 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1089 _mesa_bitcount(width
- 2 * border
) != 1) ||
1090 height
< 2 * border
|| height
> 2 + maxSize
||
1091 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1092 _mesa_bitcount(height
- 2 * border
) != 1) ||
1093 depth
< 2 * border
|| depth
> 2 + maxSize
||
1094 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1095 _mesa_bitcount(depth
- 2 * border
) != 1) ||
1096 level
>= ctx
->Const
.Max3DTextureLevels
) {
1097 /* bad width or height or depth or level */
1101 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1102 if (width
< 1 || width
> ctx
->Const
.MaxTextureRectSize
||
1103 height
< 1 || height
> ctx
->Const
.MaxTextureRectSize
||
1105 /* bad width or height or level */
1109 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1110 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1111 if (width
< 2 * border
|| width
> 2 + maxSize
||
1112 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1113 _mesa_bitcount(width
- 2 * border
) != 1) ||
1114 height
< 2 * border
|| height
> 2 + maxSize
||
1115 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1116 _mesa_bitcount(height
- 2 * border
) != 1) ||
1117 level
>= ctx
->Const
.MaxCubeTextureLevels
) {
1118 /* bad width or height */
1123 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1130 * Test the glTexImage[123]D() parameters for errors.
1132 * \param ctx GL context.
1133 * \param target texture target given by the user.
1134 * \param level image level given by the user.
1135 * \param internalFormat internal format given by the user.
1136 * \param format pixel data format given by the user.
1137 * \param type pixel data type given by the user.
1138 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1139 * \param width image width given by the user.
1140 * \param height image height given by the user.
1141 * \param depth image depth given by the user.
1142 * \param border image border given by the user.
1144 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1146 * Verifies each of the parameters against the constants specified in
1147 * __GLcontextRec::Const and the supported extensions, and according to the
1148 * OpenGL specification.
1151 texture_error_check( GLcontext
*ctx
, GLenum target
,
1152 GLint level
, GLint internalFormat
,
1153 GLenum format
, GLenum type
,
1155 GLint width
, GLint height
,
1156 GLint depth
, GLint border
)
1158 const GLboolean isProxy
= is_proxy_target(target
);
1161 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1162 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1164 _mesa_error(ctx
, GL_INVALID_VALUE
,
1165 "glTexImage%dD(level=%d)", dimensions
, level
);
1171 if (border
< 0 || border
> 1 ||
1172 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1173 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1175 _mesa_error(ctx
, GL_INVALID_VALUE
,
1176 "glTexImage%dD(border=%d)", dimensions
, border
);
1181 if (width
< 0 || height
< 0 || depth
< 0) {
1183 _mesa_error(ctx
, GL_INVALID_VALUE
,
1184 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1189 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1190 * level, width, height and depth.
1192 if (dimensions
== 1) {
1193 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1194 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1195 level
, internalFormat
,
1197 width
, 1, 1, border
);
1200 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1204 else if (dimensions
== 2) {
1205 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1206 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1207 level
, internalFormat
,
1209 width
, height
, 1, border
);
1211 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1212 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1213 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1214 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1215 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1218 sizeOK
= (width
== height
) &&
1219 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1220 level
, internalFormat
, format
, type
,
1221 width
, height
, 1, border
);
1223 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1224 target
== GL_TEXTURE_RECTANGLE_NV
) {
1225 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1226 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1229 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1230 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1231 level
, internalFormat
,
1233 width
, height
, 1, border
);
1236 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1240 else if (dimensions
== 3) {
1241 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1242 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_3D
,
1243 level
, internalFormat
,
1245 width
, height
, depth
, border
);
1248 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1253 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1259 _mesa_error(ctx
, GL_INVALID_VALUE
,
1260 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1261 dimensions
, level
, width
, height
, depth
);
1266 /* Check internalFormat */
1267 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1269 _mesa_error(ctx
, GL_INVALID_VALUE
,
1270 "glTexImage%dD(internalFormat=0x%x)",
1271 dimensions
, internalFormat
);
1276 /* Check incoming image format and type */
1277 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1278 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1279 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1282 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1283 "glTexImage%dD(format or type)", dimensions
);
1288 if (format
== GL_YCBCR_MESA
|| internalFormat
== GL_YCBCR_MESA
) {
1289 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1290 if (format
!= GL_YCBCR_MESA
||
1291 internalFormat
!= GL_YCBCR_MESA
||
1292 (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1293 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
)) {
1295 _mesa_sprintf(message
,
1296 "glTexImage%d(format/type/internalFormat YCBCR mismatch",
1298 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
1299 return GL_TRUE
; /* error */
1301 if (target
!= GL_TEXTURE_2D
&&
1302 target
!= GL_PROXY_TEXTURE_2D
&&
1303 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1304 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1306 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1312 _mesa_sprintf(message
,
1313 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1314 dimensions
, border
);
1315 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1321 if (is_compressed_format(ctx
, internalFormat
)) {
1322 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1325 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1326 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1327 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1328 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1333 _mesa_error(ctx
, GL_INVALID_ENUM
,
1334 "glTexImage%d(target)", dimensions
);
1340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1341 "glTexImage%D(border!=0)", dimensions
);
1347 /* if we get here, the parameters are OK */
1353 * Test glTexSubImage[123]D() parameters for errors.
1355 * \param ctx GL context.
1356 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1357 * \param target texture target given by the user.
1358 * \param level image level given by the user.
1359 * \param xoffset sub-image x offset given by the user.
1360 * \param yoffset sub-image y offset given by the user.
1361 * \param zoffset sub-image z offset given by the user.
1362 * \param format pixel data format given by the user.
1363 * \param type pixel data type given by the user.
1364 * \param width image width given by the user.
1365 * \param height image height given by the user.
1366 * \param depth image depth given by the user.
1368 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1370 * Verifies each of the parameters against the constants specified in
1371 * __GLcontextRec::Const and the supported extensions, and according to the
1372 * OpenGL specification.
1375 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1376 GLenum target
, GLint level
,
1377 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1378 GLint width
, GLint height
, GLint depth
,
1379 GLenum format
, GLenum type
)
1381 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1382 struct gl_texture_image
*destTex
;
1385 if (dimensions
== 1) {
1386 if (target
!= GL_TEXTURE_1D
) {
1387 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1391 else if (dimensions
== 2) {
1392 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1393 target
<=GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1394 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1395 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1399 else if (ctx
->Extensions
.NV_texture_rectangle
&&
1400 target
== GL_TEXTURE_RECTANGLE_NV
) {
1401 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1402 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1406 else if (target
!= GL_TEXTURE_2D
) {
1407 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1411 else if (dimensions
== 3) {
1412 if (target
!= GL_TEXTURE_3D
) {
1413 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1418 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1422 /* Basic level check */
1423 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1424 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1429 _mesa_error(ctx
, GL_INVALID_VALUE
,
1430 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1433 if (height
< 0 && dimensions
> 1) {
1434 _mesa_error(ctx
, GL_INVALID_VALUE
,
1435 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1438 if (depth
< 0 && dimensions
> 2) {
1439 _mesa_error(ctx
, GL_INVALID_VALUE
,
1440 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1444 destTex
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1447 /* undefined image level */
1448 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1452 if (xoffset
< -((GLint
)destTex
->Border
)) {
1453 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1457 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1458 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1462 if (dimensions
> 1) {
1463 if (yoffset
< -((GLint
)destTex
->Border
)) {
1464 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1468 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1469 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1474 if (dimensions
> 2) {
1475 if (zoffset
< -((GLint
)destTex
->Border
)) {
1476 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1479 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1480 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1485 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1486 _mesa_error(ctx
, GL_INVALID_ENUM
,
1487 "glTexSubImage%dD(format or type)", dimensions
);
1491 if (destTex
->IsCompressed
) {
1492 const struct gl_texture_unit
*texUnit
;
1493 const struct gl_texture_image
*texImage
;
1494 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1495 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1497 if (target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
) {
1500 else if (ctx
->Extensions
.ARB_texture_cube_map
&&
1501 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1502 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1503 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
1507 _mesa_error(ctx
, GL_INVALID_ENUM
,
1508 "glTexSubImage%D(target)", dimensions
);
1511 /* offset must be multiple of 4 */
1512 if ((xoffset
& 3) || (yoffset
& 3)) {
1513 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1514 "glTexSubImage%D(xoffset or yoffset)", dimensions
);
1517 /* size must be multiple of 4 or equal to whole texture size */
1518 if ((width
& 3) && (GLuint
) width
!= texImage
->Width
) {
1519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1520 "glTexSubImage%D(width)", dimensions
);
1523 if ((height
& 3) && (GLuint
) height
!= texImage
->Height
) {
1524 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1525 "glTexSubImage%D(width)", dimensions
);
1535 * Test glCopyTexImage[12]D() parameters for errors.
1537 * \param ctx GL context.
1538 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1539 * \param target texture target given by the user.
1540 * \param level image level given by the user.
1541 * \param internalFormat internal format given by the user.
1542 * \param width image width given by the user.
1543 * \param height image height given by the user.
1544 * \param depth image depth given by the user.
1545 * \param border texture border.
1547 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1549 * Verifies each of the parameters against the constants specified in
1550 * __GLcontextRec::Const and the supported extensions, and according to the
1551 * OpenGL specification.
1554 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1555 GLenum target
, GLint level
, GLint internalFormat
,
1556 GLint width
, GLint height
, GLint border
)
1558 GLenum format
, type
;
1561 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1562 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1563 _mesa_error(ctx
, GL_INVALID_VALUE
,
1564 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1569 if (border
< 0 || border
> 1 ||
1570 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1571 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1575 /* The format and type aren't really significant here, but we need to pass
1576 * something to TestProxyTexImage().
1578 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1581 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1582 * level, width, height and depth.
1584 if (dimensions
== 1) {
1585 if (target
== GL_TEXTURE_1D
) {
1586 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1587 level
, internalFormat
,
1589 width
, 1, 1, border
);
1592 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1596 else if (dimensions
== 2) {
1597 if (target
== GL_TEXTURE_2D
) {
1598 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1599 level
, internalFormat
,
1601 width
, height
, 1, border
);
1603 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1604 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1605 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1606 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1609 sizeOK
= (width
== height
) &&
1610 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1611 level
, internalFormat
, format
, type
,
1612 width
, height
, 1, border
);
1614 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1615 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1616 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1619 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1620 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1621 level
, internalFormat
,
1623 width
, height
, 1, border
);
1626 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1631 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1636 if (dimensions
== 1) {
1637 _mesa_error(ctx
, GL_INVALID_VALUE
,
1638 "glCopyTexImage1D(width=%d)", width
);
1641 ASSERT(dimensions
== 2);
1642 _mesa_error(ctx
, GL_INVALID_VALUE
,
1643 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1648 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1649 _mesa_error(ctx
, GL_INVALID_VALUE
,
1650 "glCopyTexImage%dD(internalFormat)", dimensions
);
1654 if (is_compressed_format(ctx
, internalFormat
)) {
1655 if (target
!= GL_TEXTURE_2D
) {
1656 _mesa_error(ctx
, GL_INVALID_ENUM
,
1657 "glCopyTexImage%d(target)", dimensions
);
1661 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1662 "glCopyTexImage%D(border!=0)", dimensions
);
1667 /* if we get here, the parameters are OK */
1673 * Test glCopyTexImage[12]D() parameters for errors.
1675 * \param ctx GL context.
1676 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1677 * \param target texture target given by the user.
1678 * \param level image level given by the user.
1679 * \param xoffset sub-image x offset given by the user.
1680 * \param yoffset sub-image y offset given by the user.
1681 * \param zoffset sub-image z offset given by the user.
1682 * \param width image width given by the user.
1683 * \param height image height given by the user.
1685 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1687 * Verifies each of the parameters against the constants specified in
1688 * __GLcontextRec::Const and the supported extensions, and according to the
1689 * OpenGL specification.
1692 copytexsubimage_error_check( GLcontext
*ctx
, GLuint dimensions
,
1693 GLenum target
, GLint level
,
1694 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1695 GLsizei width
, GLsizei height
)
1697 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1698 struct gl_texture_image
*teximage
;
1701 if (dimensions
== 1) {
1702 if (target
!= GL_TEXTURE_1D
) {
1703 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1707 else if (dimensions
== 2) {
1708 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1709 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1710 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1711 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1715 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1716 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1717 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1721 else if (target
!= GL_TEXTURE_2D
) {
1722 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1726 else if (dimensions
== 3) {
1727 if (target
!= GL_TEXTURE_3D
) {
1728 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1734 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1735 _mesa_error(ctx
, GL_INVALID_VALUE
,
1736 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1742 _mesa_error(ctx
, GL_INVALID_VALUE
,
1743 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1746 if (dimensions
> 1 && height
< 0) {
1747 _mesa_error(ctx
, GL_INVALID_VALUE
,
1748 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1752 teximage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1754 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1755 "glCopyTexSubImage%dD(undefined texture level: %d)",
1760 if (xoffset
< -((GLint
)teximage
->Border
)) {
1761 _mesa_error(ctx
, GL_INVALID_VALUE
,
1762 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1765 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1766 _mesa_error(ctx
, GL_INVALID_VALUE
,
1767 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1770 if (dimensions
> 1) {
1771 if (yoffset
< -((GLint
)teximage
->Border
)) {
1772 _mesa_error(ctx
, GL_INVALID_VALUE
,
1773 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1776 /* NOTE: we're adding the border here, not subtracting! */
1777 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1778 _mesa_error(ctx
, GL_INVALID_VALUE
,
1779 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1784 if (dimensions
> 2) {
1785 if (zoffset
< -((GLint
)teximage
->Border
)) {
1786 _mesa_error(ctx
, GL_INVALID_VALUE
,
1787 "glCopyTexSubImage%dD(zoffset)", dimensions
);
1790 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1791 _mesa_error(ctx
, GL_INVALID_VALUE
,
1792 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1797 if (teximage
->IsCompressed
) {
1798 if (target
!= GL_TEXTURE_2D
) {
1799 _mesa_error(ctx
, GL_INVALID_ENUM
,
1800 "glCopyTexSubImage%d(target)", dimensions
);
1803 /* offset must be multiple of 4 */
1804 if ((xoffset
& 3) || (yoffset
& 3)) {
1805 _mesa_error(ctx
, GL_INVALID_VALUE
,
1806 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions
);
1809 /* size must be multiple of 4 */
1810 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
1811 _mesa_error(ctx
, GL_INVALID_VALUE
,
1812 "glCopyTexSubImage%D(width)", dimensions
);
1815 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
1816 _mesa_error(ctx
, GL_INVALID_VALUE
,
1817 "glCopyTexSubImage%D(height)", dimensions
);
1822 if (teximage
->IntFormat
== GL_YCBCR_MESA
) {
1823 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
1827 /* if we get here, the parameters are OK */
1833 * Get texture image. Called by glGetTexImage.
1835 * \param target texture target.
1836 * \param level image level.
1837 * \param format pixel data format for returned image.
1838 * \param type pixel data type for returned image.
1839 * \param pixels returned pixel data.
1842 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1843 GLenum type
, GLvoid
*pixels
)
1845 const struct gl_texture_unit
*texUnit
;
1846 const struct gl_texture_object
*texObj
;
1847 const struct gl_texture_image
*texImage
;
1848 GLint maxLevels
= 0;
1849 GET_CURRENT_CONTEXT(ctx
);
1850 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1852 texUnit
= &(ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
]);
1853 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1854 if (!texObj
|| is_proxy_target(target
)) {
1855 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
1859 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1860 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
1862 if (level
< 0 || level
>= maxLevels
) {
1863 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
1867 if (_mesa_sizeof_packed_type(type
) <= 0) {
1868 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
1872 if (_mesa_components_in_format(format
) <= 0 ||
1873 format
== GL_STENCIL_INDEX
) {
1874 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
1878 if (!ctx
->Extensions
.EXT_paletted_texture
&& is_index_format(format
)) {
1879 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1882 if (!ctx
->Extensions
.SGIX_depth_texture
&& is_depth_format(format
)) {
1883 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1886 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& is_ycbcr_format(format
)) {
1887 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1893 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1895 /* invalid mipmap level, not an error */
1899 if (!texImage
->Data
) {
1900 /* no image data, not an error */
1904 /* Make sure the requested image format is compatible with the
1907 if (is_color_format(format
)
1908 && !is_color_format(texImage
->TexFormat
->BaseFormat
)) {
1909 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1912 else if (is_index_format(format
)
1913 && !is_index_format(texImage
->TexFormat
->BaseFormat
)) {
1914 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1917 else if (is_depth_format(format
)
1918 && !is_depth_format(texImage
->TexFormat
->BaseFormat
)) {
1919 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1922 else if (is_ycbcr_format(format
)
1923 && !is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
1924 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
1931 const GLint width
= texImage
->Width
;
1932 const GLint height
= texImage
->Height
;
1933 const GLint depth
= texImage
->Depth
;
1935 for (img
= 0; img
< depth
; img
++) {
1936 for (row
= 0; row
< height
; row
++) {
1937 /* compute destination address in client memory */
1938 GLvoid
*dest
= _mesa_image_address( &ctx
->Pack
, pixels
,
1939 width
, height
, format
, type
,
1943 if (format
== GL_COLOR_INDEX
) {
1944 GLuint indexRow
[MAX_WIDTH
];
1946 for (col
= 0; col
< width
; col
++) {
1948 (*texImage
->FetchTexelc
)(texImage
, col
, row
, img
, &indx
);
1949 indexRow
[col
] = indx
;
1951 _mesa_pack_index_span(ctx
, width
, type
, dest
,
1952 indexRow
, &ctx
->Pack
,
1953 0 /* no image transfer */);
1955 else if (format
== GL_DEPTH_COMPONENT
) {
1956 GLfloat depthRow
[MAX_WIDTH
];
1958 for (col
= 0; col
< width
; col
++) {
1959 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
,
1960 (GLvoid
*) &depthRow
[col
]);
1962 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
1963 depthRow
, &ctx
->Pack
);
1965 else if (format
== GL_YCBCR_MESA
) {
1966 /* No pixel transfer */
1967 const GLint rowstride
= texImage
->RowStride
;
1969 (const GLushort
*) texImage
->Data
+ row
* rowstride
,
1970 width
* sizeof(GLushort
));
1971 /* check for byte swapping */
1972 if ((texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR
1973 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
1974 (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR_REV
1975 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
1976 if (!ctx
->Pack
.SwapBytes
)
1977 _mesa_swap2((GLushort
*) dest
, width
);
1979 else if (ctx
->Pack
.SwapBytes
) {
1980 _mesa_swap2((GLushort
*) dest
, width
);
1984 /* general case: convert row to RGBA format */
1985 GLfloat rgba
[MAX_WIDTH
][4];
1987 for (col
= 0; col
< width
; col
++) {
1988 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
1990 _mesa_pack_rgba_span_float(ctx
, width
,
1991 (const GLfloat (*)[4]) rgba
,
1992 format
, type
, dest
, &ctx
->Pack
,
1993 0 /* no image transfer */);
2003 * Called from the API. Note that width includes the border.
2006 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2007 GLsizei width
, GLint border
, GLenum format
,
2008 GLenum type
, const GLvoid
*pixels
)
2010 GLsizei postConvWidth
= width
;
2011 GET_CURRENT_CONTEXT(ctx
);
2012 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2014 if (is_color_format(internalFormat
)) {
2015 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2018 if (target
== GL_TEXTURE_1D
) {
2019 struct gl_texture_unit
*texUnit
;
2020 struct gl_texture_object
*texObj
;
2021 struct gl_texture_image
*texImage
;
2023 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2024 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2025 return; /* error was recorded */
2028 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2029 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2030 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2033 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2036 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2037 /* free the old texture data */
2038 MESA_PBUFFER_FREE(texImage
->Data
);
2040 texImage
->Data
= NULL
;
2041 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2042 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2043 postConvWidth
, 1, 1,
2044 border
, internalFormat
);
2046 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2047 _mesa_update_state(ctx
);
2049 ASSERT(ctx
->Driver
.TexImage1D
);
2051 /* Give the texture to the driver! <pixels> may be null! */
2052 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
2053 width
, border
, format
, type
, pixels
,
2054 &ctx
->Unpack
, texObj
, texImage
);
2056 ASSERT(texImage
->TexFormat
);
2058 /* If driver didn't explicitly set this, use the defaults */
2059 if (!texImage
->FetchTexelc
)
2060 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2061 if (!texImage
->FetchTexelf
)
2062 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2063 ASSERT(texImage
->FetchTexelc
);
2064 ASSERT(texImage
->FetchTexelf
);
2067 texObj
->Complete
= GL_FALSE
;
2068 ctx
->NewState
|= _NEW_TEXTURE
;
2070 else if (target
== GL_PROXY_TEXTURE_1D
) {
2071 /* Proxy texture: check for errors and update proxy state */
2072 struct gl_texture_image
*texImage
;
2073 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2074 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2075 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2076 /* when error, clear all proxy texture image parameters */
2078 clear_teximage_fields(texImage
);
2081 /* no error, set the tex image parameters */
2083 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2084 postConvWidth
, 1, 1,
2085 border
, internalFormat
);
2086 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2087 internalFormat
, format
, type
);
2091 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2098 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2099 GLsizei width
, GLsizei height
, GLint border
,
2100 GLenum format
, GLenum type
,
2101 const GLvoid
*pixels
)
2103 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2104 GET_CURRENT_CONTEXT(ctx
);
2105 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2107 if (is_color_format(internalFormat
)) {
2108 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2112 if (target
== GL_TEXTURE_2D
||
2113 (ctx
->Extensions
.ARB_texture_cube_map
&&
2114 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2115 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2116 (ctx
->Extensions
.NV_texture_rectangle
&&
2117 target
== GL_TEXTURE_RECTANGLE_NV
)) {
2118 /* non-proxy target */
2119 struct gl_texture_unit
*texUnit
;
2120 struct gl_texture_object
*texObj
;
2121 struct gl_texture_image
*texImage
;
2123 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2124 format
, type
, 2, postConvWidth
, postConvHeight
,
2126 return; /* error was recorded */
2129 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2130 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2131 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2133 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2136 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2137 /* free the old texture data */
2138 MESA_PBUFFER_FREE(texImage
->Data
);
2140 texImage
->Data
= NULL
;
2141 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2142 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2143 postConvWidth
, postConvHeight
, 1,
2144 border
, internalFormat
);
2146 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2147 _mesa_update_state(ctx
);
2149 ASSERT(ctx
->Driver
.TexImage2D
);
2151 /* Give the texture to the driver! <pixels> may be null! */
2152 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
2153 width
, height
, border
, format
, type
, pixels
,
2154 &ctx
->Unpack
, texObj
, texImage
);
2156 ASSERT(texImage
->TexFormat
);
2158 /* If driver didn't explicitly set these, use the defaults */
2159 if (!texImage
->FetchTexelc
)
2160 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2161 if (!texImage
->FetchTexelf
)
2162 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2163 ASSERT(texImage
->FetchTexelc
);
2164 ASSERT(texImage
->FetchTexelf
);
2167 texObj
->Complete
= GL_FALSE
;
2168 ctx
->NewState
|= _NEW_TEXTURE
;
2170 else if (target
== GL_PROXY_TEXTURE_2D
||
2171 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2172 ctx
->Extensions
.ARB_texture_cube_map
) ||
2173 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2174 ctx
->Extensions
.NV_texture_rectangle
)) {
2175 /* Proxy texture: check for errors and update proxy state */
2176 struct gl_texture_image
*texImage
;
2177 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2178 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2179 format
, type
, 2, postConvWidth
, postConvHeight
,
2181 /* when error, clear all proxy texture image parameters */
2183 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[0][level
]);
2186 /* no error, set the tex image parameters */
2187 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2188 postConvWidth
, postConvHeight
, 1,
2189 border
, internalFormat
);
2190 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2191 internalFormat
, format
, type
);
2195 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2202 * Called by the API or display list executor.
2203 * Note that width and height include the border.
2206 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2207 GLsizei width
, GLsizei height
, GLsizei depth
,
2208 GLint border
, GLenum format
, GLenum type
,
2209 const GLvoid
*pixels
)
2211 GET_CURRENT_CONTEXT(ctx
);
2212 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2214 if (target
== GL_TEXTURE_3D
) {
2215 struct gl_texture_unit
*texUnit
;
2216 struct gl_texture_object
*texObj
;
2217 struct gl_texture_image
*texImage
;
2219 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2220 format
, type
, 3, width
, height
, depth
, border
)) {
2221 return; /* error was recorded */
2224 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2225 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2226 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2228 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2231 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2232 MESA_PBUFFER_FREE(texImage
->Data
);
2234 texImage
->Data
= NULL
;
2235 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2236 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2237 width
, height
, depth
,
2238 border
, internalFormat
);
2240 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2241 _mesa_update_state(ctx
);
2243 ASSERT(ctx
->Driver
.TexImage3D
);
2245 /* Give the texture to the driver! <pixels> may be null! */
2246 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, internalFormat
,
2247 width
, height
, depth
, border
, format
, type
,
2248 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2250 ASSERT(texImage
->TexFormat
);
2252 /* If driver didn't explicitly set these, use the defaults */
2253 if (!texImage
->FetchTexelc
)
2254 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel3D
;
2255 if (!texImage
->FetchTexelf
)
2256 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel3Df
;
2257 ASSERT(texImage
->FetchTexelc
);
2258 ASSERT(texImage
->FetchTexelf
);
2261 texObj
->Complete
= GL_FALSE
;
2262 ctx
->NewState
|= _NEW_TEXTURE
;
2264 else if (target
== GL_PROXY_TEXTURE_3D
) {
2265 /* Proxy texture: check for errors and update proxy state */
2266 struct gl_texture_image
*texImage
;
2267 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2268 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2269 format
, type
, 3, width
, height
, depth
, border
)) {
2270 /* when error, clear all proxy texture image parameters */
2272 clear_teximage_fields(texImage
);
2275 /* no error, set the tex image parameters */
2276 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2277 border
, internalFormat
);
2278 texImage
->TexFormat
= (*ctx
->Driver
.ChooseTextureFormat
)(ctx
,
2279 internalFormat
, format
, type
);
2283 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2290 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2291 GLsizei width
, GLsizei height
, GLsizei depth
,
2292 GLint border
, GLenum format
, GLenum type
,
2293 const GLvoid
*pixels
)
2295 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2296 depth
, border
, format
, type
, pixels
);
2302 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2303 GLint xoffset
, GLsizei width
,
2304 GLenum format
, GLenum type
,
2305 const GLvoid
*pixels
)
2307 GLsizei postConvWidth
= width
;
2308 struct gl_texture_unit
*texUnit
;
2309 struct gl_texture_object
*texObj
;
2310 struct gl_texture_image
*texImage
;
2311 GET_CURRENT_CONTEXT(ctx
);
2312 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2314 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2315 _mesa_update_state(ctx
);
2317 /* XXX should test internal format */
2318 if (is_color_format(format
)) {
2319 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2322 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2323 postConvWidth
, 1, 1, format
, type
)) {
2324 return; /* error was detected */
2327 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2328 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2329 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2332 if (width
== 0 || !pixels
)
2333 return; /* no-op, not an error */
2335 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2336 xoffset
+= texImage
->Border
;
2338 ASSERT(ctx
->Driver
.TexSubImage1D
);
2339 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
2340 format
, type
, pixels
, &ctx
->Unpack
,
2342 ctx
->NewState
|= _NEW_TEXTURE
;
2347 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2348 GLint xoffset
, GLint yoffset
,
2349 GLsizei width
, GLsizei height
,
2350 GLenum format
, GLenum type
,
2351 const GLvoid
*pixels
)
2353 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2354 struct gl_texture_unit
*texUnit
;
2355 struct gl_texture_object
*texObj
;
2356 struct gl_texture_image
*texImage
;
2357 GET_CURRENT_CONTEXT(ctx
);
2358 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2360 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2361 _mesa_update_state(ctx
);
2363 /* XXX should test internal format */
2364 if (is_color_format(format
)) {
2365 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2369 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2370 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2371 return; /* error was detected */
2374 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2375 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2376 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2379 if (width
== 0 || height
== 0 || !pixels
)
2380 return; /* no-op, not an error */
2382 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2383 xoffset
+= texImage
->Border
;
2384 yoffset
+= texImage
->Border
;
2386 ASSERT(ctx
->Driver
.TexSubImage2D
);
2387 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
2388 width
, height
, format
, type
, pixels
,
2389 &ctx
->Unpack
, texObj
, texImage
);
2390 ctx
->NewState
|= _NEW_TEXTURE
;
2396 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2397 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2398 GLsizei width
, GLsizei height
, GLsizei depth
,
2399 GLenum format
, GLenum type
,
2400 const GLvoid
*pixels
)
2402 struct gl_texture_unit
*texUnit
;
2403 struct gl_texture_object
*texObj
;
2404 struct gl_texture_image
*texImage
;
2405 GET_CURRENT_CONTEXT(ctx
);
2406 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2408 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2409 _mesa_update_state(ctx
);
2411 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2412 width
, height
, depth
, format
, type
)) {
2413 return; /* error was detected */
2416 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2417 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2418 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2421 if (width
== 0 || height
== 0 || height
== 0 || !pixels
)
2422 return; /* no-op, not an error */
2424 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2425 xoffset
+= texImage
->Border
;
2426 yoffset
+= texImage
->Border
;
2427 zoffset
+= texImage
->Border
;
2429 ASSERT(ctx
->Driver
.TexSubImage3D
);
2430 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
2431 xoffset
, yoffset
, zoffset
,
2432 width
, height
, depth
,
2433 format
, type
, pixels
,
2434 &ctx
->Unpack
, texObj
, texImage
);
2435 ctx
->NewState
|= _NEW_TEXTURE
;
2441 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2442 GLenum internalFormat
,
2444 GLsizei width
, GLint border
)
2446 struct gl_texture_unit
*texUnit
;
2447 struct gl_texture_object
*texObj
;
2448 struct gl_texture_image
*texImage
;
2449 GLsizei postConvWidth
= width
;
2450 GET_CURRENT_CONTEXT(ctx
);
2451 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2453 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2454 _mesa_update_state(ctx
);
2456 if (is_color_format(internalFormat
)) {
2457 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2460 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2461 postConvWidth
, 1, border
))
2464 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2465 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2466 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2468 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2471 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2472 /* free the old texture data */
2473 MESA_PBUFFER_FREE(texImage
->Data
);
2475 texImage
->Data
= NULL
;
2477 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2478 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2479 border
, internalFormat
);
2482 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2483 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
2484 x
, y
, width
, border
);
2486 ASSERT(texImage
->TexFormat
);
2488 /* If driver didn't explicitly set these, use the defaults */
2489 if (!texImage
->FetchTexelc
)
2490 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel1D
;
2491 if (!texImage
->FetchTexelf
)
2492 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel1Df
;
2493 ASSERT(texImage
->FetchTexelc
);
2494 ASSERT(texImage
->FetchTexelf
);
2497 texObj
->Complete
= GL_FALSE
;
2498 ctx
->NewState
|= _NEW_TEXTURE
;
2504 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2505 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2508 struct gl_texture_unit
*texUnit
;
2509 struct gl_texture_object
*texObj
;
2510 struct gl_texture_image
*texImage
;
2511 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2512 GET_CURRENT_CONTEXT(ctx
);
2513 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2515 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2516 _mesa_update_state(ctx
);
2518 if (is_color_format(internalFormat
)) {
2519 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
2523 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2524 postConvWidth
, postConvHeight
, border
))
2527 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2528 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2529 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2531 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2534 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2535 /* free the old texture data */
2536 MESA_PBUFFER_FREE(texImage
->Data
);
2538 texImage
->Data
= NULL
;
2540 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2541 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2542 postConvWidth
, postConvHeight
, 1,
2543 border
, internalFormat
);
2545 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2546 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
2547 x
, y
, width
, height
, border
);
2549 ASSERT(texImage
->TexFormat
);
2551 /* If driver didn't explicitly set these, use the defaults */
2552 if (!texImage
->FetchTexelc
)
2553 texImage
->FetchTexelc
= texImage
->TexFormat
->FetchTexel2D
;
2554 if (!texImage
->FetchTexelf
)
2555 texImage
->FetchTexelf
= texImage
->TexFormat
->FetchTexel2Df
;
2556 ASSERT(texImage
->FetchTexelc
);
2557 ASSERT(texImage
->FetchTexelf
);
2560 texObj
->Complete
= GL_FALSE
;
2561 ctx
->NewState
|= _NEW_TEXTURE
;
2567 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2568 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2570 struct gl_texture_unit
*texUnit
;
2571 struct gl_texture_image
*texImage
;
2572 GLsizei postConvWidth
= width
;
2573 GET_CURRENT_CONTEXT(ctx
);
2574 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2576 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2577 _mesa_update_state(ctx
);
2579 /* XXX should test internal format */
2580 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
2582 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
2583 xoffset
, 0, 0, postConvWidth
, 1))
2586 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2587 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2590 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2591 xoffset
+= texImage
->Border
;
2593 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2594 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
2595 ctx
->NewState
|= _NEW_TEXTURE
;
2601 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2602 GLint xoffset
, GLint yoffset
,
2603 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2605 struct gl_texture_unit
*texUnit
;
2606 struct gl_texture_image
*texImage
;
2607 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2608 GET_CURRENT_CONTEXT(ctx
);
2609 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2611 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2612 _mesa_update_state(ctx
);
2614 /* XXX should test internal format */
2615 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2617 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2618 postConvWidth
, postConvHeight
))
2621 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2622 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2625 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2626 xoffset
+= texImage
->Border
;
2627 yoffset
+= texImage
->Border
;
2629 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
2630 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
2631 xoffset
, yoffset
, x
, y
, width
, height
);
2632 ctx
->NewState
|= _NEW_TEXTURE
;
2638 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2639 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2640 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2642 struct gl_texture_unit
*texUnit
;
2643 struct gl_texture_image
*texImage
;
2644 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2645 GET_CURRENT_CONTEXT(ctx
);
2646 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2648 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2649 _mesa_update_state(ctx
);
2651 /* XXX should test internal format */
2652 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2654 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2655 zoffset
, postConvWidth
, postConvHeight
))
2658 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2659 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2662 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2663 xoffset
+= texImage
->Border
;
2664 yoffset
+= texImage
->Border
;
2665 zoffset
+= texImage
->Border
;
2667 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2668 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2669 xoffset
, yoffset
, zoffset
,
2670 x
, y
, width
, height
);
2671 ctx
->NewState
|= _NEW_TEXTURE
;
2677 /**********************************************************************/
2678 /****** Compressed Textures ******/
2679 /**********************************************************************/
2683 * Error checking for glCompressedTexImage[123]D().
2684 * \return error code or GL_NO_ERROR.
2687 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
2688 GLenum target
, GLint level
,
2689 GLenum internalFormat
, GLsizei width
,
2690 GLsizei height
, GLsizei depth
, GLint border
,
2693 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2695 if (dimensions
== 1) {
2696 /* 1D compressed textures not allowed */
2697 return GL_INVALID_ENUM
;
2699 else if (dimensions
== 2) {
2700 if (target
== GL_PROXY_TEXTURE_2D
) {
2701 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2703 else if (target
== GL_TEXTURE_2D
) {
2704 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2706 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2707 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2708 return GL_INVALID_ENUM
; /*target*/
2709 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2711 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2712 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2713 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2714 return GL_INVALID_ENUM
; /*target*/
2715 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2718 return GL_INVALID_ENUM
; /*target*/
2721 else if (dimensions
== 3) {
2722 /* 3D compressed textures not allowed */
2723 return GL_INVALID_ENUM
;
2726 maxTextureSize
= 1 << (maxLevels
- 1);
2728 if (!is_compressed_format(ctx
, internalFormat
))
2729 return GL_INVALID_ENUM
;
2732 return GL_INVALID_VALUE
;
2735 * XXX We should probably use the proxy texture error check function here.
2737 if (width
< 1 || width
> maxTextureSize
||
2738 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(width
) < 0))
2739 return GL_INVALID_VALUE
;
2741 if ((height
< 1 || height
> maxTextureSize
||
2742 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(height
) < 0))
2744 return GL_INVALID_VALUE
;
2746 if ((depth
< 1 || depth
> maxTextureSize
||
2747 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& logbase2(depth
) < 0))
2749 return GL_INVALID_VALUE
;
2751 /* For cube map, width must equal height */
2752 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2753 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
2754 return GL_INVALID_VALUE
;
2756 if (level
< 0 || level
>= maxLevels
)
2757 return GL_INVALID_VALUE
;
2759 expectedSize
= _mesa_compressed_texture_size(ctx
, width
, height
, depth
,
2761 if (expectedSize
!= imageSize
)
2762 return GL_INVALID_VALUE
;
2769 * Error checking for glCompressedTexSubImage[123]D().
2770 * \return error code or GL_NO_ERROR.
2773 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
2774 GLenum target
, GLint level
,
2775 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2776 GLsizei width
, GLsizei height
, GLsizei depth
,
2777 GLenum format
, GLsizei imageSize
)
2779 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
2781 if (dimensions
== 1) {
2782 /* 1D compressed textures not allowed */
2783 return GL_INVALID_ENUM
;
2785 else if (dimensions
== 2) {
2786 if (target
== GL_PROXY_TEXTURE_2D
) {
2787 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2789 else if (target
== GL_TEXTURE_2D
) {
2790 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2792 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
2793 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2794 return GL_INVALID_ENUM
; /*target*/
2795 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2797 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2798 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
2799 if (!ctx
->Extensions
.ARB_texture_cube_map
)
2800 return GL_INVALID_ENUM
; /*target*/
2801 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2804 return GL_INVALID_ENUM
; /*target*/
2807 else if (dimensions
== 3) {
2808 /* 3D compressed textures not allowed */
2809 return GL_INVALID_ENUM
;
2812 maxTextureSize
= 1 << (maxLevels
- 1);
2814 if (!is_compressed_format(ctx
, format
))
2815 return GL_INVALID_ENUM
;
2817 if (width
< 1 || width
> maxTextureSize
)
2818 return GL_INVALID_VALUE
;
2820 if ((height
< 1 || height
> maxTextureSize
)
2822 return GL_INVALID_VALUE
;
2824 if (level
< 0 || level
>= maxLevels
)
2825 return GL_INVALID_VALUE
;
2827 if ((xoffset
& 3) != 0 || (yoffset
& 3) != 0)
2828 return GL_INVALID_VALUE
;
2830 if ((width
& 3) != 0 && width
!= 2 && width
!= 1)
2831 return GL_INVALID_VALUE
;
2833 if ((height
& 3) != 0 && height
!= 2 && height
!= 1)
2834 return GL_INVALID_VALUE
;
2836 expectedSize
= _mesa_compressed_texture_size(ctx
, width
, height
, depth
,
2838 if (expectedSize
!= imageSize
)
2839 return GL_INVALID_VALUE
;
2847 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2848 GLenum internalFormat
, GLsizei width
,
2849 GLint border
, GLsizei imageSize
,
2852 GET_CURRENT_CONTEXT(ctx
);
2853 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2855 if (target
== GL_TEXTURE_1D
) {
2856 struct gl_texture_unit
*texUnit
;
2857 struct gl_texture_object
*texObj
;
2858 struct gl_texture_image
*texImage
;
2859 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2860 internalFormat
, width
, 1, 1, border
, imageSize
);
2862 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
2866 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2867 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2868 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2870 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
2873 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2874 MESA_PBUFFER_FREE(texImage
->Data
);
2876 texImage
->Data
= NULL
;
2878 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2879 border
, internalFormat
);
2881 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2882 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2883 internalFormat
, width
, border
,
2888 texObj
->Complete
= GL_FALSE
;
2889 ctx
->NewState
|= _NEW_TEXTURE
;
2891 else if (target
== GL_PROXY_TEXTURE_1D
) {
2892 /* Proxy texture: check for errors and update proxy state */
2893 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
2894 internalFormat
, width
, 1, 1, border
, imageSize
);
2896 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2897 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2898 internalFormat
, GL_NONE
, GL_NONE
,
2899 width
, 1, 1, border
);
2902 /* if error, clear all proxy texture image parameters */
2903 struct gl_texture_image
*texImage
;
2904 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2906 clear_teximage_fields(texImage
);
2909 /* store the teximage parameters */
2910 struct gl_texture_unit
*texUnit
;
2911 struct gl_texture_image
*texImage
;
2912 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2913 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2914 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2915 border
, internalFormat
);
2919 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
2926 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
2927 GLenum internalFormat
, GLsizei width
,
2928 GLsizei height
, GLint border
, GLsizei imageSize
,
2931 GET_CURRENT_CONTEXT(ctx
);
2932 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2934 if (target
== GL_TEXTURE_2D
||
2935 (ctx
->Extensions
.ARB_texture_cube_map
&&
2936 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2937 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
2938 struct gl_texture_unit
*texUnit
;
2939 struct gl_texture_object
*texObj
;
2940 struct gl_texture_image
*texImage
;
2941 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2942 internalFormat
, width
, height
, 1, border
, imageSize
);
2944 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
2948 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2949 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2950 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
2952 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
2955 else if (texImage
->Data
&& !texImage
->IsClientData
) {
2956 MESA_PBUFFER_FREE(texImage
->Data
);
2958 texImage
->Data
= NULL
;
2960 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2961 border
, internalFormat
);
2963 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
2964 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
2965 internalFormat
, width
, height
,
2966 border
, imageSize
, data
,
2970 texObj
->Complete
= GL_FALSE
;
2971 ctx
->NewState
|= _NEW_TEXTURE
;
2973 else if (target
== GL_PROXY_TEXTURE_2D
||
2974 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2975 ctx
->Extensions
.ARB_texture_cube_map
)) {
2976 /* Proxy texture: check for errors and update proxy state */
2977 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
2978 internalFormat
, width
, height
, 1, border
, imageSize
);
2980 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2981 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2982 internalFormat
, GL_NONE
, GL_NONE
,
2983 width
, height
, 1, border
);
2986 /* if error, clear all proxy texture image parameters */
2987 struct gl_texture_image
*texImage
;
2988 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2990 clear_teximage_fields(texImage
);
2993 /* store the teximage parameters */
2994 struct gl_texture_unit
*texUnit
;
2995 struct gl_texture_image
*texImage
;
2996 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2997 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2998 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2999 border
, internalFormat
);
3003 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3010 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3011 GLenum internalFormat
, GLsizei width
,
3012 GLsizei height
, GLsizei depth
, GLint border
,
3013 GLsizei imageSize
, const GLvoid
*data
)
3015 GET_CURRENT_CONTEXT(ctx
);
3016 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3018 if (target
== GL_TEXTURE_3D
) {
3019 struct gl_texture_unit
*texUnit
;
3020 struct gl_texture_object
*texObj
;
3021 struct gl_texture_image
*texImage
;
3022 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3023 internalFormat
, width
, height
, depth
, border
, imageSize
);
3025 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3029 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3030 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3031 texImage
= _mesa_get_tex_image(ctx
, texUnit
, target
, level
);
3033 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3036 else if (texImage
->Data
&& !texImage
->IsClientData
) {
3037 MESA_PBUFFER_FREE(texImage
->Data
);
3039 texImage
->Data
= NULL
;
3041 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, depth
,
3042 border
, internalFormat
);
3044 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3045 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
3047 width
, height
, depth
,
3048 border
, imageSize
, data
,
3052 texObj
->Complete
= GL_FALSE
;
3053 ctx
->NewState
|= _NEW_TEXTURE
;
3055 else if (target
== GL_PROXY_TEXTURE_3D
) {
3056 /* Proxy texture: check for errors and update proxy state */
3057 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3058 internalFormat
, width
, height
, depth
, border
, imageSize
);
3060 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3061 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3062 internalFormat
, GL_NONE
, GL_NONE
,
3063 width
, height
, depth
, border
);
3066 /* if error, clear all proxy texture image parameters */
3067 struct gl_texture_image
*texImage
;
3068 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3070 clear_teximage_fields(texImage
);
3073 /* store the teximage parameters */
3074 struct gl_texture_unit
*texUnit
;
3075 struct gl_texture_image
*texImage
;
3076 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3077 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3078 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3079 depth
, border
, internalFormat
);
3083 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3090 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3091 GLsizei width
, GLenum format
,
3092 GLsizei imageSize
, const GLvoid
*data
)
3094 struct gl_texture_unit
*texUnit
;
3095 struct gl_texture_object
*texObj
;
3096 struct gl_texture_image
*texImage
;
3098 GET_CURRENT_CONTEXT(ctx
);
3099 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3101 error
= compressed_subtexture_error_check(ctx
, 1, target
, level
,
3102 xoffset
, 0, 0, width
, 1, 1, format
, imageSize
);
3104 _mesa_error(ctx
, error
, "glCompressedTexSubImage1D");
3108 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3109 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3110 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3113 if ((GLint
) format
!= texImage
->IntFormat
) {
3114 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3115 "glCompressedTexSubImage1D(format)");
3119 if ((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) {
3120 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage1D(width)");
3124 if (width
== 0 || !data
)
3125 return; /* no-op, not an error */
3127 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3128 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
3130 format
, imageSize
, data
,
3133 ctx
->NewState
|= _NEW_TEXTURE
;
3138 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3139 GLint yoffset
, GLsizei width
, GLsizei height
,
3140 GLenum format
, GLsizei imageSize
,
3143 struct gl_texture_unit
*texUnit
;
3144 struct gl_texture_object
*texObj
;
3145 struct gl_texture_image
*texImage
;
3147 GET_CURRENT_CONTEXT(ctx
);
3148 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3150 error
= compressed_subtexture_error_check(ctx
, 2, target
, level
,
3151 xoffset
, yoffset
, 0, width
, height
, 1, format
, imageSize
);
3153 /* XXX proxy target? */
3154 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3158 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3159 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3160 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3163 if ((GLint
) format
!= texImage
->IntFormat
) {
3164 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3165 "glCompressedTexSubImage2D(format)");
3169 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3170 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
)) {
3171 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage2D(size)");
3175 if (width
== 0 || height
== 0 || !data
)
3176 return; /* no-op, not an error */
3178 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3179 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
3180 xoffset
, yoffset
, width
, height
,
3181 format
, imageSize
, data
,
3184 ctx
->NewState
|= _NEW_TEXTURE
;
3189 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3190 GLint yoffset
, GLint zoffset
, GLsizei width
,
3191 GLsizei height
, GLsizei depth
, GLenum format
,
3192 GLsizei imageSize
, const GLvoid
*data
)
3194 struct gl_texture_unit
*texUnit
;
3195 struct gl_texture_object
*texObj
;
3196 struct gl_texture_image
*texImage
;
3198 GET_CURRENT_CONTEXT(ctx
);
3199 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3201 error
= compressed_subtexture_error_check(ctx
, 3, target
, level
,
3202 xoffset
, yoffset
, zoffset
, width
, height
, depth
, format
, imageSize
);
3204 _mesa_error(ctx
, error
, "glCompressedTexSubImage2D");
3208 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3209 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3210 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3213 if ((GLint
) format
!= texImage
->IntFormat
) {
3214 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3215 "glCompressedTexSubImage3D(format)");
3219 if (((width
== 1 || width
== 2) && (GLuint
) width
!= texImage
->Width
) ||
3220 ((height
== 1 || height
== 2) && (GLuint
) height
!= texImage
->Height
) ||
3221 ((depth
== 1 || depth
== 2) && (GLuint
) depth
!= texImage
->Depth
)) {
3222 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage3D(size)");
3226 if (width
== 0 || height
== 0 || depth
== 0 || !data
)
3227 return; /* no-op, not an error */
3229 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3230 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
3231 xoffset
, yoffset
, zoffset
,
3232 width
, height
, depth
,
3233 format
, imageSize
, data
,
3236 ctx
->NewState
|= _NEW_TEXTURE
;
3241 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
3243 const struct gl_texture_unit
*texUnit
;
3244 const struct gl_texture_object
*texObj
;
3245 struct gl_texture_image
*texImage
;
3247 GET_CURRENT_CONTEXT(ctx
);
3248 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3250 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
3251 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
3253 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
3257 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3258 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
3260 if (level
< 0 || level
>= maxLevels
) {
3261 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3265 if (is_proxy_target(target
)) {
3266 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
3270 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
3272 /* probably invalid mipmap level */
3273 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
3277 if (!texImage
->IsCompressed
) {
3278 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
3285 /* just memcpy, no pixelstore or pixel transfer */
3286 MEMCPY(img
, texImage
->Data
, texImage
->CompressedSize
);