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
);
1874 return !texObj
->Immutable
;
1879 * Return expected size of a compressed texture.
1882 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1885 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1886 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1890 * Verify that a texture format is valid with a particular target
1892 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1893 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1896 * \param ctx GL context
1897 * \param target Texture target
1898 * \param internalFormat Internal format of the texture image
1899 * \param dimensions Dimensionality at the caller. This is \b not used
1900 * in the validation. It is only used when logging
1902 * \param caller Base name of the calling function (e.g.,
1903 * "glTexImage" or "glTexStorage").
1905 * \returns true if the combination is legal, false otherwise.
1908 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1909 GLenum target
, GLenum internalFormat
,
1910 unsigned dimensions
,
1913 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1914 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
) {
1915 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1916 * Profile spec says:
1918 * "Textures with a base internal format of DEPTH_COMPONENT or
1919 * DEPTH_STENCIL are supported by texture image specification
1920 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1921 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1922 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1923 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1924 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1925 * formats in conjunction with any other target will result in an
1926 * INVALID_OPERATION error."
1928 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1929 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1931 if (target
!= GL_TEXTURE_1D
&&
1932 target
!= GL_PROXY_TEXTURE_1D
&&
1933 target
!= GL_TEXTURE_2D
&&
1934 target
!= GL_PROXY_TEXTURE_2D
&&
1935 target
!= GL_TEXTURE_1D_ARRAY
&&
1936 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1937 target
!= GL_TEXTURE_2D_ARRAY
&&
1938 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1939 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1940 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1941 !((_mesa_is_cube_face(target
) ||
1942 target
== GL_TEXTURE_CUBE_MAP
||
1943 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1944 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1945 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1946 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1947 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1948 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1949 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1950 "%s%dD(bad target for depth texture)",
1951 caller
, dimensions
);
1960 texture_formats_agree(GLenum internalFormat
,
1963 GLboolean colorFormat
;
1964 GLboolean is_format_depth_or_depthstencil
;
1965 GLboolean is_internalFormat_depth_or_depthstencil
;
1967 /* Even though there are no color-index textures, we still have to support
1968 * uploading color-index data and remapping it to RGB via the
1969 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1971 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1973 is_internalFormat_depth_or_depthstencil
=
1974 _mesa_is_depth_format(internalFormat
) ||
1975 _mesa_is_depthstencil_format(internalFormat
);
1977 is_format_depth_or_depthstencil
=
1978 _mesa_is_depth_format(format
) ||
1979 _mesa_is_depthstencil_format(format
);
1981 colorFormat
= _mesa_is_color_format(format
);
1983 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1986 if (is_internalFormat_depth_or_depthstencil
!=
1987 is_format_depth_or_depthstencil
)
1990 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
1997 * Test the glTexImage[123]D() parameters for errors.
1999 * \param ctx GL context.
2000 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2001 * \param target texture target given by the user (already validated).
2002 * \param level image level given by the user.
2003 * \param internalFormat internal format given by the user.
2004 * \param format pixel data format given by the user.
2005 * \param type pixel data type given by the user.
2006 * \param width image width given by the user.
2007 * \param height image height given by the user.
2008 * \param depth image depth given by the user.
2009 * \param border image border given by the user.
2011 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2013 * Verifies each of the parameters against the constants specified in
2014 * __struct gl_contextRec::Const and the supported extensions, and according
2015 * to the OpenGL specification.
2016 * Note that we don't fully error-check the width, height, depth values
2017 * here. That's done in _mesa_legal_texture_dimensions() which is used
2018 * by several other GL entrypoints. Plus, texture dims have a special
2019 * interaction with proxy textures.
2022 texture_error_check( struct gl_context
*ctx
,
2023 GLuint dimensions
, GLenum target
,
2024 GLint level
, GLint internalFormat
,
2025 GLenum format
, GLenum type
,
2026 GLint width
, GLint height
,
2027 GLint depth
, GLint border
)
2031 /* Note: for proxy textures, some error conditions immediately generate
2032 * a GL error in the usual way. But others do not generate a GL error.
2033 * Instead, they cause the width, height, depth, format fields of the
2034 * texture image to be zeroed-out. The GL spec seems to indicate that the
2035 * zero-out behaviour is only used in cases related to memory allocation.
2039 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2040 _mesa_error(ctx
, GL_INVALID_VALUE
,
2041 "glTexImage%dD(level=%d)", dimensions
, level
);
2046 if (border
< 0 || border
> 1 ||
2047 ((ctx
->API
!= API_OPENGL_COMPAT
||
2048 target
== GL_TEXTURE_RECTANGLE_NV
||
2049 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2050 _mesa_error(ctx
, GL_INVALID_VALUE
,
2051 "glTexImage%dD(border=%d)", dimensions
, border
);
2055 if (width
< 0 || height
< 0 || depth
< 0) {
2056 _mesa_error(ctx
, GL_INVALID_VALUE
,
2057 "glTexImage%dD(width, height or depth < 0)", dimensions
);
2061 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2062 * combinations of format, internalFormat, and type that can be used.
2063 * Formats and types that require additional extensions (e.g., GL_FLOAT
2064 * requires GL_OES_texture_float) are filtered elsewhere.
2067 if (_mesa_is_gles(ctx
)) {
2068 if (_mesa_is_gles3(ctx
)) {
2069 err
= _mesa_es3_error_check_format_and_type(format
, type
,
2072 if (format
!= internalFormat
) {
2073 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2074 "glTexImage%dD(format = %s, internalFormat = %s)",
2076 _mesa_lookup_enum_by_nr(format
),
2077 _mesa_lookup_enum_by_nr(internalFormat
));
2081 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2083 if (err
!= GL_NO_ERROR
) {
2084 _mesa_error(ctx
, err
,
2085 "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
2087 _mesa_lookup_enum_by_nr(format
),
2088 _mesa_lookup_enum_by_nr(type
),
2089 _mesa_lookup_enum_by_nr(internalFormat
));
2094 /* Check internalFormat */
2095 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2096 _mesa_error(ctx
, GL_INVALID_VALUE
,
2097 "glTexImage%dD(internalFormat=%s)",
2098 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2102 /* Check incoming image format and type */
2103 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2104 if (err
!= GL_NO_ERROR
) {
2105 _mesa_error(ctx
, err
,
2106 "glTexImage%dD(incompatible format = %s, type = %s)",
2107 dimensions
, _mesa_lookup_enum_by_nr(format
),
2108 _mesa_lookup_enum_by_nr(type
));
2112 /* make sure internal format and format basically agree */
2113 if (!texture_formats_agree(internalFormat
, format
)) {
2114 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2115 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
2116 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
),
2117 _mesa_lookup_enum_by_nr(format
));
2121 /* additional checks for ycbcr textures */
2122 if (internalFormat
== GL_YCBCR_MESA
) {
2123 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
2124 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
2125 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
2127 _mesa_snprintf(message
, sizeof(message
),
2128 "glTexImage%dD(format/type YCBCR mismatch)",
2130 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
2131 return GL_TRUE
; /* error */
2133 if (target
!= GL_TEXTURE_2D
&&
2134 target
!= GL_PROXY_TEXTURE_2D
&&
2135 target
!= GL_TEXTURE_RECTANGLE_NV
&&
2136 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
2137 _mesa_error(ctx
, GL_INVALID_ENUM
,
2138 "glTexImage%dD(bad target for YCbCr texture)",
2144 _mesa_snprintf(message
, sizeof(message
),
2145 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2146 dimensions
, border
);
2147 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
2152 /* additional checks for depth textures */
2153 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
,
2154 dimensions
, "glTexImage"))
2157 /* additional checks for compressed textures */
2158 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2159 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2160 _mesa_error(ctx
, GL_INVALID_ENUM
,
2161 "glTexImage%dD(target can't be compressed)", dimensions
);
2164 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2166 "glTexImage%dD(no compression for format)", dimensions
);
2170 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2171 "glTexImage%dD(border!=0)", dimensions
);
2176 /* additional checks for integer textures */
2177 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
2178 (_mesa_is_enum_format_integer(format
) !=
2179 _mesa_is_enum_format_integer(internalFormat
))) {
2180 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2181 "glTexImage%dD(integer/non-integer format mismatch)",
2186 if (!mutable_tex_object(ctx
, target
)) {
2187 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2188 "glTexImage%dD(immutable texture)", dimensions
);
2192 /* if we get here, the parameters are OK */
2198 * Error checking for glCompressedTexImage[123]D().
2199 * Note that the width, height and depth values are not fully error checked
2201 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2204 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
2205 GLenum target
, GLint level
,
2206 GLenum internalFormat
, GLsizei width
,
2207 GLsizei height
, GLsizei depth
, GLint border
,
2210 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
2212 GLenum error
= GL_NO_ERROR
;
2213 char *reason
= ""; /* no error */
2215 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2217 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
2219 * "The ETC2/EAC texture compression algorithm supports only
2220 * two-dimensional images. If internalformat is an ETC2/EAC format,
2221 * CompressedTexImage3D will generate an INVALID_OPERATION error if
2222 * target is not TEXTURE_2D_ARRAY."
2224 error
= _mesa_is_desktop_gl(ctx
) ? GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
2228 /* This will detect any invalid internalFormat value */
2229 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
2230 _mesa_error(ctx
, GL_INVALID_ENUM
,
2231 "glCompressedTexImage%dD(internalFormat=%s)",
2232 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2236 switch (internalFormat
) {
2237 case GL_PALETTE4_RGB8_OES
:
2238 case GL_PALETTE4_RGBA8_OES
:
2239 case GL_PALETTE4_R5_G6_B5_OES
:
2240 case GL_PALETTE4_RGBA4_OES
:
2241 case GL_PALETTE4_RGB5_A1_OES
:
2242 case GL_PALETTE8_RGB8_OES
:
2243 case GL_PALETTE8_RGBA8_OES
:
2244 case GL_PALETTE8_R5_G6_B5_OES
:
2245 case GL_PALETTE8_RGBA4_OES
:
2246 case GL_PALETTE8_RGB5_A1_OES
:
2247 /* check level (note that level should be zero or less!) */
2248 if (level
> 0 || level
< -maxLevels
) {
2250 error
= GL_INVALID_VALUE
;
2254 if (dimensions
!= 2) {
2255 reason
= "compressed paletted textures must be 2D";
2256 error
= GL_INVALID_OPERATION
;
2260 /* Figure out the expected texture size (in bytes). This will be
2261 * checked against the actual size later.
2263 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
2266 /* This is for the benefit of the TestProxyTexImage below. It expects
2267 * level to be non-negative. OES_compressed_paletted_texture uses a
2268 * weird mechanism where the level specified to glCompressedTexImage2D
2269 * is -(n-1) number of levels in the texture, and the data specifies the
2270 * complete mipmap stack. This is done to ensure the palette is the
2271 * same for all levels.
2278 if (level
< 0 || level
>= maxLevels
) {
2280 error
= GL_INVALID_VALUE
;
2284 /* Figure out the expected texture size (in bytes). This will be
2285 * checked against the actual size later.
2287 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2291 /* This should really never fail */
2292 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2293 reason
= "internalFormat";
2294 error
= GL_INVALID_ENUM
;
2298 /* No compressed formats support borders at this time */
2300 reason
= "border != 0";
2301 error
= GL_INVALID_VALUE
;
2305 /* Check for invalid pixel storage modes */
2306 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2308 "glCompressedTexImage")) {
2312 /* check image size in bytes */
2313 if (expectedSize
!= imageSize
) {
2314 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2315 * if <imageSize> is not consistent with the format, dimensions, and
2316 * contents of the specified image.
2318 reason
= "imageSize inconsistant with width/height/format";
2319 error
= GL_INVALID_VALUE
;
2323 if (!mutable_tex_object(ctx
, target
)) {
2324 reason
= "immutable texture";
2325 error
= GL_INVALID_OPERATION
;
2332 /* Note: not all error paths exit through here. */
2333 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2334 dimensions
, reason
);
2341 * Test glTexSubImage[123]D() parameters for errors.
2343 * \param ctx GL context.
2344 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2345 * \param target texture target given by the user (already validated)
2346 * \param level image level given by the user.
2347 * \param xoffset sub-image x offset given by the user.
2348 * \param yoffset sub-image y offset given by the user.
2349 * \param zoffset sub-image z offset given by the user.
2350 * \param format pixel data format given by the user.
2351 * \param type pixel data type given by the user.
2352 * \param width image width given by the user.
2353 * \param height image height given by the user.
2354 * \param depth image depth given by the user.
2356 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2358 * Verifies each of the parameters against the constants specified in
2359 * __struct gl_contextRec::Const and the supported extensions, and according
2360 * to the OpenGL specification.
2363 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2364 struct gl_texture_object
*texObj
,
2365 GLenum target
, GLint level
,
2366 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2367 GLint width
, GLint height
, GLint depth
,
2368 GLenum format
, GLenum type
, bool dsa
)
2370 struct gl_texture_image
*texImage
;
2372 const char* suffix
= dsa
? "ture" : "";
2375 /* must be out of memory */
2376 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTex%sSubImage%dD()",
2377 suffix
, dimensions
);
2381 /* check target (proxies not allowed) */
2382 if (!legal_texsubimage_target(ctx
, dimensions
, target
, dsa
)) {
2383 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTex%sSubImage%uD(target=%s)",
2384 suffix
, dimensions
, _mesa_lookup_enum_by_nr(target
));
2389 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2390 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTex%sSubImage%uD(level=%d)",
2391 suffix
, dimensions
, level
);
2395 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2396 * combinations of format and type that can be used. Formats and types
2397 * that require additional extensions (e.g., GL_FLOAT requires
2398 * GL_OES_texture_float) are filtered elsewhere.
2400 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2401 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2402 if (err
!= GL_NO_ERROR
) {
2403 _mesa_error(ctx
, err
,
2404 "glTex%sSubImage%dD(format = %s, type = %s)",
2405 suffix
, dimensions
, _mesa_lookup_enum_by_nr(format
),
2406 _mesa_lookup_enum_by_nr(type
));
2411 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2412 if (err
!= GL_NO_ERROR
) {
2413 _mesa_error(ctx
, err
,
2414 "glTex%sSubImage%dD(incompatible format = %s, type = %s)",
2415 suffix
, dimensions
, _mesa_lookup_enum_by_nr(format
),
2416 _mesa_lookup_enum_by_nr(type
));
2420 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2422 /* non-existant texture level */
2423 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2424 "glTex%sSubImage%dD(invalid texture image)", suffix
,
2429 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2430 texImage
, xoffset
, yoffset
, 0,
2432 dsa
? "glTextureSubImage" :
2437 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2438 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2440 "glTex%sSubImage%dD(no compression for format)",
2441 suffix
, dimensions
);
2446 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2447 /* both source and dest must be integer-valued, or neither */
2448 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2449 _mesa_is_enum_format_integer(format
)) {
2450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2451 "glTex%sSubImage%dD(integer/non-integer format mismatch)",
2452 suffix
, dimensions
);
2462 * Test glCopyTexImage[12]D() parameters for errors.
2464 * \param ctx GL context.
2465 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2466 * \param target texture target given by the user.
2467 * \param level image level given by the user.
2468 * \param internalFormat internal format given by the user.
2469 * \param width image width given by the user.
2470 * \param height image height given by the user.
2471 * \param border texture border.
2473 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2475 * Verifies each of the parameters against the constants specified in
2476 * __struct gl_contextRec::Const and the supported extensions, and according
2477 * to the OpenGL specification.
2480 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2481 GLenum target
, GLint level
, GLint internalFormat
,
2482 GLint width
, GLint height
, GLint border
)
2485 GLint rb_base_format
;
2486 struct gl_renderbuffer
*rb
;
2487 GLenum rb_internal_format
;
2490 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2491 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2492 dimensions
, _mesa_lookup_enum_by_nr(target
));
2497 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2498 _mesa_error(ctx
, GL_INVALID_VALUE
,
2499 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2503 /* Check that the source buffer is complete */
2504 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2505 if (ctx
->ReadBuffer
->_Status
== 0) {
2506 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2508 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2509 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2510 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2514 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2515 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2516 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2522 if (border
< 0 || border
> 1 ||
2523 ((ctx
->API
!= API_OPENGL_COMPAT
||
2524 target
== GL_TEXTURE_RECTANGLE_NV
||
2525 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2526 _mesa_error(ctx
, GL_INVALID_VALUE
,
2527 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2531 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2533 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2534 "glCopyTexImage%dD(read buffer)", dimensions
);
2538 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2541 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2542 switch (internalFormat
) {
2547 case GL_LUMINANCE_ALPHA
:
2550 _mesa_error(ctx
, GL_INVALID_VALUE
,
2551 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2552 _mesa_lookup_enum_by_nr(internalFormat
));
2557 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2558 if (baseFormat
< 0) {
2559 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2560 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2561 _mesa_lookup_enum_by_nr(internalFormat
));
2565 rb_internal_format
= rb
->InternalFormat
;
2566 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2567 if (_mesa_is_color_format(internalFormat
)) {
2568 if (rb_base_format
< 0) {
2569 _mesa_error(ctx
, GL_INVALID_VALUE
,
2570 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2571 _mesa_lookup_enum_by_nr(internalFormat
));
2576 if (_mesa_is_gles(ctx
)) {
2578 if (_mesa_base_format_component_count(baseFormat
) >
2579 _mesa_base_format_component_count(rb_base_format
)) {
2582 if (baseFormat
== GL_DEPTH_COMPONENT
||
2583 baseFormat
== GL_DEPTH_STENCIL
||
2584 rb_base_format
== GL_DEPTH_COMPONENT
||
2585 rb_base_format
== GL_DEPTH_STENCIL
||
2586 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2587 baseFormat
== GL_ALPHA
) &&
2588 rb_base_format
!= GL_RGBA
) ||
2589 internalFormat
== GL_RGB9_E5
) {
2592 if (internalFormat
== GL_RGB9_E5
) {
2596 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2597 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2598 _mesa_lookup_enum_by_nr(internalFormat
));
2603 if (_mesa_is_gles3(ctx
)) {
2604 bool rb_is_srgb
= false;
2605 bool dst_is_srgb
= false;
2607 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2608 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2612 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2616 if (rb_is_srgb
!= dst_is_srgb
) {
2617 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2618 * OpenGLES 3.0.0 spec says:
2620 * "The error INVALID_OPERATION is also generated if the
2621 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2622 * framebuffer attachment corresponding to the read buffer
2623 * is LINEAR (see section 6.1.13) and internalformat is
2624 * one of the sRGB formats described in section 3.8.16, or
2625 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2626 * SRGB and internalformat is not one of the sRGB formats."
2628 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2629 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2633 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2634 * types for SNORM formats. Also, conversion to SNORM formats is not
2635 * allowed by Table 3.2 on Page 110.
2637 if(_mesa_is_enum_format_snorm(internalFormat
)) {
2638 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2639 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2640 _mesa_lookup_enum_by_nr(internalFormat
));
2645 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2646 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2647 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2651 /* From the EXT_texture_integer spec:
2653 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2654 * if the texture internalformat is an integer format and the read color
2655 * buffer is not an integer format, or if the internalformat is not an
2656 * integer format and the read color buffer is an integer format."
2658 if (_mesa_is_color_format(internalFormat
)) {
2659 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2660 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2661 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2662 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2663 if (is_int
|| is_rbint
) {
2664 if (is_int
!= is_rbint
) {
2665 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2666 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2668 } else if (_mesa_is_gles(ctx
) &&
2669 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2670 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2671 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2672 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2677 /* From page 138 of OpenGL ES 3.0 spec:
2678 * "The error INVALID_OPERATION is generated if floating-point RGBA
2679 * data is required; if signed integer RGBA data is required and the
2680 * format of the current color buffer is not signed integer; if
2681 * unsigned integer RGBA data is required and the format of the
2682 * current color buffer is not unsigned integer; or if fixed-point
2683 * RGBA data is required and the format of the current color buffer
2684 * is not fixed-point.
2686 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2687 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2688 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2691 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2692 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
)) {
2693 _mesa_error(ctx
, GL_INVALID_ENUM
,
2694 "glCopyTexImage%dD(target)", dimensions
);
2697 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2699 "glCopyTexImage%dD(no compression for format)", dimensions
);
2703 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2704 "glCopyTexImage%dD(border!=0)", dimensions
);
2709 if (!mutable_tex_object(ctx
, target
)) {
2710 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2711 "glCopyTexImage%dD(immutable texture)", dimensions
);
2715 /* if we get here, the parameters are OK */
2721 * Test glCopyTexSubImage[12]D() parameters for errors.
2722 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2725 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2726 const struct gl_texture_object
*texObj
,
2727 GLenum target
, GLint level
,
2728 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2729 GLint width
, GLint height
, bool dsa
)
2731 struct gl_texture_image
*texImage
;
2732 const char *suffix
= dsa
? "ture" : "";
2734 /* Check that the source buffer is complete */
2735 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2736 if (ctx
->ReadBuffer
->_Status
== 0) {
2737 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2739 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2740 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2741 "glCopyTex%sSubImage%dD(invalid readbuffer)",
2742 suffix
, dimensions
);
2746 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2747 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2748 "glCopyTex%sSubImage%dD(multisample FBO)", suffix
,
2754 /* check target (proxies not allowed) */
2755 if (!legal_texsubimage_target(ctx
, dimensions
, target
, dsa
)) {
2756 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTex%sSubImage%uD(target=%s)",
2758 _mesa_lookup_enum_by_nr(target
));
2763 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2764 _mesa_error(ctx
, GL_INVALID_VALUE
,
2765 "glCopyTex%sSubImage%dD(level=%d)", suffix
,
2770 /* Get dest image pointers */
2772 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTex%sSubImage%dD()",
2773 suffix
, dimensions
);
2777 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2779 /* destination image does not exist */
2780 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2781 "glCopyTex%sSubImage%dD(invalid texture image)",
2782 suffix
, dimensions
);
2786 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2787 xoffset
, yoffset
, zoffset
,
2788 width
, height
, 1, dsa
?
2789 "glCompressedTextureSubImage" :
2790 "glCompressedTexSubImage")) {
2794 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2795 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2796 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2797 "glCopyTex%sSubImage%dD(no compression for format)",
2798 suffix
, dimensions
);
2803 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2804 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTex%sSubImage2D", suffix
);
2808 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2809 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2810 "glCopyTex%sSubImage%dD(missing readbuffer, format=0x%x)",
2811 suffix
, dimensions
, texImage
->_BaseFormat
);
2815 /* From the EXT_texture_integer spec:
2817 * "INVALID_OPERATION is generated by CopyTexImage* and
2818 * CopyTexSubImage* if the texture internalformat is an integer format
2819 * and the read color buffer is not an integer format, or if the
2820 * internalformat is not an integer format and the read color buffer
2821 * is an integer format."
2823 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2824 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2826 if (_mesa_is_format_integer_color(rb
->Format
) !=
2827 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2828 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2829 "glCopyTex%sSubImage%dD(integer vs non-integer)",
2830 suffix
, dimensions
);
2835 /* if we get here, the parameters are OK */
2840 /** Callback info for walking over FBO hash table */
2843 struct gl_context
*ctx
;
2844 struct gl_texture_object
*texObj
;
2850 * Check render to texture callback. Called from _mesa_HashWalk().
2853 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2855 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2856 const struct cb_info
*info
= (struct cb_info
*) userData
;
2857 struct gl_context
*ctx
= info
->ctx
;
2858 const struct gl_texture_object
*texObj
= info
->texObj
;
2859 const GLuint level
= info
->level
, face
= info
->face
;
2861 /* If this is a user-created FBO */
2862 if (_mesa_is_user_fbo(fb
)) {
2864 /* check if any of the FBO's attachments point to 'texObj' */
2865 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2866 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2867 if (att
->Type
== GL_TEXTURE
&&
2868 att
->Texture
== texObj
&&
2869 att
->TextureLevel
== level
&&
2870 att
->CubeMapFace
== face
) {
2871 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2872 ASSERT(att
->Renderbuffer
->TexImage
);
2873 /* Mark fb status as indeterminate to force re-validation */
2882 * When a texture image is specified we have to check if it's bound to
2883 * any framebuffer objects (render to texture) in order to detect changes
2884 * in size or format since that effects FBO completeness.
2885 * Any FBOs rendering into the texture must be re-validated.
2888 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2889 struct gl_texture_object
*texObj
,
2890 GLuint face
, GLuint level
)
2892 /* Only check this texture if it's been marked as RenderToTexture */
2893 if (texObj
->_RenderToTexture
) {
2894 struct cb_info info
;
2896 info
.texObj
= texObj
;
2899 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2905 * If the texture object's GenerateMipmap flag is set and we've
2906 * changed the texture base level image, regenerate the rest of the
2907 * mipmap levels now.
2910 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2911 struct gl_texture_object
*texObj
, GLint level
)
2913 if (texObj
->GenerateMipmap
&&
2914 level
== texObj
->BaseLevel
&&
2915 level
< texObj
->MaxLevel
) {
2916 ASSERT(ctx
->Driver
.GenerateMipmap
);
2917 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2922 /** Debug helper: override the user-requested internal format */
2924 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2927 if (internalFormat
== GL_RGBA16F_ARB
||
2928 internalFormat
== GL_RGBA32F_ARB
) {
2929 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2932 else if (internalFormat
== GL_RGB16F_ARB
||
2933 internalFormat
== GL_RGB32F_ARB
) {
2934 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2937 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2938 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2939 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2940 return GL_LUMINANCE_ALPHA
;
2942 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2943 internalFormat
== GL_LUMINANCE32F_ARB
) {
2944 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2945 return GL_LUMINANCE
;
2947 else if (internalFormat
== GL_ALPHA16F_ARB
||
2948 internalFormat
== GL_ALPHA32F_ARB
) {
2949 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2953 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2954 internalFormat = GL_RGBA;
2958 return internalFormat
;
2961 return internalFormat
;
2967 * Choose the actual hardware format for a texture image.
2968 * Try to use the same format as the previous image level when possible.
2969 * Otherwise, ask the driver for the best format.
2970 * It's important to try to choose a consistant format for all levels
2971 * for efficient texture memory layout/allocation. In particular, this
2972 * comes up during automatic mipmap generation.
2975 _mesa_choose_texture_format(struct gl_context
*ctx
,
2976 struct gl_texture_object
*texObj
,
2977 GLenum target
, GLint level
,
2978 GLenum internalFormat
, GLenum format
, GLenum type
)
2982 /* see if we've already chosen a format for the previous level */
2984 struct gl_texture_image
*prevImage
=
2985 _mesa_select_tex_image(texObj
, target
, level
- 1);
2986 /* See if the prev level is defined and has an internal format which
2987 * matches the new internal format.
2990 prevImage
->Width
> 0 &&
2991 prevImage
->InternalFormat
== internalFormat
) {
2992 /* use the same format */
2993 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2994 return prevImage
->TexFormat
;
2998 /* If the application requested compression to an S3TC format but we don't
2999 * have the DXTn library, force a generic compressed format instead.
3001 if (internalFormat
!= format
&& format
!= GL_NONE
) {
3002 const GLenum before
= internalFormat
;
3004 switch (internalFormat
) {
3005 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
3006 if (!ctx
->Mesa_DXTn
)
3007 internalFormat
= GL_COMPRESSED_RGB
;
3009 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
3010 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
3011 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
3012 if (!ctx
->Mesa_DXTn
)
3013 internalFormat
= GL_COMPRESSED_RGBA
;
3019 if (before
!= internalFormat
) {
3021 "DXT compression requested (%s), "
3022 "but libtxc_dxtn library not installed. Using %s "
3024 _mesa_lookup_enum_by_nr(before
),
3025 _mesa_lookup_enum_by_nr(internalFormat
));
3029 /* choose format from scratch */
3030 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
3032 ASSERT(f
!= MESA_FORMAT_NONE
);
3038 * Adjust pixel unpack params and image dimensions to strip off the
3039 * one-pixel texture border.
3041 * Gallium and intel don't support texture borders. They've seldem been used
3042 * and seldom been implemented correctly anyway.
3044 * \param unpackNew returns the new pixel unpack parameters
3047 strip_texture_border(GLenum target
,
3048 GLint
*width
, GLint
*height
, GLint
*depth
,
3049 const struct gl_pixelstore_attrib
*unpack
,
3050 struct gl_pixelstore_attrib
*unpackNew
)
3056 *unpackNew
= *unpack
;
3058 if (unpackNew
->RowLength
== 0)
3059 unpackNew
->RowLength
= *width
;
3061 if (unpackNew
->ImageHeight
== 0)
3062 unpackNew
->ImageHeight
= *height
;
3064 assert(*width
>= 3);
3065 unpackNew
->SkipPixels
++; /* skip the border */
3066 *width
= *width
- 2; /* reduce the width by two border pixels */
3068 /* The min height of a texture with a border is 3 */
3069 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
3070 unpackNew
->SkipRows
++; /* skip the border */
3071 *height
= *height
- 2; /* reduce the height by two border pixels */
3075 target
!= GL_TEXTURE_2D_ARRAY
&&
3076 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
3077 unpackNew
->SkipImages
++; /* skip the border */
3078 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
3084 * Common code to implement all the glTexImage1D/2D/3D functions
3085 * as well as glCompressedTexImage1D/2D/3D.
3086 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3087 * \param format the user's image format (only used if !compressed)
3088 * \param type the user's image type (only used if !compressed)
3089 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
3092 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
3093 GLenum target
, GLint level
, GLint internalFormat
,
3094 GLsizei width
, GLsizei height
, GLsizei depth
,
3095 GLint border
, GLenum format
, GLenum type
,
3096 GLsizei imageSize
, const GLvoid
*pixels
)
3098 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
3099 struct gl_pixelstore_attrib unpack_no_border
;
3100 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
3101 struct gl_texture_object
*texObj
;
3102 mesa_format texFormat
;
3103 GLboolean dimensionsOK
, sizeOK
;
3105 FLUSH_VERTICES(ctx
, 0);
3107 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
3110 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3112 _mesa_lookup_enum_by_nr(target
), level
,
3113 _mesa_lookup_enum_by_nr(internalFormat
),
3114 width
, height
, depth
, border
, pixels
);
3117 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3119 _mesa_lookup_enum_by_nr(target
), level
,
3120 _mesa_lookup_enum_by_nr(internalFormat
),
3121 width
, height
, depth
, border
,
3122 _mesa_lookup_enum_by_nr(format
),
3123 _mesa_lookup_enum_by_nr(type
), pixels
);
3126 internalFormat
= override_internal_format(internalFormat
, width
, height
);
3128 /* target error checking */
3129 if (!legal_teximage_target(ctx
, dims
, target
)) {
3130 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
3131 func
, dims
, _mesa_lookup_enum_by_nr(target
));
3135 /* general error checking */
3137 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
3139 width
, height
, depth
,
3144 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3145 format
, type
, width
, height
, depth
, border
))
3149 /* Here we convert a cpal compressed image into a regular glTexImage2D
3150 * call by decompressing the texture. If we really want to support cpal
3151 * textures in any driver this would have to be changed.
3153 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
3154 switch (internalFormat
) {
3155 case GL_PALETTE4_RGB8_OES
:
3156 case GL_PALETTE4_RGBA8_OES
:
3157 case GL_PALETTE4_R5_G6_B5_OES
:
3158 case GL_PALETTE4_RGBA4_OES
:
3159 case GL_PALETTE4_RGB5_A1_OES
:
3160 case GL_PALETTE8_RGB8_OES
:
3161 case GL_PALETTE8_RGBA8_OES
:
3162 case GL_PALETTE8_R5_G6_B5_OES
:
3163 case GL_PALETTE8_RGBA4_OES
:
3164 case GL_PALETTE8_RGB5_A1_OES
:
3165 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3166 width
, height
, imageSize
, pixels
);
3171 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3175 /* For glCompressedTexImage() the driver has no choice about the
3176 * texture format since we'll never transcode the user's compressed
3177 * image data. The internalFormat was error checked earlier.
3179 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
3182 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3183 internalFormat
, format
, type
);
3186 assert(texFormat
!= MESA_FORMAT_NONE
);
3188 /* check that width, height, depth are legal for the mipmap level */
3189 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
3190 height
, depth
, border
);
3192 /* check that the texture won't take too much memory, etc */
3193 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3195 width
, height
, depth
, border
);
3197 if (_mesa_is_proxy_texture(target
)) {
3198 /* Proxy texture: just clear or set state depending on error checking */
3199 struct gl_texture_image
*texImage
=
3200 get_proxy_tex_image(ctx
, target
, level
);
3203 return; /* GL_OUT_OF_MEMORY already recorded */
3205 if (dimensionsOK
&& sizeOK
) {
3206 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3207 border
, internalFormat
, texFormat
);
3210 clear_teximage_fields(texImage
);
3214 /* non-proxy target */
3215 const GLuint face
= _mesa_tex_target_to_face(target
);
3216 struct gl_texture_image
*texImage
;
3218 if (!dimensionsOK
) {
3219 _mesa_error(ctx
, GL_INVALID_VALUE
,
3220 "glTexImage%uD(invalid width or height or depth)",
3226 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3227 "glTexImage%uD(image too large)", dims
);
3231 /* Allow a hardware driver to just strip out the border, to provide
3232 * reliable but slightly incorrect hardware rendering instead of
3233 * rarely-tested software fallback rendering.
3235 if (border
&& ctx
->Const
.StripTextureBorder
) {
3236 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3239 unpack
= &unpack_no_border
;
3242 if (ctx
->NewState
& _NEW_PIXEL
)
3243 _mesa_update_state(ctx
);
3245 _mesa_lock_texture(ctx
, texObj
);
3247 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3250 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3253 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3255 _mesa_init_teximage_fields(ctx
, texImage
,
3256 width
, height
, depth
,
3257 border
, internalFormat
, texFormat
);
3259 /* Give the texture to the driver. <pixels> may be null. */
3260 if (width
> 0 && height
> 0 && depth
> 0) {
3262 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3266 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3267 type
, pixels
, unpack
);
3271 check_gen_mipmap(ctx
, target
, texObj
, level
);
3273 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3275 _mesa_dirty_texobj(ctx
, texObj
);
3278 _mesa_unlock_texture(ctx
, texObj
);
3285 * Called from the API. Note that width includes the border.
3288 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3289 GLsizei width
, GLint border
, GLenum format
,
3290 GLenum type
, const GLvoid
*pixels
)
3292 GET_CURRENT_CONTEXT(ctx
);
3293 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3294 border
, format
, type
, 0, pixels
);
3299 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3300 GLsizei width
, GLsizei height
, GLint border
,
3301 GLenum format
, GLenum type
,
3302 const GLvoid
*pixels
)
3304 GET_CURRENT_CONTEXT(ctx
);
3305 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3306 border
, format
, type
, 0, pixels
);
3311 * Called by the API or display list executor.
3312 * Note that width and height include the border.
3315 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3316 GLsizei width
, GLsizei height
, GLsizei depth
,
3317 GLint border
, GLenum format
, GLenum type
,
3318 const GLvoid
*pixels
)
3320 GET_CURRENT_CONTEXT(ctx
);
3321 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3322 width
, height
, depth
,
3323 border
, format
, type
, 0, pixels
);
3328 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3329 GLsizei width
, GLsizei height
, GLsizei depth
,
3330 GLint border
, GLenum format
, GLenum type
,
3331 const GLvoid
*pixels
)
3333 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3334 depth
, border
, format
, type
, pixels
);
3339 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3341 struct gl_texture_object
*texObj
;
3342 struct gl_texture_image
*texImage
;
3344 GET_CURRENT_CONTEXT(ctx
);
3345 FLUSH_VERTICES(ctx
, 0);
3349 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3351 case GL_TEXTURE_EXTERNAL_OES
:
3353 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3356 valid_target
= false;
3360 if (!valid_target
) {
3361 _mesa_error(ctx
, GL_INVALID_ENUM
,
3362 "glEGLImageTargetTexture2D(target=%d)", target
);
3367 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3368 "glEGLImageTargetTexture2D(image=%p)", image
);
3372 if (ctx
->NewState
& _NEW_PIXEL
)
3373 _mesa_update_state(ctx
);
3375 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3376 _mesa_lock_texture(ctx
, texObj
);
3378 if (texObj
->Immutable
) {
3379 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3380 "glEGLImageTargetTexture2D(texture is immutable)");
3381 _mesa_unlock_texture(ctx
, texObj
);
3385 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3387 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3389 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3391 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3392 texObj
, texImage
, image
);
3394 _mesa_dirty_texobj(ctx
, texObj
);
3396 _mesa_unlock_texture(ctx
, texObj
);
3402 * Helper that implements the glTexSubImage1/2/3D()
3403 * and glTextureSubImage1/2/3D() functions.
3406 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3407 struct gl_texture_object
*texObj
,
3408 struct gl_texture_image
*texImage
,
3409 GLenum target
, GLint level
,
3410 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3411 GLsizei width
, GLsizei height
, GLsizei depth
,
3412 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3415 FLUSH_VERTICES(ctx
, 0);
3417 /* check target (proxies not allowed) */
3418 if (!legal_texsubimage_target(ctx
, dims
, target
, dsa
)) {
3419 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTex%sSubImage%uD(target=%s)",
3421 dims
, _mesa_lookup_enum_by_nr(target
));
3425 if (ctx
->NewState
& _NEW_PIXEL
)
3426 _mesa_update_state(ctx
);
3428 _mesa_lock_texture(ctx
, texObj
);
3430 if (width
> 0 && height
> 0 && depth
> 0) {
3431 /* If we have a border, offset=-1 is legal. Bias by border width. */
3434 if (target
!= GL_TEXTURE_2D_ARRAY
)
3435 zoffset
+= texImage
->Border
;
3438 if (target
!= GL_TEXTURE_1D_ARRAY
)
3439 yoffset
+= texImage
->Border
;
3442 xoffset
+= texImage
->Border
;
3445 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3446 xoffset
, yoffset
, zoffset
,
3447 width
, height
, depth
,
3448 format
, type
, pixels
, &ctx
->Unpack
);
3450 check_gen_mipmap(ctx
, target
, texObj
, level
);
3452 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3453 * the texel data, not the texture format, size, etc.
3457 _mesa_unlock_texture(ctx
, texObj
);
3461 * Implement all the glTexSubImage1/2/3D() functions.
3462 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3465 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3466 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3467 GLsizei width
, GLsizei height
, GLsizei depth
,
3468 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3470 struct gl_texture_object
*texObj
;
3471 struct gl_texture_image
*texImage
;
3472 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3473 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3474 xoffset
, yoffset
, zoffset
,
3475 width
, height
, depth
, format
, type
, false)) {
3476 return; /* error was detected */
3479 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3480 /* texsubimage_error_check ensures that texImage is not NULL */
3482 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3483 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3485 _mesa_lookup_enum_by_nr(target
), level
,
3486 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3487 _mesa_lookup_enum_by_nr(format
),
3488 _mesa_lookup_enum_by_nr(type
), pixels
);
3490 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3491 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3492 format
, type
, pixels
, false);
3497 * Implement all the glTextureSubImage1/2/3D() functions.
3498 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3501 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3502 GLuint texture
, GLint level
,
3503 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3504 GLsizei width
, GLsizei height
, GLsizei depth
,
3505 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3507 struct gl_texture_object
*texObj
;
3508 struct gl_texture_image
*texImage
;
3511 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3513 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3514 dims
, texture
, level
,
3515 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3516 _mesa_lookup_enum_by_nr(format
),
3517 _mesa_lookup_enum_by_nr(type
), pixels
);
3519 /* Get the texture object by Name. */
3520 texObj
= _mesa_lookup_texture(ctx
, texture
);
3522 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3527 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3528 xoffset
, yoffset
, zoffset
,
3529 width
, height
, depth
, format
, type
, true)) {
3530 return; /* error was detected */
3534 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3535 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3537 /* Error checking */
3538 if (texObj
->NumLayers
< 6) {
3539 /* Not enough image planes for a cube map. The spec does not say
3540 * what should happen in this case because the user has always
3541 * specified each cube face separately (using
3542 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
3543 * This is addressed in Khronos Bug 13223.
3545 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3546 "glTextureSubImage%uD(insufficient cube map storage)",
3550 for (i
= 0; i
< 6; ++i
) { /* For each face. */
3551 if (!texObj
->Image
[i
][level
]) {
3552 /* Not enough image planes for a cube map. The spec does not say
3553 * what should happen in this case because the user has always
3554 * specified each cube face separately (using
3555 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
3556 * This is addressed in Khronos Bug 13223.
3558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3559 "glTextureSubImage%uD(insufficient cube map storage)",
3565 /* Copy in each face. */
3566 for (i
= 0; i
< 6; ++i
) {
3567 texImage
= texObj
->Image
[i
][level
];
3568 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3569 level
, xoffset
, yoffset
, zoffset
,
3570 width
, height
, 1, format
,
3571 type
, pixels
, true);
3572 pixels
+= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3577 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3581 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3582 level
, xoffset
, yoffset
, zoffset
,
3583 width
, height
, depth
, format
,
3584 type
, pixels
, true);
3590 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3591 GLint xoffset
, GLsizei width
,
3592 GLenum format
, GLenum type
,
3593 const GLvoid
*pixels
)
3595 GET_CURRENT_CONTEXT(ctx
);
3596 texsubimage(ctx
, 1, target
, level
,
3599 format
, type
, pixels
);
3604 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3605 GLint xoffset
, GLint yoffset
,
3606 GLsizei width
, GLsizei height
,
3607 GLenum format
, GLenum type
,
3608 const GLvoid
*pixels
)
3610 GET_CURRENT_CONTEXT(ctx
);
3611 texsubimage(ctx
, 2, target
, level
,
3612 xoffset
, yoffset
, 0,
3614 format
, type
, pixels
);
3620 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3621 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3622 GLsizei width
, GLsizei height
, GLsizei depth
,
3623 GLenum format
, GLenum type
,
3624 const GLvoid
*pixels
)
3626 GET_CURRENT_CONTEXT(ctx
);
3627 texsubimage(ctx
, 3, target
, level
,
3628 xoffset
, yoffset
, zoffset
,
3629 width
, height
, depth
,
3630 format
, type
, pixels
);
3634 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3635 GLint xoffset
, GLsizei width
,
3636 GLenum format
, GLenum type
,
3637 const GLvoid
*pixels
)
3639 GET_CURRENT_CONTEXT(ctx
);
3640 texturesubimage(ctx
, 1, texture
, level
,
3643 format
, type
, pixels
);
3648 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3649 GLint xoffset
, GLint yoffset
,
3650 GLsizei width
, GLsizei height
,
3651 GLenum format
, GLenum type
,
3652 const GLvoid
*pixels
)
3654 GET_CURRENT_CONTEXT(ctx
);
3655 texturesubimage(ctx
, 2, texture
, level
,
3656 xoffset
, yoffset
, 0,
3658 format
, type
, pixels
);
3663 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3664 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3665 GLsizei width
, GLsizei height
, GLsizei depth
,
3666 GLenum format
, GLenum type
,
3667 const GLvoid
*pixels
)
3669 GET_CURRENT_CONTEXT(ctx
);
3670 texturesubimage(ctx
, 3, texture
, level
,
3671 xoffset
, yoffset
, zoffset
,
3672 width
, height
, depth
,
3673 format
, type
, pixels
);
3678 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3679 * from. This depends on whether the texture contains color or depth values.
3681 static struct gl_renderbuffer
*
3682 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3684 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3685 /* reading from depth/stencil buffer */
3686 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3689 /* copying from color buffer */
3690 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3695 copytexsubimage_by_slice(struct gl_context
*ctx
,
3696 struct gl_texture_image
*texImage
,
3698 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3699 struct gl_renderbuffer
*rb
,
3701 GLsizei width
, GLsizei height
)
3703 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3706 /* For 1D arrays, we copy each scanline of the source rectangle into the
3709 assert(zoffset
== 0);
3711 for (slice
= 0; slice
< height
; slice
++) {
3712 assert(yoffset
+ slice
< texImage
->Height
);
3713 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3714 xoffset
, 0, yoffset
+ slice
,
3715 rb
, x
, y
+ slice
, width
, 1);
3718 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3719 xoffset
, yoffset
, zoffset
,
3720 rb
, x
, y
, width
, height
);
3725 formats_differ_in_component_sizes (mesa_format f1
,
3728 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3729 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3730 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3731 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3733 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3734 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3735 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3736 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3738 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3739 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3740 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3741 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3748 * Implement the glCopyTexImage1/2D() functions.
3751 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3752 GLenum target
, GLint level
, GLenum internalFormat
,
3753 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3755 struct gl_texture_object
*texObj
;
3756 struct gl_texture_image
*texImage
;
3757 const GLuint face
= _mesa_tex_target_to_face(target
);
3758 mesa_format texFormat
;
3759 struct gl_renderbuffer
*rb
;
3761 FLUSH_VERTICES(ctx
, 0);
3763 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3764 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3766 _mesa_lookup_enum_by_nr(target
), level
,
3767 _mesa_lookup_enum_by_nr(internalFormat
),
3768 x
, y
, width
, height
, border
);
3770 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3771 _mesa_update_state(ctx
);
3773 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3774 width
, height
, border
))
3777 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3779 _mesa_error(ctx
, GL_INVALID_VALUE
,
3780 "glCopyTexImage%uD(invalid width or height)", dims
);
3784 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3787 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3788 internalFormat
, GL_NONE
, GL_NONE
);
3790 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3792 if (_mesa_is_gles3(ctx
)) {
3793 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3794 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3795 * OpenGL ES 3.0. Khronos bug# 9807.
3797 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3798 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3799 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer and"
3800 " writing to unsized internal format)", dims
);
3804 /* From Page 139 of OpenGL ES 3.0 spec:
3805 * "If internalformat is sized, the internal format of the new texel
3806 * array is internalformat, and this is also the new texel array’s
3807 * effective internal format. If the component sizes of internalformat
3808 * do not exactly match the corresponding component sizes of the source
3809 * buffer’s effective internal format, described below, an
3810 * INVALID_OPERATION error is generated. If internalformat is unsized,
3811 * the internal format of the new texel array is the effective internal
3812 * format of the source buffer, and this is also the new texel array’s
3813 * effective internal format.
3815 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3816 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3817 "glCopyTexImage%uD(componenet size changed in"
3818 " internal format)", dims
);
3823 assert(texFormat
!= MESA_FORMAT_NONE
);
3825 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3827 width
, height
, 1, border
)) {
3828 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3829 "glCopyTexImage%uD(image too large)", dims
);
3833 if (border
&& ctx
->Const
.StripTextureBorder
) {
3835 width
-= border
* 2;
3838 height
-= border
* 2;
3843 _mesa_lock_texture(ctx
, texObj
);
3845 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3848 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3851 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3853 /* Free old texture image */
3854 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3856 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3857 border
, internalFormat
, texFormat
);
3859 if (width
&& height
) {
3860 /* Allocate texture memory (no pixel data yet) */
3861 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3863 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3865 struct gl_renderbuffer
*srcRb
=
3866 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3868 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3870 srcRb
, srcX
, srcY
, width
, height
);
3873 check_gen_mipmap(ctx
, target
, texObj
, level
);
3876 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3878 _mesa_dirty_texobj(ctx
, texObj
);
3881 _mesa_unlock_texture(ctx
, texObj
);
3887 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3888 GLenum internalFormat
,
3890 GLsizei width
, GLint border
)
3892 GET_CURRENT_CONTEXT(ctx
);
3893 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3899 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3900 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3903 GET_CURRENT_CONTEXT(ctx
);
3904 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3905 x
, y
, width
, height
, border
);
3909 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3912 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3913 struct gl_texture_object
*texObj
,
3914 GLenum target
, GLint level
,
3915 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3917 GLsizei width
, GLsizei height
,
3920 struct gl_texture_image
*texImage
;
3922 FLUSH_VERTICES(ctx
, 0);
3924 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3925 _mesa_debug(ctx
, "glCopyTex%sSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3926 dsa
? "ture" : "", dims
,
3927 _mesa_lookup_enum_by_nr(target
),
3928 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3930 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3931 _mesa_update_state(ctx
);
3933 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3934 xoffset
, yoffset
, zoffset
,
3935 width
, height
, dsa
)) {
3939 _mesa_lock_texture(ctx
, texObj
);
3941 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3943 /* If we have a border, offset=-1 is legal. Bias by border width. */
3946 if (target
!= GL_TEXTURE_2D_ARRAY
)
3947 zoffset
+= texImage
->Border
;
3950 if (target
!= GL_TEXTURE_1D_ARRAY
)
3951 yoffset
+= texImage
->Border
;
3954 xoffset
+= texImage
->Border
;
3957 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3959 struct gl_renderbuffer
*srcRb
=
3960 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3962 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3963 xoffset
, yoffset
, zoffset
,
3964 srcRb
, x
, y
, width
, height
);
3966 check_gen_mipmap(ctx
, target
, texObj
, level
);
3968 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3969 * the texel data, not the texture format, size, etc.
3973 _mesa_unlock_texture(ctx
, texObj
);
3977 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3978 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3980 struct gl_texture_object
* texObj
;
3981 GET_CURRENT_CONTEXT(ctx
);
3983 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3987 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
3988 x
, y
, width
, 1, false);
3994 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3995 GLint xoffset
, GLint yoffset
,
3996 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3998 struct gl_texture_object
* texObj
;
3999 GET_CURRENT_CONTEXT(ctx
);
4001 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4005 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
4006 xoffset
, yoffset
, 0,
4007 x
, y
, width
, height
, false);
4013 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
4014 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4015 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4017 struct gl_texture_object
* texObj
;
4018 GET_CURRENT_CONTEXT(ctx
);
4020 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4024 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
4025 xoffset
, yoffset
, zoffset
,
4026 x
, y
, width
, height
, false);
4030 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
4031 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
4033 struct gl_texture_object
* texObj
;
4034 GET_CURRENT_CONTEXT(ctx
);
4036 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage1D");
4040 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
4041 xoffset
, 0, 0, x
, y
, width
, 1, true);
4045 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
4046 GLint xoffset
, GLint yoffset
,
4047 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4049 struct gl_texture_object
* texObj
;
4050 GET_CURRENT_CONTEXT(ctx
);
4052 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage2D");
4056 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
4057 xoffset
, yoffset
, 0,
4058 x
, y
, width
, height
, true);
4064 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
4065 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4066 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
4068 struct gl_texture_object
* texObj
;
4069 GET_CURRENT_CONTEXT(ctx
);
4071 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glCopyTextureSubImage3D");
4075 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
4076 xoffset
, yoffset
, zoffset
,
4077 x
, y
, width
, height
, true);
4081 check_clear_tex_image(struct gl_context
*ctx
,
4082 const char *function
,
4083 struct gl_texture_image
*texImage
,
4084 GLenum format
, GLenum type
,
4086 GLubyte
*clearValue
)
4088 struct gl_texture_object
*texObj
= texImage
->TexObject
;
4089 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
4090 GLenum internalFormat
= texImage
->InternalFormat
;
4093 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
4094 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4095 "%s(buffer texture)", function
);
4099 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
4100 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4101 "%s(compressed texture)", function
);
4105 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
4106 if (err
!= GL_NO_ERROR
) {
4107 _mesa_error(ctx
, err
,
4108 "%s(incompatible format = %s, type = %s)",
4110 _mesa_lookup_enum_by_nr(format
),
4111 _mesa_lookup_enum_by_nr(type
));
4115 /* make sure internal format and format basically agree */
4116 if (!texture_formats_agree(internalFormat
, format
)) {
4117 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4118 "%s(incompatible internalFormat = %s, format = %s)",
4120 _mesa_lookup_enum_by_nr(internalFormat
),
4121 _mesa_lookup_enum_by_nr(format
));
4125 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
4126 /* both source and dest must be integer-valued, or neither */
4127 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
4128 _mesa_is_enum_format_integer(format
)) {
4129 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4130 "%s(integer/non-integer format mismatch)",
4136 if (!_mesa_texstore(ctx
,
4138 texImage
->_BaseFormat
,
4139 texImage
->TexFormat
,
4140 0, /* dstRowStride */
4142 1, 1, 1, /* srcWidth/Height/Depth */
4144 data
? data
: zeroData
,
4145 &ctx
->DefaultPacking
)) {
4146 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
4153 static struct gl_texture_object
*
4154 get_tex_obj_for_clear(struct gl_context
*ctx
,
4155 const char *function
,
4158 struct gl_texture_object
*texObj
;
4161 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
4165 texObj
= _mesa_lookup_texture(ctx
, texture
);
4167 if (texObj
== NULL
) {
4168 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
4172 if (texObj
->Target
== 0) {
4173 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
4181 get_tex_images_for_clear(struct gl_context
*ctx
,
4182 const char *function
,
4183 struct gl_texture_object
*texObj
,
4185 struct gl_texture_image
**texImages
)
4190 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
4191 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4195 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4196 for (i
= 0; i
< MAX_FACES
; i
++) {
4197 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
4199 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
4200 if (texImages
[i
] == NULL
) {
4201 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4202 "%s(invalid level)", function
);
4210 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4212 if (texImages
[0] == NULL
) {
4213 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4221 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4222 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4223 GLsizei width
, GLsizei height
, GLsizei depth
,
4224 GLenum format
, GLenum type
, const void *data
)
4226 GET_CURRENT_CONTEXT(ctx
);
4227 struct gl_texture_object
*texObj
;
4228 struct gl_texture_image
*texImages
[MAX_FACES
];
4229 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4231 int minDepth
, maxDepth
;
4233 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4238 _mesa_lock_texture(ctx
, texObj
);
4240 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4241 texObj
, level
, texImages
);
4245 if (numImages
== 1) {
4246 minDepth
= -(int) texImages
[0]->Border
;
4247 maxDepth
= texImages
[0]->Depth
;
4250 maxDepth
= numImages
;
4253 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4254 yoffset
< -(GLint
) texImages
[0]->Border
||
4255 zoffset
< minDepth
||
4259 xoffset
+ width
> texImages
[0]->Width
||
4260 yoffset
+ height
> texImages
[0]->Height
||
4261 zoffset
+ depth
> maxDepth
) {
4262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4263 "glClearSubTexImage(invalid dimensions)");
4267 if (numImages
== 1) {
4268 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4270 format
, type
, data
, clearValue
[0])) {
4271 ctx
->Driver
.ClearTexSubImage(ctx
,
4273 xoffset
, yoffset
, zoffset
,
4274 width
, height
, depth
,
4275 data
? clearValue
[0] : NULL
);
4278 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4279 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4281 format
, type
, data
, clearValue
[i
]))
4284 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4285 ctx
->Driver
.ClearTexSubImage(ctx
,
4287 xoffset
, yoffset
, 0,
4289 data
? clearValue
[i
] : NULL
);
4294 _mesa_unlock_texture(ctx
, texObj
);
4298 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4299 GLenum format
, GLenum type
, const void *data
)
4301 GET_CURRENT_CONTEXT(ctx
);
4302 struct gl_texture_object
*texObj
;
4303 struct gl_texture_image
*texImages
[MAX_FACES
];
4304 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4307 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4312 _mesa_lock_texture(ctx
, texObj
);
4314 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4315 texObj
, level
, texImages
);
4317 for (i
= 0; i
< numImages
; i
++) {
4318 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4325 for (i
= 0; i
< numImages
; i
++) {
4326 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4327 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4328 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4329 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4330 texImages
[i
]->Width
,
4331 texImages
[i
]->Height
,
4332 texImages
[i
]->Depth
,
4333 data
? clearValue
[i
] : NULL
);
4337 _mesa_unlock_texture(ctx
, texObj
);
4343 /**********************************************************************/
4344 /****** Compressed Textures ******/
4345 /**********************************************************************/
4349 * Error checking for glCompressedTexSubImage[123]D().
4350 * \return GL_TRUE if error, GL_FALSE if no error
4353 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4354 const struct gl_texture_object
*texObj
,
4355 GLenum target
, GLint level
,
4356 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4357 GLsizei width
, GLsizei height
, GLsizei depth
,
4358 GLenum format
, GLsizei imageSize
, bool dsa
)
4360 struct gl_texture_image
*texImage
;
4363 const char *suffix
= dsa
? "ture" : "";
4365 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4366 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4367 "glCompressedSubTexture%dD(target)", dims
);
4375 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4376 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4377 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4378 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4379 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4380 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4384 targetOK
= GL_FALSE
;
4389 targetOK
= (target
== GL_TEXTURE_3D
) ||
4390 (target
== GL_TEXTURE_2D_ARRAY
) ||
4391 (target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
4392 (target
== GL_TEXTURE_CUBE_MAP
&& dsa
);
4394 /* OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4396 * "An INVALID_OPERATION error is generated by
4397 * CompressedTex*SubImage3D if the internal format of the texture is
4398 * one of the EAC, ETC2, or RGTC formats and either border is
4399 * non-zero, or the effective target for the texture is not
4400 * TEXTURE_2D_ARRAY."
4402 if (target
!= GL_TEXTURE_2D_ARRAY
) {
4405 /* These came from _mesa_is_compressed_format in glformats.c. */
4407 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
4408 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
4409 case GL_COMPRESSED_R11_EAC
:
4410 case GL_COMPRESSED_RG11_EAC
:
4411 case GL_COMPRESSED_SIGNED_R11_EAC
:
4412 case GL_COMPRESSED_SIGNED_RG11_EAC
:
4414 case GL_COMPRESSED_RGB8_ETC2
:
4415 case GL_COMPRESSED_SRGB8_ETC2
:
4416 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
4417 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
4419 case GL_COMPRESSED_RED_RGTC1
:
4420 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
4421 case GL_COMPRESSED_RG_RGTC2
:
4422 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
4423 invalidformat
= true;
4426 invalidformat
= false;
4428 if (invalidformat
) {
4429 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4430 "glCompressedTex%sSubImage%uD(target)", suffix
, dims
);
4438 /* no 1D compressed textures at this time */
4439 targetOK
= GL_FALSE
;
4444 _mesa_error(ctx
, GL_INVALID_ENUM
,
4445 "glCompressedTex%sSubImage%uD(target)", suffix
, dims
);
4449 /* this will catch any invalid compressed format token */
4450 if (!_mesa_is_compressed_format(ctx
, format
)) {
4451 _mesa_error(ctx
, GL_INVALID_ENUM
,
4452 "glCompressedTex%sSubImage%uD(format)", suffix
, dims
);
4456 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4457 _mesa_error(ctx
, GL_INVALID_VALUE
,
4458 "glCompressedTex%sSubImage%uD(level=%d)",
4459 suffix
, dims
, level
);
4463 /* Check for invalid pixel storage modes */
4464 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4466 dsa
? "glCompressedTextureSubImage" :
4467 "glCompressedTexSubImage")) {
4471 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4472 if (expectedSize
!= imageSize
) {
4473 _mesa_error(ctx
, GL_INVALID_VALUE
,
4474 "glCompressedTex%sSubImage%uD(size=%d)",
4475 suffix
, dims
, imageSize
);
4479 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4481 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4482 "glCompressedTex%sSubImage%uD(invalid texture image)",
4487 if ((GLint
) format
!= texImage
->InternalFormat
) {
4488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4489 "glCompressedTex%sSubImage%uD(format=0x%x)",
4490 suffix
, dims
, format
);
4494 if (compressedteximage_only_format(ctx
, format
)) {
4495 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4496 "glCompressedTex%sSubImage%uD(format=0x%x cannot be updated)",
4497 suffix
, dims
, format
);
4501 if (error_check_subtexture_dimensions(ctx
, dims
,
4502 texImage
, xoffset
, yoffset
, zoffset
,
4503 width
, height
, depth
,
4504 "glCompressedTexSubImage")) {
4513 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4514 GLenum internalFormat
, GLsizei width
,
4515 GLint border
, GLsizei imageSize
,
4518 GET_CURRENT_CONTEXT(ctx
);
4519 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4520 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4525 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4526 GLenum internalFormat
, GLsizei width
,
4527 GLsizei height
, GLint border
, GLsizei imageSize
,
4530 GET_CURRENT_CONTEXT(ctx
);
4531 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4532 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4537 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4538 GLenum internalFormat
, GLsizei width
,
4539 GLsizei height
, GLsizei depth
, GLint border
,
4540 GLsizei imageSize
, const GLvoid
*data
)
4542 GET_CURRENT_CONTEXT(ctx
);
4543 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4544 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4549 * Common helper for glCompressedTexSubImage1/2/3D() and
4550 * glCompressedTextureSubImage1/2/3D().
4553 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4554 struct gl_texture_object
*texObj
,
4555 GLenum target
, GLint level
,
4556 GLint xoffset
, GLint yoffset
,
4558 GLsizei width
, GLsizei height
,
4560 GLenum format
, GLsizei imageSize
,
4561 const GLvoid
*data
, bool dsa
)
4563 struct gl_texture_image
*texImage
;
4565 if (compressed_subtexture_error_check(ctx
, dims
, texObj
, target
,
4566 level
, xoffset
, yoffset
, zoffset
,
4567 width
, height
, depth
,
4568 format
, imageSize
, dsa
)) {
4572 FLUSH_VERTICES(ctx
, 0);
4574 _mesa_lock_texture(ctx
, texObj
);
4576 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4579 if (width
> 0 && height
> 0 && depth
> 0) {
4580 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4581 xoffset
, yoffset
, zoffset
,
4582 width
, height
, depth
,
4583 format
, imageSize
, data
);
4585 check_gen_mipmap(ctx
, target
, texObj
, level
);
4587 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4588 * the texel data, not the texture format, size, etc.
4592 _mesa_unlock_texture(ctx
, texObj
);
4597 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4598 GLsizei width
, GLenum format
,
4599 GLsizei imageSize
, const GLvoid
*data
)
4601 struct gl_texture_object
*texObj
;
4602 GET_CURRENT_CONTEXT(ctx
);
4604 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4608 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, target
, level
,
4609 xoffset
, 0, 0, width
, 1, 1,
4610 format
, imageSize
, data
, false);
4614 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4615 GLsizei width
, GLenum format
,
4616 GLsizei imageSize
, const GLvoid
*data
)
4618 struct gl_texture_object
*texObj
;
4619 GET_CURRENT_CONTEXT(ctx
);
4621 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4622 "glCompressedTextureSubImage1D");
4626 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
4627 xoffset
, 0, 0, width
, 1, 1,
4628 format
, imageSize
, data
, true);
4633 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4634 GLint yoffset
, GLsizei width
, GLsizei height
,
4635 GLenum format
, GLsizei imageSize
,
4638 struct gl_texture_object
*texObj
;
4639 GET_CURRENT_CONTEXT(ctx
);
4641 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4645 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, target
, level
,
4646 xoffset
, yoffset
, 0, width
, height
, 1,
4647 format
, imageSize
, data
, false);
4651 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4653 GLsizei width
, GLsizei height
,
4654 GLenum format
, GLsizei imageSize
,
4657 struct gl_texture_object
*texObj
;
4658 GET_CURRENT_CONTEXT(ctx
);
4660 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4661 "glCompressedTextureSubImage2D");
4665 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
4666 xoffset
, yoffset
, 0, width
, height
, 1,
4667 format
, imageSize
, data
, true);
4671 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4672 GLint yoffset
, GLint zoffset
, GLsizei width
,
4673 GLsizei height
, GLsizei depth
, GLenum format
,
4674 GLsizei imageSize
, const GLvoid
*data
)
4676 struct gl_texture_object
*texObj
;
4677 GET_CURRENT_CONTEXT(ctx
);
4679 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4683 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, target
, level
,
4684 xoffset
, yoffset
, zoffset
,
4685 width
, height
, depth
,
4686 format
, imageSize
, data
, false);
4690 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4691 GLint yoffset
, GLint zoffset
, GLsizei width
,
4692 GLsizei height
, GLsizei depth
,
4693 GLenum format
, GLsizei imageSize
,
4696 struct gl_texture_object
*texObj
;
4697 GET_CURRENT_CONTEXT(ctx
);
4699 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4700 "glCompressedTextureSubImage3D");
4702 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
4703 xoffset
, yoffset
, zoffset
,
4704 width
, height
, depth
,
4705 format
, imageSize
, data
, true);
4709 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4711 if (ctx
->API
!= API_OPENGL_CORE
) {
4712 switch (internalFormat
) {
4714 return MESA_FORMAT_A_UNORM8
;
4716 return MESA_FORMAT_A_UNORM16
;
4717 case GL_ALPHA16F_ARB
:
4718 return MESA_FORMAT_A_FLOAT16
;
4719 case GL_ALPHA32F_ARB
:
4720 return MESA_FORMAT_A_FLOAT32
;
4721 case GL_ALPHA8I_EXT
:
4722 return MESA_FORMAT_A_SINT8
;
4723 case GL_ALPHA16I_EXT
:
4724 return MESA_FORMAT_A_SINT16
;
4725 case GL_ALPHA32I_EXT
:
4726 return MESA_FORMAT_A_SINT32
;
4727 case GL_ALPHA8UI_EXT
:
4728 return MESA_FORMAT_A_UINT8
;
4729 case GL_ALPHA16UI_EXT
:
4730 return MESA_FORMAT_A_UINT16
;
4731 case GL_ALPHA32UI_EXT
:
4732 return MESA_FORMAT_A_UINT32
;
4734 return MESA_FORMAT_L_UNORM8
;
4735 case GL_LUMINANCE16
:
4736 return MESA_FORMAT_L_UNORM16
;
4737 case GL_LUMINANCE16F_ARB
:
4738 return MESA_FORMAT_L_FLOAT16
;
4739 case GL_LUMINANCE32F_ARB
:
4740 return MESA_FORMAT_L_FLOAT32
;
4741 case GL_LUMINANCE8I_EXT
:
4742 return MESA_FORMAT_L_SINT8
;
4743 case GL_LUMINANCE16I_EXT
:
4744 return MESA_FORMAT_L_SINT16
;
4745 case GL_LUMINANCE32I_EXT
:
4746 return MESA_FORMAT_L_SINT32
;
4747 case GL_LUMINANCE8UI_EXT
:
4748 return MESA_FORMAT_L_UINT8
;
4749 case GL_LUMINANCE16UI_EXT
:
4750 return MESA_FORMAT_L_UINT16
;
4751 case GL_LUMINANCE32UI_EXT
:
4752 return MESA_FORMAT_L_UINT32
;
4753 case GL_LUMINANCE8_ALPHA8
:
4754 return MESA_FORMAT_L8A8_UNORM
;
4755 case GL_LUMINANCE16_ALPHA16
:
4756 return MESA_FORMAT_L16A16_UNORM
;
4757 case GL_LUMINANCE_ALPHA16F_ARB
:
4758 return MESA_FORMAT_LA_FLOAT16
;
4759 case GL_LUMINANCE_ALPHA32F_ARB
:
4760 return MESA_FORMAT_LA_FLOAT32
;
4761 case GL_LUMINANCE_ALPHA8I_EXT
:
4762 return MESA_FORMAT_LA_SINT8
;
4763 case GL_LUMINANCE_ALPHA16I_EXT
:
4764 return MESA_FORMAT_LA_SINT8
;
4765 case GL_LUMINANCE_ALPHA32I_EXT
:
4766 return MESA_FORMAT_LA_SINT16
;
4767 case GL_LUMINANCE_ALPHA8UI_EXT
:
4768 return MESA_FORMAT_LA_UINT8
;
4769 case GL_LUMINANCE_ALPHA16UI_EXT
:
4770 return MESA_FORMAT_LA_UINT16
;
4771 case GL_LUMINANCE_ALPHA32UI_EXT
:
4772 return MESA_FORMAT_LA_UINT32
;
4774 return MESA_FORMAT_I_UNORM8
;
4775 case GL_INTENSITY16
:
4776 return MESA_FORMAT_I_UNORM16
;
4777 case GL_INTENSITY16F_ARB
:
4778 return MESA_FORMAT_I_FLOAT16
;
4779 case GL_INTENSITY32F_ARB
:
4780 return MESA_FORMAT_I_FLOAT32
;
4781 case GL_INTENSITY8I_EXT
:
4782 return MESA_FORMAT_I_SINT8
;
4783 case GL_INTENSITY16I_EXT
:
4784 return MESA_FORMAT_I_SINT16
;
4785 case GL_INTENSITY32I_EXT
:
4786 return MESA_FORMAT_I_SINT32
;
4787 case GL_INTENSITY8UI_EXT
:
4788 return MESA_FORMAT_I_UINT8
;
4789 case GL_INTENSITY16UI_EXT
:
4790 return MESA_FORMAT_I_UINT16
;
4791 case GL_INTENSITY32UI_EXT
:
4792 return MESA_FORMAT_I_UINT32
;
4798 if (ctx
->API
== API_OPENGL_CORE
&&
4799 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4800 switch (internalFormat
) {
4802 return MESA_FORMAT_RGB_FLOAT32
;
4804 return MESA_FORMAT_RGB_UINT32
;
4806 return MESA_FORMAT_RGB_SINT32
;
4812 switch (internalFormat
) {
4814 return MESA_FORMAT_R8G8B8A8_UNORM
;
4816 return MESA_FORMAT_RGBA_UNORM16
;
4817 case GL_RGBA16F_ARB
:
4818 return MESA_FORMAT_RGBA_FLOAT16
;
4819 case GL_RGBA32F_ARB
:
4820 return MESA_FORMAT_RGBA_FLOAT32
;
4822 return MESA_FORMAT_RGBA_SINT8
;
4823 case GL_RGBA16I_EXT
:
4824 return MESA_FORMAT_RGBA_SINT16
;
4825 case GL_RGBA32I_EXT
:
4826 return MESA_FORMAT_RGBA_SINT32
;
4827 case GL_RGBA8UI_EXT
:
4828 return MESA_FORMAT_RGBA_UINT8
;
4829 case GL_RGBA16UI_EXT
:
4830 return MESA_FORMAT_RGBA_UINT16
;
4831 case GL_RGBA32UI_EXT
:
4832 return MESA_FORMAT_RGBA_UINT32
;
4835 return MESA_FORMAT_R8G8_UNORM
;
4837 return MESA_FORMAT_R16G16_UNORM
;
4839 return MESA_FORMAT_RG_FLOAT16
;
4841 return MESA_FORMAT_RG_FLOAT32
;
4843 return MESA_FORMAT_RG_SINT8
;
4845 return MESA_FORMAT_RG_SINT16
;
4847 return MESA_FORMAT_RG_SINT32
;
4849 return MESA_FORMAT_RG_UINT8
;
4851 return MESA_FORMAT_RG_UINT16
;
4853 return MESA_FORMAT_RG_UINT32
;
4856 return MESA_FORMAT_R_UNORM8
;
4858 return MESA_FORMAT_R_UNORM16
;
4860 return MESA_FORMAT_R_FLOAT16
;
4862 return MESA_FORMAT_R_FLOAT32
;
4864 return MESA_FORMAT_R_SINT8
;
4866 return MESA_FORMAT_R_SINT16
;
4868 return MESA_FORMAT_R_SINT32
;
4870 return MESA_FORMAT_R_UINT8
;
4872 return MESA_FORMAT_R_UINT16
;
4874 return MESA_FORMAT_R_UINT32
;
4877 return MESA_FORMAT_NONE
;
4883 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4884 GLenum internalFormat
)
4886 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4889 if (format
== MESA_FORMAT_NONE
)
4890 return MESA_FORMAT_NONE
;
4892 datatype
= _mesa_get_format_datatype(format
);
4894 /* The GL_ARB_texture_buffer_object spec says:
4896 * "If ARB_texture_float is not supported, references to the
4897 * floating-point internal formats provided by that extension should be
4898 * removed, and such formats may not be passed to TexBufferARB."
4900 * As a result, GL_HALF_FLOAT internal format depends on both
4901 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4903 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4904 !ctx
->Extensions
.ARB_texture_float
)
4905 return MESA_FORMAT_NONE
;
4907 if (!ctx
->Extensions
.ARB_texture_rg
) {
4908 GLenum base_format
= _mesa_get_format_base_format(format
);
4909 if (base_format
== GL_R
|| base_format
== GL_RG
)
4910 return MESA_FORMAT_NONE
;
4913 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4914 GLenum base_format
= _mesa_get_format_base_format(format
);
4915 if (base_format
== GL_RGB
)
4916 return MESA_FORMAT_NONE
;
4923 texbufferrange(struct gl_context
*ctx
, GLenum target
, GLenum internalFormat
,
4924 struct gl_buffer_object
*bufObj
,
4925 GLintptr offset
, GLsizeiptr size
)
4927 struct gl_texture_object
*texObj
;
4930 FLUSH_VERTICES(ctx
, 0);
4932 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4933 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
4937 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4938 if (format
== MESA_FORMAT_NONE
) {
4939 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(internalFormat 0x%x)",
4944 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4946 _mesa_lock_texture(ctx
, texObj
);
4948 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4949 texObj
->BufferObjectFormat
= internalFormat
;
4950 texObj
->_BufferObjectFormat
= format
;
4951 texObj
->BufferOffset
= offset
;
4952 texObj
->BufferSize
= size
;
4954 _mesa_unlock_texture(ctx
, texObj
);
4956 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
4959 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
4964 /** GL_ARB_texture_buffer_object */
4966 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
4968 struct gl_buffer_object
*bufObj
;
4970 GET_CURRENT_CONTEXT(ctx
);
4972 /* NOTE: ARB_texture_buffer_object has interactions with
4973 * the compatibility profile that are not implemented.
4975 if (!(ctx
->API
== API_OPENGL_CORE
&&
4976 ctx
->Extensions
.ARB_texture_buffer_object
)) {
4977 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
4981 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4982 if (!bufObj
&& buffer
) {
4983 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
4987 texbufferrange(ctx
, target
, internalFormat
, bufObj
, 0, buffer
? -1 : 0);
4991 /** GL_ARB_texture_buffer_range */
4993 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
4994 GLintptr offset
, GLsizeiptr size
)
4996 struct gl_buffer_object
*bufObj
;
4998 GET_CURRENT_CONTEXT(ctx
);
5000 if (!(ctx
->API
== API_OPENGL_CORE
&&
5001 ctx
->Extensions
.ARB_texture_buffer_range
)) {
5002 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange");
5006 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
5010 (offset
+ size
) > bufObj
->Size
) {
5011 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexBufferRange");
5014 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
5015 _mesa_error(ctx
, GL_INVALID_VALUE
,
5016 "glTexBufferRange(invalid offset alignment)");
5019 } else if (buffer
) {
5020 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange(buffer %u)",
5028 texbufferrange(ctx
, target
, internalFormat
, bufObj
, offset
, size
);
5033 is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5035 /* Everything that is allowed for renderbuffers,
5036 * except for a base format of GL_STENCIL_INDEX.
5038 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5039 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5043 /** GL_ARB_texture_multisample */
5045 check_multisample_target(GLuint dims
, GLenum target
)
5048 case GL_TEXTURE_2D_MULTISAMPLE
:
5049 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5052 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5053 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5063 teximagemultisample(GLuint dims
, GLenum target
, GLsizei samples
,
5064 GLint internalformat
, GLsizei width
, GLsizei height
,
5065 GLsizei depth
, GLboolean fixedsamplelocations
,
5066 GLboolean immutable
, const char *func
)
5068 struct gl_texture_object
*texObj
;
5069 struct gl_texture_image
*texImage
;
5070 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5071 mesa_format texFormat
;
5072 GLenum sample_count_error
;
5074 GET_CURRENT_CONTEXT(ctx
);
5076 if (!(ctx
->Extensions
.ARB_texture_multisample
5077 && _mesa_is_desktop_gl(ctx
))) {
5078 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5082 if (!check_multisample_target(dims
, target
)) {
5083 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5087 /* check that the specified internalformat is color/depth/stencil-renderable;
5088 * refer GL3.1 spec 4.4.4
5091 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5092 _mesa_error(ctx
, GL_INVALID_ENUM
,
5093 "%s(internalformat=%s not legal for immutable-format)",
5094 func
, _mesa_lookup_enum_by_nr(internalformat
));
5098 if (!is_renderable_texture_format(ctx
, internalformat
)) {
5099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5100 "%s(internalformat=%s)",
5101 func
, _mesa_lookup_enum_by_nr(internalformat
));
5105 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5106 internalformat
, samples
);
5107 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5109 /* Page 254 of OpenGL 4.4 spec says:
5110 * "Proxy arrays for two-dimensional multisample and two-dimensional
5111 * multisample array textures are operated on in the same way when
5112 * TexImage2DMultisample is called with target specified as
5113 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5114 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5115 * However, if samples is not supported, then no error is generated.
5117 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5118 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5122 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5124 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5125 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5126 "%s(texture object 0)",
5131 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5133 if (texImage
== NULL
) {
5134 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5138 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5139 internalformat
, GL_NONE
, GL_NONE
);
5140 assert(texFormat
!= MESA_FORMAT_NONE
);
5142 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5143 width
, height
, depth
, 0);
5145 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5146 width
, height
, depth
, 0);
5148 if (_mesa_is_proxy_texture(target
)) {
5149 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5150 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5151 internalformat
, texFormat
,
5152 samples
, fixedsamplelocations
);
5155 /* clear all image fields */
5156 clear_teximage_fields(texImage
);
5160 if (!dimensionsOK
) {
5161 _mesa_error(ctx
, GL_INVALID_VALUE
,
5162 "%s(invalid width or height)", func
);
5167 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
5168 "%s(texture too large)", func
);
5172 /* Check if texObj->Immutable is set */
5173 if (texObj
->Immutable
) {
5174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5178 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5180 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5181 internalformat
, texFormat
,
5182 samples
, fixedsamplelocations
);
5184 if (width
> 0 && height
> 0 && depth
> 0) {
5185 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5186 width
, height
, depth
)) {
5187 /* tidy up the texture image state. strictly speaking,
5188 * we're allowed to just leave this in whatever state we
5189 * like, but being tidy is good.
5191 _mesa_init_teximage_fields(ctx
, texImage
,
5192 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5196 texObj
->Immutable
= immutable
;
5199 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5202 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5208 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5209 GLenum internalformat
, GLsizei width
,
5210 GLsizei height
, GLboolean fixedsamplelocations
)
5212 teximagemultisample(2, target
, samples
, internalformat
,
5213 width
, height
, 1, fixedsamplelocations
, GL_FALSE
,
5214 "glTexImage2DMultisample");
5219 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5220 GLenum internalformat
, GLsizei width
,
5221 GLsizei height
, GLsizei depth
,
5222 GLboolean fixedsamplelocations
)
5224 teximagemultisample(3, target
, samples
, internalformat
,
5225 width
, height
, depth
, fixedsamplelocations
, GL_FALSE
,
5226 "glTexImage3DMultisample");
5231 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5232 GLenum internalformat
, GLsizei width
,
5233 GLsizei height
, GLboolean fixedsamplelocations
)
5235 teximagemultisample(2, target
, samples
, internalformat
,
5236 width
, height
, 1, fixedsamplelocations
, GL_TRUE
,
5237 "glTexStorage2DMultisample");
5241 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5242 GLenum internalformat
, GLsizei width
,
5243 GLsizei height
, GLsizei depth
,
5244 GLboolean fixedsamplelocations
)
5246 teximagemultisample(3, target
, samples
, internalformat
,
5247 width
, height
, depth
, fixedsamplelocations
, GL_TRUE
,
5248 "glTexStorage3DMultisample");