2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Texture image-related functions.
33 #include "bufferobj.h"
37 #include "framebuffer.h"
42 #include "mfeatures.h"
44 #include "texcompress.h"
45 #include "texcompress_cpal.h"
50 #include "glformats.h"
53 /* Inexplicably, GL_HALF_FLOAT_OES has a different value than GL_HALF_FLOAT.
55 #ifndef GL_HALF_FLOAT_OES
56 #define GL_HALF_FLOAT_OES 0x8D61
60 * State changes which we care about for glCopyTex[Sub]Image() calls.
61 * In particular, we care about pixel transfer state and buffer state
62 * (such as glReadBuffer to make sure we read from the right renderbuffer).
64 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
69 * Return the simple base format for a given internal texture format.
70 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
72 * \param ctx GL context.
73 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
75 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
76 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
78 * This is the format which is used during texture application (i.e. the
79 * texture format and env mode determine the arithmetic used.
82 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
84 switch (internalFormat
) {
90 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_ALPHA
: -1;
97 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE
: -1;
99 case GL_LUMINANCE_ALPHA
:
100 case GL_LUMINANCE4_ALPHA4
:
101 case GL_LUMINANCE6_ALPHA2
:
102 case GL_LUMINANCE8_ALPHA8
:
103 case GL_LUMINANCE12_ALPHA4
:
104 case GL_LUMINANCE12_ALPHA12
:
105 case GL_LUMINANCE16_ALPHA16
:
106 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE_ALPHA
: -1;
112 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_INTENSITY
: -1;
114 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGB
: -1;
125 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGBA
: -1;
139 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
141 if (_mesa_is_gles(ctx
)) {
142 switch (internalFormat
) {
150 if (ctx
->Extensions
.ARB_ES2_compatibility
) {
151 switch (internalFormat
) {
159 if (ctx
->Extensions
.ARB_depth_texture
) {
160 switch (internalFormat
) {
161 case GL_DEPTH_COMPONENT
:
162 case GL_DEPTH_COMPONENT16
:
163 case GL_DEPTH_COMPONENT24
:
164 case GL_DEPTH_COMPONENT32
:
165 return GL_DEPTH_COMPONENT
;
171 switch (internalFormat
) {
172 case GL_COMPRESSED_ALPHA
:
174 case GL_COMPRESSED_LUMINANCE
:
176 case GL_COMPRESSED_LUMINANCE_ALPHA
:
177 return GL_LUMINANCE_ALPHA
;
178 case GL_COMPRESSED_INTENSITY
:
180 case GL_COMPRESSED_RGB
:
182 case GL_COMPRESSED_RGBA
:
188 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
189 switch (internalFormat
) {
190 case GL_COMPRESSED_RGB_FXT1_3DFX
:
192 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
199 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
200 switch (internalFormat
) {
201 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
203 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
204 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
205 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
212 if (ctx
->Extensions
.S3_s3tc
) {
213 switch (internalFormat
) {
225 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
226 if (internalFormat
== GL_YCBCR_MESA
)
227 return GL_YCBCR_MESA
;
230 if (ctx
->Extensions
.ARB_texture_float
) {
231 switch (internalFormat
) {
232 case GL_ALPHA16F_ARB
:
233 case GL_ALPHA32F_ARB
:
241 case GL_INTENSITY16F_ARB
:
242 case GL_INTENSITY32F_ARB
:
244 case GL_LUMINANCE16F_ARB
:
245 case GL_LUMINANCE32F_ARB
:
247 case GL_LUMINANCE_ALPHA16F_ARB
:
248 case GL_LUMINANCE_ALPHA32F_ARB
:
249 return GL_LUMINANCE_ALPHA
;
255 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
256 switch (internalFormat
) {
265 if (ctx
->Extensions
.EXT_texture_snorm
) {
266 switch (internalFormat
) {
281 case GL_RGBA16_SNORM
:
284 case GL_ALPHA8_SNORM
:
285 case GL_ALPHA16_SNORM
:
287 case GL_LUMINANCE_SNORM
:
288 case GL_LUMINANCE8_SNORM
:
289 case GL_LUMINANCE16_SNORM
:
291 case GL_LUMINANCE_ALPHA_SNORM
:
292 case GL_LUMINANCE8_ALPHA8_SNORM
:
293 case GL_LUMINANCE16_ALPHA16_SNORM
:
294 return GL_LUMINANCE_ALPHA
;
295 case GL_INTENSITY_SNORM
:
296 case GL_INTENSITY8_SNORM
:
297 case GL_INTENSITY16_SNORM
:
304 if (ctx
->Extensions
.EXT_packed_depth_stencil
) {
305 switch (internalFormat
) {
306 case GL_DEPTH_STENCIL_EXT
:
307 case GL_DEPTH24_STENCIL8_EXT
:
308 return GL_DEPTH_STENCIL_EXT
;
314 if (ctx
->Extensions
.EXT_texture_sRGB
) {
315 switch (internalFormat
) {
318 case GL_COMPRESSED_SRGB_EXT
:
319 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
321 case GL_SRGB_ALPHA_EXT
:
322 case GL_SRGB8_ALPHA8_EXT
:
323 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
324 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
325 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
326 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
328 case GL_SLUMINANCE_ALPHA_EXT
:
329 case GL_SLUMINANCE8_ALPHA8_EXT
:
330 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
331 return GL_LUMINANCE_ALPHA
;
332 case GL_SLUMINANCE_EXT
:
333 case GL_SLUMINANCE8_EXT
:
334 case GL_COMPRESSED_SLUMINANCE_EXT
:
341 if (ctx
->Version
>= 30 ||
342 ctx
->Extensions
.EXT_texture_integer
) {
343 switch (internalFormat
) {
345 case GL_RGBA16UI_EXT
:
346 case GL_RGBA32UI_EXT
:
362 if (ctx
->Extensions
.EXT_texture_integer
) {
363 switch (internalFormat
) {
364 case GL_ALPHA8UI_EXT
:
365 case GL_ALPHA16UI_EXT
:
366 case GL_ALPHA32UI_EXT
:
368 case GL_ALPHA16I_EXT
:
369 case GL_ALPHA32I_EXT
:
371 case GL_INTENSITY8UI_EXT
:
372 case GL_INTENSITY16UI_EXT
:
373 case GL_INTENSITY32UI_EXT
:
374 case GL_INTENSITY8I_EXT
:
375 case GL_INTENSITY16I_EXT
:
376 case GL_INTENSITY32I_EXT
:
378 case GL_LUMINANCE8UI_EXT
:
379 case GL_LUMINANCE16UI_EXT
:
380 case GL_LUMINANCE32UI_EXT
:
381 case GL_LUMINANCE8I_EXT
:
382 case GL_LUMINANCE16I_EXT
:
383 case GL_LUMINANCE32I_EXT
:
385 case GL_LUMINANCE_ALPHA8UI_EXT
:
386 case GL_LUMINANCE_ALPHA16UI_EXT
:
387 case GL_LUMINANCE_ALPHA32UI_EXT
:
388 case GL_LUMINANCE_ALPHA8I_EXT
:
389 case GL_LUMINANCE_ALPHA16I_EXT
:
390 case GL_LUMINANCE_ALPHA32I_EXT
:
391 return GL_LUMINANCE_ALPHA
;
397 if (ctx
->Extensions
.ARB_texture_rg
) {
398 switch (internalFormat
) {
400 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
402 if (!ctx
->Extensions
.ARB_half_float_pixel
)
406 if (!ctx
->Extensions
.ARB_texture_float
)
415 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
421 case GL_COMPRESSED_RED
:
425 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
427 if (!ctx
->Extensions
.ARB_half_float_pixel
)
431 if (!ctx
->Extensions
.ARB_texture_float
)
440 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
446 case GL_COMPRESSED_RG
:
453 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
454 switch (internalFormat
) {
462 if (ctx
->Extensions
.EXT_packed_float
) {
463 switch (internalFormat
) {
464 case GL_R11F_G11F_B10F_EXT
:
471 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
472 switch (internalFormat
) {
473 case GL_DEPTH_COMPONENT32F
:
474 return GL_DEPTH_COMPONENT
;
475 case GL_DEPTH32F_STENCIL8
:
476 return GL_DEPTH_STENCIL
;
482 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
483 switch (internalFormat
) {
484 case GL_COMPRESSED_RED_RGTC1
:
485 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
487 case GL_COMPRESSED_RG_RGTC2
:
488 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
495 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
496 switch (internalFormat
) {
497 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
498 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
500 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
501 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
502 return GL_LUMINANCE_ALPHA
;
508 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
509 switch (internalFormat
) {
510 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
511 return GL_LUMINANCE_ALPHA
;
517 if (ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
) {
518 switch (internalFormat
) {
519 case GL_ETC1_RGB8_OES
:
526 if (ctx
->API
== API_OPENGLES
) {
527 switch (internalFormat
) {
528 case GL_PALETTE4_RGB8_OES
:
529 case GL_PALETTE4_R5_G6_B5_OES
:
530 case GL_PALETTE8_RGB8_OES
:
531 case GL_PALETTE8_R5_G6_B5_OES
:
533 case GL_PALETTE4_RGBA8_OES
:
534 case GL_PALETTE8_RGB5_A1_OES
:
535 case GL_PALETTE4_RGBA4_OES
:
536 case GL_PALETTE4_RGB5_A1_OES
:
537 case GL_PALETTE8_RGBA8_OES
:
538 case GL_PALETTE8_RGBA4_OES
:
545 return -1; /* error */
550 * For cube map faces, return a face index in [0,5].
551 * For other targets return 0;
554 _mesa_tex_target_to_face(GLenum target
)
556 if (_mesa_is_cube_face(target
))
557 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
565 * Install gl_texture_image in a gl_texture_object according to the target
566 * and level parameters.
568 * \param tObj texture object.
569 * \param target texture target.
570 * \param level image level.
571 * \param texImage texture image.
574 set_tex_image(struct gl_texture_object
*tObj
,
575 GLenum target
, GLint level
,
576 struct gl_texture_image
*texImage
)
578 const GLuint face
= _mesa_tex_target_to_face(target
);
582 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
585 tObj
->Image
[face
][level
] = texImage
;
587 /* Set the 'back' pointer */
588 texImage
->TexObject
= tObj
;
589 texImage
->Level
= level
;
590 texImage
->Face
= face
;
595 * Allocate a texture image structure.
597 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
600 * \return a pointer to gl_texture_image struct with all fields initialized to
603 struct gl_texture_image
*
604 _mesa_new_texture_image( struct gl_context
*ctx
)
607 return CALLOC_STRUCT(gl_texture_image
);
612 * Free a gl_texture_image and associated data.
613 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
615 * \param texImage texture image.
617 * Free the texture image structure and the associated image data.
620 _mesa_delete_texture_image(struct gl_context
*ctx
,
621 struct gl_texture_image
*texImage
)
623 /* Free texImage->Data and/or any other driver-specific texture
626 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
627 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
633 * Test if a target is a proxy target.
635 * \param target texture target.
637 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
640 _mesa_is_proxy_texture(GLenum target
)
643 * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
644 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
646 assert(NUM_TEXTURE_TARGETS
== 7 + 2);
648 return (target
== GL_PROXY_TEXTURE_1D
||
649 target
== GL_PROXY_TEXTURE_2D
||
650 target
== GL_PROXY_TEXTURE_3D
||
651 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
652 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
653 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
654 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
);
659 * Return the proxy target which corresponds to the given texture target
662 _mesa_get_proxy_target(GLenum target
)
666 case GL_PROXY_TEXTURE_1D
:
667 return GL_PROXY_TEXTURE_1D
;
669 case GL_PROXY_TEXTURE_2D
:
670 return GL_PROXY_TEXTURE_2D
;
672 case GL_PROXY_TEXTURE_3D
:
673 return GL_PROXY_TEXTURE_3D
;
674 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
675 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
676 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
677 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
678 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
679 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
680 case GL_TEXTURE_CUBE_MAP_ARB
:
681 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
682 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
683 case GL_TEXTURE_RECTANGLE_NV
:
684 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
685 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
686 case GL_TEXTURE_1D_ARRAY_EXT
:
687 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
688 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
689 case GL_TEXTURE_2D_ARRAY_EXT
:
690 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
691 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
693 _mesa_problem(NULL
, "unexpected target in _mesa_get_proxy_target()");
700 * Get the texture object that corresponds to the target of the given
701 * texture unit. The target should have already been checked for validity.
703 * \param ctx GL context.
704 * \param texUnit texture unit.
705 * \param target texture target.
707 * \return pointer to the texture object on success, or NULL on failure.
709 struct gl_texture_object
*
710 _mesa_select_tex_object(struct gl_context
*ctx
,
711 const struct gl_texture_unit
*texUnit
,
714 const GLboolean arrayTex
= (ctx
->Extensions
.MESA_texture_array
||
715 ctx
->Extensions
.EXT_texture_array
);
719 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
720 case GL_PROXY_TEXTURE_1D
:
721 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
723 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
724 case GL_PROXY_TEXTURE_2D
:
725 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
727 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
728 case GL_PROXY_TEXTURE_3D
:
729 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
730 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
731 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
732 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
733 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
734 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
735 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
736 case GL_TEXTURE_CUBE_MAP_ARB
:
737 return ctx
->Extensions
.ARB_texture_cube_map
738 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
739 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
740 return ctx
->Extensions
.ARB_texture_cube_map
741 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
742 case GL_TEXTURE_RECTANGLE_NV
:
743 return ctx
->Extensions
.NV_texture_rectangle
744 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
745 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
746 return ctx
->Extensions
.NV_texture_rectangle
747 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
748 case GL_TEXTURE_1D_ARRAY_EXT
:
749 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
750 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
751 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
752 case GL_TEXTURE_2D_ARRAY_EXT
:
753 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
754 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
755 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
756 case GL_TEXTURE_BUFFER
:
757 return _mesa_is_desktop_gl(ctx
)
758 && ctx
->Extensions
.ARB_texture_buffer_object
759 ? texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
760 case GL_TEXTURE_EXTERNAL_OES
:
761 return ctx
->Extensions
.OES_EGL_image_external
762 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
764 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
771 * Return pointer to texture object for given target on current texture unit.
773 struct gl_texture_object
*
774 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
776 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
777 return _mesa_select_tex_object(ctx
, texUnit
, target
);
782 * Get a texture image pointer from a texture object, given a texture
783 * target and mipmap level. The target and level parameters should
784 * have already been error-checked.
786 * \param ctx GL context.
787 * \param texObj texture unit.
788 * \param target texture target.
789 * \param level image level.
791 * \return pointer to the texture image structure, or NULL on failure.
793 struct gl_texture_image
*
794 _mesa_select_tex_image(struct gl_context
*ctx
,
795 const struct gl_texture_object
*texObj
,
796 GLenum target
, GLint level
)
798 const GLuint face
= _mesa_tex_target_to_face(target
);
802 ASSERT(level
< MAX_TEXTURE_LEVELS
);
804 return texObj
->Image
[face
][level
];
809 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
810 * it and install it. Only return NULL if passed a bad parameter or run
813 struct gl_texture_image
*
814 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
815 GLenum target
, GLint level
)
817 struct gl_texture_image
*texImage
;
822 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
824 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
826 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
830 set_tex_image(texObj
, target
, level
, texImage
);
838 * Return pointer to the specified proxy texture image.
839 * Note that proxy textures are per-context, not per-texture unit.
840 * \return pointer to texture image or NULL if invalid target, invalid
841 * level, or out of memory.
843 static struct gl_texture_image
*
844 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
846 struct gl_texture_image
*texImage
;
853 case GL_PROXY_TEXTURE_1D
:
854 if (level
>= ctx
->Const
.MaxTextureLevels
)
856 texIndex
= TEXTURE_1D_INDEX
;
858 case GL_PROXY_TEXTURE_2D
:
859 if (level
>= ctx
->Const
.MaxTextureLevels
)
861 texIndex
= TEXTURE_2D_INDEX
;
863 case GL_PROXY_TEXTURE_3D
:
864 if (level
>= ctx
->Const
.Max3DTextureLevels
)
866 texIndex
= TEXTURE_3D_INDEX
;
868 case GL_PROXY_TEXTURE_CUBE_MAP
:
869 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
871 texIndex
= TEXTURE_CUBE_INDEX
;
873 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
876 texIndex
= TEXTURE_RECT_INDEX
;
878 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
879 if (level
>= ctx
->Const
.MaxTextureLevels
)
881 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
883 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
884 if (level
>= ctx
->Const
.MaxTextureLevels
)
886 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
892 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
894 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
896 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
899 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
900 /* Set the 'back' pointer */
901 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
908 * Get the maximum number of allowed mipmap levels.
910 * \param ctx GL context.
911 * \param target texture target.
913 * \return the maximum number of allowed mipmap levels for the given
914 * texture target, or zero if passed a bad target.
919 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
923 case GL_PROXY_TEXTURE_1D
:
925 case GL_PROXY_TEXTURE_2D
:
926 return ctx
->Const
.MaxTextureLevels
;
928 case GL_PROXY_TEXTURE_3D
:
929 return ctx
->Const
.Max3DTextureLevels
;
930 case GL_TEXTURE_CUBE_MAP
:
931 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
932 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
933 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
934 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
935 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
936 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
937 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
938 return ctx
->Extensions
.ARB_texture_cube_map
939 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
940 case GL_TEXTURE_RECTANGLE_NV
:
941 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
942 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
943 case GL_TEXTURE_1D_ARRAY_EXT
:
944 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
945 case GL_TEXTURE_2D_ARRAY_EXT
:
946 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
947 return (ctx
->Extensions
.MESA_texture_array
||
948 ctx
->Extensions
.EXT_texture_array
)
949 ? ctx
->Const
.MaxTextureLevels
: 0;
950 case GL_TEXTURE_BUFFER
:
951 return _mesa_is_desktop_gl(ctx
)
952 && ctx
->Extensions
.ARB_texture_buffer_object
954 case GL_TEXTURE_EXTERNAL_OES
:
957 return 0; /* bad target */
963 * Return number of dimensions per mipmap level for the given texture target.
966 _mesa_get_texture_dimensions(GLenum target
)
970 case GL_PROXY_TEXTURE_1D
:
973 case GL_TEXTURE_RECTANGLE
:
974 case GL_TEXTURE_CUBE_MAP
:
975 case GL_PROXY_TEXTURE_2D
:
976 case GL_PROXY_TEXTURE_RECTANGLE
:
977 case GL_PROXY_TEXTURE_CUBE_MAP
:
978 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
979 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
980 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
981 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
982 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
983 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
984 case GL_TEXTURE_1D_ARRAY
:
985 case GL_PROXY_TEXTURE_1D_ARRAY
:
986 case GL_TEXTURE_EXTERNAL_OES
:
989 case GL_PROXY_TEXTURE_3D
:
990 case GL_TEXTURE_2D_ARRAY
:
991 case GL_PROXY_TEXTURE_2D_ARRAY
:
993 case GL_TEXTURE_BUFFER
:
996 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1005 #if 000 /* not used anymore */
1007 * glTexImage[123]D can accept a NULL image pointer. In this case we
1008 * create a texture image with unspecified image contents per the OpenGL
1012 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1014 const GLint components
= _mesa_components_in_format(format
);
1015 const GLint numPixels
= width
* height
* depth
;
1016 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
1020 * Let's see if anyone finds this. If glTexImage2D() is called with
1021 * a NULL image pointer then load the texture image with something
1022 * interesting instead of leaving it indeterminate.
1025 static const char message
[8][32] = {
1026 " X X XXXXX XXX X ",
1027 " XX XX X X X X X ",
1029 " X X XXXX XXX XXXXX ",
1032 " X X XXXXX XXX X X ",
1036 GLubyte
*imgPtr
= data
;
1038 for (h
= 0; h
< depth
; h
++) {
1039 for (i
= 0; i
< height
; i
++) {
1040 GLint srcRow
= 7 - (i
% 8);
1041 for (j
= 0; j
< width
; j
++) {
1042 GLint srcCol
= j
% 32;
1043 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1044 for (k
= 0; k
< components
; k
++) {
1060 * Set the size and format-related fields of a gl_texture_image struct
1061 * to zero. This is used when a proxy texture test fails.
1064 clear_teximage_fields(struct gl_texture_image
*img
)
1067 img
->_BaseFormat
= 0;
1068 img
->InternalFormat
= 0;
1077 img
->HeightLog2
= 0;
1079 img
->TexFormat
= MESA_FORMAT_NONE
;
1084 * Initialize basic fields of the gl_texture_image struct.
1086 * \param ctx GL context.
1087 * \param img texture image structure to be initialized.
1088 * \param width image width.
1089 * \param height image height.
1090 * \param depth image depth.
1091 * \param border image border.
1092 * \param internalFormat internal format.
1093 * \param format the actual hardware format (one of MESA_FORMAT_*)
1095 * Fills in the fields of \p img with the given information.
1096 * Note: width, height and depth include the border.
1099 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1100 struct gl_texture_image
*img
,
1101 GLsizei width
, GLsizei height
, GLsizei depth
,
1102 GLint border
, GLenum internalFormat
,
1108 ASSERT(height
>= 0);
1111 target
= img
->TexObject
->Target
;
1112 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1113 ASSERT(img
->_BaseFormat
> 0);
1114 img
->InternalFormat
= internalFormat
;
1115 img
->Border
= border
;
1117 img
->Height
= height
;
1120 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1121 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1125 case GL_TEXTURE_BUFFER
:
1126 case GL_PROXY_TEXTURE_1D
:
1131 img
->HeightLog2
= 0;
1138 case GL_TEXTURE_1D_ARRAY
:
1139 case GL_PROXY_TEXTURE_1D_ARRAY
:
1140 img
->Height2
= height
; /* no border */
1141 img
->HeightLog2
= 0; /* not used */
1149 case GL_TEXTURE_RECTANGLE
:
1150 case GL_TEXTURE_CUBE_MAP
:
1151 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1152 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1153 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1154 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1155 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1156 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1157 case GL_TEXTURE_EXTERNAL_OES
:
1158 case GL_PROXY_TEXTURE_2D
:
1159 case GL_PROXY_TEXTURE_RECTANGLE
:
1160 case GL_PROXY_TEXTURE_CUBE_MAP
:
1161 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1162 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1169 case GL_TEXTURE_2D_ARRAY
:
1170 case GL_PROXY_TEXTURE_2D_ARRAY
:
1171 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1172 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1173 img
->Depth2
= depth
; /* no border */
1174 img
->DepthLog2
= 0; /* not used */
1177 case GL_PROXY_TEXTURE_3D
:
1178 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1179 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1180 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1181 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1184 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1188 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1189 img
->TexFormat
= format
;
1194 * Free and clear fields of the gl_texture_image struct.
1196 * \param ctx GL context.
1197 * \param texImage texture image structure to be cleared.
1199 * After the call, \p texImage will have no data associated with it. Its
1200 * fields are cleared so that its parent object will test incomplete.
1203 _mesa_clear_texture_image(struct gl_context
*ctx
,
1204 struct gl_texture_image
*texImage
)
1206 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1207 clear_teximage_fields(texImage
);
1212 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1213 * level, width, height and depth against the ctx->Const limits for textures.
1215 * A hardware driver might override this function if, for example, the
1216 * max 3D texture size is 512x512x64 (i.e. not a cube).
1218 * Note that width, height, depth == 0 is not an error. However, a
1219 * texture with zero width/height/depth will be considered "incomplete"
1220 * and texturing will effectively be disabled.
1222 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1223 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1224 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1225 * \param level as passed to glTexImage
1226 * \param internalFormat as passed to glTexImage
1227 * \param format as passed to glTexImage
1228 * \param type as passed to glTexImage
1229 * \param width as passed to glTexImage
1230 * \param height as passed to glTexImage
1231 * \param depth as passed to glTexImage
1232 * \param border as passed to glTexImage
1233 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1236 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1237 GLint internalFormat
, GLenum format
, GLenum type
,
1238 GLint width
, GLint height
, GLint depth
, GLint border
)
1242 (void) internalFormat
;
1247 case GL_PROXY_TEXTURE_1D
:
1248 if (level
>= ctx
->Const
.MaxTextureLevels
)
1250 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
1251 maxSize
>>= level
; /* level size */
1252 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1254 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1255 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1260 case GL_PROXY_TEXTURE_2D
:
1261 if (level
>= ctx
->Const
.MaxTextureLevels
)
1263 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1265 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1267 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1269 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1270 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1272 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1277 case GL_PROXY_TEXTURE_3D
:
1278 if (level
>= ctx
->Const
.Max3DTextureLevels
)
1280 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1282 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1284 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1286 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1288 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1289 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1291 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1293 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1298 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1301 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1302 if (width
< 0 || width
> maxSize
)
1304 if (height
< 0 || height
> maxSize
)
1308 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1309 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1311 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1313 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1315 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1317 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1318 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1320 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1325 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1326 if (level
>= ctx
->Const
.MaxTextureLevels
)
1328 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1330 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1332 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1334 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1335 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1340 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1341 if (level
>= ctx
->Const
.MaxTextureLevels
)
1343 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1345 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1347 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1349 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1351 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1352 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1354 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1360 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1367 * Check if the memory used by the texture would exceed the driver's limit.
1368 * This lets us support a max 3D texture size of 8K (for example) but
1369 * prevents allocating a full 8K x 8K x 8K texture.
1370 * XXX this could be rolled into the proxy texture size test (above) but
1371 * we don't have the actual texture internal format at that point.
1374 legal_texture_size(struct gl_context
*ctx
, gl_format format
,
1375 GLint width
, GLint height
, GLint depth
)
1377 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1378 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1379 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1384 * Return true if the format is only valid for glCompressedTexImage.
1387 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1390 case GL_ETC1_RGB8_OES
:
1391 case GL_PALETTE4_RGB8_OES
:
1392 case GL_PALETTE4_RGBA8_OES
:
1393 case GL_PALETTE4_R5_G6_B5_OES
:
1394 case GL_PALETTE4_RGBA4_OES
:
1395 case GL_PALETTE4_RGB5_A1_OES
:
1396 case GL_PALETTE8_RGB8_OES
:
1397 case GL_PALETTE8_RGBA8_OES
:
1398 case GL_PALETTE8_R5_G6_B5_OES
:
1399 case GL_PALETTE8_RGBA4_OES
:
1400 case GL_PALETTE8_RGB5_A1_OES
:
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
:
1452 return _mesa_is_desktop_gl(ctx
);
1460 case GL_PROXY_TEXTURE_2D
:
1461 return _mesa_is_desktop_gl(ctx
);
1462 case GL_PROXY_TEXTURE_CUBE_MAP
:
1463 return _mesa_is_desktop_gl(ctx
)
1464 && ctx
->Extensions
.ARB_texture_cube_map
;
1465 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1466 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1467 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1468 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1469 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1470 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1471 return ctx
->Extensions
.ARB_texture_cube_map
;
1472 case GL_TEXTURE_RECTANGLE_NV
:
1473 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1474 return _mesa_is_desktop_gl(ctx
)
1475 && ctx
->Extensions
.NV_texture_rectangle
;
1476 case GL_TEXTURE_1D_ARRAY_EXT
:
1477 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1478 return _mesa_is_desktop_gl(ctx
)
1479 && (ctx
->Extensions
.MESA_texture_array
||
1480 ctx
->Extensions
.EXT_texture_array
);
1488 case GL_PROXY_TEXTURE_3D
:
1489 return _mesa_is_desktop_gl(ctx
);
1490 case GL_TEXTURE_2D_ARRAY_EXT
:
1491 return (_mesa_is_desktop_gl(ctx
)
1492 && (ctx
->Extensions
.MESA_texture_array
||
1493 ctx
->Extensions
.EXT_texture_array
))
1494 || _mesa_is_gles3(ctx
);
1495 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1496 return _mesa_is_desktop_gl(ctx
)
1497 && (ctx
->Extensions
.MESA_texture_array
||
1498 ctx
->Extensions
.EXT_texture_array
);
1503 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1510 * Check if the given texture target value is legal for a
1511 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1512 * The difference compared to legal_teximage_target() above is that
1513 * proxy targets are not supported.
1516 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1520 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1525 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1526 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1527 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1528 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1529 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1530 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1531 return ctx
->Extensions
.ARB_texture_cube_map
;
1532 case GL_TEXTURE_RECTANGLE_NV
:
1533 return _mesa_is_desktop_gl(ctx
)
1534 && ctx
->Extensions
.NV_texture_rectangle
;
1535 case GL_TEXTURE_1D_ARRAY_EXT
:
1536 return _mesa_is_desktop_gl(ctx
)
1537 && (ctx
->Extensions
.MESA_texture_array
||
1538 ctx
->Extensions
.EXT_texture_array
);
1546 case GL_TEXTURE_2D_ARRAY_EXT
:
1547 return (_mesa_is_desktop_gl(ctx
)
1548 && (ctx
->Extensions
.MESA_texture_array
||
1549 ctx
->Extensions
.EXT_texture_array
))
1550 || _mesa_is_gles3(ctx
);
1555 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1563 * Helper function to determine if a texture object is mutable (in terms
1564 * of GL_ARB_texture_storage).
1567 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1569 if (ctx
->Extensions
.ARB_texture_storage
) {
1570 struct gl_texture_object
*texObj
=
1571 _mesa_get_current_tex_object(ctx
, target
);
1572 return !texObj
->Immutable
;
1579 _mesa_es_error_check_format_and_type(GLenum format
, GLenum type
,
1580 unsigned dimensions
)
1582 bool type_valid
= true;
1587 case GL_LUMINANCE_ALPHA
:
1588 type_valid
= (type
== GL_UNSIGNED_BYTE
1590 || type
== GL_HALF_FLOAT_OES
);
1594 type_valid
= (type
== GL_UNSIGNED_BYTE
1595 || type
== GL_UNSIGNED_SHORT_5_6_5
1597 || type
== GL_HALF_FLOAT_OES
);
1601 type_valid
= (type
== GL_UNSIGNED_BYTE
1602 || type
== GL_UNSIGNED_SHORT_4_4_4_4
1603 || type
== GL_UNSIGNED_SHORT_5_5_5_1
1605 || type
== GL_HALF_FLOAT_OES
1606 || type
== GL_UNSIGNED_INT_2_10_10_10_REV
);
1609 case GL_DEPTH_COMPONENT
:
1610 /* This format is filtered against invalid dimensionalities elsewhere.
1612 type_valid
= (type
== GL_UNSIGNED_SHORT
1613 || type
== GL_UNSIGNED_INT
);
1616 case GL_DEPTH_STENCIL
:
1617 /* This format is filtered against invalid dimensionalities elsewhere.
1619 type_valid
= (type
== GL_UNSIGNED_INT_24_8
);
1623 type_valid
= (type
== GL_UNSIGNED_BYTE
);
1625 /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1626 * the format does not appear to be allowed for 3D textures in OpenGL
1629 if (dimensions
!= 2)
1630 return GL_INVALID_VALUE
;
1635 return GL_INVALID_VALUE
;
1638 return type_valid
? GL_NO_ERROR
: GL_INVALID_OPERATION
;
1644 * Return expected size of a compressed texture.
1647 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1650 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1651 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1656 * Return compressed texture block size, in pixels.
1659 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
1661 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1662 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
1667 * Special value returned by error some texture error checking functions when
1668 * an error is detected and the proxy texture image's width/height/depth/format
1669 * fields should be zeroed-out.
1671 #define PROXY_ERROR 2
1675 * Test the glTexImage[123]D() parameters for errors.
1677 * \param ctx GL context.
1678 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1679 * \param target texture target given by the user.
1680 * \param level image level given by the user.
1681 * \param internalFormat internal format given by the user.
1682 * \param format pixel data format given by the user.
1683 * \param type pixel data type given by the user.
1684 * \param width image width given by the user.
1685 * \param height image height given by the user.
1686 * \param depth image depth given by the user.
1687 * \param border image border given by the user.
1689 * \return PROXY_ERROR if there's an error that should zero-out the proxy image,
1690 * GL_TRUE if a regular GL error is found, or GL_FALSE if no error,
1692 * Verifies each of the parameters against the constants specified in
1693 * __struct gl_contextRec::Const and the supported extensions, and according
1694 * to the OpenGL specification.
1697 texture_error_check( struct gl_context
*ctx
,
1698 GLuint dimensions
, GLenum target
,
1699 GLint level
, GLint internalFormat
,
1700 GLenum format
, GLenum type
,
1701 GLint width
, GLint height
,
1702 GLint depth
, GLint border
)
1704 const GLenum proxyTarget
= _mesa_get_proxy_target(target
);
1705 const GLboolean isProxy
= target
== proxyTarget
;
1706 GLboolean sizeOK
= GL_TRUE
;
1707 GLboolean colorFormat
;
1710 /* Even though there are no color-index textures, we still have to support
1711 * uploading color-index data and remapping it to RGB via the
1712 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1714 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1716 /* Note: for proxy textures, some error conditions immediately generate
1717 * a GL error in the usual way. But others do not generate a GL error.
1718 * Instead, they cause the width, height, depth, format fields of the
1719 * texture image to be zeroed-out. The GL spec seems to indicate that the
1720 * zero-out behaviour is only used in cases related to memory allocation.
1723 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1724 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1725 _mesa_error(ctx
, GL_INVALID_VALUE
,
1726 "glTexImage%dD(level=%d)", dimensions
, level
);
1731 if (border
< 0 || border
> 1 ||
1732 ((ctx
->API
!= API_OPENGL
||
1733 target
== GL_TEXTURE_RECTANGLE_NV
||
1734 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1735 _mesa_error(ctx
, GL_INVALID_VALUE
,
1736 "glTexImage%dD(border=%d)", dimensions
, border
);
1740 if (width
< 0 || height
< 0 || depth
< 0) {
1741 _mesa_error(ctx
, GL_INVALID_VALUE
,
1742 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1746 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1747 * combinations of format, internalFormat, and type that can be used.
1748 * Formats and types that require additional extensions (e.g., GL_FLOAT
1749 * requires GL_OES_texture_float) are filtered elsewhere.
1751 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
1752 if (format
!= internalFormat
) {
1753 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1754 "glTexImage%dD(format = %s, internalFormat = %s)",
1756 _mesa_lookup_enum_by_nr(format
),
1757 _mesa_lookup_enum_by_nr(internalFormat
));
1761 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
1762 if (err
!= GL_NO_ERROR
) {
1763 _mesa_error(ctx
, err
,
1764 "glTexImage%dD(format = %s, type = %s)",
1766 _mesa_lookup_enum_by_nr(format
),
1767 _mesa_lookup_enum_by_nr(type
));
1772 /* Do this simple check before calling the TestProxyTexImage() function */
1773 if (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
1774 sizeOK
= (width
== height
);
1778 * Use the proxy texture driver hook to see if the size/level/etc are
1781 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
1782 internalFormat
, format
,
1783 type
, width
, height
,
1787 /* No GL error is recorded, but we need to zero-out the image dims */
1791 _mesa_error(ctx
, GL_INVALID_VALUE
,
1792 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1793 dimensions
, level
, width
, height
, depth
);
1798 /* Check internalFormat */
1799 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1800 _mesa_error(ctx
, GL_INVALID_VALUE
,
1801 "glTexImage%dD(internalFormat=%s)",
1802 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1806 /* Check incoming image format and type */
1807 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1808 if (err
!= GL_NO_ERROR
) {
1809 _mesa_error(ctx
, err
,
1810 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1811 dimensions
, format
, type
);
1815 /* make sure internal format and format basically agree */
1816 colorFormat
= _mesa_is_color_format(format
);
1817 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1818 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1819 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1820 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1821 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1822 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1823 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1824 dimensions
, internalFormat
, format
);
1828 /* additional checks for ycbcr textures */
1829 if (internalFormat
== GL_YCBCR_MESA
) {
1830 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1831 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1832 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1834 _mesa_snprintf(message
, sizeof(message
),
1835 "glTexImage%dD(format/type YCBCR mismatch)",
1837 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1838 return GL_TRUE
; /* error */
1840 if (target
!= GL_TEXTURE_2D
&&
1841 target
!= GL_PROXY_TEXTURE_2D
&&
1842 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1843 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1844 _mesa_error(ctx
, GL_INVALID_ENUM
,
1845 "glTexImage%dD(bad target for YCbCr texture)",
1851 _mesa_snprintf(message
, sizeof(message
),
1852 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1853 dimensions
, border
);
1854 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1859 /* additional checks for depth textures */
1860 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1861 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1862 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1863 if (target
!= GL_TEXTURE_1D
&&
1864 target
!= GL_PROXY_TEXTURE_1D
&&
1865 target
!= GL_TEXTURE_2D
&&
1866 target
!= GL_PROXY_TEXTURE_2D
&&
1867 target
!= GL_TEXTURE_1D_ARRAY
&&
1868 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1869 target
!= GL_TEXTURE_2D_ARRAY
&&
1870 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1871 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1872 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1873 !((_mesa_is_cube_face(target
) || target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1874 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
))) {
1875 _mesa_error(ctx
, GL_INVALID_ENUM
,
1876 "glTexImage%dD(bad target for depth texture)",
1882 /* additional checks for compressed textures */
1883 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1884 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
1885 _mesa_error(ctx
, GL_INVALID_ENUM
,
1886 "glTexImage%dD(target can't be compressed)", dimensions
);
1889 if (compressedteximage_only_format(ctx
, internalFormat
)) {
1890 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1891 "glTexImage%dD(no compression for format)", dimensions
);
1895 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1896 "glTexImage%dD(border!=0)", dimensions
);
1901 /* additional checks for integer textures */
1902 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1903 (_mesa_is_enum_format_integer(format
) !=
1904 _mesa_is_enum_format_integer(internalFormat
))) {
1905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1906 "glTexImage%dD(integer/non-integer format mismatch)",
1911 if (!mutable_tex_object(ctx
, target
)) {
1912 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1913 "glTexImage%dD(immutable texture)", dimensions
);
1917 /* if we get here, the parameters are OK */
1923 * Error checking for glCompressedTexImage[123]D().
1924 * \param reason returns reason for error, if any
1925 * \return error code or GL_NO_ERROR or PROXY_ERROR.
1928 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
1929 GLenum target
, GLint level
,
1930 GLenum internalFormat
, GLsizei width
,
1931 GLsizei height
, GLsizei depth
, GLint border
,
1934 const GLenum proxyTarget
= _mesa_get_proxy_target(target
);
1935 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1937 GLenum choose_format
;
1939 GLenum proxy_format
;
1940 GLenum error
= GL_NO_ERROR
;
1941 char *reason
= ""; /* no error */
1943 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
1945 error
= GL_INVALID_ENUM
;
1949 /* This will detect any invalid internalFormat value */
1950 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
1951 reason
= "internalFormat";
1952 error
= GL_INVALID_ENUM
;
1956 switch (internalFormat
) {
1958 case GL_PALETTE4_RGB8_OES
:
1959 case GL_PALETTE4_RGBA8_OES
:
1960 case GL_PALETTE4_R5_G6_B5_OES
:
1961 case GL_PALETTE4_RGBA4_OES
:
1962 case GL_PALETTE4_RGB5_A1_OES
:
1963 case GL_PALETTE8_RGB8_OES
:
1964 case GL_PALETTE8_RGBA8_OES
:
1965 case GL_PALETTE8_R5_G6_B5_OES
:
1966 case GL_PALETTE8_RGBA4_OES
:
1967 case GL_PALETTE8_RGB5_A1_OES
:
1968 _mesa_cpal_compressed_format_type(internalFormat
, &choose_format
,
1970 proxy_format
= choose_format
;
1972 /* check level (note that level should be zero or less!) */
1973 if (level
> 0 || level
< -maxLevels
) {
1975 error
= GL_INVALID_VALUE
;
1979 if (dimensions
!= 2) {
1980 reason
= "compressed paletted textures must be 2D";
1981 error
= GL_INVALID_OPERATION
;
1985 /* Figure out the expected texture size (in bytes). This will be
1986 * checked against the actual size later.
1988 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
1991 /* This is for the benefit of the TestProxyTexImage below. It expects
1992 * level to be non-negative. OES_compressed_paletted_texture uses a
1993 * weird mechanism where the level specified to glCompressedTexImage2D
1994 * is -(n-1) number of levels in the texture, and the data specifies the
1995 * complete mipmap stack. This is done to ensure the palette is the
1996 * same for all levels.
2003 choose_format
= GL_NONE
;
2004 choose_type
= GL_NONE
;
2005 proxy_format
= internalFormat
;
2008 if (level
< 0 || level
>= maxLevels
) {
2010 error
= GL_INVALID_VALUE
;
2014 /* Figure out the expected texture size (in bytes). This will be
2015 * checked against the actual size later.
2017 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2021 /* This should really never fail */
2022 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2023 reason
= "internalFormat";
2024 error
= GL_INVALID_ENUM
;
2028 /* No compressed formats support borders at this time */
2030 reason
= "border != 0";
2031 error
= GL_INVALID_VALUE
;
2035 /* For cube map, width must equal height */
2036 if (_mesa_is_cube_face(target
) && width
!= height
) {
2037 reason
= "width != height";
2038 error
= GL_INVALID_VALUE
;
2042 /* check image size against compression block size */
2044 gl_format texFormat
=
2045 ctx
->Driver
.ChooseTextureFormat(ctx
, target
, proxy_format
,
2046 choose_format
, choose_type
);
2049 _mesa_get_format_block_size(texFormat
, &bw
, &bh
);
2050 if ((width
> bw
&& width
% bw
> 0) ||
2051 (height
> bh
&& height
% bh
> 0)) {
2053 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
2054 * generated [...] if any parameter combinations are not
2055 * supported by the specific compressed internal format.
2057 reason
= "invalid width or height for compression format";
2058 error
= GL_INVALID_OPERATION
;
2063 /* check image sizes */
2064 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
2065 proxy_format
, choose_format
,
2067 width
, height
, depth
, border
)) {
2068 /* See error comment above */
2069 if (target
== proxyTarget
) {
2072 reason
= "invalid width, height or format";
2073 error
= GL_INVALID_OPERATION
;
2077 /* check image size in bytes */
2078 if (expectedSize
!= imageSize
) {
2079 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2080 * if <imageSize> is not consistent with the format, dimensions, and
2081 * contents of the specified image.
2083 reason
= "imageSize inconsistant with width/height/format";
2084 error
= GL_INVALID_VALUE
;
2088 if (!mutable_tex_object(ctx
, target
)) {
2089 reason
= "immutable texture";
2090 error
= GL_INVALID_OPERATION
;
2097 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)", dimensions
, reason
);
2104 * Test glTexSubImage[123]D() parameters for errors.
2106 * \param ctx GL context.
2107 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2108 * \param target texture target given by the user.
2109 * \param level image level given by the user.
2110 * \param xoffset sub-image x offset given by the user.
2111 * \param yoffset sub-image y offset given by the user.
2112 * \param zoffset sub-image z offset given by the user.
2113 * \param format pixel data format given by the user.
2114 * \param type pixel data type given by the user.
2115 * \param width image width given by the user.
2116 * \param height image height given by the user.
2117 * \param depth image depth given by the user.
2119 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2121 * Verifies each of the parameters against the constants specified in
2122 * __struct gl_contextRec::Const and the supported extensions, and according
2123 * to the OpenGL specification.
2126 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2127 GLenum target
, GLint level
,
2128 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2129 GLint width
, GLint height
, GLint depth
,
2130 GLenum format
, GLenum type
)
2134 /* Basic level check */
2135 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
2136 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
2140 /* Check for negative sizes */
2142 _mesa_error(ctx
, GL_INVALID_VALUE
,
2143 "glTexSubImage%dD(width=%d)", dimensions
, width
);
2146 if (height
< 0 && dimensions
> 1) {
2147 _mesa_error(ctx
, GL_INVALID_VALUE
,
2148 "glTexSubImage%dD(height=%d)", dimensions
, height
);
2151 if (depth
< 0 && dimensions
> 2) {
2152 _mesa_error(ctx
, GL_INVALID_VALUE
,
2153 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
2157 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2158 * combinations of format and type that can be used. Formats and types
2159 * that require additional extensions (e.g., GL_FLOAT requires
2160 * GL_OES_texture_float) are filtered elsewhere.
2162 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2163 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2164 if (err
!= GL_NO_ERROR
) {
2165 _mesa_error(ctx
, err
,
2166 "glTexSubImage%dD(format = %s, type = %s)",
2168 _mesa_lookup_enum_by_nr(format
),
2169 _mesa_lookup_enum_by_nr(type
));
2174 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2175 if (err
!= GL_NO_ERROR
) {
2176 _mesa_error(ctx
, err
,
2177 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
2178 dimensions
, format
, type
);
2187 * Do second part of glTexSubImage which depends on the destination texture.
2188 * \return GL_TRUE if error recorded, GL_FALSE otherwise
2191 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
2192 GLenum target
, GLint level
,
2193 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2194 GLint width
, GLint height
, GLint depth
,
2195 GLenum format
, GLenum type
,
2196 const struct gl_texture_image
*destTex
)
2199 /* undefined image level */
2200 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
2204 if (xoffset
< -((GLint
)destTex
->Border
)) {
2205 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
2209 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
2210 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
2214 if (dimensions
> 1) {
2215 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destTex
->Border
;
2216 if (yoffset
< -yBorder
) {
2217 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
2221 if (yoffset
+ height
> (GLint
) destTex
->Height
+ yBorder
) {
2222 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
2227 if (dimensions
> 2) {
2228 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
) ? 0 : destTex
->Border
;
2229 if (zoffset
< -zBorder
) {
2230 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
2233 if (zoffset
+ depth
> (GLint
) destTex
->Depth
+ zBorder
) {
2234 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
2239 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
2242 if (compressedteximage_only_format(ctx
, destTex
->InternalFormat
)) {
2243 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2244 "glTexSubImage%dD(no compression for format)", dimensions
);
2248 /* do tests which depend on compression block size */
2249 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
2251 /* offset must be multiple of block size */
2252 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
2253 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2254 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
2255 dimensions
, xoffset
, yoffset
);
2258 /* size must be multiple of bw by bh or equal to whole texture size */
2259 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
2260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2261 "glTexSubImage%dD(width = %d)", dimensions
, width
);
2264 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
2265 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2266 "glTexSubImage%dD(height = %d)", dimensions
, height
);
2271 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2272 /* both source and dest must be integer-valued, or neither */
2273 if (_mesa_is_format_integer_color(destTex
->TexFormat
) !=
2274 _mesa_is_enum_format_integer(format
)) {
2275 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2276 "glTexSubImage%dD(integer/non-integer format mismatch)",
2287 * Test glCopyTexImage[12]D() parameters for errors.
2289 * \param ctx GL context.
2290 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2291 * \param target texture target given by the user.
2292 * \param level image level given by the user.
2293 * \param internalFormat internal format given by the user.
2294 * \param width image width given by the user.
2295 * \param height image height given by the user.
2296 * \param border texture border.
2298 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2300 * Verifies each of the parameters against the constants specified in
2301 * __struct gl_contextRec::Const and the supported extensions, and according
2302 * to the OpenGL specification.
2305 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2306 GLenum target
, GLint level
, GLint internalFormat
,
2307 GLint width
, GLint height
, GLint border
)
2309 const GLenum proxyTarget
= _mesa_get_proxy_target(target
);
2310 const GLenum type
= GL_FLOAT
;
2315 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2316 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2317 dimensions
, _mesa_lookup_enum_by_nr(target
));
2321 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
2322 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
2323 _mesa_error(ctx
, GL_INVALID_VALUE
,
2324 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2328 /* Check that the source buffer is complete */
2329 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2330 if (ctx
->ReadBuffer
->_Status
== 0) {
2331 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2333 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2334 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2335 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2339 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2341 "glCopyTexImage%dD(multisample FBO)",
2348 if (border
< 0 || border
> 1 ||
2349 ((ctx
->API
!= API_OPENGL
||
2350 target
== GL_TEXTURE_RECTANGLE_NV
||
2351 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2352 _mesa_error(ctx
, GL_INVALID_VALUE
,
2353 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2357 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2360 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2361 switch (internalFormat
) {
2366 case GL_LUMINANCE_ALPHA
:
2369 _mesa_error(ctx
, GL_INVALID_VALUE
,
2370 "glCopyTexImage%dD(internalFormat)", dimensions
);
2375 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2376 if (baseFormat
< 0) {
2377 _mesa_error(ctx
, GL_INVALID_VALUE
,
2378 "glCopyTexImage%dD(internalFormat)", dimensions
);
2382 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2384 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2388 /* From the EXT_texture_integer spec:
2390 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2391 * if the texture internalformat is an integer format and the read color
2392 * buffer is not an integer format, or if the internalformat is not an
2393 * integer format and the read color buffer is an integer format."
2395 if (_mesa_is_color_format(internalFormat
)) {
2396 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2398 if (_mesa_is_enum_format_integer(rb
->InternalFormat
) !=
2399 _mesa_is_enum_format_integer(internalFormat
)) {
2400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2401 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2406 /* Do size, level checking */
2407 sizeOK
= (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
)
2408 ? (width
== height
) : 1;
2410 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
2411 internalFormat
, baseFormat
,
2412 type
, width
, height
,
2416 if (dimensions
== 1) {
2417 _mesa_error(ctx
, GL_INVALID_VALUE
,
2418 "glCopyTexImage1D(width=%d)", width
);
2421 ASSERT(dimensions
== 2);
2422 _mesa_error(ctx
, GL_INVALID_VALUE
,
2423 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
2428 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2429 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2430 _mesa_error(ctx
, GL_INVALID_ENUM
,
2431 "glCopyTexImage%dD(target)", dimensions
);
2434 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2435 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2436 "glCopyTexImage%dD(no compression for format)", dimensions
);
2440 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2441 "glCopyTexImage%dD(border!=0)", dimensions
);
2446 if (!mutable_tex_object(ctx
, target
)) {
2447 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2448 "glCopyTexImage%dD(immutable texture)", dimensions
);
2452 /* if we get here, the parameters are OK */
2458 * Test glCopyTexSubImage[12]D() parameters for errors.
2459 * Note that this is the first part of error checking.
2460 * See also copytexsubimage_error_check2() below for the second part.
2462 * \param ctx GL context.
2463 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2464 * \param target texture target given by the user.
2465 * \param level image level given by the user.
2467 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2470 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
2471 GLenum target
, GLint level
)
2473 /* Check that the source buffer is complete */
2474 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2475 if (ctx
->ReadBuffer
->_Status
== 0) {
2476 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2478 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2479 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2480 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2484 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2485 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2486 "glCopyTexSubImage%dD(multisample FBO)",
2492 /* check target (proxies not allowed) */
2493 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2494 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2495 dimensions
, _mesa_lookup_enum_by_nr(target
));
2500 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
2501 _mesa_error(ctx
, GL_INVALID_VALUE
,
2502 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2511 * Second part of error checking for glCopyTexSubImage[12]D().
2512 * \param xoffset sub-image x offset given by the user.
2513 * \param yoffset sub-image y offset given by the user.
2514 * \param zoffset sub-image z offset given by the user.
2515 * \param width image width given by the user.
2516 * \param height image height given by the user.
2519 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
2520 GLenum target
, GLint level
,
2521 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2522 GLsizei width
, GLsizei height
,
2523 const struct gl_texture_image
*teximage
)
2525 /* check that dest tex image exists */
2527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2528 "glCopyTexSubImage%dD(undefined texture level: %d)",
2535 _mesa_error(ctx
, GL_INVALID_VALUE
,
2536 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
2539 if (dimensions
> 1 && height
< 0) {
2540 _mesa_error(ctx
, GL_INVALID_VALUE
,
2541 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
2545 /* check x/y offsets */
2546 if (xoffset
< -((GLint
)teximage
->Border
)) {
2547 _mesa_error(ctx
, GL_INVALID_VALUE
,
2548 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
2551 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
2552 _mesa_error(ctx
, GL_INVALID_VALUE
,
2553 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
2556 if (dimensions
> 1) {
2557 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : teximage
->Border
;
2558 if (yoffset
< -yBorder
) {
2559 _mesa_error(ctx
, GL_INVALID_VALUE
,
2560 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2563 /* NOTE: we're adding the border here, not subtracting! */
2564 if (yoffset
+ height
> (GLint
) teximage
->Height
+ yBorder
) {
2565 _mesa_error(ctx
, GL_INVALID_VALUE
,
2566 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2571 /* check z offset */
2572 if (dimensions
> 2) {
2573 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
) ? 0 : teximage
->Border
;
2574 if (zoffset
< -zBorder
) {
2575 _mesa_error(ctx
, GL_INVALID_VALUE
,
2576 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2579 if (zoffset
> (GLint
) teximage
->Depth
+ zBorder
) {
2580 _mesa_error(ctx
, GL_INVALID_VALUE
,
2581 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2586 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2587 if (compressedteximage_only_format(ctx
, teximage
->InternalFormat
)) {
2588 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2589 "glCopyTexSubImage%dD(no compression for format)", dimensions
);
2592 /* offset must be multiple of 4 */
2593 if ((xoffset
& 3) || (yoffset
& 3)) {
2594 _mesa_error(ctx
, GL_INVALID_VALUE
,
2595 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2598 /* size must be multiple of 4 */
2599 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2600 _mesa_error(ctx
, GL_INVALID_VALUE
,
2601 "glCopyTexSubImage%dD(width)", dimensions
);
2604 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2605 _mesa_error(ctx
, GL_INVALID_VALUE
,
2606 "glCopyTexSubImage%dD(height)", dimensions
);
2611 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2612 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2616 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2618 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2619 dimensions
, teximage
->_BaseFormat
);
2623 /* From the EXT_texture_integer spec:
2625 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2626 * if the texture internalformat is an integer format and the read color
2627 * buffer is not an integer format, or if the internalformat is not an
2628 * integer format and the read color buffer is an integer format."
2630 if (_mesa_is_color_format(teximage
->InternalFormat
)) {
2631 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2633 if (_mesa_is_format_integer_color(rb
->Format
) !=
2634 _mesa_is_format_integer_color(teximage
->TexFormat
)) {
2635 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2636 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2641 /* if we get here, the parameters are OK */
2646 /** Callback info for walking over FBO hash table */
2649 struct gl_context
*ctx
;
2650 struct gl_texture_object
*texObj
;
2656 * Check render to texture callback. Called from _mesa_HashWalk().
2659 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2661 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2662 const struct cb_info
*info
= (struct cb_info
*) userData
;
2663 struct gl_context
*ctx
= info
->ctx
;
2664 const struct gl_texture_object
*texObj
= info
->texObj
;
2665 const GLuint level
= info
->level
, face
= info
->face
;
2667 /* If this is a user-created FBO */
2668 if (_mesa_is_user_fbo(fb
)) {
2670 /* check if any of the FBO's attachments point to 'texObj' */
2671 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2672 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2673 if (att
->Type
== GL_TEXTURE
&&
2674 att
->Texture
== texObj
&&
2675 att
->TextureLevel
== level
&&
2676 att
->CubeMapFace
== face
) {
2677 ASSERT(_mesa_get_attachment_teximage(att
));
2678 /* Tell driver about the new renderbuffer texture */
2679 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2680 /* Mark fb status as indeterminate to force re-validation */
2689 * When a texture image is specified we have to check if it's bound to
2690 * any framebuffer objects (render to texture) in order to detect changes
2691 * in size or format since that effects FBO completeness.
2692 * Any FBOs rendering into the texture must be re-validated.
2695 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2696 struct gl_texture_object
*texObj
,
2697 GLuint face
, GLuint level
)
2699 /* Only check this texture if it's been marked as RenderToTexture */
2700 if (texObj
->_RenderToTexture
) {
2701 struct cb_info info
;
2703 info
.texObj
= texObj
;
2706 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2712 * If the texture object's GenerateMipmap flag is set and we've
2713 * changed the texture base level image, regenerate the rest of the
2714 * mipmap levels now.
2717 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2718 struct gl_texture_object
*texObj
, GLint level
)
2720 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2721 if (texObj
->GenerateMipmap
&&
2722 level
== texObj
->BaseLevel
&&
2723 level
< texObj
->MaxLevel
) {
2724 ASSERT(ctx
->Driver
.GenerateMipmap
);
2725 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2730 /** Debug helper: override the user-requested internal format */
2732 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2735 if (internalFormat
== GL_RGBA16F_ARB
||
2736 internalFormat
== GL_RGBA32F_ARB
) {
2737 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2740 else if (internalFormat
== GL_RGB16F_ARB
||
2741 internalFormat
== GL_RGB32F_ARB
) {
2742 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2745 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2746 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2747 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2748 return GL_LUMINANCE_ALPHA
;
2750 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2751 internalFormat
== GL_LUMINANCE32F_ARB
) {
2752 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2753 return GL_LUMINANCE
;
2755 else if (internalFormat
== GL_ALPHA16F_ARB
||
2756 internalFormat
== GL_ALPHA32F_ARB
) {
2757 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2761 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2762 internalFormat = GL_RGBA;
2766 return internalFormat
;
2769 return internalFormat
;
2775 * Choose the actual hardware format for a texture image.
2776 * Try to use the same format as the previous image level when possible.
2777 * Otherwise, ask the driver for the best format.
2778 * It's important to try to choose a consistant format for all levels
2779 * for efficient texture memory layout/allocation. In particular, this
2780 * comes up during automatic mipmap generation.
2783 _mesa_choose_texture_format(struct gl_context
*ctx
,
2784 struct gl_texture_object
*texObj
,
2785 GLenum target
, GLint level
,
2786 GLenum internalFormat
, GLenum format
, GLenum type
)
2790 /* see if we've already chosen a format for the previous level */
2792 struct gl_texture_image
*prevImage
=
2793 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2794 /* See if the prev level is defined and has an internal format which
2795 * matches the new internal format.
2798 prevImage
->Width
> 0 &&
2799 prevImage
->InternalFormat
== internalFormat
) {
2800 /* use the same format */
2801 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2802 return prevImage
->TexFormat
;
2806 /* If the application requested compression to an S3TC format but we don't
2807 * have the DTXn library, force a generic compressed format instead.
2809 if (internalFormat
!= format
) {
2810 const GLenum before
= internalFormat
;
2812 switch (internalFormat
) {
2813 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2814 if (!ctx
->Mesa_DXTn
)
2815 internalFormat
= GL_COMPRESSED_RGB
;
2817 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2818 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2819 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2820 if (!ctx
->Mesa_DXTn
)
2821 internalFormat
= GL_COMPRESSED_RGBA
;
2827 if (before
!= internalFormat
) {
2829 "DXT compression requested (%s), "
2830 "but libtxc_dxtn library not installed. Using %s "
2832 _mesa_lookup_enum_by_nr(before
),
2833 _mesa_lookup_enum_by_nr(internalFormat
));
2837 /* choose format from scratch */
2838 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, texObj
->Target
, internalFormat
,
2840 ASSERT(f
!= MESA_FORMAT_NONE
);
2845 * Adjust pixel unpack params and image dimensions to strip off the
2846 * one-pixel texture border.
2848 * Gallium and intel don't support texture borders. They've seldem been used
2849 * and seldom been implemented correctly anyway.
2851 * \param unpackNew returns the new pixel unpack parameters
2854 strip_texture_border(GLenum target
,
2855 GLint
*width
, GLint
*height
, GLint
*depth
,
2856 const struct gl_pixelstore_attrib
*unpack
,
2857 struct gl_pixelstore_attrib
*unpackNew
)
2863 *unpackNew
= *unpack
;
2865 if (unpackNew
->RowLength
== 0)
2866 unpackNew
->RowLength
= *width
;
2868 if (unpackNew
->ImageHeight
== 0)
2869 unpackNew
->ImageHeight
= *height
;
2871 assert(*width
>= 3);
2872 unpackNew
->SkipPixels
++; /* skip the border */
2873 *width
= *width
- 2; /* reduce the width by two border pixels */
2875 /* The min height of a texture with a border is 3 */
2876 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2877 unpackNew
->SkipRows
++; /* skip the border */
2878 *height
= *height
- 2; /* reduce the height by two border pixels */
2881 if (*depth
>= 3 && target
!= GL_TEXTURE_2D_ARRAY
) {
2882 unpackNew
->SkipImages
++; /* skip the border */
2883 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2889 * Common code to implement all the glTexImage1D/2D/3D functions
2890 * as well as glCompressedTexImage1D/2D/3D.
2891 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2892 * \param format the user's image format (only used if !compressed)
2893 * \param type the user's image type (only used if !compressed)
2894 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2897 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2898 GLenum target
, GLint level
, GLint internalFormat
,
2899 GLsizei width
, GLsizei height
, GLsizei depth
,
2900 GLint border
, GLenum format
, GLenum type
,
2901 GLsizei imageSize
, const GLvoid
*pixels
)
2903 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2905 struct gl_pixelstore_attrib unpack_no_border
;
2906 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2908 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2910 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2913 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2915 _mesa_lookup_enum_by_nr(target
), level
,
2916 _mesa_lookup_enum_by_nr(internalFormat
),
2917 width
, height
, depth
, border
, pixels
);
2920 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2922 _mesa_lookup_enum_by_nr(target
), level
,
2923 _mesa_lookup_enum_by_nr(internalFormat
),
2924 width
, height
, depth
, border
,
2925 _mesa_lookup_enum_by_nr(format
),
2926 _mesa_lookup_enum_by_nr(type
), pixels
);
2929 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2931 /* target error checking */
2932 if (!legal_teximage_target(ctx
, dims
, target
)) {
2933 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2934 func
, dims
, _mesa_lookup_enum_by_nr(target
));
2938 /* general error checking */
2940 error
= compressed_texture_error_check(ctx
, dims
, target
, level
,
2942 width
, height
, depth
,
2946 error
= texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2947 format
, type
, width
, height
, depth
, border
);
2951 /* Here we convert a cpal compressed image into a regular glTexImage2D
2952 * call by decompressing the texture. If we really want to support cpal
2953 * textures in any driver this would have to be changed.
2955 if (compressed
&& !error
&& dims
== 2) {
2956 switch (internalFormat
) {
2957 case GL_PALETTE4_RGB8_OES
:
2958 case GL_PALETTE4_RGBA8_OES
:
2959 case GL_PALETTE4_R5_G6_B5_OES
:
2960 case GL_PALETTE4_RGBA4_OES
:
2961 case GL_PALETTE4_RGB5_A1_OES
:
2962 case GL_PALETTE8_RGB8_OES
:
2963 case GL_PALETTE8_RGBA8_OES
:
2964 case GL_PALETTE8_R5_G6_B5_OES
:
2965 case GL_PALETTE8_RGBA4_OES
:
2966 case GL_PALETTE8_RGB5_A1_OES
:
2967 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2968 width
, height
, imageSize
, pixels
);
2974 if (_mesa_is_proxy_texture(target
)) {
2975 /* Proxy texture: just clear or set state depending on error checking */
2976 struct gl_texture_image
*texImage
=
2977 get_proxy_tex_image(ctx
, target
, level
);
2978 gl_format texFormat
= MESA_FORMAT_NONE
;
2981 /* No parameter errors. Choose a texture format and see if we
2982 * can really allocate the texture.
2984 struct gl_texture_object
*texObj
=
2985 _mesa_get_current_tex_object(ctx
, target
);
2986 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2987 internalFormat
, format
, type
);
2988 if (!legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
2989 error
= PROXY_ERROR
;
2993 if (error
== PROXY_ERROR
) {
2994 /* image too large, etc. Clear all proxy texture image parameters. */
2996 clear_teximage_fields(texImage
);
2998 else if (error
== GL_FALSE
) {
2999 /* no error: store the teximage parameters */
3001 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3002 border
, internalFormat
, texFormat
);
3005 /* other, regular error (was already recorded) */
3009 /* non-proxy target */
3010 const GLuint face
= _mesa_tex_target_to_face(target
);
3011 struct gl_texture_object
*texObj
;
3012 struct gl_texture_image
*texImage
;
3015 return; /* error was recorded */
3018 /* Allow a hardware driver to just strip out the border, to provide
3019 * reliable but slightly incorrect hardware rendering instead of
3020 * rarely-tested software fallback rendering.
3022 if (border
&& ctx
->Const
.StripTextureBorder
) {
3023 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3026 unpack
= &unpack_no_border
;
3029 if (ctx
->NewState
& _NEW_PIXEL
)
3030 _mesa_update_state(ctx
);
3032 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3034 _mesa_lock_texture(ctx
, texObj
);
3036 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3039 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3042 gl_format texFormat
;
3044 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3046 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3047 internalFormat
, format
,
3050 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
3051 _mesa_init_teximage_fields(ctx
, texImage
,
3052 width
, height
, depth
,
3053 border
, internalFormat
, texFormat
);
3055 /* Give the texture to the driver. <pixels> may be null. */
3057 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3061 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3062 type
, pixels
, unpack
);
3065 check_gen_mipmap(ctx
, target
, texObj
, level
);
3067 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3069 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3072 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3076 _mesa_unlock_texture(ctx
, texObj
);
3082 * Called from the API. Note that width includes the border.
3085 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3086 GLsizei width
, GLint border
, GLenum format
,
3087 GLenum type
, const GLvoid
*pixels
)
3089 GET_CURRENT_CONTEXT(ctx
);
3090 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3091 border
, format
, type
, 0, pixels
);
3096 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3097 GLsizei width
, GLsizei height
, GLint border
,
3098 GLenum format
, GLenum type
,
3099 const GLvoid
*pixels
)
3101 GET_CURRENT_CONTEXT(ctx
);
3102 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3103 border
, format
, type
, 0, pixels
);
3108 * Called by the API or display list executor.
3109 * Note that width and height include the border.
3112 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3113 GLsizei width
, GLsizei height
, GLsizei depth
,
3114 GLint border
, GLenum format
, GLenum type
,
3115 const GLvoid
*pixels
)
3117 GET_CURRENT_CONTEXT(ctx
);
3118 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3119 width
, height
, depth
,
3120 border
, format
, type
, 0, pixels
);
3125 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3126 GLsizei width
, GLsizei height
, GLsizei depth
,
3127 GLint border
, GLenum format
, GLenum type
,
3128 const GLvoid
*pixels
)
3130 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3131 depth
, border
, format
, type
, pixels
);
3136 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3138 struct gl_texture_object
*texObj
;
3139 struct gl_texture_image
*texImage
;
3141 GET_CURRENT_CONTEXT(ctx
);
3142 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3146 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3148 case GL_TEXTURE_EXTERNAL_OES
:
3149 valid_target
= ctx
->Extensions
.OES_EGL_image_external
;
3152 valid_target
= false;
3156 if (!valid_target
) {
3157 _mesa_error(ctx
, GL_INVALID_ENUM
,
3158 "glEGLImageTargetTexture2D(target=%d)", target
);
3162 if (ctx
->NewState
& _NEW_PIXEL
)
3163 _mesa_update_state(ctx
);
3165 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3166 _mesa_lock_texture(ctx
, texObj
);
3168 if (texObj
->Immutable
) {
3169 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3170 "glEGLImageTargetTexture2D(texture is immutable)");
3171 _mesa_unlock_texture(ctx
, texObj
);
3175 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3177 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3179 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3181 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3182 texObj
, texImage
, image
);
3184 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3186 _mesa_unlock_texture(ctx
, texObj
);
3193 * Implement all the glTexSubImage1/2/3D() functions.
3196 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3197 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3198 GLsizei width
, GLsizei height
, GLsizei depth
,
3199 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3201 struct gl_texture_object
*texObj
;
3202 struct gl_texture_image
*texImage
;
3204 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3206 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3207 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3209 _mesa_lookup_enum_by_nr(target
), level
,
3210 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3211 _mesa_lookup_enum_by_nr(format
),
3212 _mesa_lookup_enum_by_nr(type
), pixels
);
3214 /* check target (proxies not allowed) */
3215 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
3216 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3217 dims
, _mesa_lookup_enum_by_nr(target
));
3221 if (ctx
->NewState
& _NEW_PIXEL
)
3222 _mesa_update_state(ctx
);
3224 if (subtexture_error_check(ctx
, dims
, target
, level
, xoffset
, yoffset
, zoffset
,
3225 width
, height
, depth
, format
, type
)) {
3226 return; /* error was detected */
3229 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3231 _mesa_lock_texture(ctx
, texObj
);
3233 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3235 if (subtexture_error_check2(ctx
, dims
, target
, level
,
3236 xoffset
, yoffset
, zoffset
,
3237 width
, height
, depth
,
3238 format
, type
, texImage
)) {
3239 /* error was recorded */
3241 else if (width
> 0 && height
> 0 && depth
> 0) {
3242 /* If we have a border, offset=-1 is legal. Bias by border width. */
3245 if (target
!= GL_TEXTURE_2D_ARRAY
)
3246 zoffset
+= texImage
->Border
;
3249 if (target
!= GL_TEXTURE_1D_ARRAY
)
3250 yoffset
+= texImage
->Border
;
3253 xoffset
+= texImage
->Border
;
3256 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3257 xoffset
, yoffset
, zoffset
,
3258 width
, height
, depth
,
3259 format
, type
, pixels
, &ctx
->Unpack
);
3261 check_gen_mipmap(ctx
, target
, texObj
, level
);
3263 ctx
->NewState
|= _NEW_TEXTURE
;
3266 _mesa_unlock_texture(ctx
, texObj
);
3271 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3272 GLint xoffset
, GLsizei width
,
3273 GLenum format
, GLenum type
,
3274 const GLvoid
*pixels
)
3276 GET_CURRENT_CONTEXT(ctx
);
3277 texsubimage(ctx
, 1, target
, level
,
3280 format
, type
, pixels
);
3285 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3286 GLint xoffset
, GLint yoffset
,
3287 GLsizei width
, GLsizei height
,
3288 GLenum format
, GLenum type
,
3289 const GLvoid
*pixels
)
3291 GET_CURRENT_CONTEXT(ctx
);
3292 texsubimage(ctx
, 2, target
, level
,
3293 xoffset
, yoffset
, 0,
3295 format
, type
, pixels
);
3301 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3302 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3303 GLsizei width
, GLsizei height
, GLsizei depth
,
3304 GLenum format
, GLenum type
,
3305 const GLvoid
*pixels
)
3307 GET_CURRENT_CONTEXT(ctx
);
3308 texsubimage(ctx
, 3, target
, level
,
3309 xoffset
, yoffset
, zoffset
,
3310 width
, height
, depth
,
3311 format
, type
, pixels
);
3317 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3318 * from. This depends on whether the texture contains color or depth values.
3320 static struct gl_renderbuffer
*
3321 get_copy_tex_image_source(struct gl_context
*ctx
, gl_format texFormat
)
3323 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3324 /* reading from depth/stencil buffer */
3325 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3328 /* copying from color buffer */
3329 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3336 * Implement the glCopyTexImage1/2D() functions.
3339 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3340 GLenum target
, GLint level
, GLenum internalFormat
,
3341 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3343 struct gl_texture_object
*texObj
;
3344 struct gl_texture_image
*texImage
;
3345 const GLuint face
= _mesa_tex_target_to_face(target
);
3347 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3349 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3350 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3352 _mesa_lookup_enum_by_nr(target
), level
,
3353 _mesa_lookup_enum_by_nr(internalFormat
),
3354 x
, y
, width
, height
, border
);
3356 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3357 _mesa_update_state(ctx
);
3359 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3360 width
, height
, border
))
3363 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3365 if (border
&& ctx
->Const
.StripTextureBorder
) {
3367 width
-= border
* 2;
3370 height
-= border
* 2;
3375 _mesa_lock_texture(ctx
, texObj
);
3377 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3380 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3383 /* choose actual hw format */
3384 gl_format texFormat
= _mesa_choose_texture_format(ctx
, texObj
,
3389 if (legal_texture_size(ctx
, texFormat
, width
, height
, 1)) {
3390 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3392 /* Free old texture image */
3393 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3395 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3396 border
, internalFormat
, texFormat
);
3398 /* Allocate texture memory (no pixel data yet) */
3399 ctx
->Driver
.TexImage(ctx
, dims
, texImage
,
3401 NULL
, &ctx
->Unpack
);
3403 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3405 struct gl_renderbuffer
*srcRb
=
3406 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3408 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
, dstX
, dstY
, dstZ
,
3409 srcRb
, srcX
, srcY
, width
, height
);
3412 check_gen_mipmap(ctx
, target
, texObj
, level
);
3414 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3416 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3419 /* probably too large of image */
3420 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3424 _mesa_unlock_texture(ctx
, texObj
);
3430 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3431 GLenum internalFormat
,
3433 GLsizei width
, GLint border
)
3435 GET_CURRENT_CONTEXT(ctx
);
3436 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3442 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3443 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3446 GET_CURRENT_CONTEXT(ctx
);
3447 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3448 x
, y
, width
, height
, border
);
3454 * Implementation for glCopyTexSubImage1/2/3D() functions.
3457 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3458 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3459 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3461 struct gl_texture_object
*texObj
;
3462 struct gl_texture_image
*texImage
;
3464 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3466 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3467 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3469 _mesa_lookup_enum_by_nr(target
),
3470 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3472 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3473 _mesa_update_state(ctx
);
3475 if (copytexsubimage_error_check1(ctx
, dims
, target
, level
))
3478 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3480 _mesa_lock_texture(ctx
, texObj
);
3482 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3484 if (copytexsubimage_error_check2(ctx
, dims
, target
, level
, xoffset
, yoffset
,
3485 zoffset
, width
, height
, texImage
)) {
3486 /* error was recored */
3489 /* If we have a border, offset=-1 is legal. Bias by border width. */
3492 if (target
!= GL_TEXTURE_2D_ARRAY
)
3493 zoffset
+= texImage
->Border
;
3496 if (target
!= GL_TEXTURE_1D_ARRAY
)
3497 yoffset
+= texImage
->Border
;
3500 xoffset
+= texImage
->Border
;
3503 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3505 struct gl_renderbuffer
*srcRb
=
3506 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3508 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3509 xoffset
, yoffset
, zoffset
,
3510 srcRb
, x
, y
, width
, height
);
3512 check_gen_mipmap(ctx
, target
, texObj
, level
);
3514 ctx
->NewState
|= _NEW_TEXTURE
;
3518 _mesa_unlock_texture(ctx
, texObj
);
3523 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3524 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3526 GET_CURRENT_CONTEXT(ctx
);
3527 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
3533 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3534 GLint xoffset
, GLint yoffset
,
3535 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3537 GET_CURRENT_CONTEXT(ctx
);
3538 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
3545 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3546 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3547 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3549 GET_CURRENT_CONTEXT(ctx
);
3550 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3551 x
, y
, width
, height
);
3557 /**********************************************************************/
3558 /****** Compressed Textures ******/
3559 /**********************************************************************/
3563 * Error checking for glCompressedTexSubImage[123]D().
3564 * \warning There are some bad assumptions here about the size of compressed
3565 * texture tiles (multiple of 4) used to test the validity of the
3566 * offset and size parameters.
3567 * \return error code or GL_NO_ERROR.
3570 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3571 GLenum target
, GLint level
,
3572 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3573 GLsizei width
, GLsizei height
, GLsizei depth
,
3574 GLenum format
, GLsizei imageSize
)
3576 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3580 if (dimensions
== 1) {
3581 /* 1D compressed textures not allowed */
3582 return GL_INVALID_ENUM
;
3584 else if (dimensions
== 2) {
3585 if (target
== GL_PROXY_TEXTURE_2D
) {
3586 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3588 else if (target
== GL_TEXTURE_2D
) {
3589 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3591 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3592 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3593 return GL_INVALID_ENUM
; /*target*/
3594 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3596 else if (_mesa_is_cube_face(target
)) {
3597 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3598 return GL_INVALID_ENUM
; /*target*/
3599 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3602 return GL_INVALID_ENUM
; /*target*/
3605 else if (dimensions
== 3) {
3606 /* 3D compressed textures not allowed */
3607 return GL_INVALID_ENUM
;
3610 maxTextureSize
= 1 << (maxLevels
- 1);
3612 /* this will catch any invalid compressed format token */
3613 if (!_mesa_is_compressed_format(ctx
, format
))
3614 return GL_INVALID_ENUM
;
3616 if (width
< 1 || width
> maxTextureSize
)
3617 return GL_INVALID_VALUE
;
3619 if ((height
< 1 || height
> maxTextureSize
)
3621 return GL_INVALID_VALUE
;
3623 if (level
< 0 || level
>= maxLevels
)
3624 return GL_INVALID_VALUE
;
3627 * do checks which depend on compression block size
3629 get_compressed_block_size(format
, &bw
, &bh
);
3631 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3632 return GL_INVALID_VALUE
;
3634 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3635 return GL_INVALID_VALUE
;
3637 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3638 return GL_INVALID_VALUE
;
3640 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3641 if (expectedSize
!= imageSize
)
3642 return GL_INVALID_VALUE
;
3649 * Do second part of glCompressedTexSubImage error checking.
3650 * \return GL_TRUE if error found, GL_FALSE otherwise.
3653 compressed_subtexture_error_check2(struct gl_context
*ctx
, GLuint dims
,
3654 GLsizei width
, GLsizei height
,
3655 GLsizei depth
, GLenum format
,
3656 struct gl_texture_image
*texImage
)
3659 if ((GLint
) format
!= texImage
->InternalFormat
) {
3660 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3661 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3665 if (compressedteximage_only_format(ctx
, format
)) {
3666 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3667 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3672 if (((width
== 1 || width
== 2) &&
3673 width
!= (GLsizei
) texImage
->Width
) ||
3674 (width
> (GLsizei
) texImage
->Width
)) {
3675 _mesa_error(ctx
, GL_INVALID_VALUE
,
3676 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3681 if (((height
== 1 || height
== 2) &&
3682 height
!= (GLsizei
) texImage
->Height
) ||
3683 (height
> (GLsizei
) texImage
->Height
)) {
3684 _mesa_error(ctx
, GL_INVALID_VALUE
,
3685 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3691 if (((depth
== 1 || depth
== 2) &&
3692 depth
!= (GLsizei
) texImage
->Depth
) ||
3693 (depth
> (GLsizei
) texImage
->Depth
)) {
3694 _mesa_error(ctx
, GL_INVALID_VALUE
,
3695 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3705 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3706 GLenum internalFormat
, GLsizei width
,
3707 GLint border
, GLsizei imageSize
,
3710 GET_CURRENT_CONTEXT(ctx
);
3711 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
3712 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3717 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3718 GLenum internalFormat
, GLsizei width
,
3719 GLsizei height
, GLint border
, GLsizei imageSize
,
3722 GET_CURRENT_CONTEXT(ctx
);
3723 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
3724 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3729 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3730 GLenum internalFormat
, GLsizei width
,
3731 GLsizei height
, GLsizei depth
, GLint border
,
3732 GLsizei imageSize
, const GLvoid
*data
)
3734 GET_CURRENT_CONTEXT(ctx
);
3735 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
3736 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3741 * Common helper for glCompressedTexSubImage1/2/3D().
3744 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3745 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3746 GLsizei width
, GLsizei height
, GLsizei depth
,
3747 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3749 struct gl_texture_object
*texObj
;
3750 struct gl_texture_image
*texImage
;
3752 GET_CURRENT_CONTEXT(ctx
);
3753 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3755 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3756 xoffset
, 0, 0, /* pos */
3757 width
, height
, depth
, /* size */
3760 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3764 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3766 _mesa_lock_texture(ctx
, texObj
);
3768 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3771 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3772 format
, texImage
)) {
3773 /* error was recorded */
3775 else if (width
> 0 && height
> 0 && depth
> 0) {
3776 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
3777 xoffset
, yoffset
, zoffset
,
3778 width
, height
, depth
,
3779 format
, imageSize
, data
);
3781 check_gen_mipmap(ctx
, target
, texObj
, level
);
3783 ctx
->NewState
|= _NEW_TEXTURE
;
3786 _mesa_unlock_texture(ctx
, texObj
);
3791 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3792 GLsizei width
, GLenum format
,
3793 GLsizei imageSize
, const GLvoid
*data
)
3795 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3796 format
, imageSize
, data
);
3801 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3802 GLint yoffset
, GLsizei width
, GLsizei height
,
3803 GLenum format
, GLsizei imageSize
,
3806 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3807 width
, height
, 1, format
, imageSize
, data
);
3812 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3813 GLint yoffset
, GLint zoffset
, GLsizei width
,
3814 GLsizei height
, GLsizei depth
, GLenum format
,
3815 GLsizei imageSize
, const GLvoid
*data
)
3817 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3818 width
, height
, depth
, format
, imageSize
, data
);
3822 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
3824 switch (internalFormat
) {
3826 return MESA_FORMAT_A8
;
3828 return MESA_FORMAT_A16
;
3829 case GL_ALPHA16F_ARB
:
3830 return MESA_FORMAT_ALPHA_FLOAT16
;
3831 case GL_ALPHA32F_ARB
:
3832 return MESA_FORMAT_ALPHA_FLOAT32
;
3833 case GL_ALPHA8I_EXT
:
3834 return MESA_FORMAT_ALPHA_INT8
;
3835 case GL_ALPHA16I_EXT
:
3836 return MESA_FORMAT_ALPHA_INT16
;
3837 case GL_ALPHA32I_EXT
:
3838 return MESA_FORMAT_ALPHA_INT32
;
3839 case GL_ALPHA8UI_EXT
:
3840 return MESA_FORMAT_ALPHA_UINT8
;
3841 case GL_ALPHA16UI_EXT
:
3842 return MESA_FORMAT_ALPHA_UINT16
;
3843 case GL_ALPHA32UI_EXT
:
3844 return MESA_FORMAT_ALPHA_UINT32
;
3846 return MESA_FORMAT_L8
;
3847 case GL_LUMINANCE16
:
3848 return MESA_FORMAT_L16
;
3849 case GL_LUMINANCE16F_ARB
:
3850 return MESA_FORMAT_LUMINANCE_FLOAT16
;
3851 case GL_LUMINANCE32F_ARB
:
3852 return MESA_FORMAT_LUMINANCE_FLOAT32
;
3853 case GL_LUMINANCE8I_EXT
:
3854 return MESA_FORMAT_LUMINANCE_INT8
;
3855 case GL_LUMINANCE16I_EXT
:
3856 return MESA_FORMAT_LUMINANCE_INT16
;
3857 case GL_LUMINANCE32I_EXT
:
3858 return MESA_FORMAT_LUMINANCE_INT32
;
3859 case GL_LUMINANCE8UI_EXT
:
3860 return MESA_FORMAT_LUMINANCE_UINT8
;
3861 case GL_LUMINANCE16UI_EXT
:
3862 return MESA_FORMAT_LUMINANCE_UINT16
;
3863 case GL_LUMINANCE32UI_EXT
:
3864 return MESA_FORMAT_LUMINANCE_UINT32
;
3865 case GL_LUMINANCE8_ALPHA8
:
3866 return MESA_FORMAT_AL88
;
3867 case GL_LUMINANCE16_ALPHA16
:
3868 return MESA_FORMAT_AL1616
;
3869 case GL_LUMINANCE_ALPHA16F_ARB
:
3870 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
;
3871 case GL_LUMINANCE_ALPHA32F_ARB
:
3872 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
;
3873 case GL_LUMINANCE_ALPHA8I_EXT
:
3874 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3875 case GL_LUMINANCE_ALPHA16I_EXT
:
3876 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3877 case GL_LUMINANCE_ALPHA32I_EXT
:
3878 return MESA_FORMAT_LUMINANCE_ALPHA_INT16
;
3879 case GL_LUMINANCE_ALPHA8UI_EXT
:
3880 return MESA_FORMAT_LUMINANCE_ALPHA_UINT8
;
3881 case GL_LUMINANCE_ALPHA16UI_EXT
:
3882 return MESA_FORMAT_LUMINANCE_ALPHA_UINT16
;
3883 case GL_LUMINANCE_ALPHA32UI_EXT
:
3884 return MESA_FORMAT_LUMINANCE_ALPHA_UINT32
;
3886 return MESA_FORMAT_I8
;
3887 case GL_INTENSITY16
:
3888 return MESA_FORMAT_I16
;
3889 case GL_INTENSITY16F_ARB
:
3890 return MESA_FORMAT_INTENSITY_FLOAT16
;
3891 case GL_INTENSITY32F_ARB
:
3892 return MESA_FORMAT_INTENSITY_FLOAT32
;
3893 case GL_INTENSITY8I_EXT
:
3894 return MESA_FORMAT_INTENSITY_INT8
;
3895 case GL_INTENSITY16I_EXT
:
3896 return MESA_FORMAT_INTENSITY_INT16
;
3897 case GL_INTENSITY32I_EXT
:
3898 return MESA_FORMAT_INTENSITY_INT32
;
3899 case GL_INTENSITY8UI_EXT
:
3900 return MESA_FORMAT_INTENSITY_UINT8
;
3901 case GL_INTENSITY16UI_EXT
:
3902 return MESA_FORMAT_INTENSITY_UINT16
;
3903 case GL_INTENSITY32UI_EXT
:
3904 return MESA_FORMAT_INTENSITY_UINT32
;
3906 return MESA_FORMAT_RGBA8888_REV
;
3908 return MESA_FORMAT_RGBA_16
;
3909 case GL_RGBA16F_ARB
:
3910 return MESA_FORMAT_RGBA_FLOAT16
;
3911 case GL_RGBA32F_ARB
:
3912 return MESA_FORMAT_RGBA_FLOAT32
;
3914 return MESA_FORMAT_RGBA_INT8
;
3915 case GL_RGBA16I_EXT
:
3916 return MESA_FORMAT_RGBA_INT16
;
3917 case GL_RGBA32I_EXT
:
3918 return MESA_FORMAT_RGBA_INT32
;
3919 case GL_RGBA8UI_EXT
:
3920 return MESA_FORMAT_RGBA_UINT8
;
3921 case GL_RGBA16UI_EXT
:
3922 return MESA_FORMAT_RGBA_UINT16
;
3923 case GL_RGBA32UI_EXT
:
3924 return MESA_FORMAT_RGBA_UINT32
;
3927 return MESA_FORMAT_GR88
;
3929 return MESA_FORMAT_RG1616
;
3931 return MESA_FORMAT_RG_FLOAT16
;
3933 return MESA_FORMAT_RG_FLOAT32
;
3935 return MESA_FORMAT_RG_INT8
;
3937 return MESA_FORMAT_RG_INT16
;
3939 return MESA_FORMAT_RG_INT32
;
3941 return MESA_FORMAT_RG_UINT8
;
3943 return MESA_FORMAT_RG_UINT16
;
3945 return MESA_FORMAT_RG_UINT32
;
3948 return MESA_FORMAT_R8
;
3950 return MESA_FORMAT_R16
;
3952 return MESA_FORMAT_R_FLOAT16
;
3954 return MESA_FORMAT_R_FLOAT32
;
3956 return MESA_FORMAT_R_INT8
;
3958 return MESA_FORMAT_R_INT16
;
3960 return MESA_FORMAT_R_INT32
;
3962 return MESA_FORMAT_R_UINT8
;
3964 return MESA_FORMAT_R_UINT16
;
3966 return MESA_FORMAT_R_UINT32
;
3969 return MESA_FORMAT_NONE
;
3974 validate_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
3976 gl_format format
= get_texbuffer_format(ctx
, internalFormat
);
3979 if (format
== MESA_FORMAT_NONE
)
3980 return MESA_FORMAT_NONE
;
3982 datatype
= _mesa_get_format_datatype(format
);
3983 if (datatype
== GL_FLOAT
&& !ctx
->Extensions
.ARB_texture_float
)
3984 return MESA_FORMAT_NONE
;
3986 if (datatype
== GL_HALF_FLOAT
&& !ctx
->Extensions
.ARB_half_float_pixel
)
3987 return MESA_FORMAT_NONE
;
3989 /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
3990 * any mention of R/RG formats, but they appear in the GL 3.1 core
3993 if (ctx
->Version
<= 30) {
3994 GLenum base_format
= _mesa_get_format_base_format(format
);
3996 if (base_format
== GL_R
|| base_format
== GL_RG
)
3997 return MESA_FORMAT_NONE
;
4003 /** GL_ARB_texture_buffer_object */
4005 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
4007 struct gl_texture_object
*texObj
;
4008 struct gl_buffer_object
*bufObj
;
4011 GET_CURRENT_CONTEXT(ctx
);
4012 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
4014 if (!(ctx
->Extensions
.ARB_texture_buffer_object
4015 && _mesa_is_desktop_gl(ctx
))) {
4016 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
4020 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4021 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
4025 format
= validate_texbuffer_format(ctx
, internalFormat
);
4026 if (format
== MESA_FORMAT_NONE
) {
4027 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(internalFormat 0x%x)",
4032 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4033 if (buffer
&& !bufObj
) {
4034 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
4038 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4040 _mesa_lock_texture(ctx
, texObj
);
4042 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4043 texObj
->BufferObjectFormat
= internalFormat
;
4044 texObj
->_BufferObjectFormat
= format
;
4046 _mesa_unlock_texture(ctx
, texObj
);