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"
43 #include "mfeatures.h"
45 #include "texcompress.h"
54 * State changes which we care about for glCopyTex[Sub]Image() calls.
55 * In particular, we care about pixel transfer state and buffer state
56 * (such as glReadBuffer to make sure we read from the right renderbuffer).
58 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
63 * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
67 _mesa_alloc_texmemory(GLsizei bytes
)
69 return _mesa_align_malloc(bytes
, 512);
74 * Free texture memory allocated with _mesa_alloc_texmemory()
77 _mesa_free_texmemory(void *m
)
84 * Compute floor(log_base_2(n)).
114 * Return the simple base format for a given internal texture format.
115 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
117 * \param ctx GL context.
118 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
120 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
121 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
123 * This is the format which is used during texture application (i.e. the
124 * texture format and env mode determine the arithmetic used.
126 * XXX this could be static
129 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
131 switch (internalFormat
) {
146 case GL_LUMINANCE_ALPHA
:
147 case GL_LUMINANCE4_ALPHA4
:
148 case GL_LUMINANCE6_ALPHA2
:
149 case GL_LUMINANCE8_ALPHA8
:
150 case GL_LUMINANCE12_ALPHA4
:
151 case GL_LUMINANCE12_ALPHA12
:
152 case GL_LUMINANCE16_ALPHA16
:
153 return GL_LUMINANCE_ALPHA
;
184 if (ctx
->Extensions
.EXT_texture_format_BGRA8888
) {
185 switch (internalFormat
) {
193 if (ctx
->Extensions
.EXT_paletted_texture
) {
194 switch (internalFormat
) {
196 case GL_COLOR_INDEX1_EXT
:
197 case GL_COLOR_INDEX2_EXT
:
198 case GL_COLOR_INDEX4_EXT
:
199 case GL_COLOR_INDEX8_EXT
:
200 case GL_COLOR_INDEX12_EXT
:
201 case GL_COLOR_INDEX16_EXT
:
202 return GL_COLOR_INDEX
;
208 if (ctx
->Extensions
.ARB_depth_texture
) {
209 switch (internalFormat
) {
210 case GL_DEPTH_COMPONENT
:
211 case GL_DEPTH_COMPONENT16
:
212 case GL_DEPTH_COMPONENT24
:
213 case GL_DEPTH_COMPONENT32
:
214 return GL_DEPTH_COMPONENT
;
220 switch (internalFormat
) {
221 case GL_COMPRESSED_ALPHA
:
223 case GL_COMPRESSED_LUMINANCE
:
225 case GL_COMPRESSED_LUMINANCE_ALPHA
:
226 return GL_LUMINANCE_ALPHA
;
227 case GL_COMPRESSED_INTENSITY
:
229 case GL_COMPRESSED_RGB
:
231 case GL_COMPRESSED_RGBA
:
237 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
238 switch (internalFormat
) {
239 case GL_COMPRESSED_RGB_FXT1_3DFX
:
241 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
248 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
249 switch (internalFormat
) {
250 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
252 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
253 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
254 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
261 if (ctx
->Extensions
.S3_s3tc
) {
262 switch (internalFormat
) {
274 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
275 if (internalFormat
== GL_YCBCR_MESA
)
276 return GL_YCBCR_MESA
;
279 if (ctx
->Extensions
.ARB_texture_float
) {
280 switch (internalFormat
) {
281 case GL_ALPHA16F_ARB
:
282 case GL_ALPHA32F_ARB
:
290 case GL_INTENSITY16F_ARB
:
291 case GL_INTENSITY32F_ARB
:
293 case GL_LUMINANCE16F_ARB
:
294 case GL_LUMINANCE32F_ARB
:
296 case GL_LUMINANCE_ALPHA16F_ARB
:
297 case GL_LUMINANCE_ALPHA32F_ARB
:
298 return GL_LUMINANCE_ALPHA
;
304 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
305 switch (internalFormat
) {
314 if (ctx
->Extensions
.MESA_texture_signed_rgba
) {
315 switch (internalFormat
) {
324 if (ctx
->Extensions
.EXT_packed_depth_stencil
) {
325 switch (internalFormat
) {
326 case GL_DEPTH_STENCIL_EXT
:
327 case GL_DEPTH24_STENCIL8_EXT
:
328 return GL_DEPTH_STENCIL_EXT
;
334 #if FEATURE_EXT_texture_sRGB
335 if (ctx
->Extensions
.EXT_texture_sRGB
) {
336 switch (internalFormat
) {
339 case GL_COMPRESSED_SRGB_EXT
:
340 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
342 case GL_SRGB_ALPHA_EXT
:
343 case GL_SRGB8_ALPHA8_EXT
:
344 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
345 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
346 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
347 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
349 case GL_SLUMINANCE_ALPHA_EXT
:
350 case GL_SLUMINANCE8_ALPHA8_EXT
:
351 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
352 return GL_LUMINANCE_ALPHA
;
353 case GL_SLUMINANCE_EXT
:
354 case GL_SLUMINANCE8_EXT
:
355 case GL_COMPRESSED_SLUMINANCE_EXT
:
361 #endif /* FEATURE_EXT_texture_sRGB */
363 if (ctx
->Extensions
.EXT_texture_integer
) {
364 switch (internalFormat
) {
366 case GL_RGBA16UI_EXT
:
367 case GL_RGBA32UI_EXT
:
379 case GL_ALPHA8UI_EXT
:
380 case GL_ALPHA16UI_EXT
:
381 case GL_ALPHA32UI_EXT
:
383 case GL_ALPHA16I_EXT
:
384 case GL_ALPHA32I_EXT
:
386 case GL_INTENSITY8UI_EXT
:
387 case GL_INTENSITY16UI_EXT
:
388 case GL_INTENSITY32UI_EXT
:
389 case GL_INTENSITY8I_EXT
:
390 case GL_INTENSITY16I_EXT
:
391 case GL_INTENSITY32I_EXT
:
393 case GL_LUMINANCE8UI_EXT
:
394 case GL_LUMINANCE16UI_EXT
:
395 case GL_LUMINANCE32UI_EXT
:
396 case GL_LUMINANCE8I_EXT
:
397 case GL_LUMINANCE16I_EXT
:
398 case GL_LUMINANCE32I_EXT
:
400 case GL_LUMINANCE_ALPHA8UI_EXT
:
401 case GL_LUMINANCE_ALPHA16UI_EXT
:
402 case GL_LUMINANCE_ALPHA32UI_EXT
:
403 case GL_LUMINANCE_ALPHA8I_EXT
:
404 case GL_LUMINANCE_ALPHA16I_EXT
:
405 case GL_LUMINANCE_ALPHA32I_EXT
:
406 return GL_LUMINANCE_ALPHA
;
412 if (ctx
->Extensions
.ARB_texture_rg
) {
413 switch (internalFormat
) {
415 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
417 if (!ctx
->Extensions
.ARB_half_float_pixel
)
421 if (!ctx
->Extensions
.ARB_texture_float
)
430 if (!ctx
->Extensions
.EXT_texture_integer
)
436 case GL_COMPRESSED_RED
:
440 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
442 if (!ctx
->Extensions
.ARB_half_float_pixel
)
446 if (!ctx
->Extensions
.ARB_texture_float
)
455 if (!ctx
->Extensions
.EXT_texture_integer
)
461 case GL_COMPRESSED_RG
:
468 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
469 switch (internalFormat
) {
477 if (ctx
->Extensions
.EXT_packed_float
) {
478 switch (internalFormat
) {
479 case GL_R11F_G11F_B10F_EXT
:
486 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
487 switch (internalFormat
) {
488 case GL_DEPTH_COMPONENT32F
:
489 return GL_DEPTH_COMPONENT
;
490 case GL_DEPTH32F_STENCIL8
:
491 return GL_DEPTH_STENCIL
;
497 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
498 switch (internalFormat
) {
499 case GL_COMPRESSED_RED_RGTC1
:
500 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
502 case GL_COMPRESSED_RG_RGTC2
:
503 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
510 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
511 switch (internalFormat
) {
512 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
513 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
515 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
516 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
517 return GL_LUMINANCE_ALPHA
;
523 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
524 switch (internalFormat
) {
525 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
526 return GL_LUMINANCE_ALPHA
;
532 return -1; /* error */
537 * For cube map faces, return a face index in [0,5].
538 * For other targets return 0;
541 _mesa_tex_target_to_face(GLenum target
)
543 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
544 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)
545 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
553 * Store a gl_texture_image pointer in a gl_texture_object structure
554 * according to the target and level parameters.
556 * \param tObj texture object.
557 * \param target texture target.
558 * \param level image level.
559 * \param texImage texture image.
561 * This was basically prompted by the introduction of cube maps.
564 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
565 GLenum target
, GLint level
,
566 struct gl_texture_image
*texImage
)
568 const GLuint face
= _mesa_tex_target_to_face(target
);
572 ASSERT(target
!= GL_TEXTURE_RECTANGLE_NV
|| level
== 0);
574 tObj
->Image
[face
][level
] = texImage
;
576 /* Set the 'back' pointer */
577 texImage
->TexObject
= tObj
;
582 * Allocate a texture image structure.
584 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
587 * \return a pointer to gl_texture_image struct with all fields initialized to
590 struct gl_texture_image
*
591 _mesa_new_texture_image( struct gl_context
*ctx
)
594 return CALLOC_STRUCT(gl_texture_image
);
599 * Free texture image data.
600 * This function is a fallback called via ctx->Driver.FreeTexImageData().
602 * \param texImage texture image.
604 * Free the texture image data if it's not marked as client data.
607 _mesa_free_texture_image_data(struct gl_context
*ctx
,
608 struct gl_texture_image
*texImage
)
612 if (texImage
->Data
&& !texImage
->IsClientData
) {
613 /* free the old texture data */
614 _mesa_free_texmemory(texImage
->Data
);
617 texImage
->Data
= NULL
;
622 * Free texture image.
624 * \param texImage texture image.
626 * Free the texture image structure and the associated image data.
629 _mesa_delete_texture_image(struct gl_context
*ctx
,
630 struct gl_texture_image
*texImage
)
632 /* Free texImage->Data and/or any other driver-specific texture
635 ASSERT(ctx
->Driver
.FreeTexImageData
);
636 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
638 ASSERT(texImage
->Data
== NULL
);
639 if (texImage
->ImageOffsets
)
640 free(texImage
->ImageOffsets
);
646 * Test if a target is a proxy target.
648 * \param target texture target.
650 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
653 _mesa_is_proxy_texture(GLenum target
)
655 /* NUM_TEXTURE_TARGETS should match number of terms below */
656 assert(NUM_TEXTURE_TARGETS
== 7);
658 return (target
== GL_PROXY_TEXTURE_1D
||
659 target
== GL_PROXY_TEXTURE_2D
||
660 target
== GL_PROXY_TEXTURE_3D
||
661 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
662 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
663 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
664 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
);
669 * Return the proxy target which corresponds to the given texture target
672 get_proxy_target(GLenum target
)
676 case GL_PROXY_TEXTURE_1D
:
677 return GL_PROXY_TEXTURE_1D
;
679 case GL_PROXY_TEXTURE_2D
:
680 return GL_PROXY_TEXTURE_2D
;
682 case GL_PROXY_TEXTURE_3D
:
683 return GL_PROXY_TEXTURE_3D
;
684 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
685 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
686 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
687 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
688 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
689 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
690 case GL_TEXTURE_CUBE_MAP_ARB
:
691 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
692 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
693 case GL_TEXTURE_RECTANGLE_NV
:
694 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
695 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
696 case GL_TEXTURE_1D_ARRAY_EXT
:
697 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
698 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
699 case GL_TEXTURE_2D_ARRAY_EXT
:
700 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
701 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
703 _mesa_problem(NULL
, "unexpected target in get_proxy_target()");
710 * Get the texture object that corresponds to the target of the given
713 * \param ctx GL context.
714 * \param texUnit texture unit.
715 * \param target texture target.
717 * \return pointer to the texture object on success, or NULL on failure.
719 * \sa gl_texture_unit.
721 struct gl_texture_object
*
722 _mesa_select_tex_object(struct gl_context
*ctx
,
723 const struct gl_texture_unit
*texUnit
,
726 const GLboolean arrayTex
= (ctx
->Extensions
.MESA_texture_array
||
727 ctx
->Extensions
.EXT_texture_array
);
731 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
732 case GL_PROXY_TEXTURE_1D
:
733 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
735 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
736 case GL_PROXY_TEXTURE_2D
:
737 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
739 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
740 case GL_PROXY_TEXTURE_3D
:
741 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
742 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
743 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
744 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
745 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
746 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
747 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
748 case GL_TEXTURE_CUBE_MAP_ARB
:
749 return ctx
->Extensions
.ARB_texture_cube_map
750 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
751 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
752 return ctx
->Extensions
.ARB_texture_cube_map
753 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
754 case GL_TEXTURE_RECTANGLE_NV
:
755 return ctx
->Extensions
.NV_texture_rectangle
756 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
757 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
758 return ctx
->Extensions
.NV_texture_rectangle
759 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
760 case GL_TEXTURE_1D_ARRAY_EXT
:
761 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
762 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
763 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
764 case GL_TEXTURE_2D_ARRAY_EXT
:
765 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
766 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
767 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
769 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
776 * Return pointer to texture object for given target on current texture unit.
778 struct gl_texture_object
*
779 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
781 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
782 return _mesa_select_tex_object(ctx
, texUnit
, target
);
787 * Get a texture image pointer from a texture object, given a texture
788 * target and mipmap level. The target and level parameters should
789 * have already been error-checked.
791 * \param ctx GL context.
792 * \param texObj texture unit.
793 * \param target texture target.
794 * \param level image level.
796 * \return pointer to the texture image structure, or NULL on failure.
798 struct gl_texture_image
*
799 _mesa_select_tex_image(struct gl_context
*ctx
,
800 const struct gl_texture_object
*texObj
,
801 GLenum target
, GLint level
)
803 const GLuint face
= _mesa_tex_target_to_face(target
);
807 ASSERT(level
< MAX_TEXTURE_LEVELS
);
809 return texObj
->Image
[face
][level
];
814 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
815 * it and install it. Only return NULL if passed a bad parameter or run
818 struct gl_texture_image
*
819 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
820 GLenum target
, GLint level
)
822 struct gl_texture_image
*texImage
;
827 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
829 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
831 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
835 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
843 * Return pointer to the specified proxy texture image.
844 * Note that proxy textures are per-context, not per-texture unit.
845 * \return pointer to texture image or NULL if invalid target, invalid
846 * level, or out of memory.
848 struct gl_texture_image
*
849 _mesa_get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
851 struct gl_texture_image
*texImage
;
858 case GL_PROXY_TEXTURE_1D
:
859 if (level
>= ctx
->Const
.MaxTextureLevels
)
861 texIndex
= TEXTURE_1D_INDEX
;
863 case GL_PROXY_TEXTURE_2D
:
864 if (level
>= ctx
->Const
.MaxTextureLevels
)
866 texIndex
= TEXTURE_2D_INDEX
;
868 case GL_PROXY_TEXTURE_3D
:
869 if (level
>= ctx
->Const
.Max3DTextureLevels
)
871 texIndex
= TEXTURE_3D_INDEX
;
873 case GL_PROXY_TEXTURE_CUBE_MAP
:
874 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
876 texIndex
= TEXTURE_CUBE_INDEX
;
878 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
881 texIndex
= TEXTURE_RECT_INDEX
;
883 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
884 if (level
>= ctx
->Const
.MaxTextureLevels
)
886 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
888 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
889 if (level
>= ctx
->Const
.MaxTextureLevels
)
891 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
897 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
899 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
901 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
904 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
905 /* Set the 'back' pointer */
906 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
913 * Get the maximum number of allowed mipmap levels.
915 * \param ctx GL context.
916 * \param target texture target.
918 * \return the maximum number of allowed mipmap levels for the given
919 * texture target, or zero if passed a bad target.
924 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
928 case GL_PROXY_TEXTURE_1D
:
930 case GL_PROXY_TEXTURE_2D
:
931 return ctx
->Const
.MaxTextureLevels
;
933 case GL_PROXY_TEXTURE_3D
:
934 return ctx
->Const
.Max3DTextureLevels
;
935 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
936 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
937 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
938 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
939 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
940 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
941 case GL_TEXTURE_CUBE_MAP_ARB
:
942 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
943 return ctx
->Extensions
.ARB_texture_cube_map
944 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
945 case GL_TEXTURE_RECTANGLE_NV
:
946 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
947 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
948 case GL_TEXTURE_1D_ARRAY_EXT
:
949 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
950 case GL_TEXTURE_2D_ARRAY_EXT
:
951 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
952 return (ctx
->Extensions
.MESA_texture_array
||
953 ctx
->Extensions
.EXT_texture_array
)
954 ? ctx
->Const
.MaxTextureLevels
: 0;
956 return 0; /* bad target */
962 * Return number of dimensions per mipmap level for the given texture target.
965 _mesa_get_texture_dimensions(GLenum target
)
969 case GL_PROXY_TEXTURE_1D
:
972 case GL_TEXTURE_RECTANGLE
:
973 case GL_TEXTURE_CUBE_MAP
:
974 case GL_PROXY_TEXTURE_2D
:
975 case GL_PROXY_TEXTURE_RECTANGLE
:
976 case GL_PROXY_TEXTURE_CUBE_MAP
:
977 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
978 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
979 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
980 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
981 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
982 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
983 case GL_TEXTURE_1D_ARRAY
:
984 case GL_PROXY_TEXTURE_1D_ARRAY
:
987 case GL_PROXY_TEXTURE_3D
:
988 case GL_TEXTURE_2D_ARRAY
:
989 case GL_PROXY_TEXTURE_2D_ARRAY
:
992 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1001 #if 000 /* not used anymore */
1003 * glTexImage[123]D can accept a NULL image pointer. In this case we
1004 * create a texture image with unspecified image contents per the OpenGL
1008 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1010 const GLint components
= _mesa_components_in_format(format
);
1011 const GLint numPixels
= width
* height
* depth
;
1012 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
1016 * Let's see if anyone finds this. If glTexImage2D() is called with
1017 * a NULL image pointer then load the texture image with something
1018 * interesting instead of leaving it indeterminate.
1021 static const char message
[8][32] = {
1022 " X X XXXXX XXX X ",
1023 " XX XX X X X X X ",
1025 " X X XXXX XXX XXXXX ",
1028 " X X XXXXX XXX X X ",
1032 GLubyte
*imgPtr
= data
;
1034 for (h
= 0; h
< depth
; h
++) {
1035 for (i
= 0; i
< height
; i
++) {
1036 GLint srcRow
= 7 - (i
% 8);
1037 for (j
= 0; j
< width
; j
++) {
1038 GLint srcCol
= j
% 32;
1039 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1040 for (k
= 0; k
< components
; k
++) {
1056 * Reset the fields of a gl_texture_image struct to zero.
1058 * \param img texture image structure.
1060 * This is called when a proxy texture test fails, we set all the
1061 * image members (except DriverData) to zero.
1062 * It's also used in glTexImage[123]D as a safeguard to be sure all
1063 * required fields get initialized properly by the Driver.TexImage[123]D
1067 clear_teximage_fields(struct gl_texture_image
*img
)
1070 img
->_BaseFormat
= 0;
1071 img
->InternalFormat
= 0;
1077 if (img
->ImageOffsets
) {
1078 free(img
->ImageOffsets
);
1079 img
->ImageOffsets
= NULL
;
1085 img
->HeightLog2
= 0;
1088 img
->TexFormat
= MESA_FORMAT_NONE
;
1089 img
->FetchTexelc
= NULL
;
1090 img
->FetchTexelf
= NULL
;
1095 * Initialize basic fields of the gl_texture_image struct.
1097 * \param ctx GL context.
1098 * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
1099 * \param img texture image structure to be initialized.
1100 * \param width image width.
1101 * \param height image height.
1102 * \param depth image depth.
1103 * \param border image border.
1104 * \param internalFormat internal format.
1105 * \param format the actual hardware format (one of MESA_FORMAT_*)
1107 * Fills in the fields of \p img with the given information.
1108 * Note: width, height and depth include the border.
1111 _mesa_init_teximage_fields(struct gl_context
*ctx
, GLenum target
,
1112 struct gl_texture_image
*img
,
1113 GLsizei width
, GLsizei height
, GLsizei depth
,
1114 GLint border
, GLenum internalFormat
,
1121 ASSERT(height
>= 0);
1124 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1125 ASSERT(img
->_BaseFormat
> 0);
1126 img
->InternalFormat
= internalFormat
;
1127 img
->Border
= border
;
1129 img
->Height
= height
;
1132 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1133 img
->WidthLog2
= logbase2(img
->Width2
);
1135 if (height
== 1) { /* 1-D texture */
1137 img
->HeightLog2
= 0;
1140 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1141 img
->HeightLog2
= logbase2(img
->Height2
);
1144 if (depth
== 1) { /* 2-D texture */
1149 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1150 img
->DepthLog2
= logbase2(img
->Depth2
);
1153 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1155 if ((width
== 1 || _mesa_is_pow_two(img
->Width2
)) &&
1156 (height
== 1 || _mesa_is_pow_two(img
->Height2
)) &&
1157 (depth
== 1 || _mesa_is_pow_two(img
->Depth2
)))
1158 img
->_IsPowerOfTwo
= GL_TRUE
;
1160 img
->_IsPowerOfTwo
= GL_FALSE
;
1162 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
1163 img
->RowStride
= width
;
1164 /* Allocate the ImageOffsets array and initialize to typical values.
1165 * We allocate the array for 1D/2D textures too in order to avoid special-
1166 * case code in the texstore routines.
1168 if (img
->ImageOffsets
)
1169 free(img
->ImageOffsets
);
1170 img
->ImageOffsets
= (GLuint
*) malloc(depth
* sizeof(GLuint
));
1171 for (i
= 0; i
< depth
; i
++) {
1172 img
->ImageOffsets
[i
] = i
* width
* height
;
1175 /* Compute Width/Height/DepthScale for mipmap lod computation */
1176 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1177 /* scale = 1.0 since texture coords directly map to texels */
1178 img
->WidthScale
= 1.0;
1179 img
->HeightScale
= 1.0;
1180 img
->DepthScale
= 1.0;
1183 img
->WidthScale
= (GLfloat
) img
->Width
;
1184 img
->HeightScale
= (GLfloat
) img
->Height
;
1185 img
->DepthScale
= (GLfloat
) img
->Depth
;
1188 img
->TexFormat
= format
;
1190 dims
= _mesa_get_texture_dimensions(target
);
1192 _mesa_set_fetch_functions(img
, dims
);
1197 * Free and clear fields of the gl_texture_image struct.
1199 * \param ctx GL context.
1200 * \param texImage texture image structure to be cleared.
1202 * After the call, \p texImage will have no data associated with it. Its
1203 * fields are cleared so that its parent object will test incomplete.
1206 _mesa_clear_texture_image(struct gl_context
*ctx
,
1207 struct gl_texture_image
*texImage
)
1209 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
1210 clear_teximage_fields(texImage
);
1215 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1216 * level, width, height and depth against the ctx->Const limits for textures.
1218 * A hardware driver might override this function if, for example, the
1219 * max 3D texture size is 512x512x64 (i.e. not a cube).
1221 * Note that width, height, depth == 0 is not an error. However, a
1222 * texture with zero width/height/depth will be considered "incomplete"
1223 * and texturing will effectively be disabled.
1225 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1226 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1227 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1228 * \param level as passed to glTexImage
1229 * \param internalFormat as passed to glTexImage
1230 * \param format as passed to glTexImage
1231 * \param type as passed to glTexImage
1232 * \param width as passed to glTexImage
1233 * \param height as passed to glTexImage
1234 * \param depth as passed to glTexImage
1235 * \param border as passed to glTexImage
1236 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1239 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1240 GLint internalFormat
, GLenum format
, GLenum type
,
1241 GLint width
, GLint height
, GLint depth
, GLint border
)
1245 (void) internalFormat
;
1250 case GL_PROXY_TEXTURE_1D
:
1251 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1252 if (width
< 2 * border
|| width
> 2 + maxSize
)
1254 if (level
>= ctx
->Const
.MaxTextureLevels
)
1256 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1257 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1262 case GL_PROXY_TEXTURE_2D
:
1263 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1264 if (width
< 2 * border
|| width
> 2 + maxSize
)
1266 if (height
< 2 * border
|| height
> 2 + maxSize
)
1268 if (level
>= ctx
->Const
.MaxTextureLevels
)
1270 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1271 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1273 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1278 case GL_PROXY_TEXTURE_3D
:
1279 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1280 if (width
< 2 * border
|| width
> 2 + maxSize
)
1282 if (height
< 2 * border
|| height
> 2 + maxSize
)
1284 if (depth
< 2 * border
|| depth
> 2 + maxSize
)
1286 if (level
>= ctx
->Const
.Max3DTextureLevels
)
1288 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1289 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1291 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1293 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1298 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1299 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1300 if (width
< 0 || width
> maxSize
)
1302 if (height
< 0 || height
> maxSize
)
1308 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1309 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1310 if (width
< 2 * border
|| width
> 2 + maxSize
)
1312 if (height
< 2 * border
|| height
> 2 + maxSize
)
1314 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1316 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1317 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1319 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1324 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1325 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1326 if (width
< 2 * border
|| width
> 2 + maxSize
)
1328 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1330 if (level
>= ctx
->Const
.MaxTextureLevels
)
1332 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1333 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1338 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1339 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1340 if (width
< 2 * border
|| width
> 2 + maxSize
)
1342 if (height
< 2 * border
|| height
> 2 + maxSize
)
1344 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1346 if (level
>= ctx
->Const
.MaxTextureLevels
)
1348 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1349 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1351 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1357 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1364 * Check if the memory used by the texture would exceed the driver's limit.
1365 * This lets us support a max 3D texture size of 8K (for example) but
1366 * prevents allocating a full 8K x 8K x 8K texture.
1367 * XXX this could be rolled into the proxy texture size test (above) but
1368 * we don't have the actual texture internal format at that point.
1371 legal_texture_size(struct gl_context
*ctx
, gl_format format
,
1372 GLint width
, GLint height
, GLint depth
)
1374 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1375 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1376 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1382 * Helper function to determine whether a target and specific compression
1383 * format are supported.
1386 target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1389 (void) intFormat
; /* not used yet */
1393 case GL_PROXY_TEXTURE_2D
:
1394 return GL_TRUE
; /* true for any compressed format so far */
1395 case GL_PROXY_TEXTURE_CUBE_MAP
:
1396 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1397 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1398 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1399 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1400 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1401 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1402 return ctx
->Extensions
.ARB_texture_cube_map
;
1403 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1404 case GL_TEXTURE_2D_ARRAY_EXT
:
1405 return (ctx
->Extensions
.MESA_texture_array
||
1406 ctx
->Extensions
.EXT_texture_array
);
1414 * Check if the given texture target value is legal for a
1415 * glTexImage1/2/3D call.
1418 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1424 case GL_PROXY_TEXTURE_1D
:
1432 case GL_PROXY_TEXTURE_2D
:
1434 case GL_PROXY_TEXTURE_CUBE_MAP
:
1435 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1436 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1437 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1438 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1439 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1440 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1441 return ctx
->Extensions
.ARB_texture_cube_map
;
1442 case GL_TEXTURE_RECTANGLE_NV
:
1443 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1444 return ctx
->Extensions
.NV_texture_rectangle
;
1445 case GL_TEXTURE_1D_ARRAY_EXT
:
1446 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1447 return (ctx
->Extensions
.MESA_texture_array
||
1448 ctx
->Extensions
.EXT_texture_array
);
1455 case GL_PROXY_TEXTURE_3D
:
1457 case GL_TEXTURE_2D_ARRAY_EXT
:
1458 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1459 return (ctx
->Extensions
.MESA_texture_array
||
1460 ctx
->Extensions
.EXT_texture_array
);
1465 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1472 * Check if the given texture target value is legal for a
1473 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1474 * The difference compared to legal_teximage_target() above is that
1475 * proxy targets are not supported.
1478 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1482 return target
== GL_TEXTURE_1D
;
1487 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1488 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1489 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1490 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1491 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1492 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1493 return ctx
->Extensions
.ARB_texture_cube_map
;
1494 case GL_TEXTURE_RECTANGLE_NV
:
1495 return ctx
->Extensions
.NV_texture_rectangle
;
1496 case GL_TEXTURE_1D_ARRAY_EXT
:
1497 return (ctx
->Extensions
.MESA_texture_array
||
1498 ctx
->Extensions
.EXT_texture_array
);
1506 case GL_TEXTURE_2D_ARRAY_EXT
:
1507 return (ctx
->Extensions
.MESA_texture_array
||
1508 ctx
->Extensions
.EXT_texture_array
);
1513 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1521 * Test the glTexImage[123]D() parameters for errors.
1523 * \param ctx GL context.
1524 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1525 * \param target texture target given by the user.
1526 * \param level image level given by the user.
1527 * \param internalFormat internal format given by the user.
1528 * \param format pixel data format given by the user.
1529 * \param type pixel data type given by the user.
1530 * \param width image width given by the user.
1531 * \param height image height given by the user.
1532 * \param depth image depth given by the user.
1533 * \param border image border given by the user.
1535 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1537 * Verifies each of the parameters against the constants specified in
1538 * __struct gl_contextRec::Const and the supported extensions, and according
1539 * to the OpenGL specification.
1542 texture_error_check( struct gl_context
*ctx
,
1543 GLuint dimensions
, GLenum target
,
1544 GLint level
, GLint internalFormat
,
1545 GLenum format
, GLenum type
,
1546 GLint width
, GLint height
,
1547 GLint depth
, GLint border
)
1549 const GLenum proxyTarget
= get_proxy_target(target
);
1550 const GLboolean isProxy
= target
== proxyTarget
;
1551 GLboolean sizeOK
= GL_TRUE
;
1552 GLboolean colorFormat
, indexFormat
;
1554 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1555 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1557 _mesa_error(ctx
, GL_INVALID_VALUE
,
1558 "glTexImage%dD(level=%d)", dimensions
, level
);
1564 if (border
< 0 || border
> 1 ||
1565 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1566 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1568 _mesa_error(ctx
, GL_INVALID_VALUE
,
1569 "glTexImage%dD(border=%d)", dimensions
, border
);
1574 if (width
< 0 || height
< 0 || depth
< 0) {
1576 _mesa_error(ctx
, GL_INVALID_VALUE
,
1577 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1582 /* Do this simple check before calling the TestProxyTexImage() function */
1583 if (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
1584 sizeOK
= (width
== height
);
1588 * Use the proxy texture driver hook to see if the size/level/etc are
1591 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
1592 internalFormat
, format
,
1593 type
, width
, height
,
1597 _mesa_error(ctx
, GL_INVALID_VALUE
,
1598 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1599 dimensions
, level
, width
, height
, depth
);
1604 /* Check internalFormat */
1605 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1607 _mesa_error(ctx
, GL_INVALID_VALUE
,
1608 "glTexImage%dD(internalFormat=%s)",
1609 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1614 /* Check incoming image format and type */
1615 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1616 /* Normally, GL_INVALID_OPERATION is generated by a format/type
1617 * mismatch (see the 1.2 spec page 94, sec 3.6.4.). But with the
1618 * GL_EXT_texture_integer extension, some combinations should generate
1619 * GL_INVALID_ENUM instead (grr!).
1622 GLenum error
= _mesa_is_integer_format(format
)
1623 ? GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
1624 _mesa_error(ctx
, error
,
1625 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1626 dimensions
, format
, type
);
1631 /* make sure internal format and format basically agree */
1632 colorFormat
= _mesa_is_color_format(format
);
1633 indexFormat
= _mesa_is_index_format(format
);
1634 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1635 (_mesa_is_index_format(internalFormat
) && !indexFormat
) ||
1636 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1637 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1638 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1639 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1642 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1643 dimensions
, internalFormat
, format
);
1647 /* additional checks for ycbcr textures */
1648 if (internalFormat
== GL_YCBCR_MESA
) {
1649 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1650 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1651 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1653 _mesa_snprintf(message
, sizeof(message
),
1654 "glTexImage%dD(format/type YCBCR mismatch", dimensions
);
1655 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1656 return GL_TRUE
; /* error */
1658 if (target
!= GL_TEXTURE_2D
&&
1659 target
!= GL_PROXY_TEXTURE_2D
&&
1660 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1661 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1663 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1669 _mesa_snprintf(message
, sizeof(message
),
1670 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1671 dimensions
, border
);
1672 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1678 /* additional checks for depth textures */
1679 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1680 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1681 if (target
!= GL_TEXTURE_1D
&&
1682 target
!= GL_PROXY_TEXTURE_1D
&&
1683 target
!= GL_TEXTURE_2D
&&
1684 target
!= GL_PROXY_TEXTURE_2D
&&
1685 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1686 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
) {
1688 _mesa_error(ctx
, GL_INVALID_ENUM
,
1689 "glTexImage(target/internalFormat)");
1694 /* additional checks for compressed textures */
1695 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1696 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
1698 _mesa_error(ctx
, GL_INVALID_ENUM
,
1699 "glTexImage%dD(target)", dimensions
);
1704 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1705 "glTexImage%dD(border!=0)", dimensions
);
1711 /* additional checks for integer textures */
1712 if (ctx
->Extensions
.EXT_texture_integer
&&
1713 (_mesa_is_integer_format(format
) !=
1714 _mesa_is_integer_format(internalFormat
))) {
1716 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1717 "glTexImage%dD(integer/non-integer format mismatch)",
1723 /* if we get here, the parameters are OK */
1729 * Test glTexSubImage[123]D() parameters for errors.
1731 * \param ctx GL context.
1732 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1733 * \param target texture target given by the user.
1734 * \param level image level given by the user.
1735 * \param xoffset sub-image x offset given by the user.
1736 * \param yoffset sub-image y offset given by the user.
1737 * \param zoffset sub-image z offset given by the user.
1738 * \param format pixel data format given by the user.
1739 * \param type pixel data type given by the user.
1740 * \param width image width given by the user.
1741 * \param height image height given by the user.
1742 * \param depth image depth given by the user.
1744 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1746 * Verifies each of the parameters against the constants specified in
1747 * __struct gl_contextRec::Const and the supported extensions, and according
1748 * to the OpenGL specification.
1751 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1752 GLenum target
, GLint level
,
1753 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1754 GLint width
, GLint height
, GLint depth
,
1755 GLenum format
, GLenum type
)
1757 /* Basic level check */
1758 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1759 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1763 /* Check for negative sizes */
1765 _mesa_error(ctx
, GL_INVALID_VALUE
,
1766 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1769 if (height
< 0 && dimensions
> 1) {
1770 _mesa_error(ctx
, GL_INVALID_VALUE
,
1771 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1774 if (depth
< 0 && dimensions
> 2) {
1775 _mesa_error(ctx
, GL_INVALID_VALUE
,
1776 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1780 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1781 /* As with the glTexImage2D check above, the error code here
1782 * depends on texture integer.
1784 GLenum error
= _mesa_is_integer_format(format
)
1785 ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
1786 _mesa_error(ctx
, error
,
1787 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1788 dimensions
, format
, type
);
1797 * Do second part of glTexSubImage which depends on the destination texture.
1798 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1801 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1802 GLenum target
, GLint level
,
1803 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1804 GLint width
, GLint height
, GLint depth
,
1805 GLenum format
, GLenum type
,
1806 const struct gl_texture_image
*destTex
)
1809 /* undefined image level */
1810 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1814 if (xoffset
< -((GLint
)destTex
->Border
)) {
1815 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1819 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1820 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1824 if (dimensions
> 1) {
1825 if (yoffset
< -((GLint
)destTex
->Border
)) {
1826 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1830 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1831 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1836 if (dimensions
> 2) {
1837 if (zoffset
< -((GLint
)destTex
->Border
)) {
1838 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1841 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1842 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1847 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
1850 /* do tests which depend on compression block size */
1851 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
1853 /* offset must be multiple of block size */
1854 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1855 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1856 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
1857 dimensions
, xoffset
, yoffset
);
1860 /* size must be multiple of bw by bh or equal to whole texture size */
1861 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
1862 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1863 "glTexSubImage%dD(width = %d)", dimensions
, width
);
1866 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
1867 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1868 "glTexSubImage%dD(height = %d)", dimensions
, height
);
1878 * Test glCopyTexImage[12]D() parameters for errors.
1880 * \param ctx GL context.
1881 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1882 * \param target texture target given by the user.
1883 * \param level image level given by the user.
1884 * \param internalFormat internal format given by the user.
1885 * \param width image width given by the user.
1886 * \param height image height given by the user.
1887 * \param border texture border.
1889 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1891 * Verifies each of the parameters against the constants specified in
1892 * __struct gl_contextRec::Const and the supported extensions, and according
1893 * to the OpenGL specification.
1896 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1897 GLenum target
, GLint level
, GLint internalFormat
,
1898 GLint width
, GLint height
, GLint border
)
1900 const GLenum proxyTarget
= get_proxy_target(target
);
1901 const GLenum type
= GL_FLOAT
;
1906 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
1907 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
1908 dimensions
, _mesa_lookup_enum_by_nr(target
));
1912 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1913 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1914 _mesa_error(ctx
, GL_INVALID_VALUE
,
1915 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1919 /* Check that the source buffer is complete */
1920 if (ctx
->ReadBuffer
->Name
) {
1921 if (ctx
->ReadBuffer
->_Status
== 0) {
1922 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1924 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1925 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1926 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1932 if (border
< 0 || border
> 1 ||
1933 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1934 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1938 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1940 _mesa_error(ctx
, GL_INVALID_VALUE
,
1941 "glCopyTexImage%dD(internalFormat)", dimensions
);
1945 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1946 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1947 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
1951 /* Do size, level checking */
1952 sizeOK
= (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
)
1953 ? (width
== height
) : 1;
1955 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
1956 internalFormat
, format
,
1957 type
, width
, height
,
1961 if (dimensions
== 1) {
1962 _mesa_error(ctx
, GL_INVALID_VALUE
,
1963 "glCopyTexImage1D(width=%d)", width
);
1966 ASSERT(dimensions
== 2);
1967 _mesa_error(ctx
, GL_INVALID_VALUE
,
1968 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1973 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1974 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
1975 _mesa_error(ctx
, GL_INVALID_ENUM
,
1976 "glCopyTexImage%dD(target)", dimensions
);
1980 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1981 "glCopyTexImage%dD(border!=0)", dimensions
);
1985 else if (_mesa_is_depth_format(internalFormat
)) {
1986 /* make sure we have depth/stencil buffers */
1987 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
1988 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1989 "glCopyTexImage%dD(no depth)", dimensions
);
1993 else if (_mesa_is_depthstencil_format(internalFormat
)) {
1994 /* make sure we have depth/stencil buffers */
1995 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
1996 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1997 "glCopyTexImage%dD(no depth/stencil buffer)", dimensions
);
2002 /* if we get here, the parameters are OK */
2008 * Test glCopyTexSubImage[12]D() parameters for errors.
2009 * Note that this is the first part of error checking.
2010 * See also copytexsubimage_error_check2() below for the second part.
2012 * \param ctx GL context.
2013 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2014 * \param target texture target given by the user.
2015 * \param level image level given by the user.
2017 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2020 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
2021 GLenum target
, GLint level
)
2023 /* Check that the source buffer is complete */
2024 if (ctx
->ReadBuffer
->Name
) {
2025 if (ctx
->ReadBuffer
->_Status
== 0) {
2026 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2028 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2029 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2030 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2035 /* check target (proxies not allowed) */
2036 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2037 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2038 dimensions
, _mesa_lookup_enum_by_nr(target
));
2043 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
2044 _mesa_error(ctx
, GL_INVALID_VALUE
,
2045 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2054 * Second part of error checking for glCopyTexSubImage[12]D().
2055 * \param xoffset sub-image x offset given by the user.
2056 * \param yoffset sub-image y offset given by the user.
2057 * \param zoffset sub-image z offset given by the user.
2058 * \param width image width given by the user.
2059 * \param height image height given by the user.
2062 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
2063 GLenum target
, GLint level
,
2064 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2065 GLsizei width
, GLsizei height
,
2066 const struct gl_texture_image
*teximage
)
2068 /* check that dest tex image exists */
2070 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2071 "glCopyTexSubImage%dD(undefined texture level: %d)",
2078 _mesa_error(ctx
, GL_INVALID_VALUE
,
2079 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
2082 if (dimensions
> 1 && height
< 0) {
2083 _mesa_error(ctx
, GL_INVALID_VALUE
,
2084 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
2088 /* check x/y offsets */
2089 if (xoffset
< -((GLint
)teximage
->Border
)) {
2090 _mesa_error(ctx
, GL_INVALID_VALUE
,
2091 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
2094 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
2095 _mesa_error(ctx
, GL_INVALID_VALUE
,
2096 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
2099 if (dimensions
> 1) {
2100 if (yoffset
< -((GLint
)teximage
->Border
)) {
2101 _mesa_error(ctx
, GL_INVALID_VALUE
,
2102 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2105 /* NOTE: we're adding the border here, not subtracting! */
2106 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
2107 _mesa_error(ctx
, GL_INVALID_VALUE
,
2108 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2113 /* check z offset */
2114 if (dimensions
> 2) {
2115 if (zoffset
< -((GLint
)teximage
->Border
)) {
2116 _mesa_error(ctx
, GL_INVALID_VALUE
,
2117 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2120 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
2121 _mesa_error(ctx
, GL_INVALID_VALUE
,
2122 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2127 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2128 /* offset must be multiple of 4 */
2129 if ((xoffset
& 3) || (yoffset
& 3)) {
2130 _mesa_error(ctx
, GL_INVALID_VALUE
,
2131 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2134 /* size must be multiple of 4 */
2135 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2136 _mesa_error(ctx
, GL_INVALID_VALUE
,
2137 "glCopyTexSubImage%dD(width)", dimensions
);
2140 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2141 _mesa_error(ctx
, GL_INVALID_VALUE
,
2142 "glCopyTexSubImage%dD(height)", dimensions
);
2147 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2148 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2152 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2153 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2154 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2155 dimensions
, teximage
->_BaseFormat
);
2159 if (teximage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2160 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
2161 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2162 "glCopyTexSubImage%dD(no depth buffer)",
2167 else if (teximage
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
2168 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
2169 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2170 "glCopyTexSubImage%dD(no depth/stencil buffer)",
2176 /* If copying into an integer texture, the source buffer must also be
2179 if (_mesa_is_format_integer_color(teximage
->TexFormat
)) {
2180 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2181 if (!_mesa_is_format_integer_color(rb
->Format
)) {
2182 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2183 "glCopyTexSubImage%dD(source buffer is not integer format)",
2189 /* if we get here, the parameters are OK */
2194 /** Callback info for walking over FBO hash table */
2197 struct gl_context
*ctx
;
2198 struct gl_texture_object
*texObj
;
2204 * Check render to texture callback. Called from _mesa_HashWalk().
2207 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2209 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2210 const struct cb_info
*info
= (struct cb_info
*) userData
;
2211 struct gl_context
*ctx
= info
->ctx
;
2212 const struct gl_texture_object
*texObj
= info
->texObj
;
2213 const GLuint level
= info
->level
, face
= info
->face
;
2215 /* If this is a user-created FBO */
2218 /* check if any of the FBO's attachments point to 'texObj' */
2219 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2220 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2221 if (att
->Type
== GL_TEXTURE
&&
2222 att
->Texture
== texObj
&&
2223 att
->TextureLevel
== level
&&
2224 att
->CubeMapFace
== face
) {
2225 ASSERT(att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]);
2226 /* Tell driver about the new renderbuffer texture */
2227 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2228 /* Mark fb status as indeterminate to force re-validation */
2237 * When a texture image is specified we have to check if it's bound to
2238 * any framebuffer objects (render to texture) in order to detect changes
2239 * in size or format since that effects FBO completeness.
2240 * Any FBOs rendering into the texture must be re-validated.
2243 update_fbo_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
2244 GLuint face
, GLuint level
)
2246 /* Only check this texture if it's been marked as RenderToTexture */
2247 if (texObj
->_RenderToTexture
) {
2248 struct cb_info info
;
2250 info
.texObj
= texObj
;
2253 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2259 * If the texture object's GenerateMipmap flag is set and we've
2260 * changed the texture base level image, regenerate the rest of the
2261 * mipmap levels now.
2264 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2265 struct gl_texture_object
*texObj
, GLint level
)
2267 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2268 if (texObj
->GenerateMipmap
&&
2269 level
== texObj
->BaseLevel
&&
2270 level
< texObj
->MaxLevel
) {
2271 ASSERT(ctx
->Driver
.GenerateMipmap
);
2272 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2277 /** Debug helper: override the user-requested internal format */
2279 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2282 if (internalFormat
== GL_RGBA16F_ARB
||
2283 internalFormat
== GL_RGBA32F_ARB
) {
2284 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2287 else if (internalFormat
== GL_RGB16F_ARB
||
2288 internalFormat
== GL_RGB32F_ARB
) {
2289 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2292 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2293 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2294 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2295 return GL_LUMINANCE_ALPHA
;
2297 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2298 internalFormat
== GL_LUMINANCE32F_ARB
) {
2299 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2300 return GL_LUMINANCE
;
2302 else if (internalFormat
== GL_ALPHA16F_ARB
||
2303 internalFormat
== GL_ALPHA32F_ARB
) {
2304 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2308 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2309 internalFormat = GL_RGBA;
2313 return internalFormat
;
2316 return internalFormat
;
2322 * Choose the actual hardware format for a texture image.
2323 * Try to use the same format as the previous image level when possible.
2324 * Otherwise, ask the driver for the best format.
2325 * It's important to try to choose a consistant format for all levels
2326 * for efficient texture memory layout/allocation. In particular, this
2327 * comes up during automatic mipmap generation.
2330 _mesa_choose_texture_format(struct gl_context
*ctx
,
2331 struct gl_texture_object
*texObj
,
2332 GLenum target
, GLint level
,
2333 GLenum internalFormat
, GLenum format
, GLenum type
)
2337 /* see if we've already chosen a format for the previous level */
2339 struct gl_texture_image
*prevImage
=
2340 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2341 /* See if the prev level is defined and has an internal format which
2342 * matches the new internal format.
2345 prevImage
->Width
> 0 &&
2346 prevImage
->InternalFormat
== internalFormat
) {
2347 /* use the same format */
2348 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2349 return prevImage
->TexFormat
;
2353 /* choose format from scratch */
2354 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
, format
, type
);
2355 ASSERT(f
!= MESA_FORMAT_NONE
);
2361 * Common code to implement all the glTexImage1D/2D/3D functions.
2364 teximage(struct gl_context
*ctx
, GLuint dims
,
2365 GLenum target
, GLint level
, GLint internalFormat
,
2366 GLsizei width
, GLsizei height
, GLsizei depth
,
2367 GLint border
, GLenum format
, GLenum type
,
2368 const GLvoid
*pixels
)
2372 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2374 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2375 _mesa_debug(ctx
, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2377 _mesa_lookup_enum_by_nr(target
), level
,
2378 _mesa_lookup_enum_by_nr(internalFormat
),
2379 width
, height
, depth
, border
,
2380 _mesa_lookup_enum_by_nr(format
),
2381 _mesa_lookup_enum_by_nr(type
), pixels
);
2383 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2385 /* target error checking */
2386 if (!legal_teximage_target(ctx
, dims
, target
)) {
2387 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage%uD(target=%s)",
2388 dims
, _mesa_lookup_enum_by_nr(target
));
2392 /* general error checking */
2393 error
= texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2394 format
, type
, width
, height
, depth
, border
);
2396 if (_mesa_is_proxy_texture(target
)) {
2397 /* Proxy texture: just clear or set state depending on error checking */
2398 struct gl_texture_image
*texImage
=
2399 _mesa_get_proxy_tex_image(ctx
, target
, level
);
2402 /* when error, clear all proxy texture image parameters */
2404 clear_teximage_fields(texImage
);
2407 /* no error, set the tex image parameters */
2408 struct gl_texture_object
*texObj
=
2409 _mesa_get_current_tex_object(ctx
, target
);
2410 gl_format texFormat
= _mesa_choose_texture_format(ctx
, texObj
,
2415 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
2416 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
2417 depth
, border
, internalFormat
,
2420 else if (texImage
) {
2421 clear_teximage_fields(texImage
);
2426 /* non-proxy target */
2427 const GLuint face
= _mesa_tex_target_to_face(target
);
2428 struct gl_texture_object
*texObj
;
2429 struct gl_texture_image
*texImage
;
2432 return; /* error was recorded */
2435 if (ctx
->NewState
& _NEW_PIXEL
)
2436 _mesa_update_state(ctx
);
2438 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2440 _mesa_lock_texture(ctx
, texObj
);
2442 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2445 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
2448 gl_format texFormat
;
2450 if (texImage
->Data
) {
2451 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2454 ASSERT(texImage
->Data
== NULL
);
2455 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2456 internalFormat
, format
,
2459 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
2460 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2461 width
, height
, depth
,
2462 border
, internalFormat
, texFormat
);
2464 /* Give the texture to the driver. <pixels> may be null. */
2465 ASSERT(ctx
->Driver
.TexImage3D
);
2468 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2469 width
, border
, format
,
2470 type
, pixels
, &ctx
->Unpack
, texObj
,
2474 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2475 width
, height
, border
, format
,
2476 type
, pixels
, &ctx
->Unpack
, texObj
,
2480 ctx
->Driver
.TexImage3D(ctx
, target
, level
, internalFormat
,
2481 width
, height
, depth
, border
, format
,
2482 type
, pixels
, &ctx
->Unpack
, texObj
,
2486 _mesa_problem(ctx
, "invalid dims=%u in teximage()", dims
);
2489 check_gen_mipmap(ctx
, target
, texObj
, level
);
2491 update_fbo_texture(ctx
, texObj
, face
, level
);
2494 texObj
->_Complete
= GL_FALSE
;
2495 ctx
->NewState
|= _NEW_TEXTURE
;
2498 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
2502 _mesa_unlock_texture(ctx
, texObj
);
2508 * Called from the API. Note that width includes the border.
2511 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2512 GLsizei width
, GLint border
, GLenum format
,
2513 GLenum type
, const GLvoid
*pixels
)
2515 GET_CURRENT_CONTEXT(ctx
);
2516 teximage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1,
2517 border
, format
, type
, pixels
);
2522 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2523 GLsizei width
, GLsizei height
, GLint border
,
2524 GLenum format
, GLenum type
,
2525 const GLvoid
*pixels
)
2527 GET_CURRENT_CONTEXT(ctx
);
2528 teximage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1,
2529 border
, format
, type
, pixels
);
2534 * Called by the API or display list executor.
2535 * Note that width and height include the border.
2538 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2539 GLsizei width
, GLsizei height
, GLsizei depth
,
2540 GLint border
, GLenum format
, GLenum type
,
2541 const GLvoid
*pixels
)
2543 GET_CURRENT_CONTEXT(ctx
);
2544 teximage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
2545 border
, format
, type
, pixels
);
2550 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2551 GLsizei width
, GLsizei height
, GLsizei depth
,
2552 GLint border
, GLenum format
, GLenum type
,
2553 const GLvoid
*pixels
)
2555 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2556 depth
, border
, format
, type
, pixels
);
2560 #if FEATURE_OES_EGL_image
2562 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
2564 struct gl_texture_object
*texObj
;
2565 struct gl_texture_image
*texImage
;
2566 GET_CURRENT_CONTEXT(ctx
);
2567 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2569 if (!ctx
->Extensions
.OES_EGL_image
) {
2570 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2571 "glEGLImageTargetTexture2DOES(unsupported)");
2575 if (target
!= GL_TEXTURE_2D
) {
2576 _mesa_error(ctx
, GL_INVALID_ENUM
,
2577 "glEGLImageTargetTexture2D(target=%d)", target
);
2581 if (ctx
->NewState
& _NEW_PIXEL
)
2582 _mesa_update_state(ctx
);
2584 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2585 _mesa_lock_texture(ctx
, texObj
);
2587 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
2589 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
2592 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2594 ASSERT(texImage
->Data
== NULL
);
2595 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
2596 texObj
, texImage
, image
);
2599 texObj
->_Complete
= GL_FALSE
;
2600 ctx
->NewState
|= _NEW_TEXTURE
;
2602 _mesa_unlock_texture(ctx
, texObj
);
2610 * Implement all the glTexSubImage1/2/3D() functions.
2613 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2614 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2615 GLsizei width
, GLsizei height
, GLsizei depth
,
2616 GLenum format
, GLenum type
, const GLvoid
*pixels
)
2618 struct gl_texture_object
*texObj
;
2619 struct gl_texture_image
*texImage
;
2621 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2623 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2624 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
2626 _mesa_lookup_enum_by_nr(target
), level
,
2627 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2628 _mesa_lookup_enum_by_nr(format
),
2629 _mesa_lookup_enum_by_nr(type
), pixels
);
2631 /* check target (proxies not allowed) */
2632 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
2633 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
2634 dims
, _mesa_lookup_enum_by_nr(target
));
2638 if (ctx
->NewState
& _NEW_PIXEL
)
2639 _mesa_update_state(ctx
);
2641 if (subtexture_error_check(ctx
, dims
, target
, level
, xoffset
, yoffset
, zoffset
,
2642 width
, height
, depth
, format
, type
)) {
2643 return; /* error was detected */
2646 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2648 _mesa_lock_texture(ctx
, texObj
);
2650 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2652 if (subtexture_error_check2(ctx
, dims
, target
, level
,
2653 xoffset
, yoffset
, zoffset
,
2654 width
, height
, depth
,
2655 format
, type
, texImage
)) {
2656 /* error was recorded */
2658 else if (width
> 0 && height
> 0 && height
> 0) {
2659 /* If we have a border, offset=-1 is legal. Bias by border width. */
2662 zoffset
+= texImage
->Border
;
2665 yoffset
+= texImage
->Border
;
2668 xoffset
+= texImage
->Border
;
2673 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
,
2675 format
, type
, pixels
,
2676 &ctx
->Unpack
, texObj
, texImage
);
2679 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
,
2680 xoffset
, yoffset
, width
, height
,
2681 format
, type
, pixels
,
2682 &ctx
->Unpack
, texObj
, texImage
);
2685 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
,
2686 xoffset
, yoffset
, zoffset
,
2687 width
, height
, depth
,
2688 format
, type
, pixels
,
2689 &ctx
->Unpack
, texObj
, texImage
);
2692 _mesa_problem(ctx
, "unexpected dims in subteximage()");
2695 check_gen_mipmap(ctx
, target
, texObj
, level
);
2697 ctx
->NewState
|= _NEW_TEXTURE
;
2700 _mesa_unlock_texture(ctx
, texObj
);
2705 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2706 GLint xoffset
, GLsizei width
,
2707 GLenum format
, GLenum type
,
2708 const GLvoid
*pixels
)
2710 GET_CURRENT_CONTEXT(ctx
);
2711 texsubimage(ctx
, 1, target
, level
,
2714 format
, type
, pixels
);
2719 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2720 GLint xoffset
, GLint yoffset
,
2721 GLsizei width
, GLsizei height
,
2722 GLenum format
, GLenum type
,
2723 const GLvoid
*pixels
)
2725 GET_CURRENT_CONTEXT(ctx
);
2726 texsubimage(ctx
, 2, target
, level
,
2727 xoffset
, yoffset
, 0,
2729 format
, type
, pixels
);
2735 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2736 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2737 GLsizei width
, GLsizei height
, GLsizei depth
,
2738 GLenum format
, GLenum type
,
2739 const GLvoid
*pixels
)
2741 GET_CURRENT_CONTEXT(ctx
);
2742 texsubimage(ctx
, 3, target
, level
,
2743 xoffset
, yoffset
, zoffset
,
2744 width
, height
, depth
,
2745 format
, type
, pixels
);
2751 * Implement the glCopyTexImage1/2D() functions.
2754 copyteximage(struct gl_context
*ctx
, GLuint dims
,
2755 GLenum target
, GLint level
, GLenum internalFormat
,
2756 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
2758 struct gl_texture_object
*texObj
;
2759 struct gl_texture_image
*texImage
;
2760 const GLuint face
= _mesa_tex_target_to_face(target
);
2762 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2764 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2765 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
2767 _mesa_lookup_enum_by_nr(target
), level
,
2768 _mesa_lookup_enum_by_nr(internalFormat
),
2769 x
, y
, width
, height
, border
);
2771 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2772 _mesa_update_state(ctx
);
2774 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2775 width
, height
, border
))
2778 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2780 _mesa_lock_texture(ctx
, texObj
);
2782 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2785 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2788 gl_format texFormat
;
2790 if (texImage
->Data
) {
2791 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2794 ASSERT(texImage
->Data
== NULL
);
2796 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2797 internalFormat
, GL_NONE
,
2800 if (legal_texture_size(ctx
, texFormat
, width
, height
, 1)) {
2801 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2802 border
, internalFormat
, texFormat
);
2804 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2806 ctx
->Driver
.CopyTexImage1D(ctx
, target
, level
, internalFormat
,
2807 x
, y
, width
, border
);
2809 ctx
->Driver
.CopyTexImage2D(ctx
, target
, level
, internalFormat
,
2810 x
, y
, width
, height
, border
);
2812 check_gen_mipmap(ctx
, target
, texObj
, level
);
2814 update_fbo_texture(ctx
, texObj
, face
, level
);
2817 texObj
->_Complete
= GL_FALSE
;
2818 ctx
->NewState
|= _NEW_TEXTURE
;
2821 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2825 _mesa_unlock_texture(ctx
, texObj
);
2831 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2832 GLenum internalFormat
,
2834 GLsizei width
, GLint border
)
2836 GET_CURRENT_CONTEXT(ctx
);
2837 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
2843 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2844 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2847 GET_CURRENT_CONTEXT(ctx
);
2848 copyteximage(ctx
, 2, target
, level
, internalFormat
,
2849 x
, y
, width
, height
, border
);
2855 * Implementation for glCopyTexSubImage1/2/3D() functions.
2858 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2859 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2860 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2862 struct gl_texture_object
*texObj
;
2863 struct gl_texture_image
*texImage
;
2865 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2867 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2868 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
2870 _mesa_lookup_enum_by_nr(target
),
2871 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
2873 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2874 _mesa_update_state(ctx
);
2876 if (copytexsubimage_error_check1(ctx
, dims
, target
, level
))
2879 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2881 _mesa_lock_texture(ctx
, texObj
);
2883 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2885 if (copytexsubimage_error_check2(ctx
, dims
, target
, level
, xoffset
, yoffset
,
2886 zoffset
, width
, height
, texImage
)) {
2887 /* error was recored */
2890 /* If we have a border, offset=-1 is legal. Bias by border width. */
2893 zoffset
+= texImage
->Border
;
2896 yoffset
+= texImage
->Border
;
2899 xoffset
+= texImage
->Border
;
2902 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
2906 ctx
->Driver
.CopyTexSubImage1D(ctx
, target
, level
,
2907 xoffset
, x
, y
, width
);
2910 ctx
->Driver
.CopyTexSubImage2D(ctx
, target
, level
,
2912 x
, y
, width
, height
);
2915 ctx
->Driver
.CopyTexSubImage3D(ctx
, target
, level
,
2916 xoffset
, yoffset
, zoffset
,
2917 x
, y
, width
, height
);
2920 _mesa_problem(ctx
, "bad dims in copytexsubimage()");
2923 check_gen_mipmap(ctx
, target
, texObj
, level
);
2925 ctx
->NewState
|= _NEW_TEXTURE
;
2929 _mesa_unlock_texture(ctx
, texObj
);
2934 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2935 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2937 GET_CURRENT_CONTEXT(ctx
);
2938 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
2944 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2945 GLint xoffset
, GLint yoffset
,
2946 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2948 GET_CURRENT_CONTEXT(ctx
);
2949 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
2956 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2957 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2958 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2960 GET_CURRENT_CONTEXT(ctx
);
2961 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2962 x
, y
, width
, height
);
2968 /**********************************************************************/
2969 /****** Compressed Textures ******/
2970 /**********************************************************************/
2974 * Return expected size of a compressed texture.
2977 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
2980 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
2981 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
2986 * Return compressed texture block size, in pixels.
2989 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
2991 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
2992 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
2997 * Error checking for glCompressedTexImage[123]D().
2998 * \return error code or GL_NO_ERROR.
3001 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3002 GLenum target
, GLint level
,
3003 GLenum internalFormat
, GLsizei width
,
3004 GLsizei height
, GLsizei depth
, GLint border
,
3007 const GLenum proxyTarget
= get_proxy_target(target
);
3008 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3012 if (level
< 0 || level
>= maxLevels
)
3013 return GL_INVALID_VALUE
;
3015 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
3016 return GL_INVALID_ENUM
;
3019 /* This will detect any invalid internalFormat value */
3020 if (!_mesa_is_compressed_format(ctx
, internalFormat
))
3021 return GL_INVALID_ENUM
;
3023 /* This should really never fail */
3024 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
3025 return GL_INVALID_ENUM
;
3027 /* No compressed formats support borders at this time */
3029 return GL_INVALID_VALUE
;
3031 /* For cube map, width must equal height */
3032 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3033 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
3034 return GL_INVALID_VALUE
;
3036 /* check image size against compression block size */
3038 gl_format texFormat
=
3039 ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
,
3043 _mesa_get_format_block_size(texFormat
, &bw
, &bh
);
3044 if ((width
> bw
&& width
% bw
> 0) ||
3045 (height
> bh
&& height
% bh
> 0)) {
3047 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
3048 * generated [...] if any parameter combinations are not
3049 * supported by the specific compressed internal format.
3051 return GL_INVALID_OPERATION
;
3055 /* check image sizes */
3056 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
3057 internalFormat
, GL_NONE
, GL_NONE
,
3058 width
, height
, depth
, border
)) {
3059 /* See error comment above */
3060 return GL_INVALID_OPERATION
;
3063 /* check image size in bytes */
3064 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
3065 if (expectedSize
!= imageSize
) {
3066 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
3067 * if <imageSize> is not consistent with the format, dimensions, and
3068 * contents of the specified image.
3070 return GL_INVALID_VALUE
;
3078 * Error checking for glCompressedTexSubImage[123]D().
3079 * \warning There are some bad assumptions here about the size of compressed
3080 * texture tiles (multiple of 4) used to test the validity of the
3081 * offset and size parameters.
3082 * \return error code or GL_NO_ERROR.
3085 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3086 GLenum target
, GLint level
,
3087 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3088 GLsizei width
, GLsizei height
, GLsizei depth
,
3089 GLenum format
, GLsizei imageSize
)
3091 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3095 if (dimensions
== 1) {
3096 /* 1D compressed textures not allowed */
3097 return GL_INVALID_ENUM
;
3099 else if (dimensions
== 2) {
3100 if (target
== GL_PROXY_TEXTURE_2D
) {
3101 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3103 else if (target
== GL_TEXTURE_2D
) {
3104 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3106 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3107 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3108 return GL_INVALID_ENUM
; /*target*/
3109 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3111 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3112 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3113 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3114 return GL_INVALID_ENUM
; /*target*/
3115 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3118 return GL_INVALID_ENUM
; /*target*/
3121 else if (dimensions
== 3) {
3122 /* 3D compressed textures not allowed */
3123 return GL_INVALID_ENUM
;
3126 maxTextureSize
= 1 << (maxLevels
- 1);
3128 /* this will catch any invalid compressed format token */
3129 if (!_mesa_is_compressed_format(ctx
, format
))
3130 return GL_INVALID_ENUM
;
3132 if (width
< 1 || width
> maxTextureSize
)
3133 return GL_INVALID_VALUE
;
3135 if ((height
< 1 || height
> maxTextureSize
)
3137 return GL_INVALID_VALUE
;
3139 if (level
< 0 || level
>= maxLevels
)
3140 return GL_INVALID_VALUE
;
3143 * do checks which depend on compression block size
3145 get_compressed_block_size(format
, &bw
, &bh
);
3147 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3148 return GL_INVALID_VALUE
;
3150 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3151 return GL_INVALID_VALUE
;
3153 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3154 return GL_INVALID_VALUE
;
3156 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3157 if (expectedSize
!= imageSize
)
3158 return GL_INVALID_VALUE
;
3165 * Do second part of glCompressedTexSubImage error checking.
3166 * \return GL_TRUE if error found, GL_FALSE otherwise.
3169 compressed_subtexture_error_check2(struct gl_context
*ctx
, GLuint dims
,
3170 GLsizei width
, GLsizei height
,
3171 GLsizei depth
, GLenum format
,
3172 struct gl_texture_image
*texImage
)
3175 if ((GLint
) format
!= texImage
->InternalFormat
) {
3176 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3177 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3181 if (((width
== 1 || width
== 2) &&
3182 width
!= (GLsizei
) texImage
->Width
) ||
3183 (width
> (GLsizei
) texImage
->Width
)) {
3184 _mesa_error(ctx
, GL_INVALID_VALUE
,
3185 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3190 if (((height
== 1 || height
== 2) &&
3191 height
!= (GLsizei
) texImage
->Height
) ||
3192 (height
> (GLsizei
) texImage
->Height
)) {
3193 _mesa_error(ctx
, GL_INVALID_VALUE
,
3194 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3200 if (((depth
== 1 || depth
== 2) &&
3201 depth
!= (GLsizei
) texImage
->Depth
) ||
3202 (depth
> (GLsizei
) texImage
->Depth
)) {
3203 _mesa_error(ctx
, GL_INVALID_VALUE
,
3204 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3214 * Implementation of the glCompressedTexImage1/2/3D() functions.
3217 compressedteximage(struct gl_context
*ctx
, GLuint dims
,
3218 GLenum target
, GLint level
,
3219 GLenum internalFormat
, GLsizei width
,
3220 GLsizei height
, GLsizei depth
, GLint border
,
3221 GLsizei imageSize
, const GLvoid
*data
)
3225 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3227 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3229 "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3231 _mesa_lookup_enum_by_nr(target
), level
,
3232 _mesa_lookup_enum_by_nr(internalFormat
),
3233 width
, height
, depth
, border
, imageSize
, data
);
3236 if (!legal_teximage_target(ctx
, dims
, target
)) {
3237 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(target=%s)",
3238 dims
, _mesa_lookup_enum_by_nr(target
));
3242 error
= compressed_texture_error_check(ctx
, dims
, target
, level
,
3243 internalFormat
, width
, height
, depth
,
3247 /* XXX this is kind of a hack */
3249 _mesa_error(ctx
, error
, "glTexImage2D");
3254 switch (internalFormat
) {
3255 case GL_PALETTE4_RGB8_OES
:
3256 case GL_PALETTE4_RGBA8_OES
:
3257 case GL_PALETTE4_R5_G6_B5_OES
:
3258 case GL_PALETTE4_RGBA4_OES
:
3259 case GL_PALETTE4_RGB5_A1_OES
:
3260 case GL_PALETTE8_RGB8_OES
:
3261 case GL_PALETTE8_RGBA8_OES
:
3262 case GL_PALETTE8_R5_G6_B5_OES
:
3263 case GL_PALETTE8_RGBA4_OES
:
3264 case GL_PALETTE8_RGB5_A1_OES
:
3265 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3266 width
, height
, imageSize
, data
);
3272 if (_mesa_is_proxy_texture(target
)) {
3273 /* Proxy texture: just check for errors and update proxy state */
3274 struct gl_texture_image
*texImage
;
3277 struct gl_texture_object
*texObj
=
3278 _mesa_get_current_tex_object(ctx
, target
);
3279 gl_format texFormat
=
3280 _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3281 internalFormat
, GL_NONE
, GL_NONE
);
3282 if (!legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
3283 error
= GL_OUT_OF_MEMORY
;
3287 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3290 /* if error, clear all proxy texture image parameters */
3291 clear_teximage_fields(texImage
);
3294 /* no error: store the teximage parameters */
3295 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3296 depth
, border
, internalFormat
,
3302 /* non-proxy target */
3303 struct gl_texture_object
*texObj
;
3304 struct gl_texture_image
*texImage
;
3307 _mesa_error(ctx
, error
, "glCompressedTexImage%uD", dims
);
3311 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3313 _mesa_lock_texture(ctx
, texObj
);
3315 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3317 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3318 "glCompressedTexImage%uD", dims
);
3321 gl_format texFormat
;
3323 if (texImage
->Data
) {
3324 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3326 ASSERT(texImage
->Data
== NULL
);
3328 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3329 internalFormat
, GL_NONE
,
3332 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
3333 _mesa_init_teximage_fields(ctx
, target
, texImage
,
3334 width
, height
, depth
,
3335 border
, internalFormat
, texFormat
);
3339 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
3340 ctx
->Driver
.CompressedTexImage1D(ctx
, target
, level
,
3343 border
, imageSize
, data
,
3347 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3348 ctx
->Driver
.CompressedTexImage2D(ctx
, target
, level
,
3351 border
, imageSize
, data
,
3355 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3356 ctx
->Driver
.CompressedTexImage3D(ctx
, target
, level
,
3358 width
, height
, depth
,
3359 border
, imageSize
, data
,
3363 _mesa_problem(ctx
, "bad dims in compressedteximage");
3366 check_gen_mipmap(ctx
, target
, texObj
, level
);
3369 texObj
->_Complete
= GL_FALSE
;
3370 ctx
->NewState
|= _NEW_TEXTURE
;
3373 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3374 "glCompressedTexImage%uD", dims
);
3378 _mesa_unlock_texture(ctx
, texObj
);
3384 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3385 GLenum internalFormat
, GLsizei width
,
3386 GLint border
, GLsizei imageSize
,
3389 GET_CURRENT_CONTEXT(ctx
);
3390 compressedteximage(ctx
, 1, target
, level
, internalFormat
,
3391 width
, 1, 1, border
, imageSize
, data
);
3396 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3397 GLenum internalFormat
, GLsizei width
,
3398 GLsizei height
, GLint border
, GLsizei imageSize
,
3401 GET_CURRENT_CONTEXT(ctx
);
3402 compressedteximage(ctx
, 2, target
, level
, internalFormat
,
3403 width
, height
, 1, border
, imageSize
, data
);
3408 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3409 GLenum internalFormat
, GLsizei width
,
3410 GLsizei height
, GLsizei depth
, GLint border
,
3411 GLsizei imageSize
, const GLvoid
*data
)
3413 GET_CURRENT_CONTEXT(ctx
);
3414 compressedteximage(ctx
, 3, target
, level
, internalFormat
,
3415 width
, height
, depth
, border
, imageSize
, data
);
3420 * Common helper for glCompressedTexSubImage1/2/3D().
3423 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3424 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3425 GLsizei width
, GLsizei height
, GLsizei depth
,
3426 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3428 struct gl_texture_object
*texObj
;
3429 struct gl_texture_image
*texImage
;
3431 GET_CURRENT_CONTEXT(ctx
);
3432 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3434 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3435 xoffset
, 0, 0, /* pos */
3436 width
, height
, depth
, /* size */
3439 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3443 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3445 _mesa_lock_texture(ctx
, texObj
);
3447 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3450 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3451 format
, texImage
)) {
3452 /* error was recorded */
3454 else if (width
> 0 && height
> 0 && depth
> 0) {
3457 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3458 ctx
->Driver
.CompressedTexSubImage1D(ctx
, target
, level
,
3460 format
, imageSize
, data
,
3465 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3466 ctx
->Driver
.CompressedTexSubImage2D(ctx
, target
, level
,
3469 format
, imageSize
, data
,
3474 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3475 ctx
->Driver
.CompressedTexSubImage3D(ctx
, target
, level
,
3476 xoffset
, yoffset
, zoffset
,
3477 width
, height
, depth
,
3478 format
, imageSize
, data
,
3486 check_gen_mipmap(ctx
, target
, texObj
, level
);
3488 ctx
->NewState
|= _NEW_TEXTURE
;
3491 _mesa_unlock_texture(ctx
, texObj
);
3496 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3497 GLsizei width
, GLenum format
,
3498 GLsizei imageSize
, const GLvoid
*data
)
3500 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3501 format
, imageSize
, data
);
3506 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3507 GLint yoffset
, GLsizei width
, GLsizei height
,
3508 GLenum format
, GLsizei imageSize
,
3511 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3512 width
, height
, 1, format
, imageSize
, data
);
3517 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3518 GLint yoffset
, GLint zoffset
, GLsizei width
,
3519 GLsizei height
, GLsizei depth
, GLenum format
,
3520 GLsizei imageSize
, const GLvoid
*data
)
3522 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3523 width
, height
, depth
, format
, imageSize
, data
);