2 * mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Texture image-related functions.
34 #include "bufferobj.h"
38 #include "framebuffer.h"
44 #include "texcompress.h"
53 * State changes which we care about for glCopyTex[Sub]Image() calls.
54 * In particular, we care about pixel transfer state and buffer state
55 * (such as glReadBuffer to make sure we read from the right renderbuffer).
57 #define NEW_COPY_TEX_STATE (_MESA_NEW_TRANSFER_STATE | \
64 * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
68 _mesa_alloc_texmemory(GLsizei bytes
)
70 return _mesa_align_malloc(bytes
, 512);
75 * Free texture memory allocated with _mesa_alloc_texmemory()
78 _mesa_free_texmemory(void *m
)
85 * Compute floor(log_base_2(n)).
115 * Return the simple base format for a given internal texture format.
116 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
118 * \param ctx GL context.
119 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
121 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
122 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
124 * This is the format which is used during texture application (i.e. the
125 * texture format and env mode determine the arithmetic used.
127 * XXX this could be static
130 _mesa_base_tex_format( GLcontext
*ctx
, GLint internalFormat
)
132 switch (internalFormat
) {
147 case GL_LUMINANCE_ALPHA
:
148 case GL_LUMINANCE4_ALPHA4
:
149 case GL_LUMINANCE6_ALPHA2
:
150 case GL_LUMINANCE8_ALPHA8
:
151 case GL_LUMINANCE12_ALPHA4
:
152 case GL_LUMINANCE12_ALPHA12
:
153 case GL_LUMINANCE16_ALPHA16
:
154 return GL_LUMINANCE_ALPHA
;
185 if (ctx
->Extensions
.EXT_paletted_texture
) {
186 switch (internalFormat
) {
188 case GL_COLOR_INDEX1_EXT
:
189 case GL_COLOR_INDEX2_EXT
:
190 case GL_COLOR_INDEX4_EXT
:
191 case GL_COLOR_INDEX8_EXT
:
192 case GL_COLOR_INDEX12_EXT
:
193 case GL_COLOR_INDEX16_EXT
:
194 return GL_COLOR_INDEX
;
200 if (ctx
->Extensions
.ARB_depth_texture
) {
201 switch (internalFormat
) {
202 case GL_DEPTH_COMPONENT
:
203 case GL_DEPTH_COMPONENT16
:
204 case GL_DEPTH_COMPONENT24
:
205 case GL_DEPTH_COMPONENT32
:
206 return GL_DEPTH_COMPONENT
;
212 switch (internalFormat
) {
213 case GL_COMPRESSED_ALPHA
:
215 case GL_COMPRESSED_LUMINANCE
:
217 case GL_COMPRESSED_LUMINANCE_ALPHA
:
218 return GL_LUMINANCE_ALPHA
;
219 case GL_COMPRESSED_INTENSITY
:
221 case GL_COMPRESSED_RGB
:
223 case GL_COMPRESSED_RGBA
:
229 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
230 switch (internalFormat
) {
231 case GL_COMPRESSED_RGB_FXT1_3DFX
:
233 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
240 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
241 switch (internalFormat
) {
242 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
244 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
245 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
246 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
253 if (ctx
->Extensions
.S3_s3tc
) {
254 switch (internalFormat
) {
266 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
267 if (internalFormat
== GL_YCBCR_MESA
)
268 return GL_YCBCR_MESA
;
271 if (ctx
->Extensions
.ARB_texture_float
) {
272 switch (internalFormat
) {
273 case GL_ALPHA16F_ARB
:
274 case GL_ALPHA32F_ARB
:
282 case GL_INTENSITY16F_ARB
:
283 case GL_INTENSITY32F_ARB
:
285 case GL_LUMINANCE16F_ARB
:
286 case GL_LUMINANCE32F_ARB
:
288 case GL_LUMINANCE_ALPHA16F_ARB
:
289 case GL_LUMINANCE_ALPHA32F_ARB
:
290 return GL_LUMINANCE_ALPHA
;
296 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
297 switch (internalFormat
) {
306 if (ctx
->Extensions
.MESA_texture_signed_rgba
) {
307 switch (internalFormat
) {
316 if (ctx
->Extensions
.EXT_packed_depth_stencil
) {
317 switch (internalFormat
) {
318 case GL_DEPTH_STENCIL_EXT
:
319 case GL_DEPTH24_STENCIL8_EXT
:
320 return GL_DEPTH_STENCIL_EXT
;
326 #if FEATURE_EXT_texture_sRGB
327 if (ctx
->Extensions
.EXT_texture_sRGB
) {
328 switch (internalFormat
) {
331 case GL_COMPRESSED_SRGB_EXT
:
332 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
334 case GL_SRGB_ALPHA_EXT
:
335 case GL_SRGB8_ALPHA8_EXT
:
336 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
337 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
338 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
339 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
341 case GL_SLUMINANCE_ALPHA_EXT
:
342 case GL_SLUMINANCE8_ALPHA8_EXT
:
343 case GL_COMPRESSED_SLUMINANCE_EXT
:
344 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
345 return GL_LUMINANCE_ALPHA
;
346 case GL_SLUMINANCE_EXT
:
347 case GL_SLUMINANCE8_EXT
:
353 #endif /* FEATURE_EXT_texture_sRGB */
355 if (ctx
->Extensions
.EXT_texture_integer
) {
356 switch (internalFormat
) {
358 case GL_RGBA16UI_EXT
:
359 case GL_RGBA32UI_EXT
:
371 case GL_ALPHA8UI_EXT
:
372 case GL_ALPHA16UI_EXT
:
373 case GL_ALPHA32UI_EXT
:
375 case GL_ALPHA16I_EXT
:
376 case GL_ALPHA32I_EXT
:
378 case GL_INTENSITY8UI_EXT
:
379 case GL_INTENSITY16UI_EXT
:
380 case GL_INTENSITY32UI_EXT
:
381 case GL_INTENSITY8I_EXT
:
382 case GL_INTENSITY16I_EXT
:
383 case GL_INTENSITY32I_EXT
:
385 case GL_LUMINANCE8UI_EXT
:
386 case GL_LUMINANCE16UI_EXT
:
387 case GL_LUMINANCE32UI_EXT
:
388 case GL_LUMINANCE8I_EXT
:
389 case GL_LUMINANCE16I_EXT
:
390 case GL_LUMINANCE32I_EXT
:
392 case GL_LUMINANCE_ALPHA8UI_EXT
:
393 case GL_LUMINANCE_ALPHA16UI_EXT
:
394 case GL_LUMINANCE_ALPHA32UI_EXT
:
395 case GL_LUMINANCE_ALPHA8I_EXT
:
396 case GL_LUMINANCE_ALPHA16I_EXT
:
397 case GL_LUMINANCE_ALPHA32I_EXT
:
398 return GL_LUMINANCE_ALPHA
;
404 if (ctx
->Extensions
.ARB_texture_rg
) {
405 switch (internalFormat
) {
407 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
409 if (!ctx
->Extensions
.ARB_half_float_pixel
)
413 if (!ctx
->Extensions
.ARB_texture_float
)
422 if (!ctx
->Extensions
.EXT_texture_integer
)
428 case GL_COMPRESSED_RED
:
432 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
434 if (!ctx
->Extensions
.ARB_half_float_pixel
)
438 if (!ctx
->Extensions
.ARB_texture_float
)
447 if (!ctx
->Extensions
.EXT_texture_integer
)
453 case GL_COMPRESSED_RG
:
460 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
461 switch (internalFormat
) {
469 if (ctx
->Extensions
.EXT_packed_float
) {
470 switch (internalFormat
) {
471 case GL_R11F_G11F_B10F_EXT
:
478 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
479 switch (internalFormat
) {
480 case GL_DEPTH_COMPONENT32F
:
481 return GL_DEPTH_COMPONENT
;
482 case GL_DEPTH32F_STENCIL8
:
483 return GL_DEPTH_STENCIL
;
489 if (ctx
->Extensions
.EXT_texture_compression_rgtc
) {
490 switch (internalFormat
) {
491 case GL_COMPRESSED_RED_RGTC1_EXT
:
492 case GL_COMPRESSED_SIGNED_RED_RGTC1_EXT
:
494 case GL_COMPRESSED_RED_GREEN_RGTC2_EXT
:
495 case GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT
:
502 return -1; /* error */
507 * For cube map faces, return a face index in [0,5].
508 * For other targets return 0;
511 _mesa_tex_target_to_face(GLenum target
)
513 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
514 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)
515 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
523 * Store a gl_texture_image pointer in a gl_texture_object structure
524 * according to the target and level parameters.
526 * \param tObj texture object.
527 * \param target texture target.
528 * \param level image level.
529 * \param texImage texture image.
531 * This was basically prompted by the introduction of cube maps.
534 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
535 GLenum target
, GLint level
,
536 struct gl_texture_image
*texImage
)
538 const GLuint face
= _mesa_tex_target_to_face(target
);
542 ASSERT(target
!= GL_TEXTURE_RECTANGLE_NV
|| level
== 0);
544 tObj
->Image
[face
][level
] = texImage
;
546 /* Set the 'back' pointer */
547 texImage
->TexObject
= tObj
;
552 * Allocate a texture image structure.
554 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
557 * \return a pointer to gl_texture_image struct with all fields initialized to
560 struct gl_texture_image
*
561 _mesa_new_texture_image( GLcontext
*ctx
)
564 return CALLOC_STRUCT(gl_texture_image
);
569 * Free texture image data.
570 * This function is a fallback called via ctx->Driver.FreeTexImageData().
572 * \param texImage texture image.
574 * Free the texture image data if it's not marked as client data.
577 _mesa_free_texture_image_data(GLcontext
*ctx
,
578 struct gl_texture_image
*texImage
)
582 if (texImage
->Data
&& !texImage
->IsClientData
) {
583 /* free the old texture data */
584 _mesa_free_texmemory(texImage
->Data
);
587 texImage
->Data
= NULL
;
592 * Free texture image.
594 * \param texImage texture image.
596 * Free the texture image structure and the associated image data.
599 _mesa_delete_texture_image( GLcontext
*ctx
, struct gl_texture_image
*texImage
)
601 /* Free texImage->Data and/or any other driver-specific texture
604 ASSERT(ctx
->Driver
.FreeTexImageData
);
605 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
607 ASSERT(texImage
->Data
== NULL
);
608 if (texImage
->ImageOffsets
)
609 free(texImage
->ImageOffsets
);
615 * Test if a target is a proxy target.
617 * \param target texture target.
619 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
622 _mesa_is_proxy_texture(GLenum target
)
624 /* NUM_TEXTURE_TARGETS should match number of terms below */
625 assert(NUM_TEXTURE_TARGETS
== 7);
627 return (target
== GL_PROXY_TEXTURE_1D
||
628 target
== GL_PROXY_TEXTURE_2D
||
629 target
== GL_PROXY_TEXTURE_3D
||
630 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
631 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
632 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
633 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
);
638 * Get the texture object that corresponds to the target of the given texture unit.
640 * \param ctx GL context.
641 * \param texUnit texture unit.
642 * \param target texture target.
644 * \return pointer to the texture object on success, or NULL on failure.
646 * \sa gl_texture_unit.
648 struct gl_texture_object
*
649 _mesa_select_tex_object(GLcontext
*ctx
, const struct gl_texture_unit
*texUnit
,
654 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
655 case GL_PROXY_TEXTURE_1D
:
656 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
658 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
659 case GL_PROXY_TEXTURE_2D
:
660 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
662 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
663 case GL_PROXY_TEXTURE_3D
:
664 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
665 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
666 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
667 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
668 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
669 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
670 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
671 case GL_TEXTURE_CUBE_MAP_ARB
:
672 return ctx
->Extensions
.ARB_texture_cube_map
673 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
674 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
675 return ctx
->Extensions
.ARB_texture_cube_map
676 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
677 case GL_TEXTURE_RECTANGLE_NV
:
678 return ctx
->Extensions
.NV_texture_rectangle
679 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
680 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
681 return ctx
->Extensions
.NV_texture_rectangle
682 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
683 case GL_TEXTURE_1D_ARRAY_EXT
:
684 return ctx
->Extensions
.MESA_texture_array
685 ? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
686 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
687 return ctx
->Extensions
.MESA_texture_array
688 ? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
689 case GL_TEXTURE_2D_ARRAY_EXT
:
690 return ctx
->Extensions
.MESA_texture_array
691 ? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
692 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
693 return ctx
->Extensions
.MESA_texture_array
694 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
696 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
703 * Return pointer to texture object for given target on current texture unit.
705 struct gl_texture_object
*
706 _mesa_get_current_tex_object(GLcontext
*ctx
, GLenum target
)
708 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
709 return _mesa_select_tex_object(ctx
, texUnit
, target
);
714 * Get a texture image pointer from a texture object, given a texture
715 * target and mipmap level. The target and level parameters should
716 * have already been error-checked.
718 * \param ctx GL context.
719 * \param texObj texture unit.
720 * \param target texture target.
721 * \param level image level.
723 * \return pointer to the texture image structure, or NULL on failure.
725 struct gl_texture_image
*
726 _mesa_select_tex_image(GLcontext
*ctx
, const struct gl_texture_object
*texObj
,
727 GLenum target
, GLint level
)
729 const GLuint face
= _mesa_tex_target_to_face(target
);
733 ASSERT(level
< MAX_TEXTURE_LEVELS
);
735 return texObj
->Image
[face
][level
];
740 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
741 * it and install it. Only return NULL if passed a bad parameter or run
744 struct gl_texture_image
*
745 _mesa_get_tex_image(GLcontext
*ctx
, struct gl_texture_object
*texObj
,
746 GLenum target
, GLint level
)
748 struct gl_texture_image
*texImage
;
753 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
755 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
757 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
761 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
769 * Return pointer to the specified proxy texture image.
770 * Note that proxy textures are per-context, not per-texture unit.
771 * \return pointer to texture image or NULL if invalid target, invalid
772 * level, or out of memory.
774 struct gl_texture_image
*
775 _mesa_get_proxy_tex_image(GLcontext
*ctx
, GLenum target
, GLint level
)
777 struct gl_texture_image
*texImage
;
784 case GL_PROXY_TEXTURE_1D
:
785 if (level
>= ctx
->Const
.MaxTextureLevels
)
787 texIndex
= TEXTURE_1D_INDEX
;
789 case GL_PROXY_TEXTURE_2D
:
790 if (level
>= ctx
->Const
.MaxTextureLevels
)
792 texIndex
= TEXTURE_2D_INDEX
;
794 case GL_PROXY_TEXTURE_3D
:
795 if (level
>= ctx
->Const
.Max3DTextureLevels
)
797 texIndex
= TEXTURE_3D_INDEX
;
799 case GL_PROXY_TEXTURE_CUBE_MAP
:
800 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
802 texIndex
= TEXTURE_CUBE_INDEX
;
804 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
807 texIndex
= TEXTURE_RECT_INDEX
;
809 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
810 if (level
>= ctx
->Const
.MaxTextureLevels
)
812 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
814 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
815 if (level
>= ctx
->Const
.MaxTextureLevels
)
817 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
823 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
825 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
827 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
830 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
831 /* Set the 'back' pointer */
832 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
839 * Get the maximum number of allowed mipmap levels.
841 * \param ctx GL context.
842 * \param target texture target.
844 * \return the maximum number of allowed mipmap levels for the given
845 * texture target, or zero if passed a bad target.
850 _mesa_max_texture_levels(GLcontext
*ctx
, GLenum target
)
854 case GL_PROXY_TEXTURE_1D
:
856 case GL_PROXY_TEXTURE_2D
:
857 return ctx
->Const
.MaxTextureLevels
;
859 case GL_PROXY_TEXTURE_3D
:
860 return ctx
->Const
.Max3DTextureLevels
;
861 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
862 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
863 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
864 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
865 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
866 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
867 case GL_TEXTURE_CUBE_MAP_ARB
:
868 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
869 return ctx
->Extensions
.ARB_texture_cube_map
870 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
871 case GL_TEXTURE_RECTANGLE_NV
:
872 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
873 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
874 case GL_TEXTURE_1D_ARRAY_EXT
:
875 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
876 case GL_TEXTURE_2D_ARRAY_EXT
:
877 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
878 return ctx
->Extensions
.MESA_texture_array
879 ? ctx
->Const
.MaxTextureLevels
: 0;
881 return 0; /* bad target */
887 #if 000 /* not used anymore */
889 * glTexImage[123]D can accept a NULL image pointer. In this case we
890 * create a texture image with unspecified image contents per the OpenGL
894 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
896 const GLint components
= _mesa_components_in_format(format
);
897 const GLint numPixels
= width
* height
* depth
;
898 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
902 * Let's see if anyone finds this. If glTexImage2D() is called with
903 * a NULL image pointer then load the texture image with something
904 * interesting instead of leaving it indeterminate.
907 static const char message
[8][32] = {
911 " X X XXXX XXX XXXXX ",
914 " X X XXXXX XXX X X ",
918 GLubyte
*imgPtr
= data
;
920 for (h
= 0; h
< depth
; h
++) {
921 for (i
= 0; i
< height
; i
++) {
922 GLint srcRow
= 7 - (i
% 8);
923 for (j
= 0; j
< width
; j
++) {
924 GLint srcCol
= j
% 32;
925 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
926 for (k
= 0; k
< components
; k
++) {
942 * Reset the fields of a gl_texture_image struct to zero.
944 * \param img texture image structure.
946 * This is called when a proxy texture test fails, we set all the
947 * image members (except DriverData) to zero.
948 * It's also used in glTexImage[123]D as a safeguard to be sure all
949 * required fields get initialized properly by the Driver.TexImage[123]D
953 clear_teximage_fields(struct gl_texture_image
*img
)
956 img
->_BaseFormat
= 0;
957 img
->InternalFormat
= 0;
963 if (img
->ImageOffsets
) {
964 free(img
->ImageOffsets
);
965 img
->ImageOffsets
= NULL
;
974 img
->TexFormat
= MESA_FORMAT_NONE
;
975 img
->FetchTexelc
= NULL
;
976 img
->FetchTexelf
= NULL
;
981 * Initialize basic fields of the gl_texture_image struct.
983 * \param ctx GL context.
984 * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
985 * \param img texture image structure to be initialized.
986 * \param width image width.
987 * \param height image height.
988 * \param depth image depth.
989 * \param border image border.
990 * \param internalFormat internal format.
992 * Fills in the fields of \p img with the given information.
993 * Note: width, height and depth include the border.
996 _mesa_init_teximage_fields(GLcontext
*ctx
, GLenum target
,
997 struct gl_texture_image
*img
,
998 GLsizei width
, GLsizei height
, GLsizei depth
,
999 GLint border
, GLenum internalFormat
)
1005 ASSERT(height
>= 0);
1008 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1009 ASSERT(img
->_BaseFormat
> 0);
1010 img
->InternalFormat
= internalFormat
;
1011 img
->Border
= border
;
1013 img
->Height
= height
;
1016 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1017 img
->WidthLog2
= logbase2(img
->Width2
);
1019 if (height
== 1) { /* 1-D texture */
1021 img
->HeightLog2
= 0;
1024 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1025 img
->HeightLog2
= logbase2(img
->Height2
);
1028 if (depth
== 1) { /* 2-D texture */
1033 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1034 img
->DepthLog2
= logbase2(img
->Depth2
);
1037 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1039 if ((width
== 1 || _mesa_is_pow_two(img
->Width2
)) &&
1040 (height
== 1 || _mesa_is_pow_two(img
->Height2
)) &&
1041 (depth
== 1 || _mesa_is_pow_two(img
->Depth2
)))
1042 img
->_IsPowerOfTwo
= GL_TRUE
;
1044 img
->_IsPowerOfTwo
= GL_FALSE
;
1046 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
1047 img
->RowStride
= width
;
1048 /* Allocate the ImageOffsets array and initialize to typical values.
1049 * We allocate the array for 1D/2D textures too in order to avoid special-
1050 * case code in the texstore routines.
1052 if (img
->ImageOffsets
)
1053 free(img
->ImageOffsets
);
1054 img
->ImageOffsets
= (GLuint
*) malloc(depth
* sizeof(GLuint
));
1055 for (i
= 0; i
< depth
; i
++) {
1056 img
->ImageOffsets
[i
] = i
* width
* height
;
1059 /* Compute Width/Height/DepthScale for mipmap lod computation */
1060 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1061 /* scale = 1.0 since texture coords directly map to texels */
1062 img
->WidthScale
= 1.0;
1063 img
->HeightScale
= 1.0;
1064 img
->DepthScale
= 1.0;
1067 img
->WidthScale
= (GLfloat
) img
->Width
;
1068 img
->HeightScale
= (GLfloat
) img
->Height
;
1069 img
->DepthScale
= (GLfloat
) img
->Depth
;
1072 img
->FetchTexelc
= NULL
;
1073 img
->FetchTexelf
= NULL
;
1078 * Free and clear fields of the gl_texture_image struct.
1080 * \param ctx GL context.
1081 * \param texImage texture image structure to be cleared.
1083 * After the call, \p texImage will have no data associated with it. Its
1084 * fields are cleared so that its parent object will test incomplete.
1087 _mesa_clear_texture_image(GLcontext
*ctx
, struct gl_texture_image
*texImage
)
1089 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
1090 clear_teximage_fields(texImage
);
1095 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1096 * level, width, height and depth against the ctx->Const limits for textures.
1098 * A hardware driver might override this function if, for example, the
1099 * max 3D texture size is 512x512x64 (i.e. not a cube).
1101 * Note that width, height, depth == 0 is not an error. However, a
1102 * texture with zero width/height/depth will be considered "incomplete"
1103 * and texturing will effectively be disabled.
1105 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1106 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1107 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1108 * \param level as passed to glTexImage
1109 * \param internalFormat as passed to glTexImage
1110 * \param format as passed to glTexImage
1111 * \param type as passed to glTexImage
1112 * \param width as passed to glTexImage
1113 * \param height as passed to glTexImage
1114 * \param depth as passed to glTexImage
1115 * \param border as passed to glTexImage
1116 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1119 _mesa_test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1120 GLint internalFormat
, GLenum format
, GLenum type
,
1121 GLint width
, GLint height
, GLint depth
, GLint border
)
1125 (void) internalFormat
;
1130 case GL_PROXY_TEXTURE_1D
:
1131 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1132 if (width
< 2 * border
|| width
> 2 + maxSize
||
1133 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1134 width
>0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1135 level
>= ctx
->Const
.MaxTextureLevels
) {
1136 /* bad width or level */
1140 case GL_PROXY_TEXTURE_2D
:
1141 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1142 if (width
< 2 * border
|| width
> 2 + maxSize
||
1143 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1144 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1145 height
< 2 * border
|| height
> 2 + maxSize
||
1146 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1147 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1148 level
>= ctx
->Const
.MaxTextureLevels
) {
1149 /* bad width or height or level */
1153 case GL_PROXY_TEXTURE_3D
:
1154 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1155 if (width
< 2 * border
|| width
> 2 + maxSize
||
1156 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1157 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1158 height
< 2 * border
|| height
> 2 + maxSize
||
1159 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1160 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1161 depth
< 2 * border
|| depth
> 2 + maxSize
||
1162 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1163 depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
)) ||
1164 level
>= ctx
->Const
.Max3DTextureLevels
) {
1165 /* bad width or height or depth or level */
1169 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1170 if (width
< 0 || width
> ctx
->Const
.MaxTextureRectSize
||
1171 height
< 0 || height
> ctx
->Const
.MaxTextureRectSize
||
1173 /* bad width or height or level */
1177 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1178 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1179 if (width
< 2 * border
|| width
> 2 + maxSize
||
1180 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1181 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1182 height
< 2 * border
|| height
> 2 + maxSize
||
1183 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1184 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1185 level
>= ctx
->Const
.MaxCubeTextureLevels
) {
1186 /* bad width or height */
1190 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1191 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1192 if (width
< 2 * border
|| width
> 2 + maxSize
||
1193 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1194 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1195 level
>= ctx
->Const
.MaxTextureLevels
) {
1196 /* bad width or level */
1200 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
) {
1204 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1205 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1206 if (width
< 2 * border
|| width
> 2 + maxSize
||
1207 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1208 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1209 height
< 2 * border
|| height
> 2 + maxSize
||
1210 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1211 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1212 level
>= ctx
->Const
.MaxTextureLevels
) {
1213 /* bad width or height or level */
1216 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
) {
1221 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1228 * Helper function to determine whether a target supports compressed textures
1231 target_can_be_compressed(GLcontext
*ctx
, GLenum target
)
1233 return (((target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
))
1234 || ((ctx
->Extensions
.ARB_texture_cube_map
&&
1235 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1236 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1237 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))))
1238 || ((ctx
->Extensions
.MESA_texture_array
&&
1239 ((target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
) ||
1240 (target
== GL_TEXTURE_2D_ARRAY_EXT
)))));
1245 * Test the glTexImage[123]D() parameters for errors.
1247 * \param ctx GL context.
1248 * \param target texture target given by the user.
1249 * \param level image level given by the user.
1250 * \param internalFormat internal format given by the user.
1251 * \param format pixel data format given by the user.
1252 * \param type pixel data type given by the user.
1253 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1254 * \param width image width given by the user.
1255 * \param height image height given by the user.
1256 * \param depth image depth given by the user.
1257 * \param border image border given by the user.
1259 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1261 * Verifies each of the parameters against the constants specified in
1262 * __GLcontextRec::Const and the supported extensions, and according to the
1263 * OpenGL specification.
1266 texture_error_check( GLcontext
*ctx
, GLenum target
,
1267 GLint level
, GLint internalFormat
,
1268 GLenum format
, GLenum type
,
1270 GLint width
, GLint height
,
1271 GLint depth
, GLint border
)
1273 const GLboolean isProxy
= _mesa_is_proxy_texture(target
);
1274 GLboolean sizeOK
= GL_TRUE
;
1275 GLboolean colorFormat
, indexFormat
;
1276 GLenum proxy_target
;
1278 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1279 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1281 _mesa_error(ctx
, GL_INVALID_VALUE
,
1282 "glTexImage%dD(level=%d)", dimensions
, level
);
1288 if (border
< 0 || border
> 1 ||
1289 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1290 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1292 _mesa_error(ctx
, GL_INVALID_VALUE
,
1293 "glTexImage%dD(border=%d)", dimensions
, border
);
1298 if (width
< 0 || height
< 0 || depth
< 0) {
1300 _mesa_error(ctx
, GL_INVALID_VALUE
,
1301 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1306 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1307 * level, width, height and depth.
1309 if (dimensions
== 1) {
1310 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1311 proxy_target
= GL_PROXY_TEXTURE_1D
;
1316 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1320 else if (dimensions
== 2) {
1322 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1323 proxy_target
= GL_PROXY_TEXTURE_2D
;
1325 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1326 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1327 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1328 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1332 proxy_target
= GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
1333 sizeOK
= (width
== height
);
1335 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1336 target
== GL_TEXTURE_RECTANGLE_NV
) {
1337 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1338 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1341 proxy_target
= GL_PROXY_TEXTURE_RECTANGLE_NV
;
1343 else if (target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
1344 target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1345 proxy_target
= GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
1348 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1352 else if (dimensions
== 3) {
1353 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1354 proxy_target
= GL_PROXY_TEXTURE_3D
;
1356 else if (target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
||
1357 target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1358 proxy_target
= GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
1361 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1366 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1370 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target
, level
,
1371 internalFormat
, format
,
1372 type
, width
, height
,
1376 _mesa_error(ctx
, GL_INVALID_VALUE
,
1377 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1378 dimensions
, level
, width
, height
, depth
);
1383 /* Check internalFormat */
1384 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1386 _mesa_error(ctx
, GL_INVALID_VALUE
,
1387 "glTexImage%dD(internalFormat=%s)",
1388 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1393 /* Check incoming image format and type */
1394 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1395 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1396 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1400 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1401 dimensions
, format
, type
);
1406 /* make sure internal format and format basically agree */
1407 colorFormat
= _mesa_is_color_format(format
);
1408 indexFormat
= _mesa_is_index_format(format
);
1409 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1410 (_mesa_is_index_format(internalFormat
) && !indexFormat
) ||
1411 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1412 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1413 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1414 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1416 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1417 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1418 dimensions
, internalFormat
, format
);
1422 /* additional checks for ycbcr textures */
1423 if (internalFormat
== GL_YCBCR_MESA
) {
1424 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1425 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1426 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1428 _mesa_snprintf(message
, sizeof(message
),
1429 "glTexImage%dD(format/type YCBCR mismatch", dimensions
);
1430 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1431 return GL_TRUE
; /* error */
1433 if (target
!= GL_TEXTURE_2D
&&
1434 target
!= GL_PROXY_TEXTURE_2D
&&
1435 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1436 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1438 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1444 _mesa_snprintf(message
, sizeof(message
),
1445 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1446 dimensions
, border
);
1447 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1453 /* additional checks for depth textures */
1454 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1455 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1456 if (target
!= GL_TEXTURE_1D
&&
1457 target
!= GL_PROXY_TEXTURE_1D
&&
1458 target
!= GL_TEXTURE_2D
&&
1459 target
!= GL_PROXY_TEXTURE_2D
&&
1460 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1461 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
) {
1463 _mesa_error(ctx
, GL_INVALID_ENUM
,
1464 "glTexImage(target/internalFormat)");
1469 /* additional checks for compressed textures */
1470 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1471 if (!target_can_be_compressed(ctx
, target
) && !isProxy
) {
1472 _mesa_error(ctx
, GL_INVALID_ENUM
,
1473 "glTexImage%dD(target)", dimensions
);
1478 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1479 "glTexImage%dD(border!=0)", dimensions
);
1485 /* if we get here, the parameters are OK */
1491 * Test glTexSubImage[123]D() parameters for errors.
1493 * \param ctx GL context.
1494 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1495 * \param target texture target given by the user.
1496 * \param level image level given by the user.
1497 * \param xoffset sub-image x offset given by the user.
1498 * \param yoffset sub-image y offset given by the user.
1499 * \param zoffset sub-image z offset given by the user.
1500 * \param format pixel data format given by the user.
1501 * \param type pixel data type given by the user.
1502 * \param width image width given by the user.
1503 * \param height image height given by the user.
1504 * \param depth image depth given by the user.
1506 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1508 * Verifies each of the parameters against the constants specified in
1509 * __GLcontextRec::Const and the supported extensions, and according to the
1510 * OpenGL specification.
1513 subtexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1514 GLenum target
, GLint level
,
1515 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1516 GLint width
, GLint height
, GLint depth
,
1517 GLenum format
, GLenum type
)
1520 if (dimensions
== 1) {
1521 if (target
!= GL_TEXTURE_1D
) {
1522 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1526 else if (dimensions
== 2) {
1527 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1528 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1529 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1530 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1534 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1535 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1536 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1540 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1541 if (!ctx
->Extensions
.MESA_texture_array
) {
1542 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1546 else if (target
!= GL_TEXTURE_2D
) {
1547 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1551 else if (dimensions
== 3) {
1552 if (target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1553 if (!ctx
->Extensions
.MESA_texture_array
) {
1554 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1558 else if (target
!= GL_TEXTURE_3D
) {
1559 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1564 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1568 /* Basic level check */
1569 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1570 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1575 _mesa_error(ctx
, GL_INVALID_VALUE
,
1576 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1579 if (height
< 0 && dimensions
> 1) {
1580 _mesa_error(ctx
, GL_INVALID_VALUE
,
1581 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1584 if (depth
< 0 && dimensions
> 2) {
1585 _mesa_error(ctx
, GL_INVALID_VALUE
,
1586 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1590 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1591 _mesa_error(ctx
, GL_INVALID_ENUM
,
1592 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1593 dimensions
, format
, type
);
1602 * Do second part of glTexSubImage which depends on the destination texture.
1603 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1606 subtexture_error_check2( GLcontext
*ctx
, GLuint dimensions
,
1607 GLenum target
, GLint level
,
1608 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1609 GLint width
, GLint height
, GLint depth
,
1610 GLenum format
, GLenum type
,
1611 const struct gl_texture_image
*destTex
)
1614 /* undefined image level */
1615 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1619 if (xoffset
< -((GLint
)destTex
->Border
)) {
1620 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1624 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1625 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1629 if (dimensions
> 1) {
1630 if (yoffset
< -((GLint
)destTex
->Border
)) {
1631 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1635 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1636 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1641 if (dimensions
> 2) {
1642 if (zoffset
< -((GLint
)destTex
->Border
)) {
1643 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1646 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1647 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1652 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
1655 if (!target_can_be_compressed(ctx
, target
)) {
1656 _mesa_error(ctx
, GL_INVALID_ENUM
,
1657 "glTexSubImage%dD(target=%s)", dimensions
,
1658 _mesa_lookup_enum_by_nr(target
));
1662 /* do tests which depend on compression block size */
1663 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
1665 /* offset must be multiple of block size */
1666 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1668 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
1669 dimensions
, xoffset
, yoffset
);
1672 /* size must be multiple of bw by bh or equal to whole texture size */
1673 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
1674 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1675 "glTexSubImage%dD(width = %d)", dimensions
, width
);
1678 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
1679 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1680 "glTexSubImage%dD(height = %d)", dimensions
, height
);
1690 * Test glCopyTexImage[12]D() parameters for errors.
1692 * \param ctx GL context.
1693 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1694 * \param target texture target given by the user.
1695 * \param level image level given by the user.
1696 * \param internalFormat internal format given by the user.
1697 * \param width image width given by the user.
1698 * \param height image height given by the user.
1699 * \param border texture border.
1701 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1703 * Verifies each of the parameters against the constants specified in
1704 * __GLcontextRec::Const and the supported extensions, and according to the
1705 * OpenGL specification.
1708 copytexture_error_check( GLcontext
*ctx
, GLuint dimensions
,
1709 GLenum target
, GLint level
, GLint internalFormat
,
1710 GLint width
, GLint height
, GLint border
)
1716 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1717 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1718 _mesa_error(ctx
, GL_INVALID_VALUE
,
1719 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1723 /* Check that the source buffer is complete */
1724 if (ctx
->ReadBuffer
->Name
) {
1725 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1726 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1727 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1728 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1734 if (border
< 0 || border
> 1 ||
1735 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1736 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1740 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1742 _mesa_error(ctx
, GL_INVALID_VALUE
,
1743 "glCopyTexImage%dD(internalFormat)", dimensions
);
1747 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1748 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1749 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
1753 /* NOTE: the format and type aren't really significant for
1754 * TestProxyTexImage(). Only the internalformat really matters.
1758 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1759 * level, width, height and depth.
1761 if (dimensions
== 1) {
1762 if (target
== GL_TEXTURE_1D
) {
1763 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1764 level
, internalFormat
,
1766 width
, 1, 1, border
);
1769 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1773 else if (dimensions
== 2) {
1774 if (target
== GL_TEXTURE_2D
) {
1775 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1776 level
, internalFormat
,
1778 width
, height
, 1, border
);
1780 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1781 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1782 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1783 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1786 sizeOK
= (width
== height
) &&
1787 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1788 level
, internalFormat
, format
, type
,
1789 width
, height
, 1, border
);
1791 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1792 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1793 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1796 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1797 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1798 level
, internalFormat
,
1800 width
, height
, 1, border
);
1802 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1803 if (!ctx
->Extensions
.MESA_texture_array
) {
1804 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)");
1807 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1808 GL_PROXY_TEXTURE_1D_ARRAY_EXT
,
1809 level
, internalFormat
,
1811 width
, height
, 1, border
);
1814 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1819 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1824 if (dimensions
== 1) {
1825 _mesa_error(ctx
, GL_INVALID_VALUE
,
1826 "glCopyTexImage1D(width=%d)", width
);
1829 ASSERT(dimensions
== 2);
1830 _mesa_error(ctx
, GL_INVALID_VALUE
,
1831 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1836 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1837 if (!target_can_be_compressed(ctx
, target
)) {
1838 _mesa_error(ctx
, GL_INVALID_ENUM
,
1839 "glCopyTexImage%dD(target)", dimensions
);
1843 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1844 "glCopyTexImage%dD(border!=0)", dimensions
);
1848 else if (_mesa_is_depth_format(internalFormat
)) {
1849 /* make sure we have depth/stencil buffers */
1850 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
1851 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1852 "glCopyTexImage%dD(no depth)", dimensions
);
1856 else if (_mesa_is_depthstencil_format(internalFormat
)) {
1857 /* make sure we have depth/stencil buffers */
1858 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
1859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1860 "glCopyTexImage%dD(no depth/stencil buffer)", dimensions
);
1865 /* if we get here, the parameters are OK */
1871 * Test glCopyTexSubImage[12]D() parameters for errors.
1872 * Note that this is the first part of error checking.
1873 * See also copytexsubimage_error_check2() below for the second part.
1875 * \param ctx GL context.
1876 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1877 * \param target texture target given by the user.
1878 * \param level image level given by the user.
1880 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1883 copytexsubimage_error_check1( GLcontext
*ctx
, GLuint dimensions
,
1884 GLenum target
, GLint level
)
1886 /* Check that the source buffer is complete */
1887 if (ctx
->ReadBuffer
->Name
) {
1888 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1889 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1890 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1891 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1897 if (dimensions
== 1) {
1898 if (target
!= GL_TEXTURE_1D
) {
1899 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1903 else if (dimensions
== 2) {
1904 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1905 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1906 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1907 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1911 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1912 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1913 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1917 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1918 if (!ctx
->Extensions
.MESA_texture_array
) {
1919 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1923 else if (target
!= GL_TEXTURE_2D
) {
1924 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1928 else if (dimensions
== 3) {
1929 if (((target
!= GL_TEXTURE_2D_ARRAY_EXT
) ||
1930 (!ctx
->Extensions
.MESA_texture_array
))
1931 && (target
!= GL_TEXTURE_3D
)) {
1932 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1938 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1939 _mesa_error(ctx
, GL_INVALID_VALUE
,
1940 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1949 * Second part of error checking for glCopyTexSubImage[12]D().
1950 * \param xoffset sub-image x offset given by the user.
1951 * \param yoffset sub-image y offset given by the user.
1952 * \param zoffset sub-image z offset given by the user.
1953 * \param width image width given by the user.
1954 * \param height image height given by the user.
1957 copytexsubimage_error_check2( GLcontext
*ctx
, GLuint dimensions
,
1958 GLenum target
, GLint level
,
1959 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1960 GLsizei width
, GLsizei height
,
1961 const struct gl_texture_image
*teximage
)
1963 /* check that dest tex image exists */
1965 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1966 "glCopyTexSubImage%dD(undefined texture level: %d)",
1973 _mesa_error(ctx
, GL_INVALID_VALUE
,
1974 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1977 if (dimensions
> 1 && height
< 0) {
1978 _mesa_error(ctx
, GL_INVALID_VALUE
,
1979 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1983 /* check x/y offsets */
1984 if (xoffset
< -((GLint
)teximage
->Border
)) {
1985 _mesa_error(ctx
, GL_INVALID_VALUE
,
1986 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1989 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1990 _mesa_error(ctx
, GL_INVALID_VALUE
,
1991 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1994 if (dimensions
> 1) {
1995 if (yoffset
< -((GLint
)teximage
->Border
)) {
1996 _mesa_error(ctx
, GL_INVALID_VALUE
,
1997 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2000 /* NOTE: we're adding the border here, not subtracting! */
2001 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
2002 _mesa_error(ctx
, GL_INVALID_VALUE
,
2003 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2008 /* check z offset */
2009 if (dimensions
> 2) {
2010 if (zoffset
< -((GLint
)teximage
->Border
)) {
2011 _mesa_error(ctx
, GL_INVALID_VALUE
,
2012 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2015 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
2016 _mesa_error(ctx
, GL_INVALID_VALUE
,
2017 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2022 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2023 if (!target_can_be_compressed(ctx
, target
)) {
2024 _mesa_error(ctx
, GL_INVALID_ENUM
,
2025 "glCopyTexSubImage%dD(target)", dimensions
);
2028 /* offset must be multiple of 4 */
2029 if ((xoffset
& 3) || (yoffset
& 3)) {
2030 _mesa_error(ctx
, GL_INVALID_VALUE
,
2031 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2034 /* size must be multiple of 4 */
2035 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2036 _mesa_error(ctx
, GL_INVALID_VALUE
,
2037 "glCopyTexSubImage%dD(width)", dimensions
);
2040 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2041 _mesa_error(ctx
, GL_INVALID_VALUE
,
2042 "glCopyTexSubImage%dD(height)", dimensions
);
2047 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2048 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2052 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2053 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2054 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2055 dimensions
, teximage
->_BaseFormat
);
2059 if (teximage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2060 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
2061 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2062 "glCopyTexSubImage%dD(no depth buffer)",
2067 else if (teximage
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
2068 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
2069 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2070 "glCopyTexSubImage%dD(no depth/stencil buffer)",
2076 /* if we get here, the parameters are OK */
2081 /** Callback info for walking over FBO hash table */
2085 struct gl_texture_object
*texObj
;
2091 * Check render to texture callback. Called from _mesa_HashWalk().
2094 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2096 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2097 const struct cb_info
*info
= (struct cb_info
*) userData
;
2098 GLcontext
*ctx
= info
->ctx
;
2099 const struct gl_texture_object
*texObj
= info
->texObj
;
2100 const GLuint level
= info
->level
, face
= info
->face
;
2102 /* If this is a user-created FBO */
2105 /* check if any of the FBO's attachments point to 'texObj' */
2106 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2107 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2108 if (att
->Type
== GL_TEXTURE
&&
2109 att
->Texture
== texObj
&&
2110 att
->TextureLevel
== level
&&
2111 att
->CubeMapFace
== face
) {
2112 ASSERT(att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]);
2113 /* Tell driver about the new renderbuffer texture */
2114 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2115 /* Mark fb status as indeterminate to force re-validation */
2124 * When a texture image is specified we have to check if it's bound to
2125 * any framebuffer objects (render to texture) in order to detect changes
2126 * in size or format since that effects FBO completeness.
2127 * Any FBOs rendering into the texture must be re-validated.
2130 update_fbo_texture(GLcontext
*ctx
, struct gl_texture_object
*texObj
,
2131 GLuint face
, GLuint level
)
2133 /* Only check this texture if it's been marked as RenderToTexture */
2134 if (texObj
->_RenderToTexture
) {
2135 struct cb_info info
;
2137 info
.texObj
= texObj
;
2140 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2146 * If the texture object's GenerateMipmap flag is set and we've
2147 * changed the texture base level image, regenerate the rest of the
2148 * mipmap levels now.
2151 check_gen_mipmap(GLcontext
*ctx
, GLenum target
,
2152 struct gl_texture_object
*texObj
, GLint level
)
2154 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2155 if (texObj
->GenerateMipmap
&&
2156 level
== texObj
->BaseLevel
&&
2157 level
< texObj
->MaxLevel
) {
2158 ASSERT(ctx
->Driver
.GenerateMipmap
);
2159 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2164 /** Debug helper: override the user-requested internal format */
2166 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2169 if (internalFormat
== GL_RGBA16F_ARB
||
2170 internalFormat
== GL_RGBA32F_ARB
) {
2171 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2174 else if (internalFormat
== GL_RGB16F_ARB
||
2175 internalFormat
== GL_RGB32F_ARB
) {
2176 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2179 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2180 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2181 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2182 return GL_LUMINANCE_ALPHA
;
2184 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2185 internalFormat
== GL_LUMINANCE32F_ARB
) {
2186 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2187 return GL_LUMINANCE
;
2189 else if (internalFormat
== GL_ALPHA16F_ARB
||
2190 internalFormat
== GL_ALPHA32F_ARB
) {
2191 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2195 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2196 internalFormat = GL_RGBA;
2200 return internalFormat
;
2203 return internalFormat
;
2209 * Choose the actual hardware format for a texture image.
2210 * Try to use the same format as the previous image level when possible.
2211 * Otherwise, ask the driver for the best format.
2212 * It's important to try to choose a consistant format for all levels
2213 * for efficient texture memory layout/allocation. In particular, this
2214 * comes up during automatic mipmap generation.
2217 _mesa_choose_texture_format(GLcontext
*ctx
,
2218 struct gl_texture_object
*texObj
,
2219 struct gl_texture_image
*texImage
,
2220 GLenum target
, GLint level
,
2221 GLenum internalFormat
, GLenum format
, GLenum type
)
2223 /* see if we've already chosen a format for the previous level */
2225 struct gl_texture_image
*prevImage
=
2226 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2227 /* See if the prev level is defined and has an internal format which
2228 * matches the new internal format.
2231 prevImage
->Width
> 0 &&
2232 prevImage
->InternalFormat
== internalFormat
) {
2233 /* use the same format */
2234 texImage
->TexFormat
= prevImage
->TexFormat
;
2239 /* choose format from scratch */
2240 texImage
->TexFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
,
2242 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2248 * Called from the API. Note that width includes the border.
2251 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2252 GLsizei width
, GLint border
, GLenum format
,
2253 GLenum type
, const GLvoid
*pixels
)
2255 GLsizei postConvWidth
= width
;
2256 GET_CURRENT_CONTEXT(ctx
);
2257 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2259 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2260 _mesa_debug(ctx
, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2261 _mesa_lookup_enum_by_nr(target
), level
,
2262 _mesa_lookup_enum_by_nr(internalFormat
), width
, border
,
2263 _mesa_lookup_enum_by_nr(format
),
2264 _mesa_lookup_enum_by_nr(type
), pixels
);
2266 internalFormat
= override_internal_format(internalFormat
, width
, 1);
2268 if (target
== GL_TEXTURE_1D
) {
2269 /* non-proxy target */
2270 struct gl_texture_object
*texObj
;
2271 struct gl_texture_image
*texImage
;
2272 const GLuint face
= _mesa_tex_target_to_face(target
);
2274 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2275 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2276 return; /* error was recorded */
2279 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2280 _mesa_update_state(ctx
);
2282 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2283 _mesa_lock_texture(ctx
, texObj
);
2285 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2287 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2290 if (texImage
->Data
) {
2291 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2294 ASSERT(texImage
->Data
== NULL
);
2296 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2297 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2298 postConvWidth
, 1, 1,
2299 border
, internalFormat
);
2301 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2302 internalFormat
, format
, type
);
2304 /* Give the texture to the driver. <pixels> may be null. */
2305 ASSERT(ctx
->Driver
.TexImage1D
);
2306 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2307 width
, border
, format
, type
, pixels
,
2308 &ctx
->Unpack
, texObj
, texImage
);
2310 ASSERT(texImage
->TexFormat
);
2312 _mesa_set_fetch_functions(texImage
, 1);
2314 check_gen_mipmap(ctx
, target
, texObj
, level
);
2316 update_fbo_texture(ctx
, texObj
, face
, level
);
2319 texObj
->_Complete
= GL_FALSE
;
2320 ctx
->NewState
|= _NEW_TEXTURE
;
2323 _mesa_unlock_texture(ctx
, texObj
);
2325 else if (target
== GL_PROXY_TEXTURE_1D
) {
2326 /* Proxy texture: check for errors and update proxy state */
2327 struct gl_texture_image
*texImage
;
2328 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2329 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2330 format
, type
, 1, postConvWidth
, 1, 1, border
)) {
2331 /* when error, clear all proxy texture image parameters */
2333 clear_teximage_fields(texImage
);
2336 /* no error, set the tex image parameters */
2337 struct gl_texture_object
*texObj
=
2338 _mesa_get_current_tex_object(ctx
, target
);
2340 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2341 postConvWidth
, 1, 1,
2342 border
, internalFormat
);
2343 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2344 internalFormat
, format
, type
);
2348 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2355 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2356 GLsizei width
, GLsizei height
, GLint border
,
2357 GLenum format
, GLenum type
,
2358 const GLvoid
*pixels
)
2360 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2361 GET_CURRENT_CONTEXT(ctx
);
2362 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2364 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2365 _mesa_debug(ctx
, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2366 _mesa_lookup_enum_by_nr(target
), level
,
2367 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2368 border
, _mesa_lookup_enum_by_nr(format
),
2369 _mesa_lookup_enum_by_nr(type
), pixels
);
2371 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2373 if (target
== GL_TEXTURE_2D
||
2374 (ctx
->Extensions
.ARB_texture_cube_map
&&
2375 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2376 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2377 (ctx
->Extensions
.NV_texture_rectangle
&&
2378 target
== GL_TEXTURE_RECTANGLE_NV
) ||
2379 (ctx
->Extensions
.MESA_texture_array
&&
2380 target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
2381 /* non-proxy target */
2382 struct gl_texture_object
*texObj
;
2383 struct gl_texture_image
*texImage
;
2384 const GLuint face
= _mesa_tex_target_to_face(target
);
2386 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2387 format
, type
, 2, postConvWidth
, postConvHeight
,
2389 return; /* error was recorded */
2392 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2393 _mesa_update_state(ctx
);
2395 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2396 _mesa_lock_texture(ctx
, texObj
);
2398 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2400 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2403 if (texImage
->Data
) {
2404 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2407 ASSERT(texImage
->Data
== NULL
);
2408 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2409 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2410 postConvWidth
, postConvHeight
, 1,
2411 border
, internalFormat
);
2413 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2414 internalFormat
, format
, type
);
2416 /* Give the texture to the driver. <pixels> may be null. */
2417 ASSERT(ctx
->Driver
.TexImage2D
);
2418 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2419 width
, height
, border
, format
, type
,
2420 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2422 ASSERT(texImage
->TexFormat
);
2424 _mesa_set_fetch_functions(texImage
, 2);
2426 check_gen_mipmap(ctx
, target
, texObj
, level
);
2428 update_fbo_texture(ctx
, texObj
, face
, level
);
2431 texObj
->_Complete
= GL_FALSE
;
2432 ctx
->NewState
|= _NEW_TEXTURE
;
2435 _mesa_unlock_texture(ctx
, texObj
);
2437 else if (target
== GL_PROXY_TEXTURE_2D
||
2438 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2439 ctx
->Extensions
.ARB_texture_cube_map
) ||
2440 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2441 ctx
->Extensions
.NV_texture_rectangle
) ||
2442 (ctx
->Extensions
.MESA_texture_array
&&
2443 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
)) {
2444 /* Proxy texture: check for errors and update proxy state */
2445 struct gl_texture_image
*texImage
;
2446 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2447 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2448 format
, type
, 2, postConvWidth
, postConvHeight
,
2450 /* when error, clear all proxy texture image parameters */
2452 clear_teximage_fields(texImage
);
2455 /* no error, set the tex image parameters */
2456 struct gl_texture_object
*texObj
=
2457 _mesa_get_current_tex_object(ctx
, target
);
2458 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2459 postConvWidth
, postConvHeight
, 1,
2460 border
, internalFormat
);
2461 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2462 internalFormat
, format
, type
);
2466 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2473 * Called by the API or display list executor.
2474 * Note that width and height include the border.
2477 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2478 GLsizei width
, GLsizei height
, GLsizei depth
,
2479 GLint border
, GLenum format
, GLenum type
,
2480 const GLvoid
*pixels
)
2482 GET_CURRENT_CONTEXT(ctx
);
2483 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2485 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2486 _mesa_debug(ctx
, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2487 _mesa_lookup_enum_by_nr(target
), level
,
2488 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2489 depth
, border
, _mesa_lookup_enum_by_nr(format
),
2490 _mesa_lookup_enum_by_nr(type
), pixels
);
2492 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2494 if (target
== GL_TEXTURE_3D
||
2495 (ctx
->Extensions
.MESA_texture_array
&&
2496 target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
2497 /* non-proxy target */
2498 struct gl_texture_object
*texObj
;
2499 struct gl_texture_image
*texImage
;
2500 const GLuint face
= _mesa_tex_target_to_face(target
);
2502 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2503 format
, type
, 3, width
, height
, depth
, border
)) {
2504 return; /* error was recorded */
2507 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2508 _mesa_update_state(ctx
);
2510 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2511 _mesa_lock_texture(ctx
, texObj
);
2513 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2515 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2518 if (texImage
->Data
) {
2519 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2522 ASSERT(texImage
->Data
== NULL
);
2523 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2524 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2525 width
, height
, depth
,
2526 border
, internalFormat
);
2528 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2529 internalFormat
, format
, type
);
2531 /* Give the texture to the driver. <pixels> may be null. */
2532 ASSERT(ctx
->Driver
.TexImage3D
);
2533 ctx
->Driver
.TexImage3D(ctx
, target
, level
, internalFormat
,
2534 width
, height
, depth
, border
, format
, type
,
2535 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2537 ASSERT(texImage
->TexFormat
);
2539 _mesa_set_fetch_functions(texImage
, 3);
2541 check_gen_mipmap(ctx
, target
, texObj
, level
);
2543 update_fbo_texture(ctx
, texObj
, face
, level
);
2546 texObj
->_Complete
= GL_FALSE
;
2547 ctx
->NewState
|= _NEW_TEXTURE
;
2550 _mesa_unlock_texture(ctx
, texObj
);
2552 else if (target
== GL_PROXY_TEXTURE_3D
||
2553 (ctx
->Extensions
.MESA_texture_array
&&
2554 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
)) {
2555 /* Proxy texture: check for errors and update proxy state */
2556 struct gl_texture_image
*texImage
;
2557 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2558 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2559 format
, type
, 3, width
, height
, depth
, border
)) {
2560 /* when error, clear all proxy texture image parameters */
2562 clear_teximage_fields(texImage
);
2565 /* no error, set the tex image parameters */
2566 struct gl_texture_object
*texObj
=
2567 _mesa_get_current_tex_object(ctx
, target
);
2568 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
2569 depth
, border
, internalFormat
);
2570 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2571 internalFormat
, format
, type
);
2575 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2582 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2583 GLsizei width
, GLsizei height
, GLsizei depth
,
2584 GLint border
, GLenum format
, GLenum type
,
2585 const GLvoid
*pixels
)
2587 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2588 depth
, border
, format
, type
, pixels
);
2592 #if FEATURE_OES_EGL_image
2594 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
2596 struct gl_texture_object
*texObj
;
2597 struct gl_texture_image
*texImage
;
2598 GET_CURRENT_CONTEXT(ctx
);
2599 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2601 if (!ctx
->Extensions
.OES_EGL_image
) {
2602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2603 "glEGLImageTargetTexture2DOES(unsupported)");
2607 if (target
!= GL_TEXTURE_2D
) {
2608 _mesa_error(ctx
, GL_INVALID_ENUM
,
2609 "glEGLImageTargetTexture2D(target=%d)", target
);
2613 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2614 _mesa_update_state(ctx
);
2616 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2617 _mesa_lock_texture(ctx
, texObj
);
2619 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
2621 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
2624 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2626 ASSERT(texImage
->Data
== NULL
);
2627 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
2628 texObj
, texImage
, image
);
2631 texObj
->_Complete
= GL_FALSE
;
2632 ctx
->NewState
|= _NEW_TEXTURE
;
2634 _mesa_unlock_texture(ctx
, texObj
);
2641 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2642 GLint xoffset
, GLsizei width
,
2643 GLenum format
, GLenum type
,
2644 const GLvoid
*pixels
)
2646 GLsizei postConvWidth
= width
;
2647 struct gl_texture_object
*texObj
;
2648 struct gl_texture_image
*texImage
;
2649 GET_CURRENT_CONTEXT(ctx
);
2650 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2652 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2653 _mesa_debug(ctx
, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2654 _mesa_lookup_enum_by_nr(target
), level
,
2655 xoffset
, width
, _mesa_lookup_enum_by_nr(format
),
2656 _mesa_lookup_enum_by_nr(type
), pixels
);
2658 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2659 _mesa_update_state(ctx
);
2661 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2662 postConvWidth
, 1, 1, format
, type
)) {
2663 return; /* error was detected */
2667 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2670 _mesa_lock_texture(ctx
, texObj
);
2672 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2674 if (subtexture_error_check2(ctx
, 1, target
, level
, xoffset
, 0, 0,
2675 postConvWidth
, 1, 1,
2676 format
, type
, texImage
)) {
2677 /* error was recorded */
2679 else if (width
> 0) {
2680 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2681 xoffset
+= texImage
->Border
;
2683 ASSERT(ctx
->Driver
.TexSubImage1D
);
2684 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
, width
,
2685 format
, type
, pixels
, &ctx
->Unpack
,
2688 check_gen_mipmap(ctx
, target
, texObj
, level
);
2690 ctx
->NewState
|= _NEW_TEXTURE
;
2693 _mesa_unlock_texture(ctx
, texObj
);
2698 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2699 GLint xoffset
, GLint yoffset
,
2700 GLsizei width
, GLsizei height
,
2701 GLenum format
, GLenum type
,
2702 const GLvoid
*pixels
)
2704 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2705 struct gl_texture_object
*texObj
;
2706 struct gl_texture_image
*texImage
;
2707 GET_CURRENT_CONTEXT(ctx
);
2708 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2710 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2711 _mesa_debug(ctx
, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2712 _mesa_lookup_enum_by_nr(target
), level
,
2713 xoffset
, yoffset
, width
, height
,
2714 _mesa_lookup_enum_by_nr(format
),
2715 _mesa_lookup_enum_by_nr(type
), pixels
);
2717 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2718 _mesa_update_state(ctx
);
2720 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2721 postConvWidth
, postConvHeight
, 1, format
, type
)) {
2722 return; /* error was detected */
2725 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2727 _mesa_lock_texture(ctx
, texObj
);
2729 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2731 if (subtexture_error_check2(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2732 postConvWidth
, postConvHeight
, 1,
2733 format
, type
, texImage
)) {
2734 /* error was recorded */
2736 else if (width
> 0 && height
>= 0) {
2737 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2738 xoffset
+= texImage
->Border
;
2739 yoffset
+= texImage
->Border
;
2741 ASSERT(ctx
->Driver
.TexSubImage2D
);
2742 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2743 width
, height
, format
, type
, pixels
,
2744 &ctx
->Unpack
, texObj
, texImage
);
2746 check_gen_mipmap(ctx
, target
, texObj
, level
);
2748 ctx
->NewState
|= _NEW_TEXTURE
;
2751 _mesa_unlock_texture(ctx
, texObj
);
2757 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2758 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2759 GLsizei width
, GLsizei height
, GLsizei depth
,
2760 GLenum format
, GLenum type
,
2761 const GLvoid
*pixels
)
2763 struct gl_texture_object
*texObj
;
2764 struct gl_texture_image
*texImage
;
2765 GET_CURRENT_CONTEXT(ctx
);
2766 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2768 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2769 _mesa_debug(ctx
, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2770 _mesa_lookup_enum_by_nr(target
), level
,
2771 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2772 _mesa_lookup_enum_by_nr(format
),
2773 _mesa_lookup_enum_by_nr(type
), pixels
);
2775 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2776 _mesa_update_state(ctx
);
2778 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2779 width
, height
, depth
, format
, type
)) {
2780 return; /* error was detected */
2783 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2785 _mesa_lock_texture(ctx
, texObj
);
2787 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2789 if (subtexture_error_check2(ctx
, 3, target
, level
,
2790 xoffset
, yoffset
, zoffset
,
2791 width
, height
, depth
,
2792 format
, type
, texImage
)) {
2793 /* error was recorded */
2795 else if (width
> 0 && height
> 0 && height
> 0) {
2796 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2797 xoffset
+= texImage
->Border
;
2798 yoffset
+= texImage
->Border
;
2799 zoffset
+= texImage
->Border
;
2801 ASSERT(ctx
->Driver
.TexSubImage3D
);
2802 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
,
2803 xoffset
, yoffset
, zoffset
,
2804 width
, height
, depth
,
2805 format
, type
, pixels
,
2806 &ctx
->Unpack
, texObj
, texImage
);
2808 check_gen_mipmap(ctx
, target
, texObj
, level
);
2810 ctx
->NewState
|= _NEW_TEXTURE
;
2813 _mesa_unlock_texture(ctx
, texObj
);
2819 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2820 GLenum internalFormat
,
2822 GLsizei width
, GLint border
)
2824 struct gl_texture_object
*texObj
;
2825 struct gl_texture_image
*texImage
;
2826 GLsizei postConvWidth
= width
;
2827 const GLuint face
= _mesa_tex_target_to_face(target
);
2828 GET_CURRENT_CONTEXT(ctx
);
2829 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2831 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2832 _mesa_debug(ctx
, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2833 _mesa_lookup_enum_by_nr(target
), level
,
2834 _mesa_lookup_enum_by_nr(internalFormat
),
2835 x
, y
, width
, border
);
2837 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2838 _mesa_update_state(ctx
);
2840 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2841 postConvWidth
, 1, border
))
2844 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2846 _mesa_lock_texture(ctx
, texObj
);
2848 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2850 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2853 if (texImage
->Data
) {
2854 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2857 ASSERT(texImage
->Data
== NULL
);
2859 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2860 _mesa_init_teximage_fields(ctx
, target
, texImage
, postConvWidth
, 1, 1,
2861 border
, internalFormat
);
2863 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2864 internalFormat
, GL_NONE
, GL_NONE
);
2866 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2867 ctx
->Driver
.CopyTexImage1D(ctx
, target
, level
, internalFormat
,
2868 x
, y
, width
, border
);
2870 ASSERT(texImage
->TexFormat
);
2872 _mesa_set_fetch_functions(texImage
, 1);
2874 check_gen_mipmap(ctx
, target
, texObj
, level
);
2876 update_fbo_texture(ctx
, texObj
, face
, level
);
2879 texObj
->_Complete
= GL_FALSE
;
2880 ctx
->NewState
|= _NEW_TEXTURE
;
2883 _mesa_unlock_texture(ctx
, texObj
);
2889 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2890 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2893 struct gl_texture_object
*texObj
;
2894 struct gl_texture_image
*texImage
;
2895 GLsizei postConvWidth
= width
, postConvHeight
= height
;
2896 const GLuint face
= _mesa_tex_target_to_face(target
);
2897 GET_CURRENT_CONTEXT(ctx
);
2898 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2900 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2901 _mesa_debug(ctx
, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2902 _mesa_lookup_enum_by_nr(target
), level
,
2903 _mesa_lookup_enum_by_nr(internalFormat
),
2904 x
, y
, width
, height
, border
);
2906 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2907 _mesa_update_state(ctx
);
2909 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2910 postConvWidth
, postConvHeight
, border
))
2913 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2915 _mesa_lock_texture(ctx
, texObj
);
2917 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2920 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2923 if (texImage
->Data
) {
2924 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2927 ASSERT(texImage
->Data
== NULL
);
2929 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2930 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2931 postConvWidth
, postConvHeight
, 1,
2932 border
, internalFormat
);
2934 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2935 internalFormat
, GL_NONE
, GL_NONE
);
2937 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2938 ctx
->Driver
.CopyTexImage2D(ctx
, target
, level
, internalFormat
,
2939 x
, y
, width
, height
, border
);
2941 ASSERT(texImage
->TexFormat
);
2943 _mesa_set_fetch_functions(texImage
, 2);
2945 check_gen_mipmap(ctx
, target
, texObj
, level
);
2947 update_fbo_texture(ctx
, texObj
, face
, level
);
2950 texObj
->_Complete
= GL_FALSE
;
2951 ctx
->NewState
|= _NEW_TEXTURE
;
2954 _mesa_unlock_texture(ctx
, texObj
);
2959 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2960 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2962 struct gl_texture_object
*texObj
;
2963 struct gl_texture_image
*texImage
;
2964 GLsizei postConvWidth
= width
;
2968 GET_CURRENT_CONTEXT(ctx
);
2969 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2971 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2972 _mesa_debug(ctx
, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
2973 _mesa_lookup_enum_by_nr(target
),
2974 level
, xoffset
, x
, y
, width
);
2976 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2977 _mesa_update_state(ctx
);
2979 if (copytexsubimage_error_check1(ctx
, 1, target
, level
))
2982 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2984 _mesa_lock_texture(ctx
, texObj
);
2986 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2988 if (copytexsubimage_error_check2(ctx
, 1, target
, level
,
2989 xoffset
, 0, 0, postConvWidth
, 1,
2991 /* error was recorded */
2994 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2995 xoffset
+= texImage
->Border
;
2997 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
2999 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
3000 ctx
->Driver
.CopyTexSubImage1D(ctx
, target
, level
,
3001 xoffset
, x
, y
, width
);
3003 check_gen_mipmap(ctx
, target
, texObj
, level
);
3005 ctx
->NewState
|= _NEW_TEXTURE
;
3009 _mesa_unlock_texture(ctx
, texObj
);
3015 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3016 GLint xoffset
, GLint yoffset
,
3017 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3019 struct gl_texture_object
*texObj
;
3020 struct gl_texture_image
*texImage
;
3021 GLsizei postConvWidth
= width
, postConvHeight
= height
;
3022 GET_CURRENT_CONTEXT(ctx
);
3023 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3025 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3026 _mesa_debug(ctx
, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
3027 _mesa_lookup_enum_by_nr(target
),
3028 level
, xoffset
, yoffset
, x
, y
, width
, height
);
3030 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3031 _mesa_update_state(ctx
);
3033 if (copytexsubimage_error_check1(ctx
, 2, target
, level
))
3036 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3038 _mesa_lock_texture(ctx
, texObj
);
3040 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3042 if (copytexsubimage_error_check2(ctx
, 2, target
, level
,
3043 xoffset
, yoffset
, 0,
3044 postConvWidth
, postConvHeight
,
3046 /* error was recorded */
3049 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3050 xoffset
+= texImage
->Border
;
3051 yoffset
+= texImage
->Border
;
3053 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3055 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
3056 ctx
->Driver
.CopyTexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
3057 x
, y
, width
, height
);
3059 check_gen_mipmap(ctx
, target
, texObj
, level
);
3061 ctx
->NewState
|= _NEW_TEXTURE
;
3065 _mesa_unlock_texture(ctx
, texObj
);
3071 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3072 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3073 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3075 struct gl_texture_object
*texObj
;
3076 struct gl_texture_image
*texImage
;
3077 GLsizei postConvWidth
= width
, postConvHeight
= height
;
3078 GET_CURRENT_CONTEXT(ctx
);
3079 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3081 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3082 _mesa_debug(ctx
, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
3083 _mesa_lookup_enum_by_nr(target
),
3084 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3086 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3087 _mesa_update_state(ctx
);
3089 if (copytexsubimage_error_check1(ctx
, 3, target
, level
))
3092 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3094 _mesa_lock_texture(ctx
, texObj
);
3096 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3098 if (copytexsubimage_error_check2(ctx
, 3, target
, level
, xoffset
, yoffset
,
3099 zoffset
, postConvWidth
, postConvHeight
,
3101 /* error was recored */
3104 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3105 xoffset
+= texImage
->Border
;
3106 yoffset
+= texImage
->Border
;
3107 zoffset
+= texImage
->Border
;
3109 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3111 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
3112 ctx
->Driver
.CopyTexSubImage3D(ctx
, target
, level
,
3113 xoffset
, yoffset
, zoffset
,
3114 x
, y
, width
, height
);
3116 check_gen_mipmap(ctx
, target
, texObj
, level
);
3118 ctx
->NewState
|= _NEW_TEXTURE
;
3122 _mesa_unlock_texture(ctx
, texObj
);
3128 /**********************************************************************/
3129 /****** Compressed Textures ******/
3130 /**********************************************************************/
3134 * Return expected size of a compressed texture.
3137 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
3140 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3141 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
3146 * Return compressed texture block size, in pixels.
3149 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
3151 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3152 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
3157 * Error checking for glCompressedTexImage[123]D().
3158 * \return error code or GL_NO_ERROR.
3161 compressed_texture_error_check(GLcontext
*ctx
, GLint dimensions
,
3162 GLenum target
, GLint level
,
3163 GLenum internalFormat
, GLsizei width
,
3164 GLsizei height
, GLsizei depth
, GLint border
,
3167 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3169 if (dimensions
== 1) {
3170 /* 1D compressed textures not allowed */
3171 return GL_INVALID_ENUM
;
3173 else if (dimensions
== 2) {
3174 if (target
== GL_PROXY_TEXTURE_2D
) {
3175 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3177 else if (target
== GL_TEXTURE_2D
) {
3178 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3180 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3181 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3182 return GL_INVALID_ENUM
; /*target*/
3183 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3185 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3186 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3187 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3188 return GL_INVALID_ENUM
; /*target*/
3189 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3192 return GL_INVALID_ENUM
; /*target*/
3195 else if (dimensions
== 3) {
3196 /* 3D compressed textures not allowed */
3197 return GL_INVALID_ENUM
;
3201 return GL_INVALID_ENUM
;
3204 maxTextureSize
= 1 << (maxLevels
- 1);
3206 /* This will detect any invalid internalFormat value */
3207 if (!_mesa_is_compressed_format(ctx
, internalFormat
))
3208 return GL_INVALID_ENUM
;
3210 /* This should really never fail */
3211 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
3212 return GL_INVALID_ENUM
;
3215 return GL_INVALID_VALUE
;
3218 * XXX We should probably use the proxy texture error check function here.
3220 if (width
< 1 || width
> maxTextureSize
||
3221 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(width
)))
3222 return GL_INVALID_VALUE
;
3224 if ((height
< 1 || height
> maxTextureSize
||
3225 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(height
)))
3227 return GL_INVALID_VALUE
;
3229 if ((depth
< 1 || depth
> maxTextureSize
||
3230 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(depth
)))
3232 return GL_INVALID_VALUE
;
3234 /* For cube map, width must equal height */
3235 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3236 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
3237 return GL_INVALID_VALUE
;
3239 if (level
< 0 || level
>= maxLevels
)
3240 return GL_INVALID_VALUE
;
3242 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
3243 if (expectedSize
!= imageSize
)
3244 return GL_INVALID_VALUE
;
3246 #if FEATURE_EXT_texture_sRGB
3247 if ((internalFormat
== GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
||
3248 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
||
3249 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
||
3250 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
)
3252 return GL_INVALID_OPERATION
;
3261 * Error checking for glCompressedTexSubImage[123]D().
3262 * \warning There are some bad assumptions here about the size of compressed
3263 * texture tiles (multiple of 4) used to test the validity of the
3264 * offset and size parameters.
3265 * \return error code or GL_NO_ERROR.
3268 compressed_subtexture_error_check(GLcontext
*ctx
, GLint dimensions
,
3269 GLenum target
, GLint level
,
3270 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3271 GLsizei width
, GLsizei height
, GLsizei depth
,
3272 GLenum format
, GLsizei imageSize
)
3274 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3278 if (dimensions
== 1) {
3279 /* 1D compressed textures not allowed */
3280 return GL_INVALID_ENUM
;
3282 else if (dimensions
== 2) {
3283 if (target
== GL_PROXY_TEXTURE_2D
) {
3284 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3286 else if (target
== GL_TEXTURE_2D
) {
3287 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3289 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3290 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3291 return GL_INVALID_ENUM
; /*target*/
3292 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3294 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3295 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3296 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3297 return GL_INVALID_ENUM
; /*target*/
3298 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3301 return GL_INVALID_ENUM
; /*target*/
3304 else if (dimensions
== 3) {
3305 /* 3D compressed textures not allowed */
3306 return GL_INVALID_ENUM
;
3309 maxTextureSize
= 1 << (maxLevels
- 1);
3311 /* this will catch any invalid compressed format token */
3312 if (!_mesa_is_compressed_format(ctx
, format
))
3313 return GL_INVALID_ENUM
;
3315 if (width
< 1 || width
> maxTextureSize
)
3316 return GL_INVALID_VALUE
;
3318 if ((height
< 1 || height
> maxTextureSize
)
3320 return GL_INVALID_VALUE
;
3322 if (level
< 0 || level
>= maxLevels
)
3323 return GL_INVALID_VALUE
;
3326 * do checks which depend on compression block size
3328 get_compressed_block_size(format
, &bw
, &bh
);
3330 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3331 return GL_INVALID_VALUE
;
3333 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3334 return GL_INVALID_VALUE
;
3336 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3337 return GL_INVALID_VALUE
;
3339 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3340 if (expectedSize
!= imageSize
)
3341 return GL_INVALID_VALUE
;
3348 * Do second part of glCompressedTexSubImage error checking.
3349 * \return GL_TRUE if error found, GL_FALSE otherwise.
3352 compressed_subtexture_error_check2(GLcontext
*ctx
, GLuint dims
,
3353 GLsizei width
, GLsizei height
,
3354 GLsizei depth
, GLenum format
,
3355 struct gl_texture_image
*texImage
)
3358 if ((GLint
) format
!= texImage
->InternalFormat
) {
3359 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3360 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3364 if (((width
== 1 || width
== 2) &&
3365 width
!= (GLsizei
) texImage
->Width
) ||
3366 (width
> (GLsizei
) texImage
->Width
)) {
3367 _mesa_error(ctx
, GL_INVALID_VALUE
,
3368 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3373 if (((height
== 1 || height
== 2) &&
3374 height
!= (GLsizei
) texImage
->Height
) ||
3375 (height
> (GLsizei
) texImage
->Height
)) {
3376 _mesa_error(ctx
, GL_INVALID_VALUE
,
3377 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3383 if (((depth
== 1 || depth
== 2) &&
3384 depth
!= (GLsizei
) texImage
->Depth
) ||
3385 (depth
> (GLsizei
) texImage
->Depth
)) {
3386 _mesa_error(ctx
, GL_INVALID_VALUE
,
3387 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3398 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3399 GLenum internalFormat
, GLsizei width
,
3400 GLint border
, GLsizei imageSize
,
3403 GET_CURRENT_CONTEXT(ctx
);
3404 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3406 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3407 _mesa_debug(ctx
, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3408 _mesa_lookup_enum_by_nr(target
), level
,
3409 _mesa_lookup_enum_by_nr(internalFormat
),
3410 width
, border
, imageSize
, data
);
3412 if (target
== GL_TEXTURE_1D
) {
3413 /* non-proxy target */
3414 struct gl_texture_object
*texObj
;
3415 struct gl_texture_image
*texImage
;
3416 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3417 internalFormat
, width
, 1, 1, border
, imageSize
);
3419 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
3423 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3425 _mesa_lock_texture(ctx
, texObj
);
3427 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3429 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
3432 if (texImage
->Data
) {
3433 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3435 ASSERT(texImage
->Data
== NULL
);
3437 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3438 border
, internalFormat
);
3440 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3441 internalFormat
, GL_NONE
, GL_NONE
);
3443 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
3444 ctx
->Driver
.CompressedTexImage1D(ctx
, target
, level
,
3445 internalFormat
, width
, border
,
3449 _mesa_set_fetch_functions(texImage
, 1);
3451 check_gen_mipmap(ctx
, target
, texObj
, level
);
3454 texObj
->_Complete
= GL_FALSE
;
3455 ctx
->NewState
|= _NEW_TEXTURE
;
3458 _mesa_unlock_texture(ctx
, texObj
);
3460 else if (target
== GL_PROXY_TEXTURE_1D
) {
3461 /* Proxy texture: check for errors and update proxy state */
3462 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3463 internalFormat
, width
, 1, 1, border
, imageSize
);
3465 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3466 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3467 internalFormat
, GL_NONE
, GL_NONE
,
3468 width
, 1, 1, border
);
3471 /* if error, clear all proxy texture image parameters */
3472 struct gl_texture_image
*texImage
;
3473 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3475 clear_teximage_fields(texImage
);
3478 /* store the teximage parameters */
3479 struct gl_texture_object
*texObj
;
3480 struct gl_texture_image
*texImage
;
3482 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3484 _mesa_lock_texture(ctx
, texObj
);
3486 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3487 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3488 border
, internalFormat
);
3489 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3490 internalFormat
, GL_NONE
, GL_NONE
);
3492 _mesa_unlock_texture(ctx
, texObj
);
3496 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
3502 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3503 GLenum internalFormat
, GLsizei width
,
3504 GLsizei height
, GLint border
, GLsizei imageSize
,
3507 GET_CURRENT_CONTEXT(ctx
);
3508 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3510 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3511 _mesa_debug(ctx
, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3512 _mesa_lookup_enum_by_nr(target
), level
,
3513 _mesa_lookup_enum_by_nr(internalFormat
),
3514 width
, height
, border
, imageSize
, data
);
3517 switch (internalFormat
) {
3518 case GL_PALETTE4_RGB8_OES
:
3519 case GL_PALETTE4_RGBA8_OES
:
3520 case GL_PALETTE4_R5_G6_B5_OES
:
3521 case GL_PALETTE4_RGBA4_OES
:
3522 case GL_PALETTE4_RGB5_A1_OES
:
3523 case GL_PALETTE8_RGB8_OES
:
3524 case GL_PALETTE8_RGBA8_OES
:
3525 case GL_PALETTE8_R5_G6_B5_OES
:
3526 case GL_PALETTE8_RGBA4_OES
:
3527 case GL_PALETTE8_RGB5_A1_OES
:
3528 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3529 width
, height
, imageSize
, data
);
3534 if (target
== GL_TEXTURE_2D
||
3535 (ctx
->Extensions
.ARB_texture_cube_map
&&
3536 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3537 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
3538 /* non-proxy target */
3539 struct gl_texture_object
*texObj
;
3540 struct gl_texture_image
*texImage
;
3542 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3543 internalFormat
, width
, height
, 1, border
, imageSize
);
3545 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
3549 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3551 _mesa_lock_texture(ctx
, texObj
);
3553 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3555 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
3558 if (texImage
->Data
) {
3559 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3561 ASSERT(texImage
->Data
== NULL
);
3563 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3564 border
, internalFormat
);
3566 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3567 internalFormat
, GL_NONE
, GL_NONE
);
3569 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3570 ctx
->Driver
.CompressedTexImage2D(ctx
, target
, level
,
3571 internalFormat
, width
, height
,
3572 border
, imageSize
, data
,
3575 _mesa_set_fetch_functions(texImage
, 2);
3577 check_gen_mipmap(ctx
, target
, texObj
, level
);
3580 texObj
->_Complete
= GL_FALSE
;
3581 ctx
->NewState
|= _NEW_TEXTURE
;
3584 _mesa_unlock_texture(ctx
, texObj
);
3586 else if (target
== GL_PROXY_TEXTURE_2D
||
3587 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
3588 ctx
->Extensions
.ARB_texture_cube_map
)) {
3589 /* Proxy texture: check for errors and update proxy state */
3590 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3591 internalFormat
, width
, height
, 1, border
, imageSize
);
3593 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3594 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3595 internalFormat
, GL_NONE
, GL_NONE
,
3596 width
, height
, 1, border
);
3599 /* if error, clear all proxy texture image parameters */
3600 struct gl_texture_image
*texImage
;
3601 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3603 clear_teximage_fields(texImage
);
3606 /* store the teximage parameters */
3607 struct gl_texture_object
*texObj
;
3608 struct gl_texture_image
*texImage
;
3610 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3612 _mesa_lock_texture(ctx
, texObj
);
3614 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3615 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3616 border
, internalFormat
);
3617 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3618 internalFormat
, GL_NONE
, GL_NONE
);
3620 _mesa_unlock_texture(ctx
, texObj
);
3624 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3631 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3632 GLenum internalFormat
, GLsizei width
,
3633 GLsizei height
, GLsizei depth
, GLint border
,
3634 GLsizei imageSize
, const GLvoid
*data
)
3636 GET_CURRENT_CONTEXT(ctx
);
3637 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3639 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3640 _mesa_debug(ctx
, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3641 _mesa_lookup_enum_by_nr(target
), level
,
3642 _mesa_lookup_enum_by_nr(internalFormat
),
3643 width
, height
, depth
, border
, imageSize
, data
);
3645 if (target
== GL_TEXTURE_3D
) {
3646 /* non-proxy target */
3647 struct gl_texture_object
*texObj
;
3648 struct gl_texture_image
*texImage
;
3649 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3650 internalFormat
, width
, height
, depth
, border
, imageSize
);
3652 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3656 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3658 _mesa_lock_texture(ctx
, texObj
);
3660 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3662 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3665 if (texImage
->Data
) {
3666 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3668 ASSERT(texImage
->Data
== NULL
);
3670 _mesa_init_teximage_fields(ctx
, target
, texImage
,
3671 width
, height
, depth
,
3672 border
, internalFormat
);
3674 /* Choose actual texture format */
3675 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3676 internalFormat
, GL_NONE
, GL_NONE
);
3678 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3679 ctx
->Driver
.CompressedTexImage3D(ctx
, target
, level
,
3681 width
, height
, depth
,
3682 border
, imageSize
, data
,
3685 _mesa_set_fetch_functions(texImage
, 3);
3687 check_gen_mipmap(ctx
, target
, texObj
, level
);
3690 texObj
->_Complete
= GL_FALSE
;
3691 ctx
->NewState
|= _NEW_TEXTURE
;
3694 _mesa_unlock_texture(ctx
, texObj
);
3696 else if (target
== GL_PROXY_TEXTURE_3D
) {
3697 /* Proxy texture: check for errors and update proxy state */
3698 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3699 internalFormat
, width
, height
, depth
, border
, imageSize
);
3701 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3702 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3703 internalFormat
, GL_NONE
, GL_NONE
,
3704 width
, height
, depth
, border
);
3707 /* if error, clear all proxy texture image parameters */
3708 struct gl_texture_image
*texImage
;
3709 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3711 clear_teximage_fields(texImage
);
3714 /* store the teximage parameters */
3715 struct gl_texture_object
*texObj
;
3716 struct gl_texture_image
*texImage
;
3718 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3720 _mesa_lock_texture(ctx
, texObj
);
3722 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3723 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3724 depth
, border
, internalFormat
);
3725 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3726 internalFormat
, GL_NONE
, GL_NONE
);
3728 _mesa_unlock_texture(ctx
, texObj
);
3732 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3739 * Common helper for glCompressedTexSubImage1/2/3D().
3742 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3743 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3744 GLsizei width
, GLsizei height
, GLsizei depth
,
3745 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3747 struct gl_texture_object
*texObj
;
3748 struct gl_texture_image
*texImage
;
3750 GET_CURRENT_CONTEXT(ctx
);
3751 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3753 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3754 xoffset
, 0, 0, /* pos */
3755 width
, height
, depth
, /* size */
3758 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3762 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3764 _mesa_lock_texture(ctx
, texObj
);
3766 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3769 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3770 format
, texImage
)) {
3771 /* error was recorded */
3773 else if (width
> 0 && height
> 0 && depth
> 0) {
3776 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3777 ctx
->Driver
.CompressedTexSubImage1D(ctx
, target
, level
,
3779 format
, imageSize
, data
,
3784 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3785 ctx
->Driver
.CompressedTexSubImage2D(ctx
, target
, level
,
3788 format
, imageSize
, data
,
3793 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3794 ctx
->Driver
.CompressedTexSubImage3D(ctx
, target
, level
,
3795 xoffset
, yoffset
, zoffset
,
3796 width
, height
, depth
,
3797 format
, imageSize
, data
,
3805 check_gen_mipmap(ctx
, target
, texObj
, level
);
3807 ctx
->NewState
|= _NEW_TEXTURE
;
3810 _mesa_unlock_texture(ctx
, texObj
);
3815 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3816 GLsizei width
, GLenum format
,
3817 GLsizei imageSize
, const GLvoid
*data
)
3819 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3820 format
, imageSize
, data
);
3825 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3826 GLint yoffset
, GLsizei width
, GLsizei height
,
3827 GLenum format
, GLsizei imageSize
,
3830 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3831 width
, height
, 1, format
, imageSize
, data
);
3836 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3837 GLint yoffset
, GLint zoffset
, GLsizei width
,
3838 GLsizei height
, GLsizei depth
, GLenum format
,
3839 GLsizei imageSize
, const GLvoid
*data
)
3841 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3842 width
, height
, depth
, format
, imageSize
, data
);