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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * Texture image-related functions.
34 #include "bufferobj.h"
38 #include "framebuffer.h"
43 #include "multisample.h"
44 #include "pixelstore.h"
46 #include "texcompress.h"
47 #include "texcompress_cpal.h"
51 #include "texstorage.h"
52 #include "textureview.h"
54 #include "glformats.h"
59 * State changes which we care about for glCopyTex[Sub]Image() calls.
60 * In particular, we care about pixel transfer state and buffer state
61 * (such as glReadBuffer to make sure we read from the right renderbuffer).
63 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
66 * Returns a corresponding internal floating point format for a given base
67 * format as specifed by OES_texture_float. In case of GL_FLOAT, the internal
68 * format needs to be a 32 bit component and in case of GL_HALF_FLOAT_OES it
69 * needs to be a 16 bit component.
71 * For example, given base format GL_RGBA, type GL_Float return GL_RGBA32F_ARB.
74 adjust_for_oes_float_texture(GLenum format
, GLenum type
)
84 return GL_ALPHA32F_ARB
;
86 return GL_LUMINANCE32F_ARB
;
87 case GL_LUMINANCE_ALPHA
:
88 return GL_LUMINANCE_ALPHA32F_ARB
;
94 case GL_HALF_FLOAT_OES
:
101 return GL_ALPHA16F_ARB
;
103 return GL_LUMINANCE16F_ARB
;
104 case GL_LUMINANCE_ALPHA
:
105 return GL_LUMINANCE_ALPHA16F_ARB
;
119 * Return the simple base format for a given internal texture format.
120 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
122 * \param ctx GL context.
123 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
125 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
126 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
128 * This is the format which is used during texture application (i.e. the
129 * texture format and env mode determine the arithmetic used.
132 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
134 switch (internalFormat
) {
140 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_ALPHA
: -1;
147 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE
: -1;
149 case GL_LUMINANCE_ALPHA
:
150 case GL_LUMINANCE4_ALPHA4
:
151 case GL_LUMINANCE6_ALPHA2
:
152 case GL_LUMINANCE8_ALPHA8
:
153 case GL_LUMINANCE12_ALPHA4
:
154 case GL_LUMINANCE12_ALPHA12
:
155 case GL_LUMINANCE16_ALPHA16
:
156 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE_ALPHA
: -1;
162 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_INTENSITY
: -1;
164 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGB
: -1;
175 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGBA
: -1;
189 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
191 if (_mesa_is_gles(ctx
)) {
192 switch (internalFormat
) {
200 if (ctx
->Extensions
.ARB_ES2_compatibility
) {
201 switch (internalFormat
) {
209 if (ctx
->Extensions
.ARB_depth_texture
) {
210 switch (internalFormat
) {
211 case GL_DEPTH_COMPONENT
:
212 case GL_DEPTH_COMPONENT16
:
213 case GL_DEPTH_COMPONENT24
:
214 case GL_DEPTH_COMPONENT32
:
215 return GL_DEPTH_COMPONENT
;
216 case GL_DEPTH_STENCIL
:
217 case GL_DEPTH24_STENCIL8
:
218 return GL_DEPTH_STENCIL
;
224 switch (internalFormat
) {
225 case GL_COMPRESSED_ALPHA
:
227 case GL_COMPRESSED_LUMINANCE
:
229 case GL_COMPRESSED_LUMINANCE_ALPHA
:
230 return GL_LUMINANCE_ALPHA
;
231 case GL_COMPRESSED_INTENSITY
:
233 case GL_COMPRESSED_RGB
:
235 case GL_COMPRESSED_RGBA
:
241 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
242 switch (internalFormat
) {
243 case GL_COMPRESSED_RGB_FXT1_3DFX
:
245 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
252 /* Assume that the ANGLE flag will always be set if the EXT flag is set.
254 if (ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
255 switch (internalFormat
) {
256 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
258 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
259 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
260 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
267 if (_mesa_is_desktop_gl(ctx
)
268 && ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
269 switch (internalFormat
) {
281 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
282 if (internalFormat
== GL_YCBCR_MESA
)
283 return GL_YCBCR_MESA
;
286 if (ctx
->Extensions
.ARB_texture_float
) {
287 switch (internalFormat
) {
288 case GL_ALPHA16F_ARB
:
289 case GL_ALPHA32F_ARB
:
297 case GL_INTENSITY16F_ARB
:
298 case GL_INTENSITY32F_ARB
:
300 case GL_LUMINANCE16F_ARB
:
301 case GL_LUMINANCE32F_ARB
:
303 case GL_LUMINANCE_ALPHA16F_ARB
:
304 case GL_LUMINANCE_ALPHA32F_ARB
:
305 return GL_LUMINANCE_ALPHA
;
311 if (ctx
->Extensions
.EXT_texture_snorm
) {
312 switch (internalFormat
) {
327 case GL_RGBA16_SNORM
:
330 case GL_ALPHA8_SNORM
:
331 case GL_ALPHA16_SNORM
:
333 case GL_LUMINANCE_SNORM
:
334 case GL_LUMINANCE8_SNORM
:
335 case GL_LUMINANCE16_SNORM
:
337 case GL_LUMINANCE_ALPHA_SNORM
:
338 case GL_LUMINANCE8_ALPHA8_SNORM
:
339 case GL_LUMINANCE16_ALPHA16_SNORM
:
340 return GL_LUMINANCE_ALPHA
;
341 case GL_INTENSITY_SNORM
:
342 case GL_INTENSITY8_SNORM
:
343 case GL_INTENSITY16_SNORM
:
350 if (ctx
->Extensions
.EXT_texture_sRGB
) {
351 switch (internalFormat
) {
354 case GL_COMPRESSED_SRGB_EXT
:
356 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
357 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGB
: -1;
358 case GL_SRGB_ALPHA_EXT
:
359 case GL_SRGB8_ALPHA8_EXT
:
360 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
362 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
363 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
364 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
365 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGBA
: -1;
366 case GL_SLUMINANCE_ALPHA_EXT
:
367 case GL_SLUMINANCE8_ALPHA8_EXT
:
368 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
369 return GL_LUMINANCE_ALPHA
;
370 case GL_SLUMINANCE_EXT
:
371 case GL_SLUMINANCE8_EXT
:
372 case GL_COMPRESSED_SLUMINANCE_EXT
:
379 if (ctx
->Version
>= 30 ||
380 ctx
->Extensions
.EXT_texture_integer
) {
381 switch (internalFormat
) {
383 case GL_RGBA16UI_EXT
:
384 case GL_RGBA32UI_EXT
:
400 if (ctx
->Extensions
.EXT_texture_integer
) {
401 switch (internalFormat
) {
402 case GL_ALPHA8UI_EXT
:
403 case GL_ALPHA16UI_EXT
:
404 case GL_ALPHA32UI_EXT
:
406 case GL_ALPHA16I_EXT
:
407 case GL_ALPHA32I_EXT
:
409 case GL_INTENSITY8UI_EXT
:
410 case GL_INTENSITY16UI_EXT
:
411 case GL_INTENSITY32UI_EXT
:
412 case GL_INTENSITY8I_EXT
:
413 case GL_INTENSITY16I_EXT
:
414 case GL_INTENSITY32I_EXT
:
416 case GL_LUMINANCE8UI_EXT
:
417 case GL_LUMINANCE16UI_EXT
:
418 case GL_LUMINANCE32UI_EXT
:
419 case GL_LUMINANCE8I_EXT
:
420 case GL_LUMINANCE16I_EXT
:
421 case GL_LUMINANCE32I_EXT
:
423 case GL_LUMINANCE_ALPHA8UI_EXT
:
424 case GL_LUMINANCE_ALPHA16UI_EXT
:
425 case GL_LUMINANCE_ALPHA32UI_EXT
:
426 case GL_LUMINANCE_ALPHA8I_EXT
:
427 case GL_LUMINANCE_ALPHA16I_EXT
:
428 case GL_LUMINANCE_ALPHA32I_EXT
:
429 return GL_LUMINANCE_ALPHA
;
435 if (ctx
->Extensions
.ARB_texture_rg
) {
436 switch (internalFormat
) {
439 if (!ctx
->Extensions
.ARB_texture_float
)
448 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
454 case GL_COMPRESSED_RED
:
459 if (!ctx
->Extensions
.ARB_texture_float
)
468 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
474 case GL_COMPRESSED_RG
:
481 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
482 switch (internalFormat
) {
490 if (ctx
->Extensions
.EXT_packed_float
) {
491 switch (internalFormat
) {
492 case GL_R11F_G11F_B10F_EXT
:
499 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
500 switch (internalFormat
) {
501 case GL_DEPTH_COMPONENT32F
:
502 return GL_DEPTH_COMPONENT
;
503 case GL_DEPTH32F_STENCIL8
:
504 return GL_DEPTH_STENCIL
;
510 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
511 switch (internalFormat
) {
512 case GL_COMPRESSED_RED_RGTC1
:
513 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
515 case GL_COMPRESSED_RG_RGTC2
:
516 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
523 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
524 switch (internalFormat
) {
525 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
526 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
528 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
529 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
530 return GL_LUMINANCE_ALPHA
;
536 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
537 switch (internalFormat
) {
538 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
539 return GL_LUMINANCE_ALPHA
;
545 if (ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
) {
546 switch (internalFormat
) {
547 case GL_ETC1_RGB8_OES
:
554 if (_mesa_is_gles3(ctx
) || ctx
->Extensions
.ARB_ES3_compatibility
) {
555 switch (internalFormat
) {
556 case GL_COMPRESSED_RGB8_ETC2
:
557 case GL_COMPRESSED_SRGB8_ETC2
:
559 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
560 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
561 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
562 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
564 case GL_COMPRESSED_R11_EAC
:
565 case GL_COMPRESSED_SIGNED_R11_EAC
:
567 case GL_COMPRESSED_RG11_EAC
:
568 case GL_COMPRESSED_SIGNED_RG11_EAC
:
575 if (_mesa_is_desktop_gl(ctx
) &&
576 ctx
->Extensions
.ARB_texture_compression_bptc
) {
577 switch (internalFormat
) {
578 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
579 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
581 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
582 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
589 if (ctx
->API
== API_OPENGLES
) {
590 switch (internalFormat
) {
591 case GL_PALETTE4_RGB8_OES
:
592 case GL_PALETTE4_R5_G6_B5_OES
:
593 case GL_PALETTE8_RGB8_OES
:
594 case GL_PALETTE8_R5_G6_B5_OES
:
596 case GL_PALETTE4_RGBA8_OES
:
597 case GL_PALETTE8_RGB5_A1_OES
:
598 case GL_PALETTE4_RGBA4_OES
:
599 case GL_PALETTE4_RGB5_A1_OES
:
600 case GL_PALETTE8_RGBA8_OES
:
601 case GL_PALETTE8_RGBA4_OES
:
608 return -1; /* error */
613 * For cube map faces, return a face index in [0,5].
614 * For other targets return 0;
617 _mesa_tex_target_to_face(GLenum target
)
619 if (_mesa_is_cube_face(target
))
620 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
628 * Install gl_texture_image in a gl_texture_object according to the target
629 * and level parameters.
631 * \param tObj texture object.
632 * \param target texture target.
633 * \param level image level.
634 * \param texImage texture image.
637 set_tex_image(struct gl_texture_object
*tObj
,
638 GLenum target
, GLint level
,
639 struct gl_texture_image
*texImage
)
641 const GLuint face
= _mesa_tex_target_to_face(target
);
645 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
648 tObj
->Image
[face
][level
] = texImage
;
650 /* Set the 'back' pointer */
651 texImage
->TexObject
= tObj
;
652 texImage
->Level
= level
;
653 texImage
->Face
= face
;
658 * Allocate a texture image structure.
660 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
663 * \return a pointer to gl_texture_image struct with all fields initialized to
666 struct gl_texture_image
*
667 _mesa_new_texture_image( struct gl_context
*ctx
)
670 return CALLOC_STRUCT(gl_texture_image
);
675 * Free a gl_texture_image and associated data.
676 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
678 * \param texImage texture image.
680 * Free the texture image structure and the associated image data.
683 _mesa_delete_texture_image(struct gl_context
*ctx
,
684 struct gl_texture_image
*texImage
)
686 /* Free texImage->Data and/or any other driver-specific texture
689 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
690 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
696 * Test if a target is a proxy target.
698 * \param target texture target.
700 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
703 _mesa_is_proxy_texture(GLenum target
)
706 static const GLenum targets
[] = {
710 GL_PROXY_TEXTURE_CUBE_MAP
,
711 GL_PROXY_TEXTURE_RECTANGLE
,
712 GL_PROXY_TEXTURE_1D_ARRAY
,
713 GL_PROXY_TEXTURE_2D_ARRAY
,
714 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
715 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
716 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
719 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
720 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
722 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== Elements(targets
) + 2);
724 for (i
= 0; i
< Elements(targets
); ++i
)
725 if (target
== targets
[i
])
732 * Return the proxy target which corresponds to the given texture target
735 proxy_target(GLenum target
)
739 case GL_PROXY_TEXTURE_1D
:
740 return GL_PROXY_TEXTURE_1D
;
742 case GL_PROXY_TEXTURE_2D
:
743 return GL_PROXY_TEXTURE_2D
;
745 case GL_PROXY_TEXTURE_3D
:
746 return GL_PROXY_TEXTURE_3D
;
747 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
748 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
749 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
750 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
751 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
752 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
753 case GL_TEXTURE_CUBE_MAP_ARB
:
754 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
755 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
756 case GL_TEXTURE_RECTANGLE_NV
:
757 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
758 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
759 case GL_TEXTURE_1D_ARRAY_EXT
:
760 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
761 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
762 case GL_TEXTURE_2D_ARRAY_EXT
:
763 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
764 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
765 case GL_TEXTURE_CUBE_MAP_ARRAY
:
766 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
767 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
768 case GL_TEXTURE_2D_MULTISAMPLE
:
769 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
770 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
771 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
772 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
773 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
775 _mesa_problem(NULL
, "unexpected target in proxy_target()");
784 * Get a texture image pointer from a texture object, given a texture
785 * target and mipmap level. The target and level parameters should
786 * have already been error-checked.
788 * \param texObj texture unit.
789 * \param target texture target.
790 * \param level image level.
792 * \return pointer to the texture image structure, or NULL on failure.
794 struct gl_texture_image
*
795 _mesa_select_tex_image(const struct gl_texture_object
*texObj
,
796 GLenum target
, GLint level
)
798 const GLuint face
= _mesa_tex_target_to_face(target
);
802 ASSERT(level
< MAX_TEXTURE_LEVELS
);
804 return texObj
->Image
[face
][level
];
809 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
810 * it and install it. Only return NULL if passed a bad parameter or run
813 struct gl_texture_image
*
814 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
815 GLenum target
, GLint level
)
817 struct gl_texture_image
*texImage
;
822 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
824 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
826 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
830 set_tex_image(texObj
, target
, level
, texImage
);
838 * Return pointer to the specified proxy texture image.
839 * Note that proxy textures are per-context, not per-texture unit.
840 * \return pointer to texture image or NULL if invalid target, invalid
841 * level, or out of memory.
843 static struct gl_texture_image
*
844 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
846 struct gl_texture_image
*texImage
;
853 case GL_PROXY_TEXTURE_1D
:
854 if (level
>= ctx
->Const
.MaxTextureLevels
)
856 texIndex
= TEXTURE_1D_INDEX
;
858 case GL_PROXY_TEXTURE_2D
:
859 if (level
>= ctx
->Const
.MaxTextureLevels
)
861 texIndex
= TEXTURE_2D_INDEX
;
863 case GL_PROXY_TEXTURE_3D
:
864 if (level
>= ctx
->Const
.Max3DTextureLevels
)
866 texIndex
= TEXTURE_3D_INDEX
;
868 case GL_PROXY_TEXTURE_CUBE_MAP
:
869 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
871 texIndex
= TEXTURE_CUBE_INDEX
;
873 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
876 texIndex
= TEXTURE_RECT_INDEX
;
878 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
879 if (level
>= ctx
->Const
.MaxTextureLevels
)
881 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
883 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
884 if (level
>= ctx
->Const
.MaxTextureLevels
)
886 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
888 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
889 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
891 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
893 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
896 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
898 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
901 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
907 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
909 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
911 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
914 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
915 /* Set the 'back' pointer */
916 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
923 * Get the maximum number of allowed mipmap levels.
925 * \param ctx GL context.
926 * \param target texture target.
928 * \return the maximum number of allowed mipmap levels for the given
929 * texture target, or zero if passed a bad target.
934 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
938 case GL_PROXY_TEXTURE_1D
:
940 case GL_PROXY_TEXTURE_2D
:
941 return ctx
->Const
.MaxTextureLevels
;
943 case GL_PROXY_TEXTURE_3D
:
944 return ctx
->Const
.Max3DTextureLevels
;
945 case GL_TEXTURE_CUBE_MAP
:
946 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
947 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
948 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
949 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
950 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
951 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
952 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
953 return ctx
->Extensions
.ARB_texture_cube_map
954 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
955 case GL_TEXTURE_RECTANGLE_NV
:
956 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
957 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
958 case GL_TEXTURE_1D_ARRAY_EXT
:
959 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
960 case GL_TEXTURE_2D_ARRAY_EXT
:
961 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
962 return ctx
->Extensions
.EXT_texture_array
963 ? ctx
->Const
.MaxTextureLevels
: 0;
964 case GL_TEXTURE_CUBE_MAP_ARRAY
:
965 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
966 return ctx
->Extensions
.ARB_texture_cube_map_array
967 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
968 case GL_TEXTURE_BUFFER
:
969 return ctx
->API
== API_OPENGL_CORE
&&
970 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
971 case GL_TEXTURE_2D_MULTISAMPLE
:
972 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
973 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
974 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
975 return _mesa_is_desktop_gl(ctx
)
976 && ctx
->Extensions
.ARB_texture_multisample
978 case GL_TEXTURE_EXTERNAL_OES
:
981 return 0; /* bad target */
987 * Return number of dimensions per mipmap level for the given texture target.
990 _mesa_get_texture_dimensions(GLenum target
)
994 case GL_PROXY_TEXTURE_1D
:
997 case GL_TEXTURE_RECTANGLE
:
998 case GL_TEXTURE_CUBE_MAP
:
999 case GL_PROXY_TEXTURE_2D
:
1000 case GL_PROXY_TEXTURE_RECTANGLE
:
1001 case GL_PROXY_TEXTURE_CUBE_MAP
:
1002 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1003 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1004 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1005 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1006 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1007 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1008 case GL_TEXTURE_1D_ARRAY
:
1009 case GL_PROXY_TEXTURE_1D_ARRAY
:
1010 case GL_TEXTURE_EXTERNAL_OES
:
1011 case GL_TEXTURE_2D_MULTISAMPLE
:
1012 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1015 case GL_PROXY_TEXTURE_3D
:
1016 case GL_TEXTURE_2D_ARRAY
:
1017 case GL_PROXY_TEXTURE_2D_ARRAY
:
1018 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1019 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1020 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1021 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1023 case GL_TEXTURE_BUFFER
:
1026 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1034 * Check if a texture target can have more than one layer.
1037 _mesa_tex_target_is_layered(GLenum target
)
1041 case GL_PROXY_TEXTURE_1D
:
1043 case GL_PROXY_TEXTURE_2D
:
1044 case GL_TEXTURE_RECTANGLE
:
1045 case GL_PROXY_TEXTURE_RECTANGLE
:
1046 case GL_TEXTURE_2D_MULTISAMPLE
:
1047 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1048 case GL_TEXTURE_BUFFER
:
1049 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1050 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1051 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1052 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1053 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1054 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1055 case GL_TEXTURE_EXTERNAL_OES
:
1059 case GL_PROXY_TEXTURE_3D
:
1060 case GL_TEXTURE_CUBE_MAP
:
1061 case GL_PROXY_TEXTURE_CUBE_MAP
:
1062 case GL_TEXTURE_1D_ARRAY
:
1063 case GL_PROXY_TEXTURE_1D_ARRAY
:
1064 case GL_TEXTURE_2D_ARRAY
:
1065 case GL_PROXY_TEXTURE_2D_ARRAY
:
1066 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1067 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1068 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1069 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1073 assert(!"Invalid texture target.");
1080 * Return the number of layers present in the given level of an array,
1081 * cubemap or 3D texture. If the texture is not layered return zero.
1084 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
1086 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
1088 switch (texObj
->Target
) {
1091 case GL_TEXTURE_RECTANGLE
:
1092 case GL_TEXTURE_2D_MULTISAMPLE
:
1093 case GL_TEXTURE_BUFFER
:
1094 case GL_TEXTURE_EXTERNAL_OES
:
1097 case GL_TEXTURE_CUBE_MAP
:
1100 case GL_TEXTURE_1D_ARRAY
: {
1101 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1102 return img
? img
->Height
: 0;
1106 case GL_TEXTURE_2D_ARRAY
:
1107 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1108 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
1109 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1110 return img
? img
->Depth
: 0;
1114 assert(!"Invalid texture target.");
1121 * Return the maximum number of mipmap levels for the given target
1122 * and the dimensions.
1123 * The dimensions are expected not to include the border.
1126 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
1133 case GL_TEXTURE_1D_ARRAY
:
1134 case GL_PROXY_TEXTURE_1D
:
1135 case GL_PROXY_TEXTURE_1D_ARRAY
:
1138 case GL_TEXTURE_CUBE_MAP
:
1139 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1140 case GL_PROXY_TEXTURE_CUBE_MAP
:
1141 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1145 case GL_TEXTURE_2D_ARRAY
:
1146 case GL_PROXY_TEXTURE_2D
:
1147 case GL_PROXY_TEXTURE_2D_ARRAY
:
1148 size
= MAX2(width
, height
);
1151 case GL_PROXY_TEXTURE_3D
:
1152 size
= MAX3(width
, height
, depth
);
1154 case GL_TEXTURE_RECTANGLE
:
1155 case GL_TEXTURE_EXTERNAL_OES
:
1156 case GL_TEXTURE_2D_MULTISAMPLE
:
1157 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1158 case GL_PROXY_TEXTURE_RECTANGLE
:
1159 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1160 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1167 return _mesa_logbase2(size
) + 1;
1171 #if 000 /* not used anymore */
1173 * glTexImage[123]D can accept a NULL image pointer. In this case we
1174 * create a texture image with unspecified image contents per the OpenGL
1178 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1180 const GLint components
= _mesa_components_in_format(format
);
1181 const GLint numPixels
= width
* height
* depth
;
1182 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
1186 * Let's see if anyone finds this. If glTexImage2D() is called with
1187 * a NULL image pointer then load the texture image with something
1188 * interesting instead of leaving it indeterminate.
1191 static const char message
[8][32] = {
1192 " X X XXXXX XXX X ",
1193 " XX XX X X X X X ",
1195 " X X XXXX XXX XXXXX ",
1198 " X X XXXXX XXX X X ",
1202 GLubyte
*imgPtr
= data
;
1204 for (h
= 0; h
< depth
; h
++) {
1205 for (i
= 0; i
< height
; i
++) {
1206 GLint srcRow
= 7 - (i
% 8);
1207 for (j
= 0; j
< width
; j
++) {
1208 GLint srcCol
= j
% 32;
1209 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1210 for (k
= 0; k
< components
; k
++) {
1226 * Set the size and format-related fields of a gl_texture_image struct
1227 * to zero. This is used when a proxy texture test fails.
1230 clear_teximage_fields(struct gl_texture_image
*img
)
1233 img
->_BaseFormat
= 0;
1234 img
->InternalFormat
= 0;
1243 img
->HeightLog2
= 0;
1245 img
->TexFormat
= MESA_FORMAT_NONE
;
1246 img
->NumSamples
= 0;
1247 img
->FixedSampleLocations
= GL_TRUE
;
1252 * Initialize basic fields of the gl_texture_image struct.
1254 * \param ctx GL context.
1255 * \param img texture image structure to be initialized.
1256 * \param width image width.
1257 * \param height image height.
1258 * \param depth image depth.
1259 * \param border image border.
1260 * \param internalFormat internal format.
1261 * \param format the actual hardware format (one of MESA_FORMAT_*)
1262 * \param numSamples number of samples per texel, or zero for non-MS.
1263 * \param fixedSampleLocations are sample locations fixed?
1265 * Fills in the fields of \p img with the given information.
1266 * Note: width, height and depth include the border.
1269 init_teximage_fields_ms(struct gl_context
*ctx
,
1270 struct gl_texture_image
*img
,
1271 GLsizei width
, GLsizei height
, GLsizei depth
,
1272 GLint border
, GLenum internalFormat
,
1274 GLuint numSamples
, GLboolean fixedSampleLocations
)
1279 ASSERT(height
>= 0);
1282 target
= img
->TexObject
->Target
;
1283 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1284 ASSERT(img
->_BaseFormat
!= -1);
1285 img
->InternalFormat
= internalFormat
;
1286 img
->Border
= border
;
1288 img
->Height
= height
;
1291 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1292 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1294 img
->NumSamples
= 0;
1295 img
->FixedSampleLocations
= GL_TRUE
;
1299 case GL_TEXTURE_BUFFER
:
1300 case GL_PROXY_TEXTURE_1D
:
1305 img
->HeightLog2
= 0;
1312 case GL_TEXTURE_1D_ARRAY
:
1313 case GL_PROXY_TEXTURE_1D_ARRAY
:
1314 img
->Height2
= height
; /* no border */
1315 img
->HeightLog2
= 0; /* not used */
1323 case GL_TEXTURE_RECTANGLE
:
1324 case GL_TEXTURE_CUBE_MAP
:
1325 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1326 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1327 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1328 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1329 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1330 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1331 case GL_TEXTURE_EXTERNAL_OES
:
1332 case GL_PROXY_TEXTURE_2D
:
1333 case GL_PROXY_TEXTURE_RECTANGLE
:
1334 case GL_PROXY_TEXTURE_CUBE_MAP
:
1335 case GL_TEXTURE_2D_MULTISAMPLE
:
1336 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1337 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1338 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1345 case GL_TEXTURE_2D_ARRAY
:
1346 case GL_PROXY_TEXTURE_2D_ARRAY
:
1347 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1348 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1349 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1350 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1351 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1352 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1353 img
->Depth2
= depth
; /* no border */
1354 img
->DepthLog2
= 0; /* not used */
1357 case GL_PROXY_TEXTURE_3D
:
1358 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1359 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1360 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1361 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1364 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1369 _mesa_get_tex_max_num_levels(target
,
1370 img
->Width2
, img
->Height2
, img
->Depth2
);
1371 img
->TexFormat
= format
;
1372 img
->NumSamples
= numSamples
;
1373 img
->FixedSampleLocations
= fixedSampleLocations
;
1378 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1379 struct gl_texture_image
*img
,
1380 GLsizei width
, GLsizei height
, GLsizei depth
,
1381 GLint border
, GLenum internalFormat
,
1384 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
1385 internalFormat
, format
, 0, GL_TRUE
);
1390 * Free and clear fields of the gl_texture_image struct.
1392 * \param ctx GL context.
1393 * \param texImage texture image structure to be cleared.
1395 * After the call, \p texImage will have no data associated with it. Its
1396 * fields are cleared so that its parent object will test incomplete.
1399 _mesa_clear_texture_image(struct gl_context
*ctx
,
1400 struct gl_texture_image
*texImage
)
1402 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1403 clear_teximage_fields(texImage
);
1408 * Check the width, height, depth and border of a texture image are legal.
1409 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
1411 * The target and level parameters will have already been validated.
1412 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
1415 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
1416 GLint level
, GLint width
, GLint height
,
1417 GLint depth
, GLint border
)
1423 case GL_PROXY_TEXTURE_1D
:
1424 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
1425 maxSize
>>= level
; /* level size */
1426 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1428 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1429 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1435 case GL_PROXY_TEXTURE_2D
:
1436 case GL_TEXTURE_2D_MULTISAMPLE
:
1437 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1438 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1440 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1442 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1444 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1445 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1447 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1453 case GL_PROXY_TEXTURE_3D
:
1454 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1456 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1458 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1460 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1462 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1463 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1465 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1467 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1472 case GL_TEXTURE_RECTANGLE_NV
:
1473 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1476 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1477 if (width
< 0 || width
> maxSize
)
1479 if (height
< 0 || height
> maxSize
)
1483 case GL_TEXTURE_CUBE_MAP
:
1484 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1485 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1486 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1487 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1488 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1489 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1490 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1491 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1493 if (width
!= height
)
1495 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1497 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1499 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1500 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1502 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1507 case GL_TEXTURE_1D_ARRAY_EXT
:
1508 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1509 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1511 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1513 if (height
< 0 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1515 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1516 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1521 case GL_TEXTURE_2D_ARRAY_EXT
:
1522 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1523 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1524 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1525 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1527 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1529 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1531 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1533 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1534 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1536 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1541 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1542 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1543 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1544 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1546 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1548 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1550 if (width
!= height
)
1552 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1554 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1555 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1557 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1562 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1569 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1570 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1571 * \param destImage the destination texture image.
1572 * \return GL_TRUE if error found, GL_FALSE otherwise.
1575 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1576 const struct gl_texture_image
*destImage
,
1577 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1578 GLsizei subWidth
, GLsizei subHeight
,
1579 GLsizei subDepth
, const char *func
)
1581 const GLenum target
= destImage
->TexObject
->Target
;
1586 _mesa_error(ctx
, GL_INVALID_VALUE
,
1587 "%s%dD(width=%d)", func
, dims
, subWidth
);
1591 if (dims
> 1 && subHeight
< 0) {
1592 _mesa_error(ctx
, GL_INVALID_VALUE
,
1593 "%s%dD(height=%d)", func
, dims
, subHeight
);
1597 if (dims
> 2 && subDepth
< 0) {
1598 _mesa_error(ctx
, GL_INVALID_VALUE
,
1599 "%s%dD(depth=%d)", func
, dims
, subDepth
);
1603 /* check xoffset and width */
1604 if (xoffset
< - (GLint
) destImage
->Border
) {
1605 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset)",
1610 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1611 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset+width)",
1616 /* check yoffset and height */
1618 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1619 if (yoffset
< -yBorder
) {
1620 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset)",
1624 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1625 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset+height)",
1631 /* check zoffset and depth */
1634 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1635 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1636 0 : destImage
->Border
;
1638 if (zoffset
< -zBorder
) {
1639 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset)", func
);
1643 depth
= (GLint
) destImage
->Depth
;
1644 if (target
== GL_TEXTURE_CUBE_MAP
)
1646 if (zoffset
+ subDepth
> depth
) {
1647 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset+depth)", func
);
1653 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1654 * compressed texture images can be updated. But, that restriction may be
1655 * relaxed for particular compressed formats. At this time, all the
1656 * compressed formats supported by Mesa allow sub-textures to be updated
1657 * along compressed block boundaries.
1659 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1661 if (bw
!= 1 || bh
!= 1) {
1662 /* offset must be multiple of block size */
1663 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1664 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1665 "%s%dD(xoffset = %d, yoffset = %d)",
1666 func
, dims
, xoffset
, yoffset
);
1670 /* The size must be a multiple of bw x bh, or we must be using a
1671 * offset+size that exactly hits the edge of the image. This
1672 * is important for small mipmap levels (1x1, 2x1, etc) and for
1675 if ((subWidth
% bw
!= 0) &&
1676 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1677 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1678 "%s%dD(width = %d)", func
, dims
, subWidth
);
1682 if ((subHeight
% bh
!= 0) &&
1683 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1684 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1685 "%s%dD(height = %d)", func
, dims
, subHeight
);
1697 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1698 * specific texture image size checks.
1700 * A hardware driver might override this function if, for example, the
1701 * max 3D texture size is 512x512x64 (i.e. not a cube).
1703 * Note that width, height, depth == 0 is not an error. However, a
1704 * texture with zero width/height/depth will be considered "incomplete"
1705 * and texturing will effectively be disabled.
1707 * \param target any texture target/type
1708 * \param level as passed to glTexImage
1709 * \param format the MESA_FORMAT_x for the tex image
1710 * \param width as passed to glTexImage
1711 * \param height as passed to glTexImage
1712 * \param depth as passed to glTexImage
1713 * \param border as passed to glTexImage
1714 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1717 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1719 GLint width
, GLint height
, GLint depth
, GLint border
)
1721 /* We just check if the image size is less than MaxTextureMbytes.
1722 * Some drivers may do more specific checks.
1724 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1725 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1726 mbytes
*= _mesa_num_tex_faces(target
);
1727 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1732 * Return true if the format is only valid for glCompressedTexImage.
1735 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1738 case GL_ETC1_RGB8_OES
:
1739 case GL_PALETTE4_RGB8_OES
:
1740 case GL_PALETTE4_RGBA8_OES
:
1741 case GL_PALETTE4_R5_G6_B5_OES
:
1742 case GL_PALETTE4_RGBA4_OES
:
1743 case GL_PALETTE4_RGB5_A1_OES
:
1744 case GL_PALETTE8_RGB8_OES
:
1745 case GL_PALETTE8_RGBA8_OES
:
1746 case GL_PALETTE8_R5_G6_B5_OES
:
1747 case GL_PALETTE8_RGBA4_OES
:
1748 case GL_PALETTE8_RGB5_A1_OES
:
1757 * Helper function to determine whether a target and specific compression
1758 * format are supported.
1761 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1764 (void) intFormat
; /* not used yet */
1768 case GL_PROXY_TEXTURE_2D
:
1769 return GL_TRUE
; /* true for any compressed format so far */
1770 case GL_PROXY_TEXTURE_CUBE_MAP
:
1771 case GL_TEXTURE_CUBE_MAP
:
1772 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1773 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1774 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1775 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1776 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1777 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1778 return ctx
->Extensions
.ARB_texture_cube_map
;
1779 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1780 case GL_TEXTURE_2D_ARRAY_EXT
:
1781 return ctx
->Extensions
.EXT_texture_array
;
1782 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1783 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1784 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1792 * Check if the given texture target value is legal for a
1793 * glTexImage1/2/3D call.
1796 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1802 case GL_PROXY_TEXTURE_1D
:
1803 return _mesa_is_desktop_gl(ctx
);
1811 case GL_PROXY_TEXTURE_2D
:
1812 return _mesa_is_desktop_gl(ctx
);
1813 case GL_PROXY_TEXTURE_CUBE_MAP
:
1814 return _mesa_is_desktop_gl(ctx
)
1815 && ctx
->Extensions
.ARB_texture_cube_map
;
1816 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1817 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1818 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1819 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1820 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1821 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1822 return ctx
->Extensions
.ARB_texture_cube_map
;
1823 case GL_TEXTURE_RECTANGLE_NV
:
1824 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1825 return _mesa_is_desktop_gl(ctx
)
1826 && ctx
->Extensions
.NV_texture_rectangle
;
1827 case GL_TEXTURE_1D_ARRAY_EXT
:
1828 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1829 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1837 case GL_PROXY_TEXTURE_3D
:
1838 return _mesa_is_desktop_gl(ctx
);
1839 case GL_TEXTURE_2D_ARRAY_EXT
:
1840 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1841 || _mesa_is_gles3(ctx
);
1842 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1843 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1844 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1845 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1846 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1851 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1858 * Check if the given texture target value is legal for a
1859 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1860 * The difference compared to legal_teximage_target() above is that
1861 * proxy targets are not supported.
1864 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1869 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1874 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1875 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1876 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1877 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1878 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1879 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1880 return ctx
->Extensions
.ARB_texture_cube_map
;
1881 case GL_TEXTURE_RECTANGLE_NV
:
1882 return _mesa_is_desktop_gl(ctx
)
1883 && ctx
->Extensions
.NV_texture_rectangle
;
1884 case GL_TEXTURE_1D_ARRAY_EXT
:
1885 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1893 case GL_TEXTURE_2D_ARRAY_EXT
:
1894 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1895 || _mesa_is_gles3(ctx
);
1896 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1897 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1898 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1900 /* Table 8.15 of the OpenGL 4.5 core profile spec
1901 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1902 * and CopyTextureSubImage3D.
1904 case GL_TEXTURE_CUBE_MAP
:
1910 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1918 * Helper function to determine if a texture object is mutable (in terms
1919 * of GL_ARB_texture_storage).
1922 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1924 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1928 return !texObj
->Immutable
;
1933 * Return expected size of a compressed texture.
1936 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1939 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1940 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1944 * Verify that a texture format is valid with a particular target
1946 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1947 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1950 * \param ctx GL context
1951 * \param target Texture target
1952 * \param internalFormat Internal format of the texture image
1953 * \param dimensions Dimensionality at the caller. This is \b not used
1954 * in the validation. It is only used when logging
1956 * \param caller Base name of the calling function (e.g.,
1957 * "glTexImage" or "glTexStorage").
1959 * \returns true if the combination is legal, false otherwise.
1962 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1963 GLenum target
, GLenum internalFormat
,
1964 unsigned dimensions
,
1967 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1968 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
) {
1969 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1970 * Profile spec says:
1972 * "Textures with a base internal format of DEPTH_COMPONENT or
1973 * DEPTH_STENCIL are supported by texture image specification
1974 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1975 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1976 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1977 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1978 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1979 * formats in conjunction with any other target will result in an
1980 * INVALID_OPERATION error."
1982 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1983 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1985 if (target
!= GL_TEXTURE_1D
&&
1986 target
!= GL_PROXY_TEXTURE_1D
&&
1987 target
!= GL_TEXTURE_2D
&&
1988 target
!= GL_PROXY_TEXTURE_2D
&&
1989 target
!= GL_TEXTURE_1D_ARRAY
&&
1990 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1991 target
!= GL_TEXTURE_2D_ARRAY
&&
1992 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1993 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1994 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1995 !((_mesa_is_cube_face(target
) ||
1996 target
== GL_TEXTURE_CUBE_MAP
||
1997 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1998 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1999 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
2000 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
2001 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
2002 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
2003 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2004 "%s%dD(bad target for depth texture)",
2005 caller
, dimensions
);
2014 texture_formats_agree(GLenum internalFormat
,
2017 GLboolean colorFormat
;
2018 GLboolean is_format_depth_or_depthstencil
;
2019 GLboolean is_internalFormat_depth_or_depthstencil
;
2021 /* Even though there are no color-index textures, we still have to support
2022 * uploading color-index data and remapping it to RGB via the
2023 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
2025 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
2027 is_internalFormat_depth_or_depthstencil
=
2028 _mesa_is_depth_format(internalFormat
) ||
2029 _mesa_is_depthstencil_format(internalFormat
);
2031 is_format_depth_or_depthstencil
=
2032 _mesa_is_depth_format(format
) ||
2033 _mesa_is_depthstencil_format(format
);
2035 colorFormat
= _mesa_is_color_format(format
);
2037 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
2040 if (is_internalFormat_depth_or_depthstencil
!=
2041 is_format_depth_or_depthstencil
)
2044 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
2051 * Test the glTexImage[123]D() parameters for errors.
2053 * \param ctx GL context.
2054 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2055 * \param target texture target given by the user (already validated).
2056 * \param level image level given by the user.
2057 * \param internalFormat internal format given by the user.
2058 * \param format pixel data format given by the user.
2059 * \param type pixel data type given by the user.
2060 * \param width image width given by the user.
2061 * \param height image height given by the user.
2062 * \param depth image depth given by the user.
2063 * \param border image border given by the user.
2065 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2067 * Verifies each of the parameters against the constants specified in
2068 * __struct gl_contextRec::Const and the supported extensions, and according
2069 * to the OpenGL specification.
2070 * Note that we don't fully error-check the width, height, depth values
2071 * here. That's done in _mesa_legal_texture_dimensions() which is used
2072 * by several other GL entrypoints. Plus, texture dims have a special
2073 * interaction with proxy textures.
2076 texture_error_check( struct gl_context
*ctx
,
2077 GLuint dimensions
, GLenum target
,
2078 GLint level
, GLint internalFormat
,
2079 GLenum format
, GLenum type
,
2080 GLint width
, GLint height
,
2081 GLint depth
, GLint border
)
2085 /* Note: for proxy textures, some error conditions immediately generate
2086 * a GL error in the usual way. But others do not generate a GL error.
2087 * Instead, they cause the width, height, depth, format fields of the
2088 * texture image to be zeroed-out. The GL spec seems to indicate that the
2089 * zero-out behaviour is only used in cases related to memory allocation.
2093 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2094 _mesa_error(ctx
, GL_INVALID_VALUE
,
2095 "glTexImage%dD(level=%d)", dimensions
, level
);
2100 if (border
< 0 || border
> 1 ||
2101 ((ctx
->API
!= API_OPENGL_COMPAT
||
2102 target
== GL_TEXTURE_RECTANGLE_NV
||
2103 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2104 _mesa_error(ctx
, GL_INVALID_VALUE
,
2105 "glTexImage%dD(border=%d)", dimensions
, border
);
2109 if (width
< 0 || height
< 0 || depth
< 0) {
2110 _mesa_error(ctx
, GL_INVALID_VALUE
,
2111 "glTexImage%dD(width, height or depth < 0)", dimensions
);
2115 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2116 * combinations of format, internalFormat, and type that can be used.
2117 * Formats and types that require additional extensions (e.g., GL_FLOAT
2118 * requires GL_OES_texture_float) are filtered elsewhere.
2121 if (_mesa_is_gles(ctx
)) {
2122 if (_mesa_is_gles3(ctx
)) {
2123 err
= _mesa_es3_error_check_format_and_type(format
, type
,
2126 if (format
!= internalFormat
) {
2127 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2128 "glTexImage%dD(format = %s, internalFormat = %s)",
2130 _mesa_lookup_enum_by_nr(format
),
2131 _mesa_lookup_enum_by_nr(internalFormat
));
2135 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2137 if (err
!= GL_NO_ERROR
) {
2138 _mesa_error(ctx
, err
,
2139 "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
2141 _mesa_lookup_enum_by_nr(format
),
2142 _mesa_lookup_enum_by_nr(type
),
2143 _mesa_lookup_enum_by_nr(internalFormat
));
2148 /* Check internalFormat */
2149 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2150 _mesa_error(ctx
, GL_INVALID_VALUE
,
2151 "glTexImage%dD(internalFormat=%s)",
2152 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2156 /* Check incoming image format and type */
2157 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2158 if (err
!= GL_NO_ERROR
) {
2159 _mesa_error(ctx
, err
,
2160 "glTexImage%dD(incompatible format = %s, type = %s)",
2161 dimensions
, _mesa_lookup_enum_by_nr(format
),
2162 _mesa_lookup_enum_by_nr(type
));
2166 /* make sure internal format and format basically agree */
2167 if (!texture_formats_agree(internalFormat
, format
)) {
2168 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2169 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
2170 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
),
2171 _mesa_lookup_enum_by_nr(format
));
2175 /* additional checks for ycbcr textures */
2176 if (internalFormat
== GL_YCBCR_MESA
) {
2177 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
2178 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
2179 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
2181 _mesa_snprintf(message
, sizeof(message
),
2182 "glTexImage%dD(format/type YCBCR mismatch)",
2184 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
2185 return GL_TRUE
; /* error */
2187 if (target
!= GL_TEXTURE_2D
&&
2188 target
!= GL_PROXY_TEXTURE_2D
&&
2189 target
!= GL_TEXTURE_RECTANGLE_NV
&&
2190 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
2191 _mesa_error(ctx
, GL_INVALID_ENUM
,
2192 "glTexImage%dD(bad target for YCbCr texture)",
2198 _mesa_snprintf(message
, sizeof(message
),
2199 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2200 dimensions
, border
);
2201 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
2206 /* additional checks for depth textures */
2207 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
,
2208 dimensions
, "glTexImage"))
2211 /* additional checks for compressed textures */
2212 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2213 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2214 _mesa_error(ctx
, GL_INVALID_ENUM
,
2215 "glTexImage%dD(target can't be compressed)", dimensions
);
2218 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2219 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2220 "glTexImage%dD(no compression for format)", dimensions
);
2224 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2225 "glTexImage%dD(border!=0)", dimensions
);
2230 /* additional checks for integer textures */
2231 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
2232 (_mesa_is_enum_format_integer(format
) !=
2233 _mesa_is_enum_format_integer(internalFormat
))) {
2234 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2235 "glTexImage%dD(integer/non-integer format mismatch)",
2240 if (!mutable_tex_object(ctx
, target
)) {
2241 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2242 "glTexImage%dD(immutable texture)", dimensions
);
2246 /* if we get here, the parameters are OK */
2252 * Error checking for glCompressedTexImage[123]D().
2253 * Note that the width, height and depth values are not fully error checked
2255 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2258 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
2259 GLenum target
, GLint level
,
2260 GLenum internalFormat
, GLsizei width
,
2261 GLsizei height
, GLsizei depth
, GLint border
,
2264 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
2266 GLenum error
= GL_NO_ERROR
;
2267 char *reason
= ""; /* no error */
2269 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2271 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
2273 * "The ETC2/EAC texture compression algorithm supports only
2274 * two-dimensional images. If internalformat is an ETC2/EAC format,
2275 * CompressedTexImage3D will generate an INVALID_OPERATION error if
2276 * target is not TEXTURE_2D_ARRAY."
2278 error
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
2282 /* This will detect any invalid internalFormat value */
2283 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
2284 _mesa_error(ctx
, GL_INVALID_ENUM
,
2285 "glCompressedTexImage%dD(internalFormat=%s)",
2286 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2290 switch (internalFormat
) {
2291 case GL_PALETTE4_RGB8_OES
:
2292 case GL_PALETTE4_RGBA8_OES
:
2293 case GL_PALETTE4_R5_G6_B5_OES
:
2294 case GL_PALETTE4_RGBA4_OES
:
2295 case GL_PALETTE4_RGB5_A1_OES
:
2296 case GL_PALETTE8_RGB8_OES
:
2297 case GL_PALETTE8_RGBA8_OES
:
2298 case GL_PALETTE8_R5_G6_B5_OES
:
2299 case GL_PALETTE8_RGBA4_OES
:
2300 case GL_PALETTE8_RGB5_A1_OES
:
2301 /* check level (note that level should be zero or less!) */
2302 if (level
> 0 || level
< -maxLevels
) {
2304 error
= GL_INVALID_VALUE
;
2308 if (dimensions
!= 2) {
2309 reason
= "compressed paletted textures must be 2D";
2310 error
= GL_INVALID_OPERATION
;
2314 /* Figure out the expected texture size (in bytes). This will be
2315 * checked against the actual size later.
2317 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
2320 /* This is for the benefit of the TestProxyTexImage below. It expects
2321 * level to be non-negative. OES_compressed_paletted_texture uses a
2322 * weird mechanism where the level specified to glCompressedTexImage2D
2323 * is -(n-1) number of levels in the texture, and the data specifies the
2324 * complete mipmap stack. This is done to ensure the palette is the
2325 * same for all levels.
2332 if (level
< 0 || level
>= maxLevels
) {
2334 error
= GL_INVALID_VALUE
;
2338 /* Figure out the expected texture size (in bytes). This will be
2339 * checked against the actual size later.
2341 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2345 /* This should really never fail */
2346 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2347 reason
= "internalFormat";
2348 error
= GL_INVALID_ENUM
;
2352 /* No compressed formats support borders at this time */
2354 reason
= "border != 0";
2355 error
= GL_INVALID_VALUE
;
2359 /* Check for invalid pixel storage modes */
2360 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2362 "glCompressedTexImage")) {
2366 /* check image size in bytes */
2367 if (expectedSize
!= imageSize
) {
2368 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2369 * if <imageSize> is not consistent with the format, dimensions, and
2370 * contents of the specified image.
2372 reason
= "imageSize inconsistant with width/height/format";
2373 error
= GL_INVALID_VALUE
;
2377 if (!mutable_tex_object(ctx
, target
)) {
2378 reason
= "immutable texture";
2379 error
= GL_INVALID_OPERATION
;
2386 /* Note: not all error paths exit through here. */
2387 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2388 dimensions
, reason
);
2395 * Test glTexSubImage[123]D() parameters for errors.
2397 * \param ctx GL context.
2398 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2399 * \param target texture target given by the user (already validated)
2400 * \param level image level given by the user.
2401 * \param xoffset sub-image x offset given by the user.
2402 * \param yoffset sub-image y offset given by the user.
2403 * \param zoffset sub-image z offset given by the user.
2404 * \param format pixel data format given by the user.
2405 * \param type pixel data type given by the user.
2406 * \param width image width given by the user.
2407 * \param height image height given by the user.
2408 * \param depth image depth given by the user.
2410 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2412 * Verifies each of the parameters against the constants specified in
2413 * __struct gl_contextRec::Const and the supported extensions, and according
2414 * to the OpenGL specification.
2417 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2418 struct gl_texture_object
*texObj
,
2419 GLenum target
, GLint level
,
2420 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2421 GLint width
, GLint height
, GLint depth
,
2422 GLenum format
, GLenum type
, bool dsa
)
2424 struct gl_texture_image
*texImage
;
2426 const char* suffix
= dsa
? "ture" : "";
2429 /* must be out of memory */
2430 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTex%sSubImage%dD()",
2431 suffix
, dimensions
);
2435 /* check target (proxies not allowed) */
2436 if (!legal_texsubimage_target(ctx
, dimensions
, target
, dsa
)) {
2437 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTex%sSubImage%uD(target=%s)",
2438 suffix
, dimensions
, _mesa_lookup_enum_by_nr(target
));
2443 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2444 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTex%sSubImage%uD(level=%d)",
2445 suffix
, dimensions
, level
);
2449 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2450 * combinations of format and type that can be used. Formats and types
2451 * that require additional extensions (e.g., GL_FLOAT requires
2452 * GL_OES_texture_float) are filtered elsewhere.
2454 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2455 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2456 if (err
!= GL_NO_ERROR
) {
2457 _mesa_error(ctx
, err
,
2458 "glTex%sSubImage%dD(format = %s, type = %s)",
2459 suffix
, dimensions
, _mesa_lookup_enum_by_nr(format
),
2460 _mesa_lookup_enum_by_nr(type
));
2465 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2466 if (err
!= GL_NO_ERROR
) {
2467 _mesa_error(ctx
, err
,
2468 "glTex%sSubImage%dD(incompatible format = %s, type = %s)",
2469 suffix
, dimensions
, _mesa_lookup_enum_by_nr(format
),
2470 _mesa_lookup_enum_by_nr(type
));
2474 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2476 /* non-existant texture level */
2477 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2478 "glTex%sSubImage%dD(invalid texture image)", suffix
,
2483 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2484 texImage
, xoffset
, yoffset
, 0,
2486 dsa
? "glTextureSubImage" :
2491 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2492 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2493 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2494 "glTex%sSubImage%dD(no compression for format)",
2495 suffix
, dimensions
);
2500 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2501 /* both source and dest must be integer-valued, or neither */
2502 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2503 _mesa_is_enum_format_integer(format
)) {
2504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2505 "glTex%sSubImage%dD(integer/non-integer format mismatch)",
2506 suffix
, dimensions
);
2516 * Test glCopyTexImage[12]D() parameters for errors.
2518 * \param ctx GL context.
2519 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2520 * \param target texture target given by the user.
2521 * \param level image level given by the user.
2522 * \param internalFormat internal format given by the user.
2523 * \param width image width given by the user.
2524 * \param height image height given by the user.
2525 * \param border texture border.
2527 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2529 * Verifies each of the parameters against the constants specified in
2530 * __struct gl_contextRec::Const and the supported extensions, and according
2531 * to the OpenGL specification.
2534 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2535 GLenum target
, GLint level
, GLint internalFormat
,
2536 GLint width
, GLint height
, GLint border
)
2539 GLint rb_base_format
;
2540 struct gl_renderbuffer
*rb
;
2541 GLenum rb_internal_format
;
2544 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2545 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2546 dimensions
, _mesa_lookup_enum_by_nr(target
));
2551 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2552 _mesa_error(ctx
, GL_INVALID_VALUE
,
2553 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2557 /* Check that the source buffer is complete */
2558 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2559 if (ctx
->ReadBuffer
->_Status
== 0) {
2560 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2562 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2563 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2564 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2568 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2569 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2570 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2576 if (border
< 0 || border
> 1 ||
2577 ((ctx
->API
!= API_OPENGL_COMPAT
||
2578 target
== GL_TEXTURE_RECTANGLE_NV
||
2579 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2580 _mesa_error(ctx
, GL_INVALID_VALUE
,
2581 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2585 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2587 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2588 "glCopyTexImage%dD(read buffer)", dimensions
);
2592 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2595 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2596 switch (internalFormat
) {
2601 case GL_LUMINANCE_ALPHA
:
2604 _mesa_error(ctx
, GL_INVALID_VALUE
,
2605 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2606 _mesa_lookup_enum_by_nr(internalFormat
));
2611 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2612 if (baseFormat
< 0) {
2613 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2614 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2615 _mesa_lookup_enum_by_nr(internalFormat
));
2619 rb_internal_format
= rb
->InternalFormat
;
2620 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2621 if (_mesa_is_color_format(internalFormat
)) {
2622 if (rb_base_format
< 0) {
2623 _mesa_error(ctx
, GL_INVALID_VALUE
,
2624 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2625 _mesa_lookup_enum_by_nr(internalFormat
));
2630 if (_mesa_is_gles(ctx
)) {
2632 if (_mesa_base_format_component_count(baseFormat
) >
2633 _mesa_base_format_component_count(rb_base_format
)) {
2636 if (baseFormat
== GL_DEPTH_COMPONENT
||
2637 baseFormat
== GL_DEPTH_STENCIL
||
2638 rb_base_format
== GL_DEPTH_COMPONENT
||
2639 rb_base_format
== GL_DEPTH_STENCIL
||
2640 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2641 baseFormat
== GL_ALPHA
) &&
2642 rb_base_format
!= GL_RGBA
) ||
2643 internalFormat
== GL_RGB9_E5
) {
2646 if (internalFormat
== GL_RGB9_E5
) {
2650 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2651 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2652 _mesa_lookup_enum_by_nr(internalFormat
));
2657 if (_mesa_is_gles3(ctx
)) {
2658 bool rb_is_srgb
= false;
2659 bool dst_is_srgb
= false;
2661 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2662 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2666 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2670 if (rb_is_srgb
!= dst_is_srgb
) {
2671 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2672 * OpenGLES 3.0.0 spec says:
2674 * "The error INVALID_OPERATION is also generated if the
2675 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2676 * framebuffer attachment corresponding to the read buffer
2677 * is LINEAR (see section 6.1.13) and internalformat is
2678 * one of the sRGB formats described in section 3.8.16, or
2679 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2680 * SRGB and internalformat is not one of the sRGB formats."
2682 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2683 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2687 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2688 * types for SNORM formats. Also, conversion to SNORM formats is not
2689 * allowed by Table 3.2 on Page 110.
2691 if(_mesa_is_enum_format_snorm(internalFormat
)) {
2692 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2693 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2694 _mesa_lookup_enum_by_nr(internalFormat
));
2699 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2700 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2701 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2705 /* From the EXT_texture_integer spec:
2707 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2708 * if the texture internalformat is an integer format and the read color
2709 * buffer is not an integer format, or if the internalformat is not an
2710 * integer format and the read color buffer is an integer format."
2712 if (_mesa_is_color_format(internalFormat
)) {
2713 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2714 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2715 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2716 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2717 if (is_int
|| is_rbint
) {
2718 if (is_int
!= is_rbint
) {
2719 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2720 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2722 } else if (_mesa_is_gles(ctx
) &&
2723 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2724 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2725 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2726 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2731 /* From page 138 of OpenGL ES 3.0 spec:
2732 * "The error INVALID_OPERATION is generated if floating-point RGBA
2733 * data is required; if signed integer RGBA data is required and the
2734 * format of the current color buffer is not signed integer; if
2735 * unsigned integer RGBA data is required and the format of the
2736 * current color buffer is not unsigned integer; or if fixed-point
2737 * RGBA data is required and the format of the current color buffer
2738 * is not fixed-point.
2740 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2741 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2742 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2745 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2746 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2747 _mesa_error(ctx
, GL_INVALID_ENUM
,
2748 "glCopyTexImage%dD(target)", dimensions
);
2751 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2753 "glCopyTexImage%dD(no compression for format)", dimensions
);
2757 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2758 "glCopyTexImage%dD(border!=0)", dimensions
);
2763 if (!mutable_tex_object(ctx
, target
)) {
2764 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2765 "glCopyTexImage%dD(immutable texture)", dimensions
);
2769 /* if we get here, the parameters are OK */
2775 * Test glCopyTexSubImage[12]D() parameters for errors.
2776 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2779 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2780 const struct gl_texture_object
*texObj
,
2781 GLenum target
, GLint level
,
2782 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2783 GLint width
, GLint height
, bool dsa
)
2785 struct gl_texture_image
*texImage
;
2786 const char *suffix
= dsa
? "ture" : "";
2788 /* Check that the source buffer is complete */
2789 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2790 if (ctx
->ReadBuffer
->_Status
== 0) {
2791 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2793 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2794 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2795 "glCopyTex%sSubImage%dD(invalid readbuffer)",
2796 suffix
, dimensions
);
2800 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2801 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2802 "glCopyTex%sSubImage%dD(multisample FBO)", suffix
,
2808 /* check target (proxies not allowed) */
2809 if (!legal_texsubimage_target(ctx
, dimensions
, target
, dsa
)) {
2810 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTex%sSubImage%uD(target=%s)",
2812 _mesa_lookup_enum_by_nr(target
));
2817 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2818 _mesa_error(ctx
, GL_INVALID_VALUE
,
2819 "glCopyTex%sSubImage%dD(level=%d)", suffix
,
2824 /* Get dest image pointers */
2826 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTex%sSubImage%dD()",
2827 suffix
, dimensions
);
2831 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2833 /* destination image does not exist */
2834 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2835 "glCopyTex%sSubImage%dD(invalid texture image)",
2836 suffix
, dimensions
);
2840 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2841 xoffset
, yoffset
, zoffset
,
2842 width
, height
, 1, dsa
?
2843 "glCompressedTextureSubImage" :
2844 "glCompressedTexSubImage")) {
2848 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2849 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2850 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2851 "glCopyTex%sSubImage%dD(no compression for format)",
2852 suffix
, dimensions
);
2857 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2858 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTex%sSubImage2D", suffix
);
2862 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2864 "glCopyTex%sSubImage%dD(missing readbuffer, format=0x%x)",
2865 suffix
, dimensions
, texImage
->_BaseFormat
);
2869 /* From the EXT_texture_integer spec:
2871 * "INVALID_OPERATION is generated by CopyTexImage* and
2872 * CopyTexSubImage* if the texture internalformat is an integer format
2873 * and the read color buffer is not an integer format, or if the
2874 * internalformat is not an integer format and the read color buffer
2875 * is an integer format."
2877 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2878 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2880 if (_mesa_is_format_integer_color(rb
->Format
) !=
2881 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2882 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2883 "glCopyTex%sSubImage%dD(integer vs non-integer)",
2884 suffix
, dimensions
);
2889 /* if we get here, the parameters are OK */
2894 /** Callback info for walking over FBO hash table */
2897 struct gl_context
*ctx
;
2898 struct gl_texture_object
*texObj
;
2904 * Check render to texture callback. Called from _mesa_HashWalk().
2907 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2909 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2910 const struct cb_info
*info
= (struct cb_info
*) userData
;
2911 struct gl_context
*ctx
= info
->ctx
;
2912 const struct gl_texture_object
*texObj
= info
->texObj
;
2913 const GLuint level
= info
->level
, face
= info
->face
;
2915 /* If this is a user-created FBO */
2916 if (_mesa_is_user_fbo(fb
)) {
2918 /* check if any of the FBO's attachments point to 'texObj' */
2919 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2920 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2921 if (att
->Type
== GL_TEXTURE
&&
2922 att
->Texture
== texObj
&&
2923 att
->TextureLevel
== level
&&
2924 att
->CubeMapFace
== face
) {
2925 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2926 ASSERT(att
->Renderbuffer
->TexImage
);
2927 /* Mark fb status as indeterminate to force re-validation */
2936 * When a texture image is specified we have to check if it's bound to
2937 * any framebuffer objects (render to texture) in order to detect changes
2938 * in size or format since that effects FBO completeness.
2939 * Any FBOs rendering into the texture must be re-validated.
2942 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2943 struct gl_texture_object
*texObj
,
2944 GLuint face
, GLuint level
)
2946 /* Only check this texture if it's been marked as RenderToTexture */
2947 if (texObj
->_RenderToTexture
) {
2948 struct cb_info info
;
2950 info
.texObj
= texObj
;
2953 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2959 * If the texture object's GenerateMipmap flag is set and we've
2960 * changed the texture base level image, regenerate the rest of the
2961 * mipmap levels now.
2964 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2965 struct gl_texture_object
*texObj
, GLint level
)
2967 if (texObj
->GenerateMipmap
&&
2968 level
== texObj
->BaseLevel
&&
2969 level
< texObj
->MaxLevel
) {
2970 ASSERT(ctx
->Driver
.GenerateMipmap
);
2971 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2976 /** Debug helper: override the user-requested internal format */
2978 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2981 if (internalFormat
== GL_RGBA16F_ARB
||
2982 internalFormat
== GL_RGBA32F_ARB
) {
2983 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2986 else if (internalFormat
== GL_RGB16F_ARB
||
2987 internalFormat
== GL_RGB32F_ARB
) {
2988 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2991 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2992 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2993 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2994 return GL_LUMINANCE_ALPHA
;
2996 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2997 internalFormat
== GL_LUMINANCE32F_ARB
) {
2998 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2999 return GL_LUMINANCE
;
3001 else if (internalFormat
== GL_ALPHA16F_ARB
||
3002 internalFormat
== GL_ALPHA32F_ARB
) {
3003 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
3007 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
3008 internalFormat = GL_RGBA;
3012 return internalFormat
;
3015 return internalFormat
;
3021 * Choose the actual hardware format for a texture image.
3022 * Try to use the same format as the previous image level when possible.
3023 * Otherwise, ask the driver for the best format.
3024 * It's important to try to choose a consistant format for all levels
3025 * for efficient texture memory layout/allocation. In particular, this
3026 * comes up during automatic mipmap generation.
3029 _mesa_choose_texture_format(struct gl_context
*ctx
,
3030 struct gl_texture_object
*texObj
,
3031 GLenum target
, GLint level
,
3032 GLenum internalFormat
, GLenum format
, GLenum type
)
3036 /* see if we've already chosen a format for the previous level */
3038 struct gl_texture_image
*prevImage
=
3039 _mesa_select_tex_image(texObj
, target
, level
- 1);
3040 /* See if the prev level is defined and has an internal format which
3041 * matches the new internal format.
3044 prevImage
->Width
> 0 &&
3045 prevImage
->InternalFormat
== internalFormat
) {
3046 /* use the same format */
3047 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
3048 return prevImage
->TexFormat
;
3052 /* If the application requested compression to an S3TC format but we don't
3053 * have the DXTn library, force a generic compressed format instead.
3055 if (internalFormat
!= format
&& format
!= GL_NONE
) {
3056 const GLenum before
= internalFormat
;
3058 switch (internalFormat
) {
3059 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
3060 if (!ctx
->Mesa_DXTn
)
3061 internalFormat
= GL_COMPRESSED_RGB
;
3063 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
3064 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
3065 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
3066 if (!ctx
->Mesa_DXTn
)
3067 internalFormat
= GL_COMPRESSED_RGBA
;
3073 if (before
!= internalFormat
) {
3075 "DXT compression requested (%s), "
3076 "but libtxc_dxtn library not installed. Using %s "
3078 _mesa_lookup_enum_by_nr(before
),
3079 _mesa_lookup_enum_by_nr(internalFormat
));
3083 /* choose format from scratch */
3084 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
3086 ASSERT(f
!= MESA_FORMAT_NONE
);
3092 * Adjust pixel unpack params and image dimensions to strip off the
3093 * one-pixel texture border.
3095 * Gallium and intel don't support texture borders. They've seldem been used
3096 * and seldom been implemented correctly anyway.
3098 * \param unpackNew returns the new pixel unpack parameters
3101 strip_texture_border(GLenum target
,
3102 GLint
*width
, GLint
*height
, GLint
*depth
,
3103 const struct gl_pixelstore_attrib
*unpack
,
3104 struct gl_pixelstore_attrib
*unpackNew
)
3110 *unpackNew
= *unpack
;
3112 if (unpackNew
->RowLength
== 0)
3113 unpackNew
->RowLength
= *width
;
3115 if (unpackNew
->ImageHeight
== 0)
3116 unpackNew
->ImageHeight
= *height
;
3118 assert(*width
>= 3);
3119 unpackNew
->SkipPixels
++; /* skip the border */
3120 *width
= *width
- 2; /* reduce the width by two border pixels */
3122 /* The min height of a texture with a border is 3 */
3123 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
3124 unpackNew
->SkipRows
++; /* skip the border */
3125 *height
= *height
- 2; /* reduce the height by two border pixels */
3129 target
!= GL_TEXTURE_2D_ARRAY
&&
3130 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
3131 unpackNew
->SkipImages
++; /* skip the border */
3132 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
3138 * Common code to implement all the glTexImage1D/2D/3D functions
3139 * as well as glCompressedTexImage1D/2D/3D.
3140 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3141 * \param format the user's image format (only used if !compressed)
3142 * \param type the user's image type (only used if !compressed)
3143 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
3146 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
3147 GLenum target
, GLint level
, GLint internalFormat
,
3148 GLsizei width
, GLsizei height
, GLsizei depth
,
3149 GLint border
, GLenum format
, GLenum type
,
3150 GLsizei imageSize
, const GLvoid
*pixels
)
3152 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
3153 struct gl_pixelstore_attrib unpack_no_border
;
3154 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
3155 struct gl_texture_object
*texObj
;
3156 mesa_format texFormat
;
3157 GLboolean dimensionsOK
, sizeOK
;
3159 FLUSH_VERTICES(ctx
, 0);
3161 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
3164 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3166 _mesa_lookup_enum_by_nr(target
), level
,
3167 _mesa_lookup_enum_by_nr(internalFormat
),
3168 width
, height
, depth
, border
, pixels
);
3171 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3173 _mesa_lookup_enum_by_nr(target
), level
,
3174 _mesa_lookup_enum_by_nr(internalFormat
),
3175 width
, height
, depth
, border
,
3176 _mesa_lookup_enum_by_nr(format
),
3177 _mesa_lookup_enum_by_nr(type
), pixels
);
3180 internalFormat
= override_internal_format(internalFormat
, width
, height
);
3182 /* target error checking */
3183 if (!legal_teximage_target(ctx
, dims
, target
)) {
3184 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
3185 func
, dims
, _mesa_lookup_enum_by_nr(target
));
3189 /* general error checking */
3191 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
3193 width
, height
, depth
,
3198 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3199 format
, type
, width
, height
, depth
, border
))
3203 /* Here we convert a cpal compressed image into a regular glTexImage2D
3204 * call by decompressing the texture. If we really want to support cpal
3205 * textures in any driver this would have to be changed.
3207 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
3208 switch (internalFormat
) {
3209 case GL_PALETTE4_RGB8_OES
:
3210 case GL_PALETTE4_RGBA8_OES
:
3211 case GL_PALETTE4_R5_G6_B5_OES
:
3212 case GL_PALETTE4_RGBA4_OES
:
3213 case GL_PALETTE4_RGB5_A1_OES
:
3214 case GL_PALETTE8_RGB8_OES
:
3215 case GL_PALETTE8_RGBA8_OES
:
3216 case GL_PALETTE8_R5_G6_B5_OES
:
3217 case GL_PALETTE8_RGBA4_OES
:
3218 case GL_PALETTE8_RGB5_A1_OES
:
3219 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3220 width
, height
, imageSize
, pixels
);
3225 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3229 /* For glCompressedTexImage() the driver has no choice about the
3230 * texture format since we'll never transcode the user's compressed
3231 * image data. The internalFormat was error checked earlier.
3233 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
3236 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
3237 * internal floating point format for the given base format.
3239 if (_mesa_is_gles(ctx
) && format
== internalFormat
) {
3240 if (type
== GL_FLOAT
) {
3241 texObj
->_IsFloat
= GL_TRUE
;
3242 } else if (type
== GL_HALF_FLOAT_OES
|| type
== GL_HALF_FLOAT
) {
3243 texObj
->_IsHalfFloat
= GL_TRUE
;
3246 internalFormat
= adjust_for_oes_float_texture(format
, type
);
3249 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3250 internalFormat
, format
, type
);
3253 assert(texFormat
!= MESA_FORMAT_NONE
);
3255 /* check that width, height, depth are legal for the mipmap level */
3256 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
3257 height
, depth
, border
);
3259 /* check that the texture won't take too much memory, etc */
3260 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3262 width
, height
, depth
, border
);
3264 if (_mesa_is_proxy_texture(target
)) {
3265 /* Proxy texture: just clear or set state depending on error checking */
3266 struct gl_texture_image
*texImage
=
3267 get_proxy_tex_image(ctx
, target
, level
);
3270 return; /* GL_OUT_OF_MEMORY already recorded */
3272 if (dimensionsOK
&& sizeOK
) {
3273 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3274 border
, internalFormat
, texFormat
);
3277 clear_teximage_fields(texImage
);
3281 /* non-proxy target */
3282 const GLuint face
= _mesa_tex_target_to_face(target
);
3283 struct gl_texture_image
*texImage
;
3285 if (!dimensionsOK
) {
3286 _mesa_error(ctx
, GL_INVALID_VALUE
,
3287 "glTexImage%uD(invalid width or height or depth)",
3293 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3294 "glTexImage%uD(image too large)", dims
);
3298 /* Allow a hardware driver to just strip out the border, to provide
3299 * reliable but slightly incorrect hardware rendering instead of
3300 * rarely-tested software fallback rendering.
3302 if (border
&& ctx
->Const
.StripTextureBorder
) {
3303 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3306 unpack
= &unpack_no_border
;
3309 if (ctx
->NewState
& _NEW_PIXEL
)
3310 _mesa_update_state(ctx
);
3312 _mesa_lock_texture(ctx
, texObj
);
3314 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3317 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3320 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3322 _mesa_init_teximage_fields(ctx
, texImage
,
3323 width
, height
, depth
,
3324 border
, internalFormat
, texFormat
);
3326 /* Give the texture to the driver. <pixels> may be null. */
3327 if (width
> 0 && height
> 0 && depth
> 0) {
3329 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3333 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3334 type
, pixels
, unpack
);
3338 check_gen_mipmap(ctx
, target
, texObj
, level
);
3340 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3342 _mesa_dirty_texobj(ctx
, texObj
);
3345 _mesa_unlock_texture(ctx
, texObj
);
3352 * Called from the API. Note that width includes the border.
3355 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3356 GLsizei width
, GLint border
, GLenum format
,
3357 GLenum type
, const GLvoid
*pixels
)
3359 GET_CURRENT_CONTEXT(ctx
);
3360 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3361 border
, format
, type
, 0, pixels
);
3366 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3367 GLsizei width
, GLsizei height
, GLint border
,
3368 GLenum format
, GLenum type
,
3369 const GLvoid
*pixels
)
3371 GET_CURRENT_CONTEXT(ctx
);
3372 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3373 border
, format
, type
, 0, pixels
);
3378 * Called by the API or display list executor.
3379 * Note that width and height include the border.
3382 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3383 GLsizei width
, GLsizei height
, GLsizei depth
,
3384 GLint border
, GLenum format
, GLenum type
,
3385 const GLvoid
*pixels
)
3387 GET_CURRENT_CONTEXT(ctx
);
3388 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3389 width
, height
, depth
,
3390 border
, format
, type
, 0, pixels
);
3395 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3396 GLsizei width
, GLsizei height
, GLsizei depth
,
3397 GLint border
, GLenum format
, GLenum type
,
3398 const GLvoid
*pixels
)
3400 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3401 depth
, border
, format
, type
, pixels
);
3406 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3408 struct gl_texture_object
*texObj
;
3409 struct gl_texture_image
*texImage
;
3411 GET_CURRENT_CONTEXT(ctx
);
3412 FLUSH_VERTICES(ctx
, 0);
3416 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3418 case GL_TEXTURE_EXTERNAL_OES
:
3420 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3423 valid_target
= false;
3427 if (!valid_target
) {
3428 _mesa_error(ctx
, GL_INVALID_ENUM
,
3429 "glEGLImageTargetTexture2D(target=%d)", target
);
3434 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3435 "glEGLImageTargetTexture2D(image=%p)", image
);
3439 if (ctx
->NewState
& _NEW_PIXEL
)
3440 _mesa_update_state(ctx
);
3442 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3446 _mesa_lock_texture(ctx
, texObj
);
3448 if (texObj
->Immutable
) {
3449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3450 "glEGLImageTargetTexture2D(texture is immutable)");
3451 _mesa_unlock_texture(ctx
, texObj
);
3455 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3457 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3459 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3461 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3462 texObj
, texImage
, image
);
3464 _mesa_dirty_texobj(ctx
, texObj
);
3466 _mesa_unlock_texture(ctx
, texObj
);
3472 * Helper that implements the glTexSubImage1/2/3D()
3473 * and glTextureSubImage1/2/3D() functions.
3476 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3477 struct gl_texture_object
*texObj
,
3478 struct gl_texture_image
*texImage
,
3479 GLenum target
, GLint level
,
3480 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3481 GLsizei width
, GLsizei height
, GLsizei depth
,
3482 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3485 FLUSH_VERTICES(ctx
, 0);
3487 /* check target (proxies not allowed) */
3488 if (!legal_texsubimage_target(ctx
, dims
, target
, dsa
)) {
3489 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTex%sSubImage%uD(target=%s)",
3491 dims
, _mesa_lookup_enum_by_nr(target
));
3495 if (ctx
->NewState
& _NEW_PIXEL
)
3496 _mesa_update_state(ctx
);
3498 _mesa_lock_texture(ctx
, texObj
);
3500 if (width
> 0 && height
> 0 && depth
> 0) {
3501 /* If we have a border, offset=-1 is legal. Bias by border width. */
3504 if (target
!= GL_TEXTURE_2D_ARRAY
)
3505 zoffset
+= texImage
->Border
;
3508 if (target
!= GL_TEXTURE_1D_ARRAY
)
3509 yoffset
+= texImage
->Border
;
3512 xoffset
+= texImage
->Border
;
3515 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3516 xoffset
, yoffset
, zoffset
,
3517 width
, height
, depth
,
3518 format
, type
, pixels
, &ctx
->Unpack
);
3520 check_gen_mipmap(ctx
, target
, texObj
, level
);
3522 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3523 * the texel data, not the texture format, size, etc.
3527 _mesa_unlock_texture(ctx
, texObj
);
3531 * Implement all the glTexSubImage1/2/3D() functions.
3532 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3535 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3536 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3537 GLsizei width
, GLsizei height
, GLsizei depth
,
3538 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3540 struct gl_texture_object
*texObj
;
3541 struct gl_texture_image
*texImage
;
3543 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3547 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3548 xoffset
, yoffset
, zoffset
,
3549 width
, height
, depth
, format
, type
, false)) {
3550 return; /* error was detected */
3553 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3554 /* texsubimage_error_check ensures that texImage is not NULL */
3556 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3557 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3559 _mesa_lookup_enum_by_nr(target
), level
,
3560 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3561 _mesa_lookup_enum_by_nr(format
),
3562 _mesa_lookup_enum_by_nr(type
), pixels
);
3564 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3565 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3566 format
, type
, pixels
, false);
3571 * Implement all the glTextureSubImage1/2/3D() functions.
3572 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3575 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3576 GLuint texture
, GLint level
,
3577 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3578 GLsizei width
, GLsizei height
, GLsizei depth
,
3579 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3581 struct gl_texture_object
*texObj
;
3582 struct gl_texture_image
*texImage
;
3585 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3587 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3588 dims
, texture
, level
,
3589 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3590 _mesa_lookup_enum_by_nr(format
),
3591 _mesa_lookup_enum_by_nr(type
), pixels
);
3593 /* Get the texture object by Name. */
3594 texObj
= _mesa_lookup_texture(ctx
, texture
);
3596 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3601 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3602 xoffset
, yoffset
, zoffset
,
3603 width
, height
, depth
, format
, type
, true)) {
3604 return; /* error was detected */
3608 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3609 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3612 /* Error checking */
3613 if (texObj
->NumLayers
< 6) {
3614 /* Not enough image planes for a cube map. The spec does not say
3615 * what should happen in this case because the user has always
3616 * specified each cube face separately (using
3617 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
3618 * This is addressed in Khronos Bug 13223.
3620 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3621 "glTextureSubImage%uD(insufficient cube map storage)",
3627 * What do we do if the user created a texture with the following code
3628 * and then called this function with its handle?
3631 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3632 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3633 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3634 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3635 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3636 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3637 * // wrong format, or given the wrong size, etc.
3638 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3639 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3641 * A bug has been filed against the spec for this case. In the
3642 * meantime, we will check for cube completeness.
3644 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3645 * Core Profile spec (30.10.2014):
3646 * "[A] cube map texture is cube complete if the
3647 * following conditions all hold true: The [base level] texture
3648 * images of each of the six cube map faces have identical, positive,
3649 * and square dimensions. The [base level] images were each specified
3650 * with the same internal format."
3652 * It seems reasonable to check for cube completeness of an arbitrary
3653 * level here so that the image data has a consistent format and size.
3655 if (!_mesa_cube_level_complete(texObj
, level
)) {
3656 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3657 "glTextureSubImage%uD(cube map incomplete)",
3662 rowStride
= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3664 /* Copy in each face. */
3665 for (i
= 0; i
< 6; ++i
) {
3666 texImage
= texObj
->Image
[i
][level
];
3667 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3668 level
, xoffset
, yoffset
, zoffset
,
3669 width
, height
, 1, format
,
3670 type
, pixels
, true);
3671 pixels
= (GLubyte
*) pixels
+ rowStride
;
3675 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3679 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3680 level
, xoffset
, yoffset
, zoffset
,
3681 width
, height
, depth
, format
,
3682 type
, pixels
, true);
3688 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3689 GLint xoffset
, GLsizei width
,
3690 GLenum format
, GLenum type
,
3691 const GLvoid
*pixels
)
3693 GET_CURRENT_CONTEXT(ctx
);
3694 texsubimage(ctx
, 1, target
, level
,
3697 format
, type
, pixels
);
3702 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3703 GLint xoffset
, GLint yoffset
,
3704 GLsizei width
, GLsizei height
,
3705 GLenum format
, GLenum type
,
3706 const GLvoid
*pixels
)
3708 GET_CURRENT_CONTEXT(ctx
);
3709 texsubimage(ctx
, 2, target
, level
,
3710 xoffset
, yoffset
, 0,
3712 format
, type
, pixels
);
3718 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3719 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3720 GLsizei width
, GLsizei height
, GLsizei depth
,
3721 GLenum format
, GLenum type
,
3722 const GLvoid
*pixels
)
3724 GET_CURRENT_CONTEXT(ctx
);
3725 texsubimage(ctx
, 3, target
, level
,
3726 xoffset
, yoffset
, zoffset
,
3727 width
, height
, depth
,
3728 format
, type
, pixels
);
3732 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3733 GLint xoffset
, GLsizei width
,
3734 GLenum format
, GLenum type
,
3735 const GLvoid
*pixels
)
3737 GET_CURRENT_CONTEXT(ctx
);
3738 texturesubimage(ctx
, 1, texture
, level
,
3741 format
, type
, pixels
);
3746 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3747 GLint xoffset
, GLint yoffset
,
3748 GLsizei width
, GLsizei height
,
3749 GLenum format
, GLenum type
,
3750 const GLvoid
*pixels
)
3752 GET_CURRENT_CONTEXT(ctx
);
3753 texturesubimage(ctx
, 2, texture
, level
,
3754 xoffset
, yoffset
, 0,
3756 format
, type
, pixels
);
3761 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3762 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3763 GLsizei width
, GLsizei height
, GLsizei depth
,
3764 GLenum format
, GLenum type
,
3765 const GLvoid
*pixels
)
3767 GET_CURRENT_CONTEXT(ctx
);
3768 texturesubimage(ctx
, 3, texture
, level
,
3769 xoffset
, yoffset
, zoffset
,
3770 width
, height
, depth
,
3771 format
, type
, pixels
);
3776 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3777 * from. This depends on whether the texture contains color or depth values.
3779 static struct gl_renderbuffer
*
3780 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3782 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3783 /* reading from depth/stencil buffer */
3784 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3787 /* copying from color buffer */
3788 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3793 copytexsubimage_by_slice(struct gl_context
*ctx
,
3794 struct gl_texture_image
*texImage
,
3796 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3797 struct gl_renderbuffer
*rb
,
3799 GLsizei width
, GLsizei height
)
3801 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3804 /* For 1D arrays, we copy each scanline of the source rectangle into the
3807 assert(zoffset
== 0);
3809 for (slice
= 0; slice
< height
; slice
++) {
3810 assert(yoffset
+ slice
< texImage
->Height
);
3811 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3812 xoffset
, 0, yoffset
+ slice
,
3813 rb
, x
, y
+ slice
, width
, 1);
3816 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3817 xoffset
, yoffset
, zoffset
,
3818 rb
, x
, y
, width
, height
);
3823 formats_differ_in_component_sizes (mesa_format f1
,
3826 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3827 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3828 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3829 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3831 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3832 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3833 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3834 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3836 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3837 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3838 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3839 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3846 * Implement the glCopyTexImage1/2D() functions.
3849 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3850 GLenum target
, GLint level
, GLenum internalFormat
,
3851 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3853 struct gl_texture_object
*texObj
;
3854 struct gl_texture_image
*texImage
;
3855 const GLuint face
= _mesa_tex_target_to_face(target
);
3856 mesa_format texFormat
;
3857 struct gl_renderbuffer
*rb
;
3859 FLUSH_VERTICES(ctx
, 0);
3861 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3862 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3864 _mesa_lookup_enum_by_nr(target
), level
,
3865 _mesa_lookup_enum_by_nr(internalFormat
),
3866 x
, y
, width
, height
, border
);
3868 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3869 _mesa_update_state(ctx
);
3871 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3872 width
, height
, border
))
3875 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3877 _mesa_error(ctx
, GL_INVALID_VALUE
,
3878 "glCopyTexImage%uD(invalid width or height)", dims
);
3882 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3885 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3886 internalFormat
, GL_NONE
, GL_NONE
);
3888 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3890 if (_mesa_is_gles3(ctx
)) {
3891 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3892 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3893 * OpenGL ES 3.0. Khronos bug# 9807.
3895 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3896 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3897 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer and"
3898 " writing to unsized internal format)", dims
);
3902 /* From Page 139 of OpenGL ES 3.0 spec:
3903 * "If internalformat is sized, the internal format of the new texel
3904 * array is internalformat, and this is also the new texel array’s
3905 * effective internal format. If the component sizes of internalformat
3906 * do not exactly match the corresponding component sizes of the source
3907 * buffer’s effective internal format, described below, an
3908 * INVALID_OPERATION error is generated. If internalformat is unsized,
3909 * the internal format of the new texel array is the effective internal
3910 * format of the source buffer, and this is also the new texel array’s
3911 * effective internal format.
3913 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3914 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3915 "glCopyTexImage%uD(componenet size changed in"
3916 " internal format)", dims
);
3921 assert(texFormat
!= MESA_FORMAT_NONE
);
3923 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3925 width
, height
, 1, border
)) {
3926 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3927 "glCopyTexImage%uD(image too large)", dims
);
3931 if (border
&& ctx
->Const
.StripTextureBorder
) {
3933 width
-= border
* 2;
3936 height
-= border
* 2;
3941 _mesa_lock_texture(ctx
, texObj
);
3943 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3946 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3949 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3951 /* Free old texture image */
3952 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3954 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3955 border
, internalFormat
, texFormat
);
3957 if (width
&& height
) {
3958 /* Allocate texture memory (no pixel data yet) */
3959 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3961 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3963 struct gl_renderbuffer
*srcRb
=
3964 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3966 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3968 srcRb
, srcX
, srcY
, width
, height
);
3971 check_gen_mipmap(ctx
, target
, texObj
, level
);
3974 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3976 _mesa_dirty_texobj(ctx
, texObj
);
3979 _mesa_unlock_texture(ctx
, texObj
);
3985 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3986 GLenum internalFormat
,
3988 GLsizei width
, GLint border
)
3990 GET_CURRENT_CONTEXT(ctx
);
3991 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3997 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3998 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
4001 GET_CURRENT_CONTEXT(ctx
);
4002 copyteximage(ctx
, 2, target
, level
, internalFormat
,
4003 x
, y
, width
, height
, border
);
4007 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
4010 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4011 struct gl_texture_object
*texObj
,
4012 GLenum target
, GLint level
,
4013 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4015 GLsizei width
, GLsizei height
,
4018 struct gl_texture_image
*texImage
;
4020 FLUSH_VERTICES(ctx
, 0);
4022 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
4023 _mesa_debug(ctx
, "glCopyTex%sSubImage%uD %s %d %d %d %d %d %d %d %d\n",
4024 dsa
? "ture" : "", dims
,
4025 _mesa_lookup_enum_by_nr(target
),
4026 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
4028 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
4029 _mesa_update_state(ctx
);
4031 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
4032 xoffset
, yoffset
, zoffset
,
4033 width
, height
, dsa
)) {
4037 _mesa_lock_texture(ctx
, texObj
);
4039 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4041 /* If we have a border, offset=-1 is legal. Bias by border width. */
4044 if (target
!= GL_TEXTURE_2D_ARRAY
)
4045 zoffset
+= texImage
->Border
;
4048 if (target
!= GL_TEXTURE_1D_ARRAY
)
4049 yoffset
+= texImage
->Border
;
4052 xoffset
+= texImage
->Border
;
4055 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
4057 struct gl_renderbuffer
*srcRb
=
4058 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
4060 copytexsubimage_by_slice(ctx
, texImage
, dims
,
4061 xoffset
, yoffset
, zoffset
,
4062 srcRb
, x
, y
, width
, height
);
4064 check_gen_mipmap(ctx
, target
, texObj
, level
);
4066 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4067 * the texel data, not the texture format, size, etc.
4071 _mesa_unlock_texture(ctx
, texObj
);
4075 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
4076 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
4078 struct gl_texture_object
* texObj
;
4079 GET_CURRENT_CONTEXT(ctx
);
4081 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4085 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
4086 x
, y
, width
, 1, false);
4092 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
4093 GLint xoffset
, GLint yoffset
,
4094 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4096 struct gl_texture_object
* texObj
;
4097 GET_CURRENT_CONTEXT(ctx
);
4099 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4103 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
4104 xoffset
, yoffset
, 0,
4105 x
, y
, width
, height
, false);
4111 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
4112 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4113 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4115 struct gl_texture_object
* texObj
;
4116 GET_CURRENT_CONTEXT(ctx
);
4118 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4122 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
4123 xoffset
, yoffset
, zoffset
,
4124 x
, y
, width
, height
, false);
4128 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
4129 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
4131 struct gl_texture_object
* texObj
;
4132 GET_CURRENT_CONTEXT(ctx
);
4134 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage1D");
4138 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
4139 xoffset
, 0, 0, x
, y
, width
, 1, true);
4143 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
4144 GLint xoffset
, GLint yoffset
,
4145 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4147 struct gl_texture_object
* texObj
;
4148 GET_CURRENT_CONTEXT(ctx
);
4150 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage2D");
4154 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
4155 xoffset
, yoffset
, 0,
4156 x
, y
, width
, height
, true);
4162 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
4163 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4164 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4166 struct gl_texture_object
* texObj
;
4167 GET_CURRENT_CONTEXT(ctx
);
4169 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage3D");
4173 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
4174 xoffset
, yoffset
, zoffset
,
4175 x
, y
, width
, height
, true);
4179 check_clear_tex_image(struct gl_context
*ctx
,
4180 const char *function
,
4181 struct gl_texture_image
*texImage
,
4182 GLenum format
, GLenum type
,
4184 GLubyte
*clearValue
)
4186 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4187 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
4188 GLenum internalFormat
= texImage
->InternalFormat
;
4191 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
4192 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4193 "%s(buffer texture)", function
);
4197 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
4198 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4199 "%s(compressed texture)", function
);
4203 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
4204 if (err
!= GL_NO_ERROR
) {
4205 _mesa_error(ctx
, err
,
4206 "%s(incompatible format = %s, type = %s)",
4208 _mesa_lookup_enum_by_nr(format
),
4209 _mesa_lookup_enum_by_nr(type
));
4213 /* make sure internal format and format basically agree */
4214 if (!texture_formats_agree(internalFormat
, format
)) {
4215 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4216 "%s(incompatible internalFormat = %s, format = %s)",
4218 _mesa_lookup_enum_by_nr(internalFormat
),
4219 _mesa_lookup_enum_by_nr(format
));
4223 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
4224 /* both source and dest must be integer-valued, or neither */
4225 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
4226 _mesa_is_enum_format_integer(format
)) {
4227 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4228 "%s(integer/non-integer format mismatch)",
4234 if (!_mesa_texstore(ctx
,
4236 texImage
->_BaseFormat
,
4237 texImage
->TexFormat
,
4238 0, /* dstRowStride */
4240 1, 1, 1, /* srcWidth/Height/Depth */
4242 data
? data
: zeroData
,
4243 &ctx
->DefaultPacking
)) {
4244 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
4251 static struct gl_texture_object
*
4252 get_tex_obj_for_clear(struct gl_context
*ctx
,
4253 const char *function
,
4256 struct gl_texture_object
*texObj
;
4259 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
4263 texObj
= _mesa_lookup_texture(ctx
, texture
);
4265 if (texObj
== NULL
) {
4266 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
4270 if (texObj
->Target
== 0) {
4271 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
4279 get_tex_images_for_clear(struct gl_context
*ctx
,
4280 const char *function
,
4281 struct gl_texture_object
*texObj
,
4283 struct gl_texture_image
**texImages
)
4288 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
4289 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4293 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4294 for (i
= 0; i
< MAX_FACES
; i
++) {
4295 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
4297 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
4298 if (texImages
[i
] == NULL
) {
4299 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4300 "%s(invalid level)", function
);
4308 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4310 if (texImages
[0] == NULL
) {
4311 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4319 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4320 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4321 GLsizei width
, GLsizei height
, GLsizei depth
,
4322 GLenum format
, GLenum type
, const void *data
)
4324 GET_CURRENT_CONTEXT(ctx
);
4325 struct gl_texture_object
*texObj
;
4326 struct gl_texture_image
*texImages
[MAX_FACES
];
4327 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4329 int minDepth
, maxDepth
;
4331 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4336 _mesa_lock_texture(ctx
, texObj
);
4338 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4339 texObj
, level
, texImages
);
4343 if (numImages
== 1) {
4344 minDepth
= -(int) texImages
[0]->Border
;
4345 maxDepth
= texImages
[0]->Depth
;
4348 maxDepth
= numImages
;
4351 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4352 yoffset
< -(GLint
) texImages
[0]->Border
||
4353 zoffset
< minDepth
||
4357 xoffset
+ width
> texImages
[0]->Width
||
4358 yoffset
+ height
> texImages
[0]->Height
||
4359 zoffset
+ depth
> maxDepth
) {
4360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4361 "glClearSubTexImage(invalid dimensions)");
4365 if (numImages
== 1) {
4366 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4368 format
, type
, data
, clearValue
[0])) {
4369 ctx
->Driver
.ClearTexSubImage(ctx
,
4371 xoffset
, yoffset
, zoffset
,
4372 width
, height
, depth
,
4373 data
? clearValue
[0] : NULL
);
4376 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4377 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4379 format
, type
, data
, clearValue
[i
]))
4382 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4383 ctx
->Driver
.ClearTexSubImage(ctx
,
4385 xoffset
, yoffset
, 0,
4387 data
? clearValue
[i
] : NULL
);
4392 _mesa_unlock_texture(ctx
, texObj
);
4396 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4397 GLenum format
, GLenum type
, const void *data
)
4399 GET_CURRENT_CONTEXT(ctx
);
4400 struct gl_texture_object
*texObj
;
4401 struct gl_texture_image
*texImages
[MAX_FACES
];
4402 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4405 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4410 _mesa_lock_texture(ctx
, texObj
);
4412 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4413 texObj
, level
, texImages
);
4415 for (i
= 0; i
< numImages
; i
++) {
4416 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4423 for (i
= 0; i
< numImages
; i
++) {
4424 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4425 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4426 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4427 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4428 texImages
[i
]->Width
,
4429 texImages
[i
]->Height
,
4430 texImages
[i
]->Depth
,
4431 data
? clearValue
[i
] : NULL
);
4435 _mesa_unlock_texture(ctx
, texObj
);
4441 /**********************************************************************/
4442 /****** Compressed Textures ******/
4443 /**********************************************************************/
4447 * Error checking for glCompressedTexSubImage[123]D().
4448 * \return GL_TRUE if error, GL_FALSE if no error
4451 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4452 const struct gl_texture_object
*texObj
,
4453 GLenum target
, GLint level
,
4454 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4455 GLsizei width
, GLsizei height
, GLsizei depth
,
4456 GLenum format
, GLsizei imageSize
, bool dsa
)
4458 struct gl_texture_image
*texImage
;
4461 const char *suffix
= dsa
? "ture" : "";
4463 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4464 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4465 "glCompressedSubTexture%dD(target)", dims
);
4473 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4474 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4475 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4476 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4477 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4478 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4482 targetOK
= GL_FALSE
;
4487 targetOK
= (target
== GL_TEXTURE_3D
) ||
4488 (target
== GL_TEXTURE_2D_ARRAY
) ||
4489 (target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
4490 (target
== GL_TEXTURE_CUBE_MAP
&& dsa
);
4492 /* OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4494 * "An INVALID_OPERATION error is generated by
4495 * CompressedTex*SubImage3D if the internal format of the texture is
4496 * one of the EAC, ETC2, or RGTC formats and either border is
4497 * non-zero, or the effective target for the texture is not
4498 * TEXTURE_2D_ARRAY."
4500 if (target
!= GL_TEXTURE_2D_ARRAY
) {
4503 /* These came from _mesa_is_compressed_format in glformats.c. */
4505 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
4506 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
4507 case GL_COMPRESSED_R11_EAC
:
4508 case GL_COMPRESSED_RG11_EAC
:
4509 case GL_COMPRESSED_SIGNED_R11_EAC
:
4510 case GL_COMPRESSED_SIGNED_RG11_EAC
:
4512 case GL_COMPRESSED_RGB8_ETC2
:
4513 case GL_COMPRESSED_SRGB8_ETC2
:
4514 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
4515 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
4517 case GL_COMPRESSED_RED_RGTC1
:
4518 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
4519 case GL_COMPRESSED_RG_RGTC2
:
4520 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
4521 invalidformat
= true;
4524 invalidformat
= false;
4526 if (invalidformat
) {
4527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4528 "glCompressedTex%sSubImage%uD(target)", suffix
, dims
);
4536 /* no 1D compressed textures at this time */
4537 targetOK
= GL_FALSE
;
4542 _mesa_error(ctx
, GL_INVALID_ENUM
,
4543 "glCompressedTex%sSubImage%uD(target)", suffix
, dims
);
4547 /* this will catch any invalid compressed format token */
4548 if (!_mesa_is_compressed_format(ctx
, format
)) {
4549 _mesa_error(ctx
, GL_INVALID_ENUM
,
4550 "glCompressedTex%sSubImage%uD(format)", suffix
, dims
);
4554 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4555 _mesa_error(ctx
, GL_INVALID_VALUE
,
4556 "glCompressedTex%sSubImage%uD(level=%d)",
4557 suffix
, dims
, level
);
4561 /* Check for invalid pixel storage modes */
4562 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4564 dsa
? "glCompressedTextureSubImage" :
4565 "glCompressedTexSubImage")) {
4569 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4570 if (expectedSize
!= imageSize
) {
4571 _mesa_error(ctx
, GL_INVALID_VALUE
,
4572 "glCompressedTex%sSubImage%uD(size=%d)",
4573 suffix
, dims
, imageSize
);
4577 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4579 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4580 "glCompressedTex%sSubImage%uD(invalid texture image)",
4585 if ((GLint
) format
!= texImage
->InternalFormat
) {
4586 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4587 "glCompressedTex%sSubImage%uD(format=0x%x)",
4588 suffix
, dims
, format
);
4592 if (compressedteximage_only_format(ctx
, format
)) {
4593 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4594 "glCompressedTex%sSubImage%uD(format=0x%x cannot be updated)",
4595 suffix
, dims
, format
);
4599 if (error_check_subtexture_dimensions(ctx
, dims
,
4600 texImage
, xoffset
, yoffset
, zoffset
,
4601 width
, height
, depth
,
4602 "glCompressedTexSubImage")) {
4611 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4612 GLenum internalFormat
, GLsizei width
,
4613 GLint border
, GLsizei imageSize
,
4616 GET_CURRENT_CONTEXT(ctx
);
4617 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4618 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4623 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4624 GLenum internalFormat
, GLsizei width
,
4625 GLsizei height
, GLint border
, GLsizei imageSize
,
4628 GET_CURRENT_CONTEXT(ctx
);
4629 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4630 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4635 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4636 GLenum internalFormat
, GLsizei width
,
4637 GLsizei height
, GLsizei depth
, GLint border
,
4638 GLsizei imageSize
, const GLvoid
*data
)
4640 GET_CURRENT_CONTEXT(ctx
);
4641 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4642 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4647 * Common helper for glCompressedTexSubImage1/2/3D() and
4648 * glCompressedTextureSubImage1/2/3D().
4651 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4652 struct gl_texture_object
*texObj
,
4653 GLenum target
, GLint level
,
4654 GLint xoffset
, GLint yoffset
,
4656 GLsizei width
, GLsizei height
,
4658 GLenum format
, GLsizei imageSize
,
4659 const GLvoid
*data
, bool dsa
)
4661 struct gl_texture_image
*texImage
;
4663 if (compressed_subtexture_error_check(ctx
, dims
, texObj
, target
,
4664 level
, xoffset
, yoffset
, zoffset
,
4665 width
, height
, depth
,
4666 format
, imageSize
, dsa
)) {
4670 FLUSH_VERTICES(ctx
, 0);
4672 _mesa_lock_texture(ctx
, texObj
);
4674 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4677 if (width
> 0 && height
> 0 && depth
> 0) {
4678 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4679 xoffset
, yoffset
, zoffset
,
4680 width
, height
, depth
,
4681 format
, imageSize
, data
);
4683 check_gen_mipmap(ctx
, target
, texObj
, level
);
4685 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4686 * the texel data, not the texture format, size, etc.
4690 _mesa_unlock_texture(ctx
, texObj
);
4695 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4696 GLsizei width
, GLenum format
,
4697 GLsizei imageSize
, const GLvoid
*data
)
4699 struct gl_texture_object
*texObj
;
4700 GET_CURRENT_CONTEXT(ctx
);
4702 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4706 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, target
, level
,
4707 xoffset
, 0, 0, width
, 1, 1,
4708 format
, imageSize
, data
, false);
4712 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4713 GLsizei width
, GLenum format
,
4714 GLsizei imageSize
, const GLvoid
*data
)
4716 struct gl_texture_object
*texObj
;
4717 GET_CURRENT_CONTEXT(ctx
);
4719 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4720 "glCompressedTextureSubImage1D");
4724 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
4725 xoffset
, 0, 0, width
, 1, 1,
4726 format
, imageSize
, data
, true);
4731 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4732 GLint yoffset
, GLsizei width
, GLsizei height
,
4733 GLenum format
, GLsizei imageSize
,
4736 struct gl_texture_object
*texObj
;
4737 GET_CURRENT_CONTEXT(ctx
);
4739 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4743 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, target
, level
,
4744 xoffset
, yoffset
, 0, width
, height
, 1,
4745 format
, imageSize
, data
, false);
4749 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4751 GLsizei width
, GLsizei height
,
4752 GLenum format
, GLsizei imageSize
,
4755 struct gl_texture_object
*texObj
;
4756 GET_CURRENT_CONTEXT(ctx
);
4758 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4759 "glCompressedTextureSubImage2D");
4763 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
4764 xoffset
, yoffset
, 0, width
, height
, 1,
4765 format
, imageSize
, data
, true);
4769 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4770 GLint yoffset
, GLint zoffset
, GLsizei width
,
4771 GLsizei height
, GLsizei depth
, GLenum format
,
4772 GLsizei imageSize
, const GLvoid
*data
)
4774 struct gl_texture_object
*texObj
;
4775 GET_CURRENT_CONTEXT(ctx
);
4777 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4781 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, target
, level
,
4782 xoffset
, yoffset
, zoffset
,
4783 width
, height
, depth
,
4784 format
, imageSize
, data
, false);
4788 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4789 GLint yoffset
, GLint zoffset
, GLsizei width
,
4790 GLsizei height
, GLsizei depth
,
4791 GLenum format
, GLsizei imageSize
,
4794 struct gl_texture_object
*texObj
;
4795 GET_CURRENT_CONTEXT(ctx
);
4797 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4798 "glCompressedTextureSubImage3D");
4802 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
4803 xoffset
, yoffset
, zoffset
,
4804 width
, height
, depth
,
4805 format
, imageSize
, data
, true);
4809 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4811 if (ctx
->API
!= API_OPENGL_CORE
) {
4812 switch (internalFormat
) {
4814 return MESA_FORMAT_A_UNORM8
;
4816 return MESA_FORMAT_A_UNORM16
;
4817 case GL_ALPHA16F_ARB
:
4818 return MESA_FORMAT_A_FLOAT16
;
4819 case GL_ALPHA32F_ARB
:
4820 return MESA_FORMAT_A_FLOAT32
;
4821 case GL_ALPHA8I_EXT
:
4822 return MESA_FORMAT_A_SINT8
;
4823 case GL_ALPHA16I_EXT
:
4824 return MESA_FORMAT_A_SINT16
;
4825 case GL_ALPHA32I_EXT
:
4826 return MESA_FORMAT_A_SINT32
;
4827 case GL_ALPHA8UI_EXT
:
4828 return MESA_FORMAT_A_UINT8
;
4829 case GL_ALPHA16UI_EXT
:
4830 return MESA_FORMAT_A_UINT16
;
4831 case GL_ALPHA32UI_EXT
:
4832 return MESA_FORMAT_A_UINT32
;
4834 return MESA_FORMAT_L_UNORM8
;
4835 case GL_LUMINANCE16
:
4836 return MESA_FORMAT_L_UNORM16
;
4837 case GL_LUMINANCE16F_ARB
:
4838 return MESA_FORMAT_L_FLOAT16
;
4839 case GL_LUMINANCE32F_ARB
:
4840 return MESA_FORMAT_L_FLOAT32
;
4841 case GL_LUMINANCE8I_EXT
:
4842 return MESA_FORMAT_L_SINT8
;
4843 case GL_LUMINANCE16I_EXT
:
4844 return MESA_FORMAT_L_SINT16
;
4845 case GL_LUMINANCE32I_EXT
:
4846 return MESA_FORMAT_L_SINT32
;
4847 case GL_LUMINANCE8UI_EXT
:
4848 return MESA_FORMAT_L_UINT8
;
4849 case GL_LUMINANCE16UI_EXT
:
4850 return MESA_FORMAT_L_UINT16
;
4851 case GL_LUMINANCE32UI_EXT
:
4852 return MESA_FORMAT_L_UINT32
;
4853 case GL_LUMINANCE8_ALPHA8
:
4854 return MESA_FORMAT_L8A8_UNORM
;
4855 case GL_LUMINANCE16_ALPHA16
:
4856 return MESA_FORMAT_L16A16_UNORM
;
4857 case GL_LUMINANCE_ALPHA16F_ARB
:
4858 return MESA_FORMAT_LA_FLOAT16
;
4859 case GL_LUMINANCE_ALPHA32F_ARB
:
4860 return MESA_FORMAT_LA_FLOAT32
;
4861 case GL_LUMINANCE_ALPHA8I_EXT
:
4862 return MESA_FORMAT_LA_SINT8
;
4863 case GL_LUMINANCE_ALPHA16I_EXT
:
4864 return MESA_FORMAT_LA_SINT16
;
4865 case GL_LUMINANCE_ALPHA32I_EXT
:
4866 return MESA_FORMAT_LA_SINT32
;
4867 case GL_LUMINANCE_ALPHA8UI_EXT
:
4868 return MESA_FORMAT_LA_UINT8
;
4869 case GL_LUMINANCE_ALPHA16UI_EXT
:
4870 return MESA_FORMAT_LA_UINT16
;
4871 case GL_LUMINANCE_ALPHA32UI_EXT
:
4872 return MESA_FORMAT_LA_UINT32
;
4874 return MESA_FORMAT_I_UNORM8
;
4875 case GL_INTENSITY16
:
4876 return MESA_FORMAT_I_UNORM16
;
4877 case GL_INTENSITY16F_ARB
:
4878 return MESA_FORMAT_I_FLOAT16
;
4879 case GL_INTENSITY32F_ARB
:
4880 return MESA_FORMAT_I_FLOAT32
;
4881 case GL_INTENSITY8I_EXT
:
4882 return MESA_FORMAT_I_SINT8
;
4883 case GL_INTENSITY16I_EXT
:
4884 return MESA_FORMAT_I_SINT16
;
4885 case GL_INTENSITY32I_EXT
:
4886 return MESA_FORMAT_I_SINT32
;
4887 case GL_INTENSITY8UI_EXT
:
4888 return MESA_FORMAT_I_UINT8
;
4889 case GL_INTENSITY16UI_EXT
:
4890 return MESA_FORMAT_I_UINT16
;
4891 case GL_INTENSITY32UI_EXT
:
4892 return MESA_FORMAT_I_UINT32
;
4898 if (ctx
->API
== API_OPENGL_CORE
&&
4899 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4900 switch (internalFormat
) {
4902 return MESA_FORMAT_RGB_FLOAT32
;
4904 return MESA_FORMAT_RGB_UINT32
;
4906 return MESA_FORMAT_RGB_SINT32
;
4912 switch (internalFormat
) {
4914 return MESA_FORMAT_R8G8B8A8_UNORM
;
4916 return MESA_FORMAT_RGBA_UNORM16
;
4917 case GL_RGBA16F_ARB
:
4918 return MESA_FORMAT_RGBA_FLOAT16
;
4919 case GL_RGBA32F_ARB
:
4920 return MESA_FORMAT_RGBA_FLOAT32
;
4922 return MESA_FORMAT_RGBA_SINT8
;
4923 case GL_RGBA16I_EXT
:
4924 return MESA_FORMAT_RGBA_SINT16
;
4925 case GL_RGBA32I_EXT
:
4926 return MESA_FORMAT_RGBA_SINT32
;
4927 case GL_RGBA8UI_EXT
:
4928 return MESA_FORMAT_RGBA_UINT8
;
4929 case GL_RGBA16UI_EXT
:
4930 return MESA_FORMAT_RGBA_UINT16
;
4931 case GL_RGBA32UI_EXT
:
4932 return MESA_FORMAT_RGBA_UINT32
;
4935 return MESA_FORMAT_R8G8_UNORM
;
4937 return MESA_FORMAT_R16G16_UNORM
;
4939 return MESA_FORMAT_RG_FLOAT16
;
4941 return MESA_FORMAT_RG_FLOAT32
;
4943 return MESA_FORMAT_RG_SINT8
;
4945 return MESA_FORMAT_RG_SINT16
;
4947 return MESA_FORMAT_RG_SINT32
;
4949 return MESA_FORMAT_RG_UINT8
;
4951 return MESA_FORMAT_RG_UINT16
;
4953 return MESA_FORMAT_RG_UINT32
;
4956 return MESA_FORMAT_R_UNORM8
;
4958 return MESA_FORMAT_R_UNORM16
;
4960 return MESA_FORMAT_R_FLOAT16
;
4962 return MESA_FORMAT_R_FLOAT32
;
4964 return MESA_FORMAT_R_SINT8
;
4966 return MESA_FORMAT_R_SINT16
;
4968 return MESA_FORMAT_R_SINT32
;
4970 return MESA_FORMAT_R_UINT8
;
4972 return MESA_FORMAT_R_UINT16
;
4974 return MESA_FORMAT_R_UINT32
;
4977 return MESA_FORMAT_NONE
;
4983 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4984 GLenum internalFormat
)
4986 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4989 if (format
== MESA_FORMAT_NONE
)
4990 return MESA_FORMAT_NONE
;
4992 datatype
= _mesa_get_format_datatype(format
);
4994 /* The GL_ARB_texture_buffer_object spec says:
4996 * "If ARB_texture_float is not supported, references to the
4997 * floating-point internal formats provided by that extension should be
4998 * removed, and such formats may not be passed to TexBufferARB."
5000 * As a result, GL_HALF_FLOAT internal format depends on both
5001 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
5003 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
5004 !ctx
->Extensions
.ARB_texture_float
)
5005 return MESA_FORMAT_NONE
;
5007 if (!ctx
->Extensions
.ARB_texture_rg
) {
5008 GLenum base_format
= _mesa_get_format_base_format(format
);
5009 if (base_format
== GL_R
|| base_format
== GL_RG
)
5010 return MESA_FORMAT_NONE
;
5013 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
5014 GLenum base_format
= _mesa_get_format_base_format(format
);
5015 if (base_format
== GL_RGB
)
5016 return MESA_FORMAT_NONE
;
5023 _mesa_texture_buffer_range(struct gl_context
*ctx
,
5024 struct gl_texture_object
*texObj
, GLenum target
,
5025 GLenum internalFormat
,
5026 struct gl_buffer_object
*bufObj
,
5027 GLintptr offset
, GLsizeiptr size
, bool range
,
5032 FLUSH_VERTICES(ctx
, 0);
5034 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
5035 if (format
== MESA_FORMAT_NONE
) {
5036 _mesa_error(ctx
, GL_INVALID_ENUM
,
5037 "glTex%sBuffer%s(internalFormat 0x%x)", dsa
? "ture" : "",
5038 range
? "Range" : "", internalFormat
);
5042 _mesa_lock_texture(ctx
, texObj
);
5044 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
5045 texObj
->BufferObjectFormat
= internalFormat
;
5046 texObj
->_BufferObjectFormat
= format
;
5047 texObj
->BufferOffset
= offset
;
5048 texObj
->BufferSize
= size
;
5050 _mesa_unlock_texture(ctx
, texObj
);
5052 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
5055 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
5060 /** GL_ARB_texture_buffer_object */
5062 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
5064 struct gl_texture_object
*texObj
;
5065 struct gl_buffer_object
*bufObj
;
5067 GET_CURRENT_CONTEXT(ctx
);
5069 /* Need to catch this before it gets to _mesa_get_current_tex_object */
5070 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
5071 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
5075 /* NOTE: ARB_texture_buffer_object has interactions with
5076 * the compatibility profile that are not implemented.
5078 if (!(ctx
->API
== API_OPENGL_CORE
&&
5079 ctx
->Extensions
.ARB_texture_buffer_object
)) {
5080 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
5084 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
5085 if (!bufObj
&& buffer
) {
5086 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
5090 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5094 _mesa_texture_buffer_range(ctx
, texObj
, target
, internalFormat
, bufObj
, 0,
5095 buffer
? -1 : 0, false, false);
5099 /** GL_ARB_texture_buffer_range */
5101 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5102 GLintptr offset
, GLsizeiptr size
)
5104 struct gl_texture_object
*texObj
;
5105 struct gl_buffer_object
*bufObj
;
5107 GET_CURRENT_CONTEXT(ctx
);
5109 /* Need to catch this before it gets to _mesa_get_current_tex_object */
5110 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
5111 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBufferRange(target)");
5115 if (!(ctx
->API
== API_OPENGL_CORE
&&
5116 ctx
->Extensions
.ARB_texture_buffer_range
)) {
5117 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange");
5121 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
5125 (offset
+ size
) > bufObj
->Size
) {
5126 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexBufferRange");
5129 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
5130 _mesa_error(ctx
, GL_INVALID_VALUE
,
5131 "glTexBufferRange(invalid offset alignment)");
5134 } else if (buffer
) {
5135 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange(buffer %u)",
5143 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5147 _mesa_texture_buffer_range(ctx
, texObj
, target
, internalFormat
, bufObj
,
5148 offset
, size
, true, false);
5152 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5154 struct gl_texture_object
*texObj
;
5155 struct gl_buffer_object
*bufObj
;
5157 GET_CURRENT_CONTEXT(ctx
);
5159 /* NOTE: ARB_texture_buffer_object has interactions with
5160 * the compatibility profile that are not implemented.
5162 if (!(ctx
->API
== API_OPENGL_CORE
&&
5163 ctx
->Extensions
.ARB_texture_buffer_object
)) {
5164 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureBuffer");
5168 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
5169 if (!bufObj
&& buffer
) {
5170 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureBuffer(buffer %u)",
5175 /* Get the texture object by Name. */
5176 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5177 "glTextureBuffer(texture)");
5181 if (texObj
->Target
!= GL_TEXTURE_BUFFER_ARB
) {
5182 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTextureBuffer(target)");
5186 _mesa_texture_buffer_range(ctx
, texObj
, texObj
->Target
, internalFormat
,
5187 bufObj
, 0, buffer
? -1 : 0, false, true);
5191 is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5193 /* Everything that is allowed for renderbuffers,
5194 * except for a base format of GL_STENCIL_INDEX.
5196 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5197 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5201 /** GL_ARB_texture_multisample */
5203 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5206 case GL_TEXTURE_2D_MULTISAMPLE
:
5208 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5209 return dims
== 2 && !dsa
;
5211 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5213 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5214 return dims
== 3 && !dsa
;
5223 _mesa_texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5224 struct gl_texture_object
*texObj
,
5225 GLenum target
, GLsizei samples
,
5226 GLint internalformat
, GLsizei width
,
5227 GLsizei height
, GLsizei depth
,
5228 GLboolean fixedsamplelocations
,
5229 GLboolean immutable
, const char *func
)
5231 struct gl_texture_image
*texImage
;
5232 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5233 mesa_format texFormat
;
5234 GLenum sample_count_error
;
5235 bool dsa
= strstr(func
, "ture") ? true : false;
5237 if (!(ctx
->Extensions
.ARB_texture_multisample
5238 && _mesa_is_desktop_gl(ctx
))) {
5239 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5243 if (!check_multisample_target(dims
, target
, dsa
)) {
5245 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5249 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5254 /* check that the specified internalformat is color/depth/stencil-renderable;
5255 * refer GL3.1 spec 4.4.4
5258 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5259 _mesa_error(ctx
, GL_INVALID_ENUM
,
5260 "%s(internalformat=%s not legal for immutable-format)",
5261 func
, _mesa_lookup_enum_by_nr(internalformat
));
5265 if (!is_renderable_texture_format(ctx
, internalformat
)) {
5266 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5267 "%s(internalformat=%s)",
5268 func
, _mesa_lookup_enum_by_nr(internalformat
));
5272 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5273 internalformat
, samples
);
5274 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5276 /* Page 254 of OpenGL 4.4 spec says:
5277 * "Proxy arrays for two-dimensional multisample and two-dimensional
5278 * multisample array textures are operated on in the same way when
5279 * TexImage2DMultisample is called with target specified as
5280 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5281 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5282 * However, if samples is not supported, then no error is generated.
5284 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5285 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5289 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5290 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5291 "%s(texture object 0)",
5296 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5298 if (texImage
== NULL
) {
5299 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5303 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5304 internalformat
, GL_NONE
, GL_NONE
);
5305 assert(texFormat
!= MESA_FORMAT_NONE
);
5307 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5308 width
, height
, depth
, 0);
5310 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5311 width
, height
, depth
, 0);
5313 if (_mesa_is_proxy_texture(target
)) {
5314 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5315 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5316 internalformat
, texFormat
,
5317 samples
, fixedsamplelocations
);
5320 /* clear all image fields */
5321 clear_teximage_fields(texImage
);
5325 if (!dimensionsOK
) {
5326 _mesa_error(ctx
, GL_INVALID_VALUE
,
5327 "%s(invalid width or height)", func
);
5332 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
5333 "%s(texture too large)", func
);
5337 /* Check if texObj->Immutable is set */
5338 if (texObj
->Immutable
) {
5339 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5343 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5345 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5346 internalformat
, texFormat
,
5347 samples
, fixedsamplelocations
);
5349 if (width
> 0 && height
> 0 && depth
> 0) {
5350 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5351 width
, height
, depth
)) {
5352 /* tidy up the texture image state. strictly speaking,
5353 * we're allowed to just leave this in whatever state we
5354 * like, but being tidy is good.
5356 _mesa_init_teximage_fields(ctx
, texImage
,
5357 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5361 texObj
->Immutable
|= immutable
;
5364 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5367 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5373 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5374 GLenum internalformat
, GLsizei width
,
5375 GLsizei height
, GLboolean fixedsamplelocations
)
5377 struct gl_texture_object
*texObj
;
5378 GET_CURRENT_CONTEXT(ctx
);
5380 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5384 _mesa_texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5385 internalformat
, width
, height
, 1,
5386 fixedsamplelocations
, GL_FALSE
,
5387 "glTexImage2DMultisample");
5392 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5393 GLenum internalformat
, GLsizei width
,
5394 GLsizei height
, GLsizei depth
,
5395 GLboolean fixedsamplelocations
)
5397 struct gl_texture_object
*texObj
;
5398 GET_CURRENT_CONTEXT(ctx
);
5400 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5404 _mesa_texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5405 internalformat
, width
, height
, depth
,
5406 fixedsamplelocations
, GL_FALSE
,
5407 "glTexImage3DMultisample");
5412 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5413 GLenum internalformat
, GLsizei width
,
5414 GLsizei height
, GLboolean fixedsamplelocations
)
5416 struct gl_texture_object
*texObj
;
5417 GET_CURRENT_CONTEXT(ctx
);
5419 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5423 _mesa_texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5424 internalformat
, width
, height
, 1,
5425 fixedsamplelocations
, GL_TRUE
,
5426 "glTexStorage2DMultisample");
5430 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5431 GLenum internalformat
, GLsizei width
,
5432 GLsizei height
, GLsizei depth
,
5433 GLboolean fixedsamplelocations
)
5435 struct gl_texture_object
*texObj
;
5436 GET_CURRENT_CONTEXT(ctx
);
5438 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5442 _mesa_texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5443 internalformat
, width
, height
, depth
,
5444 fixedsamplelocations
, GL_TRUE
,
5445 "glTexStorage3DMultisample");
5449 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5450 GLenum internalformat
, GLsizei width
,
5452 GLboolean fixedsamplelocations
)
5454 struct gl_texture_object
*texObj
;
5455 GET_CURRENT_CONTEXT(ctx
);
5457 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5458 "glTextureStorage2DMultisample");
5462 _mesa_texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5463 internalformat
, width
, height
, 1,
5464 fixedsamplelocations
, GL_TRUE
,
5465 "glTextureStorage2DMultisample");
5469 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5470 GLenum internalformat
, GLsizei width
,
5471 GLsizei height
, GLsizei depth
,
5472 GLboolean fixedsamplelocations
)
5474 struct gl_texture_object
*texObj
;
5475 GET_CURRENT_CONTEXT(ctx
);
5477 /* Get the texture object by Name. */
5478 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5479 "glTextureStorage3DMultisample");
5483 _mesa_texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5484 internalformat
, width
, height
, depth
,
5485 fixedsamplelocations
, GL_TRUE
,
5486 "glTextureStorage3DMultisample");