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( struct gl_context
*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
.ARB_texture_compression_rgtc
) {
490 switch (internalFormat
) {
491 case GL_COMPRESSED_RED_RGTC1
:
492 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
494 case GL_COMPRESSED_RG_RGTC2
:
495 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
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( struct gl_context
*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(struct gl_context
*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( struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*ctx
, GLenum target
)
1235 case GL_PROXY_TEXTURE_2D
:
1237 case GL_PROXY_TEXTURE_CUBE_MAP
:
1238 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1239 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1240 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1241 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1242 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1243 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1244 return ctx
->Extensions
.ARB_texture_cube_map
;
1245 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1246 case GL_TEXTURE_2D_ARRAY_EXT
:
1247 return ctx
->Extensions
.MESA_texture_array
;
1255 * Test the glTexImage[123]D() parameters for errors.
1257 * \param ctx GL context.
1258 * \param target texture target given by the user.
1259 * \param level image level given by the user.
1260 * \param internalFormat internal format given by the user.
1261 * \param format pixel data format given by the user.
1262 * \param type pixel data type given by the user.
1263 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1264 * \param width image width given by the user.
1265 * \param height image height given by the user.
1266 * \param depth image depth given by the user.
1267 * \param border image border given by the user.
1269 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1271 * Verifies each of the parameters against the constants specified in
1272 * __struct gl_contextRec::Const and the supported extensions, and according to the
1273 * OpenGL specification.
1276 texture_error_check( struct gl_context
*ctx
, GLenum target
,
1277 GLint level
, GLint internalFormat
,
1278 GLenum format
, GLenum type
,
1280 GLint width
, GLint height
,
1281 GLint depth
, GLint border
)
1283 const GLboolean isProxy
= _mesa_is_proxy_texture(target
);
1284 GLboolean sizeOK
= GL_TRUE
;
1285 GLboolean colorFormat
, indexFormat
;
1286 GLenum proxy_target
;
1288 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1289 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1291 _mesa_error(ctx
, GL_INVALID_VALUE
,
1292 "glTexImage%dD(level=%d)", dimensions
, level
);
1298 if (border
< 0 || border
> 1 ||
1299 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1300 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1302 _mesa_error(ctx
, GL_INVALID_VALUE
,
1303 "glTexImage%dD(border=%d)", dimensions
, border
);
1308 if (width
< 0 || height
< 0 || depth
< 0) {
1310 _mesa_error(ctx
, GL_INVALID_VALUE
,
1311 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1316 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1317 * level, width, height and depth.
1319 if (dimensions
== 1) {
1320 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1321 proxy_target
= GL_PROXY_TEXTURE_1D
;
1326 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1330 else if (dimensions
== 2) {
1332 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1333 proxy_target
= GL_PROXY_TEXTURE_2D
;
1335 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1336 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1337 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1338 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1339 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1342 proxy_target
= GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
1343 sizeOK
= (width
== height
);
1345 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1346 target
== GL_TEXTURE_RECTANGLE_NV
) {
1347 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1348 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1351 proxy_target
= GL_PROXY_TEXTURE_RECTANGLE_NV
;
1353 else if (target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
1354 target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1355 proxy_target
= GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
1358 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1362 else if (dimensions
== 3) {
1363 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1364 proxy_target
= GL_PROXY_TEXTURE_3D
;
1366 else if (target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
||
1367 target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1368 proxy_target
= GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
1371 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1376 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1380 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target
, level
,
1381 internalFormat
, format
,
1382 type
, width
, height
,
1386 _mesa_error(ctx
, GL_INVALID_VALUE
,
1387 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1388 dimensions
, level
, width
, height
, depth
);
1393 /* Check internalFormat */
1394 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1396 _mesa_error(ctx
, GL_INVALID_VALUE
,
1397 "glTexImage%dD(internalFormat=%s)",
1398 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1403 /* Check incoming image format and type */
1404 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1405 /* Normally, GL_INVALID_OPERATION is generated by a format/type
1406 * mismatch (see the 1.2 spec page 94, sec 3.6.4.). But with the
1407 * GL_EXT_texture_integer extension, some combinations should generate
1408 * GL_INVALID_ENUM instead (grr!).
1411 GLenum error
= _mesa_is_integer_format(format
)
1412 ? GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
1413 _mesa_error(ctx
, error
,
1414 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1415 dimensions
, format
, type
);
1420 /* make sure internal format and format basically agree */
1421 colorFormat
= _mesa_is_color_format(format
);
1422 indexFormat
= _mesa_is_index_format(format
);
1423 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1424 (_mesa_is_index_format(internalFormat
) && !indexFormat
) ||
1425 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1426 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1427 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1428 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1430 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1431 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1432 dimensions
, internalFormat
, format
);
1436 /* additional checks for ycbcr textures */
1437 if (internalFormat
== GL_YCBCR_MESA
) {
1438 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1439 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1440 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1442 _mesa_snprintf(message
, sizeof(message
),
1443 "glTexImage%dD(format/type YCBCR mismatch", dimensions
);
1444 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1445 return GL_TRUE
; /* error */
1447 if (target
!= GL_TEXTURE_2D
&&
1448 target
!= GL_PROXY_TEXTURE_2D
&&
1449 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1450 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1452 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1458 _mesa_snprintf(message
, sizeof(message
),
1459 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1460 dimensions
, border
);
1461 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1467 /* additional checks for depth textures */
1468 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1469 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1470 if (target
!= GL_TEXTURE_1D
&&
1471 target
!= GL_PROXY_TEXTURE_1D
&&
1472 target
!= GL_TEXTURE_2D
&&
1473 target
!= GL_PROXY_TEXTURE_2D
&&
1474 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1475 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
) {
1477 _mesa_error(ctx
, GL_INVALID_ENUM
,
1478 "glTexImage(target/internalFormat)");
1483 /* additional checks for compressed textures */
1484 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1485 if (!target_can_be_compressed(ctx
, target
) && !isProxy
) {
1486 _mesa_error(ctx
, GL_INVALID_ENUM
,
1487 "glTexImage%dD(target)", dimensions
);
1492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1493 "glTexImage%dD(border!=0)", dimensions
);
1499 /* additional checks for integer textures */
1500 if (ctx
->Extensions
.EXT_texture_integer
&&
1501 (_mesa_is_integer_format(format
) !=
1502 _mesa_is_integer_format(internalFormat
))) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glTexImage%dD(integer/non-integer format mismatch)",
1511 /* if we get here, the parameters are OK */
1517 * Test glTexSubImage[123]D() parameters for errors.
1519 * \param ctx GL context.
1520 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1521 * \param target texture target given by the user.
1522 * \param level image level given by the user.
1523 * \param xoffset sub-image x offset given by the user.
1524 * \param yoffset sub-image y offset given by the user.
1525 * \param zoffset sub-image z offset given by the user.
1526 * \param format pixel data format given by the user.
1527 * \param type pixel data type given by the user.
1528 * \param width image width given by the user.
1529 * \param height image height given by the user.
1530 * \param depth image depth given by the user.
1532 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1534 * Verifies each of the parameters against the constants specified in
1535 * __struct gl_contextRec::Const and the supported extensions, and according to the
1536 * OpenGL specification.
1539 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1540 GLenum target
, GLint level
,
1541 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1542 GLint width
, GLint height
, GLint depth
,
1543 GLenum format
, GLenum type
)
1546 if (dimensions
== 1) {
1547 if (target
!= GL_TEXTURE_1D
) {
1548 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1552 else if (dimensions
== 2) {
1553 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1554 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1555 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1556 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1560 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1561 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1562 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1566 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1567 if (!ctx
->Extensions
.MESA_texture_array
) {
1568 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1572 else if (target
!= GL_TEXTURE_2D
) {
1573 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1577 else if (dimensions
== 3) {
1578 if (target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1579 if (!ctx
->Extensions
.MESA_texture_array
) {
1580 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1584 else if (target
!= GL_TEXTURE_3D
) {
1585 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1590 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1594 /* Basic level check */
1595 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1596 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1601 _mesa_error(ctx
, GL_INVALID_VALUE
,
1602 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1605 if (height
< 0 && dimensions
> 1) {
1606 _mesa_error(ctx
, GL_INVALID_VALUE
,
1607 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1610 if (depth
< 0 && dimensions
> 2) {
1611 _mesa_error(ctx
, GL_INVALID_VALUE
,
1612 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1616 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1617 /* As with the glTexImage2D check above, the error code here
1618 * depends on texture integer.
1620 GLenum error
= _mesa_is_integer_format(format
)
1621 ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
1622 _mesa_error(ctx
, error
,
1623 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1624 dimensions
, format
, type
);
1633 * Do second part of glTexSubImage which depends on the destination texture.
1634 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1637 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1638 GLenum target
, GLint level
,
1639 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1640 GLint width
, GLint height
, GLint depth
,
1641 GLenum format
, GLenum type
,
1642 const struct gl_texture_image
*destTex
)
1645 /* undefined image level */
1646 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1650 if (xoffset
< -((GLint
)destTex
->Border
)) {
1651 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1655 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1656 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1660 if (dimensions
> 1) {
1661 if (yoffset
< -((GLint
)destTex
->Border
)) {
1662 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1666 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1667 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1672 if (dimensions
> 2) {
1673 if (zoffset
< -((GLint
)destTex
->Border
)) {
1674 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1677 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1678 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1683 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
1686 if (!target_can_be_compressed(ctx
, target
)) {
1687 _mesa_error(ctx
, GL_INVALID_ENUM
,
1688 "glTexSubImage%dD(target=%s)", dimensions
,
1689 _mesa_lookup_enum_by_nr(target
));
1693 /* do tests which depend on compression block size */
1694 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
1696 /* offset must be multiple of block size */
1697 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1699 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
1700 dimensions
, xoffset
, yoffset
);
1703 /* size must be multiple of bw by bh or equal to whole texture size */
1704 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
1705 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1706 "glTexSubImage%dD(width = %d)", dimensions
, width
);
1709 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
1710 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1711 "glTexSubImage%dD(height = %d)", dimensions
, height
);
1721 * Test glCopyTexImage[12]D() parameters for errors.
1723 * \param ctx GL context.
1724 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1725 * \param target texture target given by the user.
1726 * \param level image level given by the user.
1727 * \param internalFormat internal format given by the user.
1728 * \param width image width given by the user.
1729 * \param height image height given by the user.
1730 * \param border texture border.
1732 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1734 * Verifies each of the parameters against the constants specified in
1735 * __struct gl_contextRec::Const and the supported extensions, and according to the
1736 * OpenGL specification.
1739 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1740 GLenum target
, GLint level
, GLint internalFormat
,
1741 GLint width
, GLint height
, GLint border
)
1747 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1748 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1749 _mesa_error(ctx
, GL_INVALID_VALUE
,
1750 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1754 /* Check that the source buffer is complete */
1755 if (ctx
->ReadBuffer
->Name
) {
1756 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1757 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1758 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1759 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1765 if (border
< 0 || border
> 1 ||
1766 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1767 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1771 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1773 _mesa_error(ctx
, GL_INVALID_VALUE
,
1774 "glCopyTexImage%dD(internalFormat)", dimensions
);
1778 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1779 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1780 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
1784 /* NOTE: the format and type aren't really significant for
1785 * TestProxyTexImage(). Only the internalformat really matters.
1789 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1790 * level, width, height and depth.
1792 if (dimensions
== 1) {
1793 if (target
== GL_TEXTURE_1D
) {
1794 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1795 level
, internalFormat
,
1797 width
, 1, 1, border
);
1800 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1804 else if (dimensions
== 2) {
1805 if (target
== GL_TEXTURE_2D
) {
1806 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1807 level
, internalFormat
,
1809 width
, height
, 1, border
);
1811 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1812 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1813 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1814 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1817 sizeOK
= (width
== height
) &&
1818 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1819 level
, internalFormat
, format
, type
,
1820 width
, height
, 1, border
);
1822 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1823 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1824 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1827 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1828 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1829 level
, internalFormat
,
1831 width
, height
, 1, border
);
1833 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1834 if (!ctx
->Extensions
.MESA_texture_array
) {
1835 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)");
1838 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1839 GL_PROXY_TEXTURE_1D_ARRAY_EXT
,
1840 level
, internalFormat
,
1842 width
, height
, 1, border
);
1845 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1850 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1855 if (dimensions
== 1) {
1856 _mesa_error(ctx
, GL_INVALID_VALUE
,
1857 "glCopyTexImage1D(width=%d)", width
);
1860 ASSERT(dimensions
== 2);
1861 _mesa_error(ctx
, GL_INVALID_VALUE
,
1862 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1867 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1868 if (!target_can_be_compressed(ctx
, target
)) {
1869 _mesa_error(ctx
, GL_INVALID_ENUM
,
1870 "glCopyTexImage%dD(target)", dimensions
);
1874 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1875 "glCopyTexImage%dD(border!=0)", dimensions
);
1879 else if (_mesa_is_depth_format(internalFormat
)) {
1880 /* make sure we have depth/stencil buffers */
1881 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
1882 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1883 "glCopyTexImage%dD(no depth)", dimensions
);
1887 else if (_mesa_is_depthstencil_format(internalFormat
)) {
1888 /* make sure we have depth/stencil buffers */
1889 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
1890 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1891 "glCopyTexImage%dD(no depth/stencil buffer)", dimensions
);
1896 /* if we get here, the parameters are OK */
1902 * Test glCopyTexSubImage[12]D() parameters for errors.
1903 * Note that this is the first part of error checking.
1904 * See also copytexsubimage_error_check2() below for the second part.
1906 * \param ctx GL context.
1907 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1908 * \param target texture target given by the user.
1909 * \param level image level given by the user.
1911 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1914 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
1915 GLenum target
, GLint level
)
1917 /* Check that the source buffer is complete */
1918 if (ctx
->ReadBuffer
->Name
) {
1919 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1920 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1921 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1922 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1928 if (dimensions
== 1) {
1929 if (target
!= GL_TEXTURE_1D
) {
1930 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1934 else if (dimensions
== 2) {
1935 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1936 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1937 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1938 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1942 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1943 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1944 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1948 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1949 if (!ctx
->Extensions
.MESA_texture_array
) {
1950 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1954 else if (target
!= GL_TEXTURE_2D
) {
1955 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1959 else if (dimensions
== 3) {
1960 if (((target
!= GL_TEXTURE_2D_ARRAY_EXT
) ||
1961 (!ctx
->Extensions
.MESA_texture_array
))
1962 && (target
!= GL_TEXTURE_3D
)) {
1963 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1969 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1970 _mesa_error(ctx
, GL_INVALID_VALUE
,
1971 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1980 * Second part of error checking for glCopyTexSubImage[12]D().
1981 * \param xoffset sub-image x offset given by the user.
1982 * \param yoffset sub-image y offset given by the user.
1983 * \param zoffset sub-image z offset given by the user.
1984 * \param width image width given by the user.
1985 * \param height image height given by the user.
1988 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1989 GLenum target
, GLint level
,
1990 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1991 GLsizei width
, GLsizei height
,
1992 const struct gl_texture_image
*teximage
)
1994 /* check that dest tex image exists */
1996 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1997 "glCopyTexSubImage%dD(undefined texture level: %d)",
2004 _mesa_error(ctx
, GL_INVALID_VALUE
,
2005 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
2008 if (dimensions
> 1 && height
< 0) {
2009 _mesa_error(ctx
, GL_INVALID_VALUE
,
2010 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
2014 /* check x/y offsets */
2015 if (xoffset
< -((GLint
)teximage
->Border
)) {
2016 _mesa_error(ctx
, GL_INVALID_VALUE
,
2017 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
2020 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
2021 _mesa_error(ctx
, GL_INVALID_VALUE
,
2022 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
2025 if (dimensions
> 1) {
2026 if (yoffset
< -((GLint
)teximage
->Border
)) {
2027 _mesa_error(ctx
, GL_INVALID_VALUE
,
2028 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2031 /* NOTE: we're adding the border here, not subtracting! */
2032 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
2033 _mesa_error(ctx
, GL_INVALID_VALUE
,
2034 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2039 /* check z offset */
2040 if (dimensions
> 2) {
2041 if (zoffset
< -((GLint
)teximage
->Border
)) {
2042 _mesa_error(ctx
, GL_INVALID_VALUE
,
2043 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2046 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
2047 _mesa_error(ctx
, GL_INVALID_VALUE
,
2048 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2053 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2054 if (!target_can_be_compressed(ctx
, target
)) {
2055 _mesa_error(ctx
, GL_INVALID_ENUM
,
2056 "glCopyTexSubImage%dD(target)", dimensions
);
2059 /* offset must be multiple of 4 */
2060 if ((xoffset
& 3) || (yoffset
& 3)) {
2061 _mesa_error(ctx
, GL_INVALID_VALUE
,
2062 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2065 /* size must be multiple of 4 */
2066 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2067 _mesa_error(ctx
, GL_INVALID_VALUE
,
2068 "glCopyTexSubImage%dD(width)", dimensions
);
2071 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2072 _mesa_error(ctx
, GL_INVALID_VALUE
,
2073 "glCopyTexSubImage%dD(height)", dimensions
);
2078 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2079 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2083 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2084 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2085 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2086 dimensions
, teximage
->_BaseFormat
);
2090 if (teximage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2091 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
2092 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2093 "glCopyTexSubImage%dD(no depth buffer)",
2098 else if (teximage
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
2099 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
2100 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2101 "glCopyTexSubImage%dD(no depth/stencil buffer)",
2107 /* If copying into an integer texture, the source buffer must also be
2110 if (_mesa_is_format_integer_color(teximage
->TexFormat
)) {
2111 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2112 if (!_mesa_is_format_integer_color(rb
->Format
)) {
2113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2114 "glCopyTexSubImage%dD(source buffer is not integer format)",
2120 /* if we get here, the parameters are OK */
2125 /** Callback info for walking over FBO hash table */
2128 struct gl_context
*ctx
;
2129 struct gl_texture_object
*texObj
;
2135 * Check render to texture callback. Called from _mesa_HashWalk().
2138 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2140 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2141 const struct cb_info
*info
= (struct cb_info
*) userData
;
2142 struct gl_context
*ctx
= info
->ctx
;
2143 const struct gl_texture_object
*texObj
= info
->texObj
;
2144 const GLuint level
= info
->level
, face
= info
->face
;
2146 /* If this is a user-created FBO */
2149 /* check if any of the FBO's attachments point to 'texObj' */
2150 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2151 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2152 if (att
->Type
== GL_TEXTURE
&&
2153 att
->Texture
== texObj
&&
2154 att
->TextureLevel
== level
&&
2155 att
->CubeMapFace
== face
) {
2156 ASSERT(att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]);
2157 /* Tell driver about the new renderbuffer texture */
2158 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2159 /* Mark fb status as indeterminate to force re-validation */
2168 * When a texture image is specified we have to check if it's bound to
2169 * any framebuffer objects (render to texture) in order to detect changes
2170 * in size or format since that effects FBO completeness.
2171 * Any FBOs rendering into the texture must be re-validated.
2174 update_fbo_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
2175 GLuint face
, GLuint level
)
2177 /* Only check this texture if it's been marked as RenderToTexture */
2178 if (texObj
->_RenderToTexture
) {
2179 struct cb_info info
;
2181 info
.texObj
= texObj
;
2184 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2190 * If the texture object's GenerateMipmap flag is set and we've
2191 * changed the texture base level image, regenerate the rest of the
2192 * mipmap levels now.
2195 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2196 struct gl_texture_object
*texObj
, GLint level
)
2198 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2199 if (texObj
->GenerateMipmap
&&
2200 level
== texObj
->BaseLevel
&&
2201 level
< texObj
->MaxLevel
) {
2202 ASSERT(ctx
->Driver
.GenerateMipmap
);
2203 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2208 /** Debug helper: override the user-requested internal format */
2210 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2213 if (internalFormat
== GL_RGBA16F_ARB
||
2214 internalFormat
== GL_RGBA32F_ARB
) {
2215 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2218 else if (internalFormat
== GL_RGB16F_ARB
||
2219 internalFormat
== GL_RGB32F_ARB
) {
2220 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2223 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2224 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2225 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2226 return GL_LUMINANCE_ALPHA
;
2228 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2229 internalFormat
== GL_LUMINANCE32F_ARB
) {
2230 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2231 return GL_LUMINANCE
;
2233 else if (internalFormat
== GL_ALPHA16F_ARB
||
2234 internalFormat
== GL_ALPHA32F_ARB
) {
2235 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2239 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2240 internalFormat = GL_RGBA;
2244 return internalFormat
;
2247 return internalFormat
;
2253 * Choose the actual hardware format for a texture image.
2254 * Try to use the same format as the previous image level when possible.
2255 * Otherwise, ask the driver for the best format.
2256 * It's important to try to choose a consistant format for all levels
2257 * for efficient texture memory layout/allocation. In particular, this
2258 * comes up during automatic mipmap generation.
2261 _mesa_choose_texture_format(struct gl_context
*ctx
,
2262 struct gl_texture_object
*texObj
,
2263 struct gl_texture_image
*texImage
,
2264 GLenum target
, GLint level
,
2265 GLenum internalFormat
, GLenum format
, GLenum type
)
2267 /* see if we've already chosen a format for the previous level */
2269 struct gl_texture_image
*prevImage
=
2270 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2271 /* See if the prev level is defined and has an internal format which
2272 * matches the new internal format.
2275 prevImage
->Width
> 0 &&
2276 prevImage
->InternalFormat
== internalFormat
) {
2277 /* use the same format */
2278 texImage
->TexFormat
= prevImage
->TexFormat
;
2279 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2284 /* choose format from scratch */
2285 texImage
->TexFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
,
2287 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2293 * Called from the API. Note that width includes the border.
2296 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2297 GLsizei width
, GLint border
, GLenum format
,
2298 GLenum type
, const GLvoid
*pixels
)
2300 GET_CURRENT_CONTEXT(ctx
);
2301 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2303 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2304 _mesa_debug(ctx
, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2305 _mesa_lookup_enum_by_nr(target
), level
,
2306 _mesa_lookup_enum_by_nr(internalFormat
), width
, border
,
2307 _mesa_lookup_enum_by_nr(format
),
2308 _mesa_lookup_enum_by_nr(type
), pixels
);
2310 internalFormat
= override_internal_format(internalFormat
, width
, 1);
2312 if (target
== GL_TEXTURE_1D
) {
2313 /* non-proxy target */
2314 struct gl_texture_object
*texObj
;
2315 struct gl_texture_image
*texImage
;
2316 const GLuint face
= _mesa_tex_target_to_face(target
);
2318 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2319 format
, type
, 1, width
, 1, 1, border
)) {
2320 return; /* error was recorded */
2323 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2324 _mesa_update_state(ctx
);
2326 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2327 _mesa_lock_texture(ctx
, texObj
);
2329 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2331 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2334 if (texImage
->Data
) {
2335 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2338 ASSERT(texImage
->Data
== NULL
);
2340 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2341 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2343 border
, internalFormat
);
2345 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2346 internalFormat
, format
, type
);
2348 /* Give the texture to the driver. <pixels> may be null. */
2349 ASSERT(ctx
->Driver
.TexImage1D
);
2350 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2351 width
, border
, format
, type
, pixels
,
2352 &ctx
->Unpack
, texObj
, texImage
);
2354 _mesa_set_fetch_functions(texImage
, 1);
2356 check_gen_mipmap(ctx
, target
, texObj
, level
);
2358 update_fbo_texture(ctx
, texObj
, face
, level
);
2361 texObj
->_Complete
= GL_FALSE
;
2362 ctx
->NewState
|= _NEW_TEXTURE
;
2365 _mesa_unlock_texture(ctx
, texObj
);
2367 else if (target
== GL_PROXY_TEXTURE_1D
) {
2368 /* Proxy texture: check for errors and update proxy state */
2369 struct gl_texture_image
*texImage
;
2370 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2371 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2372 format
, type
, 1, width
, 1, 1, border
)) {
2373 /* when error, clear all proxy texture image parameters */
2375 clear_teximage_fields(texImage
);
2378 /* no error, set the tex image parameters */
2379 struct gl_texture_object
*texObj
=
2380 _mesa_get_current_tex_object(ctx
, target
);
2382 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2384 border
, internalFormat
);
2385 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2386 internalFormat
, format
, type
);
2390 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2397 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2398 GLsizei width
, GLsizei height
, GLint border
,
2399 GLenum format
, GLenum type
,
2400 const GLvoid
*pixels
)
2402 GET_CURRENT_CONTEXT(ctx
);
2403 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2405 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2406 _mesa_debug(ctx
, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2407 _mesa_lookup_enum_by_nr(target
), level
,
2408 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2409 border
, _mesa_lookup_enum_by_nr(format
),
2410 _mesa_lookup_enum_by_nr(type
), pixels
);
2412 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2414 if (target
== GL_TEXTURE_2D
||
2415 (ctx
->Extensions
.ARB_texture_cube_map
&&
2416 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2417 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2418 (ctx
->Extensions
.NV_texture_rectangle
&&
2419 target
== GL_TEXTURE_RECTANGLE_NV
) ||
2420 (ctx
->Extensions
.MESA_texture_array
&&
2421 target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
2422 /* non-proxy target */
2423 struct gl_texture_object
*texObj
;
2424 struct gl_texture_image
*texImage
;
2425 const GLuint face
= _mesa_tex_target_to_face(target
);
2427 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2428 format
, type
, 2, width
, height
, 1, border
)) {
2429 return; /* error was recorded */
2432 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2433 _mesa_update_state(ctx
);
2435 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2436 _mesa_lock_texture(ctx
, texObj
);
2438 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2440 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2443 if (texImage
->Data
) {
2444 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2447 ASSERT(texImage
->Data
== NULL
);
2448 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2449 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2451 border
, internalFormat
);
2453 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2454 internalFormat
, format
, type
);
2456 /* Give the texture to the driver. <pixels> may be null. */
2457 ASSERT(ctx
->Driver
.TexImage2D
);
2458 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2459 width
, height
, border
, format
, type
,
2460 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2462 _mesa_set_fetch_functions(texImage
, 2);
2464 check_gen_mipmap(ctx
, target
, texObj
, level
);
2466 update_fbo_texture(ctx
, texObj
, face
, level
);
2469 texObj
->_Complete
= GL_FALSE
;
2470 ctx
->NewState
|= _NEW_TEXTURE
;
2473 _mesa_unlock_texture(ctx
, texObj
);
2475 else if (target
== GL_PROXY_TEXTURE_2D
||
2476 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2477 ctx
->Extensions
.ARB_texture_cube_map
) ||
2478 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2479 ctx
->Extensions
.NV_texture_rectangle
) ||
2480 (ctx
->Extensions
.MESA_texture_array
&&
2481 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
)) {
2482 /* Proxy texture: check for errors and update proxy state */
2483 struct gl_texture_image
*texImage
;
2484 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2485 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2486 format
, type
, 2, width
, height
, 1, border
)) {
2487 /* when error, clear all proxy texture image parameters */
2489 clear_teximage_fields(texImage
);
2492 /* no error, set the tex image parameters */
2493 struct gl_texture_object
*texObj
=
2494 _mesa_get_current_tex_object(ctx
, target
);
2495 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2497 border
, internalFormat
);
2498 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2499 internalFormat
, format
, type
);
2503 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2510 * Called by the API or display list executor.
2511 * Note that width and height include the border.
2514 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2515 GLsizei width
, GLsizei height
, GLsizei depth
,
2516 GLint border
, GLenum format
, GLenum type
,
2517 const GLvoid
*pixels
)
2519 GET_CURRENT_CONTEXT(ctx
);
2520 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2522 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2523 _mesa_debug(ctx
, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2524 _mesa_lookup_enum_by_nr(target
), level
,
2525 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2526 depth
, border
, _mesa_lookup_enum_by_nr(format
),
2527 _mesa_lookup_enum_by_nr(type
), pixels
);
2529 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2531 if (target
== GL_TEXTURE_3D
||
2532 (ctx
->Extensions
.MESA_texture_array
&&
2533 target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
2534 /* non-proxy target */
2535 struct gl_texture_object
*texObj
;
2536 struct gl_texture_image
*texImage
;
2537 const GLuint face
= _mesa_tex_target_to_face(target
);
2539 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2540 format
, type
, 3, width
, height
, depth
, border
)) {
2541 return; /* error was recorded */
2544 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2545 _mesa_update_state(ctx
);
2547 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2548 _mesa_lock_texture(ctx
, texObj
);
2550 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2552 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2555 if (texImage
->Data
) {
2556 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2559 ASSERT(texImage
->Data
== NULL
);
2560 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2561 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2562 width
, height
, depth
,
2563 border
, internalFormat
);
2565 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2566 internalFormat
, format
, type
);
2568 /* Give the texture to the driver. <pixels> may be null. */
2569 ASSERT(ctx
->Driver
.TexImage3D
);
2570 ctx
->Driver
.TexImage3D(ctx
, target
, level
, internalFormat
,
2571 width
, height
, depth
, border
, format
, type
,
2572 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2574 _mesa_set_fetch_functions(texImage
, 3);
2576 check_gen_mipmap(ctx
, target
, texObj
, level
);
2578 update_fbo_texture(ctx
, texObj
, face
, level
);
2581 texObj
->_Complete
= GL_FALSE
;
2582 ctx
->NewState
|= _NEW_TEXTURE
;
2585 _mesa_unlock_texture(ctx
, texObj
);
2587 else if (target
== GL_PROXY_TEXTURE_3D
||
2588 (ctx
->Extensions
.MESA_texture_array
&&
2589 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
)) {
2590 /* Proxy texture: check for errors and update proxy state */
2591 struct gl_texture_image
*texImage
;
2592 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2593 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2594 format
, type
, 3, width
, height
, depth
, border
)) {
2595 /* when error, clear all proxy texture image parameters */
2597 clear_teximage_fields(texImage
);
2600 /* no error, set the tex image parameters */
2601 struct gl_texture_object
*texObj
=
2602 _mesa_get_current_tex_object(ctx
, target
);
2603 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
2604 depth
, border
, internalFormat
);
2605 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2606 internalFormat
, format
, type
);
2610 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2617 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2618 GLsizei width
, GLsizei height
, GLsizei depth
,
2619 GLint border
, GLenum format
, GLenum type
,
2620 const GLvoid
*pixels
)
2622 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2623 depth
, border
, format
, type
, pixels
);
2627 #if FEATURE_OES_EGL_image
2629 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
2631 struct gl_texture_object
*texObj
;
2632 struct gl_texture_image
*texImage
;
2633 GET_CURRENT_CONTEXT(ctx
);
2634 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2636 if (!ctx
->Extensions
.OES_EGL_image
) {
2637 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2638 "glEGLImageTargetTexture2DOES(unsupported)");
2642 if (target
!= GL_TEXTURE_2D
) {
2643 _mesa_error(ctx
, GL_INVALID_ENUM
,
2644 "glEGLImageTargetTexture2D(target=%d)", target
);
2648 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2649 _mesa_update_state(ctx
);
2651 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2652 _mesa_lock_texture(ctx
, texObj
);
2654 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
2656 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
2659 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2661 ASSERT(texImage
->Data
== NULL
);
2662 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
2663 texObj
, texImage
, image
);
2666 texObj
->_Complete
= GL_FALSE
;
2667 ctx
->NewState
|= _NEW_TEXTURE
;
2669 _mesa_unlock_texture(ctx
, texObj
);
2676 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2677 GLint xoffset
, GLsizei width
,
2678 GLenum format
, GLenum type
,
2679 const GLvoid
*pixels
)
2681 struct gl_texture_object
*texObj
;
2682 struct gl_texture_image
*texImage
;
2683 GET_CURRENT_CONTEXT(ctx
);
2684 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2686 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2687 _mesa_debug(ctx
, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2688 _mesa_lookup_enum_by_nr(target
), level
,
2689 xoffset
, width
, _mesa_lookup_enum_by_nr(format
),
2690 _mesa_lookup_enum_by_nr(type
), pixels
);
2692 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2693 _mesa_update_state(ctx
);
2695 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2696 width
, 1, 1, format
, type
)) {
2697 return; /* error was detected */
2701 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2704 _mesa_lock_texture(ctx
, texObj
);
2706 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2708 if (subtexture_error_check2(ctx
, 1, target
, level
, xoffset
, 0, 0,
2709 width
, 1, 1, format
, type
, texImage
)) {
2710 /* error was recorded */
2712 else if (width
> 0) {
2713 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2714 xoffset
+= texImage
->Border
;
2716 ASSERT(ctx
->Driver
.TexSubImage1D
);
2717 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
, width
,
2718 format
, type
, pixels
, &ctx
->Unpack
,
2721 check_gen_mipmap(ctx
, target
, texObj
, level
);
2723 ctx
->NewState
|= _NEW_TEXTURE
;
2726 _mesa_unlock_texture(ctx
, texObj
);
2731 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2732 GLint xoffset
, GLint yoffset
,
2733 GLsizei width
, GLsizei height
,
2734 GLenum format
, GLenum type
,
2735 const GLvoid
*pixels
)
2737 struct gl_texture_object
*texObj
;
2738 struct gl_texture_image
*texImage
;
2739 GET_CURRENT_CONTEXT(ctx
);
2740 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2742 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2743 _mesa_debug(ctx
, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2744 _mesa_lookup_enum_by_nr(target
), level
,
2745 xoffset
, yoffset
, width
, height
,
2746 _mesa_lookup_enum_by_nr(format
),
2747 _mesa_lookup_enum_by_nr(type
), pixels
);
2749 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2750 _mesa_update_state(ctx
);
2752 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2753 width
, height
, 1, format
, type
)) {
2754 return; /* error was detected */
2757 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2759 _mesa_lock_texture(ctx
, texObj
);
2761 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2763 if (subtexture_error_check2(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2764 width
, height
, 1, format
, type
, texImage
)) {
2765 /* error was recorded */
2767 else if (width
> 0 && height
>= 0) {
2768 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2769 xoffset
+= texImage
->Border
;
2770 yoffset
+= texImage
->Border
;
2772 ASSERT(ctx
->Driver
.TexSubImage2D
);
2773 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2774 width
, height
, format
, type
, pixels
,
2775 &ctx
->Unpack
, texObj
, texImage
);
2777 check_gen_mipmap(ctx
, target
, texObj
, level
);
2779 ctx
->NewState
|= _NEW_TEXTURE
;
2782 _mesa_unlock_texture(ctx
, texObj
);
2788 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2789 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2790 GLsizei width
, GLsizei height
, GLsizei depth
,
2791 GLenum format
, GLenum type
,
2792 const GLvoid
*pixels
)
2794 struct gl_texture_object
*texObj
;
2795 struct gl_texture_image
*texImage
;
2796 GET_CURRENT_CONTEXT(ctx
);
2797 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2799 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2800 _mesa_debug(ctx
, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2801 _mesa_lookup_enum_by_nr(target
), level
,
2802 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2803 _mesa_lookup_enum_by_nr(format
),
2804 _mesa_lookup_enum_by_nr(type
), pixels
);
2806 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2807 _mesa_update_state(ctx
);
2809 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2810 width
, height
, depth
, format
, type
)) {
2811 return; /* error was detected */
2814 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2816 _mesa_lock_texture(ctx
, texObj
);
2818 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2820 if (subtexture_error_check2(ctx
, 3, target
, level
,
2821 xoffset
, yoffset
, zoffset
,
2822 width
, height
, depth
,
2823 format
, type
, texImage
)) {
2824 /* error was recorded */
2826 else if (width
> 0 && height
> 0 && height
> 0) {
2827 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2828 xoffset
+= texImage
->Border
;
2829 yoffset
+= texImage
->Border
;
2830 zoffset
+= texImage
->Border
;
2832 ASSERT(ctx
->Driver
.TexSubImage3D
);
2833 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
,
2834 xoffset
, yoffset
, zoffset
,
2835 width
, height
, depth
,
2836 format
, type
, pixels
,
2837 &ctx
->Unpack
, texObj
, texImage
);
2839 check_gen_mipmap(ctx
, target
, texObj
, level
);
2841 ctx
->NewState
|= _NEW_TEXTURE
;
2844 _mesa_unlock_texture(ctx
, texObj
);
2850 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2851 GLenum internalFormat
,
2853 GLsizei width
, GLint border
)
2855 struct gl_texture_object
*texObj
;
2856 struct gl_texture_image
*texImage
;
2857 const GLuint face
= _mesa_tex_target_to_face(target
);
2858 GET_CURRENT_CONTEXT(ctx
);
2859 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2861 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2862 _mesa_debug(ctx
, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2863 _mesa_lookup_enum_by_nr(target
), level
,
2864 _mesa_lookup_enum_by_nr(internalFormat
),
2865 x
, y
, width
, border
);
2867 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2868 _mesa_update_state(ctx
);
2870 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2874 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2876 _mesa_lock_texture(ctx
, texObj
);
2878 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2880 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2883 if (texImage
->Data
) {
2884 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2887 ASSERT(texImage
->Data
== NULL
);
2889 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2890 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2891 border
, internalFormat
);
2893 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2894 internalFormat
, GL_NONE
, GL_NONE
);
2896 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2897 ctx
->Driver
.CopyTexImage1D(ctx
, target
, level
, internalFormat
,
2898 x
, y
, width
, border
);
2900 _mesa_set_fetch_functions(texImage
, 1);
2902 check_gen_mipmap(ctx
, target
, texObj
, level
);
2904 update_fbo_texture(ctx
, texObj
, face
, level
);
2907 texObj
->_Complete
= GL_FALSE
;
2908 ctx
->NewState
|= _NEW_TEXTURE
;
2911 _mesa_unlock_texture(ctx
, texObj
);
2917 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2918 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2921 struct gl_texture_object
*texObj
;
2922 struct gl_texture_image
*texImage
;
2923 const GLuint face
= _mesa_tex_target_to_face(target
);
2924 GET_CURRENT_CONTEXT(ctx
);
2925 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2927 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2928 _mesa_debug(ctx
, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2929 _mesa_lookup_enum_by_nr(target
), level
,
2930 _mesa_lookup_enum_by_nr(internalFormat
),
2931 x
, y
, width
, height
, border
);
2933 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2934 _mesa_update_state(ctx
);
2936 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2937 width
, height
, border
))
2940 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2942 _mesa_lock_texture(ctx
, texObj
);
2944 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2947 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2950 if (texImage
->Data
) {
2951 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2954 ASSERT(texImage
->Data
== NULL
);
2956 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2957 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2959 border
, internalFormat
);
2961 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2962 internalFormat
, GL_NONE
, GL_NONE
);
2964 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2965 ctx
->Driver
.CopyTexImage2D(ctx
, target
, level
, internalFormat
,
2966 x
, y
, width
, height
, border
);
2968 _mesa_set_fetch_functions(texImage
, 2);
2970 check_gen_mipmap(ctx
, target
, texObj
, level
);
2972 update_fbo_texture(ctx
, texObj
, face
, level
);
2975 texObj
->_Complete
= GL_FALSE
;
2976 ctx
->NewState
|= _NEW_TEXTURE
;
2979 _mesa_unlock_texture(ctx
, texObj
);
2984 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2985 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2987 struct gl_texture_object
*texObj
;
2988 struct gl_texture_image
*texImage
;
2992 GET_CURRENT_CONTEXT(ctx
);
2993 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2995 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2996 _mesa_debug(ctx
, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
2997 _mesa_lookup_enum_by_nr(target
),
2998 level
, xoffset
, x
, y
, width
);
3000 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3001 _mesa_update_state(ctx
);
3003 if (copytexsubimage_error_check1(ctx
, 1, target
, level
))
3006 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3008 _mesa_lock_texture(ctx
, texObj
);
3010 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3012 if (copytexsubimage_error_check2(ctx
, 1, target
, level
,
3013 xoffset
, 0, 0, width
, 1, texImage
)) {
3014 /* error was recorded */
3017 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3018 xoffset
+= texImage
->Border
;
3020 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3022 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
3023 ctx
->Driver
.CopyTexSubImage1D(ctx
, target
, level
,
3024 xoffset
, x
, y
, width
);
3026 check_gen_mipmap(ctx
, target
, texObj
, level
);
3028 ctx
->NewState
|= _NEW_TEXTURE
;
3032 _mesa_unlock_texture(ctx
, texObj
);
3038 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3039 GLint xoffset
, GLint yoffset
,
3040 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3042 struct gl_texture_object
*texObj
;
3043 struct gl_texture_image
*texImage
;
3044 GET_CURRENT_CONTEXT(ctx
);
3045 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3047 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3048 _mesa_debug(ctx
, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
3049 _mesa_lookup_enum_by_nr(target
),
3050 level
, xoffset
, yoffset
, x
, y
, width
, height
);
3052 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3053 _mesa_update_state(ctx
);
3055 if (copytexsubimage_error_check1(ctx
, 2, target
, level
))
3058 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3060 _mesa_lock_texture(ctx
, texObj
);
3062 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3064 if (copytexsubimage_error_check2(ctx
, 2, target
, level
,
3065 xoffset
, yoffset
, 0,
3066 width
, height
, texImage
)) {
3067 /* error was recorded */
3070 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3071 xoffset
+= texImage
->Border
;
3072 yoffset
+= texImage
->Border
;
3074 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3076 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
3077 ctx
->Driver
.CopyTexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
3078 x
, y
, width
, height
);
3080 check_gen_mipmap(ctx
, target
, texObj
, level
);
3082 ctx
->NewState
|= _NEW_TEXTURE
;
3086 _mesa_unlock_texture(ctx
, texObj
);
3092 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3093 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3094 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3096 struct gl_texture_object
*texObj
;
3097 struct gl_texture_image
*texImage
;
3098 GET_CURRENT_CONTEXT(ctx
);
3099 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3101 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3102 _mesa_debug(ctx
, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
3103 _mesa_lookup_enum_by_nr(target
),
3104 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3106 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3107 _mesa_update_state(ctx
);
3109 if (copytexsubimage_error_check1(ctx
, 3, target
, level
))
3112 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3114 _mesa_lock_texture(ctx
, texObj
);
3116 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3118 if (copytexsubimage_error_check2(ctx
, 3, target
, level
, xoffset
, yoffset
,
3119 zoffset
, width
, height
, texImage
)) {
3120 /* error was recored */
3123 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3124 xoffset
+= texImage
->Border
;
3125 yoffset
+= texImage
->Border
;
3126 zoffset
+= texImage
->Border
;
3128 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3130 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
3131 ctx
->Driver
.CopyTexSubImage3D(ctx
, target
, level
,
3132 xoffset
, yoffset
, zoffset
,
3133 x
, y
, width
, height
);
3135 check_gen_mipmap(ctx
, target
, texObj
, level
);
3137 ctx
->NewState
|= _NEW_TEXTURE
;
3141 _mesa_unlock_texture(ctx
, texObj
);
3147 /**********************************************************************/
3148 /****** Compressed Textures ******/
3149 /**********************************************************************/
3153 * Return expected size of a compressed texture.
3156 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
3159 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3160 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
3165 * Return compressed texture block size, in pixels.
3168 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
3170 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3171 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
3176 * Error checking for glCompressedTexImage[123]D().
3177 * \return error code or GL_NO_ERROR.
3180 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3181 GLenum target
, GLint level
,
3182 GLenum internalFormat
, GLsizei width
,
3183 GLsizei height
, GLsizei depth
, GLint border
,
3186 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3188 if (dimensions
== 1) {
3189 /* 1D compressed textures not allowed */
3190 return GL_INVALID_ENUM
;
3192 else if (dimensions
== 2) {
3193 if (target
== GL_PROXY_TEXTURE_2D
) {
3194 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3196 else if (target
== GL_TEXTURE_2D
) {
3197 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3199 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3200 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3201 return GL_INVALID_ENUM
; /*target*/
3202 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3204 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3205 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3206 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3207 return GL_INVALID_ENUM
; /*target*/
3208 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3211 return GL_INVALID_ENUM
; /*target*/
3214 else if (dimensions
== 3) {
3215 /* 3D compressed textures not allowed */
3216 return GL_INVALID_ENUM
;
3220 return GL_INVALID_ENUM
;
3223 maxTextureSize
= 1 << (maxLevels
- 1);
3225 /* This will detect any invalid internalFormat value */
3226 if (!_mesa_is_compressed_format(ctx
, internalFormat
))
3227 return GL_INVALID_ENUM
;
3229 /* This should really never fail */
3230 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
3231 return GL_INVALID_ENUM
;
3234 return GL_INVALID_VALUE
;
3237 * XXX We should probably use the proxy texture error check function here.
3239 if (width
< 1 || width
> maxTextureSize
||
3240 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(width
)))
3241 return GL_INVALID_VALUE
;
3243 if ((height
< 1 || height
> maxTextureSize
||
3244 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(height
)))
3246 return GL_INVALID_VALUE
;
3248 if ((depth
< 1 || depth
> maxTextureSize
||
3249 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(depth
)))
3251 return GL_INVALID_VALUE
;
3253 /* For cube map, width must equal height */
3254 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3255 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
3256 return GL_INVALID_VALUE
;
3258 if (level
< 0 || level
>= maxLevels
)
3259 return GL_INVALID_VALUE
;
3261 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
3262 if (expectedSize
!= imageSize
)
3263 return GL_INVALID_VALUE
;
3265 #if FEATURE_EXT_texture_sRGB
3266 if ((internalFormat
== GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
||
3267 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
||
3268 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
||
3269 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
)
3271 return GL_INVALID_OPERATION
;
3280 * Error checking for glCompressedTexSubImage[123]D().
3281 * \warning There are some bad assumptions here about the size of compressed
3282 * texture tiles (multiple of 4) used to test the validity of the
3283 * offset and size parameters.
3284 * \return error code or GL_NO_ERROR.
3287 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3288 GLenum target
, GLint level
,
3289 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3290 GLsizei width
, GLsizei height
, GLsizei depth
,
3291 GLenum format
, GLsizei imageSize
)
3293 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3297 if (dimensions
== 1) {
3298 /* 1D compressed textures not allowed */
3299 return GL_INVALID_ENUM
;
3301 else if (dimensions
== 2) {
3302 if (target
== GL_PROXY_TEXTURE_2D
) {
3303 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3305 else if (target
== GL_TEXTURE_2D
) {
3306 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3308 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3309 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3310 return GL_INVALID_ENUM
; /*target*/
3311 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3313 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3314 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3315 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3316 return GL_INVALID_ENUM
; /*target*/
3317 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3320 return GL_INVALID_ENUM
; /*target*/
3323 else if (dimensions
== 3) {
3324 /* 3D compressed textures not allowed */
3325 return GL_INVALID_ENUM
;
3328 maxTextureSize
= 1 << (maxLevels
- 1);
3330 /* this will catch any invalid compressed format token */
3331 if (!_mesa_is_compressed_format(ctx
, format
))
3332 return GL_INVALID_ENUM
;
3334 if (width
< 1 || width
> maxTextureSize
)
3335 return GL_INVALID_VALUE
;
3337 if ((height
< 1 || height
> maxTextureSize
)
3339 return GL_INVALID_VALUE
;
3341 if (level
< 0 || level
>= maxLevels
)
3342 return GL_INVALID_VALUE
;
3345 * do checks which depend on compression block size
3347 get_compressed_block_size(format
, &bw
, &bh
);
3349 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3350 return GL_INVALID_VALUE
;
3352 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3353 return GL_INVALID_VALUE
;
3355 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3356 return GL_INVALID_VALUE
;
3358 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3359 if (expectedSize
!= imageSize
)
3360 return GL_INVALID_VALUE
;
3367 * Do second part of glCompressedTexSubImage error checking.
3368 * \return GL_TRUE if error found, GL_FALSE otherwise.
3371 compressed_subtexture_error_check2(struct gl_context
*ctx
, GLuint dims
,
3372 GLsizei width
, GLsizei height
,
3373 GLsizei depth
, GLenum format
,
3374 struct gl_texture_image
*texImage
)
3377 if ((GLint
) format
!= texImage
->InternalFormat
) {
3378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3379 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3383 if (((width
== 1 || width
== 2) &&
3384 width
!= (GLsizei
) texImage
->Width
) ||
3385 (width
> (GLsizei
) texImage
->Width
)) {
3386 _mesa_error(ctx
, GL_INVALID_VALUE
,
3387 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3392 if (((height
== 1 || height
== 2) &&
3393 height
!= (GLsizei
) texImage
->Height
) ||
3394 (height
> (GLsizei
) texImage
->Height
)) {
3395 _mesa_error(ctx
, GL_INVALID_VALUE
,
3396 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3402 if (((depth
== 1 || depth
== 2) &&
3403 depth
!= (GLsizei
) texImage
->Depth
) ||
3404 (depth
> (GLsizei
) texImage
->Depth
)) {
3405 _mesa_error(ctx
, GL_INVALID_VALUE
,
3406 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3417 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3418 GLenum internalFormat
, GLsizei width
,
3419 GLint border
, GLsizei imageSize
,
3422 GET_CURRENT_CONTEXT(ctx
);
3423 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3425 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3426 _mesa_debug(ctx
, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3427 _mesa_lookup_enum_by_nr(target
), level
,
3428 _mesa_lookup_enum_by_nr(internalFormat
),
3429 width
, border
, imageSize
, data
);
3431 if (target
== GL_TEXTURE_1D
) {
3432 /* non-proxy target */
3433 struct gl_texture_object
*texObj
;
3434 struct gl_texture_image
*texImage
;
3435 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3436 internalFormat
, width
, 1, 1, border
, imageSize
);
3438 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
3442 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3444 _mesa_lock_texture(ctx
, texObj
);
3446 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3448 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
3451 if (texImage
->Data
) {
3452 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3454 ASSERT(texImage
->Data
== NULL
);
3456 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3457 border
, internalFormat
);
3459 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3460 internalFormat
, GL_NONE
, GL_NONE
);
3462 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
3463 ctx
->Driver
.CompressedTexImage1D(ctx
, target
, level
,
3464 internalFormat
, width
, border
,
3468 _mesa_set_fetch_functions(texImage
, 1);
3470 check_gen_mipmap(ctx
, target
, texObj
, level
);
3473 texObj
->_Complete
= GL_FALSE
;
3474 ctx
->NewState
|= _NEW_TEXTURE
;
3477 _mesa_unlock_texture(ctx
, texObj
);
3479 else if (target
== GL_PROXY_TEXTURE_1D
) {
3480 /* Proxy texture: check for errors and update proxy state */
3481 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3482 internalFormat
, width
, 1, 1, border
, imageSize
);
3484 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3485 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3486 internalFormat
, GL_NONE
, GL_NONE
,
3487 width
, 1, 1, border
);
3490 /* if error, clear all proxy texture image parameters */
3491 struct gl_texture_image
*texImage
;
3492 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3494 clear_teximage_fields(texImage
);
3497 /* store the teximage parameters */
3498 struct gl_texture_object
*texObj
;
3499 struct gl_texture_image
*texImage
;
3501 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3503 _mesa_lock_texture(ctx
, texObj
);
3505 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3506 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3507 border
, internalFormat
);
3508 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3509 internalFormat
, GL_NONE
, GL_NONE
);
3511 _mesa_unlock_texture(ctx
, texObj
);
3515 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
3521 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3522 GLenum internalFormat
, GLsizei width
,
3523 GLsizei height
, GLint border
, GLsizei imageSize
,
3526 GET_CURRENT_CONTEXT(ctx
);
3527 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3529 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3530 _mesa_debug(ctx
, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3531 _mesa_lookup_enum_by_nr(target
), level
,
3532 _mesa_lookup_enum_by_nr(internalFormat
),
3533 width
, height
, border
, imageSize
, data
);
3536 switch (internalFormat
) {
3537 case GL_PALETTE4_RGB8_OES
:
3538 case GL_PALETTE4_RGBA8_OES
:
3539 case GL_PALETTE4_R5_G6_B5_OES
:
3540 case GL_PALETTE4_RGBA4_OES
:
3541 case GL_PALETTE4_RGB5_A1_OES
:
3542 case GL_PALETTE8_RGB8_OES
:
3543 case GL_PALETTE8_RGBA8_OES
:
3544 case GL_PALETTE8_R5_G6_B5_OES
:
3545 case GL_PALETTE8_RGBA4_OES
:
3546 case GL_PALETTE8_RGB5_A1_OES
:
3547 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3548 width
, height
, imageSize
, data
);
3553 if (target
== GL_TEXTURE_2D
||
3554 (ctx
->Extensions
.ARB_texture_cube_map
&&
3555 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3556 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
3557 /* non-proxy target */
3558 struct gl_texture_object
*texObj
;
3559 struct gl_texture_image
*texImage
;
3561 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3562 internalFormat
, width
, height
, 1, border
, imageSize
);
3564 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
3568 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3570 _mesa_lock_texture(ctx
, texObj
);
3572 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3574 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
3577 if (texImage
->Data
) {
3578 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3580 ASSERT(texImage
->Data
== NULL
);
3582 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3583 border
, internalFormat
);
3585 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3586 internalFormat
, GL_NONE
, GL_NONE
);
3588 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3589 ctx
->Driver
.CompressedTexImage2D(ctx
, target
, level
,
3590 internalFormat
, width
, height
,
3591 border
, imageSize
, data
,
3594 _mesa_set_fetch_functions(texImage
, 2);
3596 check_gen_mipmap(ctx
, target
, texObj
, level
);
3599 texObj
->_Complete
= GL_FALSE
;
3600 ctx
->NewState
|= _NEW_TEXTURE
;
3603 _mesa_unlock_texture(ctx
, texObj
);
3605 else if (target
== GL_PROXY_TEXTURE_2D
||
3606 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
3607 ctx
->Extensions
.ARB_texture_cube_map
)) {
3608 /* Proxy texture: check for errors and update proxy state */
3609 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3610 internalFormat
, width
, height
, 1, border
, imageSize
);
3612 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3613 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3614 internalFormat
, GL_NONE
, GL_NONE
,
3615 width
, height
, 1, border
);
3618 /* if error, clear all proxy texture image parameters */
3619 struct gl_texture_image
*texImage
;
3620 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3622 clear_teximage_fields(texImage
);
3625 /* store the teximage parameters */
3626 struct gl_texture_object
*texObj
;
3627 struct gl_texture_image
*texImage
;
3629 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3631 _mesa_lock_texture(ctx
, texObj
);
3633 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3634 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3635 border
, internalFormat
);
3636 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3637 internalFormat
, GL_NONE
, GL_NONE
);
3639 _mesa_unlock_texture(ctx
, texObj
);
3643 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3650 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3651 GLenum internalFormat
, GLsizei width
,
3652 GLsizei height
, GLsizei depth
, GLint border
,
3653 GLsizei imageSize
, const GLvoid
*data
)
3655 GET_CURRENT_CONTEXT(ctx
);
3656 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3658 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3659 _mesa_debug(ctx
, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3660 _mesa_lookup_enum_by_nr(target
), level
,
3661 _mesa_lookup_enum_by_nr(internalFormat
),
3662 width
, height
, depth
, border
, imageSize
, data
);
3664 if (target
== GL_TEXTURE_3D
) {
3665 /* non-proxy target */
3666 struct gl_texture_object
*texObj
;
3667 struct gl_texture_image
*texImage
;
3668 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3669 internalFormat
, width
, height
, depth
, border
, imageSize
);
3671 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3675 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3677 _mesa_lock_texture(ctx
, texObj
);
3679 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3681 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3684 if (texImage
->Data
) {
3685 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3687 ASSERT(texImage
->Data
== NULL
);
3689 _mesa_init_teximage_fields(ctx
, target
, texImage
,
3690 width
, height
, depth
,
3691 border
, internalFormat
);
3693 /* Choose actual texture format */
3694 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3695 internalFormat
, GL_NONE
, GL_NONE
);
3697 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3698 ctx
->Driver
.CompressedTexImage3D(ctx
, target
, level
,
3700 width
, height
, depth
,
3701 border
, imageSize
, data
,
3704 _mesa_set_fetch_functions(texImage
, 3);
3706 check_gen_mipmap(ctx
, target
, texObj
, level
);
3709 texObj
->_Complete
= GL_FALSE
;
3710 ctx
->NewState
|= _NEW_TEXTURE
;
3713 _mesa_unlock_texture(ctx
, texObj
);
3715 else if (target
== GL_PROXY_TEXTURE_3D
) {
3716 /* Proxy texture: check for errors and update proxy state */
3717 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3718 internalFormat
, width
, height
, depth
, border
, imageSize
);
3720 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3721 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3722 internalFormat
, GL_NONE
, GL_NONE
,
3723 width
, height
, depth
, border
);
3726 /* if error, clear all proxy texture image parameters */
3727 struct gl_texture_image
*texImage
;
3728 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3730 clear_teximage_fields(texImage
);
3733 /* store the teximage parameters */
3734 struct gl_texture_object
*texObj
;
3735 struct gl_texture_image
*texImage
;
3737 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3739 _mesa_lock_texture(ctx
, texObj
);
3741 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3742 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3743 depth
, border
, internalFormat
);
3744 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3745 internalFormat
, GL_NONE
, GL_NONE
);
3747 _mesa_unlock_texture(ctx
, texObj
);
3751 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3758 * Common helper for glCompressedTexSubImage1/2/3D().
3761 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3762 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3763 GLsizei width
, GLsizei height
, GLsizei depth
,
3764 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3766 struct gl_texture_object
*texObj
;
3767 struct gl_texture_image
*texImage
;
3769 GET_CURRENT_CONTEXT(ctx
);
3770 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3772 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3773 xoffset
, 0, 0, /* pos */
3774 width
, height
, depth
, /* size */
3777 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3781 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3783 _mesa_lock_texture(ctx
, texObj
);
3785 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3788 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3789 format
, texImage
)) {
3790 /* error was recorded */
3792 else if (width
> 0 && height
> 0 && depth
> 0) {
3795 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3796 ctx
->Driver
.CompressedTexSubImage1D(ctx
, target
, level
,
3798 format
, imageSize
, data
,
3803 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3804 ctx
->Driver
.CompressedTexSubImage2D(ctx
, target
, level
,
3807 format
, imageSize
, data
,
3812 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3813 ctx
->Driver
.CompressedTexSubImage3D(ctx
, target
, level
,
3814 xoffset
, yoffset
, zoffset
,
3815 width
, height
, depth
,
3816 format
, imageSize
, data
,
3824 check_gen_mipmap(ctx
, target
, texObj
, level
);
3826 ctx
->NewState
|= _NEW_TEXTURE
;
3829 _mesa_unlock_texture(ctx
, texObj
);
3834 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3835 GLsizei width
, GLenum format
,
3836 GLsizei imageSize
, const GLvoid
*data
)
3838 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3839 format
, imageSize
, data
);
3844 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3845 GLint yoffset
, GLsizei width
, GLsizei height
,
3846 GLenum format
, GLsizei imageSize
,
3849 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3850 width
, height
, 1, format
, imageSize
, data
);
3855 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3856 GLint yoffset
, GLint zoffset
, GLsizei width
,
3857 GLsizei height
, GLsizei depth
, GLenum format
,
3858 GLsizei imageSize
, const GLvoid
*data
)
3860 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3861 width
, height
, depth
, format
, imageSize
, data
);