2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Texture image-related functions.
33 #include "bufferobj.h"
37 #include "framebuffer.h"
42 #include "mfeatures.h"
44 #include "texcompress.h"
45 #include "texcompress_cpal.h"
50 #include "glformats.h"
53 /* Inexplicably, GL_HALF_FLOAT_OES has a different value than GL_HALF_FLOAT.
55 #ifndef GL_HALF_FLOAT_OES
56 #define GL_HALF_FLOAT_OES 0x8D61
60 * State changes which we care about for glCopyTex[Sub]Image() calls.
61 * In particular, we care about pixel transfer state and buffer state
62 * (such as glReadBuffer to make sure we read from the right renderbuffer).
64 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
69 * Return the simple base format for a given internal texture format.
70 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
72 * \param ctx GL context.
73 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
75 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
76 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
78 * This is the format which is used during texture application (i.e. the
79 * texture format and env mode determine the arithmetic used.
82 _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
84 switch (internalFormat) {
90 return (ctx->API != API_OPENGL_CORE) ? GL_ALPHA : -1;
97 return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE : -1;
99 case GL_LUMINANCE_ALPHA:
100 case GL_LUMINANCE4_ALPHA4:
101 case GL_LUMINANCE6_ALPHA2:
102 case GL_LUMINANCE8_ALPHA8:
103 case GL_LUMINANCE12_ALPHA4:
104 case GL_LUMINANCE12_ALPHA12:
105 case GL_LUMINANCE16_ALPHA16:
106 return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE_ALPHA : -1;
112 return (ctx->API != API_OPENGL_CORE) ? GL_INTENSITY : -1;
114 return (ctx->API != API_OPENGL_CORE) ? GL_RGB : -1;
125 return (ctx->API != API_OPENGL_CORE) ? GL_RGBA : -1;
139 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
141 if (_mesa_is_gles(ctx)) {
142 switch (internalFormat) {
150 if (ctx->Extensions.ARB_ES2_compatibility) {
151 switch (internalFormat) {
159 if (ctx->Extensions.ARB_depth_texture) {
160 switch (internalFormat) {
161 case GL_DEPTH_COMPONENT:
162 case GL_DEPTH_COMPONENT16:
163 case GL_DEPTH_COMPONENT24:
164 case GL_DEPTH_COMPONENT32:
165 return GL_DEPTH_COMPONENT;
171 switch (internalFormat) {
172 case GL_COMPRESSED_ALPHA:
174 case GL_COMPRESSED_LUMINANCE:
176 case GL_COMPRESSED_LUMINANCE_ALPHA:
177 return GL_LUMINANCE_ALPHA;
178 case GL_COMPRESSED_INTENSITY:
180 case GL_COMPRESSED_RGB:
182 case GL_COMPRESSED_RGBA:
188 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
189 switch (internalFormat) {
190 case GL_COMPRESSED_RGB_FXT1_3DFX:
192 case GL_COMPRESSED_RGBA_FXT1_3DFX:
199 if (ctx->Extensions.EXT_texture_compression_s3tc) {
200 switch (internalFormat) {
201 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
203 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
204 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
205 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
212 if (ctx->Extensions.S3_s3tc) {
213 switch (internalFormat) {
225 if (ctx->Extensions.MESA_ycbcr_texture) {
226 if (internalFormat == GL_YCBCR_MESA)
227 return GL_YCBCR_MESA;
230 if (ctx->Extensions.ARB_texture_float) {
231 switch (internalFormat) {
232 case GL_ALPHA16F_ARB:
233 case GL_ALPHA32F_ARB:
241 case GL_INTENSITY16F_ARB:
242 case GL_INTENSITY32F_ARB:
244 case GL_LUMINANCE16F_ARB:
245 case GL_LUMINANCE32F_ARB:
247 case GL_LUMINANCE_ALPHA16F_ARB:
248 case GL_LUMINANCE_ALPHA32F_ARB:
249 return GL_LUMINANCE_ALPHA;
255 if (ctx->Extensions.ATI_envmap_bumpmap) {
256 switch (internalFormat) {
265 if (ctx->Extensions.EXT_texture_snorm) {
266 switch (internalFormat) {
281 case GL_RGBA16_SNORM:
284 case GL_ALPHA8_SNORM:
285 case GL_ALPHA16_SNORM:
287 case GL_LUMINANCE_SNORM:
288 case GL_LUMINANCE8_SNORM:
289 case GL_LUMINANCE16_SNORM:
291 case GL_LUMINANCE_ALPHA_SNORM:
292 case GL_LUMINANCE8_ALPHA8_SNORM:
293 case GL_LUMINANCE16_ALPHA16_SNORM:
294 return GL_LUMINANCE_ALPHA;
295 case GL_INTENSITY_SNORM:
296 case GL_INTENSITY8_SNORM:
297 case GL_INTENSITY16_SNORM:
304 if (ctx->Extensions.EXT_packed_depth_stencil) {
305 switch (internalFormat) {
306 case GL_DEPTH_STENCIL_EXT:
307 case GL_DEPTH24_STENCIL8_EXT:
308 return GL_DEPTH_STENCIL_EXT;
314 #if FEATURE_EXT_texture_sRGB
315 if (ctx->Extensions.EXT_texture_sRGB) {
316 switch (internalFormat) {
319 case GL_COMPRESSED_SRGB_EXT:
320 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
322 case GL_SRGB_ALPHA_EXT:
323 case GL_SRGB8_ALPHA8_EXT:
324 case GL_COMPRESSED_SRGB_ALPHA_EXT:
325 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
326 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
327 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
329 case GL_SLUMINANCE_ALPHA_EXT:
330 case GL_SLUMINANCE8_ALPHA8_EXT:
331 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
332 return GL_LUMINANCE_ALPHA;
333 case GL_SLUMINANCE_EXT:
334 case GL_SLUMINANCE8_EXT:
335 case GL_COMPRESSED_SLUMINANCE_EXT:
341 #endif /* FEATURE_EXT_texture_sRGB */
343 if (ctx->Version >= 30 ||
344 ctx->Extensions.EXT_texture_integer) {
345 switch (internalFormat) {
347 case GL_RGBA16UI_EXT:
348 case GL_RGBA32UI_EXT:
364 if (ctx->Extensions.EXT_texture_integer) {
365 switch (internalFormat) {
366 case GL_ALPHA8UI_EXT:
367 case GL_ALPHA16UI_EXT:
368 case GL_ALPHA32UI_EXT:
370 case GL_ALPHA16I_EXT:
371 case GL_ALPHA32I_EXT:
373 case GL_INTENSITY8UI_EXT:
374 case GL_INTENSITY16UI_EXT:
375 case GL_INTENSITY32UI_EXT:
376 case GL_INTENSITY8I_EXT:
377 case GL_INTENSITY16I_EXT:
378 case GL_INTENSITY32I_EXT:
380 case GL_LUMINANCE8UI_EXT:
381 case GL_LUMINANCE16UI_EXT:
382 case GL_LUMINANCE32UI_EXT:
383 case GL_LUMINANCE8I_EXT:
384 case GL_LUMINANCE16I_EXT:
385 case GL_LUMINANCE32I_EXT:
387 case GL_LUMINANCE_ALPHA8UI_EXT:
388 case GL_LUMINANCE_ALPHA16UI_EXT:
389 case GL_LUMINANCE_ALPHA32UI_EXT:
390 case GL_LUMINANCE_ALPHA8I_EXT:
391 case GL_LUMINANCE_ALPHA16I_EXT:
392 case GL_LUMINANCE_ALPHA32I_EXT:
393 return GL_LUMINANCE_ALPHA;
399 if (ctx->Extensions.ARB_texture_rg) {
400 switch (internalFormat) {
402 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
404 if (!ctx->Extensions.ARB_half_float_pixel)
408 if (!ctx->Extensions.ARB_texture_float)
417 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
423 case GL_COMPRESSED_RED:
427 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
429 if (!ctx->Extensions.ARB_half_float_pixel)
433 if (!ctx->Extensions.ARB_texture_float)
442 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
448 case GL_COMPRESSED_RG:
455 if (ctx->Extensions.EXT_texture_shared_exponent) {
456 switch (internalFormat) {
464 if (ctx->Extensions.EXT_packed_float) {
465 switch (internalFormat) {
466 case GL_R11F_G11F_B10F_EXT:
473 if (ctx->Extensions.ARB_depth_buffer_float) {
474 switch (internalFormat) {
475 case GL_DEPTH_COMPONENT32F:
476 return GL_DEPTH_COMPONENT;
477 case GL_DEPTH32F_STENCIL8:
478 return GL_DEPTH_STENCIL;
484 if (ctx->Extensions.ARB_texture_compression_rgtc) {
485 switch (internalFormat) {
486 case GL_COMPRESSED_RED_RGTC1:
487 case GL_COMPRESSED_SIGNED_RED_RGTC1:
489 case GL_COMPRESSED_RG_RGTC2:
490 case GL_COMPRESSED_SIGNED_RG_RGTC2:
497 if (ctx->Extensions.EXT_texture_compression_latc) {
498 switch (internalFormat) {
499 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
500 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
502 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
503 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
504 return GL_LUMINANCE_ALPHA;
510 if (ctx->Extensions.ATI_texture_compression_3dc) {
511 switch (internalFormat) {
512 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
513 return GL_LUMINANCE_ALPHA;
519 if (ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
520 switch (internalFormat) {
521 case GL_ETC1_RGB8_OES:
528 if (ctx->API == API_OPENGLES) {
529 switch (internalFormat) {
530 case GL_PALETTE4_RGB8_OES:
531 case GL_PALETTE4_R5_G6_B5_OES:
532 case GL_PALETTE8_RGB8_OES:
533 case GL_PALETTE8_R5_G6_B5_OES:
535 case GL_PALETTE4_RGBA8_OES:
536 case GL_PALETTE8_RGB5_A1_OES:
537 case GL_PALETTE4_RGBA4_OES:
538 case GL_PALETTE4_RGB5_A1_OES:
539 case GL_PALETTE8_RGBA8_OES:
540 case GL_PALETTE8_RGBA4_OES:
547 return -1; /* error */
552 * For cube map faces, return a face index in [0,5].
553 * For other targets return 0;
556 _mesa_tex_target_to_face(GLenum target)
558 if (_mesa_is_cube_face(target))
559 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
567 * Install gl_texture_image in a gl_texture_object according to the target
568 * and level parameters.
570 * \param tObj texture object.
571 * \param target texture target.
572 * \param level image level.
573 * \param texImage texture image.
576 set_tex_image(struct gl_texture_object *tObj,
577 GLenum target, GLint level,
578 struct gl_texture_image *texImage)
580 const GLuint face = _mesa_tex_target_to_face(target);
584 if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
587 tObj->Image[face][level] = texImage;
589 /* Set the 'back' pointer */
590 texImage->TexObject = tObj;
591 texImage->Level = level;
592 texImage->Face = face;
597 * Allocate a texture image structure.
599 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
602 * \return a pointer to gl_texture_image struct with all fields initialized to
605 struct gl_texture_image *
606 _mesa_new_texture_image( struct gl_context *ctx )
609 return CALLOC_STRUCT(gl_texture_image);
614 * Free a gl_texture_image and associated data.
615 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
617 * \param texImage texture image.
619 * Free the texture image structure and the associated image data.
622 _mesa_delete_texture_image(struct gl_context *ctx,
623 struct gl_texture_image *texImage)
625 /* Free texImage->Data and/or any other driver-specific texture
628 ASSERT(ctx->Driver.FreeTextureImageBuffer);
629 ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
635 * Test if a target is a proxy target.
637 * \param target texture target.
639 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
642 _mesa_is_proxy_texture(GLenum target)
645 * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
646 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
648 assert(NUM_TEXTURE_TARGETS == 7 + 2);
650 return (target == GL_PROXY_TEXTURE_1D ||
651 target == GL_PROXY_TEXTURE_2D ||
652 target == GL_PROXY_TEXTURE_3D ||
653 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
654 target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
655 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
656 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT);
661 * Return the proxy target which corresponds to the given texture target
664 get_proxy_target(GLenum target)
668 case GL_PROXY_TEXTURE_1D:
669 return GL_PROXY_TEXTURE_1D;
671 case GL_PROXY_TEXTURE_2D:
672 return GL_PROXY_TEXTURE_2D;
674 case GL_PROXY_TEXTURE_3D:
675 return GL_PROXY_TEXTURE_3D;
676 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
677 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
678 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
679 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
680 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
681 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
682 case GL_TEXTURE_CUBE_MAP_ARB:
683 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
684 return GL_PROXY_TEXTURE_CUBE_MAP_ARB;
685 case GL_TEXTURE_RECTANGLE_NV:
686 case GL_PROXY_TEXTURE_RECTANGLE_NV:
687 return GL_PROXY_TEXTURE_RECTANGLE_NV;
688 case GL_TEXTURE_1D_ARRAY_EXT:
689 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
690 return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
691 case GL_TEXTURE_2D_ARRAY_EXT:
692 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
693 return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
695 _mesa_problem(NULL, "unexpected target in get_proxy_target()");
702 * Get the texture object that corresponds to the target of the given
703 * texture unit. The target should have already been checked for validity.
705 * \param ctx GL context.
706 * \param texUnit texture unit.
707 * \param target texture target.
709 * \return pointer to the texture object on success, or NULL on failure.
711 struct gl_texture_object *
712 _mesa_select_tex_object(struct gl_context *ctx,
713 const struct gl_texture_unit *texUnit,
716 const GLboolean arrayTex = (ctx->Extensions.MESA_texture_array ||
717 ctx->Extensions.EXT_texture_array);
721 return texUnit->CurrentTex[TEXTURE_1D_INDEX];
722 case GL_PROXY_TEXTURE_1D:
723 return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
725 return texUnit->CurrentTex[TEXTURE_2D_INDEX];
726 case GL_PROXY_TEXTURE_2D:
727 return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
729 return texUnit->CurrentTex[TEXTURE_3D_INDEX];
730 case GL_PROXY_TEXTURE_3D:
731 return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
732 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
733 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
734 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
735 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
736 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
737 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
738 case GL_TEXTURE_CUBE_MAP_ARB:
739 return ctx->Extensions.ARB_texture_cube_map
740 ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
741 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
742 return ctx->Extensions.ARB_texture_cube_map
743 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
744 case GL_TEXTURE_RECTANGLE_NV:
745 return ctx->Extensions.NV_texture_rectangle
746 ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
747 case GL_PROXY_TEXTURE_RECTANGLE_NV:
748 return ctx->Extensions.NV_texture_rectangle
749 ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
750 case GL_TEXTURE_1D_ARRAY_EXT:
751 return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
752 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
753 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
754 case GL_TEXTURE_2D_ARRAY_EXT:
755 return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
756 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
757 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
758 case GL_TEXTURE_BUFFER:
759 return _mesa_is_desktop_gl(ctx)
760 && ctx->Extensions.ARB_texture_buffer_object
761 ? texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL;
762 case GL_TEXTURE_EXTERNAL_OES:
763 return ctx->Extensions.OES_EGL_image_external
764 ? texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX] : NULL;
766 _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
773 * Return pointer to texture object for given target on current texture unit.
775 struct gl_texture_object *
776 _mesa_get_current_tex_object(struct gl_context *ctx, GLenum target)
778 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
779 return _mesa_select_tex_object(ctx, texUnit, target);
784 * Get a texture image pointer from a texture object, given a texture
785 * target and mipmap level. The target and level parameters should
786 * have already been error-checked.
788 * \param ctx GL context.
789 * \param texObj texture unit.
790 * \param target texture target.
791 * \param level image level.
793 * \return pointer to the texture image structure, or NULL on failure.
795 struct gl_texture_image *
796 _mesa_select_tex_image(struct gl_context *ctx,
797 const struct gl_texture_object *texObj,
798 GLenum target, GLint level)
800 const GLuint face = _mesa_tex_target_to_face(target);
804 ASSERT(level < MAX_TEXTURE_LEVELS);
806 return texObj->Image[face][level];
811 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
812 * it and install it. Only return NULL if passed a bad parameter or run
815 struct gl_texture_image *
816 _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
817 GLenum target, GLint level)
819 struct gl_texture_image *texImage;
824 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
826 texImage = ctx->Driver.NewTextureImage(ctx);
828 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
832 set_tex_image(texObj, target, level, texImage);
840 * Return pointer to the specified proxy texture image.
841 * Note that proxy textures are per-context, not per-texture unit.
842 * \return pointer to texture image or NULL if invalid target, invalid
843 * level, or out of memory.
845 static struct gl_texture_image *
846 get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
848 struct gl_texture_image *texImage;
855 case GL_PROXY_TEXTURE_1D:
856 if (level >= ctx->Const.MaxTextureLevels)
858 texIndex = TEXTURE_1D_INDEX;
860 case GL_PROXY_TEXTURE_2D:
861 if (level >= ctx->Const.MaxTextureLevels)
863 texIndex = TEXTURE_2D_INDEX;
865 case GL_PROXY_TEXTURE_3D:
866 if (level >= ctx->Const.Max3DTextureLevels)
868 texIndex = TEXTURE_3D_INDEX;
870 case GL_PROXY_TEXTURE_CUBE_MAP:
871 if (level >= ctx->Const.MaxCubeTextureLevels)
873 texIndex = TEXTURE_CUBE_INDEX;
875 case GL_PROXY_TEXTURE_RECTANGLE_NV:
878 texIndex = TEXTURE_RECT_INDEX;
880 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
881 if (level >= ctx->Const.MaxTextureLevels)
883 texIndex = TEXTURE_1D_ARRAY_INDEX;
885 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
886 if (level >= ctx->Const.MaxTextureLevels)
888 texIndex = TEXTURE_2D_ARRAY_INDEX;
894 texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
896 texImage = ctx->Driver.NewTextureImage(ctx);
898 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
901 ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
902 /* Set the 'back' pointer */
903 texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
910 * Get the maximum number of allowed mipmap levels.
912 * \param ctx GL context.
913 * \param target texture target.
915 * \return the maximum number of allowed mipmap levels for the given
916 * texture target, or zero if passed a bad target.
921 _mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
925 case GL_PROXY_TEXTURE_1D:
927 case GL_PROXY_TEXTURE_2D:
928 return ctx->Const.MaxTextureLevels;
930 case GL_PROXY_TEXTURE_3D:
931 return ctx->Const.Max3DTextureLevels;
932 case GL_TEXTURE_CUBE_MAP:
933 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
934 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
935 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
936 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
937 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
938 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
939 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
940 return ctx->Extensions.ARB_texture_cube_map
941 ? ctx->Const.MaxCubeTextureLevels : 0;
942 case GL_TEXTURE_RECTANGLE_NV:
943 case GL_PROXY_TEXTURE_RECTANGLE_NV:
944 return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
945 case GL_TEXTURE_1D_ARRAY_EXT:
946 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
947 case GL_TEXTURE_2D_ARRAY_EXT:
948 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
949 return (ctx->Extensions.MESA_texture_array ||
950 ctx->Extensions.EXT_texture_array)
951 ? ctx->Const.MaxTextureLevels : 0;
952 case GL_TEXTURE_BUFFER:
953 return _mesa_is_desktop_gl(ctx)
954 && ctx->Extensions.ARB_texture_buffer_object
956 case GL_TEXTURE_EXTERNAL_OES:
959 return 0; /* bad target */
965 * Return number of dimensions per mipmap level for the given texture target.
968 _mesa_get_texture_dimensions(GLenum target)
972 case GL_PROXY_TEXTURE_1D:
975 case GL_TEXTURE_RECTANGLE:
976 case GL_TEXTURE_CUBE_MAP:
977 case GL_PROXY_TEXTURE_2D:
978 case GL_PROXY_TEXTURE_RECTANGLE:
979 case GL_PROXY_TEXTURE_CUBE_MAP:
980 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
981 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
982 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
983 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
984 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
985 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
986 case GL_TEXTURE_1D_ARRAY:
987 case GL_PROXY_TEXTURE_1D_ARRAY:
988 case GL_TEXTURE_EXTERNAL_OES:
991 case GL_PROXY_TEXTURE_3D:
992 case GL_TEXTURE_2D_ARRAY:
993 case GL_PROXY_TEXTURE_2D_ARRAY:
995 case GL_TEXTURE_BUFFER:
998 _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
1007 #if 000 /* not used anymore */
1009 * glTexImage[123]D can accept a NULL image pointer. In this case we
1010 * create a texture image with unspecified image contents per the OpenGL
1014 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
1016 const GLint components = _mesa_components_in_format(format);
1017 const GLint numPixels = width * height * depth;
1018 GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
1022 * Let's see if anyone finds this. If glTexImage2D() is called with
1023 * a NULL image pointer then load the texture image with something
1024 * interesting instead of leaving it indeterminate.
1027 static const char message[8][32] = {
1028 " X X XXXXX XXX X ",
1029 " XX XX X X X X X ",
1031 " X X XXXX XXX XXXXX ",
1034 " X X XXXXX XXX X X ",
1038 GLubyte *imgPtr = data;
1040 for (h = 0; h < depth; h++) {
1041 for (i = 0; i < height; i++) {
1042 GLint srcRow = 7 - (i % 8);
1043 for (j = 0; j < width; j++) {
1044 GLint srcCol = j % 32;
1045 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
1046 for (k = 0; k < components; k++) {
1062 * Set the size and format-related fields of a gl_texture_image struct
1063 * to zero. This is used when a proxy texture test fails.
1066 clear_teximage_fields(struct gl_texture_image *img)
1069 img->_BaseFormat = 0;
1070 img->InternalFormat = 0;
1079 img->HeightLog2 = 0;
1081 img->TexFormat = MESA_FORMAT_NONE;
1086 * Initialize basic fields of the gl_texture_image struct.
1088 * \param ctx GL context.
1089 * \param img texture image structure to be initialized.
1090 * \param width image width.
1091 * \param height image height.
1092 * \param depth image depth.
1093 * \param border image border.
1094 * \param internalFormat internal format.
1095 * \param format the actual hardware format (one of MESA_FORMAT_*)
1097 * Fills in the fields of \p img with the given information.
1098 * Note: width, height and depth include the border.
1101 _mesa_init_teximage_fields(struct gl_context *ctx,
1102 struct gl_texture_image *img,
1103 GLsizei width, GLsizei height, GLsizei depth,
1104 GLint border, GLenum internalFormat,
1110 ASSERT(height >= 0);
1113 target = img->TexObject->Target;
1114 img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
1115 ASSERT(img->_BaseFormat > 0);
1116 img->InternalFormat = internalFormat;
1117 img->Border = border;
1119 img->Height = height;
1122 img->Width2 = width - 2 * border; /* == 1 << img->WidthLog2; */
1123 img->WidthLog2 = _mesa_logbase2(img->Width2);
1127 case GL_TEXTURE_BUFFER:
1128 case GL_PROXY_TEXTURE_1D:
1133 img->HeightLog2 = 0;
1140 case GL_TEXTURE_1D_ARRAY:
1141 case GL_PROXY_TEXTURE_1D_ARRAY:
1142 img->Height2 = height; /* no border */
1143 img->HeightLog2 = 0; /* not used */
1151 case GL_TEXTURE_RECTANGLE:
1152 case GL_TEXTURE_CUBE_MAP:
1153 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1154 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1155 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1156 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1157 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1158 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1159 case GL_TEXTURE_EXTERNAL_OES:
1160 case GL_PROXY_TEXTURE_2D:
1161 case GL_PROXY_TEXTURE_RECTANGLE:
1162 case GL_PROXY_TEXTURE_CUBE_MAP:
1163 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1164 img->HeightLog2 = _mesa_logbase2(img->Height2);
1171 case GL_TEXTURE_2D_ARRAY:
1172 case GL_PROXY_TEXTURE_2D_ARRAY:
1173 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1174 img->HeightLog2 = _mesa_logbase2(img->Height2);
1175 img->Depth2 = depth; /* no border */
1176 img->DepthLog2 = 0; /* not used */
1179 case GL_PROXY_TEXTURE_3D:
1180 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1181 img->HeightLog2 = _mesa_logbase2(img->Height2);
1182 img->Depth2 = depth - 2 * border; /* == 1 << img->DepthLog2; */
1183 img->DepthLog2 = _mesa_logbase2(img->Depth2);
1186 _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
1190 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
1191 img->TexFormat = format;
1196 * Free and clear fields of the gl_texture_image struct.
1198 * \param ctx GL context.
1199 * \param texImage texture image structure to be cleared.
1201 * After the call, \p texImage will have no data associated with it. Its
1202 * fields are cleared so that its parent object will test incomplete.
1205 _mesa_clear_texture_image(struct gl_context *ctx,
1206 struct gl_texture_image *texImage)
1208 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
1209 clear_teximage_fields(texImage);
1214 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1215 * level, width, height and depth against the ctx->Const limits for textures.
1217 * A hardware driver might override this function if, for example, the
1218 * max 3D texture size is 512x512x64 (i.e. not a cube).
1220 * Note that width, height, depth == 0 is not an error. However, a
1221 * texture with zero width/height/depth will be considered "incomplete"
1222 * and texturing will effectively be disabled.
1224 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1225 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1226 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1227 * \param level as passed to glTexImage
1228 * \param internalFormat as passed to glTexImage
1229 * \param format as passed to glTexImage
1230 * \param type as passed to glTexImage
1231 * \param width as passed to glTexImage
1232 * \param height as passed to glTexImage
1233 * \param depth as passed to glTexImage
1234 * \param border as passed to glTexImage
1235 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1238 _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
1239 GLint internalFormat, GLenum format, GLenum type,
1240 GLint width, GLint height, GLint depth, GLint border)
1244 (void) internalFormat;
1249 case GL_PROXY_TEXTURE_1D:
1250 if (level >= ctx->Const.MaxTextureLevels)
1252 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
1253 maxSize >>= level; /* level size */
1254 if (width < 2 * border || width > 2 * border + maxSize)
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 if (level >= ctx->Const.MaxTextureLevels)
1265 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1267 if (width < 2 * border || width > 2 * border + maxSize)
1269 if (height < 2 * border || height > 2 * border + maxSize)
1271 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1272 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1274 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1279 case GL_PROXY_TEXTURE_3D:
1280 if (level >= ctx->Const.Max3DTextureLevels)
1282 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1284 if (width < 2 * border || width > 2 * border + maxSize)
1286 if (height < 2 * border || height > 2 * border + maxSize)
1288 if (depth < 2 * border || depth > 2 * border + maxSize)
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:
1303 maxSize = ctx->Const.MaxTextureRectSize;
1304 if (width < 0 || width > maxSize)
1306 if (height < 0 || height > maxSize)
1310 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1311 if (level >= ctx->Const.MaxCubeTextureLevels)
1313 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1315 if (width < 2 * border || width > 2 * border + maxSize)
1317 if (height < 2 * border || height > 2 * border + maxSize)
1319 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1320 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1322 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1327 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1328 if (level >= ctx->Const.MaxTextureLevels)
1330 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1332 if (width < 2 * border || width > 2 * border + maxSize)
1334 if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
1336 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1337 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1342 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1343 if (level >= ctx->Const.MaxTextureLevels)
1345 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1347 if (width < 2 * border || width > 2 * border + maxSize)
1349 if (height < 2 * border || height > 2 * border + maxSize)
1351 if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
1353 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1354 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1356 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1362 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1369 * Check if the memory used by the texture would exceed the driver's limit.
1370 * This lets us support a max 3D texture size of 8K (for example) but
1371 * prevents allocating a full 8K x 8K x 8K texture.
1372 * XXX this could be rolled into the proxy texture size test (above) but
1373 * we don't have the actual texture internal format at that point.
1376 legal_texture_size(struct gl_context *ctx, gl_format format,
1377 GLint width, GLint height, GLint depth)
1379 uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
1380 uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
1381 return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1386 * Return true if the format is only valid for glCompressedTexImage.
1389 compressedteximage_only_format(const struct gl_context *ctx, GLenum format)
1392 case GL_ETC1_RGB8_OES:
1393 case GL_PALETTE4_RGB8_OES:
1394 case GL_PALETTE4_RGBA8_OES:
1395 case GL_PALETTE4_R5_G6_B5_OES:
1396 case GL_PALETTE4_RGBA4_OES:
1397 case GL_PALETTE4_RGB5_A1_OES:
1398 case GL_PALETTE8_RGB8_OES:
1399 case GL_PALETTE8_RGBA8_OES:
1400 case GL_PALETTE8_R5_G6_B5_OES:
1401 case GL_PALETTE8_RGBA4_OES:
1402 case GL_PALETTE8_RGB5_A1_OES:
1411 * Helper function to determine whether a target and specific compression
1412 * format are supported.
1415 target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1418 (void) intFormat; /* not used yet */
1422 case GL_PROXY_TEXTURE_2D:
1423 return GL_TRUE; /* true for any compressed format so far */
1424 case GL_PROXY_TEXTURE_CUBE_MAP:
1425 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1426 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1427 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1428 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1429 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1430 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1431 return ctx->Extensions.ARB_texture_cube_map;
1432 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1433 case GL_TEXTURE_2D_ARRAY_EXT:
1434 return (ctx->Extensions.MESA_texture_array ||
1435 ctx->Extensions.EXT_texture_array);
1443 * Check if the given texture target value is legal for a
1444 * glTexImage1/2/3D call.
1447 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1453 case GL_PROXY_TEXTURE_1D:
1454 return _mesa_is_desktop_gl(ctx);
1462 case GL_PROXY_TEXTURE_2D:
1463 return _mesa_is_desktop_gl(ctx);
1464 case GL_PROXY_TEXTURE_CUBE_MAP:
1465 return _mesa_is_desktop_gl(ctx)
1466 && ctx->Extensions.ARB_texture_cube_map;
1467 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1468 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1469 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1470 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1471 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1472 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1473 return ctx->Extensions.ARB_texture_cube_map;
1474 case GL_TEXTURE_RECTANGLE_NV:
1475 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1476 return _mesa_is_desktop_gl(ctx)
1477 && ctx->Extensions.NV_texture_rectangle;
1478 case GL_TEXTURE_1D_ARRAY_EXT:
1479 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1480 return _mesa_is_desktop_gl(ctx)
1481 && (ctx->Extensions.MESA_texture_array ||
1482 ctx->Extensions.EXT_texture_array);
1490 case GL_PROXY_TEXTURE_3D:
1491 return _mesa_is_desktop_gl(ctx);
1492 case GL_TEXTURE_2D_ARRAY_EXT:
1493 return (_mesa_is_desktop_gl(ctx)
1494 && (ctx->Extensions.MESA_texture_array ||
1495 ctx->Extensions.EXT_texture_array))
1496 || _mesa_is_gles3(ctx);
1497 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1498 return _mesa_is_desktop_gl(ctx)
1499 && (ctx->Extensions.MESA_texture_array ||
1500 ctx->Extensions.EXT_texture_array);
1505 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1512 * Check if the given texture target value is legal for a
1513 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1514 * The difference compared to legal_teximage_target() above is that
1515 * proxy targets are not supported.
1518 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1522 return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1527 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1529 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1530 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1531 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1533 return ctx->Extensions.ARB_texture_cube_map;
1534 case GL_TEXTURE_RECTANGLE_NV:
1535 return _mesa_is_desktop_gl(ctx)
1536 && ctx->Extensions.NV_texture_rectangle;
1537 case GL_TEXTURE_1D_ARRAY_EXT:
1538 return _mesa_is_desktop_gl(ctx)
1539 && (ctx->Extensions.MESA_texture_array ||
1540 ctx->Extensions.EXT_texture_array);
1548 case GL_TEXTURE_2D_ARRAY_EXT:
1549 return (_mesa_is_desktop_gl(ctx)
1550 && (ctx->Extensions.MESA_texture_array ||
1551 ctx->Extensions.EXT_texture_array))
1552 || _mesa_is_gles3(ctx);
1557 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1565 * Helper function to determine if a texture object is mutable (in terms
1566 * of GL_ARB_texture_storage).
1569 mutable_tex_object(struct gl_context *ctx, GLenum target)
1571 if (ctx->Extensions.ARB_texture_storage) {
1572 struct gl_texture_object *texObj =
1573 _mesa_get_current_tex_object(ctx, target);
1574 return !texObj->Immutable;
1581 _mesa_es_error_check_format_and_type(GLenum format, GLenum type,
1582 unsigned dimensions)
1584 bool type_valid = true;
1589 case GL_LUMINANCE_ALPHA:
1590 type_valid = (type == GL_UNSIGNED_BYTE
1592 || type == GL_HALF_FLOAT_OES);
1596 type_valid = (type == GL_UNSIGNED_BYTE
1597 || type == GL_UNSIGNED_SHORT_5_6_5
1599 || type == GL_HALF_FLOAT_OES);
1603 type_valid = (type == GL_UNSIGNED_BYTE
1604 || type == GL_UNSIGNED_SHORT_4_4_4_4
1605 || type == GL_UNSIGNED_SHORT_5_5_5_1
1607 || type == GL_HALF_FLOAT_OES
1608 || type == GL_UNSIGNED_INT_2_10_10_10_REV);
1611 case GL_DEPTH_COMPONENT:
1612 /* This format is filtered against invalid dimensionalities elsewhere.
1614 type_valid = (type == GL_UNSIGNED_SHORT
1615 || type == GL_UNSIGNED_INT);
1618 case GL_DEPTH_STENCIL:
1619 /* This format is filtered against invalid dimensionalities elsewhere.
1621 type_valid = (type == GL_UNSIGNED_INT_24_8);
1625 type_valid = (type == GL_UNSIGNED_BYTE);
1627 /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1628 * the format does not appear to be allowed for 3D textures in OpenGL
1631 if (dimensions != 2)
1632 return GL_INVALID_VALUE;
1637 return GL_INVALID_VALUE;
1640 return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
1646 * Return expected size of a compressed texture.
1649 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1652 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1653 return _mesa_format_image_size(mesaFormat, width, height, depth);
1658 * Return compressed texture block size, in pixels.
1661 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
1663 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1664 _mesa_get_format_block_size(mesaFormat, bw, bh);
1669 * Special value returned by error some texture error checking functions when
1670 * an error is detected and the proxy texture image's width/height/depth/format
1671 * fields should be zeroed-out.
1673 #define PROXY_ERROR 2
1677 * Test the glTexImage[123]D() parameters for errors.
1679 * \param ctx GL context.
1680 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1681 * \param target texture target given by the user.
1682 * \param level image level given by the user.
1683 * \param internalFormat internal format given by the user.
1684 * \param format pixel data format given by the user.
1685 * \param type pixel data type given by the user.
1686 * \param width image width given by the user.
1687 * \param height image height given by the user.
1688 * \param depth image depth given by the user.
1689 * \param border image border given by the user.
1691 * \return PROXY_ERROR if there's an error that should zero-out the proxy image,
1692 * GL_TRUE if a regular GL error is found, or GL_FALSE if no error,
1694 * Verifies each of the parameters against the constants specified in
1695 * __struct gl_contextRec::Const and the supported extensions, and according
1696 * to the OpenGL specification.
1699 texture_error_check( struct gl_context *ctx,
1700 GLuint dimensions, GLenum target,
1701 GLint level, GLint internalFormat,
1702 GLenum format, GLenum type,
1703 GLint width, GLint height,
1704 GLint depth, GLint border )
1706 const GLenum proxyTarget = get_proxy_target(target);
1707 const GLboolean isProxy = target == proxyTarget;
1708 GLboolean sizeOK = GL_TRUE;
1709 GLboolean colorFormat;
1712 /* Even though there are no color-index textures, we still have to support
1713 * uploading color-index data and remapping it to RGB via the
1714 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1716 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1718 /* Note: for proxy textures, some error conditions immediately generate
1719 * a GL error in the usual way. But others do not generate a GL error.
1720 * Instead, they cause the width, height, depth, format fields of the
1721 * texture image to be zeroed-out. The GL spec seems to indicate that the
1722 * zero-out behaviour is only used in cases related to memory allocation.
1725 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1726 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1727 _mesa_error(ctx, GL_INVALID_VALUE,
1728 "glTexImage%dD(level=%d)", dimensions, level);
1733 if (border < 0 || border > 1 ||
1734 ((ctx->API != API_OPENGL ||
1735 target == GL_TEXTURE_RECTANGLE_NV ||
1736 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1737 _mesa_error(ctx, GL_INVALID_VALUE,
1738 "glTexImage%dD(border=%d)", dimensions, border);
1742 if (width < 0 || height < 0 || depth < 0) {
1743 _mesa_error(ctx, GL_INVALID_VALUE,
1744 "glTexImage%dD(width, height or depth < 0)", dimensions);
1748 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1749 * combinations of format, internalFormat, and type that can be used.
1750 * Formats and types that require additional extensions (e.g., GL_FLOAT
1751 * requires GL_OES_texture_float) are filtered elsewhere.
1753 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
1754 if (format != internalFormat) {
1755 _mesa_error(ctx, GL_INVALID_OPERATION,
1756 "glTexImage%dD(format = %s, internalFormat = %s)",
1758 _mesa_lookup_enum_by_nr(format),
1759 _mesa_lookup_enum_by_nr(internalFormat));
1763 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
1764 if (err != GL_NO_ERROR) {
1765 _mesa_error(ctx, err,
1766 "glTexImage%dD(format = %s, type = %s)",
1768 _mesa_lookup_enum_by_nr(format),
1769 _mesa_lookup_enum_by_nr(type));
1774 /* Do this simple check before calling the TestProxyTexImage() function */
1775 if (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
1776 sizeOK = (width == height);
1780 * Use the proxy texture driver hook to see if the size/level/etc are
1783 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1784 internalFormat, format,
1785 type, width, height,
1789 /* No GL error is recorded, but we need to zero-out the image dims */
1793 _mesa_error(ctx, GL_INVALID_VALUE,
1794 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1795 dimensions, level, width, height, depth);
1800 /* Check internalFormat */
1801 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1802 _mesa_error(ctx, GL_INVALID_VALUE,
1803 "glTexImage%dD(internalFormat=%s)",
1804 dimensions, _mesa_lookup_enum_by_nr(internalFormat));
1808 /* Check incoming image format and type */
1809 err = _mesa_error_check_format_and_type(ctx, format, type);
1810 if (err != GL_NO_ERROR) {
1811 _mesa_error(ctx, err,
1812 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1813 dimensions, format, type);
1817 /* make sure internal format and format basically agree */
1818 colorFormat = _mesa_is_color_format(format);
1819 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1820 (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1821 (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1822 (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1823 (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1824 _mesa_error(ctx, GL_INVALID_OPERATION,
1825 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1826 dimensions, internalFormat, format);
1830 /* additional checks for ycbcr textures */
1831 if (internalFormat == GL_YCBCR_MESA) {
1832 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1833 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1834 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1836 _mesa_snprintf(message, sizeof(message),
1837 "glTexImage%dD(format/type YCBCR mismatch)",
1839 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1840 return GL_TRUE; /* error */
1842 if (target != GL_TEXTURE_2D &&
1843 target != GL_PROXY_TEXTURE_2D &&
1844 target != GL_TEXTURE_RECTANGLE_NV &&
1845 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1846 _mesa_error(ctx, GL_INVALID_ENUM,
1847 "glTexImage%dD(bad target for YCbCr texture)",
1853 _mesa_snprintf(message, sizeof(message),
1854 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1855 dimensions, border);
1856 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1861 /* additional checks for depth textures */
1862 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1863 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1864 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1865 if (target != GL_TEXTURE_1D &&
1866 target != GL_PROXY_TEXTURE_1D &&
1867 target != GL_TEXTURE_2D &&
1868 target != GL_PROXY_TEXTURE_2D &&
1869 target != GL_TEXTURE_1D_ARRAY &&
1870 target != GL_PROXY_TEXTURE_1D_ARRAY &&
1871 target != GL_TEXTURE_2D_ARRAY &&
1872 target != GL_PROXY_TEXTURE_2D_ARRAY &&
1873 target != GL_TEXTURE_RECTANGLE_ARB &&
1874 target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1875 !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1876 (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4))) {
1877 _mesa_error(ctx, GL_INVALID_ENUM,
1878 "glTexImage%dD(bad target for depth texture)",
1884 /* additional checks for compressed textures */
1885 if (_mesa_is_compressed_format(ctx, internalFormat)) {
1886 if (!target_can_be_compressed(ctx, target, internalFormat)) {
1887 _mesa_error(ctx, GL_INVALID_ENUM,
1888 "glTexImage%dD(target can't be compressed)", dimensions);
1891 if (compressedteximage_only_format(ctx, internalFormat)) {
1892 _mesa_error(ctx, GL_INVALID_OPERATION,
1893 "glTexImage%dD(no compression for format)", dimensions);
1897 _mesa_error(ctx, GL_INVALID_OPERATION,
1898 "glTexImage%dD(border!=0)", dimensions);
1903 /* additional checks for integer textures */
1904 if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1905 (_mesa_is_enum_format_integer(format) !=
1906 _mesa_is_enum_format_integer(internalFormat))) {
1907 _mesa_error(ctx, GL_INVALID_OPERATION,
1908 "glTexImage%dD(integer/non-integer format mismatch)",
1913 if (!mutable_tex_object(ctx, target)) {
1914 _mesa_error(ctx, GL_INVALID_OPERATION,
1915 "glTexImage%dD(immutable texture)", dimensions);
1919 /* if we get here, the parameters are OK */
1925 * Error checking for glCompressedTexImage[123]D().
1926 * \param reason returns reason for error, if any
1927 * \return error code or GL_NO_ERROR or PROXY_ERROR.
1930 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
1931 GLenum target, GLint level,
1932 GLenum internalFormat, GLsizei width,
1933 GLsizei height, GLsizei depth, GLint border,
1936 const GLenum proxyTarget = get_proxy_target(target);
1937 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1939 GLenum choose_format;
1941 GLenum proxy_format;
1942 GLenum error = GL_NO_ERROR;
1943 char *reason = ""; /* no error */
1945 if (!target_can_be_compressed(ctx, target, internalFormat)) {
1947 error = GL_INVALID_ENUM;
1951 /* This will detect any invalid internalFormat value */
1952 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
1953 reason = "internalFormat";
1954 error = GL_INVALID_ENUM;
1958 switch (internalFormat) {
1960 case GL_PALETTE4_RGB8_OES:
1961 case GL_PALETTE4_RGBA8_OES:
1962 case GL_PALETTE4_R5_G6_B5_OES:
1963 case GL_PALETTE4_RGBA4_OES:
1964 case GL_PALETTE4_RGB5_A1_OES:
1965 case GL_PALETTE8_RGB8_OES:
1966 case GL_PALETTE8_RGBA8_OES:
1967 case GL_PALETTE8_R5_G6_B5_OES:
1968 case GL_PALETTE8_RGBA4_OES:
1969 case GL_PALETTE8_RGB5_A1_OES:
1970 _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
1972 proxy_format = choose_format;
1974 /* check level (note that level should be zero or less!) */
1975 if (level > 0 || level < -maxLevels) {
1977 error = GL_INVALID_VALUE;
1981 if (dimensions != 2) {
1982 reason = "compressed paletted textures must be 2D";
1983 error = GL_INVALID_OPERATION;
1987 /* Figure out the expected texture size (in bytes). This will be
1988 * checked against the actual size later.
1990 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
1993 /* This is for the benefit of the TestProxyTexImage below. It expects
1994 * level to be non-negative. OES_compressed_paletted_texture uses a
1995 * weird mechanism where the level specified to glCompressedTexImage2D
1996 * is -(n-1) number of levels in the texture, and the data specifies the
1997 * complete mipmap stack. This is done to ensure the palette is the
1998 * same for all levels.
2005 choose_format = GL_NONE;
2006 choose_type = GL_NONE;
2007 proxy_format = internalFormat;
2010 if (level < 0 || level >= maxLevels) {
2012 error = GL_INVALID_VALUE;
2016 /* Figure out the expected texture size (in bytes). This will be
2017 * checked against the actual size later.
2019 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2023 /* This should really never fail */
2024 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2025 reason = "internalFormat";
2026 error = GL_INVALID_ENUM;
2030 /* No compressed formats support borders at this time */
2032 reason = "border != 0";
2033 error = GL_INVALID_VALUE;
2037 /* For cube map, width must equal height */
2038 if (_mesa_is_cube_face(target) && width != height) {
2039 reason = "width != height";
2040 error = GL_INVALID_VALUE;
2044 /* check image size against compression block size */
2046 gl_format texFormat =
2047 ctx->Driver.ChooseTextureFormat(ctx, target, proxy_format,
2048 choose_format, choose_type);
2051 _mesa_get_format_block_size(texFormat, &bw, &bh);
2052 if ((width > bw && width % bw > 0) ||
2053 (height > bh && height % bh > 0)) {
2055 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
2056 * generated [...] if any parameter combinations are not
2057 * supported by the specific compressed internal format.
2059 reason = "invalid width or height for compression format";
2060 error = GL_INVALID_OPERATION;
2065 /* check image sizes */
2066 if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2067 proxy_format, choose_format,
2069 width, height, depth, border)) {
2070 /* See error comment above */
2071 if (target == proxyTarget) {
2074 reason = "invalid width, height or format";
2075 error = GL_INVALID_OPERATION;
2079 /* check image size in bytes */
2080 if (expectedSize != imageSize) {
2081 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2082 * if <imageSize> is not consistent with the format, dimensions, and
2083 * contents of the specified image.
2085 reason = "imageSize inconsistant with width/height/format";
2086 error = GL_INVALID_VALUE;
2090 if (!mutable_tex_object(ctx, target)) {
2091 reason = "immutable texture";
2092 error = GL_INVALID_OPERATION;
2099 _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)", dimensions, reason);
2106 * Test glTexSubImage[123]D() parameters for errors.
2108 * \param ctx GL context.
2109 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2110 * \param target texture target given by the user.
2111 * \param level image level given by the user.
2112 * \param xoffset sub-image x offset given by the user.
2113 * \param yoffset sub-image y offset given by the user.
2114 * \param zoffset sub-image z offset given by the user.
2115 * \param format pixel data format given by the user.
2116 * \param type pixel data type given by the user.
2117 * \param width image width given by the user.
2118 * \param height image height given by the user.
2119 * \param depth image depth given by the user.
2121 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2123 * Verifies each of the parameters against the constants specified in
2124 * __struct gl_contextRec::Const and the supported extensions, and according
2125 * to the OpenGL specification.
2128 subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
2129 GLenum target, GLint level,
2130 GLint xoffset, GLint yoffset, GLint zoffset,
2131 GLint width, GLint height, GLint depth,
2132 GLenum format, GLenum type )
2136 /* Basic level check */
2137 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2138 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
2142 /* Check for negative sizes */
2144 _mesa_error(ctx, GL_INVALID_VALUE,
2145 "glTexSubImage%dD(width=%d)", dimensions, width);
2148 if (height < 0 && dimensions > 1) {
2149 _mesa_error(ctx, GL_INVALID_VALUE,
2150 "glTexSubImage%dD(height=%d)", dimensions, height);
2153 if (depth < 0 && dimensions > 2) {
2154 _mesa_error(ctx, GL_INVALID_VALUE,
2155 "glTexSubImage%dD(depth=%d)", dimensions, depth);
2159 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2160 * combinations of format and type that can be used. Formats and types
2161 * that require additional extensions (e.g., GL_FLOAT requires
2162 * GL_OES_texture_float) are filtered elsewhere.
2164 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2165 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
2166 if (err != GL_NO_ERROR) {
2167 _mesa_error(ctx, err,
2168 "glTexSubImage%dD(format = %s, type = %s)",
2170 _mesa_lookup_enum_by_nr(format),
2171 _mesa_lookup_enum_by_nr(type));
2176 err = _mesa_error_check_format_and_type(ctx, format, type);
2177 if (err != GL_NO_ERROR) {
2178 _mesa_error(ctx, err,
2179 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
2180 dimensions, format, type);
2189 * Do second part of glTexSubImage which depends on the destination texture.
2190 * \return GL_TRUE if error recorded, GL_FALSE otherwise
2193 subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
2194 GLenum target, GLint level,
2195 GLint xoffset, GLint yoffset, GLint zoffset,
2196 GLint width, GLint height, GLint depth,
2197 GLenum format, GLenum type,
2198 const struct gl_texture_image *destTex )
2201 /* undefined image level */
2202 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
2206 if (xoffset < -((GLint)destTex->Border)) {
2207 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
2211 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
2212 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
2216 if (dimensions > 1) {
2217 GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destTex->Border;
2218 if (yoffset < -yBorder) {
2219 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
2223 if (yoffset + height > (GLint) destTex->Height + yBorder) {
2224 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
2229 if (dimensions > 2) {
2230 GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : destTex->Border;
2231 if (zoffset < -zBorder) {
2232 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
2235 if (zoffset + depth > (GLint) destTex->Depth + zBorder) {
2236 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
2241 if (_mesa_is_format_compressed(destTex->TexFormat)) {
2244 if (compressedteximage_only_format(ctx, destTex->InternalFormat)) {
2245 _mesa_error(ctx, GL_INVALID_OPERATION,
2246 "glTexSubImage%dD(no compression for format)", dimensions);
2250 /* do tests which depend on compression block size */
2251 _mesa_get_format_block_size(destTex->TexFormat, &bw, &bh);
2253 /* offset must be multiple of block size */
2254 if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
2255 _mesa_error(ctx, GL_INVALID_OPERATION,
2256 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
2257 dimensions, xoffset, yoffset);
2260 /* size must be multiple of bw by bh or equal to whole texture size */
2261 if ((width % bw != 0) && (GLuint) width != destTex->Width) {
2262 _mesa_error(ctx, GL_INVALID_OPERATION,
2263 "glTexSubImage%dD(width = %d)", dimensions, width);
2266 if ((height % bh != 0) && (GLuint) height != destTex->Height) {
2267 _mesa_error(ctx, GL_INVALID_OPERATION,
2268 "glTexSubImage%dD(height = %d)", dimensions, height);
2273 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2274 /* both source and dest must be integer-valued, or neither */
2275 if (_mesa_is_format_integer_color(destTex->TexFormat) !=
2276 _mesa_is_enum_format_integer(format)) {
2277 _mesa_error(ctx, GL_INVALID_OPERATION,
2278 "glTexSubImage%dD(integer/non-integer format mismatch)",
2289 * Test glCopyTexImage[12]D() parameters for errors.
2291 * \param ctx GL context.
2292 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2293 * \param target texture target given by the user.
2294 * \param level image level given by the user.
2295 * \param internalFormat internal format given by the user.
2296 * \param width image width given by the user.
2297 * \param height image height given by the user.
2298 * \param border texture border.
2300 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2302 * Verifies each of the parameters against the constants specified in
2303 * __struct gl_contextRec::Const and the supported extensions, and according
2304 * to the OpenGL specification.
2307 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2308 GLenum target, GLint level, GLint internalFormat,
2309 GLint width, GLint height, GLint border )
2311 const GLenum proxyTarget = get_proxy_target(target);
2312 const GLenum type = GL_FLOAT;
2317 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2318 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2319 dimensions, _mesa_lookup_enum_by_nr(target));
2323 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
2324 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2325 _mesa_error(ctx, GL_INVALID_VALUE,
2326 "glCopyTexImage%dD(level=%d)", dimensions, level);
2330 /* Check that the source buffer is complete */
2331 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2332 if (ctx->ReadBuffer->_Status == 0) {
2333 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2335 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2336 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2337 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2341 if (ctx->ReadBuffer->Visual.samples > 0) {
2342 _mesa_error(ctx, GL_INVALID_OPERATION,
2343 "glCopyTexImage%dD(multisample FBO)",
2350 if (border < 0 || border > 1 ||
2351 ((ctx->API != API_OPENGL ||
2352 target == GL_TEXTURE_RECTANGLE_NV ||
2353 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2354 _mesa_error(ctx, GL_INVALID_VALUE,
2355 "glCopyTexImage%dD(border=%d)", dimensions, border);
2359 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2362 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2363 switch (internalFormat) {
2368 case GL_LUMINANCE_ALPHA:
2371 _mesa_error(ctx, GL_INVALID_VALUE,
2372 "glCopyTexImage%dD(internalFormat)", dimensions);
2377 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2378 if (baseFormat < 0) {
2379 _mesa_error(ctx, GL_INVALID_VALUE,
2380 "glCopyTexImage%dD(internalFormat)", dimensions);
2384 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2385 _mesa_error(ctx, GL_INVALID_OPERATION,
2386 "glCopyTexImage%dD(missing readbuffer)", dimensions);
2390 /* From the EXT_texture_integer spec:
2392 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2393 * if the texture internalformat is an integer format and the read color
2394 * buffer is not an integer format, or if the internalformat is not an
2395 * integer format and the read color buffer is an integer format."
2397 if (_mesa_is_color_format(internalFormat)) {
2398 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2400 if (_mesa_is_enum_format_integer(rb->InternalFormat) !=
2401 _mesa_is_enum_format_integer(internalFormat)) {
2402 _mesa_error(ctx, GL_INVALID_OPERATION,
2403 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2408 /* Do size, level checking */
2409 sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
2410 ? (width == height) : 1;
2412 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
2413 internalFormat, baseFormat,
2414 type, width, height,
2418 if (dimensions == 1) {
2419 _mesa_error(ctx, GL_INVALID_VALUE,
2420 "glCopyTexImage1D(width=%d)", width);
2423 ASSERT(dimensions == 2);
2424 _mesa_error(ctx, GL_INVALID_VALUE,
2425 "glCopyTexImage2D(width=%d, height=%d)", width, height);
2430 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2431 if (!target_can_be_compressed(ctx, target, internalFormat)) {
2432 _mesa_error(ctx, GL_INVALID_ENUM,
2433 "glCopyTexImage%dD(target)", dimensions);
2436 if (compressedteximage_only_format(ctx, internalFormat)) {
2437 _mesa_error(ctx, GL_INVALID_OPERATION,
2438 "glCopyTexImage%dD(no compression for format)", dimensions);
2442 _mesa_error(ctx, GL_INVALID_OPERATION,
2443 "glCopyTexImage%dD(border!=0)", dimensions);
2448 if (!mutable_tex_object(ctx, target)) {
2449 _mesa_error(ctx, GL_INVALID_OPERATION,
2450 "glCopyTexImage%dD(immutable texture)", dimensions);
2454 /* if we get here, the parameters are OK */
2460 * Test glCopyTexSubImage[12]D() parameters for errors.
2461 * Note that this is the first part of error checking.
2462 * See also copytexsubimage_error_check2() below for the second part.
2464 * \param ctx GL context.
2465 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2466 * \param target texture target given by the user.
2467 * \param level image level given by the user.
2469 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2472 copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
2473 GLenum target, GLint level)
2475 /* Check that the source buffer is complete */
2476 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2477 if (ctx->ReadBuffer->_Status == 0) {
2478 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2480 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2481 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2482 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2486 if (ctx->ReadBuffer->Visual.samples > 0) {
2487 _mesa_error(ctx, GL_INVALID_OPERATION,
2488 "glCopyTexSubImage%dD(multisample FBO)",
2494 /* check target (proxies not allowed) */
2495 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2496 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
2497 dimensions, _mesa_lookup_enum_by_nr(target));
2502 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2503 _mesa_error(ctx, GL_INVALID_VALUE,
2504 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2513 * Second part of error checking for glCopyTexSubImage[12]D().
2514 * \param xoffset sub-image x offset given by the user.
2515 * \param yoffset sub-image y offset given by the user.
2516 * \param zoffset sub-image z offset given by the user.
2517 * \param width image width given by the user.
2518 * \param height image height given by the user.
2521 copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
2522 GLenum target, GLint level,
2523 GLint xoffset, GLint yoffset, GLint zoffset,
2524 GLsizei width, GLsizei height,
2525 const struct gl_texture_image *teximage )
2527 /* check that dest tex image exists */
2529 _mesa_error(ctx, GL_INVALID_OPERATION,
2530 "glCopyTexSubImage%dD(undefined texture level: %d)",
2537 _mesa_error(ctx, GL_INVALID_VALUE,
2538 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
2541 if (dimensions > 1 && height < 0) {
2542 _mesa_error(ctx, GL_INVALID_VALUE,
2543 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
2547 /* check x/y offsets */
2548 if (xoffset < -((GLint)teximage->Border)) {
2549 _mesa_error(ctx, GL_INVALID_VALUE,
2550 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
2553 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
2554 _mesa_error(ctx, GL_INVALID_VALUE,
2555 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
2558 if (dimensions > 1) {
2559 GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : teximage->Border;
2560 if (yoffset < -yBorder) {
2561 _mesa_error(ctx, GL_INVALID_VALUE,
2562 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
2565 /* NOTE: we're adding the border here, not subtracting! */
2566 if (yoffset + height > (GLint) teximage->Height + yBorder) {
2567 _mesa_error(ctx, GL_INVALID_VALUE,
2568 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
2573 /* check z offset */
2574 if (dimensions > 2) {
2575 GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : teximage->Border;
2576 if (zoffset < -zBorder) {
2577 _mesa_error(ctx, GL_INVALID_VALUE,
2578 "glCopyTexSubImage%dD(zoffset)", dimensions);
2581 if (zoffset > (GLint) teximage->Depth + zBorder) {
2582 _mesa_error(ctx, GL_INVALID_VALUE,
2583 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
2588 if (_mesa_is_format_compressed(teximage->TexFormat)) {
2589 if (compressedteximage_only_format(ctx, teximage->InternalFormat)) {
2590 _mesa_error(ctx, GL_INVALID_OPERATION,
2591 "glCopyTexSubImage%dD(no compression for format)", dimensions);
2594 /* offset must be multiple of 4 */
2595 if ((xoffset & 3) || (yoffset & 3)) {
2596 _mesa_error(ctx, GL_INVALID_VALUE,
2597 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions);
2600 /* size must be multiple of 4 */
2601 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
2602 _mesa_error(ctx, GL_INVALID_VALUE,
2603 "glCopyTexSubImage%dD(width)", dimensions);
2606 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
2607 _mesa_error(ctx, GL_INVALID_VALUE,
2608 "glCopyTexSubImage%dD(height)", dimensions);
2613 if (teximage->InternalFormat == GL_YCBCR_MESA) {
2614 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2618 if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
2619 _mesa_error(ctx, GL_INVALID_OPERATION,
2620 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2621 dimensions, teximage->_BaseFormat);
2625 /* From the EXT_texture_integer spec:
2627 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2628 * if the texture internalformat is an integer format and the read color
2629 * buffer is not an integer format, or if the internalformat is not an
2630 * integer format and the read color buffer is an integer format."
2632 if (_mesa_is_color_format(teximage->InternalFormat)) {
2633 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2635 if (_mesa_is_format_integer_color(rb->Format) !=
2636 _mesa_is_format_integer_color(teximage->TexFormat)) {
2637 _mesa_error(ctx, GL_INVALID_OPERATION,
2638 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2643 /* if we get here, the parameters are OK */
2648 /** Callback info for walking over FBO hash table */
2651 struct gl_context *ctx;
2652 struct gl_texture_object *texObj;
2658 * Check render to texture callback. Called from _mesa_HashWalk().
2661 check_rtt_cb(GLuint key, void *data, void *userData)
2663 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2664 const struct cb_info *info = (struct cb_info *) userData;
2665 struct gl_context *ctx = info->ctx;
2666 const struct gl_texture_object *texObj = info->texObj;
2667 const GLuint level = info->level, face = info->face;
2669 /* If this is a user-created FBO */
2670 if (_mesa_is_user_fbo(fb)) {
2672 /* check if any of the FBO's attachments point to 'texObj' */
2673 for (i = 0; i < BUFFER_COUNT; i++) {
2674 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2675 if (att->Type == GL_TEXTURE &&
2676 att->Texture == texObj &&
2677 att->TextureLevel == level &&
2678 att->CubeMapFace == face) {
2679 ASSERT(_mesa_get_attachment_teximage(att));
2680 /* Tell driver about the new renderbuffer texture */
2681 ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2682 /* Mark fb status as indeterminate to force re-validation */
2691 * When a texture image is specified we have to check if it's bound to
2692 * any framebuffer objects (render to texture) in order to detect changes
2693 * in size or format since that effects FBO completeness.
2694 * Any FBOs rendering into the texture must be re-validated.
2697 _mesa_update_fbo_texture(struct gl_context *ctx,
2698 struct gl_texture_object *texObj,
2699 GLuint face, GLuint level)
2701 /* Only check this texture if it's been marked as RenderToTexture */
2702 if (texObj->_RenderToTexture) {
2703 struct cb_info info;
2705 info.texObj = texObj;
2708 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2714 * If the texture object's GenerateMipmap flag is set and we've
2715 * changed the texture base level image, regenerate the rest of the
2716 * mipmap levels now.
2719 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2720 struct gl_texture_object *texObj, GLint level)
2722 ASSERT(target != GL_TEXTURE_CUBE_MAP);
2723 if (texObj->GenerateMipmap &&
2724 level == texObj->BaseLevel &&
2725 level < texObj->MaxLevel) {
2726 ASSERT(ctx->Driver.GenerateMipmap);
2727 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2732 /** Debug helper: override the user-requested internal format */
2734 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2737 if (internalFormat == GL_RGBA16F_ARB ||
2738 internalFormat == GL_RGBA32F_ARB) {
2739 printf("Convert rgba float tex to int %d x %d\n", width, height);
2742 else if (internalFormat == GL_RGB16F_ARB ||
2743 internalFormat == GL_RGB32F_ARB) {
2744 printf("Convert rgb float tex to int %d x %d\n", width, height);
2747 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2748 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2749 printf("Convert luminance float tex to int %d x %d\n", width, height);
2750 return GL_LUMINANCE_ALPHA;
2752 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2753 internalFormat == GL_LUMINANCE32F_ARB) {
2754 printf("Convert luminance float tex to int %d x %d\n", width, height);
2755 return GL_LUMINANCE;
2757 else if (internalFormat == GL_ALPHA16F_ARB ||
2758 internalFormat == GL_ALPHA32F_ARB) {
2759 printf("Convert luminance float tex to int %d x %d\n", width, height);
2763 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2764 internalFormat = GL_RGBA;
2768 return internalFormat;
2771 return internalFormat;
2777 * Choose the actual hardware format for a texture image.
2778 * Try to use the same format as the previous image level when possible.
2779 * Otherwise, ask the driver for the best format.
2780 * It's important to try to choose a consistant format for all levels
2781 * for efficient texture memory layout/allocation. In particular, this
2782 * comes up during automatic mipmap generation.
2785 _mesa_choose_texture_format(struct gl_context *ctx,
2786 struct gl_texture_object *texObj,
2787 GLenum target, GLint level,
2788 GLenum internalFormat, GLenum format, GLenum type)
2792 /* see if we've already chosen a format for the previous level */
2794 struct gl_texture_image *prevImage =
2795 _mesa_select_tex_image(ctx, texObj, target, level - 1);
2796 /* See if the prev level is defined and has an internal format which
2797 * matches the new internal format.
2800 prevImage->Width > 0 &&
2801 prevImage->InternalFormat == internalFormat) {
2802 /* use the same format */
2803 ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
2804 return prevImage->TexFormat;
2808 /* If the application requested compression to an S3TC format but we don't
2809 * have the DTXn library, force a generic compressed format instead.
2811 if (internalFormat != format) {
2812 const GLenum before = internalFormat;
2814 switch (internalFormat) {
2815 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2816 if (!ctx->Mesa_DXTn)
2817 internalFormat = GL_COMPRESSED_RGB;
2819 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2820 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
2821 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
2822 if (!ctx->Mesa_DXTn)
2823 internalFormat = GL_COMPRESSED_RGBA;
2829 if (before != internalFormat) {
2831 "DXT compression requested (%s), "
2832 "but libtxc_dxtn library not installed. Using %s "
2834 _mesa_lookup_enum_by_nr(before),
2835 _mesa_lookup_enum_by_nr(internalFormat));
2839 /* choose format from scratch */
2840 f = ctx->Driver.ChooseTextureFormat(ctx, texObj->Target, internalFormat,
2842 ASSERT(f != MESA_FORMAT_NONE);
2847 * Adjust pixel unpack params and image dimensions to strip off the
2848 * one-pixel texture border.
2850 * Gallium and intel don't support texture borders. They've seldem been used
2851 * and seldom been implemented correctly anyway.
2853 * \param unpackNew returns the new pixel unpack parameters
2856 strip_texture_border(GLenum target,
2857 GLint *width, GLint *height, GLint *depth,
2858 const struct gl_pixelstore_attrib *unpack,
2859 struct gl_pixelstore_attrib *unpackNew)
2865 *unpackNew = *unpack;
2867 if (unpackNew->RowLength == 0)
2868 unpackNew->RowLength = *width;
2870 if (unpackNew->ImageHeight == 0)
2871 unpackNew->ImageHeight = *height;
2873 assert(*width >= 3);
2874 unpackNew->SkipPixels++; /* skip the border */
2875 *width = *width - 2; /* reduce the width by two border pixels */
2877 /* The min height of a texture with a border is 3 */
2878 if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2879 unpackNew->SkipRows++; /* skip the border */
2880 *height = *height - 2; /* reduce the height by two border pixels */
2883 if (*depth >= 3 && target != GL_TEXTURE_2D_ARRAY) {
2884 unpackNew->SkipImages++; /* skip the border */
2885 *depth = *depth - 2; /* reduce the depth by two border pixels */
2891 * Common code to implement all the glTexImage1D/2D/3D functions
2892 * as well as glCompressedTexImage1D/2D/3D.
2893 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2894 * \param format the user's image format (only used if !compressed)
2895 * \param type the user's image type (only used if !compressed)
2896 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2899 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2900 GLenum target, GLint level, GLint internalFormat,
2901 GLsizei width, GLsizei height, GLsizei depth,
2902 GLint border, GLenum format, GLenum type,
2903 GLsizei imageSize, const GLvoid *pixels)
2905 const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2907 struct gl_pixelstore_attrib unpack_no_border;
2908 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2910 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2912 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2915 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2917 _mesa_lookup_enum_by_nr(target), level,
2918 _mesa_lookup_enum_by_nr(internalFormat),
2919 width, height, depth, border, pixels);
2922 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2924 _mesa_lookup_enum_by_nr(target), level,
2925 _mesa_lookup_enum_by_nr(internalFormat),
2926 width, height, depth, border,
2927 _mesa_lookup_enum_by_nr(format),
2928 _mesa_lookup_enum_by_nr(type), pixels);
2931 internalFormat = override_internal_format(internalFormat, width, height);
2933 /* target error checking */
2934 if (!legal_teximage_target(ctx, dims, target)) {
2935 _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2936 func, dims, _mesa_lookup_enum_by_nr(target));
2940 /* general error checking */
2942 error = compressed_texture_error_check(ctx, dims, target, level,
2944 width, height, depth,
2948 error = texture_error_check(ctx, dims, target, level, internalFormat,
2949 format, type, width, height, depth, border);
2953 /* Here we convert a cpal compressed image into a regular glTexImage2D
2954 * call by decompressing the texture. If we really want to support cpal
2955 * textures in any driver this would have to be changed.
2957 if (compressed && !error && dims == 2) {
2958 switch (internalFormat) {
2959 case GL_PALETTE4_RGB8_OES:
2960 case GL_PALETTE4_RGBA8_OES:
2961 case GL_PALETTE4_R5_G6_B5_OES:
2962 case GL_PALETTE4_RGBA4_OES:
2963 case GL_PALETTE4_RGB5_A1_OES:
2964 case GL_PALETTE8_RGB8_OES:
2965 case GL_PALETTE8_RGBA8_OES:
2966 case GL_PALETTE8_R5_G6_B5_OES:
2967 case GL_PALETTE8_RGBA4_OES:
2968 case GL_PALETTE8_RGB5_A1_OES:
2969 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2970 width, height, imageSize, pixels);
2976 if (_mesa_is_proxy_texture(target)) {
2977 /* Proxy texture: just clear or set state depending on error checking */
2978 struct gl_texture_image *texImage =
2979 get_proxy_tex_image(ctx, target, level);
2980 gl_format texFormat = MESA_FORMAT_NONE;
2983 /* No parameter errors. Choose a texture format and see if we
2984 * can really allocate the texture.
2986 struct gl_texture_object *texObj =
2987 _mesa_get_current_tex_object(ctx, target);
2988 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2989 internalFormat, format, type);
2990 if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
2991 error = PROXY_ERROR;
2995 if (error == PROXY_ERROR) {
2996 /* image too large, etc. Clear all proxy texture image parameters. */
2998 clear_teximage_fields(texImage);
3000 else if (error == GL_FALSE) {
3001 /* no error: store the teximage parameters */
3003 _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3004 border, internalFormat, texFormat);
3007 /* other, regular error (was already recorded) */
3011 /* non-proxy target */
3012 const GLuint face = _mesa_tex_target_to_face(target);
3013 struct gl_texture_object *texObj;
3014 struct gl_texture_image *texImage;
3017 return; /* error was recorded */
3020 /* Allow a hardware driver to just strip out the border, to provide
3021 * reliable but slightly incorrect hardware rendering instead of
3022 * rarely-tested software fallback rendering.
3024 if (border && ctx->Const.StripTextureBorder) {
3025 strip_texture_border(target, &width, &height, &depth, unpack,
3028 unpack = &unpack_no_border;
3031 if (ctx->NewState & _NEW_PIXEL)
3032 _mesa_update_state(ctx);
3034 texObj = _mesa_get_current_tex_object(ctx, target);
3036 _mesa_lock_texture(ctx, texObj);
3038 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3041 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3044 gl_format texFormat;
3046 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3048 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3049 internalFormat, format,
3052 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3053 _mesa_init_teximage_fields(ctx, texImage,
3054 width, height, depth,
3055 border, internalFormat, texFormat);
3057 /* Give the texture to the driver. <pixels> may be null. */
3059 ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3063 ctx->Driver.TexImage(ctx, dims, texImage, format,
3064 type, pixels, unpack);
3067 check_gen_mipmap(ctx, target, texObj, level);
3069 _mesa_update_fbo_texture(ctx, texObj, face, level);
3071 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3074 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3078 _mesa_unlock_texture(ctx, texObj);
3084 * Called from the API. Note that width includes the border.
3087 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3088 GLsizei width, GLint border, GLenum format,
3089 GLenum type, const GLvoid *pixels )
3091 GET_CURRENT_CONTEXT(ctx);
3092 teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3093 border, format, type, 0, pixels);
3098 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3099 GLsizei width, GLsizei height, GLint border,
3100 GLenum format, GLenum type,
3101 const GLvoid *pixels )
3103 GET_CURRENT_CONTEXT(ctx);
3104 teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3105 border, format, type, 0, pixels);
3110 * Called by the API or display list executor.
3111 * Note that width and height include the border.
3114 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3115 GLsizei width, GLsizei height, GLsizei depth,
3116 GLint border, GLenum format, GLenum type,
3117 const GLvoid *pixels )
3119 GET_CURRENT_CONTEXT(ctx);
3120 teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
3121 width, height, depth,
3122 border, format, type, 0, pixels);
3127 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3128 GLsizei width, GLsizei height, GLsizei depth,
3129 GLint border, GLenum format, GLenum type,
3130 const GLvoid *pixels )
3132 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3133 depth, border, format, type, pixels);
3137 #if FEATURE_OES_EGL_image
3139 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3141 struct gl_texture_object *texObj;
3142 struct gl_texture_image *texImage;
3144 GET_CURRENT_CONTEXT(ctx);
3145 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3149 valid_target = ctx->Extensions.OES_EGL_image;
3151 case GL_TEXTURE_EXTERNAL_OES:
3152 valid_target = ctx->Extensions.OES_EGL_image_external;
3155 valid_target = false;
3159 if (!valid_target) {
3160 _mesa_error(ctx, GL_INVALID_ENUM,
3161 "glEGLImageTargetTexture2D(target=%d)", target);
3165 if (ctx->NewState & _NEW_PIXEL)
3166 _mesa_update_state(ctx);
3168 texObj = _mesa_get_current_tex_object(ctx, target);
3169 _mesa_lock_texture(ctx, texObj);
3171 if (texObj->Immutable) {
3172 _mesa_error(ctx, GL_INVALID_OPERATION,
3173 "glEGLImageTargetTexture2D(texture is immutable)");
3174 _mesa_unlock_texture(ctx, texObj);
3178 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3180 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3182 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3184 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3185 texObj, texImage, image);
3187 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3189 _mesa_unlock_texture(ctx, texObj);
3197 * Implement all the glTexSubImage1/2/3D() functions.
3200 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3201 GLint xoffset, GLint yoffset, GLint zoffset,
3202 GLsizei width, GLsizei height, GLsizei depth,
3203 GLenum format, GLenum type, const GLvoid *pixels )
3205 struct gl_texture_object *texObj;
3206 struct gl_texture_image *texImage;
3208 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3210 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3211 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3213 _mesa_lookup_enum_by_nr(target), level,
3214 xoffset, yoffset, zoffset, width, height, depth,
3215 _mesa_lookup_enum_by_nr(format),
3216 _mesa_lookup_enum_by_nr(type), pixels);
3218 /* check target (proxies not allowed) */
3219 if (!legal_texsubimage_target(ctx, dims, target)) {
3220 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3221 dims, _mesa_lookup_enum_by_nr(target));
3225 if (ctx->NewState & _NEW_PIXEL)
3226 _mesa_update_state(ctx);
3228 if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
3229 width, height, depth, format, type)) {
3230 return; /* error was detected */
3233 texObj = _mesa_get_current_tex_object(ctx, target);
3235 _mesa_lock_texture(ctx, texObj);
3237 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3239 if (subtexture_error_check2(ctx, dims, target, level,
3240 xoffset, yoffset, zoffset,
3241 width, height, depth,
3242 format, type, texImage)) {
3243 /* error was recorded */
3245 else if (width > 0 && height > 0 && depth > 0) {
3246 /* If we have a border, offset=-1 is legal. Bias by border width. */
3249 if (target != GL_TEXTURE_2D_ARRAY)
3250 zoffset += texImage->Border;
3253 if (target != GL_TEXTURE_1D_ARRAY)
3254 yoffset += texImage->Border;
3257 xoffset += texImage->Border;
3260 ctx->Driver.TexSubImage(ctx, dims, texImage,
3261 xoffset, yoffset, zoffset,
3262 width, height, depth,
3263 format, type, pixels, &ctx->Unpack);
3265 check_gen_mipmap(ctx, target, texObj, level);
3267 ctx->NewState |= _NEW_TEXTURE;
3270 _mesa_unlock_texture(ctx, texObj);
3275 _mesa_TexSubImage1D( GLenum target, GLint level,
3276 GLint xoffset, GLsizei width,
3277 GLenum format, GLenum type,
3278 const GLvoid *pixels )
3280 GET_CURRENT_CONTEXT(ctx);
3281 texsubimage(ctx, 1, target, level,
3284 format, type, pixels);
3289 _mesa_TexSubImage2D( GLenum target, GLint level,
3290 GLint xoffset, GLint yoffset,
3291 GLsizei width, GLsizei height,
3292 GLenum format, GLenum type,
3293 const GLvoid *pixels )
3295 GET_CURRENT_CONTEXT(ctx);
3296 texsubimage(ctx, 2, target, level,
3297 xoffset, yoffset, 0,
3299 format, type, pixels);
3305 _mesa_TexSubImage3D( GLenum target, GLint level,
3306 GLint xoffset, GLint yoffset, GLint zoffset,
3307 GLsizei width, GLsizei height, GLsizei depth,
3308 GLenum format, GLenum type,
3309 const GLvoid *pixels )
3311 GET_CURRENT_CONTEXT(ctx);
3312 texsubimage(ctx, 3, target, level,
3313 xoffset, yoffset, zoffset,
3314 width, height, depth,
3315 format, type, pixels);
3321 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3322 * from. This depends on whether the texture contains color or depth values.
3324 static struct gl_renderbuffer *
3325 get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
3327 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3328 /* reading from depth/stencil buffer */
3329 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3332 /* copying from color buffer */
3333 return ctx->ReadBuffer->_ColorReadBuffer;
3340 * Implement the glCopyTexImage1/2D() functions.
3343 copyteximage(struct gl_context *ctx, GLuint dims,
3344 GLenum target, GLint level, GLenum internalFormat,
3345 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3347 struct gl_texture_object *texObj;
3348 struct gl_texture_image *texImage;
3349 const GLuint face = _mesa_tex_target_to_face(target);
3351 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3353 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3354 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3356 _mesa_lookup_enum_by_nr(target), level,
3357 _mesa_lookup_enum_by_nr(internalFormat),
3358 x, y, width, height, border);
3360 if (ctx->NewState & NEW_COPY_TEX_STATE)
3361 _mesa_update_state(ctx);
3363 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3364 width, height, border))
3367 texObj = _mesa_get_current_tex_object(ctx, target);
3369 if (border && ctx->Const.StripTextureBorder) {
3371 width -= border * 2;
3374 height -= border * 2;
3379 _mesa_lock_texture(ctx, texObj);
3381 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3384 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3387 /* choose actual hw format */
3388 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
3393 if (legal_texture_size(ctx, texFormat, width, height, 1)) {
3394 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3396 /* Free old texture image */
3397 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3399 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3400 border, internalFormat, texFormat);
3402 /* Allocate texture memory (no pixel data yet) */
3403 ctx->Driver.TexImage(ctx, dims, texImage,
3405 NULL, &ctx->Unpack);
3407 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3409 struct gl_renderbuffer *srcRb =
3410 get_copy_tex_image_source(ctx, texImage->TexFormat);
3412 ctx->Driver.CopyTexSubImage(ctx, dims, texImage, dstX, dstY, dstZ,
3413 srcRb, srcX, srcY, width, height);
3416 check_gen_mipmap(ctx, target, texObj, level);
3418 _mesa_update_fbo_texture(ctx, texObj, face, level);
3420 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
3423 /* probably too large of image */
3424 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3428 _mesa_unlock_texture(ctx, texObj);
3434 _mesa_CopyTexImage1D( GLenum target, GLint level,
3435 GLenum internalFormat,
3437 GLsizei width, GLint border )
3439 GET_CURRENT_CONTEXT(ctx);
3440 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3446 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3447 GLint x, GLint y, GLsizei width, GLsizei height,
3450 GET_CURRENT_CONTEXT(ctx);
3451 copyteximage(ctx, 2, target, level, internalFormat,
3452 x, y, width, height, border);
3458 * Implementation for glCopyTexSubImage1/2/3D() functions.
3461 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3462 GLint xoffset, GLint yoffset, GLint zoffset,
3463 GLint x, GLint y, GLsizei width, GLsizei height)
3465 struct gl_texture_object *texObj;
3466 struct gl_texture_image *texImage;
3468 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3470 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3471 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3473 _mesa_lookup_enum_by_nr(target),
3474 level, xoffset, yoffset, zoffset, x, y, width, height);
3476 if (ctx->NewState & NEW_COPY_TEX_STATE)
3477 _mesa_update_state(ctx);
3479 if (copytexsubimage_error_check1(ctx, dims, target, level))
3482 texObj = _mesa_get_current_tex_object(ctx, target);
3484 _mesa_lock_texture(ctx, texObj);
3486 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3488 if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
3489 zoffset, width, height, texImage)) {
3490 /* error was recored */
3493 /* If we have a border, offset=-1 is legal. Bias by border width. */
3496 if (target != GL_TEXTURE_2D_ARRAY)
3497 zoffset += texImage->Border;
3500 if (target != GL_TEXTURE_1D_ARRAY)
3501 yoffset += texImage->Border;
3504 xoffset += texImage->Border;
3507 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3509 struct gl_renderbuffer *srcRb =
3510 get_copy_tex_image_source(ctx, texImage->TexFormat);
3512 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3513 xoffset, yoffset, zoffset,
3514 srcRb, x, y, width, height);
3516 check_gen_mipmap(ctx, target, texObj, level);
3518 ctx->NewState |= _NEW_TEXTURE;
3522 _mesa_unlock_texture(ctx, texObj);
3527 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3528 GLint xoffset, GLint x, GLint y, GLsizei width )
3530 GET_CURRENT_CONTEXT(ctx);
3531 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
3537 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3538 GLint xoffset, GLint yoffset,
3539 GLint x, GLint y, GLsizei width, GLsizei height )
3541 GET_CURRENT_CONTEXT(ctx);
3542 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3549 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3550 GLint xoffset, GLint yoffset, GLint zoffset,
3551 GLint x, GLint y, GLsizei width, GLsizei height )
3553 GET_CURRENT_CONTEXT(ctx);
3554 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3555 x, y, width, height);
3561 /**********************************************************************/
3562 /****** Compressed Textures ******/
3563 /**********************************************************************/
3567 * Error checking for glCompressedTexSubImage[123]D().
3568 * \warning There are some bad assumptions here about the size of compressed
3569 * texture tiles (multiple of 4) used to test the validity of the
3570 * offset and size parameters.
3571 * \return error code or GL_NO_ERROR.
3574 compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3575 GLenum target, GLint level,
3576 GLint xoffset, GLint yoffset, GLint zoffset,
3577 GLsizei width, GLsizei height, GLsizei depth,
3578 GLenum format, GLsizei imageSize)
3580 GLint expectedSize, maxLevels = 0, maxTextureSize;
3584 if (dimensions == 1) {
3585 /* 1D compressed textures not allowed */
3586 return GL_INVALID_ENUM;
3588 else if (dimensions == 2) {
3589 if (target == GL_PROXY_TEXTURE_2D) {
3590 maxLevels = ctx->Const.MaxTextureLevels;
3592 else if (target == GL_TEXTURE_2D) {
3593 maxLevels = ctx->Const.MaxTextureLevels;
3595 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3596 if (!ctx->Extensions.ARB_texture_cube_map)
3597 return GL_INVALID_ENUM; /*target*/
3598 maxLevels = ctx->Const.MaxCubeTextureLevels;
3600 else if (_mesa_is_cube_face(target)) {
3601 if (!ctx->Extensions.ARB_texture_cube_map)
3602 return GL_INVALID_ENUM; /*target*/
3603 maxLevels = ctx->Const.MaxCubeTextureLevels;
3606 return GL_INVALID_ENUM; /*target*/
3609 else if (dimensions == 3) {
3610 /* 3D compressed textures not allowed */
3611 return GL_INVALID_ENUM;
3614 maxTextureSize = 1 << (maxLevels - 1);
3616 /* this will catch any invalid compressed format token */
3617 if (!_mesa_is_compressed_format(ctx, format))
3618 return GL_INVALID_ENUM;
3620 if (width < 1 || width > maxTextureSize)
3621 return GL_INVALID_VALUE;
3623 if ((height < 1 || height > maxTextureSize)
3625 return GL_INVALID_VALUE;
3627 if (level < 0 || level >= maxLevels)
3628 return GL_INVALID_VALUE;
3631 * do checks which depend on compression block size
3633 get_compressed_block_size(format, &bw, &bh);
3635 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3636 return GL_INVALID_VALUE;
3638 if ((width % bw != 0) && width != 2 && width != 1)
3639 return GL_INVALID_VALUE;
3641 if ((height % bh != 0) && height != 2 && height != 1)
3642 return GL_INVALID_VALUE;
3644 expectedSize = compressed_tex_size(width, height, depth, format);
3645 if (expectedSize != imageSize)
3646 return GL_INVALID_VALUE;
3653 * Do second part of glCompressedTexSubImage error checking.
3654 * \return GL_TRUE if error found, GL_FALSE otherwise.
3657 compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3658 GLsizei width, GLsizei height,
3659 GLsizei depth, GLenum format,
3660 struct gl_texture_image *texImage)
3663 if ((GLint) format != texImage->InternalFormat) {
3664 _mesa_error(ctx, GL_INVALID_OPERATION,
3665 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3669 if (compressedteximage_only_format(ctx, format)) {
3670 _mesa_error(ctx, GL_INVALID_OPERATION,
3671 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3676 if (((width == 1 || width == 2) &&
3677 width != (GLsizei) texImage->Width) ||
3678 (width > (GLsizei) texImage->Width)) {
3679 _mesa_error(ctx, GL_INVALID_VALUE,
3680 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3685 if (((height == 1 || height == 2) &&
3686 height != (GLsizei) texImage->Height) ||
3687 (height > (GLsizei) texImage->Height)) {
3688 _mesa_error(ctx, GL_INVALID_VALUE,
3689 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3695 if (((depth == 1 || depth == 2) &&
3696 depth != (GLsizei) texImage->Depth) ||
3697 (depth > (GLsizei) texImage->Depth)) {
3698 _mesa_error(ctx, GL_INVALID_VALUE,
3699 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3709 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3710 GLenum internalFormat, GLsizei width,
3711 GLint border, GLsizei imageSize,
3714 GET_CURRENT_CONTEXT(ctx);
3715 teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
3716 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
3721 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3722 GLenum internalFormat, GLsizei width,
3723 GLsizei height, GLint border, GLsizei imageSize,
3726 GET_CURRENT_CONTEXT(ctx);
3727 teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
3728 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
3733 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3734 GLenum internalFormat, GLsizei width,
3735 GLsizei height, GLsizei depth, GLint border,
3736 GLsizei imageSize, const GLvoid *data)
3738 GET_CURRENT_CONTEXT(ctx);
3739 teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
3740 width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
3745 * Common helper for glCompressedTexSubImage1/2/3D().
3748 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3749 GLint xoffset, GLint yoffset, GLint zoffset,
3750 GLsizei width, GLsizei height, GLsizei depth,
3751 GLenum format, GLsizei imageSize, const GLvoid *data)
3753 struct gl_texture_object *texObj;
3754 struct gl_texture_image *texImage;
3756 GET_CURRENT_CONTEXT(ctx);
3757 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3759 error = compressed_subtexture_error_check(ctx, dims, target, level,
3760 xoffset, 0, 0, /* pos */
3761 width, height, depth, /* size */
3764 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3768 texObj = _mesa_get_current_tex_object(ctx, target);
3770 _mesa_lock_texture(ctx, texObj);
3772 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3775 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3776 format, texImage)) {
3777 /* error was recorded */
3779 else if (width > 0 && height > 0 && depth > 0) {
3780 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
3781 xoffset, yoffset, zoffset,
3782 width, height, depth,
3783 format, imageSize, data);
3785 check_gen_mipmap(ctx, target, texObj, level);
3787 ctx->NewState |= _NEW_TEXTURE;
3790 _mesa_unlock_texture(ctx, texObj);
3795 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3796 GLsizei width, GLenum format,
3797 GLsizei imageSize, const GLvoid *data)
3799 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3800 format, imageSize, data);
3805 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3806 GLint yoffset, GLsizei width, GLsizei height,
3807 GLenum format, GLsizei imageSize,
3810 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3811 width, height, 1, format, imageSize, data);
3816 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3817 GLint yoffset, GLint zoffset, GLsizei width,
3818 GLsizei height, GLsizei depth, GLenum format,
3819 GLsizei imageSize, const GLvoid *data)
3821 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3822 width, height, depth, format, imageSize, data);
3826 get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3828 switch (internalFormat) {
3830 return MESA_FORMAT_A8;
3832 return MESA_FORMAT_A16;
3833 case GL_ALPHA16F_ARB:
3834 return MESA_FORMAT_ALPHA_FLOAT16;
3835 case GL_ALPHA32F_ARB:
3836 return MESA_FORMAT_ALPHA_FLOAT32;
3837 case GL_ALPHA8I_EXT:
3838 return MESA_FORMAT_ALPHA_INT8;
3839 case GL_ALPHA16I_EXT:
3840 return MESA_FORMAT_ALPHA_INT16;
3841 case GL_ALPHA32I_EXT:
3842 return MESA_FORMAT_ALPHA_INT32;
3843 case GL_ALPHA8UI_EXT:
3844 return MESA_FORMAT_ALPHA_UINT8;
3845 case GL_ALPHA16UI_EXT:
3846 return MESA_FORMAT_ALPHA_UINT16;
3847 case GL_ALPHA32UI_EXT:
3848 return MESA_FORMAT_ALPHA_UINT32;
3850 return MESA_FORMAT_L8;
3851 case GL_LUMINANCE16:
3852 return MESA_FORMAT_L16;
3853 case GL_LUMINANCE16F_ARB:
3854 return MESA_FORMAT_LUMINANCE_FLOAT16;
3855 case GL_LUMINANCE32F_ARB:
3856 return MESA_FORMAT_LUMINANCE_FLOAT32;
3857 case GL_LUMINANCE8I_EXT:
3858 return MESA_FORMAT_LUMINANCE_INT8;
3859 case GL_LUMINANCE16I_EXT:
3860 return MESA_FORMAT_LUMINANCE_INT16;
3861 case GL_LUMINANCE32I_EXT:
3862 return MESA_FORMAT_LUMINANCE_INT32;
3863 case GL_LUMINANCE8UI_EXT:
3864 return MESA_FORMAT_LUMINANCE_UINT8;
3865 case GL_LUMINANCE16UI_EXT:
3866 return MESA_FORMAT_LUMINANCE_UINT16;
3867 case GL_LUMINANCE32UI_EXT:
3868 return MESA_FORMAT_LUMINANCE_UINT32;
3869 case GL_LUMINANCE8_ALPHA8:
3870 return MESA_FORMAT_AL88;
3871 case GL_LUMINANCE16_ALPHA16:
3872 return MESA_FORMAT_AL1616;
3873 case GL_LUMINANCE_ALPHA16F_ARB:
3874 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
3875 case GL_LUMINANCE_ALPHA32F_ARB:
3876 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
3877 case GL_LUMINANCE_ALPHA8I_EXT:
3878 return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3879 case GL_LUMINANCE_ALPHA16I_EXT:
3880 return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
3881 case GL_LUMINANCE_ALPHA32I_EXT:
3882 return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
3883 case GL_LUMINANCE_ALPHA8UI_EXT:
3884 return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
3885 case GL_LUMINANCE_ALPHA16UI_EXT:
3886 return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
3887 case GL_LUMINANCE_ALPHA32UI_EXT:
3888 return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
3890 return MESA_FORMAT_I8;
3891 case GL_INTENSITY16:
3892 return MESA_FORMAT_I16;
3893 case GL_INTENSITY16F_ARB:
3894 return MESA_FORMAT_INTENSITY_FLOAT16;
3895 case GL_INTENSITY32F_ARB:
3896 return MESA_FORMAT_INTENSITY_FLOAT32;
3897 case GL_INTENSITY8I_EXT:
3898 return MESA_FORMAT_INTENSITY_INT8;
3899 case GL_INTENSITY16I_EXT:
3900 return MESA_FORMAT_INTENSITY_INT16;
3901 case GL_INTENSITY32I_EXT:
3902 return MESA_FORMAT_INTENSITY_INT32;
3903 case GL_INTENSITY8UI_EXT:
3904 return MESA_FORMAT_INTENSITY_UINT8;
3905 case GL_INTENSITY16UI_EXT:
3906 return MESA_FORMAT_INTENSITY_UINT16;
3907 case GL_INTENSITY32UI_EXT:
3908 return MESA_FORMAT_INTENSITY_UINT32;
3910 return MESA_FORMAT_RGBA8888_REV;
3912 return MESA_FORMAT_RGBA_16;
3913 case GL_RGBA16F_ARB:
3914 return MESA_FORMAT_RGBA_FLOAT16;
3915 case GL_RGBA32F_ARB:
3916 return MESA_FORMAT_RGBA_FLOAT32;
3918 return MESA_FORMAT_RGBA_INT8;
3919 case GL_RGBA16I_EXT:
3920 return MESA_FORMAT_RGBA_INT16;
3921 case GL_RGBA32I_EXT:
3922 return MESA_FORMAT_RGBA_INT32;
3923 case GL_RGBA8UI_EXT:
3924 return MESA_FORMAT_RGBA_UINT8;
3925 case GL_RGBA16UI_EXT:
3926 return MESA_FORMAT_RGBA_UINT16;
3927 case GL_RGBA32UI_EXT:
3928 return MESA_FORMAT_RGBA_UINT32;
3931 return MESA_FORMAT_GR88;
3933 return MESA_FORMAT_RG1616;
3935 return MESA_FORMAT_RG_FLOAT16;
3937 return MESA_FORMAT_RG_FLOAT32;
3939 return MESA_FORMAT_RG_INT8;
3941 return MESA_FORMAT_RG_INT16;
3943 return MESA_FORMAT_RG_INT32;
3945 return MESA_FORMAT_RG_UINT8;
3947 return MESA_FORMAT_RG_UINT16;
3949 return MESA_FORMAT_RG_UINT32;
3952 return MESA_FORMAT_R8;
3954 return MESA_FORMAT_R16;
3956 return MESA_FORMAT_R_FLOAT16;
3958 return MESA_FORMAT_R_FLOAT32;
3960 return MESA_FORMAT_R_INT8;
3962 return MESA_FORMAT_R_INT16;
3964 return MESA_FORMAT_R_INT32;
3966 return MESA_FORMAT_R_UINT8;
3968 return MESA_FORMAT_R_UINT16;
3970 return MESA_FORMAT_R_UINT32;
3973 return MESA_FORMAT_NONE;
3978 validate_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
3980 gl_format format = get_texbuffer_format(ctx, internalFormat);
3983 if (format == MESA_FORMAT_NONE)
3984 return MESA_FORMAT_NONE;
3986 datatype = _mesa_get_format_datatype(format);
3987 if (datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3988 return MESA_FORMAT_NONE;
3990 if (datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3991 return MESA_FORMAT_NONE;
3993 /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
3994 * any mention of R/RG formats, but they appear in the GL 3.1 core
3997 if (ctx->Version <= 30) {
3998 GLenum base_format = _mesa_get_format_base_format(format);
4000 if (base_format == GL_R || base_format == GL_RG)
4001 return MESA_FORMAT_NONE;
4007 /** GL_ARB_texture_buffer_object */
4009 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
4011 struct gl_texture_object *texObj;
4012 struct gl_buffer_object *bufObj;
4015 GET_CURRENT_CONTEXT(ctx);
4016 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4018 if (!(ctx->Extensions.ARB_texture_buffer_object
4019 && _mesa_is_desktop_gl(ctx))) {
4020 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
4024 if (target != GL_TEXTURE_BUFFER_ARB) {
4025 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
4029 format = validate_texbuffer_format(ctx, internalFormat);
4030 if (format == MESA_FORMAT_NONE) {
4031 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
4036 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4037 if (buffer && !bufObj) {
4038 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4042 texObj = _mesa_get_current_tex_object(ctx, target);
4044 _mesa_lock_texture(ctx, texObj);
4046 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4047 texObj->BufferObjectFormat = internalFormat;
4048 texObj->_BufferObjectFormat = format;
4050 _mesa_unlock_texture(ctx, texObj);