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 (_MESA_NEW_TRANSFER_STATE | \
65 * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
69 _mesa_alloc_texmemory(GLsizei bytes
)
71 return _mesa_align_malloc(bytes
, 512);
76 * Free texture memory allocated with _mesa_alloc_texmemory()
79 _mesa_free_texmemory(void *m
)
86 * Compute floor(log_base_2(n)).
116 * Return the simple base format for a given internal texture format.
117 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
119 * \param ctx GL context.
120 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
122 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
123 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
125 * This is the format which is used during texture application (i.e. the
126 * texture format and env mode determine the arithmetic used.
128 * XXX this could be static
131 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
133 switch (internalFormat
) {
148 case GL_LUMINANCE_ALPHA
:
149 case GL_LUMINANCE4_ALPHA4
:
150 case GL_LUMINANCE6_ALPHA2
:
151 case GL_LUMINANCE8_ALPHA8
:
152 case GL_LUMINANCE12_ALPHA4
:
153 case GL_LUMINANCE12_ALPHA12
:
154 case GL_LUMINANCE16_ALPHA16
:
155 return GL_LUMINANCE_ALPHA
;
186 if (ctx
->Extensions
.EXT_texture_format_BGRA8888
) {
187 switch (internalFormat
) {
195 if (ctx
->Extensions
.EXT_paletted_texture
) {
196 switch (internalFormat
) {
198 case GL_COLOR_INDEX1_EXT
:
199 case GL_COLOR_INDEX2_EXT
:
200 case GL_COLOR_INDEX4_EXT
:
201 case GL_COLOR_INDEX8_EXT
:
202 case GL_COLOR_INDEX12_EXT
:
203 case GL_COLOR_INDEX16_EXT
:
204 return GL_COLOR_INDEX
;
210 if (ctx
->Extensions
.ARB_depth_texture
) {
211 switch (internalFormat
) {
212 case GL_DEPTH_COMPONENT
:
213 case GL_DEPTH_COMPONENT16
:
214 case GL_DEPTH_COMPONENT24
:
215 case GL_DEPTH_COMPONENT32
:
216 return GL_DEPTH_COMPONENT
;
222 switch (internalFormat
) {
223 case GL_COMPRESSED_ALPHA
:
225 case GL_COMPRESSED_LUMINANCE
:
227 case GL_COMPRESSED_LUMINANCE_ALPHA
:
228 return GL_LUMINANCE_ALPHA
;
229 case GL_COMPRESSED_INTENSITY
:
231 case GL_COMPRESSED_RGB
:
233 case GL_COMPRESSED_RGBA
:
239 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
240 switch (internalFormat
) {
241 case GL_COMPRESSED_RGB_FXT1_3DFX
:
243 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
250 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
251 switch (internalFormat
) {
252 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
254 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
255 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
256 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
263 if (ctx
->Extensions
.S3_s3tc
) {
264 switch (internalFormat
) {
276 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
277 if (internalFormat
== GL_YCBCR_MESA
)
278 return GL_YCBCR_MESA
;
281 if (ctx
->Extensions
.ARB_texture_float
) {
282 switch (internalFormat
) {
283 case GL_ALPHA16F_ARB
:
284 case GL_ALPHA32F_ARB
:
292 case GL_INTENSITY16F_ARB
:
293 case GL_INTENSITY32F_ARB
:
295 case GL_LUMINANCE16F_ARB
:
296 case GL_LUMINANCE32F_ARB
:
298 case GL_LUMINANCE_ALPHA16F_ARB
:
299 case GL_LUMINANCE_ALPHA32F_ARB
:
300 return GL_LUMINANCE_ALPHA
;
306 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
307 switch (internalFormat
) {
316 if (ctx
->Extensions
.MESA_texture_signed_rgba
) {
317 switch (internalFormat
) {
326 if (ctx
->Extensions
.EXT_packed_depth_stencil
) {
327 switch (internalFormat
) {
328 case GL_DEPTH_STENCIL_EXT
:
329 case GL_DEPTH24_STENCIL8_EXT
:
330 return GL_DEPTH_STENCIL_EXT
;
336 #if FEATURE_EXT_texture_sRGB
337 if (ctx
->Extensions
.EXT_texture_sRGB
) {
338 switch (internalFormat
) {
341 case GL_COMPRESSED_SRGB_EXT
:
342 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
344 case GL_SRGB_ALPHA_EXT
:
345 case GL_SRGB8_ALPHA8_EXT
:
346 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
347 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
348 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
349 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
351 case GL_SLUMINANCE_ALPHA_EXT
:
352 case GL_SLUMINANCE8_ALPHA8_EXT
:
353 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
354 return GL_LUMINANCE_ALPHA
;
355 case GL_SLUMINANCE_EXT
:
356 case GL_SLUMINANCE8_EXT
:
357 case GL_COMPRESSED_SLUMINANCE_EXT
:
363 #endif /* FEATURE_EXT_texture_sRGB */
365 if (ctx
->Extensions
.EXT_texture_integer
) {
366 switch (internalFormat
) {
368 case GL_RGBA16UI_EXT
:
369 case GL_RGBA32UI_EXT
:
381 case GL_ALPHA8UI_EXT
:
382 case GL_ALPHA16UI_EXT
:
383 case GL_ALPHA32UI_EXT
:
385 case GL_ALPHA16I_EXT
:
386 case GL_ALPHA32I_EXT
:
388 case GL_INTENSITY8UI_EXT
:
389 case GL_INTENSITY16UI_EXT
:
390 case GL_INTENSITY32UI_EXT
:
391 case GL_INTENSITY8I_EXT
:
392 case GL_INTENSITY16I_EXT
:
393 case GL_INTENSITY32I_EXT
:
395 case GL_LUMINANCE8UI_EXT
:
396 case GL_LUMINANCE16UI_EXT
:
397 case GL_LUMINANCE32UI_EXT
:
398 case GL_LUMINANCE8I_EXT
:
399 case GL_LUMINANCE16I_EXT
:
400 case GL_LUMINANCE32I_EXT
:
402 case GL_LUMINANCE_ALPHA8UI_EXT
:
403 case GL_LUMINANCE_ALPHA16UI_EXT
:
404 case GL_LUMINANCE_ALPHA32UI_EXT
:
405 case GL_LUMINANCE_ALPHA8I_EXT
:
406 case GL_LUMINANCE_ALPHA16I_EXT
:
407 case GL_LUMINANCE_ALPHA32I_EXT
:
408 return GL_LUMINANCE_ALPHA
;
414 if (ctx
->Extensions
.ARB_texture_rg
) {
415 switch (internalFormat
) {
417 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
419 if (!ctx
->Extensions
.ARB_half_float_pixel
)
423 if (!ctx
->Extensions
.ARB_texture_float
)
432 if (!ctx
->Extensions
.EXT_texture_integer
)
438 case GL_COMPRESSED_RED
:
442 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
444 if (!ctx
->Extensions
.ARB_half_float_pixel
)
448 if (!ctx
->Extensions
.ARB_texture_float
)
457 if (!ctx
->Extensions
.EXT_texture_integer
)
463 case GL_COMPRESSED_RG
:
470 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
471 switch (internalFormat
) {
479 if (ctx
->Extensions
.EXT_packed_float
) {
480 switch (internalFormat
) {
481 case GL_R11F_G11F_B10F_EXT
:
488 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
489 switch (internalFormat
) {
490 case GL_DEPTH_COMPONENT32F
:
491 return GL_DEPTH_COMPONENT
;
492 case GL_DEPTH32F_STENCIL8
:
493 return GL_DEPTH_STENCIL
;
499 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
500 switch (internalFormat
) {
501 case GL_COMPRESSED_RED_RGTC1
:
502 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
504 case GL_COMPRESSED_RG_RGTC2
:
505 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
512 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
513 switch (internalFormat
) {
514 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
515 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
517 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
518 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
519 return GL_LUMINANCE_ALPHA
;
525 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
526 switch (internalFormat
) {
527 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
528 return GL_LUMINANCE_ALPHA
;
534 return -1; /* error */
539 * For cube map faces, return a face index in [0,5].
540 * For other targets return 0;
543 _mesa_tex_target_to_face(GLenum target
)
545 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
546 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)
547 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
555 * Store a gl_texture_image pointer in a gl_texture_object structure
556 * according to the target and level parameters.
558 * \param tObj texture object.
559 * \param target texture target.
560 * \param level image level.
561 * \param texImage texture image.
563 * This was basically prompted by the introduction of cube maps.
566 _mesa_set_tex_image(struct gl_texture_object
*tObj
,
567 GLenum target
, GLint level
,
568 struct gl_texture_image
*texImage
)
570 const GLuint face
= _mesa_tex_target_to_face(target
);
574 ASSERT(target
!= GL_TEXTURE_RECTANGLE_NV
|| level
== 0);
576 tObj
->Image
[face
][level
] = texImage
;
578 /* Set the 'back' pointer */
579 texImage
->TexObject
= tObj
;
584 * Allocate a texture image structure.
586 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
589 * \return a pointer to gl_texture_image struct with all fields initialized to
592 struct gl_texture_image
*
593 _mesa_new_texture_image( struct gl_context
*ctx
)
596 return CALLOC_STRUCT(gl_texture_image
);
601 * Free texture image data.
602 * This function is a fallback called via ctx->Driver.FreeTexImageData().
604 * \param texImage texture image.
606 * Free the texture image data if it's not marked as client data.
609 _mesa_free_texture_image_data(struct gl_context
*ctx
,
610 struct gl_texture_image
*texImage
)
614 if (texImage
->Data
&& !texImage
->IsClientData
) {
615 /* free the old texture data */
616 _mesa_free_texmemory(texImage
->Data
);
619 texImage
->Data
= NULL
;
624 * Free texture image.
626 * \param texImage texture image.
628 * Free the texture image structure and the associated image data.
631 _mesa_delete_texture_image(struct gl_context
*ctx
,
632 struct gl_texture_image
*texImage
)
634 /* Free texImage->Data and/or any other driver-specific texture
637 ASSERT(ctx
->Driver
.FreeTexImageData
);
638 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
640 ASSERT(texImage
->Data
== NULL
);
641 if (texImage
->ImageOffsets
)
642 free(texImage
->ImageOffsets
);
648 * Test if a target is a proxy target.
650 * \param target texture target.
652 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
655 _mesa_is_proxy_texture(GLenum target
)
657 /* NUM_TEXTURE_TARGETS should match number of terms below */
658 assert(NUM_TEXTURE_TARGETS
== 7);
660 return (target
== GL_PROXY_TEXTURE_1D
||
661 target
== GL_PROXY_TEXTURE_2D
||
662 target
== GL_PROXY_TEXTURE_3D
||
663 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
664 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
665 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
666 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
);
671 * Return the proxy target which corresponds to the given texture target
674 get_proxy_target(GLenum target
)
678 case GL_PROXY_TEXTURE_1D
:
679 return GL_PROXY_TEXTURE_1D
;
681 case GL_PROXY_TEXTURE_2D
:
682 return GL_PROXY_TEXTURE_2D
;
684 case GL_PROXY_TEXTURE_3D
:
685 return GL_PROXY_TEXTURE_3D
;
686 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
687 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
688 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
689 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
690 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
691 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
692 case GL_TEXTURE_CUBE_MAP_ARB
:
693 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
694 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
695 case GL_TEXTURE_RECTANGLE_NV
:
696 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
697 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
698 case GL_TEXTURE_1D_ARRAY_EXT
:
699 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
700 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
701 case GL_TEXTURE_2D_ARRAY_EXT
:
702 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
703 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
705 _mesa_problem(NULL
, "unexpected target in get_proxy_target()");
712 * Get the texture object that corresponds to the target of the given
715 * \param ctx GL context.
716 * \param texUnit texture unit.
717 * \param target texture target.
719 * \return pointer to the texture object on success, or NULL on failure.
721 * \sa gl_texture_unit.
723 struct gl_texture_object
*
724 _mesa_select_tex_object(struct gl_context
*ctx
,
725 const struct gl_texture_unit
*texUnit
,
728 const GLboolean arrayTex
= (ctx
->Extensions
.MESA_texture_array
||
729 ctx
->Extensions
.EXT_texture_array
);
733 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
734 case GL_PROXY_TEXTURE_1D
:
735 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
737 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
738 case GL_PROXY_TEXTURE_2D
:
739 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
741 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
742 case GL_PROXY_TEXTURE_3D
:
743 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
744 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
745 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
746 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
747 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
748 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
749 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
750 case GL_TEXTURE_CUBE_MAP_ARB
:
751 return ctx
->Extensions
.ARB_texture_cube_map
752 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
753 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
754 return ctx
->Extensions
.ARB_texture_cube_map
755 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
756 case GL_TEXTURE_RECTANGLE_NV
:
757 return ctx
->Extensions
.NV_texture_rectangle
758 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
759 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
760 return ctx
->Extensions
.NV_texture_rectangle
761 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
762 case GL_TEXTURE_1D_ARRAY_EXT
:
763 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
764 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
765 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
766 case GL_TEXTURE_2D_ARRAY_EXT
:
767 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
768 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
769 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
771 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
778 * Return pointer to texture object for given target on current texture unit.
780 struct gl_texture_object
*
781 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
783 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
784 return _mesa_select_tex_object(ctx
, texUnit
, target
);
789 * Get a texture image pointer from a texture object, given a texture
790 * target and mipmap level. The target and level parameters should
791 * have already been error-checked.
793 * \param ctx GL context.
794 * \param texObj texture unit.
795 * \param target texture target.
796 * \param level image level.
798 * \return pointer to the texture image structure, or NULL on failure.
800 struct gl_texture_image
*
801 _mesa_select_tex_image(struct gl_context
*ctx
,
802 const struct gl_texture_object
*texObj
,
803 GLenum target
, GLint level
)
805 const GLuint face
= _mesa_tex_target_to_face(target
);
809 ASSERT(level
< MAX_TEXTURE_LEVELS
);
811 return texObj
->Image
[face
][level
];
816 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
817 * it and install it. Only return NULL if passed a bad parameter or run
820 struct gl_texture_image
*
821 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
822 GLenum target
, GLint level
)
824 struct gl_texture_image
*texImage
;
829 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
831 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
833 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
837 _mesa_set_tex_image(texObj
, target
, level
, texImage
);
845 * Return pointer to the specified proxy texture image.
846 * Note that proxy textures are per-context, not per-texture unit.
847 * \return pointer to texture image or NULL if invalid target, invalid
848 * level, or out of memory.
850 struct gl_texture_image
*
851 _mesa_get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
853 struct gl_texture_image
*texImage
;
860 case GL_PROXY_TEXTURE_1D
:
861 if (level
>= ctx
->Const
.MaxTextureLevels
)
863 texIndex
= TEXTURE_1D_INDEX
;
865 case GL_PROXY_TEXTURE_2D
:
866 if (level
>= ctx
->Const
.MaxTextureLevels
)
868 texIndex
= TEXTURE_2D_INDEX
;
870 case GL_PROXY_TEXTURE_3D
:
871 if (level
>= ctx
->Const
.Max3DTextureLevels
)
873 texIndex
= TEXTURE_3D_INDEX
;
875 case GL_PROXY_TEXTURE_CUBE_MAP
:
876 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
878 texIndex
= TEXTURE_CUBE_INDEX
;
880 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
883 texIndex
= TEXTURE_RECT_INDEX
;
885 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
886 if (level
>= ctx
->Const
.MaxTextureLevels
)
888 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
890 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
891 if (level
>= ctx
->Const
.MaxTextureLevels
)
893 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
899 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
901 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
903 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
906 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
907 /* Set the 'back' pointer */
908 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
915 * Get the maximum number of allowed mipmap levels.
917 * \param ctx GL context.
918 * \param target texture target.
920 * \return the maximum number of allowed mipmap levels for the given
921 * texture target, or zero if passed a bad target.
926 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
930 case GL_PROXY_TEXTURE_1D
:
932 case GL_PROXY_TEXTURE_2D
:
933 return ctx
->Const
.MaxTextureLevels
;
935 case GL_PROXY_TEXTURE_3D
:
936 return ctx
->Const
.Max3DTextureLevels
;
937 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
938 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
939 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
940 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
941 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
942 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
943 case GL_TEXTURE_CUBE_MAP_ARB
:
944 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
945 return ctx
->Extensions
.ARB_texture_cube_map
946 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
947 case GL_TEXTURE_RECTANGLE_NV
:
948 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
949 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
950 case GL_TEXTURE_1D_ARRAY_EXT
:
951 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
952 case GL_TEXTURE_2D_ARRAY_EXT
:
953 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
954 return (ctx
->Extensions
.MESA_texture_array
||
955 ctx
->Extensions
.EXT_texture_array
)
956 ? ctx
->Const
.MaxTextureLevels
: 0;
958 return 0; /* bad target */
964 * Return number of dimensions per mipmap level for the given texture target.
967 _mesa_get_texture_dimensions(GLenum target
)
971 case GL_PROXY_TEXTURE_1D
:
974 case GL_TEXTURE_RECTANGLE
:
975 case GL_TEXTURE_CUBE_MAP
:
976 case GL_PROXY_TEXTURE_2D
:
977 case GL_PROXY_TEXTURE_RECTANGLE
:
978 case GL_PROXY_TEXTURE_CUBE_MAP
:
979 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
980 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
981 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
982 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
983 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
984 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
985 case GL_TEXTURE_1D_ARRAY
:
986 case GL_PROXY_TEXTURE_1D_ARRAY
:
989 case GL_PROXY_TEXTURE_3D
:
990 case GL_TEXTURE_2D_ARRAY
:
991 case GL_PROXY_TEXTURE_2D_ARRAY
:
994 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1003 #if 000 /* not used anymore */
1005 * glTexImage[123]D can accept a NULL image pointer. In this case we
1006 * create a texture image with unspecified image contents per the OpenGL
1010 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1012 const GLint components
= _mesa_components_in_format(format
);
1013 const GLint numPixels
= width
* height
* depth
;
1014 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
1018 * Let's see if anyone finds this. If glTexImage2D() is called with
1019 * a NULL image pointer then load the texture image with something
1020 * interesting instead of leaving it indeterminate.
1023 static const char message
[8][32] = {
1024 " X X XXXXX XXX X ",
1025 " XX XX X X X X X ",
1027 " X X XXXX XXX XXXXX ",
1030 " X X XXXXX XXX X X ",
1034 GLubyte
*imgPtr
= data
;
1036 for (h
= 0; h
< depth
; h
++) {
1037 for (i
= 0; i
< height
; i
++) {
1038 GLint srcRow
= 7 - (i
% 8);
1039 for (j
= 0; j
< width
; j
++) {
1040 GLint srcCol
= j
% 32;
1041 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1042 for (k
= 0; k
< components
; k
++) {
1058 * Reset the fields of a gl_texture_image struct to zero.
1060 * \param img texture image structure.
1062 * This is called when a proxy texture test fails, we set all the
1063 * image members (except DriverData) to zero.
1064 * It's also used in glTexImage[123]D as a safeguard to be sure all
1065 * required fields get initialized properly by the Driver.TexImage[123]D
1069 clear_teximage_fields(struct gl_texture_image
*img
)
1072 img
->_BaseFormat
= 0;
1073 img
->InternalFormat
= 0;
1079 if (img
->ImageOffsets
) {
1080 free(img
->ImageOffsets
);
1081 img
->ImageOffsets
= NULL
;
1087 img
->HeightLog2
= 0;
1090 img
->TexFormat
= MESA_FORMAT_NONE
;
1091 img
->FetchTexelc
= NULL
;
1092 img
->FetchTexelf
= NULL
;
1097 * Initialize basic fields of the gl_texture_image struct.
1099 * \param ctx GL context.
1100 * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
1101 * \param img texture image structure to be initialized.
1102 * \param width image width.
1103 * \param height image height.
1104 * \param depth image depth.
1105 * \param border image border.
1106 * \param internalFormat internal format.
1107 * \param format the actual hardware format (one of MESA_FORMAT_*)
1109 * Fills in the fields of \p img with the given information.
1110 * Note: width, height and depth include the border.
1113 _mesa_init_teximage_fields(struct gl_context
*ctx
, GLenum target
,
1114 struct gl_texture_image
*img
,
1115 GLsizei width
, GLsizei height
, GLsizei depth
,
1116 GLint border
, GLenum internalFormat
,
1123 ASSERT(height
>= 0);
1126 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1127 ASSERT(img
->_BaseFormat
> 0);
1128 img
->InternalFormat
= internalFormat
;
1129 img
->Border
= border
;
1131 img
->Height
= height
;
1134 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1135 img
->WidthLog2
= logbase2(img
->Width2
);
1137 if (height
== 1) { /* 1-D texture */
1139 img
->HeightLog2
= 0;
1142 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1143 img
->HeightLog2
= logbase2(img
->Height2
);
1146 if (depth
== 1) { /* 2-D texture */
1151 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1152 img
->DepthLog2
= logbase2(img
->Depth2
);
1155 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1157 if ((width
== 1 || _mesa_is_pow_two(img
->Width2
)) &&
1158 (height
== 1 || _mesa_is_pow_two(img
->Height2
)) &&
1159 (depth
== 1 || _mesa_is_pow_two(img
->Depth2
)))
1160 img
->_IsPowerOfTwo
= GL_TRUE
;
1162 img
->_IsPowerOfTwo
= GL_FALSE
;
1164 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
1165 img
->RowStride
= width
;
1166 /* Allocate the ImageOffsets array and initialize to typical values.
1167 * We allocate the array for 1D/2D textures too in order to avoid special-
1168 * case code in the texstore routines.
1170 if (img
->ImageOffsets
)
1171 free(img
->ImageOffsets
);
1172 img
->ImageOffsets
= (GLuint
*) malloc(depth
* sizeof(GLuint
));
1173 for (i
= 0; i
< depth
; i
++) {
1174 img
->ImageOffsets
[i
] = i
* width
* height
;
1177 /* Compute Width/Height/DepthScale for mipmap lod computation */
1178 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
1179 /* scale = 1.0 since texture coords directly map to texels */
1180 img
->WidthScale
= 1.0;
1181 img
->HeightScale
= 1.0;
1182 img
->DepthScale
= 1.0;
1185 img
->WidthScale
= (GLfloat
) img
->Width
;
1186 img
->HeightScale
= (GLfloat
) img
->Height
;
1187 img
->DepthScale
= (GLfloat
) img
->Depth
;
1190 img
->TexFormat
= format
;
1192 dims
= _mesa_get_texture_dimensions(target
);
1194 _mesa_set_fetch_functions(img
, dims
);
1199 * Free and clear fields of the gl_texture_image struct.
1201 * \param ctx GL context.
1202 * \param texImage texture image structure to be cleared.
1204 * After the call, \p texImage will have no data associated with it. Its
1205 * fields are cleared so that its parent object will test incomplete.
1208 _mesa_clear_texture_image(struct gl_context
*ctx
,
1209 struct gl_texture_image
*texImage
)
1211 ctx
->Driver
.FreeTexImageData(ctx
, texImage
);
1212 clear_teximage_fields(texImage
);
1217 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1218 * level, width, height and depth against the ctx->Const limits for textures.
1220 * A hardware driver might override this function if, for example, the
1221 * max 3D texture size is 512x512x64 (i.e. not a cube).
1223 * Note that width, height, depth == 0 is not an error. However, a
1224 * texture with zero width/height/depth will be considered "incomplete"
1225 * and texturing will effectively be disabled.
1227 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1228 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1229 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1230 * \param level as passed to glTexImage
1231 * \param internalFormat as passed to glTexImage
1232 * \param format as passed to glTexImage
1233 * \param type as passed to glTexImage
1234 * \param width as passed to glTexImage
1235 * \param height as passed to glTexImage
1236 * \param depth as passed to glTexImage
1237 * \param border as passed to glTexImage
1238 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1241 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1242 GLint internalFormat
, GLenum format
, GLenum type
,
1243 GLint width
, GLint height
, GLint depth
, GLint border
)
1247 (void) internalFormat
;
1252 case GL_PROXY_TEXTURE_1D
:
1253 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1254 if (width
< 2 * border
|| width
> 2 + maxSize
)
1256 if (level
>= ctx
->Const
.MaxTextureLevels
)
1258 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1259 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1264 case GL_PROXY_TEXTURE_2D
:
1265 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1266 if (width
< 2 * border
|| width
> 2 + maxSize
)
1268 if (height
< 2 * border
|| height
> 2 + maxSize
)
1270 if (level
>= ctx
->Const
.MaxTextureLevels
)
1272 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1273 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1275 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1280 case GL_PROXY_TEXTURE_3D
:
1281 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1282 if (width
< 2 * border
|| width
> 2 + maxSize
)
1284 if (height
< 2 * border
|| height
> 2 + maxSize
)
1286 if (depth
< 2 * border
|| depth
> 2 + maxSize
)
1288 if (level
>= ctx
->Const
.Max3DTextureLevels
)
1290 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1291 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1293 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1295 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1300 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1301 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1302 if (width
< 0 || width
> maxSize
)
1304 if (height
< 0 || height
> maxSize
)
1310 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1311 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1312 if (width
< 2 * border
|| width
> 2 + maxSize
)
1314 if (height
< 2 * border
|| height
> 2 + maxSize
)
1316 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1318 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1319 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1321 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1326 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1327 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1328 if (width
< 2 * border
|| width
> 2 + maxSize
)
1330 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1332 if (level
>= ctx
->Const
.MaxTextureLevels
)
1334 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1335 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1340 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1341 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1342 if (width
< 2 * border
|| width
> 2 + maxSize
)
1344 if (height
< 2 * border
|| height
> 2 + maxSize
)
1346 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1348 if (level
>= ctx
->Const
.MaxTextureLevels
)
1350 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1351 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1353 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1359 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1366 * Check if the memory used by the texture would exceed the driver's limit.
1367 * This lets us support a max 3D texture size of 8K (for example) but
1368 * prevents allocating a full 8K x 8K x 8K texture.
1369 * XXX this could be rolled into the proxy texture size test (above) but
1370 * we don't have the actual texture internal format at that point.
1373 legal_texture_size(struct gl_context
*ctx
, gl_format format
,
1374 GLint width
, GLint height
, GLint depth
)
1376 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1377 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1378 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1384 * Helper function to determine whether a target and specific compression
1385 * format are supported.
1388 target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1391 (void) intFormat
; /* not used yet */
1395 case GL_PROXY_TEXTURE_2D
:
1396 return GL_TRUE
; /* true for any compressed format so far */
1397 case GL_PROXY_TEXTURE_CUBE_MAP
:
1398 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1399 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1400 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1401 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1402 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1403 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1404 return ctx
->Extensions
.ARB_texture_cube_map
;
1405 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1406 case GL_TEXTURE_2D_ARRAY_EXT
:
1407 return (ctx
->Extensions
.MESA_texture_array
||
1408 ctx
->Extensions
.EXT_texture_array
);
1416 * Check if the given texture target value is legal for a
1417 * glTexImage1/2/3D call.
1420 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1426 case GL_PROXY_TEXTURE_1D
:
1434 case GL_PROXY_TEXTURE_2D
:
1436 case GL_PROXY_TEXTURE_CUBE_MAP
:
1437 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1438 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1439 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1440 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1441 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1442 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1443 return ctx
->Extensions
.ARB_texture_cube_map
;
1444 case GL_TEXTURE_RECTANGLE_NV
:
1445 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1446 return ctx
->Extensions
.NV_texture_rectangle
;
1447 case GL_TEXTURE_1D_ARRAY_EXT
:
1448 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1449 return (ctx
->Extensions
.MESA_texture_array
||
1450 ctx
->Extensions
.EXT_texture_array
);
1457 case GL_PROXY_TEXTURE_3D
:
1459 case GL_TEXTURE_2D_ARRAY_EXT
:
1460 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1461 return (ctx
->Extensions
.MESA_texture_array
||
1462 ctx
->Extensions
.EXT_texture_array
);
1467 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1474 * Check if the given texture target value is legal for a
1475 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1476 * The difference compared to legal_teximage_target() above is that
1477 * proxy targets are not supported.
1480 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1484 return target
== GL_TEXTURE_1D
;
1489 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1490 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1491 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1492 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1493 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1494 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1495 return ctx
->Extensions
.ARB_texture_cube_map
;
1496 case GL_TEXTURE_RECTANGLE_NV
:
1497 return ctx
->Extensions
.NV_texture_rectangle
;
1498 case GL_TEXTURE_1D_ARRAY_EXT
:
1499 return (ctx
->Extensions
.MESA_texture_array
||
1500 ctx
->Extensions
.EXT_texture_array
);
1508 case GL_TEXTURE_2D_ARRAY_EXT
:
1509 return (ctx
->Extensions
.MESA_texture_array
||
1510 ctx
->Extensions
.EXT_texture_array
);
1515 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1523 * Test the glTexImage[123]D() parameters for errors.
1525 * \param ctx GL context.
1526 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1527 * \param target texture target given by the user.
1528 * \param level image level given by the user.
1529 * \param internalFormat internal format given by the user.
1530 * \param format pixel data format given by the user.
1531 * \param type pixel data type given by the user.
1532 * \param width image width given by the user.
1533 * \param height image height given by the user.
1534 * \param depth image depth given by the user.
1535 * \param border image border given by the user.
1537 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1539 * Verifies each of the parameters against the constants specified in
1540 * __struct gl_contextRec::Const and the supported extensions, and according
1541 * to the OpenGL specification.
1544 texture_error_check( struct gl_context
*ctx
,
1545 GLuint dimensions
, GLenum target
,
1546 GLint level
, GLint internalFormat
,
1547 GLenum format
, GLenum type
,
1548 GLint width
, GLint height
,
1549 GLint depth
, GLint border
)
1551 const GLenum proxyTarget
= get_proxy_target(target
);
1552 const GLboolean isProxy
= target
== proxyTarget
;
1553 GLboolean sizeOK
= GL_TRUE
;
1554 GLboolean colorFormat
, indexFormat
;
1556 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1557 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1559 _mesa_error(ctx
, GL_INVALID_VALUE
,
1560 "glTexImage%dD(level=%d)", dimensions
, level
);
1566 if (border
< 0 || border
> 1 ||
1567 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1568 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1570 _mesa_error(ctx
, GL_INVALID_VALUE
,
1571 "glTexImage%dD(border=%d)", dimensions
, border
);
1576 if (width
< 0 || height
< 0 || depth
< 0) {
1578 _mesa_error(ctx
, GL_INVALID_VALUE
,
1579 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1584 /* Do this simple check before calling the TestProxyTexImage() function */
1585 if (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
1586 sizeOK
= (width
== height
);
1590 * Use the proxy texture driver hook to see if the size/level/etc are
1593 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
1594 internalFormat
, format
,
1595 type
, width
, height
,
1599 _mesa_error(ctx
, GL_INVALID_VALUE
,
1600 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1601 dimensions
, level
, width
, height
, depth
);
1606 /* Check internalFormat */
1607 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1609 _mesa_error(ctx
, GL_INVALID_VALUE
,
1610 "glTexImage%dD(internalFormat=%s)",
1611 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1616 /* Check incoming image format and type */
1617 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1618 /* Normally, GL_INVALID_OPERATION is generated by a format/type
1619 * mismatch (see the 1.2 spec page 94, sec 3.6.4.). But with the
1620 * GL_EXT_texture_integer extension, some combinations should generate
1621 * GL_INVALID_ENUM instead (grr!).
1624 GLenum error
= _mesa_is_integer_format(format
)
1625 ? GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
1626 _mesa_error(ctx
, error
,
1627 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1628 dimensions
, format
, type
);
1633 /* make sure internal format and format basically agree */
1634 colorFormat
= _mesa_is_color_format(format
);
1635 indexFormat
= _mesa_is_index_format(format
);
1636 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1637 (_mesa_is_index_format(internalFormat
) && !indexFormat
) ||
1638 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1639 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1640 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1641 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1643 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1644 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1645 dimensions
, internalFormat
, format
);
1649 /* additional checks for ycbcr textures */
1650 if (internalFormat
== GL_YCBCR_MESA
) {
1651 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1652 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1653 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1655 _mesa_snprintf(message
, sizeof(message
),
1656 "glTexImage%dD(format/type YCBCR mismatch", dimensions
);
1657 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1658 return GL_TRUE
; /* error */
1660 if (target
!= GL_TEXTURE_2D
&&
1661 target
!= GL_PROXY_TEXTURE_2D
&&
1662 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1663 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1665 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1671 _mesa_snprintf(message
, sizeof(message
),
1672 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1673 dimensions
, border
);
1674 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1680 /* additional checks for depth textures */
1681 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1682 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1683 if (target
!= GL_TEXTURE_1D
&&
1684 target
!= GL_PROXY_TEXTURE_1D
&&
1685 target
!= GL_TEXTURE_2D
&&
1686 target
!= GL_PROXY_TEXTURE_2D
&&
1687 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1688 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
) {
1690 _mesa_error(ctx
, GL_INVALID_ENUM
,
1691 "glTexImage(target/internalFormat)");
1696 /* additional checks for compressed textures */
1697 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1698 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
1700 _mesa_error(ctx
, GL_INVALID_ENUM
,
1701 "glTexImage%dD(target)", dimensions
);
1706 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1707 "glTexImage%dD(border!=0)", dimensions
);
1713 /* additional checks for integer textures */
1714 if (ctx
->Extensions
.EXT_texture_integer
&&
1715 (_mesa_is_integer_format(format
) !=
1716 _mesa_is_integer_format(internalFormat
))) {
1718 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1719 "glTexImage%dD(integer/non-integer format mismatch)",
1725 /* if we get here, the parameters are OK */
1731 * Test glTexSubImage[123]D() parameters for errors.
1733 * \param ctx GL context.
1734 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1735 * \param target texture target given by the user.
1736 * \param level image level given by the user.
1737 * \param xoffset sub-image x offset given by the user.
1738 * \param yoffset sub-image y offset given by the user.
1739 * \param zoffset sub-image z offset given by the user.
1740 * \param format pixel data format given by the user.
1741 * \param type pixel data type given by the user.
1742 * \param width image width given by the user.
1743 * \param height image height given by the user.
1744 * \param depth image depth given by the user.
1746 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1748 * Verifies each of the parameters against the constants specified in
1749 * __struct gl_contextRec::Const and the supported extensions, and according
1750 * to the OpenGL specification.
1753 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1754 GLenum target
, GLint level
,
1755 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1756 GLint width
, GLint height
, GLint depth
,
1757 GLenum format
, GLenum type
)
1759 /* Basic level check */
1760 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1761 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1765 /* Check for negative sizes */
1767 _mesa_error(ctx
, GL_INVALID_VALUE
,
1768 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1771 if (height
< 0 && dimensions
> 1) {
1772 _mesa_error(ctx
, GL_INVALID_VALUE
,
1773 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1776 if (depth
< 0 && dimensions
> 2) {
1777 _mesa_error(ctx
, GL_INVALID_VALUE
,
1778 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1782 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
1783 /* As with the glTexImage2D check above, the error code here
1784 * depends on texture integer.
1786 GLenum error
= _mesa_is_integer_format(format
)
1787 ? GL_INVALID_OPERATION
: GL_INVALID_ENUM
;
1788 _mesa_error(ctx
, error
,
1789 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1790 dimensions
, format
, type
);
1799 * Do second part of glTexSubImage which depends on the destination texture.
1800 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1803 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1804 GLenum target
, GLint level
,
1805 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1806 GLint width
, GLint height
, GLint depth
,
1807 GLenum format
, GLenum type
,
1808 const struct gl_texture_image
*destTex
)
1811 /* undefined image level */
1812 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1816 if (xoffset
< -((GLint
)destTex
->Border
)) {
1817 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1821 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1822 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1826 if (dimensions
> 1) {
1827 if (yoffset
< -((GLint
)destTex
->Border
)) {
1828 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1832 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1833 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1838 if (dimensions
> 2) {
1839 if (zoffset
< -((GLint
)destTex
->Border
)) {
1840 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
1843 if (zoffset
+ depth
> (GLint
) (destTex
->Depth
+ destTex
->Border
)) {
1844 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
1849 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
1852 /* do tests which depend on compression block size */
1853 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
1855 /* offset must be multiple of block size */
1856 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1858 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
1859 dimensions
, xoffset
, yoffset
);
1862 /* size must be multiple of bw by bh or equal to whole texture size */
1863 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
1864 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1865 "glTexSubImage%dD(width = %d)", dimensions
, width
);
1868 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
1869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1870 "glTexSubImage%dD(height = %d)", dimensions
, height
);
1880 * Test glCopyTexImage[12]D() parameters for errors.
1882 * \param ctx GL context.
1883 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1884 * \param target texture target given by the user.
1885 * \param level image level given by the user.
1886 * \param internalFormat internal format given by the user.
1887 * \param width image width given by the user.
1888 * \param height image height given by the user.
1889 * \param border texture border.
1891 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1893 * Verifies each of the parameters against the constants specified in
1894 * __struct gl_contextRec::Const and the supported extensions, and according
1895 * to the OpenGL specification.
1898 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1899 GLenum target
, GLint level
, GLint internalFormat
,
1900 GLint width
, GLint height
, GLint border
)
1902 const GLenum proxyTarget
= get_proxy_target(target
);
1903 const GLenum type
= GL_FLOAT
;
1908 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
1909 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
1910 dimensions
, _mesa_lookup_enum_by_nr(target
));
1914 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1915 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1916 _mesa_error(ctx
, GL_INVALID_VALUE
,
1917 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1921 /* Check that the source buffer is complete */
1922 if (ctx
->ReadBuffer
->Name
) {
1923 if (ctx
->ReadBuffer
->_Status
== 0) {
1924 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
1926 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1927 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1928 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
1934 if (border
< 0 || border
> 1 ||
1935 ((target
== GL_TEXTURE_RECTANGLE_NV
||
1936 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1940 format
= _mesa_base_tex_format(ctx
, internalFormat
);
1942 _mesa_error(ctx
, GL_INVALID_VALUE
,
1943 "glCopyTexImage%dD(internalFormat)", dimensions
);
1947 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1948 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1949 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
1953 /* Do size, level checking */
1954 sizeOK
= (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
)
1955 ? (width
== height
) : 1;
1957 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
1958 internalFormat
, format
,
1959 type
, width
, height
,
1963 if (dimensions
== 1) {
1964 _mesa_error(ctx
, GL_INVALID_VALUE
,
1965 "glCopyTexImage1D(width=%d)", width
);
1968 ASSERT(dimensions
== 2);
1969 _mesa_error(ctx
, GL_INVALID_VALUE
,
1970 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1975 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1976 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
1977 _mesa_error(ctx
, GL_INVALID_ENUM
,
1978 "glCopyTexImage%dD(target)", dimensions
);
1982 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1983 "glCopyTexImage%dD(border!=0)", dimensions
);
1987 else if (_mesa_is_depth_format(internalFormat
)) {
1988 /* make sure we have depth/stencil buffers */
1989 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
1990 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1991 "glCopyTexImage%dD(no depth)", dimensions
);
1995 else if (_mesa_is_depthstencil_format(internalFormat
)) {
1996 /* make sure we have depth/stencil buffers */
1997 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
1998 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1999 "glCopyTexImage%dD(no depth/stencil buffer)", dimensions
);
2004 /* if we get here, the parameters are OK */
2010 * Test glCopyTexSubImage[12]D() parameters for errors.
2011 * Note that this is the first part of error checking.
2012 * See also copytexsubimage_error_check2() below for the second part.
2014 * \param ctx GL context.
2015 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2016 * \param target texture target given by the user.
2017 * \param level image level given by the user.
2019 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2022 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
2023 GLenum target
, GLint level
)
2025 /* Check that the source buffer is complete */
2026 if (ctx
->ReadBuffer
->Name
) {
2027 if (ctx
->ReadBuffer
->_Status
== 0) {
2028 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2030 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2031 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2032 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2037 /* check target (proxies not allowed) */
2038 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2039 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2040 dimensions
, _mesa_lookup_enum_by_nr(target
));
2045 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
2046 _mesa_error(ctx
, GL_INVALID_VALUE
,
2047 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2056 * Second part of error checking for glCopyTexSubImage[12]D().
2057 * \param xoffset sub-image x offset given by the user.
2058 * \param yoffset sub-image y offset given by the user.
2059 * \param zoffset sub-image z offset given by the user.
2060 * \param width image width given by the user.
2061 * \param height image height given by the user.
2064 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
2065 GLenum target
, GLint level
,
2066 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2067 GLsizei width
, GLsizei height
,
2068 const struct gl_texture_image
*teximage
)
2070 /* check that dest tex image exists */
2072 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2073 "glCopyTexSubImage%dD(undefined texture level: %d)",
2080 _mesa_error(ctx
, GL_INVALID_VALUE
,
2081 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
2084 if (dimensions
> 1 && height
< 0) {
2085 _mesa_error(ctx
, GL_INVALID_VALUE
,
2086 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
2090 /* check x/y offsets */
2091 if (xoffset
< -((GLint
)teximage
->Border
)) {
2092 _mesa_error(ctx
, GL_INVALID_VALUE
,
2093 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
2096 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
2097 _mesa_error(ctx
, GL_INVALID_VALUE
,
2098 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
2101 if (dimensions
> 1) {
2102 if (yoffset
< -((GLint
)teximage
->Border
)) {
2103 _mesa_error(ctx
, GL_INVALID_VALUE
,
2104 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2107 /* NOTE: we're adding the border here, not subtracting! */
2108 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
2109 _mesa_error(ctx
, GL_INVALID_VALUE
,
2110 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2115 /* check z offset */
2116 if (dimensions
> 2) {
2117 if (zoffset
< -((GLint
)teximage
->Border
)) {
2118 _mesa_error(ctx
, GL_INVALID_VALUE
,
2119 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2122 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
2123 _mesa_error(ctx
, GL_INVALID_VALUE
,
2124 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2129 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2130 /* offset must be multiple of 4 */
2131 if ((xoffset
& 3) || (yoffset
& 3)) {
2132 _mesa_error(ctx
, GL_INVALID_VALUE
,
2133 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2136 /* size must be multiple of 4 */
2137 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2138 _mesa_error(ctx
, GL_INVALID_VALUE
,
2139 "glCopyTexSubImage%dD(width)", dimensions
);
2142 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2143 _mesa_error(ctx
, GL_INVALID_VALUE
,
2144 "glCopyTexSubImage%dD(height)", dimensions
);
2149 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2150 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2154 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2155 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2156 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2157 dimensions
, teximage
->_BaseFormat
);
2161 if (teximage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2162 if (!ctx
->ReadBuffer
->_DepthBuffer
) {
2163 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2164 "glCopyTexSubImage%dD(no depth buffer)",
2169 else if (teximage
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
) {
2170 if (!ctx
->ReadBuffer
->_DepthBuffer
|| !ctx
->ReadBuffer
->_StencilBuffer
) {
2171 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2172 "glCopyTexSubImage%dD(no depth/stencil buffer)",
2178 /* If copying into an integer texture, the source buffer must also be
2181 if (_mesa_is_format_integer_color(teximage
->TexFormat
)) {
2182 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2183 if (!_mesa_is_format_integer_color(rb
->Format
)) {
2184 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2185 "glCopyTexSubImage%dD(source buffer is not integer format)",
2191 /* if we get here, the parameters are OK */
2196 /** Callback info for walking over FBO hash table */
2199 struct gl_context
*ctx
;
2200 struct gl_texture_object
*texObj
;
2206 * Check render to texture callback. Called from _mesa_HashWalk().
2209 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2211 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2212 const struct cb_info
*info
= (struct cb_info
*) userData
;
2213 struct gl_context
*ctx
= info
->ctx
;
2214 const struct gl_texture_object
*texObj
= info
->texObj
;
2215 const GLuint level
= info
->level
, face
= info
->face
;
2217 /* If this is a user-created FBO */
2220 /* check if any of the FBO's attachments point to 'texObj' */
2221 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2222 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2223 if (att
->Type
== GL_TEXTURE
&&
2224 att
->Texture
== texObj
&&
2225 att
->TextureLevel
== level
&&
2226 att
->CubeMapFace
== face
) {
2227 ASSERT(att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
]);
2228 /* Tell driver about the new renderbuffer texture */
2229 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2230 /* Mark fb status as indeterminate to force re-validation */
2239 * When a texture image is specified we have to check if it's bound to
2240 * any framebuffer objects (render to texture) in order to detect changes
2241 * in size or format since that effects FBO completeness.
2242 * Any FBOs rendering into the texture must be re-validated.
2245 update_fbo_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
2246 GLuint face
, GLuint level
)
2248 /* Only check this texture if it's been marked as RenderToTexture */
2249 if (texObj
->_RenderToTexture
) {
2250 struct cb_info info
;
2252 info
.texObj
= texObj
;
2255 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2261 * If the texture object's GenerateMipmap flag is set and we've
2262 * changed the texture base level image, regenerate the rest of the
2263 * mipmap levels now.
2266 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2267 struct gl_texture_object
*texObj
, GLint level
)
2269 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2270 if (texObj
->GenerateMipmap
&&
2271 level
== texObj
->BaseLevel
&&
2272 level
< texObj
->MaxLevel
) {
2273 ASSERT(ctx
->Driver
.GenerateMipmap
);
2274 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2279 /** Debug helper: override the user-requested internal format */
2281 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2284 if (internalFormat
== GL_RGBA16F_ARB
||
2285 internalFormat
== GL_RGBA32F_ARB
) {
2286 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2289 else if (internalFormat
== GL_RGB16F_ARB
||
2290 internalFormat
== GL_RGB32F_ARB
) {
2291 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2294 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2295 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2296 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2297 return GL_LUMINANCE_ALPHA
;
2299 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2300 internalFormat
== GL_LUMINANCE32F_ARB
) {
2301 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2302 return GL_LUMINANCE
;
2304 else if (internalFormat
== GL_ALPHA16F_ARB
||
2305 internalFormat
== GL_ALPHA32F_ARB
) {
2306 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2310 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2311 internalFormat = GL_RGBA;
2315 return internalFormat
;
2318 return internalFormat
;
2324 * Choose the actual hardware format for a texture image.
2325 * Try to use the same format as the previous image level when possible.
2326 * Otherwise, ask the driver for the best format.
2327 * It's important to try to choose a consistant format for all levels
2328 * for efficient texture memory layout/allocation. In particular, this
2329 * comes up during automatic mipmap generation.
2332 _mesa_choose_texture_format(struct gl_context
*ctx
,
2333 struct gl_texture_object
*texObj
,
2334 GLenum target
, GLint level
,
2335 GLenum internalFormat
, GLenum format
, GLenum type
)
2339 /* see if we've already chosen a format for the previous level */
2341 struct gl_texture_image
*prevImage
=
2342 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2343 /* See if the prev level is defined and has an internal format which
2344 * matches the new internal format.
2347 prevImage
->Width
> 0 &&
2348 prevImage
->InternalFormat
== internalFormat
) {
2349 /* use the same format */
2350 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2351 return prevImage
->TexFormat
;
2355 /* choose format from scratch */
2356 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
, format
, type
);
2357 ASSERT(f
!= MESA_FORMAT_NONE
);
2363 * Common code to implement all the glTexImage1D/2D/3D functions.
2366 teximage(struct gl_context
*ctx
, GLuint dims
,
2367 GLenum target
, GLint level
, GLint internalFormat
,
2368 GLsizei width
, GLsizei height
, GLsizei depth
,
2369 GLint border
, GLenum format
, GLenum type
,
2370 const GLvoid
*pixels
)
2374 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2376 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2377 _mesa_debug(ctx
, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2379 _mesa_lookup_enum_by_nr(target
), level
,
2380 _mesa_lookup_enum_by_nr(internalFormat
),
2381 width
, height
, depth
, border
,
2382 _mesa_lookup_enum_by_nr(format
),
2383 _mesa_lookup_enum_by_nr(type
), pixels
);
2385 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2387 /* target error checking */
2388 if (!legal_teximage_target(ctx
, dims
, target
)) {
2389 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage%uD(target=%s)",
2390 dims
, _mesa_lookup_enum_by_nr(target
));
2394 /* general error checking */
2395 error
= texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2396 format
, type
, width
, height
, depth
, border
);
2398 if (_mesa_is_proxy_texture(target
)) {
2399 /* Proxy texture: just clear or set state depending on error checking */
2400 struct gl_texture_image
*texImage
=
2401 _mesa_get_proxy_tex_image(ctx
, target
, level
);
2404 /* when error, clear all proxy texture image parameters */
2406 clear_teximage_fields(texImage
);
2409 /* no error, set the tex image parameters */
2410 struct gl_texture_object
*texObj
=
2411 _mesa_get_current_tex_object(ctx
, target
);
2412 gl_format texFormat
= _mesa_choose_texture_format(ctx
, texObj
,
2417 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
2418 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
2419 depth
, border
, internalFormat
,
2422 else if (texImage
) {
2423 clear_teximage_fields(texImage
);
2428 /* non-proxy target */
2429 const GLuint face
= _mesa_tex_target_to_face(target
);
2430 struct gl_texture_object
*texObj
;
2431 struct gl_texture_image
*texImage
;
2434 return; /* error was recorded */
2437 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2438 _mesa_update_state(ctx
);
2440 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2442 _mesa_lock_texture(ctx
, texObj
);
2444 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2447 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
2450 gl_format texFormat
;
2452 if (texImage
->Data
) {
2453 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2456 ASSERT(texImage
->Data
== NULL
);
2457 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2458 internalFormat
, format
,
2461 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
2462 _mesa_init_teximage_fields(ctx
, target
, texImage
,
2463 width
, height
, depth
,
2464 border
, internalFormat
, texFormat
);
2466 /* Give the texture to the driver. <pixels> may be null. */
2467 ASSERT(ctx
->Driver
.TexImage3D
);
2470 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
2471 width
, border
, format
,
2472 type
, pixels
, &ctx
->Unpack
, texObj
,
2476 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
2477 width
, height
, border
, format
,
2478 type
, pixels
, &ctx
->Unpack
, texObj
,
2482 ctx
->Driver
.TexImage3D(ctx
, target
, level
, internalFormat
,
2483 width
, height
, depth
, border
, format
,
2484 type
, pixels
, &ctx
->Unpack
, texObj
,
2488 _mesa_problem(ctx
, "invalid dims=%u in teximage()", dims
);
2491 check_gen_mipmap(ctx
, target
, texObj
, level
);
2493 update_fbo_texture(ctx
, texObj
, face
, level
);
2496 texObj
->_Complete
= GL_FALSE
;
2497 ctx
->NewState
|= _NEW_TEXTURE
;
2500 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
2504 _mesa_unlock_texture(ctx
, texObj
);
2510 * Called from the API. Note that width includes the border.
2513 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2514 GLsizei width
, GLint border
, GLenum format
,
2515 GLenum type
, const GLvoid
*pixels
)
2517 GET_CURRENT_CONTEXT(ctx
);
2518 teximage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1,
2519 border
, format
, type
, pixels
);
2524 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2525 GLsizei width
, GLsizei height
, GLint border
,
2526 GLenum format
, GLenum type
,
2527 const GLvoid
*pixels
)
2529 GET_CURRENT_CONTEXT(ctx
);
2530 teximage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1,
2531 border
, format
, type
, pixels
);
2536 * Called by the API or display list executor.
2537 * Note that width and height include the border.
2540 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2541 GLsizei width
, GLsizei height
, GLsizei depth
,
2542 GLint border
, GLenum format
, GLenum type
,
2543 const GLvoid
*pixels
)
2545 GET_CURRENT_CONTEXT(ctx
);
2546 teximage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
2547 border
, format
, type
, pixels
);
2552 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2553 GLsizei width
, GLsizei height
, GLsizei depth
,
2554 GLint border
, GLenum format
, GLenum type
,
2555 const GLvoid
*pixels
)
2557 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2558 depth
, border
, format
, type
, pixels
);
2562 #if FEATURE_OES_EGL_image
2564 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
2566 struct gl_texture_object
*texObj
;
2567 struct gl_texture_image
*texImage
;
2568 GET_CURRENT_CONTEXT(ctx
);
2569 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2571 if (!ctx
->Extensions
.OES_EGL_image
) {
2572 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2573 "glEGLImageTargetTexture2DOES(unsupported)");
2577 if (target
!= GL_TEXTURE_2D
) {
2578 _mesa_error(ctx
, GL_INVALID_ENUM
,
2579 "glEGLImageTargetTexture2D(target=%d)", target
);
2583 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2584 _mesa_update_state(ctx
);
2586 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2587 _mesa_lock_texture(ctx
, texObj
);
2589 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
2591 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
2594 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2596 ASSERT(texImage
->Data
== NULL
);
2597 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
2598 texObj
, texImage
, image
);
2601 texObj
->_Complete
= GL_FALSE
;
2602 ctx
->NewState
|= _NEW_TEXTURE
;
2604 _mesa_unlock_texture(ctx
, texObj
);
2612 * Implement all the glTexSubImage1/2/3D() functions.
2615 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2616 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2617 GLsizei width
, GLsizei height
, GLsizei depth
,
2618 GLenum format
, GLenum type
, const GLvoid
*pixels
)
2620 struct gl_texture_object
*texObj
;
2621 struct gl_texture_image
*texImage
;
2623 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2625 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2626 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
2628 _mesa_lookup_enum_by_nr(target
), level
,
2629 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2630 _mesa_lookup_enum_by_nr(format
),
2631 _mesa_lookup_enum_by_nr(type
), pixels
);
2633 /* check target (proxies not allowed) */
2634 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
2635 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
2636 dims
, _mesa_lookup_enum_by_nr(target
));
2640 if (ctx
->NewState
& _MESA_NEW_TRANSFER_STATE
)
2641 _mesa_update_state(ctx
);
2643 if (subtexture_error_check(ctx
, dims
, target
, level
, xoffset
, yoffset
, zoffset
,
2644 width
, height
, depth
, format
, type
)) {
2645 return; /* error was detected */
2648 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2650 _mesa_lock_texture(ctx
, texObj
);
2652 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2654 if (subtexture_error_check2(ctx
, dims
, target
, level
,
2655 xoffset
, yoffset
, zoffset
,
2656 width
, height
, depth
,
2657 format
, type
, texImage
)) {
2658 /* error was recorded */
2660 else if (width
> 0 && height
> 0 && height
> 0) {
2661 /* If we have a border, offset=-1 is legal. Bias by border width. */
2664 zoffset
+= texImage
->Border
;
2667 yoffset
+= texImage
->Border
;
2670 xoffset
+= texImage
->Border
;
2675 ctx
->Driver
.TexSubImage1D(ctx
, target
, level
,
2677 format
, type
, pixels
,
2678 &ctx
->Unpack
, texObj
, texImage
);
2681 ctx
->Driver
.TexSubImage2D(ctx
, target
, level
,
2682 xoffset
, yoffset
, width
, height
,
2683 format
, type
, pixels
,
2684 &ctx
->Unpack
, texObj
, texImage
);
2687 ctx
->Driver
.TexSubImage3D(ctx
, target
, level
,
2688 xoffset
, yoffset
, zoffset
,
2689 width
, height
, depth
,
2690 format
, type
, pixels
,
2691 &ctx
->Unpack
, texObj
, texImage
);
2694 _mesa_problem(ctx
, "unexpected dims in subteximage()");
2697 check_gen_mipmap(ctx
, target
, texObj
, level
);
2699 ctx
->NewState
|= _NEW_TEXTURE
;
2702 _mesa_unlock_texture(ctx
, texObj
);
2707 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2708 GLint xoffset
, GLsizei width
,
2709 GLenum format
, GLenum type
,
2710 const GLvoid
*pixels
)
2712 GET_CURRENT_CONTEXT(ctx
);
2713 texsubimage(ctx
, 1, target
, level
,
2716 format
, type
, pixels
);
2721 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2722 GLint xoffset
, GLint yoffset
,
2723 GLsizei width
, GLsizei height
,
2724 GLenum format
, GLenum type
,
2725 const GLvoid
*pixels
)
2727 GET_CURRENT_CONTEXT(ctx
);
2728 texsubimage(ctx
, 2, target
, level
,
2729 xoffset
, yoffset
, 0,
2731 format
, type
, pixels
);
2737 _mesa_TexSubImage3D( GLenum target
, GLint level
,
2738 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2739 GLsizei width
, GLsizei height
, GLsizei depth
,
2740 GLenum format
, GLenum type
,
2741 const GLvoid
*pixels
)
2743 GET_CURRENT_CONTEXT(ctx
);
2744 texsubimage(ctx
, 3, target
, level
,
2745 xoffset
, yoffset
, zoffset
,
2746 width
, height
, depth
,
2747 format
, type
, pixels
);
2753 * Implement the glCopyTexImage1/2D() functions.
2756 copyteximage(struct gl_context
*ctx
, GLuint dims
,
2757 GLenum target
, GLint level
, GLenum internalFormat
,
2758 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
2760 struct gl_texture_object
*texObj
;
2761 struct gl_texture_image
*texImage
;
2762 const GLuint face
= _mesa_tex_target_to_face(target
);
2764 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2766 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2767 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
2769 _mesa_lookup_enum_by_nr(target
), level
,
2770 _mesa_lookup_enum_by_nr(internalFormat
),
2771 x
, y
, width
, height
, border
);
2773 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2774 _mesa_update_state(ctx
);
2776 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2777 width
, height
, border
))
2780 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2782 _mesa_lock_texture(ctx
, texObj
);
2784 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2787 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2790 gl_format texFormat
;
2792 if (texImage
->Data
) {
2793 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
2796 ASSERT(texImage
->Data
== NULL
);
2798 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2799 internalFormat
, GL_NONE
,
2802 if (legal_texture_size(ctx
, texFormat
, width
, height
, 1)) {
2803 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
, 1,
2804 border
, internalFormat
, texFormat
);
2806 ASSERT(ctx
->Driver
.CopyTexImage2D
);
2808 ctx
->Driver
.CopyTexImage1D(ctx
, target
, level
, internalFormat
,
2809 x
, y
, width
, border
);
2811 ctx
->Driver
.CopyTexImage2D(ctx
, target
, level
, internalFormat
,
2812 x
, y
, width
, height
, border
);
2814 check_gen_mipmap(ctx
, target
, texObj
, level
);
2816 update_fbo_texture(ctx
, texObj
, face
, level
);
2819 texObj
->_Complete
= GL_FALSE
;
2820 ctx
->NewState
|= _NEW_TEXTURE
;
2823 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
2827 _mesa_unlock_texture(ctx
, texObj
);
2833 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
2834 GLenum internalFormat
,
2836 GLsizei width
, GLint border
)
2838 GET_CURRENT_CONTEXT(ctx
);
2839 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
2845 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
2846 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
2849 GET_CURRENT_CONTEXT(ctx
);
2850 copyteximage(ctx
, 2, target
, level
, internalFormat
,
2851 x
, y
, width
, height
, border
);
2857 * Implementation for glCopyTexSubImage1/2/3D() functions.
2860 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2861 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2862 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2864 struct gl_texture_object
*texObj
;
2865 struct gl_texture_image
*texImage
;
2867 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2869 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2870 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
2872 _mesa_lookup_enum_by_nr(target
),
2873 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
2875 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2876 _mesa_update_state(ctx
);
2878 if (copytexsubimage_error_check1(ctx
, dims
, target
, level
))
2881 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2883 _mesa_lock_texture(ctx
, texObj
);
2885 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2887 if (copytexsubimage_error_check2(ctx
, dims
, target
, level
, xoffset
, yoffset
,
2888 zoffset
, width
, height
, texImage
)) {
2889 /* error was recored */
2892 /* If we have a border, offset=-1 is legal. Bias by border width. */
2895 zoffset
+= texImage
->Border
;
2898 yoffset
+= texImage
->Border
;
2901 xoffset
+= texImage
->Border
;
2904 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
2908 ctx
->Driver
.CopyTexSubImage1D(ctx
, target
, level
,
2909 xoffset
, x
, y
, width
);
2912 ctx
->Driver
.CopyTexSubImage2D(ctx
, target
, level
,
2914 x
, y
, width
, height
);
2917 ctx
->Driver
.CopyTexSubImage3D(ctx
, target
, level
,
2918 xoffset
, yoffset
, zoffset
,
2919 x
, y
, width
, height
);
2922 _mesa_problem(ctx
, "bad dims in copytexsubimage()");
2925 check_gen_mipmap(ctx
, target
, texObj
, level
);
2927 ctx
->NewState
|= _NEW_TEXTURE
;
2931 _mesa_unlock_texture(ctx
, texObj
);
2936 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2937 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2939 GET_CURRENT_CONTEXT(ctx
);
2940 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
2946 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2947 GLint xoffset
, GLint yoffset
,
2948 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2950 GET_CURRENT_CONTEXT(ctx
);
2951 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
2958 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
2959 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2960 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2962 GET_CURRENT_CONTEXT(ctx
);
2963 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
2964 x
, y
, width
, height
);
2970 /**********************************************************************/
2971 /****** Compressed Textures ******/
2972 /**********************************************************************/
2976 * Return expected size of a compressed texture.
2979 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
2982 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
2983 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
2988 * Return compressed texture block size, in pixels.
2991 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
2993 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
2994 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
2999 * Error checking for glCompressedTexImage[123]D().
3000 * \return error code or GL_NO_ERROR.
3003 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3004 GLenum target
, GLint level
,
3005 GLenum internalFormat
, GLsizei width
,
3006 GLsizei height
, GLsizei depth
, GLint border
,
3009 const GLenum proxyTarget
= get_proxy_target(target
);
3010 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3014 if (level
< 0 || level
>= maxLevels
)
3015 return GL_INVALID_VALUE
;
3017 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
3018 return GL_INVALID_ENUM
;
3021 /* This will detect any invalid internalFormat value */
3022 if (!_mesa_is_compressed_format(ctx
, internalFormat
))
3023 return GL_INVALID_ENUM
;
3025 /* This should really never fail */
3026 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0)
3027 return GL_INVALID_ENUM
;
3029 /* No compressed formats support borders at this time */
3031 return GL_INVALID_VALUE
;
3033 /* For cube map, width must equal height */
3034 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3035 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
&& width
!= height
)
3036 return GL_INVALID_VALUE
;
3038 /* check image size against compression block size */
3040 gl_format texFormat
=
3041 ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
,
3045 _mesa_get_format_block_size(texFormat
, &bw
, &bh
);
3046 if ((width
> bw
&& width
% bw
> 0) ||
3047 (height
> bh
&& height
% bh
> 0)) {
3049 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
3050 * generated [...] if any parameter combinations are not
3051 * supported by the specific compressed internal format.
3053 return GL_INVALID_OPERATION
;
3057 /* check image sizes */
3058 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
3059 internalFormat
, GL_NONE
, GL_NONE
,
3060 width
, height
, depth
, border
)) {
3061 /* See error comment above */
3062 return GL_INVALID_OPERATION
;
3065 /* check image size in bytes */
3066 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
3067 if (expectedSize
!= imageSize
) {
3068 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
3069 * if <imageSize> is not consistent with the format, dimensions, and
3070 * contents of the specified image.
3072 return GL_INVALID_VALUE
;
3080 * Error checking for glCompressedTexSubImage[123]D().
3081 * \warning There are some bad assumptions here about the size of compressed
3082 * texture tiles (multiple of 4) used to test the validity of the
3083 * offset and size parameters.
3084 * \return error code or GL_NO_ERROR.
3087 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3088 GLenum target
, GLint level
,
3089 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3090 GLsizei width
, GLsizei height
, GLsizei depth
,
3091 GLenum format
, GLsizei imageSize
)
3093 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3097 if (dimensions
== 1) {
3098 /* 1D compressed textures not allowed */
3099 return GL_INVALID_ENUM
;
3101 else if (dimensions
== 2) {
3102 if (target
== GL_PROXY_TEXTURE_2D
) {
3103 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3105 else if (target
== GL_TEXTURE_2D
) {
3106 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3108 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3109 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3110 return GL_INVALID_ENUM
; /*target*/
3111 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3113 else if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
3114 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
3115 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3116 return GL_INVALID_ENUM
; /*target*/
3117 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3120 return GL_INVALID_ENUM
; /*target*/
3123 else if (dimensions
== 3) {
3124 /* 3D compressed textures not allowed */
3125 return GL_INVALID_ENUM
;
3128 maxTextureSize
= 1 << (maxLevels
- 1);
3130 /* this will catch any invalid compressed format token */
3131 if (!_mesa_is_compressed_format(ctx
, format
))
3132 return GL_INVALID_ENUM
;
3134 if (width
< 1 || width
> maxTextureSize
)
3135 return GL_INVALID_VALUE
;
3137 if ((height
< 1 || height
> maxTextureSize
)
3139 return GL_INVALID_VALUE
;
3141 if (level
< 0 || level
>= maxLevels
)
3142 return GL_INVALID_VALUE
;
3145 * do checks which depend on compression block size
3147 get_compressed_block_size(format
, &bw
, &bh
);
3149 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3150 return GL_INVALID_VALUE
;
3152 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3153 return GL_INVALID_VALUE
;
3155 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3156 return GL_INVALID_VALUE
;
3158 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3159 if (expectedSize
!= imageSize
)
3160 return GL_INVALID_VALUE
;
3167 * Do second part of glCompressedTexSubImage error checking.
3168 * \return GL_TRUE if error found, GL_FALSE otherwise.
3171 compressed_subtexture_error_check2(struct gl_context
*ctx
, GLuint dims
,
3172 GLsizei width
, GLsizei height
,
3173 GLsizei depth
, GLenum format
,
3174 struct gl_texture_image
*texImage
)
3177 if ((GLint
) format
!= texImage
->InternalFormat
) {
3178 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3179 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3183 if (((width
== 1 || width
== 2) &&
3184 width
!= (GLsizei
) texImage
->Width
) ||
3185 (width
> (GLsizei
) texImage
->Width
)) {
3186 _mesa_error(ctx
, GL_INVALID_VALUE
,
3187 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3192 if (((height
== 1 || height
== 2) &&
3193 height
!= (GLsizei
) texImage
->Height
) ||
3194 (height
> (GLsizei
) texImage
->Height
)) {
3195 _mesa_error(ctx
, GL_INVALID_VALUE
,
3196 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3202 if (((depth
== 1 || depth
== 2) &&
3203 depth
!= (GLsizei
) texImage
->Depth
) ||
3204 (depth
> (GLsizei
) texImage
->Depth
)) {
3205 _mesa_error(ctx
, GL_INVALID_VALUE
,
3206 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3216 * Implementation of the glCompressedTexImage1/2/3D() functions.
3219 compressedteximage(struct gl_context
*ctx
, GLuint dims
,
3220 GLenum target
, GLint level
,
3221 GLenum internalFormat
, GLsizei width
,
3222 GLsizei height
, GLsizei depth
, GLint border
,
3223 GLsizei imageSize
, const GLvoid
*data
)
3227 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3229 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3231 "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3233 _mesa_lookup_enum_by_nr(target
), level
,
3234 _mesa_lookup_enum_by_nr(internalFormat
),
3235 width
, height
, depth
, border
, imageSize
, data
);
3238 if (!legal_teximage_target(ctx
, dims
, target
)) {
3239 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(target=%s)",
3240 dims
, _mesa_lookup_enum_by_nr(target
));
3244 error
= compressed_texture_error_check(ctx
, dims
, target
, level
,
3245 internalFormat
, width
, height
, depth
,
3249 /* XXX this is kind of a hack */
3251 _mesa_error(ctx
, error
, "glTexImage2D");
3256 switch (internalFormat
) {
3257 case GL_PALETTE4_RGB8_OES
:
3258 case GL_PALETTE4_RGBA8_OES
:
3259 case GL_PALETTE4_R5_G6_B5_OES
:
3260 case GL_PALETTE4_RGBA4_OES
:
3261 case GL_PALETTE4_RGB5_A1_OES
:
3262 case GL_PALETTE8_RGB8_OES
:
3263 case GL_PALETTE8_RGBA8_OES
:
3264 case GL_PALETTE8_R5_G6_B5_OES
:
3265 case GL_PALETTE8_RGBA4_OES
:
3266 case GL_PALETTE8_RGB5_A1_OES
:
3267 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3268 width
, height
, imageSize
, data
);
3274 if (_mesa_is_proxy_texture(target
)) {
3275 /* Proxy texture: just check for errors and update proxy state */
3276 struct gl_texture_image
*texImage
;
3279 struct gl_texture_object
*texObj
=
3280 _mesa_get_current_tex_object(ctx
, target
);
3281 gl_format texFormat
=
3282 _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3283 internalFormat
, GL_NONE
, GL_NONE
);
3284 if (!legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
3285 error
= GL_OUT_OF_MEMORY
;
3289 texImage
= _mesa_get_proxy_tex_image(ctx
, target
, level
);
3292 /* if error, clear all proxy texture image parameters */
3293 clear_teximage_fields(texImage
);
3296 /* no error: store the teximage parameters */
3297 _mesa_init_teximage_fields(ctx
, target
, texImage
, width
, height
,
3298 depth
, border
, internalFormat
,
3304 /* non-proxy target */
3305 struct gl_texture_object
*texObj
;
3306 struct gl_texture_image
*texImage
;
3309 _mesa_error(ctx
, error
, "glCompressedTexImage%uD", dims
);
3313 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3315 _mesa_lock_texture(ctx
, texObj
);
3317 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3319 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3320 "glCompressedTexImage%uD", dims
);
3323 gl_format texFormat
;
3325 if (texImage
->Data
) {
3326 ctx
->Driver
.FreeTexImageData( ctx
, texImage
);
3328 ASSERT(texImage
->Data
== NULL
);
3330 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3331 internalFormat
, GL_NONE
,
3334 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
3335 _mesa_init_teximage_fields(ctx
, target
, texImage
,
3336 width
, height
, depth
,
3337 border
, internalFormat
, texFormat
);
3341 ASSERT(ctx
->Driver
.CompressedTexImage1D
);
3342 ctx
->Driver
.CompressedTexImage1D(ctx
, target
, level
,
3345 border
, imageSize
, data
,
3349 ASSERT(ctx
->Driver
.CompressedTexImage2D
);
3350 ctx
->Driver
.CompressedTexImage2D(ctx
, target
, level
,
3353 border
, imageSize
, data
,
3357 ASSERT(ctx
->Driver
.CompressedTexImage3D
);
3358 ctx
->Driver
.CompressedTexImage3D(ctx
, target
, level
,
3360 width
, height
, depth
,
3361 border
, imageSize
, data
,
3365 _mesa_problem(ctx
, "bad dims in compressedteximage");
3368 check_gen_mipmap(ctx
, target
, texObj
, level
);
3371 texObj
->_Complete
= GL_FALSE
;
3372 ctx
->NewState
|= _NEW_TEXTURE
;
3375 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3376 "glCompressedTexImage%uD", dims
);
3380 _mesa_unlock_texture(ctx
, texObj
);
3386 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3387 GLenum internalFormat
, GLsizei width
,
3388 GLint border
, GLsizei imageSize
,
3391 GET_CURRENT_CONTEXT(ctx
);
3392 compressedteximage(ctx
, 1, target
, level
, internalFormat
,
3393 width
, 1, 1, border
, imageSize
, data
);
3398 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3399 GLenum internalFormat
, GLsizei width
,
3400 GLsizei height
, GLint border
, GLsizei imageSize
,
3403 GET_CURRENT_CONTEXT(ctx
);
3404 compressedteximage(ctx
, 2, target
, level
, internalFormat
,
3405 width
, height
, 1, border
, imageSize
, data
);
3410 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3411 GLenum internalFormat
, GLsizei width
,
3412 GLsizei height
, GLsizei depth
, GLint border
,
3413 GLsizei imageSize
, const GLvoid
*data
)
3415 GET_CURRENT_CONTEXT(ctx
);
3416 compressedteximage(ctx
, 3, target
, level
, internalFormat
,
3417 width
, height
, depth
, border
, imageSize
, data
);
3422 * Common helper for glCompressedTexSubImage1/2/3D().
3425 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3426 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3427 GLsizei width
, GLsizei height
, GLsizei depth
,
3428 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3430 struct gl_texture_object
*texObj
;
3431 struct gl_texture_image
*texImage
;
3433 GET_CURRENT_CONTEXT(ctx
);
3434 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3436 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3437 xoffset
, 0, 0, /* pos */
3438 width
, height
, depth
, /* size */
3441 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3445 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3447 _mesa_lock_texture(ctx
, texObj
);
3449 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3452 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3453 format
, texImage
)) {
3454 /* error was recorded */
3456 else if (width
> 0 && height
> 0 && depth
> 0) {
3459 if (ctx
->Driver
.CompressedTexSubImage1D
) {
3460 ctx
->Driver
.CompressedTexSubImage1D(ctx
, target
, level
,
3462 format
, imageSize
, data
,
3467 if (ctx
->Driver
.CompressedTexSubImage2D
) {
3468 ctx
->Driver
.CompressedTexSubImage2D(ctx
, target
, level
,
3471 format
, imageSize
, data
,
3476 if (ctx
->Driver
.CompressedTexSubImage3D
) {
3477 ctx
->Driver
.CompressedTexSubImage3D(ctx
, target
, level
,
3478 xoffset
, yoffset
, zoffset
,
3479 width
, height
, depth
,
3480 format
, imageSize
, data
,
3488 check_gen_mipmap(ctx
, target
, texObj
, level
);
3490 ctx
->NewState
|= _NEW_TEXTURE
;
3493 _mesa_unlock_texture(ctx
, texObj
);
3498 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3499 GLsizei width
, GLenum format
,
3500 GLsizei imageSize
, const GLvoid
*data
)
3502 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3503 format
, imageSize
, data
);
3508 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3509 GLint yoffset
, GLsizei width
, GLsizei height
,
3510 GLenum format
, GLsizei imageSize
,
3513 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3514 width
, height
, 1, format
, imageSize
, data
);
3519 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3520 GLint yoffset
, GLint zoffset
, GLsizei width
,
3521 GLsizei height
, GLsizei depth
, GLenum format
,
3522 GLsizei imageSize
, const GLvoid
*data
)
3524 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3525 width
, height
, depth
, format
, imageSize
, data
);