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
)
1233 return (((target
== GL_TEXTURE_2D
|| target
== GL_PROXY_TEXTURE_2D
))
1234 || ((ctx
->Extensions
.ARB_texture_cube_map
&&
1235 (target
== GL_PROXY_TEXTURE_CUBE_MAP
||
1236 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
1237 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))))
1238 || ((ctx
->Extensions
.MESA_texture_array
&&
1239 ((target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
) ||
1240 (target
== GL_TEXTURE_2D_ARRAY_EXT
)))));
1245 * Test the glTexImage[123]D() parameters for errors.
1247 * \param ctx GL context.
1248 * \param target texture target given by the user.
1249 * \param level image level given by the user.
1250 * \param internalFormat internal format given by the user.
1251 * \param format pixel data format given by the user.
1252 * \param type pixel data type given by the user.
1253 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1254 * \param width image width given by the user.
1255 * \param height image height given by the user.
1256 * \param depth image depth given by the user.
1257 * \param border image border given by the user.
1259 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1261 * Verifies each of the parameters against the constants specified in
1262 * __struct gl_contextRec::Const and the supported extensions, and according to the
1263 * OpenGL specification.
1266 texture_error_check( struct gl_context
*ctx
, GLenum target
,
1267 GLint level
, GLint internalFormat
,
1268 GLenum format
, GLenum type
,
1270 GLint width
, GLint height
,
1271 GLint depth
, GLint border
)
1273 const GLboolean isProxy
= _mesa_is_proxy_texture(target
);
1274 GLboolean sizeOK
= GL_TRUE
;
1275 GLboolean colorFormat
, indexFormat
;
1276 GLenum proxy_target
;
1278 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1279 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1281 _mesa_error(ctx
, GL_INVALID_VALUE
,
1282 "glTexImage%dD(level=%d)", dimensions
, level
);
1288 if (border
< 0 || border
> 1 ||
1289 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1290 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1292 _mesa_error(ctx
, GL_INVALID_VALUE
,
1293 "glTexImage%dD(border=%d)", dimensions
, border
);
1298 if (width
< 0 || height
< 0 || depth
< 0) {
1300 _mesa_error(ctx
, GL_INVALID_VALUE
,
1301 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1306 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1307 * level, width, height and depth.
1309 if (dimensions
== 1) {
1310 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1311 proxy_target
= GL_PROXY_TEXTURE_1D
;
1316 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1320 else if (dimensions
== 2) {
1322 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1323 proxy_target
= GL_PROXY_TEXTURE_2D
;
1325 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1326 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1327 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1328 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1332 proxy_target
= GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
1333 sizeOK
= (width
== height
);
1335 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1336 target
== GL_TEXTURE_RECTANGLE_NV
) {
1337 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1338 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1341 proxy_target
= GL_PROXY_TEXTURE_RECTANGLE_NV
;
1343 else if (target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
1344 target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1345 proxy_target
= GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
1348 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1352 else if (dimensions
== 3) {
1353 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1354 proxy_target
= GL_PROXY_TEXTURE_3D
;
1356 else if (target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
||
1357 target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1358 proxy_target
= GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
1361 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1366 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1370 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target
, level
,
1371 internalFormat
, format
,
1372 type
, width
, height
,
1376 _mesa_error(ctx
, GL_INVALID_VALUE
,
1377 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1378 dimensions
, level
, width
, height
, depth
);
1383 /* Check internalFormat */
1384 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1386 _mesa_error(ctx
, GL_INVALID_VALUE
,
1387 "glTexImage%dD(internalFormat=%s)",
1388 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1393 /* Check incoming image format and type */
1394 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1395 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1396 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1400 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1401 dimensions
, format
, type
);
1406 /* make sure internal format and format basically agree */
1407 colorFormat
= _mesa_is_color_format(format
);
1408 indexFormat
= _mesa_is_index_format(format
);
1409 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1410 (_mesa_is_index_format(internalFormat
) && !indexFormat
) ||
1411 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1412 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1413 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1414 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1416 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1417 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1418 dimensions
, internalFormat
, format
);
1422 /* additional checks for ycbcr textures */
1423 if (internalFormat
== GL_YCBCR_MESA
) {
1424 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1425 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1426 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1428 _mesa_snprintf(message
, sizeof(message
),
1429 "glTexImage%dD(format/type YCBCR mismatch", dimensions
);
1430 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1431 return GL_TRUE
; /* error */
1433 if (target
!= GL_TEXTURE_2D
&&
1434 target
!= GL_PROXY_TEXTURE_2D
&&
1435 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1436 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1438 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1444 _mesa_snprintf(message
, sizeof(message
),
1445 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1446 dimensions
, border
);
1447 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1453 /* additional checks for depth textures */
1454 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1455 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1456 if (target
!= GL_TEXTURE_1D
&&
1457 target
!= GL_PROXY_TEXTURE_1D
&&
1458 target
!= GL_TEXTURE_2D
&&
1459 target
!= GL_PROXY_TEXTURE_2D
&&
1460 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1461 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
) {
1463 _mesa_error(ctx
, GL_INVALID_ENUM
,
1464 "glTexImage(target/internalFormat)");
1469 /* additional checks for compressed textures */
1470 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1471 if (!target_can_be_compressed(ctx
, target
) && !isProxy
) {
1472 _mesa_error(ctx
, GL_INVALID_ENUM
,
1473 "glTexImage%dD(target)", dimensions
);
1478 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1479 "glTexImage%dD(border!=0)", dimensions
);
1485 /* if we get here, the parameters are OK */
1491 * Test glTexSubImage[123]D() parameters for errors.
1493 * \param ctx GL context.
1494 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1495 * \param target texture target given by the user.
1496 * \param level image level given by the user.
1497 * \param xoffset sub-image x offset given by the user.
1498 * \param yoffset sub-image y offset given by the user.
1499 * \param zoffset sub-image z offset given by the user.
1500 * \param format pixel data format given by the user.
1501 * \param type pixel data type given by the user.
1502 * \param width image width given by the user.
1503 * \param height image height given by the user.
1504 * \param depth image depth given by the user.
1506 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1508 * Verifies each of the parameters against the constants specified in
1509 * __struct gl_contextRec::Const and the supported extensions, and according to the
1510 * OpenGL specification.
1513 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1514 GLenum target
, GLint level
,
1515 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1516 GLint width
, GLint height
, GLint depth
,
1517 GLenum format
, GLenum type
)
1520 if (dimensions
== 1) {
1521 if (target
!= GL_TEXTURE_1D
) {
1522 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1526 else if (dimensions
== 2) {
1527 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1528 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1529 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1530 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1534 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1535 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1536 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1540 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1541 if (!ctx
->Extensions
.MESA_texture_array
) {
1542 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1546 else if (target
!= GL_TEXTURE_2D
) {
1547 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1551 else if (dimensions
== 3) {
1552 if (target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1553 if (!ctx
->Extensions
.MESA_texture_array
) {
1554 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1558 else if (target
!= GL_TEXTURE_3D
) {
1559 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1564 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1568 /* Basic level check */
1569 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1570 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1575 _mesa_error(ctx
, GL_INVALID_VALUE
,
1576 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1579 if (height
< 0 && dimensions
> 1) {
1580 _mesa_error(ctx
, GL_INVALID_VALUE
,
1581 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1584 if (depth
< 0 && dimensions
> 2) {
1585 _mesa_error(ctx
, GL_INVALID_VALUE
,
1586 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1590 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1591 _mesa_error(ctx
, GL_INVALID_ENUM
,
1592 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1593 dimensions
, format
, type
);
1602 * Do second part of glTexSubImage which depends on the destination texture.
1603 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1606 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1607 GLenum target
, GLint level
,
1608 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1609 GLint width
, GLint height
, GLint depth
,
1610 GLenum format
, GLenum type
,
1611 const struct gl_texture_image
*destTex
)
1614 /* undefined image level */
1615 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1619 if (xoffset
< -((GLint
)destTex
->Border
)) {
1620 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1624 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1625 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1629 if (dimensions
> 1) {
1630 if (yoffset
< -((GLint
)destTex
->Border
)) {
1631 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1635 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1636 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1641 if (dimensions
> 2) {
1642 if (zoffset
< -((GLint
)destTex
->Border
)) {
1643 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1646 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1647 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1652 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
1655 if (!target_can_be_compressed(ctx
, target
)) {
1656 _mesa_error(ctx
, GL_INVALID_ENUM
,
1657 "glTexSubImage%dD(target=%s)", dimensions
,
1658 _mesa_lookup_enum_by_nr(target
));
1662 /* do tests which depend on compression block size */
1663 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
1665 /* offset must be multiple of block size */
1666 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1668 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
1669 dimensions
, xoffset
, yoffset
);
1672 /* size must be multiple of bw by bh or equal to whole texture size */
1673 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
1674 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1675 "glTexSubImage%dD(width = %d)", dimensions
, width
);
1678 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
1679 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1680 "glTexSubImage%dD(height = %d)", dimensions
, height
);
1690 * Test glCopyTexImage[12]D() parameters for errors.
1692 * \param ctx GL context.
1693 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1694 * \param target texture target given by the user.
1695 * \param level image level given by the user.
1696 * \param internalFormat internal format given by the user.
1697 * \param width image width given by the user.
1698 * \param height image height given by the user.
1699 * \param border texture border.
1701 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1703 * Verifies each of the parameters against the constants specified in
1704 * __struct gl_contextRec::Const and the supported extensions, and according to the
1705 * OpenGL specification.
1708 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1709 GLenum target
, GLint level
, GLint internalFormat
,
1710 GLint width
, GLint height
, GLint border
)
1716 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1717 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1718 _mesa_error(ctx
, GL_INVALID_VALUE
,
1719 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1723 /* Check that the source buffer is complete */
1724 if (ctx
->ReadBuffer
->Name
) {
1725 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1726 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1727 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1728 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1734 if (border
< 0 || border
> 1 ||
1735 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1736 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1740 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1742 _mesa_error(ctx
, GL_INVALID_VALUE
,
1743 "glCopyTexImage%dD(internalFormat)", dimensions
);
1747 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1748 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1749 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
1753 /* NOTE: the format and type aren't really significant for
1754 * TestProxyTexImage(). Only the internalformat really matters.
1758 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1759 * level, width, height and depth.
1761 if (dimensions
== 1) {
1762 if (target
== GL_TEXTURE_1D
) {
1763 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1764 level
, internalFormat
,
1766 width
, 1, 1, border
);
1769 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1773 else if (dimensions
== 2) {
1774 if (target
== GL_TEXTURE_2D
) {
1775 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1776 level
, internalFormat
,
1778 width
, height
, 1, border
);
1780 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1781 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1782 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1783 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1786 sizeOK
= (width
== height
) &&
1787 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1788 level
, internalFormat
, format
, type
,
1789 width
, height
, 1, border
);
1791 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1792 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1793 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1796 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1797 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1798 level
, internalFormat
,
1800 width
, height
, 1, border
);
1802 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1803 if (!ctx
->Extensions
.MESA_texture_array
) {
1804 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)");
1807 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1808 GL_PROXY_TEXTURE_1D_ARRAY_EXT
,
1809 level
, internalFormat
,
1811 width
, height
, 1, border
);
1814 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1819 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1824 if (dimensions
== 1) {
1825 _mesa_error(ctx
, GL_INVALID_VALUE
,
1826 "glCopyTexImage1D(width=%d)", width
);
1829 ASSERT(dimensions
== 2);
1830 _mesa_error(ctx
, GL_INVALID_VALUE
,
1831 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1836 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1837 if (!target_can_be_compressed(ctx
, target
)) {
1838 _mesa_error(ctx
, GL_INVALID_ENUM
,
1839 "glCopyTexImage%dD(target)", dimensions
);
1843 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1844 "glCopyTexImage%dD(border!=0)", dimensions
);
1848 else if (_mesa_is_depth_format(internalFormat
)) {
1849 /* make sure we have depth/stencil buffers */
1850 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
1851 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1852 "glCopyTexImage%dD(no depth)", dimensions
);
1856 else if (_mesa_is_depthstencil_format(internalFormat
)) {
1857 /* make sure we have depth/stencil buffers */
1858 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
1859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1860 "glCopyTexImage%dD(no depth/stencil buffer)", dimensions
);
1865 /* if we get here, the parameters are OK */
1871 * Test glCopyTexSubImage[12]D() parameters for errors.
1872 * Note that this is the first part of error checking.
1873 * See also copytexsubimage_error_check2() below for the second part.
1875 * \param ctx GL context.
1876 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1877 * \param target texture target given by the user.
1878 * \param level image level given by the user.
1880 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1883 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
1884 GLenum target
, GLint level
)
1886 /* Check that the source buffer is complete */
1887 if (ctx
->ReadBuffer
->Name
) {
1888 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1889 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1890 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1891 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1897 if (dimensions
== 1) {
1898 if (target
!= GL_TEXTURE_1D
) {
1899 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1903 else if (dimensions
== 2) {
1904 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1905 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1906 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1907 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1911 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1912 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1913 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1917 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1918 if (!ctx
->Extensions
.MESA_texture_array
) {
1919 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1923 else if (target
!= GL_TEXTURE_2D
) {
1924 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1928 else if (dimensions
== 3) {
1929 if (((target
!= GL_TEXTURE_2D_ARRAY_EXT
) ||
1930 (!ctx
->Extensions
.MESA_texture_array
))
1931 && (target
!= GL_TEXTURE_3D
)) {
1932 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1938 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1939 _mesa_error(ctx
, GL_INVALID_VALUE
,
1940 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1949 * Second part of error checking for glCopyTexSubImage[12]D().
1950 * \param xoffset sub-image x offset given by the user.
1951 * \param yoffset sub-image y offset given by the user.
1952 * \param zoffset sub-image z offset given by the user.
1953 * \param width image width given by the user.
1954 * \param height image height given by the user.
1957 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1958 GLenum target
, GLint level
,
1959 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1960 GLsizei width
, GLsizei height
,
1961 const struct gl_texture_image
*teximage
)
1963 /* check that dest tex image exists */
1965 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1966 "glCopyTexSubImage%dD(undefined texture level: %d)",
1973 _mesa_error(ctx
, GL_INVALID_VALUE
,
1974 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1977 if (dimensions
> 1 && height
< 0) {
1978 _mesa_error(ctx
, GL_INVALID_VALUE
,
1979 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1983 /* check x/y offsets */
1984 if (xoffset
< -((GLint
)teximage
->Border
)) {
1985 _mesa_error(ctx
, GL_INVALID_VALUE
,
1986 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1989 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1990 _mesa_error(ctx
, GL_INVALID_VALUE
,
1991 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1994 if (dimensions
> 1) {
1995 if (yoffset
< -((GLint
)teximage
->Border
)) {
1996 _mesa_error(ctx
, GL_INVALID_VALUE
,
1997 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2000 /* NOTE: we're adding the border here, not subtracting! */
2001 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
2002 _mesa_error(ctx
, GL_INVALID_VALUE
,
2003 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2008 /* check z offset */
2009 if (dimensions
> 2) {
2010 if (zoffset
< -((GLint
)teximage
->Border
)) {
2011 _mesa_error(ctx
, GL_INVALID_VALUE
,
2012 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2015 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
2016 _mesa_error(ctx
, GL_INVALID_VALUE
,
2017 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2022 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2023 if (!target_can_be_compressed(ctx
, target
)) {
2024 _mesa_error(ctx
, GL_INVALID_ENUM
,
2025 "glCopyTexSubImage%dD(target)", dimensions
);
2028 /* offset must be multiple of 4 */
2029 if ((xoffset
& 3) || (yoffset
& 3)) {
2030 _mesa_error(ctx
, GL_INVALID_VALUE
,
2031 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2034 /* size must be multiple of 4 */
2035 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2036 _mesa_error(ctx
, GL_INVALID_VALUE
,
2037 "glCopyTexSubImage%dD(width)", dimensions
);
2040 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2041 _mesa_error(ctx
, GL_INVALID_VALUE
,
2042 "glCopyTexSubImage%dD(height)", dimensions
);
2047 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2048 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2052 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2053 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2054 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2055 dimensions
, teximage
->_BaseFormat
);
2059 if (teximage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2060 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
2061 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2062 "glCopyTexSubImage%dD(no depth buffer)",
2067 else if (teximage
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
2068 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
2069 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2070 "glCopyTexSubImage%dD(no depth/stencil buffer)",
2076 /* if we get here, the parameters are OK */
2081 /** Callback info for walking over FBO hash table */
2084 struct gl_context
*ctx
;
2085 struct gl_texture_object
*texObj
;
2091 * Check render to texture callback. Called from _mesa_HashWalk().
2094 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2096 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2097 const struct cb_info
*info
= (struct cb_info
*) userData
;
2098 struct gl_context
*ctx
= info
->ctx
;
2099 const struct gl_texture_object
*texObj
= info
->texObj
;
2100 const GLuint level
= info
->level
, face
= info
->face
;
2102 /* If this is a user-created FBO */
2105 /* check if any of the FBO's attachments point to 'texObj' */
2106 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2107 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2108 if (att
->Type
== GL_TEXTURE
&&
2109 att
->Texture
== texObj
&&
2110 att
->TextureLevel
== level
&&
2111 att
->CubeMapFace
== face
) {
2112 ASSERT(att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]);
2113 /* Tell driver about the new renderbuffer texture */
2114 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2115 /* Mark fb status as indeterminate to force re-validation */
2124 * When a texture image is specified we have to check if it's bound to
2125 * any framebuffer objects (render to texture) in order to detect changes
2126 * in size or format since that effects FBO completeness.
2127 * Any FBOs rendering into the texture must be re-validated.
2130 update_fbo_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
2131 GLuint face
, GLuint level
)
2133 /* Only check this texture if it's been marked as RenderToTexture */
2134 if (texObj
->_RenderToTexture
) {
2135 struct cb_info info
;
2137 info
.texObj
= texObj
;
2140 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2146 * If the texture object's GenerateMipmap flag is set and we've
2147 * changed the texture base level image, regenerate the rest of the
2148 * mipmap levels now.
2151 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2152 struct gl_texture_object
*texObj
, GLint level
)
2154 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2155 if (texObj
->GenerateMipmap
&&
2156 level
== texObj
->BaseLevel
&&
2157 level
< texObj
->MaxLevel
) {
2158 ASSERT(ctx
->Driver
.GenerateMipmap
);
2159 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2164 /** Debug helper: override the user-requested internal format */
2166 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2169 if (internalFormat
== GL_RGBA16F_ARB
||
2170 internalFormat
== GL_RGBA32F_ARB
) {
2171 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2174 else if (internalFormat
== GL_RGB16F_ARB
||
2175 internalFormat
== GL_RGB32F_ARB
) {
2176 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2179 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2180 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2181 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2182 return GL_LUMINANCE_ALPHA
;
2184 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2185 internalFormat
== GL_LUMINANCE32F_ARB
) {
2186 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2187 return GL_LUMINANCE
;
2189 else if (internalFormat
== GL_ALPHA16F_ARB
||
2190 internalFormat
== GL_ALPHA32F_ARB
) {
2191 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2195 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2196 internalFormat = GL_RGBA;
2200 return internalFormat
;
2203 return internalFormat
;
2209 * Choose the actual hardware format for a texture image.
2210 * Try to use the same format as the previous image level when possible.
2211 * Otherwise, ask the driver for the best format.
2212 * It's important to try to choose a consistant format for all levels
2213 * for efficient texture memory layout/allocation. In particular, this
2214 * comes up during automatic mipmap generation.
2217 _mesa_choose_texture_format(struct gl_context
*ctx
,
2218 struct gl_texture_object
*texObj
,
2219 struct gl_texture_image
*texImage
,
2220 GLenum target
, GLint level
,
2221 GLenum internalFormat
, GLenum format
, GLenum type
)
2223 /* see if we've already chosen a format for the previous level */
2225 struct gl_texture_image
*prevImage
=
2226 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2227 /* See if the prev level is defined and has an internal format which
2228 * matches the new internal format.
2231 prevImage
->Width
> 0 &&
2232 prevImage
->InternalFormat
== internalFormat
) {
2233 /* use the same format */
2234 texImage
->TexFormat
= prevImage
->TexFormat
;
2239 /* choose format from scratch */
2240 texImage
->TexFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
,
2242 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2248 * Called from the API. Note that width includes the border.
2251 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2252 GLsizei width
, GLint border
, GLenum format
,
2253 GLenum type
, const GLvoid
*pixels
)
2255 GET_CURRENT_CONTEXT(ctx
);
2256 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2258 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2259 _mesa_debug(ctx
, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2260 _mesa_lookup_enum_by_nr(target
), level
,
2261 _mesa_lookup_enum_by_nr(internalFormat
), width
, border
,
2262 _mesa_lookup_enum_by_nr(format
),
2263 _mesa_lookup_enum_by_nr(type
), pixels
);
2265 internalFormat
= override_internal_format(internalFormat
, width
, 1);
2267 if (target
== GL_TEXTURE_1D
) {
2268 /* non-proxy target */
2269 struct gl_texture_object
*texObj
;
2270 struct gl_texture_image
*texImage
;
2271 const GLuint face
= _mesa_tex_target_to_face(target
);
2273 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2274 format
, type
, 1, width
, 1, 1, border
)) {
2275 return; /* error was recorded */
2278 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2279 _mesa_update_state(ctx
);
2281 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2282 _mesa_lock_texture(ctx
, texObj
);
2284 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2286 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2289 if (texImage
->Data
) {
2290 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2293 ASSERT(texImage
->Data
== NULL
);
2295 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2296 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2298 border
, internalFormat
);
2300 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2301 internalFormat
, format
, type
);
2303 /* Give the texture to the driver. <pixels> may be null. */
2304 ASSERT(ctx
->Driver
.TexImage1D
);
2305 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2306 width
, border
, format
, type
, pixels
,
2307 &ctx
->Unpack
, texObj
, texImage
);
2309 ASSERT(texImage
->TexFormat
);
2311 _mesa_set_fetch_functions(texImage
, 1);
2313 check_gen_mipmap(ctx
, target
, texObj
, level
);
2315 update_fbo_texture(ctx
, texObj
, face
, level
);
2318 texObj
->_Complete
= GL_FALSE
;
2319 ctx
->NewState
|= _NEW_TEXTURE
;
2322 _mesa_unlock_texture(ctx
, texObj
);
2324 else if (target
== GL_PROXY_TEXTURE_1D
) {
2325 /* Proxy texture: check for errors and update proxy state */
2326 struct gl_texture_image
*texImage
;
2327 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2328 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2329 format
, type
, 1, width
, 1, 1, border
)) {
2330 /* when error, clear all proxy texture image parameters */
2332 clear_teximage_fields(texImage
);
2335 /* no error, set the tex image parameters */
2336 struct gl_texture_object
*texObj
=
2337 _mesa_get_current_tex_object(ctx
, target
);
2339 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2341 border
, internalFormat
);
2342 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2343 internalFormat
, format
, type
);
2347 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2354 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2355 GLsizei width
, GLsizei height
, GLint border
,
2356 GLenum format
, GLenum type
,
2357 const GLvoid
*pixels
)
2359 GET_CURRENT_CONTEXT(ctx
);
2360 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2362 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2363 _mesa_debug(ctx
, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2364 _mesa_lookup_enum_by_nr(target
), level
,
2365 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2366 border
, _mesa_lookup_enum_by_nr(format
),
2367 _mesa_lookup_enum_by_nr(type
), pixels
);
2369 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2371 if (target
== GL_TEXTURE_2D
||
2372 (ctx
->Extensions
.ARB_texture_cube_map
&&
2373 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2374 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2375 (ctx
->Extensions
.NV_texture_rectangle
&&
2376 target
== GL_TEXTURE_RECTANGLE_NV
) ||
2377 (ctx
->Extensions
.MESA_texture_array
&&
2378 target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
2379 /* non-proxy target */
2380 struct gl_texture_object
*texObj
;
2381 struct gl_texture_image
*texImage
;
2382 const GLuint face
= _mesa_tex_target_to_face(target
);
2384 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2385 format
, type
, 2, width
, height
, 1, border
)) {
2386 return; /* error was recorded */
2389 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2390 _mesa_update_state(ctx
);
2392 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2393 _mesa_lock_texture(ctx
, texObj
);
2395 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2397 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2400 if (texImage
->Data
) {
2401 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2404 ASSERT(texImage
->Data
== NULL
);
2405 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2406 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2408 border
, internalFormat
);
2410 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2411 internalFormat
, format
, type
);
2413 /* Give the texture to the driver. <pixels> may be null. */
2414 ASSERT(ctx
->Driver
.TexImage2D
);
2415 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2416 width
, height
, border
, format
, type
,
2417 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2419 ASSERT(texImage
->TexFormat
);
2421 _mesa_set_fetch_functions(texImage
, 2);
2423 check_gen_mipmap(ctx
, target
, texObj
, level
);
2425 update_fbo_texture(ctx
, texObj
, face
, level
);
2428 texObj
->_Complete
= GL_FALSE
;
2429 ctx
->NewState
|= _NEW_TEXTURE
;
2432 _mesa_unlock_texture(ctx
, texObj
);
2434 else if (target
== GL_PROXY_TEXTURE_2D
||
2435 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2436 ctx
->Extensions
.ARB_texture_cube_map
) ||
2437 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2438 ctx
->Extensions
.NV_texture_rectangle
) ||
2439 (ctx
->Extensions
.MESA_texture_array
&&
2440 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
)) {
2441 /* Proxy texture: check for errors and update proxy state */
2442 struct gl_texture_image
*texImage
;
2443 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2444 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2445 format
, type
, 2, width
, height
, 1, border
)) {
2446 /* when error, clear all proxy texture image parameters */
2448 clear_teximage_fields(texImage
);
2451 /* no error, set the tex image parameters */
2452 struct gl_texture_object
*texObj
=
2453 _mesa_get_current_tex_object(ctx
, target
);
2454 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2456 border
, internalFormat
);
2457 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2458 internalFormat
, format
, type
);
2462 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2469 * Called by the API or display list executor.
2470 * Note that width and height include the border.
2473 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2474 GLsizei width
, GLsizei height
, GLsizei depth
,
2475 GLint border
, GLenum format
, GLenum type
,
2476 const GLvoid
*pixels
)
2478 GET_CURRENT_CONTEXT(ctx
);
2479 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2481 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2482 _mesa_debug(ctx
, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2483 _mesa_lookup_enum_by_nr(target
), level
,
2484 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2485 depth
, border
, _mesa_lookup_enum_by_nr(format
),
2486 _mesa_lookup_enum_by_nr(type
), pixels
);
2488 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2490 if (target
== GL_TEXTURE_3D
||
2491 (ctx
->Extensions
.MESA_texture_array
&&
2492 target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
2493 /* non-proxy target */
2494 struct gl_texture_object
*texObj
;
2495 struct gl_texture_image
*texImage
;
2496 const GLuint face
= _mesa_tex_target_to_face(target
);
2498 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2499 format
, type
, 3, width
, height
, depth
, border
)) {
2500 return; /* error was recorded */
2503 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2504 _mesa_update_state(ctx
);
2506 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2507 _mesa_lock_texture(ctx
, texObj
);
2509 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2511 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2514 if (texImage
->Data
) {
2515 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2518 ASSERT(texImage
->Data
== NULL
);
2519 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2520 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2521 width
, height
, depth
,
2522 border
, internalFormat
);
2524 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2525 internalFormat
, format
, type
);
2527 /* Give the texture to the driver. <pixels> may be null. */
2528 ASSERT(ctx
->Driver
.TexImage3D
);
2529 ctx
->Driver
.TexImage3D(ctx
, target
, level
, internalFormat
,
2530 width
, height
, depth
, border
, format
, type
,
2531 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2533 ASSERT(texImage
->TexFormat
);
2535 _mesa_set_fetch_functions(texImage
, 3);
2537 check_gen_mipmap(ctx
, target
, texObj
, level
);
2539 update_fbo_texture(ctx
, texObj
, face
, level
);
2542 texObj
->_Complete
= GL_FALSE
;
2543 ctx
->NewState
|= _NEW_TEXTURE
;
2546 _mesa_unlock_texture(ctx
, texObj
);
2548 else if (target
== GL_PROXY_TEXTURE_3D
||
2549 (ctx
->Extensions
.MESA_texture_array
&&
2550 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
)) {
2551 /* Proxy texture: check for errors and update proxy state */
2552 struct gl_texture_image
*texImage
;
2553 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2554 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2555 format
, type
, 3, width
, height
, depth
, border
)) {
2556 /* when error, clear all proxy texture image parameters */
2558 clear_teximage_fields(texImage
);
2561 /* no error, set the tex image parameters */
2562 struct gl_texture_object
*texObj
=
2563 _mesa_get_current_tex_object(ctx
, target
);
2564 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
2565 depth
, border
, internalFormat
);
2566 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2567 internalFormat
, format
, type
);
2571 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2578 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2579 GLsizei width
, GLsizei height
, GLsizei depth
,
2580 GLint border
, GLenum format
, GLenum type
,
2581 const GLvoid
*pixels
)
2583 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2584 depth
, border
, format
, type
, pixels
);
2588 #if FEATURE_OES_EGL_image
2590 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
2592 struct gl_texture_object
*texObj
;
2593 struct gl_texture_image
*texImage
;
2594 GET_CURRENT_CONTEXT(ctx
);
2595 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2597 if (!ctx
->Extensions
.OES_EGL_image
) {
2598 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2599 "glEGLImageTargetTexture2DOES(unsupported)");
2603 if (target
!= GL_TEXTURE_2D
) {
2604 _mesa_error(ctx
, GL_INVALID_ENUM
,
2605 "glEGLImageTargetTexture2D(target=%d)", target
);
2609 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2610 _mesa_update_state(ctx
);
2612 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2613 _mesa_lock_texture(ctx
, texObj
);
2615 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
2617 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
2620 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2622 ASSERT(texImage
->Data
== NULL
);
2623 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
2624 texObj
, texImage
, image
);
2627 texObj
->_Complete
= GL_FALSE
;
2628 ctx
->NewState
|= _NEW_TEXTURE
;
2630 _mesa_unlock_texture(ctx
, texObj
);
2637 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2638 GLint xoffset
, GLsizei width
,
2639 GLenum format
, GLenum type
,
2640 const GLvoid
*pixels
)
2642 struct gl_texture_object
*texObj
;
2643 struct gl_texture_image
*texImage
;
2644 GET_CURRENT_CONTEXT(ctx
);
2645 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2647 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2648 _mesa_debug(ctx
, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2649 _mesa_lookup_enum_by_nr(target
), level
,
2650 xoffset
, width
, _mesa_lookup_enum_by_nr(format
),
2651 _mesa_lookup_enum_by_nr(type
), pixels
);
2653 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2654 _mesa_update_state(ctx
);
2656 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2657 width
, 1, 1, format
, type
)) {
2658 return; /* error was detected */
2662 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2665 _mesa_lock_texture(ctx
, texObj
);
2667 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2669 if (subtexture_error_check2(ctx
, 1, target
, level
, xoffset
, 0, 0,
2670 width
, 1, 1, format
, type
, texImage
)) {
2671 /* error was recorded */
2673 else if (width
> 0) {
2674 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2675 xoffset
+= texImage
->Border
;
2677 ASSERT(ctx
->Driver
.TexSubImage1D
);
2678 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
, width
,
2679 format
, type
, pixels
, &ctx
->Unpack
,
2682 check_gen_mipmap(ctx
, target
, texObj
, level
);
2684 ctx
->NewState
|= _NEW_TEXTURE
;
2687 _mesa_unlock_texture(ctx
, texObj
);
2692 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2693 GLint xoffset
, GLint yoffset
,
2694 GLsizei width
, GLsizei height
,
2695 GLenum format
, GLenum type
,
2696 const GLvoid
*pixels
)
2698 struct gl_texture_object
*texObj
;
2699 struct gl_texture_image
*texImage
;
2700 GET_CURRENT_CONTEXT(ctx
);
2701 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2703 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2704 _mesa_debug(ctx
, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2705 _mesa_lookup_enum_by_nr(target
), level
,
2706 xoffset
, yoffset
, width
, height
,
2707 _mesa_lookup_enum_by_nr(format
),
2708 _mesa_lookup_enum_by_nr(type
), pixels
);
2710 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2711 _mesa_update_state(ctx
);
2713 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2714 width
, height
, 1, format
, type
)) {
2715 return; /* error was detected */
2718 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2720 _mesa_lock_texture(ctx
, texObj
);
2722 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2724 if (subtexture_error_check2(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2725 width
, height
, 1, format
, type
, texImage
)) {
2726 /* error was recorded */
2728 else if (width
> 0 && height
>= 0) {
2729 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2730 xoffset
+= texImage
->Border
;
2731 yoffset
+= texImage
->Border
;
2733 ASSERT(ctx
->Driver
.TexSubImage2D
);
2734 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2735 width
, height
, format
, type
, pixels
,
2736 &ctx
->Unpack
, texObj
, texImage
);
2738 check_gen_mipmap(ctx
, target
, texObj
, level
);
2740 ctx
->NewState
|= _NEW_TEXTURE
;
2743 _mesa_unlock_texture(ctx
, texObj
);
2749 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2750 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2751 GLsizei width
, GLsizei height
, GLsizei depth
,
2752 GLenum format
, GLenum type
,
2753 const GLvoid
*pixels
)
2755 struct gl_texture_object
*texObj
;
2756 struct gl_texture_image
*texImage
;
2757 GET_CURRENT_CONTEXT(ctx
);
2758 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2760 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2761 _mesa_debug(ctx
, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2762 _mesa_lookup_enum_by_nr(target
), level
,
2763 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2764 _mesa_lookup_enum_by_nr(format
),
2765 _mesa_lookup_enum_by_nr(type
), pixels
);
2767 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2768 _mesa_update_state(ctx
);
2770 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2771 width
, height
, depth
, format
, type
)) {
2772 return; /* error was detected */
2775 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2777 _mesa_lock_texture(ctx
, texObj
);
2779 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2781 if (subtexture_error_check2(ctx
, 3, target
, level
,
2782 xoffset
, yoffset
, zoffset
,
2783 width
, height
, depth
,
2784 format
, type
, texImage
)) {
2785 /* error was recorded */
2787 else if (width
> 0 && height
> 0 && height
> 0) {
2788 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2789 xoffset
+= texImage
->Border
;
2790 yoffset
+= texImage
->Border
;
2791 zoffset
+= texImage
->Border
;
2793 ASSERT(ctx
->Driver
.TexSubImage3D
);
2794 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
,
2795 xoffset
, yoffset
, zoffset
,
2796 width
, height
, depth
,
2797 format
, type
, pixels
,
2798 &ctx
->Unpack
, texObj
, texImage
);
2800 check_gen_mipmap(ctx
, target
, texObj
, level
);
2802 ctx
->NewState
|= _NEW_TEXTURE
;
2805 _mesa_unlock_texture(ctx
, texObj
);
2811 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2812 GLenum internalFormat
,
2814 GLsizei width
, GLint border
)
2816 struct gl_texture_object
*texObj
;
2817 struct gl_texture_image
*texImage
;
2818 const GLuint face
= _mesa_tex_target_to_face(target
);
2819 GET_CURRENT_CONTEXT(ctx
);
2820 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2822 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2823 _mesa_debug(ctx
, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2824 _mesa_lookup_enum_by_nr(target
), level
,
2825 _mesa_lookup_enum_by_nr(internalFormat
),
2826 x
, y
, width
, border
);
2828 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2829 _mesa_update_state(ctx
);
2831 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2835 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2837 _mesa_lock_texture(ctx
, texObj
);
2839 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2841 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2844 if (texImage
->Data
) {
2845 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2848 ASSERT(texImage
->Data
== NULL
);
2850 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2851 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2852 border
, internalFormat
);
2854 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2855 internalFormat
, GL_NONE
, GL_NONE
);
2857 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2858 ctx
->Driver
.CopyTexImage1D(ctx
, target
, level
, internalFormat
,
2859 x
, y
, width
, border
);
2861 ASSERT(texImage
->TexFormat
);
2863 _mesa_set_fetch_functions(texImage
, 1);
2865 check_gen_mipmap(ctx
, target
, texObj
, level
);
2867 update_fbo_texture(ctx
, texObj
, face
, level
);
2870 texObj
->_Complete
= GL_FALSE
;
2871 ctx
->NewState
|= _NEW_TEXTURE
;
2874 _mesa_unlock_texture(ctx
, texObj
);
2880 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2881 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2884 struct gl_texture_object
*texObj
;
2885 struct gl_texture_image
*texImage
;
2886 const GLuint face
= _mesa_tex_target_to_face(target
);
2887 GET_CURRENT_CONTEXT(ctx
);
2888 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2890 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2891 _mesa_debug(ctx
, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2892 _mesa_lookup_enum_by_nr(target
), level
,
2893 _mesa_lookup_enum_by_nr(internalFormat
),
2894 x
, y
, width
, height
, border
);
2896 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2897 _mesa_update_state(ctx
);
2899 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2900 width
, height
, border
))
2903 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2905 _mesa_lock_texture(ctx
, texObj
);
2907 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2910 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2913 if (texImage
->Data
) {
2914 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2917 ASSERT(texImage
->Data
== NULL
);
2919 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2920 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2922 border
, internalFormat
);
2924 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2925 internalFormat
, GL_NONE
, GL_NONE
);
2927 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2928 ctx
->Driver
.CopyTexImage2D(ctx
, target
, level
, internalFormat
,
2929 x
, y
, width
, height
, border
);
2931 ASSERT(texImage
->TexFormat
);
2933 _mesa_set_fetch_functions(texImage
, 2);
2935 check_gen_mipmap(ctx
, target
, texObj
, level
);
2937 update_fbo_texture(ctx
, texObj
, face
, level
);
2940 texObj
->_Complete
= GL_FALSE
;
2941 ctx
->NewState
|= _NEW_TEXTURE
;
2944 _mesa_unlock_texture(ctx
, texObj
);
2949 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2950 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2952 struct gl_texture_object
*texObj
;
2953 struct gl_texture_image
*texImage
;
2957 GET_CURRENT_CONTEXT(ctx
);
2958 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2960 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2961 _mesa_debug(ctx
, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
2962 _mesa_lookup_enum_by_nr(target
),
2963 level
, xoffset
, x
, y
, width
);
2965 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2966 _mesa_update_state(ctx
);
2968 if (copytexsubimage_error_check1(ctx
, 1, target
, level
))
2971 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2973 _mesa_lock_texture(ctx
, texObj
);
2975 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2977 if (copytexsubimage_error_check2(ctx
, 1, target
, level
,
2978 xoffset
, 0, 0, width
, 1, texImage
)) {
2979 /* error was recorded */
2982 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2983 xoffset
+= texImage
->Border
;
2985 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
2987 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
2988 ctx
->Driver
.CopyTexSubImage1D(ctx
, target
, level
,
2989 xoffset
, x
, y
, width
);
2991 check_gen_mipmap(ctx
, target
, texObj
, level
);
2993 ctx
->NewState
|= _NEW_TEXTURE
;
2997 _mesa_unlock_texture(ctx
, texObj
);
3003 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3004 GLint xoffset
, GLint yoffset
,
3005 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3007 struct gl_texture_object
*texObj
;
3008 struct gl_texture_image
*texImage
;
3009 GET_CURRENT_CONTEXT(ctx
);
3010 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3012 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3013 _mesa_debug(ctx
, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
3014 _mesa_lookup_enum_by_nr(target
),
3015 level
, xoffset
, yoffset
, x
, y
, width
, height
);
3017 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3018 _mesa_update_state(ctx
);
3020 if (copytexsubimage_error_check1(ctx
, 2, target
, level
))
3023 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3025 _mesa_lock_texture(ctx
, texObj
);
3027 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3029 if (copytexsubimage_error_check2(ctx
, 2, target
, level
,
3030 xoffset
, yoffset
, 0,
3031 width
, height
, texImage
)) {
3032 /* error was recorded */
3035 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3036 xoffset
+= texImage
->Border
;
3037 yoffset
+= texImage
->Border
;
3039 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3041 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
3042 ctx
->Driver
.CopyTexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
3043 x
, y
, width
, height
);
3045 check_gen_mipmap(ctx
, target
, texObj
, level
);
3047 ctx
->NewState
|= _NEW_TEXTURE
;
3051 _mesa_unlock_texture(ctx
, texObj
);
3057 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3058 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3059 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3061 struct gl_texture_object
*texObj
;
3062 struct gl_texture_image
*texImage
;
3063 GET_CURRENT_CONTEXT(ctx
);
3064 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3066 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3067 _mesa_debug(ctx
, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
3068 _mesa_lookup_enum_by_nr(target
),
3069 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3071 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3072 _mesa_update_state(ctx
);
3074 if (copytexsubimage_error_check1(ctx
, 3, target
, level
))
3077 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3079 _mesa_lock_texture(ctx
, texObj
);
3081 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3083 if (copytexsubimage_error_check2(ctx
, 3, target
, level
, xoffset
, yoffset
,
3084 zoffset
, width
, height
, texImage
)) {
3085 /* error was recored */
3088 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3089 xoffset
+= texImage
->Border
;
3090 yoffset
+= texImage
->Border
;
3091 zoffset
+= texImage
->Border
;
3093 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3095 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
3096 ctx
->Driver
.CopyTexSubImage3D(ctx
, target
, level
,
3097 xoffset
, yoffset
, zoffset
,
3098 x
, y
, width
, height
);
3100 check_gen_mipmap(ctx
, target
, texObj
, level
);
3102 ctx
->NewState
|= _NEW_TEXTURE
;
3106 _mesa_unlock_texture(ctx
, texObj
);
3112 /**********************************************************************/
3113 /****** Compressed Textures ******/
3114 /**********************************************************************/
3118 * Return expected size of a compressed texture.
3121 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
3124 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3125 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
3130 * Return compressed texture block size, in pixels.
3133 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
3135 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3136 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
3141 * Error checking for glCompressedTexImage[123]D().
3142 * \return error code or GL_NO_ERROR.
3145 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3146 GLenum target
, GLint level
,
3147 GLenum internalFormat
, GLsizei width
,
3148 GLsizei height
, GLsizei depth
, GLint border
,
3151 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3153 if (dimensions
== 1) {
3154 /* 1D compressed textures not allowed */
3155 return GL_INVALID_ENUM
;
3157 else if (dimensions
== 2) {
3158 if (target
== GL_PROXY_TEXTURE_2D
) {
3159 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3161 else if (target
== GL_TEXTURE_2D
) {
3162 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3164 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3165 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3166 return GL_INVALID_ENUM
; /*target*/
3167 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3169 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3170 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3171 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3172 return GL_INVALID_ENUM
; /*target*/
3173 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3176 return GL_INVALID_ENUM
; /*target*/
3179 else if (dimensions
== 3) {
3180 /* 3D compressed textures not allowed */
3181 return GL_INVALID_ENUM
;
3185 return GL_INVALID_ENUM
;
3188 maxTextureSize
= 1 << (maxLevels
- 1);
3190 /* This will detect any invalid internalFormat value */
3191 if (!_mesa_is_compressed_format(ctx
, internalFormat
))
3192 return GL_INVALID_ENUM
;
3194 /* This should really never fail */
3195 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
3196 return GL_INVALID_ENUM
;
3199 return GL_INVALID_VALUE
;
3202 * XXX We should probably use the proxy texture error check function here.
3204 if (width
< 1 || width
> maxTextureSize
||
3205 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(width
)))
3206 return GL_INVALID_VALUE
;
3208 if ((height
< 1 || height
> maxTextureSize
||
3209 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(height
)))
3211 return GL_INVALID_VALUE
;
3213 if ((depth
< 1 || depth
> maxTextureSize
||
3214 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(depth
)))
3216 return GL_INVALID_VALUE
;
3218 /* For cube map, width must equal height */
3219 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3220 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
3221 return GL_INVALID_VALUE
;
3223 if (level
< 0 || level
>= maxLevels
)
3224 return GL_INVALID_VALUE
;
3226 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
3227 if (expectedSize
!= imageSize
)
3228 return GL_INVALID_VALUE
;
3230 #if FEATURE_EXT_texture_sRGB
3231 if ((internalFormat
== GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
||
3232 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
||
3233 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
||
3234 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
)
3236 return GL_INVALID_OPERATION
;
3245 * Error checking for glCompressedTexSubImage[123]D().
3246 * \warning There are some bad assumptions here about the size of compressed
3247 * texture tiles (multiple of 4) used to test the validity of the
3248 * offset and size parameters.
3249 * \return error code or GL_NO_ERROR.
3252 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3253 GLenum target
, GLint level
,
3254 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3255 GLsizei width
, GLsizei height
, GLsizei depth
,
3256 GLenum format
, GLsizei imageSize
)
3258 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3262 if (dimensions
== 1) {
3263 /* 1D compressed textures not allowed */
3264 return GL_INVALID_ENUM
;
3266 else if (dimensions
== 2) {
3267 if (target
== GL_PROXY_TEXTURE_2D
) {
3268 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3270 else if (target
== GL_TEXTURE_2D
) {
3271 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3273 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3274 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3275 return GL_INVALID_ENUM
; /*target*/
3276 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3278 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3279 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3280 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3281 return GL_INVALID_ENUM
; /*target*/
3282 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3285 return GL_INVALID_ENUM
; /*target*/
3288 else if (dimensions
== 3) {
3289 /* 3D compressed textures not allowed */
3290 return GL_INVALID_ENUM
;
3293 maxTextureSize
= 1 << (maxLevels
- 1);
3295 /* this will catch any invalid compressed format token */
3296 if (!_mesa_is_compressed_format(ctx
, format
))
3297 return GL_INVALID_ENUM
;
3299 if (width
< 1 || width
> maxTextureSize
)
3300 return GL_INVALID_VALUE
;
3302 if ((height
< 1 || height
> maxTextureSize
)
3304 return GL_INVALID_VALUE
;
3306 if (level
< 0 || level
>= maxLevels
)
3307 return GL_INVALID_VALUE
;
3310 * do checks which depend on compression block size
3312 get_compressed_block_size(format
, &bw
, &bh
);
3314 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3315 return GL_INVALID_VALUE
;
3317 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3318 return GL_INVALID_VALUE
;
3320 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3321 return GL_INVALID_VALUE
;
3323 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3324 if (expectedSize
!= imageSize
)
3325 return GL_INVALID_VALUE
;
3332 * Do second part of glCompressedTexSubImage error checking.
3333 * \return GL_TRUE if error found, GL_FALSE otherwise.
3336 compressed_subtexture_error_check2(struct gl_context
*ctx
, GLuint dims
,
3337 GLsizei width
, GLsizei height
,
3338 GLsizei depth
, GLenum format
,
3339 struct gl_texture_image
*texImage
)
3342 if ((GLint
) format
!= texImage
->InternalFormat
) {
3343 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3344 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3348 if (((width
== 1 || width
== 2) &&
3349 width
!= (GLsizei
) texImage
->Width
) ||
3350 (width
> (GLsizei
) texImage
->Width
)) {
3351 _mesa_error(ctx
, GL_INVALID_VALUE
,
3352 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3357 if (((height
== 1 || height
== 2) &&
3358 height
!= (GLsizei
) texImage
->Height
) ||
3359 (height
> (GLsizei
) texImage
->Height
)) {
3360 _mesa_error(ctx
, GL_INVALID_VALUE
,
3361 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3367 if (((depth
== 1 || depth
== 2) &&
3368 depth
!= (GLsizei
) texImage
->Depth
) ||
3369 (depth
> (GLsizei
) texImage
->Depth
)) {
3370 _mesa_error(ctx
, GL_INVALID_VALUE
,
3371 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3382 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3383 GLenum internalFormat
, GLsizei width
,
3384 GLint border
, GLsizei imageSize
,
3387 GET_CURRENT_CONTEXT(ctx
);
3388 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3390 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3391 _mesa_debug(ctx
, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3392 _mesa_lookup_enum_by_nr(target
), level
,
3393 _mesa_lookup_enum_by_nr(internalFormat
),
3394 width
, border
, imageSize
, data
);
3396 if (target
== GL_TEXTURE_1D
) {
3397 /* non-proxy target */
3398 struct gl_texture_object
*texObj
;
3399 struct gl_texture_image
*texImage
;
3400 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3401 internalFormat
, width
, 1, 1, border
, imageSize
);
3403 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
3407 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3409 _mesa_lock_texture(ctx
, texObj
);
3411 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3413 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
3416 if (texImage
->Data
) {
3417 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3419 ASSERT(texImage
->Data
== NULL
);
3421 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3422 border
, internalFormat
);
3424 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3425 internalFormat
, GL_NONE
, GL_NONE
);
3427 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
3428 ctx
->Driver
.CompressedTexImage1D(ctx
, target
, level
,
3429 internalFormat
, width
, border
,
3433 _mesa_set_fetch_functions(texImage
, 1);
3435 check_gen_mipmap(ctx
, target
, texObj
, level
);
3438 texObj
->_Complete
= GL_FALSE
;
3439 ctx
->NewState
|= _NEW_TEXTURE
;
3442 _mesa_unlock_texture(ctx
, texObj
);
3444 else if (target
== GL_PROXY_TEXTURE_1D
) {
3445 /* Proxy texture: check for errors and update proxy state */
3446 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3447 internalFormat
, width
, 1, 1, border
, imageSize
);
3449 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3450 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3451 internalFormat
, GL_NONE
, GL_NONE
,
3452 width
, 1, 1, border
);
3455 /* if error, clear all proxy texture image parameters */
3456 struct gl_texture_image
*texImage
;
3457 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3459 clear_teximage_fields(texImage
);
3462 /* store the teximage parameters */
3463 struct gl_texture_object
*texObj
;
3464 struct gl_texture_image
*texImage
;
3466 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3468 _mesa_lock_texture(ctx
, texObj
);
3470 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3471 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3472 border
, internalFormat
);
3473 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3474 internalFormat
, GL_NONE
, GL_NONE
);
3476 _mesa_unlock_texture(ctx
, texObj
);
3480 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
3486 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3487 GLenum internalFormat
, GLsizei width
,
3488 GLsizei height
, GLint border
, GLsizei imageSize
,
3491 GET_CURRENT_CONTEXT(ctx
);
3492 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3494 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3495 _mesa_debug(ctx
, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3496 _mesa_lookup_enum_by_nr(target
), level
,
3497 _mesa_lookup_enum_by_nr(internalFormat
),
3498 width
, height
, border
, imageSize
, data
);
3501 switch (internalFormat
) {
3502 case GL_PALETTE4_RGB8_OES
:
3503 case GL_PALETTE4_RGBA8_OES
:
3504 case GL_PALETTE4_R5_G6_B5_OES
:
3505 case GL_PALETTE4_RGBA4_OES
:
3506 case GL_PALETTE4_RGB5_A1_OES
:
3507 case GL_PALETTE8_RGB8_OES
:
3508 case GL_PALETTE8_RGBA8_OES
:
3509 case GL_PALETTE8_R5_G6_B5_OES
:
3510 case GL_PALETTE8_RGBA4_OES
:
3511 case GL_PALETTE8_RGB5_A1_OES
:
3512 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3513 width
, height
, imageSize
, data
);
3518 if (target
== GL_TEXTURE_2D
||
3519 (ctx
->Extensions
.ARB_texture_cube_map
&&
3520 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3521 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
3522 /* non-proxy target */
3523 struct gl_texture_object
*texObj
;
3524 struct gl_texture_image
*texImage
;
3526 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3527 internalFormat
, width
, height
, 1, border
, imageSize
);
3529 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
3533 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3535 _mesa_lock_texture(ctx
, texObj
);
3537 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3539 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
3542 if (texImage
->Data
) {
3543 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3545 ASSERT(texImage
->Data
== NULL
);
3547 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3548 border
, internalFormat
);
3550 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3551 internalFormat
, GL_NONE
, GL_NONE
);
3553 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3554 ctx
->Driver
.CompressedTexImage2D(ctx
, target
, level
,
3555 internalFormat
, width
, height
,
3556 border
, imageSize
, data
,
3559 _mesa_set_fetch_functions(texImage
, 2);
3561 check_gen_mipmap(ctx
, target
, texObj
, level
);
3564 texObj
->_Complete
= GL_FALSE
;
3565 ctx
->NewState
|= _NEW_TEXTURE
;
3568 _mesa_unlock_texture(ctx
, texObj
);
3570 else if (target
== GL_PROXY_TEXTURE_2D
||
3571 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
3572 ctx
->Extensions
.ARB_texture_cube_map
)) {
3573 /* Proxy texture: check for errors and update proxy state */
3574 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3575 internalFormat
, width
, height
, 1, border
, imageSize
);
3577 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3578 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3579 internalFormat
, GL_NONE
, GL_NONE
,
3580 width
, height
, 1, border
);
3583 /* if error, clear all proxy texture image parameters */
3584 struct gl_texture_image
*texImage
;
3585 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3587 clear_teximage_fields(texImage
);
3590 /* store the teximage parameters */
3591 struct gl_texture_object
*texObj
;
3592 struct gl_texture_image
*texImage
;
3594 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3596 _mesa_lock_texture(ctx
, texObj
);
3598 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3599 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3600 border
, internalFormat
);
3601 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3602 internalFormat
, GL_NONE
, GL_NONE
);
3604 _mesa_unlock_texture(ctx
, texObj
);
3608 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3615 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3616 GLenum internalFormat
, GLsizei width
,
3617 GLsizei height
, GLsizei depth
, GLint border
,
3618 GLsizei imageSize
, const GLvoid
*data
)
3620 GET_CURRENT_CONTEXT(ctx
);
3621 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3623 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3624 _mesa_debug(ctx
, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3625 _mesa_lookup_enum_by_nr(target
), level
,
3626 _mesa_lookup_enum_by_nr(internalFormat
),
3627 width
, height
, depth
, border
, imageSize
, data
);
3629 if (target
== GL_TEXTURE_3D
) {
3630 /* non-proxy target */
3631 struct gl_texture_object
*texObj
;
3632 struct gl_texture_image
*texImage
;
3633 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3634 internalFormat
, width
, height
, depth
, border
, imageSize
);
3636 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3640 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3642 _mesa_lock_texture(ctx
, texObj
);
3644 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3646 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3649 if (texImage
->Data
) {
3650 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3652 ASSERT(texImage
->Data
== NULL
);
3654 _mesa_init_teximage_fields(ctx
, target
, texImage
,
3655 width
, height
, depth
,
3656 border
, internalFormat
);
3658 /* Choose actual texture format */
3659 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3660 internalFormat
, GL_NONE
, GL_NONE
);
3662 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3663 ctx
->Driver
.CompressedTexImage3D(ctx
, target
, level
,
3665 width
, height
, depth
,
3666 border
, imageSize
, data
,
3669 _mesa_set_fetch_functions(texImage
, 3);
3671 check_gen_mipmap(ctx
, target
, texObj
, level
);
3674 texObj
->_Complete
= GL_FALSE
;
3675 ctx
->NewState
|= _NEW_TEXTURE
;
3678 _mesa_unlock_texture(ctx
, texObj
);
3680 else if (target
== GL_PROXY_TEXTURE_3D
) {
3681 /* Proxy texture: check for errors and update proxy state */
3682 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3683 internalFormat
, width
, height
, depth
, border
, imageSize
);
3685 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3686 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3687 internalFormat
, GL_NONE
, GL_NONE
,
3688 width
, height
, depth
, border
);
3691 /* if error, clear all proxy texture image parameters */
3692 struct gl_texture_image
*texImage
;
3693 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3695 clear_teximage_fields(texImage
);
3698 /* store the teximage parameters */
3699 struct gl_texture_object
*texObj
;
3700 struct gl_texture_image
*texImage
;
3702 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3704 _mesa_lock_texture(ctx
, texObj
);
3706 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3707 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3708 depth
, border
, internalFormat
);
3709 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3710 internalFormat
, GL_NONE
, GL_NONE
);
3712 _mesa_unlock_texture(ctx
, texObj
);
3716 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3723 * Common helper for glCompressedTexSubImage1/2/3D().
3726 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3727 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3728 GLsizei width
, GLsizei height
, GLsizei depth
,
3729 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3731 struct gl_texture_object
*texObj
;
3732 struct gl_texture_image
*texImage
;
3734 GET_CURRENT_CONTEXT(ctx
);
3735 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3737 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3738 xoffset
, 0, 0, /* pos */
3739 width
, height
, depth
, /* size */
3742 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3746 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3748 _mesa_lock_texture(ctx
, texObj
);
3750 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3753 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3754 format
, texImage
)) {
3755 /* error was recorded */
3757 else if (width
> 0 && height
> 0 && depth
> 0) {
3760 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3761 ctx
->Driver
.CompressedTexSubImage1D(ctx
, target
, level
,
3763 format
, imageSize
, data
,
3768 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3769 ctx
->Driver
.CompressedTexSubImage2D(ctx
, target
, level
,
3772 format
, imageSize
, data
,
3777 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3778 ctx
->Driver
.CompressedTexSubImage3D(ctx
, target
, level
,
3779 xoffset
, yoffset
, zoffset
,
3780 width
, height
, depth
,
3781 format
, imageSize
, data
,
3789 check_gen_mipmap(ctx
, target
, texObj
, level
);
3791 ctx
->NewState
|= _NEW_TEXTURE
;
3794 _mesa_unlock_texture(ctx
, texObj
);
3799 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3800 GLsizei width
, GLenum format
,
3801 GLsizei imageSize
, const GLvoid
*data
)
3803 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3804 format
, imageSize
, data
);
3809 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3810 GLint yoffset
, GLsizei width
, GLsizei height
,
3811 GLenum format
, GLsizei imageSize
,
3814 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3815 width
, height
, 1, format
, imageSize
, data
);
3820 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3821 GLint yoffset
, GLint zoffset
, GLsizei width
,
3822 GLsizei height
, GLsizei depth
, GLenum format
,
3823 GLsizei imageSize
, const GLvoid
*data
)
3825 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3826 width
, height
, depth
, format
, imageSize
, data
);