2 * mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Texture image-related functions.
34 #include "bufferobj.h"
38 #include "framebuffer.h"
44 #include "texcompress.h"
53 * State changes which we care about for glCopyTex[Sub]Image() calls.
54 * In particular, we care about pixel transfer state and buffer state
55 * (such as glReadBuffer to make sure we read from the right renderbuffer).
57 #define NEW_COPY_TEX_STATE (_MESA_NEW_TRANSFER_STATE | \
64 * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
68 _mesa_alloc_texmemory(GLsizei bytes
)
70 return _mesa_align_malloc(bytes
, 512);
75 * Free texture memory allocated with _mesa_alloc_texmemory()
78 _mesa_free_texmemory(void *m
)
85 * Compute floor(log_base_2(n)).
115 * Return the simple base format for a given internal texture format.
116 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
118 * \param ctx GL context.
119 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
121 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
122 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
124 * This is the format which is used during texture application (i.e. the
125 * texture format and env mode determine the arithmetic used.
127 * XXX this could be static
130 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
132 switch (internalFormat
) {
147 case GL_LUMINANCE_ALPHA
:
148 case GL_LUMINANCE4_ALPHA4
:
149 case GL_LUMINANCE6_ALPHA2
:
150 case GL_LUMINANCE8_ALPHA8
:
151 case GL_LUMINANCE12_ALPHA4
:
152 case GL_LUMINANCE12_ALPHA12
:
153 case GL_LUMINANCE16_ALPHA16
:
154 return GL_LUMINANCE_ALPHA
;
185 if (ctx
->Extensions
.EXT_paletted_texture
) {
186 switch (internalFormat
) {
188 case GL_COLOR_INDEX1_EXT
:
189 case GL_COLOR_INDEX2_EXT
:
190 case GL_COLOR_INDEX4_EXT
:
191 case GL_COLOR_INDEX8_EXT
:
192 case GL_COLOR_INDEX12_EXT
:
193 case GL_COLOR_INDEX16_EXT
:
194 return GL_COLOR_INDEX
;
200 if (ctx
->Extensions
.ARB_depth_texture
) {
201 switch (internalFormat
) {
202 case GL_DEPTH_COMPONENT
:
203 case GL_DEPTH_COMPONENT16
:
204 case GL_DEPTH_COMPONENT24
:
205 case GL_DEPTH_COMPONENT32
:
206 return GL_DEPTH_COMPONENT
;
212 switch (internalFormat
) {
213 case GL_COMPRESSED_ALPHA
:
215 case GL_COMPRESSED_LUMINANCE
:
217 case GL_COMPRESSED_LUMINANCE_ALPHA
:
218 return GL_LUMINANCE_ALPHA
;
219 case GL_COMPRESSED_INTENSITY
:
221 case GL_COMPRESSED_RGB
:
223 case GL_COMPRESSED_RGBA
:
229 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
230 switch (internalFormat
) {
231 case GL_COMPRESSED_RGB_FXT1_3DFX
:
233 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
240 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
241 switch (internalFormat
) {
242 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
244 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
245 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
246 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
253 if (ctx
->Extensions
.S3_s3tc
) {
254 switch (internalFormat
) {
266 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
267 if (internalFormat
== GL_YCBCR_MESA
)
268 return GL_YCBCR_MESA
;
271 if (ctx
->Extensions
.ARB_texture_float
) {
272 switch (internalFormat
) {
273 case GL_ALPHA16F_ARB
:
274 case GL_ALPHA32F_ARB
:
282 case GL_INTENSITY16F_ARB
:
283 case GL_INTENSITY32F_ARB
:
285 case GL_LUMINANCE16F_ARB
:
286 case GL_LUMINANCE32F_ARB
:
288 case GL_LUMINANCE_ALPHA16F_ARB
:
289 case GL_LUMINANCE_ALPHA32F_ARB
:
290 return GL_LUMINANCE_ALPHA
;
296 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
297 switch (internalFormat
) {
306 if (ctx
->Extensions
.MESA_texture_signed_rgba
) {
307 switch (internalFormat
) {
316 if (ctx
->Extensions
.EXT_packed_depth_stencil
) {
317 switch (internalFormat
) {
318 case GL_DEPTH_STENCIL_EXT
:
319 case GL_DEPTH24_STENCIL8_EXT
:
320 return GL_DEPTH_STENCIL_EXT
;
326 #if FEATURE_EXT_texture_sRGB
327 if (ctx
->Extensions
.EXT_texture_sRGB
) {
328 switch (internalFormat
) {
331 case GL_COMPRESSED_SRGB_EXT
:
332 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
334 case GL_SRGB_ALPHA_EXT
:
335 case GL_SRGB8_ALPHA8_EXT
:
336 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
337 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
338 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
339 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
341 case GL_SLUMINANCE_ALPHA_EXT
:
342 case GL_SLUMINANCE8_ALPHA8_EXT
:
343 case GL_COMPRESSED_SLUMINANCE_EXT
:
344 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
345 return GL_LUMINANCE_ALPHA
;
346 case GL_SLUMINANCE_EXT
:
347 case GL_SLUMINANCE8_EXT
:
353 #endif /* FEATURE_EXT_texture_sRGB */
355 if (ctx
->Extensions
.EXT_texture_integer
) {
356 switch (internalFormat
) {
358 case GL_RGBA16UI_EXT
:
359 case GL_RGBA32UI_EXT
:
371 case GL_ALPHA8UI_EXT
:
372 case GL_ALPHA16UI_EXT
:
373 case GL_ALPHA32UI_EXT
:
375 case GL_ALPHA16I_EXT
:
376 case GL_ALPHA32I_EXT
:
378 case GL_INTENSITY8UI_EXT
:
379 case GL_INTENSITY16UI_EXT
:
380 case GL_INTENSITY32UI_EXT
:
381 case GL_INTENSITY8I_EXT
:
382 case GL_INTENSITY16I_EXT
:
383 case GL_INTENSITY32I_EXT
:
385 case GL_LUMINANCE8UI_EXT
:
386 case GL_LUMINANCE16UI_EXT
:
387 case GL_LUMINANCE32UI_EXT
:
388 case GL_LUMINANCE8I_EXT
:
389 case GL_LUMINANCE16I_EXT
:
390 case GL_LUMINANCE32I_EXT
:
392 case GL_LUMINANCE_ALPHA8UI_EXT
:
393 case GL_LUMINANCE_ALPHA16UI_EXT
:
394 case GL_LUMINANCE_ALPHA32UI_EXT
:
395 case GL_LUMINANCE_ALPHA8I_EXT
:
396 case GL_LUMINANCE_ALPHA16I_EXT
:
397 case GL_LUMINANCE_ALPHA32I_EXT
:
398 return GL_LUMINANCE_ALPHA
;
404 if (ctx
->Extensions
.ARB_texture_rg
) {
405 switch (internalFormat
) {
407 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
409 if (!ctx
->Extensions
.ARB_half_float_pixel
)
413 if (!ctx
->Extensions
.ARB_texture_float
)
422 if (!ctx
->Extensions
.EXT_texture_integer
)
428 case GL_COMPRESSED_RED
:
432 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
434 if (!ctx
->Extensions
.ARB_half_float_pixel
)
438 if (!ctx
->Extensions
.ARB_texture_float
)
447 if (!ctx
->Extensions
.EXT_texture_integer
)
453 case GL_COMPRESSED_RG
:
460 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
461 switch (internalFormat
) {
469 if (ctx
->Extensions
.EXT_packed_float
) {
470 switch (internalFormat
) {
471 case GL_R11F_G11F_B10F_EXT
:
478 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
479 switch (internalFormat
) {
480 case GL_DEPTH_COMPONENT32F
:
481 return GL_DEPTH_COMPONENT
;
482 case GL_DEPTH32F_STENCIL8
:
483 return GL_DEPTH_STENCIL
;
489 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
490 switch (internalFormat
) {
491 case GL_COMPRESSED_RED_RGTC1
:
492 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
494 case GL_COMPRESSED_RG_RGTC2
:
495 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
502 return -1; /* error */
507 * For cube map faces, return a face index in [0,5].
508 * For other targets return 0;
511 _mesa_tex_target_to_face(GLenum target
)
513 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
514 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)
515 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
523 * Store a gl_texture_image pointer in a gl_texture_object structure
524 * according to the target and level parameters.
526 * \param tObj texture object.
527 * \param target texture target.
528 * \param level image level.
529 * \param texImage texture image.
531 * This was basically prompted by the introduction of cube maps.
534 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
535 GLenum target
, GLint level
,
536 struct gl_texture_image
*texImage
)
538 const GLuint face
= _mesa_tex_target_to_face(target
);
542 ASSERT(target
!= GL_TEXTURE_RECTANGLE_NV
|| level
== 0);
544 tObj
->Image
[face
][level
] = texImage
;
546 /* Set the 'back' pointer */
547 texImage
->TexObject
= tObj
;
552 * Allocate a texture image structure.
554 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
557 * \return a pointer to gl_texture_image struct with all fields initialized to
560 struct gl_texture_image
*
561 _mesa_new_texture_image( struct gl_context
*ctx
)
564 return CALLOC_STRUCT(gl_texture_image
);
569 * Free texture image data.
570 * This function is a fallback called via ctx->Driver.FreeTexImageData().
572 * \param texImage texture image.
574 * Free the texture image data if it's not marked as client data.
577 _mesa_free_texture_image_data(struct gl_context
*ctx
,
578 struct gl_texture_image
*texImage
)
582 if (texImage
->Data
&& !texImage
->IsClientData
) {
583 /* free the old texture data */
584 _mesa_free_texmemory(texImage
->Data
);
587 texImage
->Data
= NULL
;
592 * Free texture image.
594 * \param texImage texture image.
596 * Free the texture image structure and the associated image data.
599 _mesa_delete_texture_image( struct gl_context
*ctx
, struct gl_texture_image
*texImage
)
601 /* Free texImage->Data and/or any other driver-specific texture
604 ASSERT(ctx
->Driver
.FreeTexImageData
);
605 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
607 ASSERT(texImage
->Data
== NULL
);
608 if (texImage
->ImageOffsets
)
609 free(texImage
->ImageOffsets
);
615 * Test if a target is a proxy target.
617 * \param target texture target.
619 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
622 _mesa_is_proxy_texture(GLenum target
)
624 /* NUM_TEXTURE_TARGETS should match number of terms below */
625 assert(NUM_TEXTURE_TARGETS
== 7);
627 return (target
== GL_PROXY_TEXTURE_1D
||
628 target
== GL_PROXY_TEXTURE_2D
||
629 target
== GL_PROXY_TEXTURE_3D
||
630 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
631 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
632 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
633 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
);
638 * Get the texture object that corresponds to the target of the given texture unit.
640 * \param ctx GL context.
641 * \param texUnit texture unit.
642 * \param target texture target.
644 * \return pointer to the texture object on success, or NULL on failure.
646 * \sa gl_texture_unit.
648 struct gl_texture_object
*
649 _mesa_select_tex_object(struct gl_context
*ctx
, const struct gl_texture_unit
*texUnit
,
654 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
655 case GL_PROXY_TEXTURE_1D
:
656 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
658 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
659 case GL_PROXY_TEXTURE_2D
:
660 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
662 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
663 case GL_PROXY_TEXTURE_3D
:
664 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
665 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
666 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
667 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
668 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
669 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
670 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
671 case GL_TEXTURE_CUBE_MAP_ARB
:
672 return ctx
->Extensions
.ARB_texture_cube_map
673 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
674 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
675 return ctx
->Extensions
.ARB_texture_cube_map
676 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
677 case GL_TEXTURE_RECTANGLE_NV
:
678 return ctx
->Extensions
.NV_texture_rectangle
679 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
680 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
681 return ctx
->Extensions
.NV_texture_rectangle
682 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
683 case GL_TEXTURE_1D_ARRAY_EXT
:
684 return ctx
->Extensions
.MESA_texture_array
685 ? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
686 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
687 return ctx
->Extensions
.MESA_texture_array
688 ? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
689 case GL_TEXTURE_2D_ARRAY_EXT
:
690 return ctx
->Extensions
.MESA_texture_array
691 ? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
692 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
693 return ctx
->Extensions
.MESA_texture_array
694 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
696 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
703 * Return pointer to texture object for given target on current texture unit.
705 struct gl_texture_object
*
706 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
708 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
709 return _mesa_select_tex_object(ctx
, texUnit
, target
);
714 * Get a texture image pointer from a texture object, given a texture
715 * target and mipmap level. The target and level parameters should
716 * have already been error-checked.
718 * \param ctx GL context.
719 * \param texObj texture unit.
720 * \param target texture target.
721 * \param level image level.
723 * \return pointer to the texture image structure, or NULL on failure.
725 struct gl_texture_image
*
726 _mesa_select_tex_image(struct gl_context
*ctx
, const struct gl_texture_object
*texObj
,
727 GLenum target
, GLint level
)
729 const GLuint face
= _mesa_tex_target_to_face(target
);
733 ASSERT(level
< MAX_TEXTURE_LEVELS
);
735 return texObj
->Image
[face
][level
];
740 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
741 * it and install it. Only return NULL if passed a bad parameter or run
744 struct gl_texture_image
*
745 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
746 GLenum target
, GLint level
)
748 struct gl_texture_image
*texImage
;
753 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
755 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
757 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
761 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
769 * Return pointer to the specified proxy texture image.
770 * Note that proxy textures are per-context, not per-texture unit.
771 * \return pointer to texture image or NULL if invalid target, invalid
772 * level, or out of memory.
774 struct gl_texture_image
*
775 _mesa_get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
777 struct gl_texture_image
*texImage
;
784 case GL_PROXY_TEXTURE_1D
:
785 if (level
>= ctx
->Const
.MaxTextureLevels
)
787 texIndex
= TEXTURE_1D_INDEX
;
789 case GL_PROXY_TEXTURE_2D
:
790 if (level
>= ctx
->Const
.MaxTextureLevels
)
792 texIndex
= TEXTURE_2D_INDEX
;
794 case GL_PROXY_TEXTURE_3D
:
795 if (level
>= ctx
->Const
.Max3DTextureLevels
)
797 texIndex
= TEXTURE_3D_INDEX
;
799 case GL_PROXY_TEXTURE_CUBE_MAP
:
800 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
802 texIndex
= TEXTURE_CUBE_INDEX
;
804 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
807 texIndex
= TEXTURE_RECT_INDEX
;
809 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
810 if (level
>= ctx
->Const
.MaxTextureLevels
)
812 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
814 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
815 if (level
>= ctx
->Const
.MaxTextureLevels
)
817 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
823 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
825 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
827 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
830 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
831 /* Set the 'back' pointer */
832 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
839 * Get the maximum number of allowed mipmap levels.
841 * \param ctx GL context.
842 * \param target texture target.
844 * \return the maximum number of allowed mipmap levels for the given
845 * texture target, or zero if passed a bad target.
850 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
854 case GL_PROXY_TEXTURE_1D
:
856 case GL_PROXY_TEXTURE_2D
:
857 return ctx
->Const
.MaxTextureLevels
;
859 case GL_PROXY_TEXTURE_3D
:
860 return ctx
->Const
.Max3DTextureLevels
;
861 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
862 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
863 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
864 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
865 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
866 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
867 case GL_TEXTURE_CUBE_MAP_ARB
:
868 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
869 return ctx
->Extensions
.ARB_texture_cube_map
870 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
871 case GL_TEXTURE_RECTANGLE_NV
:
872 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
873 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
874 case GL_TEXTURE_1D_ARRAY_EXT
:
875 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
876 case GL_TEXTURE_2D_ARRAY_EXT
:
877 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
878 return ctx
->Extensions
.MESA_texture_array
879 ? ctx
->Const
.MaxTextureLevels
: 0;
881 return 0; /* bad target */
887 #if 000 /* not used anymore */
889 * glTexImage[123]D can accept a NULL image pointer. In this case we
890 * create a texture image with unspecified image contents per the OpenGL
894 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
896 const GLint components
= _mesa_components_in_format(format
);
897 const GLint numPixels
= width
* height
* depth
;
898 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
902 * Let's see if anyone finds this. If glTexImage2D() is called with
903 * a NULL image pointer then load the texture image with something
904 * interesting instead of leaving it indeterminate.
907 static const char message
[8][32] = {
911 " X X XXXX XXX XXXXX ",
914 " X X XXXXX XXX X X ",
918 GLubyte
*imgPtr
= data
;
920 for (h
= 0; h
< depth
; h
++) {
921 for (i
= 0; i
< height
; i
++) {
922 GLint srcRow
= 7 - (i
% 8);
923 for (j
= 0; j
< width
; j
++) {
924 GLint srcCol
= j
% 32;
925 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
926 for (k
= 0; k
< components
; k
++) {
942 * Reset the fields of a gl_texture_image struct to zero.
944 * \param img texture image structure.
946 * This is called when a proxy texture test fails, we set all the
947 * image members (except DriverData) to zero.
948 * It's also used in glTexImage[123]D as a safeguard to be sure all
949 * required fields get initialized properly by the Driver.TexImage[123]D
953 clear_teximage_fields(struct gl_texture_image
*img
)
956 img
->_BaseFormat
= 0;
957 img
->InternalFormat
= 0;
963 if (img
->ImageOffsets
) {
964 free(img
->ImageOffsets
);
965 img
->ImageOffsets
= NULL
;
974 img
->TexFormat
= MESA_FORMAT_NONE
;
975 img
->FetchTexelc
= NULL
;
976 img
->FetchTexelf
= NULL
;
981 * Initialize basic fields of the gl_texture_image struct.
983 * \param ctx GL context.
984 * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
985 * \param img texture image structure to be initialized.
986 * \param width image width.
987 * \param height image height.
988 * \param depth image depth.
989 * \param border image border.
990 * \param internalFormat internal format.
992 * Fills in the fields of \p img with the given information.
993 * Note: width, height and depth include the border.
996 _mesa_init_teximage_fields(struct gl_context
*ctx
, GLenum target
,
997 struct gl_texture_image
*img
,
998 GLsizei width
, GLsizei height
, GLsizei depth
,
999 GLint border
, GLenum internalFormat
)
1005 ASSERT(height
>= 0);
1008 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1009 ASSERT(img
->_BaseFormat
> 0);
1010 img
->InternalFormat
= internalFormat
;
1011 img
->Border
= border
;
1013 img
->Height
= height
;
1016 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1017 img
->WidthLog2
= logbase2(img
->Width2
);
1019 if (height
== 1) { /* 1-D texture */
1021 img
->HeightLog2
= 0;
1024 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1025 img
->HeightLog2
= logbase2(img
->Height2
);
1028 if (depth
== 1) { /* 2-D texture */
1033 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1034 img
->DepthLog2
= logbase2(img
->Depth2
);
1037 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1039 if ((width
== 1 || _mesa_is_pow_two(img
->Width2
)) &&
1040 (height
== 1 || _mesa_is_pow_two(img
->Height2
)) &&
1041 (depth
== 1 || _mesa_is_pow_two(img
->Depth2
)))
1042 img
->_IsPowerOfTwo
= GL_TRUE
;
1044 img
->_IsPowerOfTwo
= GL_FALSE
;
1046 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
1047 img
->RowStride
= width
;
1048 /* Allocate the ImageOffsets array and initialize to typical values.
1049 * We allocate the array for 1D/2D textures too in order to avoid special-
1050 * case code in the texstore routines.
1052 if (img
->ImageOffsets
)
1053 free(img
->ImageOffsets
);
1054 img
->ImageOffsets
= (GLuint
*) malloc(depth
* sizeof(GLuint
));
1055 for (i
= 0; i
< depth
; i
++) {
1056 img
->ImageOffsets
[i
] = i
* width
* height
;
1059 /* Compute Width/Height/DepthScale for mipmap lod computation */
1060 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1061 /* scale = 1.0 since texture coords directly map to texels */
1062 img
->WidthScale
= 1.0;
1063 img
->HeightScale
= 1.0;
1064 img
->DepthScale
= 1.0;
1067 img
->WidthScale
= (GLfloat
) img
->Width
;
1068 img
->HeightScale
= (GLfloat
) img
->Height
;
1069 img
->DepthScale
= (GLfloat
) img
->Depth
;
1072 img
->FetchTexelc
= NULL
;
1073 img
->FetchTexelf
= NULL
;
1078 * Free and clear fields of the gl_texture_image struct.
1080 * \param ctx GL context.
1081 * \param texImage texture image structure to be cleared.
1083 * After the call, \p texImage will have no data associated with it. Its
1084 * fields are cleared so that its parent object will test incomplete.
1087 _mesa_clear_texture_image(struct gl_context
*ctx
, struct gl_texture_image
*texImage
)
1089 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
1090 clear_teximage_fields(texImage
);
1095 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1096 * level, width, height and depth against the ctx->Const limits for textures.
1098 * A hardware driver might override this function if, for example, the
1099 * max 3D texture size is 512x512x64 (i.e. not a cube).
1101 * Note that width, height, depth == 0 is not an error. However, a
1102 * texture with zero width/height/depth will be considered "incomplete"
1103 * and texturing will effectively be disabled.
1105 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1106 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1107 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1108 * \param level as passed to glTexImage
1109 * \param internalFormat as passed to glTexImage
1110 * \param format as passed to glTexImage
1111 * \param type as passed to glTexImage
1112 * \param width as passed to glTexImage
1113 * \param height as passed to glTexImage
1114 * \param depth as passed to glTexImage
1115 * \param border as passed to glTexImage
1116 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1119 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1120 GLint internalFormat
, GLenum format
, GLenum type
,
1121 GLint width
, GLint height
, GLint depth
, GLint border
)
1125 (void) internalFormat
;
1130 case GL_PROXY_TEXTURE_1D
:
1131 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1132 if (width
< 2 * border
|| width
> 2 + maxSize
||
1133 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1134 width
>0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1135 level
>= ctx
->Const
.MaxTextureLevels
) {
1136 /* bad width or level */
1140 case GL_PROXY_TEXTURE_2D
:
1141 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1142 if (width
< 2 * border
|| width
> 2 + maxSize
||
1143 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1144 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1145 height
< 2 * border
|| height
> 2 + maxSize
||
1146 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1147 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1148 level
>= ctx
->Const
.MaxTextureLevels
) {
1149 /* bad width or height or level */
1153 case GL_PROXY_TEXTURE_3D
:
1154 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1155 if (width
< 2 * border
|| width
> 2 + maxSize
||
1156 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1157 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1158 height
< 2 * border
|| height
> 2 + maxSize
||
1159 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1160 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1161 depth
< 2 * border
|| depth
> 2 + maxSize
||
1162 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1163 depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
)) ||
1164 level
>= ctx
->Const
.Max3DTextureLevels
) {
1165 /* bad width or height or depth or level */
1169 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1170 if (width
< 0 || width
> ctx
->Const
.MaxTextureRectSize
||
1171 height
< 0 || height
> ctx
->Const
.MaxTextureRectSize
||
1173 /* bad width or height or level */
1177 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1178 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1179 if (width
< 2 * border
|| width
> 2 + maxSize
||
1180 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1181 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1182 height
< 2 * border
|| height
> 2 + maxSize
||
1183 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1184 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1185 level
>= ctx
->Const
.MaxCubeTextureLevels
) {
1186 /* bad width or height */
1190 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1191 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1192 if (width
< 2 * border
|| width
> 2 + maxSize
||
1193 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1194 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1195 level
>= ctx
->Const
.MaxTextureLevels
) {
1196 /* bad width or level */
1200 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
) {
1204 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1205 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1206 if (width
< 2 * border
|| width
> 2 + maxSize
||
1207 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1208 width
> 0 && !_mesa_is_pow_two(width
- 2 * border
)) ||
1209 height
< 2 * border
|| height
> 2 + maxSize
||
1210 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1211 height
> 0 && !_mesa_is_pow_two(height
- 2 * border
)) ||
1212 level
>= ctx
->Const
.MaxTextureLevels
) {
1213 /* bad width or height or level */
1216 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
) {
1221 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1228 * Helper function to determine whether a target supports compressed textures
1231 target_can_be_compressed(struct gl_context
*ctx
, GLenum target
)
1235 case GL_PROXY_TEXTURE_2D
:
1237 case GL_PROXY_TEXTURE_CUBE_MAP
:
1238 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1239 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1240 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1241 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1242 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1243 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1244 return ctx
->Extensions
.ARB_texture_cube_map
;
1245 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1246 case GL_TEXTURE_2D_ARRAY_EXT
:
1247 return ctx
->Extensions
.MESA_texture_array
;
1255 * Test the glTexImage[123]D() parameters for errors.
1257 * \param ctx GL context.
1258 * \param target texture target given by the user.
1259 * \param level image level given by the user.
1260 * \param internalFormat internal format given by the user.
1261 * \param format pixel data format given by the user.
1262 * \param type pixel data type given by the user.
1263 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1264 * \param width image width given by the user.
1265 * \param height image height given by the user.
1266 * \param depth image depth given by the user.
1267 * \param border image border given by the user.
1269 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1271 * Verifies each of the parameters against the constants specified in
1272 * __struct gl_contextRec::Const and the supported extensions, and according to the
1273 * OpenGL specification.
1276 texture_error_check( struct gl_context
*ctx
, GLenum target
,
1277 GLint level
, GLint internalFormat
,
1278 GLenum format
, GLenum type
,
1280 GLint width
, GLint height
,
1281 GLint depth
, GLint border
)
1283 const GLboolean isProxy
= _mesa_is_proxy_texture(target
);
1284 GLboolean sizeOK
= GL_TRUE
;
1285 GLboolean colorFormat
, indexFormat
;
1286 GLenum proxy_target
;
1288 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1289 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1291 _mesa_error(ctx
, GL_INVALID_VALUE
,
1292 "glTexImage%dD(level=%d)", dimensions
, level
);
1298 if (border
< 0 || border
> 1 ||
1299 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1300 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1302 _mesa_error(ctx
, GL_INVALID_VALUE
,
1303 "glTexImage%dD(border=%d)", dimensions
, border
);
1308 if (width
< 0 || height
< 0 || depth
< 0) {
1310 _mesa_error(ctx
, GL_INVALID_VALUE
,
1311 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1316 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1317 * level, width, height and depth.
1319 if (dimensions
== 1) {
1320 if (target
== GL_PROXY_TEXTURE_1D
|| target
== GL_TEXTURE_1D
) {
1321 proxy_target
= GL_PROXY_TEXTURE_1D
;
1326 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
1330 else if (dimensions
== 2) {
1332 if (target
== GL_PROXY_TEXTURE_2D
|| target
== GL_TEXTURE_2D
) {
1333 proxy_target
= GL_PROXY_TEXTURE_2D
;
1335 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1336 (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1337 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
1338 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1339 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1342 proxy_target
= GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
1343 sizeOK
= (width
== height
);
1345 else if (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
1346 target
== GL_TEXTURE_RECTANGLE_NV
) {
1347 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1348 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1351 proxy_target
= GL_PROXY_TEXTURE_RECTANGLE_NV
;
1353 else if (target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
1354 target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1355 proxy_target
= GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
1358 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)");
1362 else if (dimensions
== 3) {
1363 if (target
== GL_PROXY_TEXTURE_3D
|| target
== GL_TEXTURE_3D
) {
1364 proxy_target
= GL_PROXY_TEXTURE_3D
;
1366 else if (target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
||
1367 target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1368 proxy_target
= GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
1371 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
1376 _mesa_problem( ctx
, "bad dims in texture_error_check" );
1380 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target
, level
,
1381 internalFormat
, format
,
1382 type
, width
, height
,
1386 _mesa_error(ctx
, GL_INVALID_VALUE
,
1387 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1388 dimensions
, level
, width
, height
, depth
);
1393 /* Check internalFormat */
1394 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1396 _mesa_error(ctx
, GL_INVALID_VALUE
,
1397 "glTexImage%dD(internalFormat=%s)",
1398 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1403 /* Check incoming image format and type */
1404 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1405 /* Normally, GL_INVALID_OPERATION is generated by a format/type
1406 * mismatch (see the 1.2 spec page 94, sec 3.6.4.). But with the
1407 * GL_EXT_texture_integer extension, some combinations should generate
1408 * GL_INVALID_ENUM instead (grr!).
1411 GLenum error
= _mesa_is_integer_format(format
)
1412 ? GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
1413 _mesa_error(ctx
, error
,
1414 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1415 dimensions
, format
, type
);
1420 /* make sure internal format and format basically agree */
1421 colorFormat
= _mesa_is_color_format(format
);
1422 indexFormat
= _mesa_is_index_format(format
);
1423 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1424 (_mesa_is_index_format(internalFormat
) && !indexFormat
) ||
1425 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1426 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1427 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1428 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1430 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1431 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1432 dimensions
, internalFormat
, format
);
1436 /* additional checks for ycbcr textures */
1437 if (internalFormat
== GL_YCBCR_MESA
) {
1438 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1439 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1440 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1442 _mesa_snprintf(message
, sizeof(message
),
1443 "glTexImage%dD(format/type YCBCR mismatch", dimensions
);
1444 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1445 return GL_TRUE
; /* error */
1447 if (target
!= GL_TEXTURE_2D
&&
1448 target
!= GL_PROXY_TEXTURE_2D
&&
1449 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1450 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1452 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1458 _mesa_snprintf(message
, sizeof(message
),
1459 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1460 dimensions
, border
);
1461 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1467 /* additional checks for depth textures */
1468 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1469 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1470 if (target
!= GL_TEXTURE_1D
&&
1471 target
!= GL_PROXY_TEXTURE_1D
&&
1472 target
!= GL_TEXTURE_2D
&&
1473 target
!= GL_PROXY_TEXTURE_2D
&&
1474 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1475 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
) {
1477 _mesa_error(ctx
, GL_INVALID_ENUM
,
1478 "glTexImage(target/internalFormat)");
1483 /* additional checks for compressed textures */
1484 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1485 if (!target_can_be_compressed(ctx
, target
) && !isProxy
) {
1486 _mesa_error(ctx
, GL_INVALID_ENUM
,
1487 "glTexImage%dD(target)", dimensions
);
1492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1493 "glTexImage%dD(border!=0)", dimensions
);
1499 /* additional checks for integer textures */
1500 if (ctx
->Extensions
.EXT_texture_integer
&&
1501 (_mesa_is_integer_format(format
) !=
1502 _mesa_is_integer_format(internalFormat
))) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glTexImage%dD(integer/non-integer format mismatch)",
1511 /* if we get here, the parameters are OK */
1517 * Test glTexSubImage[123]D() parameters for errors.
1519 * \param ctx GL context.
1520 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1521 * \param target texture target given by the user.
1522 * \param level image level given by the user.
1523 * \param xoffset sub-image x offset given by the user.
1524 * \param yoffset sub-image y offset given by the user.
1525 * \param zoffset sub-image z offset given by the user.
1526 * \param format pixel data format given by the user.
1527 * \param type pixel data type given by the user.
1528 * \param width image width given by the user.
1529 * \param height image height given by the user.
1530 * \param depth image depth given by the user.
1532 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1534 * Verifies each of the parameters against the constants specified in
1535 * __struct gl_contextRec::Const and the supported extensions, and according to the
1536 * OpenGL specification.
1539 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1540 GLenum target
, GLint level
,
1541 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1542 GLint width
, GLint height
, GLint depth
,
1543 GLenum format
, GLenum type
)
1546 if (dimensions
== 1) {
1547 if (target
!= GL_TEXTURE_1D
) {
1548 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage1D(target)" );
1552 else if (dimensions
== 2) {
1553 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1554 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1555 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1556 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1560 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1561 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1562 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1566 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1567 if (!ctx
->Extensions
.MESA_texture_array
) {
1568 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1572 else if (target
!= GL_TEXTURE_2D
) {
1573 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(target)" );
1577 else if (dimensions
== 3) {
1578 if (target
== GL_TEXTURE_2D_ARRAY_EXT
) {
1579 if (!ctx
->Extensions
.MESA_texture_array
) {
1580 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1584 else if (target
!= GL_TEXTURE_3D
) {
1585 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexSubImage3D(target)" );
1590 _mesa_problem( ctx
, "invalid dims in texture_error_check" );
1594 /* Basic level check */
1595 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1596 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1601 _mesa_error(ctx
, GL_INVALID_VALUE
,
1602 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1605 if (height
< 0 && dimensions
> 1) {
1606 _mesa_error(ctx
, GL_INVALID_VALUE
,
1607 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1610 if (depth
< 0 && dimensions
> 2) {
1611 _mesa_error(ctx
, GL_INVALID_VALUE
,
1612 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1616 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1617 /* As with the glTexImage2D check above, the error code here
1618 * depends on texture integer.
1620 GLenum error
= _mesa_is_integer_format(format
)
1621 ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
1622 _mesa_error(ctx
, error
,
1623 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1624 dimensions
, format
, type
);
1633 * Do second part of glTexSubImage which depends on the destination texture.
1634 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1637 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1638 GLenum target
, GLint level
,
1639 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1640 GLint width
, GLint height
, GLint depth
,
1641 GLenum format
, GLenum type
,
1642 const struct gl_texture_image
*destTex
)
1645 /* undefined image level */
1646 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1650 if (xoffset
< -((GLint
)destTex
->Border
)) {
1651 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1655 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1656 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1660 if (dimensions
> 1) {
1661 if (yoffset
< -((GLint
)destTex
->Border
)) {
1662 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1666 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1667 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1672 if (dimensions
> 2) {
1673 if (zoffset
< -((GLint
)destTex
->Border
)) {
1674 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1677 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1678 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1683 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
1686 if (!target_can_be_compressed(ctx
, target
)) {
1687 _mesa_error(ctx
, GL_INVALID_ENUM
,
1688 "glTexSubImage%dD(target=%s)", dimensions
,
1689 _mesa_lookup_enum_by_nr(target
));
1693 /* do tests which depend on compression block size */
1694 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
1696 /* offset must be multiple of block size */
1697 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1699 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
1700 dimensions
, xoffset
, yoffset
);
1703 /* size must be multiple of bw by bh or equal to whole texture size */
1704 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
1705 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1706 "glTexSubImage%dD(width = %d)", dimensions
, width
);
1709 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
1710 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1711 "glTexSubImage%dD(height = %d)", dimensions
, height
);
1721 * Test glCopyTexImage[12]D() parameters for errors.
1723 * \param ctx GL context.
1724 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1725 * \param target texture target given by the user.
1726 * \param level image level given by the user.
1727 * \param internalFormat internal format given by the user.
1728 * \param width image width given by the user.
1729 * \param height image height given by the user.
1730 * \param border texture border.
1732 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1734 * Verifies each of the parameters against the constants specified in
1735 * __struct gl_contextRec::Const and the supported extensions, and according to the
1736 * OpenGL specification.
1739 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1740 GLenum target
, GLint level
, GLint internalFormat
,
1741 GLint width
, GLint height
, GLint border
)
1747 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1748 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1749 _mesa_error(ctx
, GL_INVALID_VALUE
,
1750 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1754 /* Check that the source buffer is complete */
1755 if (ctx
->ReadBuffer
->Name
) {
1756 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1757 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1758 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1759 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1765 if (border
< 0 || border
> 1 ||
1766 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1767 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1771 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1773 _mesa_error(ctx
, GL_INVALID_VALUE
,
1774 "glCopyTexImage%dD(internalFormat)", dimensions
);
1778 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1779 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1780 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
1784 /* NOTE: the format and type aren't really significant for
1785 * TestProxyTexImage(). Only the internalformat really matters.
1789 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1790 * level, width, height and depth.
1792 if (dimensions
== 1) {
1793 if (target
== GL_TEXTURE_1D
) {
1794 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_1D
,
1795 level
, internalFormat
,
1797 width
, 1, 1, border
);
1800 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage1D(target)" );
1804 else if (dimensions
== 2) {
1805 if (target
== GL_TEXTURE_2D
) {
1806 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_2D
,
1807 level
, internalFormat
,
1809 width
, height
, 1, border
);
1811 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1812 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1813 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1814 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1817 sizeOK
= (width
== height
) &&
1818 ctx
->Driver
.TestProxyTexImage(ctx
, GL_PROXY_TEXTURE_CUBE_MAP_ARB
,
1819 level
, internalFormat
, format
, type
,
1820 width
, height
, 1, border
);
1822 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1823 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1824 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1827 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1828 GL_PROXY_TEXTURE_RECTANGLE_NV
,
1829 level
, internalFormat
,
1831 width
, height
, 1, border
);
1833 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1834 if (!ctx
->Extensions
.MESA_texture_array
) {
1835 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)");
1838 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
,
1839 GL_PROXY_TEXTURE_1D_ARRAY_EXT
,
1840 level
, internalFormat
,
1842 width
, height
, 1, border
);
1845 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexImage2D(target)" );
1850 _mesa_problem(ctx
, "invalid dimensions in copytexture_error_check");
1855 if (dimensions
== 1) {
1856 _mesa_error(ctx
, GL_INVALID_VALUE
,
1857 "glCopyTexImage1D(width=%d)", width
);
1860 ASSERT(dimensions
== 2);
1861 _mesa_error(ctx
, GL_INVALID_VALUE
,
1862 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1867 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1868 if (!target_can_be_compressed(ctx
, target
)) {
1869 _mesa_error(ctx
, GL_INVALID_ENUM
,
1870 "glCopyTexImage%dD(target)", dimensions
);
1874 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1875 "glCopyTexImage%dD(border!=0)", dimensions
);
1879 else if (_mesa_is_depth_format(internalFormat
)) {
1880 /* make sure we have depth/stencil buffers */
1881 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
1882 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1883 "glCopyTexImage%dD(no depth)", dimensions
);
1887 else if (_mesa_is_depthstencil_format(internalFormat
)) {
1888 /* make sure we have depth/stencil buffers */
1889 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
1890 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1891 "glCopyTexImage%dD(no depth/stencil buffer)", dimensions
);
1896 /* if we get here, the parameters are OK */
1902 * Test glCopyTexSubImage[12]D() parameters for errors.
1903 * Note that this is the first part of error checking.
1904 * See also copytexsubimage_error_check2() below for the second part.
1906 * \param ctx GL context.
1907 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1908 * \param target texture target given by the user.
1909 * \param level image level given by the user.
1911 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1914 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
1915 GLenum target
, GLint level
)
1917 /* Check that the source buffer is complete */
1918 if (ctx
->ReadBuffer
->Name
) {
1919 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1920 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1921 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1922 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1928 if (dimensions
== 1) {
1929 if (target
!= GL_TEXTURE_1D
) {
1930 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage1D(target)" );
1934 else if (dimensions
== 2) {
1935 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1936 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
1937 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
1938 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1942 else if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1943 if (!ctx
->Extensions
.NV_texture_rectangle
) {
1944 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1948 else if (target
== GL_TEXTURE_1D_ARRAY_EXT
) {
1949 if (!ctx
->Extensions
.MESA_texture_array
) {
1950 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1954 else if (target
!= GL_TEXTURE_2D
) {
1955 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage2D(target)" );
1959 else if (dimensions
== 3) {
1960 if (((target
!= GL_TEXTURE_2D_ARRAY_EXT
) ||
1961 (!ctx
->Extensions
.MESA_texture_array
))
1962 && (target
!= GL_TEXTURE_3D
)) {
1963 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage3D(target)" );
1969 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1970 _mesa_error(ctx
, GL_INVALID_VALUE
,
1971 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1980 * Second part of error checking for glCopyTexSubImage[12]D().
1981 * \param xoffset sub-image x offset given by the user.
1982 * \param yoffset sub-image y offset given by the user.
1983 * \param zoffset sub-image z offset given by the user.
1984 * \param width image width given by the user.
1985 * \param height image height given by the user.
1988 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1989 GLenum target
, GLint level
,
1990 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1991 GLsizei width
, GLsizei height
,
1992 const struct gl_texture_image
*teximage
)
1994 /* check that dest tex image exists */
1996 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1997 "glCopyTexSubImage%dD(undefined texture level: %d)",
2004 _mesa_error(ctx
, GL_INVALID_VALUE
,
2005 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
2008 if (dimensions
> 1 && height
< 0) {
2009 _mesa_error(ctx
, GL_INVALID_VALUE
,
2010 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
2014 /* check x/y offsets */
2015 if (xoffset
< -((GLint
)teximage
->Border
)) {
2016 _mesa_error(ctx
, GL_INVALID_VALUE
,
2017 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
2020 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
2021 _mesa_error(ctx
, GL_INVALID_VALUE
,
2022 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
2025 if (dimensions
> 1) {
2026 if (yoffset
< -((GLint
)teximage
->Border
)) {
2027 _mesa_error(ctx
, GL_INVALID_VALUE
,
2028 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2031 /* NOTE: we're adding the border here, not subtracting! */
2032 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
2033 _mesa_error(ctx
, GL_INVALID_VALUE
,
2034 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2039 /* check z offset */
2040 if (dimensions
> 2) {
2041 if (zoffset
< -((GLint
)teximage
->Border
)) {
2042 _mesa_error(ctx
, GL_INVALID_VALUE
,
2043 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2046 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
2047 _mesa_error(ctx
, GL_INVALID_VALUE
,
2048 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2053 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2054 if (!target_can_be_compressed(ctx
, target
)) {
2055 _mesa_error(ctx
, GL_INVALID_ENUM
,
2056 "glCopyTexSubImage%dD(target)", dimensions
);
2059 /* offset must be multiple of 4 */
2060 if ((xoffset
& 3) || (yoffset
& 3)) {
2061 _mesa_error(ctx
, GL_INVALID_VALUE
,
2062 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2065 /* size must be multiple of 4 */
2066 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2067 _mesa_error(ctx
, GL_INVALID_VALUE
,
2068 "glCopyTexSubImage%dD(width)", dimensions
);
2071 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2072 _mesa_error(ctx
, GL_INVALID_VALUE
,
2073 "glCopyTexSubImage%dD(height)", dimensions
);
2078 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2079 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2083 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2084 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2085 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2086 dimensions
, teximage
->_BaseFormat
);
2090 if (teximage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2091 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
2092 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2093 "glCopyTexSubImage%dD(no depth buffer)",
2098 else if (teximage
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
2099 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
2100 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2101 "glCopyTexSubImage%dD(no depth/stencil buffer)",
2107 /* If copying into an integer texture, the source buffer must also be
2110 if (_mesa_is_format_integer_color(teximage
->TexFormat
)) {
2111 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2112 if (!_mesa_is_format_integer_color(rb
->Format
)) {
2113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2114 "glCopyTexSubImage%dD(source buffer is not integer format)",
2120 /* if we get here, the parameters are OK */
2125 /** Callback info for walking over FBO hash table */
2128 struct gl_context
*ctx
;
2129 struct gl_texture_object
*texObj
;
2135 * Check render to texture callback. Called from _mesa_HashWalk().
2138 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2140 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2141 const struct cb_info
*info
= (struct cb_info
*) userData
;
2142 struct gl_context
*ctx
= info
->ctx
;
2143 const struct gl_texture_object
*texObj
= info
->texObj
;
2144 const GLuint level
= info
->level
, face
= info
->face
;
2146 /* If this is a user-created FBO */
2149 /* check if any of the FBO's attachments point to 'texObj' */
2150 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2151 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2152 if (att
->Type
== GL_TEXTURE
&&
2153 att
->Texture
== texObj
&&
2154 att
->TextureLevel
== level
&&
2155 att
->CubeMapFace
== face
) {
2156 ASSERT(att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]);
2157 /* Tell driver about the new renderbuffer texture */
2158 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2159 /* Mark fb status as indeterminate to force re-validation */
2168 * When a texture image is specified we have to check if it's bound to
2169 * any framebuffer objects (render to texture) in order to detect changes
2170 * in size or format since that effects FBO completeness.
2171 * Any FBOs rendering into the texture must be re-validated.
2174 update_fbo_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
2175 GLuint face
, GLuint level
)
2177 /* Only check this texture if it's been marked as RenderToTexture */
2178 if (texObj
->_RenderToTexture
) {
2179 struct cb_info info
;
2181 info
.texObj
= texObj
;
2184 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2190 * If the texture object's GenerateMipmap flag is set and we've
2191 * changed the texture base level image, regenerate the rest of the
2192 * mipmap levels now.
2195 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2196 struct gl_texture_object
*texObj
, GLint level
)
2198 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2199 if (texObj
->GenerateMipmap
&&
2200 level
== texObj
->BaseLevel
&&
2201 level
< texObj
->MaxLevel
) {
2202 ASSERT(ctx
->Driver
.GenerateMipmap
);
2203 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2208 /** Debug helper: override the user-requested internal format */
2210 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2213 if (internalFormat
== GL_RGBA16F_ARB
||
2214 internalFormat
== GL_RGBA32F_ARB
) {
2215 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2218 else if (internalFormat
== GL_RGB16F_ARB
||
2219 internalFormat
== GL_RGB32F_ARB
) {
2220 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2223 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2224 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2225 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2226 return GL_LUMINANCE_ALPHA
;
2228 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2229 internalFormat
== GL_LUMINANCE32F_ARB
) {
2230 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2231 return GL_LUMINANCE
;
2233 else if (internalFormat
== GL_ALPHA16F_ARB
||
2234 internalFormat
== GL_ALPHA32F_ARB
) {
2235 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2239 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2240 internalFormat = GL_RGBA;
2244 return internalFormat
;
2247 return internalFormat
;
2253 * Choose the actual hardware format for a texture image.
2254 * Try to use the same format as the previous image level when possible.
2255 * Otherwise, ask the driver for the best format.
2256 * It's important to try to choose a consistant format for all levels
2257 * for efficient texture memory layout/allocation. In particular, this
2258 * comes up during automatic mipmap generation.
2261 _mesa_choose_texture_format(struct gl_context
*ctx
,
2262 struct gl_texture_object
*texObj
,
2263 struct gl_texture_image
*texImage
,
2264 GLenum target
, GLint level
,
2265 GLenum internalFormat
, GLenum format
, GLenum type
)
2267 /* see if we've already chosen a format for the previous level */
2269 struct gl_texture_image
*prevImage
=
2270 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2271 /* See if the prev level is defined and has an internal format which
2272 * matches the new internal format.
2275 prevImage
->Width
> 0 &&
2276 prevImage
->InternalFormat
== internalFormat
) {
2277 /* use the same format */
2278 texImage
->TexFormat
= prevImage
->TexFormat
;
2283 /* choose format from scratch */
2284 texImage
->TexFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
,
2286 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
2292 * Called from the API. Note that width includes the border.
2295 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2296 GLsizei width
, GLint border
, GLenum format
,
2297 GLenum type
, const GLvoid
*pixels
)
2299 GET_CURRENT_CONTEXT(ctx
);
2300 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2302 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2303 _mesa_debug(ctx
, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2304 _mesa_lookup_enum_by_nr(target
), level
,
2305 _mesa_lookup_enum_by_nr(internalFormat
), width
, border
,
2306 _mesa_lookup_enum_by_nr(format
),
2307 _mesa_lookup_enum_by_nr(type
), pixels
);
2309 internalFormat
= override_internal_format(internalFormat
, width
, 1);
2311 if (target
== GL_TEXTURE_1D
) {
2312 /* non-proxy target */
2313 struct gl_texture_object
*texObj
;
2314 struct gl_texture_image
*texImage
;
2315 const GLuint face
= _mesa_tex_target_to_face(target
);
2317 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2318 format
, type
, 1, width
, 1, 1, border
)) {
2319 return; /* error was recorded */
2322 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2323 _mesa_update_state(ctx
);
2325 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2326 _mesa_lock_texture(ctx
, texObj
);
2328 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2330 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
2333 if (texImage
->Data
) {
2334 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2337 ASSERT(texImage
->Data
== NULL
);
2339 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2340 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2342 border
, internalFormat
);
2344 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2345 internalFormat
, format
, type
);
2347 /* Give the texture to the driver. <pixels> may be null. */
2348 ASSERT(ctx
->Driver
.TexImage1D
);
2349 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2350 width
, border
, format
, type
, pixels
,
2351 &ctx
->Unpack
, texObj
, texImage
);
2353 ASSERT(texImage
->TexFormat
);
2355 _mesa_set_fetch_functions(texImage
, 1);
2357 check_gen_mipmap(ctx
, target
, texObj
, level
);
2359 update_fbo_texture(ctx
, texObj
, face
, level
);
2362 texObj
->_Complete
= GL_FALSE
;
2363 ctx
->NewState
|= _NEW_TEXTURE
;
2366 _mesa_unlock_texture(ctx
, texObj
);
2368 else if (target
== GL_PROXY_TEXTURE_1D
) {
2369 /* Proxy texture: check for errors and update proxy state */
2370 struct gl_texture_image
*texImage
;
2371 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2372 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2373 format
, type
, 1, width
, 1, 1, border
)) {
2374 /* when error, clear all proxy texture image parameters */
2376 clear_teximage_fields(texImage
);
2379 /* no error, set the tex image parameters */
2380 struct gl_texture_object
*texObj
=
2381 _mesa_get_current_tex_object(ctx
, target
);
2383 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2385 border
, internalFormat
);
2386 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2387 internalFormat
, format
, type
);
2391 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage1D(target)" );
2398 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2399 GLsizei width
, GLsizei height
, GLint border
,
2400 GLenum format
, GLenum type
,
2401 const GLvoid
*pixels
)
2403 GET_CURRENT_CONTEXT(ctx
);
2404 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2406 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2407 _mesa_debug(ctx
, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2408 _mesa_lookup_enum_by_nr(target
), level
,
2409 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2410 border
, _mesa_lookup_enum_by_nr(format
),
2411 _mesa_lookup_enum_by_nr(type
), pixels
);
2413 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2415 if (target
== GL_TEXTURE_2D
||
2416 (ctx
->Extensions
.ARB_texture_cube_map
&&
2417 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
2418 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) ||
2419 (ctx
->Extensions
.NV_texture_rectangle
&&
2420 target
== GL_TEXTURE_RECTANGLE_NV
) ||
2421 (ctx
->Extensions
.MESA_texture_array
&&
2422 target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
2423 /* non-proxy target */
2424 struct gl_texture_object
*texObj
;
2425 struct gl_texture_image
*texImage
;
2426 const GLuint face
= _mesa_tex_target_to_face(target
);
2428 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2429 format
, type
, 2, width
, height
, 1, border
)) {
2430 return; /* error was recorded */
2433 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2434 _mesa_update_state(ctx
);
2436 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2437 _mesa_lock_texture(ctx
, texObj
);
2439 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2441 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
2444 if (texImage
->Data
) {
2445 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2448 ASSERT(texImage
->Data
== NULL
);
2449 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2450 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2452 border
, internalFormat
);
2454 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2455 internalFormat
, format
, type
);
2457 /* Give the texture to the driver. <pixels> may be null. */
2458 ASSERT(ctx
->Driver
.TexImage2D
);
2459 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2460 width
, height
, border
, format
, type
,
2461 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2463 ASSERT(texImage
->TexFormat
);
2465 _mesa_set_fetch_functions(texImage
, 2);
2467 check_gen_mipmap(ctx
, target
, texObj
, level
);
2469 update_fbo_texture(ctx
, texObj
, face
, level
);
2472 texObj
->_Complete
= GL_FALSE
;
2473 ctx
->NewState
|= _NEW_TEXTURE
;
2476 _mesa_unlock_texture(ctx
, texObj
);
2478 else if (target
== GL_PROXY_TEXTURE_2D
||
2479 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
2480 ctx
->Extensions
.ARB_texture_cube_map
) ||
2481 (target
== GL_PROXY_TEXTURE_RECTANGLE_NV
&&
2482 ctx
->Extensions
.NV_texture_rectangle
) ||
2483 (ctx
->Extensions
.MESA_texture_array
&&
2484 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
)) {
2485 /* Proxy texture: check for errors and update proxy state */
2486 struct gl_texture_image
*texImage
;
2487 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2488 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2489 format
, type
, 2, width
, height
, 1, border
)) {
2490 /* when error, clear all proxy texture image parameters */
2492 clear_teximage_fields(texImage
);
2495 /* no error, set the tex image parameters */
2496 struct gl_texture_object
*texObj
=
2497 _mesa_get_current_tex_object(ctx
, target
);
2498 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2500 border
, internalFormat
);
2501 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2502 internalFormat
, format
, type
);
2506 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage2D(target)" );
2513 * Called by the API or display list executor.
2514 * Note that width and height include the border.
2517 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2518 GLsizei width
, GLsizei height
, GLsizei depth
,
2519 GLint border
, GLenum format
, GLenum type
,
2520 const GLvoid
*pixels
)
2522 GET_CURRENT_CONTEXT(ctx
);
2523 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2525 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2526 _mesa_debug(ctx
, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2527 _mesa_lookup_enum_by_nr(target
), level
,
2528 _mesa_lookup_enum_by_nr(internalFormat
), width
, height
,
2529 depth
, border
, _mesa_lookup_enum_by_nr(format
),
2530 _mesa_lookup_enum_by_nr(type
), pixels
);
2532 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2534 if (target
== GL_TEXTURE_3D
||
2535 (ctx
->Extensions
.MESA_texture_array
&&
2536 target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
2537 /* non-proxy target */
2538 struct gl_texture_object
*texObj
;
2539 struct gl_texture_image
*texImage
;
2540 const GLuint face
= _mesa_tex_target_to_face(target
);
2542 if (texture_error_check(ctx
, target
, level
, (GLint
) internalFormat
,
2543 format
, type
, 3, width
, height
, depth
, border
)) {
2544 return; /* error was recorded */
2547 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2548 _mesa_update_state(ctx
);
2550 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2551 _mesa_lock_texture(ctx
, texObj
);
2553 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2555 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage3D");
2558 if (texImage
->Data
) {
2559 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2562 ASSERT(texImage
->Data
== NULL
);
2563 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2564 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2565 width
, height
, depth
,
2566 border
, internalFormat
);
2568 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2569 internalFormat
, format
, type
);
2571 /* Give the texture to the driver. <pixels> may be null. */
2572 ASSERT(ctx
->Driver
.TexImage3D
);
2573 ctx
->Driver
.TexImage3D(ctx
, target
, level
, internalFormat
,
2574 width
, height
, depth
, border
, format
, type
,
2575 pixels
, &ctx
->Unpack
, texObj
, texImage
);
2577 ASSERT(texImage
->TexFormat
);
2579 _mesa_set_fetch_functions(texImage
, 3);
2581 check_gen_mipmap(ctx
, target
, texObj
, level
);
2583 update_fbo_texture(ctx
, texObj
, face
, level
);
2586 texObj
->_Complete
= GL_FALSE
;
2587 ctx
->NewState
|= _NEW_TEXTURE
;
2590 _mesa_unlock_texture(ctx
, texObj
);
2592 else if (target
== GL_PROXY_TEXTURE_3D
||
2593 (ctx
->Extensions
.MESA_texture_array
&&
2594 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
)) {
2595 /* Proxy texture: check for errors and update proxy state */
2596 struct gl_texture_image
*texImage
;
2597 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
2598 if (texture_error_check(ctx
, target
, level
, internalFormat
,
2599 format
, type
, 3, width
, height
, depth
, border
)) {
2600 /* when error, clear all proxy texture image parameters */
2602 clear_teximage_fields(texImage
);
2605 /* no error, set the tex image parameters */
2606 struct gl_texture_object
*texObj
=
2607 _mesa_get_current_tex_object(ctx
, target
);
2608 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
2609 depth
, border
, internalFormat
);
2610 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2611 internalFormat
, format
, type
);
2615 _mesa_error( ctx
, GL_INVALID_ENUM
, "glTexImage3D(target)" );
2622 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2623 GLsizei width
, GLsizei height
, GLsizei depth
,
2624 GLint border
, GLenum format
, GLenum type
,
2625 const GLvoid
*pixels
)
2627 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2628 depth
, border
, format
, type
, pixels
);
2632 #if FEATURE_OES_EGL_image
2634 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
2636 struct gl_texture_object
*texObj
;
2637 struct gl_texture_image
*texImage
;
2638 GET_CURRENT_CONTEXT(ctx
);
2639 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2641 if (!ctx
->Extensions
.OES_EGL_image
) {
2642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2643 "glEGLImageTargetTexture2DOES(unsupported)");
2647 if (target
!= GL_TEXTURE_2D
) {
2648 _mesa_error(ctx
, GL_INVALID_ENUM
,
2649 "glEGLImageTargetTexture2D(target=%d)", target
);
2653 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2654 _mesa_update_state(ctx
);
2656 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2657 _mesa_lock_texture(ctx
, texObj
);
2659 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
2661 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
2664 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2666 ASSERT(texImage
->Data
== NULL
);
2667 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
2668 texObj
, texImage
, image
);
2671 texObj
->_Complete
= GL_FALSE
;
2672 ctx
->NewState
|= _NEW_TEXTURE
;
2674 _mesa_unlock_texture(ctx
, texObj
);
2681 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2682 GLint xoffset
, GLsizei width
,
2683 GLenum format
, GLenum type
,
2684 const GLvoid
*pixels
)
2686 struct gl_texture_object
*texObj
;
2687 struct gl_texture_image
*texImage
;
2688 GET_CURRENT_CONTEXT(ctx
);
2689 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2691 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2692 _mesa_debug(ctx
, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2693 _mesa_lookup_enum_by_nr(target
), level
,
2694 xoffset
, width
, _mesa_lookup_enum_by_nr(format
),
2695 _mesa_lookup_enum_by_nr(type
), pixels
);
2697 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2698 _mesa_update_state(ctx
);
2700 if (subtexture_error_check(ctx
, 1, target
, level
, xoffset
, 0, 0,
2701 width
, 1, 1, format
, type
)) {
2702 return; /* error was detected */
2706 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2709 _mesa_lock_texture(ctx
, texObj
);
2711 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2713 if (subtexture_error_check2(ctx
, 1, target
, level
, xoffset
, 0, 0,
2714 width
, 1, 1, format
, type
, texImage
)) {
2715 /* error was recorded */
2717 else if (width
> 0) {
2718 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2719 xoffset
+= texImage
->Border
;
2721 ASSERT(ctx
->Driver
.TexSubImage1D
);
2722 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
, xoffset
, width
,
2723 format
, type
, pixels
, &ctx
->Unpack
,
2726 check_gen_mipmap(ctx
, target
, texObj
, level
);
2728 ctx
->NewState
|= _NEW_TEXTURE
;
2731 _mesa_unlock_texture(ctx
, texObj
);
2736 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2737 GLint xoffset
, GLint yoffset
,
2738 GLsizei width
, GLsizei height
,
2739 GLenum format
, GLenum type
,
2740 const GLvoid
*pixels
)
2742 struct gl_texture_object
*texObj
;
2743 struct gl_texture_image
*texImage
;
2744 GET_CURRENT_CONTEXT(ctx
);
2745 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2747 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2748 _mesa_debug(ctx
, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2749 _mesa_lookup_enum_by_nr(target
), level
,
2750 xoffset
, yoffset
, width
, height
,
2751 _mesa_lookup_enum_by_nr(format
),
2752 _mesa_lookup_enum_by_nr(type
), pixels
);
2754 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2755 _mesa_update_state(ctx
);
2757 if (subtexture_error_check(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2758 width
, height
, 1, format
, type
)) {
2759 return; /* error was detected */
2762 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2764 _mesa_lock_texture(ctx
, texObj
);
2766 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2768 if (subtexture_error_check2(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
2769 width
, height
, 1, format
, type
, texImage
)) {
2770 /* error was recorded */
2772 else if (width
> 0 && height
>= 0) {
2773 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2774 xoffset
+= texImage
->Border
;
2775 yoffset
+= texImage
->Border
;
2777 ASSERT(ctx
->Driver
.TexSubImage2D
);
2778 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
2779 width
, height
, format
, type
, pixels
,
2780 &ctx
->Unpack
, texObj
, texImage
);
2782 check_gen_mipmap(ctx
, target
, texObj
, level
);
2784 ctx
->NewState
|= _NEW_TEXTURE
;
2787 _mesa_unlock_texture(ctx
, texObj
);
2793 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2794 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2795 GLsizei width
, GLsizei height
, GLsizei depth
,
2796 GLenum format
, GLenum type
,
2797 const GLvoid
*pixels
)
2799 struct gl_texture_object
*texObj
;
2800 struct gl_texture_image
*texImage
;
2801 GET_CURRENT_CONTEXT(ctx
);
2802 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2804 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2805 _mesa_debug(ctx
, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2806 _mesa_lookup_enum_by_nr(target
), level
,
2807 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2808 _mesa_lookup_enum_by_nr(format
),
2809 _mesa_lookup_enum_by_nr(type
), pixels
);
2811 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2812 _mesa_update_state(ctx
);
2814 if (subtexture_error_check(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2815 width
, height
, depth
, format
, type
)) {
2816 return; /* error was detected */
2819 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2821 _mesa_lock_texture(ctx
, texObj
);
2823 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2825 if (subtexture_error_check2(ctx
, 3, target
, level
,
2826 xoffset
, yoffset
, zoffset
,
2827 width
, height
, depth
,
2828 format
, type
, texImage
)) {
2829 /* error was recorded */
2831 else if (width
> 0 && height
> 0 && height
> 0) {
2832 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2833 xoffset
+= texImage
->Border
;
2834 yoffset
+= texImage
->Border
;
2835 zoffset
+= texImage
->Border
;
2837 ASSERT(ctx
->Driver
.TexSubImage3D
);
2838 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
,
2839 xoffset
, yoffset
, zoffset
,
2840 width
, height
, depth
,
2841 format
, type
, pixels
,
2842 &ctx
->Unpack
, texObj
, texImage
);
2844 check_gen_mipmap(ctx
, target
, texObj
, level
);
2846 ctx
->NewState
|= _NEW_TEXTURE
;
2849 _mesa_unlock_texture(ctx
, texObj
);
2855 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2856 GLenum internalFormat
,
2858 GLsizei width
, GLint border
)
2860 struct gl_texture_object
*texObj
;
2861 struct gl_texture_image
*texImage
;
2862 const GLuint face
= _mesa_tex_target_to_face(target
);
2863 GET_CURRENT_CONTEXT(ctx
);
2864 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2866 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2867 _mesa_debug(ctx
, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2868 _mesa_lookup_enum_by_nr(target
), level
,
2869 _mesa_lookup_enum_by_nr(internalFormat
),
2870 x
, y
, width
, border
);
2872 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2873 _mesa_update_state(ctx
);
2875 if (copytexture_error_check(ctx
, 1, target
, level
, internalFormat
,
2879 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2881 _mesa_lock_texture(ctx
, texObj
);
2883 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2885 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage1D");
2888 if (texImage
->Data
) {
2889 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2892 ASSERT(texImage
->Data
== NULL
);
2894 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2895 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
2896 border
, internalFormat
);
2898 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2899 internalFormat
, GL_NONE
, GL_NONE
);
2901 ASSERT(ctx
->Driver
.CopyTexImage1D
);
2902 ctx
->Driver
.CopyTexImage1D(ctx
, target
, level
, internalFormat
,
2903 x
, y
, width
, border
);
2905 ASSERT(texImage
->TexFormat
);
2907 _mesa_set_fetch_functions(texImage
, 1);
2909 check_gen_mipmap(ctx
, target
, texObj
, level
);
2911 update_fbo_texture(ctx
, texObj
, face
, level
);
2914 texObj
->_Complete
= GL_FALSE
;
2915 ctx
->NewState
|= _NEW_TEXTURE
;
2918 _mesa_unlock_texture(ctx
, texObj
);
2924 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2925 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2928 struct gl_texture_object
*texObj
;
2929 struct gl_texture_image
*texImage
;
2930 const GLuint face
= _mesa_tex_target_to_face(target
);
2931 GET_CURRENT_CONTEXT(ctx
);
2932 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2934 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2935 _mesa_debug(ctx
, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2936 _mesa_lookup_enum_by_nr(target
), level
,
2937 _mesa_lookup_enum_by_nr(internalFormat
),
2938 x
, y
, width
, height
, border
);
2940 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2941 _mesa_update_state(ctx
);
2943 if (copytexture_error_check(ctx
, 2, target
, level
, internalFormat
,
2944 width
, height
, border
))
2947 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2949 _mesa_lock_texture(ctx
, texObj
);
2951 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2954 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage2D");
2957 if (texImage
->Data
) {
2958 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2961 ASSERT(texImage
->Data
== NULL
);
2963 clear_teximage_fields(texImage
); /* not really needed, but helpful */
2964 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2966 border
, internalFormat
);
2968 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
2969 internalFormat
, GL_NONE
, GL_NONE
);
2971 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2972 ctx
->Driver
.CopyTexImage2D(ctx
, target
, level
, internalFormat
,
2973 x
, y
, width
, height
, border
);
2975 ASSERT(texImage
->TexFormat
);
2977 _mesa_set_fetch_functions(texImage
, 2);
2979 check_gen_mipmap(ctx
, target
, texObj
, level
);
2981 update_fbo_texture(ctx
, texObj
, face
, level
);
2984 texObj
->_Complete
= GL_FALSE
;
2985 ctx
->NewState
|= _NEW_TEXTURE
;
2988 _mesa_unlock_texture(ctx
, texObj
);
2993 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2994 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2996 struct gl_texture_object
*texObj
;
2997 struct gl_texture_image
*texImage
;
3001 GET_CURRENT_CONTEXT(ctx
);
3002 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3004 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3005 _mesa_debug(ctx
, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
3006 _mesa_lookup_enum_by_nr(target
),
3007 level
, xoffset
, x
, y
, width
);
3009 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3010 _mesa_update_state(ctx
);
3012 if (copytexsubimage_error_check1(ctx
, 1, target
, level
))
3015 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3017 _mesa_lock_texture(ctx
, texObj
);
3019 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3021 if (copytexsubimage_error_check2(ctx
, 1, target
, level
,
3022 xoffset
, 0, 0, width
, 1, texImage
)) {
3023 /* error was recorded */
3026 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3027 xoffset
+= texImage
->Border
;
3029 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3031 ASSERT(ctx
->Driver
.CopyTexSubImage1D
);
3032 ctx
->Driver
.CopyTexSubImage1D(ctx
, target
, level
,
3033 xoffset
, x
, y
, width
);
3035 check_gen_mipmap(ctx
, target
, texObj
, level
);
3037 ctx
->NewState
|= _NEW_TEXTURE
;
3041 _mesa_unlock_texture(ctx
, texObj
);
3047 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3048 GLint xoffset
, GLint yoffset
,
3049 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3051 struct gl_texture_object
*texObj
;
3052 struct gl_texture_image
*texImage
;
3053 GET_CURRENT_CONTEXT(ctx
);
3054 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3056 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3057 _mesa_debug(ctx
, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
3058 _mesa_lookup_enum_by_nr(target
),
3059 level
, xoffset
, yoffset
, x
, y
, width
, height
);
3061 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3062 _mesa_update_state(ctx
);
3064 if (copytexsubimage_error_check1(ctx
, 2, target
, level
))
3067 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3069 _mesa_lock_texture(ctx
, texObj
);
3071 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3073 if (copytexsubimage_error_check2(ctx
, 2, target
, level
,
3074 xoffset
, yoffset
, 0,
3075 width
, height
, texImage
)) {
3076 /* error was recorded */
3079 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3080 xoffset
+= texImage
->Border
;
3081 yoffset
+= texImage
->Border
;
3083 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3085 ASSERT(ctx
->Driver
.CopyTexSubImage2D
);
3086 ctx
->Driver
.CopyTexSubImage2D(ctx
, target
, level
, xoffset
, yoffset
,
3087 x
, y
, width
, height
);
3089 check_gen_mipmap(ctx
, target
, texObj
, level
);
3091 ctx
->NewState
|= _NEW_TEXTURE
;
3095 _mesa_unlock_texture(ctx
, texObj
);
3101 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3102 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3103 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3105 struct gl_texture_object
*texObj
;
3106 struct gl_texture_image
*texImage
;
3107 GET_CURRENT_CONTEXT(ctx
);
3108 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3110 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3111 _mesa_debug(ctx
, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
3112 _mesa_lookup_enum_by_nr(target
),
3113 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3115 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3116 _mesa_update_state(ctx
);
3118 if (copytexsubimage_error_check1(ctx
, 3, target
, level
))
3121 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3123 _mesa_lock_texture(ctx
, texObj
);
3125 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3127 if (copytexsubimage_error_check2(ctx
, 3, target
, level
, xoffset
, yoffset
,
3128 zoffset
, width
, height
, texImage
)) {
3129 /* error was recored */
3132 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3133 xoffset
+= texImage
->Border
;
3134 yoffset
+= texImage
->Border
;
3135 zoffset
+= texImage
->Border
;
3137 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3139 ASSERT(ctx
->Driver
.CopyTexSubImage3D
);
3140 ctx
->Driver
.CopyTexSubImage3D(ctx
, target
, level
,
3141 xoffset
, yoffset
, zoffset
,
3142 x
, y
, width
, height
);
3144 check_gen_mipmap(ctx
, target
, texObj
, level
);
3146 ctx
->NewState
|= _NEW_TEXTURE
;
3150 _mesa_unlock_texture(ctx
, texObj
);
3156 /**********************************************************************/
3157 /****** Compressed Textures ******/
3158 /**********************************************************************/
3162 * Return expected size of a compressed texture.
3165 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
3168 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3169 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
3174 * Return compressed texture block size, in pixels.
3177 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
3179 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3180 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
3185 * Error checking for glCompressedTexImage[123]D().
3186 * \return error code or GL_NO_ERROR.
3189 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3190 GLenum target
, GLint level
,
3191 GLenum internalFormat
, GLsizei width
,
3192 GLsizei height
, GLsizei depth
, GLint border
,
3195 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3197 if (dimensions
== 1) {
3198 /* 1D compressed textures not allowed */
3199 return GL_INVALID_ENUM
;
3201 else if (dimensions
== 2) {
3202 if (target
== GL_PROXY_TEXTURE_2D
) {
3203 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3205 else if (target
== GL_TEXTURE_2D
) {
3206 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3208 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3209 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3210 return GL_INVALID_ENUM
; /*target*/
3211 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3213 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3214 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3215 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3216 return GL_INVALID_ENUM
; /*target*/
3217 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3220 return GL_INVALID_ENUM
; /*target*/
3223 else if (dimensions
== 3) {
3224 /* 3D compressed textures not allowed */
3225 return GL_INVALID_ENUM
;
3229 return GL_INVALID_ENUM
;
3232 maxTextureSize
= 1 << (maxLevels
- 1);
3234 /* This will detect any invalid internalFormat value */
3235 if (!_mesa_is_compressed_format(ctx
, internalFormat
))
3236 return GL_INVALID_ENUM
;
3238 /* This should really never fail */
3239 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
3240 return GL_INVALID_ENUM
;
3243 return GL_INVALID_VALUE
;
3246 * XXX We should probably use the proxy texture error check function here.
3248 if (width
< 1 || width
> maxTextureSize
||
3249 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(width
)))
3250 return GL_INVALID_VALUE
;
3252 if ((height
< 1 || height
> maxTextureSize
||
3253 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(height
)))
3255 return GL_INVALID_VALUE
;
3257 if ((depth
< 1 || depth
> maxTextureSize
||
3258 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&& !_mesa_is_pow_two(depth
)))
3260 return GL_INVALID_VALUE
;
3262 /* For cube map, width must equal height */
3263 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3264 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
3265 return GL_INVALID_VALUE
;
3267 if (level
< 0 || level
>= maxLevels
)
3268 return GL_INVALID_VALUE
;
3270 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
3271 if (expectedSize
!= imageSize
)
3272 return GL_INVALID_VALUE
;
3274 #if FEATURE_EXT_texture_sRGB
3275 if ((internalFormat
== GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
||
3276 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
||
3277 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
||
3278 internalFormat
== GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
)
3280 return GL_INVALID_OPERATION
;
3289 * Error checking for glCompressedTexSubImage[123]D().
3290 * \warning There are some bad assumptions here about the size of compressed
3291 * texture tiles (multiple of 4) used to test the validity of the
3292 * offset and size parameters.
3293 * \return error code or GL_NO_ERROR.
3296 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3297 GLenum target
, GLint level
,
3298 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3299 GLsizei width
, GLsizei height
, GLsizei depth
,
3300 GLenum format
, GLsizei imageSize
)
3302 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3306 if (dimensions
== 1) {
3307 /* 1D compressed textures not allowed */
3308 return GL_INVALID_ENUM
;
3310 else if (dimensions
== 2) {
3311 if (target
== GL_PROXY_TEXTURE_2D
) {
3312 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3314 else if (target
== GL_TEXTURE_2D
) {
3315 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3317 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3318 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3319 return GL_INVALID_ENUM
; /*target*/
3320 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3322 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3323 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3324 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3325 return GL_INVALID_ENUM
; /*target*/
3326 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3329 return GL_INVALID_ENUM
; /*target*/
3332 else if (dimensions
== 3) {
3333 /* 3D compressed textures not allowed */
3334 return GL_INVALID_ENUM
;
3337 maxTextureSize
= 1 << (maxLevels
- 1);
3339 /* this will catch any invalid compressed format token */
3340 if (!_mesa_is_compressed_format(ctx
, format
))
3341 return GL_INVALID_ENUM
;
3343 if (width
< 1 || width
> maxTextureSize
)
3344 return GL_INVALID_VALUE
;
3346 if ((height
< 1 || height
> maxTextureSize
)
3348 return GL_INVALID_VALUE
;
3350 if (level
< 0 || level
>= maxLevels
)
3351 return GL_INVALID_VALUE
;
3354 * do checks which depend on compression block size
3356 get_compressed_block_size(format
, &bw
, &bh
);
3358 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3359 return GL_INVALID_VALUE
;
3361 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3362 return GL_INVALID_VALUE
;
3364 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3365 return GL_INVALID_VALUE
;
3367 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3368 if (expectedSize
!= imageSize
)
3369 return GL_INVALID_VALUE
;
3376 * Do second part of glCompressedTexSubImage error checking.
3377 * \return GL_TRUE if error found, GL_FALSE otherwise.
3380 compressed_subtexture_error_check2(struct gl_context
*ctx
, GLuint dims
,
3381 GLsizei width
, GLsizei height
,
3382 GLsizei depth
, GLenum format
,
3383 struct gl_texture_image
*texImage
)
3386 if ((GLint
) format
!= texImage
->InternalFormat
) {
3387 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3388 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3392 if (((width
== 1 || width
== 2) &&
3393 width
!= (GLsizei
) texImage
->Width
) ||
3394 (width
> (GLsizei
) texImage
->Width
)) {
3395 _mesa_error(ctx
, GL_INVALID_VALUE
,
3396 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3401 if (((height
== 1 || height
== 2) &&
3402 height
!= (GLsizei
) texImage
->Height
) ||
3403 (height
> (GLsizei
) texImage
->Height
)) {
3404 _mesa_error(ctx
, GL_INVALID_VALUE
,
3405 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3411 if (((depth
== 1 || depth
== 2) &&
3412 depth
!= (GLsizei
) texImage
->Depth
) ||
3413 (depth
> (GLsizei
) texImage
->Depth
)) {
3414 _mesa_error(ctx
, GL_INVALID_VALUE
,
3415 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3426 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3427 GLenum internalFormat
, GLsizei width
,
3428 GLint border
, GLsizei imageSize
,
3431 GET_CURRENT_CONTEXT(ctx
);
3432 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3434 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3435 _mesa_debug(ctx
, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3436 _mesa_lookup_enum_by_nr(target
), level
,
3437 _mesa_lookup_enum_by_nr(internalFormat
),
3438 width
, border
, imageSize
, data
);
3440 if (target
== GL_TEXTURE_1D
) {
3441 /* non-proxy target */
3442 struct gl_texture_object
*texObj
;
3443 struct gl_texture_image
*texImage
;
3444 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3445 internalFormat
, width
, 1, 1, border
, imageSize
);
3447 _mesa_error(ctx
, error
, "glCompressedTexImage1D");
3451 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3453 _mesa_lock_texture(ctx
, texObj
);
3455 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3457 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage1D");
3460 if (texImage
->Data
) {
3461 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3463 ASSERT(texImage
->Data
== NULL
);
3465 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3466 border
, internalFormat
);
3468 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3469 internalFormat
, GL_NONE
, GL_NONE
);
3471 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
3472 ctx
->Driver
.CompressedTexImage1D(ctx
, target
, level
,
3473 internalFormat
, width
, border
,
3477 _mesa_set_fetch_functions(texImage
, 1);
3479 check_gen_mipmap(ctx
, target
, texObj
, level
);
3482 texObj
->_Complete
= GL_FALSE
;
3483 ctx
->NewState
|= _NEW_TEXTURE
;
3486 _mesa_unlock_texture(ctx
, texObj
);
3488 else if (target
== GL_PROXY_TEXTURE_1D
) {
3489 /* Proxy texture: check for errors and update proxy state */
3490 GLenum error
= compressed_texture_error_check(ctx
, 1, target
, level
,
3491 internalFormat
, width
, 1, 1, border
, imageSize
);
3493 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3494 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3495 internalFormat
, GL_NONE
, GL_NONE
,
3496 width
, 1, 1, border
);
3499 /* if error, clear all proxy texture image parameters */
3500 struct gl_texture_image
*texImage
;
3501 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3503 clear_teximage_fields(texImage
);
3506 /* store the teximage parameters */
3507 struct gl_texture_object
*texObj
;
3508 struct gl_texture_image
*texImage
;
3510 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3512 _mesa_lock_texture(ctx
, texObj
);
3514 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3515 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, 1, 1,
3516 border
, internalFormat
);
3517 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3518 internalFormat
, GL_NONE
, GL_NONE
);
3520 _mesa_unlock_texture(ctx
, texObj
);
3524 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage1D(target)");
3530 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3531 GLenum internalFormat
, GLsizei width
,
3532 GLsizei height
, GLint border
, GLsizei imageSize
,
3535 GET_CURRENT_CONTEXT(ctx
);
3536 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3538 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3539 _mesa_debug(ctx
, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3540 _mesa_lookup_enum_by_nr(target
), level
,
3541 _mesa_lookup_enum_by_nr(internalFormat
),
3542 width
, height
, border
, imageSize
, data
);
3545 switch (internalFormat
) {
3546 case GL_PALETTE4_RGB8_OES
:
3547 case GL_PALETTE4_RGBA8_OES
:
3548 case GL_PALETTE4_R5_G6_B5_OES
:
3549 case GL_PALETTE4_RGBA4_OES
:
3550 case GL_PALETTE4_RGB5_A1_OES
:
3551 case GL_PALETTE8_RGB8_OES
:
3552 case GL_PALETTE8_RGBA8_OES
:
3553 case GL_PALETTE8_R5_G6_B5_OES
:
3554 case GL_PALETTE8_RGBA4_OES
:
3555 case GL_PALETTE8_RGB5_A1_OES
:
3556 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3557 width
, height
, imageSize
, data
);
3562 if (target
== GL_TEXTURE_2D
||
3563 (ctx
->Extensions
.ARB_texture_cube_map
&&
3564 target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3565 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)) {
3566 /* non-proxy target */
3567 struct gl_texture_object
*texObj
;
3568 struct gl_texture_image
*texImage
;
3570 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3571 internalFormat
, width
, height
, 1, border
, imageSize
);
3573 _mesa_error(ctx
, error
, "glCompressedTexImage2D");
3577 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3579 _mesa_lock_texture(ctx
, texObj
);
3581 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3583 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage2D");
3586 if (texImage
->Data
) {
3587 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3589 ASSERT(texImage
->Data
== NULL
);
3591 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3592 border
, internalFormat
);
3594 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3595 internalFormat
, GL_NONE
, GL_NONE
);
3597 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3598 ctx
->Driver
.CompressedTexImage2D(ctx
, target
, level
,
3599 internalFormat
, width
, height
,
3600 border
, imageSize
, data
,
3603 _mesa_set_fetch_functions(texImage
, 2);
3605 check_gen_mipmap(ctx
, target
, texObj
, level
);
3608 texObj
->_Complete
= GL_FALSE
;
3609 ctx
->NewState
|= _NEW_TEXTURE
;
3612 _mesa_unlock_texture(ctx
, texObj
);
3614 else if (target
== GL_PROXY_TEXTURE_2D
||
3615 (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
&&
3616 ctx
->Extensions
.ARB_texture_cube_map
)) {
3617 /* Proxy texture: check for errors and update proxy state */
3618 GLenum error
= compressed_texture_error_check(ctx
, 2, target
, level
,
3619 internalFormat
, width
, height
, 1, border
, imageSize
);
3621 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3622 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3623 internalFormat
, GL_NONE
, GL_NONE
,
3624 width
, height
, 1, border
);
3627 /* if error, clear all proxy texture image parameters */
3628 struct gl_texture_image
*texImage
;
3629 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3631 clear_teximage_fields(texImage
);
3634 /* store the teximage parameters */
3635 struct gl_texture_object
*texObj
;
3636 struct gl_texture_image
*texImage
;
3638 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3640 _mesa_lock_texture(ctx
, texObj
);
3642 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3643 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
3644 border
, internalFormat
);
3645 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3646 internalFormat
, GL_NONE
, GL_NONE
);
3648 _mesa_unlock_texture(ctx
, texObj
);
3652 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage2D(target)");
3659 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3660 GLenum internalFormat
, GLsizei width
,
3661 GLsizei height
, GLsizei depth
, GLint border
,
3662 GLsizei imageSize
, const GLvoid
*data
)
3664 GET_CURRENT_CONTEXT(ctx
);
3665 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3667 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3668 _mesa_debug(ctx
, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3669 _mesa_lookup_enum_by_nr(target
), level
,
3670 _mesa_lookup_enum_by_nr(internalFormat
),
3671 width
, height
, depth
, border
, imageSize
, data
);
3673 if (target
== GL_TEXTURE_3D
) {
3674 /* non-proxy target */
3675 struct gl_texture_object
*texObj
;
3676 struct gl_texture_image
*texImage
;
3677 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3678 internalFormat
, width
, height
, depth
, border
, imageSize
);
3680 _mesa_error(ctx
, error
, "glCompressedTexImage3D");
3684 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3686 _mesa_lock_texture(ctx
, texObj
);
3688 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3690 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage3D");
3693 if (texImage
->Data
) {
3694 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3696 ASSERT(texImage
->Data
== NULL
);
3698 _mesa_init_teximage_fields(ctx
, target
, texImage
,
3699 width
, height
, depth
,
3700 border
, internalFormat
);
3702 /* Choose actual texture format */
3703 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3704 internalFormat
, GL_NONE
, GL_NONE
);
3706 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3707 ctx
->Driver
.CompressedTexImage3D(ctx
, target
, level
,
3709 width
, height
, depth
,
3710 border
, imageSize
, data
,
3713 _mesa_set_fetch_functions(texImage
, 3);
3715 check_gen_mipmap(ctx
, target
, texObj
, level
);
3718 texObj
->_Complete
= GL_FALSE
;
3719 ctx
->NewState
|= _NEW_TEXTURE
;
3722 _mesa_unlock_texture(ctx
, texObj
);
3724 else if (target
== GL_PROXY_TEXTURE_3D
) {
3725 /* Proxy texture: check for errors and update proxy state */
3726 GLenum error
= compressed_texture_error_check(ctx
, 3, target
, level
,
3727 internalFormat
, width
, height
, depth
, border
, imageSize
);
3729 ASSERT(ctx
->Driver
.TestProxyTexImage
);
3730 error
= !(*ctx
->Driver
.TestProxyTexImage
)(ctx
, target
, level
,
3731 internalFormat
, GL_NONE
, GL_NONE
,
3732 width
, height
, depth
, border
);
3735 /* if error, clear all proxy texture image parameters */
3736 struct gl_texture_image
*texImage
;
3737 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3739 clear_teximage_fields(texImage
);
3742 /* store the teximage parameters */
3743 struct gl_texture_object
*texObj
;
3744 struct gl_texture_image
*texImage
;
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
);
3751 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3752 depth
, border
, internalFormat
);
3753 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
3754 internalFormat
, GL_NONE
, GL_NONE
);
3756 _mesa_unlock_texture(ctx
, texObj
);
3760 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage3D(target)");
3767 * Common helper for glCompressedTexSubImage1/2/3D().
3770 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3771 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3772 GLsizei width
, GLsizei height
, GLsizei depth
,
3773 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3775 struct gl_texture_object
*texObj
;
3776 struct gl_texture_image
*texImage
;
3778 GET_CURRENT_CONTEXT(ctx
);
3779 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3781 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3782 xoffset
, 0, 0, /* pos */
3783 width
, height
, depth
, /* size */
3786 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3790 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3792 _mesa_lock_texture(ctx
, texObj
);
3794 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3797 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3798 format
, texImage
)) {
3799 /* error was recorded */
3801 else if (width
> 0 && height
> 0 && depth
> 0) {
3804 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3805 ctx
->Driver
.CompressedTexSubImage1D(ctx
, target
, level
,
3807 format
, imageSize
, data
,
3812 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3813 ctx
->Driver
.CompressedTexSubImage2D(ctx
, target
, level
,
3816 format
, imageSize
, data
,
3821 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3822 ctx
->Driver
.CompressedTexSubImage3D(ctx
, target
, level
,
3823 xoffset
, yoffset
, zoffset
,
3824 width
, height
, depth
,
3825 format
, imageSize
, data
,
3833 check_gen_mipmap(ctx
, target
, texObj
, level
);
3835 ctx
->NewState
|= _NEW_TEXTURE
;
3838 _mesa_unlock_texture(ctx
, texObj
);
3843 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3844 GLsizei width
, GLenum format
,
3845 GLsizei imageSize
, const GLvoid
*data
)
3847 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3848 format
, imageSize
, data
);
3853 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3854 GLint yoffset
, GLsizei width
, GLsizei height
,
3855 GLenum format
, GLsizei imageSize
,
3858 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3859 width
, height
, 1, format
, imageSize
, data
);
3864 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3865 GLint yoffset
, GLint zoffset
, GLsizei width
,
3866 GLsizei height
, GLsizei depth
, GLenum format
,
3867 GLsizei imageSize
, const GLvoid
*data
)
3869 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3870 width
, height
, depth
, format
, imageSize
, data
);