1 /* $Id: teximage.c,v 1.99 2001/07/13 15:44:21 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 #include "texformat.h"
44 #include "swrast/s_span.h" /* XXX SWRAST hack */
51 * Mesa's native texture datatype is GLchan. Native formats are
52 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA,
54 * Device drivers are free to implement any internal format they want.
59 static void PrintTexture(const struct gl_texture_image
*img
)
62 const GLchan
*data
= (const GLchan
*) img
->Data
;
65 printf("No texture data\n");
69 switch (img
->Format
) {
76 case GL_LUMINANCE_ALPHA
:
86 _mesa_problem(NULL
, "error in PrintTexture\n");
90 for (i
= 0; i
< img
->Height
; i
++) {
91 for (j
= 0; j
< img
->Width
; j
++) {
93 printf("%02x ", data
[0]);
95 printf("%02x%02x ", data
[0], data
[1]);
97 printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
99 printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
110 * Compute log base 2 of n.
111 * If n isn't an exact power of two return -1.
112 * If n < 0 return -1.
139 * Given an internal texture format enum or 1, 2, 3, 4 return the
140 * corresponding _base_ internal format: GL_ALPHA, GL_LUMINANCE,
141 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.
142 * Return -1 if invalid enum.
145 _mesa_base_tex_format( GLcontext
*ctx
, GLint format
)
148 * Ask the driver for the base format, if it doesn't
149 * know, it will return -1;
151 if (ctx
->Driver
.BaseCompressedTexFormat
) {
152 GLint ifmt
= (*ctx
->Driver
.BaseCompressedTexFormat
)(ctx
, format
);
172 case GL_LUMINANCE_ALPHA
:
173 case GL_LUMINANCE4_ALPHA4
:
174 case GL_LUMINANCE6_ALPHA2
:
175 case GL_LUMINANCE8_ALPHA8
:
176 case GL_LUMINANCE12_ALPHA4
:
177 case GL_LUMINANCE12_ALPHA12
:
178 case GL_LUMINANCE16_ALPHA16
:
179 return GL_LUMINANCE_ALPHA
;
207 case GL_COLOR_INDEX1_EXT
:
208 case GL_COLOR_INDEX2_EXT
:
209 case GL_COLOR_INDEX4_EXT
:
210 case GL_COLOR_INDEX8_EXT
:
211 case GL_COLOR_INDEX12_EXT
:
212 case GL_COLOR_INDEX16_EXT
:
213 return GL_COLOR_INDEX
;
214 case GL_DEPTH_COMPONENT
:
215 case GL_DEPTH_COMPONENT16_SGIX
:
216 case GL_DEPTH_COMPONENT24_SGIX
:
217 case GL_DEPTH_COMPONENT32_SGIX
:
218 if (ctx
->Extensions
.SGIX_depth_texture
)
219 return GL_DEPTH_COMPONENT
;
223 return -1; /* error */
229 * Test if the given image format is a color/rgba format. That is,
230 * not color index, depth, stencil, etc.
233 is_color_format(GLenum format
)
248 case GL_LUMINANCE_ALPHA
:
249 case GL_LUMINANCE4_ALPHA4
:
250 case GL_LUMINANCE6_ALPHA2
:
251 case GL_LUMINANCE8_ALPHA8
:
252 case GL_LUMINANCE12_ALPHA4
:
253 case GL_LUMINANCE12_ALPHA12
:
254 case GL_LUMINANCE16_ALPHA16
:
286 is_index_format(GLenum format
)
290 case GL_COLOR_INDEX1_EXT
:
291 case GL_COLOR_INDEX2_EXT
:
292 case GL_COLOR_INDEX4_EXT
:
293 case GL_COLOR_INDEX8_EXT
:
294 case GL_COLOR_INDEX12_EXT
:
295 case GL_COLOR_INDEX16_EXT
:
304 * Return GL_TRUE if internalFormat is a compressed format, return GL_FALSE
308 is_compressed_format(GLcontext
*ctx
, GLenum internalFormat
)
310 if (ctx
->Driver
.BaseCompressedTexFormat
) {
311 GLint b
= (*ctx
->Driver
.BaseCompressedTexFormat
)(ctx
, internalFormat
);
323 * Store a gl_texture_image pointer in a gl_texture_object structure
324 * according to the target and level parameters.
325 * This was basically prompted by the introduction of cube maps.
328 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
329 GLenum target
, GLint level
,
330 struct gl_texture_image
*texImage
)
338 tObj
->Image
[level
] = texImage
;
340 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
341 tObj
->Image
[level
] = texImage
;
343 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
344 tObj
->NegX
[level
] = texImage
;
346 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
347 tObj
->PosY
[level
] = texImage
;
349 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
350 tObj
->NegY
[level
] = texImage
;
352 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
353 tObj
->PosZ
[level
] = texImage
;
355 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
356 tObj
->NegZ
[level
] = texImage
;
359 _mesa_problem(NULL
, "bad target in _mesa_set_tex_image()");
367 * Return new gl_texture_image struct with all fields initialized to zero.
369 struct gl_texture_image
*
370 _mesa_alloc_texture_image( void )
372 return CALLOC_STRUCT(gl_texture_image
);
378 _mesa_free_texture_image( struct gl_texture_image
*teximage
)
380 if (teximage
->Data
) {
381 FREE( teximage
->Data
);
382 teximage
->Data
= NULL
;
389 * Return GL_TRUE if the target is a proxy target.
392 is_proxy_target(GLenum target
)
394 return (target
== GL_PROXY_TEXTURE_1D
||
395 target
== GL_PROXY_TEXTURE_2D
||
396 target
== GL_PROXY_TEXTURE_3D
||
397 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
);
402 * Given a texture unit and a texture target, return the corresponding
405 struct gl_texture_object
*
406 _mesa_select_tex_object(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
411 return texUnit
->Current1D
;
412 case GL_PROXY_TEXTURE_1D
:
413 return ctx
->Texture
.Proxy1D
;
415 return texUnit
->Current2D
;
416 case GL_PROXY_TEXTURE_2D
:
417 return ctx
->Texture
.Proxy2D
;
419 return texUnit
->Current3D
;
420 case GL_PROXY_TEXTURE_3D
:
421 return ctx
->Texture
.Proxy3D
;
422 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
423 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
424 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
425 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
426 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
427 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
428 case GL_TEXTURE_CUBE_MAP_ARB
:
429 return ctx
->Extensions
.ARB_texture_cube_map
430 ? texUnit
->CurrentCubeMap
: NULL
;
431 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
432 return ctx
->Extensions
.ARB_texture_cube_map
433 ? ctx
->Texture
.ProxyCubeMap
: NULL
;
435 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
442 * Return the texture image struct which corresponds to target and level
443 * for the given texture unit.
445 struct gl_texture_image
*
446 _mesa_select_tex_image(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
447 GLenum target
, GLint level
)
450 ASSERT(level
< MAX_TEXTURE_LEVELS
);
453 return texUnit
->Current1D
->Image
[level
];
454 case GL_PROXY_TEXTURE_1D
:
455 return ctx
->Texture
.Proxy1D
->Image
[level
];
457 return texUnit
->Current2D
->Image
[level
];
458 case GL_PROXY_TEXTURE_2D
:
459 return ctx
->Texture
.Proxy2D
->Image
[level
];
461 return texUnit
->Current3D
->Image
[level
];
462 case GL_PROXY_TEXTURE_3D
:
463 return ctx
->Texture
.Proxy3D
->Image
[level
];
464 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
465 if (ctx
->Extensions
.ARB_texture_cube_map
)
466 return texUnit
->CurrentCubeMap
->Image
[level
];
469 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
470 if (ctx
->Extensions
.ARB_texture_cube_map
)
471 return texUnit
->CurrentCubeMap
->NegX
[level
];
474 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
475 if (ctx
->Extensions
.ARB_texture_cube_map
)
476 return texUnit
->CurrentCubeMap
->PosY
[level
];
479 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
480 if (ctx
->Extensions
.ARB_texture_cube_map
)
481 return texUnit
->CurrentCubeMap
->NegY
[level
];
484 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
485 if (ctx
->Extensions
.ARB_texture_cube_map
)
486 return texUnit
->CurrentCubeMap
->PosZ
[level
];
489 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
490 if (ctx
->Extensions
.ARB_texture_cube_map
)
491 return texUnit
->CurrentCubeMap
->NegZ
[level
];
494 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
495 if (ctx
->Extensions
.ARB_texture_cube_map
)
496 return ctx
->Texture
.ProxyCubeMap
->Image
[level
];
500 _mesa_problem(ctx
, "bad target in _mesa_select_tex_image()");
508 * glTexImage[123]D can accept a NULL image pointer. In this case we
509 * create a texture image with unspecified image contents per the OpenGL
513 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
515 const GLint components
= _mesa_components_in_format(format
);
516 const GLint numPixels
= width
* height
* depth
;
517 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
520 * Let's see if anyone finds this. If glTexImage2D() is called with
521 * a NULL image pointer then load the texture image with something
522 * interesting instead of leaving it indeterminate.
525 static const char message
[8][32] = {
529 " X X XXXX XXX XXXXX ",
532 " X X XXXXX XXX X X ",
536 GLubyte
*imgPtr
= data
;
538 for (h
= 0; h
< depth
; h
++) {
539 for (i
= 0; i
< height
; i
++) {
540 GLint srcRow
= 7 - (i
% 8);
541 for (j
= 0; j
< width
; j
++) {
542 GLint srcCol
= j
% 32;
543 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
544 for (k
= 0; k
< components
; k
++) {
558 * Reset the fields of a gl_texture_image struct to zero.
559 * This is called when a proxy texture test fails, we set all the
560 * image members (except DriverData) to zero.
561 * It's also used in glTexImage[123]D as a safeguard to be sure all
562 * required fields get initialized properly by the Driver.TexImage[123]D
566 clear_teximage_fields(struct gl_texture_image
*img
)
582 img
->TexFormat
= &_mesa_null_texformat
;
583 img
->FetchTexel
= NULL
;
584 img
->IsCompressed
= 0;
585 img
->CompressedSize
= 0;
590 * Initialize basic fields of the gl_texture_image struct.
593 _mesa_init_teximage_fields(GLcontext
*ctx
,
594 struct gl_texture_image
*img
,
595 GLsizei width
, GLsizei height
, GLsizei depth
,
596 GLint border
, GLenum internalFormat
)
599 img
->Format
= _mesa_base_tex_format( ctx
, internalFormat
);
600 ASSERT(img
->Format
> 0);
601 img
->IntFormat
= internalFormat
;
602 img
->Border
= border
;
604 img
->Height
= height
;
606 img
->WidthLog2
= logbase2(width
- 2 * border
);
607 if (height
== 1) /* 1-D texture */
610 img
->HeightLog2
= logbase2(height
- 2 * border
);
611 if (depth
== 1) /* 2-D texture */
614 img
->DepthLog2
= logbase2(depth
- 2 * border
);
615 img
->Width2
= 1 << img
->WidthLog2
;
616 img
->Height2
= 1 << img
->HeightLog2
;
617 img
->Depth2
= 1 << img
->DepthLog2
;
618 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
619 img
->IsCompressed
= is_compressed_format(ctx
, internalFormat
);
625 * Test glTexImage[123]D() parameters for errors.
627 * dimensions - must be 1 or 2 or 3
628 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
631 texture_error_check( GLcontext
*ctx
, GLenum target
,
632 GLint level
, GLint internalFormat
,
633 GLenum format
, GLenum type
,
635 GLint width
, GLint height
,
636 GLint depth
, GLint border
)
640 GLint maxLevels
= 0, maxTextureSize
;
642 if (dimensions
== 1) {
643 isProxy
= (GLboolean
) (target
== GL_PROXY_TEXTURE_1D
);
644 if (target
!= GL_TEXTURE_1D
&& !isProxy
) {
645 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
648 maxLevels
= ctx
->Const
.MaxTextureLevels
;
650 else if (dimensions
== 2) {
651 isProxy
= (GLboolean
) (target
== GL_PROXY_TEXTURE_2D
||
652 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
);
653 if (target
!= GL_TEXTURE_2D
&& !isProxy
&&
654 !(ctx
->Extensions
.ARB_texture_cube_map
&&
655 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
656 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
657 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
660 if (target
== GL_PROXY_TEXTURE_2D
&& target
== GL_TEXTURE_2D
)
661 maxLevels
= ctx
->Const
.MaxTextureLevels
;
663 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
665 else if (dimensions
== 3) {
666 isProxy
= (GLboolean
) (target
== GL_PROXY_TEXTURE_3D
);
667 if (target
!= GL_TEXTURE_3D
&& !isProxy
) {
668 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
671 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
674 _mesa_problem( ctx
, "bad dims in texture_error_check" );
678 ASSERT(maxLevels
> 0);
679 maxTextureSize
= 1 << (maxLevels
- 1);
682 if (border
!= 0 && border
!= 1) {
685 sprintf(message
, "glTexImage%dD(border=%d)", dimensions
, border
);
686 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
692 if (width
< 2 * border
|| width
> 2 + maxTextureSize
693 || logbase2( width
- 2 * border
) < 0) {
696 sprintf(message
, "glTexImage%dD(width=%d)", dimensions
, width
);
697 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
703 if (dimensions
>= 2) {
704 if (height
< 2 * border
|| height
> 2 + maxTextureSize
705 || logbase2( height
- 2 * border
) < 0) {
708 sprintf(message
, "glTexImage%dD(height=%d)", dimensions
, height
);
709 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
715 /* For cube map, width must equal height */
716 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
717 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
718 if (width
!= height
) {
720 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexImage2D(width != height)");
727 if (dimensions
>= 3) {
728 if (depth
< 2 * border
|| depth
> 2 + maxTextureSize
729 || logbase2( depth
- 2 * border
) < 0) {
732 sprintf(message
, "glTexImage3D(depth=%d)", depth
);
733 _mesa_error( ctx
, GL_INVALID_VALUE
, message
);
740 if (level
< 0 || level
>= maxLevels
) {
743 sprintf(message
, "glTexImage%dD(level=%d)", dimensions
, level
);
744 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
749 /* For cube map, width must equal height */
750 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
751 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
752 if (width
!= height
) {
753 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexImage2D(width != height)");
758 iformat
= _mesa_base_tex_format( ctx
, internalFormat
);
762 sprintf(message
, "glTexImage%dD(internalFormat=0x%x)", dimensions
,
764 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
771 if (!is_compressed_format( ctx
, internalFormat
) &&
772 !_mesa_is_legal_format_and_type( format
, type
)) {
773 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
774 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
778 sprintf(message
, "glTexImage%dD(format or type)", dimensions
);
779 _mesa_error(ctx
, GL_INVALID_OPERATION
, message
);
784 /* if we get here, the parameters are OK */
791 * Test glTexSubImage[123]D() parameters for errors.
793 * dimensions - must be 1 or 2 or 3
794 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
797 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
798 GLenum target
, GLint level
,
799 GLint xoffset
, GLint yoffset
, GLint zoffset
,
800 GLint width
, GLint height
, GLint depth
,
801 GLenum format
, GLenum type
)
803 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
804 struct gl_texture_image
*destTex
;
806 GLboolean compressed
;
808 if (dimensions
== 1) {
809 if (target
!= GL_TEXTURE_1D
) {
810 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
813 maxLevels
= ctx
->Const
.MaxTextureLevels
;
815 else if (dimensions
== 2) {
816 if (ctx
->Extensions
.ARB_texture_cube_map
) {
817 if ((target
< GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
||
818 target
> GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) &&
819 target
!= GL_TEXTURE_2D
) {
820 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
824 else if (target
!= GL_TEXTURE_2D
) {
825 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
828 if (target
== GL_PROXY_TEXTURE_2D
&& target
== GL_TEXTURE_2D
)
829 maxLevels
= ctx
->Const
.MaxTextureLevels
;
831 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
833 else if (dimensions
== 3) {
834 if (target
!= GL_TEXTURE_3D
) {
835 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
838 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
841 _mesa_problem( ctx
, "bad dims in texture_error_check" );
845 ASSERT(maxLevels
> 0);
847 if (level
< 0 || level
>= maxLevels
) {
849 sprintf(message
, "glTexSubImage2D(level=%d)", level
);
850 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
856 sprintf(message
, "glTexSubImage%dD(width=%d)", dimensions
, width
);
857 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
860 if (height
< 0 && dimensions
> 1) {
862 sprintf(message
, "glTexSubImage%dD(height=%d)", dimensions
, height
);
863 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
866 if (depth
< 0 && dimensions
> 2) {
868 sprintf(message
, "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
869 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
873 destTex
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
876 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage2D");
880 if (xoffset
< -((GLint
)destTex
->Border
)) {
881 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage1/2/3D(xoffset)");
884 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
885 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage1/2/3D(xoffset+width)");
888 if (dimensions
> 1) {
889 if (yoffset
< -((GLint
)destTex
->Border
)) {
890 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage2/3D(yoffset)");
893 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
894 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage2/3D(yoffset+height)");
898 if (dimensions
> 2) {
899 if (zoffset
< -((GLint
)destTex
->Border
)) {
900 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
903 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
904 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
909 compressed
= is_compressed_format(ctx
, destTex
->IntFormat
);
911 if (!compressed
&& !_mesa_is_legal_format_and_type(format
, type
)) {
913 sprintf(message
, "glTexSubImage%dD(format or type)", dimensions
);
914 _mesa_error(ctx
, GL_INVALID_ENUM
, message
);
919 if (xoffset
!= -destTex
->Border
) {
920 _mesa_error(ctx
, GL_INVALID_OPERATION
,
921 "glTexSubImage1/2/3D(xoffset != -border");
924 if (dimensions
> 1 && yoffset
!= -destTex
->Border
) {
925 _mesa_error(ctx
, GL_INVALID_OPERATION
,
926 "glTexSubImage2/3D(yoffset != -border");
929 if (dimensions
> 2 && zoffset
!= -destTex
->Border
) {
930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
931 "glTexSubImage3D(zoffset != -border");
941 * Test glCopyTexImage[12]D() parameters for errors.
942 * Input: dimensions - must be 1 or 2 or 3
943 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
946 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
947 GLenum target
, GLint level
, GLint internalFormat
,
948 GLint width
, GLint height
, GLint border
)
951 GLint maxLevels
= 0, maxTextureSize
;
953 if (dimensions
== 1) {
954 if (target
!= GL_TEXTURE_1D
) {
955 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
958 maxLevels
= ctx
->Const
.MaxTextureLevels
;
960 else if (dimensions
== 2) {
961 if (ctx
->Extensions
.ARB_texture_cube_map
) {
962 if ((target
< GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
||
963 target
> GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) &&
964 target
!= GL_TEXTURE_2D
) {
965 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
969 else if (target
!= GL_TEXTURE_2D
) {
970 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
973 if (target
== GL_PROXY_TEXTURE_2D
&& target
== GL_TEXTURE_2D
)
974 maxLevels
= ctx
->Const
.MaxTextureLevels
;
976 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
979 ASSERT(maxLevels
> 0);
980 maxTextureSize
= 1 << (maxLevels
- 1);
983 if (border
!= 0 && border
!= 1) {
985 sprintf(message
, "glCopyTexImage%dD(border)", dimensions
);
986 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
991 if (width
< 2 * border
|| width
> 2 + maxTextureSize
992 || logbase2( width
- 2 * border
) < 0) {
994 sprintf(message
, "glCopyTexImage%dD(width=%d)", dimensions
, width
);
995 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1000 if (dimensions
>= 2) {
1001 if (height
< 2 * border
|| height
> 2 + maxTextureSize
1002 || logbase2( height
- 2 * border
) < 0) {
1004 sprintf(message
, "glCopyTexImage%dD(height=%d)", dimensions
, height
);
1005 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1010 /* For cube map, width must equal height */
1011 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1012 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1013 if (width
!= height
) {
1014 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyTexImage2D(width != height)");
1020 if (level
< 0 || level
>= maxLevels
) {
1022 sprintf(message
, "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1023 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1027 iformat
= _mesa_base_tex_format(ctx
, internalFormat
);
1030 sprintf(message
, "glCopyTexImage%dD(internalFormat)", dimensions
);
1031 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1035 /* if we get here, the parameters are OK */
1041 copytexsubimage_error_check( GLcontext
*ctx
, GLuint dimensions
,
1042 GLenum target
, GLint level
,
1043 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1044 GLsizei width
, GLsizei height
)
1046 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1047 struct gl_texture_image
*teximage
;
1048 GLint maxLevels
= 0;
1049 GLboolean compressed
;
1051 if (dimensions
== 1) {
1052 if (target
!= GL_TEXTURE_1D
) {
1053 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1056 maxLevels
= ctx
->Const
.MaxTextureLevels
;
1058 else if (dimensions
== 2) {
1059 if (ctx
->Extensions
.ARB_texture_cube_map
) {
1060 if ((target
< GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
||
1061 target
> GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) &&
1062 target
!= GL_TEXTURE_2D
) {
1063 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1067 else if (target
!= GL_TEXTURE_2D
) {
1068 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1071 if (target
== GL_PROXY_TEXTURE_2D
&& target
== GL_TEXTURE_2D
)
1072 maxLevels
= ctx
->Const
.MaxTextureLevels
;
1074 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
1076 else if (dimensions
== 3) {
1077 if (target
!= GL_TEXTURE_3D
) {
1078 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1081 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
1084 ASSERT(maxLevels
> 0);
1086 if (level
< 0 || level
>= maxLevels
) {
1088 sprintf(message
, "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1089 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1095 sprintf(message
, "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1096 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1099 if (dimensions
> 1 && height
< 0) {
1101 sprintf(message
, "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1102 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1106 teximage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1109 sprintf(message
, "glCopyTexSubImage%dD(undefined texture)", dimensions
);
1110 _mesa_error(ctx
, GL_INVALID_OPERATION
, message
);
1114 if (xoffset
< -((GLint
)teximage
->Border
)) {
1116 sprintf(message
, "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1117 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1120 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1122 sprintf(message
, "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1123 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1126 if (dimensions
> 1) {
1127 if (yoffset
< -((GLint
)teximage
->Border
)) {
1129 sprintf(message
, "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1130 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1133 /* NOTE: we're adding the border here, not subtracting! */
1134 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1136 sprintf(message
, "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1137 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1142 if (dimensions
> 2) {
1143 if (zoffset
< -((GLint
)teximage
->Border
)) {
1145 sprintf(message
, "glCopyTexSubImage%dD(zoffset)", dimensions
);
1146 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1149 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1151 sprintf(message
, "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1152 _mesa_error(ctx
, GL_INVALID_VALUE
, message
);
1157 compressed
= is_compressed_format(ctx
, teximage
->IntFormat
);
1159 if (xoffset
!= -teximage
->Border
) {
1160 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1161 "glCopyTexSubImage1/2/3D(xoffset != -border");
1164 if (dimensions
> 1 && yoffset
!= -teximage
->Border
) {
1165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1166 "glCopyTexSubImage2/3D(yoffset != -border");
1169 if (dimensions
> 2 && zoffset
!= -teximage
->Border
) {
1170 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1171 "glCopyTexSubImage3D(zoffset != -border");
1176 /* if we get here, the parameters are OK */
1183 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1184 GLenum type
, GLvoid
*pixels
)
1186 const struct gl_texture_unit
*texUnit
;
1187 const struct gl_texture_object
*texObj
;
1188 struct gl_texture_image
*texImage
;
1189 GLint maxLevels
= 0;
1190 GET_CURRENT_CONTEXT(ctx
);
1191 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1193 texUnit
= &(ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
]);
1194 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1195 if (!texObj
|| is_proxy_target(target
)) {
1196 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
1200 if (target
== GL_TEXTURE_1D
|| target
== GL_TEXTURE_2D
) {
1201 maxLevels
= ctx
->Const
.MaxTextureLevels
;
1203 else if (target
== GL_TEXTURE_3D
) {
1204 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
1207 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
1210 ASSERT(maxLevels
> 0);
1212 if (level
< 0 || level
>= maxLevels
) {
1213 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
1217 if (_mesa_sizeof_type(type
) <= 0) {
1218 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
1222 if (_mesa_components_in_format(format
) <= 0 ||
1223 format
== GL_STENCIL_INDEX
) {
1224 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
1228 if (!ctx
->Extensions
.EXT_paletted_texture
&& is_index_format(format
)) {
1229 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1232 if (!ctx
->Extensions
.SGIX_depth_texture
&& format
== GL_DEPTH_COMPONENT
) {
1233 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
1236 /* XXX what if format/type doesn't match texture format/type? */
1241 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1243 /* invalid mipmap level, not an error */
1247 if (!texImage
->Data
) {
1248 /* no image data, not an error */
1253 const GLint width
= texImage
->Width
;
1254 const GLint height
= texImage
->Height
;
1255 const GLint depth
= texImage
->Depth
;
1257 for (img
= 0; img
< depth
; img
++) {
1258 for (row
= 0; row
< height
; row
++) {
1259 /* compute destination address in client memory */
1260 GLvoid
*dest
= _mesa_image_address( &ctx
->Unpack
, pixels
,
1261 width
, height
, format
, type
,
1265 if (format
== GL_COLOR_INDEX
) {
1266 GLuint indexRow
[MAX_WIDTH
];
1268 for (col
= 0; col
< width
; col
++) {
1269 (*texImage
->FetchTexel
)(texImage
, col
, row
, img
,
1270 (GLvoid
*) &indexRow
[col
]);
1272 _mesa_pack_index_span(ctx
, width
, type
, dest
,
1273 indexRow
, &ctx
->Pack
,
1274 0 /* no image transfer */);
1276 else if (format
== GL_DEPTH_COMPONENT
) {
1277 GLfloat depthRow
[MAX_WIDTH
];
1279 for (col
= 0; col
< width
; col
++) {
1280 (*texImage
->FetchTexel
)(texImage
, col
, row
, img
,
1281 (GLvoid
*) &depthRow
[col
]);
1283 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
1284 depthRow
, &ctx
->Pack
);
1287 /* general case: convert row to RGBA format */
1288 GLchan rgba
[MAX_WIDTH
][4];
1290 for (col
= 0; col
< width
; col
++) {
1291 (*texImage
->FetchTexel
)(texImage
, col
, row
, img
,
1292 (GLvoid
*) rgba
[col
]);
1294 _mesa_pack_rgba_span(ctx
, width
, (const GLchan (*)[4])rgba
,
1295 format
, type
, dest
, &ctx
->Pack
,
1296 0 /* no image transfer */);
1306 * Called from the API. Note that width includes the border.
1309 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
1310 GLsizei width
, GLint border
, GLenum format
,
1311 GLenum type
, const GLvoid
*pixels
)
1313 GLsizei postConvWidth
= width
;
1314 GET_CURRENT_CONTEXT(ctx
);
1315 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1317 if (is_color_format(internalFormat
)) {
1318 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
1321 if (target
== GL_TEXTURE_1D
) {
1322 struct gl_texture_unit
*texUnit
;
1323 struct gl_texture_object
*texObj
;
1324 struct gl_texture_image
*texImage
;
1326 if (texture_error_check(ctx
, target
, level
, internalFormat
,
1327 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
1328 return; /* error was recorded */
1331 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1332 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1333 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1336 texImage
= _mesa_alloc_texture_image();
1337 texObj
->Image
[level
] = texImage
;
1339 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
1343 else if (texImage
->Data
) {
1344 /* free the old texture data */
1345 FREE(texImage
->Data
);
1346 texImage
->Data
= NULL
;
1348 clear_teximage_fields(texImage
); /* not really needed, but helpful */
1349 _mesa_init_teximage_fields(ctx
, texImage
, postConvWidth
, 1, 1,
1350 border
, internalFormat
);
1352 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1353 _mesa_update_state(ctx
);
1355 ASSERT(ctx
->Driver
.TexImage1D
);
1357 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
1358 width
, border
, format
, type
, pixels
,
1359 &ctx
->Unpack
, texObj
, texImage
);
1362 GLubyte
*dummy
= make_null_texture(width
, 1, 1, format
);
1364 (*ctx
->Driver
.TexImage1D
)(ctx
, target
, level
, internalFormat
,
1366 format
, GL_UNSIGNED_BYTE
, dummy
,
1367 &_mesa_native_packing
, texObj
, texImage
);
1372 ASSERT(texImage
->TexFormat
);
1373 if (!texImage
->FetchTexel
) {
1374 /* If driver didn't explicitly set this, use the default */
1375 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel1D
;
1377 ASSERT(texImage
->FetchTexel
);
1379 if (texImage
->IsCompressed
) {
1380 ASSERT(texImage
->CompressedSize
> 0);
1384 texObj
->Complete
= GL_FALSE
;
1385 ctx
->NewState
|= _NEW_TEXTURE
;
1387 else if (target
== GL_PROXY_TEXTURE_1D
) {
1388 /* Proxy texture: check for errors and update proxy state */
1389 GLenum error
= texture_error_check(ctx
, target
, level
, internalFormat
,
1391 postConvWidth
, 1, 1, border
);
1393 struct gl_texture_unit
*texUnit
;
1394 struct gl_texture_image
*texImage
;
1395 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1396 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1397 _mesa_init_teximage_fields(ctx
, texImage
, postConvWidth
, 1, 1,
1398 border
, internalFormat
);
1399 ASSERT(ctx
->Driver
.TestProxyTexImage
);
1400 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
1401 internalFormat
, format
, type
,
1402 postConvWidth
, 1, 1, border
);
1405 /* if error, clear all proxy texture image parameters */
1406 if (level
>= 0 && level
< ctx
->Const
.MaxTextureLevels
) {
1407 clear_teximage_fields(ctx
->Texture
.Proxy1D
->Image
[level
]);
1412 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1419 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
1420 GLsizei width
, GLsizei height
, GLint border
,
1421 GLenum format
, GLenum type
,
1422 const GLvoid
*pixels
)
1424 GLsizei postConvWidth
= width
, postConvHeight
= height
;
1425 GET_CURRENT_CONTEXT(ctx
);
1426 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1428 if (is_color_format(internalFormat
)) {
1429 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
1433 if (target
== GL_TEXTURE_2D
||
1434 (ctx
->Extensions
.ARB_texture_cube_map
&&
1435 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1436 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1437 /* non-proxy target */
1438 struct gl_texture_unit
*texUnit
;
1439 struct gl_texture_object
*texObj
;
1440 struct gl_texture_image
*texImage
;
1442 if (texture_error_check(ctx
, target
, level
, internalFormat
,
1443 format
, type
, 2, postConvWidth
, postConvHeight
,
1445 return; /* error was recorded */
1448 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1449 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1450 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1453 texImage
= _mesa_alloc_texture_image();
1454 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
1456 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
1460 else if (texImage
->Data
) {
1461 /* free the old texture data */
1462 FREE(texImage
->Data
);
1463 texImage
->Data
= NULL
;
1465 clear_teximage_fields(texImage
); /* not really needed, but helpful */
1466 _mesa_init_teximage_fields(ctx
, texImage
, postConvWidth
, postConvHeight
,
1467 1, border
, internalFormat
);
1469 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1470 _mesa_update_state(ctx
);
1472 ASSERT(ctx
->Driver
.TexImage2D
);
1474 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
1475 width
, height
, border
, format
, type
, pixels
,
1476 &ctx
->Unpack
, texObj
, texImage
);
1479 GLubyte
*dummy
= make_null_texture(width
, height
, 1, format
);
1481 (*ctx
->Driver
.TexImage2D
)(ctx
, target
, level
, internalFormat
,
1482 width
, height
, border
,
1483 format
, GL_UNSIGNED_BYTE
, dummy
,
1484 &_mesa_native_packing
, texObj
, texImage
);
1489 ASSERT(texImage
->TexFormat
);
1490 if (!texImage
->FetchTexel
) {
1491 /* If driver didn't explicitly set this, use the default */
1492 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel2D
;
1494 ASSERT(texImage
->FetchTexel
);
1496 if (texImage
->IsCompressed
) {
1497 ASSERT(texImage
->CompressedSize
> 0);
1501 texObj
->Complete
= GL_FALSE
;
1502 ctx
->NewState
|= _NEW_TEXTURE
;
1504 else if (target
== GL_PROXY_TEXTURE_2D
) {
1505 /* Proxy texture: check for errors and update proxy state */
1506 GLenum error
= texture_error_check(ctx
, target
, level
, internalFormat
,
1508 postConvWidth
, postConvHeight
, 1, border
);
1510 struct gl_texture_unit
*texUnit
;
1511 struct gl_texture_image
*texImage
;
1512 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1513 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1514 _mesa_init_teximage_fields(ctx
, texImage
, postConvWidth
,
1515 postConvHeight
, 1, border
, internalFormat
);
1516 ASSERT(ctx
->Driver
.TestProxyTexImage
);
1517 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
1518 internalFormat
, format
, type
,
1519 postConvWidth
, postConvHeight
, 1, border
);
1522 /* if error, clear all proxy texture image parameters */
1523 const GLint maxLevels
= (target
== GL_PROXY_TEXTURE_2D
) ?
1524 ctx
->Const
.MaxTextureLevels
: ctx
->Const
.MaxCubeTextureLevels
;
1525 if (level
>= 0 && level
< maxLevels
) {
1526 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[level
]);
1531 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
1538 * Called by the API or display list executor.
1539 * Note that width and height include the border.
1542 _mesa_TexImage3D( GLenum target
, GLint level
, GLenum internalFormat
,
1543 GLsizei width
, GLsizei height
, GLsizei depth
,
1544 GLint border
, GLenum format
, GLenum type
,
1545 const GLvoid
*pixels
)
1547 GET_CURRENT_CONTEXT(ctx
);
1548 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1550 if (target
== GL_TEXTURE_3D
) {
1551 struct gl_texture_unit
*texUnit
;
1552 struct gl_texture_object
*texObj
;
1553 struct gl_texture_image
*texImage
;
1555 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
1556 format
, type
, 3, width
, height
, depth
, border
)) {
1557 return; /* error was recorded */
1560 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1561 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1562 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1565 texImage
= _mesa_alloc_texture_image();
1566 texObj
->Image
[level
] = texImage
;
1568 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
1572 else if (texImage
->Data
) {
1573 FREE(texImage
->Data
);
1574 texImage
->Data
= NULL
;
1576 clear_teximage_fields(texImage
); /* not really needed, but helpful */
1577 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
, border
,
1580 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1581 _mesa_update_state(ctx
);
1583 ASSERT(ctx
->Driver
.TexImage3D
);
1585 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
, (GLint
) internalFormat
,
1586 width
, height
, depth
, border
,
1587 format
, type
, pixels
,
1588 &ctx
->Unpack
, texObj
, texImage
);
1591 GLubyte
*dummy
= make_null_texture(width
, height
, depth
, format
);
1593 (*ctx
->Driver
.TexImage3D
)(ctx
, target
, level
,
1594 (GLint
) internalFormat
,
1595 width
, height
, depth
, border
,
1596 format
, GL_UNSIGNED_BYTE
, dummy
,
1597 &_mesa_native_packing
, texObj
, texImage
);
1602 ASSERT(texImage
->TexFormat
);
1603 if (!texImage
->FetchTexel
) {
1604 /* If driver didn't explicitly set this, use the default */
1605 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel3D
;
1607 ASSERT(texImage
->FetchTexel
);
1609 if (texImage
->IsCompressed
) {
1610 ASSERT(texImage
->CompressedSize
> 0);
1614 texObj
->Complete
= GL_FALSE
;
1615 ctx
->NewState
|= _NEW_TEXTURE
;
1617 else if (target
== GL_PROXY_TEXTURE_3D
) {
1618 /* Proxy texture: check for errors and update proxy state */
1619 GLenum error
= texture_error_check(ctx
, target
, level
, internalFormat
,
1620 format
, type
, 3, width
, height
, depth
, border
);
1622 struct gl_texture_unit
*texUnit
;
1623 struct gl_texture_image
*texImage
;
1624 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1625 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1626 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
1627 border
, internalFormat
);
1628 ASSERT(ctx
->Driver
.TestProxyTexImage
);
1629 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
1630 internalFormat
, format
, type
,
1631 width
, height
, depth
, border
);
1634 /* if error, clear all proxy texture image parameters */
1635 if (level
>= 0 && level
< ctx
->Const
.Max3DTextureLevels
) {
1636 clear_teximage_fields(ctx
->Texture
.Proxy3D
->Image
[level
]);
1641 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1648 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
1649 GLsizei width
, GLsizei height
, GLsizei depth
,
1650 GLint border
, GLenum format
, GLenum type
,
1651 const GLvoid
*pixels
)
1653 _mesa_TexImage3D(target
, level
, internalFormat
, width
, height
,
1654 depth
, border
, format
, type
, pixels
);
1660 _mesa_TexSubImage1D( GLenum target
, GLint level
,
1661 GLint xoffset
, GLsizei width
,
1662 GLenum format
, GLenum type
,
1663 const GLvoid
*pixels
)
1665 GLsizei postConvWidth
= width
;
1666 struct gl_texture_unit
*texUnit
;
1667 struct gl_texture_object
*texObj
;
1668 struct gl_texture_image
*texImage
;
1669 GET_CURRENT_CONTEXT(ctx
);
1670 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1672 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1673 _mesa_update_state(ctx
);
1675 /* XXX should test internal format */
1676 if (is_color_format(format
)) {
1677 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
1680 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
1681 postConvWidth
, 1, 1, format
, type
)) {
1682 return; /* error was detected */
1685 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1686 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1687 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1690 if (width
== 0 || !pixels
)
1691 return; /* no-op, not an error */
1693 /* If we have a border, xoffset=-1 is legal. Bias by border width */
1694 xoffset
+= texImage
->Border
;
1696 ASSERT(ctx
->Driver
.TexSubImage1D
);
1697 (*ctx
->Driver
.TexSubImage1D
)(ctx
, target
, level
, xoffset
, width
,
1698 format
, type
, pixels
, &ctx
->Unpack
,
1700 ctx
->NewState
|= _NEW_TEXTURE
;
1705 _mesa_TexSubImage2D( GLenum target
, GLint level
,
1706 GLint xoffset
, GLint yoffset
,
1707 GLsizei width
, GLsizei height
,
1708 GLenum format
, GLenum type
,
1709 const GLvoid
*pixels
)
1711 GLsizei postConvWidth
= width
, postConvHeight
= height
;
1712 struct gl_texture_unit
*texUnit
;
1713 struct gl_texture_object
*texObj
;
1714 struct gl_texture_image
*texImage
;
1715 GET_CURRENT_CONTEXT(ctx
);
1716 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1718 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1719 _mesa_update_state(ctx
);
1721 /* XXX should test internal format */
1722 if (is_color_format(format
)) {
1723 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
1727 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1728 postConvWidth
, postConvHeight
, 1, format
, type
)) {
1729 return; /* error was detected */
1732 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1733 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1734 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1737 if (width
== 0 || height
== 0 || !pixels
)
1738 return; /* no-op, not an error */
1740 /* If we have a border, xoffset=-1 is legal. Bias by border width */
1741 xoffset
+= texImage
->Border
;
1742 yoffset
+= texImage
->Border
;
1744 ASSERT(ctx
->Driver
.TexSubImage2D
);
1745 (*ctx
->Driver
.TexSubImage2D
)(ctx
, target
, level
, xoffset
, yoffset
,
1746 width
, height
, format
, type
, pixels
,
1747 &ctx
->Unpack
, texObj
, texImage
);
1748 ctx
->NewState
|= _NEW_TEXTURE
;
1754 _mesa_TexSubImage3D( GLenum target
, GLint level
,
1755 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1756 GLsizei width
, GLsizei height
, GLsizei depth
,
1757 GLenum format
, GLenum type
,
1758 const GLvoid
*pixels
)
1760 struct gl_texture_unit
*texUnit
;
1761 struct gl_texture_object
*texObj
;
1762 struct gl_texture_image
*texImage
;
1763 GET_CURRENT_CONTEXT(ctx
);
1764 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1766 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1767 _mesa_update_state(ctx
);
1769 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1770 width
, height
, depth
, format
, type
)) {
1771 return; /* error was detected */
1774 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1775 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1776 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1779 if (width
== 0 || height
== 0 || height
== 0 || !pixels
)
1780 return; /* no-op, not an error */
1782 /* If we have a border, xoffset=-1 is legal. Bias by border width */
1783 xoffset
+= texImage
->Border
;
1784 yoffset
+= texImage
->Border
;
1785 zoffset
+= texImage
->Border
;
1787 ASSERT(ctx
->Driver
.TexSubImage3D
);
1788 (*ctx
->Driver
.TexSubImage3D
)(ctx
, target
, level
,
1789 xoffset
, yoffset
, zoffset
,
1790 width
, height
, depth
,
1791 format
, type
, pixels
,
1792 &ctx
->Unpack
, texObj
, texImage
);
1793 ctx
->NewState
|= _NEW_TEXTURE
;
1799 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
1800 GLenum internalFormat
,
1802 GLsizei width
, GLint border
)
1804 struct gl_texture_unit
*texUnit
;
1805 struct gl_texture_object
*texObj
;
1806 struct gl_texture_image
*texImage
;
1807 GLsizei postConvWidth
= width
;
1808 GET_CURRENT_CONTEXT(ctx
);
1809 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1811 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1812 _mesa_update_state(ctx
);
1814 if (is_color_format(internalFormat
)) {
1815 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
1818 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
1819 postConvWidth
, 1, border
))
1822 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1823 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1824 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1826 texImage
= _mesa_alloc_texture_image();
1827 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
1829 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
1833 else if (texImage
->Data
) {
1834 /* free the old texture data */
1835 FREE(texImage
->Data
);
1836 texImage
->Data
= NULL
;
1839 clear_teximage_fields(texImage
); /* not really needed, but helpful */
1840 _mesa_init_teximage_fields(ctx
, texImage
, postConvWidth
, 1, 1,
1841 border
, internalFormat
);
1844 ASSERT(ctx
->Driver
.CopyTexImage1D
);
1845 (*ctx
->Driver
.CopyTexImage1D
)(ctx
, target
, level
, internalFormat
,
1846 x
, y
, width
, border
);
1848 ASSERT(texImage
->TexFormat
);
1849 if (!texImage
->FetchTexel
) {
1850 /* If driver didn't explicitly set this, use the default */
1851 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel1D
;
1853 ASSERT(texImage
->FetchTexel
);
1856 texObj
->Complete
= GL_FALSE
;
1857 ctx
->NewState
|= _NEW_TEXTURE
;
1863 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
1864 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1867 struct gl_texture_unit
*texUnit
;
1868 struct gl_texture_object
*texObj
;
1869 struct gl_texture_image
*texImage
;
1870 GLsizei postConvWidth
= width
, postConvHeight
= height
;
1871 GET_CURRENT_CONTEXT(ctx
);
1872 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1874 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1875 _mesa_update_state(ctx
);
1877 if (is_color_format(internalFormat
)) {
1878 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
,
1882 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
1883 postConvWidth
, postConvHeight
, border
))
1886 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1887 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1888 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1890 texImage
= _mesa_alloc_texture_image();
1891 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
1893 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
1897 else if (texImage
->Data
) {
1898 /* free the old texture data */
1899 FREE(texImage
->Data
);
1900 texImage
->Data
= NULL
;
1903 clear_teximage_fields(texImage
); /* not really needed, but helpful */
1904 _mesa_init_teximage_fields(ctx
, texImage
, postConvWidth
, postConvHeight
, 1,
1905 border
, internalFormat
);
1907 ASSERT(ctx
->Driver
.CopyTexImage2D
);
1908 (*ctx
->Driver
.CopyTexImage2D
)(ctx
, target
, level
, internalFormat
,
1909 x
, y
, width
, height
, border
);
1911 ASSERT(texImage
->TexFormat
);
1912 if (!texImage
->FetchTexel
) {
1913 /* If driver didn't explicitly set this, use the default */
1914 texImage
->FetchTexel
= texImage
->TexFormat
->FetchTexel2D
;
1916 ASSERT(texImage
->FetchTexel
);
1919 texObj
->Complete
= GL_FALSE
;
1920 ctx
->NewState
|= _NEW_TEXTURE
;
1926 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
1927 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1929 struct gl_texture_unit
*texUnit
;
1930 struct gl_texture_object
*texObj
;
1931 struct gl_texture_image
*texImage
;
1932 GLsizei postConvWidth
= width
;
1933 GET_CURRENT_CONTEXT(ctx
);
1934 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1936 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1937 _mesa_update_state(ctx
);
1939 /* XXX should test internal format */
1940 _mesa_adjust_image_for_convolution(ctx
, 1, &postConvWidth
, NULL
);
1942 if (copytexsubimage_error_check(ctx
, 1, target
, level
,
1943 xoffset
, 0, 0, postConvWidth
, 1))
1946 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1947 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1948 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1950 /* If we have a border, xoffset=-1 is legal. Bias by border width */
1951 xoffset
+= texImage
->Border
;
1953 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
1954 (*ctx
->Driver
.CopyTexSubImage1D
)(ctx
, target
, level
, xoffset
, x
, y
, width
);
1955 ctx
->NewState
|= _NEW_TEXTURE
;
1961 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
1962 GLint xoffset
, GLint yoffset
,
1963 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1965 struct gl_texture_unit
*texUnit
;
1966 struct gl_texture_object
*texObj
;
1967 struct gl_texture_image
*texImage
;
1968 GLsizei postConvWidth
= width
, postConvHeight
= height
;
1969 GET_CURRENT_CONTEXT(ctx
);
1970 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1972 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
1973 _mesa_update_state(ctx
);
1975 /* XXX should test internal format */
1976 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
1978 if (copytexsubimage_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1979 postConvWidth
, postConvHeight
))
1982 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1983 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
1984 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
1986 /* If we have a border, xoffset=-1 is legal. Bias by border width */
1987 xoffset
+= texImage
->Border
;
1988 yoffset
+= texImage
->Border
;
1990 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
1991 (*ctx
->Driver
.CopyTexSubImage2D
)(ctx
, target
, level
,
1992 xoffset
, yoffset
, x
, y
, width
, height
);
1993 ctx
->NewState
|= _NEW_TEXTURE
;
1999 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2000 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2001 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2003 struct gl_texture_unit
*texUnit
;
2004 struct gl_texture_object
*texObj
;
2005 struct gl_texture_image
*texImage
;
2006 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2007 GET_CURRENT_CONTEXT(ctx
);
2008 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2010 if (ctx
->NewState
& _IMAGE_NEW_TRANSFER_STATE
)
2011 _mesa_update_state(ctx
);
2013 /* XXX should test internal format */
2014 _mesa_adjust_image_for_convolution(ctx
, 2, &postConvWidth
, &postConvHeight
);
2016 if (copytexsubimage_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
,
2017 zoffset
, postConvWidth
, postConvHeight
))
2020 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2021 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2022 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2024 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2025 xoffset
+= texImage
->Border
;
2026 yoffset
+= texImage
->Border
;
2027 zoffset
+= texImage
->Border
;
2029 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
2030 (*ctx
->Driver
.CopyTexSubImage3D
)(ctx
, target
, level
,
2031 xoffset
, yoffset
, zoffset
,
2032 x
, y
, width
, height
);
2033 ctx
->NewState
|= _NEW_TEXTURE
;
2039 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
2040 GLenum internalFormat
, GLsizei width
,
2041 GLint border
, GLsizei imageSize
,
2044 GET_CURRENT_CONTEXT(ctx
);
2045 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2047 switch (internalFormat
) {
2048 case GL_COMPRESSED_ALPHA_ARB
:
2049 case GL_COMPRESSED_LUMINANCE_ARB
:
2050 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
2051 case GL_COMPRESSED_INTENSITY_ARB
:
2052 case GL_COMPRESSED_RGB_ARB
:
2053 case GL_COMPRESSED_RGBA_ARB
:
2054 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1DARB");
2057 /* silence compiler warning */
2061 if (target
== GL_TEXTURE_1D
) {
2062 struct gl_texture_unit
*texUnit
;
2063 struct gl_texture_object
*texObj
;
2064 struct gl_texture_image
*texImage
;
2066 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2067 GL_NONE
, GL_NONE
, 1, width
, 1, 1, border
)) {
2068 return; /* error in texture image was detected */
2071 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2072 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2073 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2076 texImage
= _mesa_alloc_texture_image();
2077 texObj
->Image
[level
] = texImage
;
2079 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1DARB");
2083 else if (texImage
->Data
) {
2084 FREE(texImage
->Data
);
2085 texImage
->Data
= NULL
;
2088 _mesa_init_teximage_fields(ctx
, texImage
, width
, 1, 1,
2089 border
, internalFormat
);
2091 if (ctx
->Extensions
.ARB_texture_compression
) {
2092 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
2093 (*ctx
->Driver
.CompressedTexImage1D
)(ctx
, target
, level
,
2094 internalFormat
, width
, border
,
2097 ASSERT(texImage
->CompressedSize
> 0); /* sanity */
2101 texObj
->Complete
= GL_FALSE
;
2102 ctx
->NewState
|= _NEW_TEXTURE
;
2104 else if (target
== GL_PROXY_TEXTURE_1D
) {
2105 /* Proxy texture: check for errors and update proxy state */
2106 GLenum error
= texture_error_check(ctx
, target
, level
, internalFormat
,
2107 GL_NONE
, GL_NONE
, 1, width
, 1, 1, border
);
2109 struct gl_texture_unit
*texUnit
;
2110 struct gl_texture_image
*texImage
;
2111 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2112 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2113 _mesa_init_teximage_fields(ctx
, texImage
, width
, 1, 1,
2114 border
, internalFormat
);
2115 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2116 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2117 internalFormat
, GL_NONE
, GL_NONE
,
2118 width
, 1, 1, border
);
2121 /* if error, clear all proxy texture image parameters */
2122 if (level
>= 0 && level
< ctx
->Const
.MaxTextureLevels
) {
2123 clear_teximage_fields(ctx
->Texture
.Proxy1D
->Image
[level
]);
2128 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1DARB(target)");
2135 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
2136 GLenum internalFormat
, GLsizei width
,
2137 GLsizei height
, GLint border
, GLsizei imageSize
,
2140 GET_CURRENT_CONTEXT(ctx
);
2141 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2143 switch (internalFormat
) {
2144 case GL_COMPRESSED_ALPHA_ARB
:
2145 case GL_COMPRESSED_LUMINANCE_ARB
:
2146 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
2147 case GL_COMPRESSED_INTENSITY_ARB
:
2148 case GL_COMPRESSED_RGB_ARB
:
2149 case GL_COMPRESSED_RGBA_ARB
:
2150 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2DARB");
2153 /* silence compiler warning */
2157 if (target
== GL_TEXTURE_2D
||
2158 (ctx
->Extensions
.ARB_texture_cube_map
&&
2159 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2160 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
2161 struct gl_texture_unit
*texUnit
;
2162 struct gl_texture_object
*texObj
;
2163 struct gl_texture_image
*texImage
;
2165 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2166 GL_NONE
, GL_NONE
, 1, width
, height
, 1, border
)) {
2167 return; /* error in texture image was detected */
2170 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2171 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2172 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2175 texImage
= _mesa_alloc_texture_image();
2176 texObj
->Image
[level
] = texImage
;
2178 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2DARB");
2182 else if (texImage
->Data
) {
2183 FREE(texImage
->Data
);
2184 texImage
->Data
= NULL
;
2187 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1, border
,
2190 if (ctx
->Extensions
.ARB_texture_compression
) {
2191 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
2192 (*ctx
->Driver
.CompressedTexImage2D
)(ctx
, target
, level
,
2193 internalFormat
, width
, height
,
2194 border
, imageSize
, data
,
2196 ASSERT(texImage
->CompressedSize
> 0); /* sanity */
2200 texObj
->Complete
= GL_FALSE
;
2201 ctx
->NewState
|= _NEW_TEXTURE
;
2203 else if (target
== GL_PROXY_TEXTURE_2D
) {
2204 /* Proxy texture: check for errors and update proxy state */
2205 GLenum error
= texture_error_check(ctx
, target
, level
, internalFormat
,
2206 GL_NONE
, GL_NONE
, 2, width
, height
, 1, border
);
2208 struct gl_texture_unit
*texUnit
;
2209 struct gl_texture_image
*texImage
;
2210 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2211 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2212 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
2213 border
, internalFormat
);
2214 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2215 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2216 internalFormat
, GL_NONE
, GL_NONE
,
2217 width
, height
, 1, border
);
2220 /* if error, clear all proxy texture image parameters */
2221 const GLint maxLevels
= (target
== GL_PROXY_TEXTURE_2D
) ?
2222 ctx
->Const
.MaxTextureLevels
: ctx
->Const
.MaxCubeTextureLevels
;
2223 if (level
>= 0 && level
< maxLevels
) {
2224 clear_teximage_fields(ctx
->Texture
.Proxy2D
->Image
[level
]);
2229 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2DARB(target)");
2236 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
2237 GLenum internalFormat
, GLsizei width
,
2238 GLsizei height
, GLsizei depth
, GLint border
,
2239 GLsizei imageSize
, const GLvoid
*data
)
2241 GET_CURRENT_CONTEXT(ctx
);
2242 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2244 switch (internalFormat
) {
2245 case GL_COMPRESSED_ALPHA_ARB
:
2246 case GL_COMPRESSED_LUMINANCE_ARB
:
2247 case GL_COMPRESSED_LUMINANCE_ALPHA_ARB
:
2248 case GL_COMPRESSED_INTENSITY_ARB
:
2249 case GL_COMPRESSED_RGB_ARB
:
2250 case GL_COMPRESSED_RGBA_ARB
:
2251 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3DARB");
2254 /* silence compiler warning */
2258 if (target
== GL_TEXTURE_3D
) {
2259 struct gl_texture_unit
*texUnit
;
2260 struct gl_texture_object
*texObj
;
2261 struct gl_texture_image
*texImage
;
2263 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2264 GL_NONE
, GL_NONE
, 1, width
, height
, depth
, border
)) {
2265 return; /* error in texture image was detected */
2268 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2269 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2270 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2273 texImage
= _mesa_alloc_texture_image();
2274 texObj
->Image
[level
] = texImage
;
2276 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3DARB");
2280 else if (texImage
->Data
) {
2281 FREE(texImage
->Data
);
2282 texImage
->Data
= NULL
;
2285 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
, border
,
2288 if (ctx
->Extensions
.ARB_texture_compression
) {
2289 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
2290 (*ctx
->Driver
.CompressedTexImage3D
)(ctx
, target
, level
,
2292 width
, height
, depth
,
2293 border
, imageSize
, data
,
2295 ASSERT(texImage
->CompressedSize
> 0); /* sanity */
2299 texObj
->Complete
= GL_FALSE
;
2300 ctx
->NewState
|= _NEW_TEXTURE
;
2302 else if (target
== GL_PROXY_TEXTURE_3D
) {
2303 /* Proxy texture: check for errors and update proxy state */
2304 GLenum error
= texture_error_check(ctx
, target
, level
, internalFormat
,
2305 GL_NONE
, GL_NONE
, 1, width
, height
, depth
, border
);
2307 struct gl_texture_unit
*texUnit
;
2308 struct gl_texture_image
*texImage
;
2309 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2310 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2311 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2312 border
, internalFormat
);
2313 ASSERT(ctx
->Driver
.TestProxyTexImage
);
2314 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
2315 internalFormat
, GL_NONE
, GL_NONE
,
2316 width
, height
, depth
, border
);
2319 /* if error, clear all proxy texture image parameters */
2320 if (level
>= 0 && level
< ctx
->Const
.Max3DTextureLevels
) {
2321 clear_teximage_fields(ctx
->Texture
.Proxy3D
->Image
[level
]);
2326 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3DARB(target)");
2333 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
2334 GLsizei width
, GLenum format
,
2335 GLsizei imageSize
, const GLvoid
*data
)
2337 struct gl_texture_unit
*texUnit
;
2338 struct gl_texture_object
*texObj
;
2339 struct gl_texture_image
*texImage
;
2340 GET_CURRENT_CONTEXT(ctx
);
2341 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2343 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2344 width
, 1, 1, format
, GL_NONE
)) {
2345 return; /* error was detected */
2348 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2349 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2350 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2353 if (width
== 0 || !data
)
2354 return; /* no-op, not an error */
2356 if (ctx
->Driver
.CompressedTexSubImage1D
) {
2357 (*ctx
->Driver
.CompressedTexSubImage1D
)(ctx
, target
, level
,
2359 format
, imageSize
, data
,
2362 ctx
->NewState
|= _NEW_TEXTURE
;
2367 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
2368 GLint yoffset
, GLsizei width
, GLsizei height
,
2369 GLenum format
, GLsizei imageSize
,
2372 struct gl_texture_unit
*texUnit
;
2373 struct gl_texture_object
*texObj
;
2374 struct gl_texture_image
*texImage
;
2375 GET_CURRENT_CONTEXT(ctx
);
2376 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2378 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2379 width
, height
, 1, format
, GL_NONE
)) {
2380 return; /* error was detected */
2383 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2384 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2385 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2388 if (width
== 0 || height
== 0 || !data
)
2389 return; /* no-op, not an error */
2391 if (ctx
->Driver
.CompressedTexSubImage2D
) {
2392 (*ctx
->Driver
.CompressedTexSubImage2D
)(ctx
, target
, level
,
2393 xoffset
, yoffset
, width
, height
,
2394 format
, imageSize
, data
,
2397 ctx
->NewState
|= _NEW_TEXTURE
;
2402 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
2403 GLint yoffset
, GLint zoffset
, GLsizei width
,
2404 GLsizei height
, GLsizei depth
, GLenum format
,
2405 GLsizei imageSize
, const GLvoid
*data
)
2407 struct gl_texture_unit
*texUnit
;
2408 struct gl_texture_object
*texObj
;
2409 struct gl_texture_image
*texImage
;
2410 GET_CURRENT_CONTEXT(ctx
);
2411 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2413 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2414 width
, height
, depth
, format
, GL_NONE
)) {
2415 return; /* error was detected */
2418 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2419 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2420 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2423 if (width
== 0 || height
== 0 || depth
== 0 || !data
)
2424 return; /* no-op, not an error */
2426 if (ctx
->Driver
.CompressedTexSubImage3D
) {
2427 (*ctx
->Driver
.CompressedTexSubImage3D
)(ctx
, target
, level
,
2428 xoffset
, yoffset
, zoffset
,
2429 width
, height
, depth
,
2430 format
, imageSize
, data
,
2433 ctx
->NewState
|= _NEW_TEXTURE
;
2438 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
2440 const struct gl_texture_unit
*texUnit
;
2441 const struct gl_texture_object
*texObj
;
2442 struct gl_texture_image
*texImage
;
2444 GET_CURRENT_CONTEXT(ctx
);
2445 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2447 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
2448 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
2450 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
2454 if (target
== GL_TEXTURE_1D
|| target
== GL_TEXTURE_2D
) {
2455 maxLevels
= ctx
->Const
.MaxTextureLevels
;
2457 else if (target
== GL_TEXTURE_3D
) {
2458 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
2461 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
2464 ASSERT(maxLevels
> 0);
2466 if (level
< 0 || level
>= maxLevels
) {
2467 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
2471 if (is_proxy_target(target
)) {
2472 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
2476 texImage
= _mesa_select_tex_image(ctx
, texUnit
, target
, level
);
2478 /* probably invalid mipmap level */
2479 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
2483 if (!texImage
->IsCompressed
) {
2484 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetCompressedTexImageARB");
2491 if (ctx
->Extensions
.ARB_texture_compression
) {
2492 ASSERT(ctx
->Driver
.GetCompressedTexImage
);
2493 (*ctx
->Driver
.GetCompressedTexImage
)(ctx
, target
, level
, img
, texObj
,