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
) {
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 GL_LUMINANCE_ALPHA
;
137 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
139 if (_mesa_is_gles(ctx
)) {
140 switch (internalFormat
) {
148 if (ctx
->Extensions
.ARB_ES2_compatibility
) {
149 switch (internalFormat
) {
157 if (ctx
->Extensions
.ARB_depth_texture
) {
158 switch (internalFormat
) {
159 case GL_DEPTH_COMPONENT
:
160 case GL_DEPTH_COMPONENT16
:
161 case GL_DEPTH_COMPONENT24
:
162 case GL_DEPTH_COMPONENT32
:
163 return GL_DEPTH_COMPONENT
;
169 switch (internalFormat
) {
170 case GL_COMPRESSED_ALPHA
:
172 case GL_COMPRESSED_LUMINANCE
:
174 case GL_COMPRESSED_LUMINANCE_ALPHA
:
175 return GL_LUMINANCE_ALPHA
;
176 case GL_COMPRESSED_INTENSITY
:
178 case GL_COMPRESSED_RGB
:
180 case GL_COMPRESSED_RGBA
:
186 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
187 switch (internalFormat
) {
188 case GL_COMPRESSED_RGB_FXT1_3DFX
:
190 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
197 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
198 switch (internalFormat
) {
199 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
201 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
202 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
203 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
210 if (ctx
->Extensions
.S3_s3tc
) {
211 switch (internalFormat
) {
223 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
224 if (internalFormat
== GL_YCBCR_MESA
)
225 return GL_YCBCR_MESA
;
228 if (ctx
->Extensions
.ARB_texture_float
) {
229 switch (internalFormat
) {
230 case GL_ALPHA16F_ARB
:
231 case GL_ALPHA32F_ARB
:
239 case GL_INTENSITY16F_ARB
:
240 case GL_INTENSITY32F_ARB
:
242 case GL_LUMINANCE16F_ARB
:
243 case GL_LUMINANCE32F_ARB
:
245 case GL_LUMINANCE_ALPHA16F_ARB
:
246 case GL_LUMINANCE_ALPHA32F_ARB
:
247 return GL_LUMINANCE_ALPHA
;
253 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
254 switch (internalFormat
) {
263 if (ctx
->Extensions
.EXT_texture_snorm
) {
264 switch (internalFormat
) {
279 case GL_RGBA16_SNORM
:
282 case GL_ALPHA8_SNORM
:
283 case GL_ALPHA16_SNORM
:
285 case GL_LUMINANCE_SNORM
:
286 case GL_LUMINANCE8_SNORM
:
287 case GL_LUMINANCE16_SNORM
:
289 case GL_LUMINANCE_ALPHA_SNORM
:
290 case GL_LUMINANCE8_ALPHA8_SNORM
:
291 case GL_LUMINANCE16_ALPHA16_SNORM
:
292 return GL_LUMINANCE_ALPHA
;
293 case GL_INTENSITY_SNORM
:
294 case GL_INTENSITY8_SNORM
:
295 case GL_INTENSITY16_SNORM
:
302 if (ctx
->Extensions
.EXT_packed_depth_stencil
) {
303 switch (internalFormat
) {
304 case GL_DEPTH_STENCIL_EXT
:
305 case GL_DEPTH24_STENCIL8_EXT
:
306 return GL_DEPTH_STENCIL_EXT
;
312 #if FEATURE_EXT_texture_sRGB
313 if (ctx
->Extensions
.EXT_texture_sRGB
) {
314 switch (internalFormat
) {
317 case GL_COMPRESSED_SRGB_EXT
:
318 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
320 case GL_SRGB_ALPHA_EXT
:
321 case GL_SRGB8_ALPHA8_EXT
:
322 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
323 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
324 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
325 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
327 case GL_SLUMINANCE_ALPHA_EXT
:
328 case GL_SLUMINANCE8_ALPHA8_EXT
:
329 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
330 return GL_LUMINANCE_ALPHA
;
331 case GL_SLUMINANCE_EXT
:
332 case GL_SLUMINANCE8_EXT
:
333 case GL_COMPRESSED_SLUMINANCE_EXT
:
339 #endif /* FEATURE_EXT_texture_sRGB */
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 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 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 ctx
->Extensions
.ARB_texture_buffer_object
758 ? texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
759 case GL_TEXTURE_EXTERNAL_OES
:
760 return ctx
->Extensions
.OES_EGL_image_external
761 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
763 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
770 * Return pointer to texture object for given target on current texture unit.
772 struct gl_texture_object
*
773 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
775 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
776 return _mesa_select_tex_object(ctx
, texUnit
, target
);
781 * Get a texture image pointer from a texture object, given a texture
782 * target and mipmap level. The target and level parameters should
783 * have already been error-checked.
785 * \param ctx GL context.
786 * \param texObj texture unit.
787 * \param target texture target.
788 * \param level image level.
790 * \return pointer to the texture image structure, or NULL on failure.
792 struct gl_texture_image
*
793 _mesa_select_tex_image(struct gl_context
*ctx
,
794 const struct gl_texture_object
*texObj
,
795 GLenum target
, GLint level
)
797 const GLuint face
= _mesa_tex_target_to_face(target
);
801 ASSERT(level
< MAX_TEXTURE_LEVELS
);
803 return texObj
->Image
[face
][level
];
808 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
809 * it and install it. Only return NULL if passed a bad parameter or run
812 struct gl_texture_image
*
813 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
814 GLenum target
, GLint level
)
816 struct gl_texture_image
*texImage
;
821 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
823 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
825 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
829 set_tex_image(texObj
, target
, level
, texImage
);
837 * Return pointer to the specified proxy texture image.
838 * Note that proxy textures are per-context, not per-texture unit.
839 * \return pointer to texture image or NULL if invalid target, invalid
840 * level, or out of memory.
842 static struct gl_texture_image
*
843 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
845 struct gl_texture_image
*texImage
;
852 case GL_PROXY_TEXTURE_1D
:
853 if (level
>= ctx
->Const
.MaxTextureLevels
)
855 texIndex
= TEXTURE_1D_INDEX
;
857 case GL_PROXY_TEXTURE_2D
:
858 if (level
>= ctx
->Const
.MaxTextureLevels
)
860 texIndex
= TEXTURE_2D_INDEX
;
862 case GL_PROXY_TEXTURE_3D
:
863 if (level
>= ctx
->Const
.Max3DTextureLevels
)
865 texIndex
= TEXTURE_3D_INDEX
;
867 case GL_PROXY_TEXTURE_CUBE_MAP
:
868 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
870 texIndex
= TEXTURE_CUBE_INDEX
;
872 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
875 texIndex
= TEXTURE_RECT_INDEX
;
877 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
878 if (level
>= ctx
->Const
.MaxTextureLevels
)
880 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
882 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
883 if (level
>= ctx
->Const
.MaxTextureLevels
)
885 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
891 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
893 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
895 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
898 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
899 /* Set the 'back' pointer */
900 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
907 * Get the maximum number of allowed mipmap levels.
909 * \param ctx GL context.
910 * \param target texture target.
912 * \return the maximum number of allowed mipmap levels for the given
913 * texture target, or zero if passed a bad target.
918 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
922 case GL_PROXY_TEXTURE_1D
:
924 case GL_PROXY_TEXTURE_2D
:
925 return ctx
->Const
.MaxTextureLevels
;
927 case GL_PROXY_TEXTURE_3D
:
928 return ctx
->Const
.Max3DTextureLevels
;
929 case GL_TEXTURE_CUBE_MAP
:
930 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
931 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
932 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
933 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
934 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
935 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
936 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
937 return ctx
->Extensions
.ARB_texture_cube_map
938 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
939 case GL_TEXTURE_RECTANGLE_NV
:
940 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
941 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
942 case GL_TEXTURE_1D_ARRAY_EXT
:
943 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
944 case GL_TEXTURE_2D_ARRAY_EXT
:
945 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
946 return (ctx
->Extensions
.MESA_texture_array
||
947 ctx
->Extensions
.EXT_texture_array
)
948 ? ctx
->Const
.MaxTextureLevels
: 0;
949 case GL_TEXTURE_BUFFER
:
950 return _mesa_is_desktop_gl(ctx
) &&
951 (ctx
->Extensions
.ARB_texture_buffer_object
||
952 (ctx
->Version
>= 31)) ? 1 : 0;
953 case GL_TEXTURE_EXTERNAL_OES
:
956 return 0; /* bad target */
962 * Return number of dimensions per mipmap level for the given texture target.
965 _mesa_get_texture_dimensions(GLenum target
)
969 case GL_PROXY_TEXTURE_1D
:
972 case GL_TEXTURE_RECTANGLE
:
973 case GL_TEXTURE_CUBE_MAP
:
974 case GL_PROXY_TEXTURE_2D
:
975 case GL_PROXY_TEXTURE_RECTANGLE
:
976 case GL_PROXY_TEXTURE_CUBE_MAP
:
977 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
978 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
979 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
980 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
981 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
982 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
983 case GL_TEXTURE_1D_ARRAY
:
984 case GL_PROXY_TEXTURE_1D_ARRAY
:
985 case GL_TEXTURE_EXTERNAL_OES
:
988 case GL_PROXY_TEXTURE_3D
:
989 case GL_TEXTURE_2D_ARRAY
:
990 case GL_PROXY_TEXTURE_2D_ARRAY
:
992 case GL_TEXTURE_BUFFER
:
995 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1004 #if 000 /* not used anymore */
1006 * glTexImage[123]D can accept a NULL image pointer. In this case we
1007 * create a texture image with unspecified image contents per the OpenGL
1011 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1013 const GLint components
= _mesa_components_in_format(format
);
1014 const GLint numPixels
= width
* height
* depth
;
1015 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
1019 * Let's see if anyone finds this. If glTexImage2D() is called with
1020 * a NULL image pointer then load the texture image with something
1021 * interesting instead of leaving it indeterminate.
1024 static const char message
[8][32] = {
1025 " X X XXXXX XXX X ",
1026 " XX XX X X X X X ",
1028 " X X XXXX XXX XXXXX ",
1031 " X X XXXXX XXX X X ",
1035 GLubyte
*imgPtr
= data
;
1037 for (h
= 0; h
< depth
; h
++) {
1038 for (i
= 0; i
< height
; i
++) {
1039 GLint srcRow
= 7 - (i
% 8);
1040 for (j
= 0; j
< width
; j
++) {
1041 GLint srcCol
= j
% 32;
1042 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1043 for (k
= 0; k
< components
; k
++) {
1059 * Set the size and format-related fields of a gl_texture_image struct
1060 * to zero. This is used when a proxy texture test fails.
1063 clear_teximage_fields(struct gl_texture_image
*img
)
1066 img
->_BaseFormat
= 0;
1067 img
->InternalFormat
= 0;
1076 img
->HeightLog2
= 0;
1078 img
->TexFormat
= MESA_FORMAT_NONE
;
1083 * Initialize basic fields of the gl_texture_image struct.
1085 * \param ctx GL context.
1086 * \param img texture image structure to be initialized.
1087 * \param width image width.
1088 * \param height image height.
1089 * \param depth image depth.
1090 * \param border image border.
1091 * \param internalFormat internal format.
1092 * \param format the actual hardware format (one of MESA_FORMAT_*)
1094 * Fills in the fields of \p img with the given information.
1095 * Note: width, height and depth include the border.
1098 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1099 struct gl_texture_image
*img
,
1100 GLsizei width
, GLsizei height
, GLsizei depth
,
1101 GLint border
, GLenum internalFormat
,
1107 ASSERT(height
>= 0);
1110 target
= img
->TexObject
->Target
;
1111 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1112 ASSERT(img
->_BaseFormat
> 0);
1113 img
->InternalFormat
= internalFormat
;
1114 img
->Border
= border
;
1116 img
->Height
= height
;
1119 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1120 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1124 case GL_TEXTURE_BUFFER
:
1125 case GL_PROXY_TEXTURE_1D
:
1130 img
->HeightLog2
= 0;
1137 case GL_TEXTURE_1D_ARRAY
:
1138 case GL_PROXY_TEXTURE_1D_ARRAY
:
1139 img
->Height2
= height
; /* no border */
1140 img
->HeightLog2
= 0; /* not used */
1148 case GL_TEXTURE_RECTANGLE
:
1149 case GL_TEXTURE_CUBE_MAP
:
1150 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1151 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1152 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1153 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1154 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1155 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1156 case GL_TEXTURE_EXTERNAL_OES
:
1157 case GL_PROXY_TEXTURE_2D
:
1158 case GL_PROXY_TEXTURE_RECTANGLE
:
1159 case GL_PROXY_TEXTURE_CUBE_MAP
:
1160 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1161 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1168 case GL_TEXTURE_2D_ARRAY
:
1169 case GL_PROXY_TEXTURE_2D_ARRAY
:
1170 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1171 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1172 img
->Depth2
= depth
; /* no border */
1173 img
->DepthLog2
= 0; /* not used */
1176 case GL_PROXY_TEXTURE_3D
:
1177 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1178 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1179 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1180 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1183 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1187 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
1188 img
->TexFormat
= format
;
1193 * Free and clear fields of the gl_texture_image struct.
1195 * \param ctx GL context.
1196 * \param texImage texture image structure to be cleared.
1198 * After the call, \p texImage will have no data associated with it. Its
1199 * fields are cleared so that its parent object will test incomplete.
1202 _mesa_clear_texture_image(struct gl_context
*ctx
,
1203 struct gl_texture_image
*texImage
)
1205 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1206 clear_teximage_fields(texImage
);
1211 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1212 * level, width, height and depth against the ctx->Const limits for textures.
1214 * A hardware driver might override this function if, for example, the
1215 * max 3D texture size is 512x512x64 (i.e. not a cube).
1217 * Note that width, height, depth == 0 is not an error. However, a
1218 * texture with zero width/height/depth will be considered "incomplete"
1219 * and texturing will effectively be disabled.
1221 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1222 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1223 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1224 * \param level as passed to glTexImage
1225 * \param internalFormat as passed to glTexImage
1226 * \param format as passed to glTexImage
1227 * \param type as passed to glTexImage
1228 * \param width as passed to glTexImage
1229 * \param height as passed to glTexImage
1230 * \param depth as passed to glTexImage
1231 * \param border as passed to glTexImage
1232 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1235 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1236 GLint internalFormat
, GLenum format
, GLenum type
,
1237 GLint width
, GLint height
, GLint depth
, GLint border
)
1241 (void) internalFormat
;
1246 case GL_PROXY_TEXTURE_1D
:
1247 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1248 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1250 if (level
>= ctx
->Const
.MaxTextureLevels
)
1252 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1253 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1258 case GL_PROXY_TEXTURE_2D
:
1259 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1260 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1262 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1264 if (level
>= ctx
->Const
.MaxTextureLevels
)
1266 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1267 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1269 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1274 case GL_PROXY_TEXTURE_3D
:
1275 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1276 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1278 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1280 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1282 if (level
>= ctx
->Const
.Max3DTextureLevels
)
1284 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1285 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1287 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1289 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1294 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1295 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1296 if (width
< 0 || width
> maxSize
)
1298 if (height
< 0 || height
> maxSize
)
1304 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1305 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1306 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1308 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1310 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1312 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1313 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1315 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1320 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1321 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1322 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1324 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1326 if (level
>= ctx
->Const
.MaxTextureLevels
)
1328 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1329 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1334 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1335 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1336 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1338 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1340 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1342 if (level
>= ctx
->Const
.MaxTextureLevels
)
1344 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1345 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1347 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1353 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
1360 * Check if the memory used by the texture would exceed the driver's limit.
1361 * This lets us support a max 3D texture size of 8K (for example) but
1362 * prevents allocating a full 8K x 8K x 8K texture.
1363 * XXX this could be rolled into the proxy texture size test (above) but
1364 * we don't have the actual texture internal format at that point.
1367 legal_texture_size(struct gl_context
*ctx
, gl_format format
,
1368 GLint width
, GLint height
, GLint depth
)
1370 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1371 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1372 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1377 * Return true if the format is only valid for glCompressedTexImage.
1380 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1383 case GL_ETC1_RGB8_OES
:
1384 case GL_PALETTE4_RGB8_OES
:
1385 case GL_PALETTE4_RGBA8_OES
:
1386 case GL_PALETTE4_R5_G6_B5_OES
:
1387 case GL_PALETTE4_RGBA4_OES
:
1388 case GL_PALETTE4_RGB5_A1_OES
:
1389 case GL_PALETTE8_RGB8_OES
:
1390 case GL_PALETTE8_RGBA8_OES
:
1391 case GL_PALETTE8_R5_G6_B5_OES
:
1392 case GL_PALETTE8_RGBA4_OES
:
1393 case GL_PALETTE8_RGB5_A1_OES
:
1402 * Helper function to determine whether a target and specific compression
1403 * format are supported.
1406 target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1409 (void) intFormat
; /* not used yet */
1413 case GL_PROXY_TEXTURE_2D
:
1414 return GL_TRUE
; /* true for any compressed format so far */
1415 case GL_PROXY_TEXTURE_CUBE_MAP
:
1416 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1417 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1418 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1419 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1420 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1421 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1422 return ctx
->Extensions
.ARB_texture_cube_map
;
1423 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1424 case GL_TEXTURE_2D_ARRAY_EXT
:
1425 return (ctx
->Extensions
.MESA_texture_array
||
1426 ctx
->Extensions
.EXT_texture_array
);
1434 * Check if the given texture target value is legal for a
1435 * glTexImage1/2/3D call.
1438 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1444 case GL_PROXY_TEXTURE_1D
:
1445 return _mesa_is_desktop_gl(ctx
);
1453 case GL_PROXY_TEXTURE_2D
:
1454 return _mesa_is_desktop_gl(ctx
);
1455 case GL_PROXY_TEXTURE_CUBE_MAP
:
1456 return _mesa_is_desktop_gl(ctx
)
1457 && ctx
->Extensions
.ARB_texture_cube_map
;
1458 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1459 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1460 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1461 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1462 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1463 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1464 return ctx
->Extensions
.ARB_texture_cube_map
;
1465 case GL_TEXTURE_RECTANGLE_NV
:
1466 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1467 return _mesa_is_desktop_gl(ctx
)
1468 && ctx
->Extensions
.NV_texture_rectangle
;
1469 case GL_TEXTURE_1D_ARRAY_EXT
:
1470 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1471 return _mesa_is_desktop_gl(ctx
)
1472 && (ctx
->Extensions
.MESA_texture_array
||
1473 ctx
->Extensions
.EXT_texture_array
);
1481 case GL_PROXY_TEXTURE_3D
:
1482 return _mesa_is_desktop_gl(ctx
);
1483 case GL_TEXTURE_2D_ARRAY_EXT
:
1484 return (_mesa_is_desktop_gl(ctx
)
1485 && (ctx
->Extensions
.MESA_texture_array
||
1486 ctx
->Extensions
.EXT_texture_array
))
1487 || _mesa_is_gles3(ctx
);
1488 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1489 return _mesa_is_desktop_gl(ctx
)
1490 && (ctx
->Extensions
.MESA_texture_array
||
1491 ctx
->Extensions
.EXT_texture_array
);
1496 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1503 * Check if the given texture target value is legal for a
1504 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1505 * The difference compared to legal_teximage_target() above is that
1506 * proxy targets are not supported.
1509 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1513 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1518 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1519 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1520 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1521 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1522 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1523 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1524 return ctx
->Extensions
.ARB_texture_cube_map
;
1525 case GL_TEXTURE_RECTANGLE_NV
:
1526 return _mesa_is_desktop_gl(ctx
)
1527 && ctx
->Extensions
.NV_texture_rectangle
;
1528 case GL_TEXTURE_1D_ARRAY_EXT
:
1529 return _mesa_is_desktop_gl(ctx
)
1530 && (ctx
->Extensions
.MESA_texture_array
||
1531 ctx
->Extensions
.EXT_texture_array
);
1539 case GL_TEXTURE_2D_ARRAY_EXT
:
1540 return (_mesa_is_desktop_gl(ctx
)
1541 && (ctx
->Extensions
.MESA_texture_array
||
1542 ctx
->Extensions
.EXT_texture_array
))
1543 || _mesa_is_gles3(ctx
);
1548 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1556 * Helper function to determine if a texture object is mutable (in terms
1557 * of GL_ARB_texture_storage).
1560 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1562 if (ctx
->Extensions
.ARB_texture_storage
) {
1563 struct gl_texture_object
*texObj
=
1564 _mesa_get_current_tex_object(ctx
, target
);
1565 return !texObj
->Immutable
;
1572 _mesa_es_error_check_format_and_type(GLenum format
, GLenum type
,
1573 unsigned dimensions
)
1575 bool type_valid
= true;
1580 case GL_LUMINANCE_ALPHA
:
1581 type_valid
= (type
== GL_UNSIGNED_BYTE
1583 || type
== GL_HALF_FLOAT_OES
);
1587 type_valid
= (type
== GL_UNSIGNED_BYTE
1588 || type
== GL_UNSIGNED_SHORT_5_6_5
1590 || type
== GL_HALF_FLOAT_OES
);
1594 type_valid
= (type
== GL_UNSIGNED_BYTE
1595 || type
== GL_UNSIGNED_SHORT_4_4_4_4
1596 || type
== GL_UNSIGNED_SHORT_5_5_5_1
1598 || type
== GL_HALF_FLOAT_OES
1599 || type
== GL_UNSIGNED_INT_2_10_10_10_REV
);
1602 case GL_DEPTH_COMPONENT
:
1603 /* This format is filtered against invalid dimensionalities elsewhere.
1605 type_valid
= (type
== GL_UNSIGNED_SHORT
1606 || type
== GL_UNSIGNED_INT
);
1609 case GL_DEPTH_STENCIL
:
1610 /* This format is filtered against invalid dimensionalities elsewhere.
1612 type_valid
= (type
== GL_UNSIGNED_INT_24_8
);
1616 type_valid
= (type
== GL_UNSIGNED_BYTE
);
1618 /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1619 * the format does not appear to be allowed for 3D textures in OpenGL
1622 if (dimensions
!= 2)
1623 return GL_INVALID_VALUE
;
1628 return GL_INVALID_VALUE
;
1631 return type_valid
? GL_NO_ERROR
: GL_INVALID_OPERATION
;
1637 * Special value returned by error some texture error checking functions when
1638 * an error is detected and the proxy texture image's width/height/depth/format
1639 * fields should be zeroed-out.
1641 #define PROXY_ERROR 2
1645 * Test the glTexImage[123]D() parameters for errors.
1647 * \param ctx GL context.
1648 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1649 * \param target texture target given by the user.
1650 * \param level image level given by the user.
1651 * \param internalFormat internal format given by the user.
1652 * \param format pixel data format given by the user.
1653 * \param type pixel data type given by the user.
1654 * \param width image width given by the user.
1655 * \param height image height given by the user.
1656 * \param depth image depth given by the user.
1657 * \param border image border given by the user.
1659 * \return PROXY_ERROR if there's an error that should zero-out the proxy image,
1660 * GL_TRUE if a regular GL error is found, or GL_FALSE if no error,
1662 * Verifies each of the parameters against the constants specified in
1663 * __struct gl_contextRec::Const and the supported extensions, and according
1664 * to the OpenGL specification.
1667 texture_error_check( struct gl_context
*ctx
,
1668 GLuint dimensions
, GLenum target
,
1669 GLint level
, GLint internalFormat
,
1670 GLenum format
, GLenum type
,
1671 GLint width
, GLint height
,
1672 GLint depth
, GLint border
)
1674 const GLenum proxyTarget
= get_proxy_target(target
);
1675 const GLboolean isProxy
= target
== proxyTarget
;
1676 GLboolean sizeOK
= GL_TRUE
;
1677 GLboolean colorFormat
;
1680 /* Even though there are no color-index textures, we still have to support
1681 * uploading color-index data and remapping it to RGB via the
1682 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1684 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1686 /* Note: for proxy textures, some error conditions immediately generate
1687 * a GL error in the usual way. But others do not generate a GL error.
1688 * Instead, they cause the width, height, depth, format fields of the
1689 * texture image to be zeroed-out. The GL spec seems to indicate that the
1690 * zero-out behaviour is only used in cases related to memory allocation.
1693 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1694 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1695 _mesa_error(ctx
, GL_INVALID_VALUE
,
1696 "glTexImage%dD(level=%d)", dimensions
, level
);
1701 if (border
< 0 || border
> 1 ||
1702 ((ctx
->API
!= API_OPENGL
||
1703 target
== GL_TEXTURE_RECTANGLE_NV
||
1704 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1705 _mesa_error(ctx
, GL_INVALID_VALUE
,
1706 "glTexImage%dD(border=%d)", dimensions
, border
);
1710 if (width
< 0 || height
< 0 || depth
< 0) {
1711 _mesa_error(ctx
, GL_INVALID_VALUE
,
1712 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1716 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1717 * combinations of format, internalFormat, and type that can be used.
1718 * Formats and types that require additional extensions (e.g., GL_FLOAT
1719 * requires GL_OES_texture_float) are filtered elsewhere.
1721 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
1722 if (format
!= internalFormat
) {
1723 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1724 "glTexImage%dD(format = %s, internalFormat = %s)",
1726 _mesa_lookup_enum_by_nr(format
),
1727 _mesa_lookup_enum_by_nr(internalFormat
));
1731 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
1732 if (err
!= GL_NO_ERROR
) {
1733 _mesa_error(ctx
, err
,
1734 "glTexImage%dD(format = %s, type = %s)",
1736 _mesa_lookup_enum_by_nr(format
),
1737 _mesa_lookup_enum_by_nr(type
));
1742 /* Do this simple check before calling the TestProxyTexImage() function */
1743 if (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
1744 sizeOK
= (width
== height
);
1748 * Use the proxy texture driver hook to see if the size/level/etc are
1751 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
1752 internalFormat
, format
,
1753 type
, width
, height
,
1757 /* No GL error is recorded, but we need to zero-out the image dims */
1761 _mesa_error(ctx
, GL_INVALID_VALUE
,
1762 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1763 dimensions
, level
, width
, height
, depth
);
1768 /* Check internalFormat */
1769 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1770 _mesa_error(ctx
, GL_INVALID_VALUE
,
1771 "glTexImage%dD(internalFormat=%s)",
1772 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1776 /* Check incoming image format and type */
1777 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1778 if (err
!= GL_NO_ERROR
) {
1779 _mesa_error(ctx
, err
,
1780 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1781 dimensions
, format
, type
);
1785 /* make sure internal format and format basically agree */
1786 colorFormat
= _mesa_is_color_format(format
);
1787 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1788 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1789 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1790 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1791 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1792 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1793 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1794 dimensions
, internalFormat
, format
);
1798 /* additional checks for ycbcr textures */
1799 if (internalFormat
== GL_YCBCR_MESA
) {
1800 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1801 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1802 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1804 _mesa_snprintf(message
, sizeof(message
),
1805 "glTexImage%dD(format/type YCBCR mismatch)",
1807 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1808 return GL_TRUE
; /* error */
1810 if (target
!= GL_TEXTURE_2D
&&
1811 target
!= GL_PROXY_TEXTURE_2D
&&
1812 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1813 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1814 _mesa_error(ctx
, GL_INVALID_ENUM
,
1815 "glTexImage%dD(bad target for YCbCr texture)",
1821 _mesa_snprintf(message
, sizeof(message
),
1822 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1823 dimensions
, border
);
1824 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1829 /* additional checks for depth textures */
1830 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1831 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1832 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1833 if (target
!= GL_TEXTURE_1D
&&
1834 target
!= GL_PROXY_TEXTURE_1D
&&
1835 target
!= GL_TEXTURE_2D
&&
1836 target
!= GL_PROXY_TEXTURE_2D
&&
1837 target
!= GL_TEXTURE_1D_ARRAY
&&
1838 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1839 target
!= GL_TEXTURE_2D_ARRAY
&&
1840 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1841 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1842 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1843 !((_mesa_is_cube_face(target
) || target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1844 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
))) {
1845 _mesa_error(ctx
, GL_INVALID_ENUM
,
1846 "glTexImage%dD(bad target for depth texture)",
1852 /* additional checks for compressed textures */
1853 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1854 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
1855 _mesa_error(ctx
, GL_INVALID_ENUM
,
1856 "glTexImage%dD(target can't be compressed)", dimensions
);
1859 if (compressedteximage_only_format(ctx
, internalFormat
)) {
1860 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1861 "glTexImage%dD(no compression for format)", dimensions
);
1865 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1866 "glTexImage%dD(border!=0)", dimensions
);
1871 /* additional checks for integer textures */
1872 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1873 (_mesa_is_enum_format_integer(format
) !=
1874 _mesa_is_enum_format_integer(internalFormat
))) {
1875 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1876 "glTexImage%dD(integer/non-integer format mismatch)",
1881 if (!mutable_tex_object(ctx
, target
)) {
1882 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1883 "glTexImage%dD(immutable texture)", dimensions
);
1887 /* if we get here, the parameters are OK */
1893 * Test glTexSubImage[123]D() parameters for errors.
1895 * \param ctx GL context.
1896 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1897 * \param target texture target given by the user.
1898 * \param level image level given by the user.
1899 * \param xoffset sub-image x offset given by the user.
1900 * \param yoffset sub-image y offset given by the user.
1901 * \param zoffset sub-image z offset given by the user.
1902 * \param format pixel data format given by the user.
1903 * \param type pixel data type given by the user.
1904 * \param width image width given by the user.
1905 * \param height image height given by the user.
1906 * \param depth image depth given by the user.
1908 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1910 * Verifies each of the parameters against the constants specified in
1911 * __struct gl_contextRec::Const and the supported extensions, and according
1912 * to the OpenGL specification.
1915 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1916 GLenum target
, GLint level
,
1917 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1918 GLint width
, GLint height
, GLint depth
,
1919 GLenum format
, GLenum type
)
1923 /* Basic level check */
1924 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1925 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1929 /* Check for negative sizes */
1931 _mesa_error(ctx
, GL_INVALID_VALUE
,
1932 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1935 if (height
< 0 && dimensions
> 1) {
1936 _mesa_error(ctx
, GL_INVALID_VALUE
,
1937 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1940 if (depth
< 0 && dimensions
> 2) {
1941 _mesa_error(ctx
, GL_INVALID_VALUE
,
1942 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1946 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1947 * combinations of format and type that can be used. Formats and types
1948 * that require additional extensions (e.g., GL_FLOAT requires
1949 * GL_OES_texture_float) are filtered elsewhere.
1951 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
1952 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
1953 if (err
!= GL_NO_ERROR
) {
1954 _mesa_error(ctx
, err
,
1955 "glTexSubImage%dD(format = %s, type = %s)",
1957 _mesa_lookup_enum_by_nr(format
),
1958 _mesa_lookup_enum_by_nr(type
));
1963 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1964 if (err
!= GL_NO_ERROR
) {
1965 _mesa_error(ctx
, err
,
1966 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1967 dimensions
, format
, type
);
1976 * Do second part of glTexSubImage which depends on the destination texture.
1977 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1980 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1981 GLenum target
, GLint level
,
1982 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1983 GLint width
, GLint height
, GLint depth
,
1984 GLenum format
, GLenum type
,
1985 const struct gl_texture_image
*destTex
)
1988 /* undefined image level */
1989 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1993 if (xoffset
< -((GLint
)destTex
->Border
)) {
1994 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1998 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1999 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
2003 if (dimensions
> 1) {
2004 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destTex
->Border
;
2005 if (yoffset
< -yBorder
) {
2006 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
2010 if (yoffset
+ height
> (GLint
) destTex
->Height
+ yBorder
) {
2011 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
2016 if (dimensions
> 2) {
2017 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
) ? 0 : destTex
->Border
;
2018 if (zoffset
< -zBorder
) {
2019 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset)");
2022 if (zoffset
+ depth
> (GLint
) destTex
->Depth
+ zBorder
) {
2023 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage3D(zoffset+depth)");
2028 if (_mesa_is_format_compressed(destTex
->TexFormat
)) {
2031 if (compressedteximage_only_format(ctx
, destTex
->InternalFormat
)) {
2032 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2033 "glTexSubImage%dD(no compression for format)", dimensions
);
2037 /* do tests which depend on compression block size */
2038 _mesa_get_format_block_size(destTex
->TexFormat
, &bw
, &bh
);
2040 /* offset must be multiple of block size */
2041 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
2042 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2043 "glTexSubImage%dD(xoffset = %d, yoffset = %d)",
2044 dimensions
, xoffset
, yoffset
);
2047 /* size must be multiple of bw by bh or equal to whole texture size */
2048 if ((width
% bw
!= 0) && (GLuint
) width
!= destTex
->Width
) {
2049 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2050 "glTexSubImage%dD(width = %d)", dimensions
, width
);
2053 if ((height
% bh
!= 0) && (GLuint
) height
!= destTex
->Height
) {
2054 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2055 "glTexSubImage%dD(height = %d)", dimensions
, height
);
2060 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2061 /* both source and dest must be integer-valued, or neither */
2062 if (_mesa_is_format_integer_color(destTex
->TexFormat
) !=
2063 _mesa_is_enum_format_integer(format
)) {
2064 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2065 "glTexSubImage%dD(integer/non-integer format mismatch)",
2076 * Test glCopyTexImage[12]D() parameters for errors.
2078 * \param ctx GL context.
2079 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2080 * \param target texture target given by the user.
2081 * \param level image level given by the user.
2082 * \param internalFormat internal format given by the user.
2083 * \param width image width given by the user.
2084 * \param height image height given by the user.
2085 * \param border texture border.
2087 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2089 * Verifies each of the parameters against the constants specified in
2090 * __struct gl_contextRec::Const and the supported extensions, and according
2091 * to the OpenGL specification.
2094 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2095 GLenum target
, GLint level
, GLint internalFormat
,
2096 GLint width
, GLint height
, GLint border
)
2098 const GLenum proxyTarget
= get_proxy_target(target
);
2099 const GLenum type
= GL_FLOAT
;
2104 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2105 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2106 dimensions
, _mesa_lookup_enum_by_nr(target
));
2110 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
2111 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
2112 _mesa_error(ctx
, GL_INVALID_VALUE
,
2113 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2117 /* Check that the source buffer is complete */
2118 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2119 if (ctx
->ReadBuffer
->_Status
== 0) {
2120 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2122 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2123 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2124 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2128 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2129 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2130 "glCopyTexImage%dD(multisample FBO)",
2137 if (border
< 0 || border
> 1 ||
2138 ((ctx
->API
!= API_OPENGL
||
2139 target
== GL_TEXTURE_RECTANGLE_NV
||
2140 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2141 _mesa_error(ctx
, GL_INVALID_VALUE
,
2142 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2146 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2149 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2150 switch (internalFormat
) {
2155 case GL_LUMINANCE_ALPHA
:
2158 _mesa_error(ctx
, GL_INVALID_VALUE
,
2159 "glCopyTexImage%dD(internalFormat)", dimensions
);
2164 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2165 if (baseFormat
< 0) {
2166 _mesa_error(ctx
, GL_INVALID_VALUE
,
2167 "glCopyTexImage%dD(internalFormat)", dimensions
);
2171 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2172 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2173 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2177 /* From the EXT_texture_integer spec:
2179 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2180 * if the texture internalformat is an integer format and the read color
2181 * buffer is not an integer format, or if the internalformat is not an
2182 * integer format and the read color buffer is an integer format."
2184 if (_mesa_is_color_format(internalFormat
)) {
2185 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2187 if (_mesa_is_enum_format_integer(rb
->InternalFormat
) !=
2188 _mesa_is_enum_format_integer(internalFormat
)) {
2189 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2190 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2195 /* Do size, level checking */
2196 sizeOK
= (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
)
2197 ? (width
== height
) : 1;
2199 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
2200 internalFormat
, baseFormat
,
2201 type
, width
, height
,
2205 if (dimensions
== 1) {
2206 _mesa_error(ctx
, GL_INVALID_VALUE
,
2207 "glCopyTexImage1D(width=%d)", width
);
2210 ASSERT(dimensions
== 2);
2211 _mesa_error(ctx
, GL_INVALID_VALUE
,
2212 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
2217 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2218 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2219 _mesa_error(ctx
, GL_INVALID_ENUM
,
2220 "glCopyTexImage%dD(target)", dimensions
);
2223 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2224 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2225 "glCopyTexImage%dD(no compression for format)", dimensions
);
2229 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2230 "glCopyTexImage%dD(border!=0)", dimensions
);
2235 if (!mutable_tex_object(ctx
, target
)) {
2236 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2237 "glCopyTexImage%dD(immutable texture)", dimensions
);
2241 /* if we get here, the parameters are OK */
2247 * Test glCopyTexSubImage[12]D() parameters for errors.
2248 * Note that this is the first part of error checking.
2249 * See also copytexsubimage_error_check2() below for the second part.
2251 * \param ctx GL context.
2252 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2253 * \param target texture target given by the user.
2254 * \param level image level given by the user.
2256 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2259 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
2260 GLenum target
, GLint level
)
2262 /* Check that the source buffer is complete */
2263 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2264 if (ctx
->ReadBuffer
->_Status
== 0) {
2265 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2267 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2268 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2269 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2273 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2275 "glCopyTexSubImage%dD(multisample FBO)",
2281 /* check target (proxies not allowed) */
2282 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2283 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2284 dimensions
, _mesa_lookup_enum_by_nr(target
));
2289 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
2290 _mesa_error(ctx
, GL_INVALID_VALUE
,
2291 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2300 * Second part of error checking for glCopyTexSubImage[12]D().
2301 * \param xoffset sub-image x offset given by the user.
2302 * \param yoffset sub-image y offset given by the user.
2303 * \param zoffset sub-image z offset given by the user.
2304 * \param width image width given by the user.
2305 * \param height image height given by the user.
2308 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
2309 GLenum target
, GLint level
,
2310 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2311 GLsizei width
, GLsizei height
,
2312 const struct gl_texture_image
*teximage
)
2314 /* check that dest tex image exists */
2316 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2317 "glCopyTexSubImage%dD(undefined texture level: %d)",
2324 _mesa_error(ctx
, GL_INVALID_VALUE
,
2325 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
2328 if (dimensions
> 1 && height
< 0) {
2329 _mesa_error(ctx
, GL_INVALID_VALUE
,
2330 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
2334 /* check x/y offsets */
2335 if (xoffset
< -((GLint
)teximage
->Border
)) {
2336 _mesa_error(ctx
, GL_INVALID_VALUE
,
2337 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
2340 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
2341 _mesa_error(ctx
, GL_INVALID_VALUE
,
2342 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
2345 if (dimensions
> 1) {
2346 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : teximage
->Border
;
2347 if (yoffset
< -yBorder
) {
2348 _mesa_error(ctx
, GL_INVALID_VALUE
,
2349 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
2352 /* NOTE: we're adding the border here, not subtracting! */
2353 if (yoffset
+ height
> (GLint
) teximage
->Height
+ yBorder
) {
2354 _mesa_error(ctx
, GL_INVALID_VALUE
,
2355 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
2360 /* check z offset */
2361 if (dimensions
> 2) {
2362 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
) ? 0 : teximage
->Border
;
2363 if (zoffset
< -zBorder
) {
2364 _mesa_error(ctx
, GL_INVALID_VALUE
,
2365 "glCopyTexSubImage%dD(zoffset)", dimensions
);
2368 if (zoffset
> (GLint
) teximage
->Depth
+ zBorder
) {
2369 _mesa_error(ctx
, GL_INVALID_VALUE
,
2370 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
2375 if (_mesa_is_format_compressed(teximage
->TexFormat
)) {
2376 if (compressedteximage_only_format(ctx
, teximage
->InternalFormat
)) {
2377 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2378 "glCopyTexSubImage%dD(no compression for format)", dimensions
);
2381 /* offset must be multiple of 4 */
2382 if ((xoffset
& 3) || (yoffset
& 3)) {
2383 _mesa_error(ctx
, GL_INVALID_VALUE
,
2384 "glCopyTexSubImage%dD(xoffset or yoffset)", dimensions
);
2387 /* size must be multiple of 4 */
2388 if ((width
& 3) != 0 && (GLuint
) width
!= teximage
->Width
) {
2389 _mesa_error(ctx
, GL_INVALID_VALUE
,
2390 "glCopyTexSubImage%dD(width)", dimensions
);
2393 if ((height
& 3) != 0 && (GLuint
) height
!= teximage
->Height
) {
2394 _mesa_error(ctx
, GL_INVALID_VALUE
,
2395 "glCopyTexSubImage%dD(height)", dimensions
);
2400 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
2401 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2405 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
2406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2407 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2408 dimensions
, teximage
->_BaseFormat
);
2412 /* From the EXT_texture_integer spec:
2414 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2415 * if the texture internalformat is an integer format and the read color
2416 * buffer is not an integer format, or if the internalformat is not an
2417 * integer format and the read color buffer is an integer format."
2419 if (_mesa_is_color_format(teximage
->InternalFormat
)) {
2420 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2422 if (_mesa_is_format_integer_color(rb
->Format
) !=
2423 _mesa_is_format_integer_color(teximage
->TexFormat
)) {
2424 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2425 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2430 /* if we get here, the parameters are OK */
2435 /** Callback info for walking over FBO hash table */
2438 struct gl_context
*ctx
;
2439 struct gl_texture_object
*texObj
;
2445 * Check render to texture callback. Called from _mesa_HashWalk().
2448 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2450 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2451 const struct cb_info
*info
= (struct cb_info
*) userData
;
2452 struct gl_context
*ctx
= info
->ctx
;
2453 const struct gl_texture_object
*texObj
= info
->texObj
;
2454 const GLuint level
= info
->level
, face
= info
->face
;
2456 /* If this is a user-created FBO */
2457 if (_mesa_is_user_fbo(fb
)) {
2459 /* check if any of the FBO's attachments point to 'texObj' */
2460 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2461 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2462 if (att
->Type
== GL_TEXTURE
&&
2463 att
->Texture
== texObj
&&
2464 att
->TextureLevel
== level
&&
2465 att
->CubeMapFace
== face
) {
2466 ASSERT(_mesa_get_attachment_teximage(att
));
2467 /* Tell driver about the new renderbuffer texture */
2468 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2469 /* Mark fb status as indeterminate to force re-validation */
2478 * When a texture image is specified we have to check if it's bound to
2479 * any framebuffer objects (render to texture) in order to detect changes
2480 * in size or format since that effects FBO completeness.
2481 * Any FBOs rendering into the texture must be re-validated.
2484 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2485 struct gl_texture_object
*texObj
,
2486 GLuint face
, GLuint level
)
2488 /* Only check this texture if it's been marked as RenderToTexture */
2489 if (texObj
->_RenderToTexture
) {
2490 struct cb_info info
;
2492 info
.texObj
= texObj
;
2495 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2501 * If the texture object's GenerateMipmap flag is set and we've
2502 * changed the texture base level image, regenerate the rest of the
2503 * mipmap levels now.
2506 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2507 struct gl_texture_object
*texObj
, GLint level
)
2509 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2510 if (texObj
->GenerateMipmap
&&
2511 level
== texObj
->BaseLevel
&&
2512 level
< texObj
->MaxLevel
) {
2513 ASSERT(ctx
->Driver
.GenerateMipmap
);
2514 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2519 /** Debug helper: override the user-requested internal format */
2521 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2524 if (internalFormat
== GL_RGBA16F_ARB
||
2525 internalFormat
== GL_RGBA32F_ARB
) {
2526 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2529 else if (internalFormat
== GL_RGB16F_ARB
||
2530 internalFormat
== GL_RGB32F_ARB
) {
2531 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2534 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2535 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2536 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2537 return GL_LUMINANCE_ALPHA
;
2539 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2540 internalFormat
== GL_LUMINANCE32F_ARB
) {
2541 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2542 return GL_LUMINANCE
;
2544 else if (internalFormat
== GL_ALPHA16F_ARB
||
2545 internalFormat
== GL_ALPHA32F_ARB
) {
2546 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2550 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2551 internalFormat = GL_RGBA;
2555 return internalFormat
;
2558 return internalFormat
;
2564 * Choose the actual hardware format for a texture image.
2565 * Try to use the same format as the previous image level when possible.
2566 * Otherwise, ask the driver for the best format.
2567 * It's important to try to choose a consistant format for all levels
2568 * for efficient texture memory layout/allocation. In particular, this
2569 * comes up during automatic mipmap generation.
2572 _mesa_choose_texture_format(struct gl_context
*ctx
,
2573 struct gl_texture_object
*texObj
,
2574 GLenum target
, GLint level
,
2575 GLenum internalFormat
, GLenum format
, GLenum type
)
2579 /* see if we've already chosen a format for the previous level */
2581 struct gl_texture_image
*prevImage
=
2582 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2583 /* See if the prev level is defined and has an internal format which
2584 * matches the new internal format.
2587 prevImage
->Width
> 0 &&
2588 prevImage
->InternalFormat
== internalFormat
) {
2589 /* use the same format */
2590 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2591 return prevImage
->TexFormat
;
2595 /* choose format from scratch */
2596 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
, format
, type
);
2597 ASSERT(f
!= MESA_FORMAT_NONE
);
2602 * Adjust pixel unpack params and image dimensions to strip off the
2603 * one-pixel texture border.
2605 * Gallium and intel don't support texture borders. They've seldem been used
2606 * and seldom been implemented correctly anyway.
2608 * \param unpackNew returns the new pixel unpack parameters
2611 strip_texture_border(GLenum target
,
2612 GLint
*width
, GLint
*height
, GLint
*depth
,
2613 const struct gl_pixelstore_attrib
*unpack
,
2614 struct gl_pixelstore_attrib
*unpackNew
)
2620 *unpackNew
= *unpack
;
2622 if (unpackNew
->RowLength
== 0)
2623 unpackNew
->RowLength
= *width
;
2625 if (unpackNew
->ImageHeight
== 0)
2626 unpackNew
->ImageHeight
= *height
;
2628 assert(*width
>= 3);
2629 unpackNew
->SkipPixels
++; /* skip the border */
2630 *width
= *width
- 2; /* reduce the width by two border pixels */
2632 /* The min height of a texture with a border is 3 */
2633 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2634 unpackNew
->SkipRows
++; /* skip the border */
2635 *height
= *height
- 2; /* reduce the height by two border pixels */
2638 if (*depth
>= 3 && target
!= GL_TEXTURE_2D_ARRAY
) {
2639 unpackNew
->SkipImages
++; /* skip the border */
2640 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2645 * Common code to implement all the glTexImage1D/2D/3D functions.
2648 teximage(struct gl_context
*ctx
, GLuint dims
,
2649 GLenum target
, GLint level
, GLint internalFormat
,
2650 GLsizei width
, GLsizei height
, GLsizei depth
,
2651 GLint border
, GLenum format
, GLenum type
,
2652 const GLvoid
*pixels
)
2655 struct gl_pixelstore_attrib unpack_no_border
;
2656 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2658 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2660 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2661 _mesa_debug(ctx
, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2663 _mesa_lookup_enum_by_nr(target
), level
,
2664 _mesa_lookup_enum_by_nr(internalFormat
),
2665 width
, height
, depth
, border
,
2666 _mesa_lookup_enum_by_nr(format
),
2667 _mesa_lookup_enum_by_nr(type
), pixels
);
2669 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2671 /* target error checking */
2672 if (!legal_teximage_target(ctx
, dims
, target
)) {
2673 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage%uD(target=%s)",
2674 dims
, _mesa_lookup_enum_by_nr(target
));
2678 /* general error checking */
2679 error
= texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2680 format
, type
, width
, height
, depth
, border
);
2682 if (_mesa_is_proxy_texture(target
)) {
2683 /* Proxy texture: just clear or set state depending on error checking */
2684 struct gl_texture_image
*texImage
=
2685 get_proxy_tex_image(ctx
, target
, level
);
2686 gl_format texFormat
= MESA_FORMAT_NONE
;
2689 /* No parameter errors. Choose a texture format and see if we
2690 * can really allocate the texture.
2692 struct gl_texture_object
*texObj
=
2693 _mesa_get_current_tex_object(ctx
, target
);
2694 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2695 internalFormat
, format
, type
);
2696 if (!legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
2697 error
= PROXY_ERROR
;
2701 texImage
= get_proxy_tex_image(ctx
, target
, level
);
2703 if (error
== PROXY_ERROR
) {
2704 /* image too large, etc. Clear all proxy texture image parameters. */
2706 clear_teximage_fields(texImage
);
2708 else if (error
== GL_FALSE
) {
2709 /* no error: store the teximage parameters */
2711 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2712 border
, internalFormat
, texFormat
);
2715 /* other, regular error (was already recorded) */
2719 /* non-proxy target */
2720 const GLuint face
= _mesa_tex_target_to_face(target
);
2721 struct gl_texture_object
*texObj
;
2722 struct gl_texture_image
*texImage
;
2725 return; /* error was recorded */
2728 /* Allow a hardware driver to just strip out the border, to provide
2729 * reliable but slightly incorrect hardware rendering instead of
2730 * rarely-tested software fallback rendering.
2732 if (border
&& ctx
->Const
.StripTextureBorder
) {
2733 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
2736 unpack
= &unpack_no_border
;
2739 if (ctx
->NewState
& _NEW_PIXEL
)
2740 _mesa_update_state(ctx
);
2742 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2744 _mesa_lock_texture(ctx
, texObj
);
2746 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2749 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
2752 gl_format texFormat
;
2754 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
2756 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2757 internalFormat
, format
,
2760 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
2761 _mesa_init_teximage_fields(ctx
, texImage
,
2762 width
, height
, depth
,
2763 border
, internalFormat
, texFormat
);
2765 /* Give the texture to the driver. <pixels> may be null. */
2766 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
2767 type
, pixels
, unpack
);
2769 check_gen_mipmap(ctx
, target
, texObj
, level
);
2771 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
2773 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
2776 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
2780 _mesa_unlock_texture(ctx
, texObj
);
2786 * Called from the API. Note that width includes the border.
2789 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2790 GLsizei width
, GLint border
, GLenum format
,
2791 GLenum type
, const GLvoid
*pixels
)
2793 GET_CURRENT_CONTEXT(ctx
);
2794 teximage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1,
2795 border
, format
, type
, pixels
);
2800 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
2801 GLsizei width
, GLsizei height
, GLint border
,
2802 GLenum format
, GLenum type
,
2803 const GLvoid
*pixels
)
2805 GET_CURRENT_CONTEXT(ctx
);
2806 teximage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1,
2807 border
, format
, type
, pixels
);
2812 * Called by the API or display list executor.
2813 * Note that width and height include the border.
2816 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
2817 GLsizei width
, GLsizei height
, GLsizei depth
,
2818 GLint border
, GLenum format
, GLenum type
,
2819 const GLvoid
*pixels
)
2821 GET_CURRENT_CONTEXT(ctx
);
2822 teximage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
2823 border
, format
, type
, pixels
);
2828 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
2829 GLsizei width
, GLsizei height
, GLsizei depth
,
2830 GLint border
, GLenum format
, GLenum type
,
2831 const GLvoid
*pixels
)
2833 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
2834 depth
, border
, format
, type
, pixels
);
2838 #if FEATURE_OES_EGL_image
2840 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
2842 struct gl_texture_object
*texObj
;
2843 struct gl_texture_image
*texImage
;
2845 GET_CURRENT_CONTEXT(ctx
);
2846 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2850 valid_target
= ctx
->Extensions
.OES_EGL_image
;
2852 case GL_TEXTURE_EXTERNAL_OES
:
2853 valid_target
= ctx
->Extensions
.OES_EGL_image_external
;
2856 valid_target
= false;
2860 if (!valid_target
) {
2861 _mesa_error(ctx
, GL_INVALID_ENUM
,
2862 "glEGLImageTargetTexture2D(target=%d)", target
);
2866 if (ctx
->NewState
& _NEW_PIXEL
)
2867 _mesa_update_state(ctx
);
2869 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2870 _mesa_lock_texture(ctx
, texObj
);
2872 if (texObj
->Immutable
) {
2873 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2874 "glEGLImageTargetTexture2D(texture is immutable)");
2875 _mesa_unlock_texture(ctx
, texObj
);
2879 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
2881 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
2883 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
2885 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
2886 texObj
, texImage
, image
);
2888 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
2890 _mesa_unlock_texture(ctx
, texObj
);
2898 * Implement all the glTexSubImage1/2/3D() functions.
2901 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2902 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2903 GLsizei width
, GLsizei height
, GLsizei depth
,
2904 GLenum format
, GLenum type
, const GLvoid
*pixels
)
2906 struct gl_texture_object
*texObj
;
2907 struct gl_texture_image
*texImage
;
2909 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2911 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2912 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
2914 _mesa_lookup_enum_by_nr(target
), level
,
2915 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
2916 _mesa_lookup_enum_by_nr(format
),
2917 _mesa_lookup_enum_by_nr(type
), pixels
);
2919 /* check target (proxies not allowed) */
2920 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
2921 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
2922 dims
, _mesa_lookup_enum_by_nr(target
));
2926 if (ctx
->NewState
& _NEW_PIXEL
)
2927 _mesa_update_state(ctx
);
2929 if (subtexture_error_check(ctx
, dims
, target
, level
, xoffset
, yoffset
, zoffset
,
2930 width
, height
, depth
, format
, type
)) {
2931 return; /* error was detected */
2934 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2936 _mesa_lock_texture(ctx
, texObj
);
2938 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2940 if (subtexture_error_check2(ctx
, dims
, target
, level
,
2941 xoffset
, yoffset
, zoffset
,
2942 width
, height
, depth
,
2943 format
, type
, texImage
)) {
2944 /* error was recorded */
2946 else if (width
> 0 && height
> 0 && depth
> 0) {
2947 /* If we have a border, offset=-1 is legal. Bias by border width. */
2950 if (target
!= GL_TEXTURE_2D_ARRAY
)
2951 zoffset
+= texImage
->Border
;
2954 if (target
!= GL_TEXTURE_1D_ARRAY
)
2955 yoffset
+= texImage
->Border
;
2958 xoffset
+= texImage
->Border
;
2961 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
2962 xoffset
, yoffset
, zoffset
,
2963 width
, height
, depth
,
2964 format
, type
, pixels
, &ctx
->Unpack
);
2966 check_gen_mipmap(ctx
, target
, texObj
, level
);
2968 ctx
->NewState
|= _NEW_TEXTURE
;
2971 _mesa_unlock_texture(ctx
, texObj
);
2976 _mesa_TexSubImage1D( GLenum target
, GLint level
,
2977 GLint xoffset
, GLsizei width
,
2978 GLenum format
, GLenum type
,
2979 const GLvoid
*pixels
)
2981 GET_CURRENT_CONTEXT(ctx
);
2982 texsubimage(ctx
, 1, target
, level
,
2985 format
, type
, pixels
);
2990 _mesa_TexSubImage2D( GLenum target
, GLint level
,
2991 GLint xoffset
, GLint yoffset
,
2992 GLsizei width
, GLsizei height
,
2993 GLenum format
, GLenum type
,
2994 const GLvoid
*pixels
)
2996 GET_CURRENT_CONTEXT(ctx
);
2997 texsubimage(ctx
, 2, target
, level
,
2998 xoffset
, yoffset
, 0,
3000 format
, type
, pixels
);
3006 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3007 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3008 GLsizei width
, GLsizei height
, GLsizei depth
,
3009 GLenum format
, GLenum type
,
3010 const GLvoid
*pixels
)
3012 GET_CURRENT_CONTEXT(ctx
);
3013 texsubimage(ctx
, 3, target
, level
,
3014 xoffset
, yoffset
, zoffset
,
3015 width
, height
, depth
,
3016 format
, type
, pixels
);
3022 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3023 * from. This depends on whether the texture contains color or depth values.
3025 static struct gl_renderbuffer
*
3026 get_copy_tex_image_source(struct gl_context
*ctx
, gl_format texFormat
)
3028 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3029 /* reading from depth/stencil buffer */
3030 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3033 /* copying from color buffer */
3034 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3041 * Implement the glCopyTexImage1/2D() functions.
3044 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3045 GLenum target
, GLint level
, GLenum internalFormat
,
3046 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3048 struct gl_texture_object
*texObj
;
3049 struct gl_texture_image
*texImage
;
3050 const GLuint face
= _mesa_tex_target_to_face(target
);
3052 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3054 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3055 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3057 _mesa_lookup_enum_by_nr(target
), level
,
3058 _mesa_lookup_enum_by_nr(internalFormat
),
3059 x
, y
, width
, height
, border
);
3061 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3062 _mesa_update_state(ctx
);
3064 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3065 width
, height
, border
))
3068 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3070 if (border
&& ctx
->Const
.StripTextureBorder
) {
3072 width
-= border
* 2;
3075 height
-= border
* 2;
3080 _mesa_lock_texture(ctx
, texObj
);
3082 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3085 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3088 /* choose actual hw format */
3089 gl_format texFormat
= _mesa_choose_texture_format(ctx
, texObj
,
3094 if (legal_texture_size(ctx
, texFormat
, width
, height
, 1)) {
3095 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3097 /* Free old texture image */
3098 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3100 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3101 border
, internalFormat
, texFormat
);
3103 /* Allocate texture memory (no pixel data yet) */
3104 ctx
->Driver
.TexImage(ctx
, dims
, texImage
,
3106 NULL
, &ctx
->Unpack
);
3108 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3110 struct gl_renderbuffer
*srcRb
=
3111 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3113 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
, dstX
, dstY
, dstZ
,
3114 srcRb
, srcX
, srcY
, width
, height
);
3117 check_gen_mipmap(ctx
, target
, texObj
, level
);
3119 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3121 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3124 /* probably too large of image */
3125 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3129 _mesa_unlock_texture(ctx
, texObj
);
3135 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3136 GLenum internalFormat
,
3138 GLsizei width
, GLint border
)
3140 GET_CURRENT_CONTEXT(ctx
);
3141 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3147 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3148 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3151 GET_CURRENT_CONTEXT(ctx
);
3152 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3153 x
, y
, width
, height
, border
);
3159 * Implementation for glCopyTexSubImage1/2/3D() functions.
3162 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3163 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3164 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3166 struct gl_texture_object
*texObj
;
3167 struct gl_texture_image
*texImage
;
3169 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3171 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3172 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3174 _mesa_lookup_enum_by_nr(target
),
3175 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3177 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3178 _mesa_update_state(ctx
);
3180 if (copytexsubimage_error_check1(ctx
, dims
, target
, level
))
3183 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3185 _mesa_lock_texture(ctx
, texObj
);
3187 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3189 if (copytexsubimage_error_check2(ctx
, dims
, target
, level
, xoffset
, yoffset
,
3190 zoffset
, width
, height
, texImage
)) {
3191 /* error was recored */
3194 /* If we have a border, offset=-1 is legal. Bias by border width. */
3197 if (target
!= GL_TEXTURE_2D_ARRAY
)
3198 zoffset
+= texImage
->Border
;
3201 if (target
!= GL_TEXTURE_1D_ARRAY
)
3202 yoffset
+= texImage
->Border
;
3205 xoffset
+= texImage
->Border
;
3208 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3210 struct gl_renderbuffer
*srcRb
=
3211 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3213 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3214 xoffset
, yoffset
, zoffset
,
3215 srcRb
, x
, y
, width
, height
);
3217 check_gen_mipmap(ctx
, target
, texObj
, level
);
3219 ctx
->NewState
|= _NEW_TEXTURE
;
3223 _mesa_unlock_texture(ctx
, texObj
);
3228 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3229 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3231 GET_CURRENT_CONTEXT(ctx
);
3232 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
3238 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3239 GLint xoffset
, GLint yoffset
,
3240 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3242 GET_CURRENT_CONTEXT(ctx
);
3243 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
3250 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3251 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3252 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3254 GET_CURRENT_CONTEXT(ctx
);
3255 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3256 x
, y
, width
, height
);
3262 /**********************************************************************/
3263 /****** Compressed Textures ******/
3264 /**********************************************************************/
3268 * Return expected size of a compressed texture.
3271 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
3274 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3275 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
3280 * Return compressed texture block size, in pixels.
3283 get_compressed_block_size(GLenum glformat
, GLuint
*bw
, GLuint
*bh
)
3285 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
3286 _mesa_get_format_block_size(mesaFormat
, bw
, bh
);
3291 * Error checking for glCompressedTexImage[123]D().
3292 * \param reason returns reason for error, if any
3293 * \return error code or GL_NO_ERROR or PROXY_ERROR.
3296 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3297 GLenum target
, GLint level
,
3298 GLenum internalFormat
, GLsizei width
,
3299 GLsizei height
, GLsizei depth
, GLint border
,
3300 GLsizei imageSize
, char **reason
)
3302 const GLenum proxyTarget
= get_proxy_target(target
);
3303 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
3305 GLenum choose_format
;
3307 GLenum proxy_format
;
3309 *reason
= ""; /* no error */
3311 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
3313 return GL_INVALID_ENUM
;
3316 /* This will detect any invalid internalFormat value */
3317 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
3318 *reason
= "internalFormat";
3319 return GL_INVALID_ENUM
;
3322 switch (internalFormat
) {
3324 case GL_PALETTE4_RGB8_OES
:
3325 case GL_PALETTE4_RGBA8_OES
:
3326 case GL_PALETTE4_R5_G6_B5_OES
:
3327 case GL_PALETTE4_RGBA4_OES
:
3328 case GL_PALETTE4_RGB5_A1_OES
:
3329 case GL_PALETTE8_RGB8_OES
:
3330 case GL_PALETTE8_RGBA8_OES
:
3331 case GL_PALETTE8_R5_G6_B5_OES
:
3332 case GL_PALETTE8_RGBA4_OES
:
3333 case GL_PALETTE8_RGB5_A1_OES
:
3334 _mesa_cpal_compressed_format_type(internalFormat
, &choose_format
,
3336 proxy_format
= choose_format
;
3339 if (level
> 0 || level
< -maxLevels
) {
3341 return GL_INVALID_VALUE
;
3344 if (dimensions
!= 2) {
3345 *reason
= "compressed paletted textures must be 2D";
3346 return GL_INVALID_OPERATION
;
3349 /* Figure out the expected texture size (in bytes). This will be
3350 * checked against the actual size later.
3352 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
3355 /* This is for the benefit of the TestProxyTexImage below. It expects
3356 * level to be non-negative. OES_compressed_paletted_texture uses a
3357 * weird mechanism where the level specified to glCompressedTexImage2D
3358 * is -(n-1) number of levels in the texture, and the data specifies the
3359 * complete mipmap stack. This is done to ensure the palette is the
3360 * same for all levels.
3367 choose_format
= GL_NONE
;
3368 choose_type
= GL_NONE
;
3369 proxy_format
= internalFormat
;
3372 if (level
< 0 || level
>= maxLevels
) {
3374 return GL_INVALID_VALUE
;
3377 /* Figure out the expected texture size (in bytes). This will be
3378 * checked against the actual size later.
3380 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
3384 /* This should really never fail */
3385 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
3386 *reason
= "internalFormat";
3387 return GL_INVALID_ENUM
;
3390 /* No compressed formats support borders at this time */
3392 *reason
= "border != 0";
3393 return GL_INVALID_VALUE
;
3396 /* For cube map, width must equal height */
3397 if (_mesa_is_cube_face(target
) && width
!= height
) {
3398 *reason
= "width != height";
3399 return GL_INVALID_VALUE
;
3402 /* check image size against compression block size */
3404 gl_format texFormat
=
3405 ctx
->Driver
.ChooseTextureFormat(ctx
, proxy_format
,
3406 choose_format
, choose_type
);
3409 _mesa_get_format_block_size(texFormat
, &bw
, &bh
);
3410 if ((width
> bw
&& width
% bw
> 0) ||
3411 (height
> bh
&& height
% bh
> 0)) {
3413 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
3414 * generated [...] if any parameter combinations are not
3415 * supported by the specific compressed internal format.
3417 *reason
= "invalid width or height for compression format";
3418 return GL_INVALID_OPERATION
;
3422 /* check image sizes */
3423 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
3424 proxy_format
, choose_format
,
3426 width
, height
, depth
, border
)) {
3427 /* See error comment above */
3428 *reason
= "invalid width, height or format";
3429 if (target
== proxyTarget
) {
3432 return GL_INVALID_OPERATION
;
3435 /* check image size in bytes */
3436 if (expectedSize
!= imageSize
) {
3437 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
3438 * if <imageSize> is not consistent with the format, dimensions, and
3439 * contents of the specified image.
3441 *reason
= "imageSize inconsistant with width/height/format";
3442 return GL_INVALID_VALUE
;
3445 if (!mutable_tex_object(ctx
, target
)) {
3446 *reason
= "immutable texture";
3447 return GL_INVALID_OPERATION
;
3455 * Error checking for glCompressedTexSubImage[123]D().
3456 * \warning There are some bad assumptions here about the size of compressed
3457 * texture tiles (multiple of 4) used to test the validity of the
3458 * offset and size parameters.
3459 * \return error code or GL_NO_ERROR.
3462 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dimensions
,
3463 GLenum target
, GLint level
,
3464 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3465 GLsizei width
, GLsizei height
, GLsizei depth
,
3466 GLenum format
, GLsizei imageSize
)
3468 GLint expectedSize
, maxLevels
= 0, maxTextureSize
;
3472 if (dimensions
== 1) {
3473 /* 1D compressed textures not allowed */
3474 return GL_INVALID_ENUM
;
3476 else if (dimensions
== 2) {
3477 if (target
== GL_PROXY_TEXTURE_2D
) {
3478 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3480 else if (target
== GL_TEXTURE_2D
) {
3481 maxLevels
= ctx
->Const
.MaxTextureLevels
;
3483 else if (target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
3484 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3485 return GL_INVALID_ENUM
; /*target*/
3486 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3488 else if (_mesa_is_cube_face(target
)) {
3489 if (!ctx
->Extensions
.ARB_texture_cube_map
)
3490 return GL_INVALID_ENUM
; /*target*/
3491 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
3494 return GL_INVALID_ENUM
; /*target*/
3497 else if (dimensions
== 3) {
3498 /* 3D compressed textures not allowed */
3499 return GL_INVALID_ENUM
;
3502 maxTextureSize
= 1 << (maxLevels
- 1);
3504 /* this will catch any invalid compressed format token */
3505 if (!_mesa_is_compressed_format(ctx
, format
))
3506 return GL_INVALID_ENUM
;
3508 if (width
< 1 || width
> maxTextureSize
)
3509 return GL_INVALID_VALUE
;
3511 if ((height
< 1 || height
> maxTextureSize
)
3513 return GL_INVALID_VALUE
;
3515 if (level
< 0 || level
>= maxLevels
)
3516 return GL_INVALID_VALUE
;
3519 * do checks which depend on compression block size
3521 get_compressed_block_size(format
, &bw
, &bh
);
3523 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0))
3524 return GL_INVALID_VALUE
;
3526 if ((width
% bw
!= 0) && width
!= 2 && width
!= 1)
3527 return GL_INVALID_VALUE
;
3529 if ((height
% bh
!= 0) && height
!= 2 && height
!= 1)
3530 return GL_INVALID_VALUE
;
3532 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3533 if (expectedSize
!= imageSize
)
3534 return GL_INVALID_VALUE
;
3541 * Do second part of glCompressedTexSubImage error checking.
3542 * \return GL_TRUE if error found, GL_FALSE otherwise.
3545 compressed_subtexture_error_check2(struct gl_context
*ctx
, GLuint dims
,
3546 GLsizei width
, GLsizei height
,
3547 GLsizei depth
, GLenum format
,
3548 struct gl_texture_image
*texImage
)
3551 if ((GLint
) format
!= texImage
->InternalFormat
) {
3552 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3553 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3557 if (compressedteximage_only_format(ctx
, format
)) {
3558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3559 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3564 if (((width
== 1 || width
== 2) &&
3565 width
!= (GLsizei
) texImage
->Width
) ||
3566 (width
> (GLsizei
) texImage
->Width
)) {
3567 _mesa_error(ctx
, GL_INVALID_VALUE
,
3568 "glCompressedTexSubImage%uD(width=%d)", dims
, width
);
3573 if (((height
== 1 || height
== 2) &&
3574 height
!= (GLsizei
) texImage
->Height
) ||
3575 (height
> (GLsizei
) texImage
->Height
)) {
3576 _mesa_error(ctx
, GL_INVALID_VALUE
,
3577 "glCompressedTexSubImage%uD(height=%d)", dims
, height
);
3583 if (((depth
== 1 || depth
== 2) &&
3584 depth
!= (GLsizei
) texImage
->Depth
) ||
3585 (depth
> (GLsizei
) texImage
->Depth
)) {
3586 _mesa_error(ctx
, GL_INVALID_VALUE
,
3587 "glCompressedTexSubImage%uD(depth=%d)", dims
, depth
);
3597 * Implementation of the glCompressedTexImage1/2/3D() functions.
3600 compressedteximage(struct gl_context
*ctx
, GLuint dims
,
3601 GLenum target
, GLint level
,
3602 GLenum internalFormat
, GLsizei width
,
3603 GLsizei height
, GLsizei depth
, GLint border
,
3604 GLsizei imageSize
, const GLvoid
*data
)
3609 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3611 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3613 "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3615 _mesa_lookup_enum_by_nr(target
), level
,
3616 _mesa_lookup_enum_by_nr(internalFormat
),
3617 width
, height
, depth
, border
, imageSize
, data
);
3620 if (!legal_teximage_target(ctx
, dims
, target
)) {
3621 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(target=%s)",
3622 dims
, _mesa_lookup_enum_by_nr(target
));
3626 error
= compressed_texture_error_check(ctx
, dims
, target
, level
,
3627 internalFormat
, width
, height
, depth
,
3628 border
, imageSize
, &reason
);
3631 /* XXX this is kind of a hack */
3632 if (!error
&& dims
== 2) {
3633 switch (internalFormat
) {
3634 case GL_PALETTE4_RGB8_OES
:
3635 case GL_PALETTE4_RGBA8_OES
:
3636 case GL_PALETTE4_R5_G6_B5_OES
:
3637 case GL_PALETTE4_RGBA4_OES
:
3638 case GL_PALETTE4_RGB5_A1_OES
:
3639 case GL_PALETTE8_RGB8_OES
:
3640 case GL_PALETTE8_RGBA8_OES
:
3641 case GL_PALETTE8_R5_G6_B5_OES
:
3642 case GL_PALETTE8_RGBA4_OES
:
3643 case GL_PALETTE8_RGB5_A1_OES
:
3644 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3645 width
, height
, imageSize
, data
);
3651 if (_mesa_is_proxy_texture(target
)) {
3652 /* Proxy texture: just check for errors and update proxy state */
3653 struct gl_texture_image
*texImage
=
3654 get_proxy_tex_image(ctx
, target
, level
);
3655 gl_format texFormat
= MESA_FORMAT_NONE
;
3658 /* No parameter errors. Choose a texture format and see if we
3659 * can really allocate the texture.
3661 struct gl_texture_object
*texObj
=
3662 _mesa_get_current_tex_object(ctx
, target
);
3663 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3666 if (!legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
3667 error
= PROXY_ERROR
;
3671 if (error
== PROXY_ERROR
) {
3672 /* image too large, etc. Clear all proxy texture image parameters. */
3674 clear_teximage_fields(texImage
);
3676 else if (error
== GL_NO_ERROR
) {
3677 /* no error: store the teximage parameters */
3679 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3680 border
, internalFormat
, texFormat
);
3683 /* other, regular error */
3684 _mesa_error(ctx
, error
, "glCompressedTexImage%uD(%s)", dims
, reason
);
3688 /* non-proxy target */
3689 struct gl_texture_object
*texObj
;
3690 struct gl_texture_image
*texImage
;
3693 _mesa_error(ctx
, error
, "glCompressedTexImage%uD(%s)", dims
, reason
);
3697 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3699 _mesa_lock_texture(ctx
, texObj
);
3701 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3703 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3704 "glCompressedTexImage%uD", dims
);
3707 gl_format texFormat
;
3709 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3711 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3712 internalFormat
, GL_NONE
,
3715 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
3716 _mesa_init_teximage_fields(ctx
, texImage
,
3717 width
, height
, depth
,
3718 border
, internalFormat
, texFormat
);
3720 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
, imageSize
,
3723 check_gen_mipmap(ctx
, target
, texObj
, level
);
3725 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3728 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3729 "glCompressedTexImage%uD", dims
);
3733 _mesa_unlock_texture(ctx
, texObj
);
3739 _mesa_CompressedTexImage1DARB(GLenum target
, GLint level
,
3740 GLenum internalFormat
, GLsizei width
,
3741 GLint border
, GLsizei imageSize
,
3744 GET_CURRENT_CONTEXT(ctx
);
3745 compressedteximage(ctx
, 1, target
, level
, internalFormat
,
3746 width
, 1, 1, border
, imageSize
, data
);
3751 _mesa_CompressedTexImage2DARB(GLenum target
, GLint level
,
3752 GLenum internalFormat
, GLsizei width
,
3753 GLsizei height
, GLint border
, GLsizei imageSize
,
3756 GET_CURRENT_CONTEXT(ctx
);
3757 compressedteximage(ctx
, 2, target
, level
, internalFormat
,
3758 width
, height
, 1, border
, imageSize
, data
);
3763 _mesa_CompressedTexImage3DARB(GLenum target
, GLint level
,
3764 GLenum internalFormat
, GLsizei width
,
3765 GLsizei height
, GLsizei depth
, GLint border
,
3766 GLsizei imageSize
, const GLvoid
*data
)
3768 GET_CURRENT_CONTEXT(ctx
);
3769 compressedteximage(ctx
, 3, target
, level
, internalFormat
,
3770 width
, height
, depth
, border
, imageSize
, data
);
3775 * Common helper for glCompressedTexSubImage1/2/3D().
3778 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3779 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3780 GLsizei width
, GLsizei height
, GLsizei depth
,
3781 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3783 struct gl_texture_object
*texObj
;
3784 struct gl_texture_image
*texImage
;
3786 GET_CURRENT_CONTEXT(ctx
);
3787 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3789 error
= compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3790 xoffset
, 0, 0, /* pos */
3791 width
, height
, depth
, /* size */
3794 _mesa_error(ctx
, error
, "glCompressedTexSubImage%uD", dims
);
3798 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3800 _mesa_lock_texture(ctx
, texObj
);
3802 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3805 if (compressed_subtexture_error_check2(ctx
, dims
, width
, height
, depth
,
3806 format
, texImage
)) {
3807 /* error was recorded */
3809 else if (width
> 0 && height
> 0 && depth
> 0) {
3810 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
3811 xoffset
, yoffset
, zoffset
,
3812 width
, height
, depth
,
3813 format
, imageSize
, data
);
3815 check_gen_mipmap(ctx
, target
, texObj
, level
);
3817 ctx
->NewState
|= _NEW_TEXTURE
;
3820 _mesa_unlock_texture(ctx
, texObj
);
3825 _mesa_CompressedTexSubImage1DARB(GLenum target
, GLint level
, GLint xoffset
,
3826 GLsizei width
, GLenum format
,
3827 GLsizei imageSize
, const GLvoid
*data
)
3829 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3830 format
, imageSize
, data
);
3835 _mesa_CompressedTexSubImage2DARB(GLenum target
, GLint level
, GLint xoffset
,
3836 GLint yoffset
, GLsizei width
, GLsizei height
,
3837 GLenum format
, GLsizei imageSize
,
3840 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3841 width
, height
, 1, format
, imageSize
, data
);
3846 _mesa_CompressedTexSubImage3DARB(GLenum target
, GLint level
, GLint xoffset
,
3847 GLint yoffset
, GLint zoffset
, GLsizei width
,
3848 GLsizei height
, GLsizei depth
, GLenum format
,
3849 GLsizei imageSize
, const GLvoid
*data
)
3851 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3852 width
, height
, depth
, format
, imageSize
, data
);
3856 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
3858 switch (internalFormat
) {
3860 return MESA_FORMAT_A8
;
3862 return MESA_FORMAT_A16
;
3863 case GL_ALPHA16F_ARB
:
3864 return MESA_FORMAT_ALPHA_FLOAT16
;
3865 case GL_ALPHA32F_ARB
:
3866 return MESA_FORMAT_ALPHA_FLOAT32
;
3867 case GL_ALPHA8I_EXT
:
3868 return MESA_FORMAT_ALPHA_INT8
;
3869 case GL_ALPHA16I_EXT
:
3870 return MESA_FORMAT_ALPHA_INT16
;
3871 case GL_ALPHA32I_EXT
:
3872 return MESA_FORMAT_ALPHA_INT32
;
3873 case GL_ALPHA8UI_EXT
:
3874 return MESA_FORMAT_ALPHA_UINT8
;
3875 case GL_ALPHA16UI_EXT
:
3876 return MESA_FORMAT_ALPHA_UINT16
;
3877 case GL_ALPHA32UI_EXT
:
3878 return MESA_FORMAT_ALPHA_UINT32
;
3880 return MESA_FORMAT_L8
;
3881 case GL_LUMINANCE16
:
3882 return MESA_FORMAT_L16
;
3883 case GL_LUMINANCE16F_ARB
:
3884 return MESA_FORMAT_LUMINANCE_FLOAT16
;
3885 case GL_LUMINANCE32F_ARB
:
3886 return MESA_FORMAT_LUMINANCE_FLOAT32
;
3887 case GL_LUMINANCE8I_EXT
:
3888 return MESA_FORMAT_LUMINANCE_INT8
;
3889 case GL_LUMINANCE16I_EXT
:
3890 return MESA_FORMAT_LUMINANCE_INT16
;
3891 case GL_LUMINANCE32I_EXT
:
3892 return MESA_FORMAT_LUMINANCE_INT32
;
3893 case GL_LUMINANCE8UI_EXT
:
3894 return MESA_FORMAT_LUMINANCE_UINT8
;
3895 case GL_LUMINANCE16UI_EXT
:
3896 return MESA_FORMAT_LUMINANCE_UINT16
;
3897 case GL_LUMINANCE32UI_EXT
:
3898 return MESA_FORMAT_LUMINANCE_UINT32
;
3899 case GL_LUMINANCE8_ALPHA8
:
3900 return MESA_FORMAT_AL88
;
3901 case GL_LUMINANCE16_ALPHA16
:
3902 return MESA_FORMAT_AL1616
;
3903 case GL_LUMINANCE_ALPHA16F_ARB
:
3904 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
;
3905 case GL_LUMINANCE_ALPHA32F_ARB
:
3906 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
;
3907 case GL_LUMINANCE_ALPHA8I_EXT
:
3908 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3909 case GL_LUMINANCE_ALPHA16I_EXT
:
3910 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3911 case GL_LUMINANCE_ALPHA32I_EXT
:
3912 return MESA_FORMAT_LUMINANCE_ALPHA_INT16
;
3913 case GL_LUMINANCE_ALPHA8UI_EXT
:
3914 return MESA_FORMAT_LUMINANCE_ALPHA_UINT8
;
3915 case GL_LUMINANCE_ALPHA16UI_EXT
:
3916 return MESA_FORMAT_LUMINANCE_ALPHA_UINT16
;
3917 case GL_LUMINANCE_ALPHA32UI_EXT
:
3918 return MESA_FORMAT_LUMINANCE_ALPHA_UINT32
;
3920 return MESA_FORMAT_I8
;
3921 case GL_INTENSITY16
:
3922 return MESA_FORMAT_I16
;
3923 case GL_INTENSITY16F_ARB
:
3924 return MESA_FORMAT_INTENSITY_FLOAT16
;
3925 case GL_INTENSITY32F_ARB
:
3926 return MESA_FORMAT_INTENSITY_FLOAT32
;
3927 case GL_INTENSITY8I_EXT
:
3928 return MESA_FORMAT_INTENSITY_INT8
;
3929 case GL_INTENSITY16I_EXT
:
3930 return MESA_FORMAT_INTENSITY_INT16
;
3931 case GL_INTENSITY32I_EXT
:
3932 return MESA_FORMAT_INTENSITY_INT32
;
3933 case GL_INTENSITY8UI_EXT
:
3934 return MESA_FORMAT_INTENSITY_UINT8
;
3935 case GL_INTENSITY16UI_EXT
:
3936 return MESA_FORMAT_INTENSITY_UINT16
;
3937 case GL_INTENSITY32UI_EXT
:
3938 return MESA_FORMAT_INTENSITY_UINT32
;
3940 return MESA_FORMAT_RGBA8888_REV
;
3942 return MESA_FORMAT_RGBA_16
;
3943 case GL_RGBA16F_ARB
:
3944 return MESA_FORMAT_RGBA_FLOAT16
;
3945 case GL_RGBA32F_ARB
:
3946 return MESA_FORMAT_RGBA_FLOAT32
;
3948 return MESA_FORMAT_RGBA_INT8
;
3949 case GL_RGBA16I_EXT
:
3950 return MESA_FORMAT_RGBA_INT16
;
3951 case GL_RGBA32I_EXT
:
3952 return MESA_FORMAT_RGBA_INT32
;
3953 case GL_RGBA8UI_EXT
:
3954 return MESA_FORMAT_RGBA_UINT8
;
3955 case GL_RGBA16UI_EXT
:
3956 return MESA_FORMAT_RGBA_UINT16
;
3957 case GL_RGBA32UI_EXT
:
3958 return MESA_FORMAT_RGBA_UINT32
;
3961 return MESA_FORMAT_GR88
;
3963 return MESA_FORMAT_RG1616
;
3965 return MESA_FORMAT_RG_FLOAT16
;
3967 return MESA_FORMAT_RG_FLOAT32
;
3969 return MESA_FORMAT_RG_INT8
;
3971 return MESA_FORMAT_RG_INT16
;
3973 return MESA_FORMAT_RG_INT32
;
3975 return MESA_FORMAT_RG_UINT8
;
3977 return MESA_FORMAT_RG_UINT16
;
3979 return MESA_FORMAT_RG_UINT32
;
3982 return MESA_FORMAT_R8
;
3984 return MESA_FORMAT_R16
;
3986 return MESA_FORMAT_R_FLOAT16
;
3988 return MESA_FORMAT_R_FLOAT32
;
3990 return MESA_FORMAT_R_INT8
;
3992 return MESA_FORMAT_R_INT16
;
3994 return MESA_FORMAT_R_INT32
;
3996 return MESA_FORMAT_R_UINT8
;
3998 return MESA_FORMAT_R_UINT16
;
4000 return MESA_FORMAT_R_UINT32
;
4003 return MESA_FORMAT_NONE
;
4008 validate_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4010 gl_format format
= get_texbuffer_format(ctx
, internalFormat
);
4013 if (format
== MESA_FORMAT_NONE
)
4014 return MESA_FORMAT_NONE
;
4016 datatype
= _mesa_get_format_datatype(format
);
4017 if (datatype
== GL_FLOAT
&& !ctx
->Extensions
.ARB_texture_float
)
4018 return MESA_FORMAT_NONE
;
4020 if (datatype
== GL_HALF_FLOAT
&& !ctx
->Extensions
.ARB_half_float_pixel
)
4021 return MESA_FORMAT_NONE
;
4023 /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
4024 * any mention of R/RG formats, but they appear in the GL 3.1 core
4027 if (ctx
->Version
<= 30) {
4028 GLenum base_format
= _mesa_get_format_base_format(format
);
4030 if (base_format
== GL_R
|| base_format
== GL_RG
)
4031 return MESA_FORMAT_NONE
;
4037 /** GL_ARB_texture_buffer_object */
4039 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
4041 struct gl_texture_object
*texObj
;
4042 struct gl_buffer_object
*bufObj
;
4045 GET_CURRENT_CONTEXT(ctx
);
4046 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
4048 if (!ctx
->Extensions
.ARB_texture_buffer_object
) {
4049 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
4053 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4054 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
4058 format
= validate_texbuffer_format(ctx
, internalFormat
);
4059 if (format
== MESA_FORMAT_NONE
) {
4060 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(internalFormat 0x%x)",
4065 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4066 if (buffer
&& !bufObj
) {
4067 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
4071 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4073 _mesa_lock_texture(ctx
, texObj
);
4075 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4076 texObj
->BufferObjectFormat
= internalFormat
;
4077 texObj
->_BufferObjectFormat
= format
;
4079 _mesa_unlock_texture(ctx
, texObj
);