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)
68 * Return the simple base format for a given internal texture format.
69 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
71 * \param ctx GL context.
72 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
74 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
75 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
77 * This is the format which is used during texture application (i.e. the
78 * texture format and env mode determine the arithmetic used.
81 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
83 switch (internalFormat
) {
89 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_ALPHA
: -1;
96 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE
: -1;
98 case GL_LUMINANCE_ALPHA
:
99 case GL_LUMINANCE4_ALPHA4
:
100 case GL_LUMINANCE6_ALPHA2
:
101 case GL_LUMINANCE8_ALPHA8
:
102 case GL_LUMINANCE12_ALPHA4
:
103 case GL_LUMINANCE12_ALPHA12
:
104 case GL_LUMINANCE16_ALPHA16
:
105 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE_ALPHA
: -1;
111 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_INTENSITY
: -1;
113 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGB
: -1;
124 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGBA
: -1;
138 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
140 if (_mesa_is_gles(ctx
)) {
141 switch (internalFormat
) {
149 if (ctx
->Extensions
.ARB_ES2_compatibility
) {
150 switch (internalFormat
) {
158 if (ctx
->Extensions
.ARB_depth_texture
) {
159 switch (internalFormat
) {
160 case GL_DEPTH_COMPONENT
:
161 case GL_DEPTH_COMPONENT16
:
162 case GL_DEPTH_COMPONENT24
:
163 case GL_DEPTH_COMPONENT32
:
164 return GL_DEPTH_COMPONENT
;
165 case GL_DEPTH_STENCIL
:
166 case GL_DEPTH24_STENCIL8
:
167 return GL_DEPTH_STENCIL
;
173 switch (internalFormat
) {
174 case GL_COMPRESSED_ALPHA
:
176 case GL_COMPRESSED_LUMINANCE
:
178 case GL_COMPRESSED_LUMINANCE_ALPHA
:
179 return GL_LUMINANCE_ALPHA
;
180 case GL_COMPRESSED_INTENSITY
:
182 case GL_COMPRESSED_RGB
:
184 case GL_COMPRESSED_RGBA
:
190 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
191 switch (internalFormat
) {
192 case GL_COMPRESSED_RGB_FXT1_3DFX
:
194 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
201 /* Assume that the ANGLE flag will always be set if the EXT flag is set.
203 if (ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
204 switch (internalFormat
) {
205 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
207 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
208 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
209 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
216 if (_mesa_is_desktop_gl(ctx
)
217 && ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
218 switch (internalFormat
) {
230 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
231 if (internalFormat
== GL_YCBCR_MESA
)
232 return GL_YCBCR_MESA
;
235 if (ctx
->Extensions
.ARB_texture_float
) {
236 switch (internalFormat
) {
237 case GL_ALPHA16F_ARB
:
238 case GL_ALPHA32F_ARB
:
246 case GL_INTENSITY16F_ARB
:
247 case GL_INTENSITY32F_ARB
:
249 case GL_LUMINANCE16F_ARB
:
250 case GL_LUMINANCE32F_ARB
:
252 case GL_LUMINANCE_ALPHA16F_ARB
:
253 case GL_LUMINANCE_ALPHA32F_ARB
:
254 return GL_LUMINANCE_ALPHA
;
260 if (ctx
->Extensions
.EXT_texture_snorm
) {
261 switch (internalFormat
) {
276 case GL_RGBA16_SNORM
:
279 case GL_ALPHA8_SNORM
:
280 case GL_ALPHA16_SNORM
:
282 case GL_LUMINANCE_SNORM
:
283 case GL_LUMINANCE8_SNORM
:
284 case GL_LUMINANCE16_SNORM
:
286 case GL_LUMINANCE_ALPHA_SNORM
:
287 case GL_LUMINANCE8_ALPHA8_SNORM
:
288 case GL_LUMINANCE16_ALPHA16_SNORM
:
289 return GL_LUMINANCE_ALPHA
;
290 case GL_INTENSITY_SNORM
:
291 case GL_INTENSITY8_SNORM
:
292 case GL_INTENSITY16_SNORM
:
299 if (ctx
->Extensions
.EXT_texture_sRGB
) {
300 switch (internalFormat
) {
303 case GL_COMPRESSED_SRGB_EXT
:
305 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
306 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGB
: -1;
307 case GL_SRGB_ALPHA_EXT
:
308 case GL_SRGB8_ALPHA8_EXT
:
309 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
311 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
312 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
313 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
314 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGBA
: -1;
315 case GL_SLUMINANCE_ALPHA_EXT
:
316 case GL_SLUMINANCE8_ALPHA8_EXT
:
317 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
318 return GL_LUMINANCE_ALPHA
;
319 case GL_SLUMINANCE_EXT
:
320 case GL_SLUMINANCE8_EXT
:
321 case GL_COMPRESSED_SLUMINANCE_EXT
:
328 if (ctx
->Version
>= 30 ||
329 ctx
->Extensions
.EXT_texture_integer
) {
330 switch (internalFormat
) {
332 case GL_RGBA16UI_EXT
:
333 case GL_RGBA32UI_EXT
:
349 if (ctx
->Extensions
.EXT_texture_integer
) {
350 switch (internalFormat
) {
351 case GL_ALPHA8UI_EXT
:
352 case GL_ALPHA16UI_EXT
:
353 case GL_ALPHA32UI_EXT
:
355 case GL_ALPHA16I_EXT
:
356 case GL_ALPHA32I_EXT
:
358 case GL_INTENSITY8UI_EXT
:
359 case GL_INTENSITY16UI_EXT
:
360 case GL_INTENSITY32UI_EXT
:
361 case GL_INTENSITY8I_EXT
:
362 case GL_INTENSITY16I_EXT
:
363 case GL_INTENSITY32I_EXT
:
365 case GL_LUMINANCE8UI_EXT
:
366 case GL_LUMINANCE16UI_EXT
:
367 case GL_LUMINANCE32UI_EXT
:
368 case GL_LUMINANCE8I_EXT
:
369 case GL_LUMINANCE16I_EXT
:
370 case GL_LUMINANCE32I_EXT
:
372 case GL_LUMINANCE_ALPHA8UI_EXT
:
373 case GL_LUMINANCE_ALPHA16UI_EXT
:
374 case GL_LUMINANCE_ALPHA32UI_EXT
:
375 case GL_LUMINANCE_ALPHA8I_EXT
:
376 case GL_LUMINANCE_ALPHA16I_EXT
:
377 case GL_LUMINANCE_ALPHA32I_EXT
:
378 return GL_LUMINANCE_ALPHA
;
384 if (ctx
->Extensions
.ARB_texture_rg
) {
385 switch (internalFormat
) {
388 if (!ctx
->Extensions
.ARB_texture_float
)
397 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
403 case GL_COMPRESSED_RED
:
408 if (!ctx
->Extensions
.ARB_texture_float
)
417 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
423 case GL_COMPRESSED_RG
:
430 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
431 switch (internalFormat
) {
439 if (ctx
->Extensions
.EXT_packed_float
) {
440 switch (internalFormat
) {
441 case GL_R11F_G11F_B10F_EXT
:
448 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
449 switch (internalFormat
) {
450 case GL_DEPTH_COMPONENT32F
:
451 return GL_DEPTH_COMPONENT
;
452 case GL_DEPTH32F_STENCIL8
:
453 return GL_DEPTH_STENCIL
;
459 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
460 switch (internalFormat
) {
461 case GL_COMPRESSED_RED_RGTC1
:
462 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
464 case GL_COMPRESSED_RG_RGTC2
:
465 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
472 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
473 switch (internalFormat
) {
474 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
475 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
477 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
478 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
479 return GL_LUMINANCE_ALPHA
;
485 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
486 switch (internalFormat
) {
487 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
488 return GL_LUMINANCE_ALPHA
;
494 if (ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
) {
495 switch (internalFormat
) {
496 case GL_ETC1_RGB8_OES
:
503 if (_mesa_is_gles3(ctx
) || ctx
->Extensions
.ARB_ES3_compatibility
) {
504 switch (internalFormat
) {
505 case GL_COMPRESSED_RGB8_ETC2
:
506 case GL_COMPRESSED_SRGB8_ETC2
:
508 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
509 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
510 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
511 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
513 case GL_COMPRESSED_R11_EAC
:
514 case GL_COMPRESSED_SIGNED_R11_EAC
:
516 case GL_COMPRESSED_RG11_EAC
:
517 case GL_COMPRESSED_SIGNED_RG11_EAC
:
524 if (_mesa_is_desktop_gl(ctx
) &&
525 ctx
->Extensions
.ARB_texture_compression_bptc
) {
526 switch (internalFormat
) {
527 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
528 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
530 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
531 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
538 if (ctx
->API
== API_OPENGLES
) {
539 switch (internalFormat
) {
540 case GL_PALETTE4_RGB8_OES
:
541 case GL_PALETTE4_R5_G6_B5_OES
:
542 case GL_PALETTE8_RGB8_OES
:
543 case GL_PALETTE8_R5_G6_B5_OES
:
545 case GL_PALETTE4_RGBA8_OES
:
546 case GL_PALETTE8_RGB5_A1_OES
:
547 case GL_PALETTE4_RGBA4_OES
:
548 case GL_PALETTE4_RGB5_A1_OES
:
549 case GL_PALETTE8_RGBA8_OES
:
550 case GL_PALETTE8_RGBA4_OES
:
557 return -1; /* error */
562 * For cube map faces, return a face index in [0,5].
563 * For other targets return 0;
566 _mesa_tex_target_to_face(GLenum target
)
568 if (_mesa_is_cube_face(target
))
569 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
577 * Install gl_texture_image in a gl_texture_object according to the target
578 * and level parameters.
580 * \param tObj texture object.
581 * \param target texture target.
582 * \param level image level.
583 * \param texImage texture image.
586 set_tex_image(struct gl_texture_object
*tObj
,
587 GLenum target
, GLint level
,
588 struct gl_texture_image
*texImage
)
590 const GLuint face
= _mesa_tex_target_to_face(target
);
594 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
597 tObj
->Image
[face
][level
] = texImage
;
599 /* Set the 'back' pointer */
600 texImage
->TexObject
= tObj
;
601 texImage
->Level
= level
;
602 texImage
->Face
= face
;
607 * Allocate a texture image structure.
609 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
612 * \return a pointer to gl_texture_image struct with all fields initialized to
615 struct gl_texture_image
*
616 _mesa_new_texture_image( struct gl_context
*ctx
)
619 return CALLOC_STRUCT(gl_texture_image
);
624 * Free a gl_texture_image and associated data.
625 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
627 * \param texImage texture image.
629 * Free the texture image structure and the associated image data.
632 _mesa_delete_texture_image(struct gl_context
*ctx
,
633 struct gl_texture_image
*texImage
)
635 /* Free texImage->Data and/or any other driver-specific texture
638 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
639 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
645 * Test if a target is a proxy target.
647 * \param target texture target.
649 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
652 _mesa_is_proxy_texture(GLenum target
)
655 static const GLenum targets
[] = {
659 GL_PROXY_TEXTURE_CUBE_MAP
,
660 GL_PROXY_TEXTURE_RECTANGLE
,
661 GL_PROXY_TEXTURE_1D_ARRAY
,
662 GL_PROXY_TEXTURE_2D_ARRAY
,
663 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
664 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
665 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
668 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
669 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
671 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== Elements(targets
) + 2);
673 for (i
= 0; i
< Elements(targets
); ++i
)
674 if (target
== targets
[i
])
681 * Return the proxy target which corresponds to the given texture target
684 proxy_target(GLenum target
)
688 case GL_PROXY_TEXTURE_1D
:
689 return GL_PROXY_TEXTURE_1D
;
691 case GL_PROXY_TEXTURE_2D
:
692 return GL_PROXY_TEXTURE_2D
;
694 case GL_PROXY_TEXTURE_3D
:
695 return GL_PROXY_TEXTURE_3D
;
696 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
697 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
698 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
699 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
700 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
701 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
702 case GL_TEXTURE_CUBE_MAP_ARB
:
703 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
704 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
705 case GL_TEXTURE_RECTANGLE_NV
:
706 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
707 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
708 case GL_TEXTURE_1D_ARRAY_EXT
:
709 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
710 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
711 case GL_TEXTURE_2D_ARRAY_EXT
:
712 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
713 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
714 case GL_TEXTURE_CUBE_MAP_ARRAY
:
715 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
716 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
717 case GL_TEXTURE_2D_MULTISAMPLE
:
718 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
719 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
720 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
721 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
722 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
724 _mesa_problem(NULL
, "unexpected target in proxy_target()");
733 * Get a texture image pointer from a texture object, given a texture
734 * target and mipmap level. The target and level parameters should
735 * have already been error-checked.
737 * \param texObj texture unit.
738 * \param target texture target.
739 * \param level image level.
741 * \return pointer to the texture image structure, or NULL on failure.
743 struct gl_texture_image
*
744 _mesa_select_tex_image(const struct gl_texture_object
*texObj
,
745 GLenum target
, GLint level
)
747 const GLuint face
= _mesa_tex_target_to_face(target
);
751 ASSERT(level
< MAX_TEXTURE_LEVELS
);
753 return texObj
->Image
[face
][level
];
758 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
759 * it and install it. Only return NULL if passed a bad parameter or run
762 struct gl_texture_image
*
763 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
764 GLenum target
, GLint level
)
766 struct gl_texture_image
*texImage
;
771 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
773 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
775 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
779 set_tex_image(texObj
, target
, level
, texImage
);
787 * Return pointer to the specified proxy texture image.
788 * Note that proxy textures are per-context, not per-texture unit.
789 * \return pointer to texture image or NULL if invalid target, invalid
790 * level, or out of memory.
792 static struct gl_texture_image
*
793 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
795 struct gl_texture_image
*texImage
;
802 case GL_PROXY_TEXTURE_1D
:
803 if (level
>= ctx
->Const
.MaxTextureLevels
)
805 texIndex
= TEXTURE_1D_INDEX
;
807 case GL_PROXY_TEXTURE_2D
:
808 if (level
>= ctx
->Const
.MaxTextureLevels
)
810 texIndex
= TEXTURE_2D_INDEX
;
812 case GL_PROXY_TEXTURE_3D
:
813 if (level
>= ctx
->Const
.Max3DTextureLevels
)
815 texIndex
= TEXTURE_3D_INDEX
;
817 case GL_PROXY_TEXTURE_CUBE_MAP
:
818 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
820 texIndex
= TEXTURE_CUBE_INDEX
;
822 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
825 texIndex
= TEXTURE_RECT_INDEX
;
827 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
828 if (level
>= ctx
->Const
.MaxTextureLevels
)
830 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
832 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
833 if (level
>= ctx
->Const
.MaxTextureLevels
)
835 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
837 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
838 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
840 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
842 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
845 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
847 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
850 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
856 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
858 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
860 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
863 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
864 /* Set the 'back' pointer */
865 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
872 * Get the maximum number of allowed mipmap levels.
874 * \param ctx GL context.
875 * \param target texture target.
877 * \return the maximum number of allowed mipmap levels for the given
878 * texture target, or zero if passed a bad target.
883 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
887 case GL_PROXY_TEXTURE_1D
:
889 case GL_PROXY_TEXTURE_2D
:
890 return ctx
->Const
.MaxTextureLevels
;
892 case GL_PROXY_TEXTURE_3D
:
893 return ctx
->Const
.Max3DTextureLevels
;
894 case GL_TEXTURE_CUBE_MAP
:
895 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
896 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
897 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
898 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
899 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
900 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
901 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
902 return ctx
->Extensions
.ARB_texture_cube_map
903 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
904 case GL_TEXTURE_RECTANGLE_NV
:
905 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
906 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
907 case GL_TEXTURE_1D_ARRAY_EXT
:
908 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
909 case GL_TEXTURE_2D_ARRAY_EXT
:
910 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
911 return ctx
->Extensions
.EXT_texture_array
912 ? ctx
->Const
.MaxTextureLevels
: 0;
913 case GL_TEXTURE_CUBE_MAP_ARRAY
:
914 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
915 return ctx
->Extensions
.ARB_texture_cube_map_array
916 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
917 case GL_TEXTURE_BUFFER
:
918 return ctx
->API
== API_OPENGL_CORE
&&
919 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
920 case GL_TEXTURE_2D_MULTISAMPLE
:
921 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
922 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
923 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
924 return _mesa_is_desktop_gl(ctx
)
925 && ctx
->Extensions
.ARB_texture_multisample
927 case GL_TEXTURE_EXTERNAL_OES
:
930 return 0; /* bad target */
936 * Return number of dimensions per mipmap level for the given texture target.
939 _mesa_get_texture_dimensions(GLenum target
)
943 case GL_PROXY_TEXTURE_1D
:
946 case GL_TEXTURE_RECTANGLE
:
947 case GL_TEXTURE_CUBE_MAP
:
948 case GL_PROXY_TEXTURE_2D
:
949 case GL_PROXY_TEXTURE_RECTANGLE
:
950 case GL_PROXY_TEXTURE_CUBE_MAP
:
951 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
952 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
953 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
954 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
955 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
956 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
957 case GL_TEXTURE_1D_ARRAY
:
958 case GL_PROXY_TEXTURE_1D_ARRAY
:
959 case GL_TEXTURE_EXTERNAL_OES
:
960 case GL_TEXTURE_2D_MULTISAMPLE
:
961 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
964 case GL_PROXY_TEXTURE_3D
:
965 case GL_TEXTURE_2D_ARRAY
:
966 case GL_PROXY_TEXTURE_2D_ARRAY
:
967 case GL_TEXTURE_CUBE_MAP_ARRAY
:
968 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
969 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
970 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
972 case GL_TEXTURE_BUFFER
:
975 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
983 * Check if a texture target can have more than one layer.
986 _mesa_tex_target_is_layered(GLenum target
)
990 case GL_PROXY_TEXTURE_1D
:
992 case GL_PROXY_TEXTURE_2D
:
993 case GL_TEXTURE_RECTANGLE
:
994 case GL_PROXY_TEXTURE_RECTANGLE
:
995 case GL_TEXTURE_2D_MULTISAMPLE
:
996 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
997 case GL_TEXTURE_BUFFER
:
998 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
999 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1000 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1001 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1002 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1003 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1004 case GL_TEXTURE_EXTERNAL_OES
:
1008 case GL_PROXY_TEXTURE_3D
:
1009 case GL_TEXTURE_CUBE_MAP
:
1010 case GL_PROXY_TEXTURE_CUBE_MAP
:
1011 case GL_TEXTURE_1D_ARRAY
:
1012 case GL_PROXY_TEXTURE_1D_ARRAY
:
1013 case GL_TEXTURE_2D_ARRAY
:
1014 case GL_PROXY_TEXTURE_2D_ARRAY
:
1015 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1016 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1017 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1018 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1022 assert(!"Invalid texture target.");
1029 * Return the number of layers present in the given level of an array,
1030 * cubemap or 3D texture. If the texture is not layered return zero.
1033 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
1035 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
1037 switch (texObj
->Target
) {
1040 case GL_TEXTURE_RECTANGLE
:
1041 case GL_TEXTURE_2D_MULTISAMPLE
:
1042 case GL_TEXTURE_BUFFER
:
1043 case GL_TEXTURE_EXTERNAL_OES
:
1046 case GL_TEXTURE_CUBE_MAP
:
1049 case GL_TEXTURE_1D_ARRAY
: {
1050 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1051 return img
? img
->Height
: 0;
1055 case GL_TEXTURE_2D_ARRAY
:
1056 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1057 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
1058 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1059 return img
? img
->Depth
: 0;
1063 assert(!"Invalid texture target.");
1070 * Return the maximum number of mipmap levels for the given target
1071 * and the dimensions.
1072 * The dimensions are expected not to include the border.
1075 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
1082 case GL_TEXTURE_1D_ARRAY
:
1083 case GL_PROXY_TEXTURE_1D
:
1084 case GL_PROXY_TEXTURE_1D_ARRAY
:
1087 case GL_TEXTURE_CUBE_MAP
:
1088 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1089 case GL_PROXY_TEXTURE_CUBE_MAP
:
1090 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1094 case GL_TEXTURE_2D_ARRAY
:
1095 case GL_PROXY_TEXTURE_2D
:
1096 case GL_PROXY_TEXTURE_2D_ARRAY
:
1097 size
= MAX2(width
, height
);
1100 case GL_PROXY_TEXTURE_3D
:
1101 size
= MAX3(width
, height
, depth
);
1103 case GL_TEXTURE_RECTANGLE
:
1104 case GL_TEXTURE_EXTERNAL_OES
:
1105 case GL_TEXTURE_2D_MULTISAMPLE
:
1106 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1107 case GL_PROXY_TEXTURE_RECTANGLE
:
1108 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1109 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1116 return _mesa_logbase2(size
) + 1;
1120 #if 000 /* not used anymore */
1122 * glTexImage[123]D can accept a NULL image pointer. In this case we
1123 * create a texture image with unspecified image contents per the OpenGL
1127 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1129 const GLint components
= _mesa_components_in_format(format
);
1130 const GLint numPixels
= width
* height
* depth
;
1131 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
1135 * Let's see if anyone finds this. If glTexImage2D() is called with
1136 * a NULL image pointer then load the texture image with something
1137 * interesting instead of leaving it indeterminate.
1140 static const char message
[8][32] = {
1141 " X X XXXXX XXX X ",
1142 " XX XX X X X X X ",
1144 " X X XXXX XXX XXXXX ",
1147 " X X XXXXX XXX X X ",
1151 GLubyte
*imgPtr
= data
;
1153 for (h
= 0; h
< depth
; h
++) {
1154 for (i
= 0; i
< height
; i
++) {
1155 GLint srcRow
= 7 - (i
% 8);
1156 for (j
= 0; j
< width
; j
++) {
1157 GLint srcCol
= j
% 32;
1158 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1159 for (k
= 0; k
< components
; k
++) {
1175 * Set the size and format-related fields of a gl_texture_image struct
1176 * to zero. This is used when a proxy texture test fails.
1179 clear_teximage_fields(struct gl_texture_image
*img
)
1182 img
->_BaseFormat
= 0;
1183 img
->InternalFormat
= 0;
1192 img
->HeightLog2
= 0;
1194 img
->TexFormat
= MESA_FORMAT_NONE
;
1195 img
->NumSamples
= 0;
1196 img
->FixedSampleLocations
= GL_TRUE
;
1201 * Initialize basic fields of the gl_texture_image struct.
1203 * \param ctx GL context.
1204 * \param img texture image structure to be initialized.
1205 * \param width image width.
1206 * \param height image height.
1207 * \param depth image depth.
1208 * \param border image border.
1209 * \param internalFormat internal format.
1210 * \param format the actual hardware format (one of MESA_FORMAT_*)
1211 * \param numSamples number of samples per texel, or zero for non-MS.
1212 * \param fixedSampleLocations are sample locations fixed?
1214 * Fills in the fields of \p img with the given information.
1215 * Note: width, height and depth include the border.
1218 init_teximage_fields_ms(struct gl_context
*ctx
,
1219 struct gl_texture_image
*img
,
1220 GLsizei width
, GLsizei height
, GLsizei depth
,
1221 GLint border
, GLenum internalFormat
,
1223 GLuint numSamples
, GLboolean fixedSampleLocations
)
1228 ASSERT(height
>= 0);
1231 target
= img
->TexObject
->Target
;
1232 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1233 ASSERT(img
->_BaseFormat
> 0);
1234 img
->InternalFormat
= internalFormat
;
1235 img
->Border
= border
;
1237 img
->Height
= height
;
1240 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1241 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1243 img
->NumSamples
= 0;
1244 img
->FixedSampleLocations
= GL_TRUE
;
1248 case GL_TEXTURE_BUFFER
:
1249 case GL_PROXY_TEXTURE_1D
:
1254 img
->HeightLog2
= 0;
1261 case GL_TEXTURE_1D_ARRAY
:
1262 case GL_PROXY_TEXTURE_1D_ARRAY
:
1263 img
->Height2
= height
; /* no border */
1264 img
->HeightLog2
= 0; /* not used */
1272 case GL_TEXTURE_RECTANGLE
:
1273 case GL_TEXTURE_CUBE_MAP
:
1274 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1275 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1276 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1277 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1278 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1279 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1280 case GL_TEXTURE_EXTERNAL_OES
:
1281 case GL_PROXY_TEXTURE_2D
:
1282 case GL_PROXY_TEXTURE_RECTANGLE
:
1283 case GL_PROXY_TEXTURE_CUBE_MAP
:
1284 case GL_TEXTURE_2D_MULTISAMPLE
:
1285 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1286 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1287 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1294 case GL_TEXTURE_2D_ARRAY
:
1295 case GL_PROXY_TEXTURE_2D_ARRAY
:
1296 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1297 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1298 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1299 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1300 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1301 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1302 img
->Depth2
= depth
; /* no border */
1303 img
->DepthLog2
= 0; /* not used */
1306 case GL_PROXY_TEXTURE_3D
:
1307 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1308 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1309 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1310 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1313 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1318 _mesa_get_tex_max_num_levels(target
,
1319 img
->Width2
, img
->Height2
, img
->Depth2
);
1320 img
->TexFormat
= format
;
1321 img
->NumSamples
= numSamples
;
1322 img
->FixedSampleLocations
= fixedSampleLocations
;
1327 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1328 struct gl_texture_image
*img
,
1329 GLsizei width
, GLsizei height
, GLsizei depth
,
1330 GLint border
, GLenum internalFormat
,
1333 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
1334 internalFormat
, format
, 0, GL_TRUE
);
1339 * Free and clear fields of the gl_texture_image struct.
1341 * \param ctx GL context.
1342 * \param texImage texture image structure to be cleared.
1344 * After the call, \p texImage will have no data associated with it. Its
1345 * fields are cleared so that its parent object will test incomplete.
1348 _mesa_clear_texture_image(struct gl_context
*ctx
,
1349 struct gl_texture_image
*texImage
)
1351 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1352 clear_teximage_fields(texImage
);
1357 * Check the width, height, depth and border of a texture image are legal.
1358 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
1360 * The target and level parameters will have already been validated.
1361 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
1364 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
1365 GLint level
, GLint width
, GLint height
,
1366 GLint depth
, GLint border
)
1372 case GL_PROXY_TEXTURE_1D
:
1373 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
1374 maxSize
>>= level
; /* level size */
1375 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1377 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1378 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1384 case GL_PROXY_TEXTURE_2D
:
1385 case GL_TEXTURE_2D_MULTISAMPLE
:
1386 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1387 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1389 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1391 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1393 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1394 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1396 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1402 case GL_PROXY_TEXTURE_3D
:
1403 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1405 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1407 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1409 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1411 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1412 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1414 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1416 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1421 case GL_TEXTURE_RECTANGLE_NV
:
1422 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1425 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1426 if (width
< 0 || width
> maxSize
)
1428 if (height
< 0 || height
> maxSize
)
1432 case GL_TEXTURE_CUBE_MAP
:
1433 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1434 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1435 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1436 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1437 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1438 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1439 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1440 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1442 if (width
!= height
)
1444 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1446 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1448 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1449 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1451 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1456 case GL_TEXTURE_1D_ARRAY_EXT
:
1457 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1458 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1460 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1462 if (height
< 0 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1464 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1465 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1470 case GL_TEXTURE_2D_ARRAY_EXT
:
1471 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1472 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1473 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1474 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1476 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1478 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1480 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1482 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1483 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1485 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1490 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1491 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1492 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1493 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1495 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1497 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1499 if (width
!= height
)
1501 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1503 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1504 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1506 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1511 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1518 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1519 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1520 * \param destImage the destination texture image.
1521 * \return GL_TRUE if error found, GL_FALSE otherwise.
1524 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1525 const struct gl_texture_image
*destImage
,
1526 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1527 GLsizei subWidth
, GLsizei subHeight
,
1528 GLsizei subDepth
, const char *func
)
1530 const GLenum target
= destImage
->TexObject
->Target
;
1535 _mesa_error(ctx
, GL_INVALID_VALUE
,
1536 "%s%dD(width=%d)", func
, dims
, subWidth
);
1540 if (dims
> 1 && subHeight
< 0) {
1541 _mesa_error(ctx
, GL_INVALID_VALUE
,
1542 "%s%dD(height=%d)", func
, dims
, subHeight
);
1546 if (dims
> 2 && subDepth
< 0) {
1547 _mesa_error(ctx
, GL_INVALID_VALUE
,
1548 "%s%dD(depth=%d)", func
, dims
, subDepth
);
1552 /* check xoffset and width */
1553 if (xoffset
< - (GLint
) destImage
->Border
) {
1554 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset)",
1559 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1560 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset+width)",
1565 /* check yoffset and height */
1567 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1568 if (yoffset
< -yBorder
) {
1569 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset)",
1573 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1574 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset+height)",
1580 /* check zoffset and depth */
1583 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1584 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1585 0 : destImage
->Border
;
1587 if (zoffset
< -zBorder
) {
1588 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset)", func
);
1592 depth
= (GLint
) destImage
->Depth
;
1593 if (target
== GL_TEXTURE_CUBE_MAP
)
1595 if (zoffset
+ subDepth
> depth
) {
1596 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset+depth)", func
);
1602 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1603 * compressed texture images can be updated. But, that restriction may be
1604 * relaxed for particular compressed formats. At this time, all the
1605 * compressed formats supported by Mesa allow sub-textures to be updated
1606 * along compressed block boundaries.
1608 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1610 if (bw
!= 1 || bh
!= 1) {
1611 /* offset must be multiple of block size */
1612 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1613 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1614 "%s%dD(xoffset = %d, yoffset = %d)",
1615 func
, dims
, xoffset
, yoffset
);
1619 /* The size must be a multiple of bw x bh, or we must be using a
1620 * offset+size that exactly hits the edge of the image. This
1621 * is important for small mipmap levels (1x1, 2x1, etc) and for
1624 if ((subWidth
% bw
!= 0) &&
1625 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1626 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1627 "%s%dD(width = %d)", func
, dims
, subWidth
);
1631 if ((subHeight
% bh
!= 0) &&
1632 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1633 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1634 "%s%dD(height = %d)", func
, dims
, subHeight
);
1646 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1647 * specific texture image size checks.
1649 * A hardware driver might override this function if, for example, the
1650 * max 3D texture size is 512x512x64 (i.e. not a cube).
1652 * Note that width, height, depth == 0 is not an error. However, a
1653 * texture with zero width/height/depth will be considered "incomplete"
1654 * and texturing will effectively be disabled.
1656 * \param target any texture target/type
1657 * \param level as passed to glTexImage
1658 * \param format the MESA_FORMAT_x for the tex image
1659 * \param width as passed to glTexImage
1660 * \param height as passed to glTexImage
1661 * \param depth as passed to glTexImage
1662 * \param border as passed to glTexImage
1663 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1666 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1668 GLint width
, GLint height
, GLint depth
, GLint border
)
1670 /* We just check if the image size is less than MaxTextureMbytes.
1671 * Some drivers may do more specific checks.
1673 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1674 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1675 mbytes
*= _mesa_num_tex_faces(target
);
1676 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1681 * Return true if the format is only valid for glCompressedTexImage.
1684 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1687 case GL_ETC1_RGB8_OES
:
1688 case GL_PALETTE4_RGB8_OES
:
1689 case GL_PALETTE4_RGBA8_OES
:
1690 case GL_PALETTE4_R5_G6_B5_OES
:
1691 case GL_PALETTE4_RGBA4_OES
:
1692 case GL_PALETTE4_RGB5_A1_OES
:
1693 case GL_PALETTE8_RGB8_OES
:
1694 case GL_PALETTE8_RGBA8_OES
:
1695 case GL_PALETTE8_R5_G6_B5_OES
:
1696 case GL_PALETTE8_RGBA4_OES
:
1697 case GL_PALETTE8_RGB5_A1_OES
:
1706 * Helper function to determine whether a target and specific compression
1707 * format are supported.
1710 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1713 (void) intFormat
; /* not used yet */
1717 case GL_PROXY_TEXTURE_2D
:
1718 return GL_TRUE
; /* true for any compressed format so far */
1719 case GL_PROXY_TEXTURE_CUBE_MAP
:
1720 case GL_TEXTURE_CUBE_MAP
:
1721 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1722 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1723 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1724 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1725 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1726 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1727 return ctx
->Extensions
.ARB_texture_cube_map
;
1728 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1729 case GL_TEXTURE_2D_ARRAY_EXT
:
1730 return ctx
->Extensions
.EXT_texture_array
;
1731 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1732 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1733 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1741 * Check if the given texture target value is legal for a
1742 * glTexImage1/2/3D call.
1745 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1751 case GL_PROXY_TEXTURE_1D
:
1752 return _mesa_is_desktop_gl(ctx
);
1760 case GL_PROXY_TEXTURE_2D
:
1761 return _mesa_is_desktop_gl(ctx
);
1762 case GL_PROXY_TEXTURE_CUBE_MAP
:
1763 return _mesa_is_desktop_gl(ctx
)
1764 && ctx
->Extensions
.ARB_texture_cube_map
;
1765 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1766 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1767 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1768 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1769 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1770 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1771 return ctx
->Extensions
.ARB_texture_cube_map
;
1772 case GL_TEXTURE_RECTANGLE_NV
:
1773 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1774 return _mesa_is_desktop_gl(ctx
)
1775 && ctx
->Extensions
.NV_texture_rectangle
;
1776 case GL_TEXTURE_1D_ARRAY_EXT
:
1777 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1778 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1786 case GL_PROXY_TEXTURE_3D
:
1787 return _mesa_is_desktop_gl(ctx
);
1788 case GL_TEXTURE_2D_ARRAY_EXT
:
1789 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1790 || _mesa_is_gles3(ctx
);
1791 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1792 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1793 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1794 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1795 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1800 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1807 * Check if the given texture target value is legal for a
1808 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1809 * The difference compared to legal_teximage_target() above is that
1810 * proxy targets are not supported.
1813 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1818 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1823 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1824 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1825 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1826 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1827 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1828 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1829 return ctx
->Extensions
.ARB_texture_cube_map
;
1830 case GL_TEXTURE_RECTANGLE_NV
:
1831 return _mesa_is_desktop_gl(ctx
)
1832 && ctx
->Extensions
.NV_texture_rectangle
;
1833 case GL_TEXTURE_1D_ARRAY_EXT
:
1834 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1842 case GL_TEXTURE_2D_ARRAY_EXT
:
1843 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1844 || _mesa_is_gles3(ctx
);
1845 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1846 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1847 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1849 /* Table 8.15 of the OpenGL 4.5 core profile spec
1850 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1851 * and CopyTextureSubImage3D.
1853 case GL_TEXTURE_CUBE_MAP
:
1859 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1867 * Helper function to determine if a texture object is mutable (in terms
1868 * of GL_ARB_texture_storage).
1871 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1873 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1877 return !texObj
->Immutable
;
1882 * Return expected size of a compressed texture.
1885 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1888 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1889 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1893 * Verify that a texture format is valid with a particular target
1895 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1896 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1899 * \param ctx GL context
1900 * \param target Texture target
1901 * \param internalFormat Internal format of the texture image
1902 * \param dimensions Dimensionality at the caller. This is \b not used
1903 * in the validation. It is only used when logging
1905 * \param caller Base name of the calling function (e.g.,
1906 * "glTexImage" or "glTexStorage").
1908 * \returns true if the combination is legal, false otherwise.
1911 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1912 GLenum target
, GLenum internalFormat
,
1913 unsigned dimensions
,
1916 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1917 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
) {
1918 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1919 * Profile spec says:
1921 * "Textures with a base internal format of DEPTH_COMPONENT or
1922 * DEPTH_STENCIL are supported by texture image specification
1923 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1924 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1925 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1926 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1927 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1928 * formats in conjunction with any other target will result in an
1929 * INVALID_OPERATION error."
1931 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1932 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1934 if (target
!= GL_TEXTURE_1D
&&
1935 target
!= GL_PROXY_TEXTURE_1D
&&
1936 target
!= GL_TEXTURE_2D
&&
1937 target
!= GL_PROXY_TEXTURE_2D
&&
1938 target
!= GL_TEXTURE_1D_ARRAY
&&
1939 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1940 target
!= GL_TEXTURE_2D_ARRAY
&&
1941 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1942 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1943 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1944 !((_mesa_is_cube_face(target
) ||
1945 target
== GL_TEXTURE_CUBE_MAP
||
1946 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1947 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1948 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1949 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1950 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1951 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1952 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1953 "%s%dD(bad target for depth texture)",
1954 caller
, dimensions
);
1963 texture_formats_agree(GLenum internalFormat
,
1966 GLboolean colorFormat
;
1967 GLboolean is_format_depth_or_depthstencil
;
1968 GLboolean is_internalFormat_depth_or_depthstencil
;
1970 /* Even though there are no color-index textures, we still have to support
1971 * uploading color-index data and remapping it to RGB via the
1972 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1974 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1976 is_internalFormat_depth_or_depthstencil
=
1977 _mesa_is_depth_format(internalFormat
) ||
1978 _mesa_is_depthstencil_format(internalFormat
);
1980 is_format_depth_or_depthstencil
=
1981 _mesa_is_depth_format(format
) ||
1982 _mesa_is_depthstencil_format(format
);
1984 colorFormat
= _mesa_is_color_format(format
);
1986 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1989 if (is_internalFormat_depth_or_depthstencil
!=
1990 is_format_depth_or_depthstencil
)
1993 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
2000 * Test the glTexImage[123]D() parameters for errors.
2002 * \param ctx GL context.
2003 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2004 * \param target texture target given by the user (already validated).
2005 * \param level image level given by the user.
2006 * \param internalFormat internal format given by the user.
2007 * \param format pixel data format given by the user.
2008 * \param type pixel data type given by the user.
2009 * \param width image width given by the user.
2010 * \param height image height given by the user.
2011 * \param depth image depth given by the user.
2012 * \param border image border given by the user.
2014 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2016 * Verifies each of the parameters against the constants specified in
2017 * __struct gl_contextRec::Const and the supported extensions, and according
2018 * to the OpenGL specification.
2019 * Note that we don't fully error-check the width, height, depth values
2020 * here. That's done in _mesa_legal_texture_dimensions() which is used
2021 * by several other GL entrypoints. Plus, texture dims have a special
2022 * interaction with proxy textures.
2025 texture_error_check( struct gl_context
*ctx
,
2026 GLuint dimensions
, GLenum target
,
2027 GLint level
, GLint internalFormat
,
2028 GLenum format
, GLenum type
,
2029 GLint width
, GLint height
,
2030 GLint depth
, GLint border
)
2034 /* Note: for proxy textures, some error conditions immediately generate
2035 * a GL error in the usual way. But others do not generate a GL error.
2036 * Instead, they cause the width, height, depth, format fields of the
2037 * texture image to be zeroed-out. The GL spec seems to indicate that the
2038 * zero-out behaviour is only used in cases related to memory allocation.
2042 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2043 _mesa_error(ctx
, GL_INVALID_VALUE
,
2044 "glTexImage%dD(level=%d)", dimensions
, level
);
2049 if (border
< 0 || border
> 1 ||
2050 ((ctx
->API
!= API_OPENGL_COMPAT
||
2051 target
== GL_TEXTURE_RECTANGLE_NV
||
2052 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2053 _mesa_error(ctx
, GL_INVALID_VALUE
,
2054 "glTexImage%dD(border=%d)", dimensions
, border
);
2058 if (width
< 0 || height
< 0 || depth
< 0) {
2059 _mesa_error(ctx
, GL_INVALID_VALUE
,
2060 "glTexImage%dD(width, height or depth < 0)", dimensions
);
2064 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2065 * combinations of format, internalFormat, and type that can be used.
2066 * Formats and types that require additional extensions (e.g., GL_FLOAT
2067 * requires GL_OES_texture_float) are filtered elsewhere.
2070 if (_mesa_is_gles(ctx
)) {
2071 if (_mesa_is_gles3(ctx
)) {
2072 err
= _mesa_es3_error_check_format_and_type(format
, type
,
2075 if (format
!= internalFormat
) {
2076 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2077 "glTexImage%dD(format = %s, internalFormat = %s)",
2079 _mesa_lookup_enum_by_nr(format
),
2080 _mesa_lookup_enum_by_nr(internalFormat
));
2084 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2086 if (err
!= GL_NO_ERROR
) {
2087 _mesa_error(ctx
, err
,
2088 "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
2090 _mesa_lookup_enum_by_nr(format
),
2091 _mesa_lookup_enum_by_nr(type
),
2092 _mesa_lookup_enum_by_nr(internalFormat
));
2097 /* Check internalFormat */
2098 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2099 _mesa_error(ctx
, GL_INVALID_VALUE
,
2100 "glTexImage%dD(internalFormat=%s)",
2101 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2105 /* Check incoming image format and type */
2106 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2107 if (err
!= GL_NO_ERROR
) {
2108 _mesa_error(ctx
, err
,
2109 "glTexImage%dD(incompatible format = %s, type = %s)",
2110 dimensions
, _mesa_lookup_enum_by_nr(format
),
2111 _mesa_lookup_enum_by_nr(type
));
2115 /* make sure internal format and format basically agree */
2116 if (!texture_formats_agree(internalFormat
, format
)) {
2117 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2118 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
2119 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
),
2120 _mesa_lookup_enum_by_nr(format
));
2124 /* additional checks for ycbcr textures */
2125 if (internalFormat
== GL_YCBCR_MESA
) {
2126 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
2127 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
2128 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
2130 _mesa_snprintf(message
, sizeof(message
),
2131 "glTexImage%dD(format/type YCBCR mismatch)",
2133 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
2134 return GL_TRUE
; /* error */
2136 if (target
!= GL_TEXTURE_2D
&&
2137 target
!= GL_PROXY_TEXTURE_2D
&&
2138 target
!= GL_TEXTURE_RECTANGLE_NV
&&
2139 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
2140 _mesa_error(ctx
, GL_INVALID_ENUM
,
2141 "glTexImage%dD(bad target for YCbCr texture)",
2147 _mesa_snprintf(message
, sizeof(message
),
2148 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2149 dimensions
, border
);
2150 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
2155 /* additional checks for depth textures */
2156 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
,
2157 dimensions
, "glTexImage"))
2160 /* additional checks for compressed textures */
2161 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2162 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2163 _mesa_error(ctx
, GL_INVALID_ENUM
,
2164 "glTexImage%dD(target can't be compressed)", dimensions
);
2167 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2168 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2169 "glTexImage%dD(no compression for format)", dimensions
);
2173 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2174 "glTexImage%dD(border!=0)", dimensions
);
2179 /* additional checks for integer textures */
2180 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
2181 (_mesa_is_enum_format_integer(format
) !=
2182 _mesa_is_enum_format_integer(internalFormat
))) {
2183 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2184 "glTexImage%dD(integer/non-integer format mismatch)",
2189 if (!mutable_tex_object(ctx
, target
)) {
2190 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2191 "glTexImage%dD(immutable texture)", dimensions
);
2195 /* if we get here, the parameters are OK */
2201 * Error checking for glCompressedTexImage[123]D().
2202 * Note that the width, height and depth values are not fully error checked
2204 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2207 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
2208 GLenum target
, GLint level
,
2209 GLenum internalFormat
, GLsizei width
,
2210 GLsizei height
, GLsizei depth
, GLint border
,
2213 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
2215 GLenum error
= GL_NO_ERROR
;
2216 char *reason
= ""; /* no error */
2218 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2220 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
2222 * "The ETC2/EAC texture compression algorithm supports only
2223 * two-dimensional images. If internalformat is an ETC2/EAC format,
2224 * CompressedTexImage3D will generate an INVALID_OPERATION error if
2225 * target is not TEXTURE_2D_ARRAY."
2227 error
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
2231 /* This will detect any invalid internalFormat value */
2232 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
2233 _mesa_error(ctx
, GL_INVALID_ENUM
,
2234 "glCompressedTexImage%dD(internalFormat=%s)",
2235 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2239 switch (internalFormat
) {
2240 case GL_PALETTE4_RGB8_OES
:
2241 case GL_PALETTE4_RGBA8_OES
:
2242 case GL_PALETTE4_R5_G6_B5_OES
:
2243 case GL_PALETTE4_RGBA4_OES
:
2244 case GL_PALETTE4_RGB5_A1_OES
:
2245 case GL_PALETTE8_RGB8_OES
:
2246 case GL_PALETTE8_RGBA8_OES
:
2247 case GL_PALETTE8_R5_G6_B5_OES
:
2248 case GL_PALETTE8_RGBA4_OES
:
2249 case GL_PALETTE8_RGB5_A1_OES
:
2250 /* check level (note that level should be zero or less!) */
2251 if (level
> 0 || level
< -maxLevels
) {
2253 error
= GL_INVALID_VALUE
;
2257 if (dimensions
!= 2) {
2258 reason
= "compressed paletted textures must be 2D";
2259 error
= GL_INVALID_OPERATION
;
2263 /* Figure out the expected texture size (in bytes). This will be
2264 * checked against the actual size later.
2266 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
2269 /* This is for the benefit of the TestProxyTexImage below. It expects
2270 * level to be non-negative. OES_compressed_paletted_texture uses a
2271 * weird mechanism where the level specified to glCompressedTexImage2D
2272 * is -(n-1) number of levels in the texture, and the data specifies the
2273 * complete mipmap stack. This is done to ensure the palette is the
2274 * same for all levels.
2281 if (level
< 0 || level
>= maxLevels
) {
2283 error
= GL_INVALID_VALUE
;
2287 /* Figure out the expected texture size (in bytes). This will be
2288 * checked against the actual size later.
2290 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2294 /* This should really never fail */
2295 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2296 reason
= "internalFormat";
2297 error
= GL_INVALID_ENUM
;
2301 /* No compressed formats support borders at this time */
2303 reason
= "border != 0";
2304 error
= GL_INVALID_VALUE
;
2308 /* Check for invalid pixel storage modes */
2309 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2311 "glCompressedTexImage")) {
2315 /* check image size in bytes */
2316 if (expectedSize
!= imageSize
) {
2317 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2318 * if <imageSize> is not consistent with the format, dimensions, and
2319 * contents of the specified image.
2321 reason
= "imageSize inconsistant with width/height/format";
2322 error
= GL_INVALID_VALUE
;
2326 if (!mutable_tex_object(ctx
, target
)) {
2327 reason
= "immutable texture";
2328 error
= GL_INVALID_OPERATION
;
2335 /* Note: not all error paths exit through here. */
2336 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2337 dimensions
, reason
);
2344 * Test glTexSubImage[123]D() parameters for errors.
2346 * \param ctx GL context.
2347 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2348 * \param target texture target given by the user (already validated)
2349 * \param level image level given by the user.
2350 * \param xoffset sub-image x offset given by the user.
2351 * \param yoffset sub-image y offset given by the user.
2352 * \param zoffset sub-image z offset given by the user.
2353 * \param format pixel data format given by the user.
2354 * \param type pixel data type given by the user.
2355 * \param width image width given by the user.
2356 * \param height image height given by the user.
2357 * \param depth image depth given by the user.
2359 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2361 * Verifies each of the parameters against the constants specified in
2362 * __struct gl_contextRec::Const and the supported extensions, and according
2363 * to the OpenGL specification.
2366 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2367 struct gl_texture_object
*texObj
,
2368 GLenum target
, GLint level
,
2369 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2370 GLint width
, GLint height
, GLint depth
,
2371 GLenum format
, GLenum type
, bool dsa
)
2373 struct gl_texture_image
*texImage
;
2375 const char* suffix
= dsa
? "ture" : "";
2378 /* must be out of memory */
2379 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTex%sSubImage%dD()",
2380 suffix
, dimensions
);
2384 /* check target (proxies not allowed) */
2385 if (!legal_texsubimage_target(ctx
, dimensions
, target
, dsa
)) {
2386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTex%sSubImage%uD(target=%s)",
2387 suffix
, dimensions
, _mesa_lookup_enum_by_nr(target
));
2392 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2393 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTex%sSubImage%uD(level=%d)",
2394 suffix
, dimensions
, level
);
2398 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2399 * combinations of format and type that can be used. Formats and types
2400 * that require additional extensions (e.g., GL_FLOAT requires
2401 * GL_OES_texture_float) are filtered elsewhere.
2403 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2404 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2405 if (err
!= GL_NO_ERROR
) {
2406 _mesa_error(ctx
, err
,
2407 "glTex%sSubImage%dD(format = %s, type = %s)",
2408 suffix
, dimensions
, _mesa_lookup_enum_by_nr(format
),
2409 _mesa_lookup_enum_by_nr(type
));
2414 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2415 if (err
!= GL_NO_ERROR
) {
2416 _mesa_error(ctx
, err
,
2417 "glTex%sSubImage%dD(incompatible format = %s, type = %s)",
2418 suffix
, dimensions
, _mesa_lookup_enum_by_nr(format
),
2419 _mesa_lookup_enum_by_nr(type
));
2423 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2425 /* non-existant texture level */
2426 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2427 "glTex%sSubImage%dD(invalid texture image)", suffix
,
2432 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2433 texImage
, xoffset
, yoffset
, 0,
2435 dsa
? "glTextureSubImage" :
2440 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2441 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2442 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2443 "glTex%sSubImage%dD(no compression for format)",
2444 suffix
, dimensions
);
2449 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2450 /* both source and dest must be integer-valued, or neither */
2451 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2452 _mesa_is_enum_format_integer(format
)) {
2453 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2454 "glTex%sSubImage%dD(integer/non-integer format mismatch)",
2455 suffix
, dimensions
);
2465 * Test glCopyTexImage[12]D() parameters for errors.
2467 * \param ctx GL context.
2468 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2469 * \param target texture target given by the user.
2470 * \param level image level given by the user.
2471 * \param internalFormat internal format given by the user.
2472 * \param width image width given by the user.
2473 * \param height image height given by the user.
2474 * \param border texture border.
2476 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2478 * Verifies each of the parameters against the constants specified in
2479 * __struct gl_contextRec::Const and the supported extensions, and according
2480 * to the OpenGL specification.
2483 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2484 GLenum target
, GLint level
, GLint internalFormat
,
2485 GLint width
, GLint height
, GLint border
)
2488 GLint rb_base_format
;
2489 struct gl_renderbuffer
*rb
;
2490 GLenum rb_internal_format
;
2493 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2494 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2495 dimensions
, _mesa_lookup_enum_by_nr(target
));
2500 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2501 _mesa_error(ctx
, GL_INVALID_VALUE
,
2502 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2506 /* Check that the source buffer is complete */
2507 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2508 if (ctx
->ReadBuffer
->_Status
== 0) {
2509 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2511 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2512 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2513 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2517 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2518 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2519 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2525 if (border
< 0 || border
> 1 ||
2526 ((ctx
->API
!= API_OPENGL_COMPAT
||
2527 target
== GL_TEXTURE_RECTANGLE_NV
||
2528 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2529 _mesa_error(ctx
, GL_INVALID_VALUE
,
2530 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2534 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2536 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2537 "glCopyTexImage%dD(read buffer)", dimensions
);
2541 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2544 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2545 switch (internalFormat
) {
2550 case GL_LUMINANCE_ALPHA
:
2553 _mesa_error(ctx
, GL_INVALID_VALUE
,
2554 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2555 _mesa_lookup_enum_by_nr(internalFormat
));
2560 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2561 if (baseFormat
< 0) {
2562 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2563 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2564 _mesa_lookup_enum_by_nr(internalFormat
));
2568 rb_internal_format
= rb
->InternalFormat
;
2569 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2570 if (_mesa_is_color_format(internalFormat
)) {
2571 if (rb_base_format
< 0) {
2572 _mesa_error(ctx
, GL_INVALID_VALUE
,
2573 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2574 _mesa_lookup_enum_by_nr(internalFormat
));
2579 if (_mesa_is_gles(ctx
)) {
2581 if (_mesa_base_format_component_count(baseFormat
) >
2582 _mesa_base_format_component_count(rb_base_format
)) {
2585 if (baseFormat
== GL_DEPTH_COMPONENT
||
2586 baseFormat
== GL_DEPTH_STENCIL
||
2587 rb_base_format
== GL_DEPTH_COMPONENT
||
2588 rb_base_format
== GL_DEPTH_STENCIL
||
2589 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2590 baseFormat
== GL_ALPHA
) &&
2591 rb_base_format
!= GL_RGBA
) ||
2592 internalFormat
== GL_RGB9_E5
) {
2595 if (internalFormat
== GL_RGB9_E5
) {
2599 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2600 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2601 _mesa_lookup_enum_by_nr(internalFormat
));
2606 if (_mesa_is_gles3(ctx
)) {
2607 bool rb_is_srgb
= false;
2608 bool dst_is_srgb
= false;
2610 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2611 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2615 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2619 if (rb_is_srgb
!= dst_is_srgb
) {
2620 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2621 * OpenGLES 3.0.0 spec says:
2623 * "The error INVALID_OPERATION is also generated if the
2624 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2625 * framebuffer attachment corresponding to the read buffer
2626 * is LINEAR (see section 6.1.13) and internalformat is
2627 * one of the sRGB formats described in section 3.8.16, or
2628 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2629 * SRGB and internalformat is not one of the sRGB formats."
2631 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2632 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2636 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2637 * types for SNORM formats. Also, conversion to SNORM formats is not
2638 * allowed by Table 3.2 on Page 110.
2640 if(_mesa_is_enum_format_snorm(internalFormat
)) {
2641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2642 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2643 _mesa_lookup_enum_by_nr(internalFormat
));
2648 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2649 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2650 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2654 /* From the EXT_texture_integer spec:
2656 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2657 * if the texture internalformat is an integer format and the read color
2658 * buffer is not an integer format, or if the internalformat is not an
2659 * integer format and the read color buffer is an integer format."
2661 if (_mesa_is_color_format(internalFormat
)) {
2662 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2663 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2664 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2665 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2666 if (is_int
|| is_rbint
) {
2667 if (is_int
!= is_rbint
) {
2668 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2669 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2671 } else if (_mesa_is_gles(ctx
) &&
2672 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2673 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2674 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2675 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2680 /* From page 138 of OpenGL ES 3.0 spec:
2681 * "The error INVALID_OPERATION is generated if floating-point RGBA
2682 * data is required; if signed integer RGBA data is required and the
2683 * format of the current color buffer is not signed integer; if
2684 * unsigned integer RGBA data is required and the format of the
2685 * current color buffer is not unsigned integer; or if fixed-point
2686 * RGBA data is required and the format of the current color buffer
2687 * is not fixed-point.
2689 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2690 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2691 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2694 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2695 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2696 _mesa_error(ctx
, GL_INVALID_ENUM
,
2697 "glCopyTexImage%dD(target)", dimensions
);
2700 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2701 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2702 "glCopyTexImage%dD(no compression for format)", dimensions
);
2706 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2707 "glCopyTexImage%dD(border!=0)", dimensions
);
2712 if (!mutable_tex_object(ctx
, target
)) {
2713 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2714 "glCopyTexImage%dD(immutable texture)", dimensions
);
2718 /* if we get here, the parameters are OK */
2724 * Test glCopyTexSubImage[12]D() parameters for errors.
2725 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2728 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2729 const struct gl_texture_object
*texObj
,
2730 GLenum target
, GLint level
,
2731 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2732 GLint width
, GLint height
, bool dsa
)
2734 struct gl_texture_image
*texImage
;
2735 const char *suffix
= dsa
? "ture" : "";
2737 /* Check that the source buffer is complete */
2738 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2739 if (ctx
->ReadBuffer
->_Status
== 0) {
2740 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2742 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2743 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2744 "glCopyTex%sSubImage%dD(invalid readbuffer)",
2745 suffix
, dimensions
);
2749 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2750 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2751 "glCopyTex%sSubImage%dD(multisample FBO)", suffix
,
2757 /* check target (proxies not allowed) */
2758 if (!legal_texsubimage_target(ctx
, dimensions
, target
, dsa
)) {
2759 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTex%sSubImage%uD(target=%s)",
2761 _mesa_lookup_enum_by_nr(target
));
2766 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2767 _mesa_error(ctx
, GL_INVALID_VALUE
,
2768 "glCopyTex%sSubImage%dD(level=%d)", suffix
,
2773 /* Get dest image pointers */
2775 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTex%sSubImage%dD()",
2776 suffix
, dimensions
);
2780 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2782 /* destination image does not exist */
2783 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2784 "glCopyTex%sSubImage%dD(invalid texture image)",
2785 suffix
, dimensions
);
2789 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2790 xoffset
, yoffset
, zoffset
,
2791 width
, height
, 1, dsa
?
2792 "glCompressedTextureSubImage" :
2793 "glCompressedTexSubImage")) {
2797 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2798 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2799 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2800 "glCopyTex%sSubImage%dD(no compression for format)",
2801 suffix
, dimensions
);
2806 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2807 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTex%sSubImage2D", suffix
);
2811 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2812 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2813 "glCopyTex%sSubImage%dD(missing readbuffer, format=0x%x)",
2814 suffix
, dimensions
, texImage
->_BaseFormat
);
2818 /* From the EXT_texture_integer spec:
2820 * "INVALID_OPERATION is generated by CopyTexImage* and
2821 * CopyTexSubImage* if the texture internalformat is an integer format
2822 * and the read color buffer is not an integer format, or if the
2823 * internalformat is not an integer format and the read color buffer
2824 * is an integer format."
2826 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2827 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2829 if (_mesa_is_format_integer_color(rb
->Format
) !=
2830 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2831 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2832 "glCopyTex%sSubImage%dD(integer vs non-integer)",
2833 suffix
, dimensions
);
2838 /* if we get here, the parameters are OK */
2843 /** Callback info for walking over FBO hash table */
2846 struct gl_context
*ctx
;
2847 struct gl_texture_object
*texObj
;
2853 * Check render to texture callback. Called from _mesa_HashWalk().
2856 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2858 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2859 const struct cb_info
*info
= (struct cb_info
*) userData
;
2860 struct gl_context
*ctx
= info
->ctx
;
2861 const struct gl_texture_object
*texObj
= info
->texObj
;
2862 const GLuint level
= info
->level
, face
= info
->face
;
2864 /* If this is a user-created FBO */
2865 if (_mesa_is_user_fbo(fb
)) {
2867 /* check if any of the FBO's attachments point to 'texObj' */
2868 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2869 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2870 if (att
->Type
== GL_TEXTURE
&&
2871 att
->Texture
== texObj
&&
2872 att
->TextureLevel
== level
&&
2873 att
->CubeMapFace
== face
) {
2874 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2875 ASSERT(att
->Renderbuffer
->TexImage
);
2876 /* Mark fb status as indeterminate to force re-validation */
2885 * When a texture image is specified we have to check if it's bound to
2886 * any framebuffer objects (render to texture) in order to detect changes
2887 * in size or format since that effects FBO completeness.
2888 * Any FBOs rendering into the texture must be re-validated.
2891 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2892 struct gl_texture_object
*texObj
,
2893 GLuint face
, GLuint level
)
2895 /* Only check this texture if it's been marked as RenderToTexture */
2896 if (texObj
->_RenderToTexture
) {
2897 struct cb_info info
;
2899 info
.texObj
= texObj
;
2902 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2908 * If the texture object's GenerateMipmap flag is set and we've
2909 * changed the texture base level image, regenerate the rest of the
2910 * mipmap levels now.
2913 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2914 struct gl_texture_object
*texObj
, GLint level
)
2916 if (texObj
->GenerateMipmap
&&
2917 level
== texObj
->BaseLevel
&&
2918 level
< texObj
->MaxLevel
) {
2919 ASSERT(ctx
->Driver
.GenerateMipmap
);
2920 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2925 /** Debug helper: override the user-requested internal format */
2927 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2930 if (internalFormat
== GL_RGBA16F_ARB
||
2931 internalFormat
== GL_RGBA32F_ARB
) {
2932 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2935 else if (internalFormat
== GL_RGB16F_ARB
||
2936 internalFormat
== GL_RGB32F_ARB
) {
2937 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2940 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2941 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2942 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2943 return GL_LUMINANCE_ALPHA
;
2945 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2946 internalFormat
== GL_LUMINANCE32F_ARB
) {
2947 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2948 return GL_LUMINANCE
;
2950 else if (internalFormat
== GL_ALPHA16F_ARB
||
2951 internalFormat
== GL_ALPHA32F_ARB
) {
2952 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2956 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2957 internalFormat = GL_RGBA;
2961 return internalFormat
;
2964 return internalFormat
;
2970 * Choose the actual hardware format for a texture image.
2971 * Try to use the same format as the previous image level when possible.
2972 * Otherwise, ask the driver for the best format.
2973 * It's important to try to choose a consistant format for all levels
2974 * for efficient texture memory layout/allocation. In particular, this
2975 * comes up during automatic mipmap generation.
2978 _mesa_choose_texture_format(struct gl_context
*ctx
,
2979 struct gl_texture_object
*texObj
,
2980 GLenum target
, GLint level
,
2981 GLenum internalFormat
, GLenum format
, GLenum type
)
2985 /* see if we've already chosen a format for the previous level */
2987 struct gl_texture_image
*prevImage
=
2988 _mesa_select_tex_image(texObj
, target
, level
- 1);
2989 /* See if the prev level is defined and has an internal format which
2990 * matches the new internal format.
2993 prevImage
->Width
> 0 &&
2994 prevImage
->InternalFormat
== internalFormat
) {
2995 /* use the same format */
2996 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2997 return prevImage
->TexFormat
;
3001 /* If the application requested compression to an S3TC format but we don't
3002 * have the DXTn library, force a generic compressed format instead.
3004 if (internalFormat
!= format
&& format
!= GL_NONE
) {
3005 const GLenum before
= internalFormat
;
3007 switch (internalFormat
) {
3008 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
3009 if (!ctx
->Mesa_DXTn
)
3010 internalFormat
= GL_COMPRESSED_RGB
;
3012 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
3013 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
3014 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
3015 if (!ctx
->Mesa_DXTn
)
3016 internalFormat
= GL_COMPRESSED_RGBA
;
3022 if (before
!= internalFormat
) {
3024 "DXT compression requested (%s), "
3025 "but libtxc_dxtn library not installed. Using %s "
3027 _mesa_lookup_enum_by_nr(before
),
3028 _mesa_lookup_enum_by_nr(internalFormat
));
3032 /* choose format from scratch */
3033 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
3035 ASSERT(f
!= MESA_FORMAT_NONE
);
3041 * Adjust pixel unpack params and image dimensions to strip off the
3042 * one-pixel texture border.
3044 * Gallium and intel don't support texture borders. They've seldem been used
3045 * and seldom been implemented correctly anyway.
3047 * \param unpackNew returns the new pixel unpack parameters
3050 strip_texture_border(GLenum target
,
3051 GLint
*width
, GLint
*height
, GLint
*depth
,
3052 const struct gl_pixelstore_attrib
*unpack
,
3053 struct gl_pixelstore_attrib
*unpackNew
)
3059 *unpackNew
= *unpack
;
3061 if (unpackNew
->RowLength
== 0)
3062 unpackNew
->RowLength
= *width
;
3064 if (unpackNew
->ImageHeight
== 0)
3065 unpackNew
->ImageHeight
= *height
;
3067 assert(*width
>= 3);
3068 unpackNew
->SkipPixels
++; /* skip the border */
3069 *width
= *width
- 2; /* reduce the width by two border pixels */
3071 /* The min height of a texture with a border is 3 */
3072 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
3073 unpackNew
->SkipRows
++; /* skip the border */
3074 *height
= *height
- 2; /* reduce the height by two border pixels */
3078 target
!= GL_TEXTURE_2D_ARRAY
&&
3079 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
3080 unpackNew
->SkipImages
++; /* skip the border */
3081 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
3087 * Common code to implement all the glTexImage1D/2D/3D functions
3088 * as well as glCompressedTexImage1D/2D/3D.
3089 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3090 * \param format the user's image format (only used if !compressed)
3091 * \param type the user's image type (only used if !compressed)
3092 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
3095 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
3096 GLenum target
, GLint level
, GLint internalFormat
,
3097 GLsizei width
, GLsizei height
, GLsizei depth
,
3098 GLint border
, GLenum format
, GLenum type
,
3099 GLsizei imageSize
, const GLvoid
*pixels
)
3101 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
3102 struct gl_pixelstore_attrib unpack_no_border
;
3103 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
3104 struct gl_texture_object
*texObj
;
3105 mesa_format texFormat
;
3106 GLboolean dimensionsOK
, sizeOK
;
3108 FLUSH_VERTICES(ctx
, 0);
3110 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
3113 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3115 _mesa_lookup_enum_by_nr(target
), level
,
3116 _mesa_lookup_enum_by_nr(internalFormat
),
3117 width
, height
, depth
, border
, pixels
);
3120 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3122 _mesa_lookup_enum_by_nr(target
), level
,
3123 _mesa_lookup_enum_by_nr(internalFormat
),
3124 width
, height
, depth
, border
,
3125 _mesa_lookup_enum_by_nr(format
),
3126 _mesa_lookup_enum_by_nr(type
), pixels
);
3129 internalFormat
= override_internal_format(internalFormat
, width
, height
);
3131 /* target error checking */
3132 if (!legal_teximage_target(ctx
, dims
, target
)) {
3133 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
3134 func
, dims
, _mesa_lookup_enum_by_nr(target
));
3138 /* general error checking */
3140 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
3142 width
, height
, depth
,
3147 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3148 format
, type
, width
, height
, depth
, border
))
3152 /* Here we convert a cpal compressed image into a regular glTexImage2D
3153 * call by decompressing the texture. If we really want to support cpal
3154 * textures in any driver this would have to be changed.
3156 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
3157 switch (internalFormat
) {
3158 case GL_PALETTE4_RGB8_OES
:
3159 case GL_PALETTE4_RGBA8_OES
:
3160 case GL_PALETTE4_R5_G6_B5_OES
:
3161 case GL_PALETTE4_RGBA4_OES
:
3162 case GL_PALETTE4_RGB5_A1_OES
:
3163 case GL_PALETTE8_RGB8_OES
:
3164 case GL_PALETTE8_RGBA8_OES
:
3165 case GL_PALETTE8_R5_G6_B5_OES
:
3166 case GL_PALETTE8_RGBA4_OES
:
3167 case GL_PALETTE8_RGB5_A1_OES
:
3168 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3169 width
, height
, imageSize
, pixels
);
3174 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3178 /* For glCompressedTexImage() the driver has no choice about the
3179 * texture format since we'll never transcode the user's compressed
3180 * image data. The internalFormat was error checked earlier.
3182 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
3185 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3186 internalFormat
, format
, type
);
3189 assert(texFormat
!= MESA_FORMAT_NONE
);
3191 /* check that width, height, depth are legal for the mipmap level */
3192 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
3193 height
, depth
, border
);
3195 /* check that the texture won't take too much memory, etc */
3196 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3198 width
, height
, depth
, border
);
3200 if (_mesa_is_proxy_texture(target
)) {
3201 /* Proxy texture: just clear or set state depending on error checking */
3202 struct gl_texture_image
*texImage
=
3203 get_proxy_tex_image(ctx
, target
, level
);
3206 return; /* GL_OUT_OF_MEMORY already recorded */
3208 if (dimensionsOK
&& sizeOK
) {
3209 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3210 border
, internalFormat
, texFormat
);
3213 clear_teximage_fields(texImage
);
3217 /* non-proxy target */
3218 const GLuint face
= _mesa_tex_target_to_face(target
);
3219 struct gl_texture_image
*texImage
;
3221 if (!dimensionsOK
) {
3222 _mesa_error(ctx
, GL_INVALID_VALUE
,
3223 "glTexImage%uD(invalid width or height or depth)",
3229 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3230 "glTexImage%uD(image too large)", dims
);
3234 /* Allow a hardware driver to just strip out the border, to provide
3235 * reliable but slightly incorrect hardware rendering instead of
3236 * rarely-tested software fallback rendering.
3238 if (border
&& ctx
->Const
.StripTextureBorder
) {
3239 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3242 unpack
= &unpack_no_border
;
3245 if (ctx
->NewState
& _NEW_PIXEL
)
3246 _mesa_update_state(ctx
);
3248 _mesa_lock_texture(ctx
, texObj
);
3250 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3253 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3256 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3258 _mesa_init_teximage_fields(ctx
, texImage
,
3259 width
, height
, depth
,
3260 border
, internalFormat
, texFormat
);
3262 /* Give the texture to the driver. <pixels> may be null. */
3263 if (width
> 0 && height
> 0 && depth
> 0) {
3265 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3269 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3270 type
, pixels
, unpack
);
3274 check_gen_mipmap(ctx
, target
, texObj
, level
);
3276 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3278 _mesa_dirty_texobj(ctx
, texObj
);
3281 _mesa_unlock_texture(ctx
, texObj
);
3288 * Called from the API. Note that width includes the border.
3291 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3292 GLsizei width
, GLint border
, GLenum format
,
3293 GLenum type
, const GLvoid
*pixels
)
3295 GET_CURRENT_CONTEXT(ctx
);
3296 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3297 border
, format
, type
, 0, pixels
);
3302 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3303 GLsizei width
, GLsizei height
, GLint border
,
3304 GLenum format
, GLenum type
,
3305 const GLvoid
*pixels
)
3307 GET_CURRENT_CONTEXT(ctx
);
3308 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3309 border
, format
, type
, 0, pixels
);
3314 * Called by the API or display list executor.
3315 * Note that width and height include the border.
3318 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3319 GLsizei width
, GLsizei height
, GLsizei depth
,
3320 GLint border
, GLenum format
, GLenum type
,
3321 const GLvoid
*pixels
)
3323 GET_CURRENT_CONTEXT(ctx
);
3324 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3325 width
, height
, depth
,
3326 border
, format
, type
, 0, pixels
);
3331 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3332 GLsizei width
, GLsizei height
, GLsizei depth
,
3333 GLint border
, GLenum format
, GLenum type
,
3334 const GLvoid
*pixels
)
3336 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3337 depth
, border
, format
, type
, pixels
);
3342 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3344 struct gl_texture_object
*texObj
;
3345 struct gl_texture_image
*texImage
;
3347 GET_CURRENT_CONTEXT(ctx
);
3348 FLUSH_VERTICES(ctx
, 0);
3352 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3354 case GL_TEXTURE_EXTERNAL_OES
:
3356 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3359 valid_target
= false;
3363 if (!valid_target
) {
3364 _mesa_error(ctx
, GL_INVALID_ENUM
,
3365 "glEGLImageTargetTexture2D(target=%d)", target
);
3370 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3371 "glEGLImageTargetTexture2D(image=%p)", image
);
3375 if (ctx
->NewState
& _NEW_PIXEL
)
3376 _mesa_update_state(ctx
);
3378 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3382 _mesa_lock_texture(ctx
, texObj
);
3384 if (texObj
->Immutable
) {
3385 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3386 "glEGLImageTargetTexture2D(texture is immutable)");
3387 _mesa_unlock_texture(ctx
, texObj
);
3391 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3393 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3395 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3397 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3398 texObj
, texImage
, image
);
3400 _mesa_dirty_texobj(ctx
, texObj
);
3402 _mesa_unlock_texture(ctx
, texObj
);
3408 * Helper that implements the glTexSubImage1/2/3D()
3409 * and glTextureSubImage1/2/3D() functions.
3412 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3413 struct gl_texture_object
*texObj
,
3414 struct gl_texture_image
*texImage
,
3415 GLenum target
, GLint level
,
3416 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3417 GLsizei width
, GLsizei height
, GLsizei depth
,
3418 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3421 FLUSH_VERTICES(ctx
, 0);
3423 /* check target (proxies not allowed) */
3424 if (!legal_texsubimage_target(ctx
, dims
, target
, dsa
)) {
3425 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTex%sSubImage%uD(target=%s)",
3427 dims
, _mesa_lookup_enum_by_nr(target
));
3431 if (ctx
->NewState
& _NEW_PIXEL
)
3432 _mesa_update_state(ctx
);
3434 _mesa_lock_texture(ctx
, texObj
);
3436 if (width
> 0 && height
> 0 && depth
> 0) {
3437 /* If we have a border, offset=-1 is legal. Bias by border width. */
3440 if (target
!= GL_TEXTURE_2D_ARRAY
)
3441 zoffset
+= texImage
->Border
;
3444 if (target
!= GL_TEXTURE_1D_ARRAY
)
3445 yoffset
+= texImage
->Border
;
3448 xoffset
+= texImage
->Border
;
3451 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3452 xoffset
, yoffset
, zoffset
,
3453 width
, height
, depth
,
3454 format
, type
, pixels
, &ctx
->Unpack
);
3456 check_gen_mipmap(ctx
, target
, texObj
, level
);
3458 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3459 * the texel data, not the texture format, size, etc.
3463 _mesa_unlock_texture(ctx
, texObj
);
3467 * Implement all the glTexSubImage1/2/3D() functions.
3468 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3471 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3472 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3473 GLsizei width
, GLsizei height
, GLsizei depth
,
3474 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3476 struct gl_texture_object
*texObj
;
3477 struct gl_texture_image
*texImage
;
3479 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3483 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3484 xoffset
, yoffset
, zoffset
,
3485 width
, height
, depth
, format
, type
, false)) {
3486 return; /* error was detected */
3489 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3490 /* texsubimage_error_check ensures that texImage is not NULL */
3492 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3493 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3495 _mesa_lookup_enum_by_nr(target
), level
,
3496 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3497 _mesa_lookup_enum_by_nr(format
),
3498 _mesa_lookup_enum_by_nr(type
), pixels
);
3500 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3501 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3502 format
, type
, pixels
, false);
3507 * Implement all the glTextureSubImage1/2/3D() functions.
3508 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3511 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3512 GLuint texture
, GLint level
,
3513 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3514 GLsizei width
, GLsizei height
, GLsizei depth
,
3515 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3517 struct gl_texture_object
*texObj
;
3518 struct gl_texture_image
*texImage
;
3521 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3523 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3524 dims
, texture
, level
,
3525 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3526 _mesa_lookup_enum_by_nr(format
),
3527 _mesa_lookup_enum_by_nr(type
), pixels
);
3529 /* Get the texture object by Name. */
3530 texObj
= _mesa_lookup_texture(ctx
, texture
);
3532 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3537 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3538 xoffset
, yoffset
, zoffset
,
3539 width
, height
, depth
, format
, type
, true)) {
3540 return; /* error was detected */
3544 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3545 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3547 /* Error checking */
3548 if (texObj
->NumLayers
< 6) {
3549 /* Not enough image planes for a cube map. The spec does not say
3550 * what should happen in this case because the user has always
3551 * specified each cube face separately (using
3552 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
3553 * This is addressed in Khronos Bug 13223.
3555 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3556 "glTextureSubImage%uD(insufficient cube map storage)",
3562 * What do we do if the user created a texture with the following code
3563 * and then called this function with its handle?
3566 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3567 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3568 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3569 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3570 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3571 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3572 * // wrong format, or given the wrong size, etc.
3573 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3574 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3576 * A bug has been filed against the spec for this case. In the
3577 * meantime, we will check for cube completeness.
3579 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3580 * Core Profile spec (30.10.2014):
3581 * "[A] cube map texture is cube complete if the
3582 * following conditions all hold true: The [base level] texture
3583 * images of each of the six cube map faces have identical, positive,
3584 * and square dimensions. The [base level] images were each specified
3585 * with the same internal format."
3587 * It seems reasonable to check for cube completeness of an arbitrary
3588 * level here so that the image data has a consistent format and size.
3590 if (!_mesa_cube_level_complete(texObj
, level
)) {
3591 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3592 "glTextureSubImage%uD(cube map incomplete)",
3597 /* Copy in each face. */
3598 for (i
= 0; i
< 6; ++i
) {
3599 texImage
= texObj
->Image
[i
][level
];
3600 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3601 level
, xoffset
, yoffset
, zoffset
,
3602 width
, height
, 1, format
,
3603 type
, pixels
, true);
3604 pixels
+= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3609 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3613 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3614 level
, xoffset
, yoffset
, zoffset
,
3615 width
, height
, depth
, format
,
3616 type
, pixels
, true);
3622 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3623 GLint xoffset
, GLsizei width
,
3624 GLenum format
, GLenum type
,
3625 const GLvoid
*pixels
)
3627 GET_CURRENT_CONTEXT(ctx
);
3628 texsubimage(ctx
, 1, target
, level
,
3631 format
, type
, pixels
);
3636 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3637 GLint xoffset
, GLint yoffset
,
3638 GLsizei width
, GLsizei height
,
3639 GLenum format
, GLenum type
,
3640 const GLvoid
*pixels
)
3642 GET_CURRENT_CONTEXT(ctx
);
3643 texsubimage(ctx
, 2, target
, level
,
3644 xoffset
, yoffset
, 0,
3646 format
, type
, pixels
);
3652 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3653 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3654 GLsizei width
, GLsizei height
, GLsizei depth
,
3655 GLenum format
, GLenum type
,
3656 const GLvoid
*pixels
)
3658 GET_CURRENT_CONTEXT(ctx
);
3659 texsubimage(ctx
, 3, target
, level
,
3660 xoffset
, yoffset
, zoffset
,
3661 width
, height
, depth
,
3662 format
, type
, pixels
);
3666 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3667 GLint xoffset
, GLsizei width
,
3668 GLenum format
, GLenum type
,
3669 const GLvoid
*pixels
)
3671 GET_CURRENT_CONTEXT(ctx
);
3672 texturesubimage(ctx
, 1, texture
, level
,
3675 format
, type
, pixels
);
3680 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3681 GLint xoffset
, GLint yoffset
,
3682 GLsizei width
, GLsizei height
,
3683 GLenum format
, GLenum type
,
3684 const GLvoid
*pixels
)
3686 GET_CURRENT_CONTEXT(ctx
);
3687 texturesubimage(ctx
, 2, texture
, level
,
3688 xoffset
, yoffset
, 0,
3690 format
, type
, pixels
);
3695 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3696 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3697 GLsizei width
, GLsizei height
, GLsizei depth
,
3698 GLenum format
, GLenum type
,
3699 const GLvoid
*pixels
)
3701 GET_CURRENT_CONTEXT(ctx
);
3702 texturesubimage(ctx
, 3, texture
, level
,
3703 xoffset
, yoffset
, zoffset
,
3704 width
, height
, depth
,
3705 format
, type
, pixels
);
3710 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3711 * from. This depends on whether the texture contains color or depth values.
3713 static struct gl_renderbuffer
*
3714 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3716 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3717 /* reading from depth/stencil buffer */
3718 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3721 /* copying from color buffer */
3722 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3727 copytexsubimage_by_slice(struct gl_context
*ctx
,
3728 struct gl_texture_image
*texImage
,
3730 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3731 struct gl_renderbuffer
*rb
,
3733 GLsizei width
, GLsizei height
)
3735 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3738 /* For 1D arrays, we copy each scanline of the source rectangle into the
3741 assert(zoffset
== 0);
3743 for (slice
= 0; slice
< height
; slice
++) {
3744 assert(yoffset
+ slice
< texImage
->Height
);
3745 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3746 xoffset
, 0, yoffset
+ slice
,
3747 rb
, x
, y
+ slice
, width
, 1);
3750 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3751 xoffset
, yoffset
, zoffset
,
3752 rb
, x
, y
, width
, height
);
3757 formats_differ_in_component_sizes (mesa_format f1
,
3760 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3761 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3762 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3763 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3765 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3766 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3767 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3768 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3770 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3771 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3772 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3773 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3780 * Implement the glCopyTexImage1/2D() functions.
3783 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3784 GLenum target
, GLint level
, GLenum internalFormat
,
3785 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3787 struct gl_texture_object
*texObj
;
3788 struct gl_texture_image
*texImage
;
3789 const GLuint face
= _mesa_tex_target_to_face(target
);
3790 mesa_format texFormat
;
3791 struct gl_renderbuffer
*rb
;
3793 FLUSH_VERTICES(ctx
, 0);
3795 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3796 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3798 _mesa_lookup_enum_by_nr(target
), level
,
3799 _mesa_lookup_enum_by_nr(internalFormat
),
3800 x
, y
, width
, height
, border
);
3802 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3803 _mesa_update_state(ctx
);
3805 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3806 width
, height
, border
))
3809 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3811 _mesa_error(ctx
, GL_INVALID_VALUE
,
3812 "glCopyTexImage%uD(invalid width or height)", dims
);
3816 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3819 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3820 internalFormat
, GL_NONE
, GL_NONE
);
3822 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3824 if (_mesa_is_gles3(ctx
)) {
3825 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3826 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3827 * OpenGL ES 3.0. Khronos bug# 9807.
3829 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3830 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3831 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer and"
3832 " writing to unsized internal format)", dims
);
3836 /* From Page 139 of OpenGL ES 3.0 spec:
3837 * "If internalformat is sized, the internal format of the new texel
3838 * array is internalformat, and this is also the new texel array’s
3839 * effective internal format. If the component sizes of internalformat
3840 * do not exactly match the corresponding component sizes of the source
3841 * buffer’s effective internal format, described below, an
3842 * INVALID_OPERATION error is generated. If internalformat is unsized,
3843 * the internal format of the new texel array is the effective internal
3844 * format of the source buffer, and this is also the new texel array’s
3845 * effective internal format.
3847 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3848 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3849 "glCopyTexImage%uD(componenet size changed in"
3850 " internal format)", dims
);
3855 assert(texFormat
!= MESA_FORMAT_NONE
);
3857 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3859 width
, height
, 1, border
)) {
3860 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3861 "glCopyTexImage%uD(image too large)", dims
);
3865 if (border
&& ctx
->Const
.StripTextureBorder
) {
3867 width
-= border
* 2;
3870 height
-= border
* 2;
3875 _mesa_lock_texture(ctx
, texObj
);
3877 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3880 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3883 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3885 /* Free old texture image */
3886 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3888 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3889 border
, internalFormat
, texFormat
);
3891 if (width
&& height
) {
3892 /* Allocate texture memory (no pixel data yet) */
3893 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3895 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3897 struct gl_renderbuffer
*srcRb
=
3898 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3900 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3902 srcRb
, srcX
, srcY
, width
, height
);
3905 check_gen_mipmap(ctx
, target
, texObj
, level
);
3908 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3910 _mesa_dirty_texobj(ctx
, texObj
);
3913 _mesa_unlock_texture(ctx
, texObj
);
3919 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3920 GLenum internalFormat
,
3922 GLsizei width
, GLint border
)
3924 GET_CURRENT_CONTEXT(ctx
);
3925 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3931 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3932 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3935 GET_CURRENT_CONTEXT(ctx
);
3936 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3937 x
, y
, width
, height
, border
);
3941 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3944 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3945 struct gl_texture_object
*texObj
,
3946 GLenum target
, GLint level
,
3947 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3949 GLsizei width
, GLsizei height
,
3952 struct gl_texture_image
*texImage
;
3954 FLUSH_VERTICES(ctx
, 0);
3956 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3957 _mesa_debug(ctx
, "glCopyTex%sSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3958 dsa
? "ture" : "", dims
,
3959 _mesa_lookup_enum_by_nr(target
),
3960 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3962 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3963 _mesa_update_state(ctx
);
3965 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3966 xoffset
, yoffset
, zoffset
,
3967 width
, height
, dsa
)) {
3971 _mesa_lock_texture(ctx
, texObj
);
3973 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3975 /* If we have a border, offset=-1 is legal. Bias by border width. */
3978 if (target
!= GL_TEXTURE_2D_ARRAY
)
3979 zoffset
+= texImage
->Border
;
3982 if (target
!= GL_TEXTURE_1D_ARRAY
)
3983 yoffset
+= texImage
->Border
;
3986 xoffset
+= texImage
->Border
;
3989 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3991 struct gl_renderbuffer
*srcRb
=
3992 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3994 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3995 xoffset
, yoffset
, zoffset
,
3996 srcRb
, x
, y
, width
, height
);
3998 check_gen_mipmap(ctx
, target
, texObj
, level
);
4000 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4001 * the texel data, not the texture format, size, etc.
4005 _mesa_unlock_texture(ctx
, texObj
);
4009 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
4010 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
4012 struct gl_texture_object
* texObj
;
4013 GET_CURRENT_CONTEXT(ctx
);
4015 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4019 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
4020 x
, y
, width
, 1, false);
4026 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
4027 GLint xoffset
, GLint yoffset
,
4028 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4030 struct gl_texture_object
* texObj
;
4031 GET_CURRENT_CONTEXT(ctx
);
4033 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4037 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
4038 xoffset
, yoffset
, 0,
4039 x
, y
, width
, height
, false);
4045 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
4046 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4047 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4049 struct gl_texture_object
* texObj
;
4050 GET_CURRENT_CONTEXT(ctx
);
4052 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4056 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
4057 xoffset
, yoffset
, zoffset
,
4058 x
, y
, width
, height
, false);
4062 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
4063 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
4065 struct gl_texture_object
* texObj
;
4066 GET_CURRENT_CONTEXT(ctx
);
4068 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage1D");
4072 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
4073 xoffset
, 0, 0, x
, y
, width
, 1, true);
4077 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
4078 GLint xoffset
, GLint yoffset
,
4079 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4081 struct gl_texture_object
* texObj
;
4082 GET_CURRENT_CONTEXT(ctx
);
4084 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage2D");
4088 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
4089 xoffset
, yoffset
, 0,
4090 x
, y
, width
, height
, true);
4096 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
4097 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4098 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4100 struct gl_texture_object
* texObj
;
4101 GET_CURRENT_CONTEXT(ctx
);
4103 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage3D");
4107 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
4108 xoffset
, yoffset
, zoffset
,
4109 x
, y
, width
, height
, true);
4113 check_clear_tex_image(struct gl_context
*ctx
,
4114 const char *function
,
4115 struct gl_texture_image
*texImage
,
4116 GLenum format
, GLenum type
,
4118 GLubyte
*clearValue
)
4120 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4121 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
4122 GLenum internalFormat
= texImage
->InternalFormat
;
4125 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
4126 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4127 "%s(buffer texture)", function
);
4131 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
4132 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4133 "%s(compressed texture)", function
);
4137 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
4138 if (err
!= GL_NO_ERROR
) {
4139 _mesa_error(ctx
, err
,
4140 "%s(incompatible format = %s, type = %s)",
4142 _mesa_lookup_enum_by_nr(format
),
4143 _mesa_lookup_enum_by_nr(type
));
4147 /* make sure internal format and format basically agree */
4148 if (!texture_formats_agree(internalFormat
, format
)) {
4149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4150 "%s(incompatible internalFormat = %s, format = %s)",
4152 _mesa_lookup_enum_by_nr(internalFormat
),
4153 _mesa_lookup_enum_by_nr(format
));
4157 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
4158 /* both source and dest must be integer-valued, or neither */
4159 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
4160 _mesa_is_enum_format_integer(format
)) {
4161 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4162 "%s(integer/non-integer format mismatch)",
4168 if (!_mesa_texstore(ctx
,
4170 texImage
->_BaseFormat
,
4171 texImage
->TexFormat
,
4172 0, /* dstRowStride */
4174 1, 1, 1, /* srcWidth/Height/Depth */
4176 data
? data
: zeroData
,
4177 &ctx
->DefaultPacking
)) {
4178 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
4185 static struct gl_texture_object
*
4186 get_tex_obj_for_clear(struct gl_context
*ctx
,
4187 const char *function
,
4190 struct gl_texture_object
*texObj
;
4193 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
4197 texObj
= _mesa_lookup_texture(ctx
, texture
);
4199 if (texObj
== NULL
) {
4200 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
4204 if (texObj
->Target
== 0) {
4205 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
4213 get_tex_images_for_clear(struct gl_context
*ctx
,
4214 const char *function
,
4215 struct gl_texture_object
*texObj
,
4217 struct gl_texture_image
**texImages
)
4222 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
4223 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4227 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4228 for (i
= 0; i
< MAX_FACES
; i
++) {
4229 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
4231 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
4232 if (texImages
[i
] == NULL
) {
4233 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4234 "%s(invalid level)", function
);
4242 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4244 if (texImages
[0] == NULL
) {
4245 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4253 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4254 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4255 GLsizei width
, GLsizei height
, GLsizei depth
,
4256 GLenum format
, GLenum type
, const void *data
)
4258 GET_CURRENT_CONTEXT(ctx
);
4259 struct gl_texture_object
*texObj
;
4260 struct gl_texture_image
*texImages
[MAX_FACES
];
4261 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4263 int minDepth
, maxDepth
;
4265 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4270 _mesa_lock_texture(ctx
, texObj
);
4272 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4273 texObj
, level
, texImages
);
4277 if (numImages
== 1) {
4278 minDepth
= -(int) texImages
[0]->Border
;
4279 maxDepth
= texImages
[0]->Depth
;
4282 maxDepth
= numImages
;
4285 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4286 yoffset
< -(GLint
) texImages
[0]->Border
||
4287 zoffset
< minDepth
||
4291 xoffset
+ width
> texImages
[0]->Width
||
4292 yoffset
+ height
> texImages
[0]->Height
||
4293 zoffset
+ depth
> maxDepth
) {
4294 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4295 "glClearSubTexImage(invalid dimensions)");
4299 if (numImages
== 1) {
4300 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4302 format
, type
, data
, clearValue
[0])) {
4303 ctx
->Driver
.ClearTexSubImage(ctx
,
4305 xoffset
, yoffset
, zoffset
,
4306 width
, height
, depth
,
4307 data
? clearValue
[0] : NULL
);
4310 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4311 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4313 format
, type
, data
, clearValue
[i
]))
4316 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4317 ctx
->Driver
.ClearTexSubImage(ctx
,
4319 xoffset
, yoffset
, 0,
4321 data
? clearValue
[i
] : NULL
);
4326 _mesa_unlock_texture(ctx
, texObj
);
4330 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4331 GLenum format
, GLenum type
, const void *data
)
4333 GET_CURRENT_CONTEXT(ctx
);
4334 struct gl_texture_object
*texObj
;
4335 struct gl_texture_image
*texImages
[MAX_FACES
];
4336 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4339 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4344 _mesa_lock_texture(ctx
, texObj
);
4346 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4347 texObj
, level
, texImages
);
4349 for (i
= 0; i
< numImages
; i
++) {
4350 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4357 for (i
= 0; i
< numImages
; i
++) {
4358 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4359 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4360 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4361 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4362 texImages
[i
]->Width
,
4363 texImages
[i
]->Height
,
4364 texImages
[i
]->Depth
,
4365 data
? clearValue
[i
] : NULL
);
4369 _mesa_unlock_texture(ctx
, texObj
);
4375 /**********************************************************************/
4376 /****** Compressed Textures ******/
4377 /**********************************************************************/
4381 * Error checking for glCompressedTexSubImage[123]D().
4382 * \return GL_TRUE if error, GL_FALSE if no error
4385 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4386 const struct gl_texture_object
*texObj
,
4387 GLenum target
, GLint level
,
4388 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4389 GLsizei width
, GLsizei height
, GLsizei depth
,
4390 GLenum format
, GLsizei imageSize
, bool dsa
)
4392 struct gl_texture_image
*texImage
;
4395 const char *suffix
= dsa
? "ture" : "";
4397 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4398 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4399 "glCompressedSubTexture%dD(target)", dims
);
4407 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4408 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4409 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4410 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4411 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4412 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4416 targetOK
= GL_FALSE
;
4421 targetOK
= (target
== GL_TEXTURE_3D
) ||
4422 (target
== GL_TEXTURE_2D_ARRAY
) ||
4423 (target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
4424 (target
== GL_TEXTURE_CUBE_MAP
&& dsa
);
4426 /* OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4428 * "An INVALID_OPERATION error is generated by
4429 * CompressedTex*SubImage3D if the internal format of the texture is
4430 * one of the EAC, ETC2, or RGTC formats and either border is
4431 * non-zero, or the effective target for the texture is not
4432 * TEXTURE_2D_ARRAY."
4434 if (target
!= GL_TEXTURE_2D_ARRAY
) {
4437 /* These came from _mesa_is_compressed_format in glformats.c. */
4439 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
4440 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
4441 case GL_COMPRESSED_R11_EAC
:
4442 case GL_COMPRESSED_RG11_EAC
:
4443 case GL_COMPRESSED_SIGNED_R11_EAC
:
4444 case GL_COMPRESSED_SIGNED_RG11_EAC
:
4446 case GL_COMPRESSED_RGB8_ETC2
:
4447 case GL_COMPRESSED_SRGB8_ETC2
:
4448 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
4449 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
4451 case GL_COMPRESSED_RED_RGTC1
:
4452 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
4453 case GL_COMPRESSED_RG_RGTC2
:
4454 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
4455 invalidformat
= true;
4458 invalidformat
= false;
4460 if (invalidformat
) {
4461 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4462 "glCompressedTex%sSubImage%uD(target)", suffix
, dims
);
4470 /* no 1D compressed textures at this time */
4471 targetOK
= GL_FALSE
;
4476 _mesa_error(ctx
, GL_INVALID_ENUM
,
4477 "glCompressedTex%sSubImage%uD(target)", suffix
, dims
);
4481 /* this will catch any invalid compressed format token */
4482 if (!_mesa_is_compressed_format(ctx
, format
)) {
4483 _mesa_error(ctx
, GL_INVALID_ENUM
,
4484 "glCompressedTex%sSubImage%uD(format)", suffix
, dims
);
4488 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4489 _mesa_error(ctx
, GL_INVALID_VALUE
,
4490 "glCompressedTex%sSubImage%uD(level=%d)",
4491 suffix
, dims
, level
);
4495 /* Check for invalid pixel storage modes */
4496 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4498 dsa
? "glCompressedTextureSubImage" :
4499 "glCompressedTexSubImage")) {
4503 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4504 if (expectedSize
!= imageSize
) {
4505 _mesa_error(ctx
, GL_INVALID_VALUE
,
4506 "glCompressedTex%sSubImage%uD(size=%d)",
4507 suffix
, dims
, imageSize
);
4511 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4513 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4514 "glCompressedTex%sSubImage%uD(invalid texture image)",
4519 if ((GLint
) format
!= texImage
->InternalFormat
) {
4520 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4521 "glCompressedTex%sSubImage%uD(format=0x%x)",
4522 suffix
, dims
, format
);
4526 if (compressedteximage_only_format(ctx
, format
)) {
4527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4528 "glCompressedTex%sSubImage%uD(format=0x%x cannot be updated)",
4529 suffix
, dims
, format
);
4533 if (error_check_subtexture_dimensions(ctx
, dims
,
4534 texImage
, xoffset
, yoffset
, zoffset
,
4535 width
, height
, depth
,
4536 "glCompressedTexSubImage")) {
4545 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4546 GLenum internalFormat
, GLsizei width
,
4547 GLint border
, GLsizei imageSize
,
4550 GET_CURRENT_CONTEXT(ctx
);
4551 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4552 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4557 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4558 GLenum internalFormat
, GLsizei width
,
4559 GLsizei height
, GLint border
, GLsizei imageSize
,
4562 GET_CURRENT_CONTEXT(ctx
);
4563 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4564 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4569 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4570 GLenum internalFormat
, GLsizei width
,
4571 GLsizei height
, GLsizei depth
, GLint border
,
4572 GLsizei imageSize
, const GLvoid
*data
)
4574 GET_CURRENT_CONTEXT(ctx
);
4575 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4576 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4581 * Common helper for glCompressedTexSubImage1/2/3D() and
4582 * glCompressedTextureSubImage1/2/3D().
4585 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4586 struct gl_texture_object
*texObj
,
4587 GLenum target
, GLint level
,
4588 GLint xoffset
, GLint yoffset
,
4590 GLsizei width
, GLsizei height
,
4592 GLenum format
, GLsizei imageSize
,
4593 const GLvoid
*data
, bool dsa
)
4595 struct gl_texture_image
*texImage
;
4597 if (compressed_subtexture_error_check(ctx
, dims
, texObj
, target
,
4598 level
, xoffset
, yoffset
, zoffset
,
4599 width
, height
, depth
,
4600 format
, imageSize
, dsa
)) {
4604 FLUSH_VERTICES(ctx
, 0);
4606 _mesa_lock_texture(ctx
, texObj
);
4608 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4611 if (width
> 0 && height
> 0 && depth
> 0) {
4612 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4613 xoffset
, yoffset
, zoffset
,
4614 width
, height
, depth
,
4615 format
, imageSize
, data
);
4617 check_gen_mipmap(ctx
, target
, texObj
, level
);
4619 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4620 * the texel data, not the texture format, size, etc.
4624 _mesa_unlock_texture(ctx
, texObj
);
4629 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4630 GLsizei width
, GLenum format
,
4631 GLsizei imageSize
, const GLvoid
*data
)
4633 struct gl_texture_object
*texObj
;
4634 GET_CURRENT_CONTEXT(ctx
);
4636 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4640 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, target
, level
,
4641 xoffset
, 0, 0, width
, 1, 1,
4642 format
, imageSize
, data
, false);
4646 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4647 GLsizei width
, GLenum format
,
4648 GLsizei imageSize
, const GLvoid
*data
)
4650 struct gl_texture_object
*texObj
;
4651 GET_CURRENT_CONTEXT(ctx
);
4653 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4654 "glCompressedTextureSubImage1D");
4658 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
4659 xoffset
, 0, 0, width
, 1, 1,
4660 format
, imageSize
, data
, true);
4665 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4666 GLint yoffset
, GLsizei width
, GLsizei height
,
4667 GLenum format
, GLsizei imageSize
,
4670 struct gl_texture_object
*texObj
;
4671 GET_CURRENT_CONTEXT(ctx
);
4673 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4677 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, target
, level
,
4678 xoffset
, yoffset
, 0, width
, height
, 1,
4679 format
, imageSize
, data
, false);
4683 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4685 GLsizei width
, GLsizei height
,
4686 GLenum format
, GLsizei imageSize
,
4689 struct gl_texture_object
*texObj
;
4690 GET_CURRENT_CONTEXT(ctx
);
4692 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4693 "glCompressedTextureSubImage2D");
4697 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
4698 xoffset
, yoffset
, 0, width
, height
, 1,
4699 format
, imageSize
, data
, true);
4703 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4704 GLint yoffset
, GLint zoffset
, GLsizei width
,
4705 GLsizei height
, GLsizei depth
, GLenum format
,
4706 GLsizei imageSize
, const GLvoid
*data
)
4708 struct gl_texture_object
*texObj
;
4709 GET_CURRENT_CONTEXT(ctx
);
4711 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4715 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, target
, level
,
4716 xoffset
, yoffset
, zoffset
,
4717 width
, height
, depth
,
4718 format
, imageSize
, data
, false);
4722 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4723 GLint yoffset
, GLint zoffset
, GLsizei width
,
4724 GLsizei height
, GLsizei depth
,
4725 GLenum format
, GLsizei imageSize
,
4728 struct gl_texture_object
*texObj
;
4729 GET_CURRENT_CONTEXT(ctx
);
4731 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4732 "glCompressedTextureSubImage3D");
4734 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
4735 xoffset
, yoffset
, zoffset
,
4736 width
, height
, depth
,
4737 format
, imageSize
, data
, true);
4741 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4743 if (ctx
->API
!= API_OPENGL_CORE
) {
4744 switch (internalFormat
) {
4746 return MESA_FORMAT_A_UNORM8
;
4748 return MESA_FORMAT_A_UNORM16
;
4749 case GL_ALPHA16F_ARB
:
4750 return MESA_FORMAT_A_FLOAT16
;
4751 case GL_ALPHA32F_ARB
:
4752 return MESA_FORMAT_A_FLOAT32
;
4753 case GL_ALPHA8I_EXT
:
4754 return MESA_FORMAT_A_SINT8
;
4755 case GL_ALPHA16I_EXT
:
4756 return MESA_FORMAT_A_SINT16
;
4757 case GL_ALPHA32I_EXT
:
4758 return MESA_FORMAT_A_SINT32
;
4759 case GL_ALPHA8UI_EXT
:
4760 return MESA_FORMAT_A_UINT8
;
4761 case GL_ALPHA16UI_EXT
:
4762 return MESA_FORMAT_A_UINT16
;
4763 case GL_ALPHA32UI_EXT
:
4764 return MESA_FORMAT_A_UINT32
;
4766 return MESA_FORMAT_L_UNORM8
;
4767 case GL_LUMINANCE16
:
4768 return MESA_FORMAT_L_UNORM16
;
4769 case GL_LUMINANCE16F_ARB
:
4770 return MESA_FORMAT_L_FLOAT16
;
4771 case GL_LUMINANCE32F_ARB
:
4772 return MESA_FORMAT_L_FLOAT32
;
4773 case GL_LUMINANCE8I_EXT
:
4774 return MESA_FORMAT_L_SINT8
;
4775 case GL_LUMINANCE16I_EXT
:
4776 return MESA_FORMAT_L_SINT16
;
4777 case GL_LUMINANCE32I_EXT
:
4778 return MESA_FORMAT_L_SINT32
;
4779 case GL_LUMINANCE8UI_EXT
:
4780 return MESA_FORMAT_L_UINT8
;
4781 case GL_LUMINANCE16UI_EXT
:
4782 return MESA_FORMAT_L_UINT16
;
4783 case GL_LUMINANCE32UI_EXT
:
4784 return MESA_FORMAT_L_UINT32
;
4785 case GL_LUMINANCE8_ALPHA8
:
4786 return MESA_FORMAT_L8A8_UNORM
;
4787 case GL_LUMINANCE16_ALPHA16
:
4788 return MESA_FORMAT_L16A16_UNORM
;
4789 case GL_LUMINANCE_ALPHA16F_ARB
:
4790 return MESA_FORMAT_LA_FLOAT16
;
4791 case GL_LUMINANCE_ALPHA32F_ARB
:
4792 return MESA_FORMAT_LA_FLOAT32
;
4793 case GL_LUMINANCE_ALPHA8I_EXT
:
4794 return MESA_FORMAT_LA_SINT8
;
4795 case GL_LUMINANCE_ALPHA16I_EXT
:
4796 return MESA_FORMAT_LA_SINT8
;
4797 case GL_LUMINANCE_ALPHA32I_EXT
:
4798 return MESA_FORMAT_LA_SINT16
;
4799 case GL_LUMINANCE_ALPHA8UI_EXT
:
4800 return MESA_FORMAT_LA_UINT8
;
4801 case GL_LUMINANCE_ALPHA16UI_EXT
:
4802 return MESA_FORMAT_LA_UINT16
;
4803 case GL_LUMINANCE_ALPHA32UI_EXT
:
4804 return MESA_FORMAT_LA_UINT32
;
4806 return MESA_FORMAT_I_UNORM8
;
4807 case GL_INTENSITY16
:
4808 return MESA_FORMAT_I_UNORM16
;
4809 case GL_INTENSITY16F_ARB
:
4810 return MESA_FORMAT_I_FLOAT16
;
4811 case GL_INTENSITY32F_ARB
:
4812 return MESA_FORMAT_I_FLOAT32
;
4813 case GL_INTENSITY8I_EXT
:
4814 return MESA_FORMAT_I_SINT8
;
4815 case GL_INTENSITY16I_EXT
:
4816 return MESA_FORMAT_I_SINT16
;
4817 case GL_INTENSITY32I_EXT
:
4818 return MESA_FORMAT_I_SINT32
;
4819 case GL_INTENSITY8UI_EXT
:
4820 return MESA_FORMAT_I_UINT8
;
4821 case GL_INTENSITY16UI_EXT
:
4822 return MESA_FORMAT_I_UINT16
;
4823 case GL_INTENSITY32UI_EXT
:
4824 return MESA_FORMAT_I_UINT32
;
4830 if (ctx
->API
== API_OPENGL_CORE
&&
4831 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4832 switch (internalFormat
) {
4834 return MESA_FORMAT_RGB_FLOAT32
;
4836 return MESA_FORMAT_RGB_UINT32
;
4838 return MESA_FORMAT_RGB_SINT32
;
4844 switch (internalFormat
) {
4846 return MESA_FORMAT_R8G8B8A8_UNORM
;
4848 return MESA_FORMAT_RGBA_UNORM16
;
4849 case GL_RGBA16F_ARB
:
4850 return MESA_FORMAT_RGBA_FLOAT16
;
4851 case GL_RGBA32F_ARB
:
4852 return MESA_FORMAT_RGBA_FLOAT32
;
4854 return MESA_FORMAT_RGBA_SINT8
;
4855 case GL_RGBA16I_EXT
:
4856 return MESA_FORMAT_RGBA_SINT16
;
4857 case GL_RGBA32I_EXT
:
4858 return MESA_FORMAT_RGBA_SINT32
;
4859 case GL_RGBA8UI_EXT
:
4860 return MESA_FORMAT_RGBA_UINT8
;
4861 case GL_RGBA16UI_EXT
:
4862 return MESA_FORMAT_RGBA_UINT16
;
4863 case GL_RGBA32UI_EXT
:
4864 return MESA_FORMAT_RGBA_UINT32
;
4867 return MESA_FORMAT_R8G8_UNORM
;
4869 return MESA_FORMAT_R16G16_UNORM
;
4871 return MESA_FORMAT_RG_FLOAT16
;
4873 return MESA_FORMAT_RG_FLOAT32
;
4875 return MESA_FORMAT_RG_SINT8
;
4877 return MESA_FORMAT_RG_SINT16
;
4879 return MESA_FORMAT_RG_SINT32
;
4881 return MESA_FORMAT_RG_UINT8
;
4883 return MESA_FORMAT_RG_UINT16
;
4885 return MESA_FORMAT_RG_UINT32
;
4888 return MESA_FORMAT_R_UNORM8
;
4890 return MESA_FORMAT_R_UNORM16
;
4892 return MESA_FORMAT_R_FLOAT16
;
4894 return MESA_FORMAT_R_FLOAT32
;
4896 return MESA_FORMAT_R_SINT8
;
4898 return MESA_FORMAT_R_SINT16
;
4900 return MESA_FORMAT_R_SINT32
;
4902 return MESA_FORMAT_R_UINT8
;
4904 return MESA_FORMAT_R_UINT16
;
4906 return MESA_FORMAT_R_UINT32
;
4909 return MESA_FORMAT_NONE
;
4915 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4916 GLenum internalFormat
)
4918 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4921 if (format
== MESA_FORMAT_NONE
)
4922 return MESA_FORMAT_NONE
;
4924 datatype
= _mesa_get_format_datatype(format
);
4926 /* The GL_ARB_texture_buffer_object spec says:
4928 * "If ARB_texture_float is not supported, references to the
4929 * floating-point internal formats provided by that extension should be
4930 * removed, and such formats may not be passed to TexBufferARB."
4932 * As a result, GL_HALF_FLOAT internal format depends on both
4933 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4935 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4936 !ctx
->Extensions
.ARB_texture_float
)
4937 return MESA_FORMAT_NONE
;
4939 if (!ctx
->Extensions
.ARB_texture_rg
) {
4940 GLenum base_format
= _mesa_get_format_base_format(format
);
4941 if (base_format
== GL_R
|| base_format
== GL_RG
)
4942 return MESA_FORMAT_NONE
;
4945 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4946 GLenum base_format
= _mesa_get_format_base_format(format
);
4947 if (base_format
== GL_RGB
)
4948 return MESA_FORMAT_NONE
;
4955 _mesa_texture_buffer_range(struct gl_context
*ctx
,
4956 struct gl_texture_object
*texObj
, GLenum target
,
4957 GLenum internalFormat
,
4958 struct gl_buffer_object
*bufObj
,
4959 GLintptr offset
, GLsizeiptr size
, bool range
,
4964 FLUSH_VERTICES(ctx
, 0);
4966 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4967 if (format
== MESA_FORMAT_NONE
) {
4968 _mesa_error(ctx
, GL_INVALID_ENUM
,
4969 "glTex%sBuffer%s(internalFormat 0x%x)", dsa
? "ture" : "",
4970 range
? "Range" : "", internalFormat
);
4974 _mesa_lock_texture(ctx
, texObj
);
4976 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4977 texObj
->BufferObjectFormat
= internalFormat
;
4978 texObj
->_BufferObjectFormat
= format
;
4979 texObj
->BufferOffset
= offset
;
4980 texObj
->BufferSize
= size
;
4982 _mesa_unlock_texture(ctx
, texObj
);
4984 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
4987 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
4992 /** GL_ARB_texture_buffer_object */
4994 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
4996 struct gl_texture_object
*texObj
;
4997 struct gl_buffer_object
*bufObj
;
4999 GET_CURRENT_CONTEXT(ctx
);
5001 /* Need to catch this before it gets to _mesa_get_current_tex_object */
5002 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
5003 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
5007 /* NOTE: ARB_texture_buffer_object has interactions with
5008 * the compatibility profile that are not implemented.
5010 if (!(ctx
->API
== API_OPENGL_CORE
&&
5011 ctx
->Extensions
.ARB_texture_buffer_object
)) {
5012 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
5016 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
5017 if (!bufObj
&& buffer
) {
5018 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
5022 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5026 _mesa_texture_buffer_range(ctx
, texObj
, target
, internalFormat
, bufObj
, 0,
5027 buffer
? -1 : 0, false, false);
5031 /** GL_ARB_texture_buffer_range */
5033 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5034 GLintptr offset
, GLsizeiptr size
)
5036 struct gl_texture_object
*texObj
;
5037 struct gl_buffer_object
*bufObj
;
5039 GET_CURRENT_CONTEXT(ctx
);
5041 /* Need to catch this before it gets to _mesa_get_current_tex_object */
5042 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
5043 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBufferRange(target)");
5047 if (!(ctx
->API
== API_OPENGL_CORE
&&
5048 ctx
->Extensions
.ARB_texture_buffer_range
)) {
5049 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange");
5053 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
5057 (offset
+ size
) > bufObj
->Size
) {
5058 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexBufferRange");
5061 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
5062 _mesa_error(ctx
, GL_INVALID_VALUE
,
5063 "glTexBufferRange(invalid offset alignment)");
5066 } else if (buffer
) {
5067 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange(buffer %u)",
5075 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5079 _mesa_texture_buffer_range(ctx
, texObj
, target
, internalFormat
, bufObj
,
5080 offset
, size
, true, false);
5084 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5086 struct gl_texture_object
*texObj
;
5087 struct gl_buffer_object
*bufObj
;
5089 GET_CURRENT_CONTEXT(ctx
);
5091 /* NOTE: ARB_texture_buffer_object has interactions with
5092 * the compatibility profile that are not implemented.
5094 if (!(ctx
->API
== API_OPENGL_CORE
&&
5095 ctx
->Extensions
.ARB_texture_buffer_object
)) {
5096 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureBuffer");
5100 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
5101 if (!bufObj
&& buffer
) {
5102 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureBuffer(buffer %u)",
5107 /* Get the texture object by Name. */
5108 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5109 "glTextureBuffer(texture)");
5113 if (texObj
->Target
!= GL_TEXTURE_BUFFER_ARB
) {
5114 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTextureBuffer(target)");
5118 _mesa_texture_buffer_range(ctx
, texObj
, texObj
->Target
, internalFormat
,
5119 bufObj
, 0, buffer
? -1 : 0, false, true);
5123 is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5125 /* Everything that is allowed for renderbuffers,
5126 * except for a base format of GL_STENCIL_INDEX.
5128 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5129 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5133 /** GL_ARB_texture_multisample */
5135 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5138 case GL_TEXTURE_2D_MULTISAMPLE
:
5140 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5141 return dims
== 2 && !dsa
;
5143 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5145 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5146 return dims
== 3 && !dsa
;
5155 _mesa_texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5156 struct gl_texture_object
*texObj
,
5157 GLenum target
, GLsizei samples
,
5158 GLint internalformat
, GLsizei width
,
5159 GLsizei height
, GLsizei depth
,
5160 GLboolean fixedsamplelocations
,
5161 GLboolean immutable
, const char *func
)
5163 struct gl_texture_image
*texImage
;
5164 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5165 mesa_format texFormat
;
5166 GLenum sample_count_error
;
5167 bool dsa
= strstr(func
, "ture") ? true : false;
5169 if (!(ctx
->Extensions
.ARB_texture_multisample
5170 && _mesa_is_desktop_gl(ctx
))) {
5171 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5175 if (!check_multisample_target(dims
, target
, dsa
)) {
5177 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5181 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5186 /* check that the specified internalformat is color/depth/stencil-renderable;
5187 * refer GL3.1 spec 4.4.4
5190 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5191 _mesa_error(ctx
, GL_INVALID_ENUM
,
5192 "%s(internalformat=%s not legal for immutable-format)",
5193 func
, _mesa_lookup_enum_by_nr(internalformat
));
5197 if (!is_renderable_texture_format(ctx
, internalformat
)) {
5198 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5199 "%s(internalformat=%s)",
5200 func
, _mesa_lookup_enum_by_nr(internalformat
));
5204 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5205 internalformat
, samples
);
5206 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5208 /* Page 254 of OpenGL 4.4 spec says:
5209 * "Proxy arrays for two-dimensional multisample and two-dimensional
5210 * multisample array textures are operated on in the same way when
5211 * TexImage2DMultisample is called with target specified as
5212 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5213 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5214 * However, if samples is not supported, then no error is generated.
5216 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5217 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5221 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5222 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5223 "%s(texture object 0)",
5228 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5230 if (texImage
== NULL
) {
5231 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5235 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5236 internalformat
, GL_NONE
, GL_NONE
);
5237 assert(texFormat
!= MESA_FORMAT_NONE
);
5239 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5240 width
, height
, depth
, 0);
5242 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5243 width
, height
, depth
, 0);
5245 if (_mesa_is_proxy_texture(target
)) {
5246 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5247 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5248 internalformat
, texFormat
,
5249 samples
, fixedsamplelocations
);
5252 /* clear all image fields */
5253 clear_teximage_fields(texImage
);
5257 if (!dimensionsOK
) {
5258 _mesa_error(ctx
, GL_INVALID_VALUE
,
5259 "%s(invalid width or height)", func
);
5264 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
5265 "%s(texture too large)", func
);
5269 /* Check if texObj->Immutable is set */
5270 if (texObj
->Immutable
) {
5271 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5275 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5277 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5278 internalformat
, texFormat
,
5279 samples
, fixedsamplelocations
);
5281 if (width
> 0 && height
> 0 && depth
> 0) {
5282 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5283 width
, height
, depth
)) {
5284 /* tidy up the texture image state. strictly speaking,
5285 * we're allowed to just leave this in whatever state we
5286 * like, but being tidy is good.
5288 _mesa_init_teximage_fields(ctx
, texImage
,
5289 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5293 texObj
->Immutable
|= immutable
;
5296 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5299 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5305 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5306 GLenum internalformat
, GLsizei width
,
5307 GLsizei height
, GLboolean fixedsamplelocations
)
5309 struct gl_texture_object
*texObj
;
5310 GET_CURRENT_CONTEXT(ctx
);
5312 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5316 _mesa_texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5317 internalformat
, width
, height
, 1,
5318 fixedsamplelocations
, GL_FALSE
,
5319 "glTexImage2DMultisample");
5324 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5325 GLenum internalformat
, GLsizei width
,
5326 GLsizei height
, GLsizei depth
,
5327 GLboolean fixedsamplelocations
)
5329 struct gl_texture_object
*texObj
;
5330 GET_CURRENT_CONTEXT(ctx
);
5332 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5336 _mesa_texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5337 internalformat
, width
, height
, depth
,
5338 fixedsamplelocations
, GL_FALSE
,
5339 "glTexImage3DMultisample");
5344 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5345 GLenum internalformat
, GLsizei width
,
5346 GLsizei height
, GLboolean fixedsamplelocations
)
5348 struct gl_texture_object
*texObj
;
5349 GET_CURRENT_CONTEXT(ctx
);
5351 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5355 _mesa_texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5356 internalformat
, width
, height
, 1,
5357 fixedsamplelocations
, GL_TRUE
,
5358 "glTexStorage2DMultisample");
5362 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5363 GLenum internalformat
, GLsizei width
,
5364 GLsizei height
, GLsizei depth
,
5365 GLboolean fixedsamplelocations
)
5367 struct gl_texture_object
*texObj
;
5368 GET_CURRENT_CONTEXT(ctx
);
5370 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5374 _mesa_texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5375 internalformat
, width
, height
, depth
,
5376 fixedsamplelocations
, GL_TRUE
,
5377 "glTexStorage3DMultisample");
5381 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5382 GLenum internalformat
, GLsizei width
,
5384 GLboolean fixedsamplelocations
)
5386 struct gl_texture_object
*texObj
;
5387 GET_CURRENT_CONTEXT(ctx
);
5389 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5390 "glTextureStorage2DMultisample");
5394 _mesa_texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5395 internalformat
, width
, height
, 1,
5396 fixedsamplelocations
, GL_TRUE
,
5397 "glTextureStorage2DMultisample");
5401 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5402 GLenum internalformat
, GLsizei width
,
5403 GLsizei height
, GLsizei depth
,
5404 GLboolean fixedsamplelocations
)
5406 struct gl_texture_object
*texObj
;
5407 GET_CURRENT_CONTEXT(ctx
);
5409 /* Get the texture object by Name. */
5410 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5411 "glTextureStorage3DMultisample");
5415 _mesa_texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5416 internalformat
, width
, height
, depth
,
5417 fixedsamplelocations
, GL_TRUE
,
5418 "glTextureStorage3DMultisample");