2 * mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Texture image-related functions.
34 #include "bufferobj.h"
38 #include "framebuffer.h"
43 #include "mfeatures.h"
45 #include "texcompress.h"
54 * State changes which we care about for glCopyTex[Sub]Image() calls.
55 * In particular, we care about pixel transfer state and buffer state
56 * (such as glReadBuffer to make sure we read from the right renderbuffer).
58 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
63 * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
67 _mesa_alloc_texmemory(GLsizei bytes)
69 return _mesa_align_malloc(bytes, 512);
74 * Free texture memory allocated with _mesa_alloc_texmemory()
77 _mesa_free_texmemory(void *m)
84 * Return the simple base format for a given internal texture format.
85 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
87 * \param ctx GL context.
88 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
90 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
91 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
93 * This is the format which is used during texture application (i.e. the
94 * texture format and env mode determine the arithmetic used.
96 * XXX this could be static
99 _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
101 switch (internalFormat) {
116 case GL_LUMINANCE_ALPHA:
117 case GL_LUMINANCE4_ALPHA4:
118 case GL_LUMINANCE6_ALPHA2:
119 case GL_LUMINANCE8_ALPHA8:
120 case GL_LUMINANCE12_ALPHA4:
121 case GL_LUMINANCE12_ALPHA12:
122 case GL_LUMINANCE16_ALPHA16:
123 return GL_LUMINANCE_ALPHA;
154 if (ctx->Extensions.EXT_texture_format_BGRA8888) {
155 switch (internalFormat) {
163 if (ctx->Extensions.EXT_paletted_texture) {
164 switch (internalFormat) {
166 case GL_COLOR_INDEX1_EXT:
167 case GL_COLOR_INDEX2_EXT:
168 case GL_COLOR_INDEX4_EXT:
169 case GL_COLOR_INDEX8_EXT:
170 case GL_COLOR_INDEX12_EXT:
171 case GL_COLOR_INDEX16_EXT:
172 return GL_COLOR_INDEX;
178 if (ctx->Extensions.ARB_depth_texture) {
179 switch (internalFormat) {
180 case GL_DEPTH_COMPONENT:
181 case GL_DEPTH_COMPONENT16:
182 case GL_DEPTH_COMPONENT24:
183 case GL_DEPTH_COMPONENT32:
184 return GL_DEPTH_COMPONENT;
190 switch (internalFormat) {
191 case GL_COMPRESSED_ALPHA:
193 case GL_COMPRESSED_LUMINANCE:
195 case GL_COMPRESSED_LUMINANCE_ALPHA:
196 return GL_LUMINANCE_ALPHA;
197 case GL_COMPRESSED_INTENSITY:
199 case GL_COMPRESSED_RGB:
201 case GL_COMPRESSED_RGBA:
207 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
208 switch (internalFormat) {
209 case GL_COMPRESSED_RGB_FXT1_3DFX:
211 case GL_COMPRESSED_RGBA_FXT1_3DFX:
218 if (ctx->Extensions.EXT_texture_compression_s3tc) {
219 switch (internalFormat) {
220 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
222 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
223 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
224 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
231 if (ctx->Extensions.S3_s3tc) {
232 switch (internalFormat) {
244 if (ctx->Extensions.MESA_ycbcr_texture) {
245 if (internalFormat == GL_YCBCR_MESA)
246 return GL_YCBCR_MESA;
249 if (ctx->Extensions.ARB_texture_float) {
250 switch (internalFormat) {
251 case GL_ALPHA16F_ARB:
252 case GL_ALPHA32F_ARB:
260 case GL_INTENSITY16F_ARB:
261 case GL_INTENSITY32F_ARB:
263 case GL_LUMINANCE16F_ARB:
264 case GL_LUMINANCE32F_ARB:
266 case GL_LUMINANCE_ALPHA16F_ARB:
267 case GL_LUMINANCE_ALPHA32F_ARB:
268 return GL_LUMINANCE_ALPHA;
274 if (ctx->Extensions.ATI_envmap_bumpmap) {
275 switch (internalFormat) {
284 if (ctx->Extensions.EXT_texture_snorm) {
285 switch (internalFormat) {
300 case GL_RGBA16_SNORM:
303 case GL_ALPHA8_SNORM:
304 case GL_ALPHA16_SNORM:
306 case GL_LUMINANCE_SNORM:
307 case GL_LUMINANCE8_SNORM:
308 case GL_LUMINANCE16_SNORM:
310 case GL_LUMINANCE_ALPHA_SNORM:
311 case GL_LUMINANCE8_ALPHA8_SNORM:
312 case GL_LUMINANCE16_ALPHA16_SNORM:
313 return GL_LUMINANCE_ALPHA;
314 case GL_INTENSITY_SNORM:
315 case GL_INTENSITY8_SNORM:
316 case GL_INTENSITY16_SNORM:
323 if (ctx->Extensions.EXT_packed_depth_stencil) {
324 switch (internalFormat) {
325 case GL_DEPTH_STENCIL_EXT:
326 case GL_DEPTH24_STENCIL8_EXT:
327 return GL_DEPTH_STENCIL_EXT;
333 #if FEATURE_EXT_texture_sRGB
334 if (ctx->Extensions.EXT_texture_sRGB) {
335 switch (internalFormat) {
338 case GL_COMPRESSED_SRGB_EXT:
339 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
341 case GL_SRGB_ALPHA_EXT:
342 case GL_SRGB8_ALPHA8_EXT:
343 case GL_COMPRESSED_SRGB_ALPHA_EXT:
344 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
345 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
346 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
348 case GL_SLUMINANCE_ALPHA_EXT:
349 case GL_SLUMINANCE8_ALPHA8_EXT:
350 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
351 return GL_LUMINANCE_ALPHA;
352 case GL_SLUMINANCE_EXT:
353 case GL_SLUMINANCE8_EXT:
354 case GL_COMPRESSED_SLUMINANCE_EXT:
360 #endif /* FEATURE_EXT_texture_sRGB */
362 if (ctx->Extensions.EXT_texture_integer) {
363 switch (internalFormat) {
365 case GL_RGBA16UI_EXT:
366 case GL_RGBA32UI_EXT:
378 case GL_ALPHA8UI_EXT:
379 case GL_ALPHA16UI_EXT:
380 case GL_ALPHA32UI_EXT:
382 case GL_ALPHA16I_EXT:
383 case GL_ALPHA32I_EXT:
385 case GL_INTENSITY8UI_EXT:
386 case GL_INTENSITY16UI_EXT:
387 case GL_INTENSITY32UI_EXT:
388 case GL_INTENSITY8I_EXT:
389 case GL_INTENSITY16I_EXT:
390 case GL_INTENSITY32I_EXT:
392 case GL_LUMINANCE8UI_EXT:
393 case GL_LUMINANCE16UI_EXT:
394 case GL_LUMINANCE32UI_EXT:
395 case GL_LUMINANCE8I_EXT:
396 case GL_LUMINANCE16I_EXT:
397 case GL_LUMINANCE32I_EXT:
399 case GL_LUMINANCE_ALPHA8UI_EXT:
400 case GL_LUMINANCE_ALPHA16UI_EXT:
401 case GL_LUMINANCE_ALPHA32UI_EXT:
402 case GL_LUMINANCE_ALPHA8I_EXT:
403 case GL_LUMINANCE_ALPHA16I_EXT:
404 case GL_LUMINANCE_ALPHA32I_EXT:
405 return GL_LUMINANCE_ALPHA;
411 if (ctx->Extensions.ARB_texture_rg) {
412 switch (internalFormat) {
414 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
416 if (!ctx->Extensions.ARB_half_float_pixel)
420 if (!ctx->Extensions.ARB_texture_float)
429 if (!ctx->Extensions.EXT_texture_integer)
435 case GL_COMPRESSED_RED:
439 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
441 if (!ctx->Extensions.ARB_half_float_pixel)
445 if (!ctx->Extensions.ARB_texture_float)
454 if (!ctx->Extensions.EXT_texture_integer)
460 case GL_COMPRESSED_RG:
467 if (ctx->Extensions.EXT_texture_shared_exponent) {
468 switch (internalFormat) {
476 if (ctx->Extensions.EXT_packed_float) {
477 switch (internalFormat) {
478 case GL_R11F_G11F_B10F_EXT:
485 if (ctx->Extensions.ARB_depth_buffer_float) {
486 switch (internalFormat) {
487 case GL_DEPTH_COMPONENT32F:
488 return GL_DEPTH_COMPONENT;
489 case GL_DEPTH32F_STENCIL8:
490 return GL_DEPTH_STENCIL;
496 if (ctx->Extensions.ARB_texture_compression_rgtc) {
497 switch (internalFormat) {
498 case GL_COMPRESSED_RED_RGTC1:
499 case GL_COMPRESSED_SIGNED_RED_RGTC1:
501 case GL_COMPRESSED_RG_RGTC2:
502 case GL_COMPRESSED_SIGNED_RG_RGTC2:
509 if (ctx->Extensions.EXT_texture_compression_latc) {
510 switch (internalFormat) {
511 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
512 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
514 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
515 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
516 return GL_LUMINANCE_ALPHA;
522 if (ctx->Extensions.ATI_texture_compression_3dc) {
523 switch (internalFormat) {
524 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
525 return GL_LUMINANCE_ALPHA;
531 if (ctx->API == API_OPENGLES) {
532 switch (internalFormat) {
533 case GL_PALETTE4_RGB8_OES:
534 case GL_PALETTE4_R5_G6_B5_OES:
535 case GL_PALETTE8_RGB8_OES:
536 case GL_PALETTE8_R5_G6_B5_OES:
538 case GL_PALETTE4_RGBA8_OES:
539 case GL_PALETTE8_RGB5_A1_OES:
540 case GL_PALETTE4_RGBA4_OES:
541 case GL_PALETTE4_RGB5_A1_OES:
542 case GL_PALETTE8_RGBA8_OES:
543 case GL_PALETTE8_RGBA4_OES:
550 return -1; /* error */
555 * For cube map faces, return a face index in [0,5].
556 * For other targets return 0;
559 _mesa_tex_target_to_face(GLenum target)
561 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
562 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
563 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
571 * Store a gl_texture_image pointer in a gl_texture_object structure
572 * according to the target and level parameters.
574 * \param tObj texture object.
575 * \param target texture target.
576 * \param level image level.
577 * \param texImage texture image.
579 * This was basically prompted by the introduction of cube maps.
582 _mesa_set_tex_image(struct gl_texture_object *tObj,
583 GLenum target, GLint level,
584 struct gl_texture_image *texImage)
586 const GLuint face = _mesa_tex_target_to_face(target);
590 ASSERT(target != GL_TEXTURE_RECTANGLE_NV || level == 0);
592 tObj->Image[face][level] = texImage;
594 /* Set the 'back' pointer */
595 texImage->TexObject = tObj;
600 * Allocate a texture image structure.
602 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
605 * \return a pointer to gl_texture_image struct with all fields initialized to
608 struct gl_texture_image *
609 _mesa_new_texture_image( struct gl_context *ctx )
612 return CALLOC_STRUCT(gl_texture_image);
617 * Free texture image data.
618 * This function is a fallback called via ctx->Driver.FreeTexImageData().
620 * \param texImage texture image.
622 * Free the texture image data if it's not marked as client data.
625 _mesa_free_texture_image_data(struct gl_context *ctx,
626 struct gl_texture_image *texImage)
630 if (texImage->Data && !texImage->IsClientData) {
631 /* free the old texture data */
632 _mesa_free_texmemory(texImage->Data);
635 texImage->Data = NULL;
640 * Free texture image.
642 * \param texImage texture image.
644 * Free the texture image structure and the associated image data.
647 _mesa_delete_texture_image(struct gl_context *ctx,
648 struct gl_texture_image *texImage)
650 /* Free texImage->Data and/or any other driver-specific texture
653 ASSERT(ctx->Driver.FreeTexImageData);
654 ctx->Driver.FreeTexImageData( ctx, texImage );
656 ASSERT(texImage->Data == NULL);
657 if (texImage->ImageOffsets)
658 free(texImage->ImageOffsets);
664 * Test if a target is a proxy target.
666 * \param target texture target.
668 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
671 _mesa_is_proxy_texture(GLenum target)
673 /* NUM_TEXTURE_TARGETS should match number of terms below,
674 * except there's no proxy for GL_TEXTURE_BUFFER.
676 assert(NUM_TEXTURE_TARGETS == 8);
678 return (target == GL_PROXY_TEXTURE_1D ||
679 target == GL_PROXY_TEXTURE_2D ||
680 target == GL_PROXY_TEXTURE_3D ||
681 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
682 target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
683 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
684 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT);
689 * Return the proxy target which corresponds to the given texture target
692 get_proxy_target(GLenum target)
696 case GL_PROXY_TEXTURE_1D:
697 return GL_PROXY_TEXTURE_1D;
699 case GL_PROXY_TEXTURE_2D:
700 return GL_PROXY_TEXTURE_2D;
702 case GL_PROXY_TEXTURE_3D:
703 return GL_PROXY_TEXTURE_3D;
704 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
705 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
706 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
707 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
708 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
709 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
710 case GL_TEXTURE_CUBE_MAP_ARB:
711 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
712 return GL_PROXY_TEXTURE_CUBE_MAP_ARB;
713 case GL_TEXTURE_RECTANGLE_NV:
714 case GL_PROXY_TEXTURE_RECTANGLE_NV:
715 return GL_PROXY_TEXTURE_RECTANGLE_NV;
716 case GL_TEXTURE_1D_ARRAY_EXT:
717 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
718 return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
719 case GL_TEXTURE_2D_ARRAY_EXT:
720 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
721 return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
723 _mesa_problem(NULL, "unexpected target in get_proxy_target()");
730 * Get the texture object that corresponds to the target of the given
733 * \param ctx GL context.
734 * \param texUnit texture unit.
735 * \param target texture target.
737 * \return pointer to the texture object on success, or NULL on failure.
739 * \sa gl_texture_unit.
741 struct gl_texture_object *
742 _mesa_select_tex_object(struct gl_context *ctx,
743 const struct gl_texture_unit *texUnit,
746 const GLboolean arrayTex = (ctx->Extensions.MESA_texture_array ||
747 ctx->Extensions.EXT_texture_array);
751 return texUnit->CurrentTex[TEXTURE_1D_INDEX];
752 case GL_PROXY_TEXTURE_1D:
753 return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
755 return texUnit->CurrentTex[TEXTURE_2D_INDEX];
756 case GL_PROXY_TEXTURE_2D:
757 return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
759 return texUnit->CurrentTex[TEXTURE_3D_INDEX];
760 case GL_PROXY_TEXTURE_3D:
761 return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
762 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
763 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
764 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
765 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
766 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
767 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
768 case GL_TEXTURE_CUBE_MAP_ARB:
769 return ctx->Extensions.ARB_texture_cube_map
770 ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
771 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
772 return ctx->Extensions.ARB_texture_cube_map
773 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
774 case GL_TEXTURE_RECTANGLE_NV:
775 return ctx->Extensions.NV_texture_rectangle
776 ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
777 case GL_PROXY_TEXTURE_RECTANGLE_NV:
778 return ctx->Extensions.NV_texture_rectangle
779 ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
780 case GL_TEXTURE_1D_ARRAY_EXT:
781 return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
782 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
783 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
784 case GL_TEXTURE_2D_ARRAY_EXT:
785 return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
786 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
787 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
788 case GL_TEXTURE_BUFFER:
789 return ctx->Extensions.ARB_texture_buffer_object
790 ? texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL;
792 _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
799 * Return pointer to texture object for given target on current texture unit.
801 struct gl_texture_object *
802 _mesa_get_current_tex_object(struct gl_context *ctx, GLenum target)
804 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
805 return _mesa_select_tex_object(ctx, texUnit, target);
810 * Get a texture image pointer from a texture object, given a texture
811 * target and mipmap level. The target and level parameters should
812 * have already been error-checked.
814 * \param ctx GL context.
815 * \param texObj texture unit.
816 * \param target texture target.
817 * \param level image level.
819 * \return pointer to the texture image structure, or NULL on failure.
821 struct gl_texture_image *
822 _mesa_select_tex_image(struct gl_context *ctx,
823 const struct gl_texture_object *texObj,
824 GLenum target, GLint level)
826 const GLuint face = _mesa_tex_target_to_face(target);
830 ASSERT(level < MAX_TEXTURE_LEVELS);
832 return texObj->Image[face][level];
837 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
838 * it and install it. Only return NULL if passed a bad parameter or run
841 struct gl_texture_image *
842 _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
843 GLenum target, GLint level)
845 struct gl_texture_image *texImage;
850 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
852 texImage = ctx->Driver.NewTextureImage(ctx);
854 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
858 _mesa_set_tex_image(texObj, target, level, texImage);
866 * Return pointer to the specified proxy texture image.
867 * Note that proxy textures are per-context, not per-texture unit.
868 * \return pointer to texture image or NULL if invalid target, invalid
869 * level, or out of memory.
871 struct gl_texture_image *
872 _mesa_get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
874 struct gl_texture_image *texImage;
881 case GL_PROXY_TEXTURE_1D:
882 if (level >= ctx->Const.MaxTextureLevels)
884 texIndex = TEXTURE_1D_INDEX;
886 case GL_PROXY_TEXTURE_2D:
887 if (level >= ctx->Const.MaxTextureLevels)
889 texIndex = TEXTURE_2D_INDEX;
891 case GL_PROXY_TEXTURE_3D:
892 if (level >= ctx->Const.Max3DTextureLevels)
894 texIndex = TEXTURE_3D_INDEX;
896 case GL_PROXY_TEXTURE_CUBE_MAP:
897 if (level >= ctx->Const.MaxCubeTextureLevels)
899 texIndex = TEXTURE_CUBE_INDEX;
901 case GL_PROXY_TEXTURE_RECTANGLE_NV:
904 texIndex = TEXTURE_RECT_INDEX;
906 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
907 if (level >= ctx->Const.MaxTextureLevels)
909 texIndex = TEXTURE_1D_ARRAY_INDEX;
911 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
912 if (level >= ctx->Const.MaxTextureLevels)
914 texIndex = TEXTURE_2D_ARRAY_INDEX;
920 texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
922 texImage = ctx->Driver.NewTextureImage(ctx);
924 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
927 ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
928 /* Set the 'back' pointer */
929 texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
936 * Get the maximum number of allowed mipmap levels.
938 * \param ctx GL context.
939 * \param target texture target.
941 * \return the maximum number of allowed mipmap levels for the given
942 * texture target, or zero if passed a bad target.
947 _mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
951 case GL_PROXY_TEXTURE_1D:
953 case GL_PROXY_TEXTURE_2D:
954 return ctx->Const.MaxTextureLevels;
956 case GL_PROXY_TEXTURE_3D:
957 return ctx->Const.Max3DTextureLevels;
958 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
959 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
960 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
961 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
962 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
963 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
964 case GL_TEXTURE_CUBE_MAP_ARB:
965 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
966 return ctx->Extensions.ARB_texture_cube_map
967 ? ctx->Const.MaxCubeTextureLevels : 0;
968 case GL_TEXTURE_RECTANGLE_NV:
969 case GL_PROXY_TEXTURE_RECTANGLE_NV:
970 return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
971 case GL_TEXTURE_1D_ARRAY_EXT:
972 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
973 case GL_TEXTURE_2D_ARRAY_EXT:
974 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
975 return (ctx->Extensions.MESA_texture_array ||
976 ctx->Extensions.EXT_texture_array)
977 ? ctx->Const.MaxTextureLevels : 0;
978 case GL_TEXTURE_BUFFER:
981 return 0; /* bad target */
987 * Return number of dimensions per mipmap level for the given texture target.
990 _mesa_get_texture_dimensions(GLenum target)
994 case GL_PROXY_TEXTURE_1D:
997 case GL_TEXTURE_RECTANGLE:
998 case GL_TEXTURE_CUBE_MAP:
999 case GL_PROXY_TEXTURE_2D:
1000 case GL_PROXY_TEXTURE_RECTANGLE:
1001 case GL_PROXY_TEXTURE_CUBE_MAP:
1002 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1003 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1004 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1005 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1006 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1007 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1008 case GL_TEXTURE_1D_ARRAY:
1009 case GL_PROXY_TEXTURE_1D_ARRAY:
1012 case GL_PROXY_TEXTURE_3D:
1013 case GL_TEXTURE_2D_ARRAY:
1014 case GL_PROXY_TEXTURE_2D_ARRAY:
1016 case GL_TEXTURE_BUFFER:
1019 _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
1028 #if 000 /* not used anymore */
1030 * glTexImage[123]D can accept a NULL image pointer. In this case we
1031 * create a texture image with unspecified image contents per the OpenGL
1035 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
1037 const GLint components = _mesa_components_in_format(format);
1038 const GLint numPixels = width * height * depth;
1039 GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
1043 * Let's see if anyone finds this. If glTexImage2D() is called with
1044 * a NULL image pointer then load the texture image with something
1045 * interesting instead of leaving it indeterminate.
1048 static const char message[8][32] = {
1049 " X X XXXXX XXX X ",
1050 " XX XX X X X X X ",
1052 " X X XXXX XXX XXXXX ",
1055 " X X XXXXX XXX X X ",
1059 GLubyte *imgPtr = data;
1061 for (h = 0; h < depth; h++) {
1062 for (i = 0; i < height; i++) {
1063 GLint srcRow = 7 - (i % 8);
1064 for (j = 0; j < width; j++) {
1065 GLint srcCol = j % 32;
1066 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
1067 for (k = 0; k < components; k++) {
1083 * Reset the fields of a gl_texture_image struct to zero.
1085 * \param img texture image structure.
1087 * This is called when a proxy texture test fails, we set all the
1088 * image members (except DriverData) to zero.
1089 * It's also used in glTexImage[123]D as a safeguard to be sure all
1090 * required fields get initialized properly by the Driver.TexImage[123]D
1094 clear_teximage_fields(struct gl_texture_image *img)
1097 img->_BaseFormat = 0;
1098 img->InternalFormat = 0;
1104 if (img->ImageOffsets) {
1105 free(img->ImageOffsets);
1106 img->ImageOffsets = NULL;
1112 img->HeightLog2 = 0;
1115 img->TexFormat = MESA_FORMAT_NONE;
1116 img->FetchTexelc = NULL;
1117 img->FetchTexelf = NULL;
1122 * Initialize basic fields of the gl_texture_image struct.
1124 * \param ctx GL context.
1125 * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
1126 * \param img texture image structure to be initialized.
1127 * \param width image width.
1128 * \param height image height.
1129 * \param depth image depth.
1130 * \param border image border.
1131 * \param internalFormat internal format.
1132 * \param format the actual hardware format (one of MESA_FORMAT_*)
1134 * Fills in the fields of \p img with the given information.
1135 * Note: width, height and depth include the border.
1138 _mesa_init_teximage_fields(struct gl_context *ctx, GLenum target,
1139 struct gl_texture_image *img,
1140 GLsizei width, GLsizei height, GLsizei depth,
1141 GLint border, GLenum internalFormat,
1148 ASSERT(height >= 0);
1151 img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
1152 ASSERT(img->_BaseFormat > 0);
1153 img->InternalFormat = internalFormat;
1154 img->Border = border;
1156 img->Height = height;
1159 img->Width2 = width - 2 * border; /* == 1 << img->WidthLog2; */
1160 img->WidthLog2 = _mesa_logbase2(img->Width2);
1162 if (height == 1) { /* 1-D texture */
1164 img->HeightLog2 = 0;
1167 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
1168 img->HeightLog2 = _mesa_logbase2(img->Height2);
1171 if (depth == 1) { /* 2-D texture */
1176 img->Depth2 = depth - 2 * border; /* == 1 << img->DepthLog2; */
1177 img->DepthLog2 = _mesa_logbase2(img->Depth2);
1180 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
1182 if ((width == 1 || _mesa_is_pow_two(img->Width2)) &&
1183 (height == 1 || _mesa_is_pow_two(img->Height2)) &&
1184 (depth == 1 || _mesa_is_pow_two(img->Depth2)))
1185 img->_IsPowerOfTwo = GL_TRUE;
1187 img->_IsPowerOfTwo = GL_FALSE;
1189 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
1190 img->RowStride = width;
1191 /* Allocate the ImageOffsets array and initialize to typical values.
1192 * We allocate the array for 1D/2D textures too in order to avoid special-
1193 * case code in the texstore routines.
1195 if (img->ImageOffsets)
1196 free(img->ImageOffsets);
1197 img->ImageOffsets = (GLuint *) malloc(depth * sizeof(GLuint));
1198 for (i = 0; i < depth; i++) {
1199 img->ImageOffsets[i] = i * width * height;
1202 /* Compute Width/Height/DepthScale for mipmap lod computation */
1203 if (target == GL_TEXTURE_RECTANGLE_NV) {
1204 /* scale = 1.0 since texture coords directly map to texels */
1205 img->WidthScale = 1.0;
1206 img->HeightScale = 1.0;
1207 img->DepthScale = 1.0;
1210 img->WidthScale = (GLfloat) img->Width;
1211 img->HeightScale = (GLfloat) img->Height;
1212 img->DepthScale = (GLfloat) img->Depth;
1215 img->TexFormat = format;
1217 dims = _mesa_get_texture_dimensions(target);
1219 _mesa_set_fetch_functions(img, dims);
1224 * Free and clear fields of the gl_texture_image struct.
1226 * \param ctx GL context.
1227 * \param texImage texture image structure to be cleared.
1229 * After the call, \p texImage will have no data associated with it. Its
1230 * fields are cleared so that its parent object will test incomplete.
1233 _mesa_clear_texture_image(struct gl_context *ctx,
1234 struct gl_texture_image *texImage)
1236 ctx->Driver.FreeTexImageData(ctx, texImage);
1237 clear_teximage_fields(texImage);
1242 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1243 * level, width, height and depth against the ctx->Const limits for textures.
1245 * A hardware driver might override this function if, for example, the
1246 * max 3D texture size is 512x512x64 (i.e. not a cube).
1248 * Note that width, height, depth == 0 is not an error. However, a
1249 * texture with zero width/height/depth will be considered "incomplete"
1250 * and texturing will effectively be disabled.
1252 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1253 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1254 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1255 * \param level as passed to glTexImage
1256 * \param internalFormat as passed to glTexImage
1257 * \param format as passed to glTexImage
1258 * \param type as passed to glTexImage
1259 * \param width as passed to glTexImage
1260 * \param height as passed to glTexImage
1261 * \param depth as passed to glTexImage
1262 * \param border as passed to glTexImage
1263 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1266 _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
1267 GLint internalFormat, GLenum format, GLenum type,
1268 GLint width, GLint height, GLint depth, GLint border)
1272 (void) internalFormat;
1277 case GL_PROXY_TEXTURE_1D:
1278 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1279 if (width < 2 * border || width > 2 + maxSize)
1281 if (level >= ctx->Const.MaxTextureLevels)
1283 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1284 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1289 case GL_PROXY_TEXTURE_2D:
1290 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1291 if (width < 2 * border || width > 2 + maxSize)
1293 if (height < 2 * border || height > 2 + maxSize)
1295 if (level >= ctx->Const.MaxTextureLevels)
1297 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1298 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1300 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1305 case GL_PROXY_TEXTURE_3D:
1306 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1307 if (width < 2 * border || width > 2 + maxSize)
1309 if (height < 2 * border || height > 2 + maxSize)
1311 if (depth < 2 * border || depth > 2 + maxSize)
1313 if (level >= ctx->Const.Max3DTextureLevels)
1315 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1316 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1318 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1320 if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
1325 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1326 maxSize = ctx->Const.MaxTextureRectSize;
1327 if (width < 0 || width > maxSize)
1329 if (height < 0 || height > maxSize)
1335 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1336 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1337 if (width < 2 * border || width > 2 + maxSize)
1339 if (height < 2 * border || height > 2 + maxSize)
1341 if (level >= ctx->Const.MaxCubeTextureLevels)
1343 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1344 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1346 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1351 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1352 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1353 if (width < 2 * border || width > 2 + maxSize)
1355 if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
1357 if (level >= ctx->Const.MaxTextureLevels)
1359 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1360 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1365 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1366 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1367 if (width < 2 * border || width > 2 + maxSize)
1369 if (height < 2 * border || height > 2 + maxSize)
1371 if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
1373 if (level >= ctx->Const.MaxTextureLevels)
1375 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1376 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1378 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1384 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1391 * Check if the memory used by the texture would exceed the driver's limit.
1392 * This lets us support a max 3D texture size of 8K (for example) but
1393 * prevents allocating a full 8K x 8K x 8K texture.
1394 * XXX this could be rolled into the proxy texture size test (above) but
1395 * we don't have the actual texture internal format at that point.
1398 legal_texture_size(struct gl_context *ctx, gl_format format,
1399 GLint width, GLint height, GLint depth)
1401 uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
1402 uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
1403 return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1409 * Helper function to determine whether a target and specific compression
1410 * format are supported.
1413 target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1416 (void) intFormat; /* not used yet */
1420 case GL_PROXY_TEXTURE_2D:
1421 return GL_TRUE; /* true for any compressed format so far */
1422 case GL_PROXY_TEXTURE_CUBE_MAP:
1423 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1424 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1425 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1426 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1427 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1428 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1429 return ctx->Extensions.ARB_texture_cube_map;
1430 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1431 case GL_TEXTURE_2D_ARRAY_EXT:
1432 return (ctx->Extensions.MESA_texture_array ||
1433 ctx->Extensions.EXT_texture_array);
1441 * Check if the given texture target value is legal for a
1442 * glTexImage1/2/3D call.
1445 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1451 case GL_PROXY_TEXTURE_1D:
1459 case GL_PROXY_TEXTURE_2D:
1461 case GL_PROXY_TEXTURE_CUBE_MAP:
1462 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1463 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1464 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1465 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1466 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1467 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1468 return ctx->Extensions.ARB_texture_cube_map;
1469 case GL_TEXTURE_RECTANGLE_NV:
1470 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1471 return ctx->Extensions.NV_texture_rectangle;
1472 case GL_TEXTURE_1D_ARRAY_EXT:
1473 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1474 return (ctx->Extensions.MESA_texture_array ||
1475 ctx->Extensions.EXT_texture_array);
1482 case GL_PROXY_TEXTURE_3D:
1484 case GL_TEXTURE_2D_ARRAY_EXT:
1485 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1486 return (ctx->Extensions.MESA_texture_array ||
1487 ctx->Extensions.EXT_texture_array);
1492 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1499 * Check if the given texture target value is legal for a
1500 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1501 * The difference compared to legal_teximage_target() above is that
1502 * proxy targets are not supported.
1505 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1509 return target == GL_TEXTURE_1D;
1514 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1515 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1516 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1517 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1518 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1519 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1520 return ctx->Extensions.ARB_texture_cube_map;
1521 case GL_TEXTURE_RECTANGLE_NV:
1522 return ctx->Extensions.NV_texture_rectangle;
1523 case GL_TEXTURE_1D_ARRAY_EXT:
1524 return (ctx->Extensions.MESA_texture_array ||
1525 ctx->Extensions.EXT_texture_array);
1533 case GL_TEXTURE_2D_ARRAY_EXT:
1534 return (ctx->Extensions.MESA_texture_array ||
1535 ctx->Extensions.EXT_texture_array);
1540 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1548 * Test the glTexImage[123]D() parameters for errors.
1550 * \param ctx GL context.
1551 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1552 * \param target texture target given by the user.
1553 * \param level image level given by the user.
1554 * \param internalFormat internal format given by the user.
1555 * \param format pixel data format given by the user.
1556 * \param type pixel data type given by the user.
1557 * \param width image width given by the user.
1558 * \param height image height given by the user.
1559 * \param depth image depth given by the user.
1560 * \param border image border given by the user.
1562 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1564 * Verifies each of the parameters against the constants specified in
1565 * __struct gl_contextRec::Const and the supported extensions, and according
1566 * to the OpenGL specification.
1569 texture_error_check( struct gl_context *ctx,
1570 GLuint dimensions, GLenum target,
1571 GLint level, GLint internalFormat,
1572 GLenum format, GLenum type,
1573 GLint width, GLint height,
1574 GLint depth, GLint border )
1576 const GLenum proxyTarget = get_proxy_target(target);
1577 const GLboolean isProxy = target == proxyTarget;
1578 GLboolean sizeOK = GL_TRUE;
1579 GLboolean colorFormat, indexFormat;
1581 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1582 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1584 _mesa_error(ctx, GL_INVALID_VALUE,
1585 "glTexImage%dD(level=%d)", dimensions, level);
1591 if (border < 0 || border > 1 ||
1592 ((target == GL_TEXTURE_RECTANGLE_NV ||
1593 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1595 _mesa_error(ctx, GL_INVALID_VALUE,
1596 "glTexImage%dD(border=%d)", dimensions, border);
1601 if (width < 0 || height < 0 || depth < 0) {
1603 _mesa_error(ctx, GL_INVALID_VALUE,
1604 "glTexImage%dD(width, height or depth < 0)", dimensions);
1609 /* Do this simple check before calling the TestProxyTexImage() function */
1610 if (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
1611 sizeOK = (width == height);
1615 * Use the proxy texture driver hook to see if the size/level/etc are
1618 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1619 internalFormat, format,
1620 type, width, height,
1624 _mesa_error(ctx, GL_INVALID_VALUE,
1625 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1626 dimensions, level, width, height, depth);
1631 /* Check internalFormat */
1632 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1634 _mesa_error(ctx, GL_INVALID_VALUE,
1635 "glTexImage%dD(internalFormat=%s)",
1636 dimensions, _mesa_lookup_enum_by_nr(internalFormat));
1641 /* Check incoming image format and type */
1642 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1643 /* Normally, GL_INVALID_OPERATION is generated by a format/type
1644 * mismatch (see the 1.2 spec page 94, sec 3.6.4.). But with the
1645 * GL_EXT_texture_integer extension, some combinations should generate
1646 * GL_INVALID_ENUM instead (grr!).
1649 GLenum error = _mesa_is_integer_format(format)
1650 ? GL_INVALID_ENUM : GL_INVALID_OPERATION;
1651 _mesa_error(ctx, error,
1652 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1653 dimensions, format, type);
1658 /* make sure internal format and format basically agree */
1659 colorFormat = _mesa_is_color_format(format);
1660 indexFormat = _mesa_is_index_format(format);
1661 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1662 (_mesa_is_index_format(internalFormat) && !indexFormat) ||
1663 (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1664 (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1665 (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1666 (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1668 _mesa_error(ctx, GL_INVALID_OPERATION,
1669 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1670 dimensions, internalFormat, format);
1674 /* additional checks for ycbcr textures */
1675 if (internalFormat == GL_YCBCR_MESA) {
1676 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1677 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1678 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1680 _mesa_snprintf(message, sizeof(message),
1681 "glTexImage%dD(format/type YCBCR mismatch", dimensions);
1682 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1683 return GL_TRUE; /* error */
1685 if (target != GL_TEXTURE_2D &&
1686 target != GL_PROXY_TEXTURE_2D &&
1687 target != GL_TEXTURE_RECTANGLE_NV &&
1688 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1690 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1696 _mesa_snprintf(message, sizeof(message),
1697 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1698 dimensions, border);
1699 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1705 /* additional checks for depth textures */
1706 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1707 /* Only 1D, 2D, rect and array textures supported, not 3D or cubes */
1708 if (target != GL_TEXTURE_1D &&
1709 target != GL_PROXY_TEXTURE_1D &&
1710 target != GL_TEXTURE_2D &&
1711 target != GL_PROXY_TEXTURE_2D &&
1712 target != GL_TEXTURE_1D_ARRAY &&
1713 target != GL_PROXY_TEXTURE_1D_ARRAY &&
1714 target != GL_TEXTURE_2D_ARRAY &&
1715 target != GL_PROXY_TEXTURE_2D_ARRAY &&
1716 target != GL_TEXTURE_RECTANGLE_ARB &&
1717 target != GL_PROXY_TEXTURE_RECTANGLE_ARB) {
1719 _mesa_error(ctx, GL_INVALID_ENUM,
1720 "glTexImage(target/internalFormat)");
1725 /* additional checks for compressed textures */
1726 if (_mesa_is_compressed_format(ctx, internalFormat)) {
1727 if (!target_can_be_compressed(ctx, target, internalFormat)) {
1729 _mesa_error(ctx, GL_INVALID_ENUM,
1730 "glTexImage%dD(target)", dimensions);
1735 _mesa_error(ctx, GL_INVALID_OPERATION,
1736 "glTexImage%dD(border!=0)", dimensions);
1742 /* additional checks for integer textures */
1743 if (ctx->Extensions.EXT_texture_integer &&
1744 (_mesa_is_integer_format(format) !=
1745 _mesa_is_integer_format(internalFormat))) {
1747 _mesa_error(ctx, GL_INVALID_OPERATION,
1748 "glTexImage%dD(integer/non-integer format mismatch)",
1754 /* if we get here, the parameters are OK */
1760 * Test glTexSubImage[123]D() parameters for errors.
1762 * \param ctx GL context.
1763 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1764 * \param target texture target given by the user.
1765 * \param level image level given by the user.
1766 * \param xoffset sub-image x offset given by the user.
1767 * \param yoffset sub-image y offset given by the user.
1768 * \param zoffset sub-image z offset given by the user.
1769 * \param format pixel data format given by the user.
1770 * \param type pixel data type given by the user.
1771 * \param width image width given by the user.
1772 * \param height image height given by the user.
1773 * \param depth image depth given by the user.
1775 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1777 * Verifies each of the parameters against the constants specified in
1778 * __struct gl_contextRec::Const and the supported extensions, and according
1779 * to the OpenGL specification.
1782 subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
1783 GLenum target, GLint level,
1784 GLint xoffset, GLint yoffset, GLint zoffset,
1785 GLint width, GLint height, GLint depth,
1786 GLenum format, GLenum type )
1788 /* Basic level check */
1789 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1790 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1794 /* Check for negative sizes */
1796 _mesa_error(ctx, GL_INVALID_VALUE,
1797 "glTexSubImage%dD(width=%d)", dimensions, width);
1800 if (height < 0 && dimensions > 1) {
1801 _mesa_error(ctx, GL_INVALID_VALUE,
1802 "glTexSubImage%dD(height=%d)", dimensions, height);
1805 if (depth < 0 && dimensions > 2) {
1806 _mesa_error(ctx, GL_INVALID_VALUE,
1807 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1811 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1812 /* As with the glTexImage2D check above, the error code here
1813 * depends on texture integer.
1815 GLenum error = _mesa_is_integer_format(format)
1816 ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
1817 _mesa_error(ctx, error,
1818 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1819 dimensions, format, type);
1828 * Do second part of glTexSubImage which depends on the destination texture.
1829 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1832 subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
1833 GLenum target, GLint level,
1834 GLint xoffset, GLint yoffset, GLint zoffset,
1835 GLint width, GLint height, GLint depth,
1836 GLenum format, GLenum type,
1837 const struct gl_texture_image *destTex )
1840 /* undefined image level */
1841 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1845 if (xoffset < -((GLint)destTex->Border)) {
1846 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1850 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1851 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1855 if (dimensions > 1) {
1856 if (yoffset < -((GLint)destTex->Border)) {
1857 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1861 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1862 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1867 if (dimensions > 2) {
1868 if (zoffset < -((GLint)destTex->Border)) {
1869 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1872 if (zoffset + depth > (GLint) (destTex->Depth + destTex->Border)) {
1873 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1878 if (_mesa_is_format_compressed(destTex->TexFormat)) {
1881 /* do tests which depend on compression block size */
1882 _mesa_get_format_block_size(destTex->TexFormat, &bw, &bh);
1884 /* offset must be multiple of block size */
1885 if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
1886 _mesa_error(ctx, GL_INVALID_OPERATION,
1887 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
1888 dimensions, xoffset, yoffset);
1891 /* size must be multiple of bw by bh or equal to whole texture size */
1892 if ((width % bw != 0) && (GLuint) width != destTex->Width) {
1893 _mesa_error(ctx, GL_INVALID_OPERATION,
1894 "glTexSubImage%dD(width = %d)", dimensions, width);
1897 if ((height % bh != 0) && (GLuint) height != destTex->Height) {
1898 _mesa_error(ctx, GL_INVALID_OPERATION,
1899 "glTexSubImage%dD(height = %d)", dimensions, height);
1909 * Test glCopyTexImage[12]D() parameters for errors.
1911 * \param ctx GL context.
1912 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1913 * \param target texture target given by the user.
1914 * \param level image level given by the user.
1915 * \param internalFormat internal format given by the user.
1916 * \param width image width given by the user.
1917 * \param height image height given by the user.
1918 * \param border texture border.
1920 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1922 * Verifies each of the parameters against the constants specified in
1923 * __struct gl_contextRec::Const and the supported extensions, and according
1924 * to the OpenGL specification.
1927 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
1928 GLenum target, GLint level, GLint internalFormat,
1929 GLint width, GLint height, GLint border )
1931 const GLenum proxyTarget = get_proxy_target(target);
1932 const GLenum type = GL_FLOAT;
1937 if (!legal_texsubimage_target(ctx, dimensions, target)) {
1938 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
1939 dimensions, _mesa_lookup_enum_by_nr(target));
1943 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1944 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1945 _mesa_error(ctx, GL_INVALID_VALUE,
1946 "glCopyTexImage%dD(level=%d)", dimensions, level);
1950 /* Check that the source buffer is complete */
1951 if (ctx->ReadBuffer->Name) {
1952 if (ctx->ReadBuffer->_Status == 0) {
1953 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
1955 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1956 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1957 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
1963 if (border < 0 || border > 1 ||
1964 ((target == GL_TEXTURE_RECTANGLE_NV ||
1965 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1969 format = _mesa_base_tex_format(ctx, internalFormat);
1971 _mesa_error(ctx, GL_INVALID_VALUE,
1972 "glCopyTexImage%dD(internalFormat)", dimensions);
1976 if (!_mesa_source_buffer_exists(ctx, format)) {
1977 _mesa_error(ctx, GL_INVALID_OPERATION,
1978 "glCopyTexImage%dD(missing readbuffer)", dimensions);
1982 /* Do size, level checking */
1983 sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
1984 ? (width == height) : 1;
1986 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1987 internalFormat, format,
1988 type, width, height,
1992 if (dimensions == 1) {
1993 _mesa_error(ctx, GL_INVALID_VALUE,
1994 "glCopyTexImage1D(width=%d)", width);
1997 ASSERT(dimensions == 2);
1998 _mesa_error(ctx, GL_INVALID_VALUE,
1999 "glCopyTexImage2D(width=%d, height=%d)", width, height);
2004 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2005 if (!target_can_be_compressed(ctx, target, internalFormat)) {
2006 _mesa_error(ctx, GL_INVALID_ENUM,
2007 "glCopyTexImage%dD(target)", dimensions);
2011 _mesa_error(ctx, GL_INVALID_OPERATION,
2012 "glCopyTexImage%dD(border!=0)", dimensions);
2016 else if (_mesa_is_depth_format(internalFormat)) {
2017 /* make sure we have depth/stencil buffers */
2018 if (!ctx->ReadBuffer->_DepthBuffer) {
2019 _mesa_error(ctx, GL_INVALID_OPERATION,
2020 "glCopyTexImage%dD(no depth)", dimensions);
2024 else if (_mesa_is_depthstencil_format(internalFormat)) {
2025 /* make sure we have depth/stencil buffers */
2026 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
2027 _mesa_error(ctx, GL_INVALID_OPERATION,
2028 "glCopyTexImage%dD(no depth/stencil buffer)", dimensions);
2033 /* if we get here, the parameters are OK */
2039 * Test glCopyTexSubImage[12]D() parameters for errors.
2040 * Note that this is the first part of error checking.
2041 * See also copytexsubimage_error_check2() below for the second part.
2043 * \param ctx GL context.
2044 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2045 * \param target texture target given by the user.
2046 * \param level image level given by the user.
2048 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2051 copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
2052 GLenum target, GLint level)
2054 /* Check that the source buffer is complete */
2055 if (ctx->ReadBuffer->Name) {
2056 if (ctx->ReadBuffer->_Status == 0) {
2057 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2059 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2060 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2061 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2066 /* check target (proxies not allowed) */
2067 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2068 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
2069 dimensions, _mesa_lookup_enum_by_nr(target));
2074 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2075 _mesa_error(ctx, GL_INVALID_VALUE,
2076 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2085 * Second part of error checking for glCopyTexSubImage[12]D().
2086 * \param xoffset sub-image x offset given by the user.
2087 * \param yoffset sub-image y offset given by the user.
2088 * \param zoffset sub-image z offset given by the user.
2089 * \param width image width given by the user.
2090 * \param height image height given by the user.
2093 copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
2094 GLenum target, GLint level,
2095 GLint xoffset, GLint yoffset, GLint zoffset,
2096 GLsizei width, GLsizei height,
2097 const struct gl_texture_image *teximage )
2099 /* check that dest tex image exists */
2101 _mesa_error(ctx, GL_INVALID_OPERATION,
2102 "glCopyTexSubImage%dD(undefined texture level: %d)",
2109 _mesa_error(ctx, GL_INVALID_VALUE,
2110 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
2113 if (dimensions > 1 && height < 0) {
2114 _mesa_error(ctx, GL_INVALID_VALUE,
2115 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
2119 /* check x/y offsets */
2120 if (xoffset < -((GLint)teximage->Border)) {
2121 _mesa_error(ctx, GL_INVALID_VALUE,
2122 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
2125 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
2126 _mesa_error(ctx, GL_INVALID_VALUE,
2127 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
2130 if (dimensions > 1) {
2131 if (yoffset < -((GLint)teximage->Border)) {
2132 _mesa_error(ctx, GL_INVALID_VALUE,
2133 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
2136 /* NOTE: we're adding the border here, not subtracting! */
2137 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
2138 _mesa_error(ctx, GL_INVALID_VALUE,
2139 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
2144 /* check z offset */
2145 if (dimensions > 2) {
2146 if (zoffset < -((GLint)teximage->Border)) {
2147 _mesa_error(ctx, GL_INVALID_VALUE,
2148 "glCopyTexSubImage%dD(zoffset)", dimensions);
2151 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
2152 _mesa_error(ctx, GL_INVALID_VALUE,
2153 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
2158 if (_mesa_is_format_compressed(teximage->TexFormat)) {
2159 /* offset must be multiple of 4 */
2160 if ((xoffset & 3) || (yoffset & 3)) {
2161 _mesa_error(ctx, GL_INVALID_VALUE,
2162 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions);
2165 /* size must be multiple of 4 */
2166 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
2167 _mesa_error(ctx, GL_INVALID_VALUE,
2168 "glCopyTexSubImage%dD(width)", dimensions);
2171 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
2172 _mesa_error(ctx, GL_INVALID_VALUE,
2173 "glCopyTexSubImage%dD(height)", dimensions);
2178 if (teximage->InternalFormat == GL_YCBCR_MESA) {
2179 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2183 if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
2184 _mesa_error(ctx, GL_INVALID_OPERATION,
2185 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2186 dimensions, teximage->_BaseFormat);
2190 if (teximage->_BaseFormat == GL_DEPTH_COMPONENT) {
2191 if (!ctx->ReadBuffer->_DepthBuffer) {
2192 _mesa_error(ctx, GL_INVALID_OPERATION,
2193 "glCopyTexSubImage%dD(no depth buffer)",
2198 else if (teximage->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
2199 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
2200 _mesa_error(ctx, GL_INVALID_OPERATION,
2201 "glCopyTexSubImage%dD(no depth/stencil buffer)",
2207 /* If copying into an integer texture, the source buffer must also be
2210 if (_mesa_is_format_integer_color(teximage->TexFormat)) {
2211 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2212 if (!_mesa_is_format_integer_color(rb->Format)) {
2213 _mesa_error(ctx, GL_INVALID_OPERATION,
2214 "glCopyTexSubImage%dD(source buffer is not integer format)",
2220 /* if we get here, the parameters are OK */
2225 /** Callback info for walking over FBO hash table */
2228 struct gl_context *ctx;
2229 struct gl_texture_object *texObj;
2235 * Check render to texture callback. Called from _mesa_HashWalk().
2238 check_rtt_cb(GLuint key, void *data, void *userData)
2240 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2241 const struct cb_info *info = (struct cb_info *) userData;
2242 struct gl_context *ctx = info->ctx;
2243 const struct gl_texture_object *texObj = info->texObj;
2244 const GLuint level = info->level, face = info->face;
2246 /* If this is a user-created FBO */
2249 /* check if any of the FBO's attachments point to 'texObj' */
2250 for (i = 0; i < BUFFER_COUNT; i++) {
2251 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2252 if (att->Type == GL_TEXTURE &&
2253 att->Texture == texObj &&
2254 att->TextureLevel == level &&
2255 att->CubeMapFace == face) {
2256 ASSERT(_mesa_get_attachment_teximage(att));
2257 /* Tell driver about the new renderbuffer texture */
2258 ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2259 /* Mark fb status as indeterminate to force re-validation */
2268 * When a texture image is specified we have to check if it's bound to
2269 * any framebuffer objects (render to texture) in order to detect changes
2270 * in size or format since that effects FBO completeness.
2271 * Any FBOs rendering into the texture must be re-validated.
2274 update_fbo_texture(struct gl_context *ctx, struct gl_texture_object *texObj,
2275 GLuint face, GLuint level)
2277 /* Only check this texture if it's been marked as RenderToTexture */
2278 if (texObj->_RenderToTexture) {
2279 struct cb_info info;
2281 info.texObj = texObj;
2284 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2290 * If the texture object's GenerateMipmap flag is set and we've
2291 * changed the texture base level image, regenerate the rest of the
2292 * mipmap levels now.
2295 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2296 struct gl_texture_object *texObj, GLint level)
2298 ASSERT(target != GL_TEXTURE_CUBE_MAP);
2299 if (texObj->GenerateMipmap &&
2300 level == texObj->BaseLevel &&
2301 level < texObj->MaxLevel) {
2302 ASSERT(ctx->Driver.GenerateMipmap);
2303 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2308 /** Debug helper: override the user-requested internal format */
2310 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2313 if (internalFormat == GL_RGBA16F_ARB ||
2314 internalFormat == GL_RGBA32F_ARB) {
2315 printf("Convert rgba float tex to int %d x %d\n", width, height);
2318 else if (internalFormat == GL_RGB16F_ARB ||
2319 internalFormat == GL_RGB32F_ARB) {
2320 printf("Convert rgb float tex to int %d x %d\n", width, height);
2323 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2324 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2325 printf("Convert luminance float tex to int %d x %d\n", width, height);
2326 return GL_LUMINANCE_ALPHA;
2328 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2329 internalFormat == GL_LUMINANCE32F_ARB) {
2330 printf("Convert luminance float tex to int %d x %d\n", width, height);
2331 return GL_LUMINANCE;
2333 else if (internalFormat == GL_ALPHA16F_ARB ||
2334 internalFormat == GL_ALPHA32F_ARB) {
2335 printf("Convert luminance float tex to int %d x %d\n", width, height);
2339 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2340 internalFormat = GL_RGBA;
2344 return internalFormat;
2347 return internalFormat;
2353 * Choose the actual hardware format for a texture image.
2354 * Try to use the same format as the previous image level when possible.
2355 * Otherwise, ask the driver for the best format.
2356 * It's important to try to choose a consistant format for all levels
2357 * for efficient texture memory layout/allocation. In particular, this
2358 * comes up during automatic mipmap generation.
2361 _mesa_choose_texture_format(struct gl_context *ctx,
2362 struct gl_texture_object *texObj,
2363 GLenum target, GLint level,
2364 GLenum internalFormat, GLenum format, GLenum type)
2368 /* see if we've already chosen a format for the previous level */
2370 struct gl_texture_image *prevImage =
2371 _mesa_select_tex_image(ctx, texObj, target, level - 1);
2372 /* See if the prev level is defined and has an internal format which
2373 * matches the new internal format.
2376 prevImage->Width > 0 &&
2377 prevImage->InternalFormat == internalFormat) {
2378 /* use the same format */
2379 ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
2380 return prevImage->TexFormat;
2384 /* choose format from scratch */
2385 f = ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
2386 ASSERT(f != MESA_FORMAT_NONE);
2392 * Common code to implement all the glTexImage1D/2D/3D functions.
2395 teximage(struct gl_context *ctx, GLuint dims,
2396 GLenum target, GLint level, GLint internalFormat,
2397 GLsizei width, GLsizei height, GLsizei depth,
2398 GLint border, GLenum format, GLenum type,
2399 const GLvoid *pixels)
2403 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2405 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2406 _mesa_debug(ctx, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2408 _mesa_lookup_enum_by_nr(target), level,
2409 _mesa_lookup_enum_by_nr(internalFormat),
2410 width, height, depth, border,
2411 _mesa_lookup_enum_by_nr(format),
2412 _mesa_lookup_enum_by_nr(type), pixels);
2414 internalFormat = override_internal_format(internalFormat, width, height);
2416 /* target error checking */
2417 if (!legal_teximage_target(ctx, dims, target)) {
2418 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage%uD(target=%s)",
2419 dims, _mesa_lookup_enum_by_nr(target));
2423 /* general error checking */
2424 error = texture_error_check(ctx, dims, target, level, internalFormat,
2425 format, type, width, height, depth, border);
2427 if (_mesa_is_proxy_texture(target)) {
2428 /* Proxy texture: just clear or set state depending on error checking */
2429 struct gl_texture_image *texImage =
2430 _mesa_get_proxy_tex_image(ctx, target, level);
2433 /* when error, clear all proxy texture image parameters */
2435 clear_teximage_fields(texImage);
2438 /* no error, set the tex image parameters */
2439 struct gl_texture_object *texObj =
2440 _mesa_get_current_tex_object(ctx, target);
2441 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
2446 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
2447 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
2448 depth, border, internalFormat,
2451 else if (texImage) {
2452 clear_teximage_fields(texImage);
2457 /* non-proxy target */
2458 const GLuint face = _mesa_tex_target_to_face(target);
2459 struct gl_texture_object *texObj;
2460 struct gl_texture_image *texImage;
2463 return; /* error was recorded */
2466 if (ctx->NewState & _NEW_PIXEL)
2467 _mesa_update_state(ctx);
2469 texObj = _mesa_get_current_tex_object(ctx, target);
2471 _mesa_lock_texture(ctx, texObj);
2473 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2476 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2479 gl_format texFormat;
2481 if (texImage->Data) {
2482 ctx->Driver.FreeTexImageData( ctx, texImage );
2485 ASSERT(texImage->Data == NULL);
2486 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2487 internalFormat, format,
2490 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
2491 _mesa_init_teximage_fields(ctx, target, texImage,
2492 width, height, depth,
2493 border, internalFormat, texFormat);
2495 /* Give the texture to the driver. <pixels> may be null. */
2496 ASSERT(ctx->Driver.TexImage3D);
2499 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2500 width, border, format,
2501 type, pixels, &ctx->Unpack, texObj,
2505 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2506 width, height, border, format,
2507 type, pixels, &ctx->Unpack, texObj,
2511 ctx->Driver.TexImage3D(ctx, target, level, internalFormat,
2512 width, height, depth, border, format,
2513 type, pixels, &ctx->Unpack, texObj,
2517 _mesa_problem(ctx, "invalid dims=%u in teximage()", dims);
2520 check_gen_mipmap(ctx, target, texObj, level);
2522 update_fbo_texture(ctx, texObj, face, level);
2525 texObj->_Complete = GL_FALSE;
2526 ctx->NewState |= _NEW_TEXTURE;
2529 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2533 _mesa_unlock_texture(ctx, texObj);
2539 * Called from the API. Note that width includes the border.
2542 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2543 GLsizei width, GLint border, GLenum format,
2544 GLenum type, const GLvoid *pixels )
2546 GET_CURRENT_CONTEXT(ctx);
2547 teximage(ctx, 1, target, level, internalFormat, width, 1, 1,
2548 border, format, type, pixels);
2553 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2554 GLsizei width, GLsizei height, GLint border,
2555 GLenum format, GLenum type,
2556 const GLvoid *pixels )
2558 GET_CURRENT_CONTEXT(ctx);
2559 teximage(ctx, 2, target, level, internalFormat, width, height, 1,
2560 border, format, type, pixels);
2565 * Called by the API or display list executor.
2566 * Note that width and height include the border.
2569 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2570 GLsizei width, GLsizei height, GLsizei depth,
2571 GLint border, GLenum format, GLenum type,
2572 const GLvoid *pixels )
2574 GET_CURRENT_CONTEXT(ctx);
2575 teximage(ctx, 3, target, level, internalFormat, width, height, depth,
2576 border, format, type, pixels);
2581 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2582 GLsizei width, GLsizei height, GLsizei depth,
2583 GLint border, GLenum format, GLenum type,
2584 const GLvoid *pixels )
2586 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2587 depth, border, format, type, pixels);
2591 #if FEATURE_OES_EGL_image
2593 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
2595 struct gl_texture_object *texObj;
2596 struct gl_texture_image *texImage;
2597 GET_CURRENT_CONTEXT(ctx);
2598 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2600 if (!ctx->Extensions.OES_EGL_image) {
2601 _mesa_error(ctx, GL_INVALID_OPERATION,
2602 "glEGLImageTargetTexture2DOES(unsupported)");
2606 if (target != GL_TEXTURE_2D) {
2607 _mesa_error(ctx, GL_INVALID_ENUM,
2608 "glEGLImageTargetTexture2D(target=%d)", target);
2612 if (ctx->NewState & _NEW_PIXEL)
2613 _mesa_update_state(ctx);
2615 texObj = _mesa_get_current_tex_object(ctx, target);
2616 _mesa_lock_texture(ctx, texObj);
2618 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
2620 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
2623 ctx->Driver.FreeTexImageData( ctx, texImage );
2625 ASSERT(texImage->Data == NULL);
2626 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
2627 texObj, texImage, image);
2630 texObj->_Complete = GL_FALSE;
2631 ctx->NewState |= _NEW_TEXTURE;
2633 _mesa_unlock_texture(ctx, texObj);
2641 * Implement all the glTexSubImage1/2/3D() functions.
2644 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2645 GLint xoffset, GLint yoffset, GLint zoffset,
2646 GLsizei width, GLsizei height, GLsizei depth,
2647 GLenum format, GLenum type, const GLvoid *pixels )
2649 struct gl_texture_object *texObj;
2650 struct gl_texture_image *texImage;
2652 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2654 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2655 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
2657 _mesa_lookup_enum_by_nr(target), level,
2658 xoffset, yoffset, zoffset, width, height, depth,
2659 _mesa_lookup_enum_by_nr(format),
2660 _mesa_lookup_enum_by_nr(type), pixels);
2662 /* check target (proxies not allowed) */
2663 if (!legal_texsubimage_target(ctx, dims, target)) {
2664 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
2665 dims, _mesa_lookup_enum_by_nr(target));
2669 if (ctx->NewState & _NEW_PIXEL)
2670 _mesa_update_state(ctx);
2672 if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
2673 width, height, depth, format, type)) {
2674 return; /* error was detected */
2677 texObj = _mesa_get_current_tex_object(ctx, target);
2679 _mesa_lock_texture(ctx, texObj);
2681 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2683 if (subtexture_error_check2(ctx, dims, target, level,
2684 xoffset, yoffset, zoffset,
2685 width, height, depth,
2686 format, type, texImage)) {
2687 /* error was recorded */
2689 else if (width > 0 && height > 0 && depth > 0) {
2690 /* If we have a border, offset=-1 is legal. Bias by border width. */
2693 zoffset += texImage->Border;
2696 yoffset += texImage->Border;
2699 xoffset += texImage->Border;
2704 ctx->Driver.TexSubImage1D(ctx, target, level,
2706 format, type, pixels,
2707 &ctx->Unpack, texObj, texImage );
2710 ctx->Driver.TexSubImage2D(ctx, target, level,
2711 xoffset, yoffset, width, height,
2712 format, type, pixels,
2713 &ctx->Unpack, texObj, texImage );
2716 ctx->Driver.TexSubImage3D(ctx, target, level,
2717 xoffset, yoffset, zoffset,
2718 width, height, depth,
2719 format, type, pixels,
2720 &ctx->Unpack, texObj, texImage );
2723 _mesa_problem(ctx, "unexpected dims in subteximage()");
2726 check_gen_mipmap(ctx, target, texObj, level);
2728 ctx->NewState |= _NEW_TEXTURE;
2731 _mesa_unlock_texture(ctx, texObj);
2736 _mesa_TexSubImage1D( GLenum target, GLint level,
2737 GLint xoffset, GLsizei width,
2738 GLenum format, GLenum type,
2739 const GLvoid *pixels )
2741 GET_CURRENT_CONTEXT(ctx);
2742 texsubimage(ctx, 1, target, level,
2745 format, type, pixels);
2750 _mesa_TexSubImage2D( GLenum target, GLint level,
2751 GLint xoffset, GLint yoffset,
2752 GLsizei width, GLsizei height,
2753 GLenum format, GLenum type,
2754 const GLvoid *pixels )
2756 GET_CURRENT_CONTEXT(ctx);
2757 texsubimage(ctx, 2, target, level,
2758 xoffset, yoffset, 0,
2760 format, type, pixels);
2766 _mesa_TexSubImage3D( GLenum target, GLint level,
2767 GLint xoffset, GLint yoffset, GLint zoffset,
2768 GLsizei width, GLsizei height, GLsizei depth,
2769 GLenum format, GLenum type,
2770 const GLvoid *pixels )
2772 GET_CURRENT_CONTEXT(ctx);
2773 texsubimage(ctx, 3, target, level,
2774 xoffset, yoffset, zoffset,
2775 width, height, depth,
2776 format, type, pixels);
2782 * Implement the glCopyTexImage1/2D() functions.
2785 copyteximage(struct gl_context *ctx, GLuint dims,
2786 GLenum target, GLint level, GLenum internalFormat,
2787 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
2789 struct gl_texture_object *texObj;
2790 struct gl_texture_image *texImage;
2791 const GLuint face = _mesa_tex_target_to_face(target);
2793 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2795 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2796 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
2798 _mesa_lookup_enum_by_nr(target), level,
2799 _mesa_lookup_enum_by_nr(internalFormat),
2800 x, y, width, height, border);
2802 if (ctx->NewState & NEW_COPY_TEX_STATE)
2803 _mesa_update_state(ctx);
2805 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
2806 width, height, border))
2809 texObj = _mesa_get_current_tex_object(ctx, target);
2811 _mesa_lock_texture(ctx, texObj);
2813 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2816 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2819 /* choose actual hw format */
2820 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
2825 if (legal_texture_size(ctx, texFormat, width, height, 1)) {
2826 GLint srcX = x, srcY = y, dstX = 0, dstY = 0;
2828 /* Free old texture image */
2829 ctx->Driver.FreeTexImageData(ctx, texImage);
2831 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
2832 border, internalFormat, texFormat);
2834 /* Allocate texture memory (no pixel data yet) */
2836 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
2837 width, border, GL_NONE, GL_NONE, NULL,
2838 &ctx->Unpack, texObj, texImage);
2841 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
2842 width, height, border, GL_NONE, GL_NONE,
2843 NULL, &ctx->Unpack, texObj, texImage);
2846 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
2849 ctx->Driver.CopyTexSubImage1D(ctx, target, level, dstX,
2853 ctx->Driver.CopyTexSubImage2D(ctx, target, level, dstX, dstY,
2854 srcX, srcY, width, height);
2857 check_gen_mipmap(ctx, target, texObj, level);
2859 update_fbo_texture(ctx, texObj, face, level);
2862 texObj->_Complete = GL_FALSE;
2863 ctx->NewState |= _NEW_TEXTURE;
2866 /* probably too large of image */
2867 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2871 _mesa_unlock_texture(ctx, texObj);
2877 _mesa_CopyTexImage1D( GLenum target, GLint level,
2878 GLenum internalFormat,
2880 GLsizei width, GLint border )
2882 GET_CURRENT_CONTEXT(ctx);
2883 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
2889 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2890 GLint x, GLint y, GLsizei width, GLsizei height,
2893 GET_CURRENT_CONTEXT(ctx);
2894 copyteximage(ctx, 2, target, level, internalFormat,
2895 x, y, width, height, border);
2901 * Implementation for glCopyTexSubImage1/2/3D() functions.
2904 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2905 GLint xoffset, GLint yoffset, GLint zoffset,
2906 GLint x, GLint y, GLsizei width, GLsizei height)
2908 struct gl_texture_object *texObj;
2909 struct gl_texture_image *texImage;
2911 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2913 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2914 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
2916 _mesa_lookup_enum_by_nr(target),
2917 level, xoffset, yoffset, zoffset, x, y, width, height);
2919 if (ctx->NewState & NEW_COPY_TEX_STATE)
2920 _mesa_update_state(ctx);
2922 if (copytexsubimage_error_check1(ctx, dims, target, level))
2925 texObj = _mesa_get_current_tex_object(ctx, target);
2927 _mesa_lock_texture(ctx, texObj);
2929 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2931 if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
2932 zoffset, width, height, texImage)) {
2933 /* error was recored */
2936 /* If we have a border, offset=-1 is legal. Bias by border width. */
2939 zoffset += texImage->Border;
2942 yoffset += texImage->Border;
2945 xoffset += texImage->Border;
2948 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2952 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
2953 xoffset, x, y, width);
2956 ctx->Driver.CopyTexSubImage2D(ctx, target, level,
2958 x, y, width, height);
2961 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
2962 xoffset, yoffset, zoffset,
2963 x, y, width, height);
2966 _mesa_problem(ctx, "bad dims in copytexsubimage()");
2969 check_gen_mipmap(ctx, target, texObj, level);
2971 ctx->NewState |= _NEW_TEXTURE;
2975 _mesa_unlock_texture(ctx, texObj);
2980 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2981 GLint xoffset, GLint x, GLint y, GLsizei width )
2983 GET_CURRENT_CONTEXT(ctx);
2984 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
2990 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2991 GLint xoffset, GLint yoffset,
2992 GLint x, GLint y, GLsizei width, GLsizei height )
2994 GET_CURRENT_CONTEXT(ctx);
2995 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3002 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3003 GLint xoffset, GLint yoffset, GLint zoffset,
3004 GLint x, GLint y, GLsizei width, GLsizei height )
3006 GET_CURRENT_CONTEXT(ctx);
3007 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3008 x, y, width, height);
3014 /**********************************************************************/
3015 /****** Compressed Textures ******/
3016 /**********************************************************************/
3020 * Return expected size of a compressed texture.
3023 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
3026 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3027 return _mesa_format_image_size(mesaFormat, width, height, depth);
3032 * Return compressed texture block size, in pixels.
3035 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3037 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3038 _mesa_get_format_block_size(mesaFormat, bw, bh);
3043 * Error checking for glCompressedTexImage[123]D().
3044 * \param reason returns reason for error, if any
3045 * \return error code or GL_NO_ERROR.
3048 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
3049 GLenum target, GLint level,
3050 GLenum internalFormat, GLsizei width,
3051 GLsizei height, GLsizei depth, GLint border,
3052 GLsizei imageSize, char **reason)
3054 const GLenum proxyTarget = get_proxy_target(target);
3055 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
3057 GLenum choose_format;
3059 GLenum proxy_format;
3061 *reason = ""; /* no error */
3063 if (!target_can_be_compressed(ctx, target, internalFormat)) {
3065 return GL_INVALID_ENUM;
3068 /* This will detect any invalid internalFormat value */
3069 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
3070 *reason = "internalFormat";
3071 return GL_INVALID_ENUM;
3074 switch (internalFormat) {
3076 case GL_PALETTE4_RGB8_OES:
3077 case GL_PALETTE4_RGBA8_OES:
3078 case GL_PALETTE4_R5_G6_B5_OES:
3079 case GL_PALETTE4_RGBA4_OES:
3080 case GL_PALETTE4_RGB5_A1_OES:
3081 case GL_PALETTE8_RGB8_OES:
3082 case GL_PALETTE8_RGBA8_OES:
3083 case GL_PALETTE8_R5_G6_B5_OES:
3084 case GL_PALETTE8_RGBA4_OES:
3085 case GL_PALETTE8_RGB5_A1_OES:
3086 _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
3088 proxy_format = choose_format;
3091 if (level > 0 || level < -maxLevels) {
3093 return GL_INVALID_VALUE;
3096 if (dimensions != 2) {
3097 *reason = "compressed paletted textures must be 2D";
3098 return GL_INVALID_OPERATION;
3101 /* Figure out the expected texture size (in bytes). This will be
3102 * checked against the actual size later.
3104 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
3107 /* This is for the benefit of the TestProxyTexImage below. It expects
3108 * level to be non-negative. OES_compressed_paletted_texture uses a
3109 * weird mechanism where the level specified to glCompressedTexImage2D
3110 * is -(n-1) number of levels in the texture, and the data specifies the
3111 * complete mipmap stack. This is done to ensure the palette is the
3112 * same for all levels.
3119 choose_format = GL_NONE;
3120 choose_type = GL_NONE;
3121 proxy_format = internalFormat;
3124 if (level < 0 || level >= maxLevels) {
3126 return GL_INVALID_VALUE;
3129 /* Figure out the expected texture size (in bytes). This will be
3130 * checked against the actual size later.
3132 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3136 /* This should really never fail */
3137 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
3138 *reason = "internalFormat";
3139 return GL_INVALID_ENUM;
3142 /* No compressed formats support borders at this time */
3144 *reason = "border != 0";
3145 return GL_INVALID_VALUE;
3148 /* For cube map, width must equal height */
3149 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3150 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height) {
3151 *reason = "width != height";
3152 return GL_INVALID_VALUE;
3155 /* check image size against compression block size */
3157 gl_format texFormat =
3158 ctx->Driver.ChooseTextureFormat(ctx, proxy_format,
3159 choose_format, choose_type);
3162 _mesa_get_format_block_size(texFormat, &bw, &bh);
3163 if ((width > bw && width % bw > 0) ||
3164 (height > bh && height % bh > 0)) {
3166 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
3167 * generated [...] if any parameter combinations are not
3168 * supported by the specific compressed internal format.
3170 *reason = "invalid width or height for compression format";
3171 return GL_INVALID_OPERATION;
3175 /* check image sizes */
3176 if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
3177 proxy_format, choose_format,
3179 width, height, depth, border)) {
3180 /* See error comment above */
3181 *reason = "invalid width, height or format";
3182 return GL_INVALID_OPERATION;
3185 /* check image size in bytes */
3186 if (expectedSize != imageSize) {
3187 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
3188 * if <imageSize> is not consistent with the format, dimensions, and
3189 * contents of the specified image.
3191 *reason = "imageSize inconsistant with width/height/format";
3192 return GL_INVALID_VALUE;
3200 * Error checking for glCompressedTexSubImage[123]D().
3201 * \warning There are some bad assumptions here about the size of compressed
3202 * texture tiles (multiple of 4) used to test the validity of the
3203 * offset and size parameters.
3204 * \return error code or GL_NO_ERROR.
3207 compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3208 GLenum target, GLint level,
3209 GLint xoffset, GLint yoffset, GLint zoffset,
3210 GLsizei width, GLsizei height, GLsizei depth,
3211 GLenum format, GLsizei imageSize)
3213 GLint expectedSize, maxLevels = 0, maxTextureSize;
3217 if (dimensions == 1) {
3218 /* 1D compressed textures not allowed */
3219 return GL_INVALID_ENUM;
3221 else if (dimensions == 2) {
3222 if (target == GL_PROXY_TEXTURE_2D) {
3223 maxLevels = ctx->Const.MaxTextureLevels;
3225 else if (target == GL_TEXTURE_2D) {
3226 maxLevels = ctx->Const.MaxTextureLevels;
3228 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3229 if (!ctx->Extensions.ARB_texture_cube_map)
3230 return GL_INVALID_ENUM; /*target*/
3231 maxLevels = ctx->Const.MaxCubeTextureLevels;
3233 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3234 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3235 if (!ctx->Extensions.ARB_texture_cube_map)
3236 return GL_INVALID_ENUM; /*target*/
3237 maxLevels = ctx->Const.MaxCubeTextureLevels;
3240 return GL_INVALID_ENUM; /*target*/
3243 else if (dimensions == 3) {
3244 /* 3D compressed textures not allowed */
3245 return GL_INVALID_ENUM;
3248 maxTextureSize = 1 << (maxLevels - 1);
3250 /* this will catch any invalid compressed format token */
3251 if (!_mesa_is_compressed_format(ctx, format))
3252 return GL_INVALID_ENUM;
3254 if (width < 1 || width > maxTextureSize)
3255 return GL_INVALID_VALUE;
3257 if ((height < 1 || height > maxTextureSize)
3259 return GL_INVALID_VALUE;
3261 if (level < 0 || level >= maxLevels)
3262 return GL_INVALID_VALUE;
3265 * do checks which depend on compression block size
3267 get_compressed_block_size(format, &bw, &bh);
3269 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3270 return GL_INVALID_VALUE;
3272 if ((width % bw != 0) && width != 2 && width != 1)
3273 return GL_INVALID_VALUE;
3275 if ((height % bh != 0) && height != 2 && height != 1)
3276 return GL_INVALID_VALUE;
3278 expectedSize = compressed_tex_size(width, height, depth, format);
3279 if (expectedSize != imageSize)
3280 return GL_INVALID_VALUE;
3287 * Do second part of glCompressedTexSubImage error checking.
3288 * \return GL_TRUE if error found, GL_FALSE otherwise.
3291 compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3292 GLsizei width, GLsizei height,
3293 GLsizei depth, GLenum format,
3294 struct gl_texture_image *texImage)
3297 if ((GLint) format != texImage->InternalFormat) {
3298 _mesa_error(ctx, GL_INVALID_OPERATION,
3299 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3303 if (((width == 1 || width == 2) &&
3304 width != (GLsizei) texImage->Width) ||
3305 (width > (GLsizei) texImage->Width)) {
3306 _mesa_error(ctx, GL_INVALID_VALUE,
3307 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3312 if (((height == 1 || height == 2) &&
3313 height != (GLsizei) texImage->Height) ||
3314 (height > (GLsizei) texImage->Height)) {
3315 _mesa_error(ctx, GL_INVALID_VALUE,
3316 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3322 if (((depth == 1 || depth == 2) &&
3323 depth != (GLsizei) texImage->Depth) ||
3324 (depth > (GLsizei) texImage->Depth)) {
3325 _mesa_error(ctx, GL_INVALID_VALUE,
3326 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3336 * Implementation of the glCompressedTexImage1/2/3D() functions.
3339 compressedteximage(struct gl_context *ctx, GLuint dims,
3340 GLenum target, GLint level,
3341 GLenum internalFormat, GLsizei width,
3342 GLsizei height, GLsizei depth, GLint border,
3343 GLsizei imageSize, const GLvoid *data)
3348 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3350 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3352 "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3354 _mesa_lookup_enum_by_nr(target), level,
3355 _mesa_lookup_enum_by_nr(internalFormat),
3356 width, height, depth, border, imageSize, data);
3359 if (!legal_teximage_target(ctx, dims, target)) {
3360 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(target=%s)",
3361 dims, _mesa_lookup_enum_by_nr(target));
3365 error = compressed_texture_error_check(ctx, dims, target, level,
3366 internalFormat, width, height, depth,
3367 border, imageSize, &reason);
3370 _mesa_error(ctx, error, "glCompressedTexImage%uD(%s)", dims, reason);
3375 /* XXX this is kind of a hack */
3377 switch (internalFormat) {
3378 case GL_PALETTE4_RGB8_OES:
3379 case GL_PALETTE4_RGBA8_OES:
3380 case GL_PALETTE4_R5_G6_B5_OES:
3381 case GL_PALETTE4_RGBA4_OES:
3382 case GL_PALETTE4_RGB5_A1_OES:
3383 case GL_PALETTE8_RGB8_OES:
3384 case GL_PALETTE8_RGBA8_OES:
3385 case GL_PALETTE8_R5_G6_B5_OES:
3386 case GL_PALETTE8_RGBA4_OES:
3387 case GL_PALETTE8_RGB5_A1_OES:
3388 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3389 width, height, imageSize, data);
3395 if (_mesa_is_proxy_texture(target)) {
3396 /* Proxy texture: just check for errors and update proxy state */
3397 struct gl_texture_image *texImage;
3400 struct gl_texture_object *texObj =
3401 _mesa_get_current_tex_object(ctx, target);
3402 gl_format texFormat =
3403 _mesa_choose_texture_format(ctx, texObj, target, level,
3404 internalFormat, GL_NONE, GL_NONE);
3405 if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
3406 error = GL_OUT_OF_MEMORY;
3410 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3413 /* if error, clear all proxy texture image parameters */
3414 clear_teximage_fields(texImage);
3417 /* no error: store the teximage parameters */
3418 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3419 depth, border, internalFormat,
3425 /* non-proxy target */
3426 struct gl_texture_object *texObj;
3427 struct gl_texture_image *texImage;
3430 _mesa_error(ctx, error, "glCompressedTexImage%uD", dims);
3434 texObj = _mesa_get_current_tex_object(ctx, target);
3436 _mesa_lock_texture(ctx, texObj);
3438 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3440 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3441 "glCompressedTexImage%uD", dims);
3444 gl_format texFormat;
3446 if (texImage->Data) {
3447 ctx->Driver.FreeTexImageData( ctx, texImage );
3449 ASSERT(texImage->Data == NULL);
3451 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3452 internalFormat, GL_NONE,
3455 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3456 _mesa_init_teximage_fields(ctx, target, texImage,
3457 width, height, depth,
3458 border, internalFormat, texFormat);
3462 ASSERT(ctx->Driver.CompressedTexImage1D);
3463 ctx->Driver.CompressedTexImage1D(ctx, target, level,
3466 border, imageSize, data,
3470 ASSERT(ctx->Driver.CompressedTexImage2D);
3471 ctx->Driver.CompressedTexImage2D(ctx, target, level,
3474 border, imageSize, data,
3478 ASSERT(ctx->Driver.CompressedTexImage3D);
3479 ctx->Driver.CompressedTexImage3D(ctx, target, level,
3481 width, height, depth,
3482 border, imageSize, data,
3486 _mesa_problem(ctx, "bad dims in compressedteximage");
3489 check_gen_mipmap(ctx, target, texObj, level);
3492 texObj->_Complete = GL_FALSE;
3493 ctx->NewState |= _NEW_TEXTURE;
3496 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3497 "glCompressedTexImage%uD", dims);
3501 _mesa_unlock_texture(ctx, texObj);
3507 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3508 GLenum internalFormat, GLsizei width,
3509 GLint border, GLsizei imageSize,
3512 GET_CURRENT_CONTEXT(ctx);
3513 compressedteximage(ctx, 1, target, level, internalFormat,
3514 width, 1, 1, border, imageSize, data);
3519 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3520 GLenum internalFormat, GLsizei width,
3521 GLsizei height, GLint border, GLsizei imageSize,
3524 GET_CURRENT_CONTEXT(ctx);
3525 compressedteximage(ctx, 2, target, level, internalFormat,
3526 width, height, 1, border, imageSize, data);
3531 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3532 GLenum internalFormat, GLsizei width,
3533 GLsizei height, GLsizei depth, GLint border,
3534 GLsizei imageSize, const GLvoid *data)
3536 GET_CURRENT_CONTEXT(ctx);
3537 compressedteximage(ctx, 3, target, level, internalFormat,
3538 width, height, depth, border, imageSize, data);
3543 * Common helper for glCompressedTexSubImage1/2/3D().
3546 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3547 GLint xoffset, GLint yoffset, GLint zoffset,
3548 GLsizei width, GLsizei height, GLsizei depth,
3549 GLenum format, GLsizei imageSize, const GLvoid *data)
3551 struct gl_texture_object *texObj;
3552 struct gl_texture_image *texImage;
3554 GET_CURRENT_CONTEXT(ctx);
3555 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3557 error = compressed_subtexture_error_check(ctx, dims, target, level,
3558 xoffset, 0, 0, /* pos */
3559 width, height, depth, /* size */
3562 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3566 texObj = _mesa_get_current_tex_object(ctx, target);
3568 _mesa_lock_texture(ctx, texObj);
3570 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3573 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3574 format, texImage)) {
3575 /* error was recorded */
3577 else if (width > 0 && height > 0 && depth > 0) {
3580 if (ctx->Driver.CompressedTexSubImage1D) {
3581 ctx->Driver.CompressedTexSubImage1D(ctx, target, level,
3583 format, imageSize, data,
3588 if (ctx->Driver.CompressedTexSubImage2D) {
3589 ctx->Driver.CompressedTexSubImage2D(ctx, target, level,
3592 format, imageSize, data,
3597 if (ctx->Driver.CompressedTexSubImage3D) {
3598 ctx->Driver.CompressedTexSubImage3D(ctx, target, level,
3599 xoffset, yoffset, zoffset,
3600 width, height, depth,
3601 format, imageSize, data,
3609 check_gen_mipmap(ctx, target, texObj, level);
3611 ctx->NewState |= _NEW_TEXTURE;
3614 _mesa_unlock_texture(ctx, texObj);
3619 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3620 GLsizei width, GLenum format,
3621 GLsizei imageSize, const GLvoid *data)
3623 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3624 format, imageSize, data);
3629 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3630 GLint yoffset, GLsizei width, GLsizei height,
3631 GLenum format, GLsizei imageSize,
3634 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3635 width, height, 1, format, imageSize, data);
3640 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3641 GLint yoffset, GLint zoffset, GLsizei width,
3642 GLsizei height, GLsizei depth, GLenum format,
3643 GLsizei imageSize, const GLvoid *data)
3645 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3646 width, height, depth, format, imageSize, data);
3651 * Helper for glTexBuffer(). Check if internalFormat is legal. If so,
3652 * return the basic data type and number of components for the format.
3653 * \param return GL_TRUE if internalFormat is legal, GL_FALSE otherwise
3656 get_sized_format_info(const struct gl_context *ctx, GLenum internalFormat,
3657 GLenum *datatype, GLuint *components)
3659 switch (internalFormat) {
3661 *datatype = GL_UNSIGNED_BYTE;
3665 *datatype = GL_UNSIGNED_SHORT;
3668 case GL_ALPHA16F_ARB:
3669 *datatype = GL_HALF_FLOAT;
3672 case GL_ALPHA32F_ARB:
3673 *datatype = GL_FLOAT;
3676 case GL_ALPHA8I_EXT:
3677 *datatype = GL_BYTE;
3680 case GL_ALPHA16I_EXT:
3681 *datatype = GL_SHORT;
3684 case GL_ALPHA32I_EXT:
3688 case GL_ALPHA8UI_EXT:
3689 *datatype = GL_UNSIGNED_BYTE;
3692 case GL_ALPHA16UI_EXT:
3693 *datatype = GL_UNSIGNED_SHORT;
3696 case GL_ALPHA32UI_EXT:
3697 *datatype = GL_UNSIGNED_INT;
3701 *datatype = GL_UNSIGNED_BYTE;
3704 case GL_LUMINANCE16:
3705 *datatype = GL_UNSIGNED_SHORT;
3708 case GL_LUMINANCE16F_ARB:
3709 *datatype = GL_HALF_FLOAT;
3712 case GL_LUMINANCE32F_ARB:
3713 *datatype = GL_FLOAT;
3716 case GL_LUMINANCE8I_EXT:
3717 *datatype = GL_BYTE;
3720 case GL_LUMINANCE16I_EXT:
3721 *datatype = GL_SHORT;
3724 case GL_LUMINANCE32I_EXT:
3728 case GL_LUMINANCE8UI_EXT:
3729 *datatype = GL_UNSIGNED_BYTE;
3732 case GL_LUMINANCE16UI_EXT:
3733 *datatype = GL_UNSIGNED_SHORT;
3736 case GL_LUMINANCE32UI_EXT:
3737 *datatype = GL_UNSIGNED_INT;
3740 case GL_LUMINANCE8_ALPHA8:
3741 *datatype = GL_UNSIGNED_BYTE;
3744 case GL_LUMINANCE16_ALPHA16:
3745 *datatype = GL_UNSIGNED_SHORT;
3748 case GL_LUMINANCE_ALPHA16F_ARB:
3749 *datatype = GL_HALF_FLOAT;
3752 case GL_LUMINANCE_ALPHA32F_ARB:
3753 *datatype = GL_FLOAT;
3756 case GL_LUMINANCE_ALPHA8I_EXT:
3757 *datatype = GL_BYTE;
3760 case GL_LUMINANCE_ALPHA16I_EXT:
3761 *datatype = GL_SHORT;
3764 case GL_LUMINANCE_ALPHA32I_EXT:
3768 case GL_LUMINANCE_ALPHA8UI_EXT:
3769 *datatype = GL_UNSIGNED_BYTE;
3772 case GL_LUMINANCE_ALPHA16UI_EXT:
3773 *datatype = GL_UNSIGNED_SHORT;
3776 case GL_LUMINANCE_ALPHA32UI_EXT:
3777 *datatype = GL_UNSIGNED_INT;
3781 *datatype = GL_UNSIGNED_BYTE;
3784 case GL_INTENSITY16:
3785 *datatype = GL_UNSIGNED_SHORT;
3788 case GL_INTENSITY16F_ARB:
3789 *datatype = GL_HALF_FLOAT;
3792 case GL_INTENSITY32F_ARB:
3793 *datatype = GL_FLOAT;
3796 case GL_INTENSITY8I_EXT:
3797 *datatype = GL_BYTE;
3800 case GL_INTENSITY16I_EXT:
3801 *datatype = GL_SHORT;
3804 case GL_INTENSITY32I_EXT:
3808 case GL_INTENSITY8UI_EXT:
3809 *datatype = GL_UNSIGNED_BYTE;
3812 case GL_INTENSITY16UI_EXT:
3813 *datatype = GL_UNSIGNED_SHORT;
3816 case GL_INTENSITY32UI_EXT:
3817 *datatype = GL_UNSIGNED_INT;
3821 *datatype = GL_UNSIGNED_BYTE;
3825 *datatype = GL_UNSIGNED_SHORT;
3828 case GL_RGBA16F_ARB:
3829 *datatype = GL_HALF_FLOAT;
3832 case GL_RGBA32F_ARB:
3833 *datatype = GL_FLOAT;
3837 *datatype = GL_BYTE;
3840 case GL_RGBA16I_EXT:
3841 *datatype = GL_SHORT;
3844 case GL_RGBA32I_EXT:
3848 case GL_RGBA8UI_EXT:
3849 *datatype = GL_UNSIGNED_BYTE;
3852 case GL_RGBA16UI_EXT:
3853 *datatype = GL_UNSIGNED_SHORT;
3856 case GL_RGBA32UI_EXT:
3857 *datatype = GL_UNSIGNED_INT;
3864 if (*datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3867 if (*datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3874 /** GL_ARB_texture_buffer_object */
3876 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
3878 struct gl_texture_object *texObj;
3879 struct gl_buffer_object *bufObj;
3883 GET_CURRENT_CONTEXT(ctx);
3884 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3886 if (!ctx->Extensions.ARB_texture_buffer_object) {
3887 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
3891 if (target != GL_TEXTURE_BUFFER_ARB) {
3892 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
3896 if (!get_sized_format_info(ctx, internalFormat, &dataType, &comps)) {
3897 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
3902 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3903 if (buffer && !bufObj) {
3904 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
3908 texObj = _mesa_get_current_tex_object(ctx, target);
3910 _mesa_lock_texture(ctx, texObj);
3912 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
3913 texObj->BufferObjectFormat = internalFormat;
3915 _mesa_unlock_texture(ctx, texObj);